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