LCOV - code coverage report
Current view: top level - src/ap - hostapd.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 940 1207 77.9 %
Date: 2014-05-28 Functions: 55 58 94.8 %

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

Generated by: LCOV version 1.10