LCOV - code coverage report
Current view: top level - src/ap - ieee802_11_auth.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 63 295 21.4 %
Date: 2014-03-02 Functions: 9 15 60.0 %
Branches: 19 154 12.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / IEEE 802.11 authentication (ACL)
       3                 :            :  * Copyright (c) 2003-2012, 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                 :            :  * Access control list for IEEE 802.11 authentication can uses statically
       9                 :            :  * configured ACL from configuration files or an external RADIUS server.
      10                 :            :  * Results from external RADIUS queries are cached to allow faster
      11                 :            :  * authentication frame processing.
      12                 :            :  */
      13                 :            : 
      14                 :            : #include "utils/includes.h"
      15                 :            : 
      16                 :            : #include "utils/common.h"
      17                 :            : #include "utils/eloop.h"
      18                 :            : #include "crypto/sha1.h"
      19                 :            : #include "radius/radius.h"
      20                 :            : #include "radius/radius_client.h"
      21                 :            : #include "hostapd.h"
      22                 :            : #include "ap_config.h"
      23                 :            : #include "ap_drv_ops.h"
      24                 :            : #include "ieee802_11.h"
      25                 :            : #include "ieee802_1x.h"
      26                 :            : #include "ieee802_11_auth.h"
      27                 :            : 
      28                 :            : #define RADIUS_ACL_TIMEOUT 30
      29                 :            : 
      30                 :            : 
      31                 :            : struct hostapd_cached_radius_acl {
      32                 :            :         struct os_reltime timestamp;
      33                 :            :         macaddr addr;
      34                 :            :         int accepted; /* HOSTAPD_ACL_* */
      35                 :            :         struct hostapd_cached_radius_acl *next;
      36                 :            :         u32 session_timeout;
      37                 :            :         u32 acct_interim_interval;
      38                 :            :         int vlan_id;
      39                 :            :         struct hostapd_sta_wpa_psk_short *psk;
      40                 :            :         char *identity;
      41                 :            :         char *radius_cui;
      42                 :            : };
      43                 :            : 
      44                 :            : 
      45                 :            : struct hostapd_acl_query_data {
      46                 :            :         struct os_reltime timestamp;
      47                 :            :         u8 radius_id;
      48                 :            :         macaddr addr;
      49                 :            :         u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
      50                 :            :         size_t auth_msg_len;
      51                 :            :         struct hostapd_acl_query_data *next;
      52                 :            : };
      53                 :            : 
      54                 :            : 
      55                 :            : #ifndef CONFIG_NO_RADIUS
      56                 :          0 : static void hostapd_acl_cache_free_entry(struct hostapd_cached_radius_acl *e)
      57                 :            : {
      58                 :          0 :         os_free(e->identity);
      59                 :          0 :         os_free(e->radius_cui);
      60                 :          0 :         hostapd_free_psk_list(e->psk);
      61                 :          0 :         os_free(e);
      62                 :          0 : }
      63                 :            : 
      64                 :            : 
      65                 :        343 : static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
      66                 :            : {
      67                 :            :         struct hostapd_cached_radius_acl *prev;
      68                 :            : 
      69         [ -  + ]:        343 :         while (acl_cache) {
      70                 :          0 :                 prev = acl_cache;
      71                 :          0 :                 acl_cache = acl_cache->next;
      72                 :          0 :                 hostapd_acl_cache_free_entry(prev);
      73                 :            :         }
      74                 :        343 : }
      75                 :            : 
      76                 :            : 
      77                 :          0 : static void copy_psk_list(struct hostapd_sta_wpa_psk_short **psk,
      78                 :            :                           struct hostapd_sta_wpa_psk_short *src)
      79                 :            : {
      80                 :            :         struct hostapd_sta_wpa_psk_short **copy_to;
      81                 :            :         struct hostapd_sta_wpa_psk_short *copy_from;
      82                 :            : 
      83                 :            :         /* Copy PSK linked list */
      84                 :          0 :         copy_to = psk;
      85                 :          0 :         copy_from = src;
      86 [ #  # ][ #  # ]:          0 :         while (copy_from && copy_to) {
      87                 :          0 :                 *copy_to = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
      88         [ #  # ]:          0 :                 if (*copy_to == NULL)
      89                 :          0 :                         break;
      90                 :          0 :                 os_memcpy(*copy_to, copy_from,
      91                 :            :                           sizeof(struct hostapd_sta_wpa_psk_short));
      92                 :          0 :                 copy_from = copy_from->next;
      93                 :          0 :                 copy_to = &((*copy_to)->next);
      94                 :            :         }
      95         [ #  # ]:          0 :         if (copy_to)
      96                 :          0 :                 *copy_to = NULL;
      97                 :          0 : }
      98                 :            : 
      99                 :            : 
     100                 :          0 : static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
     101                 :            :                                  u32 *session_timeout,
     102                 :            :                                  u32 *acct_interim_interval, int *vlan_id,
     103                 :            :                                  struct hostapd_sta_wpa_psk_short **psk,
     104                 :            :                                  char **identity, char **radius_cui)
     105                 :            : {
     106                 :            :         struct hostapd_cached_radius_acl *entry;
     107                 :            :         struct os_reltime now;
     108                 :            : 
     109                 :          0 :         os_get_reltime(&now);
     110                 :            : 
     111         [ #  # ]:          0 :         for (entry = hapd->acl_cache; entry; entry = entry->next) {
     112         [ #  # ]:          0 :                 if (os_memcmp(entry->addr, addr, ETH_ALEN) != 0)
     113                 :          0 :                         continue;
     114                 :            : 
     115         [ #  # ]:          0 :                 if (os_reltime_expired(&now, &entry->timestamp,
     116                 :            :                                        RADIUS_ACL_TIMEOUT))
     117                 :          0 :                         return -1; /* entry has expired */
     118         [ #  # ]:          0 :                 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
     119         [ #  # ]:          0 :                         if (session_timeout)
     120                 :          0 :                                 *session_timeout = entry->session_timeout;
     121         [ #  # ]:          0 :                 if (acct_interim_interval)
     122                 :          0 :                         *acct_interim_interval =
     123                 :          0 :                                 entry->acct_interim_interval;
     124         [ #  # ]:          0 :                 if (vlan_id)
     125                 :          0 :                         *vlan_id = entry->vlan_id;
     126                 :          0 :                 copy_psk_list(psk, entry->psk);
     127         [ #  # ]:          0 :                 if (identity) {
     128         [ #  # ]:          0 :                         if (entry->identity)
     129                 :          0 :                                 *identity = os_strdup(entry->identity);
     130                 :            :                         else
     131                 :          0 :                                 *identity = NULL;
     132                 :            :                 }
     133         [ #  # ]:          0 :                 if (radius_cui) {
     134         [ #  # ]:          0 :                         if (entry->radius_cui)
     135                 :          0 :                                 *radius_cui = os_strdup(entry->radius_cui);
     136                 :            :                         else
     137                 :          0 :                                 *radius_cui = NULL;
     138                 :            :                 }
     139                 :          0 :                 return entry->accepted;
     140                 :            :         }
     141                 :            : 
     142                 :          0 :         return -1;
     143                 :            : }
     144                 :            : #endif /* CONFIG_NO_RADIUS */
     145                 :            : 
     146                 :            : 
     147                 :          0 : static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
     148                 :            : {
     149         [ #  # ]:          0 :         if (query == NULL)
     150                 :          0 :                 return;
     151                 :          0 :         os_free(query->auth_msg);
     152                 :          0 :         os_free(query);
     153                 :            : }
     154                 :            : 
     155                 :            : 
     156                 :            : #ifndef CONFIG_NO_RADIUS
     157                 :          0 : static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
     158                 :            :                                     struct hostapd_acl_query_data *query)
     159                 :            : {
     160                 :            :         struct radius_msg *msg;
     161                 :            :         char buf[128];
     162                 :            : 
     163                 :          0 :         query->radius_id = radius_client_get_id(hapd->radius);
     164                 :          0 :         msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
     165         [ #  # ]:          0 :         if (msg == NULL)
     166                 :          0 :                 return -1;
     167                 :            : 
     168                 :          0 :         radius_msg_make_authenticator(msg, addr, ETH_ALEN);
     169                 :            : 
     170                 :          0 :         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
     171         [ #  # ]:          0 :         if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
     172                 :            :                                  os_strlen(buf))) {
     173                 :          0 :                 wpa_printf(MSG_DEBUG, "Could not add User-Name");
     174                 :          0 :                 goto fail;
     175                 :            :         }
     176                 :            : 
     177         [ #  # ]:          0 :         if (!radius_msg_add_attr_user_password(
     178                 :            :                     msg, (u8 *) buf, os_strlen(buf),
     179                 :          0 :                     hapd->conf->radius->auth_server->shared_secret,
     180                 :          0 :                     hapd->conf->radius->auth_server->shared_secret_len)) {
     181                 :          0 :                 wpa_printf(MSG_DEBUG, "Could not add User-Password");
     182                 :          0 :                 goto fail;
     183                 :            :         }
     184                 :            : 
     185         [ #  # ]:          0 :         if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr,
     186                 :            :                                    NULL, msg) < 0)
     187                 :          0 :                 goto fail;
     188                 :            : 
     189                 :          0 :         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
     190                 :          0 :                     MAC2STR(addr));
     191         [ #  # ]:          0 :         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
     192                 :            :                                  (u8 *) buf, os_strlen(buf))) {
     193                 :          0 :                 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
     194                 :          0 :                 goto fail;
     195                 :            :         }
     196                 :            : 
     197                 :          0 :         os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
     198         [ #  # ]:          0 :         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
     199                 :            :                                  (u8 *) buf, os_strlen(buf))) {
     200                 :          0 :                 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
     201                 :          0 :                 goto fail;
     202                 :            :         }
     203                 :            : 
     204         [ #  # ]:          0 :         if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr) < 0)
     205                 :          0 :                 goto fail;
     206                 :          0 :         return 0;
     207                 :            : 
     208                 :            :  fail:
     209                 :          0 :         radius_msg_free(msg);
     210                 :          0 :         return -1;
     211                 :            : }
     212                 :            : #endif /* CONFIG_NO_RADIUS */
     213                 :            : 
     214                 :            : 
     215                 :            : /**
     216                 :            :  * hostapd_allowed_address - Check whether a specified STA can be authenticated
     217                 :            :  * @hapd: hostapd BSS data
     218                 :            :  * @addr: MAC address of the STA
     219                 :            :  * @msg: Authentication message
     220                 :            :  * @len: Length of msg in octets
     221                 :            :  * @session_timeout: Buffer for returning session timeout (from RADIUS)
     222                 :            :  * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
     223                 :            :  * @vlan_id: Buffer for returning VLAN ID
     224                 :            :  * @psk: Linked list buffer for returning WPA PSK
     225                 :            :  * @identity: Buffer for returning identity (from RADIUS)
     226                 :            :  * @radius_cui: Buffer for returning CUI (from RADIUS)
     227                 :            :  * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
     228                 :            :  *
     229                 :            :  * The caller is responsible for freeing the returned *identity and *radius_cui
     230                 :            :  * values with os_free().
     231                 :            :  */
     232                 :        690 : int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
     233                 :            :                             const u8 *msg, size_t len, u32 *session_timeout,
     234                 :            :                             u32 *acct_interim_interval, int *vlan_id,
     235                 :            :                             struct hostapd_sta_wpa_psk_short **psk,
     236                 :            :                             char **identity, char **radius_cui)
     237                 :            : {
     238         [ +  - ]:        690 :         if (session_timeout)
     239                 :        690 :                 *session_timeout = 0;
     240         [ +  - ]:        690 :         if (acct_interim_interval)
     241                 :        690 :                 *acct_interim_interval = 0;
     242         [ +  - ]:        690 :         if (vlan_id)
     243                 :        690 :                 *vlan_id = 0;
     244         [ +  - ]:        690 :         if (psk)
     245                 :        690 :                 *psk = NULL;
     246         [ +  - ]:        690 :         if (identity)
     247                 :        690 :                 *identity = NULL;
     248         [ +  - ]:        690 :         if (radius_cui)
     249                 :        690 :                 *radius_cui = NULL;
     250                 :            : 
     251         [ +  + ]:        690 :         if (hostapd_maclist_found(hapd->conf->accept_mac,
     252                 :        690 :                                   hapd->conf->num_accept_mac, addr, vlan_id))
     253                 :         45 :                 return HOSTAPD_ACL_ACCEPT;
     254                 :            : 
     255         [ -  + ]:        645 :         if (hostapd_maclist_found(hapd->conf->deny_mac,
     256                 :        645 :                                   hapd->conf->num_deny_mac, addr, vlan_id))
     257                 :          0 :                 return HOSTAPD_ACL_REJECT;
     258                 :            : 
     259         [ +  - ]:        645 :         if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
     260                 :        645 :                 return HOSTAPD_ACL_ACCEPT;
     261         [ #  # ]:          0 :         if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
     262                 :          0 :                 return HOSTAPD_ACL_REJECT;
     263                 :            : 
     264         [ #  # ]:          0 :         if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
     265                 :            : #ifdef CONFIG_NO_RADIUS
     266                 :          0 :                 return HOSTAPD_ACL_REJECT;
     267                 :            : #else /* CONFIG_NO_RADIUS */
     268                 :            :                 struct hostapd_acl_query_data *query;
     269                 :            : 
     270                 :            :                 /* Check whether ACL cache has an entry for this station */
     271                 :          0 :                 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
     272                 :            :                                                 acct_interim_interval,
     273                 :            :                                                 vlan_id, psk,
     274                 :            :                                                 identity, radius_cui);
     275 [ #  # ][ #  # ]:          0 :                 if (res == HOSTAPD_ACL_ACCEPT ||
     276                 :            :                     res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
     277                 :          0 :                         return res;
     278         [ #  # ]:          0 :                 if (res == HOSTAPD_ACL_REJECT)
     279                 :          0 :                         return HOSTAPD_ACL_REJECT;
     280                 :            : 
     281                 :          0 :                 query = hapd->acl_queries;
     282         [ #  # ]:          0 :                 while (query) {
     283         [ #  # ]:          0 :                         if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
     284                 :            :                                 /* pending query in RADIUS retransmit queue;
     285                 :            :                                  * do not generate a new one */
     286         [ #  # ]:          0 :                                 if (identity) {
     287                 :          0 :                                         os_free(*identity);
     288                 :          0 :                                         *identity = NULL;
     289                 :            :                                 }
     290         [ #  # ]:          0 :                                 if (radius_cui) {
     291                 :          0 :                                         os_free(*radius_cui);
     292                 :          0 :                                         *radius_cui = NULL;
     293                 :            :                                 }
     294                 :          0 :                                 return HOSTAPD_ACL_PENDING;
     295                 :            :                         }
     296                 :          0 :                         query = query->next;
     297                 :            :                 }
     298                 :            : 
     299         [ #  # ]:          0 :                 if (!hapd->conf->radius->auth_server)
     300                 :          0 :                         return HOSTAPD_ACL_REJECT;
     301                 :            : 
     302                 :            :                 /* No entry in the cache - query external RADIUS server */
     303                 :          0 :                 query = os_zalloc(sizeof(*query));
     304         [ #  # ]:          0 :                 if (query == NULL) {
     305                 :          0 :                         wpa_printf(MSG_ERROR, "malloc for query data failed");
     306                 :          0 :                         return HOSTAPD_ACL_REJECT;
     307                 :            :                 }
     308                 :          0 :                 os_get_reltime(&query->timestamp);
     309                 :          0 :                 os_memcpy(query->addr, addr, ETH_ALEN);
     310         [ #  # ]:          0 :                 if (hostapd_radius_acl_query(hapd, addr, query)) {
     311                 :          0 :                         wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
     312                 :            :                                    "for ACL query.");
     313                 :          0 :                         hostapd_acl_query_free(query);
     314                 :          0 :                         return HOSTAPD_ACL_REJECT;
     315                 :            :                 }
     316                 :            : 
     317                 :          0 :                 query->auth_msg = os_malloc(len);
     318         [ #  # ]:          0 :                 if (query->auth_msg == NULL) {
     319                 :          0 :                         wpa_printf(MSG_ERROR, "Failed to allocate memory for "
     320                 :            :                                    "auth frame.");
     321                 :          0 :                         hostapd_acl_query_free(query);
     322                 :          0 :                         return HOSTAPD_ACL_REJECT;
     323                 :            :                 }
     324                 :          0 :                 os_memcpy(query->auth_msg, msg, len);
     325                 :          0 :                 query->auth_msg_len = len;
     326                 :          0 :                 query->next = hapd->acl_queries;
     327                 :          0 :                 hapd->acl_queries = query;
     328                 :            : 
     329                 :            :                 /* Queued data will be processed in hostapd_acl_recv_radius()
     330                 :            :                  * when RADIUS server replies to the sent Access-Request. */
     331                 :          0 :                 return HOSTAPD_ACL_PENDING;
     332                 :            : #endif /* CONFIG_NO_RADIUS */
     333                 :            :         }
     334                 :            : 
     335                 :        690 :         return HOSTAPD_ACL_REJECT;
     336                 :            : }
     337                 :            : 
     338                 :            : 
     339                 :            : #ifndef CONFIG_NO_RADIUS
     340                 :        156 : static void hostapd_acl_expire_cache(struct hostapd_data *hapd,
     341                 :            :                                      struct os_reltime *now)
     342                 :            : {
     343                 :            :         struct hostapd_cached_radius_acl *prev, *entry, *tmp;
     344                 :            : 
     345                 :        156 :         prev = NULL;
     346                 :        156 :         entry = hapd->acl_cache;
     347                 :            : 
     348         [ -  + ]:        156 :         while (entry) {
     349         [ #  # ]:          0 :                 if (os_reltime_expired(now, &entry->timestamp,
     350                 :            :                                        RADIUS_ACL_TIMEOUT)) {
     351                 :          0 :                         wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
     352                 :          0 :                                    " has expired.", MAC2STR(entry->addr));
     353         [ #  # ]:          0 :                         if (prev)
     354                 :          0 :                                 prev->next = entry->next;
     355                 :            :                         else
     356                 :          0 :                                 hapd->acl_cache = entry->next;
     357                 :          0 :                         hostapd_drv_set_radius_acl_expire(hapd, entry->addr);
     358                 :          0 :                         tmp = entry;
     359                 :          0 :                         entry = entry->next;
     360                 :          0 :                         hostapd_acl_cache_free_entry(tmp);
     361                 :          0 :                         continue;
     362                 :            :                 }
     363                 :            : 
     364                 :          0 :                 prev = entry;
     365                 :          0 :                 entry = entry->next;
     366                 :            :         }
     367                 :        156 : }
     368                 :            : 
     369                 :            : 
     370                 :        156 : static void hostapd_acl_expire_queries(struct hostapd_data *hapd,
     371                 :            :                                        struct os_reltime *now)
     372                 :            : {
     373                 :            :         struct hostapd_acl_query_data *prev, *entry, *tmp;
     374                 :            : 
     375                 :        156 :         prev = NULL;
     376                 :        156 :         entry = hapd->acl_queries;
     377                 :            : 
     378         [ -  + ]:        156 :         while (entry) {
     379         [ #  # ]:          0 :                 if (os_reltime_expired(now, &entry->timestamp,
     380                 :            :                                        RADIUS_ACL_TIMEOUT)) {
     381                 :          0 :                         wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
     382                 :          0 :                                    " has expired.", MAC2STR(entry->addr));
     383         [ #  # ]:          0 :                         if (prev)
     384                 :          0 :                                 prev->next = entry->next;
     385                 :            :                         else
     386                 :          0 :                                 hapd->acl_queries = entry->next;
     387                 :            : 
     388                 :          0 :                         tmp = entry;
     389                 :          0 :                         entry = entry->next;
     390                 :          0 :                         hostapd_acl_query_free(tmp);
     391                 :          0 :                         continue;
     392                 :            :                 }
     393                 :            : 
     394                 :          0 :                 prev = entry;
     395                 :          0 :                 entry = entry->next;
     396                 :            :         }
     397                 :        156 : }
     398                 :            : 
     399                 :            : 
     400                 :            : /**
     401                 :            :  * hostapd_acl_expire - ACL cache expiration callback
     402                 :            :  * @eloop_ctx: struct hostapd_data *
     403                 :            :  * @timeout_ctx: Not used
     404                 :            :  */
     405                 :        156 : static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
     406                 :            : {
     407                 :        156 :         struct hostapd_data *hapd = eloop_ctx;
     408                 :            :         struct os_reltime now;
     409                 :            : 
     410                 :        156 :         os_get_reltime(&now);
     411                 :        156 :         hostapd_acl_expire_cache(hapd, &now);
     412                 :        156 :         hostapd_acl_expire_queries(hapd, &now);
     413                 :            : 
     414                 :        156 :         eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
     415                 :        156 : }
     416                 :            : 
     417                 :            : 
     418                 :          0 : static void decode_tunnel_passwords(struct hostapd_data *hapd,
     419                 :            :                                     const u8 *shared_secret,
     420                 :            :                                     size_t shared_secret_len,
     421                 :            :                                     struct radius_msg *msg,
     422                 :            :                                     struct radius_msg *req,
     423                 :            :                                     struct hostapd_cached_radius_acl *cache)
     424                 :            : {
     425                 :            :         int passphraselen;
     426                 :            :         char *passphrase, *strpassphrase;
     427                 :            :         size_t i;
     428                 :            :         struct hostapd_sta_wpa_psk_short *psk;
     429                 :            : 
     430                 :            :         /*
     431                 :            :          * Decode all tunnel passwords as PSK and save them into a linked list.
     432                 :            :          */
     433                 :          0 :         for (i = 0; ; i++) {
     434                 :          0 :                 passphrase = radius_msg_get_tunnel_password(
     435                 :            :                         msg, &passphraselen, shared_secret, shared_secret_len,
     436                 :            :                         req, i);
     437                 :            :                 /*
     438                 :            :                  * Passphrase is NULL iff there is no i-th Tunnel-Password
     439                 :            :                  * attribute in msg.
     440                 :            :                  */
     441         [ #  # ]:          0 :                 if (passphrase == NULL)
     442                 :          0 :                         break;
     443                 :            :                 /*
     444                 :            :                  * passphrase does not contain the NULL termination.
     445                 :            :                  * Add it here as pbkdf2_sha1() requires it.
     446                 :            :                  */
     447                 :          0 :                 strpassphrase = os_zalloc(passphraselen + 1);
     448                 :          0 :                 psk = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
     449 [ #  # ][ #  # ]:          0 :                 if (strpassphrase && psk) {
     450                 :          0 :                         os_memcpy(strpassphrase, passphrase, passphraselen);
     451                 :          0 :                         pbkdf2_sha1(strpassphrase,
     452                 :          0 :                                     hapd->conf->ssid.ssid,
     453                 :          0 :                                     hapd->conf->ssid.ssid_len, 4096,
     454                 :          0 :                                     psk->psk, PMK_LEN);
     455                 :          0 :                         psk->next = cache->psk;
     456                 :          0 :                         cache->psk = psk;
     457                 :          0 :                         psk = NULL;
     458                 :            :                 }
     459                 :          0 :                 os_free(strpassphrase);
     460                 :          0 :                 os_free(psk);
     461                 :          0 :                 os_free(passphrase);
     462                 :          0 :         }
     463                 :          0 : }
     464                 :            : 
     465                 :            : 
     466                 :            : /**
     467                 :            :  * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
     468                 :            :  * @msg: RADIUS response message
     469                 :            :  * @req: RADIUS request message
     470                 :            :  * @shared_secret: RADIUS shared secret
     471                 :            :  * @shared_secret_len: Length of shared_secret in octets
     472                 :            :  * @data: Context data (struct hostapd_data *)
     473                 :            :  * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
     474                 :            :  * was processed here) or RADIUS_RX_UNKNOWN if not.
     475                 :            :  */
     476                 :            : static RadiusRxResult
     477                 :       1049 : hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
     478                 :            :                         const u8 *shared_secret, size_t shared_secret_len,
     479                 :            :                         void *data)
     480                 :            : {
     481                 :       1049 :         struct hostapd_data *hapd = data;
     482                 :            :         struct hostapd_acl_query_data *query, *prev;
     483                 :            :         struct hostapd_cached_radius_acl *cache;
     484                 :       1049 :         struct radius_hdr *hdr = radius_msg_get_hdr(msg);
     485                 :            : 
     486                 :       1049 :         query = hapd->acl_queries;
     487                 :       1049 :         prev = NULL;
     488         [ -  + ]:       1049 :         while (query) {
     489         [ #  # ]:          0 :                 if (query->radius_id == hdr->identifier)
     490                 :          0 :                         break;
     491                 :          0 :                 prev = query;
     492                 :          0 :                 query = query->next;
     493                 :            :         }
     494         [ +  - ]:       1049 :         if (query == NULL)
     495                 :       1049 :                 return RADIUS_RX_UNKNOWN;
     496                 :            : 
     497                 :          0 :         wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
     498                 :          0 :                    "message (id=%d)", query->radius_id);
     499                 :            : 
     500         [ #  # ]:          0 :         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
     501                 :          0 :                 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
     502                 :            :                            "correct authenticator - dropped\n");
     503                 :          0 :                 return RADIUS_RX_INVALID_AUTHENTICATOR;
     504                 :            :         }
     505                 :            : 
     506 [ #  # ][ #  # ]:          0 :         if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
     507                 :          0 :             hdr->code != RADIUS_CODE_ACCESS_REJECT) {
     508                 :          0 :                 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
     509                 :          0 :                            "query", hdr->code);
     510                 :          0 :                 return RADIUS_RX_UNKNOWN;
     511                 :            :         }
     512                 :            : 
     513                 :            :         /* Insert Accept/Reject info into ACL cache */
     514                 :          0 :         cache = os_zalloc(sizeof(*cache));
     515         [ #  # ]:          0 :         if (cache == NULL) {
     516                 :          0 :                 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
     517                 :          0 :                 goto done;
     518                 :            :         }
     519                 :          0 :         os_get_reltime(&cache->timestamp);
     520                 :          0 :         os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
     521         [ #  # ]:          0 :         if (hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
     522                 :            :                 u8 *buf;
     523                 :            :                 size_t len;
     524                 :            : 
     525         [ #  # ]:          0 :                 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
     526                 :            :                                               &cache->session_timeout) == 0)
     527                 :          0 :                         cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
     528                 :            :                 else
     529                 :          0 :                         cache->accepted = HOSTAPD_ACL_ACCEPT;
     530                 :            : 
     531         [ #  # ]:          0 :                 if (radius_msg_get_attr_int32(
     532                 :            :                             msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
     533         [ #  # ]:          0 :                             &cache->acct_interim_interval) == 0 &&
     534                 :          0 :                     cache->acct_interim_interval < 60) {
     535                 :          0 :                         wpa_printf(MSG_DEBUG, "Ignored too small "
     536                 :            :                                    "Acct-Interim-Interval %d for STA " MACSTR,
     537                 :            :                                    cache->acct_interim_interval,
     538                 :          0 :                                    MAC2STR(query->addr));
     539                 :          0 :                         cache->acct_interim_interval = 0;
     540                 :            :                 }
     541                 :            : 
     542                 :          0 :                 cache->vlan_id = radius_msg_get_vlanid(msg);
     543                 :            : 
     544                 :          0 :                 decode_tunnel_passwords(hapd, shared_secret, shared_secret_len,
     545                 :            :                                         msg, req, cache);
     546                 :            : 
     547         [ #  # ]:          0 :                 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
     548                 :            :                                             &buf, &len, NULL) == 0) {
     549                 :          0 :                         cache->identity = os_zalloc(len + 1);
     550         [ #  # ]:          0 :                         if (cache->identity)
     551                 :          0 :                                 os_memcpy(cache->identity, buf, len);
     552                 :            :                 }
     553         [ #  # ]:          0 :                 if (radius_msg_get_attr_ptr(
     554                 :            :                             msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
     555                 :            :                             &buf, &len, NULL) == 0) {
     556                 :          0 :                         cache->radius_cui = os_zalloc(len + 1);
     557         [ #  # ]:          0 :                         if (cache->radius_cui)
     558                 :          0 :                                 os_memcpy(cache->radius_cui, buf, len);
     559                 :            :                 }
     560                 :            : 
     561 [ #  # ][ #  # ]:          0 :                 if (hapd->conf->wpa_psk_radius == PSK_RADIUS_REQUIRED &&
     562                 :          0 :                     !cache->psk)
     563                 :          0 :                         cache->accepted = HOSTAPD_ACL_REJECT;
     564                 :            :         } else
     565                 :          0 :                 cache->accepted = HOSTAPD_ACL_REJECT;
     566                 :          0 :         cache->next = hapd->acl_cache;
     567                 :          0 :         hapd->acl_cache = cache;
     568                 :            : 
     569                 :            : #ifdef CONFIG_DRIVER_RADIUS_ACL
     570                 :            :         hostapd_drv_set_radius_acl_auth(hapd, query->addr, cache->accepted,
     571                 :            :                                         cache->session_timeout);
     572                 :            : #else /* CONFIG_DRIVER_RADIUS_ACL */
     573                 :            : #ifdef NEED_AP_MLME
     574                 :            :         /* Re-send original authentication frame for 802.11 processing */
     575                 :          0 :         wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
     576                 :            :                    "successful RADIUS ACL query");
     577                 :          0 :         ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len, NULL);
     578                 :            : #endif /* NEED_AP_MLME */
     579                 :            : #endif /* CONFIG_DRIVER_RADIUS_ACL */
     580                 :            : 
     581                 :            :  done:
     582         [ #  # ]:          0 :         if (prev == NULL)
     583                 :          0 :                 hapd->acl_queries = query->next;
     584                 :            :         else
     585                 :          0 :                 prev->next = query->next;
     586                 :            : 
     587                 :          0 :         hostapd_acl_query_free(query);
     588                 :            : 
     589                 :       1049 :         return RADIUS_RX_PROCESSED;
     590                 :            : }
     591                 :            : #endif /* CONFIG_NO_RADIUS */
     592                 :            : 
     593                 :            : 
     594                 :            : /**
     595                 :            :  * hostapd_acl_init: Initialize IEEE 802.11 ACL
     596                 :            :  * @hapd: hostapd BSS data
     597                 :            :  * Returns: 0 on success, -1 on failure
     598                 :            :  */
     599                 :        429 : int hostapd_acl_init(struct hostapd_data *hapd)
     600                 :            : {
     601                 :            : #ifndef CONFIG_NO_RADIUS
     602         [ -  + ]:        343 :         if (radius_client_register(hapd->radius, RADIUS_AUTH,
     603                 :            :                                    hostapd_acl_recv_radius, hapd))
     604                 :          0 :                 return -1;
     605                 :            : 
     606                 :        343 :         eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
     607                 :            : #endif /* CONFIG_NO_RADIUS */
     608                 :            : 
     609                 :        429 :         return 0;
     610                 :            : }
     611                 :            : 
     612                 :            : 
     613                 :            : /**
     614                 :            :  * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
     615                 :            :  * @hapd: hostapd BSS data
     616                 :            :  */
     617                 :        429 : void hostapd_acl_deinit(struct hostapd_data *hapd)
     618                 :            : {
     619                 :            :         struct hostapd_acl_query_data *query, *prev;
     620                 :            : 
     621                 :            : #ifndef CONFIG_NO_RADIUS
     622                 :        343 :         eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
     623                 :            : 
     624                 :        343 :         hostapd_acl_cache_free(hapd->acl_cache);
     625                 :            : #endif /* CONFIG_NO_RADIUS */
     626                 :            : 
     627                 :        429 :         query = hapd->acl_queries;
     628 [ -  + ][ -  + ]:        429 :         while (query) {
     629                 :          0 :                 prev = query;
     630                 :          0 :                 query = query->next;
     631                 :          0 :                 hostapd_acl_query_free(prev);
     632                 :            :         }
     633                 :        429 : }
     634                 :            : 
     635                 :            : 
     636                 :       1989 : void hostapd_free_psk_list(struct hostapd_sta_wpa_psk_short *psk)
     637                 :            : {
     638         [ -  + ]:       1989 :         while (psk) {
     639                 :          0 :                 struct hostapd_sta_wpa_psk_short *prev = psk;
     640                 :          0 :                 psk = psk->next;
     641                 :          0 :                 os_free(prev);
     642                 :            :         }
     643                 :       1989 : }

Generated by: LCOV version 1.9