LCOV - code coverage report
Current view: top level - src/ap - ap_config.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 515 540 95.4 %
Date: 2015-02-03 Functions: 23 23 100.0 %

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

Generated by: LCOV version 1.10