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