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