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