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