blob: 8e2b8bd5f940d7d3086e8077b464efc9e78b1c6b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#ifndef CONFIG_NATIVE_WINDOWS
12
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080013#ifdef CONFIG_TESTING_OPTIONS
Hai Shalomfdcde762020-04-02 11:19:20 -070014#ifdef __NetBSD__
15#include <net/if_ether.h>
16#else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080017#include <net/ethernet.h>
Hai Shalomfdcde762020-04-02 11:19:20 -070018#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080019#include <netinet/ip.h>
20#endif /* CONFIG_TESTING_OPTIONS */
21
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <sys/un.h>
23#include <sys/stat.h>
24#include <stddef.h>
25
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080026#ifdef CONFIG_CTRL_IFACE_UDP
27#include <netdb.h>
28#endif /* CONFIG_CTRL_IFACE_UDP */
29
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "utils/common.h"
31#include "utils/eloop.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070032#include "utils/module_tests.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "common/version.h"
34#include "common/ieee802_11_defs.h"
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080035#include "common/ctrl_iface_common.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070036#ifdef CONFIG_DPP
37#include "common/dpp.h"
38#endif /* CONFIG_DPP */
39#include "common/wpa_ctrl.h"
Hai Shalom60840252021-02-19 19:02:11 -080040#include "common/ptksa_cache.h"
Sunil Ravi036cec52023-03-29 11:35:17 -070041#include "common/hw_features_common.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000042#include "common/nan_de.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080043#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080045#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080047#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080048#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049#include "ap/hostapd.h"
50#include "ap/ap_config.h"
51#include "ap/ieee802_1x.h"
52#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070053#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "ap/ieee802_11.h"
55#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056#include "ap/wps_hostapd.h"
57#include "ap/ctrl_iface_ap.h"
58#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080059#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070061#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080062#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070063#include "ap/neighbor_db.h"
64#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070065#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070066#include "ap/dfs.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000067#include "ap/nan_usd_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068#include "wps/wps_defs.h"
69#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080070#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072#include "ctrl_iface.h"
73
74
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080075#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
76
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080077#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080078#define HOSTAPD_CTRL_IFACE_PORT 8877
79#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
80#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
81#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
82#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083
84static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020085 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 const char *buf, size_t len);
87
88
89static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080090 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070091 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092{
Roshan Pius3a1667e2018-07-03 15:17:14 -070093 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094}
95
96
97static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080098 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 socklen_t fromlen)
100{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800101 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102}
103
104
105static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800106 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 socklen_t fromlen,
108 char *level)
109{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800110 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
115 const char *txtaddr)
116{
117 u8 addr[ETH_ALEN];
118 struct sta_info *sta;
119
120 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
121
122 if (hwaddr_aton(txtaddr, addr))
123 return -1;
124
125 sta = ap_get_sta(hapd, addr);
126 if (sta)
127 return 0;
128
129 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
130 "notification", MAC2STR(addr));
131 sta = ap_sta_add(hapd, addr);
132 if (sta == NULL)
133 return -1;
134
135 hostapd_new_assoc_sta(hapd, sta, 0);
136 return 0;
137}
138
139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140#ifdef NEED_AP_MLME
141static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
142 const char *txtaddr)
143{
144 u8 addr[ETH_ALEN];
145 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
146
147 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
148
149 if (hwaddr_aton(txtaddr, addr) ||
150 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
151 return -1;
152
153 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
154
155 return 0;
156}
157#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159
160#ifdef CONFIG_WPS
161static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
162{
163 char *pin = os_strchr(txt, ' ');
164 char *timeout_txt;
165 int timeout;
166 u8 addr_buf[ETH_ALEN], *addr = NULL;
167 char *pos;
168
169 if (pin == NULL)
170 return -1;
171 *pin++ = '\0';
172
173 timeout_txt = os_strchr(pin, ' ');
174 if (timeout_txt) {
175 *timeout_txt++ = '\0';
176 timeout = atoi(timeout_txt);
177 pos = os_strchr(timeout_txt, ' ');
178 if (pos) {
179 *pos++ = '\0';
180 if (hwaddr_aton(pos, addr_buf) == 0)
181 addr = addr_buf;
182 }
183 } else
184 timeout = 0;
185
186 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
187}
188
189
190static int hostapd_ctrl_iface_wps_check_pin(
191 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
192{
193 char pin[9];
194 size_t len;
195 char *pos;
196 int ret;
197
198 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
199 (u8 *) cmd, os_strlen(cmd));
200 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
201 if (*pos < '0' || *pos > '9')
202 continue;
203 pin[len++] = *pos;
204 if (len == 9) {
205 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
206 return -1;
207 }
208 }
209 if (len != 4 && len != 8) {
210 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
211 return -1;
212 }
213 pin[len] = '\0';
214
215 if (len == 8) {
216 unsigned int pin_val;
217 pin_val = atoi(pin);
218 if (!wps_pin_valid(pin_val)) {
219 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
220 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800221 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 return -1;
223 return ret;
224 }
225 }
226
227 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800228 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 return -1;
230
231 return ret;
232}
233
234
Dmitry Shmidt04949592012-07-19 12:16:46 -0700235#ifdef CONFIG_WPS_NFC
236static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
237 char *pos)
238{
239 size_t len;
240 struct wpabuf *buf;
241 int ret;
242
243 len = os_strlen(pos);
244 if (len & 0x01)
245 return -1;
246 len /= 2;
247
248 buf = wpabuf_alloc(len);
249 if (buf == NULL)
250 return -1;
251 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
252 wpabuf_free(buf);
253 return -1;
254 }
255
256 ret = hostapd_wps_nfc_tag_read(hapd, buf);
257 wpabuf_free(buf);
258
259 return ret;
260}
261
262
263static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
264 char *cmd, char *reply,
265 size_t max_len)
266{
267 int ndef;
268 struct wpabuf *buf;
269 int res;
270
271 if (os_strcmp(cmd, "WPS") == 0)
272 ndef = 0;
273 else if (os_strcmp(cmd, "NDEF") == 0)
274 ndef = 1;
275 else
276 return -1;
277
278 buf = hostapd_wps_nfc_config_token(hapd, ndef);
279 if (buf == NULL)
280 return -1;
281
282 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
283 wpabuf_len(buf));
284 reply[res++] = '\n';
285 reply[res] = '\0';
286
287 wpabuf_free(buf);
288
289 return res;
290}
291
292
293static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
294 char *reply, size_t max_len,
295 int ndef)
296{
297 struct wpabuf *buf;
298 int res;
299
300 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
301 if (buf == NULL)
302 return -1;
303
304 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305 wpabuf_len(buf));
306 reply[res++] = '\n';
307 reply[res] = '\0';
308
309 wpabuf_free(buf);
310
311 return res;
312}
313
314
315static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
316 char *cmd, char *reply,
317 size_t max_len)
318{
319 if (os_strcmp(cmd, "WPS") == 0)
320 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
321 max_len, 0);
322
323 if (os_strcmp(cmd, "NDEF") == 0)
324 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
325 max_len, 1);
326
327 if (os_strcmp(cmd, "enable") == 0)
328 return hostapd_wps_nfc_token_enable(hapd);
329
330 if (os_strcmp(cmd, "disable") == 0) {
331 hostapd_wps_nfc_token_disable(hapd);
332 return 0;
333 }
334
335 return -1;
336}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800337
338
339static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
340 char *cmd, char *reply,
341 size_t max_len)
342{
343 struct wpabuf *buf;
344 int res;
345 char *pos;
346 int ndef;
347
348 pos = os_strchr(cmd, ' ');
349 if (pos == NULL)
350 return -1;
351 *pos++ = '\0';
352
353 if (os_strcmp(cmd, "WPS") == 0)
354 ndef = 0;
355 else if (os_strcmp(cmd, "NDEF") == 0)
356 ndef = 1;
357 else
358 return -1;
359
360 if (os_strcmp(pos, "WPS-CR") == 0)
361 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
362 else
363 buf = NULL;
364 if (buf == NULL)
365 return -1;
366
367 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
368 wpabuf_len(buf));
369 reply[res++] = '\n';
370 reply[res] = '\0';
371
372 wpabuf_free(buf);
373
374 return res;
375}
376
377
378static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
379 char *cmd)
380{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800381 size_t len;
382 struct wpabuf *req, *sel;
383 int ret;
384 char *pos, *role, *type, *pos2;
385
386 role = cmd;
387 pos = os_strchr(role, ' ');
388 if (pos == NULL)
389 return -1;
390 *pos++ = '\0';
391
392 type = pos;
393 pos = os_strchr(type, ' ');
394 if (pos == NULL)
395 return -1;
396 *pos++ = '\0';
397
398 pos2 = os_strchr(pos, ' ');
399 if (pos2 == NULL)
400 return -1;
401 *pos2++ = '\0';
402
403 len = os_strlen(pos);
404 if (len & 0x01)
405 return -1;
406 len /= 2;
407
408 req = wpabuf_alloc(len);
409 if (req == NULL)
410 return -1;
411 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
412 wpabuf_free(req);
413 return -1;
414 }
415
416 len = os_strlen(pos2);
417 if (len & 0x01) {
418 wpabuf_free(req);
419 return -1;
420 }
421 len /= 2;
422
423 sel = wpabuf_alloc(len);
424 if (sel == NULL) {
425 wpabuf_free(req);
426 return -1;
427 }
428 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
429 wpabuf_free(req);
430 wpabuf_free(sel);
431 return -1;
432 }
433
434 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
435 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
436 } else {
437 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
438 "reported: role=%s type=%s", role, type);
439 ret = -1;
440 }
441 wpabuf_free(req);
442 wpabuf_free(sel);
443
444 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800445}
446
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447#endif /* CONFIG_WPS_NFC */
448
449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
451 char *buf, size_t buflen)
452{
453 int timeout = 300;
454 char *pos;
455 const char *pin_txt;
456
457 pos = os_strchr(txt, ' ');
458 if (pos)
459 *pos++ = '\0';
460
461 if (os_strcmp(txt, "disable") == 0) {
462 hostapd_wps_ap_pin_disable(hapd);
463 return os_snprintf(buf, buflen, "OK\n");
464 }
465
466 if (os_strcmp(txt, "random") == 0) {
467 if (pos)
468 timeout = atoi(pos);
469 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
470 if (pin_txt == NULL)
471 return -1;
472 return os_snprintf(buf, buflen, "%s", pin_txt);
473 }
474
475 if (os_strcmp(txt, "get") == 0) {
476 pin_txt = hostapd_wps_ap_pin_get(hapd);
477 if (pin_txt == NULL)
478 return -1;
479 return os_snprintf(buf, buflen, "%s", pin_txt);
480 }
481
482 if (os_strcmp(txt, "set") == 0) {
483 char *pin;
484 if (pos == NULL)
485 return -1;
486 pin = pos;
487 pos = os_strchr(pos, ' ');
488 if (pos) {
489 *pos++ = '\0';
490 timeout = atoi(pos);
491 }
492 if (os_strlen(pin) > buflen)
493 return -1;
494 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
495 return -1;
496 return os_snprintf(buf, buflen, "%s", pin);
497 }
498
499 return -1;
500}
501
502
503static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
504{
505 char *pos;
506 char *ssid, *auth, *encr = NULL, *key = NULL;
507
508 ssid = txt;
509 pos = os_strchr(txt, ' ');
510 if (!pos)
511 return -1;
512 *pos++ = '\0';
513
514 auth = pos;
515 pos = os_strchr(pos, ' ');
516 if (pos) {
517 *pos++ = '\0';
518 encr = pos;
519 pos = os_strchr(pos, ' ');
520 if (pos) {
521 *pos++ = '\0';
522 key = pos;
523 }
524 }
525
526 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
527}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700528
529
530static const char * pbc_status_str(enum pbc_status status)
531{
532 switch (status) {
533 case WPS_PBC_STATUS_DISABLE:
534 return "Disabled";
535 case WPS_PBC_STATUS_ACTIVE:
536 return "Active";
537 case WPS_PBC_STATUS_TIMEOUT:
538 return "Timed-out";
539 case WPS_PBC_STATUS_OVERLAP:
540 return "Overlap";
541 default:
542 return "Unknown";
543 }
544}
545
546
547static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
548 char *buf, size_t buflen)
549{
550 int ret;
551 char *pos, *end;
552
553 pos = buf;
554 end = buf + buflen;
555
556 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
557 pbc_status_str(hapd->wps_stats.pbc_status));
558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700560 return pos - buf;
561 pos += ret;
562
563 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
564 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
565 "Success":
566 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
567 "Failed" : "None")));
568
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800569 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700570 return pos - buf;
571 pos += ret;
572
573 /* If status == Failure - Add possible Reasons */
574 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
575 hapd->wps_stats.failure_reason > 0) {
576 ret = os_snprintf(pos, end - pos,
577 "Failure Reason: %s\n",
578 wps_ei_str(hapd->wps_stats.failure_reason));
579
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800580 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700581 return pos - buf;
582 pos += ret;
583 }
584
585 if (hapd->wps_stats.status) {
586 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
587 MAC2STR(hapd->wps_stats.peer_addr));
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700590 return pos - buf;
591 pos += ret;
592 }
593
594 return pos - buf;
595}
596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597#endif /* CONFIG_WPS */
598
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800599#ifdef CONFIG_HS20
600
601static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
602 const char *cmd)
603{
604 u8 addr[ETH_ALEN];
605 const char *url;
606
607 if (hwaddr_aton(cmd, addr))
608 return -1;
609 url = cmd + 17;
610 if (*url == '\0') {
611 url = NULL;
612 } else {
613 if (*url != ' ')
614 return -1;
615 url++;
616 if (*url == '\0')
617 url = NULL;
618 }
619
620 return hs20_send_wnm_notification(hapd, addr, 1, url);
621}
622
623
624static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
625 const char *cmd)
626{
627 u8 addr[ETH_ALEN];
628 int code, reauth_delay, ret;
629 const char *pos;
630 size_t url_len;
631 struct wpabuf *req;
632
633 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
634 if (hwaddr_aton(cmd, addr))
635 return -1;
636
637 pos = os_strchr(cmd, ' ');
638 if (pos == NULL)
639 return -1;
640 pos++;
641 code = atoi(pos);
642
643 pos = os_strchr(pos, ' ');
644 if (pos == NULL)
645 return -1;
646 pos++;
647 reauth_delay = atoi(pos);
648
649 url_len = 0;
650 pos = os_strchr(pos, ' ');
651 if (pos) {
652 pos++;
653 url_len = os_strlen(pos);
654 }
655
656 req = wpabuf_alloc(4 + url_len);
657 if (req == NULL)
658 return -1;
659 wpabuf_put_u8(req, code);
660 wpabuf_put_le16(req, reauth_delay);
661 wpabuf_put_u8(req, url_len);
662 if (pos)
663 wpabuf_put_data(req, pos, url_len);
664
665 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
666 " to indicate imminent deauthentication (code=%d "
667 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
668 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
669 wpabuf_free(req);
670 return ret;
671}
672
673#endif /* CONFIG_HS20 */
674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675
Dmitry Shmidt051af732013-10-22 13:52:46 -0700676#ifdef CONFIG_INTERWORKING
677
678static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
679 const char *cmd)
680{
681 u8 qos_map_set[16 + 2 * 21], count = 0;
682 const char *pos = cmd;
683 int val, ret;
684
685 for (;;) {
686 if (count == sizeof(qos_map_set)) {
687 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
688 return -1;
689 }
690
691 val = atoi(pos);
692 if (val < 0 || val > 255) {
693 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
694 return -1;
695 }
696
697 qos_map_set[count++] = val;
698 pos = os_strchr(pos, ',');
699 if (!pos)
700 break;
701 pos++;
702 }
703
704 if (count < 16 || count & 1) {
705 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
706 return -1;
707 }
708
709 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
710 if (ret) {
711 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
712 return -1;
713 }
714
715 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
716 hapd->conf->qos_map_set_len = count;
717
718 return 0;
719}
720
721
722static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
723 const char *cmd)
724{
725 u8 addr[ETH_ALEN];
726 struct sta_info *sta;
727 struct wpabuf *buf;
728 u8 *qos_map_set = hapd->conf->qos_map_set;
729 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
730 int ret;
731
732 if (!qos_map_set_len) {
733 wpa_printf(MSG_INFO, "QoS Map Set is not set");
734 return -1;
735 }
736
737 if (hwaddr_aton(cmd, addr))
738 return -1;
739
740 sta = ap_get_sta(hapd, addr);
741 if (sta == NULL) {
742 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
743 "for QoS Map Configuration message",
744 MAC2STR(addr));
745 return -1;
746 }
747
748 if (!sta->qos_map_enabled) {
749 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
750 "support for QoS Map", MAC2STR(addr));
751 return -1;
752 }
753
754 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
755 if (buf == NULL)
756 return -1;
757
758 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
759 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
760
761 /* QoS Map Set Element */
762 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
763 wpabuf_put_u8(buf, qos_map_set_len);
764 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
765
766 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
767 wpabuf_head(buf), wpabuf_len(buf));
768 wpabuf_free(buf);
769
770 return ret;
771}
772
773#endif /* CONFIG_INTERWORKING */
774
775
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700776#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800777
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800778static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
779 const char *cmd)
780{
781 u8 addr[ETH_ALEN];
782 struct sta_info *sta;
783 const char *pos;
784 unsigned int auto_report, timeout;
785
786 if (hwaddr_aton(cmd, addr)) {
787 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
788 return -1;
789 }
790
791 sta = ap_get_sta(hapd, addr);
792 if (!sta) {
793 wpa_printf(MSG_DEBUG, "Station " MACSTR
794 " not found for Collocated Interference Request",
795 MAC2STR(addr));
796 return -1;
797 }
798
799 pos = cmd + 17;
800 if (*pos != ' ')
801 return -1;
802 pos++;
803 auto_report = atoi(pos);
804 pos = os_strchr(pos, ' ');
805 if (!pos)
806 return -1;
807 pos++;
808 timeout = atoi(pos);
809
810 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
811}
812
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700813#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800814
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800816static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
817 char *buf, size_t buflen)
818{
819 int ret = 0;
820 char *pos, *end;
821
822 pos = buf;
823 end = buf + buflen;
824
825 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
826
827 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
828 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
829 if (os_snprintf_error(end - pos, ret))
830 return pos - buf;
831 pos += ret;
832 }
833 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
834 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
835 if (os_snprintf_error(end - pos, ret))
836 return pos - buf;
837 pos += ret;
838 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800839#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800840 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
841 ret = os_snprintf(pos, end - pos, "FT-PSK ");
842 if (os_snprintf_error(end - pos, ret))
843 return pos - buf;
844 pos += ret;
845 }
846 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
847 ret = os_snprintf(pos, end - pos, "FT-EAP ");
848 if (os_snprintf_error(end - pos, ret))
849 return pos - buf;
850 pos += ret;
851 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700852#ifdef CONFIG_SHA384
853 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
854 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
855 if (os_snprintf_error(end - pos, ret))
856 return pos - buf;
857 pos += ret;
858 }
859#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800860#ifdef CONFIG_SAE
861 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
862 ret = os_snprintf(pos, end - pos, "FT-SAE ");
863 if (os_snprintf_error(end - pos, ret))
864 return pos - buf;
865 pos += ret;
866 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700867 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
868 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
869 if (os_snprintf_error(end - pos, ret))
870 return pos - buf;
871 pos += ret;
872 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800874#ifdef CONFIG_FILS
875 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
876 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
877 if (os_snprintf_error(end - pos, ret))
878 return pos - buf;
879 pos += ret;
880 }
881 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
882 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
883 if (os_snprintf_error(end - pos, ret))
884 return pos - buf;
885 pos += ret;
886 }
887#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800888#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800889 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
890 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
891 if (os_snprintf_error(end - pos, ret))
892 return pos - buf;
893 pos += ret;
894 }
895 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
896 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
897 if (os_snprintf_error(end - pos, ret))
898 return pos - buf;
899 pos += ret;
900 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901#ifdef CONFIG_SAE
902 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
903 ret = os_snprintf(pos, end - pos, "SAE ");
904 if (os_snprintf_error(end - pos, ret))
905 return pos - buf;
906 pos += ret;
907 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700908 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
909 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
910 if (os_snprintf_error(end - pos, ret))
911 return pos - buf;
912 pos += ret;
913 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800914#endif /* CONFIG_SAE */
915 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
916 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
917 if (os_snprintf_error(end - pos, ret))
918 return pos - buf;
919 pos += ret;
920 }
921 if (hapd->conf->wpa_key_mgmt &
922 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
923 ret = os_snprintf(pos, end - pos,
924 "WPA-EAP-SUITE-B-192 ");
925 if (os_snprintf_error(end - pos, ret))
926 return pos - buf;
927 pos += ret;
928 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800929#ifdef CONFIG_FILS
930 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
931 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
932 if (os_snprintf_error(end - pos, ret))
933 return pos - buf;
934 pos += ret;
935 }
936 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
937 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
938 if (os_snprintf_error(end - pos, ret))
939 return pos - buf;
940 pos += ret;
941 }
942#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800943
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700944#ifdef CONFIG_OWE
945 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
946 ret = os_snprintf(pos, end - pos, "OWE ");
947 if (os_snprintf_error(end - pos, ret))
948 return pos - buf;
949 pos += ret;
950 }
951#endif /* CONFIG_OWE */
952
953#ifdef CONFIG_DPP
954 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
955 ret = os_snprintf(pos, end - pos, "DPP ");
956 if (os_snprintf_error(end - pos, ret))
957 return pos - buf;
958 pos += ret;
959 }
960#endif /* CONFIG_DPP */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000961#ifdef CONFIG_SHA384
962 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
963 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA384 ");
964 if (os_snprintf_error(end - pos, ret))
965 return pos - buf;
966 pos += ret;
967 }
968#endif /* CONFIG_SHA384 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700969
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800970 if (pos > buf && *(pos - 1) == ' ') {
971 *(pos - 1) = '\0';
972 pos--;
973 }
974
975 return pos - buf;
976}
977
978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
980 char *buf, size_t buflen)
981{
982 int ret;
983 char *pos, *end;
984
985 pos = buf;
986 end = buf + buflen;
987
988 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
989 "ssid=%s\n",
990 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700991 wpa_ssid_txt(hapd->conf->ssid.ssid,
992 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800993 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 return pos - buf;
995 pos += ret;
996
Sunil Ravi77d572f2023-01-17 23:58:31 +0000997 if ((hapd->conf->config_id)) {
998 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
999 hapd->conf->config_id);
1000 if (os_snprintf_error(end - pos, ret))
1001 return pos - buf;
1002 pos += ret;
1003 }
1004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005#ifdef CONFIG_WPS
1006 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1007 hapd->conf->wps_state == 0 ? "disabled" :
1008 (hapd->conf->wps_state == 1 ? "not configured" :
1009 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001010 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 return pos - buf;
1012 pos += ret;
1013
1014 if (hapd->conf->wps_state && hapd->conf->wpa &&
1015 hapd->conf->ssid.wpa_passphrase) {
1016 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1017 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001018 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 return pos - buf;
1020 pos += ret;
1021 }
1022
1023 if (hapd->conf->wps_state && hapd->conf->wpa &&
1024 hapd->conf->ssid.wpa_psk &&
1025 hapd->conf->ssid.wpa_psk->group) {
1026 char hex[PMK_LEN * 2 + 1];
1027 wpa_snprintf_hex(hex, sizeof(hex),
1028 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1029 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001030 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 return pos - buf;
1032 pos += ret;
1033 }
Hai Shalom60840252021-02-19 19:02:11 -08001034
1035 if (hapd->conf->multi_ap) {
1036 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1037
1038 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1039 hapd->conf->multi_ap);
1040 if (os_snprintf_error(end - pos, ret))
1041 return pos - buf;
1042 pos += ret;
1043
1044 if (ssid->ssid_len) {
1045 ret = os_snprintf(pos, end - pos,
1046 "multi_ap_backhaul_ssid=%s\n",
1047 wpa_ssid_txt(ssid->ssid,
1048 ssid->ssid_len));
1049 if (os_snprintf_error(end - pos, ret))
1050 return pos - buf;
1051 pos += ret;
1052 }
1053
1054 if (hapd->conf->wps_state && hapd->conf->wpa &&
1055 ssid->wpa_passphrase) {
1056 ret = os_snprintf(pos, end - pos,
1057 "multi_ap_backhaul_wpa_passphrase=%s\n",
1058 ssid->wpa_passphrase);
1059 if (os_snprintf_error(end - pos, ret))
1060 return pos - buf;
1061 pos += ret;
1062 }
1063
1064 if (hapd->conf->wps_state && hapd->conf->wpa &&
1065 ssid->wpa_psk &&
1066 ssid->wpa_psk->group) {
1067 char hex[PMK_LEN * 2 + 1];
1068
1069 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1070 PMK_LEN);
1071 ret = os_snprintf(pos, end - pos,
1072 "multi_ap_backhaul_wpa_psk=%s\n",
1073 hex);
1074 forced_memzero(hex, sizeof(hex));
1075 if (os_snprintf_error(end - pos, ret))
1076 return pos - buf;
1077 pos += ret;
1078 }
1079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080#endif /* CONFIG_WPS */
1081
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001082 if (hapd->conf->wpa) {
1083 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1084 if (os_snprintf_error(end - pos, ret))
1085 return pos - buf;
1086 pos += ret;
1087 }
1088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1090 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 return pos - buf;
1093 pos += ret;
1094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001095 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096
1097 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001098 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return pos - buf;
1100 pos += ret;
1101 }
1102
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001103 if (hapd->conf->wpa) {
1104 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1105 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001106 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 return pos - buf;
1108 pos += ret;
1109 }
1110
1111 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1112 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001113 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 return pos - buf;
1115 pos += ret;
1116
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1118 " ");
1119 if (ret < 0)
1120 return pos - buf;
1121 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122
1123 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001124 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 return pos - buf;
1126 pos += ret;
1127 }
1128
1129 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1130 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001131 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 return pos - buf;
1133 pos += ret;
1134
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001135 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001136 " ");
1137 if (ret < 0)
1138 return pos - buf;
1139 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
1141 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001142 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return pos - buf;
1144 pos += ret;
1145 }
1146
Hai Shalomfdcde762020-04-02 11:19:20 -07001147 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1148 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1149 hapd->conf->wpa_deny_ptk0_rekey);
1150 if (os_snprintf_error(end - pos, ret))
1151 return pos - buf;
1152 pos += ret;
1153 }
1154
1155 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1156 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1157 hapd->conf->extended_key_id);
1158 if (os_snprintf_error(end - pos, ret))
1159 return pos - buf;
1160 pos += ret;
1161 }
1162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 return pos - buf;
1164}
1165
1166
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001167static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001168 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001169{
1170 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001171 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001172
Hai Shalom60840252021-02-19 19:02:11 -08001173 /*
1174 * For example:
1175 * SET setband 2G,6G
1176 * SET setband 5G
1177 * SET setband AUTO
1178 */
1179 if (!os_strstr(bands, "AUTO")) {
1180 if (os_strstr(bands, "5G"))
1181 setband_mask |= WPA_SETBAND_5G;
1182 if (os_strstr(bands, "6G"))
1183 setband_mask |= WPA_SETBAND_6G;
1184 if (os_strstr(bands, "2G"))
1185 setband_mask |= WPA_SETBAND_2G;
1186 if (setband_mask == WPA_SETBAND_AUTO)
1187 return -1;
1188 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001189
Hai Shalom60840252021-02-19 19:02:11 -08001190 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001191 os_memset(&event, 0, sizeof(event));
1192 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1193 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1194 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1195 }
1196
1197 return 0;
1198}
1199
1200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1202{
1203 char *value;
1204 int ret = 0;
1205
1206 value = os_strchr(cmd, ' ');
1207 if (value == NULL)
1208 return -1;
1209 *value++ = '\0';
1210
1211 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1212 if (0) {
1213#ifdef CONFIG_WPS_TESTING
1214 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1215 long int val;
1216 val = strtol(value, NULL, 0);
1217 if (val < 0 || val > 0xff) {
1218 ret = -1;
1219 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1220 "wps_version_number %ld", val);
1221 } else {
1222 wps_version_number = val;
1223 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1224 "version %u.%u",
1225 (wps_version_number & 0xf0) >> 4,
1226 wps_version_number & 0x0f);
1227 hostapd_wps_update_ie(hapd);
1228 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001229 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1230 wps_testing_stub_cred = atoi(value);
1231 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1232 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001233 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1234 wps_corrupt_pkhash = atoi(value);
1235 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1236 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001238#ifdef CONFIG_TESTING_OPTIONS
1239 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1240 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1242 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001243 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1244 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001245#ifdef CONFIG_DPP
1246 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1247 os_free(hapd->dpp_config_obj_override);
1248 hapd->dpp_config_obj_override = os_strdup(value);
1249 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1250 os_free(hapd->dpp_discovery_override);
1251 hapd->dpp_discovery_override = os_strdup(value);
1252 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1253 os_free(hapd->dpp_groups_override);
1254 hapd->dpp_groups_override = os_strdup(value);
1255 } else if (os_strcasecmp(cmd,
1256 "dpp_ignore_netaccesskey_mismatch") == 0) {
1257 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001258 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1259 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001260 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1261 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001262#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001263#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001264#ifdef CONFIG_MBO
1265 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1266 int val;
1267
1268 if (!hapd->conf->mbo_enabled)
1269 return -1;
1270
1271 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001272 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001273 return -1;
1274
1275 hapd->mbo_assoc_disallow = val;
1276 ieee802_11_update_beacons(hapd->iface);
1277
1278 /*
1279 * TODO: Need to configure drivers that do AP MLME offload with
1280 * disallowing station logic.
1281 */
1282#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001283#ifdef CONFIG_DPP
1284 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1285 os_free(hapd->dpp_configurator_params);
1286 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001287#ifdef CONFIG_DPP2
1288 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1289#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001290 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1291 hapd->dpp_init_max_tries = atoi(value);
1292 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1293 hapd->dpp_init_retry_time = atoi(value);
1294 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1295 hapd->dpp_resp_wait_time = atoi(value);
1296 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1297 hapd->dpp_resp_max_tries = atoi(value);
1298 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1299 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001300#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001301 } else if (os_strcasecmp(cmd, "setband") == 0) {
1302 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001305 if (ret)
1306 return ret;
1307
1308 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001309 hostapd_disassoc_deny_mac(hapd);
1310 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1311 hostapd_disassoc_accept_mac(hapd);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001312 } else if (os_strcasecmp(cmd, "ssid") == 0) {
1313 hostapd_neighbor_sync_own_report(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001314 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1315 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1316 hapd->parameter_set_count++;
1317 if (ieee802_11_update_beacons(hapd->iface))
1318 wpa_printf(MSG_DEBUG,
1319 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001320 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1321 os_strcmp(cmd, "sae_password") == 0 ||
1322 os_strcmp(cmd, "sae_pwe") == 0) {
1323 if (hapd->started)
1324 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001325 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1326 wpa_auth_set_transition_disable(hapd->wpa_auth,
1327 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001328 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001329
1330#ifdef CONFIG_TESTING_OPTIONS
1331 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1332 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1333 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001334 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1335 wpa_auth_set_ocv_override_freq(
1336 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1337 atoi(value));
1338 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1339 wpa_auth_set_ocv_override_freq(
1340 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1341 atoi(value));
1342 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1343 wpa_auth_set_ocv_override_freq(
1344 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1345 atoi(value));
1346 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1347 wpa_auth_set_ocv_override_freq(
1348 hapd->wpa_auth,
1349 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001350 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1351 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1352 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1353 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001354#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356
1357 return ret;
1358}
1359
1360
1361static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1362 char *buf, size_t buflen)
1363{
1364 int res;
1365
1366 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1367
1368 if (os_strcmp(cmd, "version") == 0) {
1369 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001370 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 return -1;
1372 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001373 } else if (os_strcmp(cmd, "tls_library") == 0) {
1374 res = tls_get_library_version(buf, buflen);
1375 if (os_snprintf_error(buflen, res))
1376 return -1;
1377 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 }
1379
1380 return -1;
1381}
1382
1383
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001384static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1385{
1386 if (hostapd_enable_iface(iface) < 0) {
1387 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1388 return -1;
1389 }
1390 return 0;
1391}
1392
1393
1394static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1395{
1396 if (hostapd_reload_iface(iface) < 0) {
1397 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1398 return -1;
1399 }
1400 return 0;
1401}
1402
1403
Sunil Ravi77d572f2023-01-17 23:58:31 +00001404static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1405{
1406 if (hostapd_reload_bss_only(bss) < 0) {
1407 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1408 return -1;
1409 }
1410 return 0;
1411}
1412
1413
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001414static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1415{
1416 if (hostapd_disable_iface(iface) < 0) {
1417 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1418 return -1;
1419 }
1420 return 0;
1421}
1422
1423
Hai Shalom74f70d42019-02-11 14:42:39 -08001424static int
1425hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1426 struct sta_info *sta, void *ctx)
1427{
1428 struct hostapd_wpa_psk *psk;
1429 const u8 *pmk;
1430 int pmk_len;
1431 int pmk_match;
1432 int sta_match;
1433 int bss_match;
1434 int reason;
1435
1436 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1437
1438 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1439 pmk_match = PMK_LEN == pmk_len &&
1440 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1441 sta_match = psk->group == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001442 ether_addr_equal(sta->addr, psk->addr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001443 bss_match = psk->group == 1;
1444
1445 if (pmk_match && (sta_match || bss_match))
1446 return 0;
1447 }
1448
1449 wpa_printf(MSG_INFO, "STA " MACSTR
1450 " PSK/passphrase no longer valid - disconnect",
1451 MAC2STR(sta->addr));
1452 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1453 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1454 ap_sta_deauthenticate(hapd, sta, reason);
1455
1456 return 0;
1457}
1458
1459
1460static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1461{
1462 struct hostapd_bss_config *conf = hapd->conf;
1463 int err;
1464
1465 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1466
1467 err = hostapd_setup_wpa_psk(conf);
1468 if (err < 0) {
1469 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1470 err);
1471 return -1;
1472 }
1473
1474 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1475 NULL);
1476
1477 return 0;
1478}
1479
1480
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001481#ifdef CONFIG_IEEE80211R_AP
1482
1483static int hostapd_ctrl_iface_get_rxkhs(struct hostapd_data *hapd,
1484 char *buf, size_t buflen)
1485{
1486 int ret, start_pos;
1487 char *pos, *end;
1488 struct ft_remote_r0kh *r0kh;
1489 struct ft_remote_r1kh *r1kh;
1490 struct hostapd_bss_config *conf = hapd->conf;
1491
1492 pos = buf;
1493 end = buf + buflen;
1494
1495 for (r0kh = conf->r0kh_list; r0kh; r0kh=r0kh->next) {
1496 start_pos = pos - buf;
1497 ret = os_snprintf(pos, end - pos, "r0kh=" MACSTR " ",
1498 MAC2STR(r0kh->addr));
1499 if (os_snprintf_error(end - pos, ret))
1500 return start_pos;
1501 pos += ret;
1502 if (r0kh->id_len + 1 >= (size_t) (end - pos))
1503 return start_pos;
1504 os_memcpy(pos, r0kh->id, r0kh->id_len);
1505 pos += r0kh->id_len;
1506 *pos++ = ' ';
1507 pos += wpa_snprintf_hex(pos, end - pos, r0kh->key,
1508 sizeof(r0kh->key));
1509 ret = os_snprintf(pos, end - pos, "\n");
1510 if (os_snprintf_error(end - pos, ret))
1511 return start_pos;
1512 pos += ret;
1513 }
1514
1515 for (r1kh = conf->r1kh_list; r1kh; r1kh=r1kh->next) {
1516 start_pos = pos - buf;
1517 ret = os_snprintf(pos, end - pos, "r1kh=" MACSTR " " MACSTR " ",
1518 MAC2STR(r1kh->addr), MAC2STR(r1kh->id));
1519 if (os_snprintf_error(end - pos, ret))
1520 return start_pos;
1521 pos += ret;
1522 pos += wpa_snprintf_hex(pos, end - pos, r1kh->key,
1523 sizeof(r1kh->key));
1524 ret = os_snprintf(pos, end - pos, "\n");
1525 if (os_snprintf_error(end - pos, ret))
1526 return start_pos;
1527 pos += ret;
1528 }
1529
1530 return pos - buf;
1531}
1532
1533
1534static int hostapd_ctrl_iface_reload_rxkhs(struct hostapd_data *hapd)
1535{
1536 struct hostapd_bss_config *conf = hapd->conf;
1537 int err;
1538
1539 hostapd_config_clear_rxkhs(conf);
1540
1541 err = hostapd_config_read_rxkh_file(conf, conf->rxkh_file);
1542 if (err < 0) {
1543 wpa_printf(MSG_ERROR, "Reloading RxKHs failed: %d",
1544 err);
1545 return -1;
1546 }
1547
1548 return 0;
1549}
1550
1551#endif /* CONFIG_IEEE80211R_AP */
1552
1553
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001554#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001555
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1557{
1558 union wpa_event_data data;
1559 char *pos, *param;
1560 enum wpa_event_type event;
1561
1562 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1563
1564 os_memset(&data, 0, sizeof(data));
1565
1566 param = os_strchr(cmd, ' ');
1567 if (param == NULL)
1568 return -1;
1569 *param++ = '\0';
1570
1571 if (os_strcmp(cmd, "DETECTED") == 0)
1572 event = EVENT_DFS_RADAR_DETECTED;
1573 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1574 event = EVENT_DFS_CAC_FINISHED;
1575 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1576 event = EVENT_DFS_CAC_ABORTED;
1577 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1578 event = EVENT_DFS_NOP_FINISHED;
1579 else {
1580 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1581 cmd);
1582 return -1;
1583 }
1584
1585 pos = os_strstr(param, "freq=");
1586 if (pos)
1587 data.dfs_event.freq = atoi(pos + 5);
1588
1589 pos = os_strstr(param, "ht_enabled=1");
1590 if (pos)
1591 data.dfs_event.ht_enabled = 1;
1592
1593 pos = os_strstr(param, "chan_offset=");
1594 if (pos)
1595 data.dfs_event.chan_offset = atoi(pos + 12);
1596
1597 pos = os_strstr(param, "chan_width=");
1598 if (pos)
1599 data.dfs_event.chan_width = atoi(pos + 11);
1600
1601 pos = os_strstr(param, "cf1=");
1602 if (pos)
1603 data.dfs_event.cf1 = atoi(pos + 4);
1604
1605 pos = os_strstr(param, "cf2=");
1606 if (pos)
1607 data.dfs_event.cf2 = atoi(pos + 4);
1608
1609 wpa_supplicant_event(hapd, event, &data);
1610
1611 return 0;
1612}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613
1614
1615static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1616{
1617 size_t len;
1618 u8 *buf;
1619 int res;
1620
1621 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1622
1623 len = os_strlen(cmd);
1624 if (len & 1)
1625 return -1;
1626 len /= 2;
1627
1628 buf = os_malloc(len);
1629 if (buf == NULL)
1630 return -1;
1631
1632 if (hexstr2bin(cmd, buf, len) < 0) {
1633 os_free(buf);
1634 return -1;
1635 }
1636
Hai Shalomfdcde762020-04-02 11:19:20 -07001637 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001638 os_free(buf);
1639 return res;
1640}
1641
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001643static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1644 char *cmd)
1645{
1646 char *pos, *param;
1647 size_t len;
1648 u8 *buf;
1649 int stype = 0, ok = 0;
1650 union wpa_event_data event;
1651
1652 if (!hapd->ext_mgmt_frame_handling)
1653 return -1;
1654
1655 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1656
1657 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1658
1659 pos = cmd;
1660 param = os_strstr(pos, "stype=");
1661 if (param) {
1662 param += 6;
1663 stype = atoi(param);
1664 }
1665
1666 param = os_strstr(pos, " ok=");
1667 if (param) {
1668 param += 4;
1669 ok = atoi(param);
1670 }
1671
1672 param = os_strstr(pos, " buf=");
1673 if (!param)
1674 return -1;
1675 param += 5;
1676
1677 len = os_strlen(param);
1678 if (len & 1)
1679 return -1;
1680 len /= 2;
1681
1682 buf = os_malloc(len);
1683 if (!buf || hexstr2bin(param, buf, len) < 0) {
1684 os_free(buf);
1685 return -1;
1686 }
1687
1688 os_memset(&event, 0, sizeof(event));
1689 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1690 event.tx_status.data = buf;
1691 event.tx_status.data_len = len;
1692 event.tx_status.stype = stype;
1693 event.tx_status.ack = ok;
1694 hapd->ext_mgmt_frame_handling = 0;
1695 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1696 hapd->ext_mgmt_frame_handling = 1;
1697
1698 os_free(buf);
1699
1700 return 0;
1701}
1702
1703
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001704static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1705 char *cmd)
1706{
1707 char *pos, *param;
1708 size_t len;
1709 u8 *buf;
1710 int freq = 0, datarate = 0, ssi_signal = 0;
1711 union wpa_event_data event;
1712
1713 if (!hapd->ext_mgmt_frame_handling)
1714 return -1;
1715
1716 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1717
1718 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1719
1720 pos = cmd;
1721 param = os_strstr(pos, "freq=");
1722 if (param) {
1723 param += 5;
1724 freq = atoi(param);
1725 }
1726
1727 param = os_strstr(pos, " datarate=");
1728 if (param) {
1729 param += 10;
1730 datarate = atoi(param);
1731 }
1732
1733 param = os_strstr(pos, " ssi_signal=");
1734 if (param) {
1735 param += 12;
1736 ssi_signal = atoi(param);
1737 }
1738
1739 param = os_strstr(pos, " frame=");
1740 if (param == NULL)
1741 return -1;
1742 param += 7;
1743
1744 len = os_strlen(param);
1745 if (len & 1)
1746 return -1;
1747 len /= 2;
1748
1749 buf = os_malloc(len);
1750 if (buf == NULL)
1751 return -1;
1752
1753 if (hexstr2bin(param, buf, len) < 0) {
1754 os_free(buf);
1755 return -1;
1756 }
1757
1758 os_memset(&event, 0, sizeof(event));
1759 event.rx_mgmt.freq = freq;
1760 event.rx_mgmt.frame = buf;
1761 event.rx_mgmt.frame_len = len;
1762 event.rx_mgmt.ssi_signal = ssi_signal;
1763 event.rx_mgmt.datarate = datarate;
1764 hapd->ext_mgmt_frame_handling = 0;
1765 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1766 hapd->ext_mgmt_frame_handling = 1;
1767
1768 os_free(buf);
1769
1770 return 0;
1771}
1772
1773
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001774static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1775{
1776 char *pos;
1777 u8 src[ETH_ALEN], *buf;
1778 int used;
1779 size_t len;
1780
1781 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1782
1783 pos = cmd;
1784 used = hwaddr_aton2(pos, src);
1785 if (used < 0)
1786 return -1;
1787 pos += used;
1788 while (*pos == ' ')
1789 pos++;
1790
1791 len = os_strlen(pos);
1792 if (len & 1)
1793 return -1;
1794 len /= 2;
1795
1796 buf = os_malloc(len);
1797 if (buf == NULL)
1798 return -1;
1799
1800 if (hexstr2bin(pos, buf, len) < 0) {
1801 os_free(buf);
1802 return -1;
1803 }
1804
Sunil8cd6f4d2022-06-28 18:40:46 +00001805 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806 os_free(buf);
1807
1808 return 0;
1809}
1810
1811
Hai Shaloma20dcd72022-02-04 13:43:00 -08001812static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1813{
1814 char *pos, *pos2;
1815 u8 dst[ETH_ALEN], *buf;
1816 int used, ret;
1817 size_t len;
1818 unsigned int prev;
1819 int encrypt = 0;
1820
1821 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1822
1823 pos = cmd;
1824 used = hwaddr_aton2(pos, dst);
1825 if (used < 0)
1826 return -1;
1827 pos += used;
1828 while (*pos == ' ')
1829 pos++;
1830
1831 pos2 = os_strchr(pos, ' ');
1832 if (pos2) {
1833 len = pos2 - pos;
1834 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1835 } else {
1836 len = os_strlen(pos);
1837 }
1838 if (len & 1)
1839 return -1;
1840 len /= 2;
1841
1842 buf = os_malloc(len);
1843 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1844 os_free(buf);
1845 return -1;
1846 }
1847
1848 prev = hapd->ext_eapol_frame_io;
1849 hapd->ext_eapol_frame_io = 0;
1850 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1851 hapd->ext_eapol_frame_io = prev;
1852 os_free(buf);
1853
1854 return ret;
1855}
1856
1857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1859{
1860 size_t i;
1861 u32 sum = 0;
1862 const u16 *pos = buf;
1863
1864 for (i = 0; i < len / 2; i++)
1865 sum += *pos++;
1866
1867 while (sum >> 16)
1868 sum = (sum & 0xffff) + (sum >> 16);
1869
1870 return sum ^ 0xffff;
1871}
1872
1873
1874#define HWSIM_PACKETLEN 1500
1875#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1876
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001877static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1878 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879{
1880 struct hostapd_data *hapd = ctx;
1881 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001882 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001883 const u8 *pos;
1884 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001885 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001886
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1888 wpa_printf(MSG_DEBUG,
1889 "test data: RX - ignore unexpected length %d",
1890 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001891 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893
1894 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001895 os_memcpy(&ip, eth + 1, sizeof(ip));
1896 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001897
Hai Shalomfdcde762020-04-02 11:19:20 -07001898 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1899 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001900 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001901 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001902 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001903 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904
Hai Shalomfdcde762020-04-02 11:19:20 -07001905 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001906 if (*pos != (u8) i) {
1907 wpa_printf(MSG_DEBUG,
1908 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001909 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001910 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001911 pos++;
1912 }
1913
Hai Shalom81f62d82019-07-22 12:10:00 -07001914 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001915 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1916 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001917 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1918 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919}
1920
1921
1922static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1923 char *cmd)
1924{
1925 int enabled = atoi(cmd);
1926 char *pos;
1927 const char *ifname;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001928 const u8 *addr = hapd->own_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001929
1930 if (!enabled) {
1931 if (hapd->l2_test) {
1932 l2_packet_deinit(hapd->l2_test);
1933 hapd->l2_test = NULL;
1934 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1935 "test data: Disabled");
1936 }
1937 return 0;
1938 }
1939
1940 if (hapd->l2_test)
1941 return 0;
1942
1943 pos = os_strstr(cmd, " ifname=");
1944 if (pos)
1945 ifname = pos + 8;
1946 else
1947 ifname = hapd->conf->iface;
1948
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001949#ifdef CONFIG_IEEE80211BE
1950 if (hapd->conf->mld_ap)
Sunil Ravi99c035e2024-07-12 01:42:03 +00001951 addr = hapd->mld->mld_addr;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001952#endif /* CONFIG_IEEE80211BE */
1953 hapd->l2_test = l2_packet_init(ifname, addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001954 ETHERTYPE_IP, hostapd_data_test_rx,
1955 hapd, 1);
1956 if (hapd->l2_test == NULL)
1957 return -1;
1958
1959 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1960
1961 return 0;
1962}
1963
1964
1965static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1966{
1967 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001968 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001969 int used;
1970 long int val;
1971 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001972 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001973 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001974 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001975 u8 *dpos;
1976 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001977 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001978
1979 if (hapd->l2_test == NULL)
1980 return -1;
1981
Hai Shalom81f62d82019-07-22 12:10:00 -07001982 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001983
1984 pos = cmd;
1985 used = hwaddr_aton2(pos, dst);
1986 if (used < 0)
1987 return -1;
1988 pos += used;
1989 while (*pos == ' ')
1990 pos++;
1991 used = hwaddr_aton2(pos, src);
1992 if (used < 0)
1993 return -1;
1994 pos += used;
1995
Hai Shalom81f62d82019-07-22 12:10:00 -07001996 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001997 if (val < 0 || val > 0xff)
1998 return -1;
1999 tos = val;
2000
Hai Shalom81f62d82019-07-22 12:10:00 -07002001 pos = os_strstr(pos2, " len=");
2002 if (pos) {
2003 i = atoi(pos + 5);
2004 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2005 return -1;
2006 send_len = i;
2007 }
2008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002009 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002010 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2011 os_memcpy(eth->ether_shost, src, ETH_ALEN);
2012 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07002013 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07002015 ip->ip_hl = 5;
2016 ip->ip_v = 4;
2017 ip->ip_ttl = 64;
2018 ip->ip_tos = tos;
2019 ip->ip_len = htons(send_len);
2020 ip->ip_p = 1;
2021 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2022 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2023 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07002025 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002026 *dpos++ = i;
2027
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002028 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07002029 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002030 return -1;
2031
2032 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2033 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2034
2035 return 0;
2036}
2037
2038
2039static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2040 char *cmd)
2041{
2042 u8 *buf;
2043 struct ether_header *eth;
2044 struct l2_packet_data *l2 = NULL;
2045 size_t len;
2046 u16 ethertype;
2047 int res = -1;
2048 const char *ifname = hapd->conf->iface;
2049
2050 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2051 cmd += 7;
2052 ifname = cmd;
2053 cmd = os_strchr(cmd, ' ');
2054 if (cmd == NULL)
2055 return -1;
2056 *cmd++ = '\0';
2057 }
2058
2059 len = os_strlen(cmd);
2060 if (len & 1 || len < ETH_HLEN * 2)
2061 return -1;
2062 len /= 2;
2063
2064 buf = os_malloc(len);
2065 if (buf == NULL)
2066 return -1;
2067
2068 if (hexstr2bin(cmd, buf, len) < 0)
2069 goto done;
2070
2071 eth = (struct ether_header *) buf;
2072 ethertype = ntohs(eth->ether_type);
2073
2074 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2075 hostapd_data_test_rx, hapd, 1);
2076 if (l2 == NULL)
2077 goto done;
2078
2079 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2080 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2081done:
2082 if (l2)
2083 l2_packet_deinit(l2);
2084 os_free(buf);
2085
2086 return res < 0 ? -1 : 0;
2087}
2088
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002089
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002090static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2091{
2092 struct sta_info *sta;
2093 u8 addr[ETH_ALEN];
2094 u8 zero[WPA_TK_MAX_LEN];
2095
2096 os_memset(zero, 0, sizeof(zero));
2097
2098 if (hwaddr_aton(cmd, addr))
2099 return -1;
2100
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002101 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2102 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2103 return -1;
2104
2105 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2106
2107 /* First, use a zero key to avoid any possible duplicate key
2108 * avoidance in the driver. */
2109 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2110 hapd->last_bigtk_alg,
2111 broadcast_ether_addr,
2112 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2113 zero, hapd->last_bigtk_len,
2114 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2115 return -1;
2116
2117 /* Set the previously configured key to reset its TSC */
2118 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2119 hapd->last_bigtk_alg,
2120 broadcast_ether_addr,
2121 hapd->last_bigtk_key_idx, 0, 1, NULL,
2122 0, hapd->last_bigtk,
2123 hapd->last_bigtk_len,
2124 KEY_FLAG_GROUP_TX_DEFAULT);
2125 }
2126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002127 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2128 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2129 return -1;
2130
2131 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2132
2133 /* First, use a zero key to avoid any possible duplicate key
2134 * avoidance in the driver. */
2135 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2136 hapd->last_igtk_alg,
2137 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002138 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2139 zero, hapd->last_igtk_len,
2140 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002141 return -1;
2142
2143 /* Set the previously configured key to reset its TSC */
2144 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2145 hapd->last_igtk_alg,
2146 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002147 hapd->last_igtk_key_idx, 0, 1, NULL,
2148 0, hapd->last_igtk,
2149 hapd->last_igtk_len,
2150 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002151 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002152
2153 if (is_broadcast_ether_addr(addr)) {
2154 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2155 return -1;
2156
2157 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2158
2159 /* First, use a zero key to avoid any possible duplicate key
2160 * avoidance in the driver. */
2161 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2162 hapd->last_gtk_alg,
2163 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002164 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2165 zero, hapd->last_gtk_len,
2166 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002167 return -1;
2168
2169 /* Set the previously configured key to reset its TSC */
2170 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2171 hapd->last_gtk_alg,
2172 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002173 hapd->last_gtk_key_idx, 0, 1, NULL,
2174 0, hapd->last_gtk,
2175 hapd->last_gtk_len,
2176 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002177 }
2178
2179 sta = ap_get_sta(hapd, addr);
2180 if (!sta)
2181 return -1;
2182
2183 if (sta->last_tk_alg == WPA_ALG_NONE)
2184 return -1;
2185
2186 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2187 MAC2STR(sta->addr));
2188
2189 /* First, use a zero key to avoid any possible duplicate key avoidance
2190 * in the driver. */
2191 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002192 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2193 zero, sta->last_tk_len,
2194 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002195 return -1;
2196
2197 /* Set the previously configured key to reset its TSC/RSC */
2198 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002199 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2200 0, sta->last_tk, sta->last_tk_len,
2201 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002202}
2203
2204
2205static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2206{
2207 u8 addr[ETH_ALEN];
2208 const char *pos = cmd;
2209 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002210 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002211 int idx, set_tx;
2212 u8 seq[6], key[WPA_TK_MAX_LEN];
2213 size_t key_len;
2214
Hai Shalomfdcde762020-04-02 11:19:20 -07002215 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216
2217 alg = atoi(pos);
2218 pos = os_strchr(pos, ' ');
2219 if (!pos)
2220 return -1;
2221 pos++;
2222 if (hwaddr_aton(pos, addr))
2223 return -1;
2224 pos += 17;
2225 if (*pos != ' ')
2226 return -1;
2227 pos++;
2228 idx = atoi(pos);
2229 pos = os_strchr(pos, ' ');
2230 if (!pos)
2231 return -1;
2232 pos++;
2233 set_tx = atoi(pos);
2234 pos = os_strchr(pos, ' ');
2235 if (!pos)
2236 return -1;
2237 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002238 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002239 return -1;
2240 pos += 2 * 6;
2241 if (*pos != ' ')
2242 return -1;
2243 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002244 if (!os_strchr(pos, ' '))
2245 return -1;
2246 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002247 if (hexstr2bin(pos, key, key_len) < 0)
2248 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002249 pos += 2 * key_len;
2250 if (*pos != ' ')
2251 return -1;
2252
2253 pos++;
2254 key_flag = atoi(pos);
2255 pos = os_strchr(pos, ' ');
2256 if (pos)
2257 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002258
2259 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002260 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2261 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002262}
2263
2264
2265static void restore_tk(void *ctx1, void *ctx2)
2266{
2267 struct hostapd_data *hapd = ctx1;
2268 struct sta_info *sta = ctx2;
2269
2270 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2271 MAC2STR(sta->addr));
2272 /* This does not really restore the TSC properly, so this will result
2273 * in replay protection issues for now since there is no clean way of
2274 * preventing encryption of a single EAPOL frame. */
2275 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002276 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2277 sta->last_tk, sta->last_tk_len,
2278 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002279}
2280
2281
2282static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2283{
2284 struct sta_info *sta;
2285 u8 addr[ETH_ALEN];
2286 int plain = os_strstr(cmd, "plaintext") != NULL;
2287
2288 if (hwaddr_aton(cmd, addr))
2289 return -1;
2290
2291 sta = ap_get_sta(hapd, addr);
2292 if (!sta || !sta->wpa_sm)
2293 return -1;
2294
2295 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2296 plain = 0; /* no need for special processing */
2297 if (plain) {
2298 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2299 MAC2STR(sta->addr));
2300 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002301 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2302 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002303 }
2304
2305 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2306 return wpa_auth_resend_m1(sta->wpa_sm,
2307 os_strstr(cmd, "change-anonce") != NULL,
2308 plain ? restore_tk : NULL, hapd, sta);
2309}
2310
2311
2312static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2313{
2314 struct sta_info *sta;
2315 u8 addr[ETH_ALEN];
2316 int plain = os_strstr(cmd, "plaintext") != NULL;
2317
2318 if (hwaddr_aton(cmd, addr))
2319 return -1;
2320
2321 sta = ap_get_sta(hapd, addr);
2322 if (!sta || !sta->wpa_sm)
2323 return -1;
2324
2325 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2326 plain = 0; /* no need for special processing */
2327 if (plain) {
2328 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2329 MAC2STR(sta->addr));
2330 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002331 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2332 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002333 }
2334
2335 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2336 return wpa_auth_resend_m3(sta->wpa_sm,
2337 plain ? restore_tk : NULL, hapd, sta);
2338}
2339
2340
2341static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2342 const char *cmd)
2343{
2344 struct sta_info *sta;
2345 u8 addr[ETH_ALEN];
2346 int plain = os_strstr(cmd, "plaintext") != NULL;
2347
2348 if (hwaddr_aton(cmd, addr))
2349 return -1;
2350
2351 sta = ap_get_sta(hapd, addr);
2352 if (!sta || !sta->wpa_sm)
2353 return -1;
2354
2355 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2356 plain = 0; /* no need for special processing */
2357 if (plain) {
2358 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2359 MAC2STR(sta->addr));
2360 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002361 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2362 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002363 }
2364
2365 wpa_printf(MSG_INFO,
2366 "TESTING: Send group M1 for the same GTK and zero RSC to "
2367 MACSTR, MAC2STR(sta->addr));
2368 return wpa_auth_resend_group_m1(sta->wpa_sm,
2369 plain ? restore_tk : NULL, hapd, sta);
2370}
2371
Hai Shalomfdcde762020-04-02 11:19:20 -07002372
Hai Shaloma20dcd72022-02-04 13:43:00 -08002373static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2374{
2375 struct sta_info *sta;
2376 u8 addr[ETH_ALEN];
2377
2378 if (hwaddr_aton(cmd, addr))
2379 return -1;
2380
2381 sta = ap_get_sta(hapd, addr);
2382 if (!sta || !sta->wpa_sm)
2383 return -1;
2384
2385 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2386}
2387
2388
Hai Shalom899fcc72020-10-19 14:38:18 -07002389static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2390 char *buf, size_t buflen)
2391{
2392 struct rsn_pmksa_cache_entry *pmksa;
2393
2394 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2395 if (!pmksa)
2396 return -1;
2397
2398 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2399}
2400
2401
Hai Shalomfdcde762020-04-02 11:19:20 -07002402static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2403 char *buf, size_t buflen)
2404{
2405 struct sta_info *sta;
2406 u8 addr[ETH_ALEN];
2407 const u8 *pmk;
2408 int pmk_len;
2409
2410 if (hwaddr_aton(cmd, addr))
2411 return -1;
2412
2413 sta = ap_get_sta(hapd, addr);
2414 if (!sta || !sta->wpa_sm) {
2415 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2416 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002417 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002418 }
2419 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002420 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002421 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2422 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002423 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002424 }
2425
2426 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2427}
2428
Hai Shaloma20dcd72022-02-04 13:43:00 -08002429
2430static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2431 const char *cmd)
2432{
2433 u16 type;
2434 char *pos, *end;
2435 u8 match[10];
2436 size_t match_len;
2437 bool multicast = false;
2438
2439 type = strtol(cmd, &pos, 16);
2440 if (*pos != ' ')
2441 return -1;
2442 pos++;
2443 end = os_strchr(pos, ' ');
2444 if (end) {
2445 match_len = end - pos;
2446 multicast = os_strstr(end, "multicast") != NULL;
2447 } else {
2448 match_len = os_strlen(pos) / 2;
2449 }
2450 if (hexstr2bin(pos, match, match_len))
2451 return -1;
2452
2453 return hostapd_drv_register_frame(hapd, type, match, match_len,
2454 multicast);
2455}
2456
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002457#endif /* CONFIG_TESTING_OPTIONS */
2458
2459
Hai Shalomb755a2a2020-04-23 21:49:02 -07002460#ifdef NEED_AP_MLME
Sunil 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 }
Sunil Ravi7f769292024-07-23 22:21:32 +00002484 } else { /* Non-6 GHz channel */
2485 /* An EHT STA is also an HE STA as defined in
2486 * IEEE P802.11be/D5.0, 4.3.16a. */
2487 if (params->he_enabled || params->eht_enabled) {
2488 params->he_enabled = 1;
2489 /* An HE STA is also a VHT STA if operating in the 5 GHz
2490 * band and an HE STA is also an HT STA in the 2.4 GHz
2491 * band as defined in IEEE Std 802.11ax-2021, 4.3.15a.
2492 * A VHT STA is an HT STA as defined in IEEE
2493 * Std 802.11, 4.3.15. */
2494 if (IS_5GHZ(params->freq))
2495 params->vht_enabled = 1;
2496
2497 params->ht_enabled = 1;
2498 }
Sunil Ravi036cec52023-03-29 11:35:17 -07002499 }
2500
Hai Shalomfdcde762020-04-02 11:19:20 -07002501 switch (params->bandwidth) {
2502 case 0:
2503 /* bandwidth not specified: use 20 MHz by default */
2504 /* fall-through */
2505 case 20:
2506 if (params->center_freq1 &&
2507 params->center_freq1 != params->freq)
2508 return -1;
2509
2510 if (params->center_freq2 || params->sec_channel_offset)
2511 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002512
2513 if (punct_bitmap)
2514 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002515 break;
2516 case 40:
2517 if (params->center_freq2 || !params->sec_channel_offset)
2518 return -1;
2519
Sunil Ravi036cec52023-03-29 11:35:17 -07002520 if (punct_bitmap)
2521 return -1;
2522
Hai Shalomfdcde762020-04-02 11:19:20 -07002523 if (!params->center_freq1)
2524 break;
2525 switch (params->sec_channel_offset) {
2526 case 1:
2527 if (params->freq + 10 != params->center_freq1)
2528 return -1;
2529 break;
2530 case -1:
2531 if (params->freq - 10 != params->center_freq1)
2532 return -1;
2533 break;
2534 default:
2535 return -1;
2536 }
2537 break;
2538 case 80:
2539 if (!params->center_freq1 || !params->sec_channel_offset)
2540 return 1;
2541
2542 switch (params->sec_channel_offset) {
2543 case 1:
2544 if (params->freq - 10 != params->center_freq1 &&
2545 params->freq + 30 != params->center_freq1)
2546 return 1;
2547 break;
2548 case -1:
2549 if (params->freq + 10 != params->center_freq1 &&
2550 params->freq - 30 != params->center_freq1)
2551 return -1;
2552 break;
2553 default:
2554 return -1;
2555 }
2556
Sunil Ravi036cec52023-03-29 11:35:17 -07002557 if (params->center_freq2 && punct_bitmap)
2558 return -1;
2559
Hai Shalomfdcde762020-04-02 11:19:20 -07002560 /* Adjacent and overlapped are not allowed for 80+80 */
2561 if (params->center_freq2 &&
2562 params->center_freq1 - params->center_freq2 <= 80 &&
2563 params->center_freq2 - params->center_freq1 <= 80)
2564 return 1;
2565 break;
2566 case 160:
2567 if (!params->center_freq1 || params->center_freq2 ||
2568 !params->sec_channel_offset)
2569 return -1;
2570
2571 switch (params->sec_channel_offset) {
2572 case 1:
2573 if (params->freq + 70 != params->center_freq1 &&
2574 params->freq + 30 != params->center_freq1 &&
2575 params->freq - 10 != params->center_freq1 &&
2576 params->freq - 50 != params->center_freq1)
2577 return -1;
2578 break;
2579 case -1:
2580 if (params->freq + 50 != params->center_freq1 &&
2581 params->freq + 10 != params->center_freq1 &&
2582 params->freq - 30 != params->center_freq1 &&
2583 params->freq - 70 != params->center_freq1)
2584 return -1;
2585 break;
2586 default:
2587 return -1;
2588 }
2589 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00002590 case 320:
2591 if (!params->center_freq1 || params->center_freq2 ||
2592 !params->sec_channel_offset)
2593 return -1;
2594
2595 switch (params->sec_channel_offset) {
2596 case 1:
2597 if (params->freq + 150 != params->center_freq1 &&
2598 params->freq + 110 != params->center_freq1 &&
2599 params->freq + 70 != params->center_freq1 &&
2600 params->freq + 30 != params->center_freq1 &&
2601 params->freq - 10 != params->center_freq1 &&
2602 params->freq - 50 != params->center_freq1 &&
2603 params->freq - 90 != params->center_freq1 &&
2604 params->freq - 130 != params->center_freq1)
2605 return -1;
2606 break;
2607 case -1:
2608 if (params->freq + 130 != params->center_freq1 &&
2609 params->freq + 90 != params->center_freq1 &&
2610 params->freq + 50 != params->center_freq1 &&
2611 params->freq + 10 != params->center_freq1 &&
2612 params->freq - 30 != params->center_freq1 &&
2613 params->freq - 70 != params->center_freq1 &&
2614 params->freq - 110 != params->center_freq1 &&
2615 params->freq - 150 != params->center_freq1)
2616 return -1;
2617 break;
2618 }
2619 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002620 default:
2621 return -1;
2622 }
2623
Sunil Ravi036cec52023-03-29 11:35:17 -07002624 if (!punct_bitmap)
2625 return 0;
2626
2627 if (!params->eht_enabled) {
2628 wpa_printf(MSG_ERROR,
2629 "Preamble puncturing supported only in EHT");
2630 return -1;
2631 }
2632
2633 if (params->freq >= 2412 && params->freq <= 2484) {
2634 wpa_printf(MSG_ERROR,
2635 "Preamble puncturing is not supported in 2.4 GHz");
2636 return -1;
2637 }
2638
2639 start_freq = params->center_freq1 - (params->bandwidth / 2);
2640 if (!is_punct_bitmap_valid(params->bandwidth,
2641 (params->freq - start_freq) / 20,
2642 punct_bitmap)) {
2643 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2644 return -1;
2645 }
2646
Hai Shalomfdcde762020-04-02 11:19:20 -07002647 return 0;
2648}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002649#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002650
2651
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002652static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2653 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002654{
2655#ifdef NEED_AP_MLME
2656 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002657 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002658 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002659 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002660 int bandwidth;
2661 u8 chan;
Sunil Ravi99c035e2024-07-12 01:42:03 +00002662 unsigned int num_err = 0;
2663 int err = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002664
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002665 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002666 if (ret)
2667 return ret;
2668
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002669 settings.link_id = -1;
2670#ifdef CONFIG_IEEE80211BE
2671 if (iface->num_bss && iface->bss[0]->conf->mld_ap)
2672 settings.link_id = iface->bss[0]->mld_link_id;
2673#endif /* CONFIG_IEEE80211BE */
2674
Sunil Ravi036cec52023-03-29 11:35:17 -07002675 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
2676 settings.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002677 if (ret) {
2678 wpa_printf(MSG_INFO,
2679 "chanswitch: invalid frequency settings provided");
2680 return ret;
2681 }
2682
2683 switch (settings.freq_params.bandwidth) {
2684 case 40:
2685 bandwidth = CHAN_WIDTH_40;
2686 break;
2687 case 80:
2688 if (settings.freq_params.center_freq2)
2689 bandwidth = CHAN_WIDTH_80P80;
2690 else
2691 bandwidth = CHAN_WIDTH_80;
2692 break;
2693 case 160:
2694 bandwidth = CHAN_WIDTH_160;
2695 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002696 case 320:
2697 bandwidth = CHAN_WIDTH_320;
2698 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002699 default:
2700 bandwidth = CHAN_WIDTH_20;
2701 break;
2702 }
2703
2704 if (settings.freq_params.center_freq1)
2705 dfs_range += hostapd_is_dfs_overlap(
2706 iface, bandwidth, settings.freq_params.center_freq1);
2707 else
2708 dfs_range += hostapd_is_dfs_overlap(
2709 iface, bandwidth, settings.freq_params.freq);
2710
2711 if (settings.freq_params.center_freq2)
2712 dfs_range += hostapd_is_dfs_overlap(
2713 iface, bandwidth, settings.freq_params.center_freq2);
2714
2715 if (dfs_range) {
2716 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2717 if (ret == NUM_HOSTAPD_MODES) {
2718 wpa_printf(MSG_ERROR,
2719 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2720 settings.freq_params.freq,
2721 settings.freq_params.sec_channel_offset,
2722 settings.freq_params.bandwidth);
2723 return -1;
2724 }
2725
2726 settings.freq_params.channel = chan;
2727
2728 wpa_printf(MSG_DEBUG,
2729 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2730 settings.freq_params.channel,
2731 settings.freq_params.freq,
2732 settings.freq_params.sec_channel_offset,
2733 settings.freq_params.bandwidth,
2734 settings.freq_params.center_freq1);
2735
2736 /* Perform CAC and switch channel */
Sunil Ravi99c035e2024-07-12 01:42:03 +00002737 iface->is_ch_switch_dfs = true;
Hai Shalomfdcde762020-04-02 11:19:20 -07002738 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2739 return 0;
2740 }
2741
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002742 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002743
Sunil Ravia04bd252022-05-02 22:54:18 -07002744 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002745 hostapd_chan_switch_config(iface->bss[i],
2746 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002747
Sunil Ravi99c035e2024-07-12 01:42:03 +00002748 err = hostapd_switch_channel(iface->bss[i], &settings);
2749 if (err) {
2750 ret = err;
2751 num_err++;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002752 }
2753 }
2754
Sunil Ravi99c035e2024-07-12 01:42:03 +00002755 return (iface->num_bss == num_err) ? ret : 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002756#else /* NEED_AP_MLME */
2757 return -1;
2758#endif /* NEED_AP_MLME */
2759}
2760
2761
Sunil Ravi7f769292024-07-23 22:21:32 +00002762#ifdef CONFIG_IEEE80211AX
2763static int hostapd_ctrl_iface_color_change(struct hostapd_iface *iface,
2764 const char *pos)
2765{
2766#ifdef NEED_AP_MLME
2767 struct cca_settings settings;
2768 struct hostapd_data *hapd = iface->bss[0];
2769 int ret, color;
2770 unsigned int i;
2771 char *end;
2772
2773 os_memset(&settings, 0, sizeof(settings));
2774
2775 color = strtol(pos, &end, 10);
2776 if (pos == end || color < 0 || color > 63) {
2777 wpa_printf(MSG_ERROR, "color_change: Invalid color provided");
2778 return -1;
2779 }
2780
2781 /* Color value is expected to be [1-63]. If 0 comes, assumption is this
2782 * is to disable the color. In this case no need to do CCA, just
2783 * changing Beacon frames is sufficient. */
2784 if (color == 0) {
2785 if (iface->conf->he_op.he_bss_color_disabled) {
2786 wpa_printf(MSG_ERROR,
2787 "color_change: Color is already disabled");
2788 return -1;
2789 }
2790
2791 iface->conf->he_op.he_bss_color_disabled = 1;
2792
2793 for (i = 0; i < iface->num_bss; i++)
2794 ieee802_11_set_beacon(iface->bss[i]);
2795
2796 return 0;
2797 }
2798
2799 if (color == iface->conf->he_op.he_bss_color) {
2800 if (!iface->conf->he_op.he_bss_color_disabled) {
2801 wpa_printf(MSG_ERROR,
2802 "color_change: Provided color is already set");
2803 return -1;
2804 }
2805
2806 iface->conf->he_op.he_bss_color_disabled = 0;
2807
2808 for (i = 0; i < iface->num_bss; i++)
2809 ieee802_11_set_beacon(iface->bss[i]);
2810
2811 return 0;
2812 }
2813
2814 if (hapd->cca_in_progress) {
2815 wpa_printf(MSG_ERROR,
2816 "color_change: CCA is already in progress");
2817 return -1;
2818 }
2819
2820 iface->conf->he_op.he_bss_color_disabled = 0;
2821
2822 for (i = 0; i < iface->num_bss; i++) {
2823 struct hostapd_data *bss = iface->bss[i];
2824
2825 hostapd_cleanup_cca_params(bss);
2826
2827 bss->cca_color = color;
2828 bss->cca_count = 10;
2829
2830 if (hostapd_fill_cca_settings(bss, &settings)) {
2831 wpa_printf(MSG_DEBUG,
2832 "color_change: Filling CCA settings failed for color: %d\n",
2833 color);
2834 hostapd_cleanup_cca_params(bss);
2835 continue;
2836 }
2837
2838 wpa_printf(MSG_DEBUG, "Setting user selected color: %d", color);
2839 ret = hostapd_drv_switch_color(bss, &settings);
2840 if (ret)
2841 hostapd_cleanup_cca_params(bss);
2842
2843 free_beacon_data(&settings.beacon_cca);
2844 free_beacon_data(&settings.beacon_after);
2845 }
2846
2847 return 0;
2848#else /* NEED_AP_MLME */
2849 return -1;
2850#endif /* NEED_AP_MLME */
2851}
2852#endif /* CONFIG_IEEE80211AX */
2853
2854
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002855static u8 hostapd_maxnss(struct hostapd_data *hapd, struct sta_info *sta)
2856{
2857 u8 *mcs_set = NULL;
2858 u16 mcs_map;
2859 u8 ht_rx_nss = 0;
2860 u8 vht_rx_nss = 1;
2861 u8 mcs;
2862 bool ht_supported = false;
2863 bool vht_supported = false;
2864 int i;
2865
2866 if (sta->ht_capabilities && (sta->flags & WLAN_STA_HT)) {
2867 mcs_set = sta->ht_capabilities->supported_mcs_set;
2868 ht_supported = true;
2869 }
2870
2871 if (sta->vht_capabilities && (sta->flags & WLAN_STA_VHT)) {
2872 mcs_map = le_to_host16(
2873 sta->vht_capabilities->vht_supported_mcs_set.rx_map);
2874 vht_supported = true;
2875 }
2876
2877 if (ht_supported && mcs_set) {
2878 if (mcs_set[0])
2879 ht_rx_nss++;
2880 if (mcs_set[1])
2881 ht_rx_nss++;
2882 if (mcs_set[2])
2883 ht_rx_nss++;
2884 if (mcs_set[3])
2885 ht_rx_nss++;
2886 }
2887 if (vht_supported) {
2888 for (i = 7; i >= 0; i--) {
2889 mcs = (mcs_map >> (2 * i)) & 0x03;
2890 if (mcs != 0x03) {
2891 vht_rx_nss = i + 1;
2892 break;
2893 }
2894 }
2895 }
2896
2897 return ht_rx_nss > vht_rx_nss ? ht_rx_nss : vht_rx_nss;
2898}
2899
2900
2901static char hostapd_ctrl_iface_notify_cw_htaction(struct hostapd_data *hapd,
2902 const u8 *addr, u8 width)
2903{
2904 u8 buf[3];
2905 char ret;
2906
2907 width = width >= 1 ? 1 : 0;
2908
2909 buf[0] = WLAN_ACTION_HT;
2910 buf[1] = WLAN_HT_ACTION_NOTIFY_CHANWIDTH;
2911 buf[2] = width;
2912
2913 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2914 buf, sizeof(buf));
2915 if (ret)
2916 wpa_printf(MSG_DEBUG,
2917 "Failed to send Notify Channel Width frame to "
2918 MACSTR, MAC2STR(addr));
2919
2920 return ret;
2921}
2922
2923
2924static char hostapd_ctrl_iface_notify_cw_vhtaction(struct hostapd_data *hapd,
2925 const u8 *addr, u8 width)
2926{
2927 u8 buf[3];
2928 char ret;
2929
2930 buf[0] = WLAN_ACTION_VHT;
2931 buf[1] = WLAN_VHT_ACTION_OPMODE_NOTIF;
2932 buf[2] = width;
2933
2934 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
2935 buf, sizeof(buf));
2936 if (ret)
2937 wpa_printf(MSG_DEBUG,
2938 "Failed to send Opeating Mode Notification frame to "
2939 MACSTR, MAC2STR(addr));
2940
2941 return ret;
2942}
2943
2944
2945static char hostapd_ctrl_iface_notify_cw_change(struct hostapd_data *hapd,
2946 const char *cmd)
2947{
2948 u8 cw, operating_mode = 0, nss;
2949 struct sta_info *sta;
2950 enum hostapd_hw_mode hw_mode;
2951
2952 if (is_6ghz_freq(hapd->iface->freq)) {
2953 wpa_printf(MSG_ERROR, "20/40 BSS coex not supported in 6 GHz");
2954 return -1;
2955 }
2956
2957 cw = atoi(cmd);
2958 hw_mode = hapd->iface->current_mode->mode;
2959 if ((hw_mode == HOSTAPD_MODE_IEEE80211G ||
2960 hw_mode == HOSTAPD_MODE_IEEE80211B) &&
2961 !(cw == 0 || cw == 1)) {
2962 wpa_printf(MSG_ERROR,
2963 "Channel width should be either 20 MHz or 40 MHz for 2.4 GHz band");
2964 return -1;
2965 }
2966
2967 switch (cw) {
2968 case 0:
2969 operating_mode = 0;
2970 break;
2971 case 1:
2972 operating_mode = VHT_OPMODE_CHANNEL_40MHZ;
2973 break;
2974 case 2:
2975 operating_mode = VHT_OPMODE_CHANNEL_80MHZ;
2976 break;
2977 case 3:
2978 operating_mode = VHT_OPMODE_CHANNEL_160MHZ;
2979 break;
2980 default:
2981 wpa_printf(MSG_ERROR, "Channel width should be between 0 to 3");
2982 return -1;
2983 }
2984
2985 for (sta = hapd->sta_list; sta; sta = sta->next) {
2986 if ((sta->flags & WLAN_STA_VHT) && sta->vht_capabilities) {
2987 nss = hostapd_maxnss(hapd, sta) - 1;
2988 hostapd_ctrl_iface_notify_cw_vhtaction(hapd, sta->addr,
2989 operating_mode |
2990 (u8) (nss << 4));
2991 continue;
2992 }
2993
2994 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) ==
2995 WLAN_STA_HT && sta->ht_capabilities)
2996 hostapd_ctrl_iface_notify_cw_htaction(hapd, sta->addr,
2997 cw);
2998 }
2999
3000 return 0;
3001}
3002
3003
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003004static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
3005 int reply_size, const char *param)
3006{
3007#ifdef RADIUS_SERVER
3008 if (os_strcmp(param, "radius_server") == 0) {
3009 return radius_server_get_mib(hapd->radius_srv, reply,
3010 reply_size);
3011 }
3012#endif /* RADIUS_SERVER */
3013 return -1;
3014}
3015
3016
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003017static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
3018 char *buf, size_t buflen)
3019{
3020 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08003021 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003022 u8 *data = NULL;
3023 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08003024 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003025 struct wpabuf *reply;
3026 size_t data_len = 0;
3027
Hai Shalom60840252021-02-19 19:02:11 -08003028 /**
3029 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
3030 * [nested=<0|1>]
3031 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003032 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003033 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003034 return -EINVAL;
3035
3036 subcmd = strtoul(pos, &pos, 10);
3037
3038 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003039 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003040 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08003041
3042 temp = os_strchr(pos, ' ');
3043 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003044 }
3045
3046 if (data_len) {
3047 data_len /= 2;
3048 data = os_malloc(data_len);
3049 if (!data)
3050 return -ENOBUFS;
3051
3052 if (hexstr2bin(pos, data, data_len)) {
3053 wpa_printf(MSG_DEBUG,
3054 "Vendor command: wrong parameter format");
3055 os_free(data);
3056 return -EINVAL;
3057 }
3058 }
3059
Hai Shalom60840252021-02-19 19:02:11 -08003060 pos = os_strstr(cmd, "nested=");
3061 if (pos)
3062 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
3063 NESTED_ATTR_NOT_USED;
3064
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003065 reply = wpabuf_alloc((buflen - 1) / 2);
3066 if (!reply) {
3067 os_free(data);
3068 return -ENOBUFS;
3069 }
3070
3071 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08003072 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003073
3074 if (ret == 0)
3075 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
3076 wpabuf_len(reply));
3077
3078 wpabuf_free(reply);
3079 os_free(data);
3080
3081 return ret;
3082}
3083
3084
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003085static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
3086 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003088 u8 addr[ETH_ALEN];
3089 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003091 if (hwaddr_aton(cmd, addr))
3092 return -1;
3093
3094 sta = ap_get_sta(hapd, addr);
3095 if (!sta || !sta->eapol_sm)
3096 return -1;
3097
3098 eapol_auth_reauthenticate(sta->eapol_sm);
3099 return 0;
3100}
3101
3102
3103static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
3104{
3105 u8 addr[ETH_ALEN];
3106 struct sta_info *sta;
3107 char *pos = cmd, *param;
3108
3109 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
3110 return -1;
3111 pos += 18;
3112 param = pos;
3113 pos = os_strchr(pos, ' ');
3114 if (!pos)
3115 return -1;
3116 *pos++ = '\0';
3117
3118 sta = ap_get_sta(hapd, addr);
3119 if (!sta || !sta->eapol_sm)
3120 return -1;
3121
3122 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
3123}
3124
3125
3126static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
3127 char *buf, size_t buflen)
3128{
3129 char *pos, *end, *stamp;
3130 int ret;
3131
3132 /* cmd: "LOG_LEVEL [<level>]" */
3133 if (*cmd == '\0') {
3134 pos = buf;
3135 end = buf + buflen;
3136 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
3137 "Timestamp: %d\n",
3138 debug_level_str(wpa_debug_level),
3139 wpa_debug_timestamp);
3140 if (os_snprintf_error(end - pos, ret))
3141 ret = 0;
3142
3143 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003146 while (*cmd == ' ')
3147 cmd++;
3148
3149 stamp = os_strchr(cmd, ' ');
3150 if (stamp) {
3151 *stamp++ = '\0';
3152 while (*stamp == ' ') {
3153 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 }
3156
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003157 if (os_strlen(cmd)) {
3158 int level = str_to_debug_level(cmd);
3159 if (level < 0)
3160 return -1;
3161 wpa_debug_level = level;
3162 }
3163
3164 if (stamp && os_strlen(stamp))
3165 wpa_debug_timestamp = atoi(stamp);
3166
3167 os_memcpy(buf, "OK\n", 3);
3168 return 3;
3169}
3170
3171
3172#ifdef NEED_AP_MLME
3173static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3174 char *buf, size_t buflen)
3175{
3176 struct hostapd_iface *iface = hapd->iface;
3177 char *pos, *end;
3178 struct hostapd_sta_info *info;
3179 struct os_reltime now;
3180
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003181 if (!iface->num_sta_seen)
3182 return 0;
3183
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003184 sta_track_expire(iface, 0);
3185
3186 pos = buf;
3187 end = buf + buflen;
3188
3189 os_get_reltime(&now);
3190 dl_list_for_each_reverse(info, &iface->sta_seen,
3191 struct hostapd_sta_info, list) {
3192 struct os_reltime age;
3193 int ret;
3194
3195 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003196 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3197 MAC2STR(info->addr), (unsigned int) age.sec,
3198 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003199 if (os_snprintf_error(end - pos, ret))
3200 break;
3201 pos += ret;
3202 }
3203
3204 return pos - buf;
3205}
3206#endif /* NEED_AP_MLME */
3207
3208
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003209static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3210 const char *cmd)
3211{
3212 u8 addr[ETH_ALEN];
3213
3214 if (hwaddr_aton(cmd, addr)) {
3215 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3216 return -1;
3217 }
3218
3219 return hostapd_send_lci_req(hapd, addr);
3220}
3221
3222
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003223static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003224{
3225 u8 addr[ETH_ALEN];
3226 char *token, *context = NULL;
3227 int random_interval, min_ap;
3228 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3229 unsigned int n_responders;
3230
3231 token = str_token(cmd, " ", &context);
3232 if (!token || hwaddr_aton(token, addr)) {
3233 wpa_printf(MSG_INFO,
3234 "CTRL: REQ_RANGE - Bad destination address");
3235 return -1;
3236 }
3237
3238 token = str_token(cmd, " ", &context);
3239 if (!token)
3240 return -1;
3241
3242 random_interval = atoi(token);
3243 if (random_interval < 0 || random_interval > 0xffff)
3244 return -1;
3245
3246 token = str_token(cmd, " ", &context);
3247 if (!token)
3248 return -1;
3249
3250 min_ap = atoi(token);
3251 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3252 return -1;
3253
3254 n_responders = 0;
3255 while ((token = str_token(cmd, " ", &context))) {
3256 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3257 wpa_printf(MSG_INFO,
3258 "CTRL: REQ_RANGE: Too many responders");
3259 return -1;
3260 }
3261
3262 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3263 wpa_printf(MSG_INFO,
3264 "CTRL: REQ_RANGE: Bad responder address");
3265 return -1;
3266 }
3267
3268 n_responders++;
3269 }
3270
3271 if (!n_responders) {
3272 wpa_printf(MSG_INFO,
3273 "CTRL: REQ_RANGE - No FTM responder address");
3274 return -1;
3275 }
3276
3277 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3278 responders, n_responders);
3279}
3280
3281
Dmitry Shmidt29333592017-01-09 12:27:11 -08003282static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3283 const char *cmd, char *reply,
3284 size_t reply_size)
3285{
3286 u8 addr[ETH_ALEN];
3287 const char *pos;
3288 struct wpabuf *req;
3289 int ret;
3290 u8 req_mode = 0;
3291
3292 if (hwaddr_aton(cmd, addr))
3293 return -1;
3294 pos = os_strchr(cmd, ' ');
3295 if (!pos)
3296 return -1;
3297 pos++;
3298 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3299 int val = hex2byte(pos + 9);
3300
3301 if (val < 0)
3302 return -1;
3303 req_mode = val;
3304 pos += 11;
3305 pos = os_strchr(pos, ' ');
3306 if (!pos)
3307 return -1;
3308 pos++;
3309 }
3310 req = wpabuf_parse_bin(pos);
3311 if (!req)
3312 return -1;
3313
3314 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3315 wpabuf_free(req);
3316 if (ret >= 0)
3317 ret = os_snprintf(reply, reply_size, "%d", ret);
3318 return ret;
3319}
3320
3321
Sunil Ravi99c035e2024-07-12 01:42:03 +00003322static int hostapd_ctrl_iface_req_link_measurement(struct hostapd_data *hapd,
3323 const char *cmd, char *reply,
3324 size_t reply_size)
3325{
3326 u8 addr[ETH_ALEN];
3327 int ret;
3328
3329 if (hwaddr_aton(cmd, addr)) {
3330 wpa_printf(MSG_ERROR,
3331 "CTRL: REQ_LINK_MEASUREMENT: Invalid MAC address");
3332 return -1;
3333 }
3334
3335 ret = hostapd_send_link_measurement_req(hapd, addr);
3336 if (ret >= 0)
3337 ret = os_snprintf(reply, reply_size, "%d", ret);
3338 return ret;
3339}
3340
3341
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003342static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3343 char *buf, size_t buflen)
3344{
3345 if (!(hapd->conf->radio_measurements[0] &
3346 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3347 wpa_printf(MSG_ERROR,
3348 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3349 return -1;
3350 }
3351
3352 return hostapd_neighbor_show(hapd, buf, buflen);
3353}
3354
3355
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003356static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3357{
3358 struct wpa_ssid_value ssid;
3359 u8 bssid[ETH_ALEN];
3360 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003361 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003362 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003363 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003364 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003365
3366 if (!(hapd->conf->radio_measurements[0] &
3367 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3368 wpa_printf(MSG_ERROR,
3369 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3370 return -1;
3371 }
3372
3373 if (hwaddr_aton(buf, bssid)) {
3374 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3375 return -1;
3376 }
3377
3378 tmp = os_strstr(buf, "ssid=");
3379 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3380 wpa_printf(MSG_ERROR,
3381 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3382 return -1;
3383 }
3384 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3385 if (!buf)
3386 return -1;
3387
3388 tmp = os_strstr(buf, "nr=");
3389 if (!tmp) {
3390 wpa_printf(MSG_ERROR,
3391 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3392 return -1;
3393 }
3394
3395 buf = os_strchr(tmp, ' ');
3396 if (buf)
3397 *buf++ = '\0';
3398
3399 nr = wpabuf_parse_bin(tmp + 3);
3400 if (!nr) {
3401 wpa_printf(MSG_ERROR,
3402 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3403 return -1;
3404 }
3405
3406 if (!buf)
3407 goto set;
3408
3409 tmp = os_strstr(buf, "lci=");
3410 if (tmp) {
3411 buf = os_strchr(tmp, ' ');
3412 if (buf)
3413 *buf++ = '\0';
3414 lci = wpabuf_parse_bin(tmp + 4);
3415 if (!lci) {
3416 wpa_printf(MSG_ERROR,
3417 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003418 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003419 }
3420 }
3421
3422 if (!buf)
3423 goto set;
3424
3425 tmp = os_strstr(buf, "civic=");
3426 if (tmp) {
3427 buf = os_strchr(tmp, ' ');
3428 if (buf)
3429 *buf++ = '\0';
3430 civic = wpabuf_parse_bin(tmp + 6);
3431 if (!civic) {
3432 wpa_printf(MSG_ERROR,
3433 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003434 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003435 }
3436 }
3437
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003438 if (!buf)
3439 goto set;
3440
3441 if (os_strstr(buf, "stat"))
3442 stationary = 1;
3443
Hai Shaloma20dcd72022-02-04 13:43:00 -08003444 tmp = os_strstr(buf, "bss_parameter=");
3445 if (tmp) {
3446 bss_parameters = atoi(tmp + 14);
3447 if (bss_parameters < 0 || bss_parameters > 0xff) {
3448 wpa_printf(MSG_ERROR,
3449 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3450 goto fail;
3451 }
3452 }
3453
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003454set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003455 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003456 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003457
Hai Shaloma20dcd72022-02-04 13:43:00 -08003458fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003459 wpabuf_free(nr);
3460 wpabuf_free(lci);
3461 wpabuf_free(civic);
3462
3463 return ret;
3464}
3465
3466
3467static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3468 char *buf)
3469{
3470 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003471 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003472 u8 bssid[ETH_ALEN];
3473 char *tmp;
3474
3475 if (hwaddr_aton(buf, bssid)) {
3476 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3477 return -1;
3478 }
3479
3480 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003481 if (tmp) {
3482 ssidp = &ssid;
3483 if (ssid_parse(tmp + 5, &ssid)) {
3484 wpa_printf(MSG_ERROR,
3485 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3486 return -1;
3487 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003488 }
3489
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003490 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003491}
3492
3493
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003494static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3495 size_t buflen)
3496{
3497 int ret, i;
3498 char *pos, *end;
3499
3500 ret = os_snprintf(buf, buflen, "%016llX:\n",
3501 (long long unsigned) iface->drv_flags);
3502 if (os_snprintf_error(buflen, ret))
3503 return -1;
3504
3505 pos = buf + ret;
3506 end = buf + buflen;
3507
3508 for (i = 0; i < 64; i++) {
3509 if (iface->drv_flags & (1LLU << i)) {
3510 ret = os_snprintf(pos, end - pos, "%s\n",
3511 driver_flag_to_string(1LLU << i));
3512 if (os_snprintf_error(end - pos, ret))
3513 return -1;
3514 pos += ret;
3515 }
3516 }
3517
3518 return pos - buf;
3519}
3520
3521
Hai Shalomb755a2a2020-04-23 21:49:02 -07003522static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3523 size_t buflen)
3524{
3525 int ret, i;
3526 char *pos, *end;
3527
3528 ret = os_snprintf(buf, buflen, "%016llX:\n",
3529 (long long unsigned) iface->drv_flags2);
3530 if (os_snprintf_error(buflen, ret))
3531 return -1;
3532
3533 pos = buf + ret;
3534 end = buf + buflen;
3535
3536 for (i = 0; i < 64; i++) {
3537 if (iface->drv_flags2 & (1LLU << i)) {
3538 ret = os_snprintf(pos, end - pos, "%s\n",
3539 driver_flag2_to_string(1LLU << i));
3540 if (os_snprintf_error(end - pos, ret))
3541 return -1;
3542 pos += ret;
3543 }
3544 }
3545
3546 return pos - buf;
3547}
3548
3549
Hai Shalom021b0b52019-04-10 11:17:58 -07003550static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3551 const char *field, char *buf,
3552 size_t buflen)
3553{
3554 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3555
3556#ifdef CONFIG_DPP
3557 if (os_strcmp(field, "dpp") == 0) {
3558 int res;
3559
Hai Shaloma20dcd72022-02-04 13:43:00 -08003560#ifdef CONFIG_DPP3
3561 res = os_snprintf(buf, buflen, "DPP=3");
3562#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003563 res = os_snprintf(buf, buflen, "DPP=2");
3564#else /* CONFIG_DPP2 */
3565 res = os_snprintf(buf, buflen, "DPP=1");
3566#endif /* CONFIG_DPP2 */
3567 if (os_snprintf_error(buflen, res))
3568 return -1;
3569 return res;
3570 }
3571#endif /* CONFIG_DPP */
3572
3573 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3574 field);
3575
3576 return -1;
3577}
3578
3579
Hai Shaloma20dcd72022-02-04 13:43:00 -08003580#ifdef ANDROID
3581static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3582 char *buf, size_t buflen)
3583{
3584 int ret;
3585
3586 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3587 if (ret == 0) {
3588 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3589 if (os_snprintf_error(buflen, ret))
3590 ret = -1;
3591 }
3592 return ret;
3593}
3594#endif /* ANDROID */
3595
3596
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003597#ifdef CONFIG_IEEE80211BE
3598
3599static int hostapd_ctrl_iface_enable_mld(struct hostapd_iface *iface)
3600{
3601 unsigned int i;
3602
3603 if (!iface || !iface->bss[0]->conf->mld_ap) {
3604 wpa_printf(MSG_ERROR,
3605 "Trying to enable AP MLD on an interface that is not affiliated with an AP MLD");
3606 return -1;
3607 }
3608
3609 for (i = 0; i < iface->interfaces->count; ++i) {
3610 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3611 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003612
Sunil Ravi99c035e2024-07-12 01:42:03 +00003613 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003614 continue;
3615
3616 if (hostapd_enable_iface(h_iface)) {
3617 wpa_printf(MSG_ERROR, "Enabling of AP MLD failed");
3618 return -1;
3619 }
3620 }
3621 return 0;
3622}
3623
3624
3625static void hostapd_disable_iface_bss(struct hostapd_iface *iface)
3626{
3627 unsigned int i;
3628
3629 for (i = 0; i < iface->num_bss; i++)
3630 hostapd_bss_deinit_no_free(iface->bss[i]);
3631}
3632
3633
3634static int hostapd_ctrl_iface_disable_mld(struct hostapd_iface *iface)
3635{
3636 unsigned int i;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003637
3638 if (!iface || !iface->bss[0]->conf->mld_ap) {
3639 wpa_printf(MSG_ERROR,
3640 "Trying to disable AP MLD on an interface that is not affiliated with an AP MLD.");
3641 return -1;
3642 }
3643
3644 /* First, disable BSSs before stopping beaconing and doing driver
3645 * deinit so that the broadcast Deauthentication frames go out. */
3646
3647 for (i = 0; i < iface->interfaces->count; ++i) {
3648 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3649 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003650
Sunil Ravi99c035e2024-07-12 01:42:03 +00003651 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003652 continue;
3653
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003654 hostapd_disable_iface_bss(iface);
3655 }
3656
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003657 /* Then, fully disable interfaces */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003658 for (i = 0; i < iface->interfaces->count; ++i) {
3659 struct hostapd_iface *h_iface = iface->interfaces->iface[i];
3660 struct hostapd_data *h_hapd = h_iface->bss[0];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003661
Sunil Ravi99c035e2024-07-12 01:42:03 +00003662 if (!hostapd_is_ml_partner(h_hapd, iface->bss[0]))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003663 continue;
3664
3665 if (hostapd_disable_iface(h_iface)) {
3666 wpa_printf(MSG_ERROR, "Disabling AP MLD failed");
3667 return -1;
3668 }
3669 }
3670
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003671 return 0;
3672}
3673
3674
3675#ifdef CONFIG_TESTING_OPTIONS
3676static int hostapd_ctrl_iface_link_remove(struct hostapd_data *hapd, char *cmd,
3677 char *buf, size_t buflen)
3678{
3679 int ret;
3680 u32 count = atoi(cmd);
3681
3682 if (!count)
3683 count = 1;
3684
3685 ret = hostapd_link_remove(hapd, count);
3686 if (ret == 0) {
3687 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3688 if (os_snprintf_error(buflen, ret))
3689 ret = -1;
3690 else
3691 ret = 0;
3692 }
3693
3694 return ret;
3695}
3696#endif /* CONFIG_TESTING_OPTIONS */
3697#endif /* CONFIG_IEEE80211BE */
3698
3699
3700#ifdef CONFIG_NAN_USD
3701
3702static int hostapd_ctrl_nan_publish(struct hostapd_data *hapd, char *cmd,
3703 char *buf, size_t buflen)
3704{
3705 char *token, *context = NULL;
3706 int publish_id;
3707 struct nan_publish_params params;
3708 const char *service_name = NULL;
3709 struct wpabuf *ssi = NULL;
3710 int ret = -1;
3711 enum nan_service_protocol_type srv_proto_type = 0;
3712
3713 os_memset(&params, 0, sizeof(params));
3714 /* USD shall use both solicited and unsolicited transmissions */
3715 params.unsolicited = true;
3716 params.solicited = true;
3717 /* USD shall require FSD without GAS */
3718 params.fsd = true;
3719
3720 while ((token = str_token(cmd, " ", &context))) {
3721 if (os_strncmp(token, "service_name=", 13) == 0) {
3722 service_name = token + 13;
3723 continue;
3724 }
3725
3726 if (os_strncmp(token, "ttl=", 4) == 0) {
3727 params.ttl = atoi(token + 4);
3728 continue;
3729 }
3730
3731 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3732 srv_proto_type = atoi(token + 15);
3733 continue;
3734 }
3735
3736 if (os_strncmp(token, "ssi=", 4) == 0) {
3737 if (ssi)
3738 goto fail;
3739 ssi = wpabuf_parse_bin(token + 4);
3740 if (!ssi)
3741 goto fail;
3742 continue;
3743 }
3744
3745 if (os_strcmp(token, "solicited=0") == 0) {
3746 params.solicited = false;
3747 continue;
3748 }
3749
3750 if (os_strcmp(token, "unsolicited=0") == 0) {
3751 params.unsolicited = false;
3752 continue;
3753 }
3754
3755 if (os_strcmp(token, "fsd=0") == 0) {
3756 params.fsd = false;
3757 continue;
3758 }
3759
3760 wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
3761 token);
3762 goto fail;
3763 }
3764
3765 publish_id = hostapd_nan_usd_publish(hapd, service_name, srv_proto_type,
3766 ssi, &params);
3767 if (publish_id > 0)
3768 ret = os_snprintf(buf, buflen, "%d", publish_id);
3769fail:
3770 wpabuf_free(ssi);
3771 return ret;
3772}
3773
3774
3775static int hostapd_ctrl_nan_cancel_publish(struct hostapd_data *hapd,
3776 char *cmd)
3777{
3778 char *token, *context = NULL;
3779 int publish_id = 0;
3780
3781 while ((token = str_token(cmd, " ", &context))) {
3782 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3783 continue;
3784 wpa_printf(MSG_INFO,
3785 "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
3786 token);
3787 return -1;
3788 }
3789
3790 if (publish_id <= 0) {
3791 wpa_printf(MSG_INFO,
3792 "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
3793 return -1;
3794 }
3795
3796 hostapd_nan_usd_cancel_publish(hapd, publish_id);
3797 return 0;
3798}
3799
3800
3801static int hostapd_ctrl_nan_update_publish(struct hostapd_data *hapd,
3802 char *cmd)
3803{
3804 char *token, *context = NULL;
3805 int publish_id = 0;
3806 struct wpabuf *ssi = NULL;
3807 int ret = -1;
3808
3809 while ((token = str_token(cmd, " ", &context))) {
3810 if (sscanf(token, "publish_id=%i", &publish_id) == 1)
3811 continue;
3812 if (os_strncmp(token, "ssi=", 4) == 0) {
3813 if (ssi)
3814 goto fail;
3815 ssi = wpabuf_parse_bin(token + 4);
3816 if (!ssi)
3817 goto fail;
3818 continue;
3819 }
3820 wpa_printf(MSG_INFO,
3821 "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
3822 token);
3823 goto fail;
3824 }
3825
3826 if (publish_id <= 0) {
3827 wpa_printf(MSG_INFO,
3828 "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
3829 goto fail;
3830 }
3831
3832 ret = hostapd_nan_usd_update_publish(hapd, publish_id, ssi);
3833fail:
3834 wpabuf_free(ssi);
3835 return ret;
3836}
3837
3838
3839static int hostapd_ctrl_nan_subscribe(struct hostapd_data *hapd, char *cmd,
3840 char *buf, size_t buflen)
3841{
3842 char *token, *context = NULL;
3843 int subscribe_id;
3844 struct nan_subscribe_params params;
3845 const char *service_name = NULL;
3846 struct wpabuf *ssi = NULL;
3847 int ret = -1;
3848 enum nan_service_protocol_type srv_proto_type = 0;
3849
3850 os_memset(&params, 0, sizeof(params));
3851
3852 while ((token = str_token(cmd, " ", &context))) {
3853 if (os_strncmp(token, "service_name=", 13) == 0) {
3854 service_name = token + 13;
3855 continue;
3856 }
3857
3858 if (os_strcmp(token, "active=1") == 0) {
3859 params.active = true;
3860 continue;
3861 }
3862
3863 if (os_strncmp(token, "ttl=", 4) == 0) {
3864 params.ttl = atoi(token + 4);
3865 continue;
3866 }
3867
3868 if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
3869 srv_proto_type = atoi(token + 15);
3870 continue;
3871 }
3872
3873 if (os_strncmp(token, "ssi=", 4) == 0) {
3874 if (ssi)
3875 goto fail;
3876 ssi = wpabuf_parse_bin(token + 4);
3877 if (!ssi)
3878 goto fail;
3879 continue;
3880 }
3881
3882 wpa_printf(MSG_INFO,
3883 "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
3884 token);
3885 goto fail;
3886 }
3887
3888 subscribe_id = hostapd_nan_usd_subscribe(hapd, service_name,
3889 srv_proto_type, ssi,
3890 &params);
3891 if (subscribe_id > 0)
3892 ret = os_snprintf(buf, buflen, "%d", subscribe_id);
3893fail:
3894 wpabuf_free(ssi);
3895 return ret;
3896}
3897
3898
3899static int hostapd_ctrl_nan_cancel_subscribe(struct hostapd_data *hapd,
3900 char *cmd)
3901{
3902 char *token, *context = NULL;
3903 int subscribe_id = 0;
3904
3905 while ((token = str_token(cmd, " ", &context))) {
3906 if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
3907 continue;
3908 wpa_printf(MSG_INFO,
3909 "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
3910 token);
3911 return -1;
3912 }
3913
3914 if (subscribe_id <= 0) {
3915 wpa_printf(MSG_INFO,
3916 "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
3917 return -1;
3918 }
3919
3920 hostapd_nan_usd_cancel_subscribe(hapd, subscribe_id);
3921 return 0;
3922}
3923
3924
3925static int hostapd_ctrl_nan_transmit(struct hostapd_data *hapd, char *cmd)
3926{
3927 char *token, *context = NULL;
3928 int handle = 0;
3929 int req_instance_id = 0;
3930 struct wpabuf *ssi = NULL;
3931 u8 peer_addr[ETH_ALEN];
3932 int ret = -1;
3933
3934 os_memset(peer_addr, 0, ETH_ALEN);
3935
3936 while ((token = str_token(cmd, " ", &context))) {
3937 if (sscanf(token, "handle=%i", &handle) == 1)
3938 continue;
3939
3940 if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
3941 continue;
3942
3943 if (os_strncmp(token, "address=", 8) == 0) {
3944 if (hwaddr_aton(token + 8, peer_addr) < 0)
3945 return -1;
3946 continue;
3947 }
3948
3949 if (os_strncmp(token, "ssi=", 4) == 0) {
3950 if (ssi)
3951 goto fail;
3952 ssi = wpabuf_parse_bin(token + 4);
3953 if (!ssi)
3954 goto fail;
3955 continue;
3956 }
3957
3958 wpa_printf(MSG_INFO,
3959 "CTRL: Invalid NAN_TRANSMIT parameter: %s",
3960 token);
3961 goto fail;
3962 }
3963
3964 if (handle <= 0) {
3965 wpa_printf(MSG_INFO,
3966 "CTRL: Invalid or missing NAN_TRANSMIT handle");
3967 goto fail;
3968 }
3969
3970 if (is_zero_ether_addr(peer_addr)) {
3971 wpa_printf(MSG_INFO,
3972 "CTRL: Invalid or missing NAN_TRANSMIT address");
3973 goto fail;
3974 }
3975
3976 ret = hostapd_nan_usd_transmit(hapd, handle, ssi, NULL, peer_addr,
3977 req_instance_id);
3978fail:
3979 wpabuf_free(ssi);
3980 return ret;
3981}
3982
3983#endif /* CONFIG_NAN_USD */
3984
3985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003986static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3987 char *buf, char *reply,
3988 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003989 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003990 socklen_t fromlen)
3991{
3992 int reply_len, res;
3993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 os_memcpy(reply, "OK\n", 3);
3995 reply_len = 3;
3996
3997 if (os_strcmp(buf, "PING") == 0) {
3998 os_memcpy(reply, "PONG\n", 5);
3999 reply_len = 5;
4000 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4001 if (wpa_debug_reopen_file() < 0)
4002 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004003 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
4004 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004005 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4006 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004007 } else if (os_strcmp(buf, "STATUS") == 0) {
4008 reply_len = hostapd_ctrl_iface_status(hapd, reply,
4009 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004010 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
4011 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004012 } else if (os_strcmp(buf, "MIB") == 0) {
4013 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
4014 if (reply_len >= 0) {
4015 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
4016 reply_size - reply_len);
4017 if (res < 0)
4018 reply_len = -1;
4019 else
4020 reply_len += res;
4021 }
4022 if (reply_len >= 0) {
4023 res = ieee802_1x_get_mib(hapd, reply + reply_len,
4024 reply_size - reply_len);
4025 if (res < 0)
4026 reply_len = -1;
4027 else
4028 reply_len += res;
4029 }
4030#ifndef CONFIG_NO_RADIUS
4031 if (reply_len >= 0) {
4032 res = radius_client_get_mib(hapd->radius,
4033 reply + reply_len,
4034 reply_size - reply_len);
4035 if (res < 0)
4036 reply_len = -1;
4037 else
4038 reply_len += res;
4039 }
4040#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004041 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
4042 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
4043 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4045 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
4046 reply_size);
4047 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4048 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
4049 reply_size);
4050 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4051 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
4052 reply_size);
4053 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004054 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
4055 reply_len = -1;
4056 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4057 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 reply_len = -1;
4059 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004060 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 reply_len = -1;
4062 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004063 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064 buf + 6))
4065 reply_len = -1;
4066 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
4067 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
4068 reply_len = -1;
4069 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4070 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
4071 reply_len = -1;
4072 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4073 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
4074 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004075#ifdef CONFIG_TAXONOMY
4076 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
4077 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
4078 reply, reply_size);
4079#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004080 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
4081 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
4082 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004083 } else if (os_strcmp(buf, "STOP_AP") == 0) {
4084 if (hostapd_ctrl_iface_stop_ap(hapd))
4085 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086#ifdef NEED_AP_MLME
4087 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
4088 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
4089 reply_len = -1;
4090#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091#ifdef CONFIG_WPS
4092 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4093 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
4094 reply_len = -1;
4095 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4096 reply_len = hostapd_ctrl_iface_wps_check_pin(
4097 hapd, buf + 14, reply, reply_size);
4098 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4099 if (hostapd_wps_button_pushed(hapd, NULL))
4100 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004101 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4102 if (hostapd_wps_cancel(hapd))
4103 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4105 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
4106 reply, reply_size);
4107 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
4108 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
4109 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004110 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
4111 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
4112 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113#ifdef CONFIG_WPS_NFC
4114 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4115 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
4116 reply_len = -1;
4117 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
4118 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
4119 hapd, buf + 21, reply, reply_size);
4120 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4121 reply_len = hostapd_ctrl_iface_wps_nfc_token(
4122 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004123 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
4124 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
4125 hapd, buf + 21, reply, reply_size);
4126 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
4127 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
4128 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004129#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07004131#ifdef CONFIG_INTERWORKING
4132 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
4133 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
4134 reply_len = -1;
4135 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
4136 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
4137 reply_len = -1;
4138#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004139#ifdef CONFIG_HS20
4140 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
4141 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
4142 reply_len = -1;
4143 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
4144 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
4145 reply_len = -1;
4146#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004147#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004148 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
4149 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
4150 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004151 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
4152 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
4153 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004154 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
4155 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
4156 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004157 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
4158 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
4159 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004160#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
4162 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
4163 reply_size);
4164 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4165 if (hostapd_ctrl_iface_set(hapd, buf + 4))
4166 reply_len = -1;
4167 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4168 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
4169 reply_size);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004170 } else if (os_strcmp(buf, "ENABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004171 if (hostapd_ctrl_iface_enable(hapd->iface))
4172 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08004173 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
4174 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
4175 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004176#ifdef CONFIG_IEEE80211R_AP
4177 } else if (os_strcmp(buf, "GET_RXKHS") == 0) {
4178 reply_len = hostapd_ctrl_iface_get_rxkhs(hapd, reply,
4179 reply_size);
4180 } else if (os_strcmp(buf, "RELOAD_RXKHS") == 0) {
4181 if (hostapd_ctrl_iface_reload_rxkhs(hapd))
4182 reply_len = -1;
4183#endif /* CONFIG_IEEE80211R_AP */
Sunil Ravi77d572f2023-01-17 23:58:31 +00004184 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
4185 if (hostapd_ctrl_iface_reload_bss(hapd))
4186 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004187 } else if (os_strcmp(buf, "RELOAD_CONFIG") == 0) {
4188 if (hostapd_reload_config(hapd->iface))
4189 reply_len = -1;
4190 } else if (os_strcmp(buf, "RELOAD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004191 if (hostapd_ctrl_iface_reload(hapd->iface))
4192 reply_len = -1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004193 } else if (os_strcmp(buf, "DISABLE") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004194 if (hostapd_ctrl_iface_disable(hapd->iface))
4195 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004196 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
4197 if (ieee802_11_set_beacon(hapd))
4198 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004199#ifdef CONFIG_TESTING_OPTIONS
4200 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
4201 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
4202 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004203 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
4204 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
4205 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004206 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
4207 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
4208 buf + 23) < 0)
4209 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08004210 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
4211 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
4212 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004213 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
4214 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
4215 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004216 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
4217 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
4218 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004219 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
4220 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
4221 reply_len = -1;
4222 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
4223 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
4224 reply_len = -1;
4225 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
4226 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
4227 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004228 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004229 if (testing_set_fail_pattern(true, buf + 16) < 0)
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004230 reply_len = -1;
4231 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004232 reply_len = testing_get_fail_pattern(true, reply, reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004233 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004234 if (testing_set_fail_pattern(false, buf + 10) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004235 reply_len = -1;
4236 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004237 reply_len = testing_get_fail_pattern(false, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004238 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
4239 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
4240 reply_len = -1;
4241 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
4242 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
4243 reply_len = -1;
4244 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
4245 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
4246 reply_len = -1;
4247 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
4248 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
4249 reply_len = -1;
4250 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
4251 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
4252 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004253 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
4254 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
4255 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004256 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
4257 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
4258 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004259 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
4260 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
4261 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08004262 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
4263 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
4264 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004265#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004266 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004267 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004268 reply_len = -1;
Sunil Ravi7f769292024-07-23 22:21:32 +00004269#ifdef CONFIG_IEEE80211AX
4270 } else if (os_strncmp(buf, "COLOR_CHANGE ", 13) == 0) {
4271 if (hostapd_ctrl_iface_color_change(hapd->iface, buf + 13))
4272 reply_len = -1;
4273#endif /* CONFIG_IEEE80211AX */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004274 } else if (os_strncmp(buf, "NOTIFY_CW_CHANGE ", 17) == 0) {
4275 if (hostapd_ctrl_iface_notify_cw_change(hapd, buf + 17))
4276 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004277 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
4278 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
4279 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004280 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
4281 ieee802_1x_erp_flush(hapd);
4282#ifdef RADIUS_SERVER
4283 radius_server_erp_flush(hapd->radius_srv);
4284#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004285 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
4286 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
4287 reply_len = -1;
4288 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
4289 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
4290 reply_len = -1;
4291 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4292 reply_len = hostapd_ctrl_iface_log_level(
4293 hapd, buf + 9, reply, reply_size);
4294#ifdef NEED_AP_MLME
4295 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
4296 reply_len = hostapd_ctrl_iface_track_sta_list(
4297 hapd, reply, reply_size);
4298#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07004299 } else if (os_strcmp(buf, "PMKSA") == 0) {
4300 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
4301 reply_size);
4302 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4303 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004304 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
4305 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
4306 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004307 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
4308 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
4309 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004310 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
4311 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
4312 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004313 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
4314 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
4315 reply_len = -1;
4316 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
4317 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
4318 reply_len = -1;
4319 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
4320 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
4321 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08004322 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
4323 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
4324 reply, reply_size);
Sunil Ravi99c035e2024-07-12 01:42:03 +00004325 } else if (os_strncmp(buf, "REQ_LINK_MEASUREMENT ", 21) == 0) {
4326 reply_len = hostapd_ctrl_iface_req_link_measurement(
4327 hapd, buf + 21, reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004328 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
4329 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
4330 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07004331 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
4332 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
4333 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08004334 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4335 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07004336 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
4337 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004338 if (hostapd_ctrl_iface_acl_add_mac(
4339 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004340 &hapd->conf->num_accept_mac, buf + 19) ||
4341 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004342 reply_len = -1;
4343 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004344 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004345 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004346 &hapd->conf->num_accept_mac, buf + 19) ||
4347 hostapd_set_acl(hapd) ||
4348 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07004349 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004350 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
4351 reply_len = hostapd_ctrl_iface_acl_show_mac(
4352 hapd->conf->accept_mac,
4353 hapd->conf->num_accept_mac, reply, reply_size);
4354 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
4355 hostapd_ctrl_iface_acl_clear_list(
4356 &hapd->conf->accept_mac,
4357 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004358 if (hostapd_set_acl(hapd) ||
4359 hostapd_disassoc_accept_mac(hapd))
4360 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004361 } else {
4362 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004363 }
4364 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
4365 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07004366 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07004367 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004368 &hapd->conf->num_deny_mac, buf + 17) ||
4369 hostapd_set_acl(hapd) ||
4370 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004371 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004372 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08004373 if (hostapd_ctrl_iface_acl_del_mac(
4374 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07004375 &hapd->conf->num_deny_mac, buf + 17) ||
4376 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08004377 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004378 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
4379 reply_len = hostapd_ctrl_iface_acl_show_mac(
4380 hapd->conf->deny_mac,
4381 hapd->conf->num_deny_mac, reply, reply_size);
4382 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
4383 hostapd_ctrl_iface_acl_clear_list(
4384 &hapd->conf->deny_mac,
4385 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07004386 if (hostapd_set_acl(hapd))
4387 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07004388 } else {
4389 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004390 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004391#ifdef CONFIG_DPP
4392 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
4393 res = hostapd_dpp_qr_code(hapd, buf + 12);
4394 if (res < 0) {
4395 reply_len = -1;
4396 } else {
4397 reply_len = os_snprintf(reply, reply_size, "%d", res);
4398 if (os_snprintf_error(reply_size, reply_len))
4399 reply_len = -1;
4400 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004401 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
4402 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
4403 if (res < 0) {
4404 reply_len = -1;
4405 } else {
4406 reply_len = os_snprintf(reply, reply_size, "%d", res);
4407 if (os_snprintf_error(reply_size, reply_len))
4408 reply_len = -1;
4409 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004410 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
4411 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
4412 if (res < 0) {
4413 reply_len = -1;
4414 } else {
4415 reply_len = os_snprintf(reply, reply_size, "%d", res);
4416 if (os_snprintf_error(reply_size, reply_len))
4417 reply_len = -1;
4418 }
4419 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
4420 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
4421 if (res < 0) {
4422 reply_len = -1;
4423 } else {
4424 reply_len = os_snprintf(reply, reply_size, "%d", res);
4425 if (os_snprintf_error(reply_size, reply_len))
4426 reply_len = -1;
4427 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004428 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004429 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004430 if (res < 0) {
4431 reply_len = -1;
4432 } else {
4433 reply_len = os_snprintf(reply, reply_size, "%d", res);
4434 if (os_snprintf_error(reply_size, reply_len))
4435 reply_len = -1;
4436 }
4437 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004438 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
4439 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004440 reply_len = -1;
4441 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
4442 const char *uri;
4443
Hai Shalom021b0b52019-04-10 11:17:58 -07004444 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
4445 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004446 if (!uri) {
4447 reply_len = -1;
4448 } else {
4449 reply_len = os_snprintf(reply, reply_size, "%s", uri);
4450 if (os_snprintf_error(reply_size, reply_len))
4451 reply_len = -1;
4452 }
4453 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004454 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
4455 atoi(buf + 19),
4456 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07004457 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
4458 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
4459 atoi(buf + 18),
4460 os_strchr(buf + 18, ' ')) < 0)
4461 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004462 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
4463 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
4464 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004465 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
4466 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
4467 reply_len = -1;
4468 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
4469 hostapd_dpp_stop(hapd);
4470 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004471 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004472 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
4473 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004474 if (res < 0) {
4475 reply_len = -1;
4476 } else {
4477 reply_len = os_snprintf(reply, reply_size, "%d", res);
4478 if (os_snprintf_error(reply_size, reply_len))
4479 reply_len = -1;
4480 }
Sunil Ravia04bd252022-05-02 22:54:18 -07004481 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
4482 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
4483 buf + 20) < 0)
4484 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004485 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004486 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
4487 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004488 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004489 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004490 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07004491 reply_len = -1;
4492 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07004493 reply_len = dpp_configurator_get_key_id(
4494 hapd->iface->interfaces->dpp,
4495 atoi(buf + 25),
4496 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004497 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
4498 res = hostapd_dpp_pkex_add(hapd, buf + 12);
4499 if (res < 0) {
4500 reply_len = -1;
4501 } else {
4502 reply_len = os_snprintf(reply, reply_size, "%d", res);
4503 if (os_snprintf_error(reply_size, reply_len))
4504 reply_len = -1;
4505 }
4506 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
4507 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
4508 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004509#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07004510 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
4511 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
4512 reply_len = -1;
4513 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
4514 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
4515 reply_len = -1;
4516 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
4517 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004518 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4519 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4520 reply_len = -1;
4521 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4522 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07004523 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
4524 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
4525 reply_len = -1;
4526 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
4527 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004528#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07004529#ifdef CONFIG_DPP3
4530 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
4531 if (hostapd_dpp_push_button(hapd, NULL) < 0)
4532 reply_len = -1;
4533 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
4534 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
4535 reply_len = -1;
4536#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004537#endif /* CONFIG_DPP */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004538#ifdef CONFIG_NAN_USD
4539 } else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
4540 reply_len = hostapd_ctrl_nan_publish(hapd, buf + 12, reply,
4541 reply_size);
4542 } else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
4543 if (hostapd_ctrl_nan_cancel_publish(hapd, buf + 19) < 0)
4544 reply_len = -1;
4545 } else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
4546 if (hostapd_ctrl_nan_update_publish(hapd, buf + 19) < 0)
4547 reply_len = -1;
4548 } else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
4549 reply_len = hostapd_ctrl_nan_subscribe(hapd, buf + 14, reply,
4550 reply_size);
4551 } else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
4552 if (hostapd_ctrl_nan_cancel_subscribe(hapd, buf + 21) < 0)
4553 reply_len = -1;
4554 } else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
4555 if (hostapd_ctrl_nan_transmit(hapd, buf + 13) < 0)
4556 reply_len = -1;
4557#endif /* CONFIG_NAN_USD */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004558#ifdef RADIUS_SERVER
4559 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4560 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4561 reply_len = -1;
4562#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07004563 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4564 reply_len = hostapd_ctrl_iface_get_capability(
4565 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08004566#ifdef CONFIG_PASN
4567 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4568 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4569#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08004570#ifdef ANDROID
4571 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4572 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4573 reply_size);
4574#endif /* ANDROID */
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004575#ifdef CONFIG_IEEE80211BE
4576 } else if (os_strcmp(buf, "ENABLE_MLD") == 0) {
4577 if (hostapd_ctrl_iface_enable_mld(hapd->iface))
4578 reply_len = -1;
4579 } else if (os_strcmp(buf, "DISABLE_MLD") == 0) {
4580 if (hostapd_ctrl_iface_disable_mld(hapd->iface))
4581 reply_len = -1;
4582#ifdef CONFIG_TESTING_OPTIONS
4583 } else if (os_strncmp(buf, "LINK_REMOVE ", 12) == 0) {
4584 if (hostapd_ctrl_iface_link_remove(hapd, buf + 12,
4585 reply, reply_size))
4586 reply_len = -1;
4587#endif /* CONFIG_TESTING_OPTIONS */
4588#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 } else {
4590 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4591 reply_len = 16;
4592 }
4593
4594 if (reply_len < 0) {
4595 os_memcpy(reply, "FAIL\n", 5);
4596 reply_len = 5;
4597 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004598
4599 return reply_len;
4600}
4601
4602
4603static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4604 void *sock_ctx)
4605{
4606 struct hostapd_data *hapd = eloop_ctx;
4607 char buf[4096];
4608 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004609 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004610 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004611 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004612 const int reply_size = 4096;
4613 int reply_len;
4614 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004615#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004616 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004617#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004618
4619 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4620 (struct sockaddr *) &from, &fromlen);
4621 if (res < 0) {
4622 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4623 strerror(errno));
4624 return;
4625 }
4626 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004627
4628 reply = os_malloc(reply_size);
4629 if (reply == NULL) {
4630 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4631 fromlen) < 0) {
4632 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4633 strerror(errno));
4634 }
4635 return;
4636 }
4637
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004638#ifdef CONFIG_CTRL_IFACE_UDP
4639 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4640 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004641 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4642 hapd->ctrl_iface_cookie,
4643 CTRL_IFACE_COOKIE_LEN);
4644 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004645 goto done;
4646 }
4647
4648 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004649 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004650 wpa_printf(MSG_DEBUG,
4651 "CTRL: No cookie in the request - drop request");
4652 os_free(reply);
4653 return;
4654 }
4655
Hai Shalomfdcde762020-04-02 11:19:20 -07004656 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4657 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004658 wpa_printf(MSG_DEBUG,
4659 "CTRL: Invalid cookie in the request - drop request");
4660 os_free(reply);
4661 return;
4662 }
4663
Hai Shalomfdcde762020-04-02 11:19:20 -07004664 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004665 while (*pos == ' ')
4666 pos++;
4667#endif /* CONFIG_CTRL_IFACE_UDP */
4668
4669 if (os_strcmp(pos, "PING") == 0)
4670 level = MSG_EXCESSIVE;
4671 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4672
4673 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004674 reply, reply_size,
4675 &from, fromlen);
4676
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004677#ifdef CONFIG_CTRL_IFACE_UDP
4678done:
4679#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004680 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4681 fromlen) < 0) {
4682 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4683 strerror(errno));
4684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 os_free(reply);
4686}
4687
4688
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004689#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004690static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
4691{
4692 char *buf;
4693 size_t len;
4694
4695 if (hapd->conf->ctrl_interface == NULL)
4696 return NULL;
4697
4698 len = os_strlen(hapd->conf->ctrl_interface) +
4699 os_strlen(hapd->conf->iface) + 2;
4700 buf = os_malloc(len);
4701 if (buf == NULL)
4702 return NULL;
4703
4704 os_snprintf(buf, len, "%s/%s",
4705 hapd->conf->ctrl_interface, hapd->conf->iface);
4706 buf[len - 1] = '\0';
4707 return buf;
4708}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004709#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710
4711
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004712static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
4713 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 const char *txt, size_t len)
4715{
4716 struct hostapd_data *hapd = ctx;
4717 if (hapd == NULL)
4718 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004719 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720}
4721
4722
4723int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
4724{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004725#ifdef CONFIG_CTRL_IFACE_UDP
4726 int port = HOSTAPD_CTRL_IFACE_PORT;
4727 char p[32] = { 0 };
4728 char port_str[40], *tmp;
4729 char *pos;
4730 struct addrinfo hints = { 0 }, *res, *saveres;
4731 int n;
4732
4733 if (hapd->ctrl_sock > -1) {
4734 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4735 return 0;
4736 }
4737
4738 if (hapd->conf->ctrl_interface == NULL)
4739 return 0;
4740
4741 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
4742 if (pos) {
4743 pos += 4;
4744 port = atoi(pos);
4745 if (port <= 0) {
4746 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
4747 goto fail;
4748 }
4749 }
4750
4751 dl_list_init(&hapd->ctrl_dst);
4752 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004753 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004754
4755#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4756 hints.ai_flags = AI_PASSIVE;
4757#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4758
4759#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4760 hints.ai_family = AF_INET6;
4761#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4762 hints.ai_family = AF_INET;
4763#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4764 hints.ai_socktype = SOCK_DGRAM;
4765
4766try_again:
4767 os_snprintf(p, sizeof(p), "%d", port);
4768 n = getaddrinfo(NULL, p, &hints, &res);
4769 if (n) {
4770 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4771 goto fail;
4772 }
4773
4774 saveres = res;
4775 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4776 res->ai_protocol);
4777 if (hapd->ctrl_sock < 0) {
4778 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4779 goto fail;
4780 }
4781
4782 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4783 port--;
4784 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4785 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4786 goto try_again;
4787 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4788 goto fail;
4789 }
4790
4791 freeaddrinfo(saveres);
4792
4793 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4794 tmp = os_strdup(port_str);
4795 if (tmp) {
4796 os_free(hapd->conf->ctrl_interface);
4797 hapd->conf->ctrl_interface = tmp;
4798 }
4799 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4800
4801 if (eloop_register_read_sock(hapd->ctrl_sock,
4802 hostapd_ctrl_iface_receive, hapd, NULL) <
4803 0) {
4804 hostapd_ctrl_iface_deinit(hapd);
4805 return -1;
4806 }
4807
4808 hapd->msg_ctx = hapd;
4809 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4810
4811 return 0;
4812
4813fail:
4814 if (hapd->ctrl_sock >= 0)
4815 close(hapd->ctrl_sock);
4816 return -1;
4817#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 struct sockaddr_un addr;
4819 int s = -1;
4820 char *fname = NULL;
4821
Dmitry Shmidt04949592012-07-19 12:16:46 -07004822 if (hapd->ctrl_sock > -1) {
4823 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4824 return 0;
4825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004827 dl_list_init(&hapd->ctrl_dst);
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 if (hapd->conf->ctrl_interface == NULL)
4830 return 0;
4831
4832 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4833 if (errno == EEXIST) {
4834 wpa_printf(MSG_DEBUG, "Using existing control "
4835 "interface directory.");
4836 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004837 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4838 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004839 goto fail;
4840 }
4841 }
4842
4843 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004844 lchown(hapd->conf->ctrl_interface, -1,
4845 hapd->conf->ctrl_interface_gid) < 0) {
4846 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004847 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 return -1;
4849 }
4850
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004851 if (!hapd->conf->ctrl_interface_gid_set &&
4852 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004853 lchown(hapd->conf->ctrl_interface, -1,
4854 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4855 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004856 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004857 return -1;
4858 }
4859
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004860#ifdef ANDROID
4861 /*
4862 * Android is using umask 0077 which would leave the control interface
4863 * directory without group access. This breaks things since Wi-Fi
4864 * framework assumes that this directory can be accessed by other
4865 * applications in the wifi group. Fix this by adding group access even
4866 * if umask value would prevent this.
4867 */
4868 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4869 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4870 strerror(errno));
4871 /* Try to continue anyway */
4872 }
4873#endif /* ANDROID */
4874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4876 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4877 goto fail;
4878
4879 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4880 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004881 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882 goto fail;
4883 }
4884
4885 os_memset(&addr, 0, sizeof(addr));
4886#ifdef __FreeBSD__
4887 addr.sun_len = sizeof(addr);
4888#endif /* __FreeBSD__ */
4889 addr.sun_family = AF_UNIX;
4890 fname = hostapd_ctrl_iface_path(hapd);
4891 if (fname == NULL)
4892 goto fail;
4893 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4894 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4895 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4896 strerror(errno));
4897 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4898 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4899 " allow connections - assuming it was left"
4900 "over from forced program termination");
4901 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004902 wpa_printf(MSG_ERROR,
4903 "Could not unlink existing ctrl_iface socket '%s': %s",
4904 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 goto fail;
4906 }
4907 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4908 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004909 wpa_printf(MSG_ERROR,
4910 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4911 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 goto fail;
4913 }
4914 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4915 "ctrl_iface socket '%s'", fname);
4916 } else {
4917 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4918 "be in use - cannot override it");
4919 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4920 "not used anymore", fname);
4921 os_free(fname);
4922 fname = NULL;
4923 goto fail;
4924 }
4925 }
4926
4927 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004928 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4929 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004930 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 goto fail;
4932 }
4933
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004934 if (!hapd->conf->ctrl_interface_gid_set &&
4935 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004936 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4937 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004938 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004939 goto fail;
4940 }
4941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004943 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4944 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 goto fail;
4946 }
4947 os_free(fname);
4948
4949 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004950 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4951 NULL) < 0) {
4952 hostapd_ctrl_iface_deinit(hapd);
4953 return -1;
4954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 hapd->msg_ctx = hapd;
4956 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4957
4958 return 0;
4959
4960fail:
4961 if (s >= 0)
4962 close(s);
4963 if (fname) {
4964 unlink(fname);
4965 os_free(fname);
4966 }
4967 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004968#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969}
4970
4971
4972void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4973{
4974 struct wpa_ctrl_dst *dst, *prev;
4975
4976 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004977#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004979#endif /* !CONFIG_CTRL_IFACE_UDP */
4980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 eloop_unregister_read_sock(hapd->ctrl_sock);
4982 close(hapd->ctrl_sock);
4983 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004984#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985 fname = hostapd_ctrl_iface_path(hapd);
4986 if (fname)
4987 unlink(fname);
4988 os_free(fname);
4989
4990 if (hapd->conf->ctrl_interface &&
4991 rmdir(hapd->conf->ctrl_interface) < 0) {
4992 if (errno == ENOTEMPTY) {
4993 wpa_printf(MSG_DEBUG, "Control interface "
4994 "directory not empty - leaving it "
4995 "behind");
4996 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004997 wpa_printf(MSG_ERROR,
4998 "rmdir[ctrl_interface=%s]: %s",
4999 hapd->conf->ctrl_interface,
5000 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 }
5002 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005003#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004 }
5005
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005006 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
5007 list)
5008 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005009
5010#ifdef CONFIG_TESTING_OPTIONS
5011 l2_packet_deinit(hapd->l2_test);
5012 hapd->l2_test = NULL;
5013#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014}
5015
5016
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005017static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
5018 char *buf)
5019{
5020 if (hostapd_add_iface(interfaces, buf) < 0) {
5021 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
5022 return -1;
5023 }
5024 return 0;
5025}
5026
5027
5028static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
5029 char *buf)
5030{
5031 if (hostapd_remove_iface(interfaces, buf) < 0) {
5032 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
5033 return -1;
5034 }
5035 return 0;
5036}
5037
5038
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005039static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005040 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005041 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005042{
Roshan Pius3a1667e2018-07-03 15:17:14 -07005043 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
5044 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005045}
5046
5047
5048static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005049 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005050 socklen_t fromlen)
5051{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005052 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005053}
5054
5055
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005056static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
5057{
5058#ifdef CONFIG_WPS_TESTING
5059 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005060 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005061 wps_corrupt_pkhash = 0;
5062#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005063
5064#ifdef CONFIG_TESTING_OPTIONS
5065#ifdef CONFIG_DPP
5066 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08005067#ifdef CONFIG_DPP3
5068 dpp_version_override = 3;
5069#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07005070 dpp_version_override = 2;
5071#else /* CONFIG_DPP2 */
5072 dpp_version_override = 1;
5073#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005074#endif /* CONFIG_DPP */
5075#endif /* CONFIG_TESTING_OPTIONS */
5076
5077#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07005078 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07005079#ifdef CONFIG_DPP3
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005080 interfaces->dpp_pb_bi = NULL;
Sunil Ravi89eba102022-09-13 21:04:37 -07005081 {
5082 int i;
5083
5084 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
5085 struct dpp_pb_info *info;
5086
5087 info = &interfaces->dpp_pb[i];
5088 info->rx_time.sec = 0;
5089 info->rx_time.usec = 0;
5090 }
5091 }
5092#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07005093#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005094}
5095
5096
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005097#ifdef CONFIG_FST
5098
5099static int
5100hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
5101 const char *cmd)
5102{
5103 char ifname[IFNAMSIZ + 1];
5104 struct fst_iface_cfg cfg;
5105 struct hostapd_data *hapd;
5106 struct fst_wpa_obj iface_obj;
5107
5108 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
5109 hapd = hostapd_get_iface(interfaces, ifname);
5110 if (hapd) {
5111 if (hapd->iface->fst) {
5112 wpa_printf(MSG_INFO, "FST: Already attached");
5113 return -1;
5114 }
5115 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
5116 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
5117 &iface_obj, &cfg);
5118 if (hapd->iface->fst)
5119 return 0;
5120 }
5121 }
5122
5123 return -EINVAL;
5124}
5125
5126
5127static int
5128hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
5129 const char *cmd)
5130{
5131 char ifname[IFNAMSIZ + 1];
5132 struct hostapd_data * hapd;
5133
5134 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
5135 hapd = hostapd_get_iface(interfaces, ifname);
5136 if (hapd) {
5137 if (!fst_iface_detach(ifname)) {
5138 hapd->iface->fst = NULL;
5139 hapd->iface->fst_ies = NULL;
5140 return 0;
5141 }
5142 }
5143 }
5144
5145 return -EINVAL;
5146}
5147
5148#endif /* CONFIG_FST */
5149
5150
5151static struct hostapd_data *
5152hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
5153 const char *ifname)
5154{
5155 size_t i, j;
5156
5157 for (i = 0; i < interfaces->count; i++) {
5158 struct hostapd_iface *iface = interfaces->iface[i];
5159
5160 for (j = 0; j < iface->num_bss; j++) {
5161 struct hostapd_data *hapd;
5162
5163 hapd = iface->bss[j];
5164 if (os_strcmp(ifname, hapd->conf->iface) == 0)
5165 return hapd;
5166 }
5167 }
5168
5169 return NULL;
5170}
5171
5172
5173static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
5174 struct hostapd_data *dst_hapd,
5175 const char *param)
5176{
5177 int res;
5178 char *value;
5179
5180 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5181 if (!value) {
5182 wpa_printf(MSG_ERROR,
5183 "DUP: cannot allocate buffer to stringify %s",
5184 param);
5185 goto error_return;
5186 }
5187
5188 if (os_strcmp(param, "wpa") == 0) {
5189 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
5190 src_hapd->conf->wpa);
5191 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
5192 src_hapd->conf->wpa_key_mgmt) {
5193 res = hostapd_ctrl_iface_get_key_mgmt(
5194 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
5195 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
5196 goto error_stringify;
5197 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
5198 src_hapd->conf->wpa_pairwise) {
5199 res = wpa_write_ciphers(value,
5200 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5201 src_hapd->conf->wpa_pairwise, " ");
5202 if (res < 0)
5203 goto error_stringify;
5204 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
5205 src_hapd->conf->rsn_pairwise) {
5206 res = wpa_write_ciphers(value,
5207 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5208 src_hapd->conf->rsn_pairwise, " ");
5209 if (res < 0)
5210 goto error_stringify;
5211 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
5212 src_hapd->conf->ssid.wpa_passphrase) {
5213 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
5214 src_hapd->conf->ssid.wpa_passphrase);
5215 } else if (os_strcmp(param, "wpa_psk") == 0 &&
5216 src_hapd->conf->ssid.wpa_psk_set) {
5217 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
5218 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
5219 } else {
5220 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
5221 goto error_return;
5222 }
5223
5224 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
5225 os_free(value);
5226 return res;
5227
5228error_stringify:
5229 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
5230error_return:
5231 os_free(value);
5232 return -1;
5233}
5234
5235
5236static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005237hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
5238 const char *input,
5239 char *reply, int reply_size)
5240{
5241 size_t i, j;
5242 int res;
5243 char *pos, *end;
5244 struct hostapd_iface *iface;
5245 int show_ctrl = 0;
5246
5247 if (input)
5248 show_ctrl = !!os_strstr(input, "ctrl");
5249
5250 pos = reply;
5251 end = reply + reply_size;
5252
5253 for (i = 0; i < interfaces->count; i++) {
5254 iface = interfaces->iface[i];
5255
5256 for (j = 0; j < iface->num_bss; j++) {
5257 struct hostapd_bss_config *conf;
5258
5259 conf = iface->conf->bss[j];
5260 if (show_ctrl)
5261 res = os_snprintf(pos, end - pos,
5262 "%s ctrl_iface=%s\n",
5263 conf->iface,
5264 conf->ctrl_interface ?
5265 conf->ctrl_interface : "N/A");
5266 else
5267 res = os_snprintf(pos, end - pos, "%s\n",
5268 conf->iface);
5269 if (os_snprintf_error(end - pos, res)) {
5270 *pos = '\0';
5271 return pos - reply;
5272 }
5273 pos += res;
5274 }
5275 }
5276
5277 return pos - reply;
5278}
5279
5280
5281static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005282hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
5283 char *cmd)
5284{
5285 char *p_start = cmd, *p_end;
5286 struct hostapd_data *src_hapd, *dst_hapd;
5287
5288 /* cmd: "<src ifname> <dst ifname> <variable name> */
5289
5290 p_end = os_strchr(p_start, ' ');
5291 if (!p_end) {
5292 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
5293 cmd);
5294 return -1;
5295 }
5296
5297 *p_end = '\0';
5298 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5299 if (!src_hapd) {
5300 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
5301 p_start);
5302 return -1;
5303 }
5304
5305 p_start = p_end + 1;
5306 p_end = os_strchr(p_start, ' ');
5307 if (!p_end) {
5308 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
5309 cmd);
5310 return -1;
5311 }
5312
5313 *p_end = '\0';
5314 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
5315 if (!dst_hapd) {
5316 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
5317 p_start);
5318 return -1;
5319 }
5320
5321 p_start = p_end + 1;
5322 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
5323}
5324
5325
5326static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
5327 const char *ifname,
5328 char *buf, char *reply,
5329 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005330 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005331 socklen_t fromlen)
5332{
5333 struct hostapd_data *hapd;
5334
5335 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
5336 if (hapd == NULL) {
5337 int res;
5338
5339 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
5340 if (os_snprintf_error(reply_size, res))
5341 return -1;
5342 return res;
5343 }
5344
5345 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
5346 from, fromlen);
5347}
5348
5349
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005350static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
5351 void *sock_ctx)
5352{
Hai Shalomfdcde762020-04-02 11:19:20 -07005353 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005354 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005355 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005356 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005357 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005358 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005359 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005360 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005361#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07005362 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005363#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005364
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005365 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005366 (struct sockaddr *) &from, &fromlen);
5367 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005368 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
5369 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005370 return;
5371 }
5372 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005373 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005374
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005375 reply = os_malloc(reply_size);
5376 if (reply == NULL) {
5377 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
5378 fromlen) < 0) {
5379 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5380 strerror(errno));
5381 }
5382 return;
5383 }
5384
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005385 os_memcpy(reply, "OK\n", 3);
5386 reply_len = 3;
5387
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005388#ifdef CONFIG_CTRL_IFACE_UDP
5389 if (os_strcmp(buf, "GET_COOKIE") == 0) {
5390 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07005391 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
5392 interfaces->ctrl_iface_cookie,
5393 CTRL_IFACE_COOKIE_LEN);
5394 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005395 goto send_reply;
5396 }
5397
5398 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07005399 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005400 wpa_printf(MSG_DEBUG,
5401 "CTRL: No cookie in the request - drop request");
5402 os_free(reply);
5403 return;
5404 }
5405
Hai Shalomfdcde762020-04-02 11:19:20 -07005406 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
5407 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005408 wpa_printf(MSG_DEBUG,
5409 "CTRL: Invalid cookie in the request - drop request");
5410 os_free(reply);
5411 return;
5412 }
5413
Hai Shalomfdcde762020-04-02 11:19:20 -07005414 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005415 while (*buf == ' ')
5416 buf++;
5417#endif /* CONFIG_CTRL_IFACE_UDP */
5418
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005419 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
5420 char *pos = os_strchr(buf + 7, ' ');
5421
5422 if (pos) {
5423 *pos++ = '\0';
5424 reply_len = hostapd_global_ctrl_iface_ifname(
5425 interfaces, buf + 7, pos, reply, reply_size,
5426 &from, fromlen);
5427 goto send_reply;
5428 }
5429 }
5430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005431 if (os_strcmp(buf, "PING") == 0) {
5432 os_memcpy(reply, "PONG\n", 5);
5433 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005434 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
5435 if (wpa_debug_reopen_file() < 0)
5436 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005437 } else if (os_strcmp(buf, "FLUSH") == 0) {
5438 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005439 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
5440 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
5441 reply_len = -1;
5442 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
5443 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
5444 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005445 } else if (os_strcmp(buf, "ATTACH") == 0) {
5446 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07005447 fromlen, NULL))
5448 reply_len = -1;
5449 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
5450 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
5451 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005452 reply_len = -1;
5453 } else if (os_strcmp(buf, "DETACH") == 0) {
5454 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
5455 fromlen))
5456 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005457#ifdef CONFIG_MODULE_TESTS
5458 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005459 if (hapd_module_tests() < 0)
5460 reply_len = -1;
5461#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005462#ifdef CONFIG_FST
5463 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
5464 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
5465 reply_len = os_snprintf(reply, reply_size, "OK\n");
5466 else
5467 reply_len = -1;
5468 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
5469 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
5470 reply_len = os_snprintf(reply, reply_size, "OK\n");
5471 else
5472 reply_len = -1;
5473 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
5474 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
5475#endif /* CONFIG_FST */
5476 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
5477 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
5478 buf + 12))
5479 reply_len = os_snprintf(reply, reply_size, "OK\n");
5480 else
5481 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005482 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
5483 reply_len = hostapd_global_ctrl_iface_interfaces(
Sunil Ravi99c035e2024-07-12 01:42:03 +00005484 interfaces, buf + 10, reply, reply_size);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005485 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5486 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005487 } else {
5488 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
5489 "ignored");
5490 reply_len = -1;
5491 }
5492
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005493send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005494 if (reply_len < 0) {
5495 os_memcpy(reply, "FAIL\n", 5);
5496 reply_len = 5;
5497 }
5498
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005499 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
5500 fromlen) < 0) {
5501 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
5502 strerror(errno));
5503 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005504 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005505}
5506
5507
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005508#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005509static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
5510{
5511 char *buf;
5512 size_t len;
5513
5514 if (interface->global_iface_path == NULL)
5515 return NULL;
5516
5517 len = os_strlen(interface->global_iface_path) +
5518 os_strlen(interface->global_iface_name) + 2;
5519 buf = os_malloc(len);
5520 if (buf == NULL)
5521 return NULL;
5522
5523 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
5524 interface->global_iface_name);
5525 buf[len - 1] = '\0';
5526 return buf;
5527}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005528#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005529
5530
5531int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
5532{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005533#ifdef CONFIG_CTRL_IFACE_UDP
5534 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
5535 char p[32] = { 0 };
5536 char *pos;
5537 struct addrinfo hints = { 0 }, *res, *saveres;
5538 int n;
5539
5540 if (interface->global_ctrl_sock > -1) {
5541 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
5542 return 0;
5543 }
5544
5545 if (interface->global_iface_path == NULL)
5546 return 0;
5547
5548 pos = os_strstr(interface->global_iface_path, "udp:");
5549 if (pos) {
5550 pos += 4;
5551 port = atoi(pos);
5552 if (port <= 0) {
5553 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
5554 goto fail;
5555 }
5556 }
5557
Hai Shalomfdcde762020-04-02 11:19:20 -07005558 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005559
5560#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
5561 hints.ai_flags = AI_PASSIVE;
5562#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
5563
5564#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
5565 hints.ai_family = AF_INET6;
5566#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5567 hints.ai_family = AF_INET;
5568#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
5569 hints.ai_socktype = SOCK_DGRAM;
5570
5571try_again:
5572 os_snprintf(p, sizeof(p), "%d", port);
5573 n = getaddrinfo(NULL, p, &hints, &res);
5574 if (n) {
5575 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
5576 goto fail;
5577 }
5578
5579 saveres = res;
5580 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
5581 res->ai_protocol);
5582 if (interface->global_ctrl_sock < 0) {
5583 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5584 goto fail;
5585 }
5586
5587 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
5588 0) {
5589 port++;
5590 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
5591 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
5592 goto try_again;
5593 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5594 goto fail;
5595 }
5596
5597 freeaddrinfo(saveres);
5598
5599 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
5600
5601 if (eloop_register_read_sock(interface->global_ctrl_sock,
5602 hostapd_global_ctrl_iface_receive,
5603 interface, NULL) < 0) {
5604 hostapd_global_ctrl_iface_deinit(interface);
5605 return -1;
5606 }
5607
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005608 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5609
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005610 return 0;
5611
5612fail:
5613 if (interface->global_ctrl_sock >= 0)
5614 close(interface->global_ctrl_sock);
5615 return -1;
5616#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005617 struct sockaddr_un addr;
5618 int s = -1;
5619 char *fname = NULL;
5620
5621 if (interface->global_iface_path == NULL) {
5622 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
5623 return 0;
5624 }
5625
5626 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
5627 if (errno == EEXIST) {
5628 wpa_printf(MSG_DEBUG, "Using existing control "
5629 "interface directory.");
5630 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005631 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5632 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005633 goto fail;
5634 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005635 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005636 lchown(interface->global_iface_path, -1,
5637 interface->ctrl_iface_group) < 0) {
5638 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005640 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005641 }
5642
5643 if (os_strlen(interface->global_iface_path) + 1 +
5644 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
5645 goto fail;
5646
5647 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5648 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005649 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005650 goto fail;
5651 }
5652
5653 os_memset(&addr, 0, sizeof(addr));
5654#ifdef __FreeBSD__
5655 addr.sun_len = sizeof(addr);
5656#endif /* __FreeBSD__ */
5657 addr.sun_family = AF_UNIX;
5658 fname = hostapd_global_ctrl_iface_path(interface);
5659 if (fname == NULL)
5660 goto fail;
5661 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5662 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5663 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5664 strerror(errno));
5665 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5666 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5667 " allow connections - assuming it was left"
5668 "over from forced program termination");
5669 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005670 wpa_printf(MSG_ERROR,
5671 "Could not unlink existing ctrl_iface socket '%s': %s",
5672 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005673 goto fail;
5674 }
5675 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5676 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005677 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
5678 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005679 goto fail;
5680 }
5681 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5682 "ctrl_iface socket '%s'", fname);
5683 } else {
5684 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5685 "be in use - cannot override it");
5686 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5687 "not used anymore", fname);
5688 os_free(fname);
5689 fname = NULL;
5690 goto fail;
5691 }
5692 }
5693
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005694 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005695 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
5696 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005697 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005698 goto fail;
5699 }
5700
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005701 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005702 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5703 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005704 goto fail;
5705 }
5706 os_free(fname);
5707
5708 interface->global_ctrl_sock = s;
5709 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
5710 interface, NULL);
5711
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005712 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5713
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005714 return 0;
5715
5716fail:
5717 if (s >= 0)
5718 close(s);
5719 if (fname) {
5720 unlink(fname);
5721 os_free(fname);
5722 }
5723 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005724#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005725}
5726
5727
5728void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
5729{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005730#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005731 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005732#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005733 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005734
5735 if (interfaces->global_ctrl_sock > -1) {
5736 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
5737 close(interfaces->global_ctrl_sock);
5738 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005739#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005740 fname = hostapd_global_ctrl_iface_path(interfaces);
5741 if (fname) {
5742 unlink(fname);
5743 os_free(fname);
5744 }
5745
5746 if (interfaces->global_iface_path &&
5747 rmdir(interfaces->global_iface_path) < 0) {
5748 if (errno == ENOTEMPTY) {
5749 wpa_printf(MSG_DEBUG, "Control interface "
5750 "directory not empty - leaving it "
5751 "behind");
5752 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005753 wpa_printf(MSG_ERROR,
5754 "rmdir[ctrl_interface=%s]: %s",
5755 interfaces->global_iface_path,
5756 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005757 }
5758 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005759#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005760 }
5761
5762 os_free(interfaces->global_iface_path);
5763 interfaces->global_iface_path = NULL;
5764
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005765 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5766 struct wpa_ctrl_dst, list)
5767 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005768}
5769
5770
Roshan Pius3a1667e2018-07-03 15:17:14 -07005771static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5772 const char *buf)
5773{
5774 /* Enable Probe Request events based on explicit request.
5775 * Other events are enabled by default.
5776 */
5777 if (str_starts(buf, RX_PROBE_REQUEST))
5778 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5779 return 1;
5780}
5781
5782
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005783static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5784 const char *ifname, int level,
5785 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005786{
5787 struct wpa_ctrl_dst *dst, *next;
5788 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005789 int idx, res;
5790 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 char levelstr[10];
5792
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005793 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 return;
5795
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005796 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5797 if (os_snprintf_error(sizeof(levelstr), res))
5798 return;
5799 idx = 0;
5800 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005801 io[idx].iov_base = "IFNAME=";
5802 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005803 idx++;
5804 io[idx].iov_base = (char *) ifname;
5805 io[idx].iov_len = os_strlen(ifname);
5806 idx++;
5807 io[idx].iov_base = " ";
5808 io[idx].iov_len = 1;
5809 idx++;
5810 }
5811 io[idx].iov_base = levelstr;
5812 io[idx].iov_len = os_strlen(levelstr);
5813 idx++;
5814 io[idx].iov_base = (char *) buf;
5815 io[idx].iov_len = len;
5816 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817 os_memset(&msg, 0, sizeof(msg));
5818 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005819 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820
5821 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005822 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005823 if ((level >= dst->debug_level) &&
5824 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005825 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5826 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 msg.msg_name = &dst->addr;
5828 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005829 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830 int _errno = errno;
5831 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5832 "%d - %s",
5833 idx, errno, strerror(errno));
5834 dst->errors++;
5835 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005836 ctrl_iface_detach(ctrl_dst,
5837 &dst->addr,
5838 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005839 }
5840 } else
5841 dst->errors = 0;
5842 }
5843 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005844 }
5845}
5846
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005847
5848static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5849 enum wpa_msg_type type,
5850 const char *buf, size_t len)
5851{
5852 if (type != WPA_MSG_NO_GLOBAL) {
5853 hostapd_ctrl_iface_send_internal(
5854 hapd->iface->interfaces->global_ctrl_sock,
5855 &hapd->iface->interfaces->global_ctrl_dst,
5856 type != WPA_MSG_PER_INTERFACE ?
5857 NULL : hapd->conf->iface,
5858 level, buf, len);
5859 }
5860
5861 if (type != WPA_MSG_ONLY_GLOBAL) {
5862 hostapd_ctrl_iface_send_internal(
5863 hapd->ctrl_sock, &hapd->ctrl_dst,
5864 NULL, level, buf, len);
5865 }
5866}
5867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868#endif /* CONFIG_NATIVE_WINDOWS */