LCOV - code coverage report
Current view: top level - ap - ap_config.c (source / functions) Hit Total Coverage
Test: hostapd (AS) hwsim test run 1401872338 Lines: 257 525 49.0 %
Date: 2014-06-04 Functions: 14 23 60.9 %

          Line data    Source code
       1             : /*
       2             :  * hostapd / Configuration helper functions
       3             :  * Copyright (c) 2003-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 "crypto/sha1.h"
      13             : #include "radius/radius_client.h"
      14             : #include "common/ieee802_11_defs.h"
      15             : #include "common/eapol_common.h"
      16             : #include "eap_common/eap_wsc_common.h"
      17             : #include "eap_server/eap.h"
      18             : #include "wpa_auth.h"
      19             : #include "sta_info.h"
      20             : #include "ap_config.h"
      21             : 
      22             : 
      23           2 : static void hostapd_config_free_vlan(struct hostapd_bss_config *bss)
      24             : {
      25             :         struct hostapd_vlan *vlan, *prev;
      26             : 
      27           2 :         vlan = bss->vlan;
      28           2 :         prev = NULL;
      29           4 :         while (vlan) {
      30           0 :                 prev = vlan;
      31           0 :                 vlan = vlan->next;
      32           0 :                 os_free(prev);
      33             :         }
      34             : 
      35           2 :         bss->vlan = NULL;
      36           2 : }
      37             : 
      38             : 
      39           2 : void hostapd_config_defaults_bss(struct hostapd_bss_config *bss)
      40             : {
      41           2 :         bss->logger_syslog_level = HOSTAPD_LEVEL_INFO;
      42           2 :         bss->logger_stdout_level = HOSTAPD_LEVEL_INFO;
      43           2 :         bss->logger_syslog = (unsigned int) -1;
      44           2 :         bss->logger_stdout = (unsigned int) -1;
      45             : 
      46           2 :         bss->auth_algs = WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED;
      47             : 
      48           2 :         bss->wep_rekeying_period = 300;
      49             :         /* use key0 in individual key and key1 in broadcast key */
      50           2 :         bss->broadcast_key_idx_min = 1;
      51           2 :         bss->broadcast_key_idx_max = 2;
      52           2 :         bss->eap_reauth_period = 3600;
      53             : 
      54           2 :         bss->wpa_group_rekey = 600;
      55           2 :         bss->wpa_gmk_rekey = 86400;
      56           2 :         bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
      57           2 :         bss->wpa_pairwise = WPA_CIPHER_TKIP;
      58           2 :         bss->wpa_group = WPA_CIPHER_TKIP;
      59           2 :         bss->rsn_pairwise = 0;
      60             : 
      61           2 :         bss->max_num_sta = MAX_STA_COUNT;
      62             : 
      63           2 :         bss->dtim_period = 2;
      64             : 
      65           2 :         bss->radius_server_auth_port = 1812;
      66           2 :         bss->ap_max_inactivity = AP_MAX_INACTIVITY;
      67           2 :         bss->eapol_version = EAPOL_VERSION;
      68             : 
      69           2 :         bss->max_listen_interval = 65535;
      70             : 
      71           2 :         bss->pwd_group = 19; /* ECC: GF(p=256) */
      72             : 
      73             : #ifdef CONFIG_IEEE80211W
      74           2 :         bss->assoc_sa_query_max_timeout = 1000;
      75           2 :         bss->assoc_sa_query_retry_timeout = 201;
      76           2 :         bss->group_mgmt_cipher = WPA_CIPHER_AES_128_CMAC;
      77             : #endif /* CONFIG_IEEE80211W */
      78             : #ifdef EAP_SERVER_FAST
      79             :          /* both anonymous and authenticated provisioning */
      80           2 :         bss->eap_fast_prov = 3;
      81           2 :         bss->pac_key_lifetime = 7 * 24 * 60 * 60;
      82           2 :         bss->pac_key_refresh_time = 1 * 24 * 60 * 60;
      83             : #endif /* EAP_SERVER_FAST */
      84             : 
      85             :         /* Set to -1 as defaults depends on HT in setup */
      86           2 :         bss->wmm_enabled = -1;
      87             : 
      88             : #ifdef CONFIG_IEEE80211R
      89           2 :         bss->ft_over_ds = 1;
      90             : #endif /* CONFIG_IEEE80211R */
      91             : 
      92           2 :         bss->radius_das_time_window = 300;
      93             : 
      94           2 :         bss->sae_anti_clogging_threshold = 5;
      95           2 : }
      96             : 
      97             : 
      98           2 : struct hostapd_config * hostapd_config_defaults(void)
      99             : {
     100             : #define ecw2cw(ecw) ((1 << (ecw)) - 1)
     101             : 
     102             :         struct hostapd_config *conf;
     103             :         struct hostapd_bss_config *bss;
     104           2 :         const int aCWmin = 4, aCWmax = 10;
     105           2 :         const struct hostapd_wmm_ac_params ac_bk =
     106             :                 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
     107           2 :         const struct hostapd_wmm_ac_params ac_be =
     108             :                 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
     109           2 :         const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
     110           2 :                 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
     111           4 :         const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
     112           4 :                 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
     113           6 :         const struct hostapd_tx_queue_params txq_bk =
     114           4 :                 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
     115           6 :         const struct hostapd_tx_queue_params txq_be =
     116           4 :                 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0};
     117           6 :         const struct hostapd_tx_queue_params txq_vi =
     118           4 :                 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30};
     119           6 :         const struct hostapd_tx_queue_params txq_vo =
     120           2 :                 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
     121           2 :                   (ecw2cw(aCWmin) + 1) / 2 - 1, 15};
     122             : 
     123             : #undef ecw2cw
     124             : 
     125           2 :         conf = os_zalloc(sizeof(*conf));
     126           2 :         bss = os_zalloc(sizeof(*bss));
     127           2 :         if (conf == NULL || bss == NULL) {
     128           0 :                 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
     129             :                            "configuration data.");
     130           0 :                 os_free(conf);
     131           0 :                 os_free(bss);
     132           0 :                 return NULL;
     133             :         }
     134           2 :         conf->bss = os_calloc(1, sizeof(struct hostapd_bss_config *));
     135           2 :         if (conf->bss == NULL) {
     136           0 :                 os_free(conf);
     137           0 :                 os_free(bss);
     138           0 :                 return NULL;
     139             :         }
     140           2 :         conf->bss[0] = bss;
     141             : 
     142           2 :         bss->radius = os_zalloc(sizeof(*bss->radius));
     143           2 :         if (bss->radius == NULL) {
     144           0 :                 os_free(conf->bss);
     145           0 :                 os_free(conf);
     146           0 :                 os_free(bss);
     147           0 :                 return NULL;
     148             :         }
     149             : 
     150           2 :         hostapd_config_defaults_bss(bss);
     151             : 
     152           2 :         conf->num_bss = 1;
     153             : 
     154           2 :         conf->beacon_int = 100;
     155           2 :         conf->rts_threshold = -1; /* use driver default: 2347 */
     156           2 :         conf->fragm_threshold = -1; /* user driver default: 2346 */
     157           2 :         conf->send_probe_response = 1;
     158             :         /* Set to invalid value means do not add Power Constraint IE */
     159           2 :         conf->local_pwr_constraint = -1;
     160             : 
     161           2 :         conf->wmm_ac_params[0] = ac_be;
     162           2 :         conf->wmm_ac_params[1] = ac_bk;
     163           2 :         conf->wmm_ac_params[2] = ac_vi;
     164           2 :         conf->wmm_ac_params[3] = ac_vo;
     165             : 
     166           2 :         conf->tx_queue[0] = txq_vo;
     167           2 :         conf->tx_queue[1] = txq_vi;
     168           2 :         conf->tx_queue[2] = txq_be;
     169           2 :         conf->tx_queue[3] = txq_bk;
     170             : 
     171           2 :         conf->ht_capab = HT_CAP_INFO_SMPS_DISABLED;
     172             : 
     173           2 :         conf->ap_table_max_size = 255;
     174           2 :         conf->ap_table_expiration_time = 60;
     175             : 
     176             : #ifdef CONFIG_TESTING_OPTIONS
     177           2 :         conf->ignore_probe_probability = 0.0;
     178           2 :         conf->ignore_auth_probability = 0.0;
     179           2 :         conf->ignore_assoc_probability = 0.0;
     180           2 :         conf->ignore_reassoc_probability = 0.0;
     181           2 :         conf->corrupt_gtk_rekey_mic_probability = 0.0;
     182             : #endif /* CONFIG_TESTING_OPTIONS */
     183             : 
     184             : #ifdef CONFIG_ACS
     185           2 :         conf->acs_num_scans = 5;
     186             : #endif /* CONFIG_ACS */
     187             : 
     188           2 :         return conf;
     189             : }
     190             : 
     191             : 
     192           0 : int hostapd_mac_comp(const void *a, const void *b)
     193             : {
     194           0 :         return os_memcmp(a, b, sizeof(macaddr));
     195             : }
     196             : 
     197             : 
     198           4 : int hostapd_mac_comp_empty(const void *a)
     199             : {
     200           4 :         macaddr empty = { 0 };
     201           4 :         return os_memcmp(a, empty, sizeof(macaddr));
     202             : }
     203             : 
     204             : 
     205           0 : static int hostapd_config_read_wpa_psk(const char *fname,
     206             :                                        struct hostapd_ssid *ssid)
     207             : {
     208             :         FILE *f;
     209             :         char buf[128], *pos;
     210           0 :         int line = 0, ret = 0, len, ok;
     211             :         u8 addr[ETH_ALEN];
     212             :         struct hostapd_wpa_psk *psk;
     213             : 
     214           0 :         if (!fname)
     215           0 :                 return 0;
     216             : 
     217           0 :         f = fopen(fname, "r");
     218           0 :         if (!f) {
     219           0 :                 wpa_printf(MSG_ERROR, "WPA PSK file '%s' not found.", fname);
     220           0 :                 return -1;
     221             :         }
     222             : 
     223           0 :         while (fgets(buf, sizeof(buf), f)) {
     224           0 :                 line++;
     225             : 
     226           0 :                 if (buf[0] == '#')
     227           0 :                         continue;
     228           0 :                 pos = buf;
     229           0 :                 while (*pos != '\0') {
     230           0 :                         if (*pos == '\n') {
     231           0 :                                 *pos = '\0';
     232           0 :                                 break;
     233             :                         }
     234           0 :                         pos++;
     235             :                 }
     236           0 :                 if (buf[0] == '\0')
     237           0 :                         continue;
     238             : 
     239           0 :                 if (hwaddr_aton(buf, addr)) {
     240           0 :                         wpa_printf(MSG_ERROR, "Invalid MAC address '%s' on "
     241             :                                    "line %d in '%s'", buf, line, fname);
     242           0 :                         ret = -1;
     243           0 :                         break;
     244             :                 }
     245             : 
     246           0 :                 psk = os_zalloc(sizeof(*psk));
     247           0 :                 if (psk == NULL) {
     248           0 :                         wpa_printf(MSG_ERROR, "WPA PSK allocation failed");
     249           0 :                         ret = -1;
     250           0 :                         break;
     251             :                 }
     252           0 :                 if (is_zero_ether_addr(addr))
     253           0 :                         psk->group = 1;
     254             :                 else
     255           0 :                         os_memcpy(psk->addr, addr, ETH_ALEN);
     256             : 
     257           0 :                 pos = buf + 17;
     258           0 :                 if (*pos == '\0') {
     259           0 :                         wpa_printf(MSG_ERROR, "No PSK on line %d in '%s'",
     260             :                                    line, fname);
     261           0 :                         os_free(psk);
     262           0 :                         ret = -1;
     263           0 :                         break;
     264             :                 }
     265           0 :                 pos++;
     266             : 
     267           0 :                 ok = 0;
     268           0 :                 len = os_strlen(pos);
     269           0 :                 if (len == 64 && hexstr2bin(pos, psk->psk, PMK_LEN) == 0)
     270           0 :                         ok = 1;
     271           0 :                 else if (len >= 8 && len < 64) {
     272           0 :                         pbkdf2_sha1(pos, ssid->ssid, ssid->ssid_len,
     273           0 :                                     4096, psk->psk, PMK_LEN);
     274           0 :                         ok = 1;
     275             :                 }
     276           0 :                 if (!ok) {
     277           0 :                         wpa_printf(MSG_ERROR, "Invalid PSK '%s' on line %d in "
     278             :                                    "'%s'", pos, line, fname);
     279           0 :                         os_free(psk);
     280           0 :                         ret = -1;
     281           0 :                         break;
     282             :                 }
     283             : 
     284           0 :                 psk->next = ssid->wpa_psk;
     285           0 :                 ssid->wpa_psk = psk;
     286             :         }
     287             : 
     288           0 :         fclose(f);
     289             : 
     290           0 :         return ret;
     291             : }
     292             : 
     293             : 
     294           0 : static int hostapd_derive_psk(struct hostapd_ssid *ssid)
     295             : {
     296           0 :         ssid->wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
     297           0 :         if (ssid->wpa_psk == NULL) {
     298           0 :                 wpa_printf(MSG_ERROR, "Unable to alloc space for PSK");
     299           0 :                 return -1;
     300             :         }
     301           0 :         wpa_hexdump_ascii(MSG_DEBUG, "SSID",
     302           0 :                           (u8 *) ssid->ssid, ssid->ssid_len);
     303           0 :         wpa_hexdump_ascii_key(MSG_DEBUG, "PSK (ASCII passphrase)",
     304           0 :                               (u8 *) ssid->wpa_passphrase,
     305           0 :                               os_strlen(ssid->wpa_passphrase));
     306           0 :         pbkdf2_sha1(ssid->wpa_passphrase,
     307           0 :                     ssid->ssid, ssid->ssid_len,
     308           0 :                     4096, ssid->wpa_psk->psk, PMK_LEN);
     309           0 :         wpa_hexdump_key(MSG_DEBUG, "PSK (from passphrase)",
     310           0 :                         ssid->wpa_psk->psk, PMK_LEN);
     311           0 :         return 0;
     312             : }
     313             : 
     314             : 
     315           2 : int hostapd_setup_wpa_psk(struct hostapd_bss_config *conf)
     316             : {
     317           2 :         struct hostapd_ssid *ssid = &conf->ssid;
     318             : 
     319           2 :         if (ssid->wpa_passphrase != NULL) {
     320           0 :                 if (ssid->wpa_psk != NULL) {
     321           0 :                         wpa_printf(MSG_DEBUG, "Using pre-configured WPA PSK "
     322             :                                    "instead of passphrase");
     323             :                 } else {
     324           0 :                         wpa_printf(MSG_DEBUG, "Deriving WPA PSK based on "
     325             :                                    "passphrase");
     326           0 :                         if (hostapd_derive_psk(ssid) < 0)
     327           0 :                                 return -1;
     328             :                 }
     329           0 :                 ssid->wpa_psk->group = 1;
     330             :         }
     331             : 
     332           2 :         if (ssid->wpa_psk_file) {
     333           0 :                 if (hostapd_config_read_wpa_psk(ssid->wpa_psk_file,
     334             :                                                 &conf->ssid))
     335           0 :                         return -1;
     336             :         }
     337             : 
     338           2 :         return 0;
     339             : }
     340             : 
     341             : 
     342           4 : static void hostapd_config_free_radius(struct hostapd_radius_server *servers,
     343             :                                        int num_servers)
     344             : {
     345             :         int i;
     346             : 
     347           4 :         for (i = 0; i < num_servers; i++) {
     348           0 :                 os_free(servers[i].shared_secret);
     349             :         }
     350           4 :         os_free(servers);
     351           4 : }
     352             : 
     353             : 
     354             : struct hostapd_radius_attr *
     355           0 : hostapd_config_get_radius_attr(struct hostapd_radius_attr *attr, u8 type)
     356             : {
     357           0 :         for (; attr; attr = attr->next) {
     358           0 :                 if (attr->type == type)
     359           0 :                         return attr;
     360             :         }
     361           0 :         return NULL;
     362             : }
     363             : 
     364             : 
     365         106 : static void hostapd_config_free_radius_attr(struct hostapd_radius_attr *attr)
     366             : {
     367             :         struct hostapd_radius_attr *prev;
     368             : 
     369         238 :         while (attr) {
     370          26 :                 prev = attr;
     371          26 :                 attr = attr->next;
     372          26 :                 wpabuf_free(prev->val);
     373          26 :                 os_free(prev);
     374             :         }
     375         106 : }
     376             : 
     377             : 
     378         102 : void hostapd_config_free_eap_user(struct hostapd_eap_user *user)
     379             : {
     380         102 :         hostapd_config_free_radius_attr(user->accept_attr);
     381         102 :         os_free(user->identity);
     382         102 :         os_free(user->password);
     383         102 :         os_free(user);
     384         102 : }
     385             : 
     386             : 
     387           2 : static void hostapd_config_free_wep(struct hostapd_wep_keys *keys)
     388             : {
     389             :         int i;
     390          10 :         for (i = 0; i < NUM_WEP_KEYS; i++) {
     391           8 :                 os_free(keys->key[i]);
     392           8 :                 keys->key[i] = NULL;
     393             :         }
     394           2 : }
     395             : 
     396             : 
     397           2 : void hostapd_config_free_bss(struct hostapd_bss_config *conf)
     398             : {
     399             :         struct hostapd_wpa_psk *psk, *prev;
     400             :         struct hostapd_eap_user *user, *prev_user;
     401             : 
     402           2 :         if (conf == NULL)
     403           2 :                 return;
     404             : 
     405           2 :         psk = conf->ssid.wpa_psk;
     406           4 :         while (psk) {
     407           0 :                 prev = psk;
     408           0 :                 psk = psk->next;
     409           0 :                 os_free(prev);
     410             :         }
     411             : 
     412           2 :         os_free(conf->ssid.wpa_passphrase);
     413           2 :         os_free(conf->ssid.wpa_psk_file);
     414           2 :         hostapd_config_free_wep(&conf->ssid.wep);
     415             : #ifdef CONFIG_FULL_DYNAMIC_VLAN
     416           2 :         os_free(conf->ssid.vlan_tagged_interface);
     417             : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
     418             : 
     419           2 :         user = conf->eap_user;
     420         106 :         while (user) {
     421         102 :                 prev_user = user;
     422         102 :                 user = user->next;
     423         102 :                 hostapd_config_free_eap_user(prev_user);
     424             :         }
     425           2 :         os_free(conf->eap_user_sqlite);
     426             : 
     427           2 :         os_free(conf->eap_req_id_text);
     428           2 :         os_free(conf->accept_mac);
     429           2 :         os_free(conf->deny_mac);
     430           2 :         os_free(conf->nas_identifier);
     431           2 :         if (conf->radius) {
     432           2 :                 hostapd_config_free_radius(conf->radius->auth_servers,
     433           2 :                                            conf->radius->num_auth_servers);
     434           2 :                 hostapd_config_free_radius(conf->radius->acct_servers,
     435           2 :                                            conf->radius->num_acct_servers);
     436             :         }
     437           2 :         hostapd_config_free_radius_attr(conf->radius_auth_req_attr);
     438           2 :         hostapd_config_free_radius_attr(conf->radius_acct_req_attr);
     439           2 :         os_free(conf->rsn_preauth_interfaces);
     440           2 :         os_free(conf->ctrl_interface);
     441           2 :         os_free(conf->ca_cert);
     442           2 :         os_free(conf->server_cert);
     443           2 :         os_free(conf->private_key);
     444           2 :         os_free(conf->private_key_passwd);
     445           2 :         os_free(conf->ocsp_stapling_response);
     446           2 :         os_free(conf->dh_file);
     447           2 :         os_free(conf->pac_opaque_encr_key);
     448           2 :         os_free(conf->eap_fast_a_id);
     449           2 :         os_free(conf->eap_fast_a_id_info);
     450           2 :         os_free(conf->eap_sim_db);
     451           2 :         os_free(conf->radius_server_clients);
     452           2 :         os_free(conf->test_socket);
     453           2 :         os_free(conf->radius);
     454           2 :         os_free(conf->radius_das_shared_secret);
     455           2 :         hostapd_config_free_vlan(conf);
     456           2 :         os_free(conf->time_zone);
     457             : 
     458             : #ifdef CONFIG_IEEE80211R
     459             :         {
     460             :                 struct ft_remote_r0kh *r0kh, *r0kh_prev;
     461             :                 struct ft_remote_r1kh *r1kh, *r1kh_prev;
     462             : 
     463           2 :                 r0kh = conf->r0kh_list;
     464           2 :                 conf->r0kh_list = NULL;
     465           4 :                 while (r0kh) {
     466           0 :                         r0kh_prev = r0kh;
     467           0 :                         r0kh = r0kh->next;
     468           0 :                         os_free(r0kh_prev);
     469             :                 }
     470             : 
     471           2 :                 r1kh = conf->r1kh_list;
     472           2 :                 conf->r1kh_list = NULL;
     473           4 :                 while (r1kh) {
     474           0 :                         r1kh_prev = r1kh;
     475           0 :                         r1kh = r1kh->next;
     476           0 :                         os_free(r1kh_prev);
     477             :                 }
     478             :         }
     479             : #endif /* CONFIG_IEEE80211R */
     480             : 
     481             : #ifdef CONFIG_WPS
     482           2 :         os_free(conf->wps_pin_requests);
     483           2 :         os_free(conf->device_name);
     484           2 :         os_free(conf->manufacturer);
     485           2 :         os_free(conf->model_name);
     486           2 :         os_free(conf->model_number);
     487           2 :         os_free(conf->serial_number);
     488           2 :         os_free(conf->config_methods);
     489           2 :         os_free(conf->ap_pin);
     490           2 :         os_free(conf->extra_cred);
     491           2 :         os_free(conf->ap_settings);
     492           2 :         os_free(conf->upnp_iface);
     493           2 :         os_free(conf->friendly_name);
     494           2 :         os_free(conf->manufacturer_url);
     495           2 :         os_free(conf->model_description);
     496           2 :         os_free(conf->model_url);
     497           2 :         os_free(conf->upc);
     498           2 :         wpabuf_free(conf->wps_nfc_dh_pubkey);
     499           2 :         wpabuf_free(conf->wps_nfc_dh_privkey);
     500           2 :         wpabuf_free(conf->wps_nfc_dev_pw);
     501             : #endif /* CONFIG_WPS */
     502             : 
     503           2 :         os_free(conf->roaming_consortium);
     504           2 :         os_free(conf->venue_name);
     505           2 :         os_free(conf->nai_realm_data);
     506           2 :         os_free(conf->network_auth_type);
     507           2 :         os_free(conf->anqp_3gpp_cell_net);
     508           2 :         os_free(conf->domain_name);
     509             : 
     510             : #ifdef CONFIG_RADIUS_TEST
     511             :         os_free(conf->dump_msk_file);
     512             : #endif /* CONFIG_RADIUS_TEST */
     513             : 
     514             : #ifdef CONFIG_HS20
     515           2 :         os_free(conf->hs20_oper_friendly_name);
     516           2 :         os_free(conf->hs20_wan_metrics);
     517           2 :         os_free(conf->hs20_connection_capability);
     518           2 :         os_free(conf->hs20_operating_class);
     519           2 :         os_free(conf->hs20_icons);
     520           2 :         if (conf->hs20_osu_providers) {
     521             :                 size_t i;
     522           0 :                 for (i = 0; i < conf->hs20_osu_providers_count; i++) {
     523             :                         struct hs20_osu_provider *p;
     524             :                         size_t j;
     525           0 :                         p = &conf->hs20_osu_providers[i];
     526           0 :                         os_free(p->friendly_name);
     527           0 :                         os_free(p->server_uri);
     528           0 :                         os_free(p->method_list);
     529           0 :                         for (j = 0; j < p->icons_count; j++)
     530           0 :                                 os_free(p->icons[j]);
     531           0 :                         os_free(p->icons);
     532           0 :                         os_free(p->osu_nai);
     533           0 :                         os_free(p->service_desc);
     534             :                 }
     535           0 :                 os_free(conf->hs20_osu_providers);
     536             :         }
     537           2 :         os_free(conf->subscr_remediation_url);
     538             : #endif /* CONFIG_HS20 */
     539             : 
     540           2 :         wpabuf_free(conf->vendor_elements);
     541             : 
     542           2 :         os_free(conf->sae_groups);
     543             : 
     544           2 :         os_free(conf->server_id);
     545             : 
     546           2 :         os_free(conf);
     547             : }
     548             : 
     549             : 
     550             : /**
     551             :  * hostapd_config_free - Free hostapd configuration
     552             :  * @conf: Configuration data from hostapd_config_read().
     553             :  */
     554           2 : void hostapd_config_free(struct hostapd_config *conf)
     555             : {
     556             :         size_t i;
     557             : 
     558           2 :         if (conf == NULL)
     559           2 :                 return;
     560             : 
     561           4 :         for (i = 0; i < conf->num_bss; i++)
     562           2 :                 hostapd_config_free_bss(conf->bss[i]);
     563           2 :         os_free(conf->bss);
     564           2 :         os_free(conf->supported_rates);
     565           2 :         os_free(conf->basic_rates);
     566           2 :         os_free(conf->chanlist);
     567             : 
     568           2 :         os_free(conf);
     569             : }
     570             : 
     571             : 
     572             : /**
     573             :  * hostapd_maclist_found - Find a MAC address from a list
     574             :  * @list: MAC address list
     575             :  * @num_entries: Number of addresses in the list
     576             :  * @addr: Address to search for
     577             :  * @vlan_id: Buffer for returning VLAN ID or %NULL if not needed
     578             :  * Returns: 1 if address is in the list or 0 if not.
     579             :  *
     580             :  * Perform a binary search for given MAC address from a pre-sorted list.
     581             :  */
     582           0 : int hostapd_maclist_found(struct mac_acl_entry *list, int num_entries,
     583             :                           const u8 *addr, int *vlan_id)
     584             : {
     585             :         int start, end, middle, res;
     586             : 
     587           0 :         start = 0;
     588           0 :         end = num_entries - 1;
     589             : 
     590           0 :         while (start <= end) {
     591           0 :                 middle = (start + end) / 2;
     592           0 :                 res = os_memcmp(list[middle].addr, addr, ETH_ALEN);
     593           0 :                 if (res == 0) {
     594           0 :                         if (vlan_id)
     595           0 :                                 *vlan_id = list[middle].vlan_id;
     596           0 :                         return 1;
     597             :                 }
     598           0 :                 if (res < 0)
     599           0 :                         start = middle + 1;
     600             :                 else
     601           0 :                         end = middle - 1;
     602             :         }
     603             : 
     604           0 :         return 0;
     605             : }
     606             : 
     607             : 
     608           0 : int hostapd_rate_found(int *list, int rate)
     609             : {
     610             :         int i;
     611             : 
     612           0 :         if (list == NULL)
     613           0 :                 return 0;
     614             : 
     615           0 :         for (i = 0; list[i] >= 0; i++)
     616           0 :                 if (list[i] == rate)
     617           0 :                         return 1;
     618             : 
     619           0 :         return 0;
     620             : }
     621             : 
     622             : 
     623           0 : int hostapd_vlan_id_valid(struct hostapd_vlan *vlan, int vlan_id)
     624             : {
     625           0 :         struct hostapd_vlan *v = vlan;
     626           0 :         while (v) {
     627           0 :                 if (v->vlan_id == vlan_id || v->vlan_id == VLAN_ID_WILDCARD)
     628           0 :                         return 1;
     629           0 :                 v = v->next;
     630             :         }
     631           0 :         return 0;
     632             : }
     633             : 
     634             : 
     635           0 : const char * hostapd_get_vlan_id_ifname(struct hostapd_vlan *vlan, int vlan_id)
     636             : {
     637           0 :         struct hostapd_vlan *v = vlan;
     638           0 :         while (v) {
     639           0 :                 if (v->vlan_id == vlan_id)
     640           0 :                         return v->ifname;
     641           0 :                 v = v->next;
     642             :         }
     643           0 :         return NULL;
     644             : }
     645             : 
     646             : 
     647           0 : const u8 * hostapd_get_psk(const struct hostapd_bss_config *conf,
     648             :                            const u8 *addr, const u8 *p2p_dev_addr,
     649             :                            const u8 *prev_psk)
     650             : {
     651             :         struct hostapd_wpa_psk *psk;
     652           0 :         int next_ok = prev_psk == NULL;
     653             : 
     654           0 :         if (p2p_dev_addr && !is_zero_ether_addr(p2p_dev_addr)) {
     655           0 :                 wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
     656             :                            " p2p_dev_addr=" MACSTR " prev_psk=%p",
     657           0 :                            MAC2STR(addr), MAC2STR(p2p_dev_addr), prev_psk);
     658           0 :                 addr = NULL; /* Use P2P Device Address for matching */
     659             :         } else {
     660           0 :                 wpa_printf(MSG_DEBUG, "Searching a PSK for " MACSTR
     661             :                            " prev_psk=%p",
     662           0 :                            MAC2STR(addr), prev_psk);
     663             :         }
     664             : 
     665           0 :         for (psk = conf->ssid.wpa_psk; psk != NULL; psk = psk->next) {
     666           0 :                 if (next_ok &&
     667           0 :                     (psk->group ||
     668           0 :                      (addr && os_memcmp(psk->addr, addr, ETH_ALEN) == 0) ||
     669           0 :                      (!addr && p2p_dev_addr &&
     670           0 :                       os_memcmp(psk->p2p_dev_addr, p2p_dev_addr, ETH_ALEN) ==
     671             :                       0)))
     672           0 :                         return psk->psk;
     673             : 
     674           0 :                 if (psk->psk == prev_psk)
     675           0 :                         next_ok = 1;
     676             :         }
     677             : 
     678           0 :         return NULL;
     679             : }
     680             : 
     681             : 
     682           2 : static int hostapd_config_check_bss(struct hostapd_bss_config *bss,
     683             :                                     struct hostapd_config *conf,
     684             :                                     int full_config)
     685             : {
     686           2 :         if (full_config && bss->ieee802_1x && !bss->eap_server &&
     687           0 :             !bss->radius->auth_servers) {
     688           0 :                 wpa_printf(MSG_ERROR, "Invalid IEEE 802.1X configuration (no "
     689             :                            "EAP authenticator configured).");
     690           0 :                 return -1;
     691             :         }
     692             : 
     693           2 :         if (bss->wpa) {
     694             :                 int wep, i;
     695             : 
     696           0 :                 wep = bss->default_wep_key_len > 0 ||
     697           0 :                        bss->individual_wep_key_len > 0;
     698           0 :                 for (i = 0; i < NUM_WEP_KEYS; i++) {
     699           0 :                         if (bss->ssid.wep.keys_set) {
     700           0 :                                 wep = 1;
     701           0 :                                 break;
     702             :                         }
     703             :                 }
     704             : 
     705           0 :                 if (wep) {
     706           0 :                         wpa_printf(MSG_ERROR, "WEP configuration in a WPA network is not supported");
     707           0 :                         return -1;
     708             :                 }
     709             :         }
     710             : 
     711           2 :         if (full_config && bss->wpa &&
     712           0 :             bss->wpa_psk_radius != PSK_RADIUS_IGNORED &&
     713           0 :             bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH) {
     714           0 :                 wpa_printf(MSG_ERROR, "WPA-PSK using RADIUS enabled, but no "
     715             :                            "RADIUS checking (macaddr_acl=2) enabled.");
     716           0 :                 return -1;
     717             :         }
     718             : 
     719           2 :         if (full_config && bss->wpa && (bss->wpa_key_mgmt & WPA_KEY_MGMT_PSK) &&
     720           0 :             bss->ssid.wpa_psk == NULL && bss->ssid.wpa_passphrase == NULL &&
     721           0 :             bss->ssid.wpa_psk_file == NULL &&
     722           0 :             (bss->wpa_psk_radius != PSK_RADIUS_REQUIRED ||
     723           0 :              bss->macaddr_acl != USE_EXTERNAL_RADIUS_AUTH)) {
     724           0 :                 wpa_printf(MSG_ERROR, "WPA-PSK enabled, but PSK or passphrase "
     725             :                            "is not configured.");
     726           0 :                 return -1;
     727             :         }
     728             : 
     729           2 :         if (full_config && hostapd_mac_comp_empty(bss->bssid) != 0) {
     730             :                 size_t i;
     731             : 
     732           0 :                 for (i = 0; i < conf->num_bss; i++) {
     733           0 :                         if (conf->bss[i] != bss &&
     734           0 :                             (hostapd_mac_comp(conf->bss[i]->bssid,
     735           0 :                                               bss->bssid) == 0)) {
     736           0 :                                 wpa_printf(MSG_ERROR, "Duplicate BSSID " MACSTR
     737             :                                            " on interface '%s' and '%s'.",
     738           0 :                                            MAC2STR(bss->bssid),
     739           0 :                                            conf->bss[i]->iface, bss->iface);
     740           0 :                                 return -1;
     741             :                         }
     742             :                 }
     743             :         }
     744             : 
     745             : #ifdef CONFIG_IEEE80211R
     746           2 :         if (full_config && wpa_key_mgmt_ft(bss->wpa_key_mgmt) &&
     747           0 :             (bss->nas_identifier == NULL ||
     748           0 :              os_strlen(bss->nas_identifier) < 1 ||
     749           0 :              os_strlen(bss->nas_identifier) > FT_R0KH_ID_MAX_LEN)) {
     750           0 :                 wpa_printf(MSG_ERROR, "FT (IEEE 802.11r) requires "
     751             :                            "nas_identifier to be configured as a 1..48 octet "
     752             :                            "string");
     753           0 :                 return -1;
     754             :         }
     755             : #endif /* CONFIG_IEEE80211R */
     756             : 
     757             : #ifdef CONFIG_IEEE80211N
     758           2 :         if (full_config && conf->ieee80211n &&
     759           0 :             conf->hw_mode == HOSTAPD_MODE_IEEE80211B) {
     760           0 :                 bss->disable_11n = 1;
     761           0 :                 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) in 11b mode is not "
     762             :                            "allowed, disabling HT capabilites");
     763             :         }
     764             : 
     765           2 :         if (full_config && conf->ieee80211n &&
     766           0 :             bss->ssid.security_policy == SECURITY_STATIC_WEP) {
     767           0 :                 bss->disable_11n = 1;
     768           0 :                 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WEP is not "
     769             :                            "allowed, disabling HT capabilities");
     770             :         }
     771             : 
     772           2 :         if (full_config && conf->ieee80211n && bss->wpa &&
     773           0 :             !(bss->wpa_pairwise & WPA_CIPHER_CCMP) &&
     774           0 :             !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
     775             :                                    WPA_CIPHER_CCMP_256 | WPA_CIPHER_GCMP_256)))
     776             :         {
     777           0 :                 bss->disable_11n = 1;
     778           0 :                 wpa_printf(MSG_ERROR, "HT (IEEE 802.11n) with WPA/WPA2 "
     779             :                            "requires CCMP/GCMP to be enabled, disabling HT "
     780             :                            "capabilities");
     781             :         }
     782             : #endif /* CONFIG_IEEE80211N */
     783             : 
     784             : #ifdef CONFIG_WPS
     785           2 :         if (full_config && bss->wps_state && bss->ignore_broadcast_ssid) {
     786           0 :                 wpa_printf(MSG_INFO, "WPS: ignore_broadcast_ssid "
     787             :                            "configuration forced WPS to be disabled");
     788           0 :                 bss->wps_state = 0;
     789             :         }
     790             : 
     791           2 :         if (full_config && bss->wps_state &&
     792           0 :             bss->ssid.wep.keys_set && bss->wpa == 0) {
     793           0 :                 wpa_printf(MSG_INFO, "WPS: WEP configuration forced WPS to be "
     794             :                            "disabled");
     795           0 :                 bss->wps_state = 0;
     796             :         }
     797             : 
     798           2 :         if (full_config && bss->wps_state && bss->wpa &&
     799           0 :             (!(bss->wpa & 2) ||
     800           0 :              !(bss->rsn_pairwise & WPA_CIPHER_CCMP))) {
     801           0 :                 wpa_printf(MSG_INFO, "WPS: WPA/TKIP configuration without "
     802             :                            "WPA2/CCMP forced WPS to be disabled");
     803           0 :                 bss->wps_state = 0;
     804             :         }
     805             : #endif /* CONFIG_WPS */
     806             : 
     807             : #ifdef CONFIG_HS20
     808           2 :         if (full_config && bss->hs20 &&
     809           0 :             (!(bss->wpa & 2) ||
     810           0 :              !(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
     811             :                                     WPA_CIPHER_CCMP_256 |
     812             :                                     WPA_CIPHER_GCMP_256)))) {
     813           0 :                 wpa_printf(MSG_ERROR, "HS 2.0: WPA2-Enterprise/CCMP "
     814             :                            "configuration is required for Hotspot 2.0 "
     815             :                            "functionality");
     816           0 :                 return -1;
     817             :         }
     818             : #endif /* CONFIG_HS20 */
     819             : 
     820           2 :         return 0;
     821             : }
     822             : 
     823             : 
     824           2 : int hostapd_config_check(struct hostapd_config *conf, int full_config)
     825             : {
     826             :         size_t i;
     827             : 
     828           2 :         if (full_config && conf->ieee80211d &&
     829           0 :             (!conf->country[0] || !conf->country[1])) {
     830           0 :                 wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11d without "
     831             :                            "setting the country_code");
     832           0 :                 return -1;
     833             :         }
     834             : 
     835           2 :         if (full_config && conf->ieee80211h && !conf->ieee80211d) {
     836           0 :                 wpa_printf(MSG_ERROR, "Cannot enable IEEE 802.11h without "
     837             :                            "IEEE 802.11d enabled");
     838           0 :                 return -1;
     839             :         }
     840             : 
     841           2 :         if (full_config && conf->local_pwr_constraint != -1 &&
     842           0 :             !conf->ieee80211d) {
     843           0 :                 wpa_printf(MSG_ERROR, "Cannot add Power Constraint element without Country element");
     844           0 :                 return -1;
     845             :         }
     846             : 
     847           2 :         if (full_config && conf->spectrum_mgmt_required &&
     848           0 :             conf->local_pwr_constraint == -1) {
     849           0 :                 wpa_printf(MSG_ERROR, "Cannot set Spectrum Management bit without Country and Power Constraint elements");
     850           0 :                 return -1;
     851             :         }
     852             : 
     853           4 :         for (i = 0; i < conf->num_bss; i++) {
     854           2 :                 if (hostapd_config_check_bss(conf->bss[i], conf, full_config))
     855           0 :                         return -1;
     856             :         }
     857             : 
     858           2 :         return 0;
     859             : }
     860             : 
     861             : 
     862           2 : void hostapd_set_security_params(struct hostapd_bss_config *bss,
     863             :                                  int full_config)
     864             : {
     865           2 :         if (bss->individual_wep_key_len == 0) {
     866             :                 /* individual keys are not use; can use key idx0 for
     867             :                  * broadcast keys */
     868           2 :                 bss->broadcast_key_idx_min = 0;
     869             :         }
     870             : 
     871           2 :         if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
     872           0 :                 bss->rsn_pairwise = bss->wpa_pairwise;
     873           2 :         bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
     874             :                                                     bss->rsn_pairwise);
     875             : 
     876           2 :         if (full_config) {
     877           2 :                 bss->radius->auth_server = bss->radius->auth_servers;
     878           2 :                 bss->radius->acct_server = bss->radius->acct_servers;
     879             :         }
     880             : 
     881           2 :         if (bss->wpa && bss->ieee802_1x) {
     882           0 :                 bss->ssid.security_policy = SECURITY_WPA;
     883           2 :         } else if (bss->wpa) {
     884           0 :                 bss->ssid.security_policy = SECURITY_WPA_PSK;
     885           2 :         } else if (bss->ieee802_1x) {
     886           0 :                 int cipher = WPA_CIPHER_NONE;
     887           0 :                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
     888           0 :                 bss->ssid.wep.default_len = bss->default_wep_key_len;
     889           0 :                 if (bss->default_wep_key_len)
     890           0 :                         cipher = bss->default_wep_key_len >= 13 ?
     891           0 :                                 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
     892           0 :                 bss->wpa_group = cipher;
     893           0 :                 bss->wpa_pairwise = cipher;
     894           0 :                 bss->rsn_pairwise = cipher;
     895           2 :         } else if (bss->ssid.wep.keys_set) {
     896           0 :                 int cipher = WPA_CIPHER_WEP40;
     897           0 :                 if (bss->ssid.wep.len[0] >= 13)
     898           0 :                         cipher = WPA_CIPHER_WEP104;
     899           0 :                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
     900           0 :                 bss->wpa_group = cipher;
     901           0 :                 bss->wpa_pairwise = cipher;
     902           0 :                 bss->rsn_pairwise = cipher;
     903           2 :         } else if (bss->osen) {
     904           0 :                 bss->ssid.security_policy = SECURITY_OSEN;
     905           0 :                 bss->wpa_group = WPA_CIPHER_CCMP;
     906           0 :                 bss->wpa_pairwise = 0;
     907           0 :                 bss->rsn_pairwise = WPA_CIPHER_CCMP;
     908             :         } else {
     909           2 :                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
     910           2 :                 bss->wpa_group = WPA_CIPHER_NONE;
     911           2 :                 bss->wpa_pairwise = WPA_CIPHER_NONE;
     912           2 :                 bss->rsn_pairwise = WPA_CIPHER_NONE;
     913             :         }
     914           2 : }

Generated by: LCOV version 1.10