blob: 6e8352f2fd063e5e83f8f86b48e1a5afe8f405b6 [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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080040#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080042#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080044#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080045#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "ap/hostapd.h"
47#include "ap/ap_config.h"
48#include "ap/ieee802_1x.h"
49#include "ap/wpa_auth.h"
50#include "ap/ieee802_11.h"
51#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052#include "ap/wps_hostapd.h"
53#include "ap/ctrl_iface_ap.h"
54#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080055#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080056#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070057#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080058#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070059#include "ap/neighbor_db.h"
60#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070061#include "ap/dpp_hostapd.h"
Hai Shalomfdcde762020-04-02 11:19:20 -070062#include "ap/dfs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063#include "wps/wps_defs.h"
64#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080065#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070066#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067#include "ctrl_iface.h"
68
69
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080070#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
71
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080072#ifdef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080073#define HOSTAPD_CTRL_IFACE_PORT 8877
74#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
75#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
76#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
77#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078
79static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020080 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 const char *buf, size_t len);
82
83
84static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080085 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070086 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087{
Roshan Pius3a1667e2018-07-03 15:17:14 -070088 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089}
90
91
92static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080093 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 socklen_t fromlen)
95{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080096 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097}
98
99
100static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800101 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 socklen_t fromlen,
103 char *level)
104{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800105 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106}
107
108
109static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
110 const char *txtaddr)
111{
112 u8 addr[ETH_ALEN];
113 struct sta_info *sta;
114
115 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
116
117 if (hwaddr_aton(txtaddr, addr))
118 return -1;
119
120 sta = ap_get_sta(hapd, addr);
121 if (sta)
122 return 0;
123
124 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
125 "notification", MAC2STR(addr));
126 sta = ap_sta_add(hapd, addr);
127 if (sta == NULL)
128 return -1;
129
130 hostapd_new_assoc_sta(hapd, sta, 0);
131 return 0;
132}
133
134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135#ifdef NEED_AP_MLME
136static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
137 const char *txtaddr)
138{
139 u8 addr[ETH_ALEN];
140 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
141
142 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
143
144 if (hwaddr_aton(txtaddr, addr) ||
145 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
146 return -1;
147
148 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
149
150 return 0;
151}
152#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153
154
155#ifdef CONFIG_WPS
156static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
157{
158 char *pin = os_strchr(txt, ' ');
159 char *timeout_txt;
160 int timeout;
161 u8 addr_buf[ETH_ALEN], *addr = NULL;
162 char *pos;
163
164 if (pin == NULL)
165 return -1;
166 *pin++ = '\0';
167
168 timeout_txt = os_strchr(pin, ' ');
169 if (timeout_txt) {
170 *timeout_txt++ = '\0';
171 timeout = atoi(timeout_txt);
172 pos = os_strchr(timeout_txt, ' ');
173 if (pos) {
174 *pos++ = '\0';
175 if (hwaddr_aton(pos, addr_buf) == 0)
176 addr = addr_buf;
177 }
178 } else
179 timeout = 0;
180
181 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
182}
183
184
185static int hostapd_ctrl_iface_wps_check_pin(
186 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
187{
188 char pin[9];
189 size_t len;
190 char *pos;
191 int ret;
192
193 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
194 (u8 *) cmd, os_strlen(cmd));
195 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
196 if (*pos < '0' || *pos > '9')
197 continue;
198 pin[len++] = *pos;
199 if (len == 9) {
200 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
201 return -1;
202 }
203 }
204 if (len != 4 && len != 8) {
205 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
206 return -1;
207 }
208 pin[len] = '\0';
209
210 if (len == 8) {
211 unsigned int pin_val;
212 pin_val = atoi(pin);
213 if (!wps_pin_valid(pin_val)) {
214 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
215 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800216 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 return -1;
218 return ret;
219 }
220 }
221
222 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800223 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 return -1;
225
226 return ret;
227}
228
229
Dmitry Shmidt04949592012-07-19 12:16:46 -0700230#ifdef CONFIG_WPS_NFC
231static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
232 char *pos)
233{
234 size_t len;
235 struct wpabuf *buf;
236 int ret;
237
238 len = os_strlen(pos);
239 if (len & 0x01)
240 return -1;
241 len /= 2;
242
243 buf = wpabuf_alloc(len);
244 if (buf == NULL)
245 return -1;
246 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
247 wpabuf_free(buf);
248 return -1;
249 }
250
251 ret = hostapd_wps_nfc_tag_read(hapd, buf);
252 wpabuf_free(buf);
253
254 return ret;
255}
256
257
258static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
259 char *cmd, char *reply,
260 size_t max_len)
261{
262 int ndef;
263 struct wpabuf *buf;
264 int res;
265
266 if (os_strcmp(cmd, "WPS") == 0)
267 ndef = 0;
268 else if (os_strcmp(cmd, "NDEF") == 0)
269 ndef = 1;
270 else
271 return -1;
272
273 buf = hostapd_wps_nfc_config_token(hapd, ndef);
274 if (buf == NULL)
275 return -1;
276
277 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
278 wpabuf_len(buf));
279 reply[res++] = '\n';
280 reply[res] = '\0';
281
282 wpabuf_free(buf);
283
284 return res;
285}
286
287
288static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
289 char *reply, size_t max_len,
290 int ndef)
291{
292 struct wpabuf *buf;
293 int res;
294
295 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
296 if (buf == NULL)
297 return -1;
298
299 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
300 wpabuf_len(buf));
301 reply[res++] = '\n';
302 reply[res] = '\0';
303
304 wpabuf_free(buf);
305
306 return res;
307}
308
309
310static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
311 char *cmd, char *reply,
312 size_t max_len)
313{
314 if (os_strcmp(cmd, "WPS") == 0)
315 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
316 max_len, 0);
317
318 if (os_strcmp(cmd, "NDEF") == 0)
319 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
320 max_len, 1);
321
322 if (os_strcmp(cmd, "enable") == 0)
323 return hostapd_wps_nfc_token_enable(hapd);
324
325 if (os_strcmp(cmd, "disable") == 0) {
326 hostapd_wps_nfc_token_disable(hapd);
327 return 0;
328 }
329
330 return -1;
331}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800332
333
334static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
335 char *cmd, char *reply,
336 size_t max_len)
337{
338 struct wpabuf *buf;
339 int res;
340 char *pos;
341 int ndef;
342
343 pos = os_strchr(cmd, ' ');
344 if (pos == NULL)
345 return -1;
346 *pos++ = '\0';
347
348 if (os_strcmp(cmd, "WPS") == 0)
349 ndef = 0;
350 else if (os_strcmp(cmd, "NDEF") == 0)
351 ndef = 1;
352 else
353 return -1;
354
355 if (os_strcmp(pos, "WPS-CR") == 0)
356 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
357 else
358 buf = NULL;
359 if (buf == NULL)
360 return -1;
361
362 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
363 wpabuf_len(buf));
364 reply[res++] = '\n';
365 reply[res] = '\0';
366
367 wpabuf_free(buf);
368
369 return res;
370}
371
372
373static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
374 char *cmd)
375{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800376 size_t len;
377 struct wpabuf *req, *sel;
378 int ret;
379 char *pos, *role, *type, *pos2;
380
381 role = cmd;
382 pos = os_strchr(role, ' ');
383 if (pos == NULL)
384 return -1;
385 *pos++ = '\0';
386
387 type = pos;
388 pos = os_strchr(type, ' ');
389 if (pos == NULL)
390 return -1;
391 *pos++ = '\0';
392
393 pos2 = os_strchr(pos, ' ');
394 if (pos2 == NULL)
395 return -1;
396 *pos2++ = '\0';
397
398 len = os_strlen(pos);
399 if (len & 0x01)
400 return -1;
401 len /= 2;
402
403 req = wpabuf_alloc(len);
404 if (req == NULL)
405 return -1;
406 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
407 wpabuf_free(req);
408 return -1;
409 }
410
411 len = os_strlen(pos2);
412 if (len & 0x01) {
413 wpabuf_free(req);
414 return -1;
415 }
416 len /= 2;
417
418 sel = wpabuf_alloc(len);
419 if (sel == NULL) {
420 wpabuf_free(req);
421 return -1;
422 }
423 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
424 wpabuf_free(req);
425 wpabuf_free(sel);
426 return -1;
427 }
428
429 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
430 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
431 } else {
432 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
433 "reported: role=%s type=%s", role, type);
434 ret = -1;
435 }
436 wpabuf_free(req);
437 wpabuf_free(sel);
438
439 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800440}
441
Dmitry Shmidt04949592012-07-19 12:16:46 -0700442#endif /* CONFIG_WPS_NFC */
443
444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
446 char *buf, size_t buflen)
447{
448 int timeout = 300;
449 char *pos;
450 const char *pin_txt;
451
452 pos = os_strchr(txt, ' ');
453 if (pos)
454 *pos++ = '\0';
455
456 if (os_strcmp(txt, "disable") == 0) {
457 hostapd_wps_ap_pin_disable(hapd);
458 return os_snprintf(buf, buflen, "OK\n");
459 }
460
461 if (os_strcmp(txt, "random") == 0) {
462 if (pos)
463 timeout = atoi(pos);
464 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
465 if (pin_txt == NULL)
466 return -1;
467 return os_snprintf(buf, buflen, "%s", pin_txt);
468 }
469
470 if (os_strcmp(txt, "get") == 0) {
471 pin_txt = hostapd_wps_ap_pin_get(hapd);
472 if (pin_txt == NULL)
473 return -1;
474 return os_snprintf(buf, buflen, "%s", pin_txt);
475 }
476
477 if (os_strcmp(txt, "set") == 0) {
478 char *pin;
479 if (pos == NULL)
480 return -1;
481 pin = pos;
482 pos = os_strchr(pos, ' ');
483 if (pos) {
484 *pos++ = '\0';
485 timeout = atoi(pos);
486 }
487 if (os_strlen(pin) > buflen)
488 return -1;
489 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
490 return -1;
491 return os_snprintf(buf, buflen, "%s", pin);
492 }
493
494 return -1;
495}
496
497
498static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
499{
500 char *pos;
501 char *ssid, *auth, *encr = NULL, *key = NULL;
502
503 ssid = txt;
504 pos = os_strchr(txt, ' ');
505 if (!pos)
506 return -1;
507 *pos++ = '\0';
508
509 auth = pos;
510 pos = os_strchr(pos, ' ');
511 if (pos) {
512 *pos++ = '\0';
513 encr = pos;
514 pos = os_strchr(pos, ' ');
515 if (pos) {
516 *pos++ = '\0';
517 key = pos;
518 }
519 }
520
521 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
522}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700523
524
525static const char * pbc_status_str(enum pbc_status status)
526{
527 switch (status) {
528 case WPS_PBC_STATUS_DISABLE:
529 return "Disabled";
530 case WPS_PBC_STATUS_ACTIVE:
531 return "Active";
532 case WPS_PBC_STATUS_TIMEOUT:
533 return "Timed-out";
534 case WPS_PBC_STATUS_OVERLAP:
535 return "Overlap";
536 default:
537 return "Unknown";
538 }
539}
540
541
542static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
543 char *buf, size_t buflen)
544{
545 int ret;
546 char *pos, *end;
547
548 pos = buf;
549 end = buf + buflen;
550
551 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
552 pbc_status_str(hapd->wps_stats.pbc_status));
553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800554 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700555 return pos - buf;
556 pos += ret;
557
558 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
559 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
560 "Success":
561 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
562 "Failed" : "None")));
563
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800564 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700565 return pos - buf;
566 pos += ret;
567
568 /* If status == Failure - Add possible Reasons */
569 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
570 hapd->wps_stats.failure_reason > 0) {
571 ret = os_snprintf(pos, end - pos,
572 "Failure Reason: %s\n",
573 wps_ei_str(hapd->wps_stats.failure_reason));
574
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800575 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700576 return pos - buf;
577 pos += ret;
578 }
579
580 if (hapd->wps_stats.status) {
581 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
582 MAC2STR(hapd->wps_stats.peer_addr));
583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800584 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700585 return pos - buf;
586 pos += ret;
587 }
588
589 return pos - buf;
590}
591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592#endif /* CONFIG_WPS */
593
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800594#ifdef CONFIG_HS20
595
596static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
597 const char *cmd)
598{
599 u8 addr[ETH_ALEN];
600 const char *url;
601
602 if (hwaddr_aton(cmd, addr))
603 return -1;
604 url = cmd + 17;
605 if (*url == '\0') {
606 url = NULL;
607 } else {
608 if (*url != ' ')
609 return -1;
610 url++;
611 if (*url == '\0')
612 url = NULL;
613 }
614
615 return hs20_send_wnm_notification(hapd, addr, 1, url);
616}
617
618
619static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
620 const char *cmd)
621{
622 u8 addr[ETH_ALEN];
623 int code, reauth_delay, ret;
624 const char *pos;
625 size_t url_len;
626 struct wpabuf *req;
627
628 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
629 if (hwaddr_aton(cmd, addr))
630 return -1;
631
632 pos = os_strchr(cmd, ' ');
633 if (pos == NULL)
634 return -1;
635 pos++;
636 code = atoi(pos);
637
638 pos = os_strchr(pos, ' ');
639 if (pos == NULL)
640 return -1;
641 pos++;
642 reauth_delay = atoi(pos);
643
644 url_len = 0;
645 pos = os_strchr(pos, ' ');
646 if (pos) {
647 pos++;
648 url_len = os_strlen(pos);
649 }
650
651 req = wpabuf_alloc(4 + url_len);
652 if (req == NULL)
653 return -1;
654 wpabuf_put_u8(req, code);
655 wpabuf_put_le16(req, reauth_delay);
656 wpabuf_put_u8(req, url_len);
657 if (pos)
658 wpabuf_put_data(req, pos, url_len);
659
660 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
661 " to indicate imminent deauthentication (code=%d "
662 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
663 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
664 wpabuf_free(req);
665 return ret;
666}
667
668#endif /* CONFIG_HS20 */
669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670
Dmitry Shmidt051af732013-10-22 13:52:46 -0700671#ifdef CONFIG_INTERWORKING
672
673static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
674 const char *cmd)
675{
676 u8 qos_map_set[16 + 2 * 21], count = 0;
677 const char *pos = cmd;
678 int val, ret;
679
680 for (;;) {
681 if (count == sizeof(qos_map_set)) {
682 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
683 return -1;
684 }
685
686 val = atoi(pos);
687 if (val < 0 || val > 255) {
688 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
689 return -1;
690 }
691
692 qos_map_set[count++] = val;
693 pos = os_strchr(pos, ',');
694 if (!pos)
695 break;
696 pos++;
697 }
698
699 if (count < 16 || count & 1) {
700 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
701 return -1;
702 }
703
704 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
705 if (ret) {
706 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
707 return -1;
708 }
709
710 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
711 hapd->conf->qos_map_set_len = count;
712
713 return 0;
714}
715
716
717static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
718 const char *cmd)
719{
720 u8 addr[ETH_ALEN];
721 struct sta_info *sta;
722 struct wpabuf *buf;
723 u8 *qos_map_set = hapd->conf->qos_map_set;
724 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
725 int ret;
726
727 if (!qos_map_set_len) {
728 wpa_printf(MSG_INFO, "QoS Map Set is not set");
729 return -1;
730 }
731
732 if (hwaddr_aton(cmd, addr))
733 return -1;
734
735 sta = ap_get_sta(hapd, addr);
736 if (sta == NULL) {
737 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
738 "for QoS Map Configuration message",
739 MAC2STR(addr));
740 return -1;
741 }
742
743 if (!sta->qos_map_enabled) {
744 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
745 "support for QoS Map", MAC2STR(addr));
746 return -1;
747 }
748
749 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
750 if (buf == NULL)
751 return -1;
752
753 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
754 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
755
756 /* QoS Map Set Element */
757 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
758 wpabuf_put_u8(buf, qos_map_set_len);
759 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
760
761 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
762 wpabuf_head(buf), wpabuf_len(buf));
763 wpabuf_free(buf);
764
765 return ret;
766}
767
768#endif /* CONFIG_INTERWORKING */
769
770
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700771#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800772
773static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
774 const char *cmd)
775{
776 u8 addr[ETH_ALEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800777 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800778 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800779
780 if (hwaddr_aton(cmd, addr))
781 return -1;
782 if (cmd[17] != ' ')
783 return -1;
784 disassoc_timer = atoi(cmd + 17);
785
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800786 sta = ap_get_sta(hapd, addr);
787 if (sta == NULL) {
788 wpa_printf(MSG_DEBUG, "Station " MACSTR
789 " not found for disassociation imminent message",
790 MAC2STR(addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800791 return -1;
792 }
793
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800794 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800795}
796
797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
799 const char *cmd)
800{
801 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700802 const char *url, *timerstr;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700803 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800804 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800805
806 if (hwaddr_aton(cmd, addr))
807 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700808
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800809 sta = ap_get_sta(hapd, addr);
810 if (sta == NULL) {
811 wpa_printf(MSG_DEBUG, "Station " MACSTR
812 " not found for ESS disassociation imminent message",
813 MAC2STR(addr));
814 return -1;
815 }
816
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700817 timerstr = cmd + 17;
818 if (*timerstr != ' ')
819 return -1;
820 timerstr++;
821 disassoc_timer = atoi(timerstr);
822 if (disassoc_timer < 0 || disassoc_timer > 65535)
823 return -1;
824
825 url = os_strchr(timerstr, ' ');
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700826 if (url == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 return -1;
828 url++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800829
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800830 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831}
832
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800833
834static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
835 const char *cmd)
836{
837 u8 addr[ETH_ALEN];
838 const char *pos, *end;
839 int disassoc_timer = 0;
840 struct sta_info *sta;
841 u8 req_mode = 0, valid_int = 0x01;
842 u8 bss_term_dur[12];
843 char *url = NULL;
844 int ret;
845 u8 nei_rep[1000];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700846 int nei_len;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800847 u8 mbo[10];
848 size_t mbo_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800849
850 if (hwaddr_aton(cmd, addr)) {
851 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
852 return -1;
853 }
854
855 sta = ap_get_sta(hapd, addr);
856 if (sta == NULL) {
857 wpa_printf(MSG_DEBUG, "Station " MACSTR
858 " not found for BSS TM Request message",
859 MAC2STR(addr));
860 return -1;
861 }
862
863 pos = os_strstr(cmd, " disassoc_timer=");
864 if (pos) {
865 pos += 16;
866 disassoc_timer = atoi(pos);
867 if (disassoc_timer < 0 || disassoc_timer > 65535) {
868 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
869 return -1;
870 }
871 }
872
873 pos = os_strstr(cmd, " valid_int=");
874 if (pos) {
875 pos += 11;
876 valid_int = atoi(pos);
877 }
878
879 pos = os_strstr(cmd, " bss_term=");
880 if (pos) {
881 pos += 10;
882 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
883 /* TODO: TSF configurable/learnable */
884 bss_term_dur[0] = 4; /* Subelement ID */
885 bss_term_dur[1] = 10; /* Length */
Hai Shalom5f92bc92019-04-18 11:54:11 -0700886 os_memset(&bss_term_dur[2], 0, 8);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800887 end = os_strchr(pos, ',');
888 if (end == NULL) {
889 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
890 return -1;
891 }
892 end++;
893 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
894 }
895
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700896 nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
897 sizeof(nei_rep));
898 if (nei_len < 0)
899 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800900
901 pos = os_strstr(cmd, " url=");
902 if (pos) {
903 size_t len;
904 pos += 5;
905 end = os_strchr(pos, ' ');
906 if (end)
907 len = end - pos;
908 else
909 len = os_strlen(pos);
910 url = os_malloc(len + 1);
911 if (url == NULL)
912 return -1;
913 os_memcpy(url, pos, len);
914 url[len] = '\0';
915 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
916 }
917
918 if (os_strstr(cmd, " pref=1"))
919 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
920 if (os_strstr(cmd, " abridged=1"))
921 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
922 if (os_strstr(cmd, " disassoc_imminent=1"))
923 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
924
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800925#ifdef CONFIG_MBO
926 pos = os_strstr(cmd, "mbo=");
927 if (pos) {
928 unsigned int mbo_reason, cell_pref, reassoc_delay;
929 u8 *mbo_pos = mbo;
930
931 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
932 &reassoc_delay, &cell_pref);
933 if (ret != 3) {
934 wpa_printf(MSG_DEBUG,
935 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800936 ret = -1;
937 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800938 }
939
940 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
941 wpa_printf(MSG_DEBUG,
942 "Invalid MBO transition reason code %u",
943 mbo_reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800944 ret = -1;
945 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800946 }
947
948 /* Valid values for Cellular preference are: 0, 1, 255 */
949 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
950 wpa_printf(MSG_DEBUG,
951 "Invalid MBO cellular capability %u",
952 cell_pref);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800953 ret = -1;
954 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800955 }
956
957 if (reassoc_delay > 65535 ||
958 (reassoc_delay &&
959 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
960 wpa_printf(MSG_DEBUG,
961 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
Dmitry Shmidt29333592017-01-09 12:27:11 -0800962 ret = -1;
963 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800964 }
965
966 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
967 *mbo_pos++ = 1;
968 *mbo_pos++ = mbo_reason;
969 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
970 *mbo_pos++ = 1;
971 *mbo_pos++ = cell_pref;
972
973 if (reassoc_delay) {
974 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
975 *mbo_pos++ = 2;
976 WPA_PUT_LE16(mbo_pos, reassoc_delay);
977 mbo_pos += 2;
978 }
979
980 mbo_len = mbo_pos - mbo;
981 }
982#endif /* CONFIG_MBO */
983
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800984 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
985 valid_int, bss_term_dur, url,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700986 nei_len ? nei_rep : NULL, nei_len,
987 mbo_len ? mbo : NULL, mbo_len);
Paul Stewart092955c2017-02-06 09:13:09 -0800988#ifdef CONFIG_MBO
Dmitry Shmidt29333592017-01-09 12:27:11 -0800989fail:
Paul Stewart092955c2017-02-06 09:13:09 -0800990#endif /* CONFIG_MBO */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800991 os_free(url);
992 return ret;
993}
994
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800995
996static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
997 const char *cmd)
998{
999 u8 addr[ETH_ALEN];
1000 struct sta_info *sta;
1001 const char *pos;
1002 unsigned int auto_report, timeout;
1003
1004 if (hwaddr_aton(cmd, addr)) {
1005 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1006 return -1;
1007 }
1008
1009 sta = ap_get_sta(hapd, addr);
1010 if (!sta) {
1011 wpa_printf(MSG_DEBUG, "Station " MACSTR
1012 " not found for Collocated Interference Request",
1013 MAC2STR(addr));
1014 return -1;
1015 }
1016
1017 pos = cmd + 17;
1018 if (*pos != ' ')
1019 return -1;
1020 pos++;
1021 auto_report = atoi(pos);
1022 pos = os_strchr(pos, ' ');
1023 if (!pos)
1024 return -1;
1025 pos++;
1026 timeout = atoi(pos);
1027
1028 return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
1029}
1030
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001031#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001033
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001034static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1035 char *buf, size_t buflen)
1036{
1037 int ret = 0;
1038 char *pos, *end;
1039
1040 pos = buf;
1041 end = buf + buflen;
1042
1043 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1044
1045 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1046 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1047 if (os_snprintf_error(end - pos, ret))
1048 return pos - buf;
1049 pos += ret;
1050 }
1051 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1052 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1053 if (os_snprintf_error(end - pos, ret))
1054 return pos - buf;
1055 pos += ret;
1056 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001057#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001058 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1059 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1060 if (os_snprintf_error(end - pos, ret))
1061 return pos - buf;
1062 pos += ret;
1063 }
1064 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1065 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1066 if (os_snprintf_error(end - pos, ret))
1067 return pos - buf;
1068 pos += ret;
1069 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001070#ifdef CONFIG_SHA384
1071 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1072 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
1073 if (os_snprintf_error(end - pos, ret))
1074 return pos - buf;
1075 pos += ret;
1076 }
1077#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001078#ifdef CONFIG_SAE
1079 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1080 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1081 if (os_snprintf_error(end - pos, ret))
1082 return pos - buf;
1083 pos += ret;
1084 }
1085#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001086#ifdef CONFIG_FILS
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1088 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1089 if (os_snprintf_error(end - pos, ret))
1090 return pos - buf;
1091 pos += ret;
1092 }
1093 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1094 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1095 if (os_snprintf_error(end - pos, ret))
1096 return pos - buf;
1097 pos += ret;
1098 }
1099#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001100#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001101 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1102 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1103 if (os_snprintf_error(end - pos, ret))
1104 return pos - buf;
1105 pos += ret;
1106 }
1107 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1108 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1109 if (os_snprintf_error(end - pos, ret))
1110 return pos - buf;
1111 pos += ret;
1112 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001113#ifdef CONFIG_SAE
1114 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1115 ret = os_snprintf(pos, end - pos, "SAE ");
1116 if (os_snprintf_error(end - pos, ret))
1117 return pos - buf;
1118 pos += ret;
1119 }
1120#endif /* CONFIG_SAE */
1121 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1122 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1123 if (os_snprintf_error(end - pos, ret))
1124 return pos - buf;
1125 pos += ret;
1126 }
1127 if (hapd->conf->wpa_key_mgmt &
1128 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1129 ret = os_snprintf(pos, end - pos,
1130 "WPA-EAP-SUITE-B-192 ");
1131 if (os_snprintf_error(end - pos, ret))
1132 return pos - buf;
1133 pos += ret;
1134 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001135#ifdef CONFIG_FILS
1136 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1137 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1138 if (os_snprintf_error(end - pos, ret))
1139 return pos - buf;
1140 pos += ret;
1141 }
1142 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1143 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1144 if (os_snprintf_error(end - pos, ret))
1145 return pos - buf;
1146 pos += ret;
1147 }
1148#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001149
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001150#ifdef CONFIG_OWE
1151 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
1152 ret = os_snprintf(pos, end - pos, "OWE ");
1153 if (os_snprintf_error(end - pos, ret))
1154 return pos - buf;
1155 pos += ret;
1156 }
1157#endif /* CONFIG_OWE */
1158
1159#ifdef CONFIG_DPP
1160 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
1161 ret = os_snprintf(pos, end - pos, "DPP ");
1162 if (os_snprintf_error(end - pos, ret))
1163 return pos - buf;
1164 pos += ret;
1165 }
1166#endif /* CONFIG_DPP */
1167
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001168 if (pos > buf && *(pos - 1) == ' ') {
1169 *(pos - 1) = '\0';
1170 pos--;
1171 }
1172
1173 return pos - buf;
1174}
1175
1176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1178 char *buf, size_t buflen)
1179{
1180 int ret;
1181 char *pos, *end;
1182
1183 pos = buf;
1184 end = buf + buflen;
1185
1186 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1187 "ssid=%s\n",
1188 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001189 wpa_ssid_txt(hapd->conf->ssid.ssid,
1190 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001191 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 return pos - buf;
1193 pos += ret;
1194
1195#ifdef CONFIG_WPS
1196 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1197 hapd->conf->wps_state == 0 ? "disabled" :
1198 (hapd->conf->wps_state == 1 ? "not configured" :
1199 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001200 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 return pos - buf;
1202 pos += ret;
1203
1204 if (hapd->conf->wps_state && hapd->conf->wpa &&
1205 hapd->conf->ssid.wpa_passphrase) {
1206 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1207 hapd->conf->ssid.wpa_passphrase);
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
1213 if (hapd->conf->wps_state && hapd->conf->wpa &&
1214 hapd->conf->ssid.wpa_psk &&
1215 hapd->conf->ssid.wpa_psk->group) {
1216 char hex[PMK_LEN * 2 + 1];
1217 wpa_snprintf_hex(hex, sizeof(hex),
1218 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1219 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001220 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 return pos - buf;
1222 pos += ret;
1223 }
1224#endif /* CONFIG_WPS */
1225
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001226 if (hapd->conf->wpa) {
1227 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1228 if (os_snprintf_error(end - pos, ret))
1229 return pos - buf;
1230 pos += ret;
1231 }
1232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1234 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001235 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 return pos - buf;
1237 pos += ret;
1238
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001239 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240
1241 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001242 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 return pos - buf;
1244 pos += ret;
1245 }
1246
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001247 if (hapd->conf->wpa) {
1248 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1249 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001250 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 return pos - buf;
1252 pos += ret;
1253 }
1254
1255 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1256 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001257 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 return pos - buf;
1259 pos += ret;
1260
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001261 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1262 " ");
1263 if (ret < 0)
1264 return pos - buf;
1265 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266
1267 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001268 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 return pos - buf;
1270 pos += ret;
1271 }
1272
1273 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1274 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001275 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 return pos - buf;
1277 pos += ret;
1278
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001279 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001280 " ");
1281 if (ret < 0)
1282 return pos - buf;
1283 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
1285 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001286 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 return pos - buf;
1288 pos += ret;
1289 }
1290
Hai Shalomfdcde762020-04-02 11:19:20 -07001291 if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1292 ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1293 hapd->conf->wpa_deny_ptk0_rekey);
1294 if (os_snprintf_error(end - pos, ret))
1295 return pos - buf;
1296 pos += ret;
1297 }
1298
1299 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1300 ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1301 hapd->conf->extended_key_id);
1302 if (os_snprintf_error(end - pos, ret))
1303 return pos - buf;
1304 pos += ret;
1305 }
1306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 return pos - buf;
1308}
1309
1310
Roshan Pius3a1667e2018-07-03 15:17:14 -07001311static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1312{
1313 struct sta_info *sta;
1314 struct vlan_description vlan_id;
1315
1316 if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1317 return;
1318
1319 for (sta = hapd->sta_list; sta; sta = sta->next) {
1320 if (!hostapd_maclist_found(hapd->conf->accept_mac,
1321 hapd->conf->num_accept_mac,
1322 sta->addr, &vlan_id) ||
1323 (vlan_id.notempty &&
1324 vlan_compare(&vlan_id, sta->vlan_desc)))
1325 ap_sta_disconnect(hapd, sta, sta->addr,
1326 WLAN_REASON_UNSPECIFIED);
1327 }
1328}
1329
1330
1331static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1332{
1333 struct sta_info *sta;
1334 struct vlan_description vlan_id;
1335
1336 for (sta = hapd->sta_list; sta; sta = sta->next) {
1337 if (hostapd_maclist_found(hapd->conf->deny_mac,
1338 hapd->conf->num_deny_mac, sta->addr,
1339 &vlan_id) &&
1340 (!vlan_id.notempty ||
1341 !vlan_compare(&vlan_id, sta->vlan_desc)))
1342 ap_sta_disconnect(hapd, sta, sta->addr,
1343 WLAN_REASON_UNSPECIFIED);
1344 }
1345}
1346
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001347
1348static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
1349 const char *band)
1350{
1351 union wpa_event_data event;
1352 enum set_band setband;
1353
1354 if (os_strcmp(band, "AUTO") == 0)
1355 setband = WPA_SETBAND_AUTO;
1356 else if (os_strcmp(band, "5G") == 0)
1357 setband = WPA_SETBAND_5G;
1358 else if (os_strcmp(band, "2G") == 0)
1359 setband = WPA_SETBAND_2G;
1360 else
1361 return -1;
1362
1363 if (hostapd_drv_set_band(hapd, setband) == 0) {
1364 os_memset(&event, 0, sizeof(event));
1365 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1366 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1367 wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1368 }
1369
1370 return 0;
1371}
1372
1373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1375{
1376 char *value;
1377 int ret = 0;
1378
1379 value = os_strchr(cmd, ' ');
1380 if (value == NULL)
1381 return -1;
1382 *value++ = '\0';
1383
1384 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1385 if (0) {
1386#ifdef CONFIG_WPS_TESTING
1387 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1388 long int val;
1389 val = strtol(value, NULL, 0);
1390 if (val < 0 || val > 0xff) {
1391 ret = -1;
1392 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1393 "wps_version_number %ld", val);
1394 } else {
1395 wps_version_number = val;
1396 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1397 "version %u.%u",
1398 (wps_version_number & 0xf0) >> 4,
1399 wps_version_number & 0x0f);
1400 hostapd_wps_update_ie(hapd);
1401 }
1402 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1403 wps_testing_dummy_cred = atoi(value);
1404 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1405 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001406 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1407 wps_corrupt_pkhash = atoi(value);
1408 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1409 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001411#ifdef CONFIG_TESTING_OPTIONS
1412 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1413 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001414 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1415 hapd->ext_eapol_frame_io = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001416#ifdef CONFIG_DPP
1417 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1418 os_free(hapd->dpp_config_obj_override);
1419 hapd->dpp_config_obj_override = os_strdup(value);
1420 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1421 os_free(hapd->dpp_discovery_override);
1422 hapd->dpp_discovery_override = os_strdup(value);
1423 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1424 os_free(hapd->dpp_groups_override);
1425 hapd->dpp_groups_override = os_strdup(value);
1426 } else if (os_strcasecmp(cmd,
1427 "dpp_ignore_netaccesskey_mismatch") == 0) {
1428 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001429 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1430 dpp_test = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001431#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001432#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001433#ifdef CONFIG_MBO
1434 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1435 int val;
1436
1437 if (!hapd->conf->mbo_enabled)
1438 return -1;
1439
1440 val = atoi(value);
1441 if (val < 0 || val > 1)
1442 return -1;
1443
1444 hapd->mbo_assoc_disallow = val;
1445 ieee802_11_update_beacons(hapd->iface);
1446
1447 /*
1448 * TODO: Need to configure drivers that do AP MLME offload with
1449 * disallowing station logic.
1450 */
1451#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001452#ifdef CONFIG_DPP
1453 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1454 os_free(hapd->dpp_configurator_params);
1455 hapd->dpp_configurator_params = os_strdup(value);
1456#endif /* CONFIG_DPP */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001457 } else if (os_strcasecmp(cmd, "setband") == 0) {
1458 ret = hostapd_ctrl_iface_set_band(hapd, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001460 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001461 if (ret)
1462 return ret;
1463
1464 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001465 hostapd_disassoc_deny_mac(hapd);
1466 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1467 hostapd_disassoc_accept_mac(hapd);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001468 } else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1469 os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1470 hapd->parameter_set_count++;
1471 if (ieee802_11_update_beacons(hapd->iface))
1472 wpa_printf(MSG_DEBUG,
1473 "Failed to update beacons with WMM parameters");
Hai Shalomc3565922019-10-28 11:58:20 -07001474 } else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1475 os_strcmp(cmd, "sae_password") == 0 ||
1476 os_strcmp(cmd, "sae_pwe") == 0) {
1477 if (hapd->started)
1478 hostapd_setup_sae_pt(hapd->conf);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001479 }
Hai Shalomb755a2a2020-04-23 21:49:02 -07001480
1481#ifdef CONFIG_TESTING_OPTIONS
1482 if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1483 wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1484 hapd->conf->ft_rsnxe_used);
1485#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 }
1487
1488 return ret;
1489}
1490
1491
1492static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1493 char *buf, size_t buflen)
1494{
1495 int res;
1496
1497 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1498
1499 if (os_strcmp(cmd, "version") == 0) {
1500 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001501 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 return -1;
1503 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001504 } else if (os_strcmp(cmd, "tls_library") == 0) {
1505 res = tls_get_library_version(buf, buflen);
1506 if (os_snprintf_error(buflen, res))
1507 return -1;
1508 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 }
1510
1511 return -1;
1512}
1513
1514
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001515static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1516{
1517 if (hostapd_enable_iface(iface) < 0) {
1518 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1519 return -1;
1520 }
1521 return 0;
1522}
1523
1524
1525static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1526{
1527 if (hostapd_reload_iface(iface) < 0) {
1528 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1529 return -1;
1530 }
1531 return 0;
1532}
1533
1534
1535static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1536{
1537 if (hostapd_disable_iface(iface) < 0) {
1538 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1539 return -1;
1540 }
1541 return 0;
1542}
1543
1544
Hai Shalom74f70d42019-02-11 14:42:39 -08001545static int
1546hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1547 struct sta_info *sta, void *ctx)
1548{
1549 struct hostapd_wpa_psk *psk;
1550 const u8 *pmk;
1551 int pmk_len;
1552 int pmk_match;
1553 int sta_match;
1554 int bss_match;
1555 int reason;
1556
1557 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1558
1559 for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1560 pmk_match = PMK_LEN == pmk_len &&
1561 os_memcmp(psk->psk, pmk, pmk_len) == 0;
1562 sta_match = psk->group == 0 &&
1563 os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1564 bss_match = psk->group == 1;
1565
1566 if (pmk_match && (sta_match || bss_match))
1567 return 0;
1568 }
1569
1570 wpa_printf(MSG_INFO, "STA " MACSTR
1571 " PSK/passphrase no longer valid - disconnect",
1572 MAC2STR(sta->addr));
1573 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1574 hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1575 ap_sta_deauthenticate(hapd, sta, reason);
1576
1577 return 0;
1578}
1579
1580
1581static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1582{
1583 struct hostapd_bss_config *conf = hapd->conf;
1584 int err;
1585
1586 hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1587
1588 err = hostapd_setup_wpa_psk(conf);
1589 if (err < 0) {
1590 wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1591 err);
1592 return -1;
1593 }
1594
1595 ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1596 NULL);
1597
1598 return 0;
1599}
1600
1601
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001602#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001603
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001604static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1605{
1606 union wpa_event_data data;
1607 char *pos, *param;
1608 enum wpa_event_type event;
1609
1610 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1611
1612 os_memset(&data, 0, sizeof(data));
1613
1614 param = os_strchr(cmd, ' ');
1615 if (param == NULL)
1616 return -1;
1617 *param++ = '\0';
1618
1619 if (os_strcmp(cmd, "DETECTED") == 0)
1620 event = EVENT_DFS_RADAR_DETECTED;
1621 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1622 event = EVENT_DFS_CAC_FINISHED;
1623 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1624 event = EVENT_DFS_CAC_ABORTED;
1625 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1626 event = EVENT_DFS_NOP_FINISHED;
1627 else {
1628 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1629 cmd);
1630 return -1;
1631 }
1632
1633 pos = os_strstr(param, "freq=");
1634 if (pos)
1635 data.dfs_event.freq = atoi(pos + 5);
1636
1637 pos = os_strstr(param, "ht_enabled=1");
1638 if (pos)
1639 data.dfs_event.ht_enabled = 1;
1640
1641 pos = os_strstr(param, "chan_offset=");
1642 if (pos)
1643 data.dfs_event.chan_offset = atoi(pos + 12);
1644
1645 pos = os_strstr(param, "chan_width=");
1646 if (pos)
1647 data.dfs_event.chan_width = atoi(pos + 11);
1648
1649 pos = os_strstr(param, "cf1=");
1650 if (pos)
1651 data.dfs_event.cf1 = atoi(pos + 4);
1652
1653 pos = os_strstr(param, "cf2=");
1654 if (pos)
1655 data.dfs_event.cf2 = atoi(pos + 4);
1656
1657 wpa_supplicant_event(hapd, event, &data);
1658
1659 return 0;
1660}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001661
1662
1663static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1664{
1665 size_t len;
1666 u8 *buf;
1667 int res;
1668
1669 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1670
1671 len = os_strlen(cmd);
1672 if (len & 1)
1673 return -1;
1674 len /= 2;
1675
1676 buf = os_malloc(len);
1677 if (buf == NULL)
1678 return -1;
1679
1680 if (hexstr2bin(cmd, buf, len) < 0) {
1681 os_free(buf);
1682 return -1;
1683 }
1684
Hai Shalomfdcde762020-04-02 11:19:20 -07001685 res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001686 os_free(buf);
1687 return res;
1688}
1689
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001690
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001691static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1692 char *cmd)
1693{
1694 char *pos, *param;
1695 size_t len;
1696 u8 *buf;
1697 int stype = 0, ok = 0;
1698 union wpa_event_data event;
1699
1700 if (!hapd->ext_mgmt_frame_handling)
1701 return -1;
1702
1703 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1704
1705 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1706
1707 pos = cmd;
1708 param = os_strstr(pos, "stype=");
1709 if (param) {
1710 param += 6;
1711 stype = atoi(param);
1712 }
1713
1714 param = os_strstr(pos, " ok=");
1715 if (param) {
1716 param += 4;
1717 ok = atoi(param);
1718 }
1719
1720 param = os_strstr(pos, " buf=");
1721 if (!param)
1722 return -1;
1723 param += 5;
1724
1725 len = os_strlen(param);
1726 if (len & 1)
1727 return -1;
1728 len /= 2;
1729
1730 buf = os_malloc(len);
1731 if (!buf || hexstr2bin(param, buf, len) < 0) {
1732 os_free(buf);
1733 return -1;
1734 }
1735
1736 os_memset(&event, 0, sizeof(event));
1737 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1738 event.tx_status.data = buf;
1739 event.tx_status.data_len = len;
1740 event.tx_status.stype = stype;
1741 event.tx_status.ack = ok;
1742 hapd->ext_mgmt_frame_handling = 0;
1743 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1744 hapd->ext_mgmt_frame_handling = 1;
1745
1746 os_free(buf);
1747
1748 return 0;
1749}
1750
1751
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001752static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1753 char *cmd)
1754{
1755 char *pos, *param;
1756 size_t len;
1757 u8 *buf;
1758 int freq = 0, datarate = 0, ssi_signal = 0;
1759 union wpa_event_data event;
1760
1761 if (!hapd->ext_mgmt_frame_handling)
1762 return -1;
1763
1764 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1765
1766 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1767
1768 pos = cmd;
1769 param = os_strstr(pos, "freq=");
1770 if (param) {
1771 param += 5;
1772 freq = atoi(param);
1773 }
1774
1775 param = os_strstr(pos, " datarate=");
1776 if (param) {
1777 param += 10;
1778 datarate = atoi(param);
1779 }
1780
1781 param = os_strstr(pos, " ssi_signal=");
1782 if (param) {
1783 param += 12;
1784 ssi_signal = atoi(param);
1785 }
1786
1787 param = os_strstr(pos, " frame=");
1788 if (param == NULL)
1789 return -1;
1790 param += 7;
1791
1792 len = os_strlen(param);
1793 if (len & 1)
1794 return -1;
1795 len /= 2;
1796
1797 buf = os_malloc(len);
1798 if (buf == NULL)
1799 return -1;
1800
1801 if (hexstr2bin(param, buf, len) < 0) {
1802 os_free(buf);
1803 return -1;
1804 }
1805
1806 os_memset(&event, 0, sizeof(event));
1807 event.rx_mgmt.freq = freq;
1808 event.rx_mgmt.frame = buf;
1809 event.rx_mgmt.frame_len = len;
1810 event.rx_mgmt.ssi_signal = ssi_signal;
1811 event.rx_mgmt.datarate = datarate;
1812 hapd->ext_mgmt_frame_handling = 0;
1813 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1814 hapd->ext_mgmt_frame_handling = 1;
1815
1816 os_free(buf);
1817
1818 return 0;
1819}
1820
1821
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001822static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1823{
1824 char *pos;
1825 u8 src[ETH_ALEN], *buf;
1826 int used;
1827 size_t len;
1828
1829 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1830
1831 pos = cmd;
1832 used = hwaddr_aton2(pos, src);
1833 if (used < 0)
1834 return -1;
1835 pos += used;
1836 while (*pos == ' ')
1837 pos++;
1838
1839 len = os_strlen(pos);
1840 if (len & 1)
1841 return -1;
1842 len /= 2;
1843
1844 buf = os_malloc(len);
1845 if (buf == NULL)
1846 return -1;
1847
1848 if (hexstr2bin(pos, buf, len) < 0) {
1849 os_free(buf);
1850 return -1;
1851 }
1852
1853 ieee802_1x_receive(hapd, src, buf, len);
1854 os_free(buf);
1855
1856 return 0;
1857}
1858
1859
1860static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1861{
1862 size_t i;
1863 u32 sum = 0;
1864 const u16 *pos = buf;
1865
1866 for (i = 0; i < len / 2; i++)
1867 sum += *pos++;
1868
1869 while (sum >> 16)
1870 sum = (sum & 0xffff) + (sum >> 16);
1871
1872 return sum ^ 0xffff;
1873}
1874
1875
1876#define HWSIM_PACKETLEN 1500
1877#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1878
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001879static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1880 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001881{
1882 struct hostapd_data *hapd = ctx;
1883 const struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001884 struct ip ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885 const u8 *pos;
1886 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001887 char extra[30];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001888
Hai Shalom81f62d82019-07-22 12:10:00 -07001889 if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
1890 wpa_printf(MSG_DEBUG,
1891 "test data: RX - ignore unexpected length %d",
1892 (int) len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001894 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001895
1896 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001897 os_memcpy(&ip, eth + 1, sizeof(ip));
1898 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001899
Hai Shalomfdcde762020-04-02 11:19:20 -07001900 if (ip.ip_hl != 5 || ip.ip_v != 4 ||
1901 ntohs(ip.ip_len) > HWSIM_IP_LEN) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001902 wpa_printf(MSG_DEBUG,
1903 "test data: RX - ignore unexpect IP header");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001905 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001906
Hai Shalomfdcde762020-04-02 11:19:20 -07001907 for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001908 if (*pos != (u8) i) {
1909 wpa_printf(MSG_DEBUG,
1910 "test data: RX - ignore mismatching payload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001911 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001912 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001913 pos++;
1914 }
1915
Hai Shalom81f62d82019-07-22 12:10:00 -07001916 extra[0] = '\0';
Hai Shalomfdcde762020-04-02 11:19:20 -07001917 if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
1918 os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
Hai Shalom81f62d82019-07-22 12:10:00 -07001919 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
1920 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001921}
1922
1923
1924static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1925 char *cmd)
1926{
1927 int enabled = atoi(cmd);
1928 char *pos;
1929 const char *ifname;
1930
1931 if (!enabled) {
1932 if (hapd->l2_test) {
1933 l2_packet_deinit(hapd->l2_test);
1934 hapd->l2_test = NULL;
1935 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1936 "test data: Disabled");
1937 }
1938 return 0;
1939 }
1940
1941 if (hapd->l2_test)
1942 return 0;
1943
1944 pos = os_strstr(cmd, " ifname=");
1945 if (pos)
1946 ifname = pos + 8;
1947 else
1948 ifname = hapd->conf->iface;
1949
1950 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1951 ETHERTYPE_IP, hostapd_data_test_rx,
1952 hapd, 1);
1953 if (hapd->l2_test == NULL)
1954 return -1;
1955
1956 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1957
1958 return 0;
1959}
1960
1961
1962static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1963{
1964 u8 dst[ETH_ALEN], src[ETH_ALEN];
Hai Shalom81f62d82019-07-22 12:10:00 -07001965 char *pos, *pos2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001966 int used;
1967 long int val;
1968 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001969 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001970 struct ether_header *eth;
Hai Shalomfdcde762020-04-02 11:19:20 -07001971 struct ip *ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001972 u8 *dpos;
1973 unsigned int i;
Hai Shalom81f62d82019-07-22 12:10:00 -07001974 size_t send_len = HWSIM_IP_LEN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001975
1976 if (hapd->l2_test == NULL)
1977 return -1;
1978
Hai Shalom81f62d82019-07-22 12:10:00 -07001979 /* format: <dst> <src> <tos> [len=<length>] */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001980
1981 pos = cmd;
1982 used = hwaddr_aton2(pos, dst);
1983 if (used < 0)
1984 return -1;
1985 pos += used;
1986 while (*pos == ' ')
1987 pos++;
1988 used = hwaddr_aton2(pos, src);
1989 if (used < 0)
1990 return -1;
1991 pos += used;
1992
Hai Shalom81f62d82019-07-22 12:10:00 -07001993 val = strtol(pos, &pos2, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001994 if (val < 0 || val > 0xff)
1995 return -1;
1996 tos = val;
1997
Hai Shalom81f62d82019-07-22 12:10:00 -07001998 pos = os_strstr(pos2, " len=");
1999 if (pos) {
2000 i = atoi(pos + 5);
2001 if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2002 return -1;
2003 send_len = i;
2004 }
2005
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002006 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002007 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2008 os_memcpy(eth->ether_shost, src, ETH_ALEN);
2009 eth->ether_type = htons(ETHERTYPE_IP);
Hai Shalomfdcde762020-04-02 11:19:20 -07002010 ip = (struct ip *) (eth + 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011 os_memset(ip, 0, sizeof(*ip));
Hai Shalomfdcde762020-04-02 11:19:20 -07002012 ip->ip_hl = 5;
2013 ip->ip_v = 4;
2014 ip->ip_ttl = 64;
2015 ip->ip_tos = tos;
2016 ip->ip_len = htons(send_len);
2017 ip->ip_p = 1;
2018 ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2019 ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2020 ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002021 dpos = (u8 *) (ip + 1);
Hai Shalom81f62d82019-07-22 12:10:00 -07002022 for (i = 0; i < send_len - sizeof(*ip); i++)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002023 *dpos++ = i;
2024
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002025 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Hai Shalom81f62d82019-07-22 12:10:00 -07002026 sizeof(struct ether_header) + send_len) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002027 return -1;
2028
2029 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2030 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2031
2032 return 0;
2033}
2034
2035
2036static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2037 char *cmd)
2038{
2039 u8 *buf;
2040 struct ether_header *eth;
2041 struct l2_packet_data *l2 = NULL;
2042 size_t len;
2043 u16 ethertype;
2044 int res = -1;
2045 const char *ifname = hapd->conf->iface;
2046
2047 if (os_strncmp(cmd, "ifname=", 7) == 0) {
2048 cmd += 7;
2049 ifname = cmd;
2050 cmd = os_strchr(cmd, ' ');
2051 if (cmd == NULL)
2052 return -1;
2053 *cmd++ = '\0';
2054 }
2055
2056 len = os_strlen(cmd);
2057 if (len & 1 || len < ETH_HLEN * 2)
2058 return -1;
2059 len /= 2;
2060
2061 buf = os_malloc(len);
2062 if (buf == NULL)
2063 return -1;
2064
2065 if (hexstr2bin(cmd, buf, len) < 0)
2066 goto done;
2067
2068 eth = (struct ether_header *) buf;
2069 ethertype = ntohs(eth->ether_type);
2070
2071 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2072 hostapd_data_test_rx, hapd, 1);
2073 if (l2 == NULL)
2074 goto done;
2075
2076 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2077 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2078done:
2079 if (l2)
2080 l2_packet_deinit(l2);
2081 os_free(buf);
2082
2083 return res < 0 ? -1 : 0;
2084}
2085
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002086
2087static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2088{
2089#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002090 char *pos;
2091
2092 wpa_trace_fail_after = atoi(cmd);
2093 pos = os_strchr(cmd, ':');
2094 if (pos) {
2095 pos++;
2096 os_strlcpy(wpa_trace_fail_func, pos,
2097 sizeof(wpa_trace_fail_func));
2098 } else {
2099 wpa_trace_fail_after = 0;
2100 }
2101
2102 return 0;
2103#else /* WPA_TRACE_BFD */
2104 return -1;
2105#endif /* WPA_TRACE_BFD */
2106}
2107
2108
2109static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2110 char *buf, size_t buflen)
2111{
2112#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002113 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2114 wpa_trace_fail_func);
2115#else /* WPA_TRACE_BFD */
2116 return -1;
2117#endif /* WPA_TRACE_BFD */
2118}
2119
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002120
2121static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2122{
2123#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002124 char *pos;
2125
2126 wpa_trace_test_fail_after = atoi(cmd);
2127 pos = os_strchr(cmd, ':');
2128 if (pos) {
2129 pos++;
2130 os_strlcpy(wpa_trace_test_fail_func, pos,
2131 sizeof(wpa_trace_test_fail_func));
2132 } else {
2133 wpa_trace_test_fail_after = 0;
2134 }
2135
2136 return 0;
2137#else /* WPA_TRACE_BFD */
2138 return -1;
2139#endif /* WPA_TRACE_BFD */
2140}
2141
2142
2143static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2144 char *buf, size_t buflen)
2145{
2146#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002147 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2148 wpa_trace_test_fail_func);
2149#else /* WPA_TRACE_BFD */
2150 return -1;
2151#endif /* WPA_TRACE_BFD */
2152}
2153
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002154
2155static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2156{
2157 struct sta_info *sta;
2158 u8 addr[ETH_ALEN];
2159 u8 zero[WPA_TK_MAX_LEN];
2160
2161 os_memset(zero, 0, sizeof(zero));
2162
2163 if (hwaddr_aton(cmd, addr))
2164 return -1;
2165
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002166 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2167 if (hapd->last_igtk_alg == WPA_ALG_NONE)
2168 return -1;
2169
2170 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2171
2172 /* First, use a zero key to avoid any possible duplicate key
2173 * avoidance in the driver. */
2174 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2175 hapd->last_igtk_alg,
2176 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002177 hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2178 zero, hapd->last_igtk_len,
2179 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002180 return -1;
2181
2182 /* Set the previously configured key to reset its TSC */
2183 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2184 hapd->last_igtk_alg,
2185 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002186 hapd->last_igtk_key_idx, 0, 1, NULL,
2187 0, hapd->last_igtk,
2188 hapd->last_igtk_len,
2189 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002190 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002191
2192 if (is_broadcast_ether_addr(addr)) {
2193 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2194 return -1;
2195
2196 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2197
2198 /* First, use a zero key to avoid any possible duplicate key
2199 * avoidance in the driver. */
2200 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2201 hapd->last_gtk_alg,
2202 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002203 hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2204 zero, hapd->last_gtk_len,
2205 KEY_FLAG_GROUP_TX_DEFAULT) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002206 return -1;
2207
2208 /* Set the previously configured key to reset its TSC */
2209 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2210 hapd->last_gtk_alg,
2211 broadcast_ether_addr,
Hai Shalomfdcde762020-04-02 11:19:20 -07002212 hapd->last_gtk_key_idx, 0, 1, NULL,
2213 0, hapd->last_gtk,
2214 hapd->last_gtk_len,
2215 KEY_FLAG_GROUP_TX_DEFAULT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216 }
2217
2218 sta = ap_get_sta(hapd, addr);
2219 if (!sta)
2220 return -1;
2221
2222 if (sta->last_tk_alg == WPA_ALG_NONE)
2223 return -1;
2224
2225 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2226 MAC2STR(sta->addr));
2227
2228 /* First, use a zero key to avoid any possible duplicate key avoidance
2229 * in the driver. */
2230 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002231 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2232 zero, sta->last_tk_len,
2233 KEY_FLAG_PAIRWISE_RX_TX) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002234 return -1;
2235
2236 /* Set the previously configured key to reset its TSC/RSC */
2237 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002238 sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2239 0, sta->last_tk, sta->last_tk_len,
2240 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002241}
2242
2243
2244static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2245{
2246 u8 addr[ETH_ALEN];
2247 const char *pos = cmd;
2248 enum wpa_alg alg;
Hai Shalomfdcde762020-04-02 11:19:20 -07002249 enum key_flag key_flag;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002250 int idx, set_tx;
2251 u8 seq[6], key[WPA_TK_MAX_LEN];
2252 size_t key_len;
2253
Hai Shalomfdcde762020-04-02 11:19:20 -07002254 /* parameters: alg addr idx set_tx seq key key_flag */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002255
2256 alg = atoi(pos);
2257 pos = os_strchr(pos, ' ');
2258 if (!pos)
2259 return -1;
2260 pos++;
2261 if (hwaddr_aton(pos, addr))
2262 return -1;
2263 pos += 17;
2264 if (*pos != ' ')
2265 return -1;
2266 pos++;
2267 idx = atoi(pos);
2268 pos = os_strchr(pos, ' ');
2269 if (!pos)
2270 return -1;
2271 pos++;
2272 set_tx = atoi(pos);
2273 pos = os_strchr(pos, ' ');
2274 if (!pos)
2275 return -1;
2276 pos++;
Hai Shalom74f70d42019-02-11 14:42:39 -08002277 if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002278 return -1;
2279 pos += 2 * 6;
2280 if (*pos != ' ')
2281 return -1;
2282 pos++;
Hai Shalomfdcde762020-04-02 11:19:20 -07002283 if (!os_strchr(pos, ' '))
2284 return -1;
2285 key_len = (os_strchr(pos, ' ') - pos) / 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002286 if (hexstr2bin(pos, key, key_len) < 0)
2287 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07002288 pos += 2 * key_len;
2289 if (*pos != ' ')
2290 return -1;
2291
2292 pos++;
2293 key_flag = atoi(pos);
2294 pos = os_strchr(pos, ' ');
2295 if (pos)
2296 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002297
2298 wpa_printf(MSG_INFO, "TESTING: Set key");
Hai Shalomfdcde762020-04-02 11:19:20 -07002299 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2300 set_tx, seq, 6, key, key_len, key_flag);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002301}
2302
2303
2304static void restore_tk(void *ctx1, void *ctx2)
2305{
2306 struct hostapd_data *hapd = ctx1;
2307 struct sta_info *sta = ctx2;
2308
2309 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2310 MAC2STR(sta->addr));
2311 /* This does not really restore the TSC properly, so this will result
2312 * in replay protection issues for now since there is no clean way of
2313 * preventing encryption of a single EAPOL frame. */
2314 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
Hai Shalomfdcde762020-04-02 11:19:20 -07002315 sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2316 sta->last_tk, sta->last_tk_len,
2317 KEY_FLAG_PAIRWISE_RX_TX);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002318}
2319
2320
2321static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2322{
2323 struct sta_info *sta;
2324 u8 addr[ETH_ALEN];
2325 int plain = os_strstr(cmd, "plaintext") != NULL;
2326
2327 if (hwaddr_aton(cmd, addr))
2328 return -1;
2329
2330 sta = ap_get_sta(hapd, addr);
2331 if (!sta || !sta->wpa_sm)
2332 return -1;
2333
2334 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2335 plain = 0; /* no need for special processing */
2336 if (plain) {
2337 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2338 MAC2STR(sta->addr));
2339 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002340 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2341 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002342 }
2343
2344 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2345 return wpa_auth_resend_m1(sta->wpa_sm,
2346 os_strstr(cmd, "change-anonce") != NULL,
2347 plain ? restore_tk : NULL, hapd, sta);
2348}
2349
2350
2351static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2352{
2353 struct sta_info *sta;
2354 u8 addr[ETH_ALEN];
2355 int plain = os_strstr(cmd, "plaintext") != NULL;
2356
2357 if (hwaddr_aton(cmd, addr))
2358 return -1;
2359
2360 sta = ap_get_sta(hapd, addr);
2361 if (!sta || !sta->wpa_sm)
2362 return -1;
2363
2364 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2365 plain = 0; /* no need for special processing */
2366 if (plain) {
2367 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2368 MAC2STR(sta->addr));
2369 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002370 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2371 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002372 }
2373
2374 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2375 return wpa_auth_resend_m3(sta->wpa_sm,
2376 plain ? restore_tk : NULL, hapd, sta);
2377}
2378
2379
2380static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2381 const char *cmd)
2382{
2383 struct sta_info *sta;
2384 u8 addr[ETH_ALEN];
2385 int plain = os_strstr(cmd, "plaintext") != NULL;
2386
2387 if (hwaddr_aton(cmd, addr))
2388 return -1;
2389
2390 sta = ap_get_sta(hapd, addr);
2391 if (!sta || !sta->wpa_sm)
2392 return -1;
2393
2394 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2395 plain = 0; /* no need for special processing */
2396 if (plain) {
2397 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2398 MAC2STR(sta->addr));
2399 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
Hai Shalomfdcde762020-04-02 11:19:20 -07002400 sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2401 0, NULL, 0, KEY_FLAG_PAIRWISE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002402 }
2403
2404 wpa_printf(MSG_INFO,
2405 "TESTING: Send group M1 for the same GTK and zero RSC to "
2406 MACSTR, MAC2STR(sta->addr));
2407 return wpa_auth_resend_group_m1(sta->wpa_sm,
2408 plain ? restore_tk : NULL, hapd, sta);
2409}
2410
Hai Shalomfdcde762020-04-02 11:19:20 -07002411
2412static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2413 char *buf, size_t buflen)
2414{
2415 struct sta_info *sta;
2416 u8 addr[ETH_ALEN];
2417 const u8 *pmk;
2418 int pmk_len;
2419
2420 if (hwaddr_aton(cmd, addr))
2421 return -1;
2422
2423 sta = ap_get_sta(hapd, addr);
2424 if (!sta || !sta->wpa_sm) {
2425 wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2426 MAC2STR(addr));
2427 return -1;
2428 }
2429 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
2430 if (!pmk) {
2431 wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2432 MAC2STR(addr));
2433 return -1;
2434 }
2435
2436 return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2437}
2438
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002439#endif /* CONFIG_TESTING_OPTIONS */
2440
2441
Hai Shalomb755a2a2020-04-23 21:49:02 -07002442#ifdef NEED_AP_MLME
Hai Shalomfdcde762020-04-02 11:19:20 -07002443static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2444{
2445 switch (params->bandwidth) {
2446 case 0:
2447 /* bandwidth not specified: use 20 MHz by default */
2448 /* fall-through */
2449 case 20:
2450 if (params->center_freq1 &&
2451 params->center_freq1 != params->freq)
2452 return -1;
2453
2454 if (params->center_freq2 || params->sec_channel_offset)
2455 return -1;
2456 break;
2457 case 40:
2458 if (params->center_freq2 || !params->sec_channel_offset)
2459 return -1;
2460
2461 if (!params->center_freq1)
2462 break;
2463 switch (params->sec_channel_offset) {
2464 case 1:
2465 if (params->freq + 10 != params->center_freq1)
2466 return -1;
2467 break;
2468 case -1:
2469 if (params->freq - 10 != params->center_freq1)
2470 return -1;
2471 break;
2472 default:
2473 return -1;
2474 }
2475 break;
2476 case 80:
2477 if (!params->center_freq1 || !params->sec_channel_offset)
2478 return 1;
2479
2480 switch (params->sec_channel_offset) {
2481 case 1:
2482 if (params->freq - 10 != params->center_freq1 &&
2483 params->freq + 30 != params->center_freq1)
2484 return 1;
2485 break;
2486 case -1:
2487 if (params->freq + 10 != params->center_freq1 &&
2488 params->freq - 30 != params->center_freq1)
2489 return -1;
2490 break;
2491 default:
2492 return -1;
2493 }
2494
2495 /* Adjacent and overlapped are not allowed for 80+80 */
2496 if (params->center_freq2 &&
2497 params->center_freq1 - params->center_freq2 <= 80 &&
2498 params->center_freq2 - params->center_freq1 <= 80)
2499 return 1;
2500 break;
2501 case 160:
2502 if (!params->center_freq1 || params->center_freq2 ||
2503 !params->sec_channel_offset)
2504 return -1;
2505
2506 switch (params->sec_channel_offset) {
2507 case 1:
2508 if (params->freq + 70 != params->center_freq1 &&
2509 params->freq + 30 != params->center_freq1 &&
2510 params->freq - 10 != params->center_freq1 &&
2511 params->freq - 50 != params->center_freq1)
2512 return -1;
2513 break;
2514 case -1:
2515 if (params->freq + 50 != params->center_freq1 &&
2516 params->freq + 10 != params->center_freq1 &&
2517 params->freq - 30 != params->center_freq1 &&
2518 params->freq - 70 != params->center_freq1)
2519 return -1;
2520 break;
2521 default:
2522 return -1;
2523 }
2524 break;
2525 default:
2526 return -1;
2527 }
2528
2529 return 0;
2530}
Hai Shalomb755a2a2020-04-23 21:49:02 -07002531#endif /* NEED_AP_MLME */
Hai Shalomfdcde762020-04-02 11:19:20 -07002532
2533
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002534static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2535 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002536{
2537#ifdef NEED_AP_MLME
2538 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002539 int ret;
Hai Shalomfdcde762020-04-02 11:19:20 -07002540 int dfs_range = 0;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002541 unsigned int i;
Hai Shalomfdcde762020-04-02 11:19:20 -07002542 int bandwidth;
2543 u8 chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002544
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002545 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002546 if (ret)
2547 return ret;
2548
Hai Shalomfdcde762020-04-02 11:19:20 -07002549 ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2550 if (ret) {
2551 wpa_printf(MSG_INFO,
2552 "chanswitch: invalid frequency settings provided");
2553 return ret;
2554 }
2555
2556 switch (settings.freq_params.bandwidth) {
2557 case 40:
2558 bandwidth = CHAN_WIDTH_40;
2559 break;
2560 case 80:
2561 if (settings.freq_params.center_freq2)
2562 bandwidth = CHAN_WIDTH_80P80;
2563 else
2564 bandwidth = CHAN_WIDTH_80;
2565 break;
2566 case 160:
2567 bandwidth = CHAN_WIDTH_160;
2568 break;
2569 default:
2570 bandwidth = CHAN_WIDTH_20;
2571 break;
2572 }
2573
2574 if (settings.freq_params.center_freq1)
2575 dfs_range += hostapd_is_dfs_overlap(
2576 iface, bandwidth, settings.freq_params.center_freq1);
2577 else
2578 dfs_range += hostapd_is_dfs_overlap(
2579 iface, bandwidth, settings.freq_params.freq);
2580
2581 if (settings.freq_params.center_freq2)
2582 dfs_range += hostapd_is_dfs_overlap(
2583 iface, bandwidth, settings.freq_params.center_freq2);
2584
2585 if (dfs_range) {
2586 ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2587 if (ret == NUM_HOSTAPD_MODES) {
2588 wpa_printf(MSG_ERROR,
2589 "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2590 settings.freq_params.freq,
2591 settings.freq_params.sec_channel_offset,
2592 settings.freq_params.bandwidth);
2593 return -1;
2594 }
2595
2596 settings.freq_params.channel = chan;
2597
2598 wpa_printf(MSG_DEBUG,
2599 "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2600 settings.freq_params.channel,
2601 settings.freq_params.freq,
2602 settings.freq_params.sec_channel_offset,
2603 settings.freq_params.bandwidth,
2604 settings.freq_params.center_freq1);
2605
2606 /* Perform CAC and switch channel */
2607 hostapd_switch_channel_fallback(iface, &settings.freq_params);
2608 return 0;
2609 }
2610
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002611 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002612
2613 /* Save CHAN_SWITCH VHT config */
2614 hostapd_chan_switch_vht_config(
2615 iface->bss[i], settings.freq_params.vht_enabled);
2616
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002617 ret = hostapd_switch_channel(iface->bss[i], &settings);
2618 if (ret) {
2619 /* FIX: What do we do if CSA fails in the middle of
2620 * submitting multi-BSS CSA requests? */
2621 return ret;
2622 }
2623 }
2624
2625 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002626#else /* NEED_AP_MLME */
2627 return -1;
2628#endif /* NEED_AP_MLME */
2629}
2630
2631
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002632static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2633 int reply_size, const char *param)
2634{
2635#ifdef RADIUS_SERVER
2636 if (os_strcmp(param, "radius_server") == 0) {
2637 return radius_server_get_mib(hapd->radius_srv, reply,
2638 reply_size);
2639 }
2640#endif /* RADIUS_SERVER */
2641 return -1;
2642}
2643
2644
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002645static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2646 char *buf, size_t buflen)
2647{
2648 int ret;
2649 char *pos;
2650 u8 *data = NULL;
2651 unsigned int vendor_id, subcmd;
2652 struct wpabuf *reply;
2653 size_t data_len = 0;
2654
2655 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2656 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002657 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002658 return -EINVAL;
2659
2660 subcmd = strtoul(pos, &pos, 10);
2661
2662 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002663 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002664 return -EINVAL;
2665 data_len = os_strlen(pos);
2666 }
2667
2668 if (data_len) {
2669 data_len /= 2;
2670 data = os_malloc(data_len);
2671 if (!data)
2672 return -ENOBUFS;
2673
2674 if (hexstr2bin(pos, data, data_len)) {
2675 wpa_printf(MSG_DEBUG,
2676 "Vendor command: wrong parameter format");
2677 os_free(data);
2678 return -EINVAL;
2679 }
2680 }
2681
2682 reply = wpabuf_alloc((buflen - 1) / 2);
2683 if (!reply) {
2684 os_free(data);
2685 return -ENOBUFS;
2686 }
2687
2688 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2689 reply);
2690
2691 if (ret == 0)
2692 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2693 wpabuf_len(reply));
2694
2695 wpabuf_free(reply);
2696 os_free(data);
2697
2698 return ret;
2699}
2700
2701
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002702static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2703 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002705 u8 addr[ETH_ALEN];
2706 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002708 if (hwaddr_aton(cmd, addr))
2709 return -1;
2710
2711 sta = ap_get_sta(hapd, addr);
2712 if (!sta || !sta->eapol_sm)
2713 return -1;
2714
2715 eapol_auth_reauthenticate(sta->eapol_sm);
2716 return 0;
2717}
2718
2719
2720static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2721{
2722 u8 addr[ETH_ALEN];
2723 struct sta_info *sta;
2724 char *pos = cmd, *param;
2725
2726 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2727 return -1;
2728 pos += 18;
2729 param = pos;
2730 pos = os_strchr(pos, ' ');
2731 if (!pos)
2732 return -1;
2733 *pos++ = '\0';
2734
2735 sta = ap_get_sta(hapd, addr);
2736 if (!sta || !sta->eapol_sm)
2737 return -1;
2738
2739 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2740}
2741
2742
2743static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2744 char *buf, size_t buflen)
2745{
2746 char *pos, *end, *stamp;
2747 int ret;
2748
2749 /* cmd: "LOG_LEVEL [<level>]" */
2750 if (*cmd == '\0') {
2751 pos = buf;
2752 end = buf + buflen;
2753 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2754 "Timestamp: %d\n",
2755 debug_level_str(wpa_debug_level),
2756 wpa_debug_timestamp);
2757 if (os_snprintf_error(end - pos, ret))
2758 ret = 0;
2759
2760 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002763 while (*cmd == ' ')
2764 cmd++;
2765
2766 stamp = os_strchr(cmd, ' ');
2767 if (stamp) {
2768 *stamp++ = '\0';
2769 while (*stamp == ' ') {
2770 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 }
2773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002774 if (os_strlen(cmd)) {
2775 int level = str_to_debug_level(cmd);
2776 if (level < 0)
2777 return -1;
2778 wpa_debug_level = level;
2779 }
2780
2781 if (stamp && os_strlen(stamp))
2782 wpa_debug_timestamp = atoi(stamp);
2783
2784 os_memcpy(buf, "OK\n", 3);
2785 return 3;
2786}
2787
2788
2789#ifdef NEED_AP_MLME
2790static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2791 char *buf, size_t buflen)
2792{
2793 struct hostapd_iface *iface = hapd->iface;
2794 char *pos, *end;
2795 struct hostapd_sta_info *info;
2796 struct os_reltime now;
2797
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002798 if (!iface->num_sta_seen)
2799 return 0;
2800
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002801 sta_track_expire(iface, 0);
2802
2803 pos = buf;
2804 end = buf + buflen;
2805
2806 os_get_reltime(&now);
2807 dl_list_for_each_reverse(info, &iface->sta_seen,
2808 struct hostapd_sta_info, list) {
2809 struct os_reltime age;
2810 int ret;
2811
2812 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002813 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2814 MAC2STR(info->addr), (unsigned int) age.sec,
2815 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002816 if (os_snprintf_error(end - pos, ret))
2817 break;
2818 pos += ret;
2819 }
2820
2821 return pos - buf;
2822}
2823#endif /* NEED_AP_MLME */
2824
2825
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002826static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2827 const char *cmd)
2828{
2829 u8 addr[ETH_ALEN];
2830
2831 if (hwaddr_aton(cmd, addr)) {
2832 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2833 return -1;
2834 }
2835
2836 return hostapd_send_lci_req(hapd, addr);
2837}
2838
2839
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002840static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002841{
2842 u8 addr[ETH_ALEN];
2843 char *token, *context = NULL;
2844 int random_interval, min_ap;
2845 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2846 unsigned int n_responders;
2847
2848 token = str_token(cmd, " ", &context);
2849 if (!token || hwaddr_aton(token, addr)) {
2850 wpa_printf(MSG_INFO,
2851 "CTRL: REQ_RANGE - Bad destination address");
2852 return -1;
2853 }
2854
2855 token = str_token(cmd, " ", &context);
2856 if (!token)
2857 return -1;
2858
2859 random_interval = atoi(token);
2860 if (random_interval < 0 || random_interval > 0xffff)
2861 return -1;
2862
2863 token = str_token(cmd, " ", &context);
2864 if (!token)
2865 return -1;
2866
2867 min_ap = atoi(token);
2868 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2869 return -1;
2870
2871 n_responders = 0;
2872 while ((token = str_token(cmd, " ", &context))) {
2873 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2874 wpa_printf(MSG_INFO,
2875 "CTRL: REQ_RANGE: Too many responders");
2876 return -1;
2877 }
2878
2879 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2880 wpa_printf(MSG_INFO,
2881 "CTRL: REQ_RANGE: Bad responder address");
2882 return -1;
2883 }
2884
2885 n_responders++;
2886 }
2887
2888 if (!n_responders) {
2889 wpa_printf(MSG_INFO,
2890 "CTRL: REQ_RANGE - No FTM responder address");
2891 return -1;
2892 }
2893
2894 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2895 responders, n_responders);
2896}
2897
2898
Dmitry Shmidt29333592017-01-09 12:27:11 -08002899static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2900 const char *cmd, char *reply,
2901 size_t reply_size)
2902{
2903 u8 addr[ETH_ALEN];
2904 const char *pos;
2905 struct wpabuf *req;
2906 int ret;
2907 u8 req_mode = 0;
2908
2909 if (hwaddr_aton(cmd, addr))
2910 return -1;
2911 pos = os_strchr(cmd, ' ');
2912 if (!pos)
2913 return -1;
2914 pos++;
2915 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2916 int val = hex2byte(pos + 9);
2917
2918 if (val < 0)
2919 return -1;
2920 req_mode = val;
2921 pos += 11;
2922 pos = os_strchr(pos, ' ');
2923 if (!pos)
2924 return -1;
2925 pos++;
2926 }
2927 req = wpabuf_parse_bin(pos);
2928 if (!req)
2929 return -1;
2930
2931 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2932 wpabuf_free(req);
2933 if (ret >= 0)
2934 ret = os_snprintf(reply, reply_size, "%d", ret);
2935 return ret;
2936}
2937
2938
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002939static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
2940 char *buf, size_t buflen)
2941{
2942 if (!(hapd->conf->radio_measurements[0] &
2943 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2944 wpa_printf(MSG_ERROR,
2945 "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
2946 return -1;
2947 }
2948
2949 return hostapd_neighbor_show(hapd, buf, buflen);
2950}
2951
2952
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002953static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2954{
2955 struct wpa_ssid_value ssid;
2956 u8 bssid[ETH_ALEN];
2957 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002958 int stationary = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002959 char *tmp;
2960 int ret;
2961
2962 if (!(hapd->conf->radio_measurements[0] &
2963 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2964 wpa_printf(MSG_ERROR,
2965 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2966 return -1;
2967 }
2968
2969 if (hwaddr_aton(buf, bssid)) {
2970 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2971 return -1;
2972 }
2973
2974 tmp = os_strstr(buf, "ssid=");
2975 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2976 wpa_printf(MSG_ERROR,
2977 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2978 return -1;
2979 }
2980 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2981 if (!buf)
2982 return -1;
2983
2984 tmp = os_strstr(buf, "nr=");
2985 if (!tmp) {
2986 wpa_printf(MSG_ERROR,
2987 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2988 return -1;
2989 }
2990
2991 buf = os_strchr(tmp, ' ');
2992 if (buf)
2993 *buf++ = '\0';
2994
2995 nr = wpabuf_parse_bin(tmp + 3);
2996 if (!nr) {
2997 wpa_printf(MSG_ERROR,
2998 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2999 return -1;
3000 }
3001
3002 if (!buf)
3003 goto set;
3004
3005 tmp = os_strstr(buf, "lci=");
3006 if (tmp) {
3007 buf = os_strchr(tmp, ' ');
3008 if (buf)
3009 *buf++ = '\0';
3010 lci = wpabuf_parse_bin(tmp + 4);
3011 if (!lci) {
3012 wpa_printf(MSG_ERROR,
3013 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
3014 wpabuf_free(nr);
3015 return -1;
3016 }
3017 }
3018
3019 if (!buf)
3020 goto set;
3021
3022 tmp = os_strstr(buf, "civic=");
3023 if (tmp) {
3024 buf = os_strchr(tmp, ' ');
3025 if (buf)
3026 *buf++ = '\0';
3027 civic = wpabuf_parse_bin(tmp + 6);
3028 if (!civic) {
3029 wpa_printf(MSG_ERROR,
3030 "CTRL: SET_NEIGHBOR: Bad civic subelement");
3031 wpabuf_free(nr);
3032 wpabuf_free(lci);
3033 return -1;
3034 }
3035 }
3036
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003037 if (!buf)
3038 goto set;
3039
3040 if (os_strstr(buf, "stat"))
3041 stationary = 1;
3042
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003043set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003044 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
3045 stationary);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003046
3047 wpabuf_free(nr);
3048 wpabuf_free(lci);
3049 wpabuf_free(civic);
3050
3051 return ret;
3052}
3053
3054
3055static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3056 char *buf)
3057{
3058 struct wpa_ssid_value ssid;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003059 struct wpa_ssid_value *ssidp = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003060 u8 bssid[ETH_ALEN];
3061 char *tmp;
3062
3063 if (hwaddr_aton(buf, bssid)) {
3064 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3065 return -1;
3066 }
3067
3068 tmp = os_strstr(buf, "ssid=");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003069 if (tmp) {
3070 ssidp = &ssid;
3071 if (ssid_parse(tmp + 5, &ssid)) {
3072 wpa_printf(MSG_ERROR,
3073 "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3074 return -1;
3075 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003076 }
3077
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003078 return hostapd_neighbor_remove(hapd, bssid, ssidp);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003079}
3080
3081
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003082static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3083 size_t buflen)
3084{
3085 int ret, i;
3086 char *pos, *end;
3087
3088 ret = os_snprintf(buf, buflen, "%016llX:\n",
3089 (long long unsigned) iface->drv_flags);
3090 if (os_snprintf_error(buflen, ret))
3091 return -1;
3092
3093 pos = buf + ret;
3094 end = buf + buflen;
3095
3096 for (i = 0; i < 64; i++) {
3097 if (iface->drv_flags & (1LLU << i)) {
3098 ret = os_snprintf(pos, end - pos, "%s\n",
3099 driver_flag_to_string(1LLU << i));
3100 if (os_snprintf_error(end - pos, ret))
3101 return -1;
3102 pos += ret;
3103 }
3104 }
3105
3106 return pos - buf;
3107}
3108
3109
Hai Shalomb755a2a2020-04-23 21:49:02 -07003110static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3111 size_t buflen)
3112{
3113 int ret, i;
3114 char *pos, *end;
3115
3116 ret = os_snprintf(buf, buflen, "%016llX:\n",
3117 (long long unsigned) iface->drv_flags2);
3118 if (os_snprintf_error(buflen, ret))
3119 return -1;
3120
3121 pos = buf + ret;
3122 end = buf + buflen;
3123
3124 for (i = 0; i < 64; i++) {
3125 if (iface->drv_flags2 & (1LLU << i)) {
3126 ret = os_snprintf(pos, end - pos, "%s\n",
3127 driver_flag2_to_string(1LLU << i));
3128 if (os_snprintf_error(end - pos, ret))
3129 return -1;
3130 pos += ret;
3131 }
3132 }
3133
3134 return pos - buf;
3135}
3136
3137
Roshan Pius3a1667e2018-07-03 15:17:14 -07003138static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
3139 const char *txtaddr)
3140{
3141 u8 addr[ETH_ALEN];
3142 struct vlan_description vlan_id;
3143
3144 if (!(*num))
3145 return 0;
3146
3147 if (hwaddr_aton(txtaddr, addr))
3148 return -1;
3149
3150 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
3151 hostapd_remove_acl_mac(acl, num, addr);
3152
3153 return 0;
3154}
3155
3156
3157static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
3158 int *num)
3159{
3160 while (*num)
3161 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
3162}
3163
3164
3165static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
3166 char *buf, size_t buflen)
3167{
3168 int i = 0, len = 0, ret = 0;
3169
3170 if (!acl)
3171 return 0;
3172
3173 while (i < num) {
3174 ret = os_snprintf(buf + len, buflen - len,
3175 MACSTR " VLAN_ID=%d\n",
3176 MAC2STR(acl[i].addr),
3177 acl[i].vlan_id.untagged);
3178 if (ret < 0 || (size_t) ret >= buflen - len)
3179 return len;
3180 i++;
3181 len += ret;
3182 }
3183 return len;
3184}
3185
3186
3187static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
3188 const char *cmd)
3189{
3190 u8 addr[ETH_ALEN];
3191 struct vlan_description vlan_id;
3192 int ret = 0, vlanid = 0;
3193 const char *pos;
3194
3195 if (hwaddr_aton(cmd, addr))
3196 return -1;
3197
3198 pos = os_strstr(cmd, "VLAN_ID=");
3199 if (pos)
3200 vlanid = atoi(pos + 8);
3201
3202 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
3203 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
3204 if (ret != -1 && *acl)
3205 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
3206 }
3207
3208 return ret < 0 ? -1 : 0;
3209}
3210
3211
Hai Shalom021b0b52019-04-10 11:17:58 -07003212static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3213 const char *field, char *buf,
3214 size_t buflen)
3215{
3216 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3217
3218#ifdef CONFIG_DPP
3219 if (os_strcmp(field, "dpp") == 0) {
3220 int res;
3221
3222#ifdef CONFIG_DPP2
3223 res = os_snprintf(buf, buflen, "DPP=2");
3224#else /* CONFIG_DPP2 */
3225 res = os_snprintf(buf, buflen, "DPP=1");
3226#endif /* CONFIG_DPP2 */
3227 if (os_snprintf_error(buflen, res))
3228 return -1;
3229 return res;
3230 }
3231#endif /* CONFIG_DPP */
3232
3233 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3234 field);
3235
3236 return -1;
3237}
3238
3239
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003240static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3241 char *buf, char *reply,
3242 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003243 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003244 socklen_t fromlen)
3245{
3246 int reply_len, res;
3247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 os_memcpy(reply, "OK\n", 3);
3249 reply_len = 3;
3250
3251 if (os_strcmp(buf, "PING") == 0) {
3252 os_memcpy(reply, "PONG\n", 5);
3253 reply_len = 5;
3254 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3255 if (wpa_debug_reopen_file() < 0)
3256 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003257 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3258 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003259 } else if (os_strcmp(buf, "STATUS") == 0) {
3260 reply_len = hostapd_ctrl_iface_status(hapd, reply,
3261 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003262 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3263 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 } else if (os_strcmp(buf, "MIB") == 0) {
3265 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3266 if (reply_len >= 0) {
3267 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3268 reply_size - reply_len);
3269 if (res < 0)
3270 reply_len = -1;
3271 else
3272 reply_len += res;
3273 }
3274 if (reply_len >= 0) {
3275 res = ieee802_1x_get_mib(hapd, reply + reply_len,
3276 reply_size - reply_len);
3277 if (res < 0)
3278 reply_len = -1;
3279 else
3280 reply_len += res;
3281 }
3282#ifndef CONFIG_NO_RADIUS
3283 if (reply_len >= 0) {
3284 res = radius_client_get_mib(hapd->radius,
3285 reply + reply_len,
3286 reply_size - reply_len);
3287 if (res < 0)
3288 reply_len = -1;
3289 else
3290 reply_len += res;
3291 }
3292#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003293 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
3294 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3295 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
3297 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3298 reply_size);
3299 } else if (os_strncmp(buf, "STA ", 4) == 0) {
3300 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3301 reply_size);
3302 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3303 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3304 reply_size);
3305 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003306 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3307 reply_len = -1;
3308 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3309 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 reply_len = -1;
3311 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003312 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 reply_len = -1;
3314 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003315 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316 buf + 6))
3317 reply_len = -1;
3318 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3319 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3320 reply_len = -1;
3321 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3322 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3323 reply_len = -1;
3324 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3325 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3326 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003327#ifdef CONFIG_TAXONOMY
3328 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3329 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3330 reply, reply_size);
3331#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003332 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3333 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3334 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003335 } else if (os_strcmp(buf, "STOP_AP") == 0) {
3336 if (hostapd_ctrl_iface_stop_ap(hapd))
3337 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338#ifdef NEED_AP_MLME
3339 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3340 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3341 reply_len = -1;
3342#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343#ifdef CONFIG_WPS
3344 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3345 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3346 reply_len = -1;
3347 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3348 reply_len = hostapd_ctrl_iface_wps_check_pin(
3349 hapd, buf + 14, reply, reply_size);
3350 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3351 if (hostapd_wps_button_pushed(hapd, NULL))
3352 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003353 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3354 if (hostapd_wps_cancel(hapd))
3355 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3357 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3358 reply, reply_size);
3359 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3360 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3361 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003362 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3363 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3364 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003365#ifdef CONFIG_WPS_NFC
3366 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3367 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3368 reply_len = -1;
3369 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3370 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3371 hapd, buf + 21, reply, reply_size);
3372 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3373 reply_len = hostapd_ctrl_iface_wps_nfc_token(
3374 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003375 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3376 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3377 hapd, buf + 21, reply, reply_size);
3378 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3379 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3380 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003381#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07003383#ifdef CONFIG_INTERWORKING
3384 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3385 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3386 reply_len = -1;
3387 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3388 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3389 reply_len = -1;
3390#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003391#ifdef CONFIG_HS20
3392 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3393 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3394 reply_len = -1;
3395 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3396 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3397 reply_len = -1;
3398#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003399#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003400 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3401 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3402 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003403 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3404 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3405 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3407 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3408 reply_len = -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003409 } else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3410 if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3411 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003412#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3414 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3415 reply_size);
3416 } else if (os_strncmp(buf, "SET ", 4) == 0) {
3417 if (hostapd_ctrl_iface_set(hapd, buf + 4))
3418 reply_len = -1;
3419 } else if (os_strncmp(buf, "GET ", 4) == 0) {
3420 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3421 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003422 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3423 if (hostapd_ctrl_iface_enable(hapd->iface))
3424 reply_len = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003425 } else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3426 if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3427 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003428 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3429 if (hostapd_ctrl_iface_reload(hapd->iface))
3430 reply_len = -1;
3431 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3432 if (hostapd_ctrl_iface_disable(hapd->iface))
3433 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003434 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3435 if (ieee802_11_set_beacon(hapd))
3436 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003437#ifdef CONFIG_TESTING_OPTIONS
3438 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3439 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3440 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003441 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3442 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3443 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003444 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3445 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3446 buf + 23) < 0)
3447 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003448 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3449 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3450 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003451 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3452 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3453 reply_len = -1;
3454 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3455 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3456 reply_len = -1;
3457 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3458 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3459 reply_len = -1;
3460 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3461 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3462 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003463 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3464 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3465 reply_len = -1;
3466 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3467 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3468 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003469 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3470 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3471 reply_len = -1;
3472 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3473 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003474 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3475 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3476 reply_len = -1;
3477 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3478 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3479 reply_len = -1;
3480 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3481 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3482 reply_len = -1;
3483 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3484 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3485 reply_len = -1;
3486 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3487 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3488 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003489 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3490 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3491 reply_len = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003492 } else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3493 reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3494 reply_size);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003495#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003496 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003497 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003498 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003499 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3500 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3501 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003502 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3503 ieee802_1x_erp_flush(hapd);
3504#ifdef RADIUS_SERVER
3505 radius_server_erp_flush(hapd->radius_srv);
3506#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003507 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3508 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3509 reply_len = -1;
3510 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3511 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3512 reply_len = -1;
3513 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3514 reply_len = hostapd_ctrl_iface_log_level(
3515 hapd, buf + 9, reply, reply_size);
3516#ifdef NEED_AP_MLME
3517 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3518 reply_len = hostapd_ctrl_iface_track_sta_list(
3519 hapd, reply, reply_size);
3520#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003521 } else if (os_strcmp(buf, "PMKSA") == 0) {
3522 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3523 reply_size);
3524 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3525 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003526 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3527 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3528 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003529 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3530 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3531 reply_len = -1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003532 } else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3533 reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3534 reply_size);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003535 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3536 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3537 reply_len = -1;
3538 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3539 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3540 reply_len = -1;
3541 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3542 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3543 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003544 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3545 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3546 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003547 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3548 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3549 reply_size);
Hai Shalomb755a2a2020-04-23 21:49:02 -07003550 } else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3551 reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3552 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003553 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3554 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003555 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3556 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3557 if (!hostapd_ctrl_iface_acl_add_mac(
3558 &hapd->conf->accept_mac,
3559 &hapd->conf->num_accept_mac, buf + 19))
3560 hostapd_disassoc_accept_mac(hapd);
3561 else
3562 reply_len = -1;
3563 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3564 hostapd_ctrl_iface_acl_del_mac(
3565 &hapd->conf->accept_mac,
3566 &hapd->conf->num_accept_mac, buf + 19);
3567 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3568 reply_len = hostapd_ctrl_iface_acl_show_mac(
3569 hapd->conf->accept_mac,
3570 hapd->conf->num_accept_mac, reply, reply_size);
3571 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3572 hostapd_ctrl_iface_acl_clear_list(
3573 &hapd->conf->accept_mac,
3574 &hapd->conf->num_accept_mac);
3575 }
3576 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3577 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3578 if (!hostapd_ctrl_iface_acl_add_mac(
3579 &hapd->conf->deny_mac,
3580 &hapd->conf->num_deny_mac, buf + 17))
3581 hostapd_disassoc_deny_mac(hapd);
3582 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3583 hostapd_ctrl_iface_acl_del_mac(
3584 &hapd->conf->deny_mac,
3585 &hapd->conf->num_deny_mac, buf + 17);
3586 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3587 reply_len = hostapd_ctrl_iface_acl_show_mac(
3588 hapd->conf->deny_mac,
3589 hapd->conf->num_deny_mac, reply, reply_size);
3590 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3591 hostapd_ctrl_iface_acl_clear_list(
3592 &hapd->conf->deny_mac,
3593 &hapd->conf->num_deny_mac);
3594 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003595#ifdef CONFIG_DPP
3596 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3597 res = hostapd_dpp_qr_code(hapd, buf + 12);
3598 if (res < 0) {
3599 reply_len = -1;
3600 } else {
3601 reply_len = os_snprintf(reply, reply_size, "%d", res);
3602 if (os_snprintf_error(reply_size, reply_len))
3603 reply_len = -1;
3604 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003605 } else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3606 res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3607 if (res < 0) {
3608 reply_len = -1;
3609 } else {
3610 reply_len = os_snprintf(reply, reply_size, "%d", res);
3611 if (os_snprintf_error(reply_size, reply_len))
3612 reply_len = -1;
3613 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003614 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3615 res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3616 if (res < 0) {
3617 reply_len = -1;
3618 } else {
3619 reply_len = os_snprintf(reply, reply_size, "%d", res);
3620 if (os_snprintf_error(reply_size, reply_len))
3621 reply_len = -1;
3622 }
3623 } else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3624 res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3625 if (res < 0) {
3626 reply_len = -1;
3627 } else {
3628 reply_len = os_snprintf(reply, reply_size, "%d", res);
3629 if (os_snprintf_error(reply_size, reply_len))
3630 reply_len = -1;
3631 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003632 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003633 res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003634 if (res < 0) {
3635 reply_len = -1;
3636 } else {
3637 reply_len = os_snprintf(reply, reply_size, "%d", res);
3638 if (os_snprintf_error(reply_size, reply_len))
3639 reply_len = -1;
3640 }
3641 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003642 if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3643 buf + 21) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003644 reply_len = -1;
3645 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3646 const char *uri;
3647
Hai Shalom021b0b52019-04-10 11:17:58 -07003648 uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3649 atoi(buf + 22));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003650 if (!uri) {
3651 reply_len = -1;
3652 } else {
3653 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3654 if (os_snprintf_error(reply_size, reply_len))
3655 reply_len = -1;
3656 }
3657 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003658 reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3659 atoi(buf + 19),
3660 reply, reply_size);
Hai Shalomfdcde762020-04-02 11:19:20 -07003661 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3662 if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3663 atoi(buf + 18),
3664 os_strchr(buf + 18, ' ')) < 0)
3665 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003666 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3667 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3668 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003669 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3670 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3671 reply_len = -1;
3672 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3673 hostapd_dpp_stop(hapd);
3674 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003675 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003676 res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3677 buf + 20);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003678 if (res < 0) {
3679 reply_len = -1;
3680 } else {
3681 reply_len = os_snprintf(reply, reply_size, "%d", res);
3682 if (os_snprintf_error(reply_size, reply_len))
3683 reply_len = -1;
3684 }
3685 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003686 if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3687 buf + 24) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003688 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003689 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003690 if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003691 reply_len = -1;
3692 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07003693 reply_len = dpp_configurator_get_key_id(
3694 hapd->iface->interfaces->dpp,
3695 atoi(buf + 25),
3696 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003697 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3698 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3699 if (res < 0) {
3700 reply_len = -1;
3701 } else {
3702 reply_len = os_snprintf(reply, reply_size, "%d", res);
3703 if (os_snprintf_error(reply_size, reply_len))
3704 reply_len = -1;
3705 }
3706 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3707 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3708 reply_len = -1;
3709#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003710#ifdef RADIUS_SERVER
3711 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3712 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3713 reply_len = -1;
3714#endif /* RADIUS_SERVER */
Hai Shalom021b0b52019-04-10 11:17:58 -07003715 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3716 reply_len = hostapd_ctrl_iface_get_capability(
3717 hapd, buf + 15, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 } else {
3719 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3720 reply_len = 16;
3721 }
3722
3723 if (reply_len < 0) {
3724 os_memcpy(reply, "FAIL\n", 5);
3725 reply_len = 5;
3726 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003727
3728 return reply_len;
3729}
3730
3731
3732static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3733 void *sock_ctx)
3734{
3735 struct hostapd_data *hapd = eloop_ctx;
3736 char buf[4096];
3737 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003738 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003739 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003740 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003741 const int reply_size = 4096;
3742 int reply_len;
3743 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003744#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07003745 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003746#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003747
3748 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3749 (struct sockaddr *) &from, &fromlen);
3750 if (res < 0) {
3751 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3752 strerror(errno));
3753 return;
3754 }
3755 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003756
3757 reply = os_malloc(reply_size);
3758 if (reply == NULL) {
3759 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3760 fromlen) < 0) {
3761 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3762 strerror(errno));
3763 }
3764 return;
3765 }
3766
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003767#ifdef CONFIG_CTRL_IFACE_UDP
3768 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3769 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07003770 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
3771 hapd->ctrl_iface_cookie,
3772 CTRL_IFACE_COOKIE_LEN);
3773 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003774 goto done;
3775 }
3776
3777 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07003778 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003779 wpa_printf(MSG_DEBUG,
3780 "CTRL: No cookie in the request - drop request");
3781 os_free(reply);
3782 return;
3783 }
3784
Hai Shalomfdcde762020-04-02 11:19:20 -07003785 if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
3786 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003787 wpa_printf(MSG_DEBUG,
3788 "CTRL: Invalid cookie in the request - drop request");
3789 os_free(reply);
3790 return;
3791 }
3792
Hai Shalomfdcde762020-04-02 11:19:20 -07003793 pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003794 while (*pos == ' ')
3795 pos++;
3796#endif /* CONFIG_CTRL_IFACE_UDP */
3797
3798 if (os_strcmp(pos, "PING") == 0)
3799 level = MSG_EXCESSIVE;
3800 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3801
3802 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003803 reply, reply_size,
3804 &from, fromlen);
3805
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003806#ifdef CONFIG_CTRL_IFACE_UDP
3807done:
3808#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003809 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3810 fromlen) < 0) {
3811 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3812 strerror(errno));
3813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814 os_free(reply);
3815}
3816
3817
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003818#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3820{
3821 char *buf;
3822 size_t len;
3823
3824 if (hapd->conf->ctrl_interface == NULL)
3825 return NULL;
3826
3827 len = os_strlen(hapd->conf->ctrl_interface) +
3828 os_strlen(hapd->conf->iface) + 2;
3829 buf = os_malloc(len);
3830 if (buf == NULL)
3831 return NULL;
3832
3833 os_snprintf(buf, len, "%s/%s",
3834 hapd->conf->ctrl_interface, hapd->conf->iface);
3835 buf[len - 1] = '\0';
3836 return buf;
3837}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003838#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839
3840
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003841static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3842 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003843 const char *txt, size_t len)
3844{
3845 struct hostapd_data *hapd = ctx;
3846 if (hapd == NULL)
3847 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003848 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849}
3850
3851
3852int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3853{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003854#ifdef CONFIG_CTRL_IFACE_UDP
3855 int port = HOSTAPD_CTRL_IFACE_PORT;
3856 char p[32] = { 0 };
3857 char port_str[40], *tmp;
3858 char *pos;
3859 struct addrinfo hints = { 0 }, *res, *saveres;
3860 int n;
3861
3862 if (hapd->ctrl_sock > -1) {
3863 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3864 return 0;
3865 }
3866
3867 if (hapd->conf->ctrl_interface == NULL)
3868 return 0;
3869
3870 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3871 if (pos) {
3872 pos += 4;
3873 port = atoi(pos);
3874 if (port <= 0) {
3875 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3876 goto fail;
3877 }
3878 }
3879
3880 dl_list_init(&hapd->ctrl_dst);
3881 hapd->ctrl_sock = -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003882 os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003883
3884#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3885 hints.ai_flags = AI_PASSIVE;
3886#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3887
3888#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3889 hints.ai_family = AF_INET6;
3890#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3891 hints.ai_family = AF_INET;
3892#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3893 hints.ai_socktype = SOCK_DGRAM;
3894
3895try_again:
3896 os_snprintf(p, sizeof(p), "%d", port);
3897 n = getaddrinfo(NULL, p, &hints, &res);
3898 if (n) {
3899 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3900 goto fail;
3901 }
3902
3903 saveres = res;
3904 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3905 res->ai_protocol);
3906 if (hapd->ctrl_sock < 0) {
3907 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3908 goto fail;
3909 }
3910
3911 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3912 port--;
3913 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3914 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3915 goto try_again;
3916 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3917 goto fail;
3918 }
3919
3920 freeaddrinfo(saveres);
3921
3922 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3923 tmp = os_strdup(port_str);
3924 if (tmp) {
3925 os_free(hapd->conf->ctrl_interface);
3926 hapd->conf->ctrl_interface = tmp;
3927 }
3928 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3929
3930 if (eloop_register_read_sock(hapd->ctrl_sock,
3931 hostapd_ctrl_iface_receive, hapd, NULL) <
3932 0) {
3933 hostapd_ctrl_iface_deinit(hapd);
3934 return -1;
3935 }
3936
3937 hapd->msg_ctx = hapd;
3938 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3939
3940 return 0;
3941
3942fail:
3943 if (hapd->ctrl_sock >= 0)
3944 close(hapd->ctrl_sock);
3945 return -1;
3946#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 struct sockaddr_un addr;
3948 int s = -1;
3949 char *fname = NULL;
3950
Dmitry Shmidt04949592012-07-19 12:16:46 -07003951 if (hapd->ctrl_sock > -1) {
3952 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3953 return 0;
3954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003956 dl_list_init(&hapd->ctrl_dst);
3957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 if (hapd->conf->ctrl_interface == NULL)
3959 return 0;
3960
3961 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3962 if (errno == EEXIST) {
3963 wpa_printf(MSG_DEBUG, "Using existing control "
3964 "interface directory.");
3965 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003966 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3967 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 goto fail;
3969 }
3970 }
3971
3972 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003973 lchown(hapd->conf->ctrl_interface, -1,
3974 hapd->conf->ctrl_interface_gid) < 0) {
3975 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977 return -1;
3978 }
3979
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003980 if (!hapd->conf->ctrl_interface_gid_set &&
3981 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08003982 lchown(hapd->conf->ctrl_interface, -1,
3983 hapd->iface->interfaces->ctrl_iface_group) < 0) {
3984 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003985 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003986 return -1;
3987 }
3988
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003989#ifdef ANDROID
3990 /*
3991 * Android is using umask 0077 which would leave the control interface
3992 * directory without group access. This breaks things since Wi-Fi
3993 * framework assumes that this directory can be accessed by other
3994 * applications in the wifi group. Fix this by adding group access even
3995 * if umask value would prevent this.
3996 */
3997 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3998 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3999 strerror(errno));
4000 /* Try to continue anyway */
4001 }
4002#endif /* ANDROID */
4003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4005 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4006 goto fail;
4007
4008 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4009 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004010 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 goto fail;
4012 }
4013
4014 os_memset(&addr, 0, sizeof(addr));
4015#ifdef __FreeBSD__
4016 addr.sun_len = sizeof(addr);
4017#endif /* __FreeBSD__ */
4018 addr.sun_family = AF_UNIX;
4019 fname = hostapd_ctrl_iface_path(hapd);
4020 if (fname == NULL)
4021 goto fail;
4022 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4023 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4024 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4025 strerror(errno));
4026 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4027 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4028 " allow connections - assuming it was left"
4029 "over from forced program termination");
4030 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004031 wpa_printf(MSG_ERROR,
4032 "Could not unlink existing ctrl_iface socket '%s': %s",
4033 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 goto fail;
4035 }
4036 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4037 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004038 wpa_printf(MSG_ERROR,
4039 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4040 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 goto fail;
4042 }
4043 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4044 "ctrl_iface socket '%s'", fname);
4045 } else {
4046 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4047 "be in use - cannot override it");
4048 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4049 "not used anymore", fname);
4050 os_free(fname);
4051 fname = NULL;
4052 goto fail;
4053 }
4054 }
4055
4056 if (hapd->conf->ctrl_interface_gid_set &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004057 lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4058 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004059 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 goto fail;
4061 }
4062
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004063 if (!hapd->conf->ctrl_interface_gid_set &&
4064 hapd->iface->interfaces->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004065 lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4066 wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004068 goto fail;
4069 }
4070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004072 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4073 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074 goto fail;
4075 }
4076 os_free(fname);
4077
4078 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004079 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4080 NULL) < 0) {
4081 hostapd_ctrl_iface_deinit(hapd);
4082 return -1;
4083 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084 hapd->msg_ctx = hapd;
4085 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4086
4087 return 0;
4088
4089fail:
4090 if (s >= 0)
4091 close(s);
4092 if (fname) {
4093 unlink(fname);
4094 os_free(fname);
4095 }
4096 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004097#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098}
4099
4100
4101void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4102{
4103 struct wpa_ctrl_dst *dst, *prev;
4104
4105 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004106#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004108#endif /* !CONFIG_CTRL_IFACE_UDP */
4109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 eloop_unregister_read_sock(hapd->ctrl_sock);
4111 close(hapd->ctrl_sock);
4112 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004113#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 fname = hostapd_ctrl_iface_path(hapd);
4115 if (fname)
4116 unlink(fname);
4117 os_free(fname);
4118
4119 if (hapd->conf->ctrl_interface &&
4120 rmdir(hapd->conf->ctrl_interface) < 0) {
4121 if (errno == ENOTEMPTY) {
4122 wpa_printf(MSG_DEBUG, "Control interface "
4123 "directory not empty - leaving it "
4124 "behind");
4125 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004126 wpa_printf(MSG_ERROR,
4127 "rmdir[ctrl_interface=%s]: %s",
4128 hapd->conf->ctrl_interface,
4129 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130 }
4131 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004132#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 }
4134
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004135 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4136 list)
4137 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004138
4139#ifdef CONFIG_TESTING_OPTIONS
4140 l2_packet_deinit(hapd->l2_test);
4141 hapd->l2_test = NULL;
4142#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143}
4144
4145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004146static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4147 char *buf)
4148{
4149 if (hostapd_add_iface(interfaces, buf) < 0) {
4150 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4151 return -1;
4152 }
4153 return 0;
4154}
4155
4156
4157static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4158 char *buf)
4159{
4160 if (hostapd_remove_iface(interfaces, buf) < 0) {
4161 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4162 return -1;
4163 }
4164 return 0;
4165}
4166
4167
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004168static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004169 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004170 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004171{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004172 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4173 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004174}
4175
4176
4177static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004178 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004179 socklen_t fromlen)
4180{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004181 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004182}
4183
4184
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004185static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4186{
4187#ifdef CONFIG_WPS_TESTING
4188 wps_version_number = 0x20;
4189 wps_testing_dummy_cred = 0;
4190 wps_corrupt_pkhash = 0;
4191#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07004192
4193#ifdef CONFIG_TESTING_OPTIONS
4194#ifdef CONFIG_DPP
4195 dpp_test = DPP_TEST_DISABLED;
4196#endif /* CONFIG_DPP */
4197#endif /* CONFIG_TESTING_OPTIONS */
4198
4199#ifdef CONFIG_DPP
Hai Shalom021b0b52019-04-10 11:17:58 -07004200 dpp_global_clear(interfaces->dpp);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004201#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004202}
4203
4204
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004205#ifdef CONFIG_FST
4206
4207static int
4208hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4209 const char *cmd)
4210{
4211 char ifname[IFNAMSIZ + 1];
4212 struct fst_iface_cfg cfg;
4213 struct hostapd_data *hapd;
4214 struct fst_wpa_obj iface_obj;
4215
4216 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4217 hapd = hostapd_get_iface(interfaces, ifname);
4218 if (hapd) {
4219 if (hapd->iface->fst) {
4220 wpa_printf(MSG_INFO, "FST: Already attached");
4221 return -1;
4222 }
4223 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4224 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4225 &iface_obj, &cfg);
4226 if (hapd->iface->fst)
4227 return 0;
4228 }
4229 }
4230
4231 return -EINVAL;
4232}
4233
4234
4235static int
4236hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4237 const char *cmd)
4238{
4239 char ifname[IFNAMSIZ + 1];
4240 struct hostapd_data * hapd;
4241
4242 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4243 hapd = hostapd_get_iface(interfaces, ifname);
4244 if (hapd) {
4245 if (!fst_iface_detach(ifname)) {
4246 hapd->iface->fst = NULL;
4247 hapd->iface->fst_ies = NULL;
4248 return 0;
4249 }
4250 }
4251 }
4252
4253 return -EINVAL;
4254}
4255
4256#endif /* CONFIG_FST */
4257
4258
4259static struct hostapd_data *
4260hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4261 const char *ifname)
4262{
4263 size_t i, j;
4264
4265 for (i = 0; i < interfaces->count; i++) {
4266 struct hostapd_iface *iface = interfaces->iface[i];
4267
4268 for (j = 0; j < iface->num_bss; j++) {
4269 struct hostapd_data *hapd;
4270
4271 hapd = iface->bss[j];
4272 if (os_strcmp(ifname, hapd->conf->iface) == 0)
4273 return hapd;
4274 }
4275 }
4276
4277 return NULL;
4278}
4279
4280
4281static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4282 struct hostapd_data *dst_hapd,
4283 const char *param)
4284{
4285 int res;
4286 char *value;
4287
4288 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4289 if (!value) {
4290 wpa_printf(MSG_ERROR,
4291 "DUP: cannot allocate buffer to stringify %s",
4292 param);
4293 goto error_return;
4294 }
4295
4296 if (os_strcmp(param, "wpa") == 0) {
4297 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4298 src_hapd->conf->wpa);
4299 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4300 src_hapd->conf->wpa_key_mgmt) {
4301 res = hostapd_ctrl_iface_get_key_mgmt(
4302 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4303 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4304 goto error_stringify;
4305 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4306 src_hapd->conf->wpa_pairwise) {
4307 res = wpa_write_ciphers(value,
4308 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4309 src_hapd->conf->wpa_pairwise, " ");
4310 if (res < 0)
4311 goto error_stringify;
4312 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4313 src_hapd->conf->rsn_pairwise) {
4314 res = wpa_write_ciphers(value,
4315 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4316 src_hapd->conf->rsn_pairwise, " ");
4317 if (res < 0)
4318 goto error_stringify;
4319 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4320 src_hapd->conf->ssid.wpa_passphrase) {
4321 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4322 src_hapd->conf->ssid.wpa_passphrase);
4323 } else if (os_strcmp(param, "wpa_psk") == 0 &&
4324 src_hapd->conf->ssid.wpa_psk_set) {
4325 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4326 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4327 } else {
4328 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4329 goto error_return;
4330 }
4331
4332 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4333 os_free(value);
4334 return res;
4335
4336error_stringify:
4337 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4338error_return:
4339 os_free(value);
4340 return -1;
4341}
4342
4343
4344static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004345hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4346 const char *input,
4347 char *reply, int reply_size)
4348{
4349 size_t i, j;
4350 int res;
4351 char *pos, *end;
4352 struct hostapd_iface *iface;
4353 int show_ctrl = 0;
4354
4355 if (input)
4356 show_ctrl = !!os_strstr(input, "ctrl");
4357
4358 pos = reply;
4359 end = reply + reply_size;
4360
4361 for (i = 0; i < interfaces->count; i++) {
4362 iface = interfaces->iface[i];
4363
4364 for (j = 0; j < iface->num_bss; j++) {
4365 struct hostapd_bss_config *conf;
4366
4367 conf = iface->conf->bss[j];
4368 if (show_ctrl)
4369 res = os_snprintf(pos, end - pos,
4370 "%s ctrl_iface=%s\n",
4371 conf->iface,
4372 conf->ctrl_interface ?
4373 conf->ctrl_interface : "N/A");
4374 else
4375 res = os_snprintf(pos, end - pos, "%s\n",
4376 conf->iface);
4377 if (os_snprintf_error(end - pos, res)) {
4378 *pos = '\0';
4379 return pos - reply;
4380 }
4381 pos += res;
4382 }
4383 }
4384
4385 return pos - reply;
4386}
4387
4388
4389static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004390hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4391 char *cmd)
4392{
4393 char *p_start = cmd, *p_end;
4394 struct hostapd_data *src_hapd, *dst_hapd;
4395
4396 /* cmd: "<src ifname> <dst ifname> <variable name> */
4397
4398 p_end = os_strchr(p_start, ' ');
4399 if (!p_end) {
4400 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4401 cmd);
4402 return -1;
4403 }
4404
4405 *p_end = '\0';
4406 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4407 if (!src_hapd) {
4408 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4409 p_start);
4410 return -1;
4411 }
4412
4413 p_start = p_end + 1;
4414 p_end = os_strchr(p_start, ' ');
4415 if (!p_end) {
4416 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4417 cmd);
4418 return -1;
4419 }
4420
4421 *p_end = '\0';
4422 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4423 if (!dst_hapd) {
4424 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4425 p_start);
4426 return -1;
4427 }
4428
4429 p_start = p_end + 1;
4430 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4431}
4432
4433
4434static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4435 const char *ifname,
4436 char *buf, char *reply,
4437 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004438 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004439 socklen_t fromlen)
4440{
4441 struct hostapd_data *hapd;
4442
4443 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4444 if (hapd == NULL) {
4445 int res;
4446
4447 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4448 if (os_snprintf_error(reply_size, res))
4449 return -1;
4450 return res;
4451 }
4452
4453 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4454 from, fromlen);
4455}
4456
4457
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004458static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4459 void *sock_ctx)
4460{
Hai Shalomfdcde762020-04-02 11:19:20 -07004461 struct hapd_interfaces *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004462 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004463 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004464 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004465 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004466 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004467 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004468 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004469#ifdef CONFIG_CTRL_IFACE_UDP
Hai Shalomfdcde762020-04-02 11:19:20 -07004470 unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004471#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004472
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004473 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004474 (struct sockaddr *) &from, &fromlen);
4475 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004476 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4477 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004478 return;
4479 }
4480 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004481 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004482
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004483 reply = os_malloc(reply_size);
4484 if (reply == NULL) {
4485 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4486 fromlen) < 0) {
4487 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4488 strerror(errno));
4489 }
4490 return;
4491 }
4492
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004493 os_memcpy(reply, "OK\n", 3);
4494 reply_len = 3;
4495
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004496#ifdef CONFIG_CTRL_IFACE_UDP
4497 if (os_strcmp(buf, "GET_COOKIE") == 0) {
4498 os_memcpy(reply, "COOKIE=", 7);
Hai Shalomfdcde762020-04-02 11:19:20 -07004499 wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4500 interfaces->ctrl_iface_cookie,
4501 CTRL_IFACE_COOKIE_LEN);
4502 reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004503 goto send_reply;
4504 }
4505
4506 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
Hai Shalomfdcde762020-04-02 11:19:20 -07004507 hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004508 wpa_printf(MSG_DEBUG,
4509 "CTRL: No cookie in the request - drop request");
4510 os_free(reply);
4511 return;
4512 }
4513
Hai Shalomfdcde762020-04-02 11:19:20 -07004514 if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4515 CTRL_IFACE_COOKIE_LEN) != 0) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004516 wpa_printf(MSG_DEBUG,
4517 "CTRL: Invalid cookie in the request - drop request");
4518 os_free(reply);
4519 return;
4520 }
4521
Hai Shalomfdcde762020-04-02 11:19:20 -07004522 buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004523 while (*buf == ' ')
4524 buf++;
4525#endif /* CONFIG_CTRL_IFACE_UDP */
4526
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004527 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4528 char *pos = os_strchr(buf + 7, ' ');
4529
4530 if (pos) {
4531 *pos++ = '\0';
4532 reply_len = hostapd_global_ctrl_iface_ifname(
4533 interfaces, buf + 7, pos, reply, reply_size,
4534 &from, fromlen);
4535 goto send_reply;
4536 }
4537 }
4538
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004539 if (os_strcmp(buf, "PING") == 0) {
4540 os_memcpy(reply, "PONG\n", 5);
4541 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004542 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4543 if (wpa_debug_reopen_file() < 0)
4544 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004545 } else if (os_strcmp(buf, "FLUSH") == 0) {
4546 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004547 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4548 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4549 reply_len = -1;
4550 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4551 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4552 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004553 } else if (os_strcmp(buf, "ATTACH") == 0) {
4554 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004555 fromlen, NULL))
4556 reply_len = -1;
4557 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4558 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4559 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004560 reply_len = -1;
4561 } else if (os_strcmp(buf, "DETACH") == 0) {
4562 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4563 fromlen))
4564 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004565#ifdef CONFIG_MODULE_TESTS
4566 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004567 if (hapd_module_tests() < 0)
4568 reply_len = -1;
4569#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004570#ifdef CONFIG_FST
4571 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4572 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4573 reply_len = os_snprintf(reply, reply_size, "OK\n");
4574 else
4575 reply_len = -1;
4576 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4577 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4578 reply_len = os_snprintf(reply, reply_size, "OK\n");
4579 else
4580 reply_len = -1;
4581 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4582 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4583#endif /* CONFIG_FST */
4584 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4585 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4586 buf + 12))
4587 reply_len = os_snprintf(reply, reply_size, "OK\n");
4588 else
4589 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004590 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4591 reply_len = hostapd_global_ctrl_iface_interfaces(
4592 interfaces, buf + 10, reply, sizeof(buffer));
4593 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4594 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004595 } else {
4596 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4597 "ignored");
4598 reply_len = -1;
4599 }
4600
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004601send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004602 if (reply_len < 0) {
4603 os_memcpy(reply, "FAIL\n", 5);
4604 reply_len = 5;
4605 }
4606
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004607 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4608 fromlen) < 0) {
4609 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4610 strerror(errno));
4611 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004612 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004613}
4614
4615
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004616#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004617static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4618{
4619 char *buf;
4620 size_t len;
4621
4622 if (interface->global_iface_path == NULL)
4623 return NULL;
4624
4625 len = os_strlen(interface->global_iface_path) +
4626 os_strlen(interface->global_iface_name) + 2;
4627 buf = os_malloc(len);
4628 if (buf == NULL)
4629 return NULL;
4630
4631 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4632 interface->global_iface_name);
4633 buf[len - 1] = '\0';
4634 return buf;
4635}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004636#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004637
4638
4639int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4640{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004641#ifdef CONFIG_CTRL_IFACE_UDP
4642 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4643 char p[32] = { 0 };
4644 char *pos;
4645 struct addrinfo hints = { 0 }, *res, *saveres;
4646 int n;
4647
4648 if (interface->global_ctrl_sock > -1) {
4649 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4650 return 0;
4651 }
4652
4653 if (interface->global_iface_path == NULL)
4654 return 0;
4655
4656 pos = os_strstr(interface->global_iface_path, "udp:");
4657 if (pos) {
4658 pos += 4;
4659 port = atoi(pos);
4660 if (port <= 0) {
4661 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4662 goto fail;
4663 }
4664 }
4665
Hai Shalomfdcde762020-04-02 11:19:20 -07004666 os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004667
4668#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4669 hints.ai_flags = AI_PASSIVE;
4670#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4671
4672#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4673 hints.ai_family = AF_INET6;
4674#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4675 hints.ai_family = AF_INET;
4676#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4677 hints.ai_socktype = SOCK_DGRAM;
4678
4679try_again:
4680 os_snprintf(p, sizeof(p), "%d", port);
4681 n = getaddrinfo(NULL, p, &hints, &res);
4682 if (n) {
4683 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4684 goto fail;
4685 }
4686
4687 saveres = res;
4688 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4689 res->ai_protocol);
4690 if (interface->global_ctrl_sock < 0) {
4691 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4692 goto fail;
4693 }
4694
4695 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4696 0) {
4697 port++;
4698 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4699 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4700 goto try_again;
4701 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4702 goto fail;
4703 }
4704
4705 freeaddrinfo(saveres);
4706
4707 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4708
4709 if (eloop_register_read_sock(interface->global_ctrl_sock,
4710 hostapd_global_ctrl_iface_receive,
4711 interface, NULL) < 0) {
4712 hostapd_global_ctrl_iface_deinit(interface);
4713 return -1;
4714 }
4715
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004716 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4717
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004718 return 0;
4719
4720fail:
4721 if (interface->global_ctrl_sock >= 0)
4722 close(interface->global_ctrl_sock);
4723 return -1;
4724#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004725 struct sockaddr_un addr;
4726 int s = -1;
4727 char *fname = NULL;
4728
4729 if (interface->global_iface_path == NULL) {
4730 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4731 return 0;
4732 }
4733
4734 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4735 if (errno == EEXIST) {
4736 wpa_printf(MSG_DEBUG, "Using existing control "
4737 "interface directory.");
4738 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004739 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4740 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004741 goto fail;
4742 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004743 } else if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004744 lchown(interface->global_iface_path, -1,
4745 interface->ctrl_iface_group) < 0) {
4746 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004747 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004748 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004749 }
4750
4751 if (os_strlen(interface->global_iface_path) + 1 +
4752 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4753 goto fail;
4754
4755 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4756 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004757 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004758 goto fail;
4759 }
4760
4761 os_memset(&addr, 0, sizeof(addr));
4762#ifdef __FreeBSD__
4763 addr.sun_len = sizeof(addr);
4764#endif /* __FreeBSD__ */
4765 addr.sun_family = AF_UNIX;
4766 fname = hostapd_global_ctrl_iface_path(interface);
4767 if (fname == NULL)
4768 goto fail;
4769 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4770 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4771 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4772 strerror(errno));
4773 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4774 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4775 " allow connections - assuming it was left"
4776 "over from forced program termination");
4777 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004778 wpa_printf(MSG_ERROR,
4779 "Could not unlink existing ctrl_iface socket '%s': %s",
4780 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004781 goto fail;
4782 }
4783 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4784 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004785 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4786 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004787 goto fail;
4788 }
4789 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4790 "ctrl_iface socket '%s'", fname);
4791 } else {
4792 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4793 "be in use - cannot override it");
4794 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4795 "not used anymore", fname);
4796 os_free(fname);
4797 fname = NULL;
4798 goto fail;
4799 }
4800 }
4801
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004802 if (interface->ctrl_iface_group &&
Hai Shalom74f70d42019-02-11 14:42:39 -08004803 lchown(fname, -1, interface->ctrl_iface_group) < 0) {
4804 wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004805 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004806 goto fail;
4807 }
4808
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004810 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4811 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004812 goto fail;
4813 }
4814 os_free(fname);
4815
4816 interface->global_ctrl_sock = s;
4817 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4818 interface, NULL);
4819
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004820 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4821
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004822 return 0;
4823
4824fail:
4825 if (s >= 0)
4826 close(s);
4827 if (fname) {
4828 unlink(fname);
4829 os_free(fname);
4830 }
4831 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004832#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004833}
4834
4835
4836void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4837{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004838#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004839 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004840#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004841 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004842
4843 if (interfaces->global_ctrl_sock > -1) {
4844 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4845 close(interfaces->global_ctrl_sock);
4846 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004847#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004848 fname = hostapd_global_ctrl_iface_path(interfaces);
4849 if (fname) {
4850 unlink(fname);
4851 os_free(fname);
4852 }
4853
4854 if (interfaces->global_iface_path &&
4855 rmdir(interfaces->global_iface_path) < 0) {
4856 if (errno == ENOTEMPTY) {
4857 wpa_printf(MSG_DEBUG, "Control interface "
4858 "directory not empty - leaving it "
4859 "behind");
4860 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004861 wpa_printf(MSG_ERROR,
4862 "rmdir[ctrl_interface=%s]: %s",
4863 interfaces->global_iface_path,
4864 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004865 }
4866 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004867#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004868 }
4869
4870 os_free(interfaces->global_iface_path);
4871 interfaces->global_iface_path = NULL;
4872
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004873 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4874 struct wpa_ctrl_dst, list)
4875 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004876}
4877
4878
Roshan Pius3a1667e2018-07-03 15:17:14 -07004879static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4880 const char *buf)
4881{
4882 /* Enable Probe Request events based on explicit request.
4883 * Other events are enabled by default.
4884 */
4885 if (str_starts(buf, RX_PROBE_REQUEST))
4886 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4887 return 1;
4888}
4889
4890
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004891static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
4892 const char *ifname, int level,
4893 const char *buf, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894{
4895 struct wpa_ctrl_dst *dst, *next;
4896 struct msghdr msg;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004897 int idx, res;
4898 struct iovec io[5];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899 char levelstr[10];
4900
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004901 if (sock < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 return;
4903
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004904 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4905 if (os_snprintf_error(sizeof(levelstr), res))
4906 return;
4907 idx = 0;
4908 if (ifname) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004909 io[idx].iov_base = "IFNAME=";
4910 io[idx].iov_len = 7;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004911 idx++;
4912 io[idx].iov_base = (char *) ifname;
4913 io[idx].iov_len = os_strlen(ifname);
4914 idx++;
4915 io[idx].iov_base = " ";
4916 io[idx].iov_len = 1;
4917 idx++;
4918 }
4919 io[idx].iov_base = levelstr;
4920 io[idx].iov_len = os_strlen(levelstr);
4921 idx++;
4922 io[idx].iov_base = (char *) buf;
4923 io[idx].iov_len = len;
4924 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004925 os_memset(&msg, 0, sizeof(msg));
4926 msg.msg_iov = io;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004927 msg.msg_iovlen = idx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928
4929 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004930 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004931 if ((level >= dst->debug_level) &&
4932 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004933 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4934 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 msg.msg_name = &dst->addr;
4936 msg.msg_namelen = dst->addrlen;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004937 if (sendmsg(sock, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 int _errno = errno;
4939 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4940 "%d - %s",
4941 idx, errno, strerror(errno));
4942 dst->errors++;
4943 if (dst->errors > 10 || _errno == ENOENT) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004944 ctrl_iface_detach(ctrl_dst,
4945 &dst->addr,
4946 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 }
4948 } else
4949 dst->errors = 0;
4950 }
4951 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 }
4953}
4954
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004955
4956static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
4957 enum wpa_msg_type type,
4958 const char *buf, size_t len)
4959{
4960 if (type != WPA_MSG_NO_GLOBAL) {
4961 hostapd_ctrl_iface_send_internal(
4962 hapd->iface->interfaces->global_ctrl_sock,
4963 &hapd->iface->interfaces->global_ctrl_dst,
4964 type != WPA_MSG_PER_INTERFACE ?
4965 NULL : hapd->conf->iface,
4966 level, buf, len);
4967 }
4968
4969 if (type != WPA_MSG_ONLY_GLOBAL) {
4970 hostapd_ctrl_iface_send_internal(
4971 hapd->ctrl_sock, &hapd->ctrl_dst,
4972 NULL, level, buf, len);
4973 }
4974}
4975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976#endif /* CONFIG_NATIVE_WINDOWS */