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