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