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