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