blob: 2d68e88f6503a1641cbd5e66d26a61fc20c1ff77 [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
14#include <net/ethernet.h>
15#include <netinet/ip.h>
16#endif /* CONFIG_TESTING_OPTIONS */
17
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <sys/un.h>
19#include <sys/stat.h>
20#include <stddef.h>
21
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080022#ifdef CONFIG_CTRL_IFACE_UDP
23#include <netdb.h>
24#endif /* CONFIG_CTRL_IFACE_UDP */
25
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "utils/common.h"
27#include "utils/eloop.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070028#include "utils/module_tests.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "common/version.h"
30#include "common/ieee802_11_defs.h"
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080031#include "common/ctrl_iface_common.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070032#ifdef CONFIG_DPP
33#include "common/dpp.h"
34#endif /* CONFIG_DPP */
35#include "common/wpa_ctrl.h"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080036#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080038#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080040#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080041#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "ap/hostapd.h"
43#include "ap/ap_config.h"
44#include "ap/ieee802_1x.h"
45#include "ap/wpa_auth.h"
46#include "ap/ieee802_11.h"
47#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#include "ap/wps_hostapd.h"
49#include "ap/ctrl_iface_ap.h"
50#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080051#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080052#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070053#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080054#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070055#include "ap/neighbor_db.h"
56#include "ap/rrm.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070057#include "ap/dpp_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058#include "wps/wps_defs.h"
59#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080060#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070061#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062#include "ctrl_iface.h"
63
64
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080065#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
66
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080067#ifdef CONFIG_CTRL_IFACE_UDP
68#define COOKIE_LEN 8
69static unsigned char cookie[COOKIE_LEN];
70static unsigned char gcookie[COOKIE_LEN];
71#define HOSTAPD_CTRL_IFACE_PORT 8877
72#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
73#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
74#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
75#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076
77static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020078 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 const char *buf, size_t len);
80
81
82static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080083 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -070084 socklen_t fromlen, const char *input)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085{
Roshan Pius3a1667e2018-07-03 15:17:14 -070086 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087}
88
89
90static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080091 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 socklen_t fromlen)
93{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080094 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095}
96
97
98static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080099 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 socklen_t fromlen,
101 char *level)
102{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -0800103 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104}
105
106
107static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
108 const char *txtaddr)
109{
110 u8 addr[ETH_ALEN];
111 struct sta_info *sta;
112
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
114
115 if (hwaddr_aton(txtaddr, addr))
116 return -1;
117
118 sta = ap_get_sta(hapd, addr);
119 if (sta)
120 return 0;
121
122 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
123 "notification", MAC2STR(addr));
124 sta = ap_sta_add(hapd, addr);
125 if (sta == NULL)
126 return -1;
127
128 hostapd_new_assoc_sta(hapd, sta, 0);
129 return 0;
130}
131
132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133#ifdef CONFIG_IEEE80211W
134#ifdef NEED_AP_MLME
135static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
136 const char *txtaddr)
137{
138 u8 addr[ETH_ALEN];
139 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
140
141 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
142
143 if (hwaddr_aton(txtaddr, addr) ||
144 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
145 return -1;
146
147 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
148
149 return 0;
150}
151#endif /* NEED_AP_MLME */
152#endif /* CONFIG_IEEE80211W */
153
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 */
886 os_memset(bss_term_dur, 2, 8);
887 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700995#endif /* CONFIG_WNM_AP */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800996
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800997
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800998static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
999 char *buf, size_t buflen)
1000{
1001 int ret = 0;
1002 char *pos, *end;
1003
1004 pos = buf;
1005 end = buf + buflen;
1006
1007 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1008
1009 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1010 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1011 if (os_snprintf_error(end - pos, ret))
1012 return pos - buf;
1013 pos += ret;
1014 }
1015 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1016 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1017 if (os_snprintf_error(end - pos, ret))
1018 return pos - buf;
1019 pos += ret;
1020 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001021#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1023 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1024 if (os_snprintf_error(end - pos, ret))
1025 return pos - buf;
1026 pos += ret;
1027 }
1028 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1029 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1030 if (os_snprintf_error(end - pos, ret))
1031 return pos - buf;
1032 pos += ret;
1033 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001034#ifdef CONFIG_SHA384
1035 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1036 ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
1037 if (os_snprintf_error(end - pos, ret))
1038 return pos - buf;
1039 pos += ret;
1040 }
1041#endif /* CONFIG_SHA384 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001042#ifdef CONFIG_SAE
1043 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1044 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1045 if (os_snprintf_error(end - pos, ret))
1046 return pos - buf;
1047 pos += ret;
1048 }
1049#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001050#ifdef CONFIG_FILS
1051 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1052 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1053 if (os_snprintf_error(end - pos, ret))
1054 return pos - buf;
1055 pos += ret;
1056 }
1057 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1058 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1059 if (os_snprintf_error(end - pos, ret))
1060 return pos - buf;
1061 pos += ret;
1062 }
1063#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001064#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001065#ifdef CONFIG_IEEE80211W
1066 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1067 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1068 if (os_snprintf_error(end - pos, ret))
1069 return pos - buf;
1070 pos += ret;
1071 }
1072 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1073 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1074 if (os_snprintf_error(end - pos, ret))
1075 return pos - buf;
1076 pos += ret;
1077 }
1078#endif /* CONFIG_IEEE80211W */
1079#ifdef CONFIG_SAE
1080 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1081 ret = os_snprintf(pos, end - pos, "SAE ");
1082 if (os_snprintf_error(end - pos, ret))
1083 return pos - buf;
1084 pos += ret;
1085 }
1086#endif /* CONFIG_SAE */
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1088 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1089 if (os_snprintf_error(end - pos, ret))
1090 return pos - buf;
1091 pos += ret;
1092 }
1093 if (hapd->conf->wpa_key_mgmt &
1094 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1095 ret = os_snprintf(pos, end - pos,
1096 "WPA-EAP-SUITE-B-192 ");
1097 if (os_snprintf_error(end - pos, ret))
1098 return pos - buf;
1099 pos += ret;
1100 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001101#ifdef CONFIG_FILS
1102 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1103 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1104 if (os_snprintf_error(end - pos, ret))
1105 return pos - buf;
1106 pos += ret;
1107 }
1108 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1109 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1110 if (os_snprintf_error(end - pos, ret))
1111 return pos - buf;
1112 pos += ret;
1113 }
1114#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001115
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001116#ifdef CONFIG_OWE
1117 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
1118 ret = os_snprintf(pos, end - pos, "OWE ");
1119 if (os_snprintf_error(end - pos, ret))
1120 return pos - buf;
1121 pos += ret;
1122 }
1123#endif /* CONFIG_OWE */
1124
1125#ifdef CONFIG_DPP
1126 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
1127 ret = os_snprintf(pos, end - pos, "DPP ");
1128 if (os_snprintf_error(end - pos, ret))
1129 return pos - buf;
1130 pos += ret;
1131 }
1132#endif /* CONFIG_DPP */
1133
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001134 if (pos > buf && *(pos - 1) == ' ') {
1135 *(pos - 1) = '\0';
1136 pos--;
1137 }
1138
1139 return pos - buf;
1140}
1141
1142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1144 char *buf, size_t buflen)
1145{
1146 int ret;
1147 char *pos, *end;
1148
1149 pos = buf;
1150 end = buf + buflen;
1151
1152 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1153 "ssid=%s\n",
1154 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 wpa_ssid_txt(hapd->conf->ssid.ssid,
1156 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001157 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 return pos - buf;
1159 pos += ret;
1160
1161#ifdef CONFIG_WPS
1162 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1163 hapd->conf->wps_state == 0 ? "disabled" :
1164 (hapd->conf->wps_state == 1 ? "not configured" :
1165 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001166 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 return pos - buf;
1168 pos += ret;
1169
1170 if (hapd->conf->wps_state && hapd->conf->wpa &&
1171 hapd->conf->ssid.wpa_passphrase) {
1172 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1173 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001174 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 return pos - buf;
1176 pos += ret;
1177 }
1178
1179 if (hapd->conf->wps_state && hapd->conf->wpa &&
1180 hapd->conf->ssid.wpa_psk &&
1181 hapd->conf->ssid.wpa_psk->group) {
1182 char hex[PMK_LEN * 2 + 1];
1183 wpa_snprintf_hex(hex, sizeof(hex),
1184 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1185 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001186 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 return pos - buf;
1188 pos += ret;
1189 }
1190#endif /* CONFIG_WPS */
1191
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001192 if (hapd->conf->wpa) {
1193 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1194 if (os_snprintf_error(end - pos, ret))
1195 return pos - buf;
1196 pos += ret;
1197 }
1198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1200 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001201 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 return pos - buf;
1203 pos += ret;
1204
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001205 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206
1207 ret = os_snprintf(pos, end - pos, "\n");
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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001213 if (hapd->conf->wpa) {
1214 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1215 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001216 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217 return pos - buf;
1218 pos += ret;
1219 }
1220
1221 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1222 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001223 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 return pos - buf;
1225 pos += ret;
1226
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001227 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1228 " ");
1229 if (ret < 0)
1230 return pos - buf;
1231 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232
1233 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001234 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 return pos - buf;
1236 pos += ret;
1237 }
1238
1239 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1240 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 return pos - buf;
1243 pos += ret;
1244
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001245 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001246 " ");
1247 if (ret < 0)
1248 return pos - buf;
1249 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250
1251 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001252 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 return pos - buf;
1254 pos += ret;
1255 }
1256
1257 return pos - buf;
1258}
1259
1260
Roshan Pius3a1667e2018-07-03 15:17:14 -07001261static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1262{
1263 struct sta_info *sta;
1264 struct vlan_description vlan_id;
1265
1266 if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1267 return;
1268
1269 for (sta = hapd->sta_list; sta; sta = sta->next) {
1270 if (!hostapd_maclist_found(hapd->conf->accept_mac,
1271 hapd->conf->num_accept_mac,
1272 sta->addr, &vlan_id) ||
1273 (vlan_id.notempty &&
1274 vlan_compare(&vlan_id, sta->vlan_desc)))
1275 ap_sta_disconnect(hapd, sta, sta->addr,
1276 WLAN_REASON_UNSPECIFIED);
1277 }
1278}
1279
1280
1281static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1282{
1283 struct sta_info *sta;
1284 struct vlan_description vlan_id;
1285
1286 for (sta = hapd->sta_list; sta; sta = sta->next) {
1287 if (hostapd_maclist_found(hapd->conf->deny_mac,
1288 hapd->conf->num_deny_mac, sta->addr,
1289 &vlan_id) &&
1290 (!vlan_id.notempty ||
1291 !vlan_compare(&vlan_id, sta->vlan_desc)))
1292 ap_sta_disconnect(hapd, sta, sta->addr,
1293 WLAN_REASON_UNSPECIFIED);
1294 }
1295}
1296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1298{
1299 char *value;
1300 int ret = 0;
1301
1302 value = os_strchr(cmd, ' ');
1303 if (value == NULL)
1304 return -1;
1305 *value++ = '\0';
1306
1307 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1308 if (0) {
1309#ifdef CONFIG_WPS_TESTING
1310 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1311 long int val;
1312 val = strtol(value, NULL, 0);
1313 if (val < 0 || val > 0xff) {
1314 ret = -1;
1315 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1316 "wps_version_number %ld", val);
1317 } else {
1318 wps_version_number = val;
1319 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1320 "version %u.%u",
1321 (wps_version_number & 0xf0) >> 4,
1322 wps_version_number & 0x0f);
1323 hostapd_wps_update_ie(hapd);
1324 }
1325 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1326 wps_testing_dummy_cred = atoi(value);
1327 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1328 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001329 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1330 wps_corrupt_pkhash = atoi(value);
1331 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1332 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001334#ifdef CONFIG_TESTING_OPTIONS
1335 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1336 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001337 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1338 hapd->ext_eapol_frame_io = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001339#ifdef CONFIG_DPP
1340 } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1341 os_free(hapd->dpp_config_obj_override);
1342 hapd->dpp_config_obj_override = os_strdup(value);
1343 } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1344 os_free(hapd->dpp_discovery_override);
1345 hapd->dpp_discovery_override = os_strdup(value);
1346 } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1347 os_free(hapd->dpp_groups_override);
1348 hapd->dpp_groups_override = os_strdup(value);
1349 } else if (os_strcasecmp(cmd,
1350 "dpp_ignore_netaccesskey_mismatch") == 0) {
1351 hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001352 } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1353 dpp_test = atoi(value);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001354#endif /* CONFIG_DPP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001355#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001356#ifdef CONFIG_MBO
1357 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1358 int val;
1359
1360 if (!hapd->conf->mbo_enabled)
1361 return -1;
1362
1363 val = atoi(value);
1364 if (val < 0 || val > 1)
1365 return -1;
1366
1367 hapd->mbo_assoc_disallow = val;
1368 ieee802_11_update_beacons(hapd->iface);
1369
1370 /*
1371 * TODO: Need to configure drivers that do AP MLME offload with
1372 * disallowing station logic.
1373 */
1374#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001375#ifdef CONFIG_DPP
1376 } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1377 os_free(hapd->dpp_configurator_params);
1378 hapd->dpp_configurator_params = os_strdup(value);
1379#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001382 if (ret)
1383 return ret;
1384
1385 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001386 hostapd_disassoc_deny_mac(hapd);
1387 } else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1388 hostapd_disassoc_accept_mac(hapd);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 }
1391
1392 return ret;
1393}
1394
1395
1396static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1397 char *buf, size_t buflen)
1398{
1399 int res;
1400
1401 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1402
1403 if (os_strcmp(cmd, "version") == 0) {
1404 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001405 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 return -1;
1407 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001408 } else if (os_strcmp(cmd, "tls_library") == 0) {
1409 res = tls_get_library_version(buf, buflen);
1410 if (os_snprintf_error(buflen, res))
1411 return -1;
1412 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 }
1414
1415 return -1;
1416}
1417
1418
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001419static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1420{
1421 if (hostapd_enable_iface(iface) < 0) {
1422 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1423 return -1;
1424 }
1425 return 0;
1426}
1427
1428
1429static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1430{
1431 if (hostapd_reload_iface(iface) < 0) {
1432 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1433 return -1;
1434 }
1435 return 0;
1436}
1437
1438
1439static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1440{
1441 if (hostapd_disable_iface(iface) < 0) {
1442 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1443 return -1;
1444 }
1445 return 0;
1446}
1447
1448
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001449#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001451static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1452{
1453 union wpa_event_data data;
1454 char *pos, *param;
1455 enum wpa_event_type event;
1456
1457 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1458
1459 os_memset(&data, 0, sizeof(data));
1460
1461 param = os_strchr(cmd, ' ');
1462 if (param == NULL)
1463 return -1;
1464 *param++ = '\0';
1465
1466 if (os_strcmp(cmd, "DETECTED") == 0)
1467 event = EVENT_DFS_RADAR_DETECTED;
1468 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1469 event = EVENT_DFS_CAC_FINISHED;
1470 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1471 event = EVENT_DFS_CAC_ABORTED;
1472 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1473 event = EVENT_DFS_NOP_FINISHED;
1474 else {
1475 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1476 cmd);
1477 return -1;
1478 }
1479
1480 pos = os_strstr(param, "freq=");
1481 if (pos)
1482 data.dfs_event.freq = atoi(pos + 5);
1483
1484 pos = os_strstr(param, "ht_enabled=1");
1485 if (pos)
1486 data.dfs_event.ht_enabled = 1;
1487
1488 pos = os_strstr(param, "chan_offset=");
1489 if (pos)
1490 data.dfs_event.chan_offset = atoi(pos + 12);
1491
1492 pos = os_strstr(param, "chan_width=");
1493 if (pos)
1494 data.dfs_event.chan_width = atoi(pos + 11);
1495
1496 pos = os_strstr(param, "cf1=");
1497 if (pos)
1498 data.dfs_event.cf1 = atoi(pos + 4);
1499
1500 pos = os_strstr(param, "cf2=");
1501 if (pos)
1502 data.dfs_event.cf2 = atoi(pos + 4);
1503
1504 wpa_supplicant_event(hapd, event, &data);
1505
1506 return 0;
1507}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001508
1509
1510static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1511{
1512 size_t len;
1513 u8 *buf;
1514 int res;
1515
1516 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1517
1518 len = os_strlen(cmd);
1519 if (len & 1)
1520 return -1;
1521 len /= 2;
1522
1523 buf = os_malloc(len);
1524 if (buf == NULL)
1525 return -1;
1526
1527 if (hexstr2bin(cmd, buf, len) < 0) {
1528 os_free(buf);
1529 return -1;
1530 }
1531
1532 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1533 os_free(buf);
1534 return res;
1535}
1536
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001537
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001538static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1539 char *cmd)
1540{
1541 char *pos, *param;
1542 size_t len;
1543 u8 *buf;
1544 int stype = 0, ok = 0;
1545 union wpa_event_data event;
1546
1547 if (!hapd->ext_mgmt_frame_handling)
1548 return -1;
1549
1550 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1551
1552 wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1553
1554 pos = cmd;
1555 param = os_strstr(pos, "stype=");
1556 if (param) {
1557 param += 6;
1558 stype = atoi(param);
1559 }
1560
1561 param = os_strstr(pos, " ok=");
1562 if (param) {
1563 param += 4;
1564 ok = atoi(param);
1565 }
1566
1567 param = os_strstr(pos, " buf=");
1568 if (!param)
1569 return -1;
1570 param += 5;
1571
1572 len = os_strlen(param);
1573 if (len & 1)
1574 return -1;
1575 len /= 2;
1576
1577 buf = os_malloc(len);
1578 if (!buf || hexstr2bin(param, buf, len) < 0) {
1579 os_free(buf);
1580 return -1;
1581 }
1582
1583 os_memset(&event, 0, sizeof(event));
1584 event.tx_status.type = WLAN_FC_TYPE_MGMT;
1585 event.tx_status.data = buf;
1586 event.tx_status.data_len = len;
1587 event.tx_status.stype = stype;
1588 event.tx_status.ack = ok;
1589 hapd->ext_mgmt_frame_handling = 0;
1590 wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1591 hapd->ext_mgmt_frame_handling = 1;
1592
1593 os_free(buf);
1594
1595 return 0;
1596}
1597
1598
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001599static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1600 char *cmd)
1601{
1602 char *pos, *param;
1603 size_t len;
1604 u8 *buf;
1605 int freq = 0, datarate = 0, ssi_signal = 0;
1606 union wpa_event_data event;
1607
1608 if (!hapd->ext_mgmt_frame_handling)
1609 return -1;
1610
1611 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1612
1613 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1614
1615 pos = cmd;
1616 param = os_strstr(pos, "freq=");
1617 if (param) {
1618 param += 5;
1619 freq = atoi(param);
1620 }
1621
1622 param = os_strstr(pos, " datarate=");
1623 if (param) {
1624 param += 10;
1625 datarate = atoi(param);
1626 }
1627
1628 param = os_strstr(pos, " ssi_signal=");
1629 if (param) {
1630 param += 12;
1631 ssi_signal = atoi(param);
1632 }
1633
1634 param = os_strstr(pos, " frame=");
1635 if (param == NULL)
1636 return -1;
1637 param += 7;
1638
1639 len = os_strlen(param);
1640 if (len & 1)
1641 return -1;
1642 len /= 2;
1643
1644 buf = os_malloc(len);
1645 if (buf == NULL)
1646 return -1;
1647
1648 if (hexstr2bin(param, buf, len) < 0) {
1649 os_free(buf);
1650 return -1;
1651 }
1652
1653 os_memset(&event, 0, sizeof(event));
1654 event.rx_mgmt.freq = freq;
1655 event.rx_mgmt.frame = buf;
1656 event.rx_mgmt.frame_len = len;
1657 event.rx_mgmt.ssi_signal = ssi_signal;
1658 event.rx_mgmt.datarate = datarate;
1659 hapd->ext_mgmt_frame_handling = 0;
1660 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1661 hapd->ext_mgmt_frame_handling = 1;
1662
1663 os_free(buf);
1664
1665 return 0;
1666}
1667
1668
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001669static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1670{
1671 char *pos;
1672 u8 src[ETH_ALEN], *buf;
1673 int used;
1674 size_t len;
1675
1676 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1677
1678 pos = cmd;
1679 used = hwaddr_aton2(pos, src);
1680 if (used < 0)
1681 return -1;
1682 pos += used;
1683 while (*pos == ' ')
1684 pos++;
1685
1686 len = os_strlen(pos);
1687 if (len & 1)
1688 return -1;
1689 len /= 2;
1690
1691 buf = os_malloc(len);
1692 if (buf == NULL)
1693 return -1;
1694
1695 if (hexstr2bin(pos, buf, len) < 0) {
1696 os_free(buf);
1697 return -1;
1698 }
1699
1700 ieee802_1x_receive(hapd, src, buf, len);
1701 os_free(buf);
1702
1703 return 0;
1704}
1705
1706
1707static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1708{
1709 size_t i;
1710 u32 sum = 0;
1711 const u16 *pos = buf;
1712
1713 for (i = 0; i < len / 2; i++)
1714 sum += *pos++;
1715
1716 while (sum >> 16)
1717 sum = (sum & 0xffff) + (sum >> 16);
1718
1719 return sum ^ 0xffff;
1720}
1721
1722
1723#define HWSIM_PACKETLEN 1500
1724#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1725
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001726static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1727 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001728{
1729 struct hostapd_data *hapd = ctx;
1730 const struct ether_header *eth;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001731 struct iphdr ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001732 const u8 *pos;
1733 unsigned int i;
1734
1735 if (len != HWSIM_PACKETLEN)
1736 return;
1737
1738 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001739 os_memcpy(&ip, eth + 1, sizeof(ip));
1740 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001741
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001742 if (ip.ihl != 5 || ip.version != 4 ||
1743 ntohs(ip.tot_len) != HWSIM_IP_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001744 return;
1745
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001746 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001747 if (*pos != (u8) i)
1748 return;
1749 pos++;
1750 }
1751
1752 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1753 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1754}
1755
1756
1757static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1758 char *cmd)
1759{
1760 int enabled = atoi(cmd);
1761 char *pos;
1762 const char *ifname;
1763
1764 if (!enabled) {
1765 if (hapd->l2_test) {
1766 l2_packet_deinit(hapd->l2_test);
1767 hapd->l2_test = NULL;
1768 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1769 "test data: Disabled");
1770 }
1771 return 0;
1772 }
1773
1774 if (hapd->l2_test)
1775 return 0;
1776
1777 pos = os_strstr(cmd, " ifname=");
1778 if (pos)
1779 ifname = pos + 8;
1780 else
1781 ifname = hapd->conf->iface;
1782
1783 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1784 ETHERTYPE_IP, hostapd_data_test_rx,
1785 hapd, 1);
1786 if (hapd->l2_test == NULL)
1787 return -1;
1788
1789 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1790
1791 return 0;
1792}
1793
1794
1795static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1796{
1797 u8 dst[ETH_ALEN], src[ETH_ALEN];
1798 char *pos;
1799 int used;
1800 long int val;
1801 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001802 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001803 struct ether_header *eth;
1804 struct iphdr *ip;
1805 u8 *dpos;
1806 unsigned int i;
1807
1808 if (hapd->l2_test == NULL)
1809 return -1;
1810
1811 /* format: <dst> <src> <tos> */
1812
1813 pos = cmd;
1814 used = hwaddr_aton2(pos, dst);
1815 if (used < 0)
1816 return -1;
1817 pos += used;
1818 while (*pos == ' ')
1819 pos++;
1820 used = hwaddr_aton2(pos, src);
1821 if (used < 0)
1822 return -1;
1823 pos += used;
1824
1825 val = strtol(pos, NULL, 0);
1826 if (val < 0 || val > 0xff)
1827 return -1;
1828 tos = val;
1829
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001830 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1832 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1833 eth->ether_type = htons(ETHERTYPE_IP);
1834 ip = (struct iphdr *) (eth + 1);
1835 os_memset(ip, 0, sizeof(*ip));
1836 ip->ihl = 5;
1837 ip->version = 4;
1838 ip->ttl = 64;
1839 ip->tos = tos;
1840 ip->tot_len = htons(HWSIM_IP_LEN);
1841 ip->protocol = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001842 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1843 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001844 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1845 dpos = (u8 *) (ip + 1);
1846 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1847 *dpos++ = i;
1848
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001849 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001850 HWSIM_PACKETLEN) < 0)
1851 return -1;
1852
1853 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1854 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1855
1856 return 0;
1857}
1858
1859
1860static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1861 char *cmd)
1862{
1863 u8 *buf;
1864 struct ether_header *eth;
1865 struct l2_packet_data *l2 = NULL;
1866 size_t len;
1867 u16 ethertype;
1868 int res = -1;
1869 const char *ifname = hapd->conf->iface;
1870
1871 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1872 cmd += 7;
1873 ifname = cmd;
1874 cmd = os_strchr(cmd, ' ');
1875 if (cmd == NULL)
1876 return -1;
1877 *cmd++ = '\0';
1878 }
1879
1880 len = os_strlen(cmd);
1881 if (len & 1 || len < ETH_HLEN * 2)
1882 return -1;
1883 len /= 2;
1884
1885 buf = os_malloc(len);
1886 if (buf == NULL)
1887 return -1;
1888
1889 if (hexstr2bin(cmd, buf, len) < 0)
1890 goto done;
1891
1892 eth = (struct ether_header *) buf;
1893 ethertype = ntohs(eth->ether_type);
1894
1895 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1896 hostapd_data_test_rx, hapd, 1);
1897 if (l2 == NULL)
1898 goto done;
1899
1900 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1901 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1902done:
1903 if (l2)
1904 l2_packet_deinit(l2);
1905 os_free(buf);
1906
1907 return res < 0 ? -1 : 0;
1908}
1909
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001910
1911static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1912{
1913#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001914 char *pos;
1915
1916 wpa_trace_fail_after = atoi(cmd);
1917 pos = os_strchr(cmd, ':');
1918 if (pos) {
1919 pos++;
1920 os_strlcpy(wpa_trace_fail_func, pos,
1921 sizeof(wpa_trace_fail_func));
1922 } else {
1923 wpa_trace_fail_after = 0;
1924 }
1925
1926 return 0;
1927#else /* WPA_TRACE_BFD */
1928 return -1;
1929#endif /* WPA_TRACE_BFD */
1930}
1931
1932
1933static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1934 char *buf, size_t buflen)
1935{
1936#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001937 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1938 wpa_trace_fail_func);
1939#else /* WPA_TRACE_BFD */
1940 return -1;
1941#endif /* WPA_TRACE_BFD */
1942}
1943
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001944
1945static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1946{
1947#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001948 char *pos;
1949
1950 wpa_trace_test_fail_after = atoi(cmd);
1951 pos = os_strchr(cmd, ':');
1952 if (pos) {
1953 pos++;
1954 os_strlcpy(wpa_trace_test_fail_func, pos,
1955 sizeof(wpa_trace_test_fail_func));
1956 } else {
1957 wpa_trace_test_fail_after = 0;
1958 }
1959
1960 return 0;
1961#else /* WPA_TRACE_BFD */
1962 return -1;
1963#endif /* WPA_TRACE_BFD */
1964}
1965
1966
1967static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1968 char *buf, size_t buflen)
1969{
1970#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001971 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1972 wpa_trace_test_fail_func);
1973#else /* WPA_TRACE_BFD */
1974 return -1;
1975#endif /* WPA_TRACE_BFD */
1976}
1977
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001978
1979static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
1980{
1981 struct sta_info *sta;
1982 u8 addr[ETH_ALEN];
1983 u8 zero[WPA_TK_MAX_LEN];
1984
1985 os_memset(zero, 0, sizeof(zero));
1986
1987 if (hwaddr_aton(cmd, addr))
1988 return -1;
1989
1990#ifdef CONFIG_IEEE80211W
1991 if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
1992 if (hapd->last_igtk_alg == WPA_ALG_NONE)
1993 return -1;
1994
1995 wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
1996
1997 /* First, use a zero key to avoid any possible duplicate key
1998 * avoidance in the driver. */
1999 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2000 hapd->last_igtk_alg,
2001 broadcast_ether_addr,
2002 hapd->last_igtk_key_idx, 1, NULL, 0,
2003 zero, hapd->last_igtk_len) < 0)
2004 return -1;
2005
2006 /* Set the previously configured key to reset its TSC */
2007 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2008 hapd->last_igtk_alg,
2009 broadcast_ether_addr,
2010 hapd->last_igtk_key_idx, 1, NULL, 0,
2011 hapd->last_igtk,
2012 hapd->last_igtk_len);
2013 }
2014#endif /* CONFIG_IEEE80211W */
2015
2016 if (is_broadcast_ether_addr(addr)) {
2017 if (hapd->last_gtk_alg == WPA_ALG_NONE)
2018 return -1;
2019
2020 wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2021
2022 /* First, use a zero key to avoid any possible duplicate key
2023 * avoidance in the driver. */
2024 if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2025 hapd->last_gtk_alg,
2026 broadcast_ether_addr,
2027 hapd->last_gtk_key_idx, 1, NULL, 0,
2028 zero, hapd->last_gtk_len) < 0)
2029 return -1;
2030
2031 /* Set the previously configured key to reset its TSC */
2032 return hostapd_drv_set_key(hapd->conf->iface, hapd,
2033 hapd->last_gtk_alg,
2034 broadcast_ether_addr,
2035 hapd->last_gtk_key_idx, 1, NULL, 0,
2036 hapd->last_gtk, hapd->last_gtk_len);
2037 }
2038
2039 sta = ap_get_sta(hapd, addr);
2040 if (!sta)
2041 return -1;
2042
2043 if (sta->last_tk_alg == WPA_ALG_NONE)
2044 return -1;
2045
2046 wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2047 MAC2STR(sta->addr));
2048
2049 /* First, use a zero key to avoid any possible duplicate key avoidance
2050 * in the driver. */
2051 if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2052 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2053 zero, sta->last_tk_len) < 0)
2054 return -1;
2055
2056 /* Set the previously configured key to reset its TSC/RSC */
2057 return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2058 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2059 sta->last_tk, sta->last_tk_len);
2060}
2061
2062
2063static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2064{
2065 u8 addr[ETH_ALEN];
2066 const char *pos = cmd;
2067 enum wpa_alg alg;
2068 int idx, set_tx;
2069 u8 seq[6], key[WPA_TK_MAX_LEN];
2070 size_t key_len;
2071
2072 /* parameters: alg addr idx set_tx seq key */
2073
2074 alg = atoi(pos);
2075 pos = os_strchr(pos, ' ');
2076 if (!pos)
2077 return -1;
2078 pos++;
2079 if (hwaddr_aton(pos, addr))
2080 return -1;
2081 pos += 17;
2082 if (*pos != ' ')
2083 return -1;
2084 pos++;
2085 idx = atoi(pos);
2086 pos = os_strchr(pos, ' ');
2087 if (!pos)
2088 return -1;
2089 pos++;
2090 set_tx = atoi(pos);
2091 pos = os_strchr(pos, ' ');
2092 if (!pos)
2093 return -1;
2094 pos++;
2095 if (hexstr2bin(pos, seq, sizeof(6)) < 0)
2096 return -1;
2097 pos += 2 * 6;
2098 if (*pos != ' ')
2099 return -1;
2100 pos++;
2101 key_len = os_strlen(pos) / 2;
2102 if (hexstr2bin(pos, key, key_len) < 0)
2103 return -1;
2104
2105 wpa_printf(MSG_INFO, "TESTING: Set key");
2106 return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx,
2107 set_tx, seq, 6, key, key_len);
2108}
2109
2110
2111static void restore_tk(void *ctx1, void *ctx2)
2112{
2113 struct hostapd_data *hapd = ctx1;
2114 struct sta_info *sta = ctx2;
2115
2116 wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2117 MAC2STR(sta->addr));
2118 /* This does not really restore the TSC properly, so this will result
2119 * in replay protection issues for now since there is no clean way of
2120 * preventing encryption of a single EAPOL frame. */
2121 hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2122 sta->addr, sta->last_tk_key_idx, 1, NULL, 0,
2123 sta->last_tk, sta->last_tk_len);
2124}
2125
2126
2127static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2128{
2129 struct sta_info *sta;
2130 u8 addr[ETH_ALEN];
2131 int plain = os_strstr(cmd, "plaintext") != NULL;
2132
2133 if (hwaddr_aton(cmd, addr))
2134 return -1;
2135
2136 sta = ap_get_sta(hapd, addr);
2137 if (!sta || !sta->wpa_sm)
2138 return -1;
2139
2140 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2141 plain = 0; /* no need for special processing */
2142 if (plain) {
2143 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2144 MAC2STR(sta->addr));
2145 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2146 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2147 NULL, 0);
2148 }
2149
2150 wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2151 return wpa_auth_resend_m1(sta->wpa_sm,
2152 os_strstr(cmd, "change-anonce") != NULL,
2153 plain ? restore_tk : NULL, hapd, sta);
2154}
2155
2156
2157static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2158{
2159 struct sta_info *sta;
2160 u8 addr[ETH_ALEN];
2161 int plain = os_strstr(cmd, "plaintext") != NULL;
2162
2163 if (hwaddr_aton(cmd, addr))
2164 return -1;
2165
2166 sta = ap_get_sta(hapd, addr);
2167 if (!sta || !sta->wpa_sm)
2168 return -1;
2169
2170 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2171 plain = 0; /* no need for special processing */
2172 if (plain) {
2173 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2174 MAC2STR(sta->addr));
2175 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2176 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2177 NULL, 0);
2178 }
2179
2180 wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2181 return wpa_auth_resend_m3(sta->wpa_sm,
2182 plain ? restore_tk : NULL, hapd, sta);
2183}
2184
2185
2186static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2187 const char *cmd)
2188{
2189 struct sta_info *sta;
2190 u8 addr[ETH_ALEN];
2191 int plain = os_strstr(cmd, "plaintext") != NULL;
2192
2193 if (hwaddr_aton(cmd, addr))
2194 return -1;
2195
2196 sta = ap_get_sta(hapd, addr);
2197 if (!sta || !sta->wpa_sm)
2198 return -1;
2199
2200 if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2201 plain = 0; /* no need for special processing */
2202 if (plain) {
2203 wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2204 MAC2STR(sta->addr));
2205 hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2206 sta->addr, sta->last_tk_key_idx, 0, NULL, 0,
2207 NULL, 0);
2208 }
2209
2210 wpa_printf(MSG_INFO,
2211 "TESTING: Send group M1 for the same GTK and zero RSC to "
2212 MACSTR, MAC2STR(sta->addr));
2213 return wpa_auth_resend_group_m1(sta->wpa_sm,
2214 plain ? restore_tk : NULL, hapd, sta);
2215}
2216
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002217#endif /* CONFIG_TESTING_OPTIONS */
2218
2219
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002220static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2221 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002222{
2223#ifdef NEED_AP_MLME
2224 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002225 int ret;
2226 unsigned int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002227
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002228 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002229 if (ret)
2230 return ret;
2231
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002232 for (i = 0; i < iface->num_bss; i++) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002233
2234 /* Save CHAN_SWITCH VHT config */
2235 hostapd_chan_switch_vht_config(
2236 iface->bss[i], settings.freq_params.vht_enabled);
2237
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002238 ret = hostapd_switch_channel(iface->bss[i], &settings);
2239 if (ret) {
2240 /* FIX: What do we do if CSA fails in the middle of
2241 * submitting multi-BSS CSA requests? */
2242 return ret;
2243 }
2244 }
2245
2246 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002247#else /* NEED_AP_MLME */
2248 return -1;
2249#endif /* NEED_AP_MLME */
2250}
2251
2252
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002253static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2254 int reply_size, const char *param)
2255{
2256#ifdef RADIUS_SERVER
2257 if (os_strcmp(param, "radius_server") == 0) {
2258 return radius_server_get_mib(hapd->radius_srv, reply,
2259 reply_size);
2260 }
2261#endif /* RADIUS_SERVER */
2262 return -1;
2263}
2264
2265
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002266static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2267 char *buf, size_t buflen)
2268{
2269 int ret;
2270 char *pos;
2271 u8 *data = NULL;
2272 unsigned int vendor_id, subcmd;
2273 struct wpabuf *reply;
2274 size_t data_len = 0;
2275
2276 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2277 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002278 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002279 return -EINVAL;
2280
2281 subcmd = strtoul(pos, &pos, 10);
2282
2283 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002284 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002285 return -EINVAL;
2286 data_len = os_strlen(pos);
2287 }
2288
2289 if (data_len) {
2290 data_len /= 2;
2291 data = os_malloc(data_len);
2292 if (!data)
2293 return -ENOBUFS;
2294
2295 if (hexstr2bin(pos, data, data_len)) {
2296 wpa_printf(MSG_DEBUG,
2297 "Vendor command: wrong parameter format");
2298 os_free(data);
2299 return -EINVAL;
2300 }
2301 }
2302
2303 reply = wpabuf_alloc((buflen - 1) / 2);
2304 if (!reply) {
2305 os_free(data);
2306 return -ENOBUFS;
2307 }
2308
2309 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2310 reply);
2311
2312 if (ret == 0)
2313 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2314 wpabuf_len(reply));
2315
2316 wpabuf_free(reply);
2317 os_free(data);
2318
2319 return ret;
2320}
2321
2322
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002323static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2324 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002326 u8 addr[ETH_ALEN];
2327 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002329 if (hwaddr_aton(cmd, addr))
2330 return -1;
2331
2332 sta = ap_get_sta(hapd, addr);
2333 if (!sta || !sta->eapol_sm)
2334 return -1;
2335
2336 eapol_auth_reauthenticate(sta->eapol_sm);
2337 return 0;
2338}
2339
2340
2341static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2342{
2343 u8 addr[ETH_ALEN];
2344 struct sta_info *sta;
2345 char *pos = cmd, *param;
2346
2347 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2348 return -1;
2349 pos += 18;
2350 param = pos;
2351 pos = os_strchr(pos, ' ');
2352 if (!pos)
2353 return -1;
2354 *pos++ = '\0';
2355
2356 sta = ap_get_sta(hapd, addr);
2357 if (!sta || !sta->eapol_sm)
2358 return -1;
2359
2360 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2361}
2362
2363
2364static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2365 char *buf, size_t buflen)
2366{
2367 char *pos, *end, *stamp;
2368 int ret;
2369
2370 /* cmd: "LOG_LEVEL [<level>]" */
2371 if (*cmd == '\0') {
2372 pos = buf;
2373 end = buf + buflen;
2374 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2375 "Timestamp: %d\n",
2376 debug_level_str(wpa_debug_level),
2377 wpa_debug_timestamp);
2378 if (os_snprintf_error(end - pos, ret))
2379 ret = 0;
2380
2381 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002384 while (*cmd == ' ')
2385 cmd++;
2386
2387 stamp = os_strchr(cmd, ' ');
2388 if (stamp) {
2389 *stamp++ = '\0';
2390 while (*stamp == ' ') {
2391 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 }
2394
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002395 if (os_strlen(cmd)) {
2396 int level = str_to_debug_level(cmd);
2397 if (level < 0)
2398 return -1;
2399 wpa_debug_level = level;
2400 }
2401
2402 if (stamp && os_strlen(stamp))
2403 wpa_debug_timestamp = atoi(stamp);
2404
2405 os_memcpy(buf, "OK\n", 3);
2406 return 3;
2407}
2408
2409
2410#ifdef NEED_AP_MLME
2411static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2412 char *buf, size_t buflen)
2413{
2414 struct hostapd_iface *iface = hapd->iface;
2415 char *pos, *end;
2416 struct hostapd_sta_info *info;
2417 struct os_reltime now;
2418
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002419 if (!iface->num_sta_seen)
2420 return 0;
2421
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002422 sta_track_expire(iface, 0);
2423
2424 pos = buf;
2425 end = buf + buflen;
2426
2427 os_get_reltime(&now);
2428 dl_list_for_each_reverse(info, &iface->sta_seen,
2429 struct hostapd_sta_info, list) {
2430 struct os_reltime age;
2431 int ret;
2432
2433 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002434 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2435 MAC2STR(info->addr), (unsigned int) age.sec,
2436 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002437 if (os_snprintf_error(end - pos, ret))
2438 break;
2439 pos += ret;
2440 }
2441
2442 return pos - buf;
2443}
2444#endif /* NEED_AP_MLME */
2445
2446
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002447static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2448 const char *cmd)
2449{
2450 u8 addr[ETH_ALEN];
2451
2452 if (hwaddr_aton(cmd, addr)) {
2453 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2454 return -1;
2455 }
2456
2457 return hostapd_send_lci_req(hapd, addr);
2458}
2459
2460
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002461static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002462{
2463 u8 addr[ETH_ALEN];
2464 char *token, *context = NULL;
2465 int random_interval, min_ap;
2466 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2467 unsigned int n_responders;
2468
2469 token = str_token(cmd, " ", &context);
2470 if (!token || hwaddr_aton(token, addr)) {
2471 wpa_printf(MSG_INFO,
2472 "CTRL: REQ_RANGE - Bad destination address");
2473 return -1;
2474 }
2475
2476 token = str_token(cmd, " ", &context);
2477 if (!token)
2478 return -1;
2479
2480 random_interval = atoi(token);
2481 if (random_interval < 0 || random_interval > 0xffff)
2482 return -1;
2483
2484 token = str_token(cmd, " ", &context);
2485 if (!token)
2486 return -1;
2487
2488 min_ap = atoi(token);
2489 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2490 return -1;
2491
2492 n_responders = 0;
2493 while ((token = str_token(cmd, " ", &context))) {
2494 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2495 wpa_printf(MSG_INFO,
2496 "CTRL: REQ_RANGE: Too many responders");
2497 return -1;
2498 }
2499
2500 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2501 wpa_printf(MSG_INFO,
2502 "CTRL: REQ_RANGE: Bad responder address");
2503 return -1;
2504 }
2505
2506 n_responders++;
2507 }
2508
2509 if (!n_responders) {
2510 wpa_printf(MSG_INFO,
2511 "CTRL: REQ_RANGE - No FTM responder address");
2512 return -1;
2513 }
2514
2515 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2516 responders, n_responders);
2517}
2518
2519
Dmitry Shmidt29333592017-01-09 12:27:11 -08002520static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2521 const char *cmd, char *reply,
2522 size_t reply_size)
2523{
2524 u8 addr[ETH_ALEN];
2525 const char *pos;
2526 struct wpabuf *req;
2527 int ret;
2528 u8 req_mode = 0;
2529
2530 if (hwaddr_aton(cmd, addr))
2531 return -1;
2532 pos = os_strchr(cmd, ' ');
2533 if (!pos)
2534 return -1;
2535 pos++;
2536 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2537 int val = hex2byte(pos + 9);
2538
2539 if (val < 0)
2540 return -1;
2541 req_mode = val;
2542 pos += 11;
2543 pos = os_strchr(pos, ' ');
2544 if (!pos)
2545 return -1;
2546 pos++;
2547 }
2548 req = wpabuf_parse_bin(pos);
2549 if (!req)
2550 return -1;
2551
2552 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2553 wpabuf_free(req);
2554 if (ret >= 0)
2555 ret = os_snprintf(reply, reply_size, "%d", ret);
2556 return ret;
2557}
2558
2559
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002560static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2561{
2562 struct wpa_ssid_value ssid;
2563 u8 bssid[ETH_ALEN];
2564 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002565 int stationary = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002566 char *tmp;
2567 int ret;
2568
2569 if (!(hapd->conf->radio_measurements[0] &
2570 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2571 wpa_printf(MSG_ERROR,
2572 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2573 return -1;
2574 }
2575
2576 if (hwaddr_aton(buf, bssid)) {
2577 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2578 return -1;
2579 }
2580
2581 tmp = os_strstr(buf, "ssid=");
2582 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2583 wpa_printf(MSG_ERROR,
2584 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2585 return -1;
2586 }
2587 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2588 if (!buf)
2589 return -1;
2590
2591 tmp = os_strstr(buf, "nr=");
2592 if (!tmp) {
2593 wpa_printf(MSG_ERROR,
2594 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2595 return -1;
2596 }
2597
2598 buf = os_strchr(tmp, ' ');
2599 if (buf)
2600 *buf++ = '\0';
2601
2602 nr = wpabuf_parse_bin(tmp + 3);
2603 if (!nr) {
2604 wpa_printf(MSG_ERROR,
2605 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2606 return -1;
2607 }
2608
2609 if (!buf)
2610 goto set;
2611
2612 tmp = os_strstr(buf, "lci=");
2613 if (tmp) {
2614 buf = os_strchr(tmp, ' ');
2615 if (buf)
2616 *buf++ = '\0';
2617 lci = wpabuf_parse_bin(tmp + 4);
2618 if (!lci) {
2619 wpa_printf(MSG_ERROR,
2620 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2621 wpabuf_free(nr);
2622 return -1;
2623 }
2624 }
2625
2626 if (!buf)
2627 goto set;
2628
2629 tmp = os_strstr(buf, "civic=");
2630 if (tmp) {
2631 buf = os_strchr(tmp, ' ');
2632 if (buf)
2633 *buf++ = '\0';
2634 civic = wpabuf_parse_bin(tmp + 6);
2635 if (!civic) {
2636 wpa_printf(MSG_ERROR,
2637 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2638 wpabuf_free(nr);
2639 wpabuf_free(lci);
2640 return -1;
2641 }
2642 }
2643
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002644 if (!buf)
2645 goto set;
2646
2647 if (os_strstr(buf, "stat"))
2648 stationary = 1;
2649
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002650set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002651 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2652 stationary);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002653
2654 wpabuf_free(nr);
2655 wpabuf_free(lci);
2656 wpabuf_free(civic);
2657
2658 return ret;
2659}
2660
2661
2662static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2663 char *buf)
2664{
2665 struct wpa_ssid_value ssid;
2666 u8 bssid[ETH_ALEN];
2667 char *tmp;
2668
2669 if (hwaddr_aton(buf, bssid)) {
2670 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2671 return -1;
2672 }
2673
2674 tmp = os_strstr(buf, "ssid=");
2675 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2676 wpa_printf(MSG_ERROR,
2677 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2678 return -1;
2679 }
2680
2681 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2682}
2683
2684
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002685static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2686 size_t buflen)
2687{
2688 int ret, i;
2689 char *pos, *end;
2690
2691 ret = os_snprintf(buf, buflen, "%016llX:\n",
2692 (long long unsigned) iface->drv_flags);
2693 if (os_snprintf_error(buflen, ret))
2694 return -1;
2695
2696 pos = buf + ret;
2697 end = buf + buflen;
2698
2699 for (i = 0; i < 64; i++) {
2700 if (iface->drv_flags & (1LLU << i)) {
2701 ret = os_snprintf(pos, end - pos, "%s\n",
2702 driver_flag_to_string(1LLU << i));
2703 if (os_snprintf_error(end - pos, ret))
2704 return -1;
2705 pos += ret;
2706 }
2707 }
2708
2709 return pos - buf;
2710}
2711
2712
Roshan Pius3a1667e2018-07-03 15:17:14 -07002713static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
2714 const char *txtaddr)
2715{
2716 u8 addr[ETH_ALEN];
2717 struct vlan_description vlan_id;
2718
2719 if (!(*num))
2720 return 0;
2721
2722 if (hwaddr_aton(txtaddr, addr))
2723 return -1;
2724
2725 if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
2726 hostapd_remove_acl_mac(acl, num, addr);
2727
2728 return 0;
2729}
2730
2731
2732static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
2733 int *num)
2734{
2735 while (*num)
2736 hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
2737}
2738
2739
2740static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
2741 char *buf, size_t buflen)
2742{
2743 int i = 0, len = 0, ret = 0;
2744
2745 if (!acl)
2746 return 0;
2747
2748 while (i < num) {
2749 ret = os_snprintf(buf + len, buflen - len,
2750 MACSTR " VLAN_ID=%d\n",
2751 MAC2STR(acl[i].addr),
2752 acl[i].vlan_id.untagged);
2753 if (ret < 0 || (size_t) ret >= buflen - len)
2754 return len;
2755 i++;
2756 len += ret;
2757 }
2758 return len;
2759}
2760
2761
2762static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
2763 const char *cmd)
2764{
2765 u8 addr[ETH_ALEN];
2766 struct vlan_description vlan_id;
2767 int ret = 0, vlanid = 0;
2768 const char *pos;
2769
2770 if (hwaddr_aton(cmd, addr))
2771 return -1;
2772
2773 pos = os_strstr(cmd, "VLAN_ID=");
2774 if (pos)
2775 vlanid = atoi(pos + 8);
2776
2777 if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
2778 ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
2779 if (ret != -1 && *acl)
2780 qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
2781 }
2782
2783 return ret < 0 ? -1 : 0;
2784}
2785
2786
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002787static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2788 char *buf, char *reply,
2789 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002790 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791 socklen_t fromlen)
2792{
2793 int reply_len, res;
2794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 os_memcpy(reply, "OK\n", 3);
2796 reply_len = 3;
2797
2798 if (os_strcmp(buf, "PING") == 0) {
2799 os_memcpy(reply, "PONG\n", 5);
2800 reply_len = 5;
2801 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2802 if (wpa_debug_reopen_file() < 0)
2803 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002804 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
2805 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002806 } else if (os_strcmp(buf, "STATUS") == 0) {
2807 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2808 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002809 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2810 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 } else if (os_strcmp(buf, "MIB") == 0) {
2812 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2813 if (reply_len >= 0) {
2814 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2815 reply_size - reply_len);
2816 if (res < 0)
2817 reply_len = -1;
2818 else
2819 reply_len += res;
2820 }
2821 if (reply_len >= 0) {
2822 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2823 reply_size - reply_len);
2824 if (res < 0)
2825 reply_len = -1;
2826 else
2827 reply_len += res;
2828 }
2829#ifndef CONFIG_NO_RADIUS
2830 if (reply_len >= 0) {
2831 res = radius_client_get_mib(hapd->radius,
2832 reply + reply_len,
2833 reply_size - reply_len);
2834 if (res < 0)
2835 reply_len = -1;
2836 else
2837 reply_len += res;
2838 }
2839#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002840 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2841 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2842 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2844 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2845 reply_size);
2846 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2847 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2848 reply_size);
2849 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2850 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2851 reply_size);
2852 } else if (os_strcmp(buf, "ATTACH") == 0) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002853 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
2854 reply_len = -1;
2855 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
2856 if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 reply_len = -1;
2858 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002859 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 reply_len = -1;
2861 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002862 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 buf + 6))
2864 reply_len = -1;
2865 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2866 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2867 reply_len = -1;
2868 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2869 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2870 reply_len = -1;
2871 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2872 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2873 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002874#ifdef CONFIG_TAXONOMY
2875 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
2876 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
2877 reply, reply_size);
2878#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002879 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2880 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2881 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002882 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2883 if (hostapd_ctrl_iface_stop_ap(hapd))
2884 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885#ifdef CONFIG_IEEE80211W
2886#ifdef NEED_AP_MLME
2887 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2888 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2889 reply_len = -1;
2890#endif /* NEED_AP_MLME */
2891#endif /* CONFIG_IEEE80211W */
2892#ifdef CONFIG_WPS
2893 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2894 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2895 reply_len = -1;
2896 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2897 reply_len = hostapd_ctrl_iface_wps_check_pin(
2898 hapd, buf + 14, reply, reply_size);
2899 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2900 if (hostapd_wps_button_pushed(hapd, NULL))
2901 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002902 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2903 if (hostapd_wps_cancel(hapd))
2904 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2906 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2907 reply, reply_size);
2908 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2909 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2910 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002911 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2912 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2913 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002914#ifdef CONFIG_WPS_NFC
2915 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2916 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2917 reply_len = -1;
2918 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2919 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2920 hapd, buf + 21, reply, reply_size);
2921 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2922 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2923 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002924 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2925 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2926 hapd, buf + 21, reply, reply_size);
2927 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2928 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2929 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002930#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002932#ifdef CONFIG_INTERWORKING
2933 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2934 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2935 reply_len = -1;
2936 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2937 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2938 reply_len = -1;
2939#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002940#ifdef CONFIG_HS20
2941 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2942 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2943 reply_len = -1;
2944 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2945 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2946 reply_len = -1;
2947#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002948#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002949 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2950 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2951 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2953 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2954 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002955 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2956 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2957 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002958#endif /* CONFIG_WNM_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2960 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2961 reply_size);
2962 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2963 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2964 reply_len = -1;
2965 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2966 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2967 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2969 if (hostapd_ctrl_iface_enable(hapd->iface))
2970 reply_len = -1;
2971 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2972 if (hostapd_ctrl_iface_reload(hapd->iface))
2973 reply_len = -1;
2974 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2975 if (hostapd_ctrl_iface_disable(hapd->iface))
2976 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002977 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2978 if (ieee802_11_set_beacon(hapd))
2979 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002980#ifdef CONFIG_TESTING_OPTIONS
2981 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2982 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2983 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002984 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2985 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2986 reply_len = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002987 } else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
2988 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
2989 buf + 23) < 0)
2990 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002991 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
2992 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
2993 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002994 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2995 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2996 reply_len = -1;
2997 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2998 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2999 reply_len = -1;
3000 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3001 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3002 reply_len = -1;
3003 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3004 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3005 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003006 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3007 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3008 reply_len = -1;
3009 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3010 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3011 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003012 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3013 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3014 reply_len = -1;
3015 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
3016 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003017 } else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3018 if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3019 reply_len = -1;
3020 } else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3021 if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3022 reply_len = -1;
3023 } else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3024 if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3025 reply_len = -1;
3026 } else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3027 if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3028 reply_len = -1;
3029 } else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3030 if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3031 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003032 } else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3033 if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3034 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003035#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003036 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003037 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003038 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003039 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3040 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3041 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3043 ieee802_1x_erp_flush(hapd);
3044#ifdef RADIUS_SERVER
3045 radius_server_erp_flush(hapd->radius_srv);
3046#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003047 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3048 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3049 reply_len = -1;
3050 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3051 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3052 reply_len = -1;
3053 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3054 reply_len = hostapd_ctrl_iface_log_level(
3055 hapd, buf + 9, reply, reply_size);
3056#ifdef NEED_AP_MLME
3057 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3058 reply_len = hostapd_ctrl_iface_track_sta_list(
3059 hapd, reply, reply_size);
3060#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07003061 } else if (os_strcmp(buf, "PMKSA") == 0) {
3062 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3063 reply_size);
3064 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3065 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003066 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3067 if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3068 reply_len = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003069 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3070 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3071 reply_len = -1;
3072 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3073 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3074 reply_len = -1;
3075 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3076 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3077 reply_len = -1;
3078 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3079 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3080 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003081 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3082 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3083 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003084 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3085 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3086 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08003087 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3088 eloop_terminate();
Roshan Pius3a1667e2018-07-03 15:17:14 -07003089 } else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3090 if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3091 if (!hostapd_ctrl_iface_acl_add_mac(
3092 &hapd->conf->accept_mac,
3093 &hapd->conf->num_accept_mac, buf + 19))
3094 hostapd_disassoc_accept_mac(hapd);
3095 else
3096 reply_len = -1;
3097 } else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3098 hostapd_ctrl_iface_acl_del_mac(
3099 &hapd->conf->accept_mac,
3100 &hapd->conf->num_accept_mac, buf + 19);
3101 } else if (os_strcmp(buf + 11, "SHOW") == 0) {
3102 reply_len = hostapd_ctrl_iface_acl_show_mac(
3103 hapd->conf->accept_mac,
3104 hapd->conf->num_accept_mac, reply, reply_size);
3105 } else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3106 hostapd_ctrl_iface_acl_clear_list(
3107 &hapd->conf->accept_mac,
3108 &hapd->conf->num_accept_mac);
3109 }
3110 } else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3111 if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3112 if (!hostapd_ctrl_iface_acl_add_mac(
3113 &hapd->conf->deny_mac,
3114 &hapd->conf->num_deny_mac, buf + 17))
3115 hostapd_disassoc_deny_mac(hapd);
3116 } else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3117 hostapd_ctrl_iface_acl_del_mac(
3118 &hapd->conf->deny_mac,
3119 &hapd->conf->num_deny_mac, buf + 17);
3120 } else if (os_strcmp(buf + 9, "SHOW") == 0) {
3121 reply_len = hostapd_ctrl_iface_acl_show_mac(
3122 hapd->conf->deny_mac,
3123 hapd->conf->num_deny_mac, reply, reply_size);
3124 } else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3125 hostapd_ctrl_iface_acl_clear_list(
3126 &hapd->conf->deny_mac,
3127 &hapd->conf->num_deny_mac);
3128 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003129#ifdef CONFIG_DPP
3130 } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3131 res = hostapd_dpp_qr_code(hapd, buf + 12);
3132 if (res < 0) {
3133 reply_len = -1;
3134 } else {
3135 reply_len = os_snprintf(reply, reply_size, "%d", res);
3136 if (os_snprintf_error(reply_size, reply_len))
3137 reply_len = -1;
3138 }
3139 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
3140 res = hostapd_dpp_bootstrap_gen(hapd, buf + 18);
3141 if (res < 0) {
3142 reply_len = -1;
3143 } else {
3144 reply_len = os_snprintf(reply, reply_size, "%d", res);
3145 if (os_snprintf_error(reply_size, reply_len))
3146 reply_len = -1;
3147 }
3148 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3149 if (hostapd_dpp_bootstrap_remove(hapd, buf + 21) < 0)
3150 reply_len = -1;
3151 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3152 const char *uri;
3153
3154 uri = hostapd_dpp_bootstrap_get_uri(hapd, atoi(buf + 22));
3155 if (!uri) {
3156 reply_len = -1;
3157 } else {
3158 reply_len = os_snprintf(reply, reply_size, "%s", uri);
3159 if (os_snprintf_error(reply_size, reply_len))
3160 reply_len = -1;
3161 }
3162 } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3163 reply_len = hostapd_dpp_bootstrap_info(hapd, atoi(buf + 19),
3164 reply, reply_size);
3165 } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3166 if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3167 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003168 } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3169 if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3170 reply_len = -1;
3171 } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3172 hostapd_dpp_stop(hapd);
3173 hostapd_dpp_listen_stop(hapd);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003174 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3175 res = hostapd_dpp_configurator_add(hapd, buf + 20);
3176 if (res < 0) {
3177 reply_len = -1;
3178 } else {
3179 reply_len = os_snprintf(reply, reply_size, "%d", res);
3180 if (os_snprintf_error(reply_size, reply_len))
3181 reply_len = -1;
3182 }
3183 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3184 if (hostapd_dpp_configurator_remove(hapd, buf + 24) < 0)
3185 reply_len = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003186 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
3187 if (hostapd_dpp_configurator_sign(hapd, buf + 22) < 0)
3188 reply_len = -1;
3189 } else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
3190 reply_len = hostapd_dpp_configurator_get_key(hapd,
3191 atoi(buf + 25),
3192 reply, reply_size);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003193 } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3194 res = hostapd_dpp_pkex_add(hapd, buf + 12);
3195 if (res < 0) {
3196 reply_len = -1;
3197 } else {
3198 reply_len = os_snprintf(reply, reply_size, "%d", res);
3199 if (os_snprintf_error(reply_size, reply_len))
3200 reply_len = -1;
3201 }
3202 } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3203 if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3204 reply_len = -1;
3205#endif /* CONFIG_DPP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003206#ifdef RADIUS_SERVER
3207 } else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3208 if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3209 reply_len = -1;
3210#endif /* RADIUS_SERVER */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 } else {
3212 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
3213 reply_len = 16;
3214 }
3215
3216 if (reply_len < 0) {
3217 os_memcpy(reply, "FAIL\n", 5);
3218 reply_len = 5;
3219 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003220
3221 return reply_len;
3222}
3223
3224
3225static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
3226 void *sock_ctx)
3227{
3228 struct hostapd_data *hapd = eloop_ctx;
3229 char buf[4096];
3230 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003231 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003232 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003233 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003234 const int reply_size = 4096;
3235 int reply_len;
3236 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003237#ifdef CONFIG_CTRL_IFACE_UDP
3238 unsigned char lcookie[COOKIE_LEN];
3239#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003240
3241 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
3242 (struct sockaddr *) &from, &fromlen);
3243 if (res < 0) {
3244 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3245 strerror(errno));
3246 return;
3247 }
3248 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003249
3250 reply = os_malloc(reply_size);
3251 if (reply == NULL) {
3252 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3253 fromlen) < 0) {
3254 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3255 strerror(errno));
3256 }
3257 return;
3258 }
3259
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003260#ifdef CONFIG_CTRL_IFACE_UDP
3261 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3262 os_memcpy(reply, "COOKIE=", 7);
3263 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3264 cookie, COOKIE_LEN);
3265 reply_len = 7 + 2 * COOKIE_LEN;
3266 goto done;
3267 }
3268
3269 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3270 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3271 wpa_printf(MSG_DEBUG,
3272 "CTRL: No cookie in the request - drop request");
3273 os_free(reply);
3274 return;
3275 }
3276
3277 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
3278 wpa_printf(MSG_DEBUG,
3279 "CTRL: Invalid cookie in the request - drop request");
3280 os_free(reply);
3281 return;
3282 }
3283
3284 pos = buf + 7 + 2 * COOKIE_LEN;
3285 while (*pos == ' ')
3286 pos++;
3287#endif /* CONFIG_CTRL_IFACE_UDP */
3288
3289 if (os_strcmp(pos, "PING") == 0)
3290 level = MSG_EXCESSIVE;
3291 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
3292
3293 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003294 reply, reply_size,
3295 &from, fromlen);
3296
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003297#ifdef CONFIG_CTRL_IFACE_UDP
3298done:
3299#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003300 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3301 fromlen) < 0) {
3302 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3303 strerror(errno));
3304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 os_free(reply);
3306}
3307
3308
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003309#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
3311{
3312 char *buf;
3313 size_t len;
3314
3315 if (hapd->conf->ctrl_interface == NULL)
3316 return NULL;
3317
3318 len = os_strlen(hapd->conf->ctrl_interface) +
3319 os_strlen(hapd->conf->iface) + 2;
3320 buf = os_malloc(len);
3321 if (buf == NULL)
3322 return NULL;
3323
3324 os_snprintf(buf, len, "%s/%s",
3325 hapd->conf->ctrl_interface, hapd->conf->iface);
3326 buf[len - 1] = '\0';
3327 return buf;
3328}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003329#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330
3331
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07003332static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
3333 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 const char *txt, size_t len)
3335{
3336 struct hostapd_data *hapd = ctx;
3337 if (hapd == NULL)
3338 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003339 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340}
3341
3342
3343int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
3344{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003345#ifdef CONFIG_CTRL_IFACE_UDP
3346 int port = HOSTAPD_CTRL_IFACE_PORT;
3347 char p[32] = { 0 };
3348 char port_str[40], *tmp;
3349 char *pos;
3350 struct addrinfo hints = { 0 }, *res, *saveres;
3351 int n;
3352
3353 if (hapd->ctrl_sock > -1) {
3354 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3355 return 0;
3356 }
3357
3358 if (hapd->conf->ctrl_interface == NULL)
3359 return 0;
3360
3361 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
3362 if (pos) {
3363 pos += 4;
3364 port = atoi(pos);
3365 if (port <= 0) {
3366 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
3367 goto fail;
3368 }
3369 }
3370
3371 dl_list_init(&hapd->ctrl_dst);
3372 hapd->ctrl_sock = -1;
3373 os_get_random(cookie, COOKIE_LEN);
3374
3375#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3376 hints.ai_flags = AI_PASSIVE;
3377#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3378
3379#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3380 hints.ai_family = AF_INET6;
3381#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3382 hints.ai_family = AF_INET;
3383#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3384 hints.ai_socktype = SOCK_DGRAM;
3385
3386try_again:
3387 os_snprintf(p, sizeof(p), "%d", port);
3388 n = getaddrinfo(NULL, p, &hints, &res);
3389 if (n) {
3390 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3391 goto fail;
3392 }
3393
3394 saveres = res;
3395 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
3396 res->ai_protocol);
3397 if (hapd->ctrl_sock < 0) {
3398 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3399 goto fail;
3400 }
3401
3402 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
3403 port--;
3404 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
3405 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
3406 goto try_again;
3407 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3408 goto fail;
3409 }
3410
3411 freeaddrinfo(saveres);
3412
3413 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
3414 tmp = os_strdup(port_str);
3415 if (tmp) {
3416 os_free(hapd->conf->ctrl_interface);
3417 hapd->conf->ctrl_interface = tmp;
3418 }
3419 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
3420
3421 if (eloop_register_read_sock(hapd->ctrl_sock,
3422 hostapd_ctrl_iface_receive, hapd, NULL) <
3423 0) {
3424 hostapd_ctrl_iface_deinit(hapd);
3425 return -1;
3426 }
3427
3428 hapd->msg_ctx = hapd;
3429 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3430
3431 return 0;
3432
3433fail:
3434 if (hapd->ctrl_sock >= 0)
3435 close(hapd->ctrl_sock);
3436 return -1;
3437#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438 struct sockaddr_un addr;
3439 int s = -1;
3440 char *fname = NULL;
3441
Dmitry Shmidt04949592012-07-19 12:16:46 -07003442 if (hapd->ctrl_sock > -1) {
3443 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3444 return 0;
3445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003447 dl_list_init(&hapd->ctrl_dst);
3448
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 if (hapd->conf->ctrl_interface == NULL)
3450 return 0;
3451
3452 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3453 if (errno == EEXIST) {
3454 wpa_printf(MSG_DEBUG, "Using existing control "
3455 "interface directory.");
3456 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003457 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3458 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459 goto fail;
3460 }
3461 }
3462
3463 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003464 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003466 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3467 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 return -1;
3469 }
3470
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003471 if (!hapd->conf->ctrl_interface_gid_set &&
3472 hapd->iface->interfaces->ctrl_iface_group &&
3473 chown(hapd->conf->ctrl_interface, -1,
3474 hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003475 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3476 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003477 return -1;
3478 }
3479
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003480#ifdef ANDROID
3481 /*
3482 * Android is using umask 0077 which would leave the control interface
3483 * directory without group access. This breaks things since Wi-Fi
3484 * framework assumes that this directory can be accessed by other
3485 * applications in the wifi group. Fix this by adding group access even
3486 * if umask value would prevent this.
3487 */
3488 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
3489 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
3490 strerror(errno));
3491 /* Try to continue anyway */
3492 }
3493#endif /* ANDROID */
3494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
3496 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
3497 goto fail;
3498
3499 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3500 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 goto fail;
3503 }
3504
3505 os_memset(&addr, 0, sizeof(addr));
3506#ifdef __FreeBSD__
3507 addr.sun_len = sizeof(addr);
3508#endif /* __FreeBSD__ */
3509 addr.sun_family = AF_UNIX;
3510 fname = hostapd_ctrl_iface_path(hapd);
3511 if (fname == NULL)
3512 goto fail;
3513 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3514 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3515 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3516 strerror(errno));
3517 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3518 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3519 " allow connections - assuming it was left"
3520 "over from forced program termination");
3521 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003522 wpa_printf(MSG_ERROR,
3523 "Could not unlink existing ctrl_iface socket '%s': %s",
3524 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 goto fail;
3526 }
3527 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3528 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003529 wpa_printf(MSG_ERROR,
3530 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3531 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532 goto fail;
3533 }
3534 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3535 "ctrl_iface socket '%s'", fname);
3536 } else {
3537 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3538 "be in use - cannot override it");
3539 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3540 "not used anymore", fname);
3541 os_free(fname);
3542 fname = NULL;
3543 goto fail;
3544 }
3545 }
3546
3547 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003548 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003549 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3550 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 goto fail;
3552 }
3553
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003554 if (!hapd->conf->ctrl_interface_gid_set &&
3555 hapd->iface->interfaces->ctrl_iface_group &&
3556 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003557 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3558 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003559 goto fail;
3560 }
3561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003563 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3564 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565 goto fail;
3566 }
3567 os_free(fname);
3568
3569 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003570 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3571 NULL) < 0) {
3572 hostapd_ctrl_iface_deinit(hapd);
3573 return -1;
3574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 hapd->msg_ctx = hapd;
3576 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3577
3578 return 0;
3579
3580fail:
3581 if (s >= 0)
3582 close(s);
3583 if (fname) {
3584 unlink(fname);
3585 os_free(fname);
3586 }
3587 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003588#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003589}
3590
3591
3592void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3593{
3594 struct wpa_ctrl_dst *dst, *prev;
3595
3596 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003597#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003599#endif /* !CONFIG_CTRL_IFACE_UDP */
3600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 eloop_unregister_read_sock(hapd->ctrl_sock);
3602 close(hapd->ctrl_sock);
3603 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003604#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 fname = hostapd_ctrl_iface_path(hapd);
3606 if (fname)
3607 unlink(fname);
3608 os_free(fname);
3609
3610 if (hapd->conf->ctrl_interface &&
3611 rmdir(hapd->conf->ctrl_interface) < 0) {
3612 if (errno == ENOTEMPTY) {
3613 wpa_printf(MSG_DEBUG, "Control interface "
3614 "directory not empty - leaving it "
3615 "behind");
3616 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003617 wpa_printf(MSG_ERROR,
3618 "rmdir[ctrl_interface=%s]: %s",
3619 hapd->conf->ctrl_interface,
3620 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 }
3622 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003623#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 }
3625
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003626 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3627 list)
3628 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003629
3630#ifdef CONFIG_TESTING_OPTIONS
3631 l2_packet_deinit(hapd->l2_test);
3632 hapd->l2_test = NULL;
3633#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634}
3635
3636
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003637static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3638 char *buf)
3639{
3640 if (hostapd_add_iface(interfaces, buf) < 0) {
3641 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3642 return -1;
3643 }
3644 return 0;
3645}
3646
3647
3648static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3649 char *buf)
3650{
3651 if (hostapd_remove_iface(interfaces, buf) < 0) {
3652 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3653 return -1;
3654 }
3655 return 0;
3656}
3657
3658
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003659static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003660 struct sockaddr_storage *from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003661 socklen_t fromlen, char *input)
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003662{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003663 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
3664 input);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003665}
3666
3667
3668static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003669 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003670 socklen_t fromlen)
3671{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003672 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003673}
3674
3675
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003676static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3677{
3678#ifdef CONFIG_WPS_TESTING
3679 wps_version_number = 0x20;
3680 wps_testing_dummy_cred = 0;
3681 wps_corrupt_pkhash = 0;
3682#endif /* CONFIG_WPS_TESTING */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003683
3684#ifdef CONFIG_TESTING_OPTIONS
3685#ifdef CONFIG_DPP
3686 dpp_test = DPP_TEST_DISABLED;
3687#endif /* CONFIG_DPP */
3688#endif /* CONFIG_TESTING_OPTIONS */
3689
3690#ifdef CONFIG_DPP
3691 hostapd_dpp_deinit_global(interfaces);
3692#endif /* CONFIG_DPP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003693}
3694
3695
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003696#ifdef CONFIG_FST
3697
3698static int
3699hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3700 const char *cmd)
3701{
3702 char ifname[IFNAMSIZ + 1];
3703 struct fst_iface_cfg cfg;
3704 struct hostapd_data *hapd;
3705 struct fst_wpa_obj iface_obj;
3706
3707 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3708 hapd = hostapd_get_iface(interfaces, ifname);
3709 if (hapd) {
3710 if (hapd->iface->fst) {
3711 wpa_printf(MSG_INFO, "FST: Already attached");
3712 return -1;
3713 }
3714 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3715 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3716 &iface_obj, &cfg);
3717 if (hapd->iface->fst)
3718 return 0;
3719 }
3720 }
3721
3722 return -EINVAL;
3723}
3724
3725
3726static int
3727hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3728 const char *cmd)
3729{
3730 char ifname[IFNAMSIZ + 1];
3731 struct hostapd_data * hapd;
3732
3733 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3734 hapd = hostapd_get_iface(interfaces, ifname);
3735 if (hapd) {
3736 if (!fst_iface_detach(ifname)) {
3737 hapd->iface->fst = NULL;
3738 hapd->iface->fst_ies = NULL;
3739 return 0;
3740 }
3741 }
3742 }
3743
3744 return -EINVAL;
3745}
3746
3747#endif /* CONFIG_FST */
3748
3749
3750static struct hostapd_data *
3751hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3752 const char *ifname)
3753{
3754 size_t i, j;
3755
3756 for (i = 0; i < interfaces->count; i++) {
3757 struct hostapd_iface *iface = interfaces->iface[i];
3758
3759 for (j = 0; j < iface->num_bss; j++) {
3760 struct hostapd_data *hapd;
3761
3762 hapd = iface->bss[j];
3763 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3764 return hapd;
3765 }
3766 }
3767
3768 return NULL;
3769}
3770
3771
3772static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3773 struct hostapd_data *dst_hapd,
3774 const char *param)
3775{
3776 int res;
3777 char *value;
3778
3779 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3780 if (!value) {
3781 wpa_printf(MSG_ERROR,
3782 "DUP: cannot allocate buffer to stringify %s",
3783 param);
3784 goto error_return;
3785 }
3786
3787 if (os_strcmp(param, "wpa") == 0) {
3788 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3789 src_hapd->conf->wpa);
3790 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3791 src_hapd->conf->wpa_key_mgmt) {
3792 res = hostapd_ctrl_iface_get_key_mgmt(
3793 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3794 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3795 goto error_stringify;
3796 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3797 src_hapd->conf->wpa_pairwise) {
3798 res = wpa_write_ciphers(value,
3799 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3800 src_hapd->conf->wpa_pairwise, " ");
3801 if (res < 0)
3802 goto error_stringify;
3803 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3804 src_hapd->conf->rsn_pairwise) {
3805 res = wpa_write_ciphers(value,
3806 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3807 src_hapd->conf->rsn_pairwise, " ");
3808 if (res < 0)
3809 goto error_stringify;
3810 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3811 src_hapd->conf->ssid.wpa_passphrase) {
3812 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3813 src_hapd->conf->ssid.wpa_passphrase);
3814 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3815 src_hapd->conf->ssid.wpa_psk_set) {
3816 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3817 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3818 } else {
3819 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3820 goto error_return;
3821 }
3822
3823 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3824 os_free(value);
3825 return res;
3826
3827error_stringify:
3828 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3829error_return:
3830 os_free(value);
3831 return -1;
3832}
3833
3834
3835static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003836hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3837 const char *input,
3838 char *reply, int reply_size)
3839{
3840 size_t i, j;
3841 int res;
3842 char *pos, *end;
3843 struct hostapd_iface *iface;
3844 int show_ctrl = 0;
3845
3846 if (input)
3847 show_ctrl = !!os_strstr(input, "ctrl");
3848
3849 pos = reply;
3850 end = reply + reply_size;
3851
3852 for (i = 0; i < interfaces->count; i++) {
3853 iface = interfaces->iface[i];
3854
3855 for (j = 0; j < iface->num_bss; j++) {
3856 struct hostapd_bss_config *conf;
3857
3858 conf = iface->conf->bss[j];
3859 if (show_ctrl)
3860 res = os_snprintf(pos, end - pos,
3861 "%s ctrl_iface=%s\n",
3862 conf->iface,
3863 conf->ctrl_interface ?
3864 conf->ctrl_interface : "N/A");
3865 else
3866 res = os_snprintf(pos, end - pos, "%s\n",
3867 conf->iface);
3868 if (os_snprintf_error(end - pos, res)) {
3869 *pos = '\0';
3870 return pos - reply;
3871 }
3872 pos += res;
3873 }
3874 }
3875
3876 return pos - reply;
3877}
3878
3879
3880static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003881hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3882 char *cmd)
3883{
3884 char *p_start = cmd, *p_end;
3885 struct hostapd_data *src_hapd, *dst_hapd;
3886
3887 /* cmd: "<src ifname> <dst ifname> <variable name> */
3888
3889 p_end = os_strchr(p_start, ' ');
3890 if (!p_end) {
3891 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3892 cmd);
3893 return -1;
3894 }
3895
3896 *p_end = '\0';
3897 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3898 if (!src_hapd) {
3899 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3900 p_start);
3901 return -1;
3902 }
3903
3904 p_start = p_end + 1;
3905 p_end = os_strchr(p_start, ' ');
3906 if (!p_end) {
3907 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3908 cmd);
3909 return -1;
3910 }
3911
3912 *p_end = '\0';
3913 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3914 if (!dst_hapd) {
3915 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3916 p_start);
3917 return -1;
3918 }
3919
3920 p_start = p_end + 1;
3921 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3922}
3923
3924
3925static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3926 const char *ifname,
3927 char *buf, char *reply,
3928 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003929 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003930 socklen_t fromlen)
3931{
3932 struct hostapd_data *hapd;
3933
3934 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3935 if (hapd == NULL) {
3936 int res;
3937
3938 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3939 if (os_snprintf_error(reply_size, res))
3940 return -1;
3941 return res;
3942 }
3943
3944 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3945 from, fromlen);
3946}
3947
3948
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003949static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3950 void *sock_ctx)
3951{
3952 void *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003953 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003954 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003955 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003956 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003957 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003958 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003959 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003960#ifdef CONFIG_CTRL_IFACE_UDP
3961 unsigned char lcookie[COOKIE_LEN];
3962#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003963
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003964 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003965 (struct sockaddr *) &from, &fromlen);
3966 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003967 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3968 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003969 return;
3970 }
3971 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003972 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003973
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003974 reply = os_malloc(reply_size);
3975 if (reply == NULL) {
3976 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3977 fromlen) < 0) {
3978 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3979 strerror(errno));
3980 }
3981 return;
3982 }
3983
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003984 os_memcpy(reply, "OK\n", 3);
3985 reply_len = 3;
3986
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003987#ifdef CONFIG_CTRL_IFACE_UDP
3988 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3989 os_memcpy(reply, "COOKIE=", 7);
3990 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3991 gcookie, COOKIE_LEN);
3992 reply_len = 7 + 2 * COOKIE_LEN;
3993 goto send_reply;
3994 }
3995
3996 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3997 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3998 wpa_printf(MSG_DEBUG,
3999 "CTRL: No cookie in the request - drop request");
4000 os_free(reply);
4001 return;
4002 }
4003
4004 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
4005 wpa_printf(MSG_DEBUG,
4006 "CTRL: Invalid cookie in the request - drop request");
4007 os_free(reply);
4008 return;
4009 }
4010
4011 buf += 7 + 2 * COOKIE_LEN;
4012 while (*buf == ' ')
4013 buf++;
4014#endif /* CONFIG_CTRL_IFACE_UDP */
4015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004016 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4017 char *pos = os_strchr(buf + 7, ' ');
4018
4019 if (pos) {
4020 *pos++ = '\0';
4021 reply_len = hostapd_global_ctrl_iface_ifname(
4022 interfaces, buf + 7, pos, reply, reply_size,
4023 &from, fromlen);
4024 goto send_reply;
4025 }
4026 }
4027
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 if (os_strcmp(buf, "PING") == 0) {
4029 os_memcpy(reply, "PONG\n", 5);
4030 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004031 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4032 if (wpa_debug_reopen_file() < 0)
4033 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004034 } else if (os_strcmp(buf, "FLUSH") == 0) {
4035 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004036 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
4037 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4038 reply_len = -1;
4039 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4040 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4041 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004042 } else if (os_strcmp(buf, "ATTACH") == 0) {
4043 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004044 fromlen, NULL))
4045 reply_len = -1;
4046 } else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4047 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4048 fromlen, buf + 7))
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004049 reply_len = -1;
4050 } else if (os_strcmp(buf, "DETACH") == 0) {
4051 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4052 fromlen))
4053 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004054#ifdef CONFIG_MODULE_TESTS
4055 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004056 if (hapd_module_tests() < 0)
4057 reply_len = -1;
4058#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004059#ifdef CONFIG_FST
4060 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4061 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4062 reply_len = os_snprintf(reply, reply_size, "OK\n");
4063 else
4064 reply_len = -1;
4065 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4066 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4067 reply_len = os_snprintf(reply, reply_size, "OK\n");
4068 else
4069 reply_len = -1;
4070 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4071 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4072#endif /* CONFIG_FST */
4073 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4074 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4075 buf + 12))
4076 reply_len = os_snprintf(reply, reply_size, "OK\n");
4077 else
4078 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004079 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4080 reply_len = hostapd_global_ctrl_iface_interfaces(
4081 interfaces, buf + 10, reply, sizeof(buffer));
4082 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4083 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004084 } else {
4085 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4086 "ignored");
4087 reply_len = -1;
4088 }
4089
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004090send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004091 if (reply_len < 0) {
4092 os_memcpy(reply, "FAIL\n", 5);
4093 reply_len = 5;
4094 }
4095
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4097 fromlen) < 0) {
4098 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4099 strerror(errno));
4100 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004101 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004102}
4103
4104
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004105#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004106static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4107{
4108 char *buf;
4109 size_t len;
4110
4111 if (interface->global_iface_path == NULL)
4112 return NULL;
4113
4114 len = os_strlen(interface->global_iface_path) +
4115 os_strlen(interface->global_iface_name) + 2;
4116 buf = os_malloc(len);
4117 if (buf == NULL)
4118 return NULL;
4119
4120 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4121 interface->global_iface_name);
4122 buf[len - 1] = '\0';
4123 return buf;
4124}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004125#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004126
4127
4128int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4129{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004130#ifdef CONFIG_CTRL_IFACE_UDP
4131 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4132 char p[32] = { 0 };
4133 char *pos;
4134 struct addrinfo hints = { 0 }, *res, *saveres;
4135 int n;
4136
4137 if (interface->global_ctrl_sock > -1) {
4138 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4139 return 0;
4140 }
4141
4142 if (interface->global_iface_path == NULL)
4143 return 0;
4144
4145 pos = os_strstr(interface->global_iface_path, "udp:");
4146 if (pos) {
4147 pos += 4;
4148 port = atoi(pos);
4149 if (port <= 0) {
4150 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4151 goto fail;
4152 }
4153 }
4154
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004155 os_get_random(gcookie, COOKIE_LEN);
4156
4157#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4158 hints.ai_flags = AI_PASSIVE;
4159#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4160
4161#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4162 hints.ai_family = AF_INET6;
4163#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4164 hints.ai_family = AF_INET;
4165#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4166 hints.ai_socktype = SOCK_DGRAM;
4167
4168try_again:
4169 os_snprintf(p, sizeof(p), "%d", port);
4170 n = getaddrinfo(NULL, p, &hints, &res);
4171 if (n) {
4172 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4173 goto fail;
4174 }
4175
4176 saveres = res;
4177 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4178 res->ai_protocol);
4179 if (interface->global_ctrl_sock < 0) {
4180 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4181 goto fail;
4182 }
4183
4184 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4185 0) {
4186 port++;
4187 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4188 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4189 goto try_again;
4190 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4191 goto fail;
4192 }
4193
4194 freeaddrinfo(saveres);
4195
4196 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4197
4198 if (eloop_register_read_sock(interface->global_ctrl_sock,
4199 hostapd_global_ctrl_iface_receive,
4200 interface, NULL) < 0) {
4201 hostapd_global_ctrl_iface_deinit(interface);
4202 return -1;
4203 }
4204
4205 return 0;
4206
4207fail:
4208 if (interface->global_ctrl_sock >= 0)
4209 close(interface->global_ctrl_sock);
4210 return -1;
4211#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004212 struct sockaddr_un addr;
4213 int s = -1;
4214 char *fname = NULL;
4215
4216 if (interface->global_iface_path == NULL) {
4217 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
4218 return 0;
4219 }
4220
4221 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
4222 if (errno == EEXIST) {
4223 wpa_printf(MSG_DEBUG, "Using existing control "
4224 "interface directory.");
4225 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004226 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4227 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004228 goto fail;
4229 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004230 } else if (interface->ctrl_iface_group &&
4231 chown(interface->global_iface_path, -1,
4232 interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004233 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
4234 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004235 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004236 }
4237
4238 if (os_strlen(interface->global_iface_path) + 1 +
4239 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
4240 goto fail;
4241
4242 s = socket(PF_UNIX, SOCK_DGRAM, 0);
4243 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004244 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004245 goto fail;
4246 }
4247
4248 os_memset(&addr, 0, sizeof(addr));
4249#ifdef __FreeBSD__
4250 addr.sun_len = sizeof(addr);
4251#endif /* __FreeBSD__ */
4252 addr.sun_family = AF_UNIX;
4253 fname = hostapd_global_ctrl_iface_path(interface);
4254 if (fname == NULL)
4255 goto fail;
4256 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4257 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4258 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4259 strerror(errno));
4260 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4261 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4262 " allow connections - assuming it was left"
4263 "over from forced program termination");
4264 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004265 wpa_printf(MSG_ERROR,
4266 "Could not unlink existing ctrl_iface socket '%s': %s",
4267 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004268 goto fail;
4269 }
4270 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4271 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004272 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
4273 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004274 goto fail;
4275 }
4276 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4277 "ctrl_iface socket '%s'", fname);
4278 } else {
4279 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4280 "be in use - cannot override it");
4281 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4282 "not used anymore", fname);
4283 os_free(fname);
4284 fname = NULL;
4285 goto fail;
4286 }
4287 }
4288
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004289 if (interface->ctrl_iface_group &&
4290 chown(fname, -1, interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004291 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
4292 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07004293 goto fail;
4294 }
4295
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004296 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004297 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4298 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004299 goto fail;
4300 }
4301 os_free(fname);
4302
4303 interface->global_ctrl_sock = s;
4304 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
4305 interface, NULL);
4306
4307 return 0;
4308
4309fail:
4310 if (s >= 0)
4311 close(s);
4312 if (fname) {
4313 unlink(fname);
4314 os_free(fname);
4315 }
4316 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004317#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004318}
4319
4320
4321void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
4322{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004323#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004324 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004325#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004326 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004327
4328 if (interfaces->global_ctrl_sock > -1) {
4329 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
4330 close(interfaces->global_ctrl_sock);
4331 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004332#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004333 fname = hostapd_global_ctrl_iface_path(interfaces);
4334 if (fname) {
4335 unlink(fname);
4336 os_free(fname);
4337 }
4338
4339 if (interfaces->global_iface_path &&
4340 rmdir(interfaces->global_iface_path) < 0) {
4341 if (errno == ENOTEMPTY) {
4342 wpa_printf(MSG_DEBUG, "Control interface "
4343 "directory not empty - leaving it "
4344 "behind");
4345 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004346 wpa_printf(MSG_ERROR,
4347 "rmdir[ctrl_interface=%s]: %s",
4348 interfaces->global_iface_path,
4349 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004350 }
4351 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004352#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004353 }
4354
4355 os_free(interfaces->global_iface_path);
4356 interfaces->global_iface_path = NULL;
4357
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004358 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
4359 struct wpa_ctrl_dst, list)
4360 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004361}
4362
4363
Roshan Pius3a1667e2018-07-03 15:17:14 -07004364static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
4365 const char *buf)
4366{
4367 /* Enable Probe Request events based on explicit request.
4368 * Other events are enabled by default.
4369 */
4370 if (str_starts(buf, RX_PROBE_REQUEST))
4371 return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
4372 return 1;
4373}
4374
4375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004377 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 const char *buf, size_t len)
4379{
4380 struct wpa_ctrl_dst *dst, *next;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004381 struct dl_list *ctrl_dst;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 struct msghdr msg;
4383 int idx;
4384 struct iovec io[2];
4385 char levelstr[10];
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004386 int s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004388 if (type != WPA_MSG_ONLY_GLOBAL) {
4389 s = hapd->ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004390 ctrl_dst = &hapd->ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004391 } else {
4392 s = hapd->iface->interfaces->global_ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004393 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004394 }
4395
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004396 if (s < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 return;
4398
4399 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
4400 io[0].iov_base = levelstr;
4401 io[0].iov_len = os_strlen(levelstr);
4402 io[1].iov_base = (char *) buf;
4403 io[1].iov_len = len;
4404 os_memset(&msg, 0, sizeof(msg));
4405 msg.msg_iov = io;
4406 msg.msg_iovlen = 2;
4407
4408 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004409 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004410 if ((level >= dst->debug_level) &&
4411 hostapd_ctrl_check_event_enabled(dst, buf)) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08004412 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
4413 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 msg.msg_name = &dst->addr;
4415 msg.msg_namelen = dst->addrlen;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004416 if (sendmsg(s, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 int _errno = errno;
4418 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
4419 "%d - %s",
4420 idx, errno, strerror(errno));
4421 dst->errors++;
4422 if (dst->errors > 10 || _errno == ENOENT) {
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02004423 if (type != WPA_MSG_ONLY_GLOBAL)
4424 hostapd_ctrl_iface_detach(
4425 hapd, &dst->addr,
4426 dst->addrlen);
4427 else
4428 hostapd_global_ctrl_iface_detach(
4429 hapd->iface->interfaces,
4430 &dst->addr,
4431 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 }
4433 } else
4434 dst->errors = 0;
4435 }
4436 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004437 }
4438}
4439
4440#endif /* CONFIG_NATIVE_WINDOWS */