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