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