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