blob: 6b4aa054203850088e7307d087677a55114a6e40 [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>");
Dmitry Shmidt29333592017-01-09 12:27:11 -08001020 ret = -1;
1021 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001022 }
1023
1024 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1025 wpa_printf(MSG_DEBUG,
1026 "Invalid MBO transition reason code %u",
1027 mbo_reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001028 ret = -1;
1029 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001030 }
1031
1032 /* Valid values for Cellular preference are: 0, 1, 255 */
1033 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1034 wpa_printf(MSG_DEBUG,
1035 "Invalid MBO cellular capability %u",
1036 cell_pref);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001037 ret = -1;
1038 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001039 }
1040
1041 if (reassoc_delay > 65535 ||
1042 (reassoc_delay &&
1043 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1044 wpa_printf(MSG_DEBUG,
1045 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
Dmitry Shmidt29333592017-01-09 12:27:11 -08001046 ret = -1;
1047 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001048 }
1049
1050 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1051 *mbo_pos++ = 1;
1052 *mbo_pos++ = mbo_reason;
1053 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1054 *mbo_pos++ = 1;
1055 *mbo_pos++ = cell_pref;
1056
1057 if (reassoc_delay) {
1058 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1059 *mbo_pos++ = 2;
1060 WPA_PUT_LE16(mbo_pos, reassoc_delay);
1061 mbo_pos += 2;
1062 }
1063
1064 mbo_len = mbo_pos - mbo;
1065 }
1066#endif /* CONFIG_MBO */
1067
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001068 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1069 valid_int, bss_term_dur, url,
1070 nei_pos > nei_rep ? nei_rep : NULL,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001071 nei_pos - nei_rep, mbo_len ? mbo : NULL,
1072 mbo_len);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001073fail:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001074 os_free(url);
1075 return ret;
1076}
1077
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001078#endif /* CONFIG_WNM */
1079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001081static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1082 char *buf, size_t buflen)
1083{
1084 int ret = 0;
1085 char *pos, *end;
1086
1087 pos = buf;
1088 end = buf + buflen;
1089
1090 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1091
1092 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1093 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1094 if (os_snprintf_error(end - pos, ret))
1095 return pos - buf;
1096 pos += ret;
1097 }
1098 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1099 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1100 if (os_snprintf_error(end - pos, ret))
1101 return pos - buf;
1102 pos += ret;
1103 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001104#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001105 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1106 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1107 if (os_snprintf_error(end - pos, ret))
1108 return pos - buf;
1109 pos += ret;
1110 }
1111 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1112 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1113 if (os_snprintf_error(end - pos, ret))
1114 return pos - buf;
1115 pos += ret;
1116 }
1117#ifdef CONFIG_SAE
1118 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1119 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1120 if (os_snprintf_error(end - pos, ret))
1121 return pos - buf;
1122 pos += ret;
1123 }
1124#endif /* CONFIG_SAE */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001125#ifdef CONFIG_FILS
1126 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1127 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1128 if (os_snprintf_error(end - pos, ret))
1129 return pos - buf;
1130 pos += ret;
1131 }
1132 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1133 ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1134 if (os_snprintf_error(end - pos, ret))
1135 return pos - buf;
1136 pos += ret;
1137 }
1138#endif /* CONFIG_FILS */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001139#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001140#ifdef CONFIG_IEEE80211W
1141 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1142 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1143 if (os_snprintf_error(end - pos, ret))
1144 return pos - buf;
1145 pos += ret;
1146 }
1147 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1148 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1149 if (os_snprintf_error(end - pos, ret))
1150 return pos - buf;
1151 pos += ret;
1152 }
1153#endif /* CONFIG_IEEE80211W */
1154#ifdef CONFIG_SAE
1155 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1156 ret = os_snprintf(pos, end - pos, "SAE ");
1157 if (os_snprintf_error(end - pos, ret))
1158 return pos - buf;
1159 pos += ret;
1160 }
1161#endif /* CONFIG_SAE */
1162 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1163 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1164 if (os_snprintf_error(end - pos, ret))
1165 return pos - buf;
1166 pos += ret;
1167 }
1168 if (hapd->conf->wpa_key_mgmt &
1169 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1170 ret = os_snprintf(pos, end - pos,
1171 "WPA-EAP-SUITE-B-192 ");
1172 if (os_snprintf_error(end - pos, ret))
1173 return pos - buf;
1174 pos += ret;
1175 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001176#ifdef CONFIG_FILS
1177 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1178 ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1179 if (os_snprintf_error(end - pos, ret))
1180 return pos - buf;
1181 pos += ret;
1182 }
1183 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1184 ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1185 if (os_snprintf_error(end - pos, ret))
1186 return pos - buf;
1187 pos += ret;
1188 }
1189#endif /* CONFIG_FILS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001190
1191 if (pos > buf && *(pos - 1) == ' ') {
1192 *(pos - 1) = '\0';
1193 pos--;
1194 }
1195
1196 return pos - buf;
1197}
1198
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1201 char *buf, size_t buflen)
1202{
1203 int ret;
1204 char *pos, *end;
1205
1206 pos = buf;
1207 end = buf + buflen;
1208
1209 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1210 "ssid=%s\n",
1211 MAC2STR(hapd->own_addr),
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001212 wpa_ssid_txt(hapd->conf->ssid.ssid,
1213 hapd->conf->ssid.ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001214 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 return pos - buf;
1216 pos += ret;
1217
1218#ifdef CONFIG_WPS
1219 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1220 hapd->conf->wps_state == 0 ? "disabled" :
1221 (hapd->conf->wps_state == 1 ? "not configured" :
1222 "configured"));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001223 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 return pos - buf;
1225 pos += ret;
1226
1227 if (hapd->conf->wps_state && hapd->conf->wpa &&
1228 hapd->conf->ssid.wpa_passphrase) {
1229 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1230 hapd->conf->ssid.wpa_passphrase);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001231 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 return pos - buf;
1233 pos += ret;
1234 }
1235
1236 if (hapd->conf->wps_state && hapd->conf->wpa &&
1237 hapd->conf->ssid.wpa_psk &&
1238 hapd->conf->ssid.wpa_psk->group) {
1239 char hex[PMK_LEN * 2 + 1];
1240 wpa_snprintf_hex(hex, sizeof(hex),
1241 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1242 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001243 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 return pos - buf;
1245 pos += ret;
1246 }
1247#endif /* CONFIG_WPS */
1248
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001249 if (hapd->conf->wpa) {
1250 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1251 if (os_snprintf_error(end - pos, ret))
1252 return pos - buf;
1253 pos += ret;
1254 }
1255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1257 ret = os_snprintf(pos, end - pos, "key_mgmt=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001258 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 return pos - buf;
1260 pos += ret;
1261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001262 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263
1264 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001265 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 return pos - buf;
1267 pos += ret;
1268 }
1269
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001270 if (hapd->conf->wpa) {
1271 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1272 wpa_cipher_txt(hapd->conf->wpa_group));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001273 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 return pos - buf;
1275 pos += ret;
1276 }
1277
1278 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1279 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001280 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 return pos - buf;
1282 pos += ret;
1283
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001284 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1285 " ");
1286 if (ret < 0)
1287 return pos - buf;
1288 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289
1290 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001291 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 return pos - buf;
1293 pos += ret;
1294 }
1295
1296 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1297 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001298 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 return pos - buf;
1300 pos += ret;
1301
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001302 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001303 " ");
1304 if (ret < 0)
1305 return pos - buf;
1306 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307
1308 ret = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001309 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return pos - buf;
1311 pos += ret;
1312 }
1313
1314 return pos - buf;
1315}
1316
1317
1318static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1319{
1320 char *value;
1321 int ret = 0;
1322
1323 value = os_strchr(cmd, ' ');
1324 if (value == NULL)
1325 return -1;
1326 *value++ = '\0';
1327
1328 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1329 if (0) {
1330#ifdef CONFIG_WPS_TESTING
1331 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1332 long int val;
1333 val = strtol(value, NULL, 0);
1334 if (val < 0 || val > 0xff) {
1335 ret = -1;
1336 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1337 "wps_version_number %ld", val);
1338 } else {
1339 wps_version_number = val;
1340 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1341 "version %u.%u",
1342 (wps_version_number & 0xf0) >> 4,
1343 wps_version_number & 0x0f);
1344 hostapd_wps_update_ie(hapd);
1345 }
1346 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1347 wps_testing_dummy_cred = atoi(value);
1348 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1349 wps_testing_dummy_cred);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001350 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1351 wps_corrupt_pkhash = atoi(value);
1352 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1353 wps_corrupt_pkhash);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354#endif /* CONFIG_WPS_TESTING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001355#ifdef CONFIG_TESTING_OPTIONS
1356 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1357 hapd->ext_mgmt_frame_handling = atoi(value);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001358 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1359 hapd->ext_eapol_frame_io = atoi(value);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001360#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001361#ifdef CONFIG_MBO
1362 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1363 int val;
1364
1365 if (!hapd->conf->mbo_enabled)
1366 return -1;
1367
1368 val = atoi(value);
1369 if (val < 0 || val > 1)
1370 return -1;
1371
1372 hapd->mbo_assoc_disallow = val;
1373 ieee802_11_update_beacons(hapd->iface);
1374
1375 /*
1376 * TODO: Need to configure drivers that do AP MLME offload with
1377 * disallowing station logic.
1378 */
1379#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001381 struct sta_info *sta;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001382 struct vlan_description vlan_id;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001383
Dmitry Shmidt04949592012-07-19 12:16:46 -07001384 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001385 if (ret)
1386 return ret;
1387
1388 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1389 for (sta = hapd->sta_list; sta; sta = sta->next) {
1390 if (hostapd_maclist_found(
1391 hapd->conf->deny_mac,
1392 hapd->conf->num_deny_mac, sta->addr,
1393 &vlan_id) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001394 (!vlan_id.notempty ||
1395 !vlan_compare(&vlan_id, sta->vlan_desc)))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001396 ap_sta_disconnect(
1397 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001398 WLAN_REASON_UNSPECIFIED);
1399 }
1400 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1401 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1402 for (sta = hapd->sta_list; sta; sta = sta->next) {
1403 if (!hostapd_maclist_found(
1404 hapd->conf->accept_mac,
1405 hapd->conf->num_accept_mac,
1406 sta->addr, &vlan_id) ||
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001407 (vlan_id.notempty &&
1408 vlan_compare(&vlan_id, sta->vlan_desc)))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001409 ap_sta_disconnect(
1410 hapd, sta, sta->addr,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001411 WLAN_REASON_UNSPECIFIED);
1412 }
1413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 }
1415
1416 return ret;
1417}
1418
1419
1420static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1421 char *buf, size_t buflen)
1422{
1423 int res;
1424
1425 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1426
1427 if (os_strcmp(cmd, "version") == 0) {
1428 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001429 if (os_snprintf_error(buflen, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 return -1;
1431 return res;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001432 } else if (os_strcmp(cmd, "tls_library") == 0) {
1433 res = tls_get_library_version(buf, buflen);
1434 if (os_snprintf_error(buflen, res))
1435 return -1;
1436 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 }
1438
1439 return -1;
1440}
1441
1442
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001443static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1444{
1445 if (hostapd_enable_iface(iface) < 0) {
1446 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1447 return -1;
1448 }
1449 return 0;
1450}
1451
1452
1453static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1454{
1455 if (hostapd_reload_iface(iface) < 0) {
1456 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1457 return -1;
1458 }
1459 return 0;
1460}
1461
1462
1463static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1464{
1465 if (hostapd_disable_iface(iface) < 0) {
1466 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1467 return -1;
1468 }
1469 return 0;
1470}
1471
1472
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001473#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001474
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001475static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1476{
1477 union wpa_event_data data;
1478 char *pos, *param;
1479 enum wpa_event_type event;
1480
1481 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1482
1483 os_memset(&data, 0, sizeof(data));
1484
1485 param = os_strchr(cmd, ' ');
1486 if (param == NULL)
1487 return -1;
1488 *param++ = '\0';
1489
1490 if (os_strcmp(cmd, "DETECTED") == 0)
1491 event = EVENT_DFS_RADAR_DETECTED;
1492 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1493 event = EVENT_DFS_CAC_FINISHED;
1494 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1495 event = EVENT_DFS_CAC_ABORTED;
1496 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1497 event = EVENT_DFS_NOP_FINISHED;
1498 else {
1499 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1500 cmd);
1501 return -1;
1502 }
1503
1504 pos = os_strstr(param, "freq=");
1505 if (pos)
1506 data.dfs_event.freq = atoi(pos + 5);
1507
1508 pos = os_strstr(param, "ht_enabled=1");
1509 if (pos)
1510 data.dfs_event.ht_enabled = 1;
1511
1512 pos = os_strstr(param, "chan_offset=");
1513 if (pos)
1514 data.dfs_event.chan_offset = atoi(pos + 12);
1515
1516 pos = os_strstr(param, "chan_width=");
1517 if (pos)
1518 data.dfs_event.chan_width = atoi(pos + 11);
1519
1520 pos = os_strstr(param, "cf1=");
1521 if (pos)
1522 data.dfs_event.cf1 = atoi(pos + 4);
1523
1524 pos = os_strstr(param, "cf2=");
1525 if (pos)
1526 data.dfs_event.cf2 = atoi(pos + 4);
1527
1528 wpa_supplicant_event(hapd, event, &data);
1529
1530 return 0;
1531}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001532
1533
1534static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1535{
1536 size_t len;
1537 u8 *buf;
1538 int res;
1539
1540 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1541
1542 len = os_strlen(cmd);
1543 if (len & 1)
1544 return -1;
1545 len /= 2;
1546
1547 buf = os_malloc(len);
1548 if (buf == NULL)
1549 return -1;
1550
1551 if (hexstr2bin(cmd, buf, len) < 0) {
1552 os_free(buf);
1553 return -1;
1554 }
1555
1556 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1557 os_free(buf);
1558 return res;
1559}
1560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001561
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001562static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1563 char *cmd)
1564{
1565 char *pos, *param;
1566 size_t len;
1567 u8 *buf;
1568 int freq = 0, datarate = 0, ssi_signal = 0;
1569 union wpa_event_data event;
1570
1571 if (!hapd->ext_mgmt_frame_handling)
1572 return -1;
1573
1574 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1575
1576 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1577
1578 pos = cmd;
1579 param = os_strstr(pos, "freq=");
1580 if (param) {
1581 param += 5;
1582 freq = atoi(param);
1583 }
1584
1585 param = os_strstr(pos, " datarate=");
1586 if (param) {
1587 param += 10;
1588 datarate = atoi(param);
1589 }
1590
1591 param = os_strstr(pos, " ssi_signal=");
1592 if (param) {
1593 param += 12;
1594 ssi_signal = atoi(param);
1595 }
1596
1597 param = os_strstr(pos, " frame=");
1598 if (param == NULL)
1599 return -1;
1600 param += 7;
1601
1602 len = os_strlen(param);
1603 if (len & 1)
1604 return -1;
1605 len /= 2;
1606
1607 buf = os_malloc(len);
1608 if (buf == NULL)
1609 return -1;
1610
1611 if (hexstr2bin(param, buf, len) < 0) {
1612 os_free(buf);
1613 return -1;
1614 }
1615
1616 os_memset(&event, 0, sizeof(event));
1617 event.rx_mgmt.freq = freq;
1618 event.rx_mgmt.frame = buf;
1619 event.rx_mgmt.frame_len = len;
1620 event.rx_mgmt.ssi_signal = ssi_signal;
1621 event.rx_mgmt.datarate = datarate;
1622 hapd->ext_mgmt_frame_handling = 0;
1623 wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1624 hapd->ext_mgmt_frame_handling = 1;
1625
1626 os_free(buf);
1627
1628 return 0;
1629}
1630
1631
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001632static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1633{
1634 char *pos;
1635 u8 src[ETH_ALEN], *buf;
1636 int used;
1637 size_t len;
1638
1639 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1640
1641 pos = cmd;
1642 used = hwaddr_aton2(pos, src);
1643 if (used < 0)
1644 return -1;
1645 pos += used;
1646 while (*pos == ' ')
1647 pos++;
1648
1649 len = os_strlen(pos);
1650 if (len & 1)
1651 return -1;
1652 len /= 2;
1653
1654 buf = os_malloc(len);
1655 if (buf == NULL)
1656 return -1;
1657
1658 if (hexstr2bin(pos, buf, len) < 0) {
1659 os_free(buf);
1660 return -1;
1661 }
1662
1663 ieee802_1x_receive(hapd, src, buf, len);
1664 os_free(buf);
1665
1666 return 0;
1667}
1668
1669
1670static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1671{
1672 size_t i;
1673 u32 sum = 0;
1674 const u16 *pos = buf;
1675
1676 for (i = 0; i < len / 2; i++)
1677 sum += *pos++;
1678
1679 while (sum >> 16)
1680 sum = (sum & 0xffff) + (sum >> 16);
1681
1682 return sum ^ 0xffff;
1683}
1684
1685
1686#define HWSIM_PACKETLEN 1500
1687#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1688
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001689static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1690 size_t len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001691{
1692 struct hostapd_data *hapd = ctx;
1693 const struct ether_header *eth;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001694 struct iphdr ip;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001695 const u8 *pos;
1696 unsigned int i;
1697
1698 if (len != HWSIM_PACKETLEN)
1699 return;
1700
1701 eth = (const struct ether_header *) buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001702 os_memcpy(&ip, eth + 1, sizeof(ip));
1703 pos = &buf[sizeof(*eth) + sizeof(ip)];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001704
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001705 if (ip.ihl != 5 || ip.version != 4 ||
1706 ntohs(ip.tot_len) != HWSIM_IP_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001707 return;
1708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001709 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001710 if (*pos != (u8) i)
1711 return;
1712 pos++;
1713 }
1714
1715 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1716 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1717}
1718
1719
1720static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1721 char *cmd)
1722{
1723 int enabled = atoi(cmd);
1724 char *pos;
1725 const char *ifname;
1726
1727 if (!enabled) {
1728 if (hapd->l2_test) {
1729 l2_packet_deinit(hapd->l2_test);
1730 hapd->l2_test = NULL;
1731 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1732 "test data: Disabled");
1733 }
1734 return 0;
1735 }
1736
1737 if (hapd->l2_test)
1738 return 0;
1739
1740 pos = os_strstr(cmd, " ifname=");
1741 if (pos)
1742 ifname = pos + 8;
1743 else
1744 ifname = hapd->conf->iface;
1745
1746 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1747 ETHERTYPE_IP, hostapd_data_test_rx,
1748 hapd, 1);
1749 if (hapd->l2_test == NULL)
1750 return -1;
1751
1752 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1753
1754 return 0;
1755}
1756
1757
1758static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1759{
1760 u8 dst[ETH_ALEN], src[ETH_ALEN];
1761 char *pos;
1762 int used;
1763 long int val;
1764 u8 tos;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001765 u8 buf[2 + HWSIM_PACKETLEN];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001766 struct ether_header *eth;
1767 struct iphdr *ip;
1768 u8 *dpos;
1769 unsigned int i;
1770
1771 if (hapd->l2_test == NULL)
1772 return -1;
1773
1774 /* format: <dst> <src> <tos> */
1775
1776 pos = cmd;
1777 used = hwaddr_aton2(pos, dst);
1778 if (used < 0)
1779 return -1;
1780 pos += used;
1781 while (*pos == ' ')
1782 pos++;
1783 used = hwaddr_aton2(pos, src);
1784 if (used < 0)
1785 return -1;
1786 pos += used;
1787
1788 val = strtol(pos, NULL, 0);
1789 if (val < 0 || val > 0xff)
1790 return -1;
1791 tos = val;
1792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001793 eth = (struct ether_header *) &buf[2];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001794 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1795 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1796 eth->ether_type = htons(ETHERTYPE_IP);
1797 ip = (struct iphdr *) (eth + 1);
1798 os_memset(ip, 0, sizeof(*ip));
1799 ip->ihl = 5;
1800 ip->version = 4;
1801 ip->ttl = 64;
1802 ip->tos = tos;
1803 ip->tot_len = htons(HWSIM_IP_LEN);
1804 ip->protocol = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001805 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1806 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001807 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1808 dpos = (u8 *) (ip + 1);
1809 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1810 *dpos++ = i;
1811
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001812 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001813 HWSIM_PACKETLEN) < 0)
1814 return -1;
1815
1816 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1817 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1818
1819 return 0;
1820}
1821
1822
1823static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1824 char *cmd)
1825{
1826 u8 *buf;
1827 struct ether_header *eth;
1828 struct l2_packet_data *l2 = NULL;
1829 size_t len;
1830 u16 ethertype;
1831 int res = -1;
1832 const char *ifname = hapd->conf->iface;
1833
1834 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1835 cmd += 7;
1836 ifname = cmd;
1837 cmd = os_strchr(cmd, ' ');
1838 if (cmd == NULL)
1839 return -1;
1840 *cmd++ = '\0';
1841 }
1842
1843 len = os_strlen(cmd);
1844 if (len & 1 || len < ETH_HLEN * 2)
1845 return -1;
1846 len /= 2;
1847
1848 buf = os_malloc(len);
1849 if (buf == NULL)
1850 return -1;
1851
1852 if (hexstr2bin(cmd, buf, len) < 0)
1853 goto done;
1854
1855 eth = (struct ether_header *) buf;
1856 ethertype = ntohs(eth->ether_type);
1857
1858 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1859 hostapd_data_test_rx, hapd, 1);
1860 if (l2 == NULL)
1861 goto done;
1862
1863 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1864 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1865done:
1866 if (l2)
1867 l2_packet_deinit(l2);
1868 os_free(buf);
1869
1870 return res < 0 ? -1 : 0;
1871}
1872
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001873
1874static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1875{
1876#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001877 char *pos;
1878
1879 wpa_trace_fail_after = atoi(cmd);
1880 pos = os_strchr(cmd, ':');
1881 if (pos) {
1882 pos++;
1883 os_strlcpy(wpa_trace_fail_func, pos,
1884 sizeof(wpa_trace_fail_func));
1885 } else {
1886 wpa_trace_fail_after = 0;
1887 }
1888
1889 return 0;
1890#else /* WPA_TRACE_BFD */
1891 return -1;
1892#endif /* WPA_TRACE_BFD */
1893}
1894
1895
1896static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1897 char *buf, size_t buflen)
1898{
1899#ifdef WPA_TRACE_BFD
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001900 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1901 wpa_trace_fail_func);
1902#else /* WPA_TRACE_BFD */
1903 return -1;
1904#endif /* WPA_TRACE_BFD */
1905}
1906
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001907
1908static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1909{
1910#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001911 char *pos;
1912
1913 wpa_trace_test_fail_after = atoi(cmd);
1914 pos = os_strchr(cmd, ':');
1915 if (pos) {
1916 pos++;
1917 os_strlcpy(wpa_trace_test_fail_func, pos,
1918 sizeof(wpa_trace_test_fail_func));
1919 } else {
1920 wpa_trace_test_fail_after = 0;
1921 }
1922
1923 return 0;
1924#else /* WPA_TRACE_BFD */
1925 return -1;
1926#endif /* WPA_TRACE_BFD */
1927}
1928
1929
1930static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1931 char *buf, size_t buflen)
1932{
1933#ifdef WPA_TRACE_BFD
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001934 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1935 wpa_trace_test_fail_func);
1936#else /* WPA_TRACE_BFD */
1937 return -1;
1938#endif /* WPA_TRACE_BFD */
1939}
1940
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001941#endif /* CONFIG_TESTING_OPTIONS */
1942
1943
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001944static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1945 char *pos)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001946{
1947#ifdef NEED_AP_MLME
1948 struct csa_settings settings;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001949 int ret;
1950 unsigned int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001951
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001952 ret = hostapd_parse_csa_settings(pos, &settings);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001953 if (ret)
1954 return ret;
1955
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001956 for (i = 0; i < iface->num_bss; i++) {
1957 ret = hostapd_switch_channel(iface->bss[i], &settings);
1958 if (ret) {
1959 /* FIX: What do we do if CSA fails in the middle of
1960 * submitting multi-BSS CSA requests? */
1961 return ret;
1962 }
1963 }
1964
1965 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001966#else /* NEED_AP_MLME */
1967 return -1;
1968#endif /* NEED_AP_MLME */
1969}
1970
1971
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001972static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1973 int reply_size, const char *param)
1974{
1975#ifdef RADIUS_SERVER
1976 if (os_strcmp(param, "radius_server") == 0) {
1977 return radius_server_get_mib(hapd->radius_srv, reply,
1978 reply_size);
1979 }
1980#endif /* RADIUS_SERVER */
1981 return -1;
1982}
1983
1984
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001985static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1986 char *buf, size_t buflen)
1987{
1988 int ret;
1989 char *pos;
1990 u8 *data = NULL;
1991 unsigned int vendor_id, subcmd;
1992 struct wpabuf *reply;
1993 size_t data_len = 0;
1994
1995 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1996 vendor_id = strtoul(cmd, &pos, 16);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001997 if (!isblank((unsigned char) *pos))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001998 return -EINVAL;
1999
2000 subcmd = strtoul(pos, &pos, 10);
2001
2002 if (*pos != '\0') {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002003 if (!isblank((unsigned char) *pos++))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002004 return -EINVAL;
2005 data_len = os_strlen(pos);
2006 }
2007
2008 if (data_len) {
2009 data_len /= 2;
2010 data = os_malloc(data_len);
2011 if (!data)
2012 return -ENOBUFS;
2013
2014 if (hexstr2bin(pos, data, data_len)) {
2015 wpa_printf(MSG_DEBUG,
2016 "Vendor command: wrong parameter format");
2017 os_free(data);
2018 return -EINVAL;
2019 }
2020 }
2021
2022 reply = wpabuf_alloc((buflen - 1) / 2);
2023 if (!reply) {
2024 os_free(data);
2025 return -ENOBUFS;
2026 }
2027
2028 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2029 reply);
2030
2031 if (ret == 0)
2032 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2033 wpabuf_len(reply));
2034
2035 wpabuf_free(reply);
2036 os_free(data);
2037
2038 return ret;
2039}
2040
2041
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002042static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2043 const char *cmd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002045 u8 addr[ETH_ALEN];
2046 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002048 if (hwaddr_aton(cmd, addr))
2049 return -1;
2050
2051 sta = ap_get_sta(hapd, addr);
2052 if (!sta || !sta->eapol_sm)
2053 return -1;
2054
2055 eapol_auth_reauthenticate(sta->eapol_sm);
2056 return 0;
2057}
2058
2059
2060static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2061{
2062 u8 addr[ETH_ALEN];
2063 struct sta_info *sta;
2064 char *pos = cmd, *param;
2065
2066 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2067 return -1;
2068 pos += 18;
2069 param = pos;
2070 pos = os_strchr(pos, ' ');
2071 if (!pos)
2072 return -1;
2073 *pos++ = '\0';
2074
2075 sta = ap_get_sta(hapd, addr);
2076 if (!sta || !sta->eapol_sm)
2077 return -1;
2078
2079 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2080}
2081
2082
2083static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2084 char *buf, size_t buflen)
2085{
2086 char *pos, *end, *stamp;
2087 int ret;
2088
2089 /* cmd: "LOG_LEVEL [<level>]" */
2090 if (*cmd == '\0') {
2091 pos = buf;
2092 end = buf + buflen;
2093 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2094 "Timestamp: %d\n",
2095 debug_level_str(wpa_debug_level),
2096 wpa_debug_timestamp);
2097 if (os_snprintf_error(end - pos, ret))
2098 ret = 0;
2099
2100 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002103 while (*cmd == ' ')
2104 cmd++;
2105
2106 stamp = os_strchr(cmd, ' ');
2107 if (stamp) {
2108 *stamp++ = '\0';
2109 while (*stamp == ' ') {
2110 stamp++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 }
2113
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002114 if (os_strlen(cmd)) {
2115 int level = str_to_debug_level(cmd);
2116 if (level < 0)
2117 return -1;
2118 wpa_debug_level = level;
2119 }
2120
2121 if (stamp && os_strlen(stamp))
2122 wpa_debug_timestamp = atoi(stamp);
2123
2124 os_memcpy(buf, "OK\n", 3);
2125 return 3;
2126}
2127
2128
2129#ifdef NEED_AP_MLME
2130static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2131 char *buf, size_t buflen)
2132{
2133 struct hostapd_iface *iface = hapd->iface;
2134 char *pos, *end;
2135 struct hostapd_sta_info *info;
2136 struct os_reltime now;
2137
Dmitry Shmidt7d175302016-09-06 13:11:34 -07002138 if (!iface->num_sta_seen)
2139 return 0;
2140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002141 sta_track_expire(iface, 0);
2142
2143 pos = buf;
2144 end = buf + buflen;
2145
2146 os_get_reltime(&now);
2147 dl_list_for_each_reverse(info, &iface->sta_seen,
2148 struct hostapd_sta_info, list) {
2149 struct os_reltime age;
2150 int ret;
2151
2152 os_reltime_sub(&now, &info->last_seen, &age);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002153 ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
2154 MAC2STR(info->addr), (unsigned int) age.sec,
2155 info->ssi_signal);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002156 if (os_snprintf_error(end - pos, ret))
2157 break;
2158 pos += ret;
2159 }
2160
2161 return pos - buf;
2162}
2163#endif /* NEED_AP_MLME */
2164
2165
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002166static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2167 const char *cmd)
2168{
2169 u8 addr[ETH_ALEN];
2170
2171 if (hwaddr_aton(cmd, addr)) {
2172 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2173 return -1;
2174 }
2175
2176 return hostapd_send_lci_req(hapd, addr);
2177}
2178
2179
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07002180static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002181{
2182 u8 addr[ETH_ALEN];
2183 char *token, *context = NULL;
2184 int random_interval, min_ap;
2185 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2186 unsigned int n_responders;
2187
2188 token = str_token(cmd, " ", &context);
2189 if (!token || hwaddr_aton(token, addr)) {
2190 wpa_printf(MSG_INFO,
2191 "CTRL: REQ_RANGE - Bad destination address");
2192 return -1;
2193 }
2194
2195 token = str_token(cmd, " ", &context);
2196 if (!token)
2197 return -1;
2198
2199 random_interval = atoi(token);
2200 if (random_interval < 0 || random_interval > 0xffff)
2201 return -1;
2202
2203 token = str_token(cmd, " ", &context);
2204 if (!token)
2205 return -1;
2206
2207 min_ap = atoi(token);
2208 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2209 return -1;
2210
2211 n_responders = 0;
2212 while ((token = str_token(cmd, " ", &context))) {
2213 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2214 wpa_printf(MSG_INFO,
2215 "CTRL: REQ_RANGE: Too many responders");
2216 return -1;
2217 }
2218
2219 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2220 wpa_printf(MSG_INFO,
2221 "CTRL: REQ_RANGE: Bad responder address");
2222 return -1;
2223 }
2224
2225 n_responders++;
2226 }
2227
2228 if (!n_responders) {
2229 wpa_printf(MSG_INFO,
2230 "CTRL: REQ_RANGE - No FTM responder address");
2231 return -1;
2232 }
2233
2234 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2235 responders, n_responders);
2236}
2237
2238
Dmitry Shmidt29333592017-01-09 12:27:11 -08002239static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
2240 const char *cmd, char *reply,
2241 size_t reply_size)
2242{
2243 u8 addr[ETH_ALEN];
2244 const char *pos;
2245 struct wpabuf *req;
2246 int ret;
2247 u8 req_mode = 0;
2248
2249 if (hwaddr_aton(cmd, addr))
2250 return -1;
2251 pos = os_strchr(cmd, ' ');
2252 if (!pos)
2253 return -1;
2254 pos++;
2255 if (os_strncmp(pos, "req_mode=", 9) == 0) {
2256 int val = hex2byte(pos + 9);
2257
2258 if (val < 0)
2259 return -1;
2260 req_mode = val;
2261 pos += 11;
2262 pos = os_strchr(pos, ' ');
2263 if (!pos)
2264 return -1;
2265 pos++;
2266 }
2267 req = wpabuf_parse_bin(pos);
2268 if (!req)
2269 return -1;
2270
2271 ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
2272 wpabuf_free(req);
2273 if (ret >= 0)
2274 ret = os_snprintf(reply, reply_size, "%d", ret);
2275 return ret;
2276}
2277
2278
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002279static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2280{
2281 struct wpa_ssid_value ssid;
2282 u8 bssid[ETH_ALEN];
2283 struct wpabuf *nr, *lci = NULL, *civic = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002284 int stationary = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002285 char *tmp;
2286 int ret;
2287
2288 if (!(hapd->conf->radio_measurements[0] &
2289 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2290 wpa_printf(MSG_ERROR,
2291 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2292 return -1;
2293 }
2294
2295 if (hwaddr_aton(buf, bssid)) {
2296 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2297 return -1;
2298 }
2299
2300 tmp = os_strstr(buf, "ssid=");
2301 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2302 wpa_printf(MSG_ERROR,
2303 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2304 return -1;
2305 }
2306 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2307 if (!buf)
2308 return -1;
2309
2310 tmp = os_strstr(buf, "nr=");
2311 if (!tmp) {
2312 wpa_printf(MSG_ERROR,
2313 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2314 return -1;
2315 }
2316
2317 buf = os_strchr(tmp, ' ');
2318 if (buf)
2319 *buf++ = '\0';
2320
2321 nr = wpabuf_parse_bin(tmp + 3);
2322 if (!nr) {
2323 wpa_printf(MSG_ERROR,
2324 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2325 return -1;
2326 }
2327
2328 if (!buf)
2329 goto set;
2330
2331 tmp = os_strstr(buf, "lci=");
2332 if (tmp) {
2333 buf = os_strchr(tmp, ' ');
2334 if (buf)
2335 *buf++ = '\0';
2336 lci = wpabuf_parse_bin(tmp + 4);
2337 if (!lci) {
2338 wpa_printf(MSG_ERROR,
2339 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2340 wpabuf_free(nr);
2341 return -1;
2342 }
2343 }
2344
2345 if (!buf)
2346 goto set;
2347
2348 tmp = os_strstr(buf, "civic=");
2349 if (tmp) {
2350 buf = os_strchr(tmp, ' ');
2351 if (buf)
2352 *buf++ = '\0';
2353 civic = wpabuf_parse_bin(tmp + 6);
2354 if (!civic) {
2355 wpa_printf(MSG_ERROR,
2356 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2357 wpabuf_free(nr);
2358 wpabuf_free(lci);
2359 return -1;
2360 }
2361 }
2362
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002363 if (!buf)
2364 goto set;
2365
2366 if (os_strstr(buf, "stat"))
2367 stationary = 1;
2368
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002369set:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002370 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
2371 stationary);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002372
2373 wpabuf_free(nr);
2374 wpabuf_free(lci);
2375 wpabuf_free(civic);
2376
2377 return ret;
2378}
2379
2380
2381static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2382 char *buf)
2383{
2384 struct wpa_ssid_value ssid;
2385 u8 bssid[ETH_ALEN];
2386 char *tmp;
2387
2388 if (hwaddr_aton(buf, bssid)) {
2389 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2390 return -1;
2391 }
2392
2393 tmp = os_strstr(buf, "ssid=");
2394 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2395 wpa_printf(MSG_ERROR,
2396 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2397 return -1;
2398 }
2399
2400 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2401}
2402
2403
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002404static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2405 size_t buflen)
2406{
2407 int ret, i;
2408 char *pos, *end;
2409
2410 ret = os_snprintf(buf, buflen, "%016llX:\n",
2411 (long long unsigned) iface->drv_flags);
2412 if (os_snprintf_error(buflen, ret))
2413 return -1;
2414
2415 pos = buf + ret;
2416 end = buf + buflen;
2417
2418 for (i = 0; i < 64; i++) {
2419 if (iface->drv_flags & (1LLU << i)) {
2420 ret = os_snprintf(pos, end - pos, "%s\n",
2421 driver_flag_to_string(1LLU << i));
2422 if (os_snprintf_error(end - pos, ret))
2423 return -1;
2424 pos += ret;
2425 }
2426 }
2427
2428 return pos - buf;
2429}
2430
2431
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002432static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2433 char *buf, char *reply,
2434 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002435 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002436 socklen_t fromlen)
2437{
2438 int reply_len, res;
2439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 os_memcpy(reply, "OK\n", 3);
2441 reply_len = 3;
2442
2443 if (os_strcmp(buf, "PING") == 0) {
2444 os_memcpy(reply, "PONG\n", 5);
2445 reply_len = 5;
2446 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2447 if (wpa_debug_reopen_file() < 0)
2448 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002449 } else if (os_strcmp(buf, "STATUS") == 0) {
2450 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2451 reply_size);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002452 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2453 reply_len = hostapd_drv_status(hapd, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 } else if (os_strcmp(buf, "MIB") == 0) {
2455 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2456 if (reply_len >= 0) {
2457 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2458 reply_size - reply_len);
2459 if (res < 0)
2460 reply_len = -1;
2461 else
2462 reply_len += res;
2463 }
2464 if (reply_len >= 0) {
2465 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2466 reply_size - reply_len);
2467 if (res < 0)
2468 reply_len = -1;
2469 else
2470 reply_len += res;
2471 }
2472#ifndef CONFIG_NO_RADIUS
2473 if (reply_len >= 0) {
2474 res = radius_client_get_mib(hapd->radius,
2475 reply + reply_len,
2476 reply_size - reply_len);
2477 if (res < 0)
2478 reply_len = -1;
2479 else
2480 reply_len += res;
2481 }
2482#endif /* CONFIG_NO_RADIUS */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002483 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2484 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2485 buf + 4);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2487 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2488 reply_size);
2489 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2490 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2491 reply_size);
2492 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2493 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2494 reply_size);
2495 } else if (os_strcmp(buf, "ATTACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002496 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 reply_len = -1;
2498 } else if (os_strcmp(buf, "DETACH") == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002499 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 reply_len = -1;
2501 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002502 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 buf + 6))
2504 reply_len = -1;
2505 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2506 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2507 reply_len = -1;
2508 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2509 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2510 reply_len = -1;
2511 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2512 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2513 reply_len = -1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002514#ifdef CONFIG_TAXONOMY
2515 } else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
2516 reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
2517 reply, reply_size);
2518#endif /* CONFIG_TAXONOMY */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002519 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2520 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2521 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002522 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2523 if (hostapd_ctrl_iface_stop_ap(hapd))
2524 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525#ifdef CONFIG_IEEE80211W
2526#ifdef NEED_AP_MLME
2527 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2528 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2529 reply_len = -1;
2530#endif /* NEED_AP_MLME */
2531#endif /* CONFIG_IEEE80211W */
2532#ifdef CONFIG_WPS
2533 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2534 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2535 reply_len = -1;
2536 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2537 reply_len = hostapd_ctrl_iface_wps_check_pin(
2538 hapd, buf + 14, reply, reply_size);
2539 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2540 if (hostapd_wps_button_pushed(hapd, NULL))
2541 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002542 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2543 if (hostapd_wps_cancel(hapd))
2544 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2546 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2547 reply, reply_size);
2548 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2549 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2550 reply_len = -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002551 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2552 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2553 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002554#ifdef CONFIG_WPS_NFC
2555 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2556 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2557 reply_len = -1;
2558 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2559 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2560 hapd, buf + 21, reply, reply_size);
2561 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2562 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2563 hapd, buf + 14, reply, reply_size);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002564 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2565 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2566 hapd, buf + 21, reply, reply_size);
2567 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2568 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2569 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002570#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571#endif /* CONFIG_WPS */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002572#ifdef CONFIG_INTERWORKING
2573 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2574 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2575 reply_len = -1;
2576 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2577 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2578 reply_len = -1;
2579#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002580#ifdef CONFIG_HS20
2581 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2582 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2583 reply_len = -1;
2584 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2585 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2586 reply_len = -1;
2587#endif /* CONFIG_HS20 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002588#ifdef CONFIG_WNM
2589 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2590 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2591 reply_len = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002592 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2593 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2594 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002595 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2596 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2597 reply_len = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002598#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2600 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2601 reply_size);
2602 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2603 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2604 reply_len = -1;
2605 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2606 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2607 reply_size);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002608 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2609 if (hostapd_ctrl_iface_enable(hapd->iface))
2610 reply_len = -1;
2611 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2612 if (hostapd_ctrl_iface_reload(hapd->iface))
2613 reply_len = -1;
2614 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2615 if (hostapd_ctrl_iface_disable(hapd->iface))
2616 reply_len = -1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002617 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2618 if (ieee802_11_set_beacon(hapd))
2619 reply_len = -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002620#ifdef CONFIG_TESTING_OPTIONS
2621 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2622 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2623 reply_len = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002624 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2625 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2626 reply_len = -1;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002627 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
2628 if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
2629 reply_len = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002630 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2631 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2632 reply_len = -1;
2633 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2634 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2635 reply_len = -1;
2636 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2637 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2638 reply_len = -1;
2639 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2640 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2641 reply_len = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002642 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2643 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2644 reply_len = -1;
2645 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2646 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2647 reply_size);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002648 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2649 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2650 reply_len = -1;
2651 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2652 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002653#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002654 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07002655 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002656 reply_len = -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002657 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2658 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2659 reply_size);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2661 ieee802_1x_erp_flush(hapd);
2662#ifdef RADIUS_SERVER
2663 radius_server_erp_flush(hapd->radius_srv);
2664#endif /* RADIUS_SERVER */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002665 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2666 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2667 reply_len = -1;
2668 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2669 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2670 reply_len = -1;
2671 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2672 reply_len = hostapd_ctrl_iface_log_level(
2673 hapd, buf + 9, reply, reply_size);
2674#ifdef NEED_AP_MLME
2675 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2676 reply_len = hostapd_ctrl_iface_track_sta_list(
2677 hapd, reply, reply_size);
2678#endif /* NEED_AP_MLME */
Dmitry Shmidte4663042016-04-04 10:07:49 -07002679 } else if (os_strcmp(buf, "PMKSA") == 0) {
2680 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2681 reply_size);
2682 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2683 hostapd_ctrl_iface_pmksa_flush(hapd);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002684 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2685 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2686 reply_len = -1;
2687 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2688 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2689 reply_len = -1;
2690 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
2691 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
2692 reply_len = -1;
2693 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
2694 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
2695 reply_len = -1;
Dmitry Shmidt29333592017-01-09 12:27:11 -08002696 } else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
2697 reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
2698 reply, reply_size);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07002699 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
2700 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
2701 reply_size);
Dmitry Shmidt29333592017-01-09 12:27:11 -08002702 } else if (os_strcmp(buf, "TERMINATE") == 0) {
2703 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 } else {
2705 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2706 reply_len = 16;
2707 }
2708
2709 if (reply_len < 0) {
2710 os_memcpy(reply, "FAIL\n", 5);
2711 reply_len = 5;
2712 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002713
2714 return reply_len;
2715}
2716
2717
2718static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2719 void *sock_ctx)
2720{
2721 struct hostapd_data *hapd = eloop_ctx;
2722 char buf[4096];
2723 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002724 struct sockaddr_storage from;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002725 socklen_t fromlen = sizeof(from);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002726 char *reply, *pos = buf;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002727 const int reply_size = 4096;
2728 int reply_len;
2729 int level = MSG_DEBUG;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002730#ifdef CONFIG_CTRL_IFACE_UDP
2731 unsigned char lcookie[COOKIE_LEN];
2732#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002733
2734 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2735 (struct sockaddr *) &from, &fromlen);
2736 if (res < 0) {
2737 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2738 strerror(errno));
2739 return;
2740 }
2741 buf[res] = '\0';
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002742
2743 reply = os_malloc(reply_size);
2744 if (reply == NULL) {
2745 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2746 fromlen) < 0) {
2747 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2748 strerror(errno));
2749 }
2750 return;
2751 }
2752
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002753#ifdef CONFIG_CTRL_IFACE_UDP
2754 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2755 os_memcpy(reply, "COOKIE=", 7);
2756 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2757 cookie, COOKIE_LEN);
2758 reply_len = 7 + 2 * COOKIE_LEN;
2759 goto done;
2760 }
2761
2762 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2763 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2764 wpa_printf(MSG_DEBUG,
2765 "CTRL: No cookie in the request - drop request");
2766 os_free(reply);
2767 return;
2768 }
2769
2770 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2771 wpa_printf(MSG_DEBUG,
2772 "CTRL: Invalid cookie in the request - drop request");
2773 os_free(reply);
2774 return;
2775 }
2776
2777 pos = buf + 7 + 2 * COOKIE_LEN;
2778 while (*pos == ' ')
2779 pos++;
2780#endif /* CONFIG_CTRL_IFACE_UDP */
2781
2782 if (os_strcmp(pos, "PING") == 0)
2783 level = MSG_EXCESSIVE;
2784 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2785
2786 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002787 reply, reply_size,
2788 &from, fromlen);
2789
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002790#ifdef CONFIG_CTRL_IFACE_UDP
2791done:
2792#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002793 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2794 fromlen) < 0) {
2795 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2796 strerror(errno));
2797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 os_free(reply);
2799}
2800
2801
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002802#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2804{
2805 char *buf;
2806 size_t len;
2807
2808 if (hapd->conf->ctrl_interface == NULL)
2809 return NULL;
2810
2811 len = os_strlen(hapd->conf->ctrl_interface) +
2812 os_strlen(hapd->conf->iface) + 2;
2813 buf = os_malloc(len);
2814 if (buf == NULL)
2815 return NULL;
2816
2817 os_snprintf(buf, len, "%s/%s",
2818 hapd->conf->ctrl_interface, hapd->conf->iface);
2819 buf[len - 1] = '\0';
2820 return buf;
2821}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002822#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823
2824
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002825static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2826 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 const char *txt, size_t len)
2828{
2829 struct hostapd_data *hapd = ctx;
2830 if (hapd == NULL)
2831 return;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02002832 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833}
2834
2835
2836int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2837{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002838#ifdef CONFIG_CTRL_IFACE_UDP
2839 int port = HOSTAPD_CTRL_IFACE_PORT;
2840 char p[32] = { 0 };
2841 char port_str[40], *tmp;
2842 char *pos;
2843 struct addrinfo hints = { 0 }, *res, *saveres;
2844 int n;
2845
2846 if (hapd->ctrl_sock > -1) {
2847 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2848 return 0;
2849 }
2850
2851 if (hapd->conf->ctrl_interface == NULL)
2852 return 0;
2853
2854 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
2855 if (pos) {
2856 pos += 4;
2857 port = atoi(pos);
2858 if (port <= 0) {
2859 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
2860 goto fail;
2861 }
2862 }
2863
2864 dl_list_init(&hapd->ctrl_dst);
2865 hapd->ctrl_sock = -1;
2866 os_get_random(cookie, COOKIE_LEN);
2867
2868#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2869 hints.ai_flags = AI_PASSIVE;
2870#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2871
2872#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2873 hints.ai_family = AF_INET6;
2874#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2875 hints.ai_family = AF_INET;
2876#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2877 hints.ai_socktype = SOCK_DGRAM;
2878
2879try_again:
2880 os_snprintf(p, sizeof(p), "%d", port);
2881 n = getaddrinfo(NULL, p, &hints, &res);
2882 if (n) {
2883 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
2884 goto fail;
2885 }
2886
2887 saveres = res;
2888 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
2889 res->ai_protocol);
2890 if (hapd->ctrl_sock < 0) {
2891 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
2892 goto fail;
2893 }
2894
2895 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
2896 port--;
2897 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
2898 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
2899 goto try_again;
2900 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
2901 goto fail;
2902 }
2903
2904 freeaddrinfo(saveres);
2905
2906 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
2907 tmp = os_strdup(port_str);
2908 if (tmp) {
2909 os_free(hapd->conf->ctrl_interface);
2910 hapd->conf->ctrl_interface = tmp;
2911 }
2912 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
2913
2914 if (eloop_register_read_sock(hapd->ctrl_sock,
2915 hostapd_ctrl_iface_receive, hapd, NULL) <
2916 0) {
2917 hostapd_ctrl_iface_deinit(hapd);
2918 return -1;
2919 }
2920
2921 hapd->msg_ctx = hapd;
2922 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2923
2924 return 0;
2925
2926fail:
2927 if (hapd->ctrl_sock >= 0)
2928 close(hapd->ctrl_sock);
2929 return -1;
2930#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 struct sockaddr_un addr;
2932 int s = -1;
2933 char *fname = NULL;
2934
Dmitry Shmidt04949592012-07-19 12:16:46 -07002935 if (hapd->ctrl_sock > -1) {
2936 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2937 return 0;
2938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08002940 dl_list_init(&hapd->ctrl_dst);
2941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 if (hapd->conf->ctrl_interface == NULL)
2943 return 0;
2944
2945 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2946 if (errno == EEXIST) {
2947 wpa_printf(MSG_DEBUG, "Using existing control "
2948 "interface directory.");
2949 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002950 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2951 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 goto fail;
2953 }
2954 }
2955
2956 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002957 chown(hapd->conf->ctrl_interface, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002959 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2960 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 return -1;
2962 }
2963
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002964 if (!hapd->conf->ctrl_interface_gid_set &&
2965 hapd->iface->interfaces->ctrl_iface_group &&
2966 chown(hapd->conf->ctrl_interface, -1,
2967 hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002968 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2969 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002970 return -1;
2971 }
2972
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002973#ifdef ANDROID
2974 /*
2975 * Android is using umask 0077 which would leave the control interface
2976 * directory without group access. This breaks things since Wi-Fi
2977 * framework assumes that this directory can be accessed by other
2978 * applications in the wifi group. Fix this by adding group access even
2979 * if umask value would prevent this.
2980 */
2981 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2982 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2983 strerror(errno));
2984 /* Try to continue anyway */
2985 }
2986#endif /* ANDROID */
2987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2989 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2990 goto fail;
2991
2992 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2993 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002994 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 goto fail;
2996 }
2997
2998 os_memset(&addr, 0, sizeof(addr));
2999#ifdef __FreeBSD__
3000 addr.sun_len = sizeof(addr);
3001#endif /* __FreeBSD__ */
3002 addr.sun_family = AF_UNIX;
3003 fname = hostapd_ctrl_iface_path(hapd);
3004 if (fname == NULL)
3005 goto fail;
3006 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3007 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3008 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3009 strerror(errno));
3010 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3011 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3012 " allow connections - assuming it was left"
3013 "over from forced program termination");
3014 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003015 wpa_printf(MSG_ERROR,
3016 "Could not unlink existing ctrl_iface socket '%s': %s",
3017 fname, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 goto fail;
3019 }
3020 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3021 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003022 wpa_printf(MSG_ERROR,
3023 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3024 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 goto fail;
3026 }
3027 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3028 "ctrl_iface socket '%s'", fname);
3029 } else {
3030 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3031 "be in use - cannot override it");
3032 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3033 "not used anymore", fname);
3034 os_free(fname);
3035 fname = NULL;
3036 goto fail;
3037 }
3038 }
3039
3040 if (hapd->conf->ctrl_interface_gid_set &&
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003041 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003042 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3043 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 goto fail;
3045 }
3046
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003047 if (!hapd->conf->ctrl_interface_gid_set &&
3048 hapd->iface->interfaces->ctrl_iface_group &&
3049 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003050 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
3051 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003052 goto fail;
3053 }
3054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003056 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3057 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 goto fail;
3059 }
3060 os_free(fname);
3061
3062 hapd->ctrl_sock = s;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003063 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
3064 NULL) < 0) {
3065 hostapd_ctrl_iface_deinit(hapd);
3066 return -1;
3067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 hapd->msg_ctx = hapd;
3069 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
3070
3071 return 0;
3072
3073fail:
3074 if (s >= 0)
3075 close(s);
3076 if (fname) {
3077 unlink(fname);
3078 os_free(fname);
3079 }
3080 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003081#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082}
3083
3084
3085void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
3086{
3087 struct wpa_ctrl_dst *dst, *prev;
3088
3089 if (hapd->ctrl_sock > -1) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003090#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 char *fname;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003092#endif /* !CONFIG_CTRL_IFACE_UDP */
3093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 eloop_unregister_read_sock(hapd->ctrl_sock);
3095 close(hapd->ctrl_sock);
3096 hapd->ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003097#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 fname = hostapd_ctrl_iface_path(hapd);
3099 if (fname)
3100 unlink(fname);
3101 os_free(fname);
3102
3103 if (hapd->conf->ctrl_interface &&
3104 rmdir(hapd->conf->ctrl_interface) < 0) {
3105 if (errno == ENOTEMPTY) {
3106 wpa_printf(MSG_DEBUG, "Control interface "
3107 "directory not empty - leaving it "
3108 "behind");
3109 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003110 wpa_printf(MSG_ERROR,
3111 "rmdir[ctrl_interface=%s]: %s",
3112 hapd->conf->ctrl_interface,
3113 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 }
3115 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003116#endif /* !CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 }
3118
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003119 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
3120 list)
3121 os_free(dst);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003122
3123#ifdef CONFIG_TESTING_OPTIONS
3124 l2_packet_deinit(hapd->l2_test);
3125 hapd->l2_test = NULL;
3126#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127}
3128
3129
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003130static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
3131 char *buf)
3132{
3133 if (hostapd_add_iface(interfaces, buf) < 0) {
3134 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
3135 return -1;
3136 }
3137 return 0;
3138}
3139
3140
3141static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
3142 char *buf)
3143{
3144 if (hostapd_remove_iface(interfaces, buf) < 0) {
3145 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
3146 return -1;
3147 }
3148 return 0;
3149}
3150
3151
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003152static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003153 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003154 socklen_t fromlen)
3155{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003156 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003157}
3158
3159
3160static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003161 struct sockaddr_storage *from,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003162 socklen_t fromlen)
3163{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003164 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003165}
3166
3167
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003168static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3169{
3170#ifdef CONFIG_WPS_TESTING
3171 wps_version_number = 0x20;
3172 wps_testing_dummy_cred = 0;
3173 wps_corrupt_pkhash = 0;
3174#endif /* CONFIG_WPS_TESTING */
3175}
3176
3177
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003178#ifdef CONFIG_FST
3179
3180static int
3181hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3182 const char *cmd)
3183{
3184 char ifname[IFNAMSIZ + 1];
3185 struct fst_iface_cfg cfg;
3186 struct hostapd_data *hapd;
3187 struct fst_wpa_obj iface_obj;
3188
3189 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3190 hapd = hostapd_get_iface(interfaces, ifname);
3191 if (hapd) {
3192 if (hapd->iface->fst) {
3193 wpa_printf(MSG_INFO, "FST: Already attached");
3194 return -1;
3195 }
3196 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3197 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3198 &iface_obj, &cfg);
3199 if (hapd->iface->fst)
3200 return 0;
3201 }
3202 }
3203
3204 return -EINVAL;
3205}
3206
3207
3208static int
3209hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3210 const char *cmd)
3211{
3212 char ifname[IFNAMSIZ + 1];
3213 struct hostapd_data * hapd;
3214
3215 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3216 hapd = hostapd_get_iface(interfaces, ifname);
3217 if (hapd) {
3218 if (!fst_iface_detach(ifname)) {
3219 hapd->iface->fst = NULL;
3220 hapd->iface->fst_ies = NULL;
3221 return 0;
3222 }
3223 }
3224 }
3225
3226 return -EINVAL;
3227}
3228
3229#endif /* CONFIG_FST */
3230
3231
3232static struct hostapd_data *
3233hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3234 const char *ifname)
3235{
3236 size_t i, j;
3237
3238 for (i = 0; i < interfaces->count; i++) {
3239 struct hostapd_iface *iface = interfaces->iface[i];
3240
3241 for (j = 0; j < iface->num_bss; j++) {
3242 struct hostapd_data *hapd;
3243
3244 hapd = iface->bss[j];
3245 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3246 return hapd;
3247 }
3248 }
3249
3250 return NULL;
3251}
3252
3253
3254static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3255 struct hostapd_data *dst_hapd,
3256 const char *param)
3257{
3258 int res;
3259 char *value;
3260
3261 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3262 if (!value) {
3263 wpa_printf(MSG_ERROR,
3264 "DUP: cannot allocate buffer to stringify %s",
3265 param);
3266 goto error_return;
3267 }
3268
3269 if (os_strcmp(param, "wpa") == 0) {
3270 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3271 src_hapd->conf->wpa);
3272 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3273 src_hapd->conf->wpa_key_mgmt) {
3274 res = hostapd_ctrl_iface_get_key_mgmt(
3275 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3276 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3277 goto error_stringify;
3278 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3279 src_hapd->conf->wpa_pairwise) {
3280 res = wpa_write_ciphers(value,
3281 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3282 src_hapd->conf->wpa_pairwise, " ");
3283 if (res < 0)
3284 goto error_stringify;
3285 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3286 src_hapd->conf->rsn_pairwise) {
3287 res = wpa_write_ciphers(value,
3288 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3289 src_hapd->conf->rsn_pairwise, " ");
3290 if (res < 0)
3291 goto error_stringify;
3292 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3293 src_hapd->conf->ssid.wpa_passphrase) {
3294 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3295 src_hapd->conf->ssid.wpa_passphrase);
3296 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3297 src_hapd->conf->ssid.wpa_psk_set) {
3298 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3299 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3300 } else {
3301 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3302 goto error_return;
3303 }
3304
3305 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3306 os_free(value);
3307 return res;
3308
3309error_stringify:
3310 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3311error_return:
3312 os_free(value);
3313 return -1;
3314}
3315
3316
3317static int
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003318hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3319 const char *input,
3320 char *reply, int reply_size)
3321{
3322 size_t i, j;
3323 int res;
3324 char *pos, *end;
3325 struct hostapd_iface *iface;
3326 int show_ctrl = 0;
3327
3328 if (input)
3329 show_ctrl = !!os_strstr(input, "ctrl");
3330
3331 pos = reply;
3332 end = reply + reply_size;
3333
3334 for (i = 0; i < interfaces->count; i++) {
3335 iface = interfaces->iface[i];
3336
3337 for (j = 0; j < iface->num_bss; j++) {
3338 struct hostapd_bss_config *conf;
3339
3340 conf = iface->conf->bss[j];
3341 if (show_ctrl)
3342 res = os_snprintf(pos, end - pos,
3343 "%s ctrl_iface=%s\n",
3344 conf->iface,
3345 conf->ctrl_interface ?
3346 conf->ctrl_interface : "N/A");
3347 else
3348 res = os_snprintf(pos, end - pos, "%s\n",
3349 conf->iface);
3350 if (os_snprintf_error(end - pos, res)) {
3351 *pos = '\0';
3352 return pos - reply;
3353 }
3354 pos += res;
3355 }
3356 }
3357
3358 return pos - reply;
3359}
3360
3361
3362static int
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003363hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3364 char *cmd)
3365{
3366 char *p_start = cmd, *p_end;
3367 struct hostapd_data *src_hapd, *dst_hapd;
3368
3369 /* cmd: "<src ifname> <dst ifname> <variable name> */
3370
3371 p_end = os_strchr(p_start, ' ');
3372 if (!p_end) {
3373 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3374 cmd);
3375 return -1;
3376 }
3377
3378 *p_end = '\0';
3379 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3380 if (!src_hapd) {
3381 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3382 p_start);
3383 return -1;
3384 }
3385
3386 p_start = p_end + 1;
3387 p_end = os_strchr(p_start, ' ');
3388 if (!p_end) {
3389 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3390 cmd);
3391 return -1;
3392 }
3393
3394 *p_end = '\0';
3395 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3396 if (!dst_hapd) {
3397 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3398 p_start);
3399 return -1;
3400 }
3401
3402 p_start = p_end + 1;
3403 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3404}
3405
3406
3407static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3408 const char *ifname,
3409 char *buf, char *reply,
3410 int reply_size,
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003411 struct sockaddr_storage *from,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003412 socklen_t fromlen)
3413{
3414 struct hostapd_data *hapd;
3415
3416 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3417 if (hapd == NULL) {
3418 int res;
3419
3420 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3421 if (os_snprintf_error(reply_size, res))
3422 return -1;
3423 return res;
3424 }
3425
3426 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3427 from, fromlen);
3428}
3429
3430
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003431static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3432 void *sock_ctx)
3433{
3434 void *interfaces = eloop_ctx;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003435 char buffer[256], *buf = buffer;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003436 int res;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003437 struct sockaddr_storage from;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003438 socklen_t fromlen = sizeof(from);
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003439 char *reply;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003440 int reply_len;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003441 const int reply_size = 4096;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003442#ifdef CONFIG_CTRL_IFACE_UDP
3443 unsigned char lcookie[COOKIE_LEN];
3444#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003445
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003446 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003447 (struct sockaddr *) &from, &fromlen);
3448 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003449 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3450 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003451 return;
3452 }
3453 buf[res] = '\0';
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003454 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003455
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003456 reply = os_malloc(reply_size);
3457 if (reply == NULL) {
3458 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3459 fromlen) < 0) {
3460 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3461 strerror(errno));
3462 }
3463 return;
3464 }
3465
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003466 os_memcpy(reply, "OK\n", 3);
3467 reply_len = 3;
3468
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003469#ifdef CONFIG_CTRL_IFACE_UDP
3470 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3471 os_memcpy(reply, "COOKIE=", 7);
3472 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3473 gcookie, COOKIE_LEN);
3474 reply_len = 7 + 2 * COOKIE_LEN;
3475 goto send_reply;
3476 }
3477
3478 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3479 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3480 wpa_printf(MSG_DEBUG,
3481 "CTRL: No cookie in the request - drop request");
3482 os_free(reply);
3483 return;
3484 }
3485
3486 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3487 wpa_printf(MSG_DEBUG,
3488 "CTRL: Invalid cookie in the request - drop request");
3489 os_free(reply);
3490 return;
3491 }
3492
3493 buf += 7 + 2 * COOKIE_LEN;
3494 while (*buf == ' ')
3495 buf++;
3496#endif /* CONFIG_CTRL_IFACE_UDP */
3497
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003498 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3499 char *pos = os_strchr(buf + 7, ' ');
3500
3501 if (pos) {
3502 *pos++ = '\0';
3503 reply_len = hostapd_global_ctrl_iface_ifname(
3504 interfaces, buf + 7, pos, reply, reply_size,
3505 &from, fromlen);
3506 goto send_reply;
3507 }
3508 }
3509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003510 if (os_strcmp(buf, "PING") == 0) {
3511 os_memcpy(reply, "PONG\n", 5);
3512 reply_len = 5;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003513 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3514 if (wpa_debug_reopen_file() < 0)
3515 reply_len = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003516 } else if (os_strcmp(buf, "FLUSH") == 0) {
3517 hostapd_ctrl_iface_flush(interfaces);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003518 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3519 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3520 reply_len = -1;
3521 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3522 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3523 reply_len = -1;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003524 } else if (os_strcmp(buf, "ATTACH") == 0) {
3525 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3526 fromlen))
3527 reply_len = -1;
3528 } else if (os_strcmp(buf, "DETACH") == 0) {
3529 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3530 fromlen))
3531 reply_len = -1;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08003532#ifdef CONFIG_MODULE_TESTS
3533 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08003534 if (hapd_module_tests() < 0)
3535 reply_len = -1;
3536#endif /* CONFIG_MODULE_TESTS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003537#ifdef CONFIG_FST
3538 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3539 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3540 reply_len = os_snprintf(reply, reply_size, "OK\n");
3541 else
3542 reply_len = -1;
3543 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3544 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3545 reply_len = os_snprintf(reply, reply_size, "OK\n");
3546 else
3547 reply_len = -1;
3548 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3549 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3550#endif /* CONFIG_FST */
3551 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3552 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3553 buf + 12))
3554 reply_len = os_snprintf(reply, reply_size, "OK\n");
3555 else
3556 reply_len = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003557 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3558 reply_len = hostapd_global_ctrl_iface_interfaces(
3559 interfaces, buf + 10, reply, sizeof(buffer));
3560 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3561 eloop_terminate();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003562 } else {
3563 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3564 "ignored");
3565 reply_len = -1;
3566 }
3567
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003568send_reply:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003569 if (reply_len < 0) {
3570 os_memcpy(reply, "FAIL\n", 5);
3571 reply_len = 5;
3572 }
3573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003574 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3575 fromlen) < 0) {
3576 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3577 strerror(errno));
3578 }
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003579 os_free(reply);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003580}
3581
3582
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003583#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003584static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3585{
3586 char *buf;
3587 size_t len;
3588
3589 if (interface->global_iface_path == NULL)
3590 return NULL;
3591
3592 len = os_strlen(interface->global_iface_path) +
3593 os_strlen(interface->global_iface_name) + 2;
3594 buf = os_malloc(len);
3595 if (buf == NULL)
3596 return NULL;
3597
3598 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3599 interface->global_iface_name);
3600 buf[len - 1] = '\0';
3601 return buf;
3602}
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003603#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003604
3605
3606int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3607{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003608#ifdef CONFIG_CTRL_IFACE_UDP
3609 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3610 char p[32] = { 0 };
3611 char *pos;
3612 struct addrinfo hints = { 0 }, *res, *saveres;
3613 int n;
3614
3615 if (interface->global_ctrl_sock > -1) {
3616 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3617 return 0;
3618 }
3619
3620 if (interface->global_iface_path == NULL)
3621 return 0;
3622
3623 pos = os_strstr(interface->global_iface_path, "udp:");
3624 if (pos) {
3625 pos += 4;
3626 port = atoi(pos);
3627 if (port <= 0) {
3628 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3629 goto fail;
3630 }
3631 }
3632
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003633 os_get_random(gcookie, COOKIE_LEN);
3634
3635#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3636 hints.ai_flags = AI_PASSIVE;
3637#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3638
3639#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3640 hints.ai_family = AF_INET6;
3641#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3642 hints.ai_family = AF_INET;
3643#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3644 hints.ai_socktype = SOCK_DGRAM;
3645
3646try_again:
3647 os_snprintf(p, sizeof(p), "%d", port);
3648 n = getaddrinfo(NULL, p, &hints, &res);
3649 if (n) {
3650 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3651 goto fail;
3652 }
3653
3654 saveres = res;
3655 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3656 res->ai_protocol);
3657 if (interface->global_ctrl_sock < 0) {
3658 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3659 goto fail;
3660 }
3661
3662 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3663 0) {
3664 port++;
3665 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3666 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3667 goto try_again;
3668 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3669 goto fail;
3670 }
3671
3672 freeaddrinfo(saveres);
3673
3674 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3675
3676 if (eloop_register_read_sock(interface->global_ctrl_sock,
3677 hostapd_global_ctrl_iface_receive,
3678 interface, NULL) < 0) {
3679 hostapd_global_ctrl_iface_deinit(interface);
3680 return -1;
3681 }
3682
3683 return 0;
3684
3685fail:
3686 if (interface->global_ctrl_sock >= 0)
3687 close(interface->global_ctrl_sock);
3688 return -1;
3689#else /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003690 struct sockaddr_un addr;
3691 int s = -1;
3692 char *fname = NULL;
3693
3694 if (interface->global_iface_path == NULL) {
3695 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3696 return 0;
3697 }
3698
3699 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3700 if (errno == EEXIST) {
3701 wpa_printf(MSG_DEBUG, "Using existing control "
3702 "interface directory.");
3703 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003704 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3705 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003706 goto fail;
3707 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003708 } else if (interface->ctrl_iface_group &&
3709 chown(interface->global_iface_path, -1,
3710 interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003711 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3712 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003713 goto fail;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003714 }
3715
3716 if (os_strlen(interface->global_iface_path) + 1 +
3717 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3718 goto fail;
3719
3720 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3721 if (s < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003722 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003723 goto fail;
3724 }
3725
3726 os_memset(&addr, 0, sizeof(addr));
3727#ifdef __FreeBSD__
3728 addr.sun_len = sizeof(addr);
3729#endif /* __FreeBSD__ */
3730 addr.sun_family = AF_UNIX;
3731 fname = hostapd_global_ctrl_iface_path(interface);
3732 if (fname == NULL)
3733 goto fail;
3734 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3735 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3736 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3737 strerror(errno));
3738 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3739 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3740 " allow connections - assuming it was left"
3741 "over from forced program termination");
3742 if (unlink(fname) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003743 wpa_printf(MSG_ERROR,
3744 "Could not unlink existing ctrl_iface socket '%s': %s",
3745 fname, strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003746 goto fail;
3747 }
3748 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3749 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003750 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3751 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003752 goto fail;
3753 }
3754 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3755 "ctrl_iface socket '%s'", fname);
3756 } else {
3757 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3758 "be in use - cannot override it");
3759 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3760 "not used anymore", fname);
3761 os_free(fname);
3762 fname = NULL;
3763 goto fail;
3764 }
3765 }
3766
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003767 if (interface->ctrl_iface_group &&
3768 chown(fname, -1, interface->ctrl_iface_group) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003769 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3770 strerror(errno));
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003771 goto fail;
3772 }
3773
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003774 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003775 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3776 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003777 goto fail;
3778 }
3779 os_free(fname);
3780
3781 interface->global_ctrl_sock = s;
3782 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3783 interface, NULL);
3784
3785 return 0;
3786
3787fail:
3788 if (s >= 0)
3789 close(s);
3790 if (fname) {
3791 unlink(fname);
3792 os_free(fname);
3793 }
3794 return -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003795#endif /* CONFIG_CTRL_IFACE_UDP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003796}
3797
3798
3799void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3800{
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003801#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003802 char *fname = NULL;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003803#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003804 struct wpa_ctrl_dst *dst, *prev;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003805
3806 if (interfaces->global_ctrl_sock > -1) {
3807 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3808 close(interfaces->global_ctrl_sock);
3809 interfaces->global_ctrl_sock = -1;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003810#ifndef CONFIG_CTRL_IFACE_UDP
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003811 fname = hostapd_global_ctrl_iface_path(interfaces);
3812 if (fname) {
3813 unlink(fname);
3814 os_free(fname);
3815 }
3816
3817 if (interfaces->global_iface_path &&
3818 rmdir(interfaces->global_iface_path) < 0) {
3819 if (errno == ENOTEMPTY) {
3820 wpa_printf(MSG_DEBUG, "Control interface "
3821 "directory not empty - leaving it "
3822 "behind");
3823 } else {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07003824 wpa_printf(MSG_ERROR,
3825 "rmdir[ctrl_interface=%s]: %s",
3826 interfaces->global_iface_path,
3827 strerror(errno));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003828 }
3829 }
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003830#endif /* CONFIG_CTRL_IFACE_UDP */
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003831 }
3832
3833 os_free(interfaces->global_iface_path);
3834 interfaces->global_iface_path = NULL;
3835
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003836 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
3837 struct wpa_ctrl_dst, list)
3838 os_free(dst);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003839}
3840
3841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003843 enum wpa_msg_type type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 const char *buf, size_t len)
3845{
3846 struct wpa_ctrl_dst *dst, *next;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003847 struct dl_list *ctrl_dst;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848 struct msghdr msg;
3849 int idx;
3850 struct iovec io[2];
3851 char levelstr[10];
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003852 int s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003854 if (type != WPA_MSG_ONLY_GLOBAL) {
3855 s = hapd->ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003856 ctrl_dst = &hapd->ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003857 } else {
3858 s = hapd->iface->interfaces->global_ctrl_sock;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003859 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003860 }
3861
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003862 if (s < 0 || dl_list_empty(ctrl_dst))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 return;
3864
3865 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3866 io[0].iov_base = levelstr;
3867 io[0].iov_len = os_strlen(levelstr);
3868 io[1].iov_base = (char *) buf;
3869 io[1].iov_len = len;
3870 os_memset(&msg, 0, sizeof(msg));
3871 msg.msg_iov = io;
3872 msg.msg_iovlen = 2;
3873
3874 idx = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003875 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 if (level >= dst->debug_level) {
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003877 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
3878 &dst->addr, dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 msg.msg_name = &dst->addr;
3880 msg.msg_namelen = dst->addrlen;
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003881 if (sendmsg(s, &msg, 0) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 int _errno = errno;
3883 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3884 "%d - %s",
3885 idx, errno, strerror(errno));
3886 dst->errors++;
3887 if (dst->errors > 10 || _errno == ENOENT) {
Anton Nayshtutf715e8d2014-11-16 16:52:49 +02003888 if (type != WPA_MSG_ONLY_GLOBAL)
3889 hostapd_ctrl_iface_detach(
3890 hapd, &dst->addr,
3891 dst->addrlen);
3892 else
3893 hostapd_global_ctrl_iface_detach(
3894 hapd->iface->interfaces,
3895 &dst->addr,
3896 dst->addrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 }
3898 } else
3899 dst->errors = 0;
3900 }
3901 idx++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 }
3903}
3904
3905#endif /* CONFIG_NATIVE_WINDOWS */