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