blob: a62f3c7a0f29e662c8ca222553a41f4104bcb3f5 [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
775static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
776 const char *cmd)
777{
778 u8 addr[ETH_ALEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800779 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800780 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800781
782 if (hwaddr_aton(cmd, addr))
783 return -1;
784 if (cmd[17] != ' ')
785 return -1;
786 disassoc_timer = atoi(cmd + 17);
787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800788 sta = ap_get_sta(hapd, addr);
789 if (sta == NULL) {
790 wpa_printf(MSG_DEBUG, "Station " MACSTR
791 " not found for disassociation imminent message",
792 MAC2STR(addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800793 return -1;
794 }
795
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800796 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800797}
798
799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
801 const char *cmd)
802{
803 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700804 const char *url, *timerstr;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700805 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800806 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807
808 if (hwaddr_aton(cmd, addr))
809 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700810
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800811 sta = ap_get_sta(hapd, addr);
812 if (sta == NULL) {
813 wpa_printf(MSG_DEBUG, "Station " MACSTR
814 " not found for ESS disassociation imminent message",
815 MAC2STR(addr));
816 return -1;
817 }
818
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700819 timerstr = cmd + 17;
820 if (*timerstr != ' ')
821 return -1;
822 timerstr++;
823 disassoc_timer = atoi(timerstr);
824 if (disassoc_timer < 0 || disassoc_timer > 65535)
825 return -1;
826
827 url = os_strchr(timerstr, ' ');
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700828 if (url == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800829 return -1;
830 url++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800832 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800833}
834
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800835
836static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
837 const char *cmd)
838{
839 u8 addr[ETH_ALEN];
840 const char *pos, *end;
841 int disassoc_timer = 0;
842 struct sta_info *sta;
Hai Shalomc1a21442022-02-04 13:43:00 -0800843 u8 req_mode = 0, valid_int = 0x01, dialog_token = 0x01;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844 u8 bss_term_dur[12];
845 char *url = NULL;
846 int ret;
847 u8 nei_rep[1000];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700848 int nei_len;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800849 u8 mbo[10];
850 size_t mbo_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851
852 if (hwaddr_aton(cmd, addr)) {
853 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
854 return -1;
855 }
856
857 sta = ap_get_sta(hapd, addr);
858 if (sta == NULL) {
859 wpa_printf(MSG_DEBUG, "Station " MACSTR
860 " not found for BSS TM Request message",
861 MAC2STR(addr));
862 return -1;
863 }
864
865 pos = os_strstr(cmd, " disassoc_timer=");
866 if (pos) {
867 pos += 16;
868 disassoc_timer = atoi(pos);
869 if (disassoc_timer < 0 || disassoc_timer > 65535) {
870 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
871 return -1;
872 }
873 }
874
875 pos = os_strstr(cmd, " valid_int=");
876 if (pos) {
877 pos += 11;
878 valid_int = atoi(pos);
879 }
880
Hai Shalomc1a21442022-02-04 13:43:00 -0800881 pos = os_strstr(cmd, " dialog_token=");
882 if (pos) {
883 pos += 14;
884 dialog_token = atoi(pos);
885 }
886
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800887 pos = os_strstr(cmd, " bss_term=");
888 if (pos) {
889 pos += 10;
890 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
891 /* TODO: TSF configurable/learnable */
892 bss_term_dur[0] = 4; /* Subelement ID */
893 bss_term_dur[1] = 10; /* Length */
Hai Shalom5f92bc92019-04-18 11:54:11 -0700894 os_memset(&bss_term_dur[2], 0, 8);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800895 end = os_strchr(pos, ',');
896 if (end == NULL) {
897 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
898 return -1;
899 }
900 end++;
901 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
902 }
903
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700904 nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
905 sizeof(nei_rep));
906 if (nei_len < 0)
907 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800908
909 pos = os_strstr(cmd, " url=");
910 if (pos) {
911 size_t len;
912 pos += 5;
913 end = os_strchr(pos, ' ');
914 if (end)
915 len = end - pos;
916 else
917 len = os_strlen(pos);
918 url = os_malloc(len + 1);
919 if (url == NULL)
920 return -1;
921 os_memcpy(url, pos, len);
922 url[len] = '\0';
923 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
924 }
925
926 if (os_strstr(cmd, " pref=1"))
927 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
928 if (os_strstr(cmd, " abridged=1"))
929 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
930 if (os_strstr(cmd, " disassoc_imminent=1"))
931 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
932
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800933#ifdef CONFIG_MBO
934 pos = os_strstr(cmd, "mbo=");
935 if (pos) {
936 unsigned int mbo_reason, cell_pref, reassoc_delay;
937 u8 *mbo_pos = mbo;
938
939 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
940 &reassoc_delay, &cell_pref);
941 if (ret != 3) {
942 wpa_printf(MSG_DEBUG,
943 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800944 ret = -1;
945 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800946 }
947
948 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
949 wpa_printf(MSG_DEBUG,
950 "Invalid MBO transition reason code %u",
951 mbo_reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800952 ret = -1;
953 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800954 }
955
956 /* Valid values for Cellular preference are: 0, 1, 255 */
957 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
958 wpa_printf(MSG_DEBUG,
959 "Invalid MBO cellular capability %u",
960 cell_pref);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800961 ret = -1;
962 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800963 }
964
965 if (reassoc_delay > 65535 ||
966 (reassoc_delay &&
967 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
968 wpa_printf(MSG_DEBUG,
969 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800970 ret = -1;
971 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800972 }
973
974 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
975 *mbo_pos++ = 1;
976 *mbo_pos++ = mbo_reason;
977 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
978 *mbo_pos++ = 1;
979 *mbo_pos++ = cell_pref;
980
981 if (reassoc_delay) {
982 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
983 *mbo_pos++ = 2;
984 WPA_PUT_LE16(mbo_pos, reassoc_delay);
985 mbo_pos += 2;
986 }
987
988 mbo_len = mbo_pos - mbo;
989 }
990#endif /* CONFIG_MBO */
991
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800992 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
Hai Shalomc1a21442022-02-04 13:43:00 -0800993 valid_int, bss_term_dur, dialog_token, url,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700994 nei_len ? nei_rep : NULL, nei_len,
995 mbo_len ? mbo : NULL, mbo_len);
Paul Stewart092955c2017-02-06 09:13:09 -0800996#ifdef CONFIG_MBO
Dmitry Shmidt29333592017-01-09 12:27:11 -0800997fail:
Paul Stewart092955c2017-02-06 09:13:09 -0800998#endif /* CONFIG_MBO */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800999 os_free(url);
1000 return ret;
1001}
1002
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001003
1004static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
1005 const char *cmd)
1006{
1007 u8 addr[ETH_ALEN];
1008 struct sta_info *sta;
1009 const char *pos;
1010 unsigned int auto_report, timeout;
1011
1012 if (hwaddr_aton(cmd, addr)) {
1013 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1014 return -1;
1015 }
1016
1017 sta = ap_get_sta(hapd, addr);
1018 if (!sta) {
1019 wpa_printf(MSG_DEBUG, "Station " MACSTR
1020 " not found for Collocated Interference Request",
1021 MAC2STR(addr));
1022 return -1;
1023 }
1024
1025 pos = cmd + 17;
1026 if (*pos != ' ')
1027 return -1;
1028 pos++;
1029 auto_report = atoi(pos);
1030 pos = os_strchr(pos, ' ');
1031 if (!pos)
1032 return -1;
1033 pos++;
1034 timeout = atoi(pos);
1035
1036 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
1037}
1038
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001039#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001041
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001042static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1043 char *buf, size_t buflen)
1044{
1045 int ret = 0;
1046 char *pos, *end;
1047
1048 pos = buf;
1049 end = buf + buflen;
1050
1051 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1052
1053 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1054 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1055 if (os_snprintf_error(end - pos, ret))
1056 return pos - buf;
1057 pos += ret;
1058 }
1059 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1060 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1061 if (os_snprintf_error(end - pos, ret))
1062 return pos - buf;
1063 pos += ret;
1064 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001065#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001066 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1067 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1068 if (os_snprintf_error(end - pos, ret))
1069 return pos - buf;
1070 pos += ret;
1071 }
1072 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1073 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1074 if (os_snprintf_error(end - pos, ret))
1075 return pos - buf;
1076 pos += ret;
1077 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001078#ifdef CONFIG_SHA384
1079 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1080 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
1081 if (os_snprintf_error(end - pos, ret))
1082 return pos - buf;
1083 pos += ret;
1084 }
1085#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001086#ifdef CONFIG_SAE
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1088 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1089 if (os_snprintf_error(end - pos, ret))
1090 return pos - buf;
1091 pos += ret;
1092 }
1093#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001094#ifdef CONFIG_FILS
1095 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1096 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1097 if (os_snprintf_error(end - pos, ret))
1098 return pos - buf;
1099 pos += ret;
1100 }
1101 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1102 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1103 if (os_snprintf_error(end - pos, ret))
1104 return pos - buf;
1105 pos += ret;
1106 }
1107#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001108#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001109 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1110 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1111 if (os_snprintf_error(end - pos, ret))
1112 return pos - buf;
1113 pos += ret;
1114 }
1115 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1116 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1117 if (os_snprintf_error(end - pos, ret))
1118 return pos - buf;
1119 pos += ret;
1120 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001121#ifdef CONFIG_SAE
1122 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1123 ret = os_snprintf(pos, end - pos, "SAE ");
1124 if (os_snprintf_error(end - pos, ret))
1125 return pos - buf;
1126 pos += ret;
1127 }
1128#endif /* CONFIG_SAE */
1129 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1130 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1131 if (os_snprintf_error(end - pos, ret))
1132 return pos - buf;
1133 pos += ret;
1134 }
1135 if (hapd->conf->wpa_key_mgmt &
1136 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1137 ret = os_snprintf(pos, end - pos,
1138 "WPA-EAP-SUITE-B-192 ");
1139 if (os_snprintf_error(end - pos, ret))
1140 return pos - buf;
1141 pos += ret;
1142 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001143#ifdef CONFIG_FILS
1144 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1145 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1146 if (os_snprintf_error(end - pos, ret))
1147 return pos - buf;
1148 pos += ret;
1149 }
1150 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1151 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1152 if (os_snprintf_error(end - pos, ret))
1153 return pos - buf;
1154 pos += ret;
1155 }
1156#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001157
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001158#ifdef CONFIG_OWE
1159 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
1160 ret = os_snprintf(pos, end - pos, "OWE ");
1161 if (os_snprintf_error(end - pos, ret))
1162 return pos - buf;
1163 pos += ret;
1164 }
1165#endif /* CONFIG_OWE */
1166
1167#ifdef CONFIG_DPP
1168 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
1169 ret = os_snprintf(pos, end - pos, "DPP ");
1170 if (os_snprintf_error(end - pos, ret))
1171 return pos - buf;
1172 pos += ret;
1173 }
1174#endif /* CONFIG_DPP */
1175
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001176 if (pos > buf && *(pos - 1) == ' ') {
1177 *(pos - 1) = '\0';
1178 pos--;
1179 }
1180
1181 return pos - buf;
1182}
1183
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1186 char *buf, size_t buflen)
1187{
1188 int ret;
1189 char *pos, *end;
1190
1191 pos = buf;
1192 end = buf + buflen;
1193
1194 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1195 "ssid=%s\n",
1196 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001197 wpa_ssid_txt(hapd->conf->ssid.ssid,
1198 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001199 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 return pos - buf;
1201 pos += ret;
1202
1203#ifdef CONFIG_WPS
1204 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1205 hapd->conf->wps_state == 0 ? "disabled" :
1206 (hapd->conf->wps_state == 1 ? "not configured" :
1207 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001208 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 return pos - buf;
1210 pos += ret;
1211
1212 if (hapd->conf->wps_state && hapd->conf->wpa &&
1213 hapd->conf->ssid.wpa_passphrase) {
1214 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1215 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001216 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 return pos - buf;
1218 pos += ret;
1219 }
1220
1221 if (hapd->conf->wps_state && hapd->conf->wpa &&
1222 hapd->conf->ssid.wpa_psk &&
1223 hapd->conf->ssid.wpa_psk->group) {
1224 char hex[PMK_LEN * 2 + 1];
1225 wpa_snprintf_hex(hex, sizeof(hex),
1226 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1227 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001228 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 return pos - buf;
1230 pos += ret;
1231 }
Hai Shalom60840252021-02-19 19:02:11 -08001232
1233 if (hapd->conf->multi_ap) {
1234 struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1235
1236 ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1237 hapd->conf->multi_ap);
1238 if (os_snprintf_error(end - pos, ret))
1239 return pos - buf;
1240 pos += ret;
1241
1242 if (ssid->ssid_len) {
1243 ret = os_snprintf(pos, end - pos,
1244 "multi_ap_backhaul_ssid=%s\n",
1245 wpa_ssid_txt(ssid->ssid,
1246 ssid->ssid_len));
1247 if (os_snprintf_error(end - pos, ret))
1248 return pos - buf;
1249 pos += ret;
1250 }
1251
1252 if (hapd->conf->wps_state && hapd->conf->wpa &&
1253 ssid->wpa_passphrase) {
1254 ret = os_snprintf(pos, end - pos,
1255 "multi_ap_backhaul_wpa_passphrase=%s\n",
1256 ssid->wpa_passphrase);
1257 if (os_snprintf_error(end - pos, ret))
1258 return pos - buf;
1259 pos += ret;
1260 }
1261
1262 if (hapd->conf->wps_state && hapd->conf->wpa &&
1263 ssid->wpa_psk &&
1264 ssid->wpa_psk->group) {
1265 char hex[PMK_LEN * 2 + 1];
1266
1267 wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1268 PMK_LEN);
1269 ret = os_snprintf(pos, end - pos,
1270 "multi_ap_backhaul_wpa_psk=%s\n",
1271 hex);
1272 forced_memzero(hex, sizeof(hex));
1273 if (os_snprintf_error(end - pos, ret))
1274 return pos - buf;
1275 pos += ret;
1276 }
1277 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278#endif /* CONFIG_WPS */
1279
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001280 if (hapd->conf->wpa) {
1281 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1282 if (os_snprintf_error(end - pos, ret))
1283 return pos - buf;
1284 pos += ret;
1285 }
1286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1288 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001289 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 return pos - buf;
1291 pos += ret;
1292
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001293 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294
1295 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001296 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 return pos - buf;
1298 pos += ret;
1299 }
1300
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001301 if (hapd->conf->wpa) {
1302 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1303 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001304 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 return pos - buf;
1306 pos += ret;
1307 }
1308
1309 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1310 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001311 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 return pos - buf;
1313 pos += ret;
1314
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001315 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1316 " ");
1317 if (ret < 0)
1318 return pos - buf;
1319 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320
1321 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001322 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 return pos - buf;
1324 pos += ret;
1325 }
1326
1327 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1328 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001329 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 return pos - buf;
1331 pos += ret;
1332
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001333 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001334 " ");
1335 if (ret < 0)
1336 return pos - buf;
1337 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338
1339 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001340 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 return pos - buf;
1342 pos += ret;
1343 }
1344
Hai Shalomfdcde762020-04-02 11:19:20 -07001345 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1346 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1347 hapd->conf->wpa_deny_ptk0_rekey);
1348 if (os_snprintf_error(end - pos, ret))
1349 return pos - buf;
1350 pos += ret;
1351 }
1352
1353 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1354 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1355 hapd->conf->extended_key_id);
1356 if (os_snprintf_error(end - pos, ret))
1357 return pos - buf;
1358 pos += ret;
1359 }
1360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 return pos - buf;
1362}
1363
1364
Roshan Pius3a1667e2018-07-03 15:17:14 -07001365static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1366{
1367 struct sta_info *sta;
1368 struct vlan_description vlan_id;
1369
1370 if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1371 return;
1372
1373 for (sta = hapd->sta_list; sta; sta = sta->next) {
1374 if (!hostapd_maclist_found(hapd->conf->accept_mac,
1375 hapd->conf->num_accept_mac,
1376 sta->addr, &vlan_id) ||
1377 (vlan_id.notempty &&
1378 vlan_compare(&vlan_id, sta->vlan_desc)))
1379 ap_sta_disconnect(hapd, sta, sta->addr,
1380 WLAN_REASON_UNSPECIFIED);
1381 }
1382}
1383
1384
1385static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1386{
1387 struct sta_info *sta;
1388 struct vlan_description vlan_id;
1389
1390 for (sta = hapd->sta_list; sta; sta = sta->next) {
1391 if (hostapd_maclist_found(hapd->conf->deny_mac,
1392 hapd->conf->num_deny_mac, sta->addr,
1393 &vlan_id) &&
1394 (!vlan_id.notempty ||
1395 !vlan_compare(&vlan_id, sta->vlan_desc)))
1396 ap_sta_disconnect(hapd, sta, sta->addr,
1397 WLAN_REASON_UNSPECIFIED);
1398 }
1399}
1400
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001401
1402static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
Hai Shalom60840252021-02-19 19:02:11 -08001403 const char *bands)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001404{
1405 union wpa_event_data event;
Hai Shalom60840252021-02-19 19:02:11 -08001406 u32 setband_mask = WPA_SETBAND_AUTO;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001407
Hai Shalom60840252021-02-19 19:02:11 -08001408 /*
1409 * For example:
1410 * SET setband 2G,6G
1411 * SET setband 5G
1412 * SET setband AUTO
1413 */
1414 if (!os_strstr(bands, "AUTO")) {
1415 if (os_strstr(bands, "5G"))
1416 setband_mask |= WPA_SETBAND_5G;
1417 if (os_strstr(bands, "6G"))
1418 setband_mask |= WPA_SETBAND_6G;
1419 if (os_strstr(bands, "2G"))
1420 setband_mask |= WPA_SETBAND_2G;
1421 if (setband_mask == WPA_SETBAND_AUTO)
1422 return -1;
1423 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001424
Hai Shalom60840252021-02-19 19:02:11 -08001425 if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001426 os_memset(&event, 0, sizeof(event));
1427 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1428 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1429 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1430 }
1431
1432 return 0;
1433}
1434
1435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1437{
1438 char *value;
1439 int ret = 0;
1440
1441 value = os_strchr(cmd, ' ');
1442 if (value == NULL)
1443 return -1;
1444 *value++ = '\0';
1445
1446 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1447 if (0) {
1448#ifdef CONFIG_WPS_TESTING
1449 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1450 long int val;
1451 val = strtol(value, NULL, 0);
1452 if (val < 0 || val > 0xff) {
1453 ret = -1;
1454 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1455 "wps_version_number %ld", val);
1456 } else {
1457 wps_version_number = val;
1458 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1459 "version %u.%u",
1460 (wps_version_number & 0xf0) >> 4,
1461 wps_version_number & 0x0f);
1462 hostapd_wps_update_ie(hapd);
1463 }
Hai Shalomc1a21442022-02-04 13:43:00 -08001464 } else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
1465 wps_testing_stub_cred = atoi(value);
1466 wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
1467 wps_testing_stub_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001468 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1469 wps_corrupt_pkhash = atoi(value);
1470 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1471 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001473#ifdef CONFIG_TESTING_OPTIONS
1474 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1475 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001476 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1477 hapd->ext_eapol_frame_io = atoi(value);
Hai Shalomc1a21442022-02-04 13:43:00 -08001478 } else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1479 hapd->force_backlog_bytes = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001480#ifdef CONFIG_DPP
1481 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1482 os_free(hapd->dpp_config_obj_override);
1483 hapd->dpp_config_obj_override = os_strdup(value);
1484 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1485 os_free(hapd->dpp_discovery_override);
1486 hapd->dpp_discovery_override = os_strdup(value);
1487 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1488 os_free(hapd->dpp_groups_override);
1489 hapd->dpp_groups_override = os_strdup(value);
1490 } else if (os_strcasecmp(cmd,
1491 "dpp_ignore_netaccesskey_mismatch") == 0) {
1492 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001493 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1494 dpp_test = atoi(value);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001495 } else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1496 dpp_version_override = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001497#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001499#ifdef CONFIG_MBO
1500 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1501 int val;
1502
1503 if (!hapd->conf->mbo_enabled)
1504 return -1;
1505
1506 val = atoi(value);
Hai Shalomc1a21442022-02-04 13:43:00 -08001507 if (val < 0 || val > MBO_ASSOC_DISALLOW_REASON_LOW_RSSI)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001508 return -1;
1509
1510 hapd->mbo_assoc_disallow = val;
1511 ieee802_11_update_beacons(hapd->iface);
1512
1513 /*
1514 * TODO: Need to configure drivers that do AP MLME offload with
1515 * disallowing station logic.
1516 */
1517#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001518#ifdef CONFIG_DPP
1519 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1520 os_free(hapd->dpp_configurator_params);
1521 hapd->dpp_configurator_params = os_strdup(value);
Hai Shalomc1a21442022-02-04 13:43:00 -08001522 } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1523 hapd->dpp_init_max_tries = atoi(value);
1524 } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1525 hapd->dpp_init_retry_time = atoi(value);
1526 } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1527 hapd->dpp_resp_wait_time = atoi(value);
1528 } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1529 hapd->dpp_resp_max_tries = atoi(value);
1530 } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1531 hapd->dpp_resp_retry_time = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001532#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001533 } else if (os_strcasecmp(cmd, "setband") == 0) {
1534 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001536 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001537 if (ret)
1538 return ret;
1539
1540 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001541 hostapd_disassoc_deny_mac(hapd);
1542 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1543 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001544 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1545 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1546 hapd->parameter_set_count++;
1547 if (ieee802_11_update_beacons(hapd->iface))
1548 wpa_printf(MSG_DEBUG,
1549 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001550 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1551 os_strcmp(cmd, "sae_password") == 0 ||
1552 os_strcmp(cmd, "sae_pwe") == 0) {
1553 if (hapd->started)
1554 hostapd_setup_sae_pt(hapd->conf);
Hai Shalom899fcc72020-10-19 14:38:18 -07001555 } else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1556 wpa_auth_set_transition_disable(hapd->wpa_auth,
1557 hapd->conf->transition_disable);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001558 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001559
1560#ifdef CONFIG_TESTING_OPTIONS
1561 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1562 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1563 hapd->conf->ft_rsnxe_used);
Hai Shalom899fcc72020-10-19 14:38:18 -07001564 else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1565 wpa_auth_set_ocv_override_freq(
1566 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1567 atoi(value));
1568 else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1569 wpa_auth_set_ocv_override_freq(
1570 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1571 atoi(value));
1572 else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1573 wpa_auth_set_ocv_override_freq(
1574 hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1575 atoi(value));
1576 else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1577 wpa_auth_set_ocv_override_freq(
1578 hapd->wpa_auth,
1579 WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
Kai Shie75b0652020-11-24 20:31:29 -08001580 else if (os_strcasecmp(cmd, "skip_send_eapol") == 0)
1581 wpa_auth_set_skip_send_eapol(hapd->wpa_auth, atoi(value));
1582 else if (os_strcasecmp(cmd, "enable_eapol_large_timeout") == 0)
1583 wpa_auth_set_enable_eapol_large_timeout(hapd->wpa_auth, atoi(value));
Hai Shalomb755a2a2020-04-23 21:49:02 -07001584#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 }
1586
1587 return ret;
1588}
1589
1590
1591static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1592 char *buf, size_t buflen)
1593{
1594 int res;
1595
1596 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1597
1598 if (os_strcmp(cmd, "version") == 0) {
1599 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001600 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 return -1;
1602 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001603 } else if (os_strcmp(cmd, "tls_library") == 0) {
1604 res = tls_get_library_version(buf, buflen);
1605 if (os_snprintf_error(buflen, res))
1606 return -1;
1607 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 }
1609
1610 return -1;
1611}
1612
1613
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001614static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1615{
1616 if (hostapd_enable_iface(iface) < 0) {
1617 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1618 return -1;
1619 }
1620 return 0;
1621}
1622
1623
1624static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1625{
1626 if (hostapd_reload_iface(iface) < 0) {
1627 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1628 return -1;
1629 }
1630 return 0;
1631}
1632
1633
1634static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1635{
1636 if (hostapd_disable_iface(iface) < 0) {
1637 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1638 return -1;
1639 }
1640 return 0;
1641}
1642
1643
Hai Shalom74f70d42019-02-11 14:42:39 -08001644static int
1645hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1646 struct sta_info *sta, void *ctx)
1647{
1648 struct hostapd_wpa_psk *psk;
1649 const u8 *pmk;
1650 int pmk_len;
1651 int pmk_match;
1652 int sta_match;
1653 int bss_match;
1654 int reason;
1655
1656 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1657
1658 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1659 pmk_match = PMK_LEN == pmk_len &&
1660 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1661 sta_match = psk->group == 0 &&
1662 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1663 bss_match = psk->group == 1;
1664
1665 if (pmk_match && (sta_match || bss_match))
1666 return 0;
1667 }
1668
1669 wpa_printf(MSG_INFO, "STA " MACSTR
1670 " PSK/passphrase no longer valid - disconnect",
1671 MAC2STR(sta->addr));
1672 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1673 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1674 ap_sta_deauthenticate(hapd, sta, reason);
1675
1676 return 0;
1677}
1678
1679
1680static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1681{
1682 struct hostapd_bss_config *conf = hapd->conf;
1683 int err;
1684
1685 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1686
1687 err = hostapd_setup_wpa_psk(conf);
1688 if (err < 0) {
1689 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1690 err);
1691 return -1;
1692 }
1693
1694 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1695 NULL);
1696
1697 return 0;
1698}
1699
1700
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001701#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001702
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001703static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1704{
1705 union wpa_event_data data;
1706 char *pos, *param;
1707 enum wpa_event_type event;
1708
1709 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1710
1711 os_memset(&data, 0, sizeof(data));
1712
1713 param = os_strchr(cmd, ' ');
1714 if (param == NULL)
1715 return -1;
1716 *param++ = '\0';
1717
1718 if (os_strcmp(cmd, "DETECTED") == 0)
1719 event = EVENT_DFS_RADAR_DETECTED;
1720 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1721 event = EVENT_DFS_CAC_FINISHED;
1722 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1723 event = EVENT_DFS_CAC_ABORTED;
1724 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1725 event = EVENT_DFS_NOP_FINISHED;
1726 else {
1727 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1728 cmd);
1729 return -1;
1730 }
1731
1732 pos = os_strstr(param, "freq=");
1733 if (pos)
1734 data.dfs_event.freq = atoi(pos + 5);
1735
1736 pos = os_strstr(param, "ht_enabled=1");
1737 if (pos)
1738 data.dfs_event.ht_enabled = 1;
1739
1740 pos = os_strstr(param, "chan_offset=");
1741 if (pos)
1742 data.dfs_event.chan_offset = atoi(pos + 12);
1743
1744 pos = os_strstr(param, "chan_width=");
1745 if (pos)
1746 data.dfs_event.chan_width = atoi(pos + 11);
1747
1748 pos = os_strstr(param, "cf1=");
1749 if (pos)
1750 data.dfs_event.cf1 = atoi(pos + 4);
1751
1752 pos = os_strstr(param, "cf2=");
1753 if (pos)
1754 data.dfs_event.cf2 = atoi(pos + 4);
1755
1756 wpa_supplicant_event(hapd, event, &data);
1757
1758 return 0;
1759}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001760
1761
1762static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1763{
1764 size_t len;
1765 u8 *buf;
1766 int res;
1767
1768 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1769
1770 len = os_strlen(cmd);
1771 if (len & 1)
1772 return -1;
1773 len /= 2;
1774
1775 buf = os_malloc(len);
1776 if (buf == NULL)
1777 return -1;
1778
1779 if (hexstr2bin(cmd, buf, len) < 0) {
1780 os_free(buf);
1781 return -1;
1782 }
1783
Hai Shalomfdcde762020-04-02 11:19:20 -07001784 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001785 os_free(buf);
1786 return res;
1787}
1788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001790static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1791 char *cmd)
1792{
1793 char *pos, *param;
1794 size_t len;
1795 u8 *buf;
1796 int stype = 0, ok = 0;
1797 union wpa_event_data event;
1798
1799 if (!hapd->ext_mgmt_frame_handling)
1800 return -1;
1801
1802 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1803
1804 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1805
1806 pos = cmd;
1807 param = os_strstr(pos, "stype=");
1808 if (param) {
1809 param += 6;
1810 stype = atoi(param);
1811 }
1812
1813 param = os_strstr(pos, " ok=");
1814 if (param) {
1815 param += 4;
1816 ok = atoi(param);
1817 }
1818
1819 param = os_strstr(pos, " buf=");
1820 if (!param)
1821 return -1;
1822 param += 5;
1823
1824 len = os_strlen(param);
1825 if (len & 1)
1826 return -1;
1827 len /= 2;
1828
1829 buf = os_malloc(len);
1830 if (!buf || hexstr2bin(param, buf, len) < 0) {
1831 os_free(buf);
1832 return -1;
1833 }
1834
1835 os_memset(&event, 0, sizeof(event));
1836 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1837 event.tx_status.data = buf;
1838 event.tx_status.data_len = len;
1839 event.tx_status.stype = stype;
1840 event.tx_status.ack = ok;
1841 hapd->ext_mgmt_frame_handling = 0;
1842 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1843 hapd->ext_mgmt_frame_handling = 1;
1844
1845 os_free(buf);
1846
1847 return 0;
1848}
1849
1850
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001851static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1852 char *cmd)
1853{
1854 char *pos, *param;
1855 size_t len;
1856 u8 *buf;
1857 int freq = 0, datarate = 0, ssi_signal = 0;
1858 union wpa_event_data event;
1859
1860 if (!hapd->ext_mgmt_frame_handling)
1861 return -1;
1862
1863 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1864
1865 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1866
1867 pos = cmd;
1868 param = os_strstr(pos, "freq=");
1869 if (param) {
1870 param += 5;
1871 freq = atoi(param);
1872 }
1873
1874 param = os_strstr(pos, " datarate=");
1875 if (param) {
1876 param += 10;
1877 datarate = atoi(param);
1878 }
1879
1880 param = os_strstr(pos, " ssi_signal=");
1881 if (param) {
1882 param += 12;
1883 ssi_signal = atoi(param);
1884 }
1885
1886 param = os_strstr(pos, " frame=");
1887 if (param == NULL)
1888 return -1;
1889 param += 7;
1890
1891 len = os_strlen(param);
1892 if (len & 1)
1893 return -1;
1894 len /= 2;
1895
1896 buf = os_malloc(len);
1897 if (buf == NULL)
1898 return -1;
1899
1900 if (hexstr2bin(param, buf, len) < 0) {
1901 os_free(buf);
1902 return -1;
1903 }
1904
1905 os_memset(&event, 0, sizeof(event));
1906 event.rx_mgmt.freq = freq;
1907 event.rx_mgmt.frame = buf;
1908 event.rx_mgmt.frame_len = len;
1909 event.rx_mgmt.ssi_signal = ssi_signal;
1910 event.rx_mgmt.datarate = datarate;
1911 hapd->ext_mgmt_frame_handling = 0;
1912 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1913 hapd->ext_mgmt_frame_handling = 1;
1914
1915 os_free(buf);
1916
1917 return 0;
1918}
1919
1920
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001921static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1922{
1923 char *pos;
1924 u8 src[ETH_ALEN], *buf;
1925 int used;
1926 size_t len;
1927
1928 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1929
1930 pos = cmd;
1931 used = hwaddr_aton2(pos, src);
1932 if (used < 0)
1933 return -1;
1934 pos += used;
1935 while (*pos == ' ')
1936 pos++;
1937
1938 len = os_strlen(pos);
1939 if (len & 1)
1940 return -1;
1941 len /= 2;
1942
1943 buf = os_malloc(len);
1944 if (buf == NULL)
1945 return -1;
1946
1947 if (hexstr2bin(pos, buf, len) < 0) {
1948 os_free(buf);
1949 return -1;
1950 }
1951
1952 ieee802_1x_receive(hapd, src, buf, len);
1953 os_free(buf);
1954
1955 return 0;
1956}
1957
1958
Hai Shalomc1a21442022-02-04 13:43:00 -08001959static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1960{
1961 char *pos, *pos2;
1962 u8 dst[ETH_ALEN], *buf;
1963 int used, ret;
1964 size_t len;
1965 unsigned int prev;
1966 int encrypt = 0;
1967
1968 wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1969
1970 pos = cmd;
1971 used = hwaddr_aton2(pos, dst);
1972 if (used < 0)
1973 return -1;
1974 pos += used;
1975 while (*pos == ' ')
1976 pos++;
1977
1978 pos2 = os_strchr(pos, ' ');
1979 if (pos2) {
1980 len = pos2 - pos;
1981 encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1982 } else {
1983 len = os_strlen(pos);
1984 }
1985 if (len & 1)
1986 return -1;
1987 len /= 2;
1988
1989 buf = os_malloc(len);
1990 if (!buf || hexstr2bin(pos, buf, len) < 0) {
1991 os_free(buf);
1992 return -1;
1993 }
1994
1995 prev = hapd->ext_eapol_frame_io;
1996 hapd->ext_eapol_frame_io = 0;
1997 ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1998 hapd->ext_eapol_frame_io = prev;
1999 os_free(buf);
2000
2001 return ret;
2002}
2003
2004
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002005static u16 ipv4_hdr_checksum(const void *buf, size_t len)
2006{
2007 size_t i;
2008 u32 sum = 0;
2009 const u16 *pos = buf;
2010
2011 for (i = 0; i < len / 2; i++)
2012 sum += *pos++;
2013
2014 while (sum >> 16)
2015 sum = (sum & 0xffff) + (sum >> 16);
2016
2017 return sum ^ 0xffff;
2018}
2019
2020
2021#define HWSIM_PACKETLEN 1500
2022#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
2023
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002024static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
2025 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002026{
2027 struct hostapd_data *hapd = ctx;
2028 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07002029 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002030 const u8 *pos;
2031 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07002032 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002033
Hai Shalom81f62d82019-07-22 12:10:00 -07002034 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
2035 wpa_printf(MSG_DEBUG,
2036 "test data: RX - ignore unexpected length %d",
2037 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002038 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07002039 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002040
2041 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002042 os_memcpy(&ip, eth + 1, sizeof(ip));
2043 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002044
Hai Shalomfdcde762020-04-02 11:19:20 -07002045 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
2046 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002047 wpa_printf(MSG_DEBUG,
Hai Shalom899fcc72020-10-19 14:38:18 -07002048 "test data: RX - ignore unexpected IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002049 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07002050 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002051
Hai Shalomfdcde762020-04-02 11:19:20 -07002052 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002053 if (*pos != (u8) i) {
2054 wpa_printf(MSG_DEBUG,
2055 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002056 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07002057 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002058 pos++;
2059 }
2060
Hai Shalom81f62d82019-07-22 12:10:00 -07002061 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07002062 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
2063 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07002064 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
2065 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002066}
2067
2068
2069static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
2070 char *cmd)
2071{
2072 int enabled = atoi(cmd);
2073 char *pos;
2074 const char *ifname;
2075
2076 if (!enabled) {
2077 if (hapd->l2_test) {
2078 l2_packet_deinit(hapd->l2_test);
2079 hapd->l2_test = NULL;
2080 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
2081 "test data: Disabled");
2082 }
2083 return 0;
2084 }
2085
2086 if (hapd->l2_test)
2087 return 0;
2088
2089 pos = os_strstr(cmd, " ifname=");
2090 if (pos)
2091 ifname = pos + 8;
2092 else
2093 ifname = hapd->conf->iface;
2094
2095 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
2096 ETHERTYPE_IP, hostapd_data_test_rx,
2097 hapd, 1);
2098 if (hapd->l2_test == NULL)
2099 return -1;
2100
2101 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
2102
2103 return 0;
2104}
2105
2106
2107static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
2108{
2109 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07002110 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002111 int used;
2112 long int val;
2113 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002114 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002115 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07002116 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002117 u8 *dpos;
2118 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07002119 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002120
2121 if (hapd->l2_test == NULL)
2122 return -1;
2123
Hai Shalom81f62d82019-07-22 12:10:00 -07002124 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002125
2126 pos = cmd;
2127 used = hwaddr_aton2(pos, dst);
2128 if (used < 0)
2129 return -1;
2130 pos += used;
2131 while (*pos == ' ')
2132 pos++;
2133 used = hwaddr_aton2(pos, src);
2134 if (used < 0)
2135 return -1;
2136 pos += used;
2137
Hai Shalom81f62d82019-07-22 12:10:00 -07002138 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002139 if (val < 0 || val > 0xff)
2140 return -1;
2141 tos = val;
2142
Hai Shalom81f62d82019-07-22 12:10:00 -07002143 pos = os_strstr(pos2, " len=");
2144 if (pos) {
2145 i = atoi(pos + 5);
2146 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2147 return -1;
2148 send_len = i;
2149 }
2150
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002151 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002152 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2153 os_memcpy(eth->ether_shost, src, ETH_ALEN);
2154 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07002155 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002156 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07002157 ip->ip_hl = 5;
2158 ip->ip_v = 4;
2159 ip->ip_ttl = 64;
2160 ip->ip_tos = tos;
2161 ip->ip_len = htons(send_len);
2162 ip->ip_p = 1;
2163 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2164 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2165 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002166 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07002167 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002168 *dpos++ = i;
2169
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002170 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07002171 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002172 return -1;
2173
2174 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2175 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2176
2177 return 0;
2178}
2179
2180
2181static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2182 char *cmd)
2183{
2184 u8 *buf;
2185 struct ether_header *eth;
2186 struct l2_packet_data *l2 = NULL;
2187 size_t len;
2188 u16 ethertype;
2189 int res = -1;
2190 const char *ifname = hapd->conf->iface;
2191
2192 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2193 cmd += 7;
2194 ifname = cmd;
2195 cmd = os_strchr(cmd, ' ');
2196 if (cmd == NULL)
2197 return -1;
2198 *cmd++ = '\0';
2199 }
2200
2201 len = os_strlen(cmd);
2202 if (len & 1 || len < ETH_HLEN * 2)
2203 return -1;
2204 len /= 2;
2205
2206 buf = os_malloc(len);
2207 if (buf == NULL)
2208 return -1;
2209
2210 if (hexstr2bin(cmd, buf, len) < 0)
2211 goto done;
2212
2213 eth = (struct ether_header *) buf;
2214 ethertype = ntohs(eth->ether_type);
2215
2216 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2217 hostapd_data_test_rx, hapd, 1);
2218 if (l2 == NULL)
2219 goto done;
2220
2221 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2222 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2223done:
2224 if (l2)
2225 l2_packet_deinit(l2);
2226 os_free(buf);
2227
2228 return res < 0 ? -1 : 0;
2229}
2230
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002231
2232static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2233{
2234#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002235 char *pos;
2236
2237 wpa_trace_fail_after = atoi(cmd);
2238 pos = os_strchr(cmd, ':');
2239 if (pos) {
2240 pos++;
2241 os_strlcpy(wpa_trace_fail_func, pos,
2242 sizeof(wpa_trace_fail_func));
2243 } else {
2244 wpa_trace_fail_after = 0;
2245 }
2246
2247 return 0;
2248#else /* WPA_TRACE_BFD */
2249 return -1;
2250#endif /* WPA_TRACE_BFD */
2251}
2252
2253
2254static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2255 char *buf, size_t buflen)
2256{
2257#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002258 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2259 wpa_trace_fail_func);
2260#else /* WPA_TRACE_BFD */
2261 return -1;
2262#endif /* WPA_TRACE_BFD */
2263}
2264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002265
2266static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2267{
2268#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002269 char *pos;
2270
2271 wpa_trace_test_fail_after = atoi(cmd);
2272 pos = os_strchr(cmd, ':');
2273 if (pos) {
2274 pos++;
2275 os_strlcpy(wpa_trace_test_fail_func, pos,
2276 sizeof(wpa_trace_test_fail_func));
2277 } else {
2278 wpa_trace_test_fail_after = 0;
2279 }
2280
2281 return 0;
2282#else /* WPA_TRACE_BFD */
2283 return -1;
2284#endif /* WPA_TRACE_BFD */
2285}
2286
2287
2288static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2289 char *buf, size_t buflen)
2290{
2291#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002292 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2293 wpa_trace_test_fail_func);
2294#else /* WPA_TRACE_BFD */
2295 return -1;
2296#endif /* WPA_TRACE_BFD */
2297}
2298
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002299
2300static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2301{
2302 struct sta_info *sta;
2303 u8 addr[ETH_ALEN];
2304 u8 zero[WPA_TK_MAX_LEN];
2305
2306 os_memset(zero, 0, sizeof(zero));
2307
2308 if (hwaddr_aton(cmd, addr))
2309 return -1;
2310
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002311 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2312 if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2313 return -1;
2314
2315 wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2316
2317 /* First, use a zero key to avoid any possible duplicate key
2318 * avoidance in the driver. */
2319 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2320 hapd->last_bigtk_alg,
2321 broadcast_ether_addr,
2322 hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2323 zero, hapd->last_bigtk_len,
2324 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2325 return -1;
2326
2327 /* Set the previously configured key to reset its TSC */
2328 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2329 hapd->last_bigtk_alg,
2330 broadcast_ether_addr,
2331 hapd->last_bigtk_key_idx, 0, 1, NULL,
2332 0, hapd->last_bigtk,
2333 hapd->last_bigtk_len,
2334 KEY_FLAG_GROUP_TX_DEFAULT);
2335 }
2336
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002337 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2338 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2339 return -1;
2340
2341 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2342
2343 /* First, use a zero key to avoid any possible duplicate key
2344 * avoidance in the driver. */
2345 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2346 hapd->last_igtk_alg,
2347 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002348 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2349 zero, hapd->last_igtk_len,
2350 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002351 return -1;
2352
2353 /* Set the previously configured key to reset its TSC */
2354 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2355 hapd->last_igtk_alg,
2356 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002357 hapd->last_igtk_key_idx, 0, 1, NULL,
2358 0, hapd->last_igtk,
2359 hapd->last_igtk_len,
2360 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002361 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002362
2363 if (is_broadcast_ether_addr(addr)) {
2364 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2365 return -1;
2366
2367 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2368
2369 /* First, use a zero key to avoid any possible duplicate key
2370 * avoidance in the driver. */
2371 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2372 hapd->last_gtk_alg,
2373 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002374 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2375 zero, hapd->last_gtk_len,
2376 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002377 return -1;
2378
2379 /* Set the previously configured key to reset its TSC */
2380 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2381 hapd->last_gtk_alg,
2382 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002383 hapd->last_gtk_key_idx, 0, 1, NULL,
2384 0, hapd->last_gtk,
2385 hapd->last_gtk_len,
2386 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002387 }
2388
2389 sta = ap_get_sta(hapd, addr);
2390 if (!sta)
2391 return -1;
2392
2393 if (sta->last_tk_alg == WPA_ALG_NONE)
2394 return -1;
2395
2396 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2397 MAC2STR(sta->addr));
2398
2399 /* First, use a zero key to avoid any possible duplicate key avoidance
2400 * in the driver. */
2401 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002402 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2403 zero, sta->last_tk_len,
2404 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002405 return -1;
2406
2407 /* Set the previously configured key to reset its TSC/RSC */
2408 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002409 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2410 0, sta->last_tk, sta->last_tk_len,
2411 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002412}
2413
2414
2415static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2416{
2417 u8 addr[ETH_ALEN];
2418 const char *pos = cmd;
2419 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002420 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002421 int idx, set_tx;
2422 u8 seq[6], key[WPA_TK_MAX_LEN];
2423 size_t key_len;
2424
Hai Shalomfdcde762020-04-02 11:19:20 -07002425 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002426
2427 alg = atoi(pos);
2428 pos = os_strchr(pos, ' ');
2429 if (!pos)
2430 return -1;
2431 pos++;
2432 if (hwaddr_aton(pos, addr))
2433 return -1;
2434 pos += 17;
2435 if (*pos != ' ')
2436 return -1;
2437 pos++;
2438 idx = atoi(pos);
2439 pos = os_strchr(pos, ' ');
2440 if (!pos)
2441 return -1;
2442 pos++;
2443 set_tx = atoi(pos);
2444 pos = os_strchr(pos, ' ');
2445 if (!pos)
2446 return -1;
2447 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002448 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002449 return -1;
2450 pos += 2 * 6;
2451 if (*pos != ' ')
2452 return -1;
2453 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002454 if (!os_strchr(pos, ' '))
2455 return -1;
2456 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002457 if (hexstr2bin(pos, key, key_len) < 0)
2458 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002459 pos += 2 * key_len;
2460 if (*pos != ' ')
2461 return -1;
2462
2463 pos++;
2464 key_flag = atoi(pos);
2465 pos = os_strchr(pos, ' ');
2466 if (pos)
2467 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002468
2469 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002470 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2471 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002472}
2473
2474
2475static void restore_tk(void *ctx1, void *ctx2)
2476{
2477 struct hostapd_data *hapd = ctx1;
2478 struct sta_info *sta = ctx2;
2479
2480 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2481 MAC2STR(sta->addr));
2482 /* This does not really restore the TSC properly, so this will result
2483 * in replay protection issues for now since there is no clean way of
2484 * preventing encryption of a single EAPOL frame. */
2485 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002486 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2487 sta->last_tk, sta->last_tk_len,
2488 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002489}
2490
2491
2492static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2493{
2494 struct sta_info *sta;
2495 u8 addr[ETH_ALEN];
2496 int plain = os_strstr(cmd, "plaintext") != NULL;
2497
2498 if (hwaddr_aton(cmd, addr))
2499 return -1;
2500
2501 sta = ap_get_sta(hapd, addr);
2502 if (!sta || !sta->wpa_sm)
2503 return -1;
2504
2505 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2506 plain = 0; /* no need for special processing */
2507 if (plain) {
2508 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2509 MAC2STR(sta->addr));
2510 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002511 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2512 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002513 }
2514
2515 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2516 return wpa_auth_resend_m1(sta->wpa_sm,
2517 os_strstr(cmd, "change-anonce") != NULL,
2518 plain ? restore_tk : NULL, hapd, sta);
2519}
2520
2521
2522static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2523{
2524 struct sta_info *sta;
2525 u8 addr[ETH_ALEN];
2526 int plain = os_strstr(cmd, "plaintext") != NULL;
2527
2528 if (hwaddr_aton(cmd, addr))
2529 return -1;
2530
2531 sta = ap_get_sta(hapd, addr);
2532 if (!sta || !sta->wpa_sm)
2533 return -1;
2534
2535 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2536 plain = 0; /* no need for special processing */
2537 if (plain) {
2538 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2539 MAC2STR(sta->addr));
2540 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002541 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2542 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002543 }
2544
2545 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2546 return wpa_auth_resend_m3(sta->wpa_sm,
2547 plain ? restore_tk : NULL, hapd, sta);
2548}
2549
2550
2551static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2552 const char *cmd)
2553{
2554 struct sta_info *sta;
2555 u8 addr[ETH_ALEN];
2556 int plain = os_strstr(cmd, "plaintext") != NULL;
2557
2558 if (hwaddr_aton(cmd, addr))
2559 return -1;
2560
2561 sta = ap_get_sta(hapd, addr);
2562 if (!sta || !sta->wpa_sm)
2563 return -1;
2564
2565 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2566 plain = 0; /* no need for special processing */
2567 if (plain) {
2568 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2569 MAC2STR(sta->addr));
2570 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002571 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2572 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002573 }
2574
2575 wpa_printf(MSG_INFO,
2576 "TESTING: Send group M1 for the same GTK and zero RSC to "
2577 MACSTR, MAC2STR(sta->addr));
2578 return wpa_auth_resend_group_m1(sta->wpa_sm,
2579 plain ? restore_tk : NULL, hapd, sta);
2580}
2581
Hai Shalomfdcde762020-04-02 11:19:20 -07002582
Hai Shalomc1a21442022-02-04 13:43:00 -08002583static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2584{
2585 struct sta_info *sta;
2586 u8 addr[ETH_ALEN];
2587
2588 if (hwaddr_aton(cmd, addr))
2589 return -1;
2590
2591 sta = ap_get_sta(hapd, addr);
2592 if (!sta || !sta->wpa_sm)
2593 return -1;
2594
2595 return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2596}
2597
2598
Hai Shalom899fcc72020-10-19 14:38:18 -07002599static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2600 char *buf, size_t buflen)
2601{
2602 struct rsn_pmksa_cache_entry *pmksa;
2603
2604 pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2605 if (!pmksa)
2606 return -1;
2607
2608 return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2609}
2610
2611
Hai Shalomfdcde762020-04-02 11:19:20 -07002612static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2613 char *buf, size_t buflen)
2614{
2615 struct sta_info *sta;
2616 u8 addr[ETH_ALEN];
2617 const u8 *pmk;
2618 int pmk_len;
2619
2620 if (hwaddr_aton(cmd, addr))
2621 return -1;
2622
2623 sta = ap_get_sta(hapd, addr);
2624 if (!sta || !sta->wpa_sm) {
2625 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2626 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002627 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002628 }
2629 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
Hai Shalom899fcc72020-10-19 14:38:18 -07002630 if (!pmk || !pmk_len) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002631 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2632 MAC2STR(addr));
Hai Shalom899fcc72020-10-19 14:38:18 -07002633 return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
Hai Shalomfdcde762020-04-02 11:19:20 -07002634 }
2635
2636 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2637}
2638
Hai Shalomc1a21442022-02-04 13:43:00 -08002639
2640static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2641 const char *cmd)
2642{
2643 u16 type;
2644 char *pos, *end;
2645 u8 match[10];
2646 size_t match_len;
2647 bool multicast = false;
2648
2649 type = strtol(cmd, &pos, 16);
2650 if (*pos != ' ')
2651 return -1;
2652 pos++;
2653 end = os_strchr(pos, ' ');
2654 if (end) {
2655 match_len = end - pos;
2656 multicast = os_strstr(end, "multicast") != NULL;
2657 } else {
2658 match_len = os_strlen(pos) / 2;
2659 }
2660 if (hexstr2bin(pos, match, match_len))
2661 return -1;
2662
2663 return hostapd_drv_register_frame(hapd, type, match, match_len,
2664 multicast);
2665}
2666
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002667#endif /* CONFIG_TESTING_OPTIONS */
2668
2669
Hai Shalomb755a2a2020-04-23 21:49:02 -07002670#ifdef NEED_AP_MLME
Hai Shalomfdcde762020-04-02 11:19:20 -07002671static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2672{
2673 switch (params->bandwidth) {
2674 case 0:
2675 /* bandwidth not specified: use 20 MHz by default */
2676 /* fall-through */
2677 case 20:
2678 if (params->center_freq1 &&
2679 params->center_freq1 != params->freq)
2680 return -1;
2681
2682 if (params->center_freq2 || params->sec_channel_offset)
2683 return -1;
2684 break;
2685 case 40:
2686 if (params->center_freq2 || !params->sec_channel_offset)
2687 return -1;
2688
2689 if (!params->center_freq1)
2690 break;
2691 switch (params->sec_channel_offset) {
2692 case 1:
2693 if (params->freq + 10 != params->center_freq1)
2694 return -1;
2695 break;
2696 case -1:
2697 if (params->freq - 10 != params->center_freq1)
2698 return -1;
2699 break;
2700 default:
2701 return -1;
2702 }
2703 break;
2704 case 80:
2705 if (!params->center_freq1 || !params->sec_channel_offset)
2706 return 1;
2707
2708 switch (params->sec_channel_offset) {
2709 case 1:
2710 if (params->freq - 10 != params->center_freq1 &&
2711 params->freq + 30 != params->center_freq1)
2712 return 1;
2713 break;
2714 case -1:
2715 if (params->freq + 10 != params->center_freq1 &&
2716 params->freq - 30 != params->center_freq1)
2717 return -1;
2718 break;
2719 default:
2720 return -1;
2721 }
2722
2723 /* Adjacent and overlapped are not allowed for 80+80 */
2724 if (params->center_freq2 &&
2725 params->center_freq1 - params->center_freq2 <= 80 &&
2726 params->center_freq2 - params->center_freq1 <= 80)
2727 return 1;
2728 break;
2729 case 160:
2730 if (!params->center_freq1 || params->center_freq2 ||
2731 !params->sec_channel_offset)
2732 return -1;
2733
2734 switch (params->sec_channel_offset) {
2735 case 1:
2736 if (params->freq + 70 != params->center_freq1 &&
2737 params->freq + 30 != params->center_freq1 &&
2738 params->freq - 10 != params->center_freq1 &&
2739 params->freq - 50 != params->center_freq1)
2740 return -1;
2741 break;
2742 case -1:
2743 if (params->freq + 50 != params->center_freq1 &&
2744 params->freq + 10 != params->center_freq1 &&
2745 params->freq - 30 != params->center_freq1 &&
2746 params->freq - 70 != params->center_freq1)
2747 return -1;
2748 break;
2749 default:
2750 return -1;
2751 }
2752 break;
2753 default:
2754 return -1;
2755 }
2756
2757 return 0;
2758}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002759#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002760
2761
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002762static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2763 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002764{
2765#ifdef NEED_AP_MLME
2766 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002767 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002768 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002769 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002770 int bandwidth;
2771 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002772
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002773 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002774 if (ret)
2775 return ret;
2776
Hai Shalomfdcde762020-04-02 11:19:20 -07002777 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2778 if (ret) {
2779 wpa_printf(MSG_INFO,
2780 "chanswitch: invalid frequency settings provided");
2781 return ret;
2782 }
2783
2784 switch (settings.freq_params.bandwidth) {
2785 case 40:
2786 bandwidth = CHAN_WIDTH_40;
2787 break;
2788 case 80:
2789 if (settings.freq_params.center_freq2)
2790 bandwidth = CHAN_WIDTH_80P80;
2791 else
2792 bandwidth = CHAN_WIDTH_80;
2793 break;
2794 case 160:
2795 bandwidth = CHAN_WIDTH_160;
2796 break;
2797 default:
2798 bandwidth = CHAN_WIDTH_20;
2799 break;
2800 }
2801
2802 if (settings.freq_params.center_freq1)
2803 dfs_range += hostapd_is_dfs_overlap(
2804 iface, bandwidth, settings.freq_params.center_freq1);
2805 else
2806 dfs_range += hostapd_is_dfs_overlap(
2807 iface, bandwidth, settings.freq_params.freq);
2808
2809 if (settings.freq_params.center_freq2)
2810 dfs_range += hostapd_is_dfs_overlap(
2811 iface, bandwidth, settings.freq_params.center_freq2);
2812
2813 if (dfs_range) {
2814 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2815 if (ret == NUM_HOSTAPD_MODES) {
2816 wpa_printf(MSG_ERROR,
2817 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2818 settings.freq_params.freq,
2819 settings.freq_params.sec_channel_offset,
2820 settings.freq_params.bandwidth);
2821 return -1;
2822 }
2823
2824 settings.freq_params.channel = chan;
2825
2826 wpa_printf(MSG_DEBUG,
2827 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2828 settings.freq_params.channel,
2829 settings.freq_params.freq,
2830 settings.freq_params.sec_channel_offset,
2831 settings.freq_params.bandwidth,
2832 settings.freq_params.center_freq1);
2833
2834 /* Perform CAC and switch channel */
2835 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2836 return 0;
2837 }
2838
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002839 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002840
Hai Shalom60840252021-02-19 19:02:11 -08002841 /* Save CHAN_SWITCH VHT and HE config */
2842 hostapd_chan_switch_config(iface->bss[i],
2843 &settings.freq_params);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002844
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002845 ret = hostapd_switch_channel(iface->bss[i], &settings);
2846 if (ret) {
2847 /* FIX: What do we do if CSA fails in the middle of
2848 * submitting multi-BSS CSA requests? */
2849 return ret;
2850 }
2851 }
2852
2853 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002854#else /* NEED_AP_MLME */
2855 return -1;
2856#endif /* NEED_AP_MLME */
2857}
2858
2859
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002860static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2861 int reply_size, const char *param)
2862{
2863#ifdef RADIUS_SERVER
2864 if (os_strcmp(param, "radius_server") == 0) {
2865 return radius_server_get_mib(hapd->radius_srv, reply,
2866 reply_size);
2867 }
2868#endif /* RADIUS_SERVER */
2869 return -1;
2870}
2871
2872
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002873static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2874 char *buf, size_t buflen)
2875{
2876 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08002877 char *pos, *temp = NULL;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002878 u8 *data = NULL;
2879 unsigned int vendor_id, subcmd;
Hai Shalom60840252021-02-19 19:02:11 -08002880 enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002881 struct wpabuf *reply;
2882 size_t data_len = 0;
2883
Hai Shalom60840252021-02-19 19:02:11 -08002884 /**
2885 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2886 * [nested=<0|1>]
2887 */
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002888 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002889 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002890 return -EINVAL;
2891
2892 subcmd = strtoul(pos, &pos, 10);
2893
2894 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002895 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002896 return -EINVAL;
Hai Shalom60840252021-02-19 19:02:11 -08002897
2898 temp = os_strchr(pos, ' ');
2899 data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002900 }
2901
2902 if (data_len) {
2903 data_len /= 2;
2904 data = os_malloc(data_len);
2905 if (!data)
2906 return -ENOBUFS;
2907
2908 if (hexstr2bin(pos, data, data_len)) {
2909 wpa_printf(MSG_DEBUG,
2910 "Vendor command: wrong parameter format");
2911 os_free(data);
2912 return -EINVAL;
2913 }
2914 }
2915
Hai Shalom60840252021-02-19 19:02:11 -08002916 pos = os_strstr(cmd, "nested=");
2917 if (pos)
2918 nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2919 NESTED_ATTR_NOT_USED;
2920
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002921 reply = wpabuf_alloc((buflen - 1) / 2);
2922 if (!reply) {
2923 os_free(data);
2924 return -ENOBUFS;
2925 }
2926
2927 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
Hai Shalom60840252021-02-19 19:02:11 -08002928 nested_attr_flag, reply);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002929
2930 if (ret == 0)
2931 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2932 wpabuf_len(reply));
2933
2934 wpabuf_free(reply);
2935 os_free(data);
2936
2937 return ret;
2938}
2939
2940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002941static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2942 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002944 u8 addr[ETH_ALEN];
2945 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002947 if (hwaddr_aton(cmd, addr))
2948 return -1;
2949
2950 sta = ap_get_sta(hapd, addr);
2951 if (!sta || !sta->eapol_sm)
2952 return -1;
2953
2954 eapol_auth_reauthenticate(sta->eapol_sm);
2955 return 0;
2956}
2957
2958
2959static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2960{
2961 u8 addr[ETH_ALEN];
2962 struct sta_info *sta;
2963 char *pos = cmd, *param;
2964
2965 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2966 return -1;
2967 pos += 18;
2968 param = pos;
2969 pos = os_strchr(pos, ' ');
2970 if (!pos)
2971 return -1;
2972 *pos++ = '\0';
2973
2974 sta = ap_get_sta(hapd, addr);
2975 if (!sta || !sta->eapol_sm)
2976 return -1;
2977
2978 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2979}
2980
2981
2982static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2983 char *buf, size_t buflen)
2984{
2985 char *pos, *end, *stamp;
2986 int ret;
2987
2988 /* cmd: "LOG_LEVEL [<level>]" */
2989 if (*cmd == '\0') {
2990 pos = buf;
2991 end = buf + buflen;
2992 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2993 "Timestamp: %d\n",
2994 debug_level_str(wpa_debug_level),
2995 wpa_debug_timestamp);
2996 if (os_snprintf_error(end - pos, ret))
2997 ret = 0;
2998
2999 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003002 while (*cmd == ' ')
3003 cmd++;
3004
3005 stamp = os_strchr(cmd, ' ');
3006 if (stamp) {
3007 *stamp++ = '\0';
3008 while (*stamp == ' ') {
3009 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 }
3012
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003013 if (os_strlen(cmd)) {
3014 int level = str_to_debug_level(cmd);
3015 if (level < 0)
3016 return -1;
3017 wpa_debug_level = level;
3018 }
3019
3020 if (stamp && os_strlen(stamp))
3021 wpa_debug_timestamp = atoi(stamp);
3022
3023 os_memcpy(buf, "OK\n", 3);
3024 return 3;
3025}
3026
3027
3028#ifdef NEED_AP_MLME
3029static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3030 char *buf, size_t buflen)
3031{
3032 struct hostapd_iface *iface = hapd->iface;
3033 char *pos, *end;
3034 struct hostapd_sta_info *info;
3035 struct os_reltime now;
3036
Dmitry Shmidt7d175302016-09-06 13:11:34 -07003037 if (!iface->num_sta_seen)
3038 return 0;
3039
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003040 sta_track_expire(iface, 0);
3041
3042 pos = buf;
3043 end = buf + buflen;
3044
3045 os_get_reltime(&now);
3046 dl_list_for_each_reverse(info, &iface->sta_seen,
3047 struct hostapd_sta_info, list) {
3048 struct os_reltime age;
3049 int ret;
3050
3051 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003052 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3053 MAC2STR(info->addr), (unsigned int) age.sec,
3054 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003055 if (os_snprintf_error(end - pos, ret))
3056 break;
3057 pos += ret;
3058 }
3059
3060 return pos - buf;
3061}
3062#endif /* NEED_AP_MLME */
3063
3064
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003065static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3066 const char *cmd)
3067{
3068 u8 addr[ETH_ALEN];
3069
3070 if (hwaddr_aton(cmd, addr)) {
3071 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3072 return -1;
3073 }
3074
3075 return hostapd_send_lci_req(hapd, addr);
3076}
3077
3078
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003079static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003080{
3081 u8 addr[ETH_ALEN];
3082 char *token, *context = NULL;
3083 int random_interval, min_ap;
3084 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3085 unsigned int n_responders;
3086
3087 token = str_token(cmd, " ", &context);
3088 if (!token || hwaddr_aton(token, addr)) {
3089 wpa_printf(MSG_INFO,
3090 "CTRL: REQ_RANGE - Bad destination address");
3091 return -1;
3092 }
3093
3094 token = str_token(cmd, " ", &context);
3095 if (!token)
3096 return -1;
3097
3098 random_interval = atoi(token);
3099 if (random_interval < 0 || random_interval > 0xffff)
3100 return -1;
3101
3102 token = str_token(cmd, " ", &context);
3103 if (!token)
3104 return -1;
3105
3106 min_ap = atoi(token);
3107 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3108 return -1;
3109
3110 n_responders = 0;
3111 while ((token = str_token(cmd, " ", &context))) {
3112 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3113 wpa_printf(MSG_INFO,
3114 "CTRL: REQ_RANGE: Too many responders");
3115 return -1;
3116 }
3117
3118 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3119 wpa_printf(MSG_INFO,
3120 "CTRL: REQ_RANGE: Bad responder address");
3121 return -1;
3122 }
3123
3124 n_responders++;
3125 }
3126
3127 if (!n_responders) {
3128 wpa_printf(MSG_INFO,
3129 "CTRL: REQ_RANGE - No FTM responder address");
3130 return -1;
3131 }
3132
3133 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3134 responders, n_responders);
3135}
3136
3137
Dmitry Shmidt29333592017-01-09 12:27:11 -08003138static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3139 const char *cmd, char *reply,
3140 size_t reply_size)
3141{
3142 u8 addr[ETH_ALEN];
3143 const char *pos;
3144 struct wpabuf *req;
3145 int ret;
3146 u8 req_mode = 0;
3147
3148 if (hwaddr_aton(cmd, addr))
3149 return -1;
3150 pos = os_strchr(cmd, ' ');
3151 if (!pos)
3152 return -1;
3153 pos++;
3154 if (os_strncmp(pos, "req_mode=", 9) == 0) {
3155 int val = hex2byte(pos + 9);
3156
3157 if (val < 0)
3158 return -1;
3159 req_mode = val;
3160 pos += 11;
3161 pos = os_strchr(pos, ' ');
3162 if (!pos)
3163 return -1;
3164 pos++;
3165 }
3166 req = wpabuf_parse_bin(pos);
3167 if (!req)
3168 return -1;
3169
3170 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3171 wpabuf_free(req);
3172 if (ret >= 0)
3173 ret = os_snprintf(reply, reply_size, "%d", ret);
3174 return ret;
3175}
3176
3177
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003178static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3179 char *buf, size_t buflen)
3180{
3181 if (!(hapd->conf->radio_measurements[0] &
3182 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3183 wpa_printf(MSG_ERROR,
3184 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3185 return -1;
3186 }
3187
3188 return hostapd_neighbor_show(hapd, buf, buflen);
3189}
3190
3191
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003192static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3193{
3194 struct wpa_ssid_value ssid;
3195 u8 bssid[ETH_ALEN];
3196 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003197 int stationary = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08003198 int bss_parameters = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003199 char *tmp;
Hai Shalomc1a21442022-02-04 13:43:00 -08003200 int ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003201
3202 if (!(hapd->conf->radio_measurements[0] &
3203 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3204 wpa_printf(MSG_ERROR,
3205 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3206 return -1;
3207 }
3208
3209 if (hwaddr_aton(buf, bssid)) {
3210 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3211 return -1;
3212 }
3213
3214 tmp = os_strstr(buf, "ssid=");
3215 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3216 wpa_printf(MSG_ERROR,
3217 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3218 return -1;
3219 }
3220 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3221 if (!buf)
3222 return -1;
3223
3224 tmp = os_strstr(buf, "nr=");
3225 if (!tmp) {
3226 wpa_printf(MSG_ERROR,
3227 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3228 return -1;
3229 }
3230
3231 buf = os_strchr(tmp, ' ');
3232 if (buf)
3233 *buf++ = '\0';
3234
3235 nr = wpabuf_parse_bin(tmp + 3);
3236 if (!nr) {
3237 wpa_printf(MSG_ERROR,
3238 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3239 return -1;
3240 }
3241
3242 if (!buf)
3243 goto set;
3244
3245 tmp = os_strstr(buf, "lci=");
3246 if (tmp) {
3247 buf = os_strchr(tmp, ' ');
3248 if (buf)
3249 *buf++ = '\0';
3250 lci = wpabuf_parse_bin(tmp + 4);
3251 if (!lci) {
3252 wpa_printf(MSG_ERROR,
3253 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
Hai Shalomc1a21442022-02-04 13:43:00 -08003254 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003255 }
3256 }
3257
3258 if (!buf)
3259 goto set;
3260
3261 tmp = os_strstr(buf, "civic=");
3262 if (tmp) {
3263 buf = os_strchr(tmp, ' ');
3264 if (buf)
3265 *buf++ = '\0';
3266 civic = wpabuf_parse_bin(tmp + 6);
3267 if (!civic) {
3268 wpa_printf(MSG_ERROR,
3269 "CTRL: SET_NEIGHBOR: Bad civic subelement");
Hai Shalomc1a21442022-02-04 13:43:00 -08003270 goto fail;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003271 }
3272 }
3273
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003274 if (!buf)
3275 goto set;
3276
3277 if (os_strstr(buf, "stat"))
3278 stationary = 1;
3279
Hai Shalomc1a21442022-02-04 13:43:00 -08003280 tmp = os_strstr(buf, "bss_parameter=");
3281 if (tmp) {
3282 bss_parameters = atoi(tmp + 14);
3283 if (bss_parameters < 0 || bss_parameters > 0xff) {
3284 wpa_printf(MSG_ERROR,
3285 "CTRL: SET_NEIGHBOR: Bad bss_parameters subelement");
3286 goto fail;
3287 }
3288 }
3289
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003290set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003291 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
Hai Shalomc1a21442022-02-04 13:43:00 -08003292 stationary, bss_parameters);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003293
Hai Shalomc1a21442022-02-04 13:43:00 -08003294fail:
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003295 wpabuf_free(nr);
3296 wpabuf_free(lci);
3297 wpabuf_free(civic);
3298
3299 return ret;
3300}
3301
3302
3303static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3304 char *buf)
3305{
3306 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003307 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003308 u8 bssid[ETH_ALEN];
3309 char *tmp;
3310
3311 if (hwaddr_aton(buf, bssid)) {
3312 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3313 return -1;
3314 }
3315
3316 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003317 if (tmp) {
3318 ssidp = &ssid;
3319 if (ssid_parse(tmp + 5, &ssid)) {
3320 wpa_printf(MSG_ERROR,
3321 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3322 return -1;
3323 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003324 }
3325
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003326 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003327}
3328
3329
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003330static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3331 size_t buflen)
3332{
3333 int ret, i;
3334 char *pos, *end;
3335
3336 ret = os_snprintf(buf, buflen, "%016llX:\n",
3337 (long long unsigned) iface->drv_flags);
3338 if (os_snprintf_error(buflen, ret))
3339 return -1;
3340
3341 pos = buf + ret;
3342 end = buf + buflen;
3343
3344 for (i = 0; i < 64; i++) {
3345 if (iface->drv_flags & (1LLU << i)) {
3346 ret = os_snprintf(pos, end - pos, "%s\n",
3347 driver_flag_to_string(1LLU << i));
3348 if (os_snprintf_error(end - pos, ret))
3349 return -1;
3350 pos += ret;
3351 }
3352 }
3353
3354 return pos - buf;
3355}
3356
3357
Hai Shalomb755a2a2020-04-23 21:49:02 -07003358static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3359 size_t buflen)
3360{
3361 int ret, i;
3362 char *pos, *end;
3363
3364 ret = os_snprintf(buf, buflen, "%016llX:\n",
3365 (long long unsigned) iface->drv_flags2);
3366 if (os_snprintf_error(buflen, ret))
3367 return -1;
3368
3369 pos = buf + ret;
3370 end = buf + buflen;
3371
3372 for (i = 0; i < 64; i++) {
3373 if (iface->drv_flags2 & (1LLU << i)) {
3374 ret = os_snprintf(pos, end - pos, "%s\n",
3375 driver_flag2_to_string(1LLU << i));
3376 if (os_snprintf_error(end - pos, ret))
3377 return -1;
3378 pos += ret;
3379 }
3380 }
3381
3382 return pos - buf;
3383}
3384
3385
Roshan Pius3a1667e2018-07-03 15:17:14 -07003386static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
3387 const char *txtaddr)
3388{
3389 u8 addr[ETH_ALEN];
3390 struct vlan_description vlan_id;
3391
3392 if (!(*num))
3393 return 0;
3394
3395 if (hwaddr_aton(txtaddr, addr))
3396 return -1;
3397
3398 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
3399 hostapd_remove_acl_mac(acl, num, addr);
3400
3401 return 0;
3402}
3403
3404
3405static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
3406 int *num)
3407{
3408 while (*num)
3409 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
3410}
3411
3412
3413static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
3414 char *buf, size_t buflen)
3415{
3416 int i = 0, len = 0, ret = 0;
3417
3418 if (!acl)
3419 return 0;
3420
3421 while (i < num) {
3422 ret = os_snprintf(buf + len, buflen - len,
3423 MACSTR " VLAN_ID=%d\n",
3424 MAC2STR(acl[i].addr),
3425 acl[i].vlan_id.untagged);
3426 if (ret < 0 || (size_t) ret >= buflen - len)
3427 return len;
3428 i++;
3429 len += ret;
3430 }
3431 return len;
3432}
3433
3434
3435static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
3436 const char *cmd)
3437{
3438 u8 addr[ETH_ALEN];
3439 struct vlan_description vlan_id;
3440 int ret = 0, vlanid = 0;
3441 const char *pos;
3442
3443 if (hwaddr_aton(cmd, addr))
3444 return -1;
3445
3446 pos = os_strstr(cmd, "VLAN_ID=");
3447 if (pos)
3448 vlanid = atoi(pos + 8);
3449
3450 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
3451 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
3452 if (ret != -1 && *acl)
3453 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
3454 }
3455
3456 return ret < 0 ? -1 : 0;
3457}
3458
3459
Hai Shalom021b0b52019-04-10 11:17:58 -07003460static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3461 const char *field, char *buf,
3462 size_t buflen)
3463{
3464 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3465
3466#ifdef CONFIG_DPP
3467 if (os_strcmp(field, "dpp") == 0) {
3468 int res;
3469
Hai Shalomc1a21442022-02-04 13:43:00 -08003470#ifdef CONFIG_DPP3
3471 res = os_snprintf(buf, buflen, "DPP=3");
3472#elif defined(CONFIG_DPP2)
Hai Shalom021b0b52019-04-10 11:17:58 -07003473 res = os_snprintf(buf, buflen, "DPP=2");
3474#else /* CONFIG_DPP2 */
3475 res = os_snprintf(buf, buflen, "DPP=1");
3476#endif /* CONFIG_DPP2 */
3477 if (os_snprintf_error(buflen, res))
3478 return -1;
3479 return res;
3480 }
3481#endif /* CONFIG_DPP */
3482
3483 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3484 field);
3485
3486 return -1;
3487}
3488
3489
Hai Shalomc1a21442022-02-04 13:43:00 -08003490#ifdef ANDROID
3491static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3492 char *buf, size_t buflen)
3493{
3494 int ret;
3495
3496 ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3497 if (ret == 0) {
3498 ret = os_snprintf(buf, buflen, "%s\n", "OK");
3499 if (os_snprintf_error(buflen, ret))
3500 ret = -1;
3501 }
3502 return ret;
3503}
3504#endif /* ANDROID */
3505
3506
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003507static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3508 char *buf, char *reply,
3509 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003510 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003511 socklen_t fromlen)
3512{
3513 int reply_len, res;
3514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 os_memcpy(reply, "OK\n", 3);
3516 reply_len = 3;
3517
3518 if (os_strcmp(buf, "PING") == 0) {
3519 os_memcpy(reply, "PONG\n", 5);
3520 reply_len = 5;
3521 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3522 if (wpa_debug_reopen_file() < 0)
3523 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003524 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3525 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003526 } else if (os_strcmp(buf, "STATUS") == 0) {
3527 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3528 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003529 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3530 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 } else if (os_strcmp(buf, "MIB") == 0) {
3532 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3533 if (reply_len >= 0) {
3534 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3535 reply_size - reply_len);
3536 if (res < 0)
3537 reply_len = -1;
3538 else
3539 reply_len += res;
3540 }
3541 if (reply_len >= 0) {
3542 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3543 reply_size - reply_len);
3544 if (res < 0)
3545 reply_len = -1;
3546 else
3547 reply_len += res;
3548 }
3549#ifndef CONFIG_NO_RADIUS
3550 if (reply_len >= 0) {
3551 res = radius_client_get_mib(hapd->radius,
3552 reply + reply_len,
3553 reply_size - reply_len);
3554 if (res < 0)
3555 reply_len = -1;
3556 else
3557 reply_len += res;
3558 }
3559#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003560 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3561 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3562 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3564 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3565 reply_size);
3566 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3567 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3568 reply_size);
3569 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3570 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3571 reply_size);
3572 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003573 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3574 reply_len = -1;
3575 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3576 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577 reply_len = -1;
3578 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003579 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 reply_len = -1;
3581 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003582 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 buf + 6))
3584 reply_len = -1;
3585 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3586 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3587 reply_len = -1;
3588 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3589 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3590 reply_len = -1;
3591 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3592 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3593 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003594#ifdef CONFIG_TAXONOMY
3595 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3596 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3597 reply, reply_size);
3598#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003599 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3600 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3601 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003602 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3603 if (hostapd_ctrl_iface_stop_ap(hapd))
3604 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605#ifdef NEED_AP_MLME
3606 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3607 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3608 reply_len = -1;
3609#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610#ifdef CONFIG_WPS
3611 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3612 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3613 reply_len = -1;
3614 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3615 reply_len = hostapd_ctrl_iface_wps_check_pin(
3616 hapd, buf + 14, reply, reply_size);
3617 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3618 if (hostapd_wps_button_pushed(hapd, NULL))
3619 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003620 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3621 if (hostapd_wps_cancel(hapd))
3622 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3624 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3625 reply, reply_size);
3626 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3627 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3628 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003629 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3630 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3631 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003632#ifdef CONFIG_WPS_NFC
3633 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3634 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3635 reply_len = -1;
3636 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3637 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3638 hapd, buf + 21, reply, reply_size);
3639 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3640 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3641 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003642 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3643 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3644 hapd, buf + 21, reply, reply_size);
3645 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3646 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3647 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003648#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003650#ifdef CONFIG_INTERWORKING
3651 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3652 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3653 reply_len = -1;
3654 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3655 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3656 reply_len = -1;
3657#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003658#ifdef CONFIG_HS20
3659 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3660 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3661 reply_len = -1;
3662 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3663 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3664 reply_len = -1;
3665#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003666#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003667 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3668 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3669 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003670 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3671 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3672 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003673 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3674 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3675 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003676 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3677 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3678 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003679#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3681 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3682 reply_size);
3683 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3684 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3685 reply_len = -1;
3686 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3687 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3688 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003689 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3690 if (hostapd_ctrl_iface_enable(hapd->iface))
3691 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003692 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3693 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3694 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003695 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3696 if (hostapd_ctrl_iface_reload(hapd->iface))
3697 reply_len = -1;
3698 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3699 if (hostapd_ctrl_iface_disable(hapd->iface))
3700 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003701 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3702 if (ieee802_11_set_beacon(hapd))
3703 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003704#ifdef CONFIG_TESTING_OPTIONS
3705 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3706 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3707 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003708 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3709 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3710 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003711 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3712 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3713 buf + 23) < 0)
3714 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003715 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3716 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3717 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003718 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3719 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3720 reply_len = -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08003721 } else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3722 if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3723 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003724 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3725 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3726 reply_len = -1;
3727 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3728 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3729 reply_len = -1;
3730 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3731 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3732 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003733 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3734 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3735 reply_len = -1;
3736 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3737 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3738 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003739 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3740 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3741 reply_len = -1;
3742 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3743 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003744 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3745 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3746 reply_len = -1;
3747 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3748 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3749 reply_len = -1;
3750 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3751 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3752 reply_len = -1;
3753 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3754 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3755 reply_len = -1;
3756 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3757 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3758 reply_len = -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08003759 } else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3760 if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3761 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003762 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3763 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3764 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003765 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3766 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3767 reply_size);
Hai Shalomc1a21442022-02-04 13:43:00 -08003768 } else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3769 if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3770 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003771#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003772 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003773 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003774 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003775 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3776 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3777 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003778 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3779 ieee802_1x_erp_flush(hapd);
3780#ifdef RADIUS_SERVER
3781 radius_server_erp_flush(hapd->radius_srv);
3782#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003783 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3784 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3785 reply_len = -1;
3786 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3787 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3788 reply_len = -1;
3789 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3790 reply_len = hostapd_ctrl_iface_log_level(
3791 hapd, buf + 9, reply, reply_size);
3792#ifdef NEED_AP_MLME
3793 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3794 reply_len = hostapd_ctrl_iface_track_sta_list(
3795 hapd, reply, reply_size);
3796#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003797 } else if (os_strcmp(buf, "PMKSA") == 0) {
3798 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3799 reply_size);
3800 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3801 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003802 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3803 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3804 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003805 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3806 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3807 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003808 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3809 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3810 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003811 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3812 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3813 reply_len = -1;
3814 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3815 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3816 reply_len = -1;
3817 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3818 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3819 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003820 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3821 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3822 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003823 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3824 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3825 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003826 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3827 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3828 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003829 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3830 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003831 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3832 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003833 if (hostapd_ctrl_iface_acl_add_mac(
3834 &hapd->conf->accept_mac,
3835 &hapd->conf->num_accept_mac, buf + 19))
3836 reply_len = -1;
3837 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3838 if (!hostapd_ctrl_iface_acl_del_mac(
Roshan Pius3a1667e2018-07-03 15:17:14 -07003839 &hapd->conf->accept_mac,
3840 &hapd->conf->num_accept_mac, buf + 19))
3841 hostapd_disassoc_accept_mac(hapd);
3842 else
3843 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003844 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3845 reply_len = hostapd_ctrl_iface_acl_show_mac(
3846 hapd->conf->accept_mac,
3847 hapd->conf->num_accept_mac, reply, reply_size);
3848 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3849 hostapd_ctrl_iface_acl_clear_list(
3850 &hapd->conf->accept_mac,
3851 &hapd->conf->num_accept_mac);
Hai Shalom60840252021-02-19 19:02:11 -08003852 hostapd_disassoc_accept_mac(hapd);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003853 }
3854 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3855 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3856 if (!hostapd_ctrl_iface_acl_add_mac(
3857 &hapd->conf->deny_mac,
3858 &hapd->conf->num_deny_mac, buf + 17))
3859 hostapd_disassoc_deny_mac(hapd);
Hai Shalom60840252021-02-19 19:02:11 -08003860 else
3861 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003862 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
Hai Shalom60840252021-02-19 19:02:11 -08003863 if (hostapd_ctrl_iface_acl_del_mac(
3864 &hapd->conf->deny_mac,
3865 &hapd->conf->num_deny_mac, buf + 17))
3866 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003867 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3868 reply_len = hostapd_ctrl_iface_acl_show_mac(
3869 hapd->conf->deny_mac,
3870 hapd->conf->num_deny_mac, reply, reply_size);
3871 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3872 hostapd_ctrl_iface_acl_clear_list(
3873 &hapd->conf->deny_mac,
3874 &hapd->conf->num_deny_mac);
3875 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003876#ifdef CONFIG_DPP
3877 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3878 res = hostapd_dpp_qr_code(hapd, buf + 12);
3879 if (res < 0) {
3880 reply_len = -1;
3881 } else {
3882 reply_len = os_snprintf(reply, reply_size, "%d", res);
3883 if (os_snprintf_error(reply_size, reply_len))
3884 reply_len = -1;
3885 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003886 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3887 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3888 if (res < 0) {
3889 reply_len = -1;
3890 } else {
3891 reply_len = os_snprintf(reply, reply_size, "%d", res);
3892 if (os_snprintf_error(reply_size, reply_len))
3893 reply_len = -1;
3894 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003895 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3896 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3897 if (res < 0) {
3898 reply_len = -1;
3899 } else {
3900 reply_len = os_snprintf(reply, reply_size, "%d", res);
3901 if (os_snprintf_error(reply_size, reply_len))
3902 reply_len = -1;
3903 }
3904 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3905 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3906 if (res < 0) {
3907 reply_len = -1;
3908 } else {
3909 reply_len = os_snprintf(reply, reply_size, "%d", res);
3910 if (os_snprintf_error(reply_size, reply_len))
3911 reply_len = -1;
3912 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003913 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003914 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003915 if (res < 0) {
3916 reply_len = -1;
3917 } else {
3918 reply_len = os_snprintf(reply, reply_size, "%d", res);
3919 if (os_snprintf_error(reply_size, reply_len))
3920 reply_len = -1;
3921 }
3922 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003923 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3924 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003925 reply_len = -1;
3926 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3927 const char *uri;
3928
Hai Shalom021b0b52019-04-10 11:17:58 -07003929 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3930 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003931 if (!uri) {
3932 reply_len = -1;
3933 } else {
3934 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3935 if (os_snprintf_error(reply_size, reply_len))
3936 reply_len = -1;
3937 }
3938 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003939 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3940 atoi(buf + 19),
3941 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003942 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3943 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3944 atoi(buf + 18),
3945 os_strchr(buf + 18, ' ')) < 0)
3946 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003947 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3948 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3949 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003950 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3951 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3952 reply_len = -1;
3953 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3954 hostapd_dpp_stop(hapd);
3955 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003956 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003957 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3958 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003959 if (res < 0) {
3960 reply_len = -1;
3961 } else {
3962 reply_len = os_snprintf(reply, reply_size, "%d", res);
3963 if (os_snprintf_error(reply_size, reply_len))
3964 reply_len = -1;
3965 }
3966 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003967 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3968 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003969 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003970 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003971 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003972 reply_len = -1;
3973 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003974 reply_len = dpp_configurator_get_key_id(
3975 hapd->iface->interfaces->dpp,
3976 atoi(buf + 25),
3977 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003978 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3979 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3980 if (res < 0) {
3981 reply_len = -1;
3982 } else {
3983 reply_len = os_snprintf(reply, reply_size, "%d", res);
3984 if (os_snprintf_error(reply_size, reply_len))
3985 reply_len = -1;
3986 }
3987 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3988 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3989 reply_len = -1;
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003990#ifdef CONFIG_DPP2
Hai Shalom899fcc72020-10-19 14:38:18 -07003991 } else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3992 if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3993 reply_len = -1;
3994 } else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3995 if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3996 reply_len = -1;
3997 } else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3998 dpp_controller_stop(hapd->iface->interfaces->dpp);
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003999 } else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
4000 if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
4001 reply_len = -1;
4002 } else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
4003 hostapd_dpp_chirp_stop(hapd);
4004#endif /* CONFIG_DPP2 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004005#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004006#ifdef RADIUS_SERVER
4007 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
4008 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
4009 reply_len = -1;
4010#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07004011 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4012 reply_len = hostapd_ctrl_iface_get_capability(
4013 hapd, buf + 15, reply, reply_size);
Hai Shalom60840252021-02-19 19:02:11 -08004014#ifdef CONFIG_PASN
4015 } else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
4016 reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
4017#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08004018#ifdef ANDROID
4019 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4020 reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4021 reply_size);
4022#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 } else {
4024 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4025 reply_len = 16;
4026 }
4027
4028 if (reply_len < 0) {
4029 os_memcpy(reply, "FAIL\n", 5);
4030 reply_len = 5;
4031 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004032
4033 return reply_len;
4034}
4035
4036
4037static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4038 void *sock_ctx)
4039{
4040 struct hostapd_data *hapd = eloop_ctx;
4041 char buf[4096];
4042 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004043 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004044 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004045 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004046 const int reply_size = 4096;
4047 int reply_len;
4048 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004049#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004050 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004051#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004052
4053 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4054 (struct sockaddr *) &from, &fromlen);
4055 if (res < 0) {
4056 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4057 strerror(errno));
4058 return;
4059 }
4060 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004061
4062 reply = os_malloc(reply_size);
4063 if (reply == NULL) {
4064 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4065 fromlen) < 0) {
4066 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4067 strerror(errno));
4068 }
4069 return;
4070 }
4071
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004072#ifdef CONFIG_CTRL_IFACE_UDP
4073 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4074 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004075 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4076 hapd->ctrl_iface_cookie,
4077 CTRL_IFACE_COOKIE_LEN);
4078 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004079 goto done;
4080 }
4081
4082 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004083 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004084 wpa_printf(MSG_DEBUG,
4085 "CTRL: No cookie in the request - drop request");
4086 os_free(reply);
4087 return;
4088 }
4089
Hai Shalomfdcde762020-04-02 11:19:20 -07004090 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4091 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004092 wpa_printf(MSG_DEBUG,
4093 "CTRL: Invalid cookie in the request - drop request");
4094 os_free(reply);
4095 return;
4096 }
4097
Hai Shalomfdcde762020-04-02 11:19:20 -07004098 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004099 while (*pos == ' ')
4100 pos++;
4101#endif /* CONFIG_CTRL_IFACE_UDP */
4102
4103 if (os_strcmp(pos, "PING") == 0)
4104 level = MSG_EXCESSIVE;
4105 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4106
4107 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004108 reply, reply_size,
4109 &from, fromlen);
4110
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004111#ifdef CONFIG_CTRL_IFACE_UDP
4112done:
4113#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004114 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4115 fromlen) < 0) {
4116 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4117 strerror(errno));
4118 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 os_free(reply);
4120}
4121
4122
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004123#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
4125{
4126 char *buf;
4127 size_t len;
4128
4129 if (hapd->conf->ctrl_interface == NULL)
4130 return NULL;
4131
4132 len = os_strlen(hapd->conf->ctrl_interface) +
4133 os_strlen(hapd->conf->iface) + 2;
4134 buf = os_malloc(len);
4135 if (buf == NULL)
4136 return NULL;
4137
4138 os_snprintf(buf, len, "%s/%s",
4139 hapd->conf->ctrl_interface, hapd->conf->iface);
4140 buf[len - 1] = '\0';
4141 return buf;
4142}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004143#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004144
4145
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07004146static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
4147 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148 const char *txt, size_t len)
4149{
4150 struct hostapd_data *hapd = ctx;
4151 if (hapd == NULL)
4152 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004153 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004154}
4155
4156
4157int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
4158{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004159#ifdef CONFIG_CTRL_IFACE_UDP
4160 int port = HOSTAPD_CTRL_IFACE_PORT;
4161 char p[32] = { 0 };
4162 char port_str[40], *tmp;
4163 char *pos;
4164 struct addrinfo hints = { 0 }, *res, *saveres;
4165 int n;
4166
4167 if (hapd->ctrl_sock > -1) {
4168 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4169 return 0;
4170 }
4171
4172 if (hapd->conf->ctrl_interface == NULL)
4173 return 0;
4174
4175 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
4176 if (pos) {
4177 pos += 4;
4178 port = atoi(pos);
4179 if (port <= 0) {
4180 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
4181 goto fail;
4182 }
4183 }
4184
4185 dl_list_init(&hapd->ctrl_dst);
4186 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004187 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004188
4189#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4190 hints.ai_flags = AI_PASSIVE;
4191#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4192
4193#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4194 hints.ai_family = AF_INET6;
4195#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4196 hints.ai_family = AF_INET;
4197#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4198 hints.ai_socktype = SOCK_DGRAM;
4199
4200try_again:
4201 os_snprintf(p, sizeof(p), "%d", port);
4202 n = getaddrinfo(NULL, p, &hints, &res);
4203 if (n) {
4204 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4205 goto fail;
4206 }
4207
4208 saveres = res;
4209 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4210 res->ai_protocol);
4211 if (hapd->ctrl_sock < 0) {
4212 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4213 goto fail;
4214 }
4215
4216 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4217 port--;
4218 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4219 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4220 goto try_again;
4221 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4222 goto fail;
4223 }
4224
4225 freeaddrinfo(saveres);
4226
4227 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4228 tmp = os_strdup(port_str);
4229 if (tmp) {
4230 os_free(hapd->conf->ctrl_interface);
4231 hapd->conf->ctrl_interface = tmp;
4232 }
4233 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4234
4235 if (eloop_register_read_sock(hapd->ctrl_sock,
4236 hostapd_ctrl_iface_receive, hapd, NULL) <
4237 0) {
4238 hostapd_ctrl_iface_deinit(hapd);
4239 return -1;
4240 }
4241
4242 hapd->msg_ctx = hapd;
4243 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4244
4245 return 0;
4246
4247fail:
4248 if (hapd->ctrl_sock >= 0)
4249 close(hapd->ctrl_sock);
4250 return -1;
4251#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004252 struct sockaddr_un addr;
4253 int s = -1;
4254 char *fname = NULL;
4255
Dmitry Shmidt04949592012-07-19 12:16:46 -07004256 if (hapd->ctrl_sock > -1) {
4257 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4258 return 0;
4259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004261 dl_list_init(&hapd->ctrl_dst);
4262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 if (hapd->conf->ctrl_interface == NULL)
4264 return 0;
4265
4266 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4267 if (errno == EEXIST) {
4268 wpa_printf(MSG_DEBUG, "Using existing control "
4269 "interface directory.");
4270 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004271 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4272 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 goto fail;
4274 }
4275 }
4276
4277 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004278 lchown(hapd->conf->ctrl_interface, -1,
4279 hapd->conf->ctrl_interface_gid) < 0) {
4280 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004281 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282 return -1;
4283 }
4284
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004285 if (!hapd->conf->ctrl_interface_gid_set &&
4286 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004287 lchown(hapd->conf->ctrl_interface, -1,
4288 hapd->iface->interfaces->ctrl_iface_group) < 0) {
4289 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004290 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004291 return -1;
4292 }
4293
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004294#ifdef ANDROID
4295 /*
4296 * Android is using umask 0077 which would leave the control interface
4297 * directory without group access. This breaks things since Wi-Fi
4298 * framework assumes that this directory can be accessed by other
4299 * applications in the wifi group. Fix this by adding group access even
4300 * if umask value would prevent this.
4301 */
4302 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4303 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4304 strerror(errno));
4305 /* Try to continue anyway */
4306 }
4307#endif /* ANDROID */
4308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4310 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4311 goto fail;
4312
4313 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4314 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004315 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 goto fail;
4317 }
4318
4319 os_memset(&addr, 0, sizeof(addr));
4320#ifdef __FreeBSD__
4321 addr.sun_len = sizeof(addr);
4322#endif /* __FreeBSD__ */
4323 addr.sun_family = AF_UNIX;
4324 fname = hostapd_ctrl_iface_path(hapd);
4325 if (fname == NULL)
4326 goto fail;
4327 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4328 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4329 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4330 strerror(errno));
4331 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4332 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4333 " allow connections - assuming it was left"
4334 "over from forced program termination");
4335 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004336 wpa_printf(MSG_ERROR,
4337 "Could not unlink existing ctrl_iface socket '%s': %s",
4338 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 goto fail;
4340 }
4341 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4342 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004343 wpa_printf(MSG_ERROR,
4344 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4345 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346 goto fail;
4347 }
4348 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4349 "ctrl_iface socket '%s'", fname);
4350 } else {
4351 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4352 "be in use - cannot override it");
4353 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4354 "not used anymore", fname);
4355 os_free(fname);
4356 fname = NULL;
4357 goto fail;
4358 }
4359 }
4360
4361 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004362 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4363 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004364 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 goto fail;
4366 }
4367
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004368 if (!hapd->conf->ctrl_interface_gid_set &&
4369 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004370 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4371 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004372 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004373 goto fail;
4374 }
4375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004377 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4378 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 goto fail;
4380 }
4381 os_free(fname);
4382
4383 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004384 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4385 NULL) < 0) {
4386 hostapd_ctrl_iface_deinit(hapd);
4387 return -1;
4388 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 hapd->msg_ctx = hapd;
4390 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4391
4392 return 0;
4393
4394fail:
4395 if (s >= 0)
4396 close(s);
4397 if (fname) {
4398 unlink(fname);
4399 os_free(fname);
4400 }
4401 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004402#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403}
4404
4405
4406void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4407{
4408 struct wpa_ctrl_dst *dst, *prev;
4409
4410 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004411#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004413#endif /* !CONFIG_CTRL_IFACE_UDP */
4414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 eloop_unregister_read_sock(hapd->ctrl_sock);
4416 close(hapd->ctrl_sock);
4417 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004418#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419 fname = hostapd_ctrl_iface_path(hapd);
4420 if (fname)
4421 unlink(fname);
4422 os_free(fname);
4423
4424 if (hapd->conf->ctrl_interface &&
4425 rmdir(hapd->conf->ctrl_interface) < 0) {
4426 if (errno == ENOTEMPTY) {
4427 wpa_printf(MSG_DEBUG, "Control interface "
4428 "directory not empty - leaving it "
4429 "behind");
4430 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004431 wpa_printf(MSG_ERROR,
4432 "rmdir[ctrl_interface=%s]: %s",
4433 hapd->conf->ctrl_interface,
4434 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 }
4436 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004437#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 }
4439
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004440 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4441 list)
4442 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004443
4444#ifdef CONFIG_TESTING_OPTIONS
4445 l2_packet_deinit(hapd->l2_test);
4446 hapd->l2_test = NULL;
4447#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448}
4449
4450
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004451static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4452 char *buf)
4453{
4454 if (hostapd_add_iface(interfaces, buf) < 0) {
4455 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4456 return -1;
4457 }
4458 return 0;
4459}
4460
4461
4462static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4463 char *buf)
4464{
4465 if (hostapd_remove_iface(interfaces, buf) < 0) {
4466 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4467 return -1;
4468 }
4469 return 0;
4470}
4471
4472
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004473static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004474 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004475 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004476{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004477 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4478 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004479}
4480
4481
4482static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004483 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004484 socklen_t fromlen)
4485{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004486 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004487}
4488
4489
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004490static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4491{
4492#ifdef CONFIG_WPS_TESTING
4493 wps_version_number = 0x20;
Hai Shalomc1a21442022-02-04 13:43:00 -08004494 wps_testing_stub_cred = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004495 wps_corrupt_pkhash = 0;
4496#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004497
4498#ifdef CONFIG_TESTING_OPTIONS
4499#ifdef CONFIG_DPP
4500 dpp_test = DPP_TEST_DISABLED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004501#ifdef CONFIG_DPP3
4502 dpp_version_override = 3;
4503#elif defined(CONFIG_DPP2)
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004504 dpp_version_override = 2;
4505#else /* CONFIG_DPP2 */
4506 dpp_version_override = 1;
4507#endif /* CONFIG_DPP2 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004508#endif /* CONFIG_DPP */
4509#endif /* CONFIG_TESTING_OPTIONS */
4510
4511#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004512 dpp_global_clear(interfaces->dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004513#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004514}
4515
4516
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004517#ifdef CONFIG_FST
4518
4519static int
4520hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4521 const char *cmd)
4522{
4523 char ifname[IFNAMSIZ + 1];
4524 struct fst_iface_cfg cfg;
4525 struct hostapd_data *hapd;
4526 struct fst_wpa_obj iface_obj;
4527
4528 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4529 hapd = hostapd_get_iface(interfaces, ifname);
4530 if (hapd) {
4531 if (hapd->iface->fst) {
4532 wpa_printf(MSG_INFO, "FST: Already attached");
4533 return -1;
4534 }
4535 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4536 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4537 &iface_obj, &cfg);
4538 if (hapd->iface->fst)
4539 return 0;
4540 }
4541 }
4542
4543 return -EINVAL;
4544}
4545
4546
4547static int
4548hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4549 const char *cmd)
4550{
4551 char ifname[IFNAMSIZ + 1];
4552 struct hostapd_data * hapd;
4553
4554 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4555 hapd = hostapd_get_iface(interfaces, ifname);
4556 if (hapd) {
4557 if (!fst_iface_detach(ifname)) {
4558 hapd->iface->fst = NULL;
4559 hapd->iface->fst_ies = NULL;
4560 return 0;
4561 }
4562 }
4563 }
4564
4565 return -EINVAL;
4566}
4567
4568#endif /* CONFIG_FST */
4569
4570
4571static struct hostapd_data *
4572hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4573 const char *ifname)
4574{
4575 size_t i, j;
4576
4577 for (i = 0; i < interfaces->count; i++) {
4578 struct hostapd_iface *iface = interfaces->iface[i];
4579
4580 for (j = 0; j < iface->num_bss; j++) {
4581 struct hostapd_data *hapd;
4582
4583 hapd = iface->bss[j];
4584 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4585 return hapd;
4586 }
4587 }
4588
4589 return NULL;
4590}
4591
4592
4593static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4594 struct hostapd_data *dst_hapd,
4595 const char *param)
4596{
4597 int res;
4598 char *value;
4599
4600 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4601 if (!value) {
4602 wpa_printf(MSG_ERROR,
4603 "DUP: cannot allocate buffer to stringify %s",
4604 param);
4605 goto error_return;
4606 }
4607
4608 if (os_strcmp(param, "wpa") == 0) {
4609 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4610 src_hapd->conf->wpa);
4611 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4612 src_hapd->conf->wpa_key_mgmt) {
4613 res = hostapd_ctrl_iface_get_key_mgmt(
4614 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4615 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4616 goto error_stringify;
4617 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4618 src_hapd->conf->wpa_pairwise) {
4619 res = wpa_write_ciphers(value,
4620 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4621 src_hapd->conf->wpa_pairwise, " ");
4622 if (res < 0)
4623 goto error_stringify;
4624 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4625 src_hapd->conf->rsn_pairwise) {
4626 res = wpa_write_ciphers(value,
4627 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4628 src_hapd->conf->rsn_pairwise, " ");
4629 if (res < 0)
4630 goto error_stringify;
4631 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4632 src_hapd->conf->ssid.wpa_passphrase) {
4633 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4634 src_hapd->conf->ssid.wpa_passphrase);
4635 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4636 src_hapd->conf->ssid.wpa_psk_set) {
4637 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4638 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4639 } else {
4640 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4641 goto error_return;
4642 }
4643
4644 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4645 os_free(value);
4646 return res;
4647
4648error_stringify:
4649 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4650error_return:
4651 os_free(value);
4652 return -1;
4653}
4654
4655
4656static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004657hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4658 const char *input,
4659 char *reply, int reply_size)
4660{
4661 size_t i, j;
4662 int res;
4663 char *pos, *end;
4664 struct hostapd_iface *iface;
4665 int show_ctrl = 0;
4666
4667 if (input)
4668 show_ctrl = !!os_strstr(input, "ctrl");
4669
4670 pos = reply;
4671 end = reply + reply_size;
4672
4673 for (i = 0; i < interfaces->count; i++) {
4674 iface = interfaces->iface[i];
4675
4676 for (j = 0; j < iface->num_bss; j++) {
4677 struct hostapd_bss_config *conf;
4678
4679 conf = iface->conf->bss[j];
4680 if (show_ctrl)
4681 res = os_snprintf(pos, end - pos,
4682 "%s ctrl_iface=%s\n",
4683 conf->iface,
4684 conf->ctrl_interface ?
4685 conf->ctrl_interface : "N/A");
4686 else
4687 res = os_snprintf(pos, end - pos, "%s\n",
4688 conf->iface);
4689 if (os_snprintf_error(end - pos, res)) {
4690 *pos = '\0';
4691 return pos - reply;
4692 }
4693 pos += res;
4694 }
4695 }
4696
4697 return pos - reply;
4698}
4699
4700
4701static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004702hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4703 char *cmd)
4704{
4705 char *p_start = cmd, *p_end;
4706 struct hostapd_data *src_hapd, *dst_hapd;
4707
4708 /* cmd: "<src ifname> <dst ifname> <variable name> */
4709
4710 p_end = os_strchr(p_start, ' ');
4711 if (!p_end) {
4712 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4713 cmd);
4714 return -1;
4715 }
4716
4717 *p_end = '\0';
4718 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4719 if (!src_hapd) {
4720 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4721 p_start);
4722 return -1;
4723 }
4724
4725 p_start = p_end + 1;
4726 p_end = os_strchr(p_start, ' ');
4727 if (!p_end) {
4728 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4729 cmd);
4730 return -1;
4731 }
4732
4733 *p_end = '\0';
4734 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4735 if (!dst_hapd) {
4736 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4737 p_start);
4738 return -1;
4739 }
4740
4741 p_start = p_end + 1;
4742 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4743}
4744
4745
4746static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4747 const char *ifname,
4748 char *buf, char *reply,
4749 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004750 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004751 socklen_t fromlen)
4752{
4753 struct hostapd_data *hapd;
4754
4755 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4756 if (hapd == NULL) {
4757 int res;
4758
4759 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4760 if (os_snprintf_error(reply_size, res))
4761 return -1;
4762 return res;
4763 }
4764
4765 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4766 from, fromlen);
4767}
4768
4769
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004770static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4771 void *sock_ctx)
4772{
Hai Shalomfdcde762020-04-02 11:19:20 -07004773 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004774 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004775 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004776 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004777 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004778 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004779 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004780 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004781#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004782 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004783#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004784
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004785 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004786 (struct sockaddr *) &from, &fromlen);
4787 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004788 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4789 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004790 return;
4791 }
4792 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004793 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004794
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004795 reply = os_malloc(reply_size);
4796 if (reply == NULL) {
4797 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4798 fromlen) < 0) {
4799 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4800 strerror(errno));
4801 }
4802 return;
4803 }
4804
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004805 os_memcpy(reply, "OK\n", 3);
4806 reply_len = 3;
4807
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004808#ifdef CONFIG_CTRL_IFACE_UDP
4809 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4810 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004811 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4812 interfaces->ctrl_iface_cookie,
4813 CTRL_IFACE_COOKIE_LEN);
4814 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004815 goto send_reply;
4816 }
4817
4818 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004819 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004820 wpa_printf(MSG_DEBUG,
4821 "CTRL: No cookie in the request - drop request");
4822 os_free(reply);
4823 return;
4824 }
4825
Hai Shalomfdcde762020-04-02 11:19:20 -07004826 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4827 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004828 wpa_printf(MSG_DEBUG,
4829 "CTRL: Invalid cookie in the request - drop request");
4830 os_free(reply);
4831 return;
4832 }
4833
Hai Shalomfdcde762020-04-02 11:19:20 -07004834 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004835 while (*buf == ' ')
4836 buf++;
4837#endif /* CONFIG_CTRL_IFACE_UDP */
4838
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004839 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4840 char *pos = os_strchr(buf + 7, ' ');
4841
4842 if (pos) {
4843 *pos++ = '\0';
4844 reply_len = hostapd_global_ctrl_iface_ifname(
4845 interfaces, buf + 7, pos, reply, reply_size,
4846 &from, fromlen);
4847 goto send_reply;
4848 }
4849 }
4850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004851 if (os_strcmp(buf, "PING") == 0) {
4852 os_memcpy(reply, "PONG\n", 5);
4853 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004854 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4855 if (wpa_debug_reopen_file() < 0)
4856 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004857 } else if (os_strcmp(buf, "FLUSH") == 0) {
4858 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004859 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4860 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4861 reply_len = -1;
4862 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4863 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4864 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004865 } else if (os_strcmp(buf, "ATTACH") == 0) {
4866 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004867 fromlen, NULL))
4868 reply_len = -1;
4869 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4870 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4871 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004872 reply_len = -1;
4873 } else if (os_strcmp(buf, "DETACH") == 0) {
4874 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4875 fromlen))
4876 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004877#ifdef CONFIG_MODULE_TESTS
4878 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004879 if (hapd_module_tests() < 0)
4880 reply_len = -1;
4881#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004882#ifdef CONFIG_FST
4883 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4884 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4885 reply_len = os_snprintf(reply, reply_size, "OK\n");
4886 else
4887 reply_len = -1;
4888 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4889 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4890 reply_len = os_snprintf(reply, reply_size, "OK\n");
4891 else
4892 reply_len = -1;
4893 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4894 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4895#endif /* CONFIG_FST */
4896 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4897 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4898 buf + 12))
4899 reply_len = os_snprintf(reply, reply_size, "OK\n");
4900 else
4901 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004902 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4903 reply_len = hostapd_global_ctrl_iface_interfaces(
4904 interfaces, buf + 10, reply, sizeof(buffer));
4905 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4906 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004907 } else {
4908 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4909 "ignored");
4910 reply_len = -1;
4911 }
4912
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004913send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004914 if (reply_len < 0) {
4915 os_memcpy(reply, "FAIL\n", 5);
4916 reply_len = 5;
4917 }
4918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004919 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4920 fromlen) < 0) {
4921 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4922 strerror(errno));
4923 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004924 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004925}
4926
4927
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004928#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004929static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4930{
4931 char *buf;
4932 size_t len;
4933
4934 if (interface->global_iface_path == NULL)
4935 return NULL;
4936
4937 len = os_strlen(interface->global_iface_path) +
4938 os_strlen(interface->global_iface_name) + 2;
4939 buf = os_malloc(len);
4940 if (buf == NULL)
4941 return NULL;
4942
4943 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4944 interface->global_iface_name);
4945 buf[len - 1] = '\0';
4946 return buf;
4947}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004948#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004949
4950
4951int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4952{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004953#ifdef CONFIG_CTRL_IFACE_UDP
4954 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4955 char p[32] = { 0 };
4956 char *pos;
4957 struct addrinfo hints = { 0 }, *res, *saveres;
4958 int n;
4959
4960 if (interface->global_ctrl_sock > -1) {
4961 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4962 return 0;
4963 }
4964
4965 if (interface->global_iface_path == NULL)
4966 return 0;
4967
4968 pos = os_strstr(interface->global_iface_path, "udp:");
4969 if (pos) {
4970 pos += 4;
4971 port = atoi(pos);
4972 if (port <= 0) {
4973 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4974 goto fail;
4975 }
4976 }
4977
Hai Shalomfdcde762020-04-02 11:19:20 -07004978 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004979
4980#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4981 hints.ai_flags = AI_PASSIVE;
4982#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4983
4984#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4985 hints.ai_family = AF_INET6;
4986#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4987 hints.ai_family = AF_INET;
4988#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4989 hints.ai_socktype = SOCK_DGRAM;
4990
4991try_again:
4992 os_snprintf(p, sizeof(p), "%d", port);
4993 n = getaddrinfo(NULL, p, &hints, &res);
4994 if (n) {
4995 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4996 goto fail;
4997 }
4998
4999 saveres = res;
5000 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
5001 res->ai_protocol);
5002 if (interface->global_ctrl_sock < 0) {
5003 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
5004 goto fail;
5005 }
5006
5007 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
5008 0) {
5009 port++;
5010 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
5011 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
5012 goto try_again;
5013 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
5014 goto fail;
5015 }
5016
5017 freeaddrinfo(saveres);
5018
5019 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
5020
5021 if (eloop_register_read_sock(interface->global_ctrl_sock,
5022 hostapd_global_ctrl_iface_receive,
5023 interface, NULL) < 0) {
5024 hostapd_global_ctrl_iface_deinit(interface);
5025 return -1;
5026 }
5027
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005028 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5029
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005030 return 0;
5031
5032fail:
5033 if (interface->global_ctrl_sock >= 0)
5034 close(interface->global_ctrl_sock);
5035 return -1;
5036#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005037 struct sockaddr_un addr;
5038 int s = -1;
5039 char *fname = NULL;
5040
5041 if (interface->global_iface_path == NULL) {
5042 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
5043 return 0;
5044 }
5045
5046 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
5047 if (errno == EEXIST) {
5048 wpa_printf(MSG_DEBUG, "Using existing control "
5049 "interface directory.");
5050 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005051 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5052 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005053 goto fail;
5054 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005055 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005056 lchown(interface->global_iface_path, -1,
5057 interface->ctrl_iface_group) < 0) {
5058 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005059 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005060 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005061 }
5062
5063 if (os_strlen(interface->global_iface_path) + 1 +
5064 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
5065 goto fail;
5066
5067 s = socket(PF_UNIX, SOCK_DGRAM, 0);
5068 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005069 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005070 goto fail;
5071 }
5072
5073 os_memset(&addr, 0, sizeof(addr));
5074#ifdef __FreeBSD__
5075 addr.sun_len = sizeof(addr);
5076#endif /* __FreeBSD__ */
5077 addr.sun_family = AF_UNIX;
5078 fname = hostapd_global_ctrl_iface_path(interface);
5079 if (fname == NULL)
5080 goto fail;
5081 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5082 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5083 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5084 strerror(errno));
5085 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5086 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5087 " allow connections - assuming it was left"
5088 "over from forced program termination");
5089 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005090 wpa_printf(MSG_ERROR,
5091 "Could not unlink existing ctrl_iface socket '%s': %s",
5092 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005093 goto fail;
5094 }
5095 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5096 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005097 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
5098 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005099 goto fail;
5100 }
5101 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5102 "ctrl_iface socket '%s'", fname);
5103 } else {
5104 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5105 "be in use - cannot override it");
5106 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5107 "not used anymore", fname);
5108 os_free(fname);
5109 fname = NULL;
5110 goto fail;
5111 }
5112 }
5113
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005114 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08005115 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
5116 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005117 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005118 goto fail;
5119 }
5120
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005121 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005122 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5123 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005124 goto fail;
5125 }
5126 os_free(fname);
5127
5128 interface->global_ctrl_sock = s;
5129 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
5130 interface, NULL);
5131
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005132 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5133
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134 return 0;
5135
5136fail:
5137 if (s >= 0)
5138 close(s);
5139 if (fname) {
5140 unlink(fname);
5141 os_free(fname);
5142 }
5143 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005144#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005145}
5146
5147
5148void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
5149{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005150#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005151 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005152#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005153 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005154
5155 if (interfaces->global_ctrl_sock > -1) {
5156 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
5157 close(interfaces->global_ctrl_sock);
5158 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005159#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005160 fname = hostapd_global_ctrl_iface_path(interfaces);
5161 if (fname) {
5162 unlink(fname);
5163 os_free(fname);
5164 }
5165
5166 if (interfaces->global_iface_path &&
5167 rmdir(interfaces->global_iface_path) < 0) {
5168 if (errno == ENOTEMPTY) {
5169 wpa_printf(MSG_DEBUG, "Control interface "
5170 "directory not empty - leaving it "
5171 "behind");
5172 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005173 wpa_printf(MSG_ERROR,
5174 "rmdir[ctrl_interface=%s]: %s",
5175 interfaces->global_iface_path,
5176 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005177 }
5178 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005179#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02005180 }
5181
5182 os_free(interfaces->global_iface_path);
5183 interfaces->global_iface_path = NULL;
5184
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005185 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5186 struct wpa_ctrl_dst, list)
5187 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005188}
5189
5190
Roshan Pius3a1667e2018-07-03 15:17:14 -07005191static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5192 const char *buf)
5193{
5194 /* Enable Probe Request events based on explicit request.
5195 * Other events are enabled by default.
5196 */
5197 if (str_starts(buf, RX_PROBE_REQUEST))
5198 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5199 return 1;
5200}
5201
5202
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005203static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5204 const char *ifname, int level,
5205 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206{
5207 struct wpa_ctrl_dst *dst, *next;
5208 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005209 int idx, res;
5210 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211 char levelstr[10];
5212
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005213 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 return;
5215
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005216 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5217 if (os_snprintf_error(sizeof(levelstr), res))
5218 return;
5219 idx = 0;
5220 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005221 io[idx].iov_base = "IFNAME=";
5222 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005223 idx++;
5224 io[idx].iov_base = (char *) ifname;
5225 io[idx].iov_len = os_strlen(ifname);
5226 idx++;
5227 io[idx].iov_base = " ";
5228 io[idx].iov_len = 1;
5229 idx++;
5230 }
5231 io[idx].iov_base = levelstr;
5232 io[idx].iov_len = os_strlen(levelstr);
5233 idx++;
5234 io[idx].iov_base = (char *) buf;
5235 io[idx].iov_len = len;
5236 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237 os_memset(&msg, 0, sizeof(msg));
5238 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005239 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240
5241 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005242 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005243 if ((level >= dst->debug_level) &&
5244 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005245 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5246 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247 msg.msg_name = &dst->addr;
5248 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005249 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005250 int _errno = errno;
5251 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5252 "%d - %s",
5253 idx, errno, strerror(errno));
5254 dst->errors++;
5255 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005256 ctrl_iface_detach(ctrl_dst,
5257 &dst->addr,
5258 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 }
5260 } else
5261 dst->errors = 0;
5262 }
5263 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 }
5265}
5266
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005267
5268static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5269 enum wpa_msg_type type,
5270 const char *buf, size_t len)
5271{
5272 if (type != WPA_MSG_NO_GLOBAL) {
5273 hostapd_ctrl_iface_send_internal(
5274 hapd->iface->interfaces->global_ctrl_sock,
5275 &hapd->iface->interfaces->global_ctrl_dst,
5276 type != WPA_MSG_PER_INTERFACE ?
5277 NULL : hapd->conf->iface,
5278 level, buf, len);
5279 }
5280
5281 if (type != WPA_MSG_ONLY_GLOBAL) {
5282 hostapd_ctrl_iface_send_internal(
5283 hapd->ctrl_sock, &hapd->ctrl_dst,
5284 NULL, level, buf, len);
5285 }
5286}
5287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288#endif /* CONFIG_NATIVE_WINDOWS */