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