LCOV - code coverage report
Current view: top level - src/ap - ap_list.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1388338050 Lines: 104 167 62.3 %
Date: 2013-12-29 Functions: 13 13 100.0 %
Branches: 46 118 39.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / AP table
       3                 :            :  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
       4                 :            :  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
       5                 :            :  * Copyright (c) 2006, Devicescape Software, Inc.
       6                 :            :  *
       7                 :            :  * This software may be distributed under the terms of the BSD license.
       8                 :            :  * See README for more details.
       9                 :            :  */
      10                 :            : 
      11                 :            : #include "utils/includes.h"
      12                 :            : 
      13                 :            : #include "utils/common.h"
      14                 :            : #include "utils/eloop.h"
      15                 :            : #include "common/ieee802_11_defs.h"
      16                 :            : #include "common/ieee802_11_common.h"
      17                 :            : #include "hostapd.h"
      18                 :            : #include "ap_config.h"
      19                 :            : #include "ieee802_11.h"
      20                 :            : #include "sta_info.h"
      21                 :            : #include "beacon.h"
      22                 :            : #include "ap_list.h"
      23                 :            : 
      24                 :            : 
      25                 :            : /* AP list is a double linked list with head->prev pointing to the end of the
      26                 :            :  * list and tail->next = NULL. Entries are moved to the head of the list
      27                 :            :  * whenever a beacon has been received from the AP in question. The tail entry
      28                 :            :  * in this link will thus be the least recently used entry. */
      29                 :            : 
      30                 :            : 
      31                 :       1219 : static int ap_list_beacon_olbc(struct hostapd_iface *iface, struct ap_info *ap)
      32                 :            : {
      33                 :            :         int i;
      34                 :            : 
      35 [ +  - ][ -  + ]:       1219 :         if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G ||
      36                 :       1219 :             iface->conf->channel != ap->channel)
      37                 :          0 :                 return 0;
      38                 :            : 
      39 [ +  - ][ -  + ]:       1219 :         if (ap->erp != -1 && (ap->erp & ERP_INFO_NON_ERP_PRESENT))
      40                 :          0 :                 return 1;
      41                 :            : 
      42         [ +  - ]:       5351 :         for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
      43                 :       5351 :                 int rate = (ap->supported_rates[i] & 0x7f) * 5;
      44 [ +  + ][ +  - ]:       5351 :                 if (rate == 60 || rate == 90 || rate > 110)
                 [ -  + ]
      45                 :       1219 :                         return 0;
      46                 :            :         }
      47                 :            : 
      48                 :       1219 :         return 1;
      49                 :            : }
      50                 :            : 
      51                 :            : 
      52                 :       1219 : static struct ap_info * ap_get_ap(struct hostapd_iface *iface, const u8 *ap)
      53                 :            : {
      54                 :            :         struct ap_info *s;
      55                 :            : 
      56                 :       1219 :         s = iface->ap_hash[STA_HASH(ap)];
      57 [ +  + ][ -  + ]:       1219 :         while (s != NULL && os_memcmp(s->addr, ap, ETH_ALEN) != 0)
      58                 :          0 :                 s = s->hnext;
      59                 :       1219 :         return s;
      60                 :            : }
      61                 :            : 
      62                 :            : 
      63                 :         47 : static void ap_ap_list_add(struct hostapd_iface *iface, struct ap_info *ap)
      64                 :            : {
      65         [ -  + ]:         47 :         if (iface->ap_list) {
      66                 :          0 :                 ap->prev = iface->ap_list->prev;
      67                 :          0 :                 iface->ap_list->prev = ap;
      68                 :            :         } else
      69                 :         47 :                 ap->prev = ap;
      70                 :         47 :         ap->next = iface->ap_list;
      71                 :         47 :         iface->ap_list = ap;
      72                 :         47 : }
      73                 :            : 
      74                 :            : 
      75                 :         47 : static void ap_ap_list_del(struct hostapd_iface *iface, struct ap_info *ap)
      76                 :            : {
      77         [ +  - ]:         47 :         if (iface->ap_list == ap)
      78                 :         47 :                 iface->ap_list = ap->next;
      79                 :            :         else
      80                 :          0 :                 ap->prev->next = ap->next;
      81                 :            : 
      82         [ -  + ]:         47 :         if (ap->next)
      83                 :          0 :                 ap->next->prev = ap->prev;
      84         [ -  + ]:         47 :         else if (iface->ap_list)
      85                 :          0 :                 iface->ap_list->prev = ap->prev;
      86                 :         47 : }
      87                 :            : 
      88                 :            : 
      89                 :         47 : static void ap_ap_hash_add(struct hostapd_iface *iface, struct ap_info *ap)
      90                 :            : {
      91                 :         47 :         ap->hnext = iface->ap_hash[STA_HASH(ap->addr)];
      92                 :         47 :         iface->ap_hash[STA_HASH(ap->addr)] = ap;
      93                 :         47 : }
      94                 :            : 
      95                 :            : 
      96                 :         47 : static void ap_ap_hash_del(struct hostapd_iface *iface, struct ap_info *ap)
      97                 :            : {
      98                 :            :         struct ap_info *s;
      99                 :            : 
     100                 :         47 :         s = iface->ap_hash[STA_HASH(ap->addr)];
     101         [ -  + ]:         47 :         if (s == NULL) return;
     102         [ +  - ]:         47 :         if (os_memcmp(s->addr, ap->addr, ETH_ALEN) == 0) {
     103                 :         47 :                 iface->ap_hash[STA_HASH(ap->addr)] = s->hnext;
     104                 :         47 :                 return;
     105                 :            :         }
     106                 :            : 
     107 [ #  # ][ #  # ]:          0 :         while (s->hnext != NULL &&
     108                 :          0 :                os_memcmp(s->hnext->addr, ap->addr, ETH_ALEN) != 0)
     109                 :          0 :                 s = s->hnext;
     110         [ #  # ]:          0 :         if (s->hnext != NULL)
     111                 :          0 :                 s->hnext = s->hnext->hnext;
     112                 :            :         else
     113                 :         47 :                 printf("AP: could not remove AP " MACSTR " from hash table\n",
     114                 :          0 :                        MAC2STR(ap->addr));
     115                 :            : }
     116                 :            : 
     117                 :            : 
     118                 :         47 : static void ap_free_ap(struct hostapd_iface *iface, struct ap_info *ap)
     119                 :            : {
     120                 :         47 :         ap_ap_hash_del(iface, ap);
     121                 :         47 :         ap_ap_list_del(iface, ap);
     122                 :            : 
     123                 :         47 :         iface->num_ap--;
     124                 :         47 :         os_free(ap);
     125                 :         47 : }
     126                 :            : 
     127                 :            : 
     128                 :        189 : static void hostapd_free_aps(struct hostapd_iface *iface)
     129                 :            : {
     130                 :            :         struct ap_info *ap, *prev;
     131                 :            : 
     132                 :        189 :         ap = iface->ap_list;
     133                 :            : 
     134         [ +  + ]:        236 :         while (ap) {
     135                 :         47 :                 prev = ap;
     136                 :         47 :                 ap = ap->next;
     137                 :         47 :                 ap_free_ap(iface, prev);
     138                 :            :         }
     139                 :            : 
     140                 :        189 :         iface->ap_list = NULL;
     141                 :        189 : }
     142                 :            : 
     143                 :            : 
     144                 :         47 : static struct ap_info * ap_ap_add(struct hostapd_iface *iface, const u8 *addr)
     145                 :            : {
     146                 :            :         struct ap_info *ap;
     147                 :            : 
     148                 :         47 :         ap = os_zalloc(sizeof(struct ap_info));
     149         [ -  + ]:         47 :         if (ap == NULL)
     150                 :          0 :                 return NULL;
     151                 :            : 
     152                 :            :         /* initialize AP info data */
     153                 :         47 :         os_memcpy(ap->addr, addr, ETH_ALEN);
     154                 :         47 :         ap_ap_list_add(iface, ap);
     155                 :         47 :         iface->num_ap++;
     156                 :         47 :         ap_ap_hash_add(iface, ap);
     157                 :            : 
     158 [ -  + ][ #  # ]:         47 :         if (iface->num_ap > iface->conf->ap_table_max_size && ap != ap->prev) {
     159                 :          0 :                 wpa_printf(MSG_DEBUG, "Removing the least recently used AP "
     160                 :          0 :                            MACSTR " from AP table", MAC2STR(ap->prev->addr));
     161                 :          0 :                 ap_free_ap(iface, ap->prev);
     162                 :            :         }
     163                 :            : 
     164                 :         47 :         return ap;
     165                 :            : }
     166                 :            : 
     167                 :            : 
     168                 :       1219 : void ap_list_process_beacon(struct hostapd_iface *iface,
     169                 :            :                             const struct ieee80211_mgmt *mgmt,
     170                 :            :                             struct ieee802_11_elems *elems,
     171                 :            :                             struct hostapd_frame_info *fi)
     172                 :            : {
     173                 :            :         struct ap_info *ap;
     174                 :       1219 :         int new_ap = 0;
     175                 :       1219 :         int set_beacon = 0;
     176                 :            : 
     177         [ -  + ]:       1219 :         if (iface->conf->ap_table_max_size < 1)
     178                 :          0 :                 return;
     179                 :            : 
     180                 :       1219 :         ap = ap_get_ap(iface, mgmt->bssid);
     181         [ +  + ]:       1219 :         if (!ap) {
     182                 :         47 :                 ap = ap_ap_add(iface, mgmt->bssid);
     183         [ -  + ]:         47 :                 if (!ap) {
     184                 :          0 :                         printf("Failed to allocate AP information entry\n");
     185                 :          0 :                         return;
     186                 :            :                 }
     187                 :         47 :                 new_ap = 1;
     188                 :            :         }
     189                 :            : 
     190                 :       1219 :         merge_byte_arrays(ap->supported_rates, WLAN_SUPP_RATES_MAX,
     191                 :       1219 :                           elems->supp_rates, elems->supp_rates_len,
     192                 :       1219 :                           elems->ext_supp_rates, elems->ext_supp_rates_len);
     193                 :            : 
     194 [ +  - ][ +  - ]:       1219 :         if (elems->erp_info && elems->erp_info_len == 1)
     195                 :       1219 :                 ap->erp = elems->erp_info[0];
     196                 :            :         else
     197                 :          0 :                 ap->erp = -1;
     198                 :            : 
     199 [ +  - ][ +  - ]:       1219 :         if (elems->ds_params && elems->ds_params_len == 1)
     200                 :       1219 :                 ap->channel = elems->ds_params[0];
     201 [ #  # ][ #  # ]:          0 :         else if (elems->ht_operation && elems->ht_operation_len >= 1)
     202                 :          0 :                 ap->channel = elems->ht_operation[0];
     203         [ #  # ]:          0 :         else if (fi)
     204                 :          0 :                 ap->channel = fi->channel;
     205                 :            : 
     206         [ +  + ]:       1219 :         if (elems->ht_capabilities)
     207                 :        487 :                 ap->ht_support = 1;
     208                 :            :         else
     209                 :        732 :                 ap->ht_support = 0;
     210                 :            : 
     211                 :       1219 :         os_get_reltime(&ap->last_beacon);
     212                 :            : 
     213 [ +  + ][ -  + ]:       1219 :         if (!new_ap && ap != iface->ap_list) {
     214                 :            :                 /* move AP entry into the beginning of the list so that the
     215                 :            :                  * oldest entry is always in the end of the list */
     216                 :          0 :                 ap_ap_list_del(iface, ap);
     217                 :          0 :                 ap_ap_list_add(iface, ap);
     218                 :            :         }
     219                 :            : 
     220   [ +  -  -  + ]:       2438 :         if (!iface->olbc &&
     221                 :       1219 :             ap_list_beacon_olbc(iface, ap)) {
     222                 :          0 :                 iface->olbc = 1;
     223                 :          0 :                 wpa_printf(MSG_DEBUG, "OLBC AP detected: " MACSTR
     224                 :            :                            " (channel %d) - enable protection",
     225                 :          0 :                            MAC2STR(ap->addr), ap->channel);
     226                 :          0 :                 set_beacon++;
     227                 :            :         }
     228                 :            : 
     229                 :            : #ifdef CONFIG_IEEE80211N
     230 [ +  + ][ +  + ]:       1219 :         if (!iface->olbc_ht && !ap->ht_support &&
                 [ +  - ]
     231         [ -  + ]:         34 :             (ap->channel == 0 ||
     232         [ #  # ]:          0 :              ap->channel == iface->conf->channel ||
     233                 :          0 :              ap->channel == iface->conf->channel +
     234                 :          0 :              iface->conf->secondary_channel * 4)) {
     235                 :         34 :                 iface->olbc_ht = 1;
     236                 :         34 :                 hostapd_ht_operation_update(iface);
     237                 :         34 :                 wpa_printf(MSG_DEBUG, "OLBC HT AP detected: " MACSTR
     238                 :            :                            " (channel %d) - enable protection",
     239                 :        204 :                            MAC2STR(ap->addr), ap->channel);
     240                 :         34 :                 set_beacon++;
     241                 :            :         }
     242                 :            : #endif /* CONFIG_IEEE80211N */
     243                 :            : 
     244         [ +  + ]:       1219 :         if (set_beacon)
     245                 :       1219 :                 ieee802_11_update_beacons(iface);
     246                 :            : }
     247                 :            : 
     248                 :            : 
     249                 :         10 : static void ap_list_timer(void *eloop_ctx, void *timeout_ctx)
     250                 :            : {
     251                 :         10 :         struct hostapd_iface *iface = eloop_ctx;
     252                 :            :         struct os_reltime now;
     253                 :            :         struct ap_info *ap;
     254                 :         10 :         int set_beacon = 0;
     255                 :            : 
     256                 :         10 :         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
     257                 :            : 
     258         [ +  - ]:         10 :         if (!iface->ap_list)
     259                 :         10 :                 return;
     260                 :            : 
     261                 :          0 :         os_get_reltime(&now);
     262                 :            : 
     263         [ #  # ]:          0 :         while (iface->ap_list) {
     264                 :          0 :                 ap = iface->ap_list->prev;
     265         [ #  # ]:          0 :                 if (!os_reltime_expired(&now, &ap->last_beacon,
     266                 :          0 :                                         iface->conf->ap_table_expiration_time))
     267                 :          0 :                         break;
     268                 :            : 
     269                 :          0 :                 ap_free_ap(iface, ap);
     270                 :            :         }
     271                 :            : 
     272 [ #  # ][ #  # ]:          0 :         if (iface->olbc || iface->olbc_ht) {
     273                 :          0 :                 int olbc = 0;
     274                 :          0 :                 int olbc_ht = 0;
     275                 :            : 
     276                 :          0 :                 ap = iface->ap_list;
     277 [ #  # ][ #  # ]:          0 :                 while (ap && (olbc == 0 || olbc_ht == 0)) {
                 [ #  # ]
     278         [ #  # ]:          0 :                         if (ap_list_beacon_olbc(iface, ap))
     279                 :          0 :                                 olbc = 1;
     280         [ #  # ]:          0 :                         if (!ap->ht_support)
     281                 :          0 :                                 olbc_ht = 1;
     282                 :          0 :                         ap = ap->next;
     283                 :            :                 }
     284 [ #  # ][ #  # ]:          0 :                 if (!olbc && iface->olbc) {
     285                 :          0 :                         wpa_printf(MSG_DEBUG, "OLBC not detected anymore");
     286                 :          0 :                         iface->olbc = 0;
     287                 :          0 :                         set_beacon++;
     288                 :            :                 }
     289                 :            : #ifdef CONFIG_IEEE80211N
     290 [ #  # ][ #  # ]:          0 :                 if (!olbc_ht && iface->olbc_ht) {
     291                 :          0 :                         wpa_printf(MSG_DEBUG, "OLBC HT not detected anymore");
     292                 :          0 :                         iface->olbc_ht = 0;
     293                 :          0 :                         hostapd_ht_operation_update(iface);
     294                 :          0 :                         set_beacon++;
     295                 :            :                 }
     296                 :            : #endif /* CONFIG_IEEE80211N */
     297                 :            :         }
     298                 :            : 
     299         [ #  # ]:          0 :         if (set_beacon)
     300                 :          0 :                 ieee802_11_update_beacons(iface);
     301                 :            : }
     302                 :            : 
     303                 :            : 
     304                 :        183 : int ap_list_init(struct hostapd_iface *iface)
     305                 :            : {
     306                 :        183 :         eloop_register_timeout(10, 0, ap_list_timer, iface, NULL);
     307                 :        183 :         return 0;
     308                 :            : }
     309                 :            : 
     310                 :            : 
     311                 :        189 : void ap_list_deinit(struct hostapd_iface *iface)
     312                 :            : {
     313                 :        189 :         eloop_cancel_timeout(ap_list_timer, iface, NULL);
     314                 :        189 :         hostapd_free_aps(iface);
     315                 :        189 : }

Generated by: LCOV version 1.9