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