]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/hostapd.c
hostapd: Skip some configuration steps for mesh cases
[thirdparty/hostap.git] / src / ap / hostapd.c
1 /*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2014, 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 "common/wpa_ctrl.h"
15 #include "radius/radius_client.h"
16 #include "radius/radius_das.h"
17 #include "eap_server/tncs.h"
18 #include "hostapd.h"
19 #include "authsrv.h"
20 #include "sta_info.h"
21 #include "accounting.h"
22 #include "ap_list.h"
23 #include "beacon.h"
24 #include "iapp.h"
25 #include "ieee802_1x.h"
26 #include "ieee802_11_auth.h"
27 #include "vlan_init.h"
28 #include "wpa_auth.h"
29 #include "wps_hostapd.h"
30 #include "hw_features.h"
31 #include "wpa_auth_glue.h"
32 #include "ap_drv_ops.h"
33 #include "ap_config.h"
34 #include "p2p_hostapd.h"
35 #include "gas_serv.h"
36 #include "dfs.h"
37 #include "ieee802_11.h"
38 #include "bss_load.h"
39
40
41 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason);
42 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
43 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd);
44 static int setup_interface2(struct hostapd_iface *iface);
45 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx);
46
47
48 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
49 int (*cb)(struct hostapd_iface *iface,
50 void *ctx), void *ctx)
51 {
52 size_t i;
53 int ret;
54
55 for (i = 0; i < interfaces->count; i++) {
56 ret = cb(interfaces->iface[i], ctx);
57 if (ret)
58 return ret;
59 }
60
61 return 0;
62 }
63
64
65 static void hostapd_reload_bss(struct hostapd_data *hapd)
66 {
67 struct hostapd_ssid *ssid;
68
69 #ifndef CONFIG_NO_RADIUS
70 radius_client_reconfig(hapd->radius, hapd->conf->radius);
71 #endif /* CONFIG_NO_RADIUS */
72
73 ssid = &hapd->conf->ssid;
74 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
75 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
76 /*
77 * Force PSK to be derived again since SSID or passphrase may
78 * have changed.
79 */
80 os_free(ssid->wpa_psk);
81 ssid->wpa_psk = NULL;
82 }
83 if (hostapd_setup_wpa_psk(hapd->conf)) {
84 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
85 "after reloading configuration");
86 }
87
88 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
89 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
90 else
91 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
92
93 if ((hapd->conf->wpa || hapd->conf->osen) && hapd->wpa_auth == NULL) {
94 hostapd_setup_wpa(hapd);
95 if (hapd->wpa_auth)
96 wpa_init_keys(hapd->wpa_auth);
97 } else if (hapd->conf->wpa) {
98 const u8 *wpa_ie;
99 size_t wpa_ie_len;
100 hostapd_reconfig_wpa(hapd);
101 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
102 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
103 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
104 "the kernel driver.");
105 } else if (hapd->wpa_auth) {
106 wpa_deinit(hapd->wpa_auth);
107 hapd->wpa_auth = NULL;
108 hostapd_set_privacy(hapd, 0);
109 hostapd_setup_encryption(hapd->conf->iface, hapd);
110 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
111 }
112
113 ieee802_11_set_beacon(hapd);
114 hostapd_update_wps(hapd);
115
116 if (hapd->conf->ssid.ssid_set &&
117 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
118 hapd->conf->ssid.ssid_len)) {
119 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
120 /* try to continue */
121 }
122 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
123 }
124
125
126 static void hostapd_clear_old(struct hostapd_iface *iface)
127 {
128 size_t j;
129
130 /*
131 * Deauthenticate all stations since the new configuration may not
132 * allow them to use the BSS anymore.
133 */
134 for (j = 0; j < iface->num_bss; j++) {
135 hostapd_flush_old_stations(iface->bss[j],
136 WLAN_REASON_PREV_AUTH_NOT_VALID);
137 hostapd_broadcast_wep_clear(iface->bss[j]);
138
139 #ifndef CONFIG_NO_RADIUS
140 /* TODO: update dynamic data based on changed configuration
141 * items (e.g., open/close sockets, etc.) */
142 radius_client_flush(iface->bss[j]->radius, 0);
143 #endif /* CONFIG_NO_RADIUS */
144 }
145 }
146
147
148 int hostapd_reload_config(struct hostapd_iface *iface)
149 {
150 struct hostapd_data *hapd = iface->bss[0];
151 struct hostapd_config *newconf, *oldconf;
152 size_t j;
153
154 if (iface->config_fname == NULL) {
155 /* Only in-memory config in use - assume it has been updated */
156 hostapd_clear_old(iface);
157 for (j = 0; j < iface->num_bss; j++)
158 hostapd_reload_bss(iface->bss[j]);
159 return 0;
160 }
161
162 if (iface->interfaces == NULL ||
163 iface->interfaces->config_read_cb == NULL)
164 return -1;
165 newconf = iface->interfaces->config_read_cb(iface->config_fname);
166 if (newconf == NULL)
167 return -1;
168
169 hostapd_clear_old(iface);
170
171 oldconf = hapd->iconf;
172 iface->conf = newconf;
173
174 for (j = 0; j < iface->num_bss; j++) {
175 hapd = iface->bss[j];
176 hapd->iconf = newconf;
177 hapd->iconf->channel = oldconf->channel;
178 hapd->iconf->secondary_channel = oldconf->secondary_channel;
179 hapd->iconf->ieee80211n = oldconf->ieee80211n;
180 hapd->iconf->ieee80211ac = oldconf->ieee80211ac;
181 hapd->iconf->ht_capab = oldconf->ht_capab;
182 hapd->iconf->vht_capab = oldconf->vht_capab;
183 hapd->iconf->vht_oper_chwidth = oldconf->vht_oper_chwidth;
184 hapd->iconf->vht_oper_centr_freq_seg0_idx =
185 oldconf->vht_oper_centr_freq_seg0_idx;
186 hapd->iconf->vht_oper_centr_freq_seg1_idx =
187 oldconf->vht_oper_centr_freq_seg1_idx;
188 hapd->conf = newconf->bss[j];
189 hostapd_reload_bss(hapd);
190 }
191
192 hostapd_config_free(oldconf);
193
194
195 return 0;
196 }
197
198
199 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
200 char *ifname)
201 {
202 int i;
203
204 for (i = 0; i < NUM_WEP_KEYS; i++) {
205 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
206 0, NULL, 0, NULL, 0)) {
207 wpa_printf(MSG_DEBUG, "Failed to clear default "
208 "encryption keys (ifname=%s keyidx=%d)",
209 ifname, i);
210 }
211 }
212 #ifdef CONFIG_IEEE80211W
213 if (hapd->conf->ieee80211w) {
214 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
215 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
216 NULL, i, 0, NULL,
217 0, NULL, 0)) {
218 wpa_printf(MSG_DEBUG, "Failed to clear "
219 "default mgmt encryption keys "
220 "(ifname=%s keyidx=%d)", ifname, i);
221 }
222 }
223 }
224 #endif /* CONFIG_IEEE80211W */
225 }
226
227
228 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
229 {
230 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
231 return 0;
232 }
233
234
235 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
236 {
237 int errors = 0, idx;
238 struct hostapd_ssid *ssid = &hapd->conf->ssid;
239
240 idx = ssid->wep.idx;
241 if (ssid->wep.default_len &&
242 hostapd_drv_set_key(hapd->conf->iface,
243 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
244 1, NULL, 0, ssid->wep.key[idx],
245 ssid->wep.len[idx])) {
246 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
247 errors++;
248 }
249
250 return errors;
251 }
252
253
254 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
255 {
256 if (!hapd->started) {
257 wpa_printf(MSG_ERROR, "%s: Interface %s wasn't started",
258 __func__, hapd->conf->iface);
259 return;
260 }
261 hapd->started = 0;
262
263 wpa_printf(MSG_DEBUG, "%s(%s)", __func__, hapd->conf->iface);
264 iapp_deinit(hapd->iapp);
265 hapd->iapp = NULL;
266 accounting_deinit(hapd);
267 hostapd_deinit_wpa(hapd);
268 vlan_deinit(hapd);
269 hostapd_acl_deinit(hapd);
270 #ifndef CONFIG_NO_RADIUS
271 radius_client_deinit(hapd->radius);
272 hapd->radius = NULL;
273 radius_das_deinit(hapd->radius_das);
274 hapd->radius_das = NULL;
275 #endif /* CONFIG_NO_RADIUS */
276
277 hostapd_deinit_wps(hapd);
278
279 authsrv_deinit(hapd);
280
281 if (hapd->interface_added) {
282 hapd->interface_added = 0;
283 if (hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
284 wpa_printf(MSG_WARNING,
285 "Failed to remove BSS interface %s",
286 hapd->conf->iface);
287 hapd->interface_added = 1;
288 } else {
289 /*
290 * Since this was a dynamically added interface, the
291 * driver wrapper may have removed its internal instance
292 * and hapd->drv_priv is not valid anymore.
293 */
294 hapd->drv_priv = NULL;
295 }
296 }
297
298 os_free(hapd->probereq_cb);
299 hapd->probereq_cb = NULL;
300
301 #ifdef CONFIG_P2P
302 wpabuf_free(hapd->p2p_beacon_ie);
303 hapd->p2p_beacon_ie = NULL;
304 wpabuf_free(hapd->p2p_probe_resp_ie);
305 hapd->p2p_probe_resp_ie = NULL;
306 #endif /* CONFIG_P2P */
307
308 wpabuf_free(hapd->time_adv);
309
310 #ifdef CONFIG_INTERWORKING
311 gas_serv_deinit(hapd);
312 #endif /* CONFIG_INTERWORKING */
313
314 bss_load_update_deinit(hapd);
315
316 #ifdef CONFIG_SQLITE
317 bin_clear_free(hapd->tmp_eap_user.identity,
318 hapd->tmp_eap_user.identity_len);
319 bin_clear_free(hapd->tmp_eap_user.password,
320 hapd->tmp_eap_user.password_len);
321 #endif /* CONFIG_SQLITE */
322 }
323
324
325 /**
326 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
327 * @hapd: Pointer to BSS data
328 *
329 * This function is used to free all per-BSS data structures and resources.
330 * Most of the modules that are initialized in hostapd_setup_bss() are
331 * deinitialized here.
332 */
333 static void hostapd_cleanup(struct hostapd_data *hapd)
334 {
335 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s))", __func__, hapd,
336 hapd->conf->iface);
337 if (hapd->iface->interfaces &&
338 hapd->iface->interfaces->ctrl_iface_deinit)
339 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
340 hostapd_free_hapd_data(hapd);
341 }
342
343
344 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
345 {
346 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
347 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
348 iface->hw_features = NULL;
349 os_free(iface->current_rates);
350 iface->current_rates = NULL;
351 os_free(iface->basic_rates);
352 iface->basic_rates = NULL;
353 ap_list_deinit(iface);
354 }
355
356
357 /**
358 * hostapd_cleanup_iface - Complete per-interface cleanup
359 * @iface: Pointer to interface data
360 *
361 * This function is called after per-BSS data structures are deinitialized
362 * with hostapd_cleanup().
363 */
364 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
365 {
366 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
367 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
368
369 hostapd_cleanup_iface_partial(iface);
370 hostapd_config_free(iface->conf);
371 iface->conf = NULL;
372
373 os_free(iface->config_fname);
374 os_free(iface->bss);
375 wpa_printf(MSG_DEBUG, "%s: free iface=%p", __func__, iface);
376 os_free(iface);
377 }
378
379
380 static void hostapd_clear_wep(struct hostapd_data *hapd)
381 {
382 if (hapd->drv_priv && !hapd->iface->driver_ap_teardown) {
383 hostapd_set_privacy(hapd, 0);
384 hostapd_broadcast_wep_clear(hapd);
385 }
386 }
387
388
389 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
390 {
391 int i;
392
393 hostapd_broadcast_wep_set(hapd);
394
395 if (hapd->conf->ssid.wep.default_len) {
396 hostapd_set_privacy(hapd, 1);
397 return 0;
398 }
399
400 /*
401 * When IEEE 802.1X is not enabled, the driver may need to know how to
402 * set authentication algorithms for static WEP.
403 */
404 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
405
406 for (i = 0; i < 4; i++) {
407 if (hapd->conf->ssid.wep.key[i] &&
408 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
409 i == hapd->conf->ssid.wep.idx, NULL, 0,
410 hapd->conf->ssid.wep.key[i],
411 hapd->conf->ssid.wep.len[i])) {
412 wpa_printf(MSG_WARNING, "Could not set WEP "
413 "encryption.");
414 return -1;
415 }
416 if (hapd->conf->ssid.wep.key[i] &&
417 i == hapd->conf->ssid.wep.idx)
418 hostapd_set_privacy(hapd, 1);
419 }
420
421 return 0;
422 }
423
424
425 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
426 {
427 int ret = 0;
428 u8 addr[ETH_ALEN];
429
430 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
431 return 0;
432
433 if (!hapd->iface->driver_ap_teardown) {
434 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
435 "Flushing old station entries");
436
437 if (hostapd_flush(hapd)) {
438 wpa_msg(hapd->msg_ctx, MSG_WARNING,
439 "Could not connect to kernel driver");
440 ret = -1;
441 }
442 }
443 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
444 os_memset(addr, 0xff, ETH_ALEN);
445 hostapd_drv_sta_deauth(hapd, addr, reason);
446 hostapd_free_stas(hapd);
447
448 return ret;
449 }
450
451
452 static void hostapd_bss_deinit_no_free(struct hostapd_data *hapd)
453 {
454 hostapd_free_stas(hapd);
455 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
456 hostapd_clear_wep(hapd);
457 }
458
459
460 /**
461 * hostapd_validate_bssid_configuration - Validate BSSID configuration
462 * @iface: Pointer to interface data
463 * Returns: 0 on success, -1 on failure
464 *
465 * This function is used to validate that the configured BSSIDs are valid.
466 */
467 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
468 {
469 u8 mask[ETH_ALEN] = { 0 };
470 struct hostapd_data *hapd = iface->bss[0];
471 unsigned int i = iface->conf->num_bss, bits = 0, j;
472 int auto_addr = 0;
473
474 if (hostapd_drv_none(hapd))
475 return 0;
476
477 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
478
479 /* Determine the bits necessary to cover the number of BSSIDs. */
480 for (i--; i; i >>= 1)
481 bits++;
482
483 /* Determine the bits necessary to any configured BSSIDs,
484 if they are higher than the number of BSSIDs. */
485 for (j = 0; j < iface->conf->num_bss; j++) {
486 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
487 if (j)
488 auto_addr++;
489 continue;
490 }
491
492 for (i = 0; i < ETH_ALEN; i++) {
493 mask[i] |=
494 iface->conf->bss[j]->bssid[i] ^
495 hapd->own_addr[i];
496 }
497 }
498
499 if (!auto_addr)
500 goto skip_mask_ext;
501
502 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
503 ;
504 j = 0;
505 if (i < ETH_ALEN) {
506 j = (5 - i) * 8;
507
508 while (mask[i] != 0) {
509 mask[i] >>= 1;
510 j++;
511 }
512 }
513
514 if (bits < j)
515 bits = j;
516
517 if (bits > 40) {
518 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
519 bits);
520 return -1;
521 }
522
523 os_memset(mask, 0xff, ETH_ALEN);
524 j = bits / 8;
525 for (i = 5; i > 5 - j; i--)
526 mask[i] = 0;
527 j = bits % 8;
528 while (j--)
529 mask[i] <<= 1;
530
531 skip_mask_ext:
532 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
533 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
534
535 if (!auto_addr)
536 return 0;
537
538 for (i = 0; i < ETH_ALEN; i++) {
539 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
540 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
541 " for start address " MACSTR ".",
542 MAC2STR(mask), MAC2STR(hapd->own_addr));
543 wpa_printf(MSG_ERROR, "Start address must be the "
544 "first address in the block (i.e., addr "
545 "AND mask == addr).");
546 return -1;
547 }
548 }
549
550 return 0;
551 }
552
553
554 static int mac_in_conf(struct hostapd_config *conf, const void *a)
555 {
556 size_t i;
557
558 for (i = 0; i < conf->num_bss; i++) {
559 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
560 return 1;
561 }
562 }
563
564 return 0;
565 }
566
567
568 #ifndef CONFIG_NO_RADIUS
569
570 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
571 struct radius_das_attrs *attr)
572 {
573 if (attr->nas_identifier &&
574 (!hapd->conf->nas_identifier ||
575 os_strlen(hapd->conf->nas_identifier) !=
576 attr->nas_identifier_len ||
577 os_memcmp(hapd->conf->nas_identifier, attr->nas_identifier,
578 attr->nas_identifier_len) != 0)) {
579 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-Identifier mismatch");
580 return 1;
581 }
582
583 if (attr->nas_ip_addr &&
584 (hapd->conf->own_ip_addr.af != AF_INET ||
585 os_memcmp(&hapd->conf->own_ip_addr.u.v4, attr->nas_ip_addr, 4) !=
586 0)) {
587 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IP-Address mismatch");
588 return 1;
589 }
590
591 #ifdef CONFIG_IPV6
592 if (attr->nas_ipv6_addr &&
593 (hapd->conf->own_ip_addr.af != AF_INET6 ||
594 os_memcmp(&hapd->conf->own_ip_addr.u.v6, attr->nas_ipv6_addr, 16)
595 != 0)) {
596 wpa_printf(MSG_DEBUG, "RADIUS DAS: NAS-IPv6-Address mismatch");
597 return 1;
598 }
599 #endif /* CONFIG_IPV6 */
600
601 return 0;
602 }
603
604
605 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
606 struct radius_das_attrs *attr)
607 {
608 struct sta_info *sta = NULL;
609 char buf[128];
610
611 if (attr->sta_addr)
612 sta = ap_get_sta(hapd, attr->sta_addr);
613
614 if (sta == NULL && attr->acct_session_id &&
615 attr->acct_session_id_len == 17) {
616 for (sta = hapd->sta_list; sta; sta = sta->next) {
617 os_snprintf(buf, sizeof(buf), "%08X-%08X",
618 sta->acct_session_id_hi,
619 sta->acct_session_id_lo);
620 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
621 break;
622 }
623 }
624
625 if (sta == NULL && attr->cui) {
626 for (sta = hapd->sta_list; sta; sta = sta->next) {
627 struct wpabuf *cui;
628 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
629 if (cui && wpabuf_len(cui) == attr->cui_len &&
630 os_memcmp(wpabuf_head(cui), attr->cui,
631 attr->cui_len) == 0)
632 break;
633 }
634 }
635
636 if (sta == NULL && attr->user_name) {
637 for (sta = hapd->sta_list; sta; sta = sta->next) {
638 u8 *identity;
639 size_t identity_len;
640 identity = ieee802_1x_get_identity(sta->eapol_sm,
641 &identity_len);
642 if (identity &&
643 identity_len == attr->user_name_len &&
644 os_memcmp(identity, attr->user_name, identity_len)
645 == 0)
646 break;
647 }
648 }
649
650 return sta;
651 }
652
653
654 static enum radius_das_res
655 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
656 {
657 struct hostapd_data *hapd = ctx;
658 struct sta_info *sta;
659
660 if (hostapd_das_nas_mismatch(hapd, attr))
661 return RADIUS_DAS_NAS_MISMATCH;
662
663 sta = hostapd_das_find_sta(hapd, attr);
664 if (sta == NULL)
665 return RADIUS_DAS_SESSION_NOT_FOUND;
666
667 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
668
669 hostapd_drv_sta_deauth(hapd, sta->addr,
670 WLAN_REASON_PREV_AUTH_NOT_VALID);
671 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
672
673 return RADIUS_DAS_SUCCESS;
674 }
675
676 #endif /* CONFIG_NO_RADIUS */
677
678
679 /**
680 * hostapd_setup_bss - Per-BSS setup (initialization)
681 * @hapd: Pointer to BSS data
682 * @first: Whether this BSS is the first BSS of an interface; -1 = not first,
683 * but interface may exist
684 *
685 * This function is used to initialize all per-BSS data structures and
686 * resources. This gets called in a loop for each BSS when an interface is
687 * initialized. Most of the modules that are initialized here will be
688 * deinitialized in hostapd_cleanup().
689 */
690 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
691 {
692 struct hostapd_bss_config *conf = hapd->conf;
693 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
694 int ssid_len, set_ssid;
695 char force_ifname[IFNAMSIZ];
696 u8 if_addr[ETH_ALEN];
697 int flush_old_stations = 1;
698
699 wpa_printf(MSG_DEBUG, "%s(hapd=%p (%s), first=%d)",
700 __func__, hapd, conf->iface, first);
701
702 #ifdef EAP_SERVER_TNC
703 if (conf->tnc && tncs_global_init() < 0) {
704 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
705 return -1;
706 }
707 #endif /* EAP_SERVER_TNC */
708
709 if (hapd->started) {
710 wpa_printf(MSG_ERROR, "%s: Interface %s was already started",
711 __func__, conf->iface);
712 return -1;
713 }
714 hapd->started = 1;
715
716 if (!first || first == -1) {
717 if (hostapd_mac_comp_empty(conf->bssid) == 0) {
718 /* Allocate the next available BSSID. */
719 do {
720 inc_byte_array(hapd->own_addr, ETH_ALEN);
721 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
722 } else {
723 /* Allocate the configured BSSID. */
724 os_memcpy(hapd->own_addr, conf->bssid, ETH_ALEN);
725
726 if (hostapd_mac_comp(hapd->own_addr,
727 hapd->iface->bss[0]->own_addr) ==
728 0) {
729 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
730 "BSSID set to the MAC address of "
731 "the radio", conf->iface);
732 return -1;
733 }
734 }
735
736 hapd->interface_added = 1;
737 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
738 conf->iface, hapd->own_addr, hapd,
739 &hapd->drv_priv, force_ifname, if_addr,
740 conf->bridge[0] ? conf->bridge : NULL,
741 first == -1)) {
742 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
743 MACSTR ")", MAC2STR(hapd->own_addr));
744 hapd->interface_added = 0;
745 return -1;
746 }
747 }
748
749 if (conf->wmm_enabled < 0)
750 conf->wmm_enabled = hapd->iconf->ieee80211n;
751
752 #ifdef CONFIG_MESH
753 if (hapd->iface->mconf == NULL)
754 flush_old_stations = 0;
755 #endif /* CONFIG_MESH */
756
757 if (flush_old_stations)
758 hostapd_flush_old_stations(hapd,
759 WLAN_REASON_PREV_AUTH_NOT_VALID);
760 hostapd_set_privacy(hapd, 0);
761
762 hostapd_broadcast_wep_clear(hapd);
763 if (hostapd_setup_encryption(conf->iface, hapd))
764 return -1;
765
766 /*
767 * Fetch the SSID from the system and use it or,
768 * if one was specified in the config file, verify they
769 * match.
770 */
771 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
772 if (ssid_len < 0) {
773 wpa_printf(MSG_ERROR, "Could not read SSID from system");
774 return -1;
775 }
776 if (conf->ssid.ssid_set) {
777 /*
778 * If SSID is specified in the config file and it differs
779 * from what is being used then force installation of the
780 * new SSID.
781 */
782 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
783 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
784 } else {
785 /*
786 * No SSID in the config file; just use the one we got
787 * from the system.
788 */
789 set_ssid = 0;
790 conf->ssid.ssid_len = ssid_len;
791 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
792 }
793
794 if (!hostapd_drv_none(hapd)) {
795 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
796 " and ssid \"%s\"",
797 conf->iface, MAC2STR(hapd->own_addr),
798 wpa_ssid_txt(conf->ssid.ssid, conf->ssid.ssid_len));
799 }
800
801 if (hostapd_setup_wpa_psk(conf)) {
802 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
803 return -1;
804 }
805
806 /* Set SSID for the kernel driver (to be used in beacon and probe
807 * response frames) */
808 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
809 conf->ssid.ssid_len)) {
810 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
811 return -1;
812 }
813
814 if (wpa_debug_level <= MSG_MSGDUMP)
815 conf->radius->msg_dumps = 1;
816 #ifndef CONFIG_NO_RADIUS
817 hapd->radius = radius_client_init(hapd, conf->radius);
818 if (hapd->radius == NULL) {
819 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
820 return -1;
821 }
822
823 if (conf->radius_das_port) {
824 struct radius_das_conf das_conf;
825 os_memset(&das_conf, 0, sizeof(das_conf));
826 das_conf.port = conf->radius_das_port;
827 das_conf.shared_secret = conf->radius_das_shared_secret;
828 das_conf.shared_secret_len =
829 conf->radius_das_shared_secret_len;
830 das_conf.client_addr = &conf->radius_das_client_addr;
831 das_conf.time_window = conf->radius_das_time_window;
832 das_conf.require_event_timestamp =
833 conf->radius_das_require_event_timestamp;
834 das_conf.ctx = hapd;
835 das_conf.disconnect = hostapd_das_disconnect;
836 hapd->radius_das = radius_das_init(&das_conf);
837 if (hapd->radius_das == NULL) {
838 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
839 "failed.");
840 return -1;
841 }
842 }
843 #endif /* CONFIG_NO_RADIUS */
844
845 if (hostapd_acl_init(hapd)) {
846 wpa_printf(MSG_ERROR, "ACL initialization failed.");
847 return -1;
848 }
849 if (hostapd_init_wps(hapd, conf))
850 return -1;
851
852 if (authsrv_init(hapd) < 0)
853 return -1;
854
855 if (ieee802_1x_init(hapd)) {
856 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
857 return -1;
858 }
859
860 if ((conf->wpa || conf->osen) && hostapd_setup_wpa(hapd))
861 return -1;
862
863 if (accounting_init(hapd)) {
864 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
865 return -1;
866 }
867
868 if (conf->ieee802_11f &&
869 (hapd->iapp = iapp_init(hapd, conf->iapp_iface)) == NULL) {
870 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
871 "failed.");
872 return -1;
873 }
874
875 #ifdef CONFIG_INTERWORKING
876 if (gas_serv_init(hapd)) {
877 wpa_printf(MSG_ERROR, "GAS server initialization failed");
878 return -1;
879 }
880
881 if (conf->qos_map_set_len &&
882 hostapd_drv_set_qos_map(hapd, conf->qos_map_set,
883 conf->qos_map_set_len)) {
884 wpa_printf(MSG_ERROR, "Failed to initialize QoS Map");
885 return -1;
886 }
887 #endif /* CONFIG_INTERWORKING */
888
889 if (conf->bss_load_update_period && bss_load_update_init(hapd)) {
890 wpa_printf(MSG_ERROR, "BSS Load initialization failed");
891 return -1;
892 }
893
894 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
895 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
896 return -1;
897 }
898
899 if (!conf->start_disabled && ieee802_11_set_beacon(hapd) < 0)
900 return -1;
901
902 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
903 return -1;
904
905 if (hapd->driver && hapd->driver->set_operstate)
906 hapd->driver->set_operstate(hapd->drv_priv, 1);
907
908 return 0;
909 }
910
911
912 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
913 {
914 struct hostapd_data *hapd = iface->bss[0];
915 int i;
916 struct hostapd_tx_queue_params *p;
917
918 #ifdef CONFIG_MESH
919 if (iface->mconf == NULL)
920 return;
921 #endif /* CONFIG_MESH */
922
923 for (i = 0; i < NUM_TX_QUEUES; i++) {
924 p = &iface->conf->tx_queue[i];
925
926 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
927 p->cwmax, p->burst)) {
928 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
929 "parameters for queue %d.", i);
930 /* Continue anyway */
931 }
932 }
933 }
934
935
936 static int hostapd_set_acl_list(struct hostapd_data *hapd,
937 struct mac_acl_entry *mac_acl,
938 int n_entries, u8 accept_acl)
939 {
940 struct hostapd_acl_params *acl_params;
941 int i, err;
942
943 acl_params = os_zalloc(sizeof(*acl_params) +
944 (n_entries * sizeof(acl_params->mac_acl[0])));
945 if (!acl_params)
946 return -ENOMEM;
947
948 for (i = 0; i < n_entries; i++)
949 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
950 ETH_ALEN);
951
952 acl_params->acl_policy = accept_acl;
953 acl_params->num_mac_acl = n_entries;
954
955 err = hostapd_drv_set_acl(hapd, acl_params);
956
957 os_free(acl_params);
958
959 return err;
960 }
961
962
963 static void hostapd_set_acl(struct hostapd_data *hapd)
964 {
965 struct hostapd_config *conf = hapd->iconf;
966 int err;
967 u8 accept_acl;
968
969 if (hapd->iface->drv_max_acl_mac_addrs == 0)
970 return;
971
972 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
973 accept_acl = 1;
974 err = hostapd_set_acl_list(hapd, conf->bss[0]->accept_mac,
975 conf->bss[0]->num_accept_mac,
976 accept_acl);
977 if (err) {
978 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
979 return;
980 }
981 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
982 accept_acl = 0;
983 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
984 conf->bss[0]->num_deny_mac,
985 accept_acl);
986 if (err) {
987 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
988 return;
989 }
990 }
991 }
992
993
994 static int start_ctrl_iface_bss(struct hostapd_data *hapd)
995 {
996 if (!hapd->iface->interfaces ||
997 !hapd->iface->interfaces->ctrl_iface_init)
998 return 0;
999
1000 if (hapd->iface->interfaces->ctrl_iface_init(hapd)) {
1001 wpa_printf(MSG_ERROR,
1002 "Failed to setup control interface for %s",
1003 hapd->conf->iface);
1004 return -1;
1005 }
1006
1007 return 0;
1008 }
1009
1010
1011 static int start_ctrl_iface(struct hostapd_iface *iface)
1012 {
1013 size_t i;
1014
1015 if (!iface->interfaces || !iface->interfaces->ctrl_iface_init)
1016 return 0;
1017
1018 for (i = 0; i < iface->num_bss; i++) {
1019 struct hostapd_data *hapd = iface->bss[i];
1020 if (iface->interfaces->ctrl_iface_init(hapd)) {
1021 wpa_printf(MSG_ERROR,
1022 "Failed to setup control interface for %s",
1023 hapd->conf->iface);
1024 return -1;
1025 }
1026 }
1027
1028 return 0;
1029 }
1030
1031
1032 static void channel_list_update_timeout(void *eloop_ctx, void *timeout_ctx)
1033 {
1034 struct hostapd_iface *iface = eloop_ctx;
1035
1036 if (!iface->wait_channel_update) {
1037 wpa_printf(MSG_INFO, "Channel list update timeout, but interface was not waiting for it");
1038 return;
1039 }
1040
1041 /*
1042 * It is possible that the existing channel list is acceptable, so try
1043 * to proceed.
1044 */
1045 wpa_printf(MSG_DEBUG, "Channel list update timeout - try to continue anyway");
1046 setup_interface2(iface);
1047 }
1048
1049
1050 void hostapd_channel_list_updated(struct hostapd_iface *iface, int initiator)
1051 {
1052 if (!iface->wait_channel_update || initiator != REGDOM_SET_BY_USER)
1053 return;
1054
1055 wpa_printf(MSG_DEBUG, "Channel list updated - continue setup");
1056 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1057 setup_interface2(iface);
1058 }
1059
1060
1061 static int setup_interface(struct hostapd_iface *iface)
1062 {
1063 struct hostapd_data *hapd = iface->bss[0];
1064 size_t i;
1065
1066 /*
1067 * It is possible that setup_interface() is called after the interface
1068 * was disabled etc., in which case driver_ap_teardown is possibly set
1069 * to 1. Clear it here so any other key/station deletion, which is not
1070 * part of a teardown flow, would also call the relevant driver
1071 * callbacks.
1072 */
1073 iface->driver_ap_teardown = 0;
1074
1075 if (!iface->phy[0]) {
1076 const char *phy = hostapd_drv_get_radio_name(hapd);
1077 if (phy) {
1078 wpa_printf(MSG_DEBUG, "phy: %s", phy);
1079 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1080 }
1081 }
1082
1083 /*
1084 * Make sure that all BSSes get configured with a pointer to the same
1085 * driver interface.
1086 */
1087 for (i = 1; i < iface->num_bss; i++) {
1088 iface->bss[i]->driver = hapd->driver;
1089 iface->bss[i]->drv_priv = hapd->drv_priv;
1090 }
1091
1092 if (hostapd_validate_bssid_configuration(iface))
1093 return -1;
1094
1095 /*
1096 * Initialize control interfaces early to allow external monitoring of
1097 * channel setup operations that may take considerable amount of time
1098 * especially for DFS cases.
1099 */
1100 if (start_ctrl_iface(iface))
1101 return -1;
1102
1103 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1104 char country[4], previous_country[4];
1105
1106 hostapd_set_state(iface, HAPD_IFACE_COUNTRY_UPDATE);
1107 if (hostapd_get_country(hapd, previous_country) < 0)
1108 previous_country[0] = '\0';
1109
1110 os_memcpy(country, hapd->iconf->country, 3);
1111 country[3] = '\0';
1112 if (hostapd_set_country(hapd, country) < 0) {
1113 wpa_printf(MSG_ERROR, "Failed to set country code");
1114 return -1;
1115 }
1116
1117 wpa_printf(MSG_DEBUG, "Previous country code %s, new country code %s",
1118 previous_country, country);
1119
1120 if (os_strncmp(previous_country, country, 2) != 0) {
1121 wpa_printf(MSG_DEBUG, "Continue interface setup after channel list update");
1122 iface->wait_channel_update = 1;
1123 eloop_register_timeout(5, 0,
1124 channel_list_update_timeout,
1125 iface, NULL);
1126 return 0;
1127 }
1128 }
1129
1130 return setup_interface2(iface);
1131 }
1132
1133
1134 static int setup_interface2(struct hostapd_iface *iface)
1135 {
1136 iface->wait_channel_update = 0;
1137
1138 if (hostapd_get_hw_features(iface)) {
1139 /* Not all drivers support this yet, so continue without hw
1140 * feature data. */
1141 } else {
1142 int ret = hostapd_select_hw_mode(iface);
1143 if (ret < 0) {
1144 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1145 "channel. (%d)", ret);
1146 goto fail;
1147 }
1148 if (ret == 1) {
1149 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
1150 return 0;
1151 }
1152 ret = hostapd_check_ht_capab(iface);
1153 if (ret < 0)
1154 goto fail;
1155 if (ret == 1) {
1156 wpa_printf(MSG_DEBUG, "Interface initialization will "
1157 "be completed in a callback");
1158 return 0;
1159 }
1160
1161 if (iface->conf->ieee80211h)
1162 wpa_printf(MSG_DEBUG, "DFS support is enabled");
1163 }
1164 return hostapd_setup_interface_complete(iface, 0);
1165
1166 fail:
1167 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1168 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1169 if (iface->interfaces && iface->interfaces->terminate_on_error)
1170 eloop_terminate();
1171 return -1;
1172 }
1173
1174
1175 /**
1176 * hostapd_setup_interface_complete - Complete interface setup
1177 *
1178 * This function is called when previous steps in the interface setup has been
1179 * completed. This can also start operations, e.g., DFS, that will require
1180 * additional processing before interface is ready to be enabled. Such
1181 * operations will call this function from eloop callbacks when finished.
1182 */
1183 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1184 {
1185 struct hostapd_data *hapd = iface->bss[0];
1186 size_t j;
1187 u8 *prev_addr;
1188 int delay_apply_cfg = 0;
1189
1190 if (err)
1191 goto fail;
1192
1193 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1194 if (iface->conf->channel) {
1195 #ifdef NEED_AP_MLME
1196 int res;
1197 #endif /* NEED_AP_MLME */
1198
1199 iface->freq = hostapd_hw_get_freq(hapd, iface->conf->channel);
1200 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1201 "Frequency: %d MHz",
1202 hostapd_hw_mode_txt(iface->conf->hw_mode),
1203 iface->conf->channel, iface->freq);
1204
1205 #ifdef NEED_AP_MLME
1206 /* Handle DFS only if it is not offloaded to the driver */
1207 if (!(iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD)) {
1208 /* Check DFS */
1209 res = hostapd_handle_dfs(iface);
1210 if (res <= 0) {
1211 if (res < 0)
1212 goto fail;
1213 return res;
1214 }
1215 }
1216 #endif /* NEED_AP_MLME */
1217
1218 #ifdef CONFIG_MESH
1219 if (iface->mconf != NULL) {
1220 wpa_printf(MSG_DEBUG,
1221 "%s: Mesh configuration will be applied while joining the mesh network",
1222 iface->bss[0]->conf->iface);
1223 delay_apply_cfg = 1;
1224 }
1225 #endif /* CONFIG_MESH */
1226
1227 if (!delay_apply_cfg &&
1228 hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
1229 hapd->iconf->channel,
1230 hapd->iconf->ieee80211n,
1231 hapd->iconf->ieee80211ac,
1232 hapd->iconf->secondary_channel,
1233 hapd->iconf->vht_oper_chwidth,
1234 hapd->iconf->vht_oper_centr_freq_seg0_idx,
1235 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
1236 wpa_printf(MSG_ERROR, "Could not set channel for "
1237 "kernel driver");
1238 goto fail;
1239 }
1240 }
1241
1242 if (iface->current_mode) {
1243 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1244 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1245 "table.");
1246 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1247 HOSTAPD_LEVEL_WARNING,
1248 "Failed to prepare rates table.");
1249 goto fail;
1250 }
1251 }
1252
1253 if (hapd->iconf->rts_threshold > -1 &&
1254 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1255 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1256 "kernel driver");
1257 goto fail;
1258 }
1259
1260 if (hapd->iconf->fragm_threshold > -1 &&
1261 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1262 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1263 "for kernel driver");
1264 goto fail;
1265 }
1266
1267 prev_addr = hapd->own_addr;
1268
1269 for (j = 0; j < iface->num_bss; j++) {
1270 hapd = iface->bss[j];
1271 if (j)
1272 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1273 if (hostapd_setup_bss(hapd, j == 0)) {
1274 do {
1275 hapd = iface->bss[j];
1276 hostapd_bss_deinit_no_free(hapd);
1277 hostapd_free_hapd_data(hapd);
1278 } while (j-- > 0);
1279 goto fail;
1280 }
1281 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1282 prev_addr = hapd->own_addr;
1283 }
1284 hapd = iface->bss[0];
1285
1286 hostapd_tx_queue_params(iface);
1287
1288 ap_list_init(iface);
1289
1290 hostapd_set_acl(hapd);
1291
1292 if (hostapd_driver_commit(hapd) < 0) {
1293 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1294 "configuration", __func__);
1295 goto fail;
1296 }
1297
1298 /*
1299 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1300 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1301 * mode), the interface is up only after driver_commit, so initialize
1302 * WPS after driver_commit.
1303 */
1304 for (j = 0; j < iface->num_bss; j++) {
1305 if (hostapd_init_wps_complete(iface->bss[j]))
1306 goto fail;
1307 }
1308
1309 hostapd_set_state(iface, HAPD_IFACE_ENABLED);
1310 wpa_msg(iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_ENABLED);
1311 if (hapd->setup_complete_cb)
1312 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1313
1314 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1315 iface->bss[0]->conf->iface);
1316 if (iface->interfaces && iface->interfaces->terminate_on_error > 0)
1317 iface->interfaces->terminate_on_error--;
1318
1319 return 0;
1320
1321 fail:
1322 wpa_printf(MSG_ERROR, "Interface initialization failed");
1323 hostapd_set_state(iface, HAPD_IFACE_DISABLED);
1324 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1325 if (iface->interfaces && iface->interfaces->terminate_on_error)
1326 eloop_terminate();
1327 return -1;
1328 }
1329
1330
1331 /**
1332 * hostapd_setup_interface - Setup of an interface
1333 * @iface: Pointer to interface data.
1334 * Returns: 0 on success, -1 on failure
1335 *
1336 * Initializes the driver interface, validates the configuration,
1337 * and sets driver parameters based on the configuration.
1338 * Flushes old stations, sets the channel, encryption,
1339 * beacons, and WDS links based on the configuration.
1340 *
1341 * If interface setup requires more time, e.g., to perform HT co-ex scans, ACS,
1342 * or DFS operations, this function returns 0 before such operations have been
1343 * completed. The pending operations are registered into eloop and will be
1344 * completed from eloop callbacks. Those callbacks end up calling
1345 * hostapd_setup_interface_complete() once setup has been completed.
1346 */
1347 int hostapd_setup_interface(struct hostapd_iface *iface)
1348 {
1349 int ret;
1350
1351 ret = setup_interface(iface);
1352 if (ret) {
1353 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1354 iface->bss[0]->conf->iface);
1355 return -1;
1356 }
1357
1358 return 0;
1359 }
1360
1361
1362 /**
1363 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1364 * @hapd_iface: Pointer to interface data
1365 * @conf: Pointer to per-interface configuration
1366 * @bss: Pointer to per-BSS configuration for this BSS
1367 * Returns: Pointer to allocated BSS data
1368 *
1369 * This function is used to allocate per-BSS data structure. This data will be
1370 * freed after hostapd_cleanup() is called for it during interface
1371 * deinitialization.
1372 */
1373 struct hostapd_data *
1374 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1375 struct hostapd_config *conf,
1376 struct hostapd_bss_config *bss)
1377 {
1378 struct hostapd_data *hapd;
1379
1380 hapd = os_zalloc(sizeof(*hapd));
1381 if (hapd == NULL)
1382 return NULL;
1383
1384 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1385 hapd->iconf = conf;
1386 hapd->conf = bss;
1387 hapd->iface = hapd_iface;
1388 hapd->driver = hapd->iconf->driver;
1389 hapd->ctrl_sock = -1;
1390
1391 return hapd;
1392 }
1393
1394
1395 static void hostapd_bss_deinit(struct hostapd_data *hapd)
1396 {
1397 wpa_printf(MSG_DEBUG, "%s: deinit bss %s", __func__,
1398 hapd->conf->iface);
1399 hostapd_bss_deinit_no_free(hapd);
1400 hostapd_cleanup(hapd);
1401 }
1402
1403
1404 void hostapd_interface_deinit(struct hostapd_iface *iface)
1405 {
1406 int j;
1407
1408 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1409 if (iface == NULL)
1410 return;
1411
1412 #ifdef CONFIG_IEEE80211N
1413 #ifdef NEED_AP_MLME
1414 hostapd_stop_setup_timers(iface);
1415 eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
1416 #endif /* NEED_AP_MLME */
1417 #endif /* CONFIG_IEEE80211N */
1418 eloop_cancel_timeout(channel_list_update_timeout, iface, NULL);
1419 iface->wait_channel_update = 0;
1420
1421 for (j = iface->num_bss - 1; j >= 0; j--)
1422 hostapd_bss_deinit(iface->bss[j]);
1423 }
1424
1425
1426 void hostapd_interface_free(struct hostapd_iface *iface)
1427 {
1428 size_t j;
1429 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1430 for (j = 0; j < iface->num_bss; j++) {
1431 wpa_printf(MSG_DEBUG, "%s: free hapd %p",
1432 __func__, iface->bss[j]);
1433 os_free(iface->bss[j]);
1434 }
1435 hostapd_cleanup_iface(iface);
1436 }
1437
1438
1439 /**
1440 * hostapd_init - Allocate and initialize per-interface data
1441 * @config_file: Path to the configuration file
1442 * Returns: Pointer to the allocated interface data or %NULL on failure
1443 *
1444 * This function is used to allocate main data structures for per-interface
1445 * data. The allocated data buffer will be freed by calling
1446 * hostapd_cleanup_iface().
1447 */
1448 struct hostapd_iface * hostapd_init(struct hapd_interfaces *interfaces,
1449 const char *config_file)
1450 {
1451 struct hostapd_iface *hapd_iface = NULL;
1452 struct hostapd_config *conf = NULL;
1453 struct hostapd_data *hapd;
1454 size_t i;
1455
1456 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1457 if (hapd_iface == NULL)
1458 goto fail;
1459
1460 hapd_iface->config_fname = os_strdup(config_file);
1461 if (hapd_iface->config_fname == NULL)
1462 goto fail;
1463
1464 conf = interfaces->config_read_cb(hapd_iface->config_fname);
1465 if (conf == NULL)
1466 goto fail;
1467 hapd_iface->conf = conf;
1468
1469 hapd_iface->num_bss = conf->num_bss;
1470 hapd_iface->bss = os_calloc(conf->num_bss,
1471 sizeof(struct hostapd_data *));
1472 if (hapd_iface->bss == NULL)
1473 goto fail;
1474
1475 for (i = 0; i < conf->num_bss; i++) {
1476 hapd = hapd_iface->bss[i] =
1477 hostapd_alloc_bss_data(hapd_iface, conf,
1478 conf->bss[i]);
1479 if (hapd == NULL)
1480 goto fail;
1481 hapd->msg_ctx = hapd;
1482 }
1483
1484 return hapd_iface;
1485
1486 fail:
1487 wpa_printf(MSG_ERROR, "Failed to set up interface with %s",
1488 config_file);
1489 if (conf)
1490 hostapd_config_free(conf);
1491 if (hapd_iface) {
1492 os_free(hapd_iface->config_fname);
1493 os_free(hapd_iface->bss);
1494 wpa_printf(MSG_DEBUG, "%s: free iface %p",
1495 __func__, hapd_iface);
1496 os_free(hapd_iface);
1497 }
1498 return NULL;
1499 }
1500
1501
1502 static int ifname_in_use(struct hapd_interfaces *interfaces, const char *ifname)
1503 {
1504 size_t i, j;
1505
1506 for (i = 0; i < interfaces->count; i++) {
1507 struct hostapd_iface *iface = interfaces->iface[i];
1508 for (j = 0; j < iface->num_bss; j++) {
1509 struct hostapd_data *hapd = iface->bss[j];
1510 if (os_strcmp(ifname, hapd->conf->iface) == 0)
1511 return 1;
1512 }
1513 }
1514
1515 return 0;
1516 }
1517
1518
1519 /**
1520 * hostapd_interface_init_bss - Read configuration file and init BSS data
1521 *
1522 * This function is used to parse configuration file for a BSS. This BSS is
1523 * added to an existing interface sharing the same radio (if any) or a new
1524 * interface is created if this is the first interface on a radio. This
1525 * allocate memory for the BSS. No actual driver operations are started.
1526 *
1527 * This is similar to hostapd_interface_init(), but for a case where the
1528 * configuration is used to add a single BSS instead of all BSSes for a radio.
1529 */
1530 struct hostapd_iface *
1531 hostapd_interface_init_bss(struct hapd_interfaces *interfaces, const char *phy,
1532 const char *config_fname, int debug)
1533 {
1534 struct hostapd_iface *new_iface = NULL, *iface = NULL;
1535 struct hostapd_data *hapd;
1536 int k;
1537 size_t i, bss_idx;
1538
1539 if (!phy || !*phy)
1540 return NULL;
1541
1542 for (i = 0; i < interfaces->count; i++) {
1543 if (os_strcmp(interfaces->iface[i]->phy, phy) == 0) {
1544 iface = interfaces->iface[i];
1545 break;
1546 }
1547 }
1548
1549 wpa_printf(MSG_INFO, "Configuration file: %s (phy %s)%s",
1550 config_fname, phy, iface ? "" : " --> new PHY");
1551 if (iface) {
1552 struct hostapd_config *conf;
1553 struct hostapd_bss_config **tmp_conf;
1554 struct hostapd_data **tmp_bss;
1555 struct hostapd_bss_config *bss;
1556 const char *ifname;
1557
1558 /* Add new BSS to existing iface */
1559 conf = interfaces->config_read_cb(config_fname);
1560 if (conf == NULL)
1561 return NULL;
1562 if (conf->num_bss > 1) {
1563 wpa_printf(MSG_ERROR, "Multiple BSSes specified in BSS-config");
1564 hostapd_config_free(conf);
1565 return NULL;
1566 }
1567
1568 ifname = conf->bss[0]->iface;
1569 if (ifname[0] != '\0' && ifname_in_use(interfaces, ifname)) {
1570 wpa_printf(MSG_ERROR,
1571 "Interface name %s already in use", ifname);
1572 hostapd_config_free(conf);
1573 return NULL;
1574 }
1575
1576 tmp_conf = os_realloc_array(
1577 iface->conf->bss, iface->conf->num_bss + 1,
1578 sizeof(struct hostapd_bss_config *));
1579 tmp_bss = os_realloc_array(iface->bss, iface->num_bss + 1,
1580 sizeof(struct hostapd_data *));
1581 if (tmp_bss)
1582 iface->bss = tmp_bss;
1583 if (tmp_conf) {
1584 iface->conf->bss = tmp_conf;
1585 iface->conf->last_bss = tmp_conf[0];
1586 }
1587 if (tmp_bss == NULL || tmp_conf == NULL) {
1588 hostapd_config_free(conf);
1589 return NULL;
1590 }
1591 bss = iface->conf->bss[iface->conf->num_bss] = conf->bss[0];
1592 iface->conf->num_bss++;
1593
1594 hapd = hostapd_alloc_bss_data(iface, iface->conf, bss);
1595 if (hapd == NULL) {
1596 iface->conf->num_bss--;
1597 hostapd_config_free(conf);
1598 return NULL;
1599 }
1600 iface->conf->last_bss = bss;
1601 iface->bss[iface->num_bss] = hapd;
1602 hapd->msg_ctx = hapd;
1603
1604 bss_idx = iface->num_bss++;
1605 conf->num_bss--;
1606 conf->bss[0] = NULL;
1607 hostapd_config_free(conf);
1608 } else {
1609 /* Add a new iface with the first BSS */
1610 new_iface = iface = hostapd_init(interfaces, config_fname);
1611 if (!iface)
1612 return NULL;
1613 os_strlcpy(iface->phy, phy, sizeof(iface->phy));
1614 iface->interfaces = interfaces;
1615 bss_idx = 0;
1616 }
1617
1618 for (k = 0; k < debug; k++) {
1619 if (iface->bss[bss_idx]->conf->logger_stdout_level > 0)
1620 iface->bss[bss_idx]->conf->logger_stdout_level--;
1621 }
1622
1623 if (iface->conf->bss[bss_idx]->iface[0] == '\0' &&
1624 !hostapd_drv_none(iface->bss[bss_idx])) {
1625 wpa_printf(MSG_ERROR, "Interface name not specified in %s",
1626 config_fname);
1627 if (new_iface)
1628 hostapd_interface_deinit_free(new_iface);
1629 return NULL;
1630 }
1631
1632 return iface;
1633 }
1634
1635
1636 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1637 {
1638 const struct wpa_driver_ops *driver;
1639 void *drv_priv;
1640
1641 wpa_printf(MSG_DEBUG, "%s(%p)", __func__, iface);
1642 if (iface == NULL)
1643 return;
1644 wpa_printf(MSG_DEBUG, "%s: num_bss=%u conf->num_bss=%u",
1645 __func__, (unsigned int) iface->num_bss,
1646 (unsigned int) iface->conf->num_bss);
1647 driver = iface->bss[0]->driver;
1648 drv_priv = iface->bss[0]->drv_priv;
1649 hostapd_interface_deinit(iface);
1650 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1651 __func__, driver, drv_priv);
1652 if (driver && driver->hapd_deinit && drv_priv) {
1653 driver->hapd_deinit(drv_priv);
1654 iface->bss[0]->drv_priv = NULL;
1655 }
1656 hostapd_interface_free(iface);
1657 }
1658
1659
1660 static void hostapd_deinit_driver(const struct wpa_driver_ops *driver,
1661 void *drv_priv,
1662 struct hostapd_iface *hapd_iface)
1663 {
1664 size_t j;
1665
1666 wpa_printf(MSG_DEBUG, "%s: driver=%p drv_priv=%p -> hapd_deinit",
1667 __func__, driver, drv_priv);
1668 if (driver && driver->hapd_deinit && drv_priv) {
1669 driver->hapd_deinit(drv_priv);
1670 for (j = 0; j < hapd_iface->num_bss; j++) {
1671 wpa_printf(MSG_DEBUG, "%s:bss[%d]->drv_priv=%p",
1672 __func__, (int) j,
1673 hapd_iface->bss[j]->drv_priv);
1674 if (hapd_iface->bss[j]->drv_priv == drv_priv)
1675 hapd_iface->bss[j]->drv_priv = NULL;
1676 }
1677 }
1678 }
1679
1680
1681 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1682 {
1683 size_t j;
1684
1685 if (hapd_iface->bss[0]->drv_priv != NULL) {
1686 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1687 hapd_iface->conf->bss[0]->iface);
1688 return -1;
1689 }
1690
1691 wpa_printf(MSG_DEBUG, "Enable interface %s",
1692 hapd_iface->conf->bss[0]->iface);
1693
1694 for (j = 0; j < hapd_iface->num_bss; j++)
1695 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1696 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1697 wpa_printf(MSG_INFO, "Invalid configuration - cannot enable");
1698 return -1;
1699 }
1700
1701 if (hapd_iface->interfaces == NULL ||
1702 hapd_iface->interfaces->driver_init == NULL ||
1703 hapd_iface->interfaces->driver_init(hapd_iface))
1704 return -1;
1705
1706 if (hostapd_setup_interface(hapd_iface)) {
1707 hostapd_deinit_driver(hapd_iface->bss[0]->driver,
1708 hapd_iface->bss[0]->drv_priv,
1709 hapd_iface);
1710 return -1;
1711 }
1712
1713 return 0;
1714 }
1715
1716
1717 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1718 {
1719 size_t j;
1720
1721 wpa_printf(MSG_DEBUG, "Reload interface %s",
1722 hapd_iface->conf->bss[0]->iface);
1723 for (j = 0; j < hapd_iface->num_bss; j++)
1724 hostapd_set_security_params(hapd_iface->conf->bss[j], 1);
1725 if (hostapd_config_check(hapd_iface->conf, 1) < 0) {
1726 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1727 return -1;
1728 }
1729 hostapd_clear_old(hapd_iface);
1730 for (j = 0; j < hapd_iface->num_bss; j++)
1731 hostapd_reload_bss(hapd_iface->bss[j]);
1732
1733 return 0;
1734 }
1735
1736
1737 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1738 {
1739 size_t j;
1740 const struct wpa_driver_ops *driver;
1741 void *drv_priv;
1742
1743 if (hapd_iface == NULL)
1744 return -1;
1745
1746 if (hapd_iface->bss[0]->drv_priv == NULL) {
1747 wpa_printf(MSG_INFO, "Interface %s already disabled",
1748 hapd_iface->conf->bss[0]->iface);
1749 return -1;
1750 }
1751
1752 wpa_msg(hapd_iface->bss[0]->msg_ctx, MSG_INFO, AP_EVENT_DISABLED);
1753 driver = hapd_iface->bss[0]->driver;
1754 drv_priv = hapd_iface->bss[0]->drv_priv;
1755
1756 hapd_iface->driver_ap_teardown =
1757 !!(hapd_iface->drv_flags &
1758 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
1759
1760 /* same as hostapd_interface_deinit without deinitializing ctrl-iface */
1761 for (j = 0; j < hapd_iface->num_bss; j++) {
1762 struct hostapd_data *hapd = hapd_iface->bss[j];
1763 hostapd_bss_deinit_no_free(hapd);
1764 hostapd_free_hapd_data(hapd);
1765 }
1766
1767 hostapd_deinit_driver(driver, drv_priv, hapd_iface);
1768
1769 /* From hostapd_cleanup_iface: These were initialized in
1770 * hostapd_setup_interface and hostapd_setup_interface_complete
1771 */
1772 hostapd_cleanup_iface_partial(hapd_iface);
1773
1774 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1775 hapd_iface->bss[0]->conf->iface);
1776 hostapd_set_state(hapd_iface, HAPD_IFACE_DISABLED);
1777 return 0;
1778 }
1779
1780
1781 static struct hostapd_iface *
1782 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1783 {
1784 struct hostapd_iface **iface, *hapd_iface;
1785
1786 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1787 sizeof(struct hostapd_iface *));
1788 if (iface == NULL)
1789 return NULL;
1790 interfaces->iface = iface;
1791 hapd_iface = interfaces->iface[interfaces->count] =
1792 os_zalloc(sizeof(*hapd_iface));
1793 if (hapd_iface == NULL) {
1794 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1795 "the interface", __func__);
1796 return NULL;
1797 }
1798 interfaces->count++;
1799 hapd_iface->interfaces = interfaces;
1800
1801 return hapd_iface;
1802 }
1803
1804
1805 static struct hostapd_config *
1806 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1807 const char *ctrl_iface)
1808 {
1809 struct hostapd_bss_config *bss;
1810 struct hostapd_config *conf;
1811
1812 /* Allocates memory for bss and conf */
1813 conf = hostapd_config_defaults();
1814 if (conf == NULL) {
1815 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1816 "configuration", __func__);
1817 return NULL;
1818 }
1819
1820 conf->driver = wpa_drivers[0];
1821 if (conf->driver == NULL) {
1822 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1823 hostapd_config_free(conf);
1824 return NULL;
1825 }
1826
1827 bss = conf->last_bss = conf->bss[0];
1828
1829 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1830 bss->ctrl_interface = os_strdup(ctrl_iface);
1831 if (bss->ctrl_interface == NULL) {
1832 hostapd_config_free(conf);
1833 return NULL;
1834 }
1835
1836 /* Reading configuration file skipped, will be done in SET!
1837 * From reading the configuration till the end has to be done in
1838 * SET
1839 */
1840 return conf;
1841 }
1842
1843
1844 static struct hostapd_iface * hostapd_data_alloc(
1845 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1846 {
1847 size_t i;
1848 struct hostapd_iface *hapd_iface =
1849 interfaces->iface[interfaces->count - 1];
1850 struct hostapd_data *hapd;
1851
1852 hapd_iface->conf = conf;
1853 hapd_iface->num_bss = conf->num_bss;
1854
1855 hapd_iface->bss = os_zalloc(conf->num_bss *
1856 sizeof(struct hostapd_data *));
1857 if (hapd_iface->bss == NULL)
1858 return NULL;
1859
1860 for (i = 0; i < conf->num_bss; i++) {
1861 hapd = hapd_iface->bss[i] =
1862 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1863 if (hapd == NULL)
1864 return NULL;
1865 hapd->msg_ctx = hapd;
1866 }
1867
1868 hapd_iface->interfaces = interfaces;
1869
1870 return hapd_iface;
1871 }
1872
1873
1874 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1875 {
1876 struct hostapd_config *conf = NULL;
1877 struct hostapd_iface *hapd_iface = NULL, *new_iface = NULL;
1878 struct hostapd_data *hapd;
1879 char *ptr;
1880 size_t i, j;
1881 const char *conf_file = NULL, *phy_name = NULL;
1882
1883 if (os_strncmp(buf, "bss_config=", 11) == 0) {
1884 char *pos;
1885 phy_name = buf + 11;
1886 pos = os_strchr(phy_name, ':');
1887 if (!pos)
1888 return -1;
1889 *pos++ = '\0';
1890 conf_file = pos;
1891 if (!os_strlen(conf_file))
1892 return -1;
1893
1894 hapd_iface = hostapd_interface_init_bss(interfaces, phy_name,
1895 conf_file, 0);
1896 if (!hapd_iface)
1897 return -1;
1898 for (j = 0; j < interfaces->count; j++) {
1899 if (interfaces->iface[j] == hapd_iface)
1900 break;
1901 }
1902 if (j == interfaces->count) {
1903 struct hostapd_iface **tmp;
1904 tmp = os_realloc_array(interfaces->iface,
1905 interfaces->count + 1,
1906 sizeof(struct hostapd_iface *));
1907 if (!tmp) {
1908 hostapd_interface_deinit_free(hapd_iface);
1909 return -1;
1910 }
1911 interfaces->iface = tmp;
1912 interfaces->iface[interfaces->count++] = hapd_iface;
1913 new_iface = hapd_iface;
1914 }
1915
1916 if (new_iface) {
1917 if (interfaces->driver_init(hapd_iface) ||
1918 hostapd_setup_interface(hapd_iface)) {
1919 interfaces->count--;
1920 goto fail;
1921 }
1922 } else {
1923 /* Assign new BSS with bss[0]'s driver info */
1924 hapd = hapd_iface->bss[hapd_iface->num_bss - 1];
1925 hapd->driver = hapd_iface->bss[0]->driver;
1926 hapd->drv_priv = hapd_iface->bss[0]->drv_priv;
1927 os_memcpy(hapd->own_addr, hapd_iface->bss[0]->own_addr,
1928 ETH_ALEN);
1929
1930 if (start_ctrl_iface_bss(hapd) < 0 ||
1931 (hapd_iface->state == HAPD_IFACE_ENABLED &&
1932 hostapd_setup_bss(hapd, -1))) {
1933 hostapd_cleanup(hapd);
1934 hapd_iface->bss[hapd_iface->num_bss - 1] = NULL;
1935 hapd_iface->conf->num_bss--;
1936 hapd_iface->num_bss--;
1937 wpa_printf(MSG_DEBUG, "%s: free hapd %p %s",
1938 __func__, hapd, hapd->conf->iface);
1939 os_free(hapd);
1940 return -1;
1941 }
1942 }
1943 return 0;
1944 }
1945
1946 ptr = os_strchr(buf, ' ');
1947 if (ptr == NULL)
1948 return -1;
1949 *ptr++ = '\0';
1950
1951 if (os_strncmp(ptr, "config=", 7) == 0)
1952 conf_file = ptr + 7;
1953
1954 for (i = 0; i < interfaces->count; i++) {
1955 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1956 buf)) {
1957 wpa_printf(MSG_INFO, "Cannot add interface - it "
1958 "already exists");
1959 return -1;
1960 }
1961 }
1962
1963 hapd_iface = hostapd_iface_alloc(interfaces);
1964 if (hapd_iface == NULL) {
1965 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1966 "for interface", __func__);
1967 goto fail;
1968 }
1969
1970 if (conf_file && interfaces->config_read_cb) {
1971 conf = interfaces->config_read_cb(conf_file);
1972 if (conf && conf->bss)
1973 os_strlcpy(conf->bss[0]->iface, buf,
1974 sizeof(conf->bss[0]->iface));
1975 } else
1976 conf = hostapd_config_alloc(interfaces, buf, ptr);
1977 if (conf == NULL || conf->bss == NULL) {
1978 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1979 "for configuration", __func__);
1980 goto fail;
1981 }
1982
1983 hapd_iface = hostapd_data_alloc(interfaces, conf);
1984 if (hapd_iface == NULL) {
1985 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1986 "for hostapd", __func__);
1987 goto fail;
1988 }
1989
1990 if (start_ctrl_iface(hapd_iface) < 0)
1991 goto fail;
1992
1993 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1994
1995 return 0;
1996
1997 fail:
1998 if (conf)
1999 hostapd_config_free(conf);
2000 if (hapd_iface) {
2001 if (hapd_iface->bss) {
2002 for (i = 0; i < hapd_iface->num_bss; i++) {
2003 hapd = hapd_iface->bss[i];
2004 if (!hapd)
2005 continue;
2006 if (hapd_iface->interfaces &&
2007 hapd_iface->interfaces->ctrl_iface_deinit)
2008 hapd_iface->interfaces->
2009 ctrl_iface_deinit(hapd);
2010 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2011 __func__, hapd_iface->bss[i],
2012 hapd->conf->iface);
2013 os_free(hapd);
2014 hapd_iface->bss[i] = NULL;
2015 }
2016 os_free(hapd_iface->bss);
2017 }
2018 wpa_printf(MSG_DEBUG, "%s: free iface %p",
2019 __func__, hapd_iface);
2020 os_free(hapd_iface);
2021 }
2022 return -1;
2023 }
2024
2025
2026 static int hostapd_remove_bss(struct hostapd_iface *iface, unsigned int idx)
2027 {
2028 size_t i;
2029
2030 wpa_printf(MSG_INFO, "Remove BSS '%s'", iface->conf->bss[idx]->iface);
2031
2032 /* Remove hostapd_data only if it has already been initialized */
2033 if (idx < iface->num_bss) {
2034 struct hostapd_data *hapd = iface->bss[idx];
2035
2036 hostapd_bss_deinit(hapd);
2037 wpa_printf(MSG_DEBUG, "%s: free hapd %p (%s)",
2038 __func__, hapd, hapd->conf->iface);
2039 hostapd_config_free_bss(hapd->conf);
2040 os_free(hapd);
2041
2042 iface->num_bss--;
2043
2044 for (i = idx; i < iface->num_bss; i++)
2045 iface->bss[i] = iface->bss[i + 1];
2046 } else {
2047 hostapd_config_free_bss(iface->conf->bss[idx]);
2048 iface->conf->bss[idx] = NULL;
2049 }
2050
2051 iface->conf->num_bss--;
2052 for (i = idx; i < iface->conf->num_bss; i++)
2053 iface->conf->bss[i] = iface->conf->bss[i + 1];
2054
2055 return 0;
2056 }
2057
2058
2059 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
2060 {
2061 struct hostapd_iface *hapd_iface;
2062 size_t i, j, k = 0;
2063
2064 for (i = 0; i < interfaces->count; i++) {
2065 hapd_iface = interfaces->iface[i];
2066 if (hapd_iface == NULL)
2067 return -1;
2068 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
2069 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
2070 hapd_iface->driver_ap_teardown =
2071 !!(hapd_iface->drv_flags &
2072 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2073
2074 hostapd_interface_deinit_free(hapd_iface);
2075 k = i;
2076 while (k < (interfaces->count - 1)) {
2077 interfaces->iface[k] =
2078 interfaces->iface[k + 1];
2079 k++;
2080 }
2081 interfaces->count--;
2082 return 0;
2083 }
2084
2085 for (j = 0; j < hapd_iface->conf->num_bss; j++) {
2086 if (!os_strcmp(hapd_iface->conf->bss[j]->iface, buf)) {
2087 hapd_iface->driver_ap_teardown =
2088 !(hapd_iface->drv_flags &
2089 WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
2090 return hostapd_remove_bss(hapd_iface, j);
2091 }
2092 }
2093 }
2094 return -1;
2095 }
2096
2097
2098 /**
2099 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
2100 * @hapd: Pointer to BSS data
2101 * @sta: Pointer to the associated STA data
2102 * @reassoc: 1 to indicate this was a re-association; 0 = first association
2103 *
2104 * This function will be called whenever a station associates with the AP. It
2105 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
2106 * from drv_callbacks.c based on driver events for drivers that take care of
2107 * management frames (IEEE 802.11 authentication and association) internally.
2108 */
2109 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
2110 int reassoc)
2111 {
2112 if (hapd->tkip_countermeasures) {
2113 hostapd_drv_sta_deauth(hapd, sta->addr,
2114 WLAN_REASON_MICHAEL_MIC_FAILURE);
2115 return;
2116 }
2117
2118 hostapd_prune_associations(hapd, sta->addr);
2119
2120 /* IEEE 802.11F (IAPP) */
2121 if (hapd->conf->ieee802_11f)
2122 iapp_new_station(hapd->iapp, sta);
2123
2124 #ifdef CONFIG_P2P
2125 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
2126 sta->no_p2p_set = 1;
2127 hapd->num_sta_no_p2p++;
2128 if (hapd->num_sta_no_p2p == 1)
2129 hostapd_p2p_non_p2p_sta_connected(hapd);
2130 }
2131 #endif /* CONFIG_P2P */
2132
2133 /* Start accounting here, if IEEE 802.1X and WPA are not used.
2134 * IEEE 802.1X/WPA code will start accounting after the station has
2135 * been authorized. */
2136 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) {
2137 ap_sta_set_authorized(hapd, sta, 1);
2138 os_get_reltime(&sta->connected_time);
2139 accounting_sta_start(hapd, sta);
2140 }
2141
2142 /* Start IEEE 802.1X authentication process for new stations */
2143 ieee802_1x_new_station(hapd, sta);
2144 if (reassoc) {
2145 if (sta->auth_alg != WLAN_AUTH_FT &&
2146 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
2147 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
2148 } else
2149 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
2150
2151 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
2152 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
2153 "for " MACSTR " (%d seconds - ap_max_inactivity)",
2154 __func__, MAC2STR(sta->addr),
2155 hapd->conf->ap_max_inactivity);
2156 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
2157 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
2158 ap_handle_timer, hapd, sta);
2159 }
2160 }
2161
2162
2163 const char * hostapd_state_text(enum hostapd_iface_state s)
2164 {
2165 switch (s) {
2166 case HAPD_IFACE_UNINITIALIZED:
2167 return "UNINITIALIZED";
2168 case HAPD_IFACE_DISABLED:
2169 return "DISABLED";
2170 case HAPD_IFACE_COUNTRY_UPDATE:
2171 return "COUNTRY_UPDATE";
2172 case HAPD_IFACE_ACS:
2173 return "ACS";
2174 case HAPD_IFACE_HT_SCAN:
2175 return "HT_SCAN";
2176 case HAPD_IFACE_DFS:
2177 return "DFS";
2178 case HAPD_IFACE_ENABLED:
2179 return "ENABLED";
2180 }
2181
2182 return "UNKNOWN";
2183 }
2184
2185
2186 void hostapd_set_state(struct hostapd_iface *iface, enum hostapd_iface_state s)
2187 {
2188 wpa_printf(MSG_INFO, "%s: interface state %s->%s",
2189 iface->conf->bss[0]->iface, hostapd_state_text(iface->state),
2190 hostapd_state_text(s));
2191 iface->state = s;
2192 }
2193
2194
2195 #ifdef NEED_AP_MLME
2196
2197 static void free_beacon_data(struct beacon_data *beacon)
2198 {
2199 os_free(beacon->head);
2200 beacon->head = NULL;
2201 os_free(beacon->tail);
2202 beacon->tail = NULL;
2203 os_free(beacon->probe_resp);
2204 beacon->probe_resp = NULL;
2205 os_free(beacon->beacon_ies);
2206 beacon->beacon_ies = NULL;
2207 os_free(beacon->proberesp_ies);
2208 beacon->proberesp_ies = NULL;
2209 os_free(beacon->assocresp_ies);
2210 beacon->assocresp_ies = NULL;
2211 }
2212
2213
2214 static int hostapd_build_beacon_data(struct hostapd_data *hapd,
2215 struct beacon_data *beacon)
2216 {
2217 struct wpabuf *beacon_extra, *proberesp_extra, *assocresp_extra;
2218 struct wpa_driver_ap_params params;
2219 int ret;
2220
2221 os_memset(beacon, 0, sizeof(*beacon));
2222 ret = ieee802_11_build_ap_params(hapd, &params);
2223 if (ret < 0)
2224 return ret;
2225
2226 ret = hostapd_build_ap_extra_ies(hapd, &beacon_extra,
2227 &proberesp_extra,
2228 &assocresp_extra);
2229 if (ret)
2230 goto free_ap_params;
2231
2232 ret = -1;
2233 beacon->head = os_malloc(params.head_len);
2234 if (!beacon->head)
2235 goto free_ap_extra_ies;
2236
2237 os_memcpy(beacon->head, params.head, params.head_len);
2238 beacon->head_len = params.head_len;
2239
2240 beacon->tail = os_malloc(params.tail_len);
2241 if (!beacon->tail)
2242 goto free_beacon;
2243
2244 os_memcpy(beacon->tail, params.tail, params.tail_len);
2245 beacon->tail_len = params.tail_len;
2246
2247 if (params.proberesp != NULL) {
2248 beacon->probe_resp = os_malloc(params.proberesp_len);
2249 if (!beacon->probe_resp)
2250 goto free_beacon;
2251
2252 os_memcpy(beacon->probe_resp, params.proberesp,
2253 params.proberesp_len);
2254 beacon->probe_resp_len = params.proberesp_len;
2255 }
2256
2257 /* copy the extra ies */
2258 if (beacon_extra) {
2259 beacon->beacon_ies = os_malloc(wpabuf_len(beacon_extra));
2260 if (!beacon->beacon_ies)
2261 goto free_beacon;
2262
2263 os_memcpy(beacon->beacon_ies,
2264 beacon_extra->buf, wpabuf_len(beacon_extra));
2265 beacon->beacon_ies_len = wpabuf_len(beacon_extra);
2266 }
2267
2268 if (proberesp_extra) {
2269 beacon->proberesp_ies =
2270 os_malloc(wpabuf_len(proberesp_extra));
2271 if (!beacon->proberesp_ies)
2272 goto free_beacon;
2273
2274 os_memcpy(beacon->proberesp_ies, proberesp_extra->buf,
2275 wpabuf_len(proberesp_extra));
2276 beacon->proberesp_ies_len = wpabuf_len(proberesp_extra);
2277 }
2278
2279 if (assocresp_extra) {
2280 beacon->assocresp_ies =
2281 os_malloc(wpabuf_len(assocresp_extra));
2282 if (!beacon->assocresp_ies)
2283 goto free_beacon;
2284
2285 os_memcpy(beacon->assocresp_ies, assocresp_extra->buf,
2286 wpabuf_len(assocresp_extra));
2287 beacon->assocresp_ies_len = wpabuf_len(assocresp_extra);
2288 }
2289
2290 ret = 0;
2291 free_beacon:
2292 /* if the function fails, the caller should not free beacon data */
2293 if (ret)
2294 free_beacon_data(beacon);
2295
2296 free_ap_extra_ies:
2297 hostapd_free_ap_extra_ies(hapd, beacon_extra, proberesp_extra,
2298 assocresp_extra);
2299 free_ap_params:
2300 ieee802_11_free_ap_params(&params);
2301 return ret;
2302 }
2303
2304
2305 /*
2306 * TODO: This flow currently supports only changing frequency within the
2307 * same hw_mode. Any other changes to MAC parameters or provided settings (even
2308 * width) are not supported.
2309 */
2310 static int hostapd_change_config_freq(struct hostapd_data *hapd,
2311 struct hostapd_config *conf,
2312 struct hostapd_freq_params *params,
2313 struct hostapd_freq_params *old_params)
2314 {
2315 int channel;
2316
2317 if (!params->channel) {
2318 /* check if the new channel is supported by hw */
2319 params->channel = hostapd_hw_get_channel(hapd, params->freq);
2320 }
2321
2322 channel = params->channel;
2323 if (!channel)
2324 return -1;
2325
2326 /* if a pointer to old_params is provided we save previous state */
2327 if (old_params) {
2328 old_params->channel = conf->channel;
2329 old_params->ht_enabled = conf->ieee80211n;
2330 old_params->sec_channel_offset = conf->secondary_channel;
2331 }
2332
2333 conf->channel = channel;
2334 conf->ieee80211n = params->ht_enabled;
2335 conf->secondary_channel = params->sec_channel_offset;
2336
2337 /* TODO: maybe call here hostapd_config_check here? */
2338
2339 return 0;
2340 }
2341
2342
2343 static int hostapd_fill_csa_settings(struct hostapd_data *hapd,
2344 struct csa_settings *settings)
2345 {
2346 struct hostapd_iface *iface = hapd->iface;
2347 struct hostapd_freq_params old_freq;
2348 int ret;
2349
2350 os_memset(&old_freq, 0, sizeof(old_freq));
2351 if (!iface || !iface->freq || hapd->csa_in_progress)
2352 return -1;
2353
2354 ret = hostapd_change_config_freq(iface->bss[0], iface->conf,
2355 &settings->freq_params,
2356 &old_freq);
2357 if (ret)
2358 return ret;
2359
2360 ret = hostapd_build_beacon_data(hapd, &settings->beacon_after);
2361
2362 /* change back the configuration */
2363 hostapd_change_config_freq(iface->bss[0], iface->conf,
2364 &old_freq, NULL);
2365
2366 if (ret)
2367 return ret;
2368
2369 /* set channel switch parameters for csa ie */
2370 hapd->cs_freq_params = settings->freq_params;
2371 hapd->cs_count = settings->cs_count;
2372 hapd->cs_block_tx = settings->block_tx;
2373
2374 ret = hostapd_build_beacon_data(hapd, &settings->beacon_csa);
2375 if (ret) {
2376 free_beacon_data(&settings->beacon_after);
2377 return ret;
2378 }
2379
2380 settings->counter_offset_beacon = hapd->cs_c_off_beacon;
2381 settings->counter_offset_presp = hapd->cs_c_off_proberesp;
2382
2383 return 0;
2384 }
2385
2386
2387 void hostapd_cleanup_cs_params(struct hostapd_data *hapd)
2388 {
2389 os_memset(&hapd->cs_freq_params, 0, sizeof(hapd->cs_freq_params));
2390 hapd->cs_count = 0;
2391 hapd->cs_block_tx = 0;
2392 hapd->cs_c_off_beacon = 0;
2393 hapd->cs_c_off_proberesp = 0;
2394 hapd->csa_in_progress = 0;
2395 }
2396
2397
2398 int hostapd_switch_channel(struct hostapd_data *hapd,
2399 struct csa_settings *settings)
2400 {
2401 int ret;
2402
2403 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
2404 wpa_printf(MSG_INFO, "CSA is not supported");
2405 return -1;
2406 }
2407
2408 ret = hostapd_fill_csa_settings(hapd, settings);
2409 if (ret)
2410 return ret;
2411
2412 ret = hostapd_drv_switch_channel(hapd, settings);
2413 free_beacon_data(&settings->beacon_csa);
2414 free_beacon_data(&settings->beacon_after);
2415
2416 if (ret) {
2417 /* if we failed, clean cs parameters */
2418 hostapd_cleanup_cs_params(hapd);
2419 return ret;
2420 }
2421
2422 hapd->csa_in_progress = 1;
2423 return 0;
2424 }
2425
2426
2427 void
2428 hostapd_switch_channel_fallback(struct hostapd_iface *iface,
2429 const struct hostapd_freq_params *freq_params)
2430 {
2431 int vht_seg0_idx = 0, vht_seg1_idx = 0, vht_bw = VHT_CHANWIDTH_USE_HT;
2432 unsigned int i;
2433
2434 wpa_printf(MSG_DEBUG, "Restarting all CSA-related BSSes");
2435
2436 if (freq_params->center_freq1)
2437 vht_seg0_idx = 36 + (freq_params->center_freq1 - 5180) / 5;
2438 if (freq_params->center_freq2)
2439 vht_seg1_idx = 36 + (freq_params->center_freq2 - 5180) / 5;
2440
2441 switch (freq_params->bandwidth) {
2442 case 0:
2443 case 20:
2444 case 40:
2445 vht_bw = VHT_CHANWIDTH_USE_HT;
2446 break;
2447 case 80:
2448 if (freq_params->center_freq2)
2449 vht_bw = VHT_CHANWIDTH_80P80MHZ;
2450 else
2451 vht_bw = VHT_CHANWIDTH_80MHZ;
2452 break;
2453 case 160:
2454 vht_bw = VHT_CHANWIDTH_160MHZ;
2455 break;
2456 default:
2457 wpa_printf(MSG_WARNING, "Unknown CSA bandwidth: %d",
2458 freq_params->bandwidth);
2459 break;
2460 }
2461
2462 iface->freq = freq_params->freq;
2463 iface->conf->channel = freq_params->channel;
2464 iface->conf->secondary_channel = freq_params->sec_channel_offset;
2465 iface->conf->vht_oper_centr_freq_seg0_idx = vht_seg0_idx;
2466 iface->conf->vht_oper_centr_freq_seg1_idx = vht_seg1_idx;
2467 iface->conf->vht_oper_chwidth = vht_bw;
2468 iface->conf->ieee80211n = freq_params->ht_enabled;
2469 iface->conf->ieee80211ac = freq_params->vht_enabled;
2470
2471 /*
2472 * cs_params must not be cleared earlier because the freq_params
2473 * argument may actually point to one of these.
2474 */
2475 for (i = 0; i < iface->num_bss; i++)
2476 hostapd_cleanup_cs_params(iface->bss[i]);
2477
2478 hostapd_disable_iface(iface);
2479 hostapd_enable_iface(iface);
2480 }
2481
2482 #endif /* NEED_AP_MLME */