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