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