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