• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Initialization and configuration
3  * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "radius/radius_client.h"
15 #include "radius/radius_das.h"
16 #include "drivers/driver.h"
17 #include "hostapd.h"
18 #include "authsrv.h"
19 #include "sta_info.h"
20 #include "accounting.h"
21 #include "ap_list.h"
22 #include "beacon.h"
23 #include "iapp.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11_auth.h"
26 #include "vlan_init.h"
27 #include "wpa_auth.h"
28 #include "wps_hostapd.h"
29 #include "hw_features.h"
30 #include "wpa_auth_glue.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "p2p_hostapd.h"
34 #include "gas_serv.h"
35 
36 
37 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
38 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
39 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
40 
41 extern int wpa_debug_level;
42 extern struct wpa_driver_ops *wpa_drivers[];
43 
44 
hostapd_for_each_interface(struct hapd_interfaces * interfaces,int (* cb)(struct hostapd_iface * iface,void * ctx),void * ctx)45 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
46 			       int (*cb)(struct hostapd_iface *iface,
47 					 void *ctx), void *ctx)
48 {
49 	size_t i;
50 	int ret;
51 
52 	for (i = 0; i < interfaces->count; i++) {
53 		ret = cb(interfaces->iface[i], ctx);
54 		if (ret)
55 			return ret;
56 	}
57 
58 	return 0;
59 }
60 
61 
hostapd_reload_bss(struct hostapd_data * hapd)62 static void hostapd_reload_bss(struct hostapd_data *hapd)
63 {
64 #ifndef CONFIG_NO_RADIUS
65 	radius_client_reconfig(hapd->radius, hapd->conf->radius);
66 #endif /* CONFIG_NO_RADIUS */
67 
68 	if (hostapd_setup_wpa_psk(hapd->conf)) {
69 		wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
70 			   "after reloading configuration");
71 	}
72 
73 	if (hapd->conf->ieee802_1x || hapd->conf->wpa)
74 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
75 	else
76 		hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
77 
78 	if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
79 		hostapd_setup_wpa(hapd);
80 		if (hapd->wpa_auth)
81 			wpa_init_keys(hapd->wpa_auth);
82 	} else if (hapd->conf->wpa) {
83 		const u8 *wpa_ie;
84 		size_t wpa_ie_len;
85 		hostapd_reconfig_wpa(hapd);
86 		wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
87 		if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
88 			wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
89 				   "the kernel driver.");
90 	} else if (hapd->wpa_auth) {
91 		wpa_deinit(hapd->wpa_auth);
92 		hapd->wpa_auth = NULL;
93 		hostapd_set_privacy(hapd, 0);
94 		hostapd_setup_encryption(hapd->conf->iface, hapd);
95 		hostapd_set_generic_elem(hapd, (u8 *) "", 0);
96 	}
97 
98 	ieee802_11_set_beacon(hapd);
99 	hostapd_update_wps(hapd);
100 
101 	if (hapd->conf->ssid.ssid_set &&
102 	    hostapd_set_ssid(hapd, 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 	wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
108 }
109 
110 
hostapd_reload_config(struct hostapd_iface * iface)111 int hostapd_reload_config(struct hostapd_iface *iface)
112 {
113 	struct hostapd_data *hapd = iface->bss[0];
114 	struct hostapd_config *newconf, *oldconf;
115 	size_t j;
116 
117 	if (iface->interfaces == NULL ||
118 	    iface->interfaces->config_read_cb == NULL)
119 		return -1;
120 	newconf = iface->interfaces->config_read_cb(iface->config_fname);
121 	if (newconf == NULL)
122 		return -1;
123 
124 	/*
125 	 * Deauthenticate all stations since the new configuration may not
126 	 * allow them to use the BSS anymore.
127 	 */
128 	for (j = 0; j < iface->num_bss; j++) {
129 		hostapd_flush_old_stations(iface->bss[j],
130 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
131 		hostapd_broadcast_wep_clear(iface->bss[j]);
132 
133 #ifndef CONFIG_NO_RADIUS
134 		/* TODO: update dynamic data based on changed configuration
135 		 * items (e.g., open/close sockets, etc.) */
136 		radius_client_flush(iface->bss[j]->radius, 0);
137 #endif /* CONFIG_NO_RADIUS */
138 	}
139 
140 	oldconf = hapd->iconf;
141 	iface->conf = newconf;
142 
143 	for (j = 0; j < iface->num_bss; j++) {
144 		hapd = iface->bss[j];
145 		hapd->iconf = newconf;
146 		hapd->conf = &newconf->bss[j];
147 		hostapd_reload_bss(hapd);
148 	}
149 
150 	hostapd_config_free(oldconf);
151 
152 
153 	return 0;
154 }
155 
156 
hostapd_broadcast_key_clear_iface(struct hostapd_data * hapd,char * ifname)157 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
158 					      char *ifname)
159 {
160 	int i;
161 
162 	for (i = 0; i < NUM_WEP_KEYS; i++) {
163 		if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
164 					0, NULL, 0, NULL, 0)) {
165 			wpa_printf(MSG_DEBUG, "Failed to clear default "
166 				   "encryption keys (ifname=%s keyidx=%d)",
167 				   ifname, i);
168 		}
169 	}
170 #ifdef CONFIG_IEEE80211W
171 	if (hapd->conf->ieee80211w) {
172 		for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
173 			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
174 						NULL, i, 0, NULL,
175 						0, NULL, 0)) {
176 				wpa_printf(MSG_DEBUG, "Failed to clear "
177 					   "default mgmt encryption keys "
178 					   "(ifname=%s keyidx=%d)", ifname, i);
179 			}
180 		}
181 	}
182 #endif /* CONFIG_IEEE80211W */
183 }
184 
185 
hostapd_broadcast_wep_clear(struct hostapd_data * hapd)186 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
187 {
188 	hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
189 	return 0;
190 }
191 
192 
hostapd_broadcast_wep_set(struct hostapd_data * hapd)193 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
194 {
195 	int errors = 0, idx;
196 	struct hostapd_ssid *ssid = &hapd->conf->ssid;
197 
198 	idx = ssid->wep.idx;
199 	if (ssid->wep.default_len &&
200 	    hostapd_drv_set_key(hapd->conf->iface,
201 				hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
202 				1, NULL, 0, ssid->wep.key[idx],
203 				ssid->wep.len[idx])) {
204 		wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
205 		errors++;
206 	}
207 
208 	if (ssid->dyn_vlan_keys) {
209 		size_t i;
210 		for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
211 			const char *ifname;
212 			struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
213 			if (key == NULL)
214 				continue;
215 			ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
216 							    i);
217 			if (ifname == NULL)
218 				continue;
219 
220 			idx = key->idx;
221 			if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_WEP,
222 						broadcast_ether_addr, idx, 1,
223 						NULL, 0, key->key[idx],
224 						key->len[idx])) {
225 				wpa_printf(MSG_WARNING, "Could not set "
226 					   "dynamic VLAN WEP encryption.");
227 				errors++;
228 			}
229 		}
230 	}
231 
232 	return errors;
233 }
234 
235 
hostapd_free_hapd_data(struct hostapd_data * hapd)236 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
237 {
238 	iapp_deinit(hapd->iapp);
239 	hapd->iapp = NULL;
240 	accounting_deinit(hapd);
241 	hostapd_deinit_wpa(hapd);
242 	vlan_deinit(hapd);
243 	hostapd_acl_deinit(hapd);
244 #ifndef CONFIG_NO_RADIUS
245 	radius_client_deinit(hapd->radius);
246 	hapd->radius = NULL;
247 	radius_das_deinit(hapd->radius_das);
248 	hapd->radius_das = NULL;
249 #endif /* CONFIG_NO_RADIUS */
250 
251 	hostapd_deinit_wps(hapd);
252 
253 	authsrv_deinit(hapd);
254 
255 	if (hapd->interface_added &&
256 	    hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
257 		wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
258 			   hapd->conf->iface);
259 	}
260 
261 	os_free(hapd->probereq_cb);
262 	hapd->probereq_cb = NULL;
263 
264 #ifdef CONFIG_P2P
265 	wpabuf_free(hapd->p2p_beacon_ie);
266 	hapd->p2p_beacon_ie = NULL;
267 	wpabuf_free(hapd->p2p_probe_resp_ie);
268 	hapd->p2p_probe_resp_ie = NULL;
269 #endif /* CONFIG_P2P */
270 
271 	wpabuf_free(hapd->time_adv);
272 
273 #ifdef CONFIG_INTERWORKING
274 	gas_serv_deinit(hapd);
275 #endif /* CONFIG_INTERWORKING */
276 }
277 
278 
279 /**
280  * hostapd_cleanup - Per-BSS cleanup (deinitialization)
281  * @hapd: Pointer to BSS data
282  *
283  * This function is used to free all per-BSS data structures and resources.
284  * This gets called in a loop for each BSS between calls to
285  * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
286  * is deinitialized. Most of the modules that are initialized in
287  * hostapd_setup_bss() are deinitialized here.
288  */
hostapd_cleanup(struct hostapd_data * hapd)289 static void hostapd_cleanup(struct hostapd_data *hapd)
290 {
291 	if (hapd->iface->interfaces &&
292 	    hapd->iface->interfaces->ctrl_iface_deinit)
293 		hapd->iface->interfaces->ctrl_iface_deinit(hapd);
294 	hostapd_free_hapd_data(hapd);
295 }
296 
297 
298 /**
299  * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
300  * @iface: Pointer to interface data
301  *
302  * This function is called before per-BSS data structures are deinitialized
303  * with hostapd_cleanup().
304  */
hostapd_cleanup_iface_pre(struct hostapd_iface * iface)305 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
306 {
307 }
308 
309 
hostapd_cleanup_iface_partial(struct hostapd_iface * iface)310 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
311 {
312 	hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
313 	iface->hw_features = NULL;
314 	os_free(iface->current_rates);
315 	iface->current_rates = NULL;
316 	os_free(iface->basic_rates);
317 	iface->basic_rates = NULL;
318 	ap_list_deinit(iface);
319 }
320 
321 
322 /**
323  * hostapd_cleanup_iface - Complete per-interface cleanup
324  * @iface: Pointer to interface data
325  *
326  * This function is called after per-BSS data structures are deinitialized
327  * with hostapd_cleanup().
328  */
hostapd_cleanup_iface(struct hostapd_iface * iface)329 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
330 {
331 	hostapd_cleanup_iface_partial(iface);
332 	hostapd_config_free(iface->conf);
333 	iface->conf = NULL;
334 
335 	os_free(iface->config_fname);
336 	os_free(iface->bss);
337 	os_free(iface);
338 }
339 
340 
hostapd_clear_wep(struct hostapd_data * hapd)341 static void hostapd_clear_wep(struct hostapd_data *hapd)
342 {
343 	if (hapd->drv_priv) {
344 		hostapd_set_privacy(hapd, 0);
345 		hostapd_broadcast_wep_clear(hapd);
346 	}
347 }
348 
349 
hostapd_setup_encryption(char * iface,struct hostapd_data * hapd)350 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
351 {
352 	int i;
353 
354 	hostapd_broadcast_wep_set(hapd);
355 
356 	if (hapd->conf->ssid.wep.default_len) {
357 		hostapd_set_privacy(hapd, 1);
358 		return 0;
359 	}
360 
361 	/*
362 	 * When IEEE 802.1X is not enabled, the driver may need to know how to
363 	 * set authentication algorithms for static WEP.
364 	 */
365 	hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
366 
367 	for (i = 0; i < 4; i++) {
368 		if (hapd->conf->ssid.wep.key[i] &&
369 		    hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
370 					i == hapd->conf->ssid.wep.idx, NULL, 0,
371 					hapd->conf->ssid.wep.key[i],
372 					hapd->conf->ssid.wep.len[i])) {
373 			wpa_printf(MSG_WARNING, "Could not set WEP "
374 				   "encryption.");
375 			return -1;
376 		}
377 		if (hapd->conf->ssid.wep.key[i] &&
378 		    i == hapd->conf->ssid.wep.idx)
379 			hostapd_set_privacy(hapd, 1);
380 	}
381 
382 	return 0;
383 }
384 
385 
hostapd_flush_old_stations(struct hostapd_data * hapd,u16 reason)386 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
387 {
388 	int ret = 0;
389 	u8 addr[ETH_ALEN];
390 
391 	if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
392 		return 0;
393 
394 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
395 	if (hostapd_flush(hapd)) {
396 		wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
397 			"kernel driver");
398 		ret = -1;
399 	}
400 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
401 	os_memset(addr, 0xff, ETH_ALEN);
402 	hostapd_drv_sta_deauth(hapd, addr, reason);
403 	hostapd_free_stas(hapd);
404 
405 	return ret;
406 }
407 
408 
409 /**
410  * hostapd_validate_bssid_configuration - Validate BSSID configuration
411  * @iface: Pointer to interface data
412  * Returns: 0 on success, -1 on failure
413  *
414  * This function is used to validate that the configured BSSIDs are valid.
415  */
hostapd_validate_bssid_configuration(struct hostapd_iface * iface)416 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
417 {
418 	u8 mask[ETH_ALEN] = { 0 };
419 	struct hostapd_data *hapd = iface->bss[0];
420 	unsigned int i = iface->conf->num_bss, bits = 0, j;
421 	int auto_addr = 0;
422 
423 	if (hostapd_drv_none(hapd))
424 		return 0;
425 
426 	/* Generate BSSID mask that is large enough to cover the BSSIDs. */
427 
428 	/* Determine the bits necessary to cover the number of BSSIDs. */
429 	for (i--; i; i >>= 1)
430 		bits++;
431 
432 	/* Determine the bits necessary to any configured BSSIDs,
433 	   if they are higher than the number of BSSIDs. */
434 	for (j = 0; j < iface->conf->num_bss; j++) {
435 		if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
436 			if (j)
437 				auto_addr++;
438 			continue;
439 		}
440 
441 		for (i = 0; i < ETH_ALEN; i++) {
442 			mask[i] |=
443 				iface->conf->bss[j].bssid[i] ^
444 				hapd->own_addr[i];
445 		}
446 	}
447 
448 	if (!auto_addr)
449 		goto skip_mask_ext;
450 
451 	for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
452 		;
453 	j = 0;
454 	if (i < ETH_ALEN) {
455 		j = (5 - i) * 8;
456 
457 		while (mask[i] != 0) {
458 			mask[i] >>= 1;
459 			j++;
460 		}
461 	}
462 
463 	if (bits < j)
464 		bits = j;
465 
466 	if (bits > 40) {
467 		wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
468 			   bits);
469 		return -1;
470 	}
471 
472 	os_memset(mask, 0xff, ETH_ALEN);
473 	j = bits / 8;
474 	for (i = 5; i > 5 - j; i--)
475 		mask[i] = 0;
476 	j = bits % 8;
477 	while (j--)
478 		mask[i] <<= 1;
479 
480 skip_mask_ext:
481 	wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
482 		   (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
483 
484 	if (!auto_addr)
485 		return 0;
486 
487 	for (i = 0; i < ETH_ALEN; i++) {
488 		if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
489 			wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
490 				   " for start address " MACSTR ".",
491 				   MAC2STR(mask), MAC2STR(hapd->own_addr));
492 			wpa_printf(MSG_ERROR, "Start address must be the "
493 				   "first address in the block (i.e., addr "
494 				   "AND mask == addr).");
495 			return -1;
496 		}
497 	}
498 
499 	return 0;
500 }
501 
502 
mac_in_conf(struct hostapd_config * conf,const void * a)503 static int mac_in_conf(struct hostapd_config *conf, const void *a)
504 {
505 	size_t i;
506 
507 	for (i = 0; i < conf->num_bss; i++) {
508 		if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
509 			return 1;
510 		}
511 	}
512 
513 	return 0;
514 }
515 
516 
517 #ifndef CONFIG_NO_RADIUS
518 
hostapd_das_nas_mismatch(struct hostapd_data * hapd,struct radius_das_attrs * attr)519 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
520 				    struct radius_das_attrs *attr)
521 {
522 	/* TODO */
523 	return 0;
524 }
525 
526 
hostapd_das_find_sta(struct hostapd_data * hapd,struct radius_das_attrs * attr)527 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
528 					      struct radius_das_attrs *attr)
529 {
530 	struct sta_info *sta = NULL;
531 	char buf[128];
532 
533 	if (attr->sta_addr)
534 		sta = ap_get_sta(hapd, attr->sta_addr);
535 
536 	if (sta == NULL && attr->acct_session_id &&
537 	    attr->acct_session_id_len == 17) {
538 		for (sta = hapd->sta_list; sta; sta = sta->next) {
539 			os_snprintf(buf, sizeof(buf), "%08X-%08X",
540 				    sta->acct_session_id_hi,
541 				    sta->acct_session_id_lo);
542 			if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
543 				break;
544 		}
545 	}
546 
547 	if (sta == NULL && attr->cui) {
548 		for (sta = hapd->sta_list; sta; sta = sta->next) {
549 			struct wpabuf *cui;
550 			cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
551 			if (cui && wpabuf_len(cui) == attr->cui_len &&
552 			    os_memcmp(wpabuf_head(cui), attr->cui,
553 				      attr->cui_len) == 0)
554 				break;
555 		}
556 	}
557 
558 	if (sta == NULL && attr->user_name) {
559 		for (sta = hapd->sta_list; sta; sta = sta->next) {
560 			u8 *identity;
561 			size_t identity_len;
562 			identity = ieee802_1x_get_identity(sta->eapol_sm,
563 							   &identity_len);
564 			if (identity &&
565 			    identity_len == attr->user_name_len &&
566 			    os_memcmp(identity, attr->user_name, identity_len)
567 			    == 0)
568 				break;
569 		}
570 	}
571 
572 	return sta;
573 }
574 
575 
576 static enum radius_das_res
hostapd_das_disconnect(void * ctx,struct radius_das_attrs * attr)577 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
578 {
579 	struct hostapd_data *hapd = ctx;
580 	struct sta_info *sta;
581 
582 	if (hostapd_das_nas_mismatch(hapd, attr))
583 		return RADIUS_DAS_NAS_MISMATCH;
584 
585 	sta = hostapd_das_find_sta(hapd, attr);
586 	if (sta == NULL)
587 		return RADIUS_DAS_SESSION_NOT_FOUND;
588 
589 	hostapd_drv_sta_deauth(hapd, sta->addr,
590 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
591 	ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
592 
593 	return RADIUS_DAS_SUCCESS;
594 }
595 
596 #endif /* CONFIG_NO_RADIUS */
597 
598 
599 /**
600  * hostapd_setup_bss - Per-BSS setup (initialization)
601  * @hapd: Pointer to BSS data
602  * @first: Whether this BSS is the first BSS of an interface
603  *
604  * This function is used to initialize all per-BSS data structures and
605  * resources. This gets called in a loop for each BSS when an interface is
606  * initialized. Most of the modules that are initialized here will be
607  * deinitialized in hostapd_cleanup().
608  */
hostapd_setup_bss(struct hostapd_data * hapd,int first)609 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
610 {
611 	struct hostapd_bss_config *conf = hapd->conf;
612 	u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
613 	int ssid_len, set_ssid;
614 	char force_ifname[IFNAMSIZ];
615 	u8 if_addr[ETH_ALEN];
616 
617 	if (!first) {
618 		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
619 			/* Allocate the next available BSSID. */
620 			do {
621 				inc_byte_array(hapd->own_addr, ETH_ALEN);
622 			} while (mac_in_conf(hapd->iconf, hapd->own_addr));
623 		} else {
624 			/* Allocate the configured BSSID. */
625 			os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
626 
627 			if (hostapd_mac_comp(hapd->own_addr,
628 					     hapd->iface->bss[0]->own_addr) ==
629 			    0) {
630 				wpa_printf(MSG_ERROR, "BSS '%s' may not have "
631 					   "BSSID set to the MAC address of "
632 					   "the radio", hapd->conf->iface);
633 				return -1;
634 			}
635 		}
636 
637 		hapd->interface_added = 1;
638 		if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
639 				   hapd->conf->iface, hapd->own_addr, hapd,
640 				   &hapd->drv_priv, force_ifname, if_addr,
641 				   hapd->conf->bridge[0] ? hapd->conf->bridge :
642 				   NULL)) {
643 			wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
644 				   MACSTR ")", MAC2STR(hapd->own_addr));
645 			return -1;
646 		}
647 	}
648 
649 	if (conf->wmm_enabled < 0)
650 		conf->wmm_enabled = hapd->iconf->ieee80211n;
651 
652 	hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
653 	hostapd_set_privacy(hapd, 0);
654 
655 	hostapd_broadcast_wep_clear(hapd);
656 	if (hostapd_setup_encryption(hapd->conf->iface, hapd))
657 		return -1;
658 
659 	/*
660 	 * Fetch the SSID from the system and use it or,
661 	 * if one was specified in the config file, verify they
662 	 * match.
663 	 */
664 	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
665 	if (ssid_len < 0) {
666 		wpa_printf(MSG_ERROR, "Could not read SSID from system");
667 		return -1;
668 	}
669 	if (conf->ssid.ssid_set) {
670 		/*
671 		 * If SSID is specified in the config file and it differs
672 		 * from what is being used then force installation of the
673 		 * new SSID.
674 		 */
675 		set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
676 			    os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
677 	} else {
678 		/*
679 		 * No SSID in the config file; just use the one we got
680 		 * from the system.
681 		 */
682 		set_ssid = 0;
683 		conf->ssid.ssid_len = ssid_len;
684 		os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
685 	}
686 
687 	if (!hostapd_drv_none(hapd)) {
688 		wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
689 			   " and ssid \"%s\"",
690 			   hapd->conf->iface, MAC2STR(hapd->own_addr),
691 			   wpa_ssid_txt(hapd->conf->ssid.ssid,
692 					hapd->conf->ssid.ssid_len));
693 	}
694 
695 	if (hostapd_setup_wpa_psk(conf)) {
696 		wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
697 		return -1;
698 	}
699 
700 	/* Set SSID for the kernel driver (to be used in beacon and probe
701 	 * response frames) */
702 	if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
703 					 conf->ssid.ssid_len)) {
704 		wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
705 		return -1;
706 	}
707 
708 	if (wpa_debug_level == MSG_MSGDUMP)
709 		conf->radius->msg_dumps = 1;
710 #ifndef CONFIG_NO_RADIUS
711 	hapd->radius = radius_client_init(hapd, conf->radius);
712 	if (hapd->radius == NULL) {
713 		wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
714 		return -1;
715 	}
716 
717 	if (hapd->conf->radius_das_port) {
718 		struct radius_das_conf das_conf;
719 		os_memset(&das_conf, 0, sizeof(das_conf));
720 		das_conf.port = hapd->conf->radius_das_port;
721 		das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
722 		das_conf.shared_secret_len =
723 			hapd->conf->radius_das_shared_secret_len;
724 		das_conf.client_addr = &hapd->conf->radius_das_client_addr;
725 		das_conf.time_window = hapd->conf->radius_das_time_window;
726 		das_conf.require_event_timestamp =
727 			hapd->conf->radius_das_require_event_timestamp;
728 		das_conf.ctx = hapd;
729 		das_conf.disconnect = hostapd_das_disconnect;
730 		hapd->radius_das = radius_das_init(&das_conf);
731 		if (hapd->radius_das == NULL) {
732 			wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
733 				   "failed.");
734 			return -1;
735 		}
736 	}
737 #endif /* CONFIG_NO_RADIUS */
738 
739 	if (hostapd_acl_init(hapd)) {
740 		wpa_printf(MSG_ERROR, "ACL initialization failed.");
741 		return -1;
742 	}
743 	if (hostapd_init_wps(hapd, conf))
744 		return -1;
745 
746 	if (authsrv_init(hapd) < 0)
747 		return -1;
748 
749 	if (ieee802_1x_init(hapd)) {
750 		wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
751 		return -1;
752 	}
753 
754 	if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
755 		return -1;
756 
757 	if (accounting_init(hapd)) {
758 		wpa_printf(MSG_ERROR, "Accounting initialization failed.");
759 		return -1;
760 	}
761 
762 	if (hapd->conf->ieee802_11f &&
763 	    (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
764 		wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
765 			   "failed.");
766 		return -1;
767 	}
768 
769 #ifdef CONFIG_INTERWORKING
770 	if (gas_serv_init(hapd)) {
771 		wpa_printf(MSG_ERROR, "GAS server initialization failed");
772 		return -1;
773 	}
774 #endif /* CONFIG_INTERWORKING */
775 
776 	if (hapd->iface->interfaces &&
777 	    hapd->iface->interfaces->ctrl_iface_init &&
778 	    hapd->iface->interfaces->ctrl_iface_init(hapd)) {
779 		wpa_printf(MSG_ERROR, "Failed to setup control interface");
780 		return -1;
781 	}
782 
783 	if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
784 		wpa_printf(MSG_ERROR, "VLAN initialization failed.");
785 		return -1;
786 	}
787 
788 	ieee802_11_set_beacon(hapd);
789 
790 	if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
791 		return -1;
792 
793 	if (hapd->driver && hapd->driver->set_operstate)
794 		hapd->driver->set_operstate(hapd->drv_priv, 1);
795 
796 	return 0;
797 }
798 
799 
hostapd_tx_queue_params(struct hostapd_iface * iface)800 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
801 {
802 	struct hostapd_data *hapd = iface->bss[0];
803 	int i;
804 	struct hostapd_tx_queue_params *p;
805 
806 	for (i = 0; i < NUM_TX_QUEUES; i++) {
807 		p = &iface->conf->tx_queue[i];
808 
809 		if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
810 						p->cwmax, p->burst)) {
811 			wpa_printf(MSG_DEBUG, "Failed to set TX queue "
812 				   "parameters for queue %d.", i);
813 			/* Continue anyway */
814 		}
815 	}
816 }
817 
818 
setup_interface(struct hostapd_iface * iface)819 static int setup_interface(struct hostapd_iface *iface)
820 {
821 	struct hostapd_data *hapd = iface->bss[0];
822 	size_t i;
823 	char country[4];
824 
825 	/*
826 	 * Make sure that all BSSes get configured with a pointer to the same
827 	 * driver interface.
828 	 */
829 	for (i = 1; i < iface->num_bss; i++) {
830 		iface->bss[i]->driver = hapd->driver;
831 		iface->bss[i]->drv_priv = hapd->drv_priv;
832 	}
833 
834 	if (hostapd_validate_bssid_configuration(iface))
835 		return -1;
836 
837 	if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
838 		os_memcpy(country, hapd->iconf->country, 3);
839 		country[3] = '\0';
840 		if (hostapd_set_country(hapd, country) < 0) {
841 			wpa_printf(MSG_ERROR, "Failed to set country code");
842 			return -1;
843 		}
844 	}
845 
846 	if (hostapd_get_hw_features(iface)) {
847 		/* Not all drivers support this yet, so continue without hw
848 		 * feature data. */
849 	} else {
850 		int ret = hostapd_select_hw_mode(iface);
851 		if (ret < 0) {
852 			wpa_printf(MSG_ERROR, "Could not select hw_mode and "
853 				   "channel. (%d)", ret);
854 			return -1;
855 		}
856 		ret = hostapd_check_ht_capab(iface);
857 		if (ret < 0)
858 			return -1;
859 		if (ret == 1) {
860 			wpa_printf(MSG_DEBUG, "Interface initialization will "
861 				   "be completed in a callback");
862 			return 0;
863 		}
864 	}
865 	return hostapd_setup_interface_complete(iface, 0);
866 }
867 
868 
hostapd_setup_interface_complete(struct hostapd_iface * iface,int err)869 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
870 {
871 	struct hostapd_data *hapd = iface->bss[0];
872 	size_t j;
873 	u8 *prev_addr;
874 
875 	if (err) {
876 		wpa_printf(MSG_ERROR, "Interface initialization failed");
877 		eloop_terminate();
878 		return -1;
879 	}
880 
881 	wpa_printf(MSG_DEBUG, "Completing interface initialization");
882 	if (hapd->iconf->channel) {
883 		iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
884 		wpa_printf(MSG_DEBUG, "Mode: %s  Channel: %d  "
885 			   "Frequency: %d MHz",
886 			   hostapd_hw_mode_txt(hapd->iconf->hw_mode),
887 			   hapd->iconf->channel, iface->freq);
888 
889 		if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
890 				     hapd->iconf->channel,
891 				     hapd->iconf->ieee80211n,
892 				     hapd->iconf->secondary_channel)) {
893 			wpa_printf(MSG_ERROR, "Could not set channel for "
894 				   "kernel driver");
895 			return -1;
896 		}
897 	}
898 
899 	if (iface->current_mode) {
900 		if (hostapd_prepare_rates(iface, iface->current_mode)) {
901 			wpa_printf(MSG_ERROR, "Failed to prepare rates "
902 				   "table.");
903 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
904 				       HOSTAPD_LEVEL_WARNING,
905 				       "Failed to prepare rates table.");
906 			return -1;
907 		}
908 	}
909 
910 	if (hapd->iconf->rts_threshold > -1 &&
911 	    hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
912 		wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
913 			   "kernel driver");
914 		return -1;
915 	}
916 
917 	if (hapd->iconf->fragm_threshold > -1 &&
918 	    hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
919 		wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
920 			   "for kernel driver");
921 		return -1;
922 	}
923 
924 	prev_addr = hapd->own_addr;
925 
926 	for (j = 0; j < iface->num_bss; j++) {
927 		hapd = iface->bss[j];
928 		if (j)
929 			os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
930 		if (hostapd_setup_bss(hapd, j == 0))
931 			return -1;
932 		if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
933 			prev_addr = hapd->own_addr;
934 	}
935 
936 	hostapd_tx_queue_params(iface);
937 
938 	ap_list_init(iface);
939 
940 	if (hostapd_driver_commit(hapd) < 0) {
941 		wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
942 			   "configuration", __func__);
943 		return -1;
944 	}
945 
946 	/*
947 	 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
948 	 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
949 	 * mode), the interface is up only after driver_commit, so initialize
950 	 * WPS after driver_commit.
951 	 */
952 	for (j = 0; j < iface->num_bss; j++) {
953 		if (hostapd_init_wps_complete(iface->bss[j]))
954 			return -1;
955 	}
956 
957 	if (hapd->setup_complete_cb)
958 		hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
959 
960 	wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
961 		   iface->bss[0]->conf->iface);
962 
963 	return 0;
964 }
965 
966 
967 /**
968  * hostapd_setup_interface - Setup of an interface
969  * @iface: Pointer to interface data.
970  * Returns: 0 on success, -1 on failure
971  *
972  * Initializes the driver interface, validates the configuration,
973  * and sets driver parameters based on the configuration.
974  * Flushes old stations, sets the channel, encryption,
975  * beacons, and WDS links based on the configuration.
976  */
hostapd_setup_interface(struct hostapd_iface * iface)977 int hostapd_setup_interface(struct hostapd_iface *iface)
978 {
979 	int ret;
980 
981 	ret = setup_interface(iface);
982 	if (ret) {
983 		wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
984 			   iface->bss[0]->conf->iface);
985 		return -1;
986 	}
987 
988 	return 0;
989 }
990 
991 
992 /**
993  * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
994  * @hapd_iface: Pointer to interface data
995  * @conf: Pointer to per-interface configuration
996  * @bss: Pointer to per-BSS configuration for this BSS
997  * Returns: Pointer to allocated BSS data
998  *
999  * This function is used to allocate per-BSS data structure. This data will be
1000  * freed after hostapd_cleanup() is called for it during interface
1001  * deinitialization.
1002  */
1003 struct hostapd_data *
hostapd_alloc_bss_data(struct hostapd_iface * hapd_iface,struct hostapd_config * conf,struct hostapd_bss_config * bss)1004 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1005 		       struct hostapd_config *conf,
1006 		       struct hostapd_bss_config *bss)
1007 {
1008 	struct hostapd_data *hapd;
1009 
1010 	hapd = os_zalloc(sizeof(*hapd));
1011 	if (hapd == NULL)
1012 		return NULL;
1013 
1014 	hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1015 	hapd->iconf = conf;
1016 	hapd->conf = bss;
1017 	hapd->iface = hapd_iface;
1018 	hapd->driver = hapd->iconf->driver;
1019 	hapd->ctrl_sock = -1;
1020 
1021 	return hapd;
1022 }
1023 
1024 
hostapd_interface_deinit(struct hostapd_iface * iface)1025 void hostapd_interface_deinit(struct hostapd_iface *iface)
1026 {
1027 	size_t j;
1028 
1029 	if (iface == NULL)
1030 		return;
1031 
1032 	hostapd_cleanup_iface_pre(iface);
1033 	for (j = 0; j < iface->num_bss; j++) {
1034 		struct hostapd_data *hapd = iface->bss[j];
1035 		hostapd_free_stas(hapd);
1036 		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1037 		hostapd_clear_wep(hapd);
1038 		hostapd_cleanup(hapd);
1039 	}
1040 }
1041 
1042 
hostapd_interface_free(struct hostapd_iface * iface)1043 void hostapd_interface_free(struct hostapd_iface *iface)
1044 {
1045 	size_t j;
1046 	for (j = 0; j < iface->num_bss; j++)
1047 		os_free(iface->bss[j]);
1048 	hostapd_cleanup_iface(iface);
1049 }
1050 
1051 
1052 #ifdef HOSTAPD
1053 
hostapd_interface_deinit_free(struct hostapd_iface * iface)1054 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1055 {
1056 	const struct wpa_driver_ops *driver;
1057 	void *drv_priv;
1058 	if (iface == NULL)
1059 		return;
1060 	driver = iface->bss[0]->driver;
1061 	drv_priv = iface->bss[0]->drv_priv;
1062 	hostapd_interface_deinit(iface);
1063 	if (driver && driver->hapd_deinit && drv_priv)
1064 		driver->hapd_deinit(drv_priv);
1065 	hostapd_interface_free(iface);
1066 }
1067 
1068 
hostapd_enable_iface(struct hostapd_iface * hapd_iface)1069 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1070 {
1071 	if (hapd_iface->bss[0]->drv_priv != NULL) {
1072 		wpa_printf(MSG_ERROR, "Interface %s already enabled",
1073 			   hapd_iface->conf->bss[0].iface);
1074 		return -1;
1075 	}
1076 
1077 	wpa_printf(MSG_DEBUG, "Enable interface %s",
1078 		   hapd_iface->conf->bss[0].iface);
1079 
1080 	if (hapd_iface->interfaces == NULL ||
1081 	    hapd_iface->interfaces->driver_init == NULL ||
1082 	    hapd_iface->interfaces->driver_init(hapd_iface) ||
1083 	    hostapd_setup_interface(hapd_iface)) {
1084 		hostapd_interface_deinit_free(hapd_iface);
1085 		return -1;
1086 	}
1087 	return 0;
1088 }
1089 
1090 
hostapd_reload_iface(struct hostapd_iface * hapd_iface)1091 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1092 {
1093 	size_t j;
1094 
1095 	wpa_printf(MSG_DEBUG, "Reload interface %s",
1096 		   hapd_iface->conf->bss[0].iface);
1097 	for (j = 0; j < hapd_iface->num_bss; j++) {
1098 		hostapd_flush_old_stations(hapd_iface->bss[j],
1099 					   WLAN_REASON_PREV_AUTH_NOT_VALID);
1100 
1101 #ifndef CONFIG_NO_RADIUS
1102 		/* TODO: update dynamic data based on changed configuration
1103 		 * items (e.g., open/close sockets, etc.) */
1104 		radius_client_flush(hapd_iface->bss[j]->radius, 0);
1105 #endif  /* CONFIG_NO_RADIUS */
1106 
1107 		hostapd_reload_bss(hapd_iface->bss[j]);
1108 	}
1109 	return 0;
1110 }
1111 
1112 
hostapd_disable_iface(struct hostapd_iface * hapd_iface)1113 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1114 {
1115 	size_t j;
1116 	struct hostapd_bss_config *bss = hapd_iface->bss[0]->conf;
1117 	const struct wpa_driver_ops *driver;
1118 	void *drv_priv;
1119 
1120 	if (hapd_iface == NULL)
1121 		return -1;
1122 	driver = hapd_iface->bss[0]->driver;
1123 	drv_priv = hapd_iface->bss[0]->drv_priv;
1124 
1125 	/* whatever hostapd_interface_deinit does */
1126 	for (j = 0; j < hapd_iface->num_bss; j++) {
1127 		struct hostapd_data *hapd = hapd_iface->bss[j];
1128 		hostapd_free_stas(hapd);
1129 		hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1130 		hostapd_clear_wep(hapd);
1131 		hostapd_free_hapd_data(hapd);
1132 	}
1133 
1134 	if (driver && driver->hapd_deinit && drv_priv) {
1135 		driver->hapd_deinit(drv_priv);
1136 		hapd_iface->bss[0]->drv_priv = NULL;
1137 	}
1138 
1139 	/* From hostapd_cleanup_iface: These were initialized in
1140 	 * hostapd_setup_interface and hostapd_setup_interface_complete
1141 	 */
1142 	hostapd_cleanup_iface_partial(hapd_iface);
1143 	bss->wpa = 0;
1144 	bss->wpa_key_mgmt = -1;
1145 	bss->wpa_pairwise = -1;
1146 
1147 	wpa_printf(MSG_DEBUG, "Interface %s disabled", bss->iface);
1148 	return 0;
1149 }
1150 
1151 
1152 static struct hostapd_iface *
hostapd_iface_alloc(struct hapd_interfaces * interfaces)1153 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1154 {
1155 	struct hostapd_iface **iface, *hapd_iface;
1156 
1157 	iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1158 				 sizeof(struct hostapd_iface *));
1159 	if (iface == NULL)
1160 		return NULL;
1161 	interfaces->iface = iface;
1162 	hapd_iface = interfaces->iface[interfaces->count] =
1163 		os_zalloc(sizeof(*hapd_iface));
1164 	if (hapd_iface == NULL) {
1165 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1166 			   "the interface", __func__);
1167 		return NULL;
1168 	}
1169 	interfaces->count++;
1170 	hapd_iface->interfaces = interfaces;
1171 
1172 	return hapd_iface;
1173 }
1174 
1175 
1176 static struct hostapd_config *
hostapd_config_alloc(struct hapd_interfaces * interfaces,const char * ifname,const char * ctrl_iface)1177 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1178 		     const char *ctrl_iface)
1179 {
1180 	struct hostapd_bss_config *bss;
1181 	struct hostapd_config *conf;
1182 
1183 	/* Allocates memory for bss and conf */
1184 	conf = hostapd_config_defaults();
1185 	if (conf == NULL) {
1186 		 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1187 				"configuration", __func__);
1188 		return NULL;
1189 	}
1190 
1191 	conf->driver = wpa_drivers[0];
1192 	if (conf->driver == NULL) {
1193 		wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1194 		hostapd_config_free(conf);
1195 		return NULL;
1196 	}
1197 
1198 	bss = conf->last_bss = conf->bss;
1199 
1200 	os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1201 	bss->ctrl_interface = os_strdup(ctrl_iface);
1202 	if (bss->ctrl_interface == NULL) {
1203 		hostapd_config_free(conf);
1204 		return NULL;
1205 	}
1206 
1207 	/* Reading configuration file skipped, will be done in SET!
1208 	 * From reading the configuration till the end has to be done in
1209 	 * SET
1210 	 */
1211 	return conf;
1212 }
1213 
1214 
hostapd_data_alloc(struct hapd_interfaces * interfaces,struct hostapd_config * conf)1215 static struct hostapd_iface * hostapd_data_alloc(
1216 	struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1217 {
1218 	size_t i;
1219 	struct hostapd_iface *hapd_iface =
1220 		interfaces->iface[interfaces->count - 1];
1221 	struct hostapd_data *hapd;
1222 
1223 	hapd_iface->conf = conf;
1224 	hapd_iface->num_bss = conf->num_bss;
1225 
1226 	hapd_iface->bss = os_zalloc(conf->num_bss *
1227 				    sizeof(struct hostapd_data *));
1228 	if (hapd_iface->bss == NULL)
1229 		return NULL;
1230 
1231 	for (i = 0; i < conf->num_bss; i++) {
1232 		hapd = hapd_iface->bss[i] =
1233 			hostapd_alloc_bss_data(hapd_iface, conf,
1234 					       &conf->bss[i]);
1235 		if (hapd == NULL)
1236 			return NULL;
1237 		hapd->msg_ctx = hapd;
1238 	}
1239 
1240 	hapd_iface->interfaces = interfaces;
1241 
1242 	return hapd_iface;
1243 }
1244 
1245 
hostapd_add_iface(struct hapd_interfaces * interfaces,char * buf)1246 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1247 {
1248 	struct hostapd_config *conf = NULL;
1249 	struct hostapd_iface *hapd_iface = NULL;
1250 	char *ptr;
1251 	size_t i;
1252 
1253 	ptr = os_strchr(buf, ' ');
1254 	if (ptr == NULL)
1255 		return -1;
1256 	*ptr++ = '\0';
1257 
1258 	for (i = 0; i < interfaces->count; i++) {
1259 		if (!os_strcmp(interfaces->iface[i]->conf->bss[0].iface,
1260 			       buf)) {
1261 			wpa_printf(MSG_INFO, "Cannot add interface - it "
1262 				   "already exists");
1263 			return -1;
1264 		}
1265 	}
1266 
1267 	hapd_iface = hostapd_iface_alloc(interfaces);
1268 	if (hapd_iface == NULL) {
1269 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1270 			   "for interface", __func__);
1271 		goto fail;
1272 	}
1273 
1274 	conf = hostapd_config_alloc(interfaces, buf, ptr);
1275 	if (conf == NULL) {
1276 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1277 			   "for configuration", __func__);
1278 		goto fail;
1279 	}
1280 
1281 	hapd_iface = hostapd_data_alloc(interfaces, conf);
1282 	if (hapd_iface == NULL) {
1283 		wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1284 			   "for hostapd", __func__);
1285 		goto fail;
1286 	}
1287 
1288 	if (hapd_iface->interfaces &&
1289 	    hapd_iface->interfaces->ctrl_iface_init &&
1290 	    hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1291 		wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1292 			   "interface", __func__);
1293 		goto fail;
1294 	}
1295 	wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0].iface);
1296 
1297 	return 0;
1298 
1299 fail:
1300 	if (conf)
1301 		hostapd_config_free(conf);
1302 	if (hapd_iface) {
1303 		os_free(hapd_iface->bss[interfaces->count]);
1304 		os_free(hapd_iface);
1305 	}
1306 	return -1;
1307 }
1308 
1309 
hostapd_remove_iface(struct hapd_interfaces * interfaces,char * buf)1310 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1311 {
1312 	struct hostapd_iface *hapd_iface;
1313 	size_t i, k = 0;
1314 
1315 	for (i = 0; i < interfaces->count; i++) {
1316 		hapd_iface = interfaces->iface[i];
1317 		if (hapd_iface == NULL)
1318 			return -1;
1319 		if (!os_strcmp(hapd_iface->conf->bss[0].iface, buf)) {
1320 			wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1321 			hostapd_interface_deinit_free(hapd_iface);
1322 			k = i;
1323 			while (k < (interfaces->count - 1)) {
1324 				interfaces->iface[k] =
1325 					interfaces->iface[k + 1];
1326 				k++;
1327 			}
1328 			interfaces->count--;
1329 			return 0;
1330 		}
1331 	}
1332 	return -1;
1333 }
1334 
1335 #endif /* HOSTAPD */
1336 
1337 
1338 /**
1339  * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1340  * @hapd: Pointer to BSS data
1341  * @sta: Pointer to the associated STA data
1342  * @reassoc: 1 to indicate this was a re-association; 0 = first association
1343  *
1344  * This function will be called whenever a station associates with the AP. It
1345  * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1346  * from drv_callbacks.c based on driver events for drivers that take care of
1347  * management frames (IEEE 802.11 authentication and association) internally.
1348  */
hostapd_new_assoc_sta(struct hostapd_data * hapd,struct sta_info * sta,int reassoc)1349 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1350 			   int reassoc)
1351 {
1352 	if (hapd->tkip_countermeasures) {
1353 		hostapd_drv_sta_deauth(hapd, sta->addr,
1354 				       WLAN_REASON_MICHAEL_MIC_FAILURE);
1355 		return;
1356 	}
1357 
1358 	hostapd_prune_associations(hapd, sta->addr);
1359 
1360 	/* IEEE 802.11F (IAPP) */
1361 	if (hapd->conf->ieee802_11f)
1362 		iapp_new_station(hapd->iapp, sta);
1363 
1364 #ifdef CONFIG_P2P
1365 	if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1366 		sta->no_p2p_set = 1;
1367 		hapd->num_sta_no_p2p++;
1368 		if (hapd->num_sta_no_p2p == 1)
1369 			hostapd_p2p_non_p2p_sta_connected(hapd);
1370 	}
1371 #endif /* CONFIG_P2P */
1372 
1373 	/* Start accounting here, if IEEE 802.1X and WPA are not used.
1374 	 * IEEE 802.1X/WPA code will start accounting after the station has
1375 	 * been authorized. */
1376 	if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
1377 		accounting_sta_start(hapd, sta);
1378 
1379 	/* Start IEEE 802.1X authentication process for new stations */
1380 	ieee802_1x_new_station(hapd, sta);
1381 	if (reassoc) {
1382 		if (sta->auth_alg != WLAN_AUTH_FT &&
1383 		    !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1384 			wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1385 	} else
1386 		wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1387 
1388 	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1389 		   "for " MACSTR " (%d seconds - ap_max_inactivity)",
1390 		   __func__, MAC2STR(sta->addr),
1391 		   hapd->conf->ap_max_inactivity);
1392 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1393 	eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1394 			       ap_handle_timer, hapd, sta);
1395 }
1396