]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/hostapd.c
hostapd: Fix error path in hostapd_add_iface()
[thirdparty/hostap.git] / src / ap / hostapd.c
1 /*
2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2012, 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 "radius/radius_client.h"
15 #include "radius/radius_das.h"
16 #include "drivers/driver.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
42 extern int wpa_debug_level;
43 extern struct wpa_driver_ops *wpa_drivers[];
44
45
46 int hostapd_for_each_interface(struct hapd_interfaces *interfaces,
47 int (*cb)(struct hostapd_iface *iface,
48 void *ctx), void *ctx)
49 {
50 size_t i;
51 int ret;
52
53 for (i = 0; i < interfaces->count; i++) {
54 ret = cb(interfaces->iface[i], ctx);
55 if (ret)
56 return ret;
57 }
58
59 return 0;
60 }
61
62
63 static void hostapd_reload_bss(struct hostapd_data *hapd)
64 {
65 struct hostapd_ssid *ssid;
66
67 #ifndef CONFIG_NO_RADIUS
68 radius_client_reconfig(hapd->radius, hapd->conf->radius);
69 #endif /* CONFIG_NO_RADIUS */
70
71 ssid = &hapd->conf->ssid;
72 if (!ssid->wpa_psk_set && ssid->wpa_psk && !ssid->wpa_psk->next &&
73 ssid->wpa_passphrase_set && ssid->wpa_passphrase) {
74 /*
75 * Force PSK to be derived again since SSID or passphrase may
76 * have changed.
77 */
78 os_free(ssid->wpa_psk);
79 ssid->wpa_psk = NULL;
80 }
81 if (hostapd_setup_wpa_psk(hapd->conf)) {
82 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
83 "after reloading configuration");
84 }
85
86 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
87 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
88 else
89 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
90
91 if (hapd->conf->wpa && hapd->wpa_auth == NULL) {
92 hostapd_setup_wpa(hapd);
93 if (hapd->wpa_auth)
94 wpa_init_keys(hapd->wpa_auth);
95 } else if (hapd->conf->wpa) {
96 const u8 *wpa_ie;
97 size_t wpa_ie_len;
98 hostapd_reconfig_wpa(hapd);
99 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
100 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len))
101 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
102 "the kernel driver.");
103 } else if (hapd->wpa_auth) {
104 wpa_deinit(hapd->wpa_auth);
105 hapd->wpa_auth = NULL;
106 hostapd_set_privacy(hapd, 0);
107 hostapd_setup_encryption(hapd->conf->iface, hapd);
108 hostapd_set_generic_elem(hapd, (u8 *) "", 0);
109 }
110
111 ieee802_11_set_beacon(hapd);
112 hostapd_update_wps(hapd);
113
114 if (hapd->conf->ssid.ssid_set &&
115 hostapd_set_ssid(hapd, hapd->conf->ssid.ssid,
116 hapd->conf->ssid.ssid_len)) {
117 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
118 /* try to continue */
119 }
120 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
121 }
122
123
124 static void hostapd_clear_old(struct hostapd_iface *iface)
125 {
126 size_t j;
127
128 /*
129 * Deauthenticate all stations since the new configuration may not
130 * allow them to use the BSS anymore.
131 */
132 for (j = 0; j < iface->num_bss; j++) {
133 hostapd_flush_old_stations(iface->bss[j],
134 WLAN_REASON_PREV_AUTH_NOT_VALID);
135 hostapd_broadcast_wep_clear(iface->bss[j]);
136
137 #ifndef CONFIG_NO_RADIUS
138 /* TODO: update dynamic data based on changed configuration
139 * items (e.g., open/close sockets, etc.) */
140 radius_client_flush(iface->bss[j]->radius, 0);
141 #endif /* CONFIG_NO_RADIUS */
142 }
143 }
144
145
146 int hostapd_reload_config(struct hostapd_iface *iface)
147 {
148 struct hostapd_data *hapd = iface->bss[0];
149 struct hostapd_config *newconf, *oldconf;
150 size_t j;
151
152 if (iface->config_fname == NULL) {
153 /* Only in-memory config in use - assume it has been updated */
154 hostapd_clear_old(iface);
155 for (j = 0; j < iface->num_bss; j++)
156 hostapd_reload_bss(iface->bss[j]);
157 return 0;
158 }
159
160 if (iface->interfaces == NULL ||
161 iface->interfaces->config_read_cb == NULL)
162 return -1;
163 newconf = iface->interfaces->config_read_cb(iface->config_fname);
164 if (newconf == NULL)
165 return -1;
166
167 hostapd_clear_old(iface);
168
169 oldconf = hapd->iconf;
170 iface->conf = newconf;
171
172 for (j = 0; j < iface->num_bss; j++) {
173 hapd = iface->bss[j];
174 hapd->iconf = newconf;
175 hapd->conf = newconf->bss[j];
176 hostapd_reload_bss(hapd);
177 }
178
179 hostapd_config_free(oldconf);
180
181
182 return 0;
183 }
184
185
186 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
187 char *ifname)
188 {
189 int i;
190
191 for (i = 0; i < NUM_WEP_KEYS; i++) {
192 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
193 0, NULL, 0, NULL, 0)) {
194 wpa_printf(MSG_DEBUG, "Failed to clear default "
195 "encryption keys (ifname=%s keyidx=%d)",
196 ifname, i);
197 }
198 }
199 #ifdef CONFIG_IEEE80211W
200 if (hapd->conf->ieee80211w) {
201 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
202 if (hostapd_drv_set_key(ifname, hapd, WPA_ALG_NONE,
203 NULL, i, 0, NULL,
204 0, NULL, 0)) {
205 wpa_printf(MSG_DEBUG, "Failed to clear "
206 "default mgmt encryption keys "
207 "(ifname=%s keyidx=%d)", ifname, i);
208 }
209 }
210 }
211 #endif /* CONFIG_IEEE80211W */
212 }
213
214
215 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
216 {
217 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
218 return 0;
219 }
220
221
222 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
223 {
224 int errors = 0, idx;
225 struct hostapd_ssid *ssid = &hapd->conf->ssid;
226
227 idx = ssid->wep.idx;
228 if (ssid->wep.default_len &&
229 hostapd_drv_set_key(hapd->conf->iface,
230 hapd, WPA_ALG_WEP, broadcast_ether_addr, idx,
231 1, NULL, 0, ssid->wep.key[idx],
232 ssid->wep.len[idx])) {
233 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
234 errors++;
235 }
236
237 return errors;
238 }
239
240
241 static void hostapd_free_hapd_data(struct hostapd_data *hapd)
242 {
243 iapp_deinit(hapd->iapp);
244 hapd->iapp = NULL;
245 accounting_deinit(hapd);
246 hostapd_deinit_wpa(hapd);
247 vlan_deinit(hapd);
248 hostapd_acl_deinit(hapd);
249 #ifndef CONFIG_NO_RADIUS
250 radius_client_deinit(hapd->radius);
251 hapd->radius = NULL;
252 radius_das_deinit(hapd->radius_das);
253 hapd->radius_das = NULL;
254 #endif /* CONFIG_NO_RADIUS */
255
256 hostapd_deinit_wps(hapd);
257
258 authsrv_deinit(hapd);
259
260 if (hapd->interface_added &&
261 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
262 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
263 hapd->conf->iface);
264 }
265
266 os_free(hapd->probereq_cb);
267 hapd->probereq_cb = NULL;
268
269 #ifdef CONFIG_P2P
270 wpabuf_free(hapd->p2p_beacon_ie);
271 hapd->p2p_beacon_ie = NULL;
272 wpabuf_free(hapd->p2p_probe_resp_ie);
273 hapd->p2p_probe_resp_ie = NULL;
274 #endif /* CONFIG_P2P */
275
276 wpabuf_free(hapd->time_adv);
277
278 #ifdef CONFIG_INTERWORKING
279 gas_serv_deinit(hapd);
280 #endif /* CONFIG_INTERWORKING */
281
282 #ifdef CONFIG_SQLITE
283 os_free(hapd->tmp_eap_user.identity);
284 os_free(hapd->tmp_eap_user.password);
285 #endif /* CONFIG_SQLITE */
286 }
287
288
289 /**
290 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
291 * @hapd: Pointer to BSS data
292 *
293 * This function is used to free all per-BSS data structures and resources.
294 * This gets called in a loop for each BSS between calls to
295 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
296 * is deinitialized. Most of the modules that are initialized in
297 * hostapd_setup_bss() are deinitialized here.
298 */
299 static void hostapd_cleanup(struct hostapd_data *hapd)
300 {
301 if (hapd->iface->interfaces &&
302 hapd->iface->interfaces->ctrl_iface_deinit)
303 hapd->iface->interfaces->ctrl_iface_deinit(hapd);
304 hostapd_free_hapd_data(hapd);
305 }
306
307
308 /**
309 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
310 * @iface: Pointer to interface data
311 *
312 * This function is called before per-BSS data structures are deinitialized
313 * with hostapd_cleanup().
314 */
315 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
316 {
317 }
318
319
320 static void hostapd_cleanup_iface_partial(struct hostapd_iface *iface)
321 {
322 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
323 iface->hw_features = NULL;
324 os_free(iface->current_rates);
325 iface->current_rates = NULL;
326 os_free(iface->basic_rates);
327 iface->basic_rates = NULL;
328 ap_list_deinit(iface);
329 }
330
331
332 /**
333 * hostapd_cleanup_iface - Complete per-interface cleanup
334 * @iface: Pointer to interface data
335 *
336 * This function is called after per-BSS data structures are deinitialized
337 * with hostapd_cleanup().
338 */
339 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
340 {
341 hostapd_cleanup_iface_partial(iface);
342 hostapd_config_free(iface->conf);
343 iface->conf = NULL;
344
345 os_free(iface->config_fname);
346 os_free(iface->bss);
347 os_free(iface);
348 }
349
350
351 static void hostapd_clear_wep(struct hostapd_data *hapd)
352 {
353 if (hapd->drv_priv) {
354 hostapd_set_privacy(hapd, 0);
355 hostapd_broadcast_wep_clear(hapd);
356 }
357 }
358
359
360 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
361 {
362 int i;
363
364 hostapd_broadcast_wep_set(hapd);
365
366 if (hapd->conf->ssid.wep.default_len) {
367 hostapd_set_privacy(hapd, 1);
368 return 0;
369 }
370
371 /*
372 * When IEEE 802.1X is not enabled, the driver may need to know how to
373 * set authentication algorithms for static WEP.
374 */
375 hostapd_drv_set_authmode(hapd, hapd->conf->auth_algs);
376
377 for (i = 0; i < 4; i++) {
378 if (hapd->conf->ssid.wep.key[i] &&
379 hostapd_drv_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
380 i == hapd->conf->ssid.wep.idx, NULL, 0,
381 hapd->conf->ssid.wep.key[i],
382 hapd->conf->ssid.wep.len[i])) {
383 wpa_printf(MSG_WARNING, "Could not set WEP "
384 "encryption.");
385 return -1;
386 }
387 if (hapd->conf->ssid.wep.key[i] &&
388 i == hapd->conf->ssid.wep.idx)
389 hostapd_set_privacy(hapd, 1);
390 }
391
392 return 0;
393 }
394
395
396 static int hostapd_flush_old_stations(struct hostapd_data *hapd, u16 reason)
397 {
398 int ret = 0;
399 u8 addr[ETH_ALEN];
400
401 if (hostapd_drv_none(hapd) || hapd->drv_priv == NULL)
402 return 0;
403
404 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Flushing old station entries");
405 if (hostapd_flush(hapd)) {
406 wpa_msg(hapd->msg_ctx, MSG_WARNING, "Could not connect to "
407 "kernel driver");
408 ret = -1;
409 }
410 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "Deauthenticate all stations");
411 os_memset(addr, 0xff, ETH_ALEN);
412 hostapd_drv_sta_deauth(hapd, addr, reason);
413 hostapd_free_stas(hapd);
414
415 return ret;
416 }
417
418
419 /**
420 * hostapd_validate_bssid_configuration - Validate BSSID configuration
421 * @iface: Pointer to interface data
422 * Returns: 0 on success, -1 on failure
423 *
424 * This function is used to validate that the configured BSSIDs are valid.
425 */
426 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
427 {
428 u8 mask[ETH_ALEN] = { 0 };
429 struct hostapd_data *hapd = iface->bss[0];
430 unsigned int i = iface->conf->num_bss, bits = 0, j;
431 int auto_addr = 0;
432
433 if (hostapd_drv_none(hapd))
434 return 0;
435
436 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
437
438 /* Determine the bits necessary to cover the number of BSSIDs. */
439 for (i--; i; i >>= 1)
440 bits++;
441
442 /* Determine the bits necessary to any configured BSSIDs,
443 if they are higher than the number of BSSIDs. */
444 for (j = 0; j < iface->conf->num_bss; j++) {
445 if (hostapd_mac_comp_empty(iface->conf->bss[j]->bssid) == 0) {
446 if (j)
447 auto_addr++;
448 continue;
449 }
450
451 for (i = 0; i < ETH_ALEN; i++) {
452 mask[i] |=
453 iface->conf->bss[j]->bssid[i] ^
454 hapd->own_addr[i];
455 }
456 }
457
458 if (!auto_addr)
459 goto skip_mask_ext;
460
461 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
462 ;
463 j = 0;
464 if (i < ETH_ALEN) {
465 j = (5 - i) * 8;
466
467 while (mask[i] != 0) {
468 mask[i] >>= 1;
469 j++;
470 }
471 }
472
473 if (bits < j)
474 bits = j;
475
476 if (bits > 40) {
477 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
478 bits);
479 return -1;
480 }
481
482 os_memset(mask, 0xff, ETH_ALEN);
483 j = bits / 8;
484 for (i = 5; i > 5 - j; i--)
485 mask[i] = 0;
486 j = bits % 8;
487 while (j--)
488 mask[i] <<= 1;
489
490 skip_mask_ext:
491 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
492 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
493
494 if (!auto_addr)
495 return 0;
496
497 for (i = 0; i < ETH_ALEN; i++) {
498 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
499 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
500 " for start address " MACSTR ".",
501 MAC2STR(mask), MAC2STR(hapd->own_addr));
502 wpa_printf(MSG_ERROR, "Start address must be the "
503 "first address in the block (i.e., addr "
504 "AND mask == addr).");
505 return -1;
506 }
507 }
508
509 return 0;
510 }
511
512
513 static int mac_in_conf(struct hostapd_config *conf, const void *a)
514 {
515 size_t i;
516
517 for (i = 0; i < conf->num_bss; i++) {
518 if (hostapd_mac_comp(conf->bss[i]->bssid, a) == 0) {
519 return 1;
520 }
521 }
522
523 return 0;
524 }
525
526
527 #ifndef CONFIG_NO_RADIUS
528
529 static int hostapd_das_nas_mismatch(struct hostapd_data *hapd,
530 struct radius_das_attrs *attr)
531 {
532 /* TODO */
533 return 0;
534 }
535
536
537 static struct sta_info * hostapd_das_find_sta(struct hostapd_data *hapd,
538 struct radius_das_attrs *attr)
539 {
540 struct sta_info *sta = NULL;
541 char buf[128];
542
543 if (attr->sta_addr)
544 sta = ap_get_sta(hapd, attr->sta_addr);
545
546 if (sta == NULL && attr->acct_session_id &&
547 attr->acct_session_id_len == 17) {
548 for (sta = hapd->sta_list; sta; sta = sta->next) {
549 os_snprintf(buf, sizeof(buf), "%08X-%08X",
550 sta->acct_session_id_hi,
551 sta->acct_session_id_lo);
552 if (os_memcmp(attr->acct_session_id, buf, 17) == 0)
553 break;
554 }
555 }
556
557 if (sta == NULL && attr->cui) {
558 for (sta = hapd->sta_list; sta; sta = sta->next) {
559 struct wpabuf *cui;
560 cui = ieee802_1x_get_radius_cui(sta->eapol_sm);
561 if (cui && wpabuf_len(cui) == attr->cui_len &&
562 os_memcmp(wpabuf_head(cui), attr->cui,
563 attr->cui_len) == 0)
564 break;
565 }
566 }
567
568 if (sta == NULL && attr->user_name) {
569 for (sta = hapd->sta_list; sta; sta = sta->next) {
570 u8 *identity;
571 size_t identity_len;
572 identity = ieee802_1x_get_identity(sta->eapol_sm,
573 &identity_len);
574 if (identity &&
575 identity_len == attr->user_name_len &&
576 os_memcmp(identity, attr->user_name, identity_len)
577 == 0)
578 break;
579 }
580 }
581
582 return sta;
583 }
584
585
586 static enum radius_das_res
587 hostapd_das_disconnect(void *ctx, struct radius_das_attrs *attr)
588 {
589 struct hostapd_data *hapd = ctx;
590 struct sta_info *sta;
591
592 if (hostapd_das_nas_mismatch(hapd, attr))
593 return RADIUS_DAS_NAS_MISMATCH;
594
595 sta = hostapd_das_find_sta(hapd, attr);
596 if (sta == NULL)
597 return RADIUS_DAS_SESSION_NOT_FOUND;
598
599 hostapd_drv_sta_deauth(hapd, sta->addr,
600 WLAN_REASON_PREV_AUTH_NOT_VALID);
601 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_PREV_AUTH_NOT_VALID);
602
603 return RADIUS_DAS_SUCCESS;
604 }
605
606 #endif /* CONFIG_NO_RADIUS */
607
608
609 /**
610 * hostapd_setup_bss - Per-BSS setup (initialization)
611 * @hapd: Pointer to BSS data
612 * @first: Whether this BSS is the first BSS of an interface
613 *
614 * This function is used to initialize all per-BSS data structures and
615 * resources. This gets called in a loop for each BSS when an interface is
616 * initialized. Most of the modules that are initialized here will be
617 * deinitialized in hostapd_cleanup().
618 */
619 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
620 {
621 struct hostapd_bss_config *conf = hapd->conf;
622 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
623 int ssid_len, set_ssid;
624 char force_ifname[IFNAMSIZ];
625 u8 if_addr[ETH_ALEN];
626
627 if (!first) {
628 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
629 /* Allocate the next available BSSID. */
630 do {
631 inc_byte_array(hapd->own_addr, ETH_ALEN);
632 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
633 } else {
634 /* Allocate the configured BSSID. */
635 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
636
637 if (hostapd_mac_comp(hapd->own_addr,
638 hapd->iface->bss[0]->own_addr) ==
639 0) {
640 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
641 "BSSID set to the MAC address of "
642 "the radio", hapd->conf->iface);
643 return -1;
644 }
645 }
646
647 hapd->interface_added = 1;
648 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
649 hapd->conf->iface, hapd->own_addr, hapd,
650 &hapd->drv_priv, force_ifname, if_addr,
651 hapd->conf->bridge[0] ? hapd->conf->bridge :
652 NULL)) {
653 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
654 MACSTR ")", MAC2STR(hapd->own_addr));
655 return -1;
656 }
657 }
658
659 if (conf->wmm_enabled < 0)
660 conf->wmm_enabled = hapd->iconf->ieee80211n;
661
662 hostapd_flush_old_stations(hapd, WLAN_REASON_PREV_AUTH_NOT_VALID);
663 hostapd_set_privacy(hapd, 0);
664
665 hostapd_broadcast_wep_clear(hapd);
666 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
667 return -1;
668
669 /*
670 * Fetch the SSID from the system and use it or,
671 * if one was specified in the config file, verify they
672 * match.
673 */
674 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
675 if (ssid_len < 0) {
676 wpa_printf(MSG_ERROR, "Could not read SSID from system");
677 return -1;
678 }
679 if (conf->ssid.ssid_set) {
680 /*
681 * If SSID is specified in the config file and it differs
682 * from what is being used then force installation of the
683 * new SSID.
684 */
685 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
686 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
687 } else {
688 /*
689 * No SSID in the config file; just use the one we got
690 * from the system.
691 */
692 set_ssid = 0;
693 conf->ssid.ssid_len = ssid_len;
694 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
695 }
696
697 if (!hostapd_drv_none(hapd)) {
698 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
699 " and ssid \"%s\"",
700 hapd->conf->iface, MAC2STR(hapd->own_addr),
701 wpa_ssid_txt(hapd->conf->ssid.ssid,
702 hapd->conf->ssid.ssid_len));
703 }
704
705 if (hostapd_setup_wpa_psk(conf)) {
706 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
707 return -1;
708 }
709
710 /* Set SSID for the kernel driver (to be used in beacon and probe
711 * response frames) */
712 if (set_ssid && hostapd_set_ssid(hapd, conf->ssid.ssid,
713 conf->ssid.ssid_len)) {
714 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
715 return -1;
716 }
717
718 if (wpa_debug_level == MSG_MSGDUMP)
719 conf->radius->msg_dumps = 1;
720 #ifndef CONFIG_NO_RADIUS
721 hapd->radius = radius_client_init(hapd, conf->radius);
722 if (hapd->radius == NULL) {
723 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
724 return -1;
725 }
726
727 if (hapd->conf->radius_das_port) {
728 struct radius_das_conf das_conf;
729 os_memset(&das_conf, 0, sizeof(das_conf));
730 das_conf.port = hapd->conf->radius_das_port;
731 das_conf.shared_secret = hapd->conf->radius_das_shared_secret;
732 das_conf.shared_secret_len =
733 hapd->conf->radius_das_shared_secret_len;
734 das_conf.client_addr = &hapd->conf->radius_das_client_addr;
735 das_conf.time_window = hapd->conf->radius_das_time_window;
736 das_conf.require_event_timestamp =
737 hapd->conf->radius_das_require_event_timestamp;
738 das_conf.ctx = hapd;
739 das_conf.disconnect = hostapd_das_disconnect;
740 hapd->radius_das = radius_das_init(&das_conf);
741 if (hapd->radius_das == NULL) {
742 wpa_printf(MSG_ERROR, "RADIUS DAS initialization "
743 "failed.");
744 return -1;
745 }
746 }
747 #endif /* CONFIG_NO_RADIUS */
748
749 if (hostapd_acl_init(hapd)) {
750 wpa_printf(MSG_ERROR, "ACL initialization failed.");
751 return -1;
752 }
753 if (hostapd_init_wps(hapd, conf))
754 return -1;
755
756 if (authsrv_init(hapd) < 0)
757 return -1;
758
759 if (ieee802_1x_init(hapd)) {
760 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
761 return -1;
762 }
763
764 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
765 return -1;
766
767 if (accounting_init(hapd)) {
768 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
769 return -1;
770 }
771
772 if (hapd->conf->ieee802_11f &&
773 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
774 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
775 "failed.");
776 return -1;
777 }
778
779 #ifdef CONFIG_INTERWORKING
780 if (gas_serv_init(hapd)) {
781 wpa_printf(MSG_ERROR, "GAS server initialization failed");
782 return -1;
783 }
784 #endif /* CONFIG_INTERWORKING */
785
786 if (hapd->iface->interfaces &&
787 hapd->iface->interfaces->ctrl_iface_init &&
788 hapd->iface->interfaces->ctrl_iface_init(hapd)) {
789 wpa_printf(MSG_ERROR, "Failed to setup control interface");
790 return -1;
791 }
792
793 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
794 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
795 return -1;
796 }
797
798 if (!hapd->conf->start_disabled)
799 ieee802_11_set_beacon(hapd);
800
801 if (hapd->wpa_auth && wpa_init_keys(hapd->wpa_auth) < 0)
802 return -1;
803
804 if (hapd->driver && hapd->driver->set_operstate)
805 hapd->driver->set_operstate(hapd->drv_priv, 1);
806
807 return 0;
808 }
809
810
811 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
812 {
813 struct hostapd_data *hapd = iface->bss[0];
814 int i;
815 struct hostapd_tx_queue_params *p;
816
817 for (i = 0; i < NUM_TX_QUEUES; i++) {
818 p = &iface->conf->tx_queue[i];
819
820 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
821 p->cwmax, p->burst)) {
822 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
823 "parameters for queue %d.", i);
824 /* Continue anyway */
825 }
826 }
827 }
828
829
830 static int hostapd_set_acl_list(struct hostapd_data *hapd,
831 struct mac_acl_entry *mac_acl,
832 int n_entries, u8 accept_acl)
833 {
834 struct hostapd_acl_params *acl_params;
835 int i, err;
836
837 acl_params = os_zalloc(sizeof(*acl_params) +
838 (n_entries * sizeof(acl_params->mac_acl[0])));
839 if (!acl_params)
840 return -ENOMEM;
841
842 for (i = 0; i < n_entries; i++)
843 os_memcpy(acl_params->mac_acl[i].addr, mac_acl[i].addr,
844 ETH_ALEN);
845
846 acl_params->acl_policy = accept_acl;
847 acl_params->num_mac_acl = n_entries;
848
849 err = hostapd_drv_set_acl(hapd, acl_params);
850
851 os_free(acl_params);
852
853 return err;
854 }
855
856
857 static void hostapd_set_acl(struct hostapd_data *hapd)
858 {
859 struct hostapd_config *conf = hapd->iconf;
860 int err;
861 u8 accept_acl;
862
863 if (hapd->iface->drv_max_acl_mac_addrs == 0)
864 return;
865 if (!(conf->bss[0]->num_accept_mac || conf->bss[0]->num_deny_mac))
866 return;
867
868 if (conf->bss[0]->macaddr_acl == DENY_UNLESS_ACCEPTED) {
869 if (conf->bss[0]->num_accept_mac) {
870 accept_acl = 1;
871 err = hostapd_set_acl_list(hapd,
872 conf->bss[0]->accept_mac,
873 conf->bss[0]->num_accept_mac,
874 accept_acl);
875 if (err) {
876 wpa_printf(MSG_DEBUG, "Failed to set accept acl");
877 return;
878 }
879 } else {
880 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
881 }
882 } else if (conf->bss[0]->macaddr_acl == ACCEPT_UNLESS_DENIED) {
883 if (conf->bss[0]->num_deny_mac) {
884 accept_acl = 0;
885 err = hostapd_set_acl_list(hapd, conf->bss[0]->deny_mac,
886 conf->bss[0]->num_deny_mac,
887 accept_acl);
888 if (err) {
889 wpa_printf(MSG_DEBUG, "Failed to set deny acl");
890 return;
891 }
892 } else {
893 wpa_printf(MSG_DEBUG, "Mismatch between ACL Policy & Accept/deny lists file");
894 }
895 }
896 }
897
898
899 static int setup_interface(struct hostapd_iface *iface)
900 {
901 struct hostapd_data *hapd = iface->bss[0];
902 size_t i;
903 char country[4];
904
905 /*
906 * Make sure that all BSSes get configured with a pointer to the same
907 * driver interface.
908 */
909 for (i = 1; i < iface->num_bss; i++) {
910 iface->bss[i]->driver = hapd->driver;
911 iface->bss[i]->drv_priv = hapd->drv_priv;
912 }
913
914 if (hostapd_validate_bssid_configuration(iface))
915 return -1;
916
917 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
918 os_memcpy(country, hapd->iconf->country, 3);
919 country[3] = '\0';
920 if (hostapd_set_country(hapd, country) < 0) {
921 wpa_printf(MSG_ERROR, "Failed to set country code");
922 return -1;
923 }
924 }
925
926 if (hostapd_get_hw_features(iface)) {
927 /* Not all drivers support this yet, so continue without hw
928 * feature data. */
929 } else {
930 int ret = hostapd_select_hw_mode(iface);
931 if (ret < 0) {
932 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
933 "channel. (%d)", ret);
934 return -1;
935 }
936 if (ret == 1) {
937 wpa_printf(MSG_DEBUG, "Interface initialization will be completed in a callback (ACS)");
938 return 0;
939 }
940 ret = hostapd_check_ht_capab(iface);
941 if (ret < 0)
942 return -1;
943 if (ret == 1) {
944 wpa_printf(MSG_DEBUG, "Interface initialization will "
945 "be completed in a callback");
946 return 0;
947 }
948
949 if (iface->conf->ieee80211h)
950 wpa_printf(MSG_DEBUG, "DFS support is enabled");
951 }
952 return hostapd_setup_interface_complete(iface, 0);
953 }
954
955
956 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
957 {
958 struct hostapd_data *hapd = iface->bss[0];
959 size_t j;
960 u8 *prev_addr;
961
962 if (err) {
963 wpa_printf(MSG_ERROR, "Interface initialization failed");
964 eloop_terminate();
965 return -1;
966 }
967
968 wpa_printf(MSG_DEBUG, "Completing interface initialization");
969 if (hapd->iconf->channel) {
970 #ifdef NEED_AP_MLME
971 int res;
972 #endif /* NEED_AP_MLME */
973
974 iface->freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
975 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
976 "Frequency: %d MHz",
977 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
978 hapd->iconf->channel, iface->freq);
979
980 #ifdef NEED_AP_MLME
981 /* Check DFS */
982 res = hostapd_handle_dfs(hapd);
983 if (res <= 0)
984 return res;
985 #endif /* NEED_AP_MLME */
986
987 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, iface->freq,
988 hapd->iconf->channel,
989 hapd->iconf->ieee80211n,
990 hapd->iconf->ieee80211ac,
991 hapd->iconf->secondary_channel,
992 hapd->iconf->vht_oper_chwidth,
993 hapd->iconf->vht_oper_centr_freq_seg0_idx,
994 hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
995 wpa_printf(MSG_ERROR, "Could not set channel for "
996 "kernel driver");
997 return -1;
998 }
999 }
1000
1001 if (iface->current_mode) {
1002 if (hostapd_prepare_rates(iface, iface->current_mode)) {
1003 wpa_printf(MSG_ERROR, "Failed to prepare rates "
1004 "table.");
1005 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1006 HOSTAPD_LEVEL_WARNING,
1007 "Failed to prepare rates table.");
1008 return -1;
1009 }
1010 }
1011
1012 if (hapd->iconf->rts_threshold > -1 &&
1013 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1014 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1015 "kernel driver");
1016 return -1;
1017 }
1018
1019 if (hapd->iconf->fragm_threshold > -1 &&
1020 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1021 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1022 "for kernel driver");
1023 return -1;
1024 }
1025
1026 prev_addr = hapd->own_addr;
1027
1028 for (j = 0; j < iface->num_bss; j++) {
1029 hapd = iface->bss[j];
1030 if (j)
1031 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1032 if (hostapd_setup_bss(hapd, j == 0))
1033 return -1;
1034 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1035 prev_addr = hapd->own_addr;
1036 }
1037
1038 hostapd_tx_queue_params(iface);
1039
1040 ap_list_init(iface);
1041
1042 hostapd_set_acl(hapd);
1043
1044 if (hostapd_driver_commit(hapd) < 0) {
1045 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1046 "configuration", __func__);
1047 return -1;
1048 }
1049
1050 /*
1051 * WPS UPnP module can be initialized only when the "upnp_iface" is up.
1052 * If "interface" and "upnp_iface" are the same (e.g., non-bridge
1053 * mode), the interface is up only after driver_commit, so initialize
1054 * WPS after driver_commit.
1055 */
1056 for (j = 0; j < iface->num_bss; j++) {
1057 if (hostapd_init_wps_complete(iface->bss[j]))
1058 return -1;
1059 }
1060
1061 if (hapd->setup_complete_cb)
1062 hapd->setup_complete_cb(hapd->setup_complete_cb_ctx);
1063
1064 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1065 iface->bss[0]->conf->iface);
1066
1067 return 0;
1068 }
1069
1070
1071 /**
1072 * hostapd_setup_interface - Setup of an interface
1073 * @iface: Pointer to interface data.
1074 * Returns: 0 on success, -1 on failure
1075 *
1076 * Initializes the driver interface, validates the configuration,
1077 * and sets driver parameters based on the configuration.
1078 * Flushes old stations, sets the channel, encryption,
1079 * beacons, and WDS links based on the configuration.
1080 */
1081 int hostapd_setup_interface(struct hostapd_iface *iface)
1082 {
1083 int ret;
1084
1085 ret = setup_interface(iface);
1086 if (ret) {
1087 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1088 iface->bss[0]->conf->iface);
1089 return -1;
1090 }
1091
1092 return 0;
1093 }
1094
1095
1096 /**
1097 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1098 * @hapd_iface: Pointer to interface data
1099 * @conf: Pointer to per-interface configuration
1100 * @bss: Pointer to per-BSS configuration for this BSS
1101 * Returns: Pointer to allocated BSS data
1102 *
1103 * This function is used to allocate per-BSS data structure. This data will be
1104 * freed after hostapd_cleanup() is called for it during interface
1105 * deinitialization.
1106 */
1107 struct hostapd_data *
1108 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1109 struct hostapd_config *conf,
1110 struct hostapd_bss_config *bss)
1111 {
1112 struct hostapd_data *hapd;
1113
1114 hapd = os_zalloc(sizeof(*hapd));
1115 if (hapd == NULL)
1116 return NULL;
1117
1118 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1119 hapd->iconf = conf;
1120 hapd->conf = bss;
1121 hapd->iface = hapd_iface;
1122 hapd->driver = hapd->iconf->driver;
1123 hapd->ctrl_sock = -1;
1124
1125 return hapd;
1126 }
1127
1128
1129 void hostapd_interface_deinit(struct hostapd_iface *iface)
1130 {
1131 size_t j;
1132
1133 if (iface == NULL)
1134 return;
1135
1136 hostapd_cleanup_iface_pre(iface);
1137 for (j = 0; j < iface->num_bss; j++) {
1138 struct hostapd_data *hapd = iface->bss[j];
1139 hostapd_free_stas(hapd);
1140 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1141 hostapd_clear_wep(hapd);
1142 hostapd_cleanup(hapd);
1143 }
1144 }
1145
1146
1147 void hostapd_interface_free(struct hostapd_iface *iface)
1148 {
1149 size_t j;
1150 for (j = 0; j < iface->num_bss; j++)
1151 os_free(iface->bss[j]);
1152 hostapd_cleanup_iface(iface);
1153 }
1154
1155
1156 void hostapd_interface_deinit_free(struct hostapd_iface *iface)
1157 {
1158 const struct wpa_driver_ops *driver;
1159 void *drv_priv;
1160 if (iface == NULL)
1161 return;
1162 driver = iface->bss[0]->driver;
1163 drv_priv = iface->bss[0]->drv_priv;
1164 hostapd_interface_deinit(iface);
1165 if (driver && driver->hapd_deinit && drv_priv)
1166 driver->hapd_deinit(drv_priv);
1167 hostapd_interface_free(iface);
1168 }
1169
1170
1171 int hostapd_enable_iface(struct hostapd_iface *hapd_iface)
1172 {
1173 if (hapd_iface->bss[0]->drv_priv != NULL) {
1174 wpa_printf(MSG_ERROR, "Interface %s already enabled",
1175 hapd_iface->conf->bss[0]->iface);
1176 return -1;
1177 }
1178
1179 wpa_printf(MSG_DEBUG, "Enable interface %s",
1180 hapd_iface->conf->bss[0]->iface);
1181
1182 if (hapd_iface->interfaces == NULL ||
1183 hapd_iface->interfaces->driver_init == NULL ||
1184 hapd_iface->interfaces->driver_init(hapd_iface) ||
1185 hostapd_setup_interface(hapd_iface)) {
1186 hostapd_interface_deinit_free(hapd_iface);
1187 return -1;
1188 }
1189 return 0;
1190 }
1191
1192
1193 int hostapd_reload_iface(struct hostapd_iface *hapd_iface)
1194 {
1195 size_t j;
1196
1197 wpa_printf(MSG_DEBUG, "Reload interface %s",
1198 hapd_iface->conf->bss[0]->iface);
1199 for (j = 0; j < hapd_iface->num_bss; j++)
1200 hostapd_set_security_params(hapd_iface->conf->bss[j]);
1201 if (hostapd_config_check(hapd_iface->conf) < 0) {
1202 wpa_printf(MSG_ERROR, "Updated configuration is invalid");
1203 return -1;
1204 }
1205 hostapd_clear_old(hapd_iface);
1206 for (j = 0; j < hapd_iface->num_bss; j++)
1207 hostapd_reload_bss(hapd_iface->bss[j]);
1208
1209 return 0;
1210 }
1211
1212
1213 int hostapd_disable_iface(struct hostapd_iface *hapd_iface)
1214 {
1215 size_t j;
1216 const struct wpa_driver_ops *driver;
1217 void *drv_priv;
1218
1219 if (hapd_iface == NULL)
1220 return -1;
1221 driver = hapd_iface->bss[0]->driver;
1222 drv_priv = hapd_iface->bss[0]->drv_priv;
1223
1224 /* whatever hostapd_interface_deinit does */
1225 for (j = 0; j < hapd_iface->num_bss; j++) {
1226 struct hostapd_data *hapd = hapd_iface->bss[j];
1227 hostapd_free_stas(hapd);
1228 hostapd_flush_old_stations(hapd, WLAN_REASON_DEAUTH_LEAVING);
1229 hostapd_clear_wep(hapd);
1230 hostapd_free_hapd_data(hapd);
1231 }
1232
1233 if (driver && driver->hapd_deinit && drv_priv) {
1234 driver->hapd_deinit(drv_priv);
1235 hapd_iface->bss[0]->drv_priv = NULL;
1236 }
1237
1238 /* From hostapd_cleanup_iface: These were initialized in
1239 * hostapd_setup_interface and hostapd_setup_interface_complete
1240 */
1241 hostapd_cleanup_iface_partial(hapd_iface);
1242
1243 wpa_printf(MSG_DEBUG, "Interface %s disabled",
1244 hapd_iface->bss[0]->conf->iface);
1245 return 0;
1246 }
1247
1248
1249 static struct hostapd_iface *
1250 hostapd_iface_alloc(struct hapd_interfaces *interfaces)
1251 {
1252 struct hostapd_iface **iface, *hapd_iface;
1253
1254 iface = os_realloc_array(interfaces->iface, interfaces->count + 1,
1255 sizeof(struct hostapd_iface *));
1256 if (iface == NULL)
1257 return NULL;
1258 interfaces->iface = iface;
1259 hapd_iface = interfaces->iface[interfaces->count] =
1260 os_zalloc(sizeof(*hapd_iface));
1261 if (hapd_iface == NULL) {
1262 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1263 "the interface", __func__);
1264 return NULL;
1265 }
1266 interfaces->count++;
1267 hapd_iface->interfaces = interfaces;
1268
1269 return hapd_iface;
1270 }
1271
1272
1273 static struct hostapd_config *
1274 hostapd_config_alloc(struct hapd_interfaces *interfaces, const char *ifname,
1275 const char *ctrl_iface)
1276 {
1277 struct hostapd_bss_config *bss;
1278 struct hostapd_config *conf;
1279
1280 /* Allocates memory for bss and conf */
1281 conf = hostapd_config_defaults();
1282 if (conf == NULL) {
1283 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory for "
1284 "configuration", __func__);
1285 return NULL;
1286 }
1287
1288 conf->driver = wpa_drivers[0];
1289 if (conf->driver == NULL) {
1290 wpa_printf(MSG_ERROR, "No driver wrappers registered!");
1291 hostapd_config_free(conf);
1292 return NULL;
1293 }
1294
1295 bss = conf->last_bss = conf->bss[0];
1296
1297 os_strlcpy(bss->iface, ifname, sizeof(bss->iface));
1298 bss->ctrl_interface = os_strdup(ctrl_iface);
1299 if (bss->ctrl_interface == NULL) {
1300 hostapd_config_free(conf);
1301 return NULL;
1302 }
1303
1304 /* Reading configuration file skipped, will be done in SET!
1305 * From reading the configuration till the end has to be done in
1306 * SET
1307 */
1308 return conf;
1309 }
1310
1311
1312 static struct hostapd_iface * hostapd_data_alloc(
1313 struct hapd_interfaces *interfaces, struct hostapd_config *conf)
1314 {
1315 size_t i;
1316 struct hostapd_iface *hapd_iface =
1317 interfaces->iface[interfaces->count - 1];
1318 struct hostapd_data *hapd;
1319
1320 hapd_iface->conf = conf;
1321 hapd_iface->num_bss = conf->num_bss;
1322
1323 hapd_iface->bss = os_zalloc(conf->num_bss *
1324 sizeof(struct hostapd_data *));
1325 if (hapd_iface->bss == NULL)
1326 return NULL;
1327
1328 for (i = 0; i < conf->num_bss; i++) {
1329 hapd = hapd_iface->bss[i] =
1330 hostapd_alloc_bss_data(hapd_iface, conf, conf->bss[i]);
1331 if (hapd == NULL)
1332 return NULL;
1333 hapd->msg_ctx = hapd;
1334 }
1335
1336 hapd_iface->interfaces = interfaces;
1337
1338 return hapd_iface;
1339 }
1340
1341
1342 int hostapd_add_iface(struct hapd_interfaces *interfaces, char *buf)
1343 {
1344 struct hostapd_config *conf = NULL;
1345 struct hostapd_iface *hapd_iface = NULL;
1346 char *ptr;
1347 size_t i;
1348 const char *conf_file = NULL;
1349
1350 ptr = os_strchr(buf, ' ');
1351 if (ptr == NULL)
1352 return -1;
1353 *ptr++ = '\0';
1354
1355 if (os_strncmp(ptr, "config=", 7) == 0)
1356 conf_file = ptr + 7;
1357
1358 for (i = 0; i < interfaces->count; i++) {
1359 if (!os_strcmp(interfaces->iface[i]->conf->bss[0]->iface,
1360 buf)) {
1361 wpa_printf(MSG_INFO, "Cannot add interface - it "
1362 "already exists");
1363 return -1;
1364 }
1365 }
1366
1367 hapd_iface = hostapd_iface_alloc(interfaces);
1368 if (hapd_iface == NULL) {
1369 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1370 "for interface", __func__);
1371 goto fail;
1372 }
1373
1374 if (conf_file && interfaces->config_read_cb) {
1375 conf = interfaces->config_read_cb(conf_file);
1376 if (conf && conf->bss)
1377 os_strlcpy(conf->bss[0]->iface, buf,
1378 sizeof(conf->bss[0]->iface));
1379 } else
1380 conf = hostapd_config_alloc(interfaces, buf, ptr);
1381 if (conf == NULL || conf->bss == NULL) {
1382 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1383 "for configuration", __func__);
1384 goto fail;
1385 }
1386
1387 hapd_iface = hostapd_data_alloc(interfaces, conf);
1388 if (hapd_iface == NULL) {
1389 wpa_printf(MSG_ERROR, "%s: Failed to allocate memory "
1390 "for hostapd", __func__);
1391 goto fail;
1392 }
1393
1394 if (hapd_iface->interfaces &&
1395 hapd_iface->interfaces->ctrl_iface_init &&
1396 hapd_iface->interfaces->ctrl_iface_init(hapd_iface->bss[0])) {
1397 wpa_printf(MSG_ERROR, "%s: Failed to setup control "
1398 "interface", __func__);
1399 goto fail;
1400 }
1401 wpa_printf(MSG_INFO, "Add interface '%s'", conf->bss[0]->iface);
1402
1403 return 0;
1404
1405 fail:
1406 if (conf)
1407 hostapd_config_free(conf);
1408 if (hapd_iface) {
1409 if (hapd_iface->bss) {
1410 for (i = 0; i < hapd_iface->num_bss; i++)
1411 os_free(hapd_iface->bss[i]);
1412 os_free(hapd_iface->bss);
1413 }
1414 os_free(hapd_iface);
1415 }
1416 return -1;
1417 }
1418
1419
1420 int hostapd_remove_iface(struct hapd_interfaces *interfaces, char *buf)
1421 {
1422 struct hostapd_iface *hapd_iface;
1423 size_t i, k = 0;
1424
1425 for (i = 0; i < interfaces->count; i++) {
1426 hapd_iface = interfaces->iface[i];
1427 if (hapd_iface == NULL)
1428 return -1;
1429 if (!os_strcmp(hapd_iface->conf->bss[0]->iface, buf)) {
1430 wpa_printf(MSG_INFO, "Remove interface '%s'", buf);
1431 hostapd_interface_deinit_free(hapd_iface);
1432 k = i;
1433 while (k < (interfaces->count - 1)) {
1434 interfaces->iface[k] =
1435 interfaces->iface[k + 1];
1436 k++;
1437 }
1438 interfaces->count--;
1439 return 0;
1440 }
1441 }
1442 return -1;
1443 }
1444
1445
1446 /**
1447 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1448 * @hapd: Pointer to BSS data
1449 * @sta: Pointer to the associated STA data
1450 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1451 *
1452 * This function will be called whenever a station associates with the AP. It
1453 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1454 * from drv_callbacks.c based on driver events for drivers that take care of
1455 * management frames (IEEE 802.11 authentication and association) internally.
1456 */
1457 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1458 int reassoc)
1459 {
1460 if (hapd->tkip_countermeasures) {
1461 hostapd_drv_sta_deauth(hapd, sta->addr,
1462 WLAN_REASON_MICHAEL_MIC_FAILURE);
1463 return;
1464 }
1465
1466 hostapd_prune_associations(hapd, sta->addr);
1467
1468 /* IEEE 802.11F (IAPP) */
1469 if (hapd->conf->ieee802_11f)
1470 iapp_new_station(hapd->iapp, sta);
1471
1472 #ifdef CONFIG_P2P
1473 if (sta->p2p_ie == NULL && !sta->no_p2p_set) {
1474 sta->no_p2p_set = 1;
1475 hapd->num_sta_no_p2p++;
1476 if (hapd->num_sta_no_p2p == 1)
1477 hostapd_p2p_non_p2p_sta_connected(hapd);
1478 }
1479 #endif /* CONFIG_P2P */
1480
1481 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1482 * IEEE 802.1X/WPA code will start accounting after the station has
1483 * been authorized. */
1484 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
1485 os_get_time(&sta->connected_time);
1486 accounting_sta_start(hapd, sta);
1487 }
1488
1489 /* Start IEEE 802.1X authentication process for new stations */
1490 ieee802_1x_new_station(hapd, sta);
1491 if (reassoc) {
1492 if (sta->auth_alg != WLAN_AUTH_FT &&
1493 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1494 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1495 } else
1496 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
1497
1498 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1499 "for " MACSTR " (%d seconds - ap_max_inactivity)",
1500 __func__, MAC2STR(sta->addr),
1501 hapd->conf->ap_max_inactivity);
1502 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1503 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
1504 ap_handle_timer, hapd, sta);
1505 }