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