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