127454deb9a7225564e6c45c4500945c098d91eb
[mech_eap.git] / src / ap / 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 "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "radius/radius_client.h"
21 #include "drivers/driver.h"
22 #include "hostapd.h"
23 #include "authsrv.h"
24 #include "sta_info.h"
25 #include "accounting.h"
26 #include "ap_list.h"
27 #include "beacon.h"
28 #include "iapp.h"
29 #include "ieee802_1x.h"
30 #include "ieee802_11_auth.h"
31 #include "vlan_init.h"
32 #include "wpa_auth.h"
33 #include "wps_hostapd.h"
34 #include "hw_features.h"
35 #include "wpa_auth_glue.h"
36 #include "ap_drv_ops.h"
37 #include "ap_config.h"
38 #include "p2p_hostapd.h"
39
40
41 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
42 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
43
44 extern int wpa_debug_level;
45
46
47 static void hostapd_reload_bss(struct hostapd_data *hapd)
48 {
49         if (hostapd_setup_wpa_psk(hapd->conf)) {
50                 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
51                            "after reloading configuration");
52         }
53
54         if (hapd->conf->ieee802_1x || hapd->conf->wpa)
55                 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
56         else
57                 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
58
59         if (hapd->conf->wpa && hapd->wpa_auth == NULL)
60                 hostapd_setup_wpa(hapd);
61         else if (hapd->conf->wpa) {
62                 const u8 *wpa_ie;
63                 size_t wpa_ie_len;
64                 hostapd_reconfig_wpa(hapd);
65                 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
66                 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
67                         wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
68                                    "the kernel driver.");
69         } else if (hapd->wpa_auth) {
70                 wpa_deinit(hapd->wpa_auth);
71                 hapd->wpa_auth = NULL;
72                 hostapd_set_privacy(hapd, 0);
73                 hostapd_setup_encryption(hapd->conf->iface, hapd);
74                 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
75         }
76
77         ieee802_11_set_beacon(hapd);
78         hostapd_update_wps(hapd);
79
80         if (hapd->conf->ssid.ssid_set &&
81             hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
82                              hapd->conf->ssid.ssid_len)) {
83                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
84                 /* try to continue */
85         }
86         wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
87 }
88
89
90 int hostapd_reload_config(struct hostapd_iface *iface)
91 {
92         struct hostapd_data *hapd = iface->bss[0];
93         struct hostapd_config *newconf, *oldconf;
94         size_t j;
95
96         if (iface->config_read_cb == NULL)
97                 return -1;
98         newconf = iface->config_read_cb(iface->config_fname);
99         if (newconf == NULL)
100                 return -1;
101
102         /*
103          * Deauthenticate all stations since the new configuration may not
104          * allow them to use the BSS anymore.
105          */
106         for (j = 0; j < iface->num_bss; j++)
107                 hostapd_flush_old_stations(iface->bss[j]);
108
109 #ifndef CONFIG_NO_RADIUS
110         /* TODO: update dynamic data based on changed configuration
111          * items (e.g., open/close sockets, etc.) */
112         radius_client_flush(hapd->radius, 0);
113 #endif /* CONFIG_NO_RADIUS */
114
115         oldconf = hapd->iconf;
116         iface->conf = newconf;
117
118         for (j = 0; j < iface->num_bss; j++) {
119                 hapd = iface->bss[j];
120                 hapd->iconf = newconf;
121                 hapd->conf = &newconf->bss[j];
122                 hostapd_reload_bss(hapd);
123         }
124
125         hostapd_config_free(oldconf);
126
127
128         return 0;
129 }
130
131
132 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
133                                               char *ifname)
134 {
135         int i;
136
137         for (i = 0; i < NUM_WEP_KEYS; i++) {
138                 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
139                                         i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
140                         wpa_printf(MSG_DEBUG, "Failed to clear default "
141                                    "encryption keys (ifname=%s keyidx=%d)",
142                                    ifname, i);
143                 }
144         }
145 #ifdef CONFIG_IEEE80211W
146         if (hapd->conf->ieee80211w) {
147                 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
148                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
149                                                 NULL, i, i == 0 ? 1 : 0, NULL,
150                                                 0, NULL, 0)) {
151                                 wpa_printf(MSG_DEBUG, "Failed to clear "
152                                            "default mgmt encryption keys "
153                                            "(ifname=%s keyidx=%d)", ifname, i);
154                         }
155                 }
156         }
157 #endif /* CONFIG_IEEE80211W */
158 }
159
160
161 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
162 {
163         hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
164         return 0;
165 }
166
167
168 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
169 {
170         int errors = 0, idx;
171         struct hostapd_ssid *ssid = &hapd->conf->ssid;
172
173         idx = ssid->wep.idx;
174         if (ssid->wep.default_len &&
175             hostapd_drv_set_key(hapd->conf->iface,
176                                 hapd, WPA_ALG_WEP, NULL, idx,
177                                 idx == ssid->wep.idx,
178                                 NULL, 0, ssid->wep.key[idx],
179                                 ssid->wep.len[idx])) {
180                 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
181                 errors++;
182         }
183
184         if (ssid->dyn_vlan_keys) {
185                 size_t i;
186                 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
187                         const char *ifname;
188                         struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
189                         if (key == NULL)
190                                 continue;
191                         ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
192                                                             i);
193                         if (ifname == NULL)
194                                 continue;
195
196                         idx = key->idx;
197                         if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
198                                                 NULL, idx, idx == key->idx,
199                                                 NULL, 0, key->key[idx],
200                                                 key->len[idx])) {
201                                 wpa_printf(MSG_WARNING, "Could not set "
202                                            "dynamic VLAN WEP encryption.");
203                                 errors++;
204                         }
205                 }
206         }
207
208         return errors;
209 }
210
211 /**
212  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
213  * @hapd: Pointer to BSS data
214  *
215  * This function is used to free all per-BSS data structures and resources.
216  * This gets called in a loop for each BSS between calls to
217  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
218  * is deinitialized. Most of the modules that are initialized in
219  * hostapd_setup_bss() are deinitialized here.
220  */
221 static void hostapd_cleanup(struct hostapd_data *hapd)
222 {
223         if (hapd->iface->ctrl_iface_deinit)
224                 hapd->iface->ctrl_iface_deinit(hapd);
225
226         iapp_deinit(hapd->iapp);
227         hapd->iapp = NULL;
228         accounting_deinit(hapd);
229         hostapd_deinit_wpa(hapd);
230         vlan_deinit(hapd);
231         hostapd_acl_deinit(hapd);
232 #ifndef CONFIG_NO_RADIUS
233         radius_client_deinit(hapd->radius);
234         hapd->radius = NULL;
235 #endif /* CONFIG_NO_RADIUS */
236
237         hostapd_deinit_wps(hapd);
238
239         authsrv_deinit(hapd);
240
241         if (hapd->interface_added &&
242             hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
243                 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
244                            hapd->conf->iface);
245         }
246
247         os_free(hapd->probereq_cb);
248         hapd->probereq_cb = NULL;
249
250 #ifdef CONFIG_P2P
251         wpabuf_free(hapd->p2p_beacon_ie);
252         hapd->p2p_beacon_ie = NULL;
253         wpabuf_free(hapd->p2p_probe_resp_ie);
254         hapd->p2p_probe_resp_ie = NULL;
255 #endif /* CONFIG_P2P */
256 }
257
258
259 /**
260  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
261  * @iface: Pointer to interface data
262  *
263  * This function is called before per-BSS data structures are deinitialized
264  * with hostapd_cleanup().
265  */
266 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
267 {
268 }
269
270
271 /**
272  * hostapd_cleanup_iface - Complete per-interface cleanup
273  * @iface: Pointer to interface data
274  *
275  * This function is called after per-BSS data structures are deinitialized
276  * with hostapd_cleanup().
277  */
278 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
279 {
280         hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
281         iface->hw_features = NULL;
282         os_free(iface->current_rates);
283         iface->current_rates = NULL;
284         ap_list_deinit(iface);
285         hostapd_config_free(iface->conf);
286         iface->conf = NULL;
287
288         os_free(iface->config_fname);
289         os_free(iface->bss);
290         os_free(iface);
291 }
292
293
294 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
295 {
296         int i;
297
298         hostapd_broadcast_wep_set(hapd);
299
300         if (hapd->conf->ssid.wep.default_len) {
301                 hostapd_set_privacy(hapd, 1);
302                 return 0;
303         }
304
305         for (i = 0; i < 4; i++) {
306                 if (hapd->conf->ssid.wep.key[i] &&
307                     hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
308                                         i == hapd->conf->ssid.wep.idx, NULL, 0,
309                                         hapd->conf->ssid.wep.key[i],
310                                         hapd->conf->ssid.wep.len[i])) {
311                         wpa_printf(MSG_WARNING, "Could not set WEP "
312                                    "encryption.");
313                         return -1;
314                 }
315                 if (hapd->conf->ssid.wep.key[i] &&
316                     i == hapd->conf->ssid.wep.idx)
317                         hostapd_set_privacy(hapd, 1);
318         }
319
320         return 0;
321 }
322
323
324 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
325 {
326         int ret = 0;
327
328         if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
329                 return 0;
330
331         wpa_printf(MSG_DEBUG, "Flushing old station entries");
332         if (hostapd_flush(hapd)) {
333                 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
334                 ret = -1;
335         }
336         wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
337
338         /* New Prism2.5/3 STA firmware versions seem to have issues with this
339          * broadcast deauth frame. This gets the firmware in odd state where
340          * nothing works correctly, so let's skip sending this for the hostap
341          * driver. */
342         if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
343                 u8 addr[ETH_ALEN];
344                 os_memset(addr, 0xff, ETH_ALEN);
345                 hostapd_drv_sta_deauth(hapd, addr,
346                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
347         }
348
349         return ret;
350 }
351
352
353 /**
354  * hostapd_validate_bssid_configuration - Validate BSSID configuration
355  * @iface: Pointer to interface data
356  * Returns: 0 on success, -1 on failure
357  *
358  * This function is used to validate that the configured BSSIDs are valid.
359  */
360 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
361 {
362         u8 mask[ETH_ALEN] = { 0 };
363         struct hostapd_data *hapd = iface->bss[0];
364         unsigned int i = iface->conf->num_bss, bits = 0, j;
365         int res;
366         int auto_addr = 0;
367
368         if (hostapd_drv_none(hapd))
369                 return 0;
370
371         /* Generate BSSID mask that is large enough to cover the BSSIDs. */
372
373         /* Determine the bits necessary to cover the number of BSSIDs. */
374         for (i--; i; i >>= 1)
375                 bits++;
376
377         /* Determine the bits necessary to any configured BSSIDs,
378            if they are higher than the number of BSSIDs. */
379         for (j = 0; j < iface->conf->num_bss; j++) {
380                 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
381                         if (j)
382                                 auto_addr++;
383                         continue;
384                 }
385
386                 for (i = 0; i < ETH_ALEN; i++) {
387                         mask[i] |=
388                                 iface->conf->bss[j].bssid[i] ^
389                                 hapd->own_addr[i];
390                 }
391         }
392
393         if (!auto_addr)
394                 goto skip_mask_ext;
395
396         for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
397                 ;
398         j = 0;
399         if (i < ETH_ALEN) {
400                 j = (5 - i) * 8;
401
402                 while (mask[i] != 0) {
403                         mask[i] >>= 1;
404                         j++;
405                 }
406         }
407
408         if (bits < j)
409                 bits = j;
410
411         if (bits > 40) {
412                 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
413                            bits);
414                 return -1;
415         }
416
417         os_memset(mask, 0xff, ETH_ALEN);
418         j = bits / 8;
419         for (i = 5; i > 5 - j; i--)
420                 mask[i] = 0;
421         j = bits % 8;
422         while (j--)
423                 mask[i] <<= 1;
424
425 skip_mask_ext:
426         wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
427                    (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
428
429         res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
430         if (res == 0)
431                 return 0;
432
433         if (res < 0) {
434                 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
435                            MACSTR " for start address " MACSTR ".",
436                            MAC2STR(mask), MAC2STR(hapd->own_addr));
437                 return -1;
438         }
439
440         if (!auto_addr)
441                 return 0;
442
443         for (i = 0; i < ETH_ALEN; i++) {
444                 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
445                         wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
446                                    " for start address " MACSTR ".",
447                                    MAC2STR(mask), MAC2STR(hapd->own_addr));
448                         wpa_printf(MSG_ERROR, "Start address must be the "
449                                    "first address in the block (i.e., addr "
450                                    "AND mask == addr).");
451                         return -1;
452                 }
453         }
454
455         return 0;
456 }
457
458
459 static int mac_in_conf(struct hostapd_config *conf, const void *a)
460 {
461         size_t i;
462
463         for (i = 0; i < conf->num_bss; i++) {
464                 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
465                         return 1;
466                 }
467         }
468
469         return 0;
470 }
471
472
473
474
475 /**
476  * hostapd_setup_bss - Per-BSS setup (initialization)
477  * @hapd: Pointer to BSS data
478  * @first: Whether this BSS is the first BSS of an interface
479  *
480  * This function is used to initialize all per-BSS data structures and
481  * resources. This gets called in a loop for each BSS when an interface is
482  * initialized. Most of the modules that are initialized here will be
483  * deinitialized in hostapd_cleanup().
484  */
485 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
486 {
487         struct hostapd_bss_config *conf = hapd->conf;
488         u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
489         int ssid_len, set_ssid;
490         char force_ifname[IFNAMSIZ];
491         u8 if_addr[ETH_ALEN];
492
493         if (!first) {
494                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
495                         /* Allocate the next available BSSID. */
496                         do {
497                                 inc_byte_array(hapd->own_addr, ETH_ALEN);
498                         } while (mac_in_conf(hapd->iconf, hapd->own_addr));
499                 } else {
500                         /* Allocate the configured BSSID. */
501                         os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
502
503                         if (hostapd_mac_comp(hapd->own_addr,
504                                              hapd->iface->bss[0]->own_addr) ==
505                             0) {
506                                 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
507                                            "BSSID set to the MAC address of "
508                                            "the radio", hapd->conf->iface);
509                                 return -1;
510                         }
511                 }
512
513                 hapd->interface_added = 1;
514                 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
515                                    hapd->conf->iface, hapd->own_addr, hapd,
516                                    &hapd->drv_priv, force_ifname, if_addr)) {
517                         wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
518                                    MACSTR ")", MAC2STR(hapd->own_addr));
519                         return -1;
520                 }
521         }
522
523         hostapd_flush_old_stations(hapd);
524         hostapd_set_privacy(hapd, 0);
525
526         hostapd_broadcast_wep_clear(hapd);
527         if (hostapd_setup_encryption(hapd->conf->iface, hapd))
528                 return -1;
529
530         /*
531          * Fetch the SSID from the system and use it or,
532          * if one was specified in the config file, verify they
533          * match.
534          */
535         ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
536         if (ssid_len < 0) {
537                 wpa_printf(MSG_ERROR, "Could not read SSID from system");
538                 return -1;
539         }
540         if (conf->ssid.ssid_set) {
541                 /*
542                  * If SSID is specified in the config file and it differs
543                  * from what is being used then force installation of the
544                  * new SSID.
545                  */
546                 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
547                             os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
548         } else {
549                 /*
550                  * No SSID in the config file; just use the one we got
551                  * from the system.
552                  */
553                 set_ssid = 0;
554                 conf->ssid.ssid_len = ssid_len;
555                 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
556                 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
557         }
558
559         if (!hostapd_drv_none(hapd)) {
560                 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
561                            " and ssid '%s'",
562                            hapd->conf->iface, MAC2STR(hapd->own_addr),
563                            hapd->conf->ssid.ssid);
564         }
565
566         if (hostapd_setup_wpa_psk(conf)) {
567                 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
568                 return -1;
569         }
570
571         /* Set SSID for the kernel driver (to be used in beacon and probe
572          * response frames) */
573         if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
574                                          conf->ssid.ssid_len)) {
575                 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
576                 return -1;
577         }
578
579         if (wpa_debug_level == MSG_MSGDUMP)
580                 conf->radius->msg_dumps = 1;
581 #ifndef CONFIG_NO_RADIUS
582         hapd->radius = radius_client_init(hapd, conf->radius);
583         if (hapd->radius == NULL) {
584                 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
585                 return -1;
586         }
587 #endif /* CONFIG_NO_RADIUS */
588
589         if (hostapd_acl_init(hapd)) {
590                 wpa_printf(MSG_ERROR, "ACL initialization failed.");
591                 return -1;
592         }
593         if (hostapd_init_wps(hapd, conf))
594                 return -1;
595
596         if (authsrv_init(hapd) < 0)
597                 return -1;
598
599         if (ieee802_1x_init(hapd)) {
600                 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
601                 return -1;
602         }
603
604         if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
605                 return -1;
606
607         if (accounting_init(hapd)) {
608                 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
609                 return -1;
610         }
611
612         if (hapd->conf->ieee802_11f &&
613             (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
614                 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
615                            "failed.");
616                 return -1;
617         }
618
619         if (hapd->iface->ctrl_iface_init &&
620             hapd->iface->ctrl_iface_init(hapd)) {
621                 wpa_printf(MSG_ERROR, "Failed to setup control interface");
622                 return -1;
623         }
624
625         if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
626                 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
627                 return -1;
628         }
629
630         ieee802_11_set_beacon(hapd);
631
632         if (hapd->driver && hapd->driver->set_operstate)
633                 hapd->driver->set_operstate(hapd->drv_priv, 1);
634
635         return 0;
636 }
637
638
639 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
640 {
641         struct hostapd_data *hapd = iface->bss[0];
642         int i;
643         struct hostapd_tx_queue_params *p;
644
645         for (i = 0; i < NUM_TX_QUEUES; i++) {
646                 p = &iface->conf->tx_queue[i];
647
648                 if (!p->configured)
649                         continue;
650
651                 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
652                                                 p->cwmax, p->burst)) {
653                         wpa_printf(MSG_DEBUG, "Failed to set TX queue "
654                                    "parameters for queue %d.", i);
655                         /* Continue anyway */
656                 }
657         }
658 }
659
660
661 static int setup_interface(struct hostapd_iface *iface)
662 {
663         struct hostapd_data *hapd = iface->bss[0];
664         size_t i;
665         char country[4];
666
667         /*
668          * Make sure that all BSSes get configured with a pointer to the same
669          * driver interface.
670          */
671         for (i = 1; i < iface->num_bss; i++) {
672                 iface->bss[i]->driver = hapd->driver;
673                 iface->bss[i]->drv_priv = hapd->drv_priv;
674         }
675
676         if (hostapd_validate_bssid_configuration(iface))
677                 return -1;
678
679         if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
680                 os_memcpy(country, hapd->iconf->country, 3);
681                 country[3] = '\0';
682                 if (hostapd_set_country(hapd, country) < 0) {
683                         wpa_printf(MSG_ERROR, "Failed to set country code");
684                         return -1;
685                 }
686         }
687
688         if (hostapd_get_hw_features(iface)) {
689                 /* Not all drivers support this yet, so continue without hw
690                  * feature data. */
691         } else {
692                 int ret = hostapd_select_hw_mode(iface);
693                 if (ret < 0) {
694                         wpa_printf(MSG_ERROR, "Could not select hw_mode and "
695                                    "channel. (%d)", ret);
696                         return -1;
697                 }
698                 ret = hostapd_check_ht_capab(iface);
699                 if (ret < 0)
700                         return -1;
701                 if (ret == 1) {
702                         wpa_printf(MSG_DEBUG, "Interface initialization will "
703                                    "be completed in a callback");
704                         return 0;
705                 }
706         }
707         return hostapd_setup_interface_complete(iface, 0);
708 }
709
710
711 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
712 {
713         struct hostapd_data *hapd = iface->bss[0];
714         size_t j;
715         u8 *prev_addr;
716
717         if (err) {
718                 wpa_printf(MSG_ERROR, "Interface initialization failed");
719                 eloop_terminate();
720                 return -1;
721         }
722
723         wpa_printf(MSG_DEBUG, "Completing interface initialization");
724         if (hapd->iconf->channel) {
725                 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
726                 wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
727                            "Frequency: %d MHz",
728                            hostapd_hw_mode_txt(hapd->iconf->hw_mode),
729                            hapd->iconf->channel, iface->freq);
730
731                 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
732                                      hapd->iconf->channel,
733                                      hapd->iconf->ieee80211n,
734                                      hapd->iconf->secondary_channel)) {
735                         wpa_printf(MSG_ERROR, "Could not set channel for "
736                                    "kernel driver");
737                         return -1;
738                 }
739         }
740
741         if (iface->current_mode) {
742                 if (hostapd_prepare_rates(hapd, iface->current_mode)) {
743                         wpa_printf(MSG_ERROR, "Failed to prepare rates "
744                                    "table.");
745                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
746                                        HOSTAPD_LEVEL_WARNING,
747                                        "Failed to prepare rates table.");
748                         return -1;
749                 }
750         }
751
752         if (hapd->iconf->rts_threshold > -1 &&
753             hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
754                 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
755                            "kernel driver");
756                 return -1;
757         }
758
759         if (hapd->iconf->fragm_threshold > -1 &&
760             hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
761                 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
762                            "for kernel driver");
763                 return -1;
764         }
765
766         prev_addr = hapd->own_addr;
767
768         for (j = 0; j < iface->num_bss; j++) {
769                 hapd = iface->bss[j];
770                 if (j)
771                         os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
772                 if (hostapd_setup_bss(hapd, j == 0))
773                         return -1;
774                 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
775                         prev_addr = hapd->own_addr;
776         }
777
778         hostapd_tx_queue_params(iface);
779
780         ap_list_init(iface);
781
782         if (hostapd_driver_commit(hapd) < 0) {
783                 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
784                            "configuration", __func__);
785                 return -1;
786         }
787
788         wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
789                    iface->bss[0]->conf->iface);
790
791         return 0;
792 }
793
794
795 /**
796  * hostapd_setup_interface - Setup of an interface
797  * @iface: Pointer to interface data.
798  * Returns: 0 on success, -1 on failure
799  *
800  * Initializes the driver interface, validates the configuration,
801  * and sets driver parameters based on the configuration.
802  * Flushes old stations, sets the channel, encryption,
803  * beacons, and WDS links based on the configuration.
804  */
805 int hostapd_setup_interface(struct hostapd_iface *iface)
806 {
807         int ret;
808
809         ret = setup_interface(iface);
810         if (ret) {
811                 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
812                            iface->bss[0]->conf->iface);
813                 return -1;
814         }
815
816         return 0;
817 }
818
819
820 /**
821  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
822  * @hapd_iface: Pointer to interface data
823  * @conf: Pointer to per-interface configuration
824  * @bss: Pointer to per-BSS configuration for this BSS
825  * Returns: Pointer to allocated BSS data
826  *
827  * This function is used to allocate per-BSS data structure. This data will be
828  * freed after hostapd_cleanup() is called for it during interface
829  * deinitialization.
830  */
831 struct hostapd_data *
832 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
833                        struct hostapd_config *conf,
834                        struct hostapd_bss_config *bss)
835 {
836         struct hostapd_data *hapd;
837
838         hapd = os_zalloc(sizeof(*hapd));
839         if (hapd == NULL)
840                 return NULL;
841
842         hostapd_set_driver_ops(&hapd->drv);
843         hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
844         hapd->iconf = conf;
845         hapd->conf = bss;
846         hapd->iface = hapd_iface;
847         hapd->driver = hapd->iconf->driver;
848
849         return hapd;
850 }
851
852
853 void hostapd_interface_deinit(struct hostapd_iface *iface)
854 {
855         size_t j;
856
857         if (iface == NULL)
858                 return;
859
860         hostapd_cleanup_iface_pre(iface);
861         for (j = 0; j < iface->num_bss; j++) {
862                 struct hostapd_data *hapd = iface->bss[j];
863                 hostapd_free_stas(hapd);
864                 hostapd_flush_old_stations(hapd);
865                 hostapd_cleanup(hapd);
866         }
867 }
868
869
870 void hostapd_interface_free(struct hostapd_iface *iface)
871 {
872         size_t j;
873         for (j = 0; j < iface->num_bss; j++)
874                 os_free(iface->bss[j]);
875         hostapd_cleanup_iface(iface);
876 }
877
878
879 /**
880  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
881  * @hapd: Pointer to BSS data
882  * @sta: Pointer to the associated STA data
883  * @reassoc: 1 to indicate this was a re-association; 0 = first association
884  *
885  * This function will be called whenever a station associates with the AP. It
886  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
887  * from drv_callbacks.c based on driver events for drivers that take care of
888  * management frames (IEEE 802.11 authentication and association) internally.
889  */
890 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
891                            int reassoc)
892 {
893         if (hapd->tkip_countermeasures) {
894                 hostapd_drv_sta_deauth(hapd, sta->addr,
895                                        WLAN_REASON_MICHAEL_MIC_FAILURE);
896                 return;
897         }
898
899         hostapd_prune_associations(hapd, sta->addr);
900
901         /* IEEE 802.11F (IAPP) */
902         if (hapd->conf->ieee802_11f)
903                 iapp_new_station(hapd->iapp, sta);
904
905 #ifdef CONFIG_P2P
906         if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
907                 sta->no_p2p_set = 1;
908                 hapd->num_sta_no_p2p++;
909                 if (hapd->num_sta_no_p2p == 1)
910                         hostapd_p2p_non_p2p_sta_connected(hapd);
911         }
912 #endif /* CONFIG_P2P */
913
914         /* Start accounting here, if IEEE 802.1X and WPA are not used.
915          * IEEE 802.1X/WPA code will start accounting after the station has
916          * been authorized. */
917         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
918                 accounting_sta_start(hapd, sta);
919
920         /* Start IEEE 802.1X authentication process for new stations */
921         ieee802_1x_new_station(hapd, sta);
922         if (reassoc) {
923                 if (sta->auth_alg != WLAN_AUTH_FT &&
924                     !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
925                         wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
926         } else
927                 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
928 }