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