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