blob: b46d92102cf50a36365a77ab5905df3073c9d3db [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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080042#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080046#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080047#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#include "ap/hostapd.h"
49#include "ap/ap_config.h"
50#include "ap/ieee802_1x.h"
51#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070052#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053#include "ap/ieee802_11.h"
54#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055#include "ap/wps_hostapd.h"
56#include "ap/ctrl_iface_ap.h"
57#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080058#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080059#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070060#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080061#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070062#include "ap/neighbor_db.h"
63#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070064#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070065#include "ap/dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066#include "wps/wps_defs.h"
67#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080068#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070069#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070#include "ctrl_iface.h"
71
72
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080073#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
74
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080075#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080076#define HOSTAPD_CTRL_IFACE_PORT 8877
77#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
78#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
79#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
80#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081
82static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020083 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 const char *buf, size_t len);
85
86
87static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080088 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070089 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090{
Roshan Pius3a1667e2018-07-03 15:17:14 -070091 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092}
93
94
95static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080096 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 socklen_t fromlen)
98{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080099 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100}
101
102
103static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800104 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 socklen_t fromlen,
106 char *level)
107{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800108 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109}
110
111
112static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
113 const char *txtaddr)
114{
115 u8 addr[ETH_ALEN];
116 struct sta_info *sta;
117
118 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
119
120 if (hwaddr_aton(txtaddr, addr))
121 return -1;
122
123 sta = ap_get_sta(hapd, addr);
124 if (sta)
125 return 0;
126
127 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
128 "notification", MAC2STR(addr));
129 sta = ap_sta_add(hapd, addr);
130 if (sta == NULL)
131 return -1;
132
133 hostapd_new_assoc_sta(hapd, sta, 0);
134 return 0;
135}
136
137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138#ifdef NEED_AP_MLME
139static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
140 const char *txtaddr)
141{
142 u8 addr[ETH_ALEN];
143 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
144
145 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
146
147 if (hwaddr_aton(txtaddr, addr) ||
148 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
149 return -1;
150
151 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
152
153 return 0;
154}
155#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156
157
158#ifdef CONFIG_WPS
159static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
160{
161 char *pin = os_strchr(txt, ' ');
162 char *timeout_txt;
163 int timeout;
164 u8 addr_buf[ETH_ALEN], *addr = NULL;
165 char *pos;
166
167 if (pin == NULL)
168 return -1;
169 *pin++ = '\0';
170
171 timeout_txt = os_strchr(pin, ' ');
172 if (timeout_txt) {
173 *timeout_txt++ = '\0';
174 timeout = atoi(timeout_txt);
175 pos = os_strchr(timeout_txt, ' ');
176 if (pos) {
177 *pos++ = '\0';
178 if (hwaddr_aton(pos, addr_buf) == 0)
179 addr = addr_buf;
180 }
181 } else
182 timeout = 0;
183
184 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
185}
186
187
188static int hostapd_ctrl_iface_wps_check_pin(
189 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
190{
191 char pin[9];
192 size_t len;
193 char *pos;
194 int ret;
195
196 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
197 (u8 *) cmd, os_strlen(cmd));
198 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
199 if (*pos < '0' || *pos > '9')
200 continue;
201 pin[len++] = *pos;
202 if (len == 9) {
203 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
204 return -1;
205 }
206 }
207 if (len != 4 && len != 8) {
208 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
209 return -1;
210 }
211 pin[len] = '\0';
212
213 if (len == 8) {
214 unsigned int pin_val;
215 pin_val = atoi(pin);
216 if (!wps_pin_valid(pin_val)) {
217 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
218 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800219 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 return -1;
221 return ret;
222 }
223 }
224
225 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800226 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 return -1;
228
229 return ret;
230}
231
232
Dmitry Shmidt04949592012-07-19 12:16:46 -0700233#ifdef CONFIG_WPS_NFC
234static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
235 char *pos)
236{
237 size_t len;
238 struct wpabuf *buf;
239 int ret;
240
241 len = os_strlen(pos);
242 if (len & 0x01)
243 return -1;
244 len /= 2;
245
246 buf = wpabuf_alloc(len);
247 if (buf == NULL)
248 return -1;
249 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
250 wpabuf_free(buf);
251 return -1;
252 }
253
254 ret = hostapd_wps_nfc_tag_read(hapd, buf);
255 wpabuf_free(buf);
256
257 return ret;
258}
259
260
261static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
262 char *cmd, char *reply,
263 size_t max_len)
264{
265 int ndef;
266 struct wpabuf *buf;
267 int res;
268
269 if (os_strcmp(cmd, "WPS") == 0)
270 ndef = 0;
271 else if (os_strcmp(cmd, "NDEF") == 0)
272 ndef = 1;
273 else
274 return -1;
275
276 buf = hostapd_wps_nfc_config_token(hapd, ndef);
277 if (buf == NULL)
278 return -1;
279
280 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
281 wpabuf_len(buf));
282 reply[res++] = '\n';
283 reply[res] = '\0';
284
285 wpabuf_free(buf);
286
287 return res;
288}
289
290
291static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
292 char *reply, size_t max_len,
293 int ndef)
294{
295 struct wpabuf *buf;
296 int res;
297
298 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
299 if (buf == NULL)
300 return -1;
301
302 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
303 wpabuf_len(buf));
304 reply[res++] = '\n';
305 reply[res] = '\0';
306
307 wpabuf_free(buf);
308
309 return res;
310}
311
312
313static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
314 char *cmd, char *reply,
315 size_t max_len)
316{
317 if (os_strcmp(cmd, "WPS") == 0)
318 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
319 max_len, 0);
320
321 if (os_strcmp(cmd, "NDEF") == 0)
322 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
323 max_len, 1);
324
325 if (os_strcmp(cmd, "enable") == 0)
326 return hostapd_wps_nfc_token_enable(hapd);
327
328 if (os_strcmp(cmd, "disable") == 0) {
329 hostapd_wps_nfc_token_disable(hapd);
330 return 0;
331 }
332
333 return -1;
334}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800335
336
337static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
338 char *cmd, char *reply,
339 size_t max_len)
340{
341 struct wpabuf *buf;
342 int res;
343 char *pos;
344 int ndef;
345
346 pos = os_strchr(cmd, ' ');
347 if (pos == NULL)
348 return -1;
349 *pos++ = '\0';
350
351 if (os_strcmp(cmd, "WPS") == 0)
352 ndef = 0;
353 else if (os_strcmp(cmd, "NDEF") == 0)
354 ndef = 1;
355 else
356 return -1;
357
358 if (os_strcmp(pos, "WPS-CR") == 0)
359 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
360 else
361 buf = NULL;
362 if (buf == NULL)
363 return -1;
364
365 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
366 wpabuf_len(buf));
367 reply[res++] = '\n';
368 reply[res] = '\0';
369
370 wpabuf_free(buf);
371
372 return res;
373}
374
375
376static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
377 char *cmd)
378{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800379 size_t len;
380 struct wpabuf *req, *sel;
381 int ret;
382 char *pos, *role, *type, *pos2;
383
384 role = cmd;
385 pos = os_strchr(role, ' ');
386 if (pos == NULL)
387 return -1;
388 *pos++ = '\0';
389
390 type = pos;
391 pos = os_strchr(type, ' ');
392 if (pos == NULL)
393 return -1;
394 *pos++ = '\0';
395
396 pos2 = os_strchr(pos, ' ');
397 if (pos2 == NULL)
398 return -1;
399 *pos2++ = '\0';
400
401 len = os_strlen(pos);
402 if (len & 0x01)
403 return -1;
404 len /= 2;
405
406 req = wpabuf_alloc(len);
407 if (req == NULL)
408 return -1;
409 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
410 wpabuf_free(req);
411 return -1;
412 }
413
414 len = os_strlen(pos2);
415 if (len & 0x01) {
416 wpabuf_free(req);
417 return -1;
418 }
419 len /= 2;
420
421 sel = wpabuf_alloc(len);
422 if (sel == NULL) {
423 wpabuf_free(req);
424 return -1;
425 }
426 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
427 wpabuf_free(req);
428 wpabuf_free(sel);
429 return -1;
430 }
431
432 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
433 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
434 } else {
435 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
436 "reported: role=%s type=%s", role, type);
437 ret = -1;
438 }
439 wpabuf_free(req);
440 wpabuf_free(sel);
441
442 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800443}
444
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445#endif /* CONFIG_WPS_NFC */
446
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
449 char *buf, size_t buflen)
450{
451 int timeout = 300;
452 char *pos;
453 const char *pin_txt;
454
455 pos = os_strchr(txt, ' ');
456 if (pos)
457 *pos++ = '\0';
458
459 if (os_strcmp(txt, "disable") == 0) {
460 hostapd_wps_ap_pin_disable(hapd);
461 return os_snprintf(buf, buflen, "OK\n");
462 }
463
464 if (os_strcmp(txt, "random") == 0) {
465 if (pos)
466 timeout = atoi(pos);
467 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
468 if (pin_txt == NULL)
469 return -1;
470 return os_snprintf(buf, buflen, "%s", pin_txt);
471 }
472
473 if (os_strcmp(txt, "get") == 0) {
474 pin_txt = hostapd_wps_ap_pin_get(hapd);
475 if (pin_txt == NULL)
476 return -1;
477 return os_snprintf(buf, buflen, "%s", pin_txt);
478 }
479
480 if (os_strcmp(txt, "set") == 0) {
481 char *pin;
482 if (pos == NULL)
483 return -1;
484 pin = pos;
485 pos = os_strchr(pos, ' ');
486 if (pos) {
487 *pos++ = '\0';
488 timeout = atoi(pos);
489 }
490 if (os_strlen(pin) > buflen)
491 return -1;
492 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
493 return -1;
494 return os_snprintf(buf, buflen, "%s", pin);
495 }
496
497 return -1;
498}
499
500
501static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
502{
503 char *pos;
504 char *ssid, *auth, *encr = NULL, *key = NULL;
505
506 ssid = txt;
507 pos = os_strchr(txt, ' ');
508 if (!pos)
509 return -1;
510 *pos++ = '\0';
511
512 auth = pos;
513 pos = os_strchr(pos, ' ');
514 if (pos) {
515 *pos++ = '\0';
516 encr = pos;
517 pos = os_strchr(pos, ' ');
518 if (pos) {
519 *pos++ = '\0';
520 key = pos;
521 }
522 }
523
524 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
525}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700526
527
528static const char * pbc_status_str(enum pbc_status status)
529{
530 switch (status) {
531 case WPS_PBC_STATUS_DISABLE:
532 return "Disabled";
533 case WPS_PBC_STATUS_ACTIVE:
534 return "Active";
535 case WPS_PBC_STATUS_TIMEOUT:
536 return "Timed-out";
537 case WPS_PBC_STATUS_OVERLAP:
538 return "Overlap";
539 default:
540 return "Unknown";
541 }
542}
543
544
545static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
546 char *buf, size_t buflen)
547{
548 int ret;
549 char *pos, *end;
550
551 pos = buf;
552 end = buf + buflen;
553
554 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
555 pbc_status_str(hapd->wps_stats.pbc_status));
556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800557 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700558 return pos - buf;
559 pos += ret;
560
561 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
562 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
563 "Success":
564 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
565 "Failed" : "None")));
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700568 return pos - buf;
569 pos += ret;
570
571 /* If status == Failure - Add possible Reasons */
572 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
573 hapd->wps_stats.failure_reason > 0) {
574 ret = os_snprintf(pos, end - pos,
575 "Failure Reason: %s\n",
576 wps_ei_str(hapd->wps_stats.failure_reason));
577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700579 return pos - buf;
580 pos += ret;
581 }
582
583 if (hapd->wps_stats.status) {
584 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
585 MAC2STR(hapd->wps_stats.peer_addr));
586
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800587 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700588 return pos - buf;
589 pos += ret;
590 }
591
592 return pos - buf;
593}
594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595#endif /* CONFIG_WPS */
596
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800597#ifdef CONFIG_HS20
598
599static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
600 const char *cmd)
601{
602 u8 addr[ETH_ALEN];
603 const char *url;
604
605 if (hwaddr_aton(cmd, addr))
606 return -1;
607 url = cmd + 17;
608 if (*url == '\0') {
609 url = NULL;
610 } else {
611 if (*url != ' ')
612 return -1;
613 url++;
614 if (*url == '\0')
615 url = NULL;
616 }
617
618 return hs20_send_wnm_notification(hapd, addr, 1, url);
619}
620
621
622static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
623 const char *cmd)
624{
625 u8 addr[ETH_ALEN];
626 int code, reauth_delay, ret;
627 const char *pos;
628 size_t url_len;
629 struct wpabuf *req;
630
631 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
632 if (hwaddr_aton(cmd, addr))
633 return -1;
634
635 pos = os_strchr(cmd, ' ');
636 if (pos == NULL)
637 return -1;
638 pos++;
639 code = atoi(pos);
640
641 pos = os_strchr(pos, ' ');
642 if (pos == NULL)
643 return -1;
644 pos++;
645 reauth_delay = atoi(pos);
646
647 url_len = 0;
648 pos = os_strchr(pos, ' ');
649 if (pos) {
650 pos++;
651 url_len = os_strlen(pos);
652 }
653
654 req = wpabuf_alloc(4 + url_len);
655 if (req == NULL)
656 return -1;
657 wpabuf_put_u8(req, code);
658 wpabuf_put_le16(req, reauth_delay);
659 wpabuf_put_u8(req, url_len);
660 if (pos)
661 wpabuf_put_data(req, pos, url_len);
662
663 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
664 " to indicate imminent deauthentication (code=%d "
665 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
666 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
667 wpabuf_free(req);
668 return ret;
669}
670
671#endif /* CONFIG_HS20 */
672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673
Dmitry Shmidt051af732013-10-22 13:52:46 -0700674#ifdef CONFIG_INTERWORKING
675
676static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
677 const char *cmd)
678{
679 u8 qos_map_set[16 + 2 * 21], count = 0;
680 const char *pos = cmd;
681 int val, ret;
682
683 for (;;) {
684 if (count == sizeof(qos_map_set)) {
685 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
686 return -1;
687 }
688
689 val = atoi(pos);
690 if (val < 0 || val > 255) {
691 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
692 return -1;
693 }
694
695 qos_map_set[count++] = val;
696 pos = os_strchr(pos, ',');
697 if (!pos)
698 break;
699 pos++;
700 }
701
702 if (count < 16 || count & 1) {
703 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
704 return -1;
705 }
706
707 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
708 if (ret) {
709 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
710 return -1;
711 }
712
713 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
714 hapd->conf->qos_map_set_len = count;
715
716 return 0;
717}
718
719
720static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
721 const char *cmd)
722{
723 u8 addr[ETH_ALEN];
724 struct sta_info *sta;
725 struct wpabuf *buf;
726 u8 *qos_map_set = hapd->conf->qos_map_set;
727 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
728 int ret;
729
730 if (!qos_map_set_len) {
731 wpa_printf(MSG_INFO, "QoS Map Set is not set");
732 return -1;
733 }
734
735 if (hwaddr_aton(cmd, addr))
736 return -1;
737
738 sta = ap_get_sta(hapd, addr);
739 if (sta == NULL) {
740 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
741 "for QoS Map Configuration message",
742 MAC2STR(addr));
743 return -1;
744 }
745
746 if (!sta->qos_map_enabled) {
747 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
748 "support for QoS Map", MAC2STR(addr));
749 return -1;
750 }
751
752 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
753 if (buf == NULL)
754 return -1;
755
756 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
757 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
758
759 /* QoS Map Set Element */
760 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
761 wpabuf_put_u8(buf, qos_map_set_len);
762 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
763
764 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
765 wpabuf_head(buf), wpabuf_len(buf));
766 wpabuf_free(buf);
767
768 return ret;
769}
770
771#endif /* CONFIG_INTERWORKING */
772
773
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700774#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800775
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800776static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
777 const char *cmd)
778{
779 u8 addr[ETH_ALEN];
780 struct sta_info *sta;
781 const char *pos;
782 unsigned int auto_report, timeout;
783
784 if (hwaddr_aton(cmd, addr)) {
785 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
786 return -1;
787 }
788
789 sta = ap_get_sta(hapd, addr);
790 if (!sta) {
791 wpa_printf(MSG_DEBUG, "Station " MACSTR
792 " not found for Collocated Interference Request",
793 MAC2STR(addr));
794 return -1;
795 }
796
797 pos = cmd + 17;
798 if (*pos != ' ')
799 return -1;
800 pos++;
801 auto_report = atoi(pos);
802 pos = os_strchr(pos, ' ');
803 if (!pos)
804 return -1;
805 pos++;
806 timeout = atoi(pos);
807
808 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
809}
810
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700811#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800812
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800814static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
815 char *buf, size_t buflen)
816{
817 int ret = 0;
818 char *pos, *end;
819
820 pos = buf;
821 end = buf + buflen;
822
823 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
824
825 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
826 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
827 if (os_snprintf_error(end - pos, ret))
828 return pos - buf;
829 pos += ret;
830 }
831 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
832 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
833 if (os_snprintf_error(end - pos, ret))
834 return pos - buf;
835 pos += ret;
836 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800837#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800838 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
839 ret = os_snprintf(pos, end - pos, "FT-PSK ");
840 if (os_snprintf_error(end - pos, ret))
841 return pos - buf;
842 pos += ret;
843 }
844 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
845 ret = os_snprintf(pos, end - pos, "FT-EAP ");
846 if (os_snprintf_error(end - pos, ret))
847 return pos - buf;
848 pos += ret;
849 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700850#ifdef CONFIG_SHA384
851 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
852 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
853 if (os_snprintf_error(end - pos, ret))
854 return pos - buf;
855 pos += ret;
856 }
857#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800858#ifdef CONFIG_SAE
859 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
860 ret = os_snprintf(pos, end - pos, "FT-SAE ");
861 if (os_snprintf_error(end - pos, ret))
862 return pos - buf;
863 pos += ret;
864 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700865 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
866 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
867 if (os_snprintf_error(end - pos, ret))
868 return pos - buf;
869 pos += ret;
870 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800872#ifdef CONFIG_FILS
873 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
874 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
875 if (os_snprintf_error(end - pos, ret))
876 return pos - buf;
877 pos += ret;
878 }
879 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
880 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
881 if (os_snprintf_error(end - pos, ret))
882 return pos - buf;
883 pos += ret;
884 }
885#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800886#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800887 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
888 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
889 if (os_snprintf_error(end - pos, ret))
890 return pos - buf;
891 pos += ret;
892 }
893 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
894 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
895 if (os_snprintf_error(end - pos, ret))
896 return pos - buf;
897 pos += ret;
898 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899#ifdef CONFIG_SAE
900 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
901 ret = os_snprintf(pos, end - pos, "SAE ");
902 if (os_snprintf_error(end - pos, ret))
903 return pos - buf;
904 pos += ret;
905 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700906 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
907 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
908 if (os_snprintf_error(end - pos, ret))
909 return pos - buf;
910 pos += ret;
911 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800912#endif /* CONFIG_SAE */
913 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
914 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
915 if (os_snprintf_error(end - pos, ret))
916 return pos - buf;
917 pos += ret;
918 }
919 if (hapd->conf->wpa_key_mgmt &
920 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
921 ret = os_snprintf(pos, end - pos,
922 "WPA-EAP-SUITE-B-192 ");
923 if (os_snprintf_error(end - pos, ret))
924 return pos - buf;
925 pos += ret;
926 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800927#ifdef CONFIG_FILS
928 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
929 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
930 if (os_snprintf_error(end - pos, ret))
931 return pos - buf;
932 pos += ret;
933 }
934 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
935 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
936 if (os_snprintf_error(end - pos, ret))
937 return pos - buf;
938 pos += ret;
939 }
940#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800941
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700942#ifdef CONFIG_OWE
943 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
944 ret = os_snprintf(pos, end - pos, "OWE ");
945 if (os_snprintf_error(end - pos, ret))
946 return pos - buf;
947 pos += ret;
948 }
949#endif /* CONFIG_OWE */
950
951#ifdef CONFIG_DPP
952 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
953 ret = os_snprintf(pos, end - pos, "DPP ");
954 if (os_snprintf_error(end - pos, ret))
955 return pos - buf;
956 pos += ret;
957 }
958#endif /* CONFIG_DPP */
959
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800960 if (pos > buf && *(pos - 1) == ' ') {
961 *(pos - 1) = '\0';
962 pos--;
963 }
964
965 return pos - buf;
966}
967
968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
970 char *buf, size_t buflen)
971{
972 int ret;
973 char *pos, *end;
974
975 pos = buf;
976 end = buf + buflen;
977
978 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
979 "ssid=%s\n",
980 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700981 wpa_ssid_txt(hapd->conf->ssid.ssid,
982 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800983 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 return pos - buf;
985 pos += ret;
986
Sunil Ravi77d572f2023-01-17 23:58:31 +0000987 if ((hapd->conf->config_id)) {
988 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
989 hapd->conf->config_id);
990 if (os_snprintf_error(end - pos, ret))
991 return pos - buf;
992 pos += ret;
993 }
994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995#ifdef CONFIG_WPS
996 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
997 hapd->conf->wps_state == 0 ? "disabled" :
998 (hapd->conf->wps_state == 1 ? "not configured" :
999 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001000 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 return pos - buf;
1002 pos += ret;
1003
1004 if (hapd->conf->wps_state && hapd->conf->wpa &&
1005 hapd->conf->ssid.wpa_passphrase) {
1006 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1007 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001008 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 return pos - buf;
1010 pos += ret;
1011 }
1012
1013 if (hapd->conf->wps_state && hapd->conf->wpa &&
1014 hapd->conf->ssid.wpa_psk &&
1015 hapd->conf->ssid.wpa_psk->group) {
1016 char hex[PMK_LEN * 2 + 1];
1017 wpa_snprintf_hex(hex, sizeof(hex),
1018 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1019 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001020 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 return pos - buf;
1022 pos += ret;
1023 }
Hai Shalom60840252021-02-19 19:02:11 -08001024
1025 if (hapd->conf->multi_ap) {
1026 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1027
1028 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1029 hapd->conf->multi_ap);
1030 if (os_snprintf_error(end - pos, ret))
1031 return pos - buf;
1032 pos += ret;
1033
1034 if (ssid->ssid_len) {
1035 ret = os_snprintf(pos, end - pos,
1036 "multi_ap_backhaul_ssid=%s\n",
1037 wpa_ssid_txt(ssid->ssid,
1038 ssid->ssid_len));
1039 if (os_snprintf_error(end - pos, ret))
1040 return pos - buf;
1041 pos += ret;
1042 }
1043
1044 if (hapd->conf->wps_state && hapd->conf->wpa &&
1045 ssid->wpa_passphrase) {
1046 ret = os_snprintf(pos, end - pos,
1047 "multi_ap_backhaul_wpa_passphrase=%s\n",
1048 ssid->wpa_passphrase);
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_psk &&
1056 ssid->wpa_psk->group) {
1057 char hex[PMK_LEN * 2 + 1];
1058
1059 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1060 PMK_LEN);
1061 ret = os_snprintf(pos, end - pos,
1062 "multi_ap_backhaul_wpa_psk=%s\n",
1063 hex);
1064 forced_memzero(hex, sizeof(hex));
1065 if (os_snprintf_error(end - pos, ret))
1066 return pos - buf;
1067 pos += ret;
1068 }
1069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070#endif /* CONFIG_WPS */
1071
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001072 if (hapd->conf->wpa) {
1073 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1074 if (os_snprintf_error(end - pos, ret))
1075 return pos - buf;
1076 pos += ret;
1077 }
1078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1080 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001081 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 return pos - buf;
1083 pos += ret;
1084
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001085 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086
1087 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001088 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 return pos - buf;
1090 pos += ret;
1091 }
1092
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001093 if (hapd->conf->wpa) {
1094 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1095 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001096 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return pos - buf;
1098 pos += ret;
1099 }
1100
1101 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1102 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001103 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 return pos - buf;
1105 pos += ret;
1106
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001107 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1108 " ");
1109 if (ret < 0)
1110 return pos - buf;
1111 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112
1113 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001114 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 return pos - buf;
1116 pos += ret;
1117 }
1118
1119 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1120 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001121 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 return pos - buf;
1123 pos += ret;
1124
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001125 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001126 " ");
1127 if (ret < 0)
1128 return pos - buf;
1129 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130
1131 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001132 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 return pos - buf;
1134 pos += ret;
1135 }
1136
Hai Shalomfdcde762020-04-02 11:19:20 -07001137 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1138 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1139 hapd->conf->wpa_deny_ptk0_rekey);
1140 if (os_snprintf_error(end - pos, ret))
1141 return pos - buf;
1142 pos += ret;
1143 }
1144
1145 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1146 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1147 hapd->conf->extended_key_id);
1148 if (os_snprintf_error(end - pos, ret))
1149 return pos - buf;
1150 pos += ret;
1151 }
1152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 return pos - buf;
1154}
1155
1156
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001157static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001158 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001159{
1160 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001161 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001162
Hai Shalom60840252021-02-19 19:02:11 -08001163 /*
1164 * For example:
1165 * SET setband 2G,6G
1166 * SET setband 5G
1167 * SET setband AUTO
1168 */
1169 if (!os_strstr(bands, "AUTO")) {
1170 if (os_strstr(bands, "5G"))
1171 setband_mask |= WPA_SETBAND_5G;
1172 if (os_strstr(bands, "6G"))
1173 setband_mask |= WPA_SETBAND_6G;
1174 if (os_strstr(bands, "2G"))
1175 setband_mask |= WPA_SETBAND_2G;
1176 if (setband_mask == WPA_SETBAND_AUTO)
1177 return -1;
1178 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001179
Hai Shalom60840252021-02-19 19:02:11 -08001180 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001181 os_memset(&event, 0, sizeof(event));
1182 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1183 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1184 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1185 }
1186
1187 return 0;
1188}
1189
1190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1192{
1193 char *value;
1194 int ret = 0;
1195
1196 value = os_strchr(cmd, ' ');
1197 if (value == NULL)
1198 return -1;
1199 *value++ = '\0';
1200
1201 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1202 if (0) {
1203#ifdef CONFIG_WPS_TESTING
1204 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1205 long int val;
1206 val = strtol(value, NULL, 0);
1207 if (val < 0 || val > 0xff) {
1208 ret = -1;
1209 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1210 "wps_version_number %ld", val);
1211 } else {
1212 wps_version_number = val;
1213 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1214 "version %u.%u",
1215 (wps_version_number & 0xf0) >> 4,
1216 wps_version_number & 0x0f);
1217 hostapd_wps_update_ie(hapd);
1218 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001219 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1220 wps_testing_stub_cred = atoi(value);
1221 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1222 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001223 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1224 wps_corrupt_pkhash = atoi(value);
1225 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1226 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001228#ifdef CONFIG_TESTING_OPTIONS
1229 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1230 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001231 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1232 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001233 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1234 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001235#ifdef CONFIG_DPP
1236 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1237 os_free(hapd->dpp_config_obj_override);
1238 hapd->dpp_config_obj_override = os_strdup(value);
1239 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1240 os_free(hapd->dpp_discovery_override);
1241 hapd->dpp_discovery_override = os_strdup(value);
1242 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1243 os_free(hapd->dpp_groups_override);
1244 hapd->dpp_groups_override = os_strdup(value);
1245 } else if (os_strcasecmp(cmd,
1246 "dpp_ignore_netaccesskey_mismatch") == 0) {
1247 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001248 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1249 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001250 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1251 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001252#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001253#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001254#ifdef CONFIG_MBO
1255 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1256 int val;
1257
1258 if (!hapd->conf->mbo_enabled)
1259 return -1;
1260
1261 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001262 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001263 return -1;
1264
1265 hapd->mbo_assoc_disallow = val;
1266 ieee802_11_update_beacons(hapd->iface);
1267
1268 /*
1269 * TODO: Need to configure drivers that do AP MLME offload with
1270 * disallowing station logic.
1271 */
1272#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001273#ifdef CONFIG_DPP
1274 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1275 os_free(hapd->dpp_configurator_params);
1276 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001277#ifdef CONFIG_DPP2
1278 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1279#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001280 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1281 hapd->dpp_init_max_tries = atoi(value);
1282 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1283 hapd->dpp_init_retry_time = atoi(value);
1284 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1285 hapd->dpp_resp_wait_time = atoi(value);
1286 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1287 hapd->dpp_resp_max_tries = atoi(value);
1288 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1289 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001290#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001291 } else if (os_strcasecmp(cmd, "setband") == 0) {
1292 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001294 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001295 if (ret)
1296 return ret;
1297
1298 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001299 hostapd_disassoc_deny_mac(hapd);
1300 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1301 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001302 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1303 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1304 hapd->parameter_set_count++;
1305 if (ieee802_11_update_beacons(hapd->iface))
1306 wpa_printf(MSG_DEBUG,
1307 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001308 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1309 os_strcmp(cmd, "sae_password") == 0 ||
1310 os_strcmp(cmd, "sae_pwe") == 0) {
1311 if (hapd->started)
1312 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001313 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1314 wpa_auth_set_transition_disable(hapd->wpa_auth,
1315 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001316 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001317
1318#ifdef CONFIG_TESTING_OPTIONS
1319 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1320 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1321 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001322 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1323 wpa_auth_set_ocv_override_freq(
1324 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1325 atoi(value));
1326 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1327 wpa_auth_set_ocv_override_freq(
1328 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1329 atoi(value));
1330 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1331 wpa_auth_set_ocv_override_freq(
1332 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1333 atoi(value));
1334 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1335 wpa_auth_set_ocv_override_freq(
1336 hapd->wpa_auth,
1337 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001338 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1339 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1340 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1341 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001342#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 }
1344
1345 return ret;
1346}
1347
1348
1349static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1350 char *buf, size_t buflen)
1351{
1352 int res;
1353
1354 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1355
1356 if (os_strcmp(cmd, "version") == 0) {
1357 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001358 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 return -1;
1360 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001361 } else if (os_strcmp(cmd, "tls_library") == 0) {
1362 res = tls_get_library_version(buf, buflen);
1363 if (os_snprintf_error(buflen, res))
1364 return -1;
1365 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 }
1367
1368 return -1;
1369}
1370
1371
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001372static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1373{
1374 if (hostapd_enable_iface(iface) < 0) {
1375 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1376 return -1;
1377 }
1378 return 0;
1379}
1380
1381
1382static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1383{
1384 if (hostapd_reload_iface(iface) < 0) {
1385 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1386 return -1;
1387 }
1388 return 0;
1389}
1390
1391
Sunil Ravi77d572f2023-01-17 23:58:31 +00001392static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1393{
1394 if (hostapd_reload_bss_only(bss) < 0) {
1395 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1396 return -1;
1397 }
1398 return 0;
1399}
1400
1401
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001402static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1403{
1404 if (hostapd_disable_iface(iface) < 0) {
1405 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1406 return -1;
1407 }
1408 return 0;
1409}
1410
1411
Hai Shalom74f70d42019-02-11 14:42:39 -08001412static int
1413hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1414 struct sta_info *sta, void *ctx)
1415{
1416 struct hostapd_wpa_psk *psk;
1417 const u8 *pmk;
1418 int pmk_len;
1419 int pmk_match;
1420 int sta_match;
1421 int bss_match;
1422 int reason;
1423
1424 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1425
1426 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1427 pmk_match = PMK_LEN == pmk_len &&
1428 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1429 sta_match = psk->group == 0 &&
1430 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1431 bss_match = psk->group == 1;
1432
1433 if (pmk_match && (sta_match || bss_match))
1434 return 0;
1435 }
1436
1437 wpa_printf(MSG_INFO, "STA " MACSTR
1438 " PSK/passphrase no longer valid - disconnect",
1439 MAC2STR(sta->addr));
1440 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1441 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1442 ap_sta_deauthenticate(hapd, sta, reason);
1443
1444 return 0;
1445}
1446
1447
1448static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1449{
1450 struct hostapd_bss_config *conf = hapd->conf;
1451 int err;
1452
1453 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1454
1455 err = hostapd_setup_wpa_psk(conf);
1456 if (err < 0) {
1457 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1458 err);
1459 return -1;
1460 }
1461
1462 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1463 NULL);
1464
1465 return 0;
1466}
1467
1468
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001469#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001471static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1472{
1473 union wpa_event_data data;
1474 char *pos, *param;
1475 enum wpa_event_type event;
1476
1477 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1478
1479 os_memset(&data, 0, sizeof(data));
1480
1481 param = os_strchr(cmd, ' ');
1482 if (param == NULL)
1483 return -1;
1484 *param++ = '\0';
1485
1486 if (os_strcmp(cmd, "DETECTED") == 0)
1487 event = EVENT_DFS_RADAR_DETECTED;
1488 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1489 event = EVENT_DFS_CAC_FINISHED;
1490 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1491 event = EVENT_DFS_CAC_ABORTED;
1492 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1493 event = EVENT_DFS_NOP_FINISHED;
1494 else {
1495 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1496 cmd);
1497 return -1;
1498 }
1499
1500 pos = os_strstr(param, "freq=");
1501 if (pos)
1502 data.dfs_event.freq = atoi(pos + 5);
1503
1504 pos = os_strstr(param, "ht_enabled=1");
1505 if (pos)
1506 data.dfs_event.ht_enabled = 1;
1507
1508 pos = os_strstr(param, "chan_offset=");
1509 if (pos)
1510 data.dfs_event.chan_offset = atoi(pos + 12);
1511
1512 pos = os_strstr(param, "chan_width=");
1513 if (pos)
1514 data.dfs_event.chan_width = atoi(pos + 11);
1515
1516 pos = os_strstr(param, "cf1=");
1517 if (pos)
1518 data.dfs_event.cf1 = atoi(pos + 4);
1519
1520 pos = os_strstr(param, "cf2=");
1521 if (pos)
1522 data.dfs_event.cf2 = atoi(pos + 4);
1523
1524 wpa_supplicant_event(hapd, event, &data);
1525
1526 return 0;
1527}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528
1529
1530static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1531{
1532 size_t len;
1533 u8 *buf;
1534 int res;
1535
1536 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1537
1538 len = os_strlen(cmd);
1539 if (len & 1)
1540 return -1;
1541 len /= 2;
1542
1543 buf = os_malloc(len);
1544 if (buf == NULL)
1545 return -1;
1546
1547 if (hexstr2bin(cmd, buf, len) < 0) {
1548 os_free(buf);
1549 return -1;
1550 }
1551
Hai Shalomfdcde762020-04-02 11:19:20 -07001552 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001553 os_free(buf);
1554 return res;
1555}
1556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001557
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001558static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1559 char *cmd)
1560{
1561 char *pos, *param;
1562 size_t len;
1563 u8 *buf;
1564 int stype = 0, ok = 0;
1565 union wpa_event_data event;
1566
1567 if (!hapd->ext_mgmt_frame_handling)
1568 return -1;
1569
1570 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1571
1572 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1573
1574 pos = cmd;
1575 param = os_strstr(pos, "stype=");
1576 if (param) {
1577 param += 6;
1578 stype = atoi(param);
1579 }
1580
1581 param = os_strstr(pos, " ok=");
1582 if (param) {
1583 param += 4;
1584 ok = atoi(param);
1585 }
1586
1587 param = os_strstr(pos, " buf=");
1588 if (!param)
1589 return -1;
1590 param += 5;
1591
1592 len = os_strlen(param);
1593 if (len & 1)
1594 return -1;
1595 len /= 2;
1596
1597 buf = os_malloc(len);
1598 if (!buf || hexstr2bin(param, buf, len) < 0) {
1599 os_free(buf);
1600 return -1;
1601 }
1602
1603 os_memset(&event, 0, sizeof(event));
1604 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1605 event.tx_status.data = buf;
1606 event.tx_status.data_len = len;
1607 event.tx_status.stype = stype;
1608 event.tx_status.ack = ok;
1609 hapd->ext_mgmt_frame_handling = 0;
1610 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1611 hapd->ext_mgmt_frame_handling = 1;
1612
1613 os_free(buf);
1614
1615 return 0;
1616}
1617
1618
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001619static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1620 char *cmd)
1621{
1622 char *pos, *param;
1623 size_t len;
1624 u8 *buf;
1625 int freq = 0, datarate = 0, ssi_signal = 0;
1626 union wpa_event_data event;
1627
1628 if (!hapd->ext_mgmt_frame_handling)
1629 return -1;
1630
1631 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1632
1633 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1634
1635 pos = cmd;
1636 param = os_strstr(pos, "freq=");
1637 if (param) {
1638 param += 5;
1639 freq = atoi(param);
1640 }
1641
1642 param = os_strstr(pos, " datarate=");
1643 if (param) {
1644 param += 10;
1645 datarate = atoi(param);
1646 }
1647
1648 param = os_strstr(pos, " ssi_signal=");
1649 if (param) {
1650 param += 12;
1651 ssi_signal = atoi(param);
1652 }
1653
1654 param = os_strstr(pos, " frame=");
1655 if (param == NULL)
1656 return -1;
1657 param += 7;
1658
1659 len = os_strlen(param);
1660 if (len & 1)
1661 return -1;
1662 len /= 2;
1663
1664 buf = os_malloc(len);
1665 if (buf == NULL)
1666 return -1;
1667
1668 if (hexstr2bin(param, buf, len) < 0) {
1669 os_free(buf);
1670 return -1;
1671 }
1672
1673 os_memset(&event, 0, sizeof(event));
1674 event.rx_mgmt.freq = freq;
1675 event.rx_mgmt.frame = buf;
1676 event.rx_mgmt.frame_len = len;
1677 event.rx_mgmt.ssi_signal = ssi_signal;
1678 event.rx_mgmt.datarate = datarate;
1679 hapd->ext_mgmt_frame_handling = 0;
1680 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1681 hapd->ext_mgmt_frame_handling = 1;
1682
1683 os_free(buf);
1684
1685 return 0;
1686}
1687
1688
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001689static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1690{
1691 char *pos;
1692 u8 src[ETH_ALEN], *buf;
1693 int used;
1694 size_t len;
1695
1696 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1697
1698 pos = cmd;
1699 used = hwaddr_aton2(pos, src);
1700 if (used < 0)
1701 return -1;
1702 pos += used;
1703 while (*pos == ' ')
1704 pos++;
1705
1706 len = os_strlen(pos);
1707 if (len & 1)
1708 return -1;
1709 len /= 2;
1710
1711 buf = os_malloc(len);
1712 if (buf == NULL)
1713 return -1;
1714
1715 if (hexstr2bin(pos, buf, len) < 0) {
1716 os_free(buf);
1717 return -1;
1718 }
1719
Sunil8cd6f4d2022-06-28 18:40:46 +00001720 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001721 os_free(buf);
1722
1723 return 0;
1724}
1725
1726
Hai Shaloma20dcd72022-02-04 13:43:00 -08001727static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1728{
1729 char *pos, *pos2;
1730 u8 dst[ETH_ALEN], *buf;
1731 int used, ret;
1732 size_t len;
1733 unsigned int prev;
1734 int encrypt = 0;
1735
1736 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1737
1738 pos = cmd;
1739 used = hwaddr_aton2(pos, dst);
1740 if (used < 0)
1741 return -1;
1742 pos += used;
1743 while (*pos == ' ')
1744 pos++;
1745
1746 pos2 = os_strchr(pos, ' ');
1747 if (pos2) {
1748 len = pos2 - pos;
1749 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1750 } else {
1751 len = os_strlen(pos);
1752 }
1753 if (len & 1)
1754 return -1;
1755 len /= 2;
1756
1757 buf = os_malloc(len);
1758 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1759 os_free(buf);
1760 return -1;
1761 }
1762
1763 prev = hapd->ext_eapol_frame_io;
1764 hapd->ext_eapol_frame_io = 0;
1765 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1766 hapd->ext_eapol_frame_io = prev;
1767 os_free(buf);
1768
1769 return ret;
1770}
1771
1772
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001773static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1774{
1775 size_t i;
1776 u32 sum = 0;
1777 const u16 *pos = buf;
1778
1779 for (i = 0; i < len / 2; i++)
1780 sum += *pos++;
1781
1782 while (sum >> 16)
1783 sum = (sum & 0xffff) + (sum >> 16);
1784
1785 return sum ^ 0xffff;
1786}
1787
1788
1789#define HWSIM_PACKETLEN 1500
1790#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1791
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001792static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1793 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001794{
1795 struct hostapd_data *hapd = ctx;
1796 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001797 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001798 const u8 *pos;
1799 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001800 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001801
Hai Shalom81f62d82019-07-22 12:10:00 -07001802 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1803 wpa_printf(MSG_DEBUG,
1804 "test data: RX - ignore unexpected length %d",
1805 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001807 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001808
1809 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 os_memcpy(&ip, eth + 1, sizeof(ip));
1811 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812
Hai Shalomfdcde762020-04-02 11:19:20 -07001813 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1814 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001815 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001816 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001818 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001819
Hai Shalomfdcde762020-04-02 11:19:20 -07001820 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001821 if (*pos != (u8) i) {
1822 wpa_printf(MSG_DEBUG,
1823 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001824 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001825 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001826 pos++;
1827 }
1828
Hai Shalom81f62d82019-07-22 12:10:00 -07001829 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001830 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1831 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001832 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1833 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001834}
1835
1836
1837static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1838 char *cmd)
1839{
1840 int enabled = atoi(cmd);
1841 char *pos;
1842 const char *ifname;
1843
1844 if (!enabled) {
1845 if (hapd->l2_test) {
1846 l2_packet_deinit(hapd->l2_test);
1847 hapd->l2_test = NULL;
1848 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1849 "test data: Disabled");
1850 }
1851 return 0;
1852 }
1853
1854 if (hapd->l2_test)
1855 return 0;
1856
1857 pos = os_strstr(cmd, " ifname=");
1858 if (pos)
1859 ifname = pos + 8;
1860 else
1861 ifname = hapd->conf->iface;
1862
1863 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1864 ETHERTYPE_IP, hostapd_data_test_rx,
1865 hapd, 1);
1866 if (hapd->l2_test == NULL)
1867 return -1;
1868
1869 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1870
1871 return 0;
1872}
1873
1874
1875static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1876{
1877 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001878 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879 int used;
1880 long int val;
1881 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001883 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001884 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885 u8 *dpos;
1886 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001888
1889 if (hapd->l2_test == NULL)
1890 return -1;
1891
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893
1894 pos = cmd;
1895 used = hwaddr_aton2(pos, dst);
1896 if (used < 0)
1897 return -1;
1898 pos += used;
1899 while (*pos == ' ')
1900 pos++;
1901 used = hwaddr_aton2(pos, src);
1902 if (used < 0)
1903 return -1;
1904 pos += used;
1905
Hai Shalom81f62d82019-07-22 12:10:00 -07001906 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 if (val < 0 || val > 0xff)
1908 return -1;
1909 tos = val;
1910
Hai Shalom81f62d82019-07-22 12:10:00 -07001911 pos = os_strstr(pos2, " len=");
1912 if (pos) {
1913 i = atoi(pos + 5);
1914 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1915 return -1;
1916 send_len = i;
1917 }
1918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001919 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001920 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1921 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1922 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07001923 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07001925 ip->ip_hl = 5;
1926 ip->ip_v = 4;
1927 ip->ip_ttl = 64;
1928 ip->ip_tos = tos;
1929 ip->ip_len = htons(send_len);
1930 ip->ip_p = 1;
1931 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1932 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1933 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001934 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001935 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001936 *dpos++ = i;
1937
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001938 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07001939 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001940 return -1;
1941
1942 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1943 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1944
1945 return 0;
1946}
1947
1948
1949static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1950 char *cmd)
1951{
1952 u8 *buf;
1953 struct ether_header *eth;
1954 struct l2_packet_data *l2 = NULL;
1955 size_t len;
1956 u16 ethertype;
1957 int res = -1;
1958 const char *ifname = hapd->conf->iface;
1959
1960 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1961 cmd += 7;
1962 ifname = cmd;
1963 cmd = os_strchr(cmd, ' ');
1964 if (cmd == NULL)
1965 return -1;
1966 *cmd++ = '\0';
1967 }
1968
1969 len = os_strlen(cmd);
1970 if (len & 1 || len < ETH_HLEN * 2)
1971 return -1;
1972 len /= 2;
1973
1974 buf = os_malloc(len);
1975 if (buf == NULL)
1976 return -1;
1977
1978 if (hexstr2bin(cmd, buf, len) < 0)
1979 goto done;
1980
1981 eth = (struct ether_header *) buf;
1982 ethertype = ntohs(eth->ether_type);
1983
1984 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1985 hostapd_data_test_rx, hapd, 1);
1986 if (l2 == NULL)
1987 goto done;
1988
1989 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1990 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1991done:
1992 if (l2)
1993 l2_packet_deinit(l2);
1994 os_free(buf);
1995
1996 return res < 0 ? -1 : 0;
1997}
1998
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001999
2000static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2001{
2002#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002003 char *pos;
2004
2005 wpa_trace_fail_after = atoi(cmd);
2006 pos = os_strchr(cmd, ':');
2007 if (pos) {
2008 pos++;
2009 os_strlcpy(wpa_trace_fail_func, pos,
2010 sizeof(wpa_trace_fail_func));
2011 } else {
2012 wpa_trace_fail_after = 0;
2013 }
2014
2015 return 0;
2016#else /* WPA_TRACE_BFD */
2017 return -1;
2018#endif /* WPA_TRACE_BFD */
2019}
2020
2021
2022static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2023 char *buf, size_t buflen)
2024{
2025#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002026 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2027 wpa_trace_fail_func);
2028#else /* WPA_TRACE_BFD */
2029 return -1;
2030#endif /* WPA_TRACE_BFD */
2031}
2032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002033
2034static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2035{
2036#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002037 char *pos;
2038
2039 wpa_trace_test_fail_after = atoi(cmd);
2040 pos = os_strchr(cmd, ':');
2041 if (pos) {
2042 pos++;
2043 os_strlcpy(wpa_trace_test_fail_func, pos,
2044 sizeof(wpa_trace_test_fail_func));
2045 } else {
2046 wpa_trace_test_fail_after = 0;
2047 }
2048
2049 return 0;
2050#else /* WPA_TRACE_BFD */
2051 return -1;
2052#endif /* WPA_TRACE_BFD */
2053}
2054
2055
2056static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2057 char *buf, size_t buflen)
2058{
2059#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002060 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2061 wpa_trace_test_fail_func);
2062#else /* WPA_TRACE_BFD */
2063 return -1;
2064#endif /* WPA_TRACE_BFD */
2065}
2066
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002067
2068static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2069{
2070 struct sta_info *sta;
2071 u8 addr[ETH_ALEN];
2072 u8 zero[WPA_TK_MAX_LEN];
2073
2074 os_memset(zero, 0, sizeof(zero));
2075
2076 if (hwaddr_aton(cmd, addr))
2077 return -1;
2078
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002079 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2080 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2081 return -1;
2082
2083 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2084
2085 /* First, use a zero key to avoid any possible duplicate key
2086 * avoidance in the driver. */
2087 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2088 hapd->last_bigtk_alg,
2089 broadcast_ether_addr,
2090 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2091 zero, hapd->last_bigtk_len,
2092 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2093 return -1;
2094
2095 /* Set the previously configured key to reset its TSC */
2096 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2097 hapd->last_bigtk_alg,
2098 broadcast_ether_addr,
2099 hapd->last_bigtk_key_idx, 0, 1, NULL,
2100 0, hapd->last_bigtk,
2101 hapd->last_bigtk_len,
2102 KEY_FLAG_GROUP_TX_DEFAULT);
2103 }
2104
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002105 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2106 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2107 return -1;
2108
2109 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2110
2111 /* First, use a zero key to avoid any possible duplicate key
2112 * avoidance in the driver. */
2113 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2114 hapd->last_igtk_alg,
2115 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002116 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2117 zero, hapd->last_igtk_len,
2118 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002119 return -1;
2120
2121 /* Set the previously configured key to reset its TSC */
2122 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2123 hapd->last_igtk_alg,
2124 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002125 hapd->last_igtk_key_idx, 0, 1, NULL,
2126 0, hapd->last_igtk,
2127 hapd->last_igtk_len,
2128 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002129 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002130
2131 if (is_broadcast_ether_addr(addr)) {
2132 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2133 return -1;
2134
2135 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2136
2137 /* First, use a zero key to avoid any possible duplicate key
2138 * avoidance in the driver. */
2139 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2140 hapd->last_gtk_alg,
2141 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002142 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2143 zero, hapd->last_gtk_len,
2144 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002145 return -1;
2146
2147 /* Set the previously configured key to reset its TSC */
2148 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2149 hapd->last_gtk_alg,
2150 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002151 hapd->last_gtk_key_idx, 0, 1, NULL,
2152 0, hapd->last_gtk,
2153 hapd->last_gtk_len,
2154 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002155 }
2156
2157 sta = ap_get_sta(hapd, addr);
2158 if (!sta)
2159 return -1;
2160
2161 if (sta->last_tk_alg == WPA_ALG_NONE)
2162 return -1;
2163
2164 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2165 MAC2STR(sta->addr));
2166
2167 /* First, use a zero key to avoid any possible duplicate key avoidance
2168 * in the driver. */
2169 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002170 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2171 zero, sta->last_tk_len,
2172 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002173 return -1;
2174
2175 /* Set the previously configured key to reset its TSC/RSC */
2176 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002177 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2178 0, sta->last_tk, sta->last_tk_len,
2179 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002180}
2181
2182
2183static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2184{
2185 u8 addr[ETH_ALEN];
2186 const char *pos = cmd;
2187 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002188 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002189 int idx, set_tx;
2190 u8 seq[6], key[WPA_TK_MAX_LEN];
2191 size_t key_len;
2192
Hai Shalomfdcde762020-04-02 11:19:20 -07002193 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002194
2195 alg = atoi(pos);
2196 pos = os_strchr(pos, ' ');
2197 if (!pos)
2198 return -1;
2199 pos++;
2200 if (hwaddr_aton(pos, addr))
2201 return -1;
2202 pos += 17;
2203 if (*pos != ' ')
2204 return -1;
2205 pos++;
2206 idx = atoi(pos);
2207 pos = os_strchr(pos, ' ');
2208 if (!pos)
2209 return -1;
2210 pos++;
2211 set_tx = atoi(pos);
2212 pos = os_strchr(pos, ' ');
2213 if (!pos)
2214 return -1;
2215 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002216 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217 return -1;
2218 pos += 2 * 6;
2219 if (*pos != ' ')
2220 return -1;
2221 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002222 if (!os_strchr(pos, ' '))
2223 return -1;
2224 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002225 if (hexstr2bin(pos, key, key_len) < 0)
2226 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002227 pos += 2 * key_len;
2228 if (*pos != ' ')
2229 return -1;
2230
2231 pos++;
2232 key_flag = atoi(pos);
2233 pos = os_strchr(pos, ' ');
2234 if (pos)
2235 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002236
2237 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002238 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2239 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002240}
2241
2242
2243static void restore_tk(void *ctx1, void *ctx2)
2244{
2245 struct hostapd_data *hapd = ctx1;
2246 struct sta_info *sta = ctx2;
2247
2248 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2249 MAC2STR(sta->addr));
2250 /* This does not really restore the TSC properly, so this will result
2251 * in replay protection issues for now since there is no clean way of
2252 * preventing encryption of a single EAPOL frame. */
2253 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002254 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2255 sta->last_tk, sta->last_tk_len,
2256 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002257}
2258
2259
2260static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2261{
2262 struct sta_info *sta;
2263 u8 addr[ETH_ALEN];
2264 int plain = os_strstr(cmd, "plaintext") != NULL;
2265
2266 if (hwaddr_aton(cmd, addr))
2267 return -1;
2268
2269 sta = ap_get_sta(hapd, addr);
2270 if (!sta || !sta->wpa_sm)
2271 return -1;
2272
2273 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2274 plain = 0; /* no need for special processing */
2275 if (plain) {
2276 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2277 MAC2STR(sta->addr));
2278 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002279 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2280 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002281 }
2282
2283 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2284 return wpa_auth_resend_m1(sta->wpa_sm,
2285 os_strstr(cmd, "change-anonce") != NULL,
2286 plain ? restore_tk : NULL, hapd, sta);
2287}
2288
2289
2290static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2291{
2292 struct sta_info *sta;
2293 u8 addr[ETH_ALEN];
2294 int plain = os_strstr(cmd, "plaintext") != NULL;
2295
2296 if (hwaddr_aton(cmd, addr))
2297 return -1;
2298
2299 sta = ap_get_sta(hapd, addr);
2300 if (!sta || !sta->wpa_sm)
2301 return -1;
2302
2303 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2304 plain = 0; /* no need for special processing */
2305 if (plain) {
2306 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2307 MAC2STR(sta->addr));
2308 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002309 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2310 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002311 }
2312
2313 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2314 return wpa_auth_resend_m3(sta->wpa_sm,
2315 plain ? restore_tk : NULL, hapd, sta);
2316}
2317
2318
2319static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2320 const char *cmd)
2321{
2322 struct sta_info *sta;
2323 u8 addr[ETH_ALEN];
2324 int plain = os_strstr(cmd, "plaintext") != NULL;
2325
2326 if (hwaddr_aton(cmd, addr))
2327 return -1;
2328
2329 sta = ap_get_sta(hapd, addr);
2330 if (!sta || !sta->wpa_sm)
2331 return -1;
2332
2333 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2334 plain = 0; /* no need for special processing */
2335 if (plain) {
2336 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2337 MAC2STR(sta->addr));
2338 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002339 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2340 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002341 }
2342
2343 wpa_printf(MSG_INFO,
2344 "TESTING: Send group M1 for the same GTK and zero RSC to "
2345 MACSTR, MAC2STR(sta->addr));
2346 return wpa_auth_resend_group_m1(sta->wpa_sm,
2347 plain ? restore_tk : NULL, hapd, sta);
2348}
2349
Hai Shalomfdcde762020-04-02 11:19:20 -07002350
Hai Shaloma20dcd72022-02-04 13:43:00 -08002351static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2352{
2353 struct sta_info *sta;
2354 u8 addr[ETH_ALEN];
2355
2356 if (hwaddr_aton(cmd, addr))
2357 return -1;
2358
2359 sta = ap_get_sta(hapd, addr);
2360 if (!sta || !sta->wpa_sm)
2361 return -1;
2362
2363 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2364}
2365
2366
Hai Shalom899fcc72020-10-19 14:38:18 -07002367static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2368 char *buf, size_t buflen)
2369{
2370 struct rsn_pmksa_cache_entry *pmksa;
2371
2372 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2373 if (!pmksa)
2374 return -1;
2375
2376 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2377}
2378
2379
Hai Shalomfdcde762020-04-02 11:19:20 -07002380static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2381 char *buf, size_t buflen)
2382{
2383 struct sta_info *sta;
2384 u8 addr[ETH_ALEN];
2385 const u8 *pmk;
2386 int pmk_len;
2387
2388 if (hwaddr_aton(cmd, addr))
2389 return -1;
2390
2391 sta = ap_get_sta(hapd, addr);
2392 if (!sta || !sta->wpa_sm) {
2393 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2394 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002395 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002396 }
2397 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002398 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002399 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2400 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002401 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002402 }
2403
2404 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2405}
2406
Hai Shaloma20dcd72022-02-04 13:43:00 -08002407
2408static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2409 const char *cmd)
2410{
2411 u16 type;
2412 char *pos, *end;
2413 u8 match[10];
2414 size_t match_len;
2415 bool multicast = false;
2416
2417 type = strtol(cmd, &pos, 16);
2418 if (*pos != ' ')
2419 return -1;
2420 pos++;
2421 end = os_strchr(pos, ' ');
2422 if (end) {
2423 match_len = end - pos;
2424 multicast = os_strstr(end, "multicast") != NULL;
2425 } else {
2426 match_len = os_strlen(pos) / 2;
2427 }
2428 if (hexstr2bin(pos, match, match_len))
2429 return -1;
2430
2431 return hostapd_drv_register_frame(hapd, type, match, match_len,
2432 multicast);
2433}
2434
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002435#endif /* CONFIG_TESTING_OPTIONS */
2436
2437
Hai Shalomb755a2a2020-04-23 21:49:02 -07002438#ifdef NEED_AP_MLME
Sunil Ravi036cec52023-03-29 11:35:17 -07002439static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params,
2440 u16 punct_bitmap)
Hai Shalomfdcde762020-04-02 11:19:20 -07002441{
Sunil Ravi036cec52023-03-29 11:35:17 -07002442 u32 start_freq;
2443
2444 if (is_6ghz_freq(params->freq)) {
2445 const int bw_idx[] = { 20, 40, 80, 160, 320 };
2446 int idx, bw;
2447
2448 /* The 6 GHz band requires HE to be enabled. */
2449 params->he_enabled = 1;
2450
2451 if (params->center_freq1) {
2452 if (params->freq == 5935)
2453 idx = (params->center_freq1 - 5925) / 5;
2454 else
2455 idx = (params->center_freq1 - 5950) / 5;
2456
2457 bw = center_idx_to_bw_6ghz(idx);
2458 if (bw < 0 || bw > (int) ARRAY_SIZE(bw_idx) ||
2459 bw_idx[bw] != params->bandwidth)
2460 return -1;
2461 }
2462 }
2463
Hai Shalomfdcde762020-04-02 11:19:20 -07002464 switch (params->bandwidth) {
2465 case 0:
2466 /* bandwidth not specified: use 20 MHz by default */
2467 /* fall-through */
2468 case 20:
2469 if (params->center_freq1 &&
2470 params->center_freq1 != params->freq)
2471 return -1;
2472
2473 if (params->center_freq2 || params->sec_channel_offset)
2474 return -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07002475
2476 if (punct_bitmap)
2477 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002478 break;
2479 case 40:
2480 if (params->center_freq2 || !params->sec_channel_offset)
2481 return -1;
2482
Sunil Ravi036cec52023-03-29 11:35:17 -07002483 if (punct_bitmap)
2484 return -1;
2485
Hai Shalomfdcde762020-04-02 11:19:20 -07002486 if (!params->center_freq1)
2487 break;
2488 switch (params->sec_channel_offset) {
2489 case 1:
2490 if (params->freq + 10 != params->center_freq1)
2491 return -1;
2492 break;
2493 case -1:
2494 if (params->freq - 10 != params->center_freq1)
2495 return -1;
2496 break;
2497 default:
2498 return -1;
2499 }
2500 break;
2501 case 80:
2502 if (!params->center_freq1 || !params->sec_channel_offset)
2503 return 1;
2504
2505 switch (params->sec_channel_offset) {
2506 case 1:
2507 if (params->freq - 10 != params->center_freq1 &&
2508 params->freq + 30 != params->center_freq1)
2509 return 1;
2510 break;
2511 case -1:
2512 if (params->freq + 10 != params->center_freq1 &&
2513 params->freq - 30 != params->center_freq1)
2514 return -1;
2515 break;
2516 default:
2517 return -1;
2518 }
2519
Sunil Ravi036cec52023-03-29 11:35:17 -07002520 if (params->center_freq2 && punct_bitmap)
2521 return -1;
2522
Hai Shalomfdcde762020-04-02 11:19:20 -07002523 /* Adjacent and overlapped are not allowed for 80+80 */
2524 if (params->center_freq2 &&
2525 params->center_freq1 - params->center_freq2 <= 80 &&
2526 params->center_freq2 - params->center_freq1 <= 80)
2527 return 1;
2528 break;
2529 case 160:
2530 if (!params->center_freq1 || params->center_freq2 ||
2531 !params->sec_channel_offset)
2532 return -1;
2533
2534 switch (params->sec_channel_offset) {
2535 case 1:
2536 if (params->freq + 70 != params->center_freq1 &&
2537 params->freq + 30 != params->center_freq1 &&
2538 params->freq - 10 != params->center_freq1 &&
2539 params->freq - 50 != params->center_freq1)
2540 return -1;
2541 break;
2542 case -1:
2543 if (params->freq + 50 != params->center_freq1 &&
2544 params->freq + 10 != params->center_freq1 &&
2545 params->freq - 30 != params->center_freq1 &&
2546 params->freq - 70 != params->center_freq1)
2547 return -1;
2548 break;
2549 default:
2550 return -1;
2551 }
2552 break;
Sunil Ravi640215c2023-06-28 23:08:09 +00002553 case 320:
2554 if (!params->center_freq1 || params->center_freq2 ||
2555 !params->sec_channel_offset)
2556 return -1;
2557
2558 switch (params->sec_channel_offset) {
2559 case 1:
2560 if (params->freq + 150 != params->center_freq1 &&
2561 params->freq + 110 != params->center_freq1 &&
2562 params->freq + 70 != params->center_freq1 &&
2563 params->freq + 30 != params->center_freq1 &&
2564 params->freq - 10 != params->center_freq1 &&
2565 params->freq - 50 != params->center_freq1 &&
2566 params->freq - 90 != params->center_freq1 &&
2567 params->freq - 130 != params->center_freq1)
2568 return -1;
2569 break;
2570 case -1:
2571 if (params->freq + 130 != params->center_freq1 &&
2572 params->freq + 90 != params->center_freq1 &&
2573 params->freq + 50 != params->center_freq1 &&
2574 params->freq + 10 != params->center_freq1 &&
2575 params->freq - 30 != params->center_freq1 &&
2576 params->freq - 70 != params->center_freq1 &&
2577 params->freq - 110 != params->center_freq1 &&
2578 params->freq - 150 != params->center_freq1)
2579 return -1;
2580 break;
2581 }
2582 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002583 default:
2584 return -1;
2585 }
2586
Sunil Ravi036cec52023-03-29 11:35:17 -07002587 if (!punct_bitmap)
2588 return 0;
2589
2590 if (!params->eht_enabled) {
2591 wpa_printf(MSG_ERROR,
2592 "Preamble puncturing supported only in EHT");
2593 return -1;
2594 }
2595
2596 if (params->freq >= 2412 && params->freq <= 2484) {
2597 wpa_printf(MSG_ERROR,
2598 "Preamble puncturing is not supported in 2.4 GHz");
2599 return -1;
2600 }
2601
2602 start_freq = params->center_freq1 - (params->bandwidth / 2);
2603 if (!is_punct_bitmap_valid(params->bandwidth,
2604 (params->freq - start_freq) / 20,
2605 punct_bitmap)) {
2606 wpa_printf(MSG_ERROR, "Invalid preamble puncturing bitmap");
2607 return -1;
2608 }
2609
Hai Shalomfdcde762020-04-02 11:19:20 -07002610 return 0;
2611}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002612#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002613
2614
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002615static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2616 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002617{
2618#ifdef NEED_AP_MLME
2619 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002620 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002621 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002622 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002623 int bandwidth;
2624 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002625
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002626 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002627 if (ret)
2628 return ret;
2629
Sunil Ravi036cec52023-03-29 11:35:17 -07002630 ret = hostapd_ctrl_check_freq_params(&settings.freq_params,
2631 settings.punct_bitmap);
Hai Shalomfdcde762020-04-02 11:19:20 -07002632 if (ret) {
2633 wpa_printf(MSG_INFO,
2634 "chanswitch: invalid frequency settings provided");
2635 return ret;
2636 }
2637
2638 switch (settings.freq_params.bandwidth) {
2639 case 40:
2640 bandwidth = CHAN_WIDTH_40;
2641 break;
2642 case 80:
2643 if (settings.freq_params.center_freq2)
2644 bandwidth = CHAN_WIDTH_80P80;
2645 else
2646 bandwidth = CHAN_WIDTH_80;
2647 break;
2648 case 160:
2649 bandwidth = CHAN_WIDTH_160;
2650 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002651 case 320:
2652 bandwidth = CHAN_WIDTH_320;
2653 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002654 default:
2655 bandwidth = CHAN_WIDTH_20;
2656 break;
2657 }
2658
2659 if (settings.freq_params.center_freq1)
2660 dfs_range += hostapd_is_dfs_overlap(
2661 iface, bandwidth, settings.freq_params.center_freq1);
2662 else
2663 dfs_range += hostapd_is_dfs_overlap(
2664 iface, bandwidth, settings.freq_params.freq);
2665
2666 if (settings.freq_params.center_freq2)
2667 dfs_range += hostapd_is_dfs_overlap(
2668 iface, bandwidth, settings.freq_params.center_freq2);
2669
2670 if (dfs_range) {
2671 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2672 if (ret == NUM_HOSTAPD_MODES) {
2673 wpa_printf(MSG_ERROR,
2674 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2675 settings.freq_params.freq,
2676 settings.freq_params.sec_channel_offset,
2677 settings.freq_params.bandwidth);
2678 return -1;
2679 }
2680
2681 settings.freq_params.channel = chan;
2682
2683 wpa_printf(MSG_DEBUG,
2684 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2685 settings.freq_params.channel,
2686 settings.freq_params.freq,
2687 settings.freq_params.sec_channel_offset,
2688 settings.freq_params.bandwidth,
2689 settings.freq_params.center_freq1);
2690
2691 /* Perform CAC and switch channel */
2692 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2693 return 0;
2694 }
2695
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002696 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002697
Sunil Ravia04bd252022-05-02 22:54:18 -07002698 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002699 hostapd_chan_switch_config(iface->bss[i],
2700 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002701
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002702 ret = hostapd_switch_channel(iface->bss[i], &settings);
2703 if (ret) {
2704 /* FIX: What do we do if CSA fails in the middle of
2705 * submitting multi-BSS CSA requests? */
2706 return ret;
2707 }
2708 }
2709
2710 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002711#else /* NEED_AP_MLME */
2712 return -1;
2713#endif /* NEED_AP_MLME */
2714}
2715
2716
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002717static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2718 int reply_size, const char *param)
2719{
2720#ifdef RADIUS_SERVER
2721 if (os_strcmp(param, "radius_server") == 0) {
2722 return radius_server_get_mib(hapd->radius_srv, reply,
2723 reply_size);
2724 }
2725#endif /* RADIUS_SERVER */
2726 return -1;
2727}
2728
2729
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002730static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2731 char *buf, size_t buflen)
2732{
2733 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002734 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002735 u8 *data = NULL;
2736 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002737 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002738 struct wpabuf *reply;
2739 size_t data_len = 0;
2740
Hai Shalom60840252021-02-19 19:02:11 -08002741 /**
2742 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2743 * [nested=<0|1>]
2744 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002745 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002746 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002747 return -EINVAL;
2748
2749 subcmd = strtoul(pos, &pos, 10);
2750
2751 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002752 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002753 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002754
2755 temp = os_strchr(pos, ' ');
2756 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002757 }
2758
2759 if (data_len) {
2760 data_len /= 2;
2761 data = os_malloc(data_len);
2762 if (!data)
2763 return -ENOBUFS;
2764
2765 if (hexstr2bin(pos, data, data_len)) {
2766 wpa_printf(MSG_DEBUG,
2767 "Vendor command: wrong parameter format");
2768 os_free(data);
2769 return -EINVAL;
2770 }
2771 }
2772
Hai Shalom60840252021-02-19 19:02:11 -08002773 pos = os_strstr(cmd, "nested=");
2774 if (pos)
2775 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2776 NESTED_ATTR_NOT_USED;
2777
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002778 reply = wpabuf_alloc((buflen - 1) / 2);
2779 if (!reply) {
2780 os_free(data);
2781 return -ENOBUFS;
2782 }
2783
2784 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002785 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002786
2787 if (ret == 0)
2788 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2789 wpabuf_len(reply));
2790
2791 wpabuf_free(reply);
2792 os_free(data);
2793
2794 return ret;
2795}
2796
2797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002798static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2799 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002801 u8 addr[ETH_ALEN];
2802 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002804 if (hwaddr_aton(cmd, addr))
2805 return -1;
2806
2807 sta = ap_get_sta(hapd, addr);
2808 if (!sta || !sta->eapol_sm)
2809 return -1;
2810
2811 eapol_auth_reauthenticate(sta->eapol_sm);
2812 return 0;
2813}
2814
2815
2816static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2817{
2818 u8 addr[ETH_ALEN];
2819 struct sta_info *sta;
2820 char *pos = cmd, *param;
2821
2822 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2823 return -1;
2824 pos += 18;
2825 param = pos;
2826 pos = os_strchr(pos, ' ');
2827 if (!pos)
2828 return -1;
2829 *pos++ = '\0';
2830
2831 sta = ap_get_sta(hapd, addr);
2832 if (!sta || !sta->eapol_sm)
2833 return -1;
2834
2835 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2836}
2837
2838
2839static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2840 char *buf, size_t buflen)
2841{
2842 char *pos, *end, *stamp;
2843 int ret;
2844
2845 /* cmd: "LOG_LEVEL [<level>]" */
2846 if (*cmd == '\0') {
2847 pos = buf;
2848 end = buf + buflen;
2849 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2850 "Timestamp: %d\n",
2851 debug_level_str(wpa_debug_level),
2852 wpa_debug_timestamp);
2853 if (os_snprintf_error(end - pos, ret))
2854 ret = 0;
2855
2856 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002859 while (*cmd == ' ')
2860 cmd++;
2861
2862 stamp = os_strchr(cmd, ' ');
2863 if (stamp) {
2864 *stamp++ = '\0';
2865 while (*stamp == ' ') {
2866 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 }
2869
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002870 if (os_strlen(cmd)) {
2871 int level = str_to_debug_level(cmd);
2872 if (level < 0)
2873 return -1;
2874 wpa_debug_level = level;
2875 }
2876
2877 if (stamp && os_strlen(stamp))
2878 wpa_debug_timestamp = atoi(stamp);
2879
2880 os_memcpy(buf, "OK\n", 3);
2881 return 3;
2882}
2883
2884
2885#ifdef NEED_AP_MLME
2886static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2887 char *buf, size_t buflen)
2888{
2889 struct hostapd_iface *iface = hapd->iface;
2890 char *pos, *end;
2891 struct hostapd_sta_info *info;
2892 struct os_reltime now;
2893
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002894 if (!iface->num_sta_seen)
2895 return 0;
2896
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002897 sta_track_expire(iface, 0);
2898
2899 pos = buf;
2900 end = buf + buflen;
2901
2902 os_get_reltime(&now);
2903 dl_list_for_each_reverse(info, &iface->sta_seen,
2904 struct hostapd_sta_info, list) {
2905 struct os_reltime age;
2906 int ret;
2907
2908 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002909 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2910 MAC2STR(info->addr), (unsigned int) age.sec,
2911 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002912 if (os_snprintf_error(end - pos, ret))
2913 break;
2914 pos += ret;
2915 }
2916
2917 return pos - buf;
2918}
2919#endif /* NEED_AP_MLME */
2920
2921
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002922static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2923 const char *cmd)
2924{
2925 u8 addr[ETH_ALEN];
2926
2927 if (hwaddr_aton(cmd, addr)) {
2928 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2929 return -1;
2930 }
2931
2932 return hostapd_send_lci_req(hapd, addr);
2933}
2934
2935
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002936static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002937{
2938 u8 addr[ETH_ALEN];
2939 char *token, *context = NULL;
2940 int random_interval, min_ap;
2941 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2942 unsigned int n_responders;
2943
2944 token = str_token(cmd, " ", &context);
2945 if (!token || hwaddr_aton(token, addr)) {
2946 wpa_printf(MSG_INFO,
2947 "CTRL: REQ_RANGE - Bad destination address");
2948 return -1;
2949 }
2950
2951 token = str_token(cmd, " ", &context);
2952 if (!token)
2953 return -1;
2954
2955 random_interval = atoi(token);
2956 if (random_interval < 0 || random_interval > 0xffff)
2957 return -1;
2958
2959 token = str_token(cmd, " ", &context);
2960 if (!token)
2961 return -1;
2962
2963 min_ap = atoi(token);
2964 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2965 return -1;
2966
2967 n_responders = 0;
2968 while ((token = str_token(cmd, " ", &context))) {
2969 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2970 wpa_printf(MSG_INFO,
2971 "CTRL: REQ_RANGE: Too many responders");
2972 return -1;
2973 }
2974
2975 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2976 wpa_printf(MSG_INFO,
2977 "CTRL: REQ_RANGE: Bad responder address");
2978 return -1;
2979 }
2980
2981 n_responders++;
2982 }
2983
2984 if (!n_responders) {
2985 wpa_printf(MSG_INFO,
2986 "CTRL: REQ_RANGE - No FTM responder address");
2987 return -1;
2988 }
2989
2990 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2991 responders, n_responders);
2992}
2993
2994
Dmitry Shmidt29333592017-01-09 12:27:11 -08002995static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2996 const char *cmd, char *reply,
2997 size_t reply_size)
2998{
2999 u8 addr[ETH_ALEN];
3000 const char *pos;
3001 struct wpabuf *req;
3002 int ret;
3003 u8 req_mode = 0;
3004
3005 if (hwaddr_aton(cmd, addr))
3006 return -1;
3007 pos = os_strchr(cmd, ' ');
3008 if (!pos)
3009 return -1;
3010 pos++;
3011 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3012 int val = hex2byte(pos + 9);
3013
3014 if (val < 0)
3015 return -1;
3016 req_mode = val;
3017 pos += 11;
3018 pos = os_strchr(pos, ' ');
3019 if (!pos)
3020 return -1;
3021 pos++;
3022 }
3023 req = wpabuf_parse_bin(pos);
3024 if (!req)
3025 return -1;
3026
3027 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3028 wpabuf_free(req);
3029 if (ret >= 0)
3030 ret = os_snprintf(reply, reply_size, "%d", ret);
3031 return ret;
3032}
3033
3034
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003035static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3036 char *buf, size_t buflen)
3037{
3038 if (!(hapd->conf->radio_measurements[0] &
3039 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3040 wpa_printf(MSG_ERROR,
3041 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3042 return -1;
3043 }
3044
3045 return hostapd_neighbor_show(hapd, buf, buflen);
3046}
3047
3048
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003049static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3050{
3051 struct wpa_ssid_value ssid;
3052 u8 bssid[ETH_ALEN];
3053 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003054 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003055 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003056 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003057 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003058
3059 if (!(hapd->conf->radio_measurements[0] &
3060 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3061 wpa_printf(MSG_ERROR,
3062 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3063 return -1;
3064 }
3065
3066 if (hwaddr_aton(buf, bssid)) {
3067 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3068 return -1;
3069 }
3070
3071 tmp = os_strstr(buf, "ssid=");
3072 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3073 wpa_printf(MSG_ERROR,
3074 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3075 return -1;
3076 }
3077 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3078 if (!buf)
3079 return -1;
3080
3081 tmp = os_strstr(buf, "nr=");
3082 if (!tmp) {
3083 wpa_printf(MSG_ERROR,
3084 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3085 return -1;
3086 }
3087
3088 buf = os_strchr(tmp, ' ');
3089 if (buf)
3090 *buf++ = '\0';
3091
3092 nr = wpabuf_parse_bin(tmp + 3);
3093 if (!nr) {
3094 wpa_printf(MSG_ERROR,
3095 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3096 return -1;
3097 }
3098
3099 if (!buf)
3100 goto set;
3101
3102 tmp = os_strstr(buf, "lci=");
3103 if (tmp) {
3104 buf = os_strchr(tmp, ' ');
3105 if (buf)
3106 *buf++ = '\0';
3107 lci = wpabuf_parse_bin(tmp + 4);
3108 if (!lci) {
3109 wpa_printf(MSG_ERROR,
3110 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003111 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003112 }
3113 }
3114
3115 if (!buf)
3116 goto set;
3117
3118 tmp = os_strstr(buf, "civic=");
3119 if (tmp) {
3120 buf = os_strchr(tmp, ' ');
3121 if (buf)
3122 *buf++ = '\0';
3123 civic = wpabuf_parse_bin(tmp + 6);
3124 if (!civic) {
3125 wpa_printf(MSG_ERROR,
3126 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003127 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003128 }
3129 }
3130
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003131 if (!buf)
3132 goto set;
3133
3134 if (os_strstr(buf, "stat"))
3135 stationary = 1;
3136
Hai Shaloma20dcd72022-02-04 13:43:00 -08003137 tmp = os_strstr(buf, "bss_parameter=");
3138 if (tmp) {
3139 bss_parameters = atoi(tmp + 14);
3140 if (bss_parameters < 0 || bss_parameters > 0xff) {
3141 wpa_printf(MSG_ERROR,
3142 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3143 goto fail;
3144 }
3145 }
3146
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003147set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003148 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003149 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003150
Hai Shaloma20dcd72022-02-04 13:43:00 -08003151fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003152 wpabuf_free(nr);
3153 wpabuf_free(lci);
3154 wpabuf_free(civic);
3155
3156 return ret;
3157}
3158
3159
3160static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3161 char *buf)
3162{
3163 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003164 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003165 u8 bssid[ETH_ALEN];
3166 char *tmp;
3167
3168 if (hwaddr_aton(buf, bssid)) {
3169 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3170 return -1;
3171 }
3172
3173 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003174 if (tmp) {
3175 ssidp = &ssid;
3176 if (ssid_parse(tmp + 5, &ssid)) {
3177 wpa_printf(MSG_ERROR,
3178 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3179 return -1;
3180 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003181 }
3182
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003183 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003184}
3185
3186
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003187static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3188 size_t buflen)
3189{
3190 int ret, i;
3191 char *pos, *end;
3192
3193 ret = os_snprintf(buf, buflen, "%016llX:\n",
3194 (long long unsigned) iface->drv_flags);
3195 if (os_snprintf_error(buflen, ret))
3196 return -1;
3197
3198 pos = buf + ret;
3199 end = buf + buflen;
3200
3201 for (i = 0; i < 64; i++) {
3202 if (iface->drv_flags & (1LLU << i)) {
3203 ret = os_snprintf(pos, end - pos, "%s\n",
3204 driver_flag_to_string(1LLU << i));
3205 if (os_snprintf_error(end - pos, ret))
3206 return -1;
3207 pos += ret;
3208 }
3209 }
3210
3211 return pos - buf;
3212}
3213
3214
Hai Shalomb755a2a2020-04-23 21:49:02 -07003215static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3216 size_t buflen)
3217{
3218 int ret, i;
3219 char *pos, *end;
3220
3221 ret = os_snprintf(buf, buflen, "%016llX:\n",
3222 (long long unsigned) iface->drv_flags2);
3223 if (os_snprintf_error(buflen, ret))
3224 return -1;
3225
3226 pos = buf + ret;
3227 end = buf + buflen;
3228
3229 for (i = 0; i < 64; i++) {
3230 if (iface->drv_flags2 & (1LLU << i)) {
3231 ret = os_snprintf(pos, end - pos, "%s\n",
3232 driver_flag2_to_string(1LLU << i));
3233 if (os_snprintf_error(end - pos, ret))
3234 return -1;
3235 pos += ret;
3236 }
3237 }
3238
3239 return pos - buf;
3240}
3241
3242
Hai Shalom021b0b52019-04-10 11:17:58 -07003243static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3244 const char *field, char *buf,
3245 size_t buflen)
3246{
3247 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3248
3249#ifdef CONFIG_DPP
3250 if (os_strcmp(field, "dpp") == 0) {
3251 int res;
3252
Hai Shaloma20dcd72022-02-04 13:43:00 -08003253#ifdef CONFIG_DPP3
3254 res = os_snprintf(buf, buflen, "DPP=3");
3255#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003256 res = os_snprintf(buf, buflen, "DPP=2");
3257#else /* CONFIG_DPP2 */
3258 res = os_snprintf(buf, buflen, "DPP=1");
3259#endif /* CONFIG_DPP2 */
3260 if (os_snprintf_error(buflen, res))
3261 return -1;
3262 return res;
3263 }
3264#endif /* CONFIG_DPP */
3265
3266 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3267 field);
3268
3269 return -1;
3270}
3271
3272
Hai Shaloma20dcd72022-02-04 13:43:00 -08003273#ifdef ANDROID
3274static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3275 char *buf, size_t buflen)
3276{
3277 int ret;
3278
3279 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3280 if (ret == 0) {
3281 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3282 if (os_snprintf_error(buflen, ret))
3283 ret = -1;
3284 }
3285 return ret;
3286}
3287#endif /* ANDROID */
3288
3289
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003290static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3291 char *buf, char *reply,
3292 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003293 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003294 socklen_t fromlen)
3295{
3296 int reply_len, res;
3297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 os_memcpy(reply, "OK\n", 3);
3299 reply_len = 3;
3300
3301 if (os_strcmp(buf, "PING") == 0) {
3302 os_memcpy(reply, "PONG\n", 5);
3303 reply_len = 5;
3304 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3305 if (wpa_debug_reopen_file() < 0)
3306 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003307 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
3308 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003309 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3310 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003311 } else if (os_strcmp(buf, "STATUS") == 0) {
3312 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3313 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003314 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3315 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 } else if (os_strcmp(buf, "MIB") == 0) {
3317 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3318 if (reply_len >= 0) {
3319 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3320 reply_size - reply_len);
3321 if (res < 0)
3322 reply_len = -1;
3323 else
3324 reply_len += res;
3325 }
3326 if (reply_len >= 0) {
3327 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3328 reply_size - reply_len);
3329 if (res < 0)
3330 reply_len = -1;
3331 else
3332 reply_len += res;
3333 }
3334#ifndef CONFIG_NO_RADIUS
3335 if (reply_len >= 0) {
3336 res = radius_client_get_mib(hapd->radius,
3337 reply + reply_len,
3338 reply_size - reply_len);
3339 if (res < 0)
3340 reply_len = -1;
3341 else
3342 reply_len += res;
3343 }
3344#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003345 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3346 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3347 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3349 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3350 reply_size);
3351 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3352 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3353 reply_size);
3354 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3355 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3356 reply_size);
3357 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003358 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3359 reply_len = -1;
3360 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3361 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 reply_len = -1;
3363 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003364 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365 reply_len = -1;
3366 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003367 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003368 buf + 6))
3369 reply_len = -1;
3370 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3371 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3372 reply_len = -1;
3373 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3374 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3375 reply_len = -1;
3376 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3377 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3378 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003379#ifdef CONFIG_TAXONOMY
3380 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3381 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3382 reply, reply_size);
3383#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003384 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3385 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3386 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003387 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3388 if (hostapd_ctrl_iface_stop_ap(hapd))
3389 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390#ifdef NEED_AP_MLME
3391 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3392 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3393 reply_len = -1;
3394#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395#ifdef CONFIG_WPS
3396 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3397 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3398 reply_len = -1;
3399 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3400 reply_len = hostapd_ctrl_iface_wps_check_pin(
3401 hapd, buf + 14, reply, reply_size);
3402 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3403 if (hostapd_wps_button_pushed(hapd, NULL))
3404 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003405 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3406 if (hostapd_wps_cancel(hapd))
3407 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3409 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3410 reply, reply_size);
3411 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3412 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3413 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003414 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3415 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3416 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003417#ifdef CONFIG_WPS_NFC
3418 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3419 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3420 reply_len = -1;
3421 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3422 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3423 hapd, buf + 21, reply, reply_size);
3424 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3425 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3426 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003427 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3428 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3429 hapd, buf + 21, reply, reply_size);
3430 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3431 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3432 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003433#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003435#ifdef CONFIG_INTERWORKING
3436 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3437 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3438 reply_len = -1;
3439 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3440 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3441 reply_len = -1;
3442#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003443#ifdef CONFIG_HS20
3444 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3445 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3446 reply_len = -1;
3447 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3448 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3449 reply_len = -1;
3450#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003451#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003452 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3453 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3454 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3456 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3457 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003458 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3459 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3460 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003461 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3462 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3463 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003464#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3466 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3467 reply_size);
3468 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3469 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3470 reply_len = -1;
3471 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3472 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3473 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003474 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3475 if (hostapd_ctrl_iface_enable(hapd->iface))
3476 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003477 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3478 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3479 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003480 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
3481 if (hostapd_ctrl_iface_reload_bss(hapd))
3482 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003483 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3484 if (hostapd_ctrl_iface_reload(hapd->iface))
3485 reply_len = -1;
3486 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3487 if (hostapd_ctrl_iface_disable(hapd->iface))
3488 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003489 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3490 if (ieee802_11_set_beacon(hapd))
3491 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003492#ifdef CONFIG_TESTING_OPTIONS
3493 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3494 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3495 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003496 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3497 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3498 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003499 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3500 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3501 buf + 23) < 0)
3502 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003503 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3504 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3505 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003506 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3507 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3508 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003509 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3510 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3511 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003512 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3513 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3514 reply_len = -1;
3515 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3516 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3517 reply_len = -1;
3518 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3519 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3520 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003521 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3522 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3523 reply_len = -1;
3524 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3525 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3526 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003527 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3528 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3529 reply_len = -1;
3530 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3531 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003532 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3533 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3534 reply_len = -1;
3535 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3536 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3537 reply_len = -1;
3538 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3539 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3540 reply_len = -1;
3541 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3542 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3543 reply_len = -1;
3544 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3545 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3546 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003547 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3548 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3549 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003550 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3551 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3552 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003553 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3554 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3555 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003556 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3557 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3558 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003559#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003560 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003561 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003562 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003563 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3564 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3565 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003566 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3567 ieee802_1x_erp_flush(hapd);
3568#ifdef RADIUS_SERVER
3569 radius_server_erp_flush(hapd->radius_srv);
3570#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003571 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3572 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3573 reply_len = -1;
3574 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3575 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3576 reply_len = -1;
3577 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3578 reply_len = hostapd_ctrl_iface_log_level(
3579 hapd, buf + 9, reply, reply_size);
3580#ifdef NEED_AP_MLME
3581 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3582 reply_len = hostapd_ctrl_iface_track_sta_list(
3583 hapd, reply, reply_size);
3584#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003585 } else if (os_strcmp(buf, "PMKSA") == 0) {
3586 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3587 reply_size);
3588 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3589 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003590 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3591 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3592 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003593 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3594 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3595 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003596 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3597 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3598 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003599 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3600 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3601 reply_len = -1;
3602 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3603 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3604 reply_len = -1;
3605 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3606 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3607 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003608 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3609 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3610 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003611 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3612 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3613 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003614 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3615 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3616 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003617 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3618 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003619 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3620 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003621 if (hostapd_ctrl_iface_acl_add_mac(
3622 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003623 &hapd->conf->num_accept_mac, buf + 19) ||
3624 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003625 reply_len = -1;
3626 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003627 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003628 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003629 &hapd->conf->num_accept_mac, buf + 19) ||
3630 hostapd_set_acl(hapd) ||
3631 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003632 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003633 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3634 reply_len = hostapd_ctrl_iface_acl_show_mac(
3635 hapd->conf->accept_mac,
3636 hapd->conf->num_accept_mac, reply, reply_size);
3637 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3638 hostapd_ctrl_iface_acl_clear_list(
3639 &hapd->conf->accept_mac,
3640 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003641 if (hostapd_set_acl(hapd) ||
3642 hostapd_disassoc_accept_mac(hapd))
3643 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07003644 } else {
3645 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003646 }
3647 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3648 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003649 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003650 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003651 &hapd->conf->num_deny_mac, buf + 17) ||
3652 hostapd_set_acl(hapd) ||
3653 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003654 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003655 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003656 if (hostapd_ctrl_iface_acl_del_mac(
3657 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003658 &hapd->conf->num_deny_mac, buf + 17) ||
3659 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003660 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003661 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3662 reply_len = hostapd_ctrl_iface_acl_show_mac(
3663 hapd->conf->deny_mac,
3664 hapd->conf->num_deny_mac, reply, reply_size);
3665 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3666 hostapd_ctrl_iface_acl_clear_list(
3667 &hapd->conf->deny_mac,
3668 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003669 if (hostapd_set_acl(hapd))
3670 reply_len = -1;
Sunil Ravi036cec52023-03-29 11:35:17 -07003671 } else {
3672 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003673 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003674#ifdef CONFIG_DPP
3675 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3676 res = hostapd_dpp_qr_code(hapd, buf + 12);
3677 if (res < 0) {
3678 reply_len = -1;
3679 } else {
3680 reply_len = os_snprintf(reply, reply_size, "%d", res);
3681 if (os_snprintf_error(reply_size, reply_len))
3682 reply_len = -1;
3683 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003684 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3685 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3686 if (res < 0) {
3687 reply_len = -1;
3688 } else {
3689 reply_len = os_snprintf(reply, reply_size, "%d", res);
3690 if (os_snprintf_error(reply_size, reply_len))
3691 reply_len = -1;
3692 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003693 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3694 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3695 if (res < 0) {
3696 reply_len = -1;
3697 } else {
3698 reply_len = os_snprintf(reply, reply_size, "%d", res);
3699 if (os_snprintf_error(reply_size, reply_len))
3700 reply_len = -1;
3701 }
3702 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3703 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3704 if (res < 0) {
3705 reply_len = -1;
3706 } else {
3707 reply_len = os_snprintf(reply, reply_size, "%d", res);
3708 if (os_snprintf_error(reply_size, reply_len))
3709 reply_len = -1;
3710 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003711 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003712 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003713 if (res < 0) {
3714 reply_len = -1;
3715 } else {
3716 reply_len = os_snprintf(reply, reply_size, "%d", res);
3717 if (os_snprintf_error(reply_size, reply_len))
3718 reply_len = -1;
3719 }
3720 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003721 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3722 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003723 reply_len = -1;
3724 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3725 const char *uri;
3726
Hai Shalom021b0b52019-04-10 11:17:58 -07003727 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3728 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003729 if (!uri) {
3730 reply_len = -1;
3731 } else {
3732 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3733 if (os_snprintf_error(reply_size, reply_len))
3734 reply_len = -1;
3735 }
3736 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003737 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3738 atoi(buf + 19),
3739 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003740 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3741 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3742 atoi(buf + 18),
3743 os_strchr(buf + 18, ' ')) < 0)
3744 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003745 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3746 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3747 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003748 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3749 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3750 reply_len = -1;
3751 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3752 hostapd_dpp_stop(hapd);
3753 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003754 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003755 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3756 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003757 if (res < 0) {
3758 reply_len = -1;
3759 } else {
3760 reply_len = os_snprintf(reply, reply_size, "%d", res);
3761 if (os_snprintf_error(reply_size, reply_len))
3762 reply_len = -1;
3763 }
Sunil Ravia04bd252022-05-02 22:54:18 -07003764 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
3765 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
3766 buf + 20) < 0)
3767 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003768 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003769 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3770 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003771 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003772 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003773 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003774 reply_len = -1;
3775 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003776 reply_len = dpp_configurator_get_key_id(
3777 hapd->iface->interfaces->dpp,
3778 atoi(buf + 25),
3779 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003780 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3781 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3782 if (res < 0) {
3783 reply_len = -1;
3784 } else {
3785 reply_len = os_snprintf(reply, reply_size, "%d", res);
3786 if (os_snprintf_error(reply_size, reply_len))
3787 reply_len = -1;
3788 }
3789 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3790 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3791 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003792#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003793 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3794 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3795 reply_len = -1;
3796 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3797 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3798 reply_len = -1;
3799 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3800 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003801 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3802 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3803 reply_len = -1;
3804 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3805 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07003806 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
3807 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
3808 reply_len = -1;
3809 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
3810 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003811#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07003812#ifdef CONFIG_DPP3
3813 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
3814 if (hostapd_dpp_push_button(hapd, NULL) < 0)
3815 reply_len = -1;
3816 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
3817 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
3818 reply_len = -1;
3819#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003820#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003821#ifdef RADIUS_SERVER
3822 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3823 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3824 reply_len = -1;
3825#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003826 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3827 reply_len = hostapd_ctrl_iface_get_capability(
3828 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08003829#ifdef CONFIG_PASN
3830 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3831 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3832#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003833#ifdef ANDROID
3834 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3835 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
3836 reply_size);
3837#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838 } else {
3839 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3840 reply_len = 16;
3841 }
3842
3843 if (reply_len < 0) {
3844 os_memcpy(reply, "FAIL\n", 5);
3845 reply_len = 5;
3846 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003847
3848 return reply_len;
3849}
3850
3851
3852static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3853 void *sock_ctx)
3854{
3855 struct hostapd_data *hapd = eloop_ctx;
3856 char buf[4096];
3857 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003858 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003859 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003860 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003861 const int reply_size = 4096;
3862 int reply_len;
3863 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003864#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003865 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003866#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003867
3868 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3869 (struct sockaddr *) &from, &fromlen);
3870 if (res < 0) {
3871 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3872 strerror(errno));
3873 return;
3874 }
3875 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003876
3877 reply = os_malloc(reply_size);
3878 if (reply == NULL) {
3879 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3880 fromlen) < 0) {
3881 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3882 strerror(errno));
3883 }
3884 return;
3885 }
3886
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003887#ifdef CONFIG_CTRL_IFACE_UDP
3888 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3889 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003890 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3891 hapd->ctrl_iface_cookie,
3892 CTRL_IFACE_COOKIE_LEN);
3893 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003894 goto done;
3895 }
3896
3897 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003898 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003899 wpa_printf(MSG_DEBUG,
3900 "CTRL: No cookie in the request - drop request");
3901 os_free(reply);
3902 return;
3903 }
3904
Hai Shalomfdcde762020-04-02 11:19:20 -07003905 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3906 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003907 wpa_printf(MSG_DEBUG,
3908 "CTRL: Invalid cookie in the request - drop request");
3909 os_free(reply);
3910 return;
3911 }
3912
Hai Shalomfdcde762020-04-02 11:19:20 -07003913 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003914 while (*pos == ' ')
3915 pos++;
3916#endif /* CONFIG_CTRL_IFACE_UDP */
3917
3918 if (os_strcmp(pos, "PING") == 0)
3919 level = MSG_EXCESSIVE;
3920 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3921
3922 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003923 reply, reply_size,
3924 &from, fromlen);
3925
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003926#ifdef CONFIG_CTRL_IFACE_UDP
3927done:
3928#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003929 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3930 fromlen) < 0) {
3931 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3932 strerror(errno));
3933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 os_free(reply);
3935}
3936
3937
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003938#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3940{
3941 char *buf;
3942 size_t len;
3943
3944 if (hapd->conf->ctrl_interface == NULL)
3945 return NULL;
3946
3947 len = os_strlen(hapd->conf->ctrl_interface) +
3948 os_strlen(hapd->conf->iface) + 2;
3949 buf = os_malloc(len);
3950 if (buf == NULL)
3951 return NULL;
3952
3953 os_snprintf(buf, len, "%s/%s",
3954 hapd->conf->ctrl_interface, hapd->conf->iface);
3955 buf[len - 1] = '\0';
3956 return buf;
3957}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003958#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959
3960
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003961static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3962 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 const char *txt, size_t len)
3964{
3965 struct hostapd_data *hapd = ctx;
3966 if (hapd == NULL)
3967 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003968 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969}
3970
3971
3972int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3973{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003974#ifdef CONFIG_CTRL_IFACE_UDP
3975 int port = HOSTAPD_CTRL_IFACE_PORT;
3976 char p[32] = { 0 };
3977 char port_str[40], *tmp;
3978 char *pos;
3979 struct addrinfo hints = { 0 }, *res, *saveres;
3980 int n;
3981
3982 if (hapd->ctrl_sock > -1) {
3983 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3984 return 0;
3985 }
3986
3987 if (hapd->conf->ctrl_interface == NULL)
3988 return 0;
3989
3990 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3991 if (pos) {
3992 pos += 4;
3993 port = atoi(pos);
3994 if (port <= 0) {
3995 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3996 goto fail;
3997 }
3998 }
3999
4000 dl_list_init(&hapd->ctrl_dst);
4001 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004002 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004003
4004#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4005 hints.ai_flags = AI_PASSIVE;
4006#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4007
4008#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4009 hints.ai_family = AF_INET6;
4010#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4011 hints.ai_family = AF_INET;
4012#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4013 hints.ai_socktype = SOCK_DGRAM;
4014
4015try_again:
4016 os_snprintf(p, sizeof(p), "%d", port);
4017 n = getaddrinfo(NULL, p, &hints, &res);
4018 if (n) {
4019 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4020 goto fail;
4021 }
4022
4023 saveres = res;
4024 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4025 res->ai_protocol);
4026 if (hapd->ctrl_sock < 0) {
4027 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4028 goto fail;
4029 }
4030
4031 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4032 port--;
4033 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4034 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4035 goto try_again;
4036 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4037 goto fail;
4038 }
4039
4040 freeaddrinfo(saveres);
4041
4042 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4043 tmp = os_strdup(port_str);
4044 if (tmp) {
4045 os_free(hapd->conf->ctrl_interface);
4046 hapd->conf->ctrl_interface = tmp;
4047 }
4048 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4049
4050 if (eloop_register_read_sock(hapd->ctrl_sock,
4051 hostapd_ctrl_iface_receive, hapd, NULL) <
4052 0) {
4053 hostapd_ctrl_iface_deinit(hapd);
4054 return -1;
4055 }
4056
4057 hapd->msg_ctx = hapd;
4058 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4059
4060 return 0;
4061
4062fail:
4063 if (hapd->ctrl_sock >= 0)
4064 close(hapd->ctrl_sock);
4065 return -1;
4066#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 struct sockaddr_un addr;
4068 int s = -1;
4069 char *fname = NULL;
4070
Dmitry Shmidt04949592012-07-19 12:16:46 -07004071 if (hapd->ctrl_sock > -1) {
4072 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4073 return 0;
4074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004076 dl_list_init(&hapd->ctrl_dst);
4077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 if (hapd->conf->ctrl_interface == NULL)
4079 return 0;
4080
4081 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4082 if (errno == EEXIST) {
4083 wpa_printf(MSG_DEBUG, "Using existing control "
4084 "interface directory.");
4085 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4087 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 goto fail;
4089 }
4090 }
4091
4092 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004093 lchown(hapd->conf->ctrl_interface, -1,
4094 hapd->conf->ctrl_interface_gid) < 0) {
4095 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 return -1;
4098 }
4099
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004100 if (!hapd->conf->ctrl_interface_gid_set &&
4101 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004102 lchown(hapd->conf->ctrl_interface, -1,
4103 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4104 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004105 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004106 return -1;
4107 }
4108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004109#ifdef ANDROID
4110 /*
4111 * Android is using umask 0077 which would leave the control interface
4112 * directory without group access. This breaks things since Wi-Fi
4113 * framework assumes that this directory can be accessed by other
4114 * applications in the wifi group. Fix this by adding group access even
4115 * if umask value would prevent this.
4116 */
4117 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4118 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4119 strerror(errno));
4120 /* Try to continue anyway */
4121 }
4122#endif /* ANDROID */
4123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4125 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4126 goto fail;
4127
4128 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4129 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004130 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 goto fail;
4132 }
4133
4134 os_memset(&addr, 0, sizeof(addr));
4135#ifdef __FreeBSD__
4136 addr.sun_len = sizeof(addr);
4137#endif /* __FreeBSD__ */
4138 addr.sun_family = AF_UNIX;
4139 fname = hostapd_ctrl_iface_path(hapd);
4140 if (fname == NULL)
4141 goto fail;
4142 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4143 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4144 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4145 strerror(errno));
4146 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4147 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4148 " allow connections - assuming it was left"
4149 "over from forced program termination");
4150 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004151 wpa_printf(MSG_ERROR,
4152 "Could not unlink existing ctrl_iface socket '%s': %s",
4153 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154 goto fail;
4155 }
4156 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4157 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004158 wpa_printf(MSG_ERROR,
4159 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4160 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 goto fail;
4162 }
4163 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4164 "ctrl_iface socket '%s'", fname);
4165 } else {
4166 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4167 "be in use - cannot override it");
4168 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4169 "not used anymore", fname);
4170 os_free(fname);
4171 fname = NULL;
4172 goto fail;
4173 }
4174 }
4175
4176 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004177 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4178 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004179 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 goto fail;
4181 }
4182
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004183 if (!hapd->conf->ctrl_interface_gid_set &&
4184 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004185 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4186 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004187 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004188 goto fail;
4189 }
4190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004192 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4193 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 goto fail;
4195 }
4196 os_free(fname);
4197
4198 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004199 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4200 NULL) < 0) {
4201 hostapd_ctrl_iface_deinit(hapd);
4202 return -1;
4203 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004204 hapd->msg_ctx = hapd;
4205 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4206
4207 return 0;
4208
4209fail:
4210 if (s >= 0)
4211 close(s);
4212 if (fname) {
4213 unlink(fname);
4214 os_free(fname);
4215 }
4216 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004217#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218}
4219
4220
4221void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4222{
4223 struct wpa_ctrl_dst *dst, *prev;
4224
4225 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004226#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004228#endif /* !CONFIG_CTRL_IFACE_UDP */
4229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 eloop_unregister_read_sock(hapd->ctrl_sock);
4231 close(hapd->ctrl_sock);
4232 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004233#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 fname = hostapd_ctrl_iface_path(hapd);
4235 if (fname)
4236 unlink(fname);
4237 os_free(fname);
4238
4239 if (hapd->conf->ctrl_interface &&
4240 rmdir(hapd->conf->ctrl_interface) < 0) {
4241 if (errno == ENOTEMPTY) {
4242 wpa_printf(MSG_DEBUG, "Control interface "
4243 "directory not empty - leaving it "
4244 "behind");
4245 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004246 wpa_printf(MSG_ERROR,
4247 "rmdir[ctrl_interface=%s]: %s",
4248 hapd->conf->ctrl_interface,
4249 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250 }
4251 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004252#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 }
4254
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004255 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4256 list)
4257 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004258
4259#ifdef CONFIG_TESTING_OPTIONS
4260 l2_packet_deinit(hapd->l2_test);
4261 hapd->l2_test = NULL;
4262#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263}
4264
4265
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004266static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4267 char *buf)
4268{
4269 if (hostapd_add_iface(interfaces, buf) < 0) {
4270 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4271 return -1;
4272 }
4273 return 0;
4274}
4275
4276
4277static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4278 char *buf)
4279{
4280 if (hostapd_remove_iface(interfaces, buf) < 0) {
4281 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4282 return -1;
4283 }
4284 return 0;
4285}
4286
4287
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004288static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004289 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004290 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004291{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004292 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4293 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004294}
4295
4296
4297static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004298 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004299 socklen_t fromlen)
4300{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004301 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004302}
4303
4304
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004305static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4306{
4307#ifdef CONFIG_WPS_TESTING
4308 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004309 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004310 wps_corrupt_pkhash = 0;
4311#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004312
4313#ifdef CONFIG_TESTING_OPTIONS
4314#ifdef CONFIG_DPP
4315 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004316#ifdef CONFIG_DPP3
4317 dpp_version_override = 3;
4318#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004319 dpp_version_override = 2;
4320#else /* CONFIG_DPP2 */
4321 dpp_version_override = 1;
4322#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004323#endif /* CONFIG_DPP */
4324#endif /* CONFIG_TESTING_OPTIONS */
4325
4326#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004327 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004328#ifdef CONFIG_DPP3
4329 {
4330 int i;
4331
4332 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4333 struct dpp_pb_info *info;
4334
4335 info = &interfaces->dpp_pb[i];
4336 info->rx_time.sec = 0;
4337 info->rx_time.usec = 0;
4338 }
4339 }
4340#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004341#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004342}
4343
4344
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004345#ifdef CONFIG_FST
4346
4347static int
4348hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4349 const char *cmd)
4350{
4351 char ifname[IFNAMSIZ + 1];
4352 struct fst_iface_cfg cfg;
4353 struct hostapd_data *hapd;
4354 struct fst_wpa_obj iface_obj;
4355
4356 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4357 hapd = hostapd_get_iface(interfaces, ifname);
4358 if (hapd) {
4359 if (hapd->iface->fst) {
4360 wpa_printf(MSG_INFO, "FST: Already attached");
4361 return -1;
4362 }
4363 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4364 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4365 &iface_obj, &cfg);
4366 if (hapd->iface->fst)
4367 return 0;
4368 }
4369 }
4370
4371 return -EINVAL;
4372}
4373
4374
4375static int
4376hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4377 const char *cmd)
4378{
4379 char ifname[IFNAMSIZ + 1];
4380 struct hostapd_data * hapd;
4381
4382 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4383 hapd = hostapd_get_iface(interfaces, ifname);
4384 if (hapd) {
4385 if (!fst_iface_detach(ifname)) {
4386 hapd->iface->fst = NULL;
4387 hapd->iface->fst_ies = NULL;
4388 return 0;
4389 }
4390 }
4391 }
4392
4393 return -EINVAL;
4394}
4395
4396#endif /* CONFIG_FST */
4397
4398
4399static struct hostapd_data *
4400hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4401 const char *ifname)
4402{
4403 size_t i, j;
4404
4405 for (i = 0; i < interfaces->count; i++) {
4406 struct hostapd_iface *iface = interfaces->iface[i];
4407
4408 for (j = 0; j < iface->num_bss; j++) {
4409 struct hostapd_data *hapd;
4410
4411 hapd = iface->bss[j];
4412 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4413 return hapd;
4414 }
4415 }
4416
4417 return NULL;
4418}
4419
4420
4421static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4422 struct hostapd_data *dst_hapd,
4423 const char *param)
4424{
4425 int res;
4426 char *value;
4427
4428 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4429 if (!value) {
4430 wpa_printf(MSG_ERROR,
4431 "DUP: cannot allocate buffer to stringify %s",
4432 param);
4433 goto error_return;
4434 }
4435
4436 if (os_strcmp(param, "wpa") == 0) {
4437 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4438 src_hapd->conf->wpa);
4439 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4440 src_hapd->conf->wpa_key_mgmt) {
4441 res = hostapd_ctrl_iface_get_key_mgmt(
4442 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4443 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4444 goto error_stringify;
4445 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4446 src_hapd->conf->wpa_pairwise) {
4447 res = wpa_write_ciphers(value,
4448 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4449 src_hapd->conf->wpa_pairwise, " ");
4450 if (res < 0)
4451 goto error_stringify;
4452 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4453 src_hapd->conf->rsn_pairwise) {
4454 res = wpa_write_ciphers(value,
4455 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4456 src_hapd->conf->rsn_pairwise, " ");
4457 if (res < 0)
4458 goto error_stringify;
4459 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4460 src_hapd->conf->ssid.wpa_passphrase) {
4461 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4462 src_hapd->conf->ssid.wpa_passphrase);
4463 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4464 src_hapd->conf->ssid.wpa_psk_set) {
4465 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4466 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4467 } else {
4468 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4469 goto error_return;
4470 }
4471
4472 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4473 os_free(value);
4474 return res;
4475
4476error_stringify:
4477 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4478error_return:
4479 os_free(value);
4480 return -1;
4481}
4482
4483
4484static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004485hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4486 const char *input,
4487 char *reply, int reply_size)
4488{
4489 size_t i, j;
4490 int res;
4491 char *pos, *end;
4492 struct hostapd_iface *iface;
4493 int show_ctrl = 0;
4494
4495 if (input)
4496 show_ctrl = !!os_strstr(input, "ctrl");
4497
4498 pos = reply;
4499 end = reply + reply_size;
4500
4501 for (i = 0; i < interfaces->count; i++) {
4502 iface = interfaces->iface[i];
4503
4504 for (j = 0; j < iface->num_bss; j++) {
4505 struct hostapd_bss_config *conf;
4506
4507 conf = iface->conf->bss[j];
4508 if (show_ctrl)
4509 res = os_snprintf(pos, end - pos,
4510 "%s ctrl_iface=%s\n",
4511 conf->iface,
4512 conf->ctrl_interface ?
4513 conf->ctrl_interface : "N/A");
4514 else
4515 res = os_snprintf(pos, end - pos, "%s\n",
4516 conf->iface);
4517 if (os_snprintf_error(end - pos, res)) {
4518 *pos = '\0';
4519 return pos - reply;
4520 }
4521 pos += res;
4522 }
4523 }
4524
4525 return pos - reply;
4526}
4527
4528
4529static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004530hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4531 char *cmd)
4532{
4533 char *p_start = cmd, *p_end;
4534 struct hostapd_data *src_hapd, *dst_hapd;
4535
4536 /* cmd: "<src ifname> <dst ifname> <variable name> */
4537
4538 p_end = os_strchr(p_start, ' ');
4539 if (!p_end) {
4540 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4541 cmd);
4542 return -1;
4543 }
4544
4545 *p_end = '\0';
4546 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4547 if (!src_hapd) {
4548 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4549 p_start);
4550 return -1;
4551 }
4552
4553 p_start = p_end + 1;
4554 p_end = os_strchr(p_start, ' ');
4555 if (!p_end) {
4556 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4557 cmd);
4558 return -1;
4559 }
4560
4561 *p_end = '\0';
4562 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4563 if (!dst_hapd) {
4564 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4565 p_start);
4566 return -1;
4567 }
4568
4569 p_start = p_end + 1;
4570 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4571}
4572
4573
4574static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4575 const char *ifname,
4576 char *buf, char *reply,
4577 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004578 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 socklen_t fromlen)
4580{
4581 struct hostapd_data *hapd;
4582
4583 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4584 if (hapd == NULL) {
4585 int res;
4586
4587 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4588 if (os_snprintf_error(reply_size, res))
4589 return -1;
4590 return res;
4591 }
4592
4593 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4594 from, fromlen);
4595}
4596
4597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004598static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4599 void *sock_ctx)
4600{
Hai Shalomfdcde762020-04-02 11:19:20 -07004601 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004602 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004603 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004604 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004605 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004606 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004607 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004608 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004609#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004610 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004611#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004612
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004613 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004614 (struct sockaddr *) &from, &fromlen);
4615 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004616 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4617 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004618 return;
4619 }
4620 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004621 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004622
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004623 reply = os_malloc(reply_size);
4624 if (reply == NULL) {
4625 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4626 fromlen) < 0) {
4627 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4628 strerror(errno));
4629 }
4630 return;
4631 }
4632
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004633 os_memcpy(reply, "OK\n", 3);
4634 reply_len = 3;
4635
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004636#ifdef CONFIG_CTRL_IFACE_UDP
4637 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4638 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004639 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4640 interfaces->ctrl_iface_cookie,
4641 CTRL_IFACE_COOKIE_LEN);
4642 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004643 goto send_reply;
4644 }
4645
4646 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004647 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004648 wpa_printf(MSG_DEBUG,
4649 "CTRL: No cookie in the request - drop request");
4650 os_free(reply);
4651 return;
4652 }
4653
Hai Shalomfdcde762020-04-02 11:19:20 -07004654 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4655 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004656 wpa_printf(MSG_DEBUG,
4657 "CTRL: Invalid cookie in the request - drop request");
4658 os_free(reply);
4659 return;
4660 }
4661
Hai Shalomfdcde762020-04-02 11:19:20 -07004662 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004663 while (*buf == ' ')
4664 buf++;
4665#endif /* CONFIG_CTRL_IFACE_UDP */
4666
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004667 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4668 char *pos = os_strchr(buf + 7, ' ');
4669
4670 if (pos) {
4671 *pos++ = '\0';
4672 reply_len = hostapd_global_ctrl_iface_ifname(
4673 interfaces, buf + 7, pos, reply, reply_size,
4674 &from, fromlen);
4675 goto send_reply;
4676 }
4677 }
4678
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004679 if (os_strcmp(buf, "PING") == 0) {
4680 os_memcpy(reply, "PONG\n", 5);
4681 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004682 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4683 if (wpa_debug_reopen_file() < 0)
4684 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004685 } else if (os_strcmp(buf, "FLUSH") == 0) {
4686 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004687 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4688 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4689 reply_len = -1;
4690 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4691 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4692 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004693 } else if (os_strcmp(buf, "ATTACH") == 0) {
4694 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004695 fromlen, NULL))
4696 reply_len = -1;
4697 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4698 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4699 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004700 reply_len = -1;
4701 } else if (os_strcmp(buf, "DETACH") == 0) {
4702 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4703 fromlen))
4704 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004705#ifdef CONFIG_MODULE_TESTS
4706 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004707 if (hapd_module_tests() < 0)
4708 reply_len = -1;
4709#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004710#ifdef CONFIG_FST
4711 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4712 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4713 reply_len = os_snprintf(reply, reply_size, "OK\n");
4714 else
4715 reply_len = -1;
4716 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4717 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4718 reply_len = os_snprintf(reply, reply_size, "OK\n");
4719 else
4720 reply_len = -1;
4721 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4722 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4723#endif /* CONFIG_FST */
4724 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4725 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4726 buf + 12))
4727 reply_len = os_snprintf(reply, reply_size, "OK\n");
4728 else
4729 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004730 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4731 reply_len = hostapd_global_ctrl_iface_interfaces(
4732 interfaces, buf + 10, reply, sizeof(buffer));
4733 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4734 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004735 } else {
4736 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4737 "ignored");
4738 reply_len = -1;
4739 }
4740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004741send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004742 if (reply_len < 0) {
4743 os_memcpy(reply, "FAIL\n", 5);
4744 reply_len = 5;
4745 }
4746
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004747 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4748 fromlen) < 0) {
4749 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4750 strerror(errno));
4751 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004752 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004753}
4754
4755
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004756#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004757static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4758{
4759 char *buf;
4760 size_t len;
4761
4762 if (interface->global_iface_path == NULL)
4763 return NULL;
4764
4765 len = os_strlen(interface->global_iface_path) +
4766 os_strlen(interface->global_iface_name) + 2;
4767 buf = os_malloc(len);
4768 if (buf == NULL)
4769 return NULL;
4770
4771 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4772 interface->global_iface_name);
4773 buf[len - 1] = '\0';
4774 return buf;
4775}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004776#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004777
4778
4779int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4780{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004781#ifdef CONFIG_CTRL_IFACE_UDP
4782 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4783 char p[32] = { 0 };
4784 char *pos;
4785 struct addrinfo hints = { 0 }, *res, *saveres;
4786 int n;
4787
4788 if (interface->global_ctrl_sock > -1) {
4789 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4790 return 0;
4791 }
4792
4793 if (interface->global_iface_path == NULL)
4794 return 0;
4795
4796 pos = os_strstr(interface->global_iface_path, "udp:");
4797 if (pos) {
4798 pos += 4;
4799 port = atoi(pos);
4800 if (port <= 0) {
4801 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4802 goto fail;
4803 }
4804 }
4805
Hai Shalomfdcde762020-04-02 11:19:20 -07004806 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004807
4808#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4809 hints.ai_flags = AI_PASSIVE;
4810#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4811
4812#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4813 hints.ai_family = AF_INET6;
4814#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4815 hints.ai_family = AF_INET;
4816#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4817 hints.ai_socktype = SOCK_DGRAM;
4818
4819try_again:
4820 os_snprintf(p, sizeof(p), "%d", port);
4821 n = getaddrinfo(NULL, p, &hints, &res);
4822 if (n) {
4823 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4824 goto fail;
4825 }
4826
4827 saveres = res;
4828 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4829 res->ai_protocol);
4830 if (interface->global_ctrl_sock < 0) {
4831 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4832 goto fail;
4833 }
4834
4835 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4836 0) {
4837 port++;
4838 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4839 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4840 goto try_again;
4841 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4842 goto fail;
4843 }
4844
4845 freeaddrinfo(saveres);
4846
4847 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4848
4849 if (eloop_register_read_sock(interface->global_ctrl_sock,
4850 hostapd_global_ctrl_iface_receive,
4851 interface, NULL) < 0) {
4852 hostapd_global_ctrl_iface_deinit(interface);
4853 return -1;
4854 }
4855
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004856 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4857
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004858 return 0;
4859
4860fail:
4861 if (interface->global_ctrl_sock >= 0)
4862 close(interface->global_ctrl_sock);
4863 return -1;
4864#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004865 struct sockaddr_un addr;
4866 int s = -1;
4867 char *fname = NULL;
4868
4869 if (interface->global_iface_path == NULL) {
4870 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4871 return 0;
4872 }
4873
4874 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4875 if (errno == EEXIST) {
4876 wpa_printf(MSG_DEBUG, "Using existing control "
4877 "interface directory.");
4878 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004879 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4880 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004881 goto fail;
4882 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004883 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004884 lchown(interface->global_iface_path, -1,
4885 interface->ctrl_iface_group) < 0) {
4886 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004887 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004888 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004889 }
4890
4891 if (os_strlen(interface->global_iface_path) + 1 +
4892 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4893 goto fail;
4894
4895 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4896 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004897 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004898 goto fail;
4899 }
4900
4901 os_memset(&addr, 0, sizeof(addr));
4902#ifdef __FreeBSD__
4903 addr.sun_len = sizeof(addr);
4904#endif /* __FreeBSD__ */
4905 addr.sun_family = AF_UNIX;
4906 fname = hostapd_global_ctrl_iface_path(interface);
4907 if (fname == NULL)
4908 goto fail;
4909 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4910 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4911 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4912 strerror(errno));
4913 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4914 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4915 " allow connections - assuming it was left"
4916 "over from forced program termination");
4917 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004918 wpa_printf(MSG_ERROR,
4919 "Could not unlink existing ctrl_iface socket '%s': %s",
4920 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004921 goto fail;
4922 }
4923 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4924 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004925 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4926 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004927 goto fail;
4928 }
4929 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4930 "ctrl_iface socket '%s'", fname);
4931 } else {
4932 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4933 "be in use - cannot override it");
4934 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4935 "not used anymore", fname);
4936 os_free(fname);
4937 fname = NULL;
4938 goto fail;
4939 }
4940 }
4941
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004942 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004943 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4944 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004945 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004946 goto fail;
4947 }
4948
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004949 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004950 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4951 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004952 goto fail;
4953 }
4954 os_free(fname);
4955
4956 interface->global_ctrl_sock = s;
4957 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4958 interface, NULL);
4959
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004960 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4961
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004962 return 0;
4963
4964fail:
4965 if (s >= 0)
4966 close(s);
4967 if (fname) {
4968 unlink(fname);
4969 os_free(fname);
4970 }
4971 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004972#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004973}
4974
4975
4976void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4977{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004978#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004979 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004980#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004981 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004982
4983 if (interfaces->global_ctrl_sock > -1) {
4984 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4985 close(interfaces->global_ctrl_sock);
4986 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004987#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004988 fname = hostapd_global_ctrl_iface_path(interfaces);
4989 if (fname) {
4990 unlink(fname);
4991 os_free(fname);
4992 }
4993
4994 if (interfaces->global_iface_path &&
4995 rmdir(interfaces->global_iface_path) < 0) {
4996 if (errno == ENOTEMPTY) {
4997 wpa_printf(MSG_DEBUG, "Control interface "
4998 "directory not empty - leaving it "
4999 "behind");
5000 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005001 wpa_printf(MSG_ERROR,
5002 "rmdir[ctrl_interface=%s]: %s",
5003 interfaces->global_iface_path,
5004 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005005 }
5006 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005007#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005008 }
5009
5010 os_free(interfaces->global_iface_path);
5011 interfaces->global_iface_path = NULL;
5012
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005013 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5014 struct wpa_ctrl_dst, list)
5015 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005016}
5017
5018
Roshan Pius3a1667e2018-07-03 15:17:14 -07005019static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5020 const char *buf)
5021{
5022 /* Enable Probe Request events based on explicit request.
5023 * Other events are enabled by default.
5024 */
5025 if (str_starts(buf, RX_PROBE_REQUEST))
5026 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5027 return 1;
5028}
5029
5030
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005031static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5032 const char *ifname, int level,
5033 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034{
5035 struct wpa_ctrl_dst *dst, *next;
5036 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005037 int idx, res;
5038 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 char levelstr[10];
5040
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005041 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 return;
5043
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005044 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5045 if (os_snprintf_error(sizeof(levelstr), res))
5046 return;
5047 idx = 0;
5048 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005049 io[idx].iov_base = "IFNAME=";
5050 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005051 idx++;
5052 io[idx].iov_base = (char *) ifname;
5053 io[idx].iov_len = os_strlen(ifname);
5054 idx++;
5055 io[idx].iov_base = " ";
5056 io[idx].iov_len = 1;
5057 idx++;
5058 }
5059 io[idx].iov_base = levelstr;
5060 io[idx].iov_len = os_strlen(levelstr);
5061 idx++;
5062 io[idx].iov_base = (char *) buf;
5063 io[idx].iov_len = len;
5064 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 os_memset(&msg, 0, sizeof(msg));
5066 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005067 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068
5069 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005070 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005071 if ((level >= dst->debug_level) &&
5072 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005073 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5074 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 msg.msg_name = &dst->addr;
5076 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005077 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 int _errno = errno;
5079 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5080 "%d - %s",
5081 idx, errno, strerror(errno));
5082 dst->errors++;
5083 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005084 ctrl_iface_detach(ctrl_dst,
5085 &dst->addr,
5086 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087 }
5088 } else
5089 dst->errors = 0;
5090 }
5091 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 }
5093}
5094
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005095
5096static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5097 enum wpa_msg_type type,
5098 const char *buf, size_t len)
5099{
5100 if (type != WPA_MSG_NO_GLOBAL) {
5101 hostapd_ctrl_iface_send_internal(
5102 hapd->iface->interfaces->global_ctrl_sock,
5103 &hapd->iface->interfaces->global_ctrl_dst,
5104 type != WPA_MSG_PER_INTERFACE ?
5105 NULL : hapd->conf->iface,
5106 level, buf, len);
5107 }
5108
5109 if (type != WPA_MSG_ONLY_GLOBAL) {
5110 hostapd_ctrl_iface_send_internal(
5111 hapd->ctrl_sock, &hapd->ctrl_dst,
5112 NULL, level, buf, len);
5113 }
5114}
5115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005116#endif /* CONFIG_NATIVE_WINDOWS */