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