HS 2.0R2 AP: Add WNM-Notification Request for Subscription Remediation
[mech_eap.git] / hostapd / ctrl_iface.c
1 /*
2  * hostapd / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2014, 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 #include <sys/un.h>
14 #include <sys/stat.h>
15 #include <stddef.h>
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/version.h"
20 #include "common/ieee802_11_defs.h"
21 #include "drivers/driver.h"
22 #include "radius/radius_client.h"
23 #include "radius/radius_server.h"
24 #include "ap/hostapd.h"
25 #include "ap/ap_config.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wpa_auth.h"
28 #include "ap/ieee802_11.h"
29 #include "ap/sta_info.h"
30 #include "ap/wps_hostapd.h"
31 #include "ap/ctrl_iface_ap.h"
32 #include "ap/ap_drv_ops.h"
33 #include "ap/hs20.h"
34 #include "ap/wnm_ap.h"
35 #include "ap/wpa_auth.h"
36 #include "wps/wps_defs.h"
37 #include "wps/wps.h"
38 #include "config_file.h"
39 #include "ctrl_iface.h"
40
41
42 struct wpa_ctrl_dst {
43         struct wpa_ctrl_dst *next;
44         struct sockaddr_un addr;
45         socklen_t addrlen;
46         int debug_level;
47         int errors;
48 };
49
50
51 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
52                                     const char *buf, size_t len);
53
54
55 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
56                                      struct sockaddr_un *from,
57                                      socklen_t fromlen)
58 {
59         struct wpa_ctrl_dst *dst;
60
61         dst = os_zalloc(sizeof(*dst));
62         if (dst == NULL)
63                 return -1;
64         os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
65         dst->addrlen = fromlen;
66         dst->debug_level = MSG_INFO;
67         dst->next = hapd->ctrl_dst;
68         hapd->ctrl_dst = dst;
69         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70                     (u8 *) from->sun_path,
71                     fromlen - offsetof(struct sockaddr_un, sun_path));
72         return 0;
73 }
74
75
76 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
77                                      struct sockaddr_un *from,
78                                      socklen_t fromlen)
79 {
80         struct wpa_ctrl_dst *dst, *prev = NULL;
81
82         dst = hapd->ctrl_dst;
83         while (dst) {
84                 if (fromlen == dst->addrlen &&
85                     os_memcmp(from->sun_path, dst->addr.sun_path,
86                               fromlen - offsetof(struct sockaddr_un, sun_path))
87                     == 0) {
88                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89                                     (u8 *) from->sun_path,
90                                     fromlen -
91                                     offsetof(struct sockaddr_un, sun_path));
92                         if (prev == NULL)
93                                 hapd->ctrl_dst = dst->next;
94                         else
95                                 prev->next = dst->next;
96                         os_free(dst);
97                         return 0;
98                 }
99                 prev = dst;
100                 dst = dst->next;
101         }
102         return -1;
103 }
104
105
106 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
107                                     struct sockaddr_un *from,
108                                     socklen_t fromlen,
109                                     char *level)
110 {
111         struct wpa_ctrl_dst *dst;
112
113         wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
114
115         dst = hapd->ctrl_dst;
116         while (dst) {
117                 if (fromlen == dst->addrlen &&
118                     os_memcmp(from->sun_path, dst->addr.sun_path,
119                               fromlen - offsetof(struct sockaddr_un, sun_path))
120                     == 0) {
121                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
122                                     "level", (u8 *) from->sun_path, fromlen -
123                                     offsetof(struct sockaddr_un, sun_path));
124                         dst->debug_level = atoi(level);
125                         return 0;
126                 }
127                 dst = dst->next;
128         }
129
130         return -1;
131 }
132
133
134 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
135                                       const char *txtaddr)
136 {
137         u8 addr[ETH_ALEN];
138         struct sta_info *sta;
139
140         wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
141
142         if (hwaddr_aton(txtaddr, addr))
143                 return -1;
144
145         sta = ap_get_sta(hapd, addr);
146         if (sta)
147                 return 0;
148
149         wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
150                    "notification", MAC2STR(addr));
151         sta = ap_sta_add(hapd, addr);
152         if (sta == NULL)
153                 return -1;
154
155         hostapd_new_assoc_sta(hapd, sta, 0);
156         return 0;
157 }
158
159
160 #ifdef CONFIG_IEEE80211W
161 #ifdef NEED_AP_MLME
162 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
163                                        const char *txtaddr)
164 {
165         u8 addr[ETH_ALEN];
166         u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
167
168         wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
169
170         if (hwaddr_aton(txtaddr, addr) ||
171             os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
172                 return -1;
173
174         ieee802_11_send_sa_query_req(hapd, addr, trans_id);
175
176         return 0;
177 }
178 #endif /* NEED_AP_MLME */
179 #endif /* CONFIG_IEEE80211W */
180
181
182 #ifdef CONFIG_WPS
183 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
184 {
185         char *pin = os_strchr(txt, ' ');
186         char *timeout_txt;
187         int timeout;
188         u8 addr_buf[ETH_ALEN], *addr = NULL;
189         char *pos;
190
191         if (pin == NULL)
192                 return -1;
193         *pin++ = '\0';
194
195         timeout_txt = os_strchr(pin, ' ');
196         if (timeout_txt) {
197                 *timeout_txt++ = '\0';
198                 timeout = atoi(timeout_txt);
199                 pos = os_strchr(timeout_txt, ' ');
200                 if (pos) {
201                         *pos++ = '\0';
202                         if (hwaddr_aton(pos, addr_buf) == 0)
203                                 addr = addr_buf;
204                 }
205         } else
206                 timeout = 0;
207
208         return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
209 }
210
211
212 static int hostapd_ctrl_iface_wps_check_pin(
213         struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
214 {
215         char pin[9];
216         size_t len;
217         char *pos;
218         int ret;
219
220         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
221                               (u8 *) cmd, os_strlen(cmd));
222         for (pos = cmd, len = 0; *pos != '\0'; pos++) {
223                 if (*pos < '0' || *pos > '9')
224                         continue;
225                 pin[len++] = *pos;
226                 if (len == 9) {
227                         wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
228                         return -1;
229                 }
230         }
231         if (len != 4 && len != 8) {
232                 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
233                 return -1;
234         }
235         pin[len] = '\0';
236
237         if (len == 8) {
238                 unsigned int pin_val;
239                 pin_val = atoi(pin);
240                 if (!wps_pin_valid(pin_val)) {
241                         wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
242                         ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
243                         if (ret < 0 || (size_t) ret >= buflen)
244                                 return -1;
245                         return ret;
246                 }
247         }
248
249         ret = os_snprintf(buf, buflen, "%s", pin);
250         if (ret < 0 || (size_t) ret >= buflen)
251                 return -1;
252
253         return ret;
254 }
255
256
257 #ifdef CONFIG_WPS_NFC
258 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
259                                                char *pos)
260 {
261         size_t len;
262         struct wpabuf *buf;
263         int ret;
264
265         len = os_strlen(pos);
266         if (len & 0x01)
267                 return -1;
268         len /= 2;
269
270         buf = wpabuf_alloc(len);
271         if (buf == NULL)
272                 return -1;
273         if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
274                 wpabuf_free(buf);
275                 return -1;
276         }
277
278         ret = hostapd_wps_nfc_tag_read(hapd, buf);
279         wpabuf_free(buf);
280
281         return ret;
282 }
283
284
285 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
286                                                    char *cmd, char *reply,
287                                                    size_t max_len)
288 {
289         int ndef;
290         struct wpabuf *buf;
291         int res;
292
293         if (os_strcmp(cmd, "WPS") == 0)
294                 ndef = 0;
295         else if (os_strcmp(cmd, "NDEF") == 0)
296                 ndef = 1;
297         else
298                 return -1;
299
300         buf = hostapd_wps_nfc_config_token(hapd, ndef);
301         if (buf == NULL)
302                 return -1;
303
304         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305                                          wpabuf_len(buf));
306         reply[res++] = '\n';
307         reply[res] = '\0';
308
309         wpabuf_free(buf);
310
311         return res;
312 }
313
314
315 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
316                                                 char *reply, size_t max_len,
317                                                 int ndef)
318 {
319         struct wpabuf *buf;
320         int res;
321
322         buf = hostapd_wps_nfc_token_gen(hapd, ndef);
323         if (buf == NULL)
324                 return -1;
325
326         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
327                                          wpabuf_len(buf));
328         reply[res++] = '\n';
329         reply[res] = '\0';
330
331         wpabuf_free(buf);
332
333         return res;
334 }
335
336
337 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
338                                             char *cmd, char *reply,
339                                             size_t max_len)
340 {
341         if (os_strcmp(cmd, "WPS") == 0)
342                 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
343                                                             max_len, 0);
344
345         if (os_strcmp(cmd, "NDEF") == 0)
346                 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
347                                                             max_len, 1);
348
349         if (os_strcmp(cmd, "enable") == 0)
350                 return hostapd_wps_nfc_token_enable(hapd);
351
352         if (os_strcmp(cmd, "disable") == 0) {
353                 hostapd_wps_nfc_token_disable(hapd);
354                 return 0;
355         }
356
357         return -1;
358 }
359
360
361 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
362                                                    char *cmd, char *reply,
363                                                    size_t max_len)
364 {
365         struct wpabuf *buf;
366         int res;
367         char *pos;
368         int ndef;
369
370         pos = os_strchr(cmd, ' ');
371         if (pos == NULL)
372                 return -1;
373         *pos++ = '\0';
374
375         if (os_strcmp(cmd, "WPS") == 0)
376                 ndef = 0;
377         else if (os_strcmp(cmd, "NDEF") == 0)
378                 ndef = 1;
379         else
380                 return -1;
381
382         if (os_strcmp(pos, "WPS-CR") == 0)
383                 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
384         else
385                 buf = NULL;
386         if (buf == NULL)
387                 return -1;
388
389         res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
390                                          wpabuf_len(buf));
391         reply[res++] = '\n';
392         reply[res] = '\0';
393
394         wpabuf_free(buf);
395
396         return res;
397 }
398
399
400 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
401                                                   char *cmd)
402 {
403         size_t len;
404         struct wpabuf *req, *sel;
405         int ret;
406         char *pos, *role, *type, *pos2;
407
408         role = cmd;
409         pos = os_strchr(role, ' ');
410         if (pos == NULL)
411                 return -1;
412         *pos++ = '\0';
413
414         type = pos;
415         pos = os_strchr(type, ' ');
416         if (pos == NULL)
417                 return -1;
418         *pos++ = '\0';
419
420         pos2 = os_strchr(pos, ' ');
421         if (pos2 == NULL)
422                 return -1;
423         *pos2++ = '\0';
424
425         len = os_strlen(pos);
426         if (len & 0x01)
427                 return -1;
428         len /= 2;
429
430         req = wpabuf_alloc(len);
431         if (req == NULL)
432                 return -1;
433         if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
434                 wpabuf_free(req);
435                 return -1;
436         }
437
438         len = os_strlen(pos2);
439         if (len & 0x01) {
440                 wpabuf_free(req);
441                 return -1;
442         }
443         len /= 2;
444
445         sel = wpabuf_alloc(len);
446         if (sel == NULL) {
447                 wpabuf_free(req);
448                 return -1;
449         }
450         if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
451                 wpabuf_free(req);
452                 wpabuf_free(sel);
453                 return -1;
454         }
455
456         if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
457                 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
458         } else {
459                 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
460                            "reported: role=%s type=%s", role, type);
461                 ret = -1;
462         }
463         wpabuf_free(req);
464         wpabuf_free(sel);
465
466         return ret;
467 }
468
469 #endif /* CONFIG_WPS_NFC */
470
471
472 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
473                                          char *buf, size_t buflen)
474 {
475         int timeout = 300;
476         char *pos;
477         const char *pin_txt;
478
479         pos = os_strchr(txt, ' ');
480         if (pos)
481                 *pos++ = '\0';
482
483         if (os_strcmp(txt, "disable") == 0) {
484                 hostapd_wps_ap_pin_disable(hapd);
485                 return os_snprintf(buf, buflen, "OK\n");
486         }
487
488         if (os_strcmp(txt, "random") == 0) {
489                 if (pos)
490                         timeout = atoi(pos);
491                 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
492                 if (pin_txt == NULL)
493                         return -1;
494                 return os_snprintf(buf, buflen, "%s", pin_txt);
495         }
496
497         if (os_strcmp(txt, "get") == 0) {
498                 pin_txt = hostapd_wps_ap_pin_get(hapd);
499                 if (pin_txt == NULL)
500                         return -1;
501                 return os_snprintf(buf, buflen, "%s", pin_txt);
502         }
503
504         if (os_strcmp(txt, "set") == 0) {
505                 char *pin;
506                 if (pos == NULL)
507                         return -1;
508                 pin = pos;
509                 pos = os_strchr(pos, ' ');
510                 if (pos) {
511                         *pos++ = '\0';
512                         timeout = atoi(pos);
513                 }
514                 if (os_strlen(pin) > buflen)
515                         return -1;
516                 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
517                         return -1;
518                 return os_snprintf(buf, buflen, "%s", pin);
519         }
520
521         return -1;
522 }
523
524
525 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
526 {
527         char *pos;
528         char *ssid, *auth, *encr = NULL, *key = NULL;
529
530         ssid = txt;
531         pos = os_strchr(txt, ' ');
532         if (!pos)
533                 return -1;
534         *pos++ = '\0';
535
536         auth = pos;
537         pos = os_strchr(pos, ' ');
538         if (pos) {
539                 *pos++ = '\0';
540                 encr = pos;
541                 pos = os_strchr(pos, ' ');
542                 if (pos) {
543                         *pos++ = '\0';
544                         key = pos;
545                 }
546         }
547
548         return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
549 }
550
551
552 static const char * pbc_status_str(enum pbc_status status)
553 {
554         switch (status) {
555         case WPS_PBC_STATUS_DISABLE:
556                 return "Disabled";
557         case WPS_PBC_STATUS_ACTIVE:
558                 return "Active";
559         case WPS_PBC_STATUS_TIMEOUT:
560                 return "Timed-out";
561         case WPS_PBC_STATUS_OVERLAP:
562                 return "Overlap";
563         default:
564                 return "Unknown";
565         }
566 }
567
568
569 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
570                                              char *buf, size_t buflen)
571 {
572         int ret;
573         char *pos, *end;
574
575         pos = buf;
576         end = buf + buflen;
577
578         ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
579                           pbc_status_str(hapd->wps_stats.pbc_status));
580
581         if (ret < 0 || ret >= end - pos)
582                 return pos - buf;
583         pos += ret;
584
585         ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
586                           (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
587                            "Success":
588                            (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
589                             "Failed" : "None")));
590
591         if (ret < 0 || ret >= end - pos)
592                 return pos - buf;
593         pos += ret;
594
595         /* If status == Failure - Add possible Reasons */
596         if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
597            hapd->wps_stats.failure_reason > 0) {
598                 ret = os_snprintf(pos, end - pos,
599                                   "Failure Reason: %s\n",
600                                   wps_ei_str(hapd->wps_stats.failure_reason));
601
602                 if (ret < 0 || ret >= end - pos)
603                         return pos - buf;
604                 pos += ret;
605         }
606
607         if (hapd->wps_stats.status) {
608                 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
609                                   MAC2STR(hapd->wps_stats.peer_addr));
610
611                 if (ret < 0 || ret >= end - pos)
612                         return pos - buf;
613                 pos += ret;
614         }
615
616         return pos - buf;
617 }
618
619 #endif /* CONFIG_WPS */
620
621 #ifdef CONFIG_HS20
622
623 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
624                                              const char *cmd)
625 {
626         u8 addr[ETH_ALEN];
627         const char *url;
628
629         if (hwaddr_aton(cmd, addr))
630                 return -1;
631         url = cmd + 17;
632         if (*url == '\0') {
633                 url = NULL;
634         } else {
635                 if (*url != ' ')
636                         return -1;
637                 url++;
638                 if (*url == '\0')
639                         url = NULL;
640         }
641
642         return hs20_send_wnm_notification(hapd, addr, 1, url);
643 }
644
645 #endif /* CONFIG_HS20 */
646
647
648 #ifdef CONFIG_INTERWORKING
649
650 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
651                                               const char *cmd)
652 {
653         u8 qos_map_set[16 + 2 * 21], count = 0;
654         const char *pos = cmd;
655         int val, ret;
656
657         for (;;) {
658                 if (count == sizeof(qos_map_set)) {
659                         wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
660                         return -1;
661                 }
662
663                 val = atoi(pos);
664                 if (val < 0 || val > 255) {
665                         wpa_printf(MSG_INFO, "Invalid QoS Map Set");
666                         return -1;
667                 }
668
669                 qos_map_set[count++] = val;
670                 pos = os_strchr(pos, ',');
671                 if (!pos)
672                         break;
673                 pos++;
674         }
675
676         if (count < 16 || count & 1) {
677                 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
678                 return -1;
679         }
680
681         ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
682         if (ret) {
683                 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
684                 return -1;
685         }
686
687         os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
688         hapd->conf->qos_map_set_len = count;
689
690         return 0;
691 }
692
693
694 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
695                                                 const char *cmd)
696 {
697         u8 addr[ETH_ALEN];
698         struct sta_info *sta;
699         struct wpabuf *buf;
700         u8 *qos_map_set = hapd->conf->qos_map_set;
701         u8 qos_map_set_len = hapd->conf->qos_map_set_len;
702         int ret;
703
704         if (!qos_map_set_len) {
705                 wpa_printf(MSG_INFO, "QoS Map Set is not set");
706                 return -1;
707         }
708
709         if (hwaddr_aton(cmd, addr))
710                 return -1;
711
712         sta = ap_get_sta(hapd, addr);
713         if (sta == NULL) {
714                 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
715                            "for QoS Map Configuration message",
716                            MAC2STR(addr));
717                 return -1;
718         }
719
720         if (!sta->qos_map_enabled) {
721                 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
722                            "support for QoS Map", MAC2STR(addr));
723                 return -1;
724         }
725
726         buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
727         if (buf == NULL)
728                 return -1;
729
730         wpabuf_put_u8(buf, WLAN_ACTION_QOS);
731         wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
732
733         /* QoS Map Set Element */
734         wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
735         wpabuf_put_u8(buf, qos_map_set_len);
736         wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
737
738         ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
739                                       wpabuf_head(buf), wpabuf_len(buf));
740         wpabuf_free(buf);
741
742         return ret;
743 }
744
745 #endif /* CONFIG_INTERWORKING */
746
747
748 #ifdef CONFIG_WNM
749
750 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
751                                                 const char *cmd)
752 {
753         u8 addr[ETH_ALEN];
754         int disassoc_timer;
755         struct sta_info *sta;
756
757         if (hwaddr_aton(cmd, addr))
758                 return -1;
759         if (cmd[17] != ' ')
760                 return -1;
761         disassoc_timer = atoi(cmd + 17);
762
763         sta = ap_get_sta(hapd, addr);
764         if (sta == NULL) {
765                 wpa_printf(MSG_DEBUG, "Station " MACSTR
766                            " not found for disassociation imminent message",
767                            MAC2STR(addr));
768                 return -1;
769         }
770
771         return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
772 }
773
774
775 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
776                                            const char *cmd)
777 {
778         u8 addr[ETH_ALEN];
779         const char *url, *timerstr;
780         int disassoc_timer;
781         struct sta_info *sta;
782
783         if (hwaddr_aton(cmd, addr))
784                 return -1;
785
786         sta = ap_get_sta(hapd, addr);
787         if (sta == NULL) {
788                 wpa_printf(MSG_DEBUG, "Station " MACSTR
789                            " not found for ESS disassociation imminent message",
790                            MAC2STR(addr));
791                 return -1;
792         }
793
794         timerstr = cmd + 17;
795         if (*timerstr != ' ')
796                 return -1;
797         timerstr++;
798         disassoc_timer = atoi(timerstr);
799         if (disassoc_timer < 0 || disassoc_timer > 65535)
800                 return -1;
801
802         url = os_strchr(timerstr, ' ');
803         if (url == NULL)
804                 return -1;
805         url++;
806
807         return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
808 }
809
810 #endif /* CONFIG_WNM */
811
812
813 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
814                                          char *buf, size_t buflen)
815 {
816         int ret;
817         char *pos, *end;
818
819         pos = buf;
820         end = buf + buflen;
821
822         ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
823                           "ssid=%s\n",
824                           MAC2STR(hapd->own_addr),
825                           wpa_ssid_txt(hapd->conf->ssid.ssid,
826                                        hapd->conf->ssid.ssid_len));
827         if (ret < 0 || ret >= end - pos)
828                 return pos - buf;
829         pos += ret;
830
831 #ifdef CONFIG_WPS
832         ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
833                           hapd->conf->wps_state == 0 ? "disabled" :
834                           (hapd->conf->wps_state == 1 ? "not configured" :
835                            "configured"));
836         if (ret < 0 || ret >= end - pos)
837                 return pos - buf;
838         pos += ret;
839
840         if (hapd->conf->wps_state && hapd->conf->wpa &&
841             hapd->conf->ssid.wpa_passphrase) {
842                 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
843                                   hapd->conf->ssid.wpa_passphrase);
844                 if (ret < 0 || ret >= end - pos)
845                         return pos - buf;
846                 pos += ret;
847         }
848
849         if (hapd->conf->wps_state && hapd->conf->wpa &&
850             hapd->conf->ssid.wpa_psk &&
851             hapd->conf->ssid.wpa_psk->group) {
852                 char hex[PMK_LEN * 2 + 1];
853                 wpa_snprintf_hex(hex, sizeof(hex),
854                                  hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
855                 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
856                 if (ret < 0 || ret >= end - pos)
857                         return pos - buf;
858                 pos += ret;
859         }
860 #endif /* CONFIG_WPS */
861
862         if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
863                 ret = os_snprintf(pos, end - pos, "key_mgmt=");
864                 if (ret < 0 || ret >= end - pos)
865                         return pos - buf;
866                 pos += ret;
867
868                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
869                         ret = os_snprintf(pos, end - pos, "WPA-PSK ");
870                         if (ret < 0 || ret >= end - pos)
871                                 return pos - buf;
872                         pos += ret;
873                 }
874                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
875                         ret = os_snprintf(pos, end - pos, "WPA-EAP ");
876                         if (ret < 0 || ret >= end - pos)
877                                 return pos - buf;
878                         pos += ret;
879                 }
880 #ifdef CONFIG_IEEE80211R
881                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
882                         ret = os_snprintf(pos, end - pos, "FT-PSK ");
883                         if (ret < 0 || ret >= end - pos)
884                                 return pos - buf;
885                         pos += ret;
886                 }
887                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
888                         ret = os_snprintf(pos, end - pos, "FT-EAP ");
889                         if (ret < 0 || ret >= end - pos)
890                                 return pos - buf;
891                         pos += ret;
892                 }
893 #endif /* CONFIG_IEEE80211R */
894 #ifdef CONFIG_IEEE80211W
895                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
896                         ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
897                         if (ret < 0 || ret >= end - pos)
898                                 return pos - buf;
899                         pos += ret;
900                 }
901                 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
902                         ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
903                         if (ret < 0 || ret >= end - pos)
904                                 return pos - buf;
905                         pos += ret;
906                 }
907 #endif /* CONFIG_IEEE80211W */
908
909                 ret = os_snprintf(pos, end - pos, "\n");
910                 if (ret < 0 || ret >= end - pos)
911                         return pos - buf;
912                 pos += ret;
913         }
914
915         if (hapd->conf->wpa) {
916                 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
917                                   wpa_cipher_txt(hapd->conf->wpa_group));
918                 if (ret < 0 || ret >= end - pos)
919                         return pos - buf;
920                 pos += ret;
921         }
922
923         if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
924                 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
925                 if (ret < 0 || ret >= end - pos)
926                         return pos - buf;
927                 pos += ret;
928
929                 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
930                                         " ");
931                 if (ret < 0)
932                         return pos - buf;
933                 pos += ret;
934
935                 ret = os_snprintf(pos, end - pos, "\n");
936                 if (ret < 0 || ret >= end - pos)
937                         return pos - buf;
938                 pos += ret;
939         }
940
941         if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
942                 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
943                 if (ret < 0 || ret >= end - pos)
944                         return pos - buf;
945                 pos += ret;
946
947                 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
948                                         " ");
949                 if (ret < 0)
950                         return pos - buf;
951                 pos += ret;
952
953                 ret = os_snprintf(pos, end - pos, "\n");
954                 if (ret < 0 || ret >= end - pos)
955                         return pos - buf;
956                 pos += ret;
957         }
958
959         return pos - buf;
960 }
961
962
963 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
964 {
965         char *value;
966         int ret = 0;
967
968         value = os_strchr(cmd, ' ');
969         if (value == NULL)
970                 return -1;
971         *value++ = '\0';
972
973         wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
974         if (0) {
975 #ifdef CONFIG_WPS_TESTING
976         } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
977                 long int val;
978                 val = strtol(value, NULL, 0);
979                 if (val < 0 || val > 0xff) {
980                         ret = -1;
981                         wpa_printf(MSG_DEBUG, "WPS: Invalid "
982                                    "wps_version_number %ld", val);
983                 } else {
984                         wps_version_number = val;
985                         wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
986                                    "version %u.%u",
987                                    (wps_version_number & 0xf0) >> 4,
988                                    wps_version_number & 0x0f);
989                         hostapd_wps_update_ie(hapd);
990                 }
991         } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
992                 wps_testing_dummy_cred = atoi(value);
993                 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
994                            wps_testing_dummy_cred);
995         } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
996                 wps_corrupt_pkhash = atoi(value);
997                 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
998                            wps_corrupt_pkhash);
999 #endif /* CONFIG_WPS_TESTING */
1000 #ifdef CONFIG_INTERWORKING
1001         } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1002                 int val = atoi(value);
1003                 if (val <= 0)
1004                         ret = -1;
1005                 else
1006                         hapd->gas_frag_limit = val;
1007 #endif /* CONFIG_INTERWORKING */
1008 #ifdef CONFIG_TESTING_OPTIONS
1009         } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1010                 hapd->ext_mgmt_frame_handling = atoi(value);
1011 #endif /* CONFIG_TESTING_OPTIONS */
1012         } else {
1013                 struct sta_info *sta;
1014                 int vlan_id;
1015
1016                 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1017                 if (ret)
1018                         return ret;
1019
1020                 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1021                         for (sta = hapd->sta_list; sta; sta = sta->next) {
1022                                 if (hostapd_maclist_found(
1023                                             hapd->conf->deny_mac,
1024                                             hapd->conf->num_deny_mac, sta->addr,
1025                                             &vlan_id) &&
1026                                     (!vlan_id || vlan_id == sta->vlan_id))
1027                                         ap_sta_deauthenticate(
1028                                                 hapd, sta,
1029                                                 WLAN_REASON_UNSPECIFIED);
1030                         }
1031                 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1032                            os_strcasecmp(cmd, "accept_mac_file") == 0) {
1033                         for (sta = hapd->sta_list; sta; sta = sta->next) {
1034                                 if (!hostapd_maclist_found(
1035                                             hapd->conf->accept_mac,
1036                                             hapd->conf->num_accept_mac,
1037                                             sta->addr, &vlan_id) ||
1038                                     (vlan_id && vlan_id != sta->vlan_id))
1039                                         ap_sta_deauthenticate(
1040                                                 hapd, sta,
1041                                                 WLAN_REASON_UNSPECIFIED);
1042                         }
1043                 }
1044         }
1045
1046         return ret;
1047 }
1048
1049
1050 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1051                                   char *buf, size_t buflen)
1052 {
1053         int res;
1054
1055         wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1056
1057         if (os_strcmp(cmd, "version") == 0) {
1058                 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1059                 if (res < 0 || (unsigned int) res >= buflen)
1060                         return -1;
1061                 return res;
1062         }
1063
1064         return -1;
1065 }
1066
1067
1068 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1069 {
1070         if (hostapd_enable_iface(iface) < 0) {
1071                 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1072                 return -1;
1073         }
1074         return 0;
1075 }
1076
1077
1078 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1079 {
1080         if (hostapd_reload_iface(iface) < 0) {
1081                 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1082                 return -1;
1083         }
1084         return 0;
1085 }
1086
1087
1088 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1089 {
1090         if (hostapd_disable_iface(iface) < 0) {
1091                 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1092                 return -1;
1093         }
1094         return 0;
1095 }
1096
1097
1098 #ifdef CONFIG_TESTING_OPTIONS
1099
1100 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1101 {
1102         union wpa_event_data data;
1103         char *pos, *param;
1104         enum wpa_event_type event;
1105
1106         wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1107
1108         os_memset(&data, 0, sizeof(data));
1109
1110         param = os_strchr(cmd, ' ');
1111         if (param == NULL)
1112                 return -1;
1113         *param++ = '\0';
1114
1115         if (os_strcmp(cmd, "DETECTED") == 0)
1116                 event = EVENT_DFS_RADAR_DETECTED;
1117         else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1118                 event = EVENT_DFS_CAC_FINISHED;
1119         else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1120                 event = EVENT_DFS_CAC_ABORTED;
1121         else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1122                 event = EVENT_DFS_NOP_FINISHED;
1123         else {
1124                 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1125                            cmd);
1126                 return -1;
1127         }
1128
1129         pos = os_strstr(param, "freq=");
1130         if (pos)
1131                 data.dfs_event.freq = atoi(pos + 5);
1132
1133         pos = os_strstr(param, "ht_enabled=1");
1134         if (pos)
1135                 data.dfs_event.ht_enabled = 1;
1136
1137         pos = os_strstr(param, "chan_offset=");
1138         if (pos)
1139                 data.dfs_event.chan_offset = atoi(pos + 12);
1140
1141         pos = os_strstr(param, "chan_width=");
1142         if (pos)
1143                 data.dfs_event.chan_width = atoi(pos + 11);
1144
1145         pos = os_strstr(param, "cf1=");
1146         if (pos)
1147                 data.dfs_event.cf1 = atoi(pos + 4);
1148
1149         pos = os_strstr(param, "cf2=");
1150         if (pos)
1151                 data.dfs_event.cf2 = atoi(pos + 4);
1152
1153         wpa_supplicant_event(hapd, event, &data);
1154
1155         return 0;
1156 }
1157
1158
1159 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1160 {
1161         size_t len;
1162         u8 *buf;
1163         int res;
1164
1165         wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1166
1167         len = os_strlen(cmd);
1168         if (len & 1)
1169                 return -1;
1170         len /= 2;
1171
1172         buf = os_malloc(len);
1173         if (buf == NULL)
1174                 return -1;
1175
1176         if (hexstr2bin(cmd, buf, len) < 0) {
1177                 os_free(buf);
1178                 return -1;
1179         }
1180
1181         res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1182         os_free(buf);
1183         return res;
1184 }
1185
1186 #endif /* CONFIG_TESTING_OPTIONS */
1187
1188
1189 static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
1190 {
1191 #ifdef NEED_AP_MLME
1192         struct csa_settings settings;
1193         int ret = hostapd_parse_csa_settings(pos, &settings);
1194
1195         if (ret)
1196                 return ret;
1197
1198         return hostapd_switch_channel(hapd, &settings);
1199 #else /* NEED_AP_MLME */
1200         return -1;
1201 #endif /* NEED_AP_MLME */
1202 }
1203
1204
1205 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1206                                   int reply_size, const char *param)
1207 {
1208 #ifdef RADIUS_SERVER
1209         if (os_strcmp(param, "radius_server") == 0) {
1210                 return radius_server_get_mib(hapd->radius_srv, reply,
1211                                              reply_size);
1212         }
1213 #endif /* RADIUS_SERVER */
1214         return -1;
1215 }
1216
1217
1218 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1219                                        void *sock_ctx)
1220 {
1221         struct hostapd_data *hapd = eloop_ctx;
1222         char buf[4096];
1223         int res;
1224         struct sockaddr_un from;
1225         socklen_t fromlen = sizeof(from);
1226         char *reply;
1227         const int reply_size = 4096;
1228         int reply_len;
1229         int level = MSG_DEBUG;
1230
1231         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1232                        (struct sockaddr *) &from, &fromlen);
1233         if (res < 0) {
1234                 perror("recvfrom(ctrl_iface)");
1235                 return;
1236         }
1237         buf[res] = '\0';
1238         if (os_strcmp(buf, "PING") == 0)
1239                 level = MSG_EXCESSIVE;
1240         wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1241
1242         reply = os_malloc(reply_size);
1243         if (reply == NULL) {
1244                 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1245                        fromlen);
1246                 return;
1247         }
1248
1249         os_memcpy(reply, "OK\n", 3);
1250         reply_len = 3;
1251
1252         if (os_strcmp(buf, "PING") == 0) {
1253                 os_memcpy(reply, "PONG\n", 5);
1254                 reply_len = 5;
1255         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1256                 if (wpa_debug_reopen_file() < 0)
1257                         reply_len = -1;
1258         } else if (os_strcmp(buf, "STATUS") == 0) {
1259                 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1260                                                       reply_size);
1261         } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1262                 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1263         } else if (os_strcmp(buf, "MIB") == 0) {
1264                 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1265                 if (reply_len >= 0) {
1266                         res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1267                                           reply_size - reply_len);
1268                         if (res < 0)
1269                                 reply_len = -1;
1270                         else
1271                                 reply_len += res;
1272                 }
1273                 if (reply_len >= 0) {
1274                         res = ieee802_1x_get_mib(hapd, reply + reply_len,
1275                                                  reply_size - reply_len);
1276                         if (res < 0)
1277                                 reply_len = -1;
1278                         else
1279                                 reply_len += res;
1280                 }
1281 #ifndef CONFIG_NO_RADIUS
1282                 if (reply_len >= 0) {
1283                         res = radius_client_get_mib(hapd->radius,
1284                                                     reply + reply_len,
1285                                                     reply_size - reply_len);
1286                         if (res < 0)
1287                                 reply_len = -1;
1288                         else
1289                                 reply_len += res;
1290                 }
1291 #endif /* CONFIG_NO_RADIUS */
1292         } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1293                 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1294                                                    buf + 4);
1295         } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1296                 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1297                                                          reply_size);
1298         } else if (os_strncmp(buf, "STA ", 4) == 0) {
1299                 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1300                                                    reply_size);
1301         } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1302                 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1303                                                         reply_size);
1304         } else if (os_strcmp(buf, "ATTACH") == 0) {
1305                 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1306                         reply_len = -1;
1307         } else if (os_strcmp(buf, "DETACH") == 0) {
1308                 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1309                         reply_len = -1;
1310         } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1311                 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1312                                                     buf + 6))
1313                         reply_len = -1;
1314         } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1315                 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1316                         reply_len = -1;
1317         } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1318                 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1319                         reply_len = -1;
1320         } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1321                 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1322                         reply_len = -1;
1323 #ifdef CONFIG_IEEE80211W
1324 #ifdef NEED_AP_MLME
1325         } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1326                 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1327                         reply_len = -1;
1328 #endif /* NEED_AP_MLME */
1329 #endif /* CONFIG_IEEE80211W */
1330 #ifdef CONFIG_WPS
1331         } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1332                 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1333                         reply_len = -1;
1334         } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1335                 reply_len = hostapd_ctrl_iface_wps_check_pin(
1336                         hapd, buf + 14, reply, reply_size);
1337         } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1338                 if (hostapd_wps_button_pushed(hapd, NULL))
1339                         reply_len = -1;
1340         } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1341                 if (hostapd_wps_cancel(hapd))
1342                         reply_len = -1;
1343         } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1344                 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1345                                                           reply, reply_size);
1346         } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1347                 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1348                         reply_len = -1;
1349         } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1350                 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1351                                                               reply_size);
1352 #ifdef CONFIG_WPS_NFC
1353         } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1354                 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1355                         reply_len = -1;
1356         } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1357                 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1358                         hapd, buf + 21, reply, reply_size);
1359         } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1360                 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1361                         hapd, buf + 14, reply, reply_size);
1362         } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1363                 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1364                         hapd, buf + 21, reply, reply_size);
1365         } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1366                 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1367                         reply_len = -1;
1368 #endif /* CONFIG_WPS_NFC */
1369 #endif /* CONFIG_WPS */
1370 #ifdef CONFIG_INTERWORKING
1371         } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1372                 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
1373                         reply_len = -1;
1374         } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
1375                 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
1376                         reply_len = -1;
1377 #endif /* CONFIG_INTERWORKING */
1378 #ifdef CONFIG_HS20
1379         } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
1380                 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
1381                         reply_len = -1;
1382 #endif /* CONFIG_HS20 */
1383 #ifdef CONFIG_WNM
1384         } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1385                 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1386                         reply_len = -1;
1387         } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1388                 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1389                         reply_len = -1;
1390 #endif /* CONFIG_WNM */
1391         } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1392                 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1393                                                           reply_size);
1394         } else if (os_strncmp(buf, "SET ", 4) == 0) {
1395                 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1396                         reply_len = -1;
1397         } else if (os_strncmp(buf, "GET ", 4) == 0) {
1398                 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1399                                                    reply_size);
1400         } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1401                 if (hostapd_ctrl_iface_enable(hapd->iface))
1402                         reply_len = -1;
1403         } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1404                 if (hostapd_ctrl_iface_reload(hapd->iface))
1405                         reply_len = -1;
1406         } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1407                 if (hostapd_ctrl_iface_disable(hapd->iface))
1408                         reply_len = -1;
1409 #ifdef CONFIG_TESTING_OPTIONS
1410         } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
1411                 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
1412                         reply_len = -1;
1413         } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
1414                 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
1415                         reply_len = -1;
1416 #endif /* CONFIG_TESTING_OPTIONS */
1417         } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
1418                 if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
1419                         reply_len = -1;
1420         } else {
1421                 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1422                 reply_len = 16;
1423         }
1424
1425         if (reply_len < 0) {
1426                 os_memcpy(reply, "FAIL\n", 5);
1427                 reply_len = 5;
1428         }
1429         sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1430         os_free(reply);
1431 }
1432
1433
1434 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1435 {
1436         char *buf;
1437         size_t len;
1438
1439         if (hapd->conf->ctrl_interface == NULL)
1440                 return NULL;
1441
1442         len = os_strlen(hapd->conf->ctrl_interface) +
1443                 os_strlen(hapd->conf->iface) + 2;
1444         buf = os_malloc(len);
1445         if (buf == NULL)
1446                 return NULL;
1447
1448         os_snprintf(buf, len, "%s/%s",
1449                     hapd->conf->ctrl_interface, hapd->conf->iface);
1450         buf[len - 1] = '\0';
1451         return buf;
1452 }
1453
1454
1455 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
1456                                       const char *txt, size_t len)
1457 {
1458         struct hostapd_data *hapd = ctx;
1459         if (hapd == NULL)
1460                 return;
1461         hostapd_ctrl_iface_send(hapd, level, txt, len);
1462 }
1463
1464
1465 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1466 {
1467         struct sockaddr_un addr;
1468         int s = -1;
1469         char *fname = NULL;
1470
1471         if (hapd->ctrl_sock > -1) {
1472                 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1473                 return 0;
1474         }
1475
1476         if (hapd->conf->ctrl_interface == NULL)
1477                 return 0;
1478
1479         if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1480                 if (errno == EEXIST) {
1481                         wpa_printf(MSG_DEBUG, "Using existing control "
1482                                    "interface directory.");
1483                 } else {
1484                         perror("mkdir[ctrl_interface]");
1485                         goto fail;
1486                 }
1487         }
1488
1489         if (hapd->conf->ctrl_interface_gid_set &&
1490             chown(hapd->conf->ctrl_interface, -1,
1491                   hapd->conf->ctrl_interface_gid) < 0) {
1492                 perror("chown[ctrl_interface]");
1493                 return -1;
1494         }
1495
1496         if (!hapd->conf->ctrl_interface_gid_set &&
1497             hapd->iface->interfaces->ctrl_iface_group &&
1498             chown(hapd->conf->ctrl_interface, -1,
1499                   hapd->iface->interfaces->ctrl_iface_group) < 0) {
1500                 perror("chown[ctrl_interface]");
1501                 return -1;
1502         }
1503
1504 #ifdef ANDROID
1505         /*
1506          * Android is using umask 0077 which would leave the control interface
1507          * directory without group access. This breaks things since Wi-Fi
1508          * framework assumes that this directory can be accessed by other
1509          * applications in the wifi group. Fix this by adding group access even
1510          * if umask value would prevent this.
1511          */
1512         if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1513                 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1514                            strerror(errno));
1515                 /* Try to continue anyway */
1516         }
1517 #endif /* ANDROID */
1518
1519         if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1520             os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1521                 goto fail;
1522
1523         s = socket(PF_UNIX, SOCK_DGRAM, 0);
1524         if (s < 0) {
1525                 perror("socket(PF_UNIX)");
1526                 goto fail;
1527         }
1528
1529         os_memset(&addr, 0, sizeof(addr));
1530 #ifdef __FreeBSD__
1531         addr.sun_len = sizeof(addr);
1532 #endif /* __FreeBSD__ */
1533         addr.sun_family = AF_UNIX;
1534         fname = hostapd_ctrl_iface_path(hapd);
1535         if (fname == NULL)
1536                 goto fail;
1537         os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1538         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1539                 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1540                            strerror(errno));
1541                 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1542                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1543                                    " allow connections - assuming it was left"
1544                                    "over from forced program termination");
1545                         if (unlink(fname) < 0) {
1546                                 perror("unlink[ctrl_iface]");
1547                                 wpa_printf(MSG_ERROR, "Could not unlink "
1548                                            "existing ctrl_iface socket '%s'",
1549                                            fname);
1550                                 goto fail;
1551                         }
1552                         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1553                             0) {
1554                                 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
1555                                 goto fail;
1556                         }
1557                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1558                                    "ctrl_iface socket '%s'", fname);
1559                 } else {
1560                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1561                                    "be in use - cannot override it");
1562                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1563                                    "not used anymore", fname);
1564                         os_free(fname);
1565                         fname = NULL;
1566                         goto fail;
1567                 }
1568         }
1569
1570         if (hapd->conf->ctrl_interface_gid_set &&
1571             chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
1572                 perror("chown[ctrl_interface/ifname]");
1573                 goto fail;
1574         }
1575
1576         if (!hapd->conf->ctrl_interface_gid_set &&
1577             hapd->iface->interfaces->ctrl_iface_group &&
1578             chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1579                 perror("chown[ctrl_interface/ifname]");
1580                 goto fail;
1581         }
1582
1583         if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1584                 perror("chmod[ctrl_interface/ifname]");
1585                 goto fail;
1586         }
1587         os_free(fname);
1588
1589         hapd->ctrl_sock = s;
1590         eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1591                                  NULL);
1592         hapd->msg_ctx = hapd;
1593         wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1594
1595         return 0;
1596
1597 fail:
1598         if (s >= 0)
1599                 close(s);
1600         if (fname) {
1601                 unlink(fname);
1602                 os_free(fname);
1603         }
1604         return -1;
1605 }
1606
1607
1608 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1609 {
1610         struct wpa_ctrl_dst *dst, *prev;
1611
1612         if (hapd->ctrl_sock > -1) {
1613                 char *fname;
1614                 eloop_unregister_read_sock(hapd->ctrl_sock);
1615                 close(hapd->ctrl_sock);
1616                 hapd->ctrl_sock = -1;
1617                 fname = hostapd_ctrl_iface_path(hapd);
1618                 if (fname)
1619                         unlink(fname);
1620                 os_free(fname);
1621
1622                 if (hapd->conf->ctrl_interface &&
1623                     rmdir(hapd->conf->ctrl_interface) < 0) {
1624                         if (errno == ENOTEMPTY) {
1625                                 wpa_printf(MSG_DEBUG, "Control interface "
1626                                            "directory not empty - leaving it "
1627                                            "behind");
1628                         } else {
1629                                 wpa_printf(MSG_ERROR,
1630                                            "rmdir[ctrl_interface=%s]: %s",
1631                                            hapd->conf->ctrl_interface,
1632                                            strerror(errno));
1633                         }
1634                 }
1635         }
1636
1637         dst = hapd->ctrl_dst;
1638         while (dst) {
1639                 prev = dst;
1640                 dst = dst->next;
1641                 os_free(prev);
1642         }
1643 }
1644
1645
1646 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1647                                   char *buf)
1648 {
1649         if (hostapd_add_iface(interfaces, buf) < 0) {
1650                 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1651                 return -1;
1652         }
1653         return 0;
1654 }
1655
1656
1657 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1658                                      char *buf)
1659 {
1660         if (hostapd_remove_iface(interfaces, buf) < 0) {
1661                 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1662                 return -1;
1663         }
1664         return 0;
1665 }
1666
1667
1668 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
1669 {
1670 #ifdef CONFIG_WPS_TESTING
1671         wps_version_number = 0x20;
1672         wps_testing_dummy_cred = 0;
1673         wps_corrupt_pkhash = 0;
1674 #endif /* CONFIG_WPS_TESTING */
1675 }
1676
1677
1678 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1679                                               void *sock_ctx)
1680 {
1681         void *interfaces = eloop_ctx;
1682         char buf[256];
1683         int res;
1684         struct sockaddr_un from;
1685         socklen_t fromlen = sizeof(from);
1686         char reply[24];
1687         int reply_len;
1688
1689         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1690                        (struct sockaddr *) &from, &fromlen);
1691         if (res < 0) {
1692                 perror("recvfrom(ctrl_iface)");
1693                 return;
1694         }
1695         buf[res] = '\0';
1696         wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
1697
1698         os_memcpy(reply, "OK\n", 3);
1699         reply_len = 3;
1700
1701         if (os_strcmp(buf, "PING") == 0) {
1702                 os_memcpy(reply, "PONG\n", 5);
1703                 reply_len = 5;
1704         } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1705                 if (wpa_debug_reopen_file() < 0)
1706                         reply_len = -1;
1707         } else if (os_strcmp(buf, "FLUSH") == 0) {
1708                 hostapd_ctrl_iface_flush(interfaces);
1709         } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1710                 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1711                         reply_len = -1;
1712         } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1713                 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1714                         reply_len = -1;
1715 #ifdef CONFIG_MODULE_TESTS
1716         } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
1717                 int hapd_module_tests(void);
1718                 if (hapd_module_tests() < 0)
1719                         reply_len = -1;
1720 #endif /* CONFIG_MODULE_TESTS */
1721         } else {
1722                 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1723                            "ignored");
1724                 reply_len = -1;
1725         }
1726
1727         if (reply_len < 0) {
1728                 os_memcpy(reply, "FAIL\n", 5);
1729                 reply_len = 5;
1730         }
1731
1732         sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1733 }
1734
1735
1736 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1737 {
1738         char *buf;
1739         size_t len;
1740
1741         if (interface->global_iface_path == NULL)
1742                 return NULL;
1743
1744         len = os_strlen(interface->global_iface_path) +
1745                 os_strlen(interface->global_iface_name) + 2;
1746         buf = os_malloc(len);
1747         if (buf == NULL)
1748                 return NULL;
1749
1750         os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1751                     interface->global_iface_name);
1752         buf[len - 1] = '\0';
1753         return buf;
1754 }
1755
1756
1757 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1758 {
1759         struct sockaddr_un addr;
1760         int s = -1;
1761         char *fname = NULL;
1762
1763         if (interface->global_iface_path == NULL) {
1764                 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1765                 return 0;
1766         }
1767
1768         if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1769                 if (errno == EEXIST) {
1770                         wpa_printf(MSG_DEBUG, "Using existing control "
1771                                    "interface directory.");
1772                 } else {
1773                         perror("mkdir[ctrl_interface]");
1774                         goto fail;
1775                 }
1776         } else if (interface->ctrl_iface_group &&
1777                    chown(interface->global_iface_path, -1,
1778                          interface->ctrl_iface_group) < 0) {
1779                 perror("chown[ctrl_interface]");
1780                 goto fail;
1781         }
1782
1783         if (os_strlen(interface->global_iface_path) + 1 +
1784             os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1785                 goto fail;
1786
1787         s = socket(PF_UNIX, SOCK_DGRAM, 0);
1788         if (s < 0) {
1789                 perror("socket(PF_UNIX)");
1790                 goto fail;
1791         }
1792
1793         os_memset(&addr, 0, sizeof(addr));
1794 #ifdef __FreeBSD__
1795         addr.sun_len = sizeof(addr);
1796 #endif /* __FreeBSD__ */
1797         addr.sun_family = AF_UNIX;
1798         fname = hostapd_global_ctrl_iface_path(interface);
1799         if (fname == NULL)
1800                 goto fail;
1801         os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1802         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1803                 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1804                            strerror(errno));
1805                 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1806                         wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1807                                    " allow connections - assuming it was left"
1808                                    "over from forced program termination");
1809                         if (unlink(fname) < 0) {
1810                                 perror("unlink[ctrl_iface]");
1811                                 wpa_printf(MSG_ERROR, "Could not unlink "
1812                                            "existing ctrl_iface socket '%s'",
1813                                            fname);
1814                                 goto fail;
1815                         }
1816                         if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1817                             0) {
1818                                 perror("bind(PF_UNIX)");
1819                                 goto fail;
1820                         }
1821                         wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1822                                    "ctrl_iface socket '%s'", fname);
1823                 } else {
1824                         wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1825                                    "be in use - cannot override it");
1826                         wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1827                                    "not used anymore", fname);
1828                         os_free(fname);
1829                         fname = NULL;
1830                         goto fail;
1831                 }
1832         }
1833
1834         if (interface->ctrl_iface_group &&
1835             chown(fname, -1, interface->ctrl_iface_group) < 0) {
1836                 perror("chown[ctrl_interface]");
1837                 goto fail;
1838         }
1839
1840         if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1841                 perror("chmod[ctrl_interface/ifname]");
1842                 goto fail;
1843         }
1844         os_free(fname);
1845
1846         interface->global_ctrl_sock = s;
1847         eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1848                                  interface, NULL);
1849
1850         return 0;
1851
1852 fail:
1853         if (s >= 0)
1854                 close(s);
1855         if (fname) {
1856                 unlink(fname);
1857                 os_free(fname);
1858         }
1859         return -1;
1860 }
1861
1862
1863 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1864 {
1865         char *fname = NULL;
1866
1867         if (interfaces->global_ctrl_sock > -1) {
1868                 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1869                 close(interfaces->global_ctrl_sock);
1870                 interfaces->global_ctrl_sock = -1;
1871                 fname = hostapd_global_ctrl_iface_path(interfaces);
1872                 if (fname) {
1873                         unlink(fname);
1874                         os_free(fname);
1875                 }
1876
1877                 if (interfaces->global_iface_path &&
1878                     rmdir(interfaces->global_iface_path) < 0) {
1879                         if (errno == ENOTEMPTY) {
1880                                 wpa_printf(MSG_DEBUG, "Control interface "
1881                                            "directory not empty - leaving it "
1882                                            "behind");
1883                         } else {
1884                                 wpa_printf(MSG_ERROR,
1885                                            "rmdir[ctrl_interface=%s]: %s",
1886                                            interfaces->global_iface_path,
1887                                            strerror(errno));
1888                         }
1889                 }
1890                 os_free(interfaces->global_iface_path);
1891                 interfaces->global_iface_path = NULL;
1892         }
1893 }
1894
1895
1896 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1897                                     const char *buf, size_t len)
1898 {
1899         struct wpa_ctrl_dst *dst, *next;
1900         struct msghdr msg;
1901         int idx;
1902         struct iovec io[2];
1903         char levelstr[10];
1904
1905         dst = hapd->ctrl_dst;
1906         if (hapd->ctrl_sock < 0 || dst == NULL)
1907                 return;
1908
1909         os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1910         io[0].iov_base = levelstr;
1911         io[0].iov_len = os_strlen(levelstr);
1912         io[1].iov_base = (char *) buf;
1913         io[1].iov_len = len;
1914         os_memset(&msg, 0, sizeof(msg));
1915         msg.msg_iov = io;
1916         msg.msg_iovlen = 2;
1917
1918         idx = 0;
1919         while (dst) {
1920                 next = dst->next;
1921                 if (level >= dst->debug_level) {
1922                         wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1923                                     (u8 *) dst->addr.sun_path, dst->addrlen -
1924                                     offsetof(struct sockaddr_un, sun_path));
1925                         msg.msg_name = &dst->addr;
1926                         msg.msg_namelen = dst->addrlen;
1927                         if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1928                                 int _errno = errno;
1929                                 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1930                                            "%d - %s",
1931                                            idx, errno, strerror(errno));
1932                                 dst->errors++;
1933                                 if (dst->errors > 10 || _errno == ENOENT) {
1934                                         hostapd_ctrl_iface_detach(
1935                                                 hapd, &dst->addr,
1936                                                 dst->addrlen);
1937                                 }
1938                         } else
1939                                 dst->errors = 0;
1940                 }
1941                 idx++;
1942                 dst = next;
1943         }
1944 }
1945
1946 #endif /* CONFIG_NATIVE_WINDOWS */