blob: 94f31ee96276e157da451798e1f75f5a88447630 [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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080041#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080045#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047#include "ap/hostapd.h"
48#include "ap/ap_config.h"
49#include "ap/ieee802_1x.h"
50#include "ap/wpa_auth.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070051#include "ap/pmksa_cache_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#include "ap/ieee802_11.h"
53#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054#include "ap/wps_hostapd.h"
55#include "ap/ctrl_iface_ap.h"
56#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080057#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080058#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070059#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080060#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070061#include "ap/neighbor_db.h"
62#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070063#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070064#include "ap/dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065#include "wps/wps_defs.h"
66#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080067#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070068#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069#include "ctrl_iface.h"
70
71
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080072#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
73
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080074#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080075#define HOSTAPD_CTRL_IFACE_PORT 8877
76#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
77#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
78#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
79#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080
81static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020082 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 const char *buf, size_t len);
84
85
86static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080087 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070088 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089{
Roshan Pius3a1667e2018-07-03 15:17:14 -070090 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091}
92
93
94static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080095 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 socklen_t fromlen)
97{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080098 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099}
100
101
102static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800103 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 socklen_t fromlen,
105 char *level)
106{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800107 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108}
109
110
111static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
112 const char *txtaddr)
113{
114 u8 addr[ETH_ALEN];
115 struct sta_info *sta;
116
117 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
118
119 if (hwaddr_aton(txtaddr, addr))
120 return -1;
121
122 sta = ap_get_sta(hapd, addr);
123 if (sta)
124 return 0;
125
126 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
127 "notification", MAC2STR(addr));
128 sta = ap_sta_add(hapd, addr);
129 if (sta == NULL)
130 return -1;
131
132 hostapd_new_assoc_sta(hapd, sta, 0);
133 return 0;
134}
135
136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137#ifdef NEED_AP_MLME
138static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
139 const char *txtaddr)
140{
141 u8 addr[ETH_ALEN];
142 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
143
144 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
145
146 if (hwaddr_aton(txtaddr, addr) ||
147 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
148 return -1;
149
150 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
151
152 return 0;
153}
154#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156
157#ifdef CONFIG_WPS
158static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
159{
160 char *pin = os_strchr(txt, ' ');
161 char *timeout_txt;
162 int timeout;
163 u8 addr_buf[ETH_ALEN], *addr = NULL;
164 char *pos;
165
166 if (pin == NULL)
167 return -1;
168 *pin++ = '\0';
169
170 timeout_txt = os_strchr(pin, ' ');
171 if (timeout_txt) {
172 *timeout_txt++ = '\0';
173 timeout = atoi(timeout_txt);
174 pos = os_strchr(timeout_txt, ' ');
175 if (pos) {
176 *pos++ = '\0';
177 if (hwaddr_aton(pos, addr_buf) == 0)
178 addr = addr_buf;
179 }
180 } else
181 timeout = 0;
182
183 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
184}
185
186
187static int hostapd_ctrl_iface_wps_check_pin(
188 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
189{
190 char pin[9];
191 size_t len;
192 char *pos;
193 int ret;
194
195 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
196 (u8 *) cmd, os_strlen(cmd));
197 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
198 if (*pos < '0' || *pos > '9')
199 continue;
200 pin[len++] = *pos;
201 if (len == 9) {
202 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
203 return -1;
204 }
205 }
206 if (len != 4 && len != 8) {
207 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
208 return -1;
209 }
210 pin[len] = '\0';
211
212 if (len == 8) {
213 unsigned int pin_val;
214 pin_val = atoi(pin);
215 if (!wps_pin_valid(pin_val)) {
216 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
217 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800218 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 return -1;
220 return ret;
221 }
222 }
223
224 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800225 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 return -1;
227
228 return ret;
229}
230
231
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232#ifdef CONFIG_WPS_NFC
233static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
234 char *pos)
235{
236 size_t len;
237 struct wpabuf *buf;
238 int ret;
239
240 len = os_strlen(pos);
241 if (len & 0x01)
242 return -1;
243 len /= 2;
244
245 buf = wpabuf_alloc(len);
246 if (buf == NULL)
247 return -1;
248 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
249 wpabuf_free(buf);
250 return -1;
251 }
252
253 ret = hostapd_wps_nfc_tag_read(hapd, buf);
254 wpabuf_free(buf);
255
256 return ret;
257}
258
259
260static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
261 char *cmd, char *reply,
262 size_t max_len)
263{
264 int ndef;
265 struct wpabuf *buf;
266 int res;
267
268 if (os_strcmp(cmd, "WPS") == 0)
269 ndef = 0;
270 else if (os_strcmp(cmd, "NDEF") == 0)
271 ndef = 1;
272 else
273 return -1;
274
275 buf = hostapd_wps_nfc_config_token(hapd, ndef);
276 if (buf == NULL)
277 return -1;
278
279 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
280 wpabuf_len(buf));
281 reply[res++] = '\n';
282 reply[res] = '\0';
283
284 wpabuf_free(buf);
285
286 return res;
287}
288
289
290static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
291 char *reply, size_t max_len,
292 int ndef)
293{
294 struct wpabuf *buf;
295 int res;
296
297 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
298 if (buf == NULL)
299 return -1;
300
301 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
302 wpabuf_len(buf));
303 reply[res++] = '\n';
304 reply[res] = '\0';
305
306 wpabuf_free(buf);
307
308 return res;
309}
310
311
312static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
313 char *cmd, char *reply,
314 size_t max_len)
315{
316 if (os_strcmp(cmd, "WPS") == 0)
317 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
318 max_len, 0);
319
320 if (os_strcmp(cmd, "NDEF") == 0)
321 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
322 max_len, 1);
323
324 if (os_strcmp(cmd, "enable") == 0)
325 return hostapd_wps_nfc_token_enable(hapd);
326
327 if (os_strcmp(cmd, "disable") == 0) {
328 hostapd_wps_nfc_token_disable(hapd);
329 return 0;
330 }
331
332 return -1;
333}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800334
335
336static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
337 char *cmd, char *reply,
338 size_t max_len)
339{
340 struct wpabuf *buf;
341 int res;
342 char *pos;
343 int ndef;
344
345 pos = os_strchr(cmd, ' ');
346 if (pos == NULL)
347 return -1;
348 *pos++ = '\0';
349
350 if (os_strcmp(cmd, "WPS") == 0)
351 ndef = 0;
352 else if (os_strcmp(cmd, "NDEF") == 0)
353 ndef = 1;
354 else
355 return -1;
356
357 if (os_strcmp(pos, "WPS-CR") == 0)
358 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
359 else
360 buf = NULL;
361 if (buf == NULL)
362 return -1;
363
364 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
365 wpabuf_len(buf));
366 reply[res++] = '\n';
367 reply[res] = '\0';
368
369 wpabuf_free(buf);
370
371 return res;
372}
373
374
375static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
376 char *cmd)
377{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800378 size_t len;
379 struct wpabuf *req, *sel;
380 int ret;
381 char *pos, *role, *type, *pos2;
382
383 role = cmd;
384 pos = os_strchr(role, ' ');
385 if (pos == NULL)
386 return -1;
387 *pos++ = '\0';
388
389 type = pos;
390 pos = os_strchr(type, ' ');
391 if (pos == NULL)
392 return -1;
393 *pos++ = '\0';
394
395 pos2 = os_strchr(pos, ' ');
396 if (pos2 == NULL)
397 return -1;
398 *pos2++ = '\0';
399
400 len = os_strlen(pos);
401 if (len & 0x01)
402 return -1;
403 len /= 2;
404
405 req = wpabuf_alloc(len);
406 if (req == NULL)
407 return -1;
408 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
409 wpabuf_free(req);
410 return -1;
411 }
412
413 len = os_strlen(pos2);
414 if (len & 0x01) {
415 wpabuf_free(req);
416 return -1;
417 }
418 len /= 2;
419
420 sel = wpabuf_alloc(len);
421 if (sel == NULL) {
422 wpabuf_free(req);
423 return -1;
424 }
425 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
426 wpabuf_free(req);
427 wpabuf_free(sel);
428 return -1;
429 }
430
431 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
432 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
433 } else {
434 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
435 "reported: role=%s type=%s", role, type);
436 ret = -1;
437 }
438 wpabuf_free(req);
439 wpabuf_free(sel);
440
441 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800442}
443
Dmitry Shmidt04949592012-07-19 12:16:46 -0700444#endif /* CONFIG_WPS_NFC */
445
446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
448 char *buf, size_t buflen)
449{
450 int timeout = 300;
451 char *pos;
452 const char *pin_txt;
453
454 pos = os_strchr(txt, ' ');
455 if (pos)
456 *pos++ = '\0';
457
458 if (os_strcmp(txt, "disable") == 0) {
459 hostapd_wps_ap_pin_disable(hapd);
460 return os_snprintf(buf, buflen, "OK\n");
461 }
462
463 if (os_strcmp(txt, "random") == 0) {
464 if (pos)
465 timeout = atoi(pos);
466 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
467 if (pin_txt == NULL)
468 return -1;
469 return os_snprintf(buf, buflen, "%s", pin_txt);
470 }
471
472 if (os_strcmp(txt, "get") == 0) {
473 pin_txt = hostapd_wps_ap_pin_get(hapd);
474 if (pin_txt == NULL)
475 return -1;
476 return os_snprintf(buf, buflen, "%s", pin_txt);
477 }
478
479 if (os_strcmp(txt, "set") == 0) {
480 char *pin;
481 if (pos == NULL)
482 return -1;
483 pin = pos;
484 pos = os_strchr(pos, ' ');
485 if (pos) {
486 *pos++ = '\0';
487 timeout = atoi(pos);
488 }
489 if (os_strlen(pin) > buflen)
490 return -1;
491 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
492 return -1;
493 return os_snprintf(buf, buflen, "%s", pin);
494 }
495
496 return -1;
497}
498
499
500static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
501{
502 char *pos;
503 char *ssid, *auth, *encr = NULL, *key = NULL;
504
505 ssid = txt;
506 pos = os_strchr(txt, ' ');
507 if (!pos)
508 return -1;
509 *pos++ = '\0';
510
511 auth = pos;
512 pos = os_strchr(pos, ' ');
513 if (pos) {
514 *pos++ = '\0';
515 encr = pos;
516 pos = os_strchr(pos, ' ');
517 if (pos) {
518 *pos++ = '\0';
519 key = pos;
520 }
521 }
522
523 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
524}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700525
526
527static const char * pbc_status_str(enum pbc_status status)
528{
529 switch (status) {
530 case WPS_PBC_STATUS_DISABLE:
531 return "Disabled";
532 case WPS_PBC_STATUS_ACTIVE:
533 return "Active";
534 case WPS_PBC_STATUS_TIMEOUT:
535 return "Timed-out";
536 case WPS_PBC_STATUS_OVERLAP:
537 return "Overlap";
538 default:
539 return "Unknown";
540 }
541}
542
543
544static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
545 char *buf, size_t buflen)
546{
547 int ret;
548 char *pos, *end;
549
550 pos = buf;
551 end = buf + buflen;
552
553 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
554 pbc_status_str(hapd->wps_stats.pbc_status));
555
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700557 return pos - buf;
558 pos += ret;
559
560 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
561 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
562 "Success":
563 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
564 "Failed" : "None")));
565
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800566 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700567 return pos - buf;
568 pos += ret;
569
570 /* If status == Failure - Add possible Reasons */
571 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
572 hapd->wps_stats.failure_reason > 0) {
573 ret = os_snprintf(pos, end - pos,
574 "Failure Reason: %s\n",
575 wps_ei_str(hapd->wps_stats.failure_reason));
576
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800577 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700578 return pos - buf;
579 pos += ret;
580 }
581
582 if (hapd->wps_stats.status) {
583 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
584 MAC2STR(hapd->wps_stats.peer_addr));
585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700587 return pos - buf;
588 pos += ret;
589 }
590
591 return pos - buf;
592}
593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594#endif /* CONFIG_WPS */
595
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800596#ifdef CONFIG_HS20
597
598static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
599 const char *cmd)
600{
601 u8 addr[ETH_ALEN];
602 const char *url;
603
604 if (hwaddr_aton(cmd, addr))
605 return -1;
606 url = cmd + 17;
607 if (*url == '\0') {
608 url = NULL;
609 } else {
610 if (*url != ' ')
611 return -1;
612 url++;
613 if (*url == '\0')
614 url = NULL;
615 }
616
617 return hs20_send_wnm_notification(hapd, addr, 1, url);
618}
619
620
621static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
622 const char *cmd)
623{
624 u8 addr[ETH_ALEN];
625 int code, reauth_delay, ret;
626 const char *pos;
627 size_t url_len;
628 struct wpabuf *req;
629
630 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
631 if (hwaddr_aton(cmd, addr))
632 return -1;
633
634 pos = os_strchr(cmd, ' ');
635 if (pos == NULL)
636 return -1;
637 pos++;
638 code = atoi(pos);
639
640 pos = os_strchr(pos, ' ');
641 if (pos == NULL)
642 return -1;
643 pos++;
644 reauth_delay = atoi(pos);
645
646 url_len = 0;
647 pos = os_strchr(pos, ' ');
648 if (pos) {
649 pos++;
650 url_len = os_strlen(pos);
651 }
652
653 req = wpabuf_alloc(4 + url_len);
654 if (req == NULL)
655 return -1;
656 wpabuf_put_u8(req, code);
657 wpabuf_put_le16(req, reauth_delay);
658 wpabuf_put_u8(req, url_len);
659 if (pos)
660 wpabuf_put_data(req, pos, url_len);
661
662 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
663 " to indicate imminent deauthentication (code=%d "
664 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
665 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
666 wpabuf_free(req);
667 return ret;
668}
669
670#endif /* CONFIG_HS20 */
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672
Dmitry Shmidt051af732013-10-22 13:52:46 -0700673#ifdef CONFIG_INTERWORKING
674
675static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
676 const char *cmd)
677{
678 u8 qos_map_set[16 + 2 * 21], count = 0;
679 const char *pos = cmd;
680 int val, ret;
681
682 for (;;) {
683 if (count == sizeof(qos_map_set)) {
684 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
685 return -1;
686 }
687
688 val = atoi(pos);
689 if (val < 0 || val > 255) {
690 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
691 return -1;
692 }
693
694 qos_map_set[count++] = val;
695 pos = os_strchr(pos, ',');
696 if (!pos)
697 break;
698 pos++;
699 }
700
701 if (count < 16 || count & 1) {
702 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
703 return -1;
704 }
705
706 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
707 if (ret) {
708 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
709 return -1;
710 }
711
712 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
713 hapd->conf->qos_map_set_len = count;
714
715 return 0;
716}
717
718
719static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
720 const char *cmd)
721{
722 u8 addr[ETH_ALEN];
723 struct sta_info *sta;
724 struct wpabuf *buf;
725 u8 *qos_map_set = hapd->conf->qos_map_set;
726 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
727 int ret;
728
729 if (!qos_map_set_len) {
730 wpa_printf(MSG_INFO, "QoS Map Set is not set");
731 return -1;
732 }
733
734 if (hwaddr_aton(cmd, addr))
735 return -1;
736
737 sta = ap_get_sta(hapd, addr);
738 if (sta == NULL) {
739 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
740 "for QoS Map Configuration message",
741 MAC2STR(addr));
742 return -1;
743 }
744
745 if (!sta->qos_map_enabled) {
746 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
747 "support for QoS Map", MAC2STR(addr));
748 return -1;
749 }
750
751 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
752 if (buf == NULL)
753 return -1;
754
755 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
756 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
757
758 /* QoS Map Set Element */
759 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
760 wpabuf_put_u8(buf, qos_map_set_len);
761 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
762
763 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
764 wpabuf_head(buf), wpabuf_len(buf));
765 wpabuf_free(buf);
766
767 return ret;
768}
769
770#endif /* CONFIG_INTERWORKING */
771
772
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700773#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800774
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800775static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
776 const char *cmd)
777{
778 u8 addr[ETH_ALEN];
779 struct sta_info *sta;
780 const char *pos;
781 unsigned int auto_report, timeout;
782
783 if (hwaddr_aton(cmd, addr)) {
784 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
785 return -1;
786 }
787
788 sta = ap_get_sta(hapd, addr);
789 if (!sta) {
790 wpa_printf(MSG_DEBUG, "Station " MACSTR
791 " not found for Collocated Interference Request",
792 MAC2STR(addr));
793 return -1;
794 }
795
796 pos = cmd + 17;
797 if (*pos != ' ')
798 return -1;
799 pos++;
800 auto_report = atoi(pos);
801 pos = os_strchr(pos, ' ');
802 if (!pos)
803 return -1;
804 pos++;
805 timeout = atoi(pos);
806
807 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
808}
809
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700810#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800811
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800813static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
814 char *buf, size_t buflen)
815{
816 int ret = 0;
817 char *pos, *end;
818
819 pos = buf;
820 end = buf + buflen;
821
822 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
823
824 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
825 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
826 if (os_snprintf_error(end - pos, ret))
827 return pos - buf;
828 pos += ret;
829 }
830 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
831 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
832 if (os_snprintf_error(end - pos, ret))
833 return pos - buf;
834 pos += ret;
835 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800836#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800837 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
838 ret = os_snprintf(pos, end - pos, "FT-PSK ");
839 if (os_snprintf_error(end - pos, ret))
840 return pos - buf;
841 pos += ret;
842 }
843 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
844 ret = os_snprintf(pos, end - pos, "FT-EAP ");
845 if (os_snprintf_error(end - pos, ret))
846 return pos - buf;
847 pos += ret;
848 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700849#ifdef CONFIG_SHA384
850 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
851 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
852 if (os_snprintf_error(end - pos, ret))
853 return pos - buf;
854 pos += ret;
855 }
856#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800857#ifdef CONFIG_SAE
858 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
859 ret = os_snprintf(pos, end - pos, "FT-SAE ");
860 if (os_snprintf_error(end - pos, ret))
861 return pos - buf;
862 pos += ret;
863 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700864 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
865 ret = os_snprintf(pos, end - pos, "FT-SAE-EXT-KEY ");
866 if (os_snprintf_error(end - pos, ret))
867 return pos - buf;
868 pos += ret;
869 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800870#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800871#ifdef CONFIG_FILS
872 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
873 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
874 if (os_snprintf_error(end - pos, ret))
875 return pos - buf;
876 pos += ret;
877 }
878 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
879 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
880 if (os_snprintf_error(end - pos, ret))
881 return pos - buf;
882 pos += ret;
883 }
884#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800885#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800886 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
887 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
888 if (os_snprintf_error(end - pos, ret))
889 return pos - buf;
890 pos += ret;
891 }
892 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
893 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
894 if (os_snprintf_error(end - pos, ret))
895 return pos - buf;
896 pos += ret;
897 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800898#ifdef CONFIG_SAE
899 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
900 ret = os_snprintf(pos, end - pos, "SAE ");
901 if (os_snprintf_error(end - pos, ret))
902 return pos - buf;
903 pos += ret;
904 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700905 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
906 ret = os_snprintf(pos, end - pos, "SAE-EXT-KEY ");
907 if (os_snprintf_error(end - pos, ret))
908 return pos - buf;
909 pos += ret;
910 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800911#endif /* CONFIG_SAE */
912 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
913 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
914 if (os_snprintf_error(end - pos, ret))
915 return pos - buf;
916 pos += ret;
917 }
918 if (hapd->conf->wpa_key_mgmt &
919 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
920 ret = os_snprintf(pos, end - pos,
921 "WPA-EAP-SUITE-B-192 ");
922 if (os_snprintf_error(end - pos, ret))
923 return pos - buf;
924 pos += ret;
925 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800926#ifdef CONFIG_FILS
927 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
928 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
929 if (os_snprintf_error(end - pos, ret))
930 return pos - buf;
931 pos += ret;
932 }
933 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
934 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
935 if (os_snprintf_error(end - pos, ret))
936 return pos - buf;
937 pos += ret;
938 }
939#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800940
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700941#ifdef CONFIG_OWE
942 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
943 ret = os_snprintf(pos, end - pos, "OWE ");
944 if (os_snprintf_error(end - pos, ret))
945 return pos - buf;
946 pos += ret;
947 }
948#endif /* CONFIG_OWE */
949
950#ifdef CONFIG_DPP
951 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
952 ret = os_snprintf(pos, end - pos, "DPP ");
953 if (os_snprintf_error(end - pos, ret))
954 return pos - buf;
955 pos += ret;
956 }
957#endif /* CONFIG_DPP */
958
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800959 if (pos > buf && *(pos - 1) == ' ') {
960 *(pos - 1) = '\0';
961 pos--;
962 }
963
964 return pos - buf;
965}
966
967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
969 char *buf, size_t buflen)
970{
971 int ret;
972 char *pos, *end;
973
974 pos = buf;
975 end = buf + buflen;
976
977 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
978 "ssid=%s\n",
979 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 wpa_ssid_txt(hapd->conf->ssid.ssid,
981 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 return pos - buf;
984 pos += ret;
985
Sunil Ravi77d572f2023-01-17 23:58:31 +0000986 if ((hapd->conf->config_id)) {
987 ret = os_snprintf(pos, end - pos, "config_id=%s\n",
988 hapd->conf->config_id);
989 if (os_snprintf_error(end - pos, ret))
990 return pos - buf;
991 pos += ret;
992 }
993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994#ifdef CONFIG_WPS
995 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
996 hapd->conf->wps_state == 0 ? "disabled" :
997 (hapd->conf->wps_state == 1 ? "not configured" :
998 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800999 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return pos - buf;
1001 pos += ret;
1002
1003 if (hapd->conf->wps_state && hapd->conf->wpa &&
1004 hapd->conf->ssid.wpa_passphrase) {
1005 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1006 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001007 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 return pos - buf;
1009 pos += ret;
1010 }
1011
1012 if (hapd->conf->wps_state && hapd->conf->wpa &&
1013 hapd->conf->ssid.wpa_psk &&
1014 hapd->conf->ssid.wpa_psk->group) {
1015 char hex[PMK_LEN * 2 + 1];
1016 wpa_snprintf_hex(hex, sizeof(hex),
1017 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1018 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001019 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 return pos - buf;
1021 pos += ret;
1022 }
Hai Shalom60840252021-02-19 19:02:11 -08001023
1024 if (hapd->conf->multi_ap) {
1025 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1026
1027 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1028 hapd->conf->multi_ap);
1029 if (os_snprintf_error(end - pos, ret))
1030 return pos - buf;
1031 pos += ret;
1032
1033 if (ssid->ssid_len) {
1034 ret = os_snprintf(pos, end - pos,
1035 "multi_ap_backhaul_ssid=%s\n",
1036 wpa_ssid_txt(ssid->ssid,
1037 ssid->ssid_len));
1038 if (os_snprintf_error(end - pos, ret))
1039 return pos - buf;
1040 pos += ret;
1041 }
1042
1043 if (hapd->conf->wps_state && hapd->conf->wpa &&
1044 ssid->wpa_passphrase) {
1045 ret = os_snprintf(pos, end - pos,
1046 "multi_ap_backhaul_wpa_passphrase=%s\n",
1047 ssid->wpa_passphrase);
1048 if (os_snprintf_error(end - pos, ret))
1049 return pos - buf;
1050 pos += ret;
1051 }
1052
1053 if (hapd->conf->wps_state && hapd->conf->wpa &&
1054 ssid->wpa_psk &&
1055 ssid->wpa_psk->group) {
1056 char hex[PMK_LEN * 2 + 1];
1057
1058 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1059 PMK_LEN);
1060 ret = os_snprintf(pos, end - pos,
1061 "multi_ap_backhaul_wpa_psk=%s\n",
1062 hex);
1063 forced_memzero(hex, sizeof(hex));
1064 if (os_snprintf_error(end - pos, ret))
1065 return pos - buf;
1066 pos += ret;
1067 }
1068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069#endif /* CONFIG_WPS */
1070
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001071 if (hapd->conf->wpa) {
1072 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1073 if (os_snprintf_error(end - pos, ret))
1074 return pos - buf;
1075 pos += ret;
1076 }
1077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1079 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001080 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 return pos - buf;
1082 pos += ret;
1083
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001084 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085
1086 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001087 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 return pos - buf;
1089 pos += ret;
1090 }
1091
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001092 if (hapd->conf->wpa) {
1093 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1094 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001095 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 return pos - buf;
1097 pos += ret;
1098 }
1099
1100 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1101 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001102 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return pos - buf;
1104 pos += ret;
1105
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001106 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1107 " ");
1108 if (ret < 0)
1109 return pos - buf;
1110 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111
1112 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001113 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 return pos - buf;
1115 pos += ret;
1116 }
1117
1118 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1119 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001120 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 return pos - buf;
1122 pos += ret;
1123
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001124 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001125 " ");
1126 if (ret < 0)
1127 return pos - buf;
1128 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129
1130 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001131 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 return pos - buf;
1133 pos += ret;
1134 }
1135
Hai Shalomfdcde762020-04-02 11:19:20 -07001136 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1137 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1138 hapd->conf->wpa_deny_ptk0_rekey);
1139 if (os_snprintf_error(end - pos, ret))
1140 return pos - buf;
1141 pos += ret;
1142 }
1143
1144 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1145 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1146 hapd->conf->extended_key_id);
1147 if (os_snprintf_error(end - pos, ret))
1148 return pos - buf;
1149 pos += ret;
1150 }
1151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 return pos - buf;
1153}
1154
1155
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001156static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001157 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001158{
1159 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001160 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001161
Hai Shalom60840252021-02-19 19:02:11 -08001162 /*
1163 * For example:
1164 * SET setband 2G,6G
1165 * SET setband 5G
1166 * SET setband AUTO
1167 */
1168 if (!os_strstr(bands, "AUTO")) {
1169 if (os_strstr(bands, "5G"))
1170 setband_mask |= WPA_SETBAND_5G;
1171 if (os_strstr(bands, "6G"))
1172 setband_mask |= WPA_SETBAND_6G;
1173 if (os_strstr(bands, "2G"))
1174 setband_mask |= WPA_SETBAND_2G;
1175 if (setband_mask == WPA_SETBAND_AUTO)
1176 return -1;
1177 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001178
Hai Shalom60840252021-02-19 19:02:11 -08001179 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001180 os_memset(&event, 0, sizeof(event));
1181 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1182 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1183 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1184 }
1185
1186 return 0;
1187}
1188
1189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1191{
1192 char *value;
1193 int ret = 0;
1194
1195 value = os_strchr(cmd, ' ');
1196 if (value == NULL)
1197 return -1;
1198 *value++ = '\0';
1199
1200 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1201 if (0) {
1202#ifdef CONFIG_WPS_TESTING
1203 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1204 long int val;
1205 val = strtol(value, NULL, 0);
1206 if (val < 0 || val > 0xff) {
1207 ret = -1;
1208 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1209 "wps_version_number %ld", val);
1210 } else {
1211 wps_version_number = val;
1212 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1213 "version %u.%u",
1214 (wps_version_number & 0xf0) >> 4,
1215 wps_version_number & 0x0f);
1216 hostapd_wps_update_ie(hapd);
1217 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08001218 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1219 wps_testing_stub_cred = atoi(value);
1220 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1221 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001222 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1223 wps_corrupt_pkhash = atoi(value);
1224 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1225 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001227#ifdef CONFIG_TESTING_OPTIONS
1228 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1229 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001230 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1231 hapd->ext_eapol_frame_io = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001232 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1233 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001234#ifdef CONFIG_DPP
1235 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1236 os_free(hapd->dpp_config_obj_override);
1237 hapd->dpp_config_obj_override = os_strdup(value);
1238 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1239 os_free(hapd->dpp_discovery_override);
1240 hapd->dpp_discovery_override = os_strdup(value);
1241 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1242 os_free(hapd->dpp_groups_override);
1243 hapd->dpp_groups_override = os_strdup(value);
1244 } else if (os_strcasecmp(cmd,
1245 "dpp_ignore_netaccesskey_mismatch") == 0) {
1246 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001247 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1248 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001249 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1250 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001251#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001252#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001253#ifdef CONFIG_MBO
1254 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1255 int val;
1256
1257 if (!hapd->conf->mbo_enabled)
1258 return -1;
1259
1260 val = atoi(value);
Hai Shaloma20dcd72022-02-04 13:43:00 -08001261 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001262 return -1;
1263
1264 hapd->mbo_assoc_disallow = val;
1265 ieee802_11_update_beacons(hapd->iface);
1266
1267 /*
1268 * TODO: Need to configure drivers that do AP MLME offload with
1269 * disallowing station logic.
1270 */
1271#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001272#ifdef CONFIG_DPP
1273 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1274 os_free(hapd->dpp_configurator_params);
1275 hapd->dpp_configurator_params = os_strdup(value);
Sunil Ravia04bd252022-05-02 22:54:18 -07001276#ifdef CONFIG_DPP2
1277 dpp_controller_set_params(hapd->iface->interfaces->dpp, value);
1278#endif /* CONFIG_DPP2 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08001279 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1280 hapd->dpp_init_max_tries = atoi(value);
1281 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1282 hapd->dpp_init_retry_time = atoi(value);
1283 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1284 hapd->dpp_resp_wait_time = atoi(value);
1285 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1286 hapd->dpp_resp_max_tries = atoi(value);
1287 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1288 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001289#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001290 } else if (os_strcasecmp(cmd, "setband") == 0) {
1291 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001293 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001294 if (ret)
1295 return ret;
1296
1297 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001298 hostapd_disassoc_deny_mac(hapd);
1299 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1300 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001301 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1302 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1303 hapd->parameter_set_count++;
1304 if (ieee802_11_update_beacons(hapd->iface))
1305 wpa_printf(MSG_DEBUG,
1306 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001307 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1308 os_strcmp(cmd, "sae_password") == 0 ||
1309 os_strcmp(cmd, "sae_pwe") == 0) {
1310 if (hapd->started)
1311 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001312 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1313 wpa_auth_set_transition_disable(hapd->wpa_auth,
1314 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001315 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001316
1317#ifdef CONFIG_TESTING_OPTIONS
1318 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1319 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1320 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001321 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1322 wpa_auth_set_ocv_override_freq(
1323 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1324 atoi(value));
1325 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1326 wpa_auth_set_ocv_override_freq(
1327 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1328 atoi(value));
1329 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1330 wpa_auth_set_ocv_override_freq(
1331 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1332 atoi(value));
1333 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1334 wpa_auth_set_ocv_override_freq(
1335 hapd->wpa_auth,
1336 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001337 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1338 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1339 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1340 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001341#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 }
1343
1344 return ret;
1345}
1346
1347
1348static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1349 char *buf, size_t buflen)
1350{
1351 int res;
1352
1353 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1354
1355 if (os_strcmp(cmd, "version") == 0) {
1356 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001357 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 return -1;
1359 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001360 } else if (os_strcmp(cmd, "tls_library") == 0) {
1361 res = tls_get_library_version(buf, buflen);
1362 if (os_snprintf_error(buflen, res))
1363 return -1;
1364 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 }
1366
1367 return -1;
1368}
1369
1370
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001371static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1372{
1373 if (hostapd_enable_iface(iface) < 0) {
1374 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1375 return -1;
1376 }
1377 return 0;
1378}
1379
1380
1381static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1382{
1383 if (hostapd_reload_iface(iface) < 0) {
1384 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1385 return -1;
1386 }
1387 return 0;
1388}
1389
1390
Sunil Ravi77d572f2023-01-17 23:58:31 +00001391static int hostapd_ctrl_iface_reload_bss(struct hostapd_data *bss)
1392{
1393 if (hostapd_reload_bss_only(bss) < 0) {
1394 wpa_printf(MSG_ERROR, "Reloading of BSS failed");
1395 return -1;
1396 }
1397 return 0;
1398}
1399
1400
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001401static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1402{
1403 if (hostapd_disable_iface(iface) < 0) {
1404 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1405 return -1;
1406 }
1407 return 0;
1408}
1409
1410
Hai Shalom74f70d42019-02-11 14:42:39 -08001411static int
1412hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1413 struct sta_info *sta, void *ctx)
1414{
1415 struct hostapd_wpa_psk *psk;
1416 const u8 *pmk;
1417 int pmk_len;
1418 int pmk_match;
1419 int sta_match;
1420 int bss_match;
1421 int reason;
1422
1423 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1424
1425 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1426 pmk_match = PMK_LEN == pmk_len &&
1427 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1428 sta_match = psk->group == 0 &&
1429 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1430 bss_match = psk->group == 1;
1431
1432 if (pmk_match && (sta_match || bss_match))
1433 return 0;
1434 }
1435
1436 wpa_printf(MSG_INFO, "STA " MACSTR
1437 " PSK/passphrase no longer valid - disconnect",
1438 MAC2STR(sta->addr));
1439 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1440 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1441 ap_sta_deauthenticate(hapd, sta, reason);
1442
1443 return 0;
1444}
1445
1446
1447static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1448{
1449 struct hostapd_bss_config *conf = hapd->conf;
1450 int err;
1451
1452 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1453
1454 err = hostapd_setup_wpa_psk(conf);
1455 if (err < 0) {
1456 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1457 err);
1458 return -1;
1459 }
1460
1461 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1462 NULL);
1463
1464 return 0;
1465}
1466
1467
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001468#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001469
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001470static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1471{
1472 union wpa_event_data data;
1473 char *pos, *param;
1474 enum wpa_event_type event;
1475
1476 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1477
1478 os_memset(&data, 0, sizeof(data));
1479
1480 param = os_strchr(cmd, ' ');
1481 if (param == NULL)
1482 return -1;
1483 *param++ = '\0';
1484
1485 if (os_strcmp(cmd, "DETECTED") == 0)
1486 event = EVENT_DFS_RADAR_DETECTED;
1487 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1488 event = EVENT_DFS_CAC_FINISHED;
1489 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1490 event = EVENT_DFS_CAC_ABORTED;
1491 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1492 event = EVENT_DFS_NOP_FINISHED;
1493 else {
1494 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1495 cmd);
1496 return -1;
1497 }
1498
1499 pos = os_strstr(param, "freq=");
1500 if (pos)
1501 data.dfs_event.freq = atoi(pos + 5);
1502
1503 pos = os_strstr(param, "ht_enabled=1");
1504 if (pos)
1505 data.dfs_event.ht_enabled = 1;
1506
1507 pos = os_strstr(param, "chan_offset=");
1508 if (pos)
1509 data.dfs_event.chan_offset = atoi(pos + 12);
1510
1511 pos = os_strstr(param, "chan_width=");
1512 if (pos)
1513 data.dfs_event.chan_width = atoi(pos + 11);
1514
1515 pos = os_strstr(param, "cf1=");
1516 if (pos)
1517 data.dfs_event.cf1 = atoi(pos + 4);
1518
1519 pos = os_strstr(param, "cf2=");
1520 if (pos)
1521 data.dfs_event.cf2 = atoi(pos + 4);
1522
1523 wpa_supplicant_event(hapd, event, &data);
1524
1525 return 0;
1526}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001527
1528
1529static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1530{
1531 size_t len;
1532 u8 *buf;
1533 int res;
1534
1535 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1536
1537 len = os_strlen(cmd);
1538 if (len & 1)
1539 return -1;
1540 len /= 2;
1541
1542 buf = os_malloc(len);
1543 if (buf == NULL)
1544 return -1;
1545
1546 if (hexstr2bin(cmd, buf, len) < 0) {
1547 os_free(buf);
1548 return -1;
1549 }
1550
Hai Shalomfdcde762020-04-02 11:19:20 -07001551 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001552 os_free(buf);
1553 return res;
1554}
1555
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001556
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001557static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1558 char *cmd)
1559{
1560 char *pos, *param;
1561 size_t len;
1562 u8 *buf;
1563 int stype = 0, ok = 0;
1564 union wpa_event_data event;
1565
1566 if (!hapd->ext_mgmt_frame_handling)
1567 return -1;
1568
1569 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1570
1571 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1572
1573 pos = cmd;
1574 param = os_strstr(pos, "stype=");
1575 if (param) {
1576 param += 6;
1577 stype = atoi(param);
1578 }
1579
1580 param = os_strstr(pos, " ok=");
1581 if (param) {
1582 param += 4;
1583 ok = atoi(param);
1584 }
1585
1586 param = os_strstr(pos, " buf=");
1587 if (!param)
1588 return -1;
1589 param += 5;
1590
1591 len = os_strlen(param);
1592 if (len & 1)
1593 return -1;
1594 len /= 2;
1595
1596 buf = os_malloc(len);
1597 if (!buf || hexstr2bin(param, buf, len) < 0) {
1598 os_free(buf);
1599 return -1;
1600 }
1601
1602 os_memset(&event, 0, sizeof(event));
1603 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1604 event.tx_status.data = buf;
1605 event.tx_status.data_len = len;
1606 event.tx_status.stype = stype;
1607 event.tx_status.ack = ok;
1608 hapd->ext_mgmt_frame_handling = 0;
1609 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1610 hapd->ext_mgmt_frame_handling = 1;
1611
1612 os_free(buf);
1613
1614 return 0;
1615}
1616
1617
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001618static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1619 char *cmd)
1620{
1621 char *pos, *param;
1622 size_t len;
1623 u8 *buf;
1624 int freq = 0, datarate = 0, ssi_signal = 0;
1625 union wpa_event_data event;
1626
1627 if (!hapd->ext_mgmt_frame_handling)
1628 return -1;
1629
1630 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1631
1632 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1633
1634 pos = cmd;
1635 param = os_strstr(pos, "freq=");
1636 if (param) {
1637 param += 5;
1638 freq = atoi(param);
1639 }
1640
1641 param = os_strstr(pos, " datarate=");
1642 if (param) {
1643 param += 10;
1644 datarate = atoi(param);
1645 }
1646
1647 param = os_strstr(pos, " ssi_signal=");
1648 if (param) {
1649 param += 12;
1650 ssi_signal = atoi(param);
1651 }
1652
1653 param = os_strstr(pos, " frame=");
1654 if (param == NULL)
1655 return -1;
1656 param += 7;
1657
1658 len = os_strlen(param);
1659 if (len & 1)
1660 return -1;
1661 len /= 2;
1662
1663 buf = os_malloc(len);
1664 if (buf == NULL)
1665 return -1;
1666
1667 if (hexstr2bin(param, buf, len) < 0) {
1668 os_free(buf);
1669 return -1;
1670 }
1671
1672 os_memset(&event, 0, sizeof(event));
1673 event.rx_mgmt.freq = freq;
1674 event.rx_mgmt.frame = buf;
1675 event.rx_mgmt.frame_len = len;
1676 event.rx_mgmt.ssi_signal = ssi_signal;
1677 event.rx_mgmt.datarate = datarate;
1678 hapd->ext_mgmt_frame_handling = 0;
1679 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1680 hapd->ext_mgmt_frame_handling = 1;
1681
1682 os_free(buf);
1683
1684 return 0;
1685}
1686
1687
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001688static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1689{
1690 char *pos;
1691 u8 src[ETH_ALEN], *buf;
1692 int used;
1693 size_t len;
1694
1695 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1696
1697 pos = cmd;
1698 used = hwaddr_aton2(pos, src);
1699 if (used < 0)
1700 return -1;
1701 pos += used;
1702 while (*pos == ' ')
1703 pos++;
1704
1705 len = os_strlen(pos);
1706 if (len & 1)
1707 return -1;
1708 len /= 2;
1709
1710 buf = os_malloc(len);
1711 if (buf == NULL)
1712 return -1;
1713
1714 if (hexstr2bin(pos, buf, len) < 0) {
1715 os_free(buf);
1716 return -1;
1717 }
1718
Sunil8cd6f4d2022-06-28 18:40:46 +00001719 ieee802_1x_receive(hapd, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001720 os_free(buf);
1721
1722 return 0;
1723}
1724
1725
Hai Shaloma20dcd72022-02-04 13:43:00 -08001726static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1727{
1728 char *pos, *pos2;
1729 u8 dst[ETH_ALEN], *buf;
1730 int used, ret;
1731 size_t len;
1732 unsigned int prev;
1733 int encrypt = 0;
1734
1735 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1736
1737 pos = cmd;
1738 used = hwaddr_aton2(pos, dst);
1739 if (used < 0)
1740 return -1;
1741 pos += used;
1742 while (*pos == ' ')
1743 pos++;
1744
1745 pos2 = os_strchr(pos, ' ');
1746 if (pos2) {
1747 len = pos2 - pos;
1748 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1749 } else {
1750 len = os_strlen(pos);
1751 }
1752 if (len & 1)
1753 return -1;
1754 len /= 2;
1755
1756 buf = os_malloc(len);
1757 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1758 os_free(buf);
1759 return -1;
1760 }
1761
1762 prev = hapd->ext_eapol_frame_io;
1763 hapd->ext_eapol_frame_io = 0;
1764 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1765 hapd->ext_eapol_frame_io = prev;
1766 os_free(buf);
1767
1768 return ret;
1769}
1770
1771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001772static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1773{
1774 size_t i;
1775 u32 sum = 0;
1776 const u16 *pos = buf;
1777
1778 for (i = 0; i < len / 2; i++)
1779 sum += *pos++;
1780
1781 while (sum >> 16)
1782 sum = (sum & 0xffff) + (sum >> 16);
1783
1784 return sum ^ 0xffff;
1785}
1786
1787
1788#define HWSIM_PACKETLEN 1500
1789#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1790
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001791static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1792 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793{
1794 struct hostapd_data *hapd = ctx;
1795 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001796 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001797 const u8 *pos;
1798 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001799 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001800
Hai Shalom81f62d82019-07-22 12:10:00 -07001801 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1802 wpa_printf(MSG_DEBUG,
1803 "test data: RX - ignore unexpected length %d",
1804 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001806 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001807
1808 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001809 os_memcpy(&ip, eth + 1, sizeof(ip));
1810 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001811
Hai Shalomfdcde762020-04-02 11:19:20 -07001812 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1813 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001814 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07001815 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001816 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001817 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001818
Hai Shalomfdcde762020-04-02 11:19:20 -07001819 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001820 if (*pos != (u8) i) {
1821 wpa_printf(MSG_DEBUG,
1822 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001823 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001824 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001825 pos++;
1826 }
1827
Hai Shalom81f62d82019-07-22 12:10:00 -07001828 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001829 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1830 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001831 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1832 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001833}
1834
1835
1836static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1837 char *cmd)
1838{
1839 int enabled = atoi(cmd);
1840 char *pos;
1841 const char *ifname;
1842
1843 if (!enabled) {
1844 if (hapd->l2_test) {
1845 l2_packet_deinit(hapd->l2_test);
1846 hapd->l2_test = NULL;
1847 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1848 "test data: Disabled");
1849 }
1850 return 0;
1851 }
1852
1853 if (hapd->l2_test)
1854 return 0;
1855
1856 pos = os_strstr(cmd, " ifname=");
1857 if (pos)
1858 ifname = pos + 8;
1859 else
1860 ifname = hapd->conf->iface;
1861
1862 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1863 ETHERTYPE_IP, hostapd_data_test_rx,
1864 hapd, 1);
1865 if (hapd->l2_test == NULL)
1866 return -1;
1867
1868 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1869
1870 return 0;
1871}
1872
1873
1874static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1875{
1876 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001877 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001878 int used;
1879 long int val;
1880 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001881 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001882 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001883 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001884 u8 *dpos;
1885 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001886 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001887
1888 if (hapd->l2_test == NULL)
1889 return -1;
1890
Hai Shalom81f62d82019-07-22 12:10:00 -07001891 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001892
1893 pos = cmd;
1894 used = hwaddr_aton2(pos, dst);
1895 if (used < 0)
1896 return -1;
1897 pos += used;
1898 while (*pos == ' ')
1899 pos++;
1900 used = hwaddr_aton2(pos, src);
1901 if (used < 0)
1902 return -1;
1903 pos += used;
1904
Hai Shalom81f62d82019-07-22 12:10:00 -07001905 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001906 if (val < 0 || val > 0xff)
1907 return -1;
1908 tos = val;
1909
Hai Shalom81f62d82019-07-22 12:10:00 -07001910 pos = os_strstr(pos2, " len=");
1911 if (pos) {
1912 i = atoi(pos + 5);
1913 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
1914 return -1;
1915 send_len = i;
1916 }
1917
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001918 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1920 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1921 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07001922 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001923 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07001924 ip->ip_hl = 5;
1925 ip->ip_v = 4;
1926 ip->ip_ttl = 64;
1927 ip->ip_tos = tos;
1928 ip->ip_len = htons(send_len);
1929 ip->ip_p = 1;
1930 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1931 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1932 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001933 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07001934 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001935 *dpos++ = i;
1936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001937 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07001938 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001939 return -1;
1940
1941 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1942 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1943
1944 return 0;
1945}
1946
1947
1948static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1949 char *cmd)
1950{
1951 u8 *buf;
1952 struct ether_header *eth;
1953 struct l2_packet_data *l2 = NULL;
1954 size_t len;
1955 u16 ethertype;
1956 int res = -1;
1957 const char *ifname = hapd->conf->iface;
1958
1959 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1960 cmd += 7;
1961 ifname = cmd;
1962 cmd = os_strchr(cmd, ' ');
1963 if (cmd == NULL)
1964 return -1;
1965 *cmd++ = '\0';
1966 }
1967
1968 len = os_strlen(cmd);
1969 if (len & 1 || len < ETH_HLEN * 2)
1970 return -1;
1971 len /= 2;
1972
1973 buf = os_malloc(len);
1974 if (buf == NULL)
1975 return -1;
1976
1977 if (hexstr2bin(cmd, buf, len) < 0)
1978 goto done;
1979
1980 eth = (struct ether_header *) buf;
1981 ethertype = ntohs(eth->ether_type);
1982
1983 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1984 hostapd_data_test_rx, hapd, 1);
1985 if (l2 == NULL)
1986 goto done;
1987
1988 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1989 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1990done:
1991 if (l2)
1992 l2_packet_deinit(l2);
1993 os_free(buf);
1994
1995 return res < 0 ? -1 : 0;
1996}
1997
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001998
1999static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2000{
2001#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002002 char *pos;
2003
2004 wpa_trace_fail_after = atoi(cmd);
2005 pos = os_strchr(cmd, ':');
2006 if (pos) {
2007 pos++;
2008 os_strlcpy(wpa_trace_fail_func, pos,
2009 sizeof(wpa_trace_fail_func));
2010 } else {
2011 wpa_trace_fail_after = 0;
2012 }
2013
2014 return 0;
2015#else /* WPA_TRACE_BFD */
2016 return -1;
2017#endif /* WPA_TRACE_BFD */
2018}
2019
2020
2021static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2022 char *buf, size_t buflen)
2023{
2024#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002025 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2026 wpa_trace_fail_func);
2027#else /* WPA_TRACE_BFD */
2028 return -1;
2029#endif /* WPA_TRACE_BFD */
2030}
2031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002032
2033static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2034{
2035#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002036 char *pos;
2037
2038 wpa_trace_test_fail_after = atoi(cmd);
2039 pos = os_strchr(cmd, ':');
2040 if (pos) {
2041 pos++;
2042 os_strlcpy(wpa_trace_test_fail_func, pos,
2043 sizeof(wpa_trace_test_fail_func));
2044 } else {
2045 wpa_trace_test_fail_after = 0;
2046 }
2047
2048 return 0;
2049#else /* WPA_TRACE_BFD */
2050 return -1;
2051#endif /* WPA_TRACE_BFD */
2052}
2053
2054
2055static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2056 char *buf, size_t buflen)
2057{
2058#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002059 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2060 wpa_trace_test_fail_func);
2061#else /* WPA_TRACE_BFD */
2062 return -1;
2063#endif /* WPA_TRACE_BFD */
2064}
2065
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002066
2067static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2068{
2069 struct sta_info *sta;
2070 u8 addr[ETH_ALEN];
2071 u8 zero[WPA_TK_MAX_LEN];
2072
2073 os_memset(zero, 0, sizeof(zero));
2074
2075 if (hwaddr_aton(cmd, addr))
2076 return -1;
2077
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002078 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2079 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2080 return -1;
2081
2082 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2083
2084 /* First, use a zero key to avoid any possible duplicate key
2085 * avoidance in the driver. */
2086 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2087 hapd->last_bigtk_alg,
2088 broadcast_ether_addr,
2089 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2090 zero, hapd->last_bigtk_len,
2091 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2092 return -1;
2093
2094 /* Set the previously configured key to reset its TSC */
2095 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2096 hapd->last_bigtk_alg,
2097 broadcast_ether_addr,
2098 hapd->last_bigtk_key_idx, 0, 1, NULL,
2099 0, hapd->last_bigtk,
2100 hapd->last_bigtk_len,
2101 KEY_FLAG_GROUP_TX_DEFAULT);
2102 }
2103
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002104 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2105 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2106 return -1;
2107
2108 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2109
2110 /* First, use a zero key to avoid any possible duplicate key
2111 * avoidance in the driver. */
2112 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2113 hapd->last_igtk_alg,
2114 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002115 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2116 zero, hapd->last_igtk_len,
2117 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002118 return -1;
2119
2120 /* Set the previously configured key to reset its TSC */
2121 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2122 hapd->last_igtk_alg,
2123 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002124 hapd->last_igtk_key_idx, 0, 1, NULL,
2125 0, hapd->last_igtk,
2126 hapd->last_igtk_len,
2127 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002128 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002129
2130 if (is_broadcast_ether_addr(addr)) {
2131 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2132 return -1;
2133
2134 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2135
2136 /* First, use a zero key to avoid any possible duplicate key
2137 * avoidance in the driver. */
2138 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2139 hapd->last_gtk_alg,
2140 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002141 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2142 zero, hapd->last_gtk_len,
2143 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002144 return -1;
2145
2146 /* Set the previously configured key to reset its TSC */
2147 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2148 hapd->last_gtk_alg,
2149 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002150 hapd->last_gtk_key_idx, 0, 1, NULL,
2151 0, hapd->last_gtk,
2152 hapd->last_gtk_len,
2153 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002154 }
2155
2156 sta = ap_get_sta(hapd, addr);
2157 if (!sta)
2158 return -1;
2159
2160 if (sta->last_tk_alg == WPA_ALG_NONE)
2161 return -1;
2162
2163 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2164 MAC2STR(sta->addr));
2165
2166 /* First, use a zero key to avoid any possible duplicate key avoidance
2167 * in the driver. */
2168 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002169 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2170 zero, sta->last_tk_len,
2171 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002172 return -1;
2173
2174 /* Set the previously configured key to reset its TSC/RSC */
2175 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002176 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2177 0, sta->last_tk, sta->last_tk_len,
2178 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002179}
2180
2181
2182static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2183{
2184 u8 addr[ETH_ALEN];
2185 const char *pos = cmd;
2186 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002187 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002188 int idx, set_tx;
2189 u8 seq[6], key[WPA_TK_MAX_LEN];
2190 size_t key_len;
2191
Hai Shalomfdcde762020-04-02 11:19:20 -07002192 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002193
2194 alg = atoi(pos);
2195 pos = os_strchr(pos, ' ');
2196 if (!pos)
2197 return -1;
2198 pos++;
2199 if (hwaddr_aton(pos, addr))
2200 return -1;
2201 pos += 17;
2202 if (*pos != ' ')
2203 return -1;
2204 pos++;
2205 idx = atoi(pos);
2206 pos = os_strchr(pos, ' ');
2207 if (!pos)
2208 return -1;
2209 pos++;
2210 set_tx = atoi(pos);
2211 pos = os_strchr(pos, ' ');
2212 if (!pos)
2213 return -1;
2214 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002215 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216 return -1;
2217 pos += 2 * 6;
2218 if (*pos != ' ')
2219 return -1;
2220 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002221 if (!os_strchr(pos, ' '))
2222 return -1;
2223 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002224 if (hexstr2bin(pos, key, key_len) < 0)
2225 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002226 pos += 2 * key_len;
2227 if (*pos != ' ')
2228 return -1;
2229
2230 pos++;
2231 key_flag = atoi(pos);
2232 pos = os_strchr(pos, ' ');
2233 if (pos)
2234 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002235
2236 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002237 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2238 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002239}
2240
2241
2242static void restore_tk(void *ctx1, void *ctx2)
2243{
2244 struct hostapd_data *hapd = ctx1;
2245 struct sta_info *sta = ctx2;
2246
2247 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2248 MAC2STR(sta->addr));
2249 /* This does not really restore the TSC properly, so this will result
2250 * in replay protection issues for now since there is no clean way of
2251 * preventing encryption of a single EAPOL frame. */
2252 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002253 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2254 sta->last_tk, sta->last_tk_len,
2255 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002256}
2257
2258
2259static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2260{
2261 struct sta_info *sta;
2262 u8 addr[ETH_ALEN];
2263 int plain = os_strstr(cmd, "plaintext") != NULL;
2264
2265 if (hwaddr_aton(cmd, addr))
2266 return -1;
2267
2268 sta = ap_get_sta(hapd, addr);
2269 if (!sta || !sta->wpa_sm)
2270 return -1;
2271
2272 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2273 plain = 0; /* no need for special processing */
2274 if (plain) {
2275 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2276 MAC2STR(sta->addr));
2277 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002278 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2279 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002280 }
2281
2282 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2283 return wpa_auth_resend_m1(sta->wpa_sm,
2284 os_strstr(cmd, "change-anonce") != NULL,
2285 plain ? restore_tk : NULL, hapd, sta);
2286}
2287
2288
2289static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2290{
2291 struct sta_info *sta;
2292 u8 addr[ETH_ALEN];
2293 int plain = os_strstr(cmd, "plaintext") != NULL;
2294
2295 if (hwaddr_aton(cmd, addr))
2296 return -1;
2297
2298 sta = ap_get_sta(hapd, addr);
2299 if (!sta || !sta->wpa_sm)
2300 return -1;
2301
2302 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2303 plain = 0; /* no need for special processing */
2304 if (plain) {
2305 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2306 MAC2STR(sta->addr));
2307 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002308 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2309 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002310 }
2311
2312 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2313 return wpa_auth_resend_m3(sta->wpa_sm,
2314 plain ? restore_tk : NULL, hapd, sta);
2315}
2316
2317
2318static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2319 const char *cmd)
2320{
2321 struct sta_info *sta;
2322 u8 addr[ETH_ALEN];
2323 int plain = os_strstr(cmd, "plaintext") != NULL;
2324
2325 if (hwaddr_aton(cmd, addr))
2326 return -1;
2327
2328 sta = ap_get_sta(hapd, addr);
2329 if (!sta || !sta->wpa_sm)
2330 return -1;
2331
2332 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2333 plain = 0; /* no need for special processing */
2334 if (plain) {
2335 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2336 MAC2STR(sta->addr));
2337 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002338 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2339 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002340 }
2341
2342 wpa_printf(MSG_INFO,
2343 "TESTING: Send group M1 for the same GTK and zero RSC to "
2344 MACSTR, MAC2STR(sta->addr));
2345 return wpa_auth_resend_group_m1(sta->wpa_sm,
2346 plain ? restore_tk : NULL, hapd, sta);
2347}
2348
Hai Shalomfdcde762020-04-02 11:19:20 -07002349
Hai Shaloma20dcd72022-02-04 13:43:00 -08002350static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2351{
2352 struct sta_info *sta;
2353 u8 addr[ETH_ALEN];
2354
2355 if (hwaddr_aton(cmd, addr))
2356 return -1;
2357
2358 sta = ap_get_sta(hapd, addr);
2359 if (!sta || !sta->wpa_sm)
2360 return -1;
2361
2362 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2363}
2364
2365
Hai Shalom899fcc72020-10-19 14:38:18 -07002366static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2367 char *buf, size_t buflen)
2368{
2369 struct rsn_pmksa_cache_entry *pmksa;
2370
2371 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2372 if (!pmksa)
2373 return -1;
2374
2375 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2376}
2377
2378
Hai Shalomfdcde762020-04-02 11:19:20 -07002379static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2380 char *buf, size_t buflen)
2381{
2382 struct sta_info *sta;
2383 u8 addr[ETH_ALEN];
2384 const u8 *pmk;
2385 int pmk_len;
2386
2387 if (hwaddr_aton(cmd, addr))
2388 return -1;
2389
2390 sta = ap_get_sta(hapd, addr);
2391 if (!sta || !sta->wpa_sm) {
2392 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2393 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002394 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002395 }
2396 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002397 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002398 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2399 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002400 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002401 }
2402
2403 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2404}
2405
Hai Shaloma20dcd72022-02-04 13:43:00 -08002406
2407static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2408 const char *cmd)
2409{
2410 u16 type;
2411 char *pos, *end;
2412 u8 match[10];
2413 size_t match_len;
2414 bool multicast = false;
2415
2416 type = strtol(cmd, &pos, 16);
2417 if (*pos != ' ')
2418 return -1;
2419 pos++;
2420 end = os_strchr(pos, ' ');
2421 if (end) {
2422 match_len = end - pos;
2423 multicast = os_strstr(end, "multicast") != NULL;
2424 } else {
2425 match_len = os_strlen(pos) / 2;
2426 }
2427 if (hexstr2bin(pos, match, match_len))
2428 return -1;
2429
2430 return hostapd_drv_register_frame(hapd, type, match, match_len,
2431 multicast);
2432}
2433
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002434#endif /* CONFIG_TESTING_OPTIONS */
2435
2436
Hai Shalomb755a2a2020-04-23 21:49:02 -07002437#ifdef NEED_AP_MLME
Hai Shalomfdcde762020-04-02 11:19:20 -07002438static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2439{
2440 switch (params->bandwidth) {
2441 case 0:
2442 /* bandwidth not specified: use 20 MHz by default */
2443 /* fall-through */
2444 case 20:
2445 if (params->center_freq1 &&
2446 params->center_freq1 != params->freq)
2447 return -1;
2448
2449 if (params->center_freq2 || params->sec_channel_offset)
2450 return -1;
2451 break;
2452 case 40:
2453 if (params->center_freq2 || !params->sec_channel_offset)
2454 return -1;
2455
2456 if (!params->center_freq1)
2457 break;
2458 switch (params->sec_channel_offset) {
2459 case 1:
2460 if (params->freq + 10 != params->center_freq1)
2461 return -1;
2462 break;
2463 case -1:
2464 if (params->freq - 10 != params->center_freq1)
2465 return -1;
2466 break;
2467 default:
2468 return -1;
2469 }
2470 break;
2471 case 80:
2472 if (!params->center_freq1 || !params->sec_channel_offset)
2473 return 1;
2474
2475 switch (params->sec_channel_offset) {
2476 case 1:
2477 if (params->freq - 10 != params->center_freq1 &&
2478 params->freq + 30 != params->center_freq1)
2479 return 1;
2480 break;
2481 case -1:
2482 if (params->freq + 10 != params->center_freq1 &&
2483 params->freq - 30 != params->center_freq1)
2484 return -1;
2485 break;
2486 default:
2487 return -1;
2488 }
2489
2490 /* Adjacent and overlapped are not allowed for 80+80 */
2491 if (params->center_freq2 &&
2492 params->center_freq1 - params->center_freq2 <= 80 &&
2493 params->center_freq2 - params->center_freq1 <= 80)
2494 return 1;
2495 break;
2496 case 160:
2497 if (!params->center_freq1 || params->center_freq2 ||
2498 !params->sec_channel_offset)
2499 return -1;
2500
2501 switch (params->sec_channel_offset) {
2502 case 1:
2503 if (params->freq + 70 != params->center_freq1 &&
2504 params->freq + 30 != params->center_freq1 &&
2505 params->freq - 10 != params->center_freq1 &&
2506 params->freq - 50 != params->center_freq1)
2507 return -1;
2508 break;
2509 case -1:
2510 if (params->freq + 50 != params->center_freq1 &&
2511 params->freq + 10 != params->center_freq1 &&
2512 params->freq - 30 != params->center_freq1 &&
2513 params->freq - 70 != params->center_freq1)
2514 return -1;
2515 break;
2516 default:
2517 return -1;
2518 }
2519 break;
2520 default:
2521 return -1;
2522 }
2523
2524 return 0;
2525}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002526#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002527
2528
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002529static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2530 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002531{
2532#ifdef NEED_AP_MLME
2533 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002534 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002535 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002536 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002537 int bandwidth;
2538 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002539
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002540 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002541 if (ret)
2542 return ret;
2543
Hai Shalomfdcde762020-04-02 11:19:20 -07002544 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2545 if (ret) {
2546 wpa_printf(MSG_INFO,
2547 "chanswitch: invalid frequency settings provided");
2548 return ret;
2549 }
2550
2551 switch (settings.freq_params.bandwidth) {
2552 case 40:
2553 bandwidth = CHAN_WIDTH_40;
2554 break;
2555 case 80:
2556 if (settings.freq_params.center_freq2)
2557 bandwidth = CHAN_WIDTH_80P80;
2558 else
2559 bandwidth = CHAN_WIDTH_80;
2560 break;
2561 case 160:
2562 bandwidth = CHAN_WIDTH_160;
2563 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00002564 case 320:
2565 bandwidth = CHAN_WIDTH_320;
2566 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07002567 default:
2568 bandwidth = CHAN_WIDTH_20;
2569 break;
2570 }
2571
2572 if (settings.freq_params.center_freq1)
2573 dfs_range += hostapd_is_dfs_overlap(
2574 iface, bandwidth, settings.freq_params.center_freq1);
2575 else
2576 dfs_range += hostapd_is_dfs_overlap(
2577 iface, bandwidth, settings.freq_params.freq);
2578
2579 if (settings.freq_params.center_freq2)
2580 dfs_range += hostapd_is_dfs_overlap(
2581 iface, bandwidth, settings.freq_params.center_freq2);
2582
2583 if (dfs_range) {
2584 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2585 if (ret == NUM_HOSTAPD_MODES) {
2586 wpa_printf(MSG_ERROR,
2587 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2588 settings.freq_params.freq,
2589 settings.freq_params.sec_channel_offset,
2590 settings.freq_params.bandwidth);
2591 return -1;
2592 }
2593
2594 settings.freq_params.channel = chan;
2595
2596 wpa_printf(MSG_DEBUG,
2597 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2598 settings.freq_params.channel,
2599 settings.freq_params.freq,
2600 settings.freq_params.sec_channel_offset,
2601 settings.freq_params.bandwidth,
2602 settings.freq_params.center_freq1);
2603
2604 /* Perform CAC and switch channel */
2605 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2606 return 0;
2607 }
2608
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002609 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002610
Sunil Ravia04bd252022-05-02 22:54:18 -07002611 /* Save CHAN_SWITCH VHT, HE, and EHT config */
Hai Shalom60840252021-02-19 19:02:11 -08002612 hostapd_chan_switch_config(iface->bss[i],
2613 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002614
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002615 ret = hostapd_switch_channel(iface->bss[i], &settings);
2616 if (ret) {
2617 /* FIX: What do we do if CSA fails in the middle of
2618 * submitting multi-BSS CSA requests? */
2619 return ret;
2620 }
2621 }
2622
2623 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002624#else /* NEED_AP_MLME */
2625 return -1;
2626#endif /* NEED_AP_MLME */
2627}
2628
2629
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002630static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2631 int reply_size, const char *param)
2632{
2633#ifdef RADIUS_SERVER
2634 if (os_strcmp(param, "radius_server") == 0) {
2635 return radius_server_get_mib(hapd->radius_srv, reply,
2636 reply_size);
2637 }
2638#endif /* RADIUS_SERVER */
2639 return -1;
2640}
2641
2642
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002643static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2644 char *buf, size_t buflen)
2645{
2646 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002647 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002648 u8 *data = NULL;
2649 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002650 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002651 struct wpabuf *reply;
2652 size_t data_len = 0;
2653
Hai Shalom60840252021-02-19 19:02:11 -08002654 /**
2655 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2656 * [nested=<0|1>]
2657 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002658 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002659 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002660 return -EINVAL;
2661
2662 subcmd = strtoul(pos, &pos, 10);
2663
2664 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002665 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002666 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002667
2668 temp = os_strchr(pos, ' ');
2669 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002670 }
2671
2672 if (data_len) {
2673 data_len /= 2;
2674 data = os_malloc(data_len);
2675 if (!data)
2676 return -ENOBUFS;
2677
2678 if (hexstr2bin(pos, data, data_len)) {
2679 wpa_printf(MSG_DEBUG,
2680 "Vendor command: wrong parameter format");
2681 os_free(data);
2682 return -EINVAL;
2683 }
2684 }
2685
Hai Shalom60840252021-02-19 19:02:11 -08002686 pos = os_strstr(cmd, "nested=");
2687 if (pos)
2688 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2689 NESTED_ATTR_NOT_USED;
2690
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002691 reply = wpabuf_alloc((buflen - 1) / 2);
2692 if (!reply) {
2693 os_free(data);
2694 return -ENOBUFS;
2695 }
2696
2697 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002698 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002699
2700 if (ret == 0)
2701 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2702 wpabuf_len(reply));
2703
2704 wpabuf_free(reply);
2705 os_free(data);
2706
2707 return ret;
2708}
2709
2710
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002711static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2712 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002714 u8 addr[ETH_ALEN];
2715 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002717 if (hwaddr_aton(cmd, addr))
2718 return -1;
2719
2720 sta = ap_get_sta(hapd, addr);
2721 if (!sta || !sta->eapol_sm)
2722 return -1;
2723
2724 eapol_auth_reauthenticate(sta->eapol_sm);
2725 return 0;
2726}
2727
2728
2729static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2730{
2731 u8 addr[ETH_ALEN];
2732 struct sta_info *sta;
2733 char *pos = cmd, *param;
2734
2735 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2736 return -1;
2737 pos += 18;
2738 param = pos;
2739 pos = os_strchr(pos, ' ');
2740 if (!pos)
2741 return -1;
2742 *pos++ = '\0';
2743
2744 sta = ap_get_sta(hapd, addr);
2745 if (!sta || !sta->eapol_sm)
2746 return -1;
2747
2748 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2749}
2750
2751
2752static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2753 char *buf, size_t buflen)
2754{
2755 char *pos, *end, *stamp;
2756 int ret;
2757
2758 /* cmd: "LOG_LEVEL [<level>]" */
2759 if (*cmd == '\0') {
2760 pos = buf;
2761 end = buf + buflen;
2762 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2763 "Timestamp: %d\n",
2764 debug_level_str(wpa_debug_level),
2765 wpa_debug_timestamp);
2766 if (os_snprintf_error(end - pos, ret))
2767 ret = 0;
2768
2769 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002772 while (*cmd == ' ')
2773 cmd++;
2774
2775 stamp = os_strchr(cmd, ' ');
2776 if (stamp) {
2777 *stamp++ = '\0';
2778 while (*stamp == ' ') {
2779 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 }
2782
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783 if (os_strlen(cmd)) {
2784 int level = str_to_debug_level(cmd);
2785 if (level < 0)
2786 return -1;
2787 wpa_debug_level = level;
2788 }
2789
2790 if (stamp && os_strlen(stamp))
2791 wpa_debug_timestamp = atoi(stamp);
2792
2793 os_memcpy(buf, "OK\n", 3);
2794 return 3;
2795}
2796
2797
2798#ifdef NEED_AP_MLME
2799static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2800 char *buf, size_t buflen)
2801{
2802 struct hostapd_iface *iface = hapd->iface;
2803 char *pos, *end;
2804 struct hostapd_sta_info *info;
2805 struct os_reltime now;
2806
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002807 if (!iface->num_sta_seen)
2808 return 0;
2809
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002810 sta_track_expire(iface, 0);
2811
2812 pos = buf;
2813 end = buf + buflen;
2814
2815 os_get_reltime(&now);
2816 dl_list_for_each_reverse(info, &iface->sta_seen,
2817 struct hostapd_sta_info, list) {
2818 struct os_reltime age;
2819 int ret;
2820
2821 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002822 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2823 MAC2STR(info->addr), (unsigned int) age.sec,
2824 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002825 if (os_snprintf_error(end - pos, ret))
2826 break;
2827 pos += ret;
2828 }
2829
2830 return pos - buf;
2831}
2832#endif /* NEED_AP_MLME */
2833
2834
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002835static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2836 const char *cmd)
2837{
2838 u8 addr[ETH_ALEN];
2839
2840 if (hwaddr_aton(cmd, addr)) {
2841 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2842 return -1;
2843 }
2844
2845 return hostapd_send_lci_req(hapd, addr);
2846}
2847
2848
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002849static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002850{
2851 u8 addr[ETH_ALEN];
2852 char *token, *context = NULL;
2853 int random_interval, min_ap;
2854 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2855 unsigned int n_responders;
2856
2857 token = str_token(cmd, " ", &context);
2858 if (!token || hwaddr_aton(token, addr)) {
2859 wpa_printf(MSG_INFO,
2860 "CTRL: REQ_RANGE - Bad destination address");
2861 return -1;
2862 }
2863
2864 token = str_token(cmd, " ", &context);
2865 if (!token)
2866 return -1;
2867
2868 random_interval = atoi(token);
2869 if (random_interval < 0 || random_interval > 0xffff)
2870 return -1;
2871
2872 token = str_token(cmd, " ", &context);
2873 if (!token)
2874 return -1;
2875
2876 min_ap = atoi(token);
2877 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2878 return -1;
2879
2880 n_responders = 0;
2881 while ((token = str_token(cmd, " ", &context))) {
2882 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2883 wpa_printf(MSG_INFO,
2884 "CTRL: REQ_RANGE: Too many responders");
2885 return -1;
2886 }
2887
2888 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2889 wpa_printf(MSG_INFO,
2890 "CTRL: REQ_RANGE: Bad responder address");
2891 return -1;
2892 }
2893
2894 n_responders++;
2895 }
2896
2897 if (!n_responders) {
2898 wpa_printf(MSG_INFO,
2899 "CTRL: REQ_RANGE - No FTM responder address");
2900 return -1;
2901 }
2902
2903 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2904 responders, n_responders);
2905}
2906
2907
Dmitry Shmidt29333592017-01-09 12:27:11 -08002908static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2909 const char *cmd, char *reply,
2910 size_t reply_size)
2911{
2912 u8 addr[ETH_ALEN];
2913 const char *pos;
2914 struct wpabuf *req;
2915 int ret;
2916 u8 req_mode = 0;
2917
2918 if (hwaddr_aton(cmd, addr))
2919 return -1;
2920 pos = os_strchr(cmd, ' ');
2921 if (!pos)
2922 return -1;
2923 pos++;
2924 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2925 int val = hex2byte(pos + 9);
2926
2927 if (val < 0)
2928 return -1;
2929 req_mode = val;
2930 pos += 11;
2931 pos = os_strchr(pos, ' ');
2932 if (!pos)
2933 return -1;
2934 pos++;
2935 }
2936 req = wpabuf_parse_bin(pos);
2937 if (!req)
2938 return -1;
2939
2940 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2941 wpabuf_free(req);
2942 if (ret >= 0)
2943 ret = os_snprintf(reply, reply_size, "%d", ret);
2944 return ret;
2945}
2946
2947
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002948static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2949 char *buf, size_t buflen)
2950{
2951 if (!(hapd->conf->radio_measurements[0] &
2952 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2953 wpa_printf(MSG_ERROR,
2954 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2955 return -1;
2956 }
2957
2958 return hostapd_neighbor_show(hapd, buf, buflen);
2959}
2960
2961
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002962static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2963{
2964 struct wpa_ssid_value ssid;
2965 u8 bssid[ETH_ALEN];
2966 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002967 int stationary = 0;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002968 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002969 char *tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002970 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002971
2972 if (!(hapd->conf->radio_measurements[0] &
2973 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2974 wpa_printf(MSG_ERROR,
2975 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2976 return -1;
2977 }
2978
2979 if (hwaddr_aton(buf, bssid)) {
2980 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2981 return -1;
2982 }
2983
2984 tmp = os_strstr(buf, "ssid=");
2985 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2986 wpa_printf(MSG_ERROR,
2987 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2988 return -1;
2989 }
2990 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2991 if (!buf)
2992 return -1;
2993
2994 tmp = os_strstr(buf, "nr=");
2995 if (!tmp) {
2996 wpa_printf(MSG_ERROR,
2997 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2998 return -1;
2999 }
3000
3001 buf = os_strchr(tmp, ' ');
3002 if (buf)
3003 *buf++ = '\0';
3004
3005 nr = wpabuf_parse_bin(tmp + 3);
3006 if (!nr) {
3007 wpa_printf(MSG_ERROR,
3008 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3009 return -1;
3010 }
3011
3012 if (!buf)
3013 goto set;
3014
3015 tmp = os_strstr(buf, "lci=");
3016 if (tmp) {
3017 buf = os_strchr(tmp, ' ');
3018 if (buf)
3019 *buf++ = '\0';
3020 lci = wpabuf_parse_bin(tmp + 4);
3021 if (!lci) {
3022 wpa_printf(MSG_ERROR,
3023 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003024 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003025 }
3026 }
3027
3028 if (!buf)
3029 goto set;
3030
3031 tmp = os_strstr(buf, "civic=");
3032 if (tmp) {
3033 buf = os_strchr(tmp, ' ');
3034 if (buf)
3035 *buf++ = '\0';
3036 civic = wpabuf_parse_bin(tmp + 6);
3037 if (!civic) {
3038 wpa_printf(MSG_ERROR,
3039 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shaloma20dcd72022-02-04 13:43:00 -08003040 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003041 }
3042 }
3043
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003044 if (!buf)
3045 goto set;
3046
3047 if (os_strstr(buf, "stat"))
3048 stationary = 1;
3049
Hai Shaloma20dcd72022-02-04 13:43:00 -08003050 tmp = os_strstr(buf, "bss_parameter=");
3051 if (tmp) {
3052 bss_parameters = atoi(tmp + 14);
3053 if (bss_parameters < 0 || bss_parameters > 0xff) {
3054 wpa_printf(MSG_ERROR,
3055 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3056 goto fail;
3057 }
3058 }
3059
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003060set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003061 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shaloma20dcd72022-02-04 13:43:00 -08003062 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003063
Hai Shaloma20dcd72022-02-04 13:43:00 -08003064fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003065 wpabuf_free(nr);
3066 wpabuf_free(lci);
3067 wpabuf_free(civic);
3068
3069 return ret;
3070}
3071
3072
3073static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3074 char *buf)
3075{
3076 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003077 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003078 u8 bssid[ETH_ALEN];
3079 char *tmp;
3080
3081 if (hwaddr_aton(buf, bssid)) {
3082 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3083 return -1;
3084 }
3085
3086 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003087 if (tmp) {
3088 ssidp = &ssid;
3089 if (ssid_parse(tmp + 5, &ssid)) {
3090 wpa_printf(MSG_ERROR,
3091 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3092 return -1;
3093 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003094 }
3095
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003096 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003097}
3098
3099
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003100static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3101 size_t buflen)
3102{
3103 int ret, i;
3104 char *pos, *end;
3105
3106 ret = os_snprintf(buf, buflen, "%016llX:\n",
3107 (long long unsigned) iface->drv_flags);
3108 if (os_snprintf_error(buflen, ret))
3109 return -1;
3110
3111 pos = buf + ret;
3112 end = buf + buflen;
3113
3114 for (i = 0; i < 64; i++) {
3115 if (iface->drv_flags & (1LLU << i)) {
3116 ret = os_snprintf(pos, end - pos, "%s\n",
3117 driver_flag_to_string(1LLU << i));
3118 if (os_snprintf_error(end - pos, ret))
3119 return -1;
3120 pos += ret;
3121 }
3122 }
3123
3124 return pos - buf;
3125}
3126
3127
Hai Shalomb755a2a2020-04-23 21:49:02 -07003128static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3129 size_t buflen)
3130{
3131 int ret, i;
3132 char *pos, *end;
3133
3134 ret = os_snprintf(buf, buflen, "%016llX:\n",
3135 (long long unsigned) iface->drv_flags2);
3136 if (os_snprintf_error(buflen, ret))
3137 return -1;
3138
3139 pos = buf + ret;
3140 end = buf + buflen;
3141
3142 for (i = 0; i < 64; i++) {
3143 if (iface->drv_flags2 & (1LLU << i)) {
3144 ret = os_snprintf(pos, end - pos, "%s\n",
3145 driver_flag2_to_string(1LLU << i));
3146 if (os_snprintf_error(end - pos, ret))
3147 return -1;
3148 pos += ret;
3149 }
3150 }
3151
3152 return pos - buf;
3153}
3154
3155
Hai Shalom021b0b52019-04-10 11:17:58 -07003156static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3157 const char *field, char *buf,
3158 size_t buflen)
3159{
3160 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3161
3162#ifdef CONFIG_DPP
3163 if (os_strcmp(field, "dpp") == 0) {
3164 int res;
3165
Hai Shaloma20dcd72022-02-04 13:43:00 -08003166#ifdef CONFIG_DPP3
3167 res = os_snprintf(buf, buflen, "DPP=3");
3168#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003169 res = os_snprintf(buf, buflen, "DPP=2");
3170#else /* CONFIG_DPP2 */
3171 res = os_snprintf(buf, buflen, "DPP=1");
3172#endif /* CONFIG_DPP2 */
3173 if (os_snprintf_error(buflen, res))
3174 return -1;
3175 return res;
3176 }
3177#endif /* CONFIG_DPP */
3178
3179 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3180 field);
3181
3182 return -1;
3183}
3184
3185
Hai Shaloma20dcd72022-02-04 13:43:00 -08003186#ifdef ANDROID
3187static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3188 char *buf, size_t buflen)
3189{
3190 int ret;
3191
3192 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3193 if (ret == 0) {
3194 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3195 if (os_snprintf_error(buflen, ret))
3196 ret = -1;
3197 }
3198 return ret;
3199}
3200#endif /* ANDROID */
3201
3202
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003203static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3204 char *buf, char *reply,
3205 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003206 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003207 socklen_t fromlen)
3208{
3209 int reply_len, res;
3210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 os_memcpy(reply, "OK\n", 3);
3212 reply_len = 3;
3213
3214 if (os_strcmp(buf, "PING") == 0) {
3215 os_memcpy(reply, "PONG\n", 5);
3216 reply_len = 5;
3217 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3218 if (wpa_debug_reopen_file() < 0)
3219 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003220 } else if (os_strcmp(buf, "CLOSE_LOG") == 0) {
3221 wpa_debug_stop_log();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003222 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3223 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003224 } else if (os_strcmp(buf, "STATUS") == 0) {
3225 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3226 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003227 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3228 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 } else if (os_strcmp(buf, "MIB") == 0) {
3230 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3231 if (reply_len >= 0) {
3232 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3233 reply_size - reply_len);
3234 if (res < 0)
3235 reply_len = -1;
3236 else
3237 reply_len += res;
3238 }
3239 if (reply_len >= 0) {
3240 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3241 reply_size - reply_len);
3242 if (res < 0)
3243 reply_len = -1;
3244 else
3245 reply_len += res;
3246 }
3247#ifndef CONFIG_NO_RADIUS
3248 if (reply_len >= 0) {
3249 res = radius_client_get_mib(hapd->radius,
3250 reply + reply_len,
3251 reply_size - reply_len);
3252 if (res < 0)
3253 reply_len = -1;
3254 else
3255 reply_len += res;
3256 }
3257#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003258 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3259 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3260 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3262 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3263 reply_size);
3264 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3265 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3266 reply_size);
3267 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3268 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3269 reply_size);
3270 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003271 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3272 reply_len = -1;
3273 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3274 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 reply_len = -1;
3276 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003277 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 reply_len = -1;
3279 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003280 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 buf + 6))
3282 reply_len = -1;
3283 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3284 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3285 reply_len = -1;
3286 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3287 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3288 reply_len = -1;
3289 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3290 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3291 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003292#ifdef CONFIG_TAXONOMY
3293 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3294 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3295 reply, reply_size);
3296#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003297 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3298 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3299 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003300 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3301 if (hostapd_ctrl_iface_stop_ap(hapd))
3302 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303#ifdef NEED_AP_MLME
3304 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3305 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3306 reply_len = -1;
3307#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308#ifdef CONFIG_WPS
3309 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3310 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3311 reply_len = -1;
3312 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3313 reply_len = hostapd_ctrl_iface_wps_check_pin(
3314 hapd, buf + 14, reply, reply_size);
3315 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3316 if (hostapd_wps_button_pushed(hapd, NULL))
3317 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003318 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3319 if (hostapd_wps_cancel(hapd))
3320 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3322 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3323 reply, reply_size);
3324 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3325 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3326 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003327 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3328 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3329 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003330#ifdef CONFIG_WPS_NFC
3331 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3332 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3333 reply_len = -1;
3334 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3335 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3336 hapd, buf + 21, reply, reply_size);
3337 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3338 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3339 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003340 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3341 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3342 hapd, buf + 21, reply, reply_size);
3343 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3344 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3345 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003346#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003348#ifdef CONFIG_INTERWORKING
3349 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3350 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3351 reply_len = -1;
3352 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3353 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3354 reply_len = -1;
3355#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003356#ifdef CONFIG_HS20
3357 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3358 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3359 reply_len = -1;
3360 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3361 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3362 reply_len = -1;
3363#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003364#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003365 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3366 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3367 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3369 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3370 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003371 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3372 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3373 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003374 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3375 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3376 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003377#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3379 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3380 reply_size);
3381 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3382 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3383 reply_len = -1;
3384 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3385 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3386 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003387 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3388 if (hostapd_ctrl_iface_enable(hapd->iface))
3389 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003390 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3391 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3392 reply_len = -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003393 } else if (os_strcmp(buf, "RELOAD_BSS") == 0) {
3394 if (hostapd_ctrl_iface_reload_bss(hapd))
3395 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003396 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3397 if (hostapd_ctrl_iface_reload(hapd->iface))
3398 reply_len = -1;
3399 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3400 if (hostapd_ctrl_iface_disable(hapd->iface))
3401 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003402 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3403 if (ieee802_11_set_beacon(hapd))
3404 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003405#ifdef CONFIG_TESTING_OPTIONS
3406 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3407 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3408 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003409 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3410 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3411 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003412 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3413 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3414 buf + 23) < 0)
3415 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003416 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3417 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3418 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003419 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3420 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3421 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003422 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3423 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3424 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003425 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3426 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3427 reply_len = -1;
3428 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3429 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3430 reply_len = -1;
3431 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3432 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3433 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003434 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3435 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3436 reply_len = -1;
3437 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3438 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3439 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003440 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3441 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3442 reply_len = -1;
3443 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3444 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003445 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3446 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3447 reply_len = -1;
3448 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3449 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3450 reply_len = -1;
3451 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3452 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3453 reply_len = -1;
3454 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3455 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3456 reply_len = -1;
3457 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3458 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3459 reply_len = -1;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003460 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3461 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3462 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003463 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3464 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3465 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003466 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3467 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3468 reply_size);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003469 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3470 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3471 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003472#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003473 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003474 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003475 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003476 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3477 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3478 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003479 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3480 ieee802_1x_erp_flush(hapd);
3481#ifdef RADIUS_SERVER
3482 radius_server_erp_flush(hapd->radius_srv);
3483#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003484 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3485 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3486 reply_len = -1;
3487 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3488 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3489 reply_len = -1;
3490 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3491 reply_len = hostapd_ctrl_iface_log_level(
3492 hapd, buf + 9, reply, reply_size);
3493#ifdef NEED_AP_MLME
3494 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3495 reply_len = hostapd_ctrl_iface_track_sta_list(
3496 hapd, reply, reply_size);
3497#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003498 } else if (os_strcmp(buf, "PMKSA") == 0) {
3499 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3500 reply_size);
3501 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3502 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003503 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3504 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3505 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003506 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3507 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3508 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003509 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3510 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3511 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003512 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3513 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3514 reply_len = -1;
3515 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3516 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3517 reply_len = -1;
3518 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3519 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3520 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003521 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3522 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3523 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003524 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3525 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3526 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003527 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3528 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3529 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003530 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3531 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003532 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3533 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003534 if (hostapd_ctrl_iface_acl_add_mac(
3535 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003536 &hapd->conf->num_accept_mac, buf + 19) ||
3537 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003538 reply_len = -1;
3539 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003540 if (hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003541 &hapd->conf->accept_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003542 &hapd->conf->num_accept_mac, buf + 19) ||
3543 hostapd_set_acl(hapd) ||
3544 hostapd_disassoc_accept_mac(hapd))
Roshan Pius3a1667e2018-07-03 15:17:14 -07003545 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003546 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3547 reply_len = hostapd_ctrl_iface_acl_show_mac(
3548 hapd->conf->accept_mac,
3549 hapd->conf->num_accept_mac, reply, reply_size);
3550 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3551 hostapd_ctrl_iface_acl_clear_list(
3552 &hapd->conf->accept_mac,
3553 &hapd->conf->num_accept_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003554 if (hostapd_set_acl(hapd) ||
3555 hostapd_disassoc_accept_mac(hapd))
3556 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003557 }
3558 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3559 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003560 if (hostapd_ctrl_iface_acl_add_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003561 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003562 &hapd->conf->num_deny_mac, buf + 17) ||
3563 hostapd_set_acl(hapd) ||
3564 hostapd_disassoc_deny_mac(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003565 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003566 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003567 if (hostapd_ctrl_iface_acl_del_mac(
3568 &hapd->conf->deny_mac,
Sunil Ravia04bd252022-05-02 22:54:18 -07003569 &hapd->conf->num_deny_mac, buf + 17) ||
3570 hostapd_set_acl(hapd))
Hai Shalom60840252021-02-19 19:02:11 -08003571 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003572 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3573 reply_len = hostapd_ctrl_iface_acl_show_mac(
3574 hapd->conf->deny_mac,
3575 hapd->conf->num_deny_mac, reply, reply_size);
3576 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3577 hostapd_ctrl_iface_acl_clear_list(
3578 &hapd->conf->deny_mac,
3579 &hapd->conf->num_deny_mac);
Sunil Ravia04bd252022-05-02 22:54:18 -07003580 if (hostapd_set_acl(hapd))
3581 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003582 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003583#ifdef CONFIG_DPP
3584 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3585 res = hostapd_dpp_qr_code(hapd, buf + 12);
3586 if (res < 0) {
3587 reply_len = -1;
3588 } else {
3589 reply_len = os_snprintf(reply, reply_size, "%d", res);
3590 if (os_snprintf_error(reply_size, reply_len))
3591 reply_len = -1;
3592 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003593 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3594 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3595 if (res < 0) {
3596 reply_len = -1;
3597 } else {
3598 reply_len = os_snprintf(reply, reply_size, "%d", res);
3599 if (os_snprintf_error(reply_size, reply_len))
3600 reply_len = -1;
3601 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003602 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3603 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3604 if (res < 0) {
3605 reply_len = -1;
3606 } else {
3607 reply_len = os_snprintf(reply, reply_size, "%d", res);
3608 if (os_snprintf_error(reply_size, reply_len))
3609 reply_len = -1;
3610 }
3611 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3612 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3613 if (res < 0) {
3614 reply_len = -1;
3615 } else {
3616 reply_len = os_snprintf(reply, reply_size, "%d", res);
3617 if (os_snprintf_error(reply_size, reply_len))
3618 reply_len = -1;
3619 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003620 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003621 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003622 if (res < 0) {
3623 reply_len = -1;
3624 } else {
3625 reply_len = os_snprintf(reply, reply_size, "%d", res);
3626 if (os_snprintf_error(reply_size, reply_len))
3627 reply_len = -1;
3628 }
3629 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003630 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3631 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003632 reply_len = -1;
3633 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3634 const char *uri;
3635
Hai Shalom021b0b52019-04-10 11:17:58 -07003636 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3637 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003638 if (!uri) {
3639 reply_len = -1;
3640 } else {
3641 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3642 if (os_snprintf_error(reply_size, reply_len))
3643 reply_len = -1;
3644 }
3645 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003646 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3647 atoi(buf + 19),
3648 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003649 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3650 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3651 atoi(buf + 18),
3652 os_strchr(buf + 18, ' ')) < 0)
3653 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003654 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3655 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3656 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003657 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3658 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3659 reply_len = -1;
3660 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3661 hostapd_dpp_stop(hapd);
3662 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003663 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003664 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3665 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003666 if (res < 0) {
3667 reply_len = -1;
3668 } else {
3669 reply_len = os_snprintf(reply, reply_size, "%d", res);
3670 if (os_snprintf_error(reply_size, reply_len))
3671 reply_len = -1;
3672 }
Sunil Ravia04bd252022-05-02 22:54:18 -07003673 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
3674 if (dpp_configurator_set(hapd->iface->interfaces->dpp,
3675 buf + 20) < 0)
3676 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003677 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003678 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3679 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003680 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003681 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003682 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003683 reply_len = -1;
3684 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003685 reply_len = dpp_configurator_get_key_id(
3686 hapd->iface->interfaces->dpp,
3687 atoi(buf + 25),
3688 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003689 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3690 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3691 if (res < 0) {
3692 reply_len = -1;
3693 } else {
3694 reply_len = os_snprintf(reply, reply_size, "%d", res);
3695 if (os_snprintf_error(reply_size, reply_len))
3696 reply_len = -1;
3697 }
3698 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3699 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3700 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003701#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003702 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3703 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3704 reply_len = -1;
3705 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3706 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3707 reply_len = -1;
3708 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3709 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003710 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3711 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3712 reply_len = -1;
3713 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3714 hostapd_dpp_chirp_stop(hapd);
Sunil Ravi89eba102022-09-13 21:04:37 -07003715 } else if (os_strncmp(buf, "DPP_RELAY_ADD_CONTROLLER ", 25) == 0) {
3716 if (hostapd_dpp_add_controller(hapd, buf + 25) < 0)
3717 reply_len = -1;
3718 } else if (os_strncmp(buf, "DPP_RELAY_REMOVE_CONTROLLER ", 28) == 0) {
3719 hostapd_dpp_remove_controller(hapd, buf + 28);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003720#endif /* CONFIG_DPP2 */
Sunil Ravi89eba102022-09-13 21:04:37 -07003721#ifdef CONFIG_DPP3
3722 } else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
3723 if (hostapd_dpp_push_button(hapd, NULL) < 0)
3724 reply_len = -1;
3725 } else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
3726 if (hostapd_dpp_push_button(hapd, buf + 15) < 0)
3727 reply_len = -1;
3728#endif /* CONFIG_DPP3 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003729#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003730#ifdef RADIUS_SERVER
3731 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3732 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3733 reply_len = -1;
3734#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003735 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3736 reply_len = hostapd_ctrl_iface_get_capability(
3737 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08003738#ifdef CONFIG_PASN
3739 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3740 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3741#endif /* CONFIG_PASN */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003742#ifdef ANDROID
3743 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3744 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
3745 reply_size);
3746#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 } else {
3748 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3749 reply_len = 16;
3750 }
3751
3752 if (reply_len < 0) {
3753 os_memcpy(reply, "FAIL\n", 5);
3754 reply_len = 5;
3755 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003756
3757 return reply_len;
3758}
3759
3760
3761static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3762 void *sock_ctx)
3763{
3764 struct hostapd_data *hapd = eloop_ctx;
3765 char buf[4096];
3766 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003767 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003768 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003769 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003770 const int reply_size = 4096;
3771 int reply_len;
3772 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003773#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003774 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003775#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003776
3777 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3778 (struct sockaddr *) &from, &fromlen);
3779 if (res < 0) {
3780 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3781 strerror(errno));
3782 return;
3783 }
3784 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003785
3786 reply = os_malloc(reply_size);
3787 if (reply == NULL) {
3788 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3789 fromlen) < 0) {
3790 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3791 strerror(errno));
3792 }
3793 return;
3794 }
3795
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003796#ifdef CONFIG_CTRL_IFACE_UDP
3797 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3798 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003799 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3800 hapd->ctrl_iface_cookie,
3801 CTRL_IFACE_COOKIE_LEN);
3802 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003803 goto done;
3804 }
3805
3806 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003807 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003808 wpa_printf(MSG_DEBUG,
3809 "CTRL: No cookie in the request - drop request");
3810 os_free(reply);
3811 return;
3812 }
3813
Hai Shalomfdcde762020-04-02 11:19:20 -07003814 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3815 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003816 wpa_printf(MSG_DEBUG,
3817 "CTRL: Invalid cookie in the request - drop request");
3818 os_free(reply);
3819 return;
3820 }
3821
Hai Shalomfdcde762020-04-02 11:19:20 -07003822 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003823 while (*pos == ' ')
3824 pos++;
3825#endif /* CONFIG_CTRL_IFACE_UDP */
3826
3827 if (os_strcmp(pos, "PING") == 0)
3828 level = MSG_EXCESSIVE;
3829 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3830
3831 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003832 reply, reply_size,
3833 &from, fromlen);
3834
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003835#ifdef CONFIG_CTRL_IFACE_UDP
3836done:
3837#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003838 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3839 fromlen) < 0) {
3840 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3841 strerror(errno));
3842 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003843 os_free(reply);
3844}
3845
3846
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003847#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3849{
3850 char *buf;
3851 size_t len;
3852
3853 if (hapd->conf->ctrl_interface == NULL)
3854 return NULL;
3855
3856 len = os_strlen(hapd->conf->ctrl_interface) +
3857 os_strlen(hapd->conf->iface) + 2;
3858 buf = os_malloc(len);
3859 if (buf == NULL)
3860 return NULL;
3861
3862 os_snprintf(buf, len, "%s/%s",
3863 hapd->conf->ctrl_interface, hapd->conf->iface);
3864 buf[len - 1] = '\0';
3865 return buf;
3866}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003867#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868
3869
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003870static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3871 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 const char *txt, size_t len)
3873{
3874 struct hostapd_data *hapd = ctx;
3875 if (hapd == NULL)
3876 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003877 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878}
3879
3880
3881int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3882{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003883#ifdef CONFIG_CTRL_IFACE_UDP
3884 int port = HOSTAPD_CTRL_IFACE_PORT;
3885 char p[32] = { 0 };
3886 char port_str[40], *tmp;
3887 char *pos;
3888 struct addrinfo hints = { 0 }, *res, *saveres;
3889 int n;
3890
3891 if (hapd->ctrl_sock > -1) {
3892 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3893 return 0;
3894 }
3895
3896 if (hapd->conf->ctrl_interface == NULL)
3897 return 0;
3898
3899 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3900 if (pos) {
3901 pos += 4;
3902 port = atoi(pos);
3903 if (port <= 0) {
3904 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3905 goto fail;
3906 }
3907 }
3908
3909 dl_list_init(&hapd->ctrl_dst);
3910 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003911 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003912
3913#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3914 hints.ai_flags = AI_PASSIVE;
3915#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3916
3917#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3918 hints.ai_family = AF_INET6;
3919#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3920 hints.ai_family = AF_INET;
3921#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3922 hints.ai_socktype = SOCK_DGRAM;
3923
3924try_again:
3925 os_snprintf(p, sizeof(p), "%d", port);
3926 n = getaddrinfo(NULL, p, &hints, &res);
3927 if (n) {
3928 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3929 goto fail;
3930 }
3931
3932 saveres = res;
3933 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3934 res->ai_protocol);
3935 if (hapd->ctrl_sock < 0) {
3936 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3937 goto fail;
3938 }
3939
3940 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3941 port--;
3942 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3943 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3944 goto try_again;
3945 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3946 goto fail;
3947 }
3948
3949 freeaddrinfo(saveres);
3950
3951 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3952 tmp = os_strdup(port_str);
3953 if (tmp) {
3954 os_free(hapd->conf->ctrl_interface);
3955 hapd->conf->ctrl_interface = tmp;
3956 }
3957 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3958
3959 if (eloop_register_read_sock(hapd->ctrl_sock,
3960 hostapd_ctrl_iface_receive, hapd, NULL) <
3961 0) {
3962 hostapd_ctrl_iface_deinit(hapd);
3963 return -1;
3964 }
3965
3966 hapd->msg_ctx = hapd;
3967 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3968
3969 return 0;
3970
3971fail:
3972 if (hapd->ctrl_sock >= 0)
3973 close(hapd->ctrl_sock);
3974 return -1;
3975#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 struct sockaddr_un addr;
3977 int s = -1;
3978 char *fname = NULL;
3979
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980 if (hapd->ctrl_sock > -1) {
3981 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3982 return 0;
3983 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003985 dl_list_init(&hapd->ctrl_dst);
3986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 if (hapd->conf->ctrl_interface == NULL)
3988 return 0;
3989
3990 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3991 if (errno == EEXIST) {
3992 wpa_printf(MSG_DEBUG, "Using existing control "
3993 "interface directory.");
3994 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003995 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3996 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 goto fail;
3998 }
3999 }
4000
4001 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004002 lchown(hapd->conf->ctrl_interface, -1,
4003 hapd->conf->ctrl_interface_gid) < 0) {
4004 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004005 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 return -1;
4007 }
4008
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004009 if (!hapd->conf->ctrl_interface_gid_set &&
4010 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004011 lchown(hapd->conf->ctrl_interface, -1,
4012 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4013 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004014 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004015 return -1;
4016 }
4017
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004018#ifdef ANDROID
4019 /*
4020 * Android is using umask 0077 which would leave the control interface
4021 * directory without group access. This breaks things since Wi-Fi
4022 * framework assumes that this directory can be accessed by other
4023 * applications in the wifi group. Fix this by adding group access even
4024 * if umask value would prevent this.
4025 */
4026 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4027 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4028 strerror(errno));
4029 /* Try to continue anyway */
4030 }
4031#endif /* ANDROID */
4032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4034 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4035 goto fail;
4036
4037 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4038 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004039 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040 goto fail;
4041 }
4042
4043 os_memset(&addr, 0, sizeof(addr));
4044#ifdef __FreeBSD__
4045 addr.sun_len = sizeof(addr);
4046#endif /* __FreeBSD__ */
4047 addr.sun_family = AF_UNIX;
4048 fname = hostapd_ctrl_iface_path(hapd);
4049 if (fname == NULL)
4050 goto fail;
4051 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4052 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4053 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4054 strerror(errno));
4055 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4056 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4057 " allow connections - assuming it was left"
4058 "over from forced program termination");
4059 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004060 wpa_printf(MSG_ERROR,
4061 "Could not unlink existing ctrl_iface socket '%s': %s",
4062 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 goto fail;
4064 }
4065 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4066 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067 wpa_printf(MSG_ERROR,
4068 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4069 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 goto fail;
4071 }
4072 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4073 "ctrl_iface socket '%s'", fname);
4074 } else {
4075 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4076 "be in use - cannot override it");
4077 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4078 "not used anymore", fname);
4079 os_free(fname);
4080 fname = NULL;
4081 goto fail;
4082 }
4083 }
4084
4085 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004086 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4087 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004088 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089 goto fail;
4090 }
4091
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004092 if (!hapd->conf->ctrl_interface_gid_set &&
4093 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004094 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4095 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004097 goto fail;
4098 }
4099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004101 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4102 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 goto fail;
4104 }
4105 os_free(fname);
4106
4107 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004108 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4109 NULL) < 0) {
4110 hostapd_ctrl_iface_deinit(hapd);
4111 return -1;
4112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113 hapd->msg_ctx = hapd;
4114 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4115
4116 return 0;
4117
4118fail:
4119 if (s >= 0)
4120 close(s);
4121 if (fname) {
4122 unlink(fname);
4123 os_free(fname);
4124 }
4125 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004126#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004127}
4128
4129
4130void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4131{
4132 struct wpa_ctrl_dst *dst, *prev;
4133
4134 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004135#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004137#endif /* !CONFIG_CTRL_IFACE_UDP */
4138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139 eloop_unregister_read_sock(hapd->ctrl_sock);
4140 close(hapd->ctrl_sock);
4141 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004142#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 fname = hostapd_ctrl_iface_path(hapd);
4144 if (fname)
4145 unlink(fname);
4146 os_free(fname);
4147
4148 if (hapd->conf->ctrl_interface &&
4149 rmdir(hapd->conf->ctrl_interface) < 0) {
4150 if (errno == ENOTEMPTY) {
4151 wpa_printf(MSG_DEBUG, "Control interface "
4152 "directory not empty - leaving it "
4153 "behind");
4154 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004155 wpa_printf(MSG_ERROR,
4156 "rmdir[ctrl_interface=%s]: %s",
4157 hapd->conf->ctrl_interface,
4158 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159 }
4160 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004161#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 }
4163
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004164 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4165 list)
4166 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004167
4168#ifdef CONFIG_TESTING_OPTIONS
4169 l2_packet_deinit(hapd->l2_test);
4170 hapd->l2_test = NULL;
4171#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172}
4173
4174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004175static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4176 char *buf)
4177{
4178 if (hostapd_add_iface(interfaces, buf) < 0) {
4179 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4180 return -1;
4181 }
4182 return 0;
4183}
4184
4185
4186static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4187 char *buf)
4188{
4189 if (hostapd_remove_iface(interfaces, buf) < 0) {
4190 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4191 return -1;
4192 }
4193 return 0;
4194}
4195
4196
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004197static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004198 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004199 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004200{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004201 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4202 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004203}
4204
4205
4206static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004207 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004208 socklen_t fromlen)
4209{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004210 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004211}
4212
4213
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004214static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4215{
4216#ifdef CONFIG_WPS_TESTING
4217 wps_version_number = 0x20;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004218 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004219 wps_corrupt_pkhash = 0;
4220#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004221
4222#ifdef CONFIG_TESTING_OPTIONS
4223#ifdef CONFIG_DPP
4224 dpp_test = DPP_TEST_DISABLED;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004225#ifdef CONFIG_DPP3
4226 dpp_version_override = 3;
4227#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004228 dpp_version_override = 2;
4229#else /* CONFIG_DPP2 */
4230 dpp_version_override = 1;
4231#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004232#endif /* CONFIG_DPP */
4233#endif /* CONFIG_TESTING_OPTIONS */
4234
4235#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004236 dpp_global_clear(interfaces->dpp);
Sunil Ravi89eba102022-09-13 21:04:37 -07004237#ifdef CONFIG_DPP3
4238 {
4239 int i;
4240
4241 for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
4242 struct dpp_pb_info *info;
4243
4244 info = &interfaces->dpp_pb[i];
4245 info->rx_time.sec = 0;
4246 info->rx_time.usec = 0;
4247 }
4248 }
4249#endif /* CONFIG_DPP3 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004250#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004251}
4252
4253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004254#ifdef CONFIG_FST
4255
4256static int
4257hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4258 const char *cmd)
4259{
4260 char ifname[IFNAMSIZ + 1];
4261 struct fst_iface_cfg cfg;
4262 struct hostapd_data *hapd;
4263 struct fst_wpa_obj iface_obj;
4264
4265 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4266 hapd = hostapd_get_iface(interfaces, ifname);
4267 if (hapd) {
4268 if (hapd->iface->fst) {
4269 wpa_printf(MSG_INFO, "FST: Already attached");
4270 return -1;
4271 }
4272 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4273 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4274 &iface_obj, &cfg);
4275 if (hapd->iface->fst)
4276 return 0;
4277 }
4278 }
4279
4280 return -EINVAL;
4281}
4282
4283
4284static int
4285hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4286 const char *cmd)
4287{
4288 char ifname[IFNAMSIZ + 1];
4289 struct hostapd_data * hapd;
4290
4291 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4292 hapd = hostapd_get_iface(interfaces, ifname);
4293 if (hapd) {
4294 if (!fst_iface_detach(ifname)) {
4295 hapd->iface->fst = NULL;
4296 hapd->iface->fst_ies = NULL;
4297 return 0;
4298 }
4299 }
4300 }
4301
4302 return -EINVAL;
4303}
4304
4305#endif /* CONFIG_FST */
4306
4307
4308static struct hostapd_data *
4309hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4310 const char *ifname)
4311{
4312 size_t i, j;
4313
4314 for (i = 0; i < interfaces->count; i++) {
4315 struct hostapd_iface *iface = interfaces->iface[i];
4316
4317 for (j = 0; j < iface->num_bss; j++) {
4318 struct hostapd_data *hapd;
4319
4320 hapd = iface->bss[j];
4321 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4322 return hapd;
4323 }
4324 }
4325
4326 return NULL;
4327}
4328
4329
4330static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4331 struct hostapd_data *dst_hapd,
4332 const char *param)
4333{
4334 int res;
4335 char *value;
4336
4337 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4338 if (!value) {
4339 wpa_printf(MSG_ERROR,
4340 "DUP: cannot allocate buffer to stringify %s",
4341 param);
4342 goto error_return;
4343 }
4344
4345 if (os_strcmp(param, "wpa") == 0) {
4346 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4347 src_hapd->conf->wpa);
4348 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4349 src_hapd->conf->wpa_key_mgmt) {
4350 res = hostapd_ctrl_iface_get_key_mgmt(
4351 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4352 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4353 goto error_stringify;
4354 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4355 src_hapd->conf->wpa_pairwise) {
4356 res = wpa_write_ciphers(value,
4357 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4358 src_hapd->conf->wpa_pairwise, " ");
4359 if (res < 0)
4360 goto error_stringify;
4361 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4362 src_hapd->conf->rsn_pairwise) {
4363 res = wpa_write_ciphers(value,
4364 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4365 src_hapd->conf->rsn_pairwise, " ");
4366 if (res < 0)
4367 goto error_stringify;
4368 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4369 src_hapd->conf->ssid.wpa_passphrase) {
4370 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4371 src_hapd->conf->ssid.wpa_passphrase);
4372 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4373 src_hapd->conf->ssid.wpa_psk_set) {
4374 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4375 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4376 } else {
4377 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4378 goto error_return;
4379 }
4380
4381 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4382 os_free(value);
4383 return res;
4384
4385error_stringify:
4386 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4387error_return:
4388 os_free(value);
4389 return -1;
4390}
4391
4392
4393static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004394hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4395 const char *input,
4396 char *reply, int reply_size)
4397{
4398 size_t i, j;
4399 int res;
4400 char *pos, *end;
4401 struct hostapd_iface *iface;
4402 int show_ctrl = 0;
4403
4404 if (input)
4405 show_ctrl = !!os_strstr(input, "ctrl");
4406
4407 pos = reply;
4408 end = reply + reply_size;
4409
4410 for (i = 0; i < interfaces->count; i++) {
4411 iface = interfaces->iface[i];
4412
4413 for (j = 0; j < iface->num_bss; j++) {
4414 struct hostapd_bss_config *conf;
4415
4416 conf = iface->conf->bss[j];
4417 if (show_ctrl)
4418 res = os_snprintf(pos, end - pos,
4419 "%s ctrl_iface=%s\n",
4420 conf->iface,
4421 conf->ctrl_interface ?
4422 conf->ctrl_interface : "N/A");
4423 else
4424 res = os_snprintf(pos, end - pos, "%s\n",
4425 conf->iface);
4426 if (os_snprintf_error(end - pos, res)) {
4427 *pos = '\0';
4428 return pos - reply;
4429 }
4430 pos += res;
4431 }
4432 }
4433
4434 return pos - reply;
4435}
4436
4437
4438static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004439hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4440 char *cmd)
4441{
4442 char *p_start = cmd, *p_end;
4443 struct hostapd_data *src_hapd, *dst_hapd;
4444
4445 /* cmd: "<src ifname> <dst ifname> <variable name> */
4446
4447 p_end = os_strchr(p_start, ' ');
4448 if (!p_end) {
4449 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4450 cmd);
4451 return -1;
4452 }
4453
4454 *p_end = '\0';
4455 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4456 if (!src_hapd) {
4457 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4458 p_start);
4459 return -1;
4460 }
4461
4462 p_start = p_end + 1;
4463 p_end = os_strchr(p_start, ' ');
4464 if (!p_end) {
4465 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4466 cmd);
4467 return -1;
4468 }
4469
4470 *p_end = '\0';
4471 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4472 if (!dst_hapd) {
4473 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4474 p_start);
4475 return -1;
4476 }
4477
4478 p_start = p_end + 1;
4479 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4480}
4481
4482
4483static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4484 const char *ifname,
4485 char *buf, char *reply,
4486 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004487 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004488 socklen_t fromlen)
4489{
4490 struct hostapd_data *hapd;
4491
4492 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4493 if (hapd == NULL) {
4494 int res;
4495
4496 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4497 if (os_snprintf_error(reply_size, res))
4498 return -1;
4499 return res;
4500 }
4501
4502 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4503 from, fromlen);
4504}
4505
4506
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004507static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4508 void *sock_ctx)
4509{
Hai Shalomfdcde762020-04-02 11:19:20 -07004510 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004511 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004512 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004513 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004514 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004515 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004516 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004517 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004518#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004519 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004520#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004521
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004522 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004523 (struct sockaddr *) &from, &fromlen);
4524 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004525 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4526 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004527 return;
4528 }
4529 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004530 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004531
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004532 reply = os_malloc(reply_size);
4533 if (reply == NULL) {
4534 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4535 fromlen) < 0) {
4536 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4537 strerror(errno));
4538 }
4539 return;
4540 }
4541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004542 os_memcpy(reply, "OK\n", 3);
4543 reply_len = 3;
4544
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004545#ifdef CONFIG_CTRL_IFACE_UDP
4546 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4547 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004548 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4549 interfaces->ctrl_iface_cookie,
4550 CTRL_IFACE_COOKIE_LEN);
4551 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004552 goto send_reply;
4553 }
4554
4555 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004556 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004557 wpa_printf(MSG_DEBUG,
4558 "CTRL: No cookie in the request - drop request");
4559 os_free(reply);
4560 return;
4561 }
4562
Hai Shalomfdcde762020-04-02 11:19:20 -07004563 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4564 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004565 wpa_printf(MSG_DEBUG,
4566 "CTRL: Invalid cookie in the request - drop request");
4567 os_free(reply);
4568 return;
4569 }
4570
Hai Shalomfdcde762020-04-02 11:19:20 -07004571 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004572 while (*buf == ' ')
4573 buf++;
4574#endif /* CONFIG_CTRL_IFACE_UDP */
4575
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004576 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4577 char *pos = os_strchr(buf + 7, ' ');
4578
4579 if (pos) {
4580 *pos++ = '\0';
4581 reply_len = hostapd_global_ctrl_iface_ifname(
4582 interfaces, buf + 7, pos, reply, reply_size,
4583 &from, fromlen);
4584 goto send_reply;
4585 }
4586 }
4587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004588 if (os_strcmp(buf, "PING") == 0) {
4589 os_memcpy(reply, "PONG\n", 5);
4590 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004591 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4592 if (wpa_debug_reopen_file() < 0)
4593 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004594 } else if (os_strcmp(buf, "FLUSH") == 0) {
4595 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004596 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4597 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4598 reply_len = -1;
4599 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4600 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4601 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004602 } else if (os_strcmp(buf, "ATTACH") == 0) {
4603 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004604 fromlen, NULL))
4605 reply_len = -1;
4606 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4607 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4608 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004609 reply_len = -1;
4610 } else if (os_strcmp(buf, "DETACH") == 0) {
4611 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4612 fromlen))
4613 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004614#ifdef CONFIG_MODULE_TESTS
4615 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004616 if (hapd_module_tests() < 0)
4617 reply_len = -1;
4618#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004619#ifdef CONFIG_FST
4620 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4621 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4622 reply_len = os_snprintf(reply, reply_size, "OK\n");
4623 else
4624 reply_len = -1;
4625 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4626 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4627 reply_len = os_snprintf(reply, reply_size, "OK\n");
4628 else
4629 reply_len = -1;
4630 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4631 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4632#endif /* CONFIG_FST */
4633 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4634 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4635 buf + 12))
4636 reply_len = os_snprintf(reply, reply_size, "OK\n");
4637 else
4638 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004639 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4640 reply_len = hostapd_global_ctrl_iface_interfaces(
4641 interfaces, buf + 10, reply, sizeof(buffer));
4642 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4643 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004644 } else {
4645 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4646 "ignored");
4647 reply_len = -1;
4648 }
4649
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004650send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004651 if (reply_len < 0) {
4652 os_memcpy(reply, "FAIL\n", 5);
4653 reply_len = 5;
4654 }
4655
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004656 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4657 fromlen) < 0) {
4658 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4659 strerror(errno));
4660 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004661 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004662}
4663
4664
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004665#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004666static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4667{
4668 char *buf;
4669 size_t len;
4670
4671 if (interface->global_iface_path == NULL)
4672 return NULL;
4673
4674 len = os_strlen(interface->global_iface_path) +
4675 os_strlen(interface->global_iface_name) + 2;
4676 buf = os_malloc(len);
4677 if (buf == NULL)
4678 return NULL;
4679
4680 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4681 interface->global_iface_name);
4682 buf[len - 1] = '\0';
4683 return buf;
4684}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004685#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004686
4687
4688int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4689{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004690#ifdef CONFIG_CTRL_IFACE_UDP
4691 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4692 char p[32] = { 0 };
4693 char *pos;
4694 struct addrinfo hints = { 0 }, *res, *saveres;
4695 int n;
4696
4697 if (interface->global_ctrl_sock > -1) {
4698 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4699 return 0;
4700 }
4701
4702 if (interface->global_iface_path == NULL)
4703 return 0;
4704
4705 pos = os_strstr(interface->global_iface_path, "udp:");
4706 if (pos) {
4707 pos += 4;
4708 port = atoi(pos);
4709 if (port <= 0) {
4710 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4711 goto fail;
4712 }
4713 }
4714
Hai Shalomfdcde762020-04-02 11:19:20 -07004715 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004716
4717#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4718 hints.ai_flags = AI_PASSIVE;
4719#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4720
4721#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4722 hints.ai_family = AF_INET6;
4723#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4724 hints.ai_family = AF_INET;
4725#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4726 hints.ai_socktype = SOCK_DGRAM;
4727
4728try_again:
4729 os_snprintf(p, sizeof(p), "%d", port);
4730 n = getaddrinfo(NULL, p, &hints, &res);
4731 if (n) {
4732 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4733 goto fail;
4734 }
4735
4736 saveres = res;
4737 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4738 res->ai_protocol);
4739 if (interface->global_ctrl_sock < 0) {
4740 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4741 goto fail;
4742 }
4743
4744 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4745 0) {
4746 port++;
4747 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4748 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4749 goto try_again;
4750 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4751 goto fail;
4752 }
4753
4754 freeaddrinfo(saveres);
4755
4756 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4757
4758 if (eloop_register_read_sock(interface->global_ctrl_sock,
4759 hostapd_global_ctrl_iface_receive,
4760 interface, NULL) < 0) {
4761 hostapd_global_ctrl_iface_deinit(interface);
4762 return -1;
4763 }
4764
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004765 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4766
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004767 return 0;
4768
4769fail:
4770 if (interface->global_ctrl_sock >= 0)
4771 close(interface->global_ctrl_sock);
4772 return -1;
4773#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004774 struct sockaddr_un addr;
4775 int s = -1;
4776 char *fname = NULL;
4777
4778 if (interface->global_iface_path == NULL) {
4779 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4780 return 0;
4781 }
4782
4783 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4784 if (errno == EEXIST) {
4785 wpa_printf(MSG_DEBUG, "Using existing control "
4786 "interface directory.");
4787 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004788 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4789 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004790 goto fail;
4791 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004792 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004793 lchown(interface->global_iface_path, -1,
4794 interface->ctrl_iface_group) < 0) {
4795 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004796 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004797 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004798 }
4799
4800 if (os_strlen(interface->global_iface_path) + 1 +
4801 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4802 goto fail;
4803
4804 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4805 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004806 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004807 goto fail;
4808 }
4809
4810 os_memset(&addr, 0, sizeof(addr));
4811#ifdef __FreeBSD__
4812 addr.sun_len = sizeof(addr);
4813#endif /* __FreeBSD__ */
4814 addr.sun_family = AF_UNIX;
4815 fname = hostapd_global_ctrl_iface_path(interface);
4816 if (fname == NULL)
4817 goto fail;
4818 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4819 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4820 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4821 strerror(errno));
4822 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4823 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4824 " allow connections - assuming it was left"
4825 "over from forced program termination");
4826 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004827 wpa_printf(MSG_ERROR,
4828 "Could not unlink existing ctrl_iface socket '%s': %s",
4829 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004830 goto fail;
4831 }
4832 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4833 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004834 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4835 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004836 goto fail;
4837 }
4838 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4839 "ctrl_iface socket '%s'", fname);
4840 } else {
4841 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4842 "be in use - cannot override it");
4843 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4844 "not used anymore", fname);
4845 os_free(fname);
4846 fname = NULL;
4847 goto fail;
4848 }
4849 }
4850
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004851 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004852 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4853 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004854 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004855 goto fail;
4856 }
4857
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004858 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004859 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4860 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004861 goto fail;
4862 }
4863 os_free(fname);
4864
4865 interface->global_ctrl_sock = s;
4866 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4867 interface, NULL);
4868
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004869 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4870
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004871 return 0;
4872
4873fail:
4874 if (s >= 0)
4875 close(s);
4876 if (fname) {
4877 unlink(fname);
4878 os_free(fname);
4879 }
4880 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004881#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004882}
4883
4884
4885void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4886{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004887#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004888 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004889#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004890 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004891
4892 if (interfaces->global_ctrl_sock > -1) {
4893 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4894 close(interfaces->global_ctrl_sock);
4895 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004896#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004897 fname = hostapd_global_ctrl_iface_path(interfaces);
4898 if (fname) {
4899 unlink(fname);
4900 os_free(fname);
4901 }
4902
4903 if (interfaces->global_iface_path &&
4904 rmdir(interfaces->global_iface_path) < 0) {
4905 if (errno == ENOTEMPTY) {
4906 wpa_printf(MSG_DEBUG, "Control interface "
4907 "directory not empty - leaving it "
4908 "behind");
4909 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004910 wpa_printf(MSG_ERROR,
4911 "rmdir[ctrl_interface=%s]: %s",
4912 interfaces->global_iface_path,
4913 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004914 }
4915 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004916#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004917 }
4918
4919 os_free(interfaces->global_iface_path);
4920 interfaces->global_iface_path = NULL;
4921
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004922 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4923 struct wpa_ctrl_dst, list)
4924 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004925}
4926
4927
Roshan Pius3a1667e2018-07-03 15:17:14 -07004928static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4929 const char *buf)
4930{
4931 /* Enable Probe Request events based on explicit request.
4932 * Other events are enabled by default.
4933 */
4934 if (str_starts(buf, RX_PROBE_REQUEST))
4935 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4936 return 1;
4937}
4938
4939
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004940static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4941 const char *ifname, int level,
4942 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943{
4944 struct wpa_ctrl_dst *dst, *next;
4945 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004946 int idx, res;
4947 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 char levelstr[10];
4949
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004950 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 return;
4952
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004953 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4954 if (os_snprintf_error(sizeof(levelstr), res))
4955 return;
4956 idx = 0;
4957 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004958 io[idx].iov_base = "IFNAME=";
4959 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004960 idx++;
4961 io[idx].iov_base = (char *) ifname;
4962 io[idx].iov_len = os_strlen(ifname);
4963 idx++;
4964 io[idx].iov_base = " ";
4965 io[idx].iov_len = 1;
4966 idx++;
4967 }
4968 io[idx].iov_base = levelstr;
4969 io[idx].iov_len = os_strlen(levelstr);
4970 idx++;
4971 io[idx].iov_base = (char *) buf;
4972 io[idx].iov_len = len;
4973 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974 os_memset(&msg, 0, sizeof(msg));
4975 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004976 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977
4978 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004979 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004980 if ((level >= dst->debug_level) &&
4981 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004982 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4983 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984 msg.msg_name = &dst->addr;
4985 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004986 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 int _errno = errno;
4988 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4989 "%d - %s",
4990 idx, errno, strerror(errno));
4991 dst->errors++;
4992 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004993 ctrl_iface_detach(ctrl_dst,
4994 &dst->addr,
4995 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 }
4997 } else
4998 dst->errors = 0;
4999 }
5000 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 }
5002}
5003
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005004
5005static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5006 enum wpa_msg_type type,
5007 const char *buf, size_t len)
5008{
5009 if (type != WPA_MSG_NO_GLOBAL) {
5010 hostapd_ctrl_iface_send_internal(
5011 hapd->iface->interfaces->global_ctrl_sock,
5012 &hapd->iface->interfaces->global_ctrl_dst,
5013 type != WPA_MSG_PER_INTERFACE ?
5014 NULL : hapd->conf->iface,
5015 level, buf, len);
5016 }
5017
5018 if (type != WPA_MSG_ONLY_GLOBAL) {
5019 hostapd_ctrl_iface_send_internal(
5020 hapd->ctrl_sock, &hapd->ctrl_dst,
5021 NULL, level, buf, len);
5022 }
5023}
5024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025#endif /* CONFIG_NATIVE_WINDOWS */