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