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