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