d5c264831f480ab0f4530ca8633f26a69e2d64f6
[wpasupplicant] / hostapd / hostapd.c
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifndef CONFIG_NATIVE_WINDOWS
17 #include <syslog.h>
18 #endif /* CONFIG_NATIVE_WINDOWS */
19
20 #include "eloop.h"
21 #include "hostapd.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "beacon.h"
25 #include "hw_features.h"
26 #include "accounting.h"
27 #include "eapol_sm.h"
28 #include "iapp.h"
29 #include "ap.h"
30 #include "ieee802_11_auth.h"
31 #include "ap_list.h"
32 #include "sta_info.h"
33 #include "driver_i.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
36 #include "radius/radius.h"
37 #include "wpa.h"
38 #include "preauth.h"
39 #include "wme.h"
40 #include "vlan_init.h"
41 #include "ctrl_iface.h"
42 #include "tls.h"
43 #include "eap_server/eap_sim_db.h"
44 #include "eap_server/eap.h"
45 #include "eap_server/tncs.h"
46 #include "version.h"
47 #include "l2_packet/l2_packet.h"
48 #include "wps_hostapd.h"
49
50
51 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
52                                        size_t identity_len, int phase2,
53                                        struct eap_user *user);
54 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
55 static int hostapd_setup_wpa(struct hostapd_data *hapd);
56 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
57
58 struct hapd_interfaces {
59         size_t count;
60         struct hostapd_iface **iface;
61 };
62
63
64 extern int wpa_debug_level;
65 extern int wpa_debug_show_keys;
66 extern int wpa_debug_timestamp;
67
68
69 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
70                               int level, const char *txt, size_t len)
71 {
72         struct hostapd_data *hapd = ctx;
73         char *format, *module_str;
74         int maxlen;
75         int conf_syslog_level, conf_stdout_level;
76         unsigned int conf_syslog, conf_stdout;
77
78         maxlen = len + 100;
79         format = os_malloc(maxlen);
80         if (!format)
81                 return;
82
83         if (hapd && hapd->conf) {
84                 conf_syslog_level = hapd->conf->logger_syslog_level;
85                 conf_stdout_level = hapd->conf->logger_stdout_level;
86                 conf_syslog = hapd->conf->logger_syslog;
87                 conf_stdout = hapd->conf->logger_stdout;
88         } else {
89                 conf_syslog_level = conf_stdout_level = 0;
90                 conf_syslog = conf_stdout = (unsigned int) -1;
91         }
92
93         switch (module) {
94         case HOSTAPD_MODULE_IEEE80211:
95                 module_str = "IEEE 802.11";
96                 break;
97         case HOSTAPD_MODULE_IEEE8021X:
98                 module_str = "IEEE 802.1X";
99                 break;
100         case HOSTAPD_MODULE_RADIUS:
101                 module_str = "RADIUS";
102                 break;
103         case HOSTAPD_MODULE_WPA:
104                 module_str = "WPA";
105                 break;
106         case HOSTAPD_MODULE_DRIVER:
107                 module_str = "DRIVER";
108                 break;
109         case HOSTAPD_MODULE_IAPP:
110                 module_str = "IAPP";
111                 break;
112         case HOSTAPD_MODULE_MLME:
113                 module_str = "MLME";
114                 break;
115         default:
116                 module_str = NULL;
117                 break;
118         }
119
120         if (hapd && hapd->conf && addr)
121                 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
122                             hapd->conf->iface, MAC2STR(addr),
123                             module_str ? " " : "", module_str, txt);
124         else if (hapd && hapd->conf)
125                 os_snprintf(format, maxlen, "%s:%s%s %s",
126                             hapd->conf->iface, module_str ? " " : "",
127                             module_str, txt);
128         else if (addr)
129                 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
130                             MAC2STR(addr), module_str ? " " : "",
131                             module_str, txt);
132         else
133                 os_snprintf(format, maxlen, "%s%s%s",
134                             module_str, module_str ? ": " : "", txt);
135
136         if ((conf_stdout & module) && level >= conf_stdout_level) {
137                 wpa_debug_print_timestamp();
138                 printf("%s\n", format);
139         }
140
141 #ifndef CONFIG_NATIVE_WINDOWS
142         if ((conf_syslog & module) && level >= conf_syslog_level) {
143                 int priority;
144                 switch (level) {
145                 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
146                 case HOSTAPD_LEVEL_DEBUG:
147                         priority = LOG_DEBUG;
148                         break;
149                 case HOSTAPD_LEVEL_INFO:
150                         priority = LOG_INFO;
151                         break;
152                 case HOSTAPD_LEVEL_NOTICE:
153                         priority = LOG_NOTICE;
154                         break;
155                 case HOSTAPD_LEVEL_WARNING:
156                         priority = LOG_WARNING;
157                         break;
158                 default:
159                         priority = LOG_INFO;
160                         break;
161                 }
162                 syslog(priority, "%s", format);
163         }
164 #endif /* CONFIG_NATIVE_WINDOWS */
165
166         os_free(format);
167 }
168
169
170 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
171 {
172         u8 addr[ETH_ALEN];
173
174         /* New Prism2.5/3 STA firmware versions seem to have issues with this
175          * broadcast deauth frame. This gets the firmware in odd state where
176          * nothing works correctly, so let's skip sending this for the hostap
177          * driver. */
178
179         if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
180                 os_memset(addr, 0xff, ETH_ALEN);
181                 hostapd_sta_deauth(hapd, addr,
182                                    WLAN_REASON_PREV_AUTH_NOT_VALID);
183         }
184 }
185
186
187 /**
188  * hostapd_prune_associations - Remove extraneous associations
189  * @hapd: Pointer to BSS data for the most recent association
190  * @sta: Pointer to the associated STA data
191  *
192  * This function looks through all radios and BSS's for previous
193  * (stale) associations of STA. If any are found they are removed.
194  */
195 static void hostapd_prune_associations(struct hostapd_data *hapd,
196                                        struct sta_info *sta)
197 {
198         struct sta_info *osta;
199         struct hostapd_data *ohapd;
200         size_t i, j;
201         struct hapd_interfaces *interfaces = eloop_get_user_data();
202
203         for (i = 0; i < interfaces->count; i++) {
204                 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
205                         ohapd = interfaces->iface[i]->bss[j];
206                         if (ohapd == hapd)
207                                 continue;
208                         osta = ap_get_sta(ohapd, sta->addr);
209                         if (!osta)
210                                 continue;
211
212                         ap_sta_disassociate(ohapd, osta,
213                                             WLAN_REASON_UNSPECIFIED);
214                 }
215         }
216 }
217
218
219 /**
220  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
221  * @hapd: Pointer to BSS data
222  * @sta: Pointer to the associated STA data
223  * @reassoc: 1 to indicate this was a re-association; 0 = first association
224  *
225  * This function will be called whenever a station associates with the AP. It
226  * can be called for ieee802_11.c for drivers that export MLME to hostapd and
227  * from driver_*.c for drivers that take care of management frames (IEEE 802.11
228  * authentication and association) internally.
229  */
230 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
231                            int reassoc)
232 {
233         if (hapd->tkip_countermeasures) {
234                 hostapd_sta_deauth(hapd, sta->addr,
235                                    WLAN_REASON_MICHAEL_MIC_FAILURE);
236                 return;
237         }
238
239         hostapd_prune_associations(hapd, sta);
240
241         /* IEEE 802.11F (IAPP) */
242         if (hapd->conf->ieee802_11f)
243                 iapp_new_station(hapd->iapp, sta);
244
245         /* Start accounting here, if IEEE 802.1X and WPA are not used.
246          * IEEE 802.1X/WPA code will start accounting after the station has
247          * been authorized. */
248         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
249                 accounting_sta_start(hapd, sta);
250
251         hostapd_wme_sta_config(hapd, sta);
252
253         /* Start IEEE 802.1X authentication process for new stations */
254         ieee802_1x_new_station(hapd, sta);
255         if (reassoc) {
256                 if (sta->auth_alg != WLAN_AUTH_FT &&
257                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
258                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
259         } else
260                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
261 }
262
263
264 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
265                        const u8 *buf, size_t len, int ack)
266 {
267         struct sta_info *sta;
268
269         sta = ap_get_sta(hapd, addr);
270         if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
271                 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
272                            "activity poll", MAC2STR(sta->addr),
273                            ack ? "ACKed" : "did not ACK");
274                 if (ack)
275                         sta->flags &= ~WLAN_STA_PENDING_POLL;
276         }
277         if (sta)
278                 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
279 }
280
281
282 void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *addr)
283 {
284         struct sta_info *sta;
285
286         sta = ap_get_sta(hapd, addr);
287         if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
288                 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
289                            "STA " MACSTR, MAC2STR(addr));
290                 if (sta && (sta->flags & WLAN_STA_AUTH))
291                         hostapd_sta_disassoc(
292                                 hapd, addr,
293                                 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
294                 else
295                         hostapd_sta_deauth(
296                                 hapd, addr,
297                                 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
298         }
299 }
300
301
302 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
303                         const u8 *ie, size_t ielen)
304 {
305         struct sta_info *sta;
306         int new_assoc, res;
307
308         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
309                        HOSTAPD_LEVEL_INFO, "associated");
310
311         sta = ap_get_sta(hapd, addr);
312         if (sta) {
313                 accounting_sta_stop(hapd, sta);
314         } else {
315                 sta = ap_sta_add(hapd, addr);
316                 if (sta == NULL)
317                         return -1;
318         }
319         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
320
321         if (hapd->conf->wpa) {
322                 if (ie == NULL || ielen == 0) {
323                         if (hapd->conf->wps_state) {
324                                 wpa_printf(MSG_DEBUG, "STA did not include "
325                                            "WPA/RSN IE in (Re)Association "
326                                            "Request - possible WPS use");
327                                 sta->flags |= WLAN_STA_MAYBE_WPS;
328                                 goto skip_wpa_check;
329                         }
330
331                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
332                         return -1;
333                 }
334                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
335                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
336                         sta->flags |= WLAN_STA_WPS;
337                         goto skip_wpa_check;
338                 }
339
340                 if (sta->wpa_sm == NULL)
341                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
342                                                         sta->addr);
343                 if (sta->wpa_sm == NULL) {
344                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
345                                    "machine");
346                         return -1;
347                 }
348                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
349                                           ie, ielen, NULL, 0);
350                 if (res != WPA_IE_OK) {
351                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
352                                    "rejected? (res %u)", res);
353                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
354                         return -1;
355                 }
356         }
357 skip_wpa_check:
358
359         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
360         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
361         wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
362
363         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
364
365         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
366
367         return 0;
368 }
369
370
371 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
372 {
373         struct sta_info *sta;
374
375         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
376                        HOSTAPD_LEVEL_INFO, "disassociated");
377
378         sta = ap_get_sta(hapd, addr);
379         if (sta == NULL) {
380                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
381                            "unknown STA " MACSTR, MAC2STR(addr));
382                 return;
383         }
384
385         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
386         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
387         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
388         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
389         ap_free_sta(hapd, sta);
390 }
391
392
393 #ifdef EAP_SERVER
394 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
395                                  struct sta_info *sta, void *ctx)
396 {
397         if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
398                 return 1;
399         return 0;
400 }
401
402
403 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
404 {
405         struct hostapd_data *hapd = ctx;
406         if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
407                 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
408 }
409 #endif /* EAP_SERVER */
410
411
412 /**
413  * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
414  */
415 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
416 {
417         wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
418         eloop_terminate();
419 }
420
421
422 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
423                                   struct wpa_auth_config *wconf)
424 {
425         wconf->wpa = conf->wpa;
426         wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
427         wconf->wpa_pairwise = conf->wpa_pairwise;
428         wconf->wpa_group = conf->wpa_group;
429         wconf->wpa_group_rekey = conf->wpa_group_rekey;
430         wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
431         wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
432         wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
433         wconf->rsn_pairwise = conf->rsn_pairwise;
434         wconf->rsn_preauth = conf->rsn_preauth;
435         wconf->eapol_version = conf->eapol_version;
436         wconf->peerkey = conf->peerkey;
437         wconf->wme_enabled = conf->wme_enabled;
438         wconf->okc = conf->okc;
439 #ifdef CONFIG_IEEE80211W
440         wconf->ieee80211w = conf->ieee80211w;
441 #endif /* CONFIG_IEEE80211W */
442 #ifdef CONFIG_IEEE80211R
443         wconf->ssid_len = conf->ssid.ssid_len;
444         if (wconf->ssid_len > SSID_LEN)
445                 wconf->ssid_len = SSID_LEN;
446         os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
447         os_memcpy(wconf->mobility_domain, conf->mobility_domain,
448                   MOBILITY_DOMAIN_ID_LEN);
449         if (conf->nas_identifier &&
450             os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
451                 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
452                 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
453                           wconf->r0_key_holder_len);
454         }
455         os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
456         wconf->r0_key_lifetime = conf->r0_key_lifetime;
457         wconf->reassociation_deadline = conf->reassociation_deadline;
458         wconf->r0kh_list = conf->r0kh_list;
459         wconf->r1kh_list = conf->r1kh_list;
460         wconf->pmk_r1_push = conf->pmk_r1_push;
461 #endif /* CONFIG_IEEE80211R */
462 }
463
464
465 int hostapd_reload_config(struct hostapd_iface *iface)
466 {
467         struct hostapd_data *hapd = iface->bss[0];
468         struct hostapd_config *newconf, *oldconf;
469         struct wpa_auth_config wpa_auth_conf;
470
471         newconf = hostapd_config_read(iface->config_fname);
472         if (newconf == NULL)
473                 return -1;
474
475         /*
476          * Deauthenticate all stations since the new configuration may not
477          * allow them to use the BSS anymore.
478          */
479         hostapd_flush_old_stations(hapd);
480
481         /* TODO: update dynamic data based on changed configuration
482          * items (e.g., open/close sockets, etc.) */
483         radius_client_flush(hapd->radius, 0);
484
485         oldconf = hapd->iconf;
486         hapd->iconf = newconf;
487         hapd->conf = &newconf->bss[0];
488         iface->conf = newconf;
489
490         if (hostapd_setup_wpa_psk(hapd->conf)) {
491                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
492                            "after reloading configuration");
493         }
494
495         if (hapd->conf->wpa && hapd->wpa_auth == NULL)
496                 hostapd_setup_wpa(hapd);
497         else if (hapd->conf->wpa) {
498                 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
499                 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
500         } else if (hapd->wpa_auth) {
501                 wpa_deinit(hapd->wpa_auth);
502                 hapd->wpa_auth = NULL;
503                 hostapd_set_privacy(hapd, 0);
504                 hostapd_setup_encryption(hapd->conf->iface, hapd);
505         }
506
507         ieee802_11_set_beacon(hapd);
508
509         hostapd_config_free(oldconf);
510
511         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
512
513         return 0;
514 }
515
516
517 #ifndef CONFIG_NATIVE_WINDOWS
518 /**
519  * handle_reload - SIGHUP handler to reload configuration
520  */
521 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
522 {
523         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
524         size_t i;
525
526         wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
527                    sig);
528
529         for (i = 0; i < hapds->count; i++) {
530                 if (hostapd_reload_config(hapds->iface[i]) < 0) {
531                         wpa_printf(MSG_WARNING, "Failed to read new "
532                                    "configuration file - continuing with "
533                                    "old.");
534                         continue;
535                 }
536         }
537 }
538
539
540 #ifdef HOSTAPD_DUMP_STATE
541 /**
542  * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
543  */
544 static void hostapd_dump_state(struct hostapd_data *hapd)
545 {
546         FILE *f;
547         time_t now;
548         struct sta_info *sta;
549         int i;
550         char *buf;
551
552         if (!hapd->conf->dump_log_name) {
553                 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
554                            "request");
555                 return;
556         }
557
558         wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
559                    hapd->conf->dump_log_name);
560         f = fopen(hapd->conf->dump_log_name, "w");
561         if (f == NULL) {
562                 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
563                            "writing.", hapd->conf->dump_log_name);
564                 return;
565         }
566
567         time(&now);
568         fprintf(f, "hostapd state dump - %s", ctime(&now));
569         fprintf(f, "num_sta=%d num_sta_non_erp=%d "
570                 "num_sta_no_short_slot_time=%d\n"
571                 "num_sta_no_short_preamble=%d\n",
572                 hapd->num_sta, hapd->iface->num_sta_non_erp,
573                 hapd->iface->num_sta_no_short_slot_time,
574                 hapd->iface->num_sta_no_short_preamble);
575
576         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
577                 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
578
579                 fprintf(f,
580                         "  AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
581                         "  capability=0x%x listen_interval=%d\n",
582                         sta->aid,
583                         sta->flags,
584                         (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
585                         (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
586                         (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
587                         (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
588                         (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
589                         (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
590                          ""),
591                         (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
592                          ""),
593                         (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
594                          "[SHORT_PREAMBLE]" : ""),
595                         (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
596                         (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
597                         (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
598                         (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
599                         (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
600                         (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
601                         sta->capability,
602                         sta->listen_interval);
603
604                 fprintf(f, "  supported_rates=");
605                 for (i = 0; i < sta->supported_rates_len; i++)
606                         fprintf(f, "%02x ", sta->supported_rates[i]);
607                 fprintf(f, "\n");
608
609                 fprintf(f,
610                         "  timeout_next=%s\n",
611                         (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
612                          (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
613                           "DEAUTH")));
614
615                 ieee802_1x_dump_state(f, "  ", sta);
616         }
617
618         buf = os_malloc(4096);
619         if (buf) {
620                 int count = radius_client_get_mib(hapd->radius, buf, 4096);
621                 if (count < 0)
622                         count = 0;
623                 else if (count > 4095)
624                         count = 4095;
625                 buf[count] = '\0';
626                 fprintf(f, "%s", buf);
627
628                 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
629                 if (count < 0)
630                         count = 0;
631                 else if (count > 4095)
632                         count = 4095;
633                 buf[count] = '\0';
634                 fprintf(f, "%s", buf);
635                 os_free(buf);
636         }
637         fclose(f);
638 }
639 #endif /* HOSTAPD_DUMP_STATE */
640
641
642 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
643 {
644 #ifdef HOSTAPD_DUMP_STATE
645         struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
646         size_t i, j;
647
648         for (i = 0; i < hapds->count; i++) {
649                 struct hostapd_iface *hapd_iface = hapds->iface[i];
650                 for (j = 0; j < hapd_iface->num_bss; j++)
651                         hostapd_dump_state(hapd_iface->bss[j]);
652         }
653 #endif /* HOSTAPD_DUMP_STATE */
654 }
655 #endif /* CONFIG_NATIVE_WINDOWS */
656
657 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
658                                               char *ifname)
659 {
660         int i;
661
662         for (i = 0; i < NUM_WEP_KEYS; i++) {
663                 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
664                                            0, i == 0 ? 1 : 0)) {
665                         wpa_printf(MSG_DEBUG, "Failed to clear default "
666                                    "encryption keys (ifname=%s keyidx=%d)",
667                                    ifname, i);
668                 }
669         }
670 #ifdef CONFIG_IEEE80211W
671         if (hapd->conf->ieee80211w) {
672                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
673                         if (hostapd_set_encryption(ifname, hapd, "none", NULL,
674                                                    i, NULL, 0,
675                                                    i == 0 ? 1 : 0)) {
676                                 wpa_printf(MSG_DEBUG, "Failed to clear "
677                                            "default mgmt encryption keys "
678                                            "(ifname=%s keyidx=%d)", ifname, i);
679                         }
680                 }
681         }
682 #endif /* CONFIG_IEEE80211W */
683 }
684
685
686 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
687 {
688         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
689         return 0;
690 }
691
692
693 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
694 {
695         int errors = 0, idx;
696         struct hostapd_ssid *ssid = &hapd->conf->ssid;
697
698         idx = ssid->wep.idx;
699         if (ssid->wep.default_len &&
700             hostapd_set_encryption(hapd->conf->iface,
701                                    hapd, "WEP", NULL, idx,
702                                    ssid->wep.key[idx],
703                                    ssid->wep.len[idx],
704                                    idx == ssid->wep.idx)) {
705                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
706                 errors++;
707         }
708
709         if (ssid->dyn_vlan_keys) {
710                 size_t i;
711                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
712                         const char *ifname;
713                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
714                         if (key == NULL)
715                                 continue;
716                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
717                                                             i);
718                         if (ifname == NULL)
719                                 continue;
720
721                         idx = key->idx;
722                         if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
723                                                    idx, key->key[idx],
724                                                    key->len[idx],
725                                                    idx == key->idx)) {
726                                 wpa_printf(MSG_WARNING, "Could not set "
727                                            "dynamic VLAN WEP encryption.");
728                                 errors++;
729                         }
730                 }
731         }
732
733         return errors;
734 }
735
736 /**
737  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
738  * @hapd: Pointer to BSS data
739  *
740  * This function is used to free all per-BSS data structures and resources.
741  * This gets called in a loop for each BSS between calls to
742  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
743  * is deinitialized. Most of the modules that are initialized in
744  * hostapd_setup_bss() are deinitialized here.
745  */
746 static void hostapd_cleanup(struct hostapd_data *hapd)
747 {
748         hostapd_ctrl_iface_deinit(hapd);
749
750         os_free(hapd->default_wep_key);
751         hapd->default_wep_key = NULL;
752         iapp_deinit(hapd->iapp);
753         hapd->iapp = NULL;
754         accounting_deinit(hapd);
755         rsn_preauth_iface_deinit(hapd);
756         if (hapd->wpa_auth) {
757                 wpa_deinit(hapd->wpa_auth);
758                 hapd->wpa_auth = NULL;
759
760                 if (hostapd_set_privacy(hapd, 0)) {
761                         wpa_printf(MSG_DEBUG, "Could not disable "
762                                    "PrivacyInvoked for interface %s",
763                                    hapd->conf->iface);
764                 }
765
766                 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
767                         wpa_printf(MSG_DEBUG, "Could not remove generic "
768                                    "information element from interface %s",
769                                    hapd->conf->iface);
770                 }
771         }
772         ieee802_1x_deinit(hapd);
773         vlan_deinit(hapd);
774         hostapd_acl_deinit(hapd);
775         radius_client_deinit(hapd->radius);
776         hapd->radius = NULL;
777         radius_server_deinit(hapd->radius_srv);
778         hapd->radius_srv = NULL;
779
780 #ifdef CONFIG_IEEE80211R
781         l2_packet_deinit(hapd->l2);
782 #endif /* CONFIG_IEEE80211R */
783
784         hostapd_deinit_wps(hapd);
785
786         hostapd_wireless_event_deinit(hapd);
787
788 #ifdef EAP_TLS_FUNCS
789         if (hapd->ssl_ctx) {
790                 tls_deinit(hapd->ssl_ctx);
791                 hapd->ssl_ctx = NULL;
792         }
793 #endif /* EAP_TLS_FUNCS */
794
795 #ifdef EAP_SERVER
796         if (hapd->eap_sim_db_priv) {
797                 eap_sim_db_deinit(hapd->eap_sim_db_priv);
798                 hapd->eap_sim_db_priv = NULL;
799         }
800 #endif /* EAP_SERVER */
801
802         if (hapd->interface_added &&
803             hostapd_bss_remove(hapd, hapd->conf->iface)) {
804                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
805                            hapd->conf->iface);
806         }
807 }
808
809
810 /**
811  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
812  * @iface: Pointer to interface data
813  *
814  * This function is called before per-BSS data structures are deinitialized
815  * with hostapd_cleanup().
816  */
817 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
818 {
819 }
820
821
822 /**
823  * hostapd_cleanup_iface - Complete per-interface cleanup
824  * @iface: Pointer to interface data
825  *
826  * This function is called after per-BSS data structures are deinitialized
827  * with hostapd_cleanup().
828  */
829 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
830 {
831         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
832         iface->hw_features = NULL;
833         os_free(iface->current_rates);
834         iface->current_rates = NULL;
835         ap_list_deinit(iface);
836         hostapd_config_free(iface->conf);
837         iface->conf = NULL;
838
839         os_free(iface->config_fname);
840         os_free(iface->bss);
841         os_free(iface);
842 }
843
844
845 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
846 {
847         int i;
848
849         hostapd_broadcast_wep_set(hapd);
850
851         if (hapd->conf->ssid.wep.default_len)
852                 return 0;
853
854         for (i = 0; i < 4; i++) {
855                 if (hapd->conf->ssid.wep.key[i] &&
856                     hostapd_set_encryption(iface, hapd, "WEP", NULL,
857                                            i, hapd->conf->ssid.wep.key[i],
858                                            hapd->conf->ssid.wep.len[i],
859                                            i == hapd->conf->ssid.wep.idx)) {
860                         wpa_printf(MSG_WARNING, "Could not set WEP "
861                                    "encryption.");
862                         return -1;
863                 }
864                 if (hapd->conf->ssid.wep.key[i] &&
865                     i == hapd->conf->ssid.wep.idx)
866                         hostapd_set_privacy(hapd, 1);
867         }
868
869         return 0;
870 }
871
872
873 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
874 {
875         int ret = 0;
876
877         if (hostapd_drv_none(hapd))
878                 return 0;
879
880         wpa_printf(MSG_DEBUG, "Flushing old station entries");
881         if (hostapd_flush(hapd)) {
882                 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
883                 ret = -1;
884         }
885         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
886         hostapd_deauth_all_stas(hapd);
887
888         return ret;
889 }
890
891
892 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
893                                     logger_level level, const char *txt)
894 {
895         struct hostapd_data *hapd = ctx;
896         int hlevel;
897
898         switch (level) {
899         case LOGGER_WARNING:
900                 hlevel = HOSTAPD_LEVEL_WARNING;
901                 break;
902         case LOGGER_INFO:
903                 hlevel = HOSTAPD_LEVEL_INFO;
904                 break;
905         case LOGGER_DEBUG:
906         default:
907                 hlevel = HOSTAPD_LEVEL_DEBUG;
908                 break;
909         }
910
911         hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
912 }
913
914
915 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
916                                         u16 reason)
917 {
918         struct hostapd_data *hapd = ctx;
919         struct sta_info *sta;
920
921         wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
922                    "STA " MACSTR " reason %d",
923                    __func__, MAC2STR(addr), reason);
924
925         sta = ap_get_sta(hapd, addr);
926         hostapd_sta_deauth(hapd, addr, reason);
927         if (sta == NULL)
928                 return;
929         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
930         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
931         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
932         sta->timeout_next = STA_REMOVE;
933 }
934
935
936 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
937 {
938         struct hostapd_data *hapd = ctx;
939         ieee80211_michael_mic_failure(hapd, addr, 0);
940 }
941
942
943 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
944                                        wpa_eapol_variable var, int value)
945 {
946         struct hostapd_data *hapd = ctx;
947         struct sta_info *sta = ap_get_sta(hapd, addr);
948         if (sta == NULL)
949                 return;
950         switch (var) {
951         case WPA_EAPOL_portEnabled:
952                 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
953                 break;
954         case WPA_EAPOL_portValid:
955                 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
956                 break;
957         case WPA_EAPOL_authorized:
958                 ieee802_1x_set_sta_authorized(hapd, sta, value);
959                 break;
960         case WPA_EAPOL_portControl_Auto:
961                 if (sta->eapol_sm)
962                         sta->eapol_sm->portControl = Auto;
963                 break;
964         case WPA_EAPOL_keyRun:
965                 if (sta->eapol_sm)
966                         sta->eapol_sm->keyRun = value ? TRUE : FALSE;
967                 break;
968         case WPA_EAPOL_keyAvailable:
969                 if (sta->eapol_sm)
970                         sta->eapol_sm->eap_if->eapKeyAvailable =
971                                 value ? TRUE : FALSE;
972                 break;
973         case WPA_EAPOL_keyDone:
974                 if (sta->eapol_sm)
975                         sta->eapol_sm->keyDone = value ? TRUE : FALSE;
976                 break;
977         case WPA_EAPOL_inc_EapolFramesTx:
978                 if (sta->eapol_sm)
979                         sta->eapol_sm->dot1xAuthEapolFramesTx++;
980                 break;
981         }
982 }
983
984
985 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
986                                       wpa_eapol_variable var)
987 {
988         struct hostapd_data *hapd = ctx;
989         struct sta_info *sta = ap_get_sta(hapd, addr);
990         if (sta == NULL || sta->eapol_sm == NULL)
991                 return -1;
992         switch (var) {
993         case WPA_EAPOL_keyRun:
994                 return sta->eapol_sm->keyRun;
995         case WPA_EAPOL_keyAvailable:
996                 return sta->eapol_sm->eap_if->eapKeyAvailable;
997         default:
998                 return -1;
999         }
1000 }
1001
1002
1003 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
1004                                            const u8 *prev_psk)
1005 {
1006         struct hostapd_data *hapd = ctx;
1007         return hostapd_get_psk(hapd->conf, addr, prev_psk);
1008 }
1009
1010
1011 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
1012                                     size_t *len)
1013 {
1014         struct hostapd_data *hapd = ctx;
1015         const u8 *key;
1016         size_t keylen;
1017         struct sta_info *sta;
1018
1019         sta = ap_get_sta(hapd, addr);
1020         if (sta == NULL)
1021                 return -1;
1022
1023         key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
1024         if (key == NULL)
1025                 return -1;
1026
1027         if (keylen > *len)
1028                 keylen = *len;
1029         os_memcpy(msk, key, keylen);
1030         *len = keylen;
1031
1032         return 0;
1033 }
1034
1035
1036 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
1037                                     const u8 *addr, int idx, u8 *key,
1038                                     size_t key_len)
1039 {
1040         struct hostapd_data *hapd = ctx;
1041         const char *ifname = hapd->conf->iface;
1042
1043         if (vlan_id > 0) {
1044                 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
1045                 if (ifname == NULL)
1046                         return -1;
1047         }
1048
1049         return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
1050                                       key, key_len, 1);
1051 }
1052
1053
1054 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
1055                                        u8 *seq)
1056 {
1057         struct hostapd_data *hapd = ctx;
1058         return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
1059 }
1060
1061
1062 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
1063                                             u8 *seq)
1064 {
1065         struct hostapd_data *hapd = ctx;
1066         return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
1067                                        seq);
1068 }
1069
1070
1071 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
1072                                        const u8 *data, size_t data_len,
1073                                        int encrypt)
1074 {
1075         struct hostapd_data *hapd = ctx;
1076         return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
1077 }
1078
1079
1080 static int hostapd_wpa_auth_for_each_sta(
1081         void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
1082         void *cb_ctx)
1083 {
1084         struct hostapd_data *hapd = ctx;
1085         struct sta_info *sta;
1086
1087         for (sta = hapd->sta_list; sta; sta = sta->next) {
1088                 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
1089                         return 1;
1090         }
1091         return 0;
1092 }
1093
1094
1095 static int hostapd_wpa_auth_for_each_auth(
1096         void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
1097         void *cb_ctx)
1098 {
1099         struct hostapd_data *ohapd;
1100         size_t i, j;
1101         struct hapd_interfaces *interfaces = eloop_get_user_data();
1102
1103         for (i = 0; i < interfaces->count; i++) {
1104                 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
1105                         ohapd = interfaces->iface[i]->bss[j];
1106                         if (cb(ohapd->wpa_auth, cb_ctx))
1107                                 return 1;
1108                 }
1109         }
1110
1111         return 0;
1112 }
1113
1114
1115 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
1116                                        const u8 *data, size_t data_len)
1117 {
1118         struct hostapd_data *hapd = ctx;
1119
1120         if (hapd->driver && hapd->driver->send_ether)
1121                 return hapd->driver->send_ether(hapd->drv_priv, dst,
1122                                                 hapd->own_addr, proto,
1123                                                 data, data_len);
1124         if (hapd->l2 == NULL)
1125                 return -1;
1126         return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1127 }
1128
1129
1130 #ifdef CONFIG_IEEE80211R
1131
1132 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1133                                            const u8 *data, size_t data_len)
1134 {
1135         struct hostapd_data *hapd = ctx;
1136         int res;
1137         struct ieee80211_mgmt *m;
1138         size_t mlen;
1139         struct sta_info *sta;
1140
1141         sta = ap_get_sta(hapd, dst);
1142         if (sta == NULL || sta->wpa_sm == NULL)
1143                 return -1;
1144
1145         m = os_zalloc(sizeof(*m) + data_len);
1146         if (m == NULL)
1147                 return -1;
1148         mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1149         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1150                                         WLAN_FC_STYPE_ACTION);
1151         os_memcpy(m->da, dst, ETH_ALEN);
1152         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1153         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1154         os_memcpy(&m->u, data, data_len);
1155
1156         res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1157         os_free(m);
1158         return res;
1159 }
1160
1161
1162 static struct wpa_state_machine *
1163 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1164 {
1165         struct hostapd_data *hapd = ctx;
1166         struct sta_info *sta;
1167
1168         sta = ap_sta_add(hapd, sta_addr);
1169         if (sta == NULL)
1170                 return NULL;
1171         if (sta->wpa_sm)
1172                 return sta->wpa_sm;
1173
1174         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1175         if (sta->wpa_sm == NULL) {
1176                 ap_free_sta(hapd, sta);
1177                 return NULL;
1178         }
1179         sta->auth_alg = WLAN_AUTH_FT;
1180
1181         return sta->wpa_sm;
1182 }
1183
1184
1185 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1186                                 size_t len)
1187 {
1188         struct hostapd_data *hapd = ctx;
1189         wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1190 }
1191
1192 #endif /* CONFIG_IEEE80211R */
1193
1194
1195 /**
1196  * hostapd_validate_bssid_configuration - Validate BSSID configuration
1197  * @iface: Pointer to interface data
1198  * Returns: 0 on success, -1 on failure
1199  *
1200  * This function is used to validate that the configured BSSIDs are valid.
1201  */
1202 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1203 {
1204         u8 mask[ETH_ALEN] = { 0 };
1205         struct hostapd_data *hapd = iface->bss[0];
1206         unsigned int i = iface->conf->num_bss, bits = 0, j;
1207         int res;
1208
1209         if (hostapd_drv_none(hapd))
1210                 return 0;
1211
1212         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1213
1214         /* Determine the bits necessary to cover the number of BSSIDs. */
1215         for (i--; i; i >>= 1)
1216                 bits++;
1217
1218         /* Determine the bits necessary to any configured BSSIDs,
1219            if they are higher than the number of BSSIDs. */
1220         for (j = 0; j < iface->conf->num_bss; j++) {
1221                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1222                         continue;
1223
1224                 for (i = 0; i < ETH_ALEN; i++) {
1225                         mask[i] |=
1226                                 iface->conf->bss[j].bssid[i] ^
1227                                 hapd->own_addr[i];
1228                 }
1229         }
1230
1231         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1232                 ;
1233         j = 0;
1234         if (i < ETH_ALEN) {
1235                 j = (5 - i) * 8;
1236
1237                 while (mask[i] != 0) {
1238                         mask[i] >>= 1;
1239                         j++;
1240                 }
1241         }
1242
1243         if (bits < j)
1244                 bits = j;
1245
1246         if (bits > 40)
1247                 return -1;
1248
1249         os_memset(mask, 0xff, ETH_ALEN);
1250         j = bits / 8;
1251         for (i = 5; i > 5 - j; i--)
1252                 mask[i] = 0;
1253         j = bits % 8;
1254         while (j--)
1255                 mask[i] <<= 1;
1256
1257         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1258                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1259
1260         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1261         if (res == 0)
1262                 return 0;
1263
1264         if (res < 0) {
1265                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1266                            MACSTR " for start address " MACSTR ".",
1267                            MAC2STR(mask), MAC2STR(hapd->own_addr));
1268                 return -1;
1269         }
1270
1271         for (i = 0; i < ETH_ALEN; i++) {
1272                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1273                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1274                                    " for start address " MACSTR ".",
1275                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
1276                         wpa_printf(MSG_ERROR, "Start address must be the "
1277                                    "first address in the block (i.e., addr "
1278                                    "AND mask == addr).");
1279                         return -1;
1280                 }
1281         }
1282
1283         return 0;
1284 }
1285
1286
1287 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1288 {
1289         size_t i;
1290
1291         for (i = 0; i < conf->num_bss; i++) {
1292                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1293                         return 1;
1294                 }
1295         }
1296
1297         return 0;
1298 }
1299
1300
1301 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1302 {
1303         struct wpa_auth_config _conf;
1304         struct wpa_auth_callbacks cb;
1305         const u8 *wpa_ie;
1306         size_t wpa_ie_len;
1307
1308         hostapd_wpa_auth_conf(hapd->conf, &_conf);
1309         os_memset(&cb, 0, sizeof(cb));
1310         cb.ctx = hapd;
1311         cb.logger = hostapd_wpa_auth_logger;
1312         cb.disconnect = hostapd_wpa_auth_disconnect;
1313         cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1314         cb.set_eapol = hostapd_wpa_auth_set_eapol;
1315         cb.get_eapol = hostapd_wpa_auth_get_eapol;
1316         cb.get_psk = hostapd_wpa_auth_get_psk;
1317         cb.get_msk = hostapd_wpa_auth_get_msk;
1318         cb.set_key = hostapd_wpa_auth_set_key;
1319         cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1320         cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1321         cb.send_eapol = hostapd_wpa_auth_send_eapol;
1322         cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1323         cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1324         cb.send_ether = hostapd_wpa_auth_send_ether;
1325 #ifdef CONFIG_IEEE80211R
1326         cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1327         cb.add_sta = hostapd_wpa_auth_add_sta;
1328 #endif /* CONFIG_IEEE80211R */
1329         hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1330         if (hapd->wpa_auth == NULL) {
1331                 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1332                 return -1;
1333         }
1334
1335         if (hostapd_set_privacy(hapd, 1)) {
1336                 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1337                            "for interface %s", hapd->conf->iface);
1338                 return -1;
1339         }
1340
1341         wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1342         if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1343                 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1344                            "the kernel driver.");
1345                 return -1;
1346         }
1347
1348         if (rsn_preauth_iface_init(hapd)) {
1349                 wpa_printf(MSG_ERROR, "Initialization of RSN "
1350                            "pre-authentication failed.");
1351                 return -1;
1352         }
1353
1354         return 0;
1355
1356 }
1357
1358
1359 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1360                                     struct hostapd_bss_config *conf)
1361 {
1362         struct radius_server_conf srv;
1363         os_memset(&srv, 0, sizeof(srv));
1364         srv.client_file = conf->radius_server_clients;
1365         srv.auth_port = conf->radius_server_auth_port;
1366         srv.conf_ctx = conf;
1367         srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1368         srv.ssl_ctx = hapd->ssl_ctx;
1369         srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1370         srv.eap_fast_a_id = conf->eap_fast_a_id;
1371         srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1372         srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1373         srv.eap_fast_prov = conf->eap_fast_prov;
1374         srv.pac_key_lifetime = conf->pac_key_lifetime;
1375         srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1376         srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1377         srv.tnc = conf->tnc;
1378         srv.wps = hapd->wps;
1379         srv.ipv6 = conf->radius_server_ipv6;
1380         srv.get_eap_user = hostapd_radius_get_eap_user;
1381         srv.eap_req_id_text = conf->eap_req_id_text;
1382         srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1383
1384         hapd->radius_srv = radius_server_init(&srv);
1385         if (hapd->radius_srv == NULL) {
1386                 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1387                 return -1;
1388         }
1389
1390         return 0;
1391 }
1392
1393
1394 /**
1395  * hostapd_setup_bss - Per-BSS setup (initialization)
1396  * @hapd: Pointer to BSS data
1397  * @first: Whether this BSS is the first BSS of an interface
1398  *
1399  * This function is used to initialize all per-BSS data structures and
1400  * resources. This gets called in a loop for each BSS when an interface is
1401  * initialized. Most of the modules that are initialized here will be
1402  * deinitialized in hostapd_cleanup().
1403  */
1404 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1405 {
1406         struct hostapd_bss_config *conf = hapd->conf;
1407         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1408         int ssid_len, set_ssid;
1409
1410         if (!first) {
1411                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1412                         /* Allocate the next available BSSID. */
1413                         do {
1414                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
1415                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1416                 } else {
1417                         /* Allocate the configured BSSID. */
1418                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1419
1420                         if (hostapd_mac_comp(hapd->own_addr,
1421                                              hapd->iface->bss[0]->own_addr) ==
1422                             0) {
1423                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1424                                            "BSSID set to the MAC address of "
1425                                            "the radio", hapd->conf->iface);
1426                                 return -1;
1427                         }
1428                 }
1429
1430                 hapd->interface_added = 1;
1431                 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1432                                     hapd->own_addr)) {
1433                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1434                                    MACSTR ")", MAC2STR(hapd->own_addr));
1435                         return -1;
1436                 }
1437         }
1438
1439         /*
1440          * Fetch the SSID from the system and use it or,
1441          * if one was specified in the config file, verify they
1442          * match.
1443          */
1444         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1445         if (ssid_len < 0) {
1446                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1447                 return -1;
1448         }
1449         if (conf->ssid.ssid_set) {
1450                 /*
1451                  * If SSID is specified in the config file and it differs
1452                  * from what is being used then force installation of the
1453                  * new SSID.
1454                  */
1455                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1456                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1457         } else {
1458                 /*
1459                  * No SSID in the config file; just use the one we got
1460                  * from the system.
1461                  */
1462                 set_ssid = 0;
1463                 conf->ssid.ssid_len = ssid_len;
1464                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1465                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1466         }
1467
1468         if (!hostapd_drv_none(hapd)) {
1469                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1470                            " and ssid '%s'",
1471                            hapd->conf->iface, MAC2STR(hapd->own_addr),
1472                            hapd->conf->ssid.ssid);
1473         }
1474
1475         if (hostapd_setup_wpa_psk(conf)) {
1476                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1477                 return -1;
1478         }
1479
1480         /* Set flag for whether SSID is broadcast in beacons */
1481         if (hostapd_set_broadcast_ssid(hapd,
1482                                        !!hapd->conf->ignore_broadcast_ssid)) {
1483                 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1484                            "kernel driver");
1485                 return -1;
1486         }
1487
1488         if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1489                 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1490                            "driver");
1491                 return -1;
1492         }
1493
1494         /* Set SSID for the kernel driver (to be used in beacon and probe
1495          * response frames) */
1496         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1497                                          conf->ssid.ssid_len)) {
1498                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1499                 return -1;
1500         }
1501
1502         if (wpa_debug_level == MSG_MSGDUMP)
1503                 conf->radius->msg_dumps = 1;
1504         hapd->radius = radius_client_init(hapd, conf->radius);
1505         if (hapd->radius == NULL) {
1506                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1507                 return -1;
1508         }
1509
1510         if (hostapd_acl_init(hapd)) {
1511                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1512                 return -1;
1513         }
1514         if (hostapd_init_wps(hapd, conf))
1515                 return -1;
1516
1517         if (ieee802_1x_init(hapd)) {
1518                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1519                 return -1;
1520         }
1521
1522         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1523                 return -1;
1524
1525         if (accounting_init(hapd)) {
1526                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1527                 return -1;
1528         }
1529
1530         if (hapd->conf->ieee802_11f &&
1531             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1532                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1533                            "failed.");
1534                 return -1;
1535         }
1536
1537         if (hostapd_ctrl_iface_init(hapd)) {
1538                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1539                 return -1;
1540         }
1541
1542         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1543                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1544                 return -1;
1545         }
1546
1547 #ifdef CONFIG_IEEE80211R
1548         if (!hostapd_drv_none(hapd)) {
1549                 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1550                                           hostapd_rrb_receive, hapd, 0);
1551                 if (hapd->l2 == NULL &&
1552                     (hapd->driver == NULL ||
1553                      hapd->driver->send_ether == NULL)) {
1554                         wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1555                                    "interface");
1556                         return -1;
1557                 }
1558         }
1559 #endif /* CONFIG_IEEE80211R */
1560
1561         ieee802_11_set_beacon(hapd);
1562
1563         if (conf->radius_server_clients &&
1564             hostapd_setup_radius_srv(hapd, conf))
1565                 return -1;
1566
1567         return 0;
1568 }
1569
1570
1571 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1572 {
1573         struct hostapd_data *hapd = iface->bss[0];
1574         int i;
1575         struct hostapd_tx_queue_params *p;
1576
1577         for (i = 0; i < NUM_TX_QUEUES; i++) {
1578                 p = &iface->conf->tx_queue[i];
1579
1580                 if (!p->configured)
1581                         continue;
1582
1583                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1584                                                 p->cwmax, p->burst)) {
1585                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1586                                    "parameters for queue %d.", i);
1587                         /* Continue anyway */
1588                 }
1589         }
1590 }
1591
1592
1593 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1594                                        size_t identity_len, int phase2,
1595                                        struct eap_user *user)
1596 {
1597         const struct hostapd_eap_user *eap_user;
1598         int i, count;
1599
1600         eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1601         if (eap_user == NULL)
1602                 return -1;
1603
1604         if (user == NULL)
1605                 return 0;
1606
1607         os_memset(user, 0, sizeof(*user));
1608         count = EAP_USER_MAX_METHODS;
1609         if (count > EAP_MAX_METHODS)
1610                 count = EAP_MAX_METHODS;
1611         for (i = 0; i < count; i++) {
1612                 user->methods[i].vendor = eap_user->methods[i].vendor;
1613                 user->methods[i].method = eap_user->methods[i].method;
1614         }
1615
1616         if (eap_user->password) {
1617                 user->password = os_malloc(eap_user->password_len);
1618                 if (user->password == NULL)
1619                         return -1;
1620                 os_memcpy(user->password, eap_user->password,
1621                           eap_user->password_len);
1622                 user->password_len = eap_user->password_len;
1623                 user->password_hash = eap_user->password_hash;
1624         }
1625         user->force_version = eap_user->force_version;
1626         user->ttls_auth = eap_user->ttls_auth;
1627
1628         return 0;
1629 }
1630
1631
1632 static int setup_interface(struct hostapd_iface *iface)
1633 {
1634         struct hostapd_data *hapd = iface->bss[0];
1635         struct hostapd_bss_config *conf = hapd->conf;
1636         size_t i;
1637         char country[4];
1638         u8 *b = conf->bssid;
1639         int freq;
1640         size_t j;
1641         int ret = 0;
1642         u8 *prev_addr;
1643
1644         /*
1645          * Initialize the driver interface and make sure that all BSSes get
1646          * configured with a pointer to this driver interface.
1647          */
1648         if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1649                 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1650         } else {
1651                 hapd->drv_priv = hostapd_driver_init(hapd);
1652         }
1653
1654         if (hapd->drv_priv == NULL) {
1655                 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1656                            hapd->driver ? hapd->driver->name : "Unknown");
1657                 hapd->driver = NULL;
1658                 return -1;
1659         }
1660         for (i = 0; i < iface->num_bss; i++) {
1661                 iface->bss[i]->driver = hapd->driver;
1662                 iface->bss[i]->drv_priv = hapd->drv_priv;
1663         }
1664
1665         if (hostapd_validate_bssid_configuration(iface))
1666                 return -1;
1667
1668 #ifdef CONFIG_IEEE80211N
1669         SET_2BIT_LE16(&iface->ht_op_mode,
1670                       HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1671                       OP_MODE_PURE);
1672 #endif /* CONFIG_IEEE80211N */
1673
1674         os_memcpy(country, hapd->iconf->country, 3);
1675         country[3] = '\0';
1676         if (hostapd_set_country(hapd, country) < 0) {
1677                 wpa_printf(MSG_ERROR, "Failed to set country code");
1678                 return -1;
1679         }
1680
1681         if (hapd->iconf->ieee80211d &&
1682             hostapd_set_ieee80211d(hapd, 1) < 0) {
1683                 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1684                            hapd->iconf->ieee80211d);
1685                 return -1;
1686         }
1687
1688         if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1689             hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1690                 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1691                            "kernel driver");
1692                 return -1;
1693         }
1694
1695         /* TODO: merge with hostapd_driver_init() ? */
1696         if (hostapd_wireless_event_init(hapd) < 0)
1697                 return -1;
1698
1699         if (hostapd_get_hw_features(iface)) {
1700                 /* Not all drivers support this yet, so continue without hw
1701                  * feature data. */
1702         } else {
1703                 int ret = hostapd_select_hw_mode(iface);
1704                 if (ret < 0) {
1705                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1706                                    "channel. (%d)", ret);
1707                         return -1;
1708                 }
1709         }
1710
1711         hostapd_flush_old_stations(hapd);
1712         hostapd_set_privacy(hapd, 0);
1713
1714         if (hapd->iconf->channel) {
1715                 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1716                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
1717                            "Frequency: %d MHz",
1718                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1719                            hapd->iconf->channel, freq);
1720
1721                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1722                                      hapd->iconf->ieee80211n,
1723                                      hapd->iconf->secondary_channel)) {
1724                         wpa_printf(MSG_ERROR, "Could not set channel for "
1725                                    "kernel driver");
1726                         return -1;
1727                 }
1728         }
1729
1730         hostapd_broadcast_wep_clear(hapd);
1731         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1732                 return -1;
1733
1734         hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1735         ieee802_11_set_beacon(hapd);
1736
1737         if (hapd->iconf->rts_threshold > -1 &&
1738             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1739                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1740                            "kernel driver");
1741                 return -1;
1742         }
1743
1744         if (hapd->iconf->fragm_threshold > -1 &&
1745             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1746                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1747                            "for kernel driver");
1748                 return -1;
1749         }
1750
1751         prev_addr = hapd->own_addr;
1752
1753         for (j = 0; j < iface->num_bss; j++) {
1754                 hapd = iface->bss[j];
1755                 if (j)
1756                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1757                 if (hostapd_setup_bss(hapd, j == 0))
1758                         return -1;
1759                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1760                         prev_addr = hapd->own_addr;
1761         }
1762
1763         hostapd_tx_queue_params(iface);
1764
1765         ap_list_init(iface);
1766
1767         if (hostapd_driver_commit(hapd) < 0) {
1768                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1769                            "configuration", __func__);
1770                 return -1;
1771         }
1772
1773         return ret;
1774 }
1775
1776
1777 /**
1778  * hostapd_setup_interface - Setup of an interface
1779  * @iface: Pointer to interface data.
1780  * Returns: 0 on success, -1 on failure
1781  *
1782  * Initializes the driver interface, validates the configuration,
1783  * and sets driver parameters based on the configuration.
1784  * Flushes old stations, sets the channel, encryption,
1785  * beacons, and WDS links based on the configuration.
1786  */
1787 static int hostapd_setup_interface(struct hostapd_iface *iface)
1788 {
1789         int ret;
1790
1791         ret = setup_interface(iface);
1792         if (ret) {
1793                 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1794                            iface->bss[0]->conf->iface);
1795                 eloop_terminate();
1796                 return -1;
1797         } else if (!hostapd_drv_none(iface->bss[0])) {
1798                 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1799                            iface->bss[0]->conf->iface);
1800         }
1801
1802         return 0;
1803 }
1804
1805
1806 static void show_version(void)
1807 {
1808         fprintf(stderr,
1809                 "hostapd v" VERSION_STR "\n"
1810                 "User space daemon for IEEE 802.11 AP management,\n"
1811                 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1812                 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1813                 "and contributors\n");
1814 }
1815
1816
1817 static void usage(void)
1818 {
1819         show_version();
1820         fprintf(stderr,
1821                 "\n"
1822                 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1823                 "<configuration file(s)>\n"
1824                 "\n"
1825                 "options:\n"
1826                 "   -h   show this usage\n"
1827                 "   -d   show more debug messages (-dd for even more)\n"
1828                 "   -B   run daemon in the background\n"
1829                 "   -P   PID file\n"
1830                 "   -K   include key data in debug messages\n"
1831                 "   -t   include timestamps in some debug messages\n"
1832                 "   -v   show hostapd version\n");
1833
1834         exit(1);
1835 }
1836
1837
1838 /**
1839  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1840  * @hapd_iface: Pointer to interface data
1841  * @conf: Pointer to per-interface configuration
1842  * @bss: Pointer to per-BSS configuration for this BSS
1843  * Returns: Pointer to allocated BSS data
1844  *
1845  * This function is used to allocate per-BSS data structure. This data will be
1846  * freed after hostapd_cleanup() is called for it during interface
1847  * deinitialization.
1848  */
1849 static struct hostapd_data *
1850 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1851                        struct hostapd_config *conf,
1852                        struct hostapd_bss_config *bss)
1853 {
1854         struct hostapd_data *hapd;
1855
1856         hapd = os_zalloc(sizeof(*hapd));
1857         if (hapd == NULL)
1858                 return NULL;
1859
1860         hapd->iconf = conf;
1861         hapd->conf = bss;
1862         hapd->iface = hapd_iface;
1863
1864         if (hapd->conf->individual_wep_key_len > 0) {
1865                 /* use key0 in individual key and key1 in broadcast key */
1866                 hapd->default_wep_key_idx = 1;
1867         }
1868
1869 #ifdef EAP_TLS_FUNCS
1870         if (hapd->conf->eap_server &&
1871             (hapd->conf->ca_cert || hapd->conf->server_cert ||
1872              hapd->conf->dh_file)) {
1873                 struct tls_connection_params params;
1874
1875                 hapd->ssl_ctx = tls_init(NULL);
1876                 if (hapd->ssl_ctx == NULL) {
1877                         wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1878                         goto fail;
1879                 }
1880
1881                 os_memset(&params, 0, sizeof(params));
1882                 params.ca_cert = hapd->conf->ca_cert;
1883                 params.client_cert = hapd->conf->server_cert;
1884                 params.private_key = hapd->conf->private_key;
1885                 params.private_key_passwd = hapd->conf->private_key_passwd;
1886                 params.dh_file = hapd->conf->dh_file;
1887
1888                 if (tls_global_set_params(hapd->ssl_ctx, &params)) {
1889                         wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1890                         goto fail;
1891                 }
1892
1893                 if (tls_global_set_verify(hapd->ssl_ctx,
1894                                           hapd->conf->check_crl)) {
1895                         wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1896                         goto fail;
1897                 }
1898         }
1899 #endif /* EAP_TLS_FUNCS */
1900
1901 #ifdef EAP_SERVER
1902         if (hapd->conf->eap_sim_db) {
1903                 hapd->eap_sim_db_priv =
1904                         eap_sim_db_init(hapd->conf->eap_sim_db,
1905                                         hostapd_sim_db_cb, hapd);
1906                 if (hapd->eap_sim_db_priv == NULL) {
1907                         wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1908                                    "database interface");
1909                         goto fail;
1910                 }
1911         }
1912 #endif /* EAP_SERVER */
1913
1914         hapd->driver = hapd->iconf->driver;
1915
1916         return hapd;
1917
1918 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1919 fail:
1920 #endif
1921         /* TODO: cleanup allocated resources(?) */
1922         os_free(hapd);
1923         return NULL;
1924 }
1925
1926
1927 /**
1928  * hostapd_init - Allocate and initialize per-interface data
1929  * @config_file: Path to the configuration file
1930  * Returns: Pointer to the allocated interface data or %NULL on failure
1931  *
1932  * This function is used to allocate main data structures for per-interface
1933  * data. The allocated data buffer will be freed by calling
1934  * hostapd_cleanup_iface().
1935  */
1936 static struct hostapd_iface * hostapd_init(const char *config_file)
1937 {
1938         struct hostapd_iface *hapd_iface = NULL;
1939         struct hostapd_config *conf = NULL;
1940         struct hostapd_data *hapd;
1941         size_t i;
1942
1943         hapd_iface = os_zalloc(sizeof(*hapd_iface));
1944         if (hapd_iface == NULL)
1945                 goto fail;
1946
1947         hapd_iface->config_fname = os_strdup(config_file);
1948         if (hapd_iface->config_fname == NULL)
1949                 goto fail;
1950
1951         conf = hostapd_config_read(hapd_iface->config_fname);
1952         if (conf == NULL)
1953                 goto fail;
1954         hapd_iface->conf = conf;
1955
1956         hapd_iface->num_bss = conf->num_bss;
1957         hapd_iface->bss = os_zalloc(conf->num_bss *
1958                                     sizeof(struct hostapd_data *));
1959         if (hapd_iface->bss == NULL)
1960                 goto fail;
1961
1962         for (i = 0; i < conf->num_bss; i++) {
1963                 hapd = hapd_iface->bss[i] =
1964                         hostapd_alloc_bss_data(hapd_iface, conf,
1965                                                &conf->bss[i]);
1966                 if (hapd == NULL)
1967                         goto fail;
1968         }
1969
1970         return hapd_iface;
1971
1972 fail:
1973         if (conf)
1974                 hostapd_config_free(conf);
1975         if (hapd_iface) {
1976                 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1977                         hapd = hapd_iface->bss[i];
1978                         if (hapd && hapd->ssl_ctx)
1979                                 tls_deinit(hapd->ssl_ctx);
1980                 }
1981
1982                 os_free(hapd_iface->config_fname);
1983                 os_free(hapd_iface->bss);
1984                 os_free(hapd_iface);
1985         }
1986         return NULL;
1987 }
1988
1989
1990 int main(int argc, char *argv[])
1991 {
1992         struct hapd_interfaces interfaces;
1993         int ret = 1, k;
1994         size_t i, j;
1995         int c, debug = 0, daemonize = 0, tnc = 0;
1996         const char *pid_file = NULL;
1997
1998         hostapd_logger_register_cb(hostapd_logger_cb);
1999
2000         for (;;) {
2001                 c = getopt(argc, argv, "BdhKP:tv");
2002                 if (c < 0)
2003                         break;
2004                 switch (c) {
2005                 case 'h':
2006                         usage();
2007                         break;
2008                 case 'd':
2009                         debug++;
2010                         if (wpa_debug_level > 0)
2011                                 wpa_debug_level--;
2012                         break;
2013                 case 'B':
2014                         daemonize++;
2015                         break;
2016                 case 'K':
2017                         wpa_debug_show_keys++;
2018                         break;
2019                 case 'P':
2020                         pid_file = optarg;
2021                         break;
2022                 case 't':
2023                         wpa_debug_timestamp++;
2024                         break;
2025                 case 'v':
2026                         show_version();
2027                         exit(1);
2028                         break;
2029
2030                 default:
2031                         usage();
2032                         break;
2033                 }
2034         }
2035
2036         if (optind == argc)
2037                 usage();
2038
2039         if (eap_server_register_methods()) {
2040                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2041                 return -1;
2042         }
2043
2044         interfaces.count = argc - optind;
2045
2046         interfaces.iface = os_malloc(interfaces.count *
2047                                      sizeof(struct hostapd_iface *));
2048         if (interfaces.iface == NULL) {
2049                 wpa_printf(MSG_ERROR, "malloc failed\n");
2050                 return -1;
2051         }
2052
2053         if (eloop_init(&interfaces)) {
2054                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2055                 return -1;
2056         }
2057
2058 #ifndef CONFIG_NATIVE_WINDOWS
2059         eloop_register_signal(SIGHUP, handle_reload, NULL);
2060         eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
2061 #endif /* CONFIG_NATIVE_WINDOWS */
2062         eloop_register_signal_terminate(handle_term, NULL);
2063
2064         /* Initialize interfaces */
2065         for (i = 0; i < interfaces.count; i++) {
2066                 wpa_printf(MSG_ERROR, "Configuration file: %s",
2067                            argv[optind + i]);
2068                 interfaces.iface[i] = hostapd_init(argv[optind + i]);
2069                 if (!interfaces.iface[i])
2070                         goto out;
2071                 for (k = 0; k < debug; k++) {
2072                         if (interfaces.iface[i]->bss[0]->conf->
2073                             logger_stdout_level > 0)
2074                                 interfaces.iface[i]->bss[0]->conf->
2075                                         logger_stdout_level--;
2076                 }
2077
2078                 ret = hostapd_setup_interface(interfaces.iface[i]);
2079                 if (ret)
2080                         goto out;
2081
2082                 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
2083                         if (interfaces.iface[i]->bss[0]->conf->tnc)
2084                                 tnc++;
2085                 }
2086         }
2087
2088 #ifdef EAP_TNC
2089         if (tnc && tncs_global_init() < 0) {
2090                 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
2091                 goto out;
2092         }
2093 #endif /* EAP_TNC */
2094
2095         if (daemonize && os_daemonize(pid_file)) {
2096                 perror("daemon");
2097                 goto out;
2098         }
2099
2100 #ifndef CONFIG_NATIVE_WINDOWS
2101         openlog("hostapd", 0, LOG_DAEMON);
2102 #endif /* CONFIG_NATIVE_WINDOWS */
2103
2104         eloop_run();
2105
2106         /* Disconnect associated stations from all interfaces and BSSes */
2107         for (i = 0; i < interfaces.count; i++) {
2108                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2109                         struct hostapd_data *hapd =
2110                                 interfaces.iface[i]->bss[j];
2111                         hostapd_free_stas(hapd);
2112                         hostapd_flush_old_stations(hapd);
2113                 }
2114         }
2115
2116         ret = 0;
2117
2118  out:
2119         /* Deinitialize all interfaces */
2120         for (i = 0; i < interfaces.count; i++) {
2121                 if (!interfaces.iface[i])
2122                         continue;
2123                 hostapd_cleanup_iface_pre(interfaces.iface[i]);
2124                 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2125                         struct hostapd_data *hapd =
2126                                 interfaces.iface[i]->bss[j];
2127                         hostapd_cleanup(hapd);
2128                         if (j == interfaces.iface[i]->num_bss - 1 &&
2129                             hapd->driver)
2130                                 hostapd_driver_deinit(hapd);
2131                 }
2132                 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2133                         os_free(interfaces.iface[i]->bss[j]);
2134                 hostapd_cleanup_iface(interfaces.iface[i]);
2135         }
2136         os_free(interfaces.iface);
2137
2138 #ifdef EAP_TNC
2139         tncs_global_deinit();
2140 #endif /* EAP_TNC */
2141
2142         eloop_destroy();
2143
2144 #ifndef CONFIG_NATIVE_WINDOWS
2145         closelog();
2146 #endif /* CONFIG_NATIVE_WINDOWS */
2147
2148         eap_server_unregister_methods();
2149
2150         os_daemonize_terminate(pid_file);
2151
2152         return ret;
2153 }