blob: b9d9411e96e33e3032875812dc1f756ec2373158 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / UNIX domain socket -based control interface
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, 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"
Dmitry Shmidtff787d52015-01-12 13:01:47 -080032#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "drivers/driver.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080034#include "eapol_auth/eapol_auth_sm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "radius/radius_client.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080036#include "radius/radius_server.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080037#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "ap/hostapd.h"
39#include "ap/ap_config.h"
40#include "ap/ieee802_1x.h"
41#include "ap/wpa_auth.h"
42#include "ap/ieee802_11.h"
43#include "ap/sta_info.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "ap/wps_hostapd.h"
45#include "ap/ctrl_iface_ap.h"
46#include "ap/ap_drv_ops.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080047#include "ap/hs20.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080048#include "ap/wnm_ap.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070049#include "ap/wpa_auth.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080050#include "ap/beacon.h"
Dmitry Shmidt849734c2016-05-27 09:59:01 -070051#include "ap/neighbor_db.h"
52#include "ap/rrm.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053#include "wps/wps_defs.h"
54#include "wps/wps.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080055#include "fst/fst_ctrl_iface.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#include "config_file.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057#include "ctrl_iface.h"
58
59
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080060#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
61
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080062#ifdef CONFIG_CTRL_IFACE_UDP
63#define COOKIE_LEN 8
64static unsigned char cookie[COOKIE_LEN];
65static unsigned char gcookie[COOKIE_LEN];
66#define HOSTAPD_CTRL_IFACE_PORT 8877
67#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
68#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
69#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
70#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071
72static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +020073 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 const char *buf, size_t len);
75
76
77static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080078 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 socklen_t fromlen)
80{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080081 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082}
83
84
85static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080086 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 socklen_t fromlen)
88{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080089 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090}
91
92
93static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080094 struct sockaddr_storage *from,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 socklen_t fromlen,
96 char *level)
97{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -080098 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099}
100
101
102static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
103 const char *txtaddr)
104{
105 u8 addr[ETH_ALEN];
106 struct sta_info *sta;
107
108 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
109
110 if (hwaddr_aton(txtaddr, addr))
111 return -1;
112
113 sta = ap_get_sta(hapd, addr);
114 if (sta)
115 return 0;
116
117 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
118 "notification", MAC2STR(addr));
119 sta = ap_sta_add(hapd, addr);
120 if (sta == NULL)
121 return -1;
122
123 hostapd_new_assoc_sta(hapd, sta, 0);
124 return 0;
125}
126
127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128#ifdef CONFIG_IEEE80211W
129#ifdef NEED_AP_MLME
130static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
131 const char *txtaddr)
132{
133 u8 addr[ETH_ALEN];
134 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
135
136 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
137
138 if (hwaddr_aton(txtaddr, addr) ||
139 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
140 return -1;
141
142 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
143
144 return 0;
145}
146#endif /* NEED_AP_MLME */
147#endif /* CONFIG_IEEE80211W */
148
149
150#ifdef CONFIG_WPS
151static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
152{
153 char *pin = os_strchr(txt, ' ');
154 char *timeout_txt;
155 int timeout;
156 u8 addr_buf[ETH_ALEN], *addr = NULL;
157 char *pos;
158
159 if (pin == NULL)
160 return -1;
161 *pin++ = '\0';
162
163 timeout_txt = os_strchr(pin, ' ');
164 if (timeout_txt) {
165 *timeout_txt++ = '\0';
166 timeout = atoi(timeout_txt);
167 pos = os_strchr(timeout_txt, ' ');
168 if (pos) {
169 *pos++ = '\0';
170 if (hwaddr_aton(pos, addr_buf) == 0)
171 addr = addr_buf;
172 }
173 } else
174 timeout = 0;
175
176 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
177}
178
179
180static int hostapd_ctrl_iface_wps_check_pin(
181 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
182{
183 char pin[9];
184 size_t len;
185 char *pos;
186 int ret;
187
188 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
189 (u8 *) cmd, os_strlen(cmd));
190 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
191 if (*pos < '0' || *pos > '9')
192 continue;
193 pin[len++] = *pos;
194 if (len == 9) {
195 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
196 return -1;
197 }
198 }
199 if (len != 4 && len != 8) {
200 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
201 return -1;
202 }
203 pin[len] = '\0';
204
205 if (len == 8) {
206 unsigned int pin_val;
207 pin_val = atoi(pin);
208 if (!wps_pin_valid(pin_val)) {
209 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
210 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800211 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 return -1;
213 return ret;
214 }
215 }
216
217 ret = os_snprintf(buf, buflen, "%s", pin);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800218 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 return -1;
220
221 return ret;
222}
223
224
Dmitry Shmidt04949592012-07-19 12:16:46 -0700225#ifdef CONFIG_WPS_NFC
226static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
227 char *pos)
228{
229 size_t len;
230 struct wpabuf *buf;
231 int ret;
232
233 len = os_strlen(pos);
234 if (len & 0x01)
235 return -1;
236 len /= 2;
237
238 buf = wpabuf_alloc(len);
239 if (buf == NULL)
240 return -1;
241 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
242 wpabuf_free(buf);
243 return -1;
244 }
245
246 ret = hostapd_wps_nfc_tag_read(hapd, buf);
247 wpabuf_free(buf);
248
249 return ret;
250}
251
252
253static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
254 char *cmd, char *reply,
255 size_t max_len)
256{
257 int ndef;
258 struct wpabuf *buf;
259 int res;
260
261 if (os_strcmp(cmd, "WPS") == 0)
262 ndef = 0;
263 else if (os_strcmp(cmd, "NDEF") == 0)
264 ndef = 1;
265 else
266 return -1;
267
268 buf = hostapd_wps_nfc_config_token(hapd, ndef);
269 if (buf == NULL)
270 return -1;
271
272 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
273 wpabuf_len(buf));
274 reply[res++] = '\n';
275 reply[res] = '\0';
276
277 wpabuf_free(buf);
278
279 return res;
280}
281
282
283static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
284 char *reply, size_t max_len,
285 int ndef)
286{
287 struct wpabuf *buf;
288 int res;
289
290 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
291 if (buf == NULL)
292 return -1;
293
294 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
295 wpabuf_len(buf));
296 reply[res++] = '\n';
297 reply[res] = '\0';
298
299 wpabuf_free(buf);
300
301 return res;
302}
303
304
305static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
306 char *cmd, char *reply,
307 size_t max_len)
308{
309 if (os_strcmp(cmd, "WPS") == 0)
310 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
311 max_len, 0);
312
313 if (os_strcmp(cmd, "NDEF") == 0)
314 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
315 max_len, 1);
316
317 if (os_strcmp(cmd, "enable") == 0)
318 return hostapd_wps_nfc_token_enable(hapd);
319
320 if (os_strcmp(cmd, "disable") == 0) {
321 hostapd_wps_nfc_token_disable(hapd);
322 return 0;
323 }
324
325 return -1;
326}
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800327
328
329static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
330 char *cmd, char *reply,
331 size_t max_len)
332{
333 struct wpabuf *buf;
334 int res;
335 char *pos;
336 int ndef;
337
338 pos = os_strchr(cmd, ' ');
339 if (pos == NULL)
340 return -1;
341 *pos++ = '\0';
342
343 if (os_strcmp(cmd, "WPS") == 0)
344 ndef = 0;
345 else if (os_strcmp(cmd, "NDEF") == 0)
346 ndef = 1;
347 else
348 return -1;
349
350 if (os_strcmp(pos, "WPS-CR") == 0)
351 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
352 else
353 buf = NULL;
354 if (buf == NULL)
355 return -1;
356
357 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
358 wpabuf_len(buf));
359 reply[res++] = '\n';
360 reply[res] = '\0';
361
362 wpabuf_free(buf);
363
364 return res;
365}
366
367
368static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
369 char *cmd)
370{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800371 size_t len;
372 struct wpabuf *req, *sel;
373 int ret;
374 char *pos, *role, *type, *pos2;
375
376 role = cmd;
377 pos = os_strchr(role, ' ');
378 if (pos == NULL)
379 return -1;
380 *pos++ = '\0';
381
382 type = pos;
383 pos = os_strchr(type, ' ');
384 if (pos == NULL)
385 return -1;
386 *pos++ = '\0';
387
388 pos2 = os_strchr(pos, ' ');
389 if (pos2 == NULL)
390 return -1;
391 *pos2++ = '\0';
392
393 len = os_strlen(pos);
394 if (len & 0x01)
395 return -1;
396 len /= 2;
397
398 req = wpabuf_alloc(len);
399 if (req == NULL)
400 return -1;
401 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
402 wpabuf_free(req);
403 return -1;
404 }
405
406 len = os_strlen(pos2);
407 if (len & 0x01) {
408 wpabuf_free(req);
409 return -1;
410 }
411 len /= 2;
412
413 sel = wpabuf_alloc(len);
414 if (sel == NULL) {
415 wpabuf_free(req);
416 return -1;
417 }
418 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
419 wpabuf_free(req);
420 wpabuf_free(sel);
421 return -1;
422 }
423
424 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
425 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
426 } else {
427 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
428 "reported: role=%s type=%s", role, type);
429 ret = -1;
430 }
431 wpabuf_free(req);
432 wpabuf_free(sel);
433
434 return ret;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800435}
436
Dmitry Shmidt04949592012-07-19 12:16:46 -0700437#endif /* CONFIG_WPS_NFC */
438
439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
441 char *buf, size_t buflen)
442{
443 int timeout = 300;
444 char *pos;
445 const char *pin_txt;
446
447 pos = os_strchr(txt, ' ');
448 if (pos)
449 *pos++ = '\0';
450
451 if (os_strcmp(txt, "disable") == 0) {
452 hostapd_wps_ap_pin_disable(hapd);
453 return os_snprintf(buf, buflen, "OK\n");
454 }
455
456 if (os_strcmp(txt, "random") == 0) {
457 if (pos)
458 timeout = atoi(pos);
459 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
460 if (pin_txt == NULL)
461 return -1;
462 return os_snprintf(buf, buflen, "%s", pin_txt);
463 }
464
465 if (os_strcmp(txt, "get") == 0) {
466 pin_txt = hostapd_wps_ap_pin_get(hapd);
467 if (pin_txt == NULL)
468 return -1;
469 return os_snprintf(buf, buflen, "%s", pin_txt);
470 }
471
472 if (os_strcmp(txt, "set") == 0) {
473 char *pin;
474 if (pos == NULL)
475 return -1;
476 pin = pos;
477 pos = os_strchr(pos, ' ');
478 if (pos) {
479 *pos++ = '\0';
480 timeout = atoi(pos);
481 }
482 if (os_strlen(pin) > buflen)
483 return -1;
484 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
485 return -1;
486 return os_snprintf(buf, buflen, "%s", pin);
487 }
488
489 return -1;
490}
491
492
493static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
494{
495 char *pos;
496 char *ssid, *auth, *encr = NULL, *key = NULL;
497
498 ssid = txt;
499 pos = os_strchr(txt, ' ');
500 if (!pos)
501 return -1;
502 *pos++ = '\0';
503
504 auth = pos;
505 pos = os_strchr(pos, ' ');
506 if (pos) {
507 *pos++ = '\0';
508 encr = pos;
509 pos = os_strchr(pos, ' ');
510 if (pos) {
511 *pos++ = '\0';
512 key = pos;
513 }
514 }
515
516 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
517}
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700518
519
520static const char * pbc_status_str(enum pbc_status status)
521{
522 switch (status) {
523 case WPS_PBC_STATUS_DISABLE:
524 return "Disabled";
525 case WPS_PBC_STATUS_ACTIVE:
526 return "Active";
527 case WPS_PBC_STATUS_TIMEOUT:
528 return "Timed-out";
529 case WPS_PBC_STATUS_OVERLAP:
530 return "Overlap";
531 default:
532 return "Unknown";
533 }
534}
535
536
537static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
538 char *buf, size_t buflen)
539{
540 int ret;
541 char *pos, *end;
542
543 pos = buf;
544 end = buf + buflen;
545
546 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
547 pbc_status_str(hapd->wps_stats.pbc_status));
548
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800549 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700550 return pos - buf;
551 pos += ret;
552
553 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
554 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
555 "Success":
556 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
557 "Failed" : "None")));
558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700560 return pos - buf;
561 pos += ret;
562
563 /* If status == Failure - Add possible Reasons */
564 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
565 hapd->wps_stats.failure_reason > 0) {
566 ret = os_snprintf(pos, end - pos,
567 "Failure Reason: %s\n",
568 wps_ei_str(hapd->wps_stats.failure_reason));
569
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700571 return pos - buf;
572 pos += ret;
573 }
574
575 if (hapd->wps_stats.status) {
576 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
577 MAC2STR(hapd->wps_stats.peer_addr));
578
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800579 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700580 return pos - buf;
581 pos += ret;
582 }
583
584 return pos - buf;
585}
586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587#endif /* CONFIG_WPS */
588
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800589#ifdef CONFIG_HS20
590
591static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
592 const char *cmd)
593{
594 u8 addr[ETH_ALEN];
595 const char *url;
596
597 if (hwaddr_aton(cmd, addr))
598 return -1;
599 url = cmd + 17;
600 if (*url == '\0') {
601 url = NULL;
602 } else {
603 if (*url != ' ')
604 return -1;
605 url++;
606 if (*url == '\0')
607 url = NULL;
608 }
609
610 return hs20_send_wnm_notification(hapd, addr, 1, url);
611}
612
613
614static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
615 const char *cmd)
616{
617 u8 addr[ETH_ALEN];
618 int code, reauth_delay, ret;
619 const char *pos;
620 size_t url_len;
621 struct wpabuf *req;
622
623 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
624 if (hwaddr_aton(cmd, addr))
625 return -1;
626
627 pos = os_strchr(cmd, ' ');
628 if (pos == NULL)
629 return -1;
630 pos++;
631 code = atoi(pos);
632
633 pos = os_strchr(pos, ' ');
634 if (pos == NULL)
635 return -1;
636 pos++;
637 reauth_delay = atoi(pos);
638
639 url_len = 0;
640 pos = os_strchr(pos, ' ');
641 if (pos) {
642 pos++;
643 url_len = os_strlen(pos);
644 }
645
646 req = wpabuf_alloc(4 + url_len);
647 if (req == NULL)
648 return -1;
649 wpabuf_put_u8(req, code);
650 wpabuf_put_le16(req, reauth_delay);
651 wpabuf_put_u8(req, url_len);
652 if (pos)
653 wpabuf_put_data(req, pos, url_len);
654
655 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
656 " to indicate imminent deauthentication (code=%d "
657 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
658 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
659 wpabuf_free(req);
660 return ret;
661}
662
663#endif /* CONFIG_HS20 */
664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665
Dmitry Shmidt051af732013-10-22 13:52:46 -0700666#ifdef CONFIG_INTERWORKING
667
668static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
669 const char *cmd)
670{
671 u8 qos_map_set[16 + 2 * 21], count = 0;
672 const char *pos = cmd;
673 int val, ret;
674
675 for (;;) {
676 if (count == sizeof(qos_map_set)) {
677 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
678 return -1;
679 }
680
681 val = atoi(pos);
682 if (val < 0 || val > 255) {
683 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
684 return -1;
685 }
686
687 qos_map_set[count++] = val;
688 pos = os_strchr(pos, ',');
689 if (!pos)
690 break;
691 pos++;
692 }
693
694 if (count < 16 || count & 1) {
695 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
696 return -1;
697 }
698
699 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
700 if (ret) {
701 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
702 return -1;
703 }
704
705 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
706 hapd->conf->qos_map_set_len = count;
707
708 return 0;
709}
710
711
712static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
713 const char *cmd)
714{
715 u8 addr[ETH_ALEN];
716 struct sta_info *sta;
717 struct wpabuf *buf;
718 u8 *qos_map_set = hapd->conf->qos_map_set;
719 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
720 int ret;
721
722 if (!qos_map_set_len) {
723 wpa_printf(MSG_INFO, "QoS Map Set is not set");
724 return -1;
725 }
726
727 if (hwaddr_aton(cmd, addr))
728 return -1;
729
730 sta = ap_get_sta(hapd, addr);
731 if (sta == NULL) {
732 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
733 "for QoS Map Configuration message",
734 MAC2STR(addr));
735 return -1;
736 }
737
738 if (!sta->qos_map_enabled) {
739 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
740 "support for QoS Map", MAC2STR(addr));
741 return -1;
742 }
743
744 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
745 if (buf == NULL)
746 return -1;
747
748 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
749 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
750
751 /* QoS Map Set Element */
752 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
753 wpabuf_put_u8(buf, qos_map_set_len);
754 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
755
756 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
757 wpabuf_head(buf), wpabuf_len(buf));
758 wpabuf_free(buf);
759
760 return ret;
761}
762
763#endif /* CONFIG_INTERWORKING */
764
765
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800766#ifdef CONFIG_WNM
767
768static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
769 const char *cmd)
770{
771 u8 addr[ETH_ALEN];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800772 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800773 struct sta_info *sta;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800774
775 if (hwaddr_aton(cmd, addr))
776 return -1;
777 if (cmd[17] != ' ')
778 return -1;
779 disassoc_timer = atoi(cmd + 17);
780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800781 sta = ap_get_sta(hapd, addr);
782 if (sta == NULL) {
783 wpa_printf(MSG_DEBUG, "Station " MACSTR
784 " not found for disassociation imminent message",
785 MAC2STR(addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800786 return -1;
787 }
788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800789 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800790}
791
792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800793static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
794 const char *cmd)
795{
796 u8 addr[ETH_ALEN];
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700797 const char *url, *timerstr;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700798 int disassoc_timer;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800799 struct sta_info *sta;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800
801 if (hwaddr_aton(cmd, addr))
802 return -1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700803
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800804 sta = ap_get_sta(hapd, addr);
805 if (sta == NULL) {
806 wpa_printf(MSG_DEBUG, "Station " MACSTR
807 " not found for ESS disassociation imminent message",
808 MAC2STR(addr));
809 return -1;
810 }
811
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700812 timerstr = cmd + 17;
813 if (*timerstr != ' ')
814 return -1;
815 timerstr++;
816 disassoc_timer = atoi(timerstr);
817 if (disassoc_timer < 0 || disassoc_timer > 65535)
818 return -1;
819
820 url = os_strchr(timerstr, ' ');
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700821 if (url == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822 return -1;
823 url++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800825 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800826}
827
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800828
829static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
830 const char *cmd)
831{
832 u8 addr[ETH_ALEN];
833 const char *pos, *end;
834 int disassoc_timer = 0;
835 struct sta_info *sta;
836 u8 req_mode = 0, valid_int = 0x01;
837 u8 bss_term_dur[12];
838 char *url = NULL;
839 int ret;
840 u8 nei_rep[1000];
841 u8 *nei_pos = nei_rep;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800842 u8 mbo[10];
843 size_t mbo_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844
845 if (hwaddr_aton(cmd, addr)) {
846 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
847 return -1;
848 }
849
850 sta = ap_get_sta(hapd, addr);
851 if (sta == NULL) {
852 wpa_printf(MSG_DEBUG, "Station " MACSTR
853 " not found for BSS TM Request message",
854 MAC2STR(addr));
855 return -1;
856 }
857
858 pos = os_strstr(cmd, " disassoc_timer=");
859 if (pos) {
860 pos += 16;
861 disassoc_timer = atoi(pos);
862 if (disassoc_timer < 0 || disassoc_timer > 65535) {
863 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
864 return -1;
865 }
866 }
867
868 pos = os_strstr(cmd, " valid_int=");
869 if (pos) {
870 pos += 11;
871 valid_int = atoi(pos);
872 }
873
874 pos = os_strstr(cmd, " bss_term=");
875 if (pos) {
876 pos += 10;
877 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
878 /* TODO: TSF configurable/learnable */
879 bss_term_dur[0] = 4; /* Subelement ID */
880 bss_term_dur[1] = 10; /* Length */
881 os_memset(bss_term_dur, 2, 8);
882 end = os_strchr(pos, ',');
883 if (end == NULL) {
884 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
885 return -1;
886 }
887 end++;
888 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
889 }
890
891
892 /*
893 * BSS Transition Candidate List Entries - Neighbor Report elements
894 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
895 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
896 */
897 pos = cmd;
898 while (pos) {
899 u8 *nei_start;
900 long int val;
901 char *endptr, *tmp;
902
903 pos = os_strstr(pos, " neighbor=");
904 if (!pos)
905 break;
906 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
907 wpa_printf(MSG_DEBUG,
908 "Not enough room for additional neighbor");
909 return -1;
910 }
911 pos += 10;
912
913 nei_start = nei_pos;
914 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
915 nei_pos++; /* length to be filled in */
916
917 if (hwaddr_aton(pos, nei_pos)) {
918 wpa_printf(MSG_DEBUG, "Invalid BSSID");
919 return -1;
920 }
921 nei_pos += ETH_ALEN;
922 pos += 17;
923 if (*pos != ',') {
924 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
925 return -1;
926 }
927 pos++;
928
929 val = strtol(pos, &endptr, 0);
930 WPA_PUT_LE32(nei_pos, val);
931 nei_pos += 4;
932 if (*endptr != ',') {
933 wpa_printf(MSG_DEBUG, "Missing Operating Class");
934 return -1;
935 }
936 pos = endptr + 1;
937
938 *nei_pos++ = atoi(pos); /* Operating Class */
939 pos = os_strchr(pos, ',');
940 if (pos == NULL) {
941 wpa_printf(MSG_DEBUG, "Missing Channel Number");
942 return -1;
943 }
944 pos++;
945
946 *nei_pos++ = atoi(pos); /* Channel Number */
947 pos = os_strchr(pos, ',');
948 if (pos == NULL) {
949 wpa_printf(MSG_DEBUG, "Missing PHY Type");
950 return -1;
951 }
952 pos++;
953
954 *nei_pos++ = atoi(pos); /* PHY Type */
955 end = os_strchr(pos, ' ');
956 tmp = os_strchr(pos, ',');
957 if (tmp && (!end || tmp < end)) {
958 /* Optional Subelements (hexdump) */
959 size_t len;
960
961 pos = tmp + 1;
962 end = os_strchr(pos, ' ');
963 if (end)
964 len = end - pos;
965 else
966 len = os_strlen(pos);
967 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
968 wpa_printf(MSG_DEBUG,
969 "Not enough room for neighbor subelements");
970 return -1;
971 }
972 if (len & 0x01 ||
973 hexstr2bin(pos, nei_pos, len / 2) < 0) {
974 wpa_printf(MSG_DEBUG,
975 "Invalid neighbor subelement info");
976 return -1;
977 }
978 nei_pos += len / 2;
979 pos = end;
980 }
981
982 nei_start[1] = nei_pos - nei_start - 2;
983 }
984
985 pos = os_strstr(cmd, " url=");
986 if (pos) {
987 size_t len;
988 pos += 5;
989 end = os_strchr(pos, ' ');
990 if (end)
991 len = end - pos;
992 else
993 len = os_strlen(pos);
994 url = os_malloc(len + 1);
995 if (url == NULL)
996 return -1;
997 os_memcpy(url, pos, len);
998 url[len] = '\0';
999 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1000 }
1001
1002 if (os_strstr(cmd, " pref=1"))
1003 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1004 if (os_strstr(cmd, " abridged=1"))
1005 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1006 if (os_strstr(cmd, " disassoc_imminent=1"))
1007 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1008
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001009#ifdef CONFIG_MBO
1010 pos = os_strstr(cmd, "mbo=");
1011 if (pos) {
1012 unsigned int mbo_reason, cell_pref, reassoc_delay;
1013 u8 *mbo_pos = mbo;
1014
1015 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1016 &reassoc_delay, &cell_pref);
1017 if (ret != 3) {
1018 wpa_printf(MSG_DEBUG,
1019 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1020 return -1;
1021 }
1022
1023 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1024 wpa_printf(MSG_DEBUG,
1025 "Invalid MBO transition reason code %u",
1026 mbo_reason);
1027 return -1;
1028 }
1029
1030 /* Valid values for Cellular preference are: 0, 1, 255 */
1031 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1032 wpa_printf(MSG_DEBUG,
1033 "Invalid MBO cellular capability %u",
1034 cell_pref);
1035 return -1;
1036 }
1037
1038 if (reassoc_delay > 65535 ||
1039 (reassoc_delay &&
1040 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1041 wpa_printf(MSG_DEBUG,
1042 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1043 return -1;
1044 }
1045
1046 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1047 *mbo_pos++ = 1;
1048 *mbo_pos++ = mbo_reason;
1049 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1050 *mbo_pos++ = 1;
1051 *mbo_pos++ = cell_pref;
1052
1053 if (reassoc_delay) {
1054 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1055 *mbo_pos++ = 2;
1056 WPA_PUT_LE16(mbo_pos, reassoc_delay);
1057 mbo_pos += 2;
1058 }
1059
1060 mbo_len = mbo_pos - mbo;
1061 }
1062#endif /* CONFIG_MBO */
1063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1065 valid_int, bss_term_dur, url,
1066 nei_pos > nei_rep ? nei_rep : NULL,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001067 nei_pos - nei_rep, mbo_len ? mbo : NULL,
1068 mbo_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001069 os_free(url);
1070 return ret;
1071}
1072
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001073#endif /* CONFIG_WNM */
1074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001075
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001076static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1077 char *buf, size_t buflen)
1078{
1079 int ret = 0;
1080 char *pos, *end;
1081
1082 pos = buf;
1083 end = buf + buflen;
1084
1085 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1086
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1088 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1089 if (os_snprintf_error(end - pos, ret))
1090 return pos - buf;
1091 pos += ret;
1092 }
1093 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1094 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1095 if (os_snprintf_error(end - pos, ret))
1096 return pos - buf;
1097 pos += ret;
1098 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001099#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001100 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1101 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1102 if (os_snprintf_error(end - pos, ret))
1103 return pos - buf;
1104 pos += ret;
1105 }
1106 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1107 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1108 if (os_snprintf_error(end - pos, ret))
1109 return pos - buf;
1110 pos += ret;
1111 }
1112#ifdef CONFIG_SAE
1113 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1114 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1115 if (os_snprintf_error(end - pos, ret))
1116 return pos - buf;
1117 pos += ret;
1118 }
1119#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001120#ifdef CONFIG_FILS
1121 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1122 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1123 if (os_snprintf_error(end - pos, ret))
1124 return pos - buf;
1125 pos += ret;
1126 }
1127 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1128 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1129 if (os_snprintf_error(end - pos, ret))
1130 return pos - buf;
1131 pos += ret;
1132 }
1133#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001134#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001135#ifdef CONFIG_IEEE80211W
1136 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1137 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1138 if (os_snprintf_error(end - pos, ret))
1139 return pos - buf;
1140 pos += ret;
1141 }
1142 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1143 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1144 if (os_snprintf_error(end - pos, ret))
1145 return pos - buf;
1146 pos += ret;
1147 }
1148#endif /* CONFIG_IEEE80211W */
1149#ifdef CONFIG_SAE
1150 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1151 ret = os_snprintf(pos, end - pos, "SAE ");
1152 if (os_snprintf_error(end - pos, ret))
1153 return pos - buf;
1154 pos += ret;
1155 }
1156#endif /* CONFIG_SAE */
1157 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1158 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1159 if (os_snprintf_error(end - pos, ret))
1160 return pos - buf;
1161 pos += ret;
1162 }
1163 if (hapd->conf->wpa_key_mgmt &
1164 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1165 ret = os_snprintf(pos, end - pos,
1166 "WPA-EAP-SUITE-B-192 ");
1167 if (os_snprintf_error(end - pos, ret))
1168 return pos - buf;
1169 pos += ret;
1170 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001171#ifdef CONFIG_FILS
1172 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1173 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1174 if (os_snprintf_error(end - pos, ret))
1175 return pos - buf;
1176 pos += ret;
1177 }
1178 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1179 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1180 if (os_snprintf_error(end - pos, ret))
1181 return pos - buf;
1182 pos += ret;
1183 }
1184#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001185
1186 if (pos > buf && *(pos - 1) == ' ') {
1187 *(pos - 1) = '\0';
1188 pos--;
1189 }
1190
1191 return pos - buf;
1192}
1193
1194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1196 char *buf, size_t buflen)
1197{
1198 int ret;
1199 char *pos, *end;
1200
1201 pos = buf;
1202 end = buf + buflen;
1203
1204 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1205 "ssid=%s\n",
1206 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001207 wpa_ssid_txt(hapd->conf->ssid.ssid,
1208 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001209 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 return pos - buf;
1211 pos += ret;
1212
1213#ifdef CONFIG_WPS
1214 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1215 hapd->conf->wps_state == 0 ? "disabled" :
1216 (hapd->conf->wps_state == 1 ? "not configured" :
1217 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001218 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 return pos - buf;
1220 pos += ret;
1221
1222 if (hapd->conf->wps_state && hapd->conf->wpa &&
1223 hapd->conf->ssid.wpa_passphrase) {
1224 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1225 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001226 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 return pos - buf;
1228 pos += ret;
1229 }
1230
1231 if (hapd->conf->wps_state && hapd->conf->wpa &&
1232 hapd->conf->ssid.wpa_psk &&
1233 hapd->conf->ssid.wpa_psk->group) {
1234 char hex[PMK_LEN * 2 + 1];
1235 wpa_snprintf_hex(hex, sizeof(hex),
1236 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1237 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001238 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 return pos - buf;
1240 pos += ret;
1241 }
1242#endif /* CONFIG_WPS */
1243
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001244 if (hapd->conf->wpa) {
1245 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1246 if (os_snprintf_error(end - pos, ret))
1247 return pos - buf;
1248 pos += ret;
1249 }
1250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1252 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001253 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 return pos - buf;
1255 pos += ret;
1256
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001257 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258
1259 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001260 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 return pos - buf;
1262 pos += ret;
1263 }
1264
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001265 if (hapd->conf->wpa) {
1266 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1267 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001268 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 return pos - buf;
1270 pos += ret;
1271 }
1272
1273 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1274 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001275 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 return pos - buf;
1277 pos += ret;
1278
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001279 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1280 " ");
1281 if (ret < 0)
1282 return pos - buf;
1283 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
1285 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001286 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 return pos - buf;
1288 pos += ret;
1289 }
1290
1291 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1292 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001293 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 return pos - buf;
1295 pos += ret;
1296
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001297 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001298 " ");
1299 if (ret < 0)
1300 return pos - buf;
1301 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302
1303 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001304 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 return pos - buf;
1306 pos += ret;
1307 }
1308
1309 return pos - buf;
1310}
1311
1312
1313static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1314{
1315 char *value;
1316 int ret = 0;
1317
1318 value = os_strchr(cmd, ' ');
1319 if (value == NULL)
1320 return -1;
1321 *value++ = '\0';
1322
1323 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1324 if (0) {
1325#ifdef CONFIG_WPS_TESTING
1326 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1327 long int val;
1328 val = strtol(value, NULL, 0);
1329 if (val < 0 || val > 0xff) {
1330 ret = -1;
1331 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1332 "wps_version_number %ld", val);
1333 } else {
1334 wps_version_number = val;
1335 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1336 "version %u.%u",
1337 (wps_version_number & 0xf0) >> 4,
1338 wps_version_number & 0x0f);
1339 hostapd_wps_update_ie(hapd);
1340 }
1341 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1342 wps_testing_dummy_cred = atoi(value);
1343 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1344 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001345 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1346 wps_corrupt_pkhash = atoi(value);
1347 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1348 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001350#ifdef CONFIG_INTERWORKING
1351 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1352 int val = atoi(value);
1353 if (val <= 0)
1354 ret = -1;
1355 else
1356 hapd->gas_frag_limit = val;
1357#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001358#ifdef CONFIG_TESTING_OPTIONS
1359 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1360 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001361 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1362 hapd->ext_eapol_frame_io = atoi(value);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001363#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001364#ifdef CONFIG_MBO
1365 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1366 int val;
1367
1368 if (!hapd->conf->mbo_enabled)
1369 return -1;
1370
1371 val = atoi(value);
1372 if (val < 0 || val > 1)
1373 return -1;
1374
1375 hapd->mbo_assoc_disallow = val;
1376 ieee802_11_update_beacons(hapd->iface);
1377
1378 /*
1379 * TODO: Need to configure drivers that do AP MLME offload with
1380 * disallowing station logic.
1381 */
1382#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001384 struct sta_info *sta;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001385 struct vlan_description vlan_id;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001386
Dmitry Shmidt04949592012-07-19 12:16:46 -07001387 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001388 if (ret)
1389 return ret;
1390
1391 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1392 for (sta = hapd->sta_list; sta; sta = sta->next) {
1393 if (hostapd_maclist_found(
1394 hapd->conf->deny_mac,
1395 hapd->conf->num_deny_mac, sta->addr,
1396 &vlan_id) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001397 (!vlan_id.notempty ||
1398 !vlan_compare(&vlan_id, sta->vlan_desc)))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001399 ap_sta_disconnect(
1400 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001401 WLAN_REASON_UNSPECIFIED);
1402 }
1403 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1404 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1405 for (sta = hapd->sta_list; sta; sta = sta->next) {
1406 if (!hostapd_maclist_found(
1407 hapd->conf->accept_mac,
1408 hapd->conf->num_accept_mac,
1409 sta->addr, &vlan_id) ||
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001410 (vlan_id.notempty &&
1411 vlan_compare(&vlan_id, sta->vlan_desc)))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001412 ap_sta_disconnect(
1413 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001414 WLAN_REASON_UNSPECIFIED);
1415 }
1416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 }
1418
1419 return ret;
1420}
1421
1422
1423static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1424 char *buf, size_t buflen)
1425{
1426 int res;
1427
1428 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1429
1430 if (os_strcmp(cmd, "version") == 0) {
1431 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001432 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 return -1;
1434 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001435 } else if (os_strcmp(cmd, "tls_library") == 0) {
1436 res = tls_get_library_version(buf, buflen);
1437 if (os_snprintf_error(buflen, res))
1438 return -1;
1439 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 }
1441
1442 return -1;
1443}
1444
1445
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001446static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1447{
1448 if (hostapd_enable_iface(iface) < 0) {
1449 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1450 return -1;
1451 }
1452 return 0;
1453}
1454
1455
1456static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1457{
1458 if (hostapd_reload_iface(iface) < 0) {
1459 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1460 return -1;
1461 }
1462 return 0;
1463}
1464
1465
1466static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1467{
1468 if (hostapd_disable_iface(iface) < 0) {
1469 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1470 return -1;
1471 }
1472 return 0;
1473}
1474
1475
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001476#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001477
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001478static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1479{
1480 union wpa_event_data data;
1481 char *pos, *param;
1482 enum wpa_event_type event;
1483
1484 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1485
1486 os_memset(&data, 0, sizeof(data));
1487
1488 param = os_strchr(cmd, ' ');
1489 if (param == NULL)
1490 return -1;
1491 *param++ = '\0';
1492
1493 if (os_strcmp(cmd, "DETECTED") == 0)
1494 event = EVENT_DFS_RADAR_DETECTED;
1495 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1496 event = EVENT_DFS_CAC_FINISHED;
1497 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1498 event = EVENT_DFS_CAC_ABORTED;
1499 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1500 event = EVENT_DFS_NOP_FINISHED;
1501 else {
1502 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1503 cmd);
1504 return -1;
1505 }
1506
1507 pos = os_strstr(param, "freq=");
1508 if (pos)
1509 data.dfs_event.freq = atoi(pos + 5);
1510
1511 pos = os_strstr(param, "ht_enabled=1");
1512 if (pos)
1513 data.dfs_event.ht_enabled = 1;
1514
1515 pos = os_strstr(param, "chan_offset=");
1516 if (pos)
1517 data.dfs_event.chan_offset = atoi(pos + 12);
1518
1519 pos = os_strstr(param, "chan_width=");
1520 if (pos)
1521 data.dfs_event.chan_width = atoi(pos + 11);
1522
1523 pos = os_strstr(param, "cf1=");
1524 if (pos)
1525 data.dfs_event.cf1 = atoi(pos + 4);
1526
1527 pos = os_strstr(param, "cf2=");
1528 if (pos)
1529 data.dfs_event.cf2 = atoi(pos + 4);
1530
1531 wpa_supplicant_event(hapd, event, &data);
1532
1533 return 0;
1534}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535
1536
1537static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1538{
1539 size_t len;
1540 u8 *buf;
1541 int res;
1542
1543 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1544
1545 len = os_strlen(cmd);
1546 if (len & 1)
1547 return -1;
1548 len /= 2;
1549
1550 buf = os_malloc(len);
1551 if (buf == NULL)
1552 return -1;
1553
1554 if (hexstr2bin(cmd, buf, len) < 0) {
1555 os_free(buf);
1556 return -1;
1557 }
1558
1559 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1560 os_free(buf);
1561 return res;
1562}
1563
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001564
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001565static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1566 char *cmd)
1567{
1568 char *pos, *param;
1569 size_t len;
1570 u8 *buf;
1571 int freq = 0, datarate = 0, ssi_signal = 0;
1572 union wpa_event_data event;
1573
1574 if (!hapd->ext_mgmt_frame_handling)
1575 return -1;
1576
1577 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1578
1579 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1580
1581 pos = cmd;
1582 param = os_strstr(pos, "freq=");
1583 if (param) {
1584 param += 5;
1585 freq = atoi(param);
1586 }
1587
1588 param = os_strstr(pos, " datarate=");
1589 if (param) {
1590 param += 10;
1591 datarate = atoi(param);
1592 }
1593
1594 param = os_strstr(pos, " ssi_signal=");
1595 if (param) {
1596 param += 12;
1597 ssi_signal = atoi(param);
1598 }
1599
1600 param = os_strstr(pos, " frame=");
1601 if (param == NULL)
1602 return -1;
1603 param += 7;
1604
1605 len = os_strlen(param);
1606 if (len & 1)
1607 return -1;
1608 len /= 2;
1609
1610 buf = os_malloc(len);
1611 if (buf == NULL)
1612 return -1;
1613
1614 if (hexstr2bin(param, buf, len) < 0) {
1615 os_free(buf);
1616 return -1;
1617 }
1618
1619 os_memset(&event, 0, sizeof(event));
1620 event.rx_mgmt.freq = freq;
1621 event.rx_mgmt.frame = buf;
1622 event.rx_mgmt.frame_len = len;
1623 event.rx_mgmt.ssi_signal = ssi_signal;
1624 event.rx_mgmt.datarate = datarate;
1625 hapd->ext_mgmt_frame_handling = 0;
1626 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1627 hapd->ext_mgmt_frame_handling = 1;
1628
1629 os_free(buf);
1630
1631 return 0;
1632}
1633
1634
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001635static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1636{
1637 char *pos;
1638 u8 src[ETH_ALEN], *buf;
1639 int used;
1640 size_t len;
1641
1642 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1643
1644 pos = cmd;
1645 used = hwaddr_aton2(pos, src);
1646 if (used < 0)
1647 return -1;
1648 pos += used;
1649 while (*pos == ' ')
1650 pos++;
1651
1652 len = os_strlen(pos);
1653 if (len & 1)
1654 return -1;
1655 len /= 2;
1656
1657 buf = os_malloc(len);
1658 if (buf == NULL)
1659 return -1;
1660
1661 if (hexstr2bin(pos, buf, len) < 0) {
1662 os_free(buf);
1663 return -1;
1664 }
1665
1666 ieee802_1x_receive(hapd, src, buf, len);
1667 os_free(buf);
1668
1669 return 0;
1670}
1671
1672
1673static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1674{
1675 size_t i;
1676 u32 sum = 0;
1677 const u16 *pos = buf;
1678
1679 for (i = 0; i < len / 2; i++)
1680 sum += *pos++;
1681
1682 while (sum >> 16)
1683 sum = (sum & 0xffff) + (sum >> 16);
1684
1685 return sum ^ 0xffff;
1686}
1687
1688
1689#define HWSIM_PACKETLEN 1500
1690#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1691
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001692static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1693 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001694{
1695 struct hostapd_data *hapd = ctx;
1696 const struct ether_header *eth;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001697 struct iphdr ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001698 const u8 *pos;
1699 unsigned int i;
1700
1701 if (len != HWSIM_PACKETLEN)
1702 return;
1703
1704 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001705 os_memcpy(&ip, eth + 1, sizeof(ip));
1706 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001707
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001708 if (ip.ihl != 5 || ip.version != 4 ||
1709 ntohs(ip.tot_len) != HWSIM_IP_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001710 return;
1711
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001712 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001713 if (*pos != (u8) i)
1714 return;
1715 pos++;
1716 }
1717
1718 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1719 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1720}
1721
1722
1723static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1724 char *cmd)
1725{
1726 int enabled = atoi(cmd);
1727 char *pos;
1728 const char *ifname;
1729
1730 if (!enabled) {
1731 if (hapd->l2_test) {
1732 l2_packet_deinit(hapd->l2_test);
1733 hapd->l2_test = NULL;
1734 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1735 "test data: Disabled");
1736 }
1737 return 0;
1738 }
1739
1740 if (hapd->l2_test)
1741 return 0;
1742
1743 pos = os_strstr(cmd, " ifname=");
1744 if (pos)
1745 ifname = pos + 8;
1746 else
1747 ifname = hapd->conf->iface;
1748
1749 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1750 ETHERTYPE_IP, hostapd_data_test_rx,
1751 hapd, 1);
1752 if (hapd->l2_test == NULL)
1753 return -1;
1754
1755 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1756
1757 return 0;
1758}
1759
1760
1761static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1762{
1763 u8 dst[ETH_ALEN], src[ETH_ALEN];
1764 char *pos;
1765 int used;
1766 long int val;
1767 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001768 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001769 struct ether_header *eth;
1770 struct iphdr *ip;
1771 u8 *dpos;
1772 unsigned int i;
1773
1774 if (hapd->l2_test == NULL)
1775 return -1;
1776
1777 /* format: <dst> <src> <tos> */
1778
1779 pos = cmd;
1780 used = hwaddr_aton2(pos, dst);
1781 if (used < 0)
1782 return -1;
1783 pos += used;
1784 while (*pos == ' ')
1785 pos++;
1786 used = hwaddr_aton2(pos, src);
1787 if (used < 0)
1788 return -1;
1789 pos += used;
1790
1791 val = strtol(pos, NULL, 0);
1792 if (val < 0 || val > 0xff)
1793 return -1;
1794 tos = val;
1795
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001796 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001797 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1798 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1799 eth->ether_type = htons(ETHERTYPE_IP);
1800 ip = (struct iphdr *) (eth + 1);
1801 os_memset(ip, 0, sizeof(*ip));
1802 ip->ihl = 5;
1803 ip->version = 4;
1804 ip->ttl = 64;
1805 ip->tos = tos;
1806 ip->tot_len = htons(HWSIM_IP_LEN);
1807 ip->protocol = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001808 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1809 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001810 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1811 dpos = (u8 *) (ip + 1);
1812 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1813 *dpos++ = i;
1814
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001815 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001816 HWSIM_PACKETLEN) < 0)
1817 return -1;
1818
1819 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1820 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1821
1822 return 0;
1823}
1824
1825
1826static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1827 char *cmd)
1828{
1829 u8 *buf;
1830 struct ether_header *eth;
1831 struct l2_packet_data *l2 = NULL;
1832 size_t len;
1833 u16 ethertype;
1834 int res = -1;
1835 const char *ifname = hapd->conf->iface;
1836
1837 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1838 cmd += 7;
1839 ifname = cmd;
1840 cmd = os_strchr(cmd, ' ');
1841 if (cmd == NULL)
1842 return -1;
1843 *cmd++ = '\0';
1844 }
1845
1846 len = os_strlen(cmd);
1847 if (len & 1 || len < ETH_HLEN * 2)
1848 return -1;
1849 len /= 2;
1850
1851 buf = os_malloc(len);
1852 if (buf == NULL)
1853 return -1;
1854
1855 if (hexstr2bin(cmd, buf, len) < 0)
1856 goto done;
1857
1858 eth = (struct ether_header *) buf;
1859 ethertype = ntohs(eth->ether_type);
1860
1861 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1862 hostapd_data_test_rx, hapd, 1);
1863 if (l2 == NULL)
1864 goto done;
1865
1866 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1867 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1868done:
1869 if (l2)
1870 l2_packet_deinit(l2);
1871 os_free(buf);
1872
1873 return res < 0 ? -1 : 0;
1874}
1875
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001876
1877static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1878{
1879#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001880 char *pos;
1881
1882 wpa_trace_fail_after = atoi(cmd);
1883 pos = os_strchr(cmd, ':');
1884 if (pos) {
1885 pos++;
1886 os_strlcpy(wpa_trace_fail_func, pos,
1887 sizeof(wpa_trace_fail_func));
1888 } else {
1889 wpa_trace_fail_after = 0;
1890 }
1891
1892 return 0;
1893#else /* WPA_TRACE_BFD */
1894 return -1;
1895#endif /* WPA_TRACE_BFD */
1896}
1897
1898
1899static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1900 char *buf, size_t buflen)
1901{
1902#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001903 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1904 wpa_trace_fail_func);
1905#else /* WPA_TRACE_BFD */
1906 return -1;
1907#endif /* WPA_TRACE_BFD */
1908}
1909
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001910
1911static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1912{
1913#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001914 char *pos;
1915
1916 wpa_trace_test_fail_after = atoi(cmd);
1917 pos = os_strchr(cmd, ':');
1918 if (pos) {
1919 pos++;
1920 os_strlcpy(wpa_trace_test_fail_func, pos,
1921 sizeof(wpa_trace_test_fail_func));
1922 } else {
1923 wpa_trace_test_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_fail(struct hostapd_data *hapd,
1934 char *buf, size_t buflen)
1935{
1936#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001937 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1938 wpa_trace_test_fail_func);
1939#else /* WPA_TRACE_BFD */
1940 return -1;
1941#endif /* WPA_TRACE_BFD */
1942}
1943
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001944#endif /* CONFIG_TESTING_OPTIONS */
1945
1946
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001947static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1948 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001949{
1950#ifdef NEED_AP_MLME
1951 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001952 int ret;
1953 unsigned int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001954
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001955 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001956 if (ret)
1957 return ret;
1958
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001959 for (i = 0; i < iface->num_bss; i++) {
1960 ret = hostapd_switch_channel(iface->bss[i], &settings);
1961 if (ret) {
1962 /* FIX: What do we do if CSA fails in the middle of
1963 * submitting multi-BSS CSA requests? */
1964 return ret;
1965 }
1966 }
1967
1968 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001969#else /* NEED_AP_MLME */
1970 return -1;
1971#endif /* NEED_AP_MLME */
1972}
1973
1974
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001975static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1976 int reply_size, const char *param)
1977{
1978#ifdef RADIUS_SERVER
1979 if (os_strcmp(param, "radius_server") == 0) {
1980 return radius_server_get_mib(hapd->radius_srv, reply,
1981 reply_size);
1982 }
1983#endif /* RADIUS_SERVER */
1984 return -1;
1985}
1986
1987
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001988static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1989 char *buf, size_t buflen)
1990{
1991 int ret;
1992 char *pos;
1993 u8 *data = NULL;
1994 unsigned int vendor_id, subcmd;
1995 struct wpabuf *reply;
1996 size_t data_len = 0;
1997
1998 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1999 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002000 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002001 return -EINVAL;
2002
2003 subcmd = strtoul(pos, &pos, 10);
2004
2005 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002006 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002007 return -EINVAL;
2008 data_len = os_strlen(pos);
2009 }
2010
2011 if (data_len) {
2012 data_len /= 2;
2013 data = os_malloc(data_len);
2014 if (!data)
2015 return -ENOBUFS;
2016
2017 if (hexstr2bin(pos, data, data_len)) {
2018 wpa_printf(MSG_DEBUG,
2019 "Vendor command: wrong parameter format");
2020 os_free(data);
2021 return -EINVAL;
2022 }
2023 }
2024
2025 reply = wpabuf_alloc((buflen - 1) / 2);
2026 if (!reply) {
2027 os_free(data);
2028 return -ENOBUFS;
2029 }
2030
2031 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2032 reply);
2033
2034 if (ret == 0)
2035 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2036 wpabuf_len(reply));
2037
2038 wpabuf_free(reply);
2039 os_free(data);
2040
2041 return ret;
2042}
2043
2044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002045static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2046 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002048 u8 addr[ETH_ALEN];
2049 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002051 if (hwaddr_aton(cmd, addr))
2052 return -1;
2053
2054 sta = ap_get_sta(hapd, addr);
2055 if (!sta || !sta->eapol_sm)
2056 return -1;
2057
2058 eapol_auth_reauthenticate(sta->eapol_sm);
2059 return 0;
2060}
2061
2062
2063static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2064{
2065 u8 addr[ETH_ALEN];
2066 struct sta_info *sta;
2067 char *pos = cmd, *param;
2068
2069 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2070 return -1;
2071 pos += 18;
2072 param = pos;
2073 pos = os_strchr(pos, ' ');
2074 if (!pos)
2075 return -1;
2076 *pos++ = '\0';
2077
2078 sta = ap_get_sta(hapd, addr);
2079 if (!sta || !sta->eapol_sm)
2080 return -1;
2081
2082 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2083}
2084
2085
2086static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2087 char *buf, size_t buflen)
2088{
2089 char *pos, *end, *stamp;
2090 int ret;
2091
2092 /* cmd: "LOG_LEVEL [<level>]" */
2093 if (*cmd == '\0') {
2094 pos = buf;
2095 end = buf + buflen;
2096 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2097 "Timestamp: %d\n",
2098 debug_level_str(wpa_debug_level),
2099 wpa_debug_timestamp);
2100 if (os_snprintf_error(end - pos, ret))
2101 ret = 0;
2102
2103 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002106 while (*cmd == ' ')
2107 cmd++;
2108
2109 stamp = os_strchr(cmd, ' ');
2110 if (stamp) {
2111 *stamp++ = '\0';
2112 while (*stamp == ' ') {
2113 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002114 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 }
2116
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002117 if (os_strlen(cmd)) {
2118 int level = str_to_debug_level(cmd);
2119 if (level < 0)
2120 return -1;
2121 wpa_debug_level = level;
2122 }
2123
2124 if (stamp && os_strlen(stamp))
2125 wpa_debug_timestamp = atoi(stamp);
2126
2127 os_memcpy(buf, "OK\n", 3);
2128 return 3;
2129}
2130
2131
2132#ifdef NEED_AP_MLME
2133static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2134 char *buf, size_t buflen)
2135{
2136 struct hostapd_iface *iface = hapd->iface;
2137 char *pos, *end;
2138 struct hostapd_sta_info *info;
2139 struct os_reltime now;
2140
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002141 if (!iface->num_sta_seen)
2142 return 0;
2143
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002144 sta_track_expire(iface, 0);
2145
2146 pos = buf;
2147 end = buf + buflen;
2148
2149 os_get_reltime(&now);
2150 dl_list_for_each_reverse(info, &iface->sta_seen,
2151 struct hostapd_sta_info, list) {
2152 struct os_reltime age;
2153 int ret;
2154
2155 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002156 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2157 MAC2STR(info->addr), (unsigned int) age.sec,
2158 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002159 if (os_snprintf_error(end - pos, ret))
2160 break;
2161 pos += ret;
2162 }
2163
2164 return pos - buf;
2165}
2166#endif /* NEED_AP_MLME */
2167
2168
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002169static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2170 const char *cmd)
2171{
2172 u8 addr[ETH_ALEN];
2173
2174 if (hwaddr_aton(cmd, addr)) {
2175 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2176 return -1;
2177 }
2178
2179 return hostapd_send_lci_req(hapd, addr);
2180}
2181
2182
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002183static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002184{
2185 u8 addr[ETH_ALEN];
2186 char *token, *context = NULL;
2187 int random_interval, min_ap;
2188 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2189 unsigned int n_responders;
2190
2191 token = str_token(cmd, " ", &context);
2192 if (!token || hwaddr_aton(token, addr)) {
2193 wpa_printf(MSG_INFO,
2194 "CTRL: REQ_RANGE - Bad destination address");
2195 return -1;
2196 }
2197
2198 token = str_token(cmd, " ", &context);
2199 if (!token)
2200 return -1;
2201
2202 random_interval = atoi(token);
2203 if (random_interval < 0 || random_interval > 0xffff)
2204 return -1;
2205
2206 token = str_token(cmd, " ", &context);
2207 if (!token)
2208 return -1;
2209
2210 min_ap = atoi(token);
2211 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2212 return -1;
2213
2214 n_responders = 0;
2215 while ((token = str_token(cmd, " ", &context))) {
2216 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2217 wpa_printf(MSG_INFO,
2218 "CTRL: REQ_RANGE: Too many responders");
2219 return -1;
2220 }
2221
2222 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2223 wpa_printf(MSG_INFO,
2224 "CTRL: REQ_RANGE: Bad responder address");
2225 return -1;
2226 }
2227
2228 n_responders++;
2229 }
2230
2231 if (!n_responders) {
2232 wpa_printf(MSG_INFO,
2233 "CTRL: REQ_RANGE - No FTM responder address");
2234 return -1;
2235 }
2236
2237 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2238 responders, n_responders);
2239}
2240
2241
2242static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2243{
2244 struct wpa_ssid_value ssid;
2245 u8 bssid[ETH_ALEN];
2246 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002247 int stationary = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002248 char *tmp;
2249 int ret;
2250
2251 if (!(hapd->conf->radio_measurements[0] &
2252 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2253 wpa_printf(MSG_ERROR,
2254 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2255 return -1;
2256 }
2257
2258 if (hwaddr_aton(buf, bssid)) {
2259 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2260 return -1;
2261 }
2262
2263 tmp = os_strstr(buf, "ssid=");
2264 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2265 wpa_printf(MSG_ERROR,
2266 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2267 return -1;
2268 }
2269 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2270 if (!buf)
2271 return -1;
2272
2273 tmp = os_strstr(buf, "nr=");
2274 if (!tmp) {
2275 wpa_printf(MSG_ERROR,
2276 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2277 return -1;
2278 }
2279
2280 buf = os_strchr(tmp, ' ');
2281 if (buf)
2282 *buf++ = '\0';
2283
2284 nr = wpabuf_parse_bin(tmp + 3);
2285 if (!nr) {
2286 wpa_printf(MSG_ERROR,
2287 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2288 return -1;
2289 }
2290
2291 if (!buf)
2292 goto set;
2293
2294 tmp = os_strstr(buf, "lci=");
2295 if (tmp) {
2296 buf = os_strchr(tmp, ' ');
2297 if (buf)
2298 *buf++ = '\0';
2299 lci = wpabuf_parse_bin(tmp + 4);
2300 if (!lci) {
2301 wpa_printf(MSG_ERROR,
2302 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2303 wpabuf_free(nr);
2304 return -1;
2305 }
2306 }
2307
2308 if (!buf)
2309 goto set;
2310
2311 tmp = os_strstr(buf, "civic=");
2312 if (tmp) {
2313 buf = os_strchr(tmp, ' ');
2314 if (buf)
2315 *buf++ = '\0';
2316 civic = wpabuf_parse_bin(tmp + 6);
2317 if (!civic) {
2318 wpa_printf(MSG_ERROR,
2319 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2320 wpabuf_free(nr);
2321 wpabuf_free(lci);
2322 return -1;
2323 }
2324 }
2325
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002326 if (!buf)
2327 goto set;
2328
2329 if (os_strstr(buf, "stat"))
2330 stationary = 1;
2331
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002332set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002333 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2334 stationary);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002335
2336 wpabuf_free(nr);
2337 wpabuf_free(lci);
2338 wpabuf_free(civic);
2339
2340 return ret;
2341}
2342
2343
2344static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2345 char *buf)
2346{
2347 struct wpa_ssid_value ssid;
2348 u8 bssid[ETH_ALEN];
2349 char *tmp;
2350
2351 if (hwaddr_aton(buf, bssid)) {
2352 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2353 return -1;
2354 }
2355
2356 tmp = os_strstr(buf, "ssid=");
2357 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2358 wpa_printf(MSG_ERROR,
2359 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2360 return -1;
2361 }
2362
2363 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2364}
2365
2366
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002367static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2368 size_t buflen)
2369{
2370 int ret, i;
2371 char *pos, *end;
2372
2373 ret = os_snprintf(buf, buflen, "%016llX:\n",
2374 (long long unsigned) iface->drv_flags);
2375 if (os_snprintf_error(buflen, ret))
2376 return -1;
2377
2378 pos = buf + ret;
2379 end = buf + buflen;
2380
2381 for (i = 0; i < 64; i++) {
2382 if (iface->drv_flags & (1LLU << i)) {
2383 ret = os_snprintf(pos, end - pos, "%s\n",
2384 driver_flag_to_string(1LLU << i));
2385 if (os_snprintf_error(end - pos, ret))
2386 return -1;
2387 pos += ret;
2388 }
2389 }
2390
2391 return pos - buf;
2392}
2393
2394
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002395static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2396 char *buf, char *reply,
2397 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002398 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002399 socklen_t fromlen)
2400{
2401 int reply_len, res;
2402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 os_memcpy(reply, "OK\n", 3);
2404 reply_len = 3;
2405
2406 if (os_strcmp(buf, "PING") == 0) {
2407 os_memcpy(reply, "PONG\n", 5);
2408 reply_len = 5;
2409 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2410 if (wpa_debug_reopen_file() < 0)
2411 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002412 } else if (os_strcmp(buf, "STATUS") == 0) {
2413 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2414 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002415 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2416 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 } else if (os_strcmp(buf, "MIB") == 0) {
2418 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2419 if (reply_len >= 0) {
2420 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2421 reply_size - reply_len);
2422 if (res < 0)
2423 reply_len = -1;
2424 else
2425 reply_len += res;
2426 }
2427 if (reply_len >= 0) {
2428 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2429 reply_size - reply_len);
2430 if (res < 0)
2431 reply_len = -1;
2432 else
2433 reply_len += res;
2434 }
2435#ifndef CONFIG_NO_RADIUS
2436 if (reply_len >= 0) {
2437 res = radius_client_get_mib(hapd->radius,
2438 reply + reply_len,
2439 reply_size - reply_len);
2440 if (res < 0)
2441 reply_len = -1;
2442 else
2443 reply_len += res;
2444 }
2445#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002446 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2447 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2448 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2450 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2451 reply_size);
2452 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2453 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2454 reply_size);
2455 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2456 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2457 reply_size);
2458 } else if (os_strcmp(buf, "ATTACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002459 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 reply_len = -1;
2461 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002462 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 reply_len = -1;
2464 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002465 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 buf + 6))
2467 reply_len = -1;
2468 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2469 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2470 reply_len = -1;
2471 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2472 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2473 reply_len = -1;
2474 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2475 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2476 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002477#ifdef CONFIG_TAXONOMY
2478 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
2479 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
2480 reply, reply_size);
2481#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002482 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2483 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2484 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002485 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2486 if (hostapd_ctrl_iface_stop_ap(hapd))
2487 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488#ifdef CONFIG_IEEE80211W
2489#ifdef NEED_AP_MLME
2490 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2491 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2492 reply_len = -1;
2493#endif /* NEED_AP_MLME */
2494#endif /* CONFIG_IEEE80211W */
2495#ifdef CONFIG_WPS
2496 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2497 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2498 reply_len = -1;
2499 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2500 reply_len = hostapd_ctrl_iface_wps_check_pin(
2501 hapd, buf + 14, reply, reply_size);
2502 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2503 if (hostapd_wps_button_pushed(hapd, NULL))
2504 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2506 if (hostapd_wps_cancel(hapd))
2507 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2509 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2510 reply, reply_size);
2511 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2512 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2513 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002514 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2515 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2516 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002517#ifdef CONFIG_WPS_NFC
2518 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2519 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2520 reply_len = -1;
2521 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2522 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2523 hapd, buf + 21, reply, reply_size);
2524 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2525 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2526 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002527 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2528 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2529 hapd, buf + 21, reply, reply_size);
2530 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2531 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2532 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002533#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002535#ifdef CONFIG_INTERWORKING
2536 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2537 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2538 reply_len = -1;
2539 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2540 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2541 reply_len = -1;
2542#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002543#ifdef CONFIG_HS20
2544 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2545 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2546 reply_len = -1;
2547 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2548 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2549 reply_len = -1;
2550#endif /* CONFIG_HS20 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002551#ifdef CONFIG_WNM
2552 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2553 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2554 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2556 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2557 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002558 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2559 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2560 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002561#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2563 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2564 reply_size);
2565 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2566 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2567 reply_len = -1;
2568 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2569 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2570 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002571 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2572 if (hostapd_ctrl_iface_enable(hapd->iface))
2573 reply_len = -1;
2574 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2575 if (hostapd_ctrl_iface_reload(hapd->iface))
2576 reply_len = -1;
2577 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2578 if (hostapd_ctrl_iface_disable(hapd->iface))
2579 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002580 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2581 if (ieee802_11_set_beacon(hapd))
2582 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002583#ifdef CONFIG_TESTING_OPTIONS
2584 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2585 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2586 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002587 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2588 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2589 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002590 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
2591 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
2592 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002593 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2594 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2595 reply_len = -1;
2596 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2597 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2598 reply_len = -1;
2599 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2600 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2601 reply_len = -1;
2602 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2603 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2604 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002605 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2606 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2607 reply_len = -1;
2608 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2609 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2610 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002611 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2612 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2613 reply_len = -1;
2614 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2615 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002616#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002617 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002618 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002619 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002620 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2621 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2622 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002623 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2624 ieee802_1x_erp_flush(hapd);
2625#ifdef RADIUS_SERVER
2626 radius_server_erp_flush(hapd->radius_srv);
2627#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002628 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2629 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2630 reply_len = -1;
2631 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2632 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2633 reply_len = -1;
2634 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2635 reply_len = hostapd_ctrl_iface_log_level(
2636 hapd, buf + 9, reply, reply_size);
2637#ifdef NEED_AP_MLME
2638 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2639 reply_len = hostapd_ctrl_iface_track_sta_list(
2640 hapd, reply, reply_size);
2641#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07002642 } else if (os_strcmp(buf, "PMKSA") == 0) {
2643 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2644 reply_size);
2645 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2646 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002647 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2648 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2649 reply_len = -1;
2650 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2651 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2652 reply_len = -1;
2653 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
2654 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
2655 reply_len = -1;
2656 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
2657 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
2658 reply_len = -1;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002659 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
2660 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
2661 reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 } else {
2663 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2664 reply_len = 16;
2665 }
2666
2667 if (reply_len < 0) {
2668 os_memcpy(reply, "FAIL\n", 5);
2669 reply_len = 5;
2670 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002671
2672 return reply_len;
2673}
2674
2675
2676static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2677 void *sock_ctx)
2678{
2679 struct hostapd_data *hapd = eloop_ctx;
2680 char buf[4096];
2681 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002682 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002683 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002684 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002685 const int reply_size = 4096;
2686 int reply_len;
2687 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002688#ifdef CONFIG_CTRL_IFACE_UDP
2689 unsigned char lcookie[COOKIE_LEN];
2690#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002691
2692 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2693 (struct sockaddr *) &from, &fromlen);
2694 if (res < 0) {
2695 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2696 strerror(errno));
2697 return;
2698 }
2699 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002700
2701 reply = os_malloc(reply_size);
2702 if (reply == NULL) {
2703 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2704 fromlen) < 0) {
2705 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2706 strerror(errno));
2707 }
2708 return;
2709 }
2710
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002711#ifdef CONFIG_CTRL_IFACE_UDP
2712 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2713 os_memcpy(reply, "COOKIE=", 7);
2714 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2715 cookie, COOKIE_LEN);
2716 reply_len = 7 + 2 * COOKIE_LEN;
2717 goto done;
2718 }
2719
2720 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2721 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2722 wpa_printf(MSG_DEBUG,
2723 "CTRL: No cookie in the request - drop request");
2724 os_free(reply);
2725 return;
2726 }
2727
2728 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2729 wpa_printf(MSG_DEBUG,
2730 "CTRL: Invalid cookie in the request - drop request");
2731 os_free(reply);
2732 return;
2733 }
2734
2735 pos = buf + 7 + 2 * COOKIE_LEN;
2736 while (*pos == ' ')
2737 pos++;
2738#endif /* CONFIG_CTRL_IFACE_UDP */
2739
2740 if (os_strcmp(pos, "PING") == 0)
2741 level = MSG_EXCESSIVE;
2742 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2743
2744 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002745 reply, reply_size,
2746 &from, fromlen);
2747
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002748#ifdef CONFIG_CTRL_IFACE_UDP
2749done:
2750#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002751 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2752 fromlen) < 0) {
2753 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2754 strerror(errno));
2755 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 os_free(reply);
2757}
2758
2759
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002760#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2762{
2763 char *buf;
2764 size_t len;
2765
2766 if (hapd->conf->ctrl_interface == NULL)
2767 return NULL;
2768
2769 len = os_strlen(hapd->conf->ctrl_interface) +
2770 os_strlen(hapd->conf->iface) + 2;
2771 buf = os_malloc(len);
2772 if (buf == NULL)
2773 return NULL;
2774
2775 os_snprintf(buf, len, "%s/%s",
2776 hapd->conf->ctrl_interface, hapd->conf->iface);
2777 buf[len - 1] = '\0';
2778 return buf;
2779}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002780#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781
2782
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002783static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2784 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 const char *txt, size_t len)
2786{
2787 struct hostapd_data *hapd = ctx;
2788 if (hapd == NULL)
2789 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02002790 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791}
2792
2793
2794int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2795{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002796#ifdef CONFIG_CTRL_IFACE_UDP
2797 int port = HOSTAPD_CTRL_IFACE_PORT;
2798 char p[32] = { 0 };
2799 char port_str[40], *tmp;
2800 char *pos;
2801 struct addrinfo hints = { 0 }, *res, *saveres;
2802 int n;
2803
2804 if (hapd->ctrl_sock > -1) {
2805 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2806 return 0;
2807 }
2808
2809 if (hapd->conf->ctrl_interface == NULL)
2810 return 0;
2811
2812 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
2813 if (pos) {
2814 pos += 4;
2815 port = atoi(pos);
2816 if (port <= 0) {
2817 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
2818 goto fail;
2819 }
2820 }
2821
2822 dl_list_init(&hapd->ctrl_dst);
2823 hapd->ctrl_sock = -1;
2824 os_get_random(cookie, COOKIE_LEN);
2825
2826#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2827 hints.ai_flags = AI_PASSIVE;
2828#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2829
2830#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2831 hints.ai_family = AF_INET6;
2832#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2833 hints.ai_family = AF_INET;
2834#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2835 hints.ai_socktype = SOCK_DGRAM;
2836
2837try_again:
2838 os_snprintf(p, sizeof(p), "%d", port);
2839 n = getaddrinfo(NULL, p, &hints, &res);
2840 if (n) {
2841 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
2842 goto fail;
2843 }
2844
2845 saveres = res;
2846 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
2847 res->ai_protocol);
2848 if (hapd->ctrl_sock < 0) {
2849 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
2850 goto fail;
2851 }
2852
2853 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
2854 port--;
2855 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
2856 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
2857 goto try_again;
2858 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
2859 goto fail;
2860 }
2861
2862 freeaddrinfo(saveres);
2863
2864 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
2865 tmp = os_strdup(port_str);
2866 if (tmp) {
2867 os_free(hapd->conf->ctrl_interface);
2868 hapd->conf->ctrl_interface = tmp;
2869 }
2870 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
2871
2872 if (eloop_register_read_sock(hapd->ctrl_sock,
2873 hostapd_ctrl_iface_receive, hapd, NULL) <
2874 0) {
2875 hostapd_ctrl_iface_deinit(hapd);
2876 return -1;
2877 }
2878
2879 hapd->msg_ctx = hapd;
2880 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2881
2882 return 0;
2883
2884fail:
2885 if (hapd->ctrl_sock >= 0)
2886 close(hapd->ctrl_sock);
2887 return -1;
2888#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 struct sockaddr_un addr;
2890 int s = -1;
2891 char *fname = NULL;
2892
Dmitry Shmidt04949592012-07-19 12:16:46 -07002893 if (hapd->ctrl_sock > -1) {
2894 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2895 return 0;
2896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002898 dl_list_init(&hapd->ctrl_dst);
2899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 if (hapd->conf->ctrl_interface == NULL)
2901 return 0;
2902
2903 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2904 if (errno == EEXIST) {
2905 wpa_printf(MSG_DEBUG, "Using existing control "
2906 "interface directory.");
2907 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002908 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2909 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 goto fail;
2911 }
2912 }
2913
2914 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002915 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916 hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2918 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919 return -1;
2920 }
2921
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002922 if (!hapd->conf->ctrl_interface_gid_set &&
2923 hapd->iface->interfaces->ctrl_iface_group &&
2924 chown(hapd->conf->ctrl_interface, -1,
2925 hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002926 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2927 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002928 return -1;
2929 }
2930
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002931#ifdef ANDROID
2932 /*
2933 * Android is using umask 0077 which would leave the control interface
2934 * directory without group access. This breaks things since Wi-Fi
2935 * framework assumes that this directory can be accessed by other
2936 * applications in the wifi group. Fix this by adding group access even
2937 * if umask value would prevent this.
2938 */
2939 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2940 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2941 strerror(errno));
2942 /* Try to continue anyway */
2943 }
2944#endif /* ANDROID */
2945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2947 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2948 goto fail;
2949
2950 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2951 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002952 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 goto fail;
2954 }
2955
2956 os_memset(&addr, 0, sizeof(addr));
2957#ifdef __FreeBSD__
2958 addr.sun_len = sizeof(addr);
2959#endif /* __FreeBSD__ */
2960 addr.sun_family = AF_UNIX;
2961 fname = hostapd_ctrl_iface_path(hapd);
2962 if (fname == NULL)
2963 goto fail;
2964 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2965 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2966 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2967 strerror(errno));
2968 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2969 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2970 " allow connections - assuming it was left"
2971 "over from forced program termination");
2972 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002973 wpa_printf(MSG_ERROR,
2974 "Could not unlink existing ctrl_iface socket '%s': %s",
2975 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 goto fail;
2977 }
2978 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2979 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002980 wpa_printf(MSG_ERROR,
2981 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2982 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002983 goto fail;
2984 }
2985 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2986 "ctrl_iface socket '%s'", fname);
2987 } else {
2988 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2989 "be in use - cannot override it");
2990 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2991 "not used anymore", fname);
2992 os_free(fname);
2993 fname = NULL;
2994 goto fail;
2995 }
2996 }
2997
2998 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002999 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003000 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3001 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 goto fail;
3003 }
3004
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003005 if (!hapd->conf->ctrl_interface_gid_set &&
3006 hapd->iface->interfaces->ctrl_iface_group &&
3007 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003008 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3009 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003010 goto fail;
3011 }
3012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003014 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3015 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 goto fail;
3017 }
3018 os_free(fname);
3019
3020 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003021 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3022 NULL) < 0) {
3023 hostapd_ctrl_iface_deinit(hapd);
3024 return -1;
3025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 hapd->msg_ctx = hapd;
3027 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3028
3029 return 0;
3030
3031fail:
3032 if (s >= 0)
3033 close(s);
3034 if (fname) {
3035 unlink(fname);
3036 os_free(fname);
3037 }
3038 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003039#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040}
3041
3042
3043void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3044{
3045 struct wpa_ctrl_dst *dst, *prev;
3046
3047 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003048#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003050#endif /* !CONFIG_CTRL_IFACE_UDP */
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 eloop_unregister_read_sock(hapd->ctrl_sock);
3053 close(hapd->ctrl_sock);
3054 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003055#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 fname = hostapd_ctrl_iface_path(hapd);
3057 if (fname)
3058 unlink(fname);
3059 os_free(fname);
3060
3061 if (hapd->conf->ctrl_interface &&
3062 rmdir(hapd->conf->ctrl_interface) < 0) {
3063 if (errno == ENOTEMPTY) {
3064 wpa_printf(MSG_DEBUG, "Control interface "
3065 "directory not empty - leaving it "
3066 "behind");
3067 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003068 wpa_printf(MSG_ERROR,
3069 "rmdir[ctrl_interface=%s]: %s",
3070 hapd->conf->ctrl_interface,
3071 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 }
3073 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003074#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 }
3076
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003077 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3078 list)
3079 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003080
3081#ifdef CONFIG_TESTING_OPTIONS
3082 l2_packet_deinit(hapd->l2_test);
3083 hapd->l2_test = NULL;
3084#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085}
3086
3087
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003088static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3089 char *buf)
3090{
3091 if (hostapd_add_iface(interfaces, buf) < 0) {
3092 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3093 return -1;
3094 }
3095 return 0;
3096}
3097
3098
3099static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3100 char *buf)
3101{
3102 if (hostapd_remove_iface(interfaces, buf) < 0) {
3103 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3104 return -1;
3105 }
3106 return 0;
3107}
3108
3109
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003110static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003111 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003112 socklen_t fromlen)
3113{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003114 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003115}
3116
3117
3118static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003119 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003120 socklen_t fromlen)
3121{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003122 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003123}
3124
3125
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003126static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3127{
3128#ifdef CONFIG_WPS_TESTING
3129 wps_version_number = 0x20;
3130 wps_testing_dummy_cred = 0;
3131 wps_corrupt_pkhash = 0;
3132#endif /* CONFIG_WPS_TESTING */
3133}
3134
3135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003136#ifdef CONFIG_FST
3137
3138static int
3139hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3140 const char *cmd)
3141{
3142 char ifname[IFNAMSIZ + 1];
3143 struct fst_iface_cfg cfg;
3144 struct hostapd_data *hapd;
3145 struct fst_wpa_obj iface_obj;
3146
3147 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3148 hapd = hostapd_get_iface(interfaces, ifname);
3149 if (hapd) {
3150 if (hapd->iface->fst) {
3151 wpa_printf(MSG_INFO, "FST: Already attached");
3152 return -1;
3153 }
3154 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3155 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3156 &iface_obj, &cfg);
3157 if (hapd->iface->fst)
3158 return 0;
3159 }
3160 }
3161
3162 return -EINVAL;
3163}
3164
3165
3166static int
3167hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3168 const char *cmd)
3169{
3170 char ifname[IFNAMSIZ + 1];
3171 struct hostapd_data * hapd;
3172
3173 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3174 hapd = hostapd_get_iface(interfaces, ifname);
3175 if (hapd) {
3176 if (!fst_iface_detach(ifname)) {
3177 hapd->iface->fst = NULL;
3178 hapd->iface->fst_ies = NULL;
3179 return 0;
3180 }
3181 }
3182 }
3183
3184 return -EINVAL;
3185}
3186
3187#endif /* CONFIG_FST */
3188
3189
3190static struct hostapd_data *
3191hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3192 const char *ifname)
3193{
3194 size_t i, j;
3195
3196 for (i = 0; i < interfaces->count; i++) {
3197 struct hostapd_iface *iface = interfaces->iface[i];
3198
3199 for (j = 0; j < iface->num_bss; j++) {
3200 struct hostapd_data *hapd;
3201
3202 hapd = iface->bss[j];
3203 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3204 return hapd;
3205 }
3206 }
3207
3208 return NULL;
3209}
3210
3211
3212static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3213 struct hostapd_data *dst_hapd,
3214 const char *param)
3215{
3216 int res;
3217 char *value;
3218
3219 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3220 if (!value) {
3221 wpa_printf(MSG_ERROR,
3222 "DUP: cannot allocate buffer to stringify %s",
3223 param);
3224 goto error_return;
3225 }
3226
3227 if (os_strcmp(param, "wpa") == 0) {
3228 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3229 src_hapd->conf->wpa);
3230 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3231 src_hapd->conf->wpa_key_mgmt) {
3232 res = hostapd_ctrl_iface_get_key_mgmt(
3233 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3234 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3235 goto error_stringify;
3236 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3237 src_hapd->conf->wpa_pairwise) {
3238 res = wpa_write_ciphers(value,
3239 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3240 src_hapd->conf->wpa_pairwise, " ");
3241 if (res < 0)
3242 goto error_stringify;
3243 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3244 src_hapd->conf->rsn_pairwise) {
3245 res = wpa_write_ciphers(value,
3246 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3247 src_hapd->conf->rsn_pairwise, " ");
3248 if (res < 0)
3249 goto error_stringify;
3250 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3251 src_hapd->conf->ssid.wpa_passphrase) {
3252 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3253 src_hapd->conf->ssid.wpa_passphrase);
3254 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3255 src_hapd->conf->ssid.wpa_psk_set) {
3256 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3257 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3258 } else {
3259 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3260 goto error_return;
3261 }
3262
3263 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3264 os_free(value);
3265 return res;
3266
3267error_stringify:
3268 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3269error_return:
3270 os_free(value);
3271 return -1;
3272}
3273
3274
3275static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003276hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3277 const char *input,
3278 char *reply, int reply_size)
3279{
3280 size_t i, j;
3281 int res;
3282 char *pos, *end;
3283 struct hostapd_iface *iface;
3284 int show_ctrl = 0;
3285
3286 if (input)
3287 show_ctrl = !!os_strstr(input, "ctrl");
3288
3289 pos = reply;
3290 end = reply + reply_size;
3291
3292 for (i = 0; i < interfaces->count; i++) {
3293 iface = interfaces->iface[i];
3294
3295 for (j = 0; j < iface->num_bss; j++) {
3296 struct hostapd_bss_config *conf;
3297
3298 conf = iface->conf->bss[j];
3299 if (show_ctrl)
3300 res = os_snprintf(pos, end - pos,
3301 "%s ctrl_iface=%s\n",
3302 conf->iface,
3303 conf->ctrl_interface ?
3304 conf->ctrl_interface : "N/A");
3305 else
3306 res = os_snprintf(pos, end - pos, "%s\n",
3307 conf->iface);
3308 if (os_snprintf_error(end - pos, res)) {
3309 *pos = '\0';
3310 return pos - reply;
3311 }
3312 pos += res;
3313 }
3314 }
3315
3316 return pos - reply;
3317}
3318
3319
3320static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003321hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3322 char *cmd)
3323{
3324 char *p_start = cmd, *p_end;
3325 struct hostapd_data *src_hapd, *dst_hapd;
3326
3327 /* cmd: "<src ifname> <dst ifname> <variable name> */
3328
3329 p_end = os_strchr(p_start, ' ');
3330 if (!p_end) {
3331 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3332 cmd);
3333 return -1;
3334 }
3335
3336 *p_end = '\0';
3337 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3338 if (!src_hapd) {
3339 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3340 p_start);
3341 return -1;
3342 }
3343
3344 p_start = p_end + 1;
3345 p_end = os_strchr(p_start, ' ');
3346 if (!p_end) {
3347 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3348 cmd);
3349 return -1;
3350 }
3351
3352 *p_end = '\0';
3353 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3354 if (!dst_hapd) {
3355 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3356 p_start);
3357 return -1;
3358 }
3359
3360 p_start = p_end + 1;
3361 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3362}
3363
3364
3365static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3366 const char *ifname,
3367 char *buf, char *reply,
3368 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003369 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003370 socklen_t fromlen)
3371{
3372 struct hostapd_data *hapd;
3373
3374 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3375 if (hapd == NULL) {
3376 int res;
3377
3378 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3379 if (os_snprintf_error(reply_size, res))
3380 return -1;
3381 return res;
3382 }
3383
3384 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3385 from, fromlen);
3386}
3387
3388
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003389static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3390 void *sock_ctx)
3391{
3392 void *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003393 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003394 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003395 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003396 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003397 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003398 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003399 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003400#ifdef CONFIG_CTRL_IFACE_UDP
3401 unsigned char lcookie[COOKIE_LEN];
3402#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003403
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003404 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003405 (struct sockaddr *) &from, &fromlen);
3406 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003407 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3408 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003409 return;
3410 }
3411 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003412 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003413
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003414 reply = os_malloc(reply_size);
3415 if (reply == NULL) {
3416 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3417 fromlen) < 0) {
3418 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3419 strerror(errno));
3420 }
3421 return;
3422 }
3423
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003424 os_memcpy(reply, "OK\n", 3);
3425 reply_len = 3;
3426
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003427#ifdef CONFIG_CTRL_IFACE_UDP
3428 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3429 os_memcpy(reply, "COOKIE=", 7);
3430 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3431 gcookie, COOKIE_LEN);
3432 reply_len = 7 + 2 * COOKIE_LEN;
3433 goto send_reply;
3434 }
3435
3436 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3437 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3438 wpa_printf(MSG_DEBUG,
3439 "CTRL: No cookie in the request - drop request");
3440 os_free(reply);
3441 return;
3442 }
3443
3444 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3445 wpa_printf(MSG_DEBUG,
3446 "CTRL: Invalid cookie in the request - drop request");
3447 os_free(reply);
3448 return;
3449 }
3450
3451 buf += 7 + 2 * COOKIE_LEN;
3452 while (*buf == ' ')
3453 buf++;
3454#endif /* CONFIG_CTRL_IFACE_UDP */
3455
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3457 char *pos = os_strchr(buf + 7, ' ');
3458
3459 if (pos) {
3460 *pos++ = '\0';
3461 reply_len = hostapd_global_ctrl_iface_ifname(
3462 interfaces, buf + 7, pos, reply, reply_size,
3463 &from, fromlen);
3464 goto send_reply;
3465 }
3466 }
3467
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003468 if (os_strcmp(buf, "PING") == 0) {
3469 os_memcpy(reply, "PONG\n", 5);
3470 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003471 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3472 if (wpa_debug_reopen_file() < 0)
3473 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003474 } else if (os_strcmp(buf, "FLUSH") == 0) {
3475 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003476 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3477 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3478 reply_len = -1;
3479 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3480 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3481 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003482 } else if (os_strcmp(buf, "ATTACH") == 0) {
3483 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3484 fromlen))
3485 reply_len = -1;
3486 } else if (os_strcmp(buf, "DETACH") == 0) {
3487 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3488 fromlen))
3489 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08003490#ifdef CONFIG_MODULE_TESTS
3491 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08003492 if (hapd_module_tests() < 0)
3493 reply_len = -1;
3494#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003495#ifdef CONFIG_FST
3496 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3497 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3498 reply_len = os_snprintf(reply, reply_size, "OK\n");
3499 else
3500 reply_len = -1;
3501 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3502 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3503 reply_len = os_snprintf(reply, reply_size, "OK\n");
3504 else
3505 reply_len = -1;
3506 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3507 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3508#endif /* CONFIG_FST */
3509 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3510 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3511 buf + 12))
3512 reply_len = os_snprintf(reply, reply_size, "OK\n");
3513 else
3514 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003515 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3516 reply_len = hostapd_global_ctrl_iface_interfaces(
3517 interfaces, buf + 10, reply, sizeof(buffer));
3518 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3519 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003520 } else {
3521 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3522 "ignored");
3523 reply_len = -1;
3524 }
3525
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003526send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003527 if (reply_len < 0) {
3528 os_memcpy(reply, "FAIL\n", 5);
3529 reply_len = 5;
3530 }
3531
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003532 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3533 fromlen) < 0) {
3534 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3535 strerror(errno));
3536 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003537 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003538}
3539
3540
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003541#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003542static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3543{
3544 char *buf;
3545 size_t len;
3546
3547 if (interface->global_iface_path == NULL)
3548 return NULL;
3549
3550 len = os_strlen(interface->global_iface_path) +
3551 os_strlen(interface->global_iface_name) + 2;
3552 buf = os_malloc(len);
3553 if (buf == NULL)
3554 return NULL;
3555
3556 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3557 interface->global_iface_name);
3558 buf[len - 1] = '\0';
3559 return buf;
3560}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003561#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003562
3563
3564int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3565{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003566#ifdef CONFIG_CTRL_IFACE_UDP
3567 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3568 char p[32] = { 0 };
3569 char *pos;
3570 struct addrinfo hints = { 0 }, *res, *saveres;
3571 int n;
3572
3573 if (interface->global_ctrl_sock > -1) {
3574 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3575 return 0;
3576 }
3577
3578 if (interface->global_iface_path == NULL)
3579 return 0;
3580
3581 pos = os_strstr(interface->global_iface_path, "udp:");
3582 if (pos) {
3583 pos += 4;
3584 port = atoi(pos);
3585 if (port <= 0) {
3586 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3587 goto fail;
3588 }
3589 }
3590
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003591 os_get_random(gcookie, COOKIE_LEN);
3592
3593#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3594 hints.ai_flags = AI_PASSIVE;
3595#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3596
3597#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3598 hints.ai_family = AF_INET6;
3599#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3600 hints.ai_family = AF_INET;
3601#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3602 hints.ai_socktype = SOCK_DGRAM;
3603
3604try_again:
3605 os_snprintf(p, sizeof(p), "%d", port);
3606 n = getaddrinfo(NULL, p, &hints, &res);
3607 if (n) {
3608 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3609 goto fail;
3610 }
3611
3612 saveres = res;
3613 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3614 res->ai_protocol);
3615 if (interface->global_ctrl_sock < 0) {
3616 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3617 goto fail;
3618 }
3619
3620 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3621 0) {
3622 port++;
3623 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3624 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3625 goto try_again;
3626 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3627 goto fail;
3628 }
3629
3630 freeaddrinfo(saveres);
3631
3632 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3633
3634 if (eloop_register_read_sock(interface->global_ctrl_sock,
3635 hostapd_global_ctrl_iface_receive,
3636 interface, NULL) < 0) {
3637 hostapd_global_ctrl_iface_deinit(interface);
3638 return -1;
3639 }
3640
3641 return 0;
3642
3643fail:
3644 if (interface->global_ctrl_sock >= 0)
3645 close(interface->global_ctrl_sock);
3646 return -1;
3647#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003648 struct sockaddr_un addr;
3649 int s = -1;
3650 char *fname = NULL;
3651
3652 if (interface->global_iface_path == NULL) {
3653 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3654 return 0;
3655 }
3656
3657 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3658 if (errno == EEXIST) {
3659 wpa_printf(MSG_DEBUG, "Using existing control "
3660 "interface directory.");
3661 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003662 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3663 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003664 goto fail;
3665 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003666 } else if (interface->ctrl_iface_group &&
3667 chown(interface->global_iface_path, -1,
3668 interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003669 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3670 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003671 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003672 }
3673
3674 if (os_strlen(interface->global_iface_path) + 1 +
3675 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3676 goto fail;
3677
3678 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3679 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003680 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003681 goto fail;
3682 }
3683
3684 os_memset(&addr, 0, sizeof(addr));
3685#ifdef __FreeBSD__
3686 addr.sun_len = sizeof(addr);
3687#endif /* __FreeBSD__ */
3688 addr.sun_family = AF_UNIX;
3689 fname = hostapd_global_ctrl_iface_path(interface);
3690 if (fname == NULL)
3691 goto fail;
3692 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3693 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3694 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3695 strerror(errno));
3696 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3697 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3698 " allow connections - assuming it was left"
3699 "over from forced program termination");
3700 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003701 wpa_printf(MSG_ERROR,
3702 "Could not unlink existing ctrl_iface socket '%s': %s",
3703 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003704 goto fail;
3705 }
3706 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3707 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3709 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003710 goto fail;
3711 }
3712 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3713 "ctrl_iface socket '%s'", fname);
3714 } else {
3715 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3716 "be in use - cannot override it");
3717 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3718 "not used anymore", fname);
3719 os_free(fname);
3720 fname = NULL;
3721 goto fail;
3722 }
3723 }
3724
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003725 if (interface->ctrl_iface_group &&
3726 chown(fname, -1, interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003727 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3728 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003729 goto fail;
3730 }
3731
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003732 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003733 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3734 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003735 goto fail;
3736 }
3737 os_free(fname);
3738
3739 interface->global_ctrl_sock = s;
3740 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3741 interface, NULL);
3742
3743 return 0;
3744
3745fail:
3746 if (s >= 0)
3747 close(s);
3748 if (fname) {
3749 unlink(fname);
3750 os_free(fname);
3751 }
3752 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003753#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003754}
3755
3756
3757void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3758{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003759#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003760 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003761#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003762 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003763
3764 if (interfaces->global_ctrl_sock > -1) {
3765 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3766 close(interfaces->global_ctrl_sock);
3767 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003768#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003769 fname = hostapd_global_ctrl_iface_path(interfaces);
3770 if (fname) {
3771 unlink(fname);
3772 os_free(fname);
3773 }
3774
3775 if (interfaces->global_iface_path &&
3776 rmdir(interfaces->global_iface_path) < 0) {
3777 if (errno == ENOTEMPTY) {
3778 wpa_printf(MSG_DEBUG, "Control interface "
3779 "directory not empty - leaving it "
3780 "behind");
3781 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003782 wpa_printf(MSG_ERROR,
3783 "rmdir[ctrl_interface=%s]: %s",
3784 interfaces->global_iface_path,
3785 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003786 }
3787 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003788#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003789 }
3790
3791 os_free(interfaces->global_iface_path);
3792 interfaces->global_iface_path = NULL;
3793
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003794 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
3795 struct wpa_ctrl_dst, list)
3796 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003797}
3798
3799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003801 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 const char *buf, size_t len)
3803{
3804 struct wpa_ctrl_dst *dst, *next;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003805 struct dl_list *ctrl_dst;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 struct msghdr msg;
3807 int idx;
3808 struct iovec io[2];
3809 char levelstr[10];
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003810 int s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003812 if (type != WPA_MSG_ONLY_GLOBAL) {
3813 s = hapd->ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003814 ctrl_dst = &hapd->ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003815 } else {
3816 s = hapd->iface->interfaces->global_ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003817 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003818 }
3819
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003820 if (s < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 return;
3822
3823 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3824 io[0].iov_base = levelstr;
3825 io[0].iov_len = os_strlen(levelstr);
3826 io[1].iov_base = (char *) buf;
3827 io[1].iov_len = len;
3828 os_memset(&msg, 0, sizeof(msg));
3829 msg.msg_iov = io;
3830 msg.msg_iovlen = 2;
3831
3832 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003833 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 if (level >= dst->debug_level) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003835 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
3836 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 msg.msg_name = &dst->addr;
3838 msg.msg_namelen = dst->addrlen;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003839 if (sendmsg(s, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003840 int _errno = errno;
3841 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3842 "%d - %s",
3843 idx, errno, strerror(errno));
3844 dst->errors++;
3845 if (dst->errors > 10 || _errno == ENOENT) {
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003846 if (type != WPA_MSG_ONLY_GLOBAL)
3847 hostapd_ctrl_iface_detach(
3848 hapd, &dst->addr,
3849 dst->addrlen);
3850 else
3851 hostapd_global_ctrl_iface_detach(
3852 hapd->iface->interfaces,
3853 &dst->addr,
3854 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 }
3856 } else
3857 dst->errors = 0;
3858 }
3859 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 }
3861}
3862
3863#endif /* CONFIG_NATIVE_WINDOWS */