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