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