LCOV - code coverage report
Current view: top level - src/common - ieee802_11_common.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 350 488 71.7 %
Date: 2015-02-03 Functions: 14 17 82.4 %

          Line data    Source code
       1             : /*
       2             :  * IEEE 802.11 Common routines
       3             :  * Copyright (c) 2002-2013, 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 "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "defs.h"
      13             : #include "ieee802_11_defs.h"
      14             : #include "ieee802_11_common.h"
      15             : 
      16             : 
      17       39383 : static int ieee802_11_parse_vendor_specific(const u8 *pos, size_t elen,
      18             :                                             struct ieee802_11_elems *elems,
      19             :                                             int show_errors)
      20             : {
      21             :         unsigned int oui;
      22             : 
      23             :         /* first 3 bytes in vendor specific information element are the IEEE
      24             :          * OUI of the vendor. The following byte is used a vendor specific
      25             :          * sub-type. */
      26       39383 :         if (elen < 4) {
      27           3 :                 if (show_errors) {
      28           1 :                         wpa_printf(MSG_MSGDUMP, "short vendor specific "
      29             :                                    "information element ignored (len=%lu)",
      30             :                                    (unsigned long) elen);
      31             :                 }
      32           3 :                 return -1;
      33             :         }
      34             : 
      35       39380 :         oui = WPA_GET_BE24(pos);
      36       39380 :         switch (oui) {
      37             :         case OUI_MICROSOFT:
      38             :                 /* Microsoft/Wi-Fi information elements are further typed and
      39             :                  * subtyped */
      40       27734 :                 switch (pos[3]) {
      41             :                 case 1:
      42             :                         /* Microsoft OUI (00:50:F2) with OUI Type 1:
      43             :                          * real WPA information element */
      44         147 :                         elems->wpa_ie = pos;
      45         147 :                         elems->wpa_ie_len = elen;
      46         147 :                         break;
      47             :                 case WMM_OUI_TYPE:
      48             :                         /* WMM information element */
      49       18274 :                         if (elen < 5) {
      50           1 :                                 wpa_printf(MSG_MSGDUMP, "short WMM "
      51             :                                            "information element ignored "
      52             :                                            "(len=%lu)",
      53             :                                            (unsigned long) elen);
      54           1 :                                 return -1;
      55             :                         }
      56       18273 :                         switch (pos[4]) {
      57             :                         case WMM_OUI_SUBTYPE_INFORMATION_ELEMENT:
      58             :                         case WMM_OUI_SUBTYPE_PARAMETER_ELEMENT:
      59             :                                 /*
      60             :                                  * Share same pointer since only one of these
      61             :                                  * is used and they start with same data.
      62             :                                  * Length field can be used to distinguish the
      63             :                                  * IEs.
      64             :                                  */
      65       18252 :                                 elems->wmm = pos;
      66       18252 :                                 elems->wmm_len = elen;
      67       18252 :                                 break;
      68             :                         case WMM_OUI_SUBTYPE_TSPEC_ELEMENT:
      69          20 :                                 elems->wmm_tspec = pos;
      70          20 :                                 elems->wmm_tspec_len = elen;
      71          20 :                                 break;
      72             :                         default:
      73           1 :                                 wpa_printf(MSG_EXCESSIVE, "unknown WMM "
      74             :                                            "information element ignored "
      75             :                                            "(subtype=%d len=%lu)",
      76           1 :                                            pos[4], (unsigned long) elen);
      77           1 :                                 return -1;
      78             :                         }
      79       18272 :                         break;
      80             :                 case 4:
      81             :                         /* Wi-Fi Protected Setup (WPS) IE */
      82        9312 :                         elems->wps_ie = pos;
      83        9312 :                         elems->wps_ie_len = elen;
      84        9312 :                         break;
      85             :                 default:
      86           1 :                         wpa_printf(MSG_EXCESSIVE, "Unknown Microsoft "
      87             :                                    "information element ignored "
      88             :                                    "(type=%d len=%lu)",
      89           1 :                                    pos[3], (unsigned long) elen);
      90           1 :                         return -1;
      91             :                 }
      92       27731 :                 break;
      93             : 
      94             :         case OUI_WFA:
      95       11607 :                 switch (pos[3]) {
      96             :                 case P2P_OUI_TYPE:
      97             :                         /* Wi-Fi Alliance - P2P IE */
      98        9778 :                         elems->p2p = pos;
      99        9778 :                         elems->p2p_len = elen;
     100        9778 :                         break;
     101             :                 case WFD_OUI_TYPE:
     102             :                         /* Wi-Fi Alliance - WFD IE */
     103         248 :                         elems->wfd = pos;
     104         248 :                         elems->wfd_len = elen;
     105         248 :                         break;
     106             :                 case HS20_INDICATION_OUI_TYPE:
     107             :                         /* Hotspot 2.0 */
     108        1577 :                         elems->hs20 = pos;
     109        1577 :                         elems->hs20_len = elen;
     110        1577 :                         break;
     111             :                 case HS20_OSEN_OUI_TYPE:
     112             :                         /* Hotspot 2.0 OSEN */
     113           3 :                         elems->osen = pos;
     114           3 :                         elems->osen_len = elen;
     115           3 :                         break;
     116             :                 default:
     117           1 :                         wpa_printf(MSG_MSGDUMP, "Unknown WFA "
     118             :                                    "information element ignored "
     119             :                                    "(type=%d len=%lu)",
     120           1 :                                    pos[3], (unsigned long) elen);
     121           1 :                         return -1;
     122             :                 }
     123       11606 :                 break;
     124             : 
     125             :         case OUI_BROADCOM:
     126           9 :                 switch (pos[3]) {
     127             :                 case VENDOR_HT_CAPAB_OUI_TYPE:
     128           2 :                         elems->vendor_ht_cap = pos;
     129           2 :                         elems->vendor_ht_cap_len = elen;
     130           2 :                         break;
     131             :                 case VENDOR_VHT_TYPE:
     132          12 :                         if (elen > 4 &&
     133           9 :                             (pos[4] == VENDOR_VHT_SUBTYPE ||
     134           3 :                              pos[4] == VENDOR_VHT_SUBTYPE2)) {
     135           6 :                                 elems->vendor_vht = pos;
     136           6 :                                 elems->vendor_vht_len = elen;
     137             :                         } else
     138           0 :                                 return -1;
     139           6 :                         break;
     140             :                 default:
     141           1 :                         wpa_printf(MSG_EXCESSIVE, "Unknown Broadcom "
     142             :                                    "information element ignored "
     143             :                                    "(type=%d len=%lu)",
     144           1 :                                    pos[3], (unsigned long) elen);
     145           1 :                         return -1;
     146             :                 }
     147           8 :                 break;
     148             : 
     149             :         default:
     150          90 :                 wpa_printf(MSG_EXCESSIVE, "unknown vendor specific "
     151             :                            "information element ignored (vendor OUI "
     152             :                            "%02x:%02x:%02x len=%lu)",
     153          90 :                            pos[0], pos[1], pos[2], (unsigned long) elen);
     154          30 :                 return -1;
     155             :         }
     156             : 
     157       39345 :         return 0;
     158             : }
     159             : 
     160             : 
     161             : /**
     162             :  * ieee802_11_parse_elems - Parse information elements in management frames
     163             :  * @start: Pointer to the start of IEs
     164             :  * @len: Length of IE buffer in octets
     165             :  * @elems: Data structure for parsed elements
     166             :  * @show_errors: Whether to show parsing errors in debug log
     167             :  * Returns: Parsing result
     168             :  */
     169       28537 : ParseRes ieee802_11_parse_elems(const u8 *start, size_t len,
     170             :                                 struct ieee802_11_elems *elems,
     171             :                                 int show_errors)
     172             : {
     173       28537 :         size_t left = len;
     174       28537 :         const u8 *pos = start;
     175       28537 :         int unknown = 0;
     176             : 
     177       28537 :         os_memset(elems, 0, sizeof(*elems));
     178             : 
     179      274055 :         while (left >= 2) {
     180             :                 u8 id, elen;
     181             : 
     182      216984 :                 id = *pos++;
     183      216984 :                 elen = *pos++;
     184      216984 :                 left -= 2;
     185             : 
     186      216984 :                 if (elen > left) {
     187           3 :                         if (show_errors) {
     188           2 :                                 wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
     189             :                                            "parse failed (id=%d elen=%d "
     190             :                                            "left=%lu)",
     191             :                                            id, elen, (unsigned long) left);
     192           2 :                                 wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
     193             :                         }
     194           3 :                         return ParseFailed;
     195             :                 }
     196             : 
     197      216981 :                 switch (id) {
     198             :                 case WLAN_EID_SSID:
     199       17783 :                         elems->ssid = pos;
     200       17783 :                         elems->ssid_len = elen;
     201       17783 :                         break;
     202             :                 case WLAN_EID_SUPP_RATES:
     203       25217 :                         elems->supp_rates = pos;
     204       25217 :                         elems->supp_rates_len = elen;
     205       25217 :                         break;
     206             :                 case WLAN_EID_DS_PARAMS:
     207       14523 :                         elems->ds_params = pos;
     208       14523 :                         elems->ds_params_len = elen;
     209       14523 :                         break;
     210             :                 case WLAN_EID_CF_PARAMS:
     211             :                 case WLAN_EID_TIM:
     212        7605 :                         break;
     213             :                 case WLAN_EID_CHALLENGE:
     214           1 :                         elems->challenge = pos;
     215           1 :                         elems->challenge_len = elen;
     216           1 :                         break;
     217             :                 case WLAN_EID_ERP_INFO:
     218        8036 :                         elems->erp_info = pos;
     219        8036 :                         elems->erp_info_len = elen;
     220        8036 :                         break;
     221             :                 case WLAN_EID_EXT_SUPP_RATES:
     222       18555 :                         elems->ext_supp_rates = pos;
     223       18555 :                         elems->ext_supp_rates_len = elen;
     224       18555 :                         break;
     225             :                 case WLAN_EID_VENDOR_SPECIFIC:
     226       39383 :                         if (ieee802_11_parse_vendor_specific(pos, elen,
     227             :                                                              elems,
     228             :                                                              show_errors))
     229          38 :                                 unknown++;
     230       39383 :                         break;
     231             :                 case WLAN_EID_RSN:
     232        8392 :                         elems->rsn_ie = pos;
     233        8392 :                         elems->rsn_ie_len = elen;
     234        8392 :                         break;
     235             :                 case WLAN_EID_PWR_CAPABILITY:
     236           2 :                         break;
     237             :                 case WLAN_EID_SUPPORTED_CHANNELS:
     238           1 :                         elems->supp_channels = pos;
     239           1 :                         elems->supp_channels_len = elen;
     240           1 :                         break;
     241             :                 case WLAN_EID_MOBILITY_DOMAIN:
     242        1989 :                         elems->mdie = pos;
     243        1989 :                         elems->mdie_len = elen;
     244        1989 :                         break;
     245             :                 case WLAN_EID_FAST_BSS_TRANSITION:
     246        1177 :                         elems->ftie = pos;
     247        1177 :                         elems->ftie_len = elen;
     248        1177 :                         break;
     249             :                 case WLAN_EID_TIMEOUT_INTERVAL:
     250           1 :                         elems->timeout_int = pos;
     251           1 :                         elems->timeout_int_len = elen;
     252           1 :                         break;
     253             :                 case WLAN_EID_HT_CAP:
     254       24254 :                         elems->ht_capabilities = pos;
     255       24254 :                         elems->ht_capabilities_len = elen;
     256       24254 :                         break;
     257             :                 case WLAN_EID_HT_OPERATION:
     258       15454 :                         elems->ht_operation = pos;
     259       15454 :                         elems->ht_operation_len = elen;
     260       15454 :                         break;
     261             :                 case WLAN_EID_MESH_CONFIG:
     262         146 :                         elems->mesh_config = pos;
     263         146 :                         elems->mesh_config_len = elen;
     264         146 :                         break;
     265             :                 case WLAN_EID_MESH_ID:
     266        3134 :                         elems->mesh_id = pos;
     267        3134 :                         elems->mesh_id_len = elen;
     268        3134 :                         break;
     269             :                 case WLAN_EID_PEER_MGMT:
     270         117 :                         elems->peer_mgmt = pos;
     271         117 :                         elems->peer_mgmt_len = elen;
     272         117 :                         break;
     273             :                 case WLAN_EID_VHT_CAP:
     274         107 :                         elems->vht_capabilities = pos;
     275         107 :                         elems->vht_capabilities_len = elen;
     276         107 :                         break;
     277             :                 case WLAN_EID_VHT_OPERATION:
     278          36 :                         elems->vht_operation = pos;
     279          36 :                         elems->vht_operation_len = elen;
     280          36 :                         break;
     281             :                 case WLAN_EID_VHT_OPERATING_MODE_NOTIFICATION:
     282           2 :                         if (elen != 1)
     283           1 :                                 break;
     284           1 :                         elems->vht_opmode_notif = pos;
     285           1 :                         break;
     286             :                 case WLAN_EID_LINK_ID:
     287           2 :                         if (elen < 18)
     288           1 :                                 break;
     289           1 :                         elems->link_id = pos;
     290           1 :                         break;
     291             :                 case WLAN_EID_INTERWORKING:
     292        1398 :                         elems->interworking = pos;
     293        1398 :                         elems->interworking_len = elen;
     294        1398 :                         break;
     295             :                 case WLAN_EID_QOS_MAP_SET:
     296           4 :                         if (elen < 16)
     297           1 :                                 break;
     298           3 :                         elems->qos_map_set = pos;
     299           3 :                         elems->qos_map_set_len = elen;
     300           3 :                         break;
     301             :                 case WLAN_EID_EXT_CAPAB:
     302       21097 :                         elems->ext_capab = pos;
     303       21097 :                         elems->ext_capab_len = elen;
     304       21097 :                         break;
     305             :                 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
     306        7343 :                         if (elen < 3)
     307           1 :                                 break;
     308        7342 :                         elems->bss_max_idle_period = pos;
     309        7342 :                         break;
     310             :                 case WLAN_EID_SSID_LIST:
     311           1 :                         elems->ssid_list = pos;
     312           1 :                         elems->ssid_list_len = elen;
     313           1 :                         break;
     314             :                 case WLAN_EID_AMPE:
     315           0 :                         elems->ampe = pos;
     316           0 :                         elems->ampe_len = elen;
     317           0 :                         break;
     318             :                 case WLAN_EID_MIC:
     319          17 :                         elems->mic = pos;
     320          17 :                         elems->mic_len = elen;
     321             :                         /* after mic everything is encrypted, so stop. */
     322          17 :                         left = elen;
     323          17 :                         break;
     324             :                 default:
     325        1204 :                         unknown++;
     326        1204 :                         if (!show_errors)
     327        1187 :                                 break;
     328          17 :                         wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
     329             :                                    "ignored unknown element (id=%d elen=%d)",
     330             :                                    id, elen);
     331          17 :                         break;
     332             :                 }
     333             : 
     334      216981 :                 left -= elen;
     335      216981 :                 pos += elen;
     336             :         }
     337             : 
     338       28534 :         if (left)
     339           3 :                 return ParseFailed;
     340             : 
     341       28531 :         return unknown ? ParseUnknown : ParseOK;
     342             : }
     343             : 
     344             : 
     345         244 : int ieee802_11_ie_count(const u8 *ies, size_t ies_len)
     346             : {
     347         244 :         int count = 0;
     348             :         const u8 *pos, *end;
     349             : 
     350         244 :         if (ies == NULL)
     351         221 :                 return 0;
     352             : 
     353          23 :         pos = ies;
     354          23 :         end = ies + ies_len;
     355             : 
     356          68 :         while (pos + 2 <= end) {
     357          23 :                 if (pos + 2 + pos[1] > end)
     358           1 :                         break;
     359          22 :                 count++;
     360          22 :                 pos += 2 + pos[1];
     361             :         }
     362             : 
     363          23 :         return count;
     364             : }
     365             : 
     366             : 
     367       11903 : struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
     368             :                                             u32 oui_type)
     369             : {
     370             :         struct wpabuf *buf;
     371             :         const u8 *end, *pos, *ie;
     372             : 
     373       11903 :         pos = ies;
     374       11903 :         end = ies + ies_len;
     375       11903 :         ie = NULL;
     376             : 
     377       83318 :         while (pos + 1 < end) {
     378       69133 :                 if (pos + 2 + pos[1] > end)
     379           1 :                         return NULL;
     380       88545 :                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
     381       19413 :                     WPA_GET_BE32(&pos[2]) == oui_type) {
     382        9620 :                         ie = pos;
     383        9620 :                         break;
     384             :                 }
     385       59512 :                 pos += 2 + pos[1];
     386             :         }
     387             : 
     388       11902 :         if (ie == NULL)
     389        2282 :                 return NULL; /* No specified vendor IE found */
     390             : 
     391        9620 :         buf = wpabuf_alloc(ies_len);
     392        9620 :         if (buf == NULL)
     393           0 :                 return NULL;
     394             : 
     395             :         /*
     396             :          * There may be multiple vendor IEs in the message, so need to
     397             :          * concatenate their data fields.
     398             :          */
     399       34994 :         while (pos + 1 < end) {
     400       15754 :                 if (pos + 2 + pos[1] > end)
     401           0 :                         break;
     402       30131 :                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
     403       14377 :                     WPA_GET_BE32(&pos[2]) == oui_type)
     404        9648 :                         wpabuf_put_data(buf, pos + 6, pos[1] - 4);
     405       15754 :                 pos += 2 + pos[1];
     406             :         }
     407             : 
     408        9620 :         return buf;
     409             : }
     410             : 
     411             : 
     412       20714 : const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
     413             : {
     414             :         u16 fc, type, stype;
     415             : 
     416             :         /*
     417             :          * PS-Poll frames are 16 bytes. All other frames are
     418             :          * 24 bytes or longer.
     419             :          */
     420       20714 :         if (len < 16)
     421           0 :                 return NULL;
     422             : 
     423       20714 :         fc = le_to_host16(hdr->frame_control);
     424       20714 :         type = WLAN_FC_GET_TYPE(fc);
     425       20714 :         stype = WLAN_FC_GET_STYPE(fc);
     426             : 
     427       20714 :         switch (type) {
     428             :         case WLAN_FC_TYPE_DATA:
     429           1 :                 if (len < 24)
     430           0 :                         return NULL;
     431           1 :                 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
     432             :                 case WLAN_FC_FROMDS | WLAN_FC_TODS:
     433             :                 case WLAN_FC_TODS:
     434           1 :                         return hdr->addr1;
     435             :                 case WLAN_FC_FROMDS:
     436           0 :                         return hdr->addr2;
     437             :                 default:
     438           0 :                         return NULL;
     439             :                 }
     440             :         case WLAN_FC_TYPE_CTRL:
     441           0 :                 if (stype != WLAN_FC_STYPE_PSPOLL)
     442           0 :                         return NULL;
     443           0 :                 return hdr->addr1;
     444             :         case WLAN_FC_TYPE_MGMT:
     445       20713 :                 return hdr->addr3;
     446             :         default:
     447           0 :                 return NULL;
     448             :         }
     449             : }
     450             : 
     451             : 
     452          42 : int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
     453             :                           const char *name, const char *val)
     454             : {
     455             :         int num, v;
     456             :         const char *pos;
     457             :         struct hostapd_wmm_ac_params *ac;
     458             : 
     459             :         /* skip 'wme_ac_' or 'wmm_ac_' prefix */
     460          42 :         pos = name + 7;
     461          42 :         if (os_strncmp(pos, "be_", 3) == 0) {
     462           6 :                 num = 0;
     463           6 :                 pos += 3;
     464          36 :         } else if (os_strncmp(pos, "bk_", 3) == 0) {
     465          13 :                 num = 1;
     466          13 :                 pos += 3;
     467          23 :         } else if (os_strncmp(pos, "vi_", 3) == 0) {
     468          11 :                 num = 2;
     469          11 :                 pos += 3;
     470          12 :         } else if (os_strncmp(pos, "vo_", 3) == 0) {
     471          11 :                 num = 3;
     472          11 :                 pos += 3;
     473             :         } else {
     474           1 :                 wpa_printf(MSG_ERROR, "Unknown WMM name '%s'", pos);
     475           1 :                 return -1;
     476             :         }
     477             : 
     478          41 :         ac = &wmm_ac_params[num];
     479             : 
     480          41 :         if (os_strcmp(pos, "aifs") == 0) {
     481           6 :                 v = atoi(val);
     482           6 :                 if (v < 1 || v > 255) {
     483           2 :                         wpa_printf(MSG_ERROR, "Invalid AIFS value %d", v);
     484           2 :                         return -1;
     485             :                 }
     486           4 :                 ac->aifs = v;
     487          35 :         } else if (os_strcmp(pos, "cwmin") == 0) {
     488           6 :                 v = atoi(val);
     489           6 :                 if (v < 0 || v > 12) {
     490           2 :                         wpa_printf(MSG_ERROR, "Invalid cwMin value %d", v);
     491           2 :                         return -1;
     492             :                 }
     493           4 :                 ac->cwmin = v;
     494          29 :         } else if (os_strcmp(pos, "cwmax") == 0) {
     495           6 :                 v = atoi(val);
     496           6 :                 if (v < 0 || v > 12) {
     497           2 :                         wpa_printf(MSG_ERROR, "Invalid cwMax value %d", v);
     498           2 :                         return -1;
     499             :                 }
     500           4 :                 ac->cwmax = v;
     501          23 :         } else if (os_strcmp(pos, "txop_limit") == 0) {
     502           6 :                 v = atoi(val);
     503           6 :                 if (v < 0 || v > 0xffff) {
     504           2 :                         wpa_printf(MSG_ERROR, "Invalid txop value %d", v);
     505           2 :                         return -1;
     506             :                 }
     507           4 :                 ac->txop_limit = v;
     508          17 :         } else if (os_strcmp(pos, "acm") == 0) {
     509          16 :                 v = atoi(val);
     510          16 :                 if (v < 0 || v > 1) {
     511           2 :                         wpa_printf(MSG_ERROR, "Invalid acm value %d", v);
     512           2 :                         return -1;
     513             :                 }
     514          14 :                 ac->admission_control_mandatory = v;
     515             :         } else {
     516           1 :                 wpa_printf(MSG_ERROR, "Unknown wmm_ac_ field '%s'", pos);
     517           1 :                 return -1;
     518             :         }
     519             : 
     520          30 :         return 0;
     521             : }
     522             : 
     523             : 
     524      317734 : enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel)
     525             : {
     526      317734 :         enum hostapd_hw_mode mode = NUM_HOSTAPD_MODES;
     527             : 
     528      317734 :         if (freq >= 2412 && freq <= 2472) {
     529      108901 :                 mode = HOSTAPD_MODE_IEEE80211G;
     530      108901 :                 *channel = (freq - 2407) / 5;
     531      208833 :         } else if (freq == 2484) {
     532        8353 :                 mode = HOSTAPD_MODE_IEEE80211B;
     533        8353 :                 *channel = 14;
     534      200480 :         } else if (freq >= 4900 && freq < 5000) {
     535           0 :                 mode = HOSTAPD_MODE_IEEE80211A;
     536           0 :                 *channel = (freq - 4000) / 5;
     537      200480 :         } else if (freq >= 5000 && freq < 5900) {
     538      200480 :                 mode = HOSTAPD_MODE_IEEE80211A;
     539      200480 :                 *channel = (freq - 5000) / 5;
     540           0 :         } else if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
     541           0 :                 mode = HOSTAPD_MODE_IEEE80211AD;
     542           0 :                 *channel = (freq - 56160) / 2160;
     543             :         }
     544             : 
     545      317734 :         return mode;
     546             : }
     547             : 
     548             : 
     549             : static const char *us_op_class_cc[] = {
     550             :         "US", "CA", NULL
     551             : };
     552             : 
     553             : static const char *eu_op_class_cc[] = {
     554             :         "AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
     555             :         "DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
     556             :         "LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
     557             :         "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", NULL
     558             : };
     559             : 
     560             : static const char *jp_op_class_cc[] = {
     561             :         "JP", NULL
     562             : };
     563             : 
     564             : static const char *cn_op_class_cc[] = {
     565             :         "CN", "CA", NULL
     566             : };
     567             : 
     568             : 
     569       37082 : static int country_match(const char *cc[], const char *country)
     570             : {
     571             :         int i;
     572             : 
     573       37082 :         if (country == NULL)
     574       37060 :                 return 0;
     575         145 :         for (i = 0; cc[i]; i++) {
     576         129 :                 if (cc[i][0] == country[0] && cc[i][1] == country[1])
     577           6 :                         return 1;
     578             :         }
     579             : 
     580          16 :         return 0;
     581             : }
     582             : 
     583             : 
     584           0 : static int ieee80211_chan_to_freq_us(u8 op_class, u8 chan)
     585             : {
     586           0 :         switch (op_class) {
     587             :         case 12: /* channels 1..11 */
     588             :         case 32: /* channels 1..7; 40 MHz */
     589             :         case 33: /* channels 5..11; 40 MHz */
     590           0 :                 if (chan < 1 || chan > 11)
     591           0 :                         return -1;
     592           0 :                 return 2407 + 5 * chan;
     593             :         case 1: /* channels 36,40,44,48 */
     594             :         case 2: /* channels 52,56,60,64; dfs */
     595             :         case 22: /* channels 36,44; 40 MHz */
     596             :         case 23: /* channels 52,60; 40 MHz */
     597             :         case 27: /* channels 40,48; 40 MHz */
     598             :         case 28: /* channels 56,64; 40 MHz */
     599           0 :                 if (chan < 36 || chan > 64)
     600           0 :                         return -1;
     601           0 :                 return 5000 + 5 * chan;
     602             :         case 4: /* channels 100-144 */
     603             :         case 24: /* channels 100-140; 40 MHz */
     604           0 :                 if (chan < 100 || chan > 144)
     605           0 :                         return -1;
     606           0 :                 return 5000 + 5 * chan;
     607             :         case 3: /* channels 149,153,157,161 */
     608             :         case 25: /* channels 149,157; 40 MHz */
     609             :         case 26: /* channels 149,157; 40 MHz */
     610             :         case 30: /* channels 153,161; 40 MHz */
     611             :         case 31: /* channels 153,161; 40 MHz */
     612           0 :                 if (chan < 149 || chan > 161)
     613           0 :                         return -1;
     614           0 :                 return 5000 + 5 * chan;
     615             :         case 34: /* 60 GHz band, channels 1..3 */
     616           0 :                 if (chan < 1 || chan > 3)
     617           0 :                         return -1;
     618           0 :                 return 56160 + 2160 * chan;
     619             :         }
     620           0 :         return -1;
     621             : }
     622             : 
     623             : 
     624           6 : static int ieee80211_chan_to_freq_eu(u8 op_class, u8 chan)
     625             : {
     626           6 :         switch (op_class) {
     627             :         case 4: /* channels 1..13 */
     628             :         case 11: /* channels 1..9; 40 MHz */
     629             :         case 12: /* channels 5..13; 40 MHz */
     630           0 :                 if (chan < 1 || chan > 13)
     631           0 :                         return -1;
     632           0 :                 return 2407 + 5 * chan;
     633             :         case 1: /* channels 36,40,44,48 */
     634             :         case 2: /* channels 52,56,60,64; dfs */
     635             :         case 5: /* channels 36,44; 40 MHz */
     636             :         case 6: /* channels 52,60; 40 MHz */
     637             :         case 8: /* channels 40,48; 40 MHz */
     638             :         case 9: /* channels 56,64; 40 MHz */
     639           1 :                 if (chan < 36 || chan > 64)
     640           0 :                         return -1;
     641           1 :                 return 5000 + 5 * chan;
     642             :         case 3: /* channels 100-140 */
     643             :         case 7: /* channels 100-132; 40 MHz */
     644             :         case 10: /* channels 104-136; 40 MHz */
     645             :         case 16: /* channels 100-140 */
     646           0 :                 if (chan < 100 || chan > 140)
     647           0 :                         return -1;
     648           0 :                 return 5000 + 5 * chan;
     649             :         case 17: /* channels 149,153,157,161,165,169 */
     650           0 :                 if (chan < 149 || chan > 169)
     651           0 :                         return -1;
     652           0 :                 return 5000 + 5 * chan;
     653             :         case 18: /* 60 GHz band, channels 1..4 */
     654           0 :                 if (chan < 1 || chan > 4)
     655           0 :                         return -1;
     656           0 :                 return 56160 + 2160 * chan;
     657             :         }
     658           5 :         return -1;
     659             : }
     660             : 
     661             : 
     662           0 : static int ieee80211_chan_to_freq_jp(u8 op_class, u8 chan)
     663             : {
     664           0 :         switch (op_class) {
     665             :         case 30: /* channels 1..13 */
     666             :         case 56: /* channels 1..9; 40 MHz */
     667             :         case 57: /* channels 5..13; 40 MHz */
     668           0 :                 if (chan < 1 || chan > 13)
     669           0 :                         return -1;
     670           0 :                 return 2407 + 5 * chan;
     671             :         case 31: /* channel 14 */
     672           0 :                 if (chan != 14)
     673           0 :                         return -1;
     674           0 :                 return 2414 + 5 * chan;
     675             :         case 1: /* channels 34,38,42,46(old) or 36,40,44,48 */
     676             :         case 32: /* channels 52,56,60,64 */
     677             :         case 33: /* channels 52,56,60,64 */
     678             :         case 36: /* channels 36,44; 40 MHz */
     679             :         case 37: /* channels 52,60; 40 MHz */
     680             :         case 38: /* channels 52,60; 40 MHz */
     681             :         case 41: /* channels 40,48; 40 MHz */
     682             :         case 42: /* channels 56,64; 40 MHz */
     683             :         case 43: /* channels 56,64; 40 MHz */
     684           0 :                 if (chan < 34 || chan > 64)
     685           0 :                         return -1;
     686           0 :                 return 5000 + 5 * chan;
     687             :         case 34: /* channels 100-140 */
     688             :         case 35: /* channels 100-140 */
     689             :         case 39: /* channels 100-132; 40 MHz */
     690             :         case 40: /* channels 100-132; 40 MHz */
     691             :         case 44: /* channels 104-136; 40 MHz */
     692             :         case 45: /* channels 104-136; 40 MHz */
     693             :         case 58: /* channels 100-140 */
     694           0 :                 if (chan < 100 || chan > 140)
     695           0 :                         return -1;
     696           0 :                 return 5000 + 5 * chan;
     697             :         case 59: /* 60 GHz band, channels 1..4 */
     698           0 :                 if (chan < 1 || chan > 3)
     699           0 :                         return -1;
     700           0 :                 return 56160 + 2160 * chan;
     701             :         }
     702           0 :         return -1;
     703             : }
     704             : 
     705             : 
     706           0 : static int ieee80211_chan_to_freq_cn(u8 op_class, u8 chan)
     707             : {
     708           0 :         switch (op_class) {
     709             :         case 7: /* channels 1..13 */
     710             :         case 8: /* channels 1..9; 40 MHz */
     711             :         case 9: /* channels 5..13; 40 MHz */
     712           0 :                 if (chan < 1 || chan > 13)
     713           0 :                         return -1;
     714           0 :                 return 2407 + 5 * chan;
     715             :         case 1: /* channels 36,40,44,48 */
     716             :         case 2: /* channels 52,56,60,64; dfs */
     717             :         case 4: /* channels 36,44; 40 MHz */
     718             :         case 5: /* channels 52,60; 40 MHz */
     719           0 :                 if (chan < 36 || chan > 64)
     720           0 :                         return -1;
     721           0 :                 return 5000 + 5 * chan;
     722             :         case 3: /* channels 149,153,157,161,165 */
     723             :         case 6: /* channels 149,157; 40 MHz */
     724           0 :                 if (chan < 149 || chan > 165)
     725           0 :                         return -1;
     726           0 :                 return 5000 + 5 * chan;
     727             :         }
     728           0 :         return -1;
     729             : }
     730             : 
     731             : 
     732        9270 : static int ieee80211_chan_to_freq_global(u8 op_class, u8 chan)
     733             : {
     734             :         /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */
     735        9270 :         switch (op_class) {
     736             :         case 81:
     737             :                 /* channels 1..13 */
     738        8886 :                 if (chan < 1 || chan > 13)
     739           1 :                         return -1;
     740        8885 :                 return 2407 + 5 * chan;
     741             :         case 82:
     742             :                 /* channel 14 */
     743           0 :                 if (chan != 14)
     744           0 :                         return -1;
     745           0 :                 return 2414 + 5 * chan;
     746             :         case 83: /* channels 1..9; 40 MHz */
     747             :         case 84: /* channels 5..13; 40 MHz */
     748           0 :                 if (chan < 1 || chan > 13)
     749           0 :                         return -1;
     750           0 :                 return 2407 + 5 * chan;
     751             :         case 115: /* channels 36,40,44,48; indoor only */
     752             :         case 116: /* channels 36,44; 40 MHz; indoor only */
     753             :         case 117: /* channels 40,48; 40 MHz; indoor only */
     754             :         case 118: /* channels 52,56,60,64; dfs */
     755             :         case 119: /* channels 52,60; 40 MHz; dfs */
     756             :         case 120: /* channels 56,64; 40 MHz; dfs */
     757         145 :                 if (chan < 36 || chan > 64)
     758           1 :                         return -1;
     759         144 :                 return 5000 + 5 * chan;
     760             :         case 121: /* channels 100-140 */
     761             :         case 122: /* channels 100-142; 40 MHz */
     762             :         case 123: /* channels 104-136; 40 MHz */
     763           0 :                 if (chan < 100 || chan > 140)
     764           0 :                         return -1;
     765           0 :                 return 5000 + 5 * chan;
     766             :         case 124: /* channels 149,153,157,161 */
     767             :         case 125: /* channels 149,153,157,161,165,169 */
     768             :         case 126: /* channels 149,157; 40 MHz */
     769             :         case 127: /* channels 153,161; 40 MHz */
     770         167 :                 if (chan < 149 || chan > 161)
     771           0 :                         return -1;
     772         167 :                 return 5000 + 5 * chan;
     773             :         case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
     774             :         case 130: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
     775          72 :                 if (chan < 36 || chan > 161)
     776           0 :                         return -1;
     777          72 :                 return 5000 + 5 * chan;
     778             :         case 129: /* center freqs 50, 114; 160 MHz */
     779           0 :                 if (chan < 50 || chan > 114)
     780           0 :                         return -1;
     781           0 :                 return 5000 + 5 * chan;
     782             :         case 180: /* 60 GHz band, channels 1..4 */
     783           0 :                 if (chan < 1 || chan > 4)
     784           0 :                         return -1;
     785           0 :                 return 56160 + 2160 * chan;
     786             :         }
     787           0 :         return -1;
     788             : }
     789             : 
     790             : /**
     791             :  * ieee80211_chan_to_freq - Convert channel info to frequency
     792             :  * @country: Country code, if known; otherwise, global operating class is used
     793             :  * @op_class: Operating class
     794             :  * @chan: Channel number
     795             :  * Returns: Frequency in MHz or -1 if the specified channel is unknown
     796             :  */
     797        9271 : int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan)
     798             : {
     799             :         int freq;
     800             : 
     801        9271 :         if (country_match(us_op_class_cc, country)) {
     802           0 :                 freq = ieee80211_chan_to_freq_us(op_class, chan);
     803           0 :                 if (freq > 0)
     804           0 :                         return freq;
     805             :         }
     806             : 
     807        9271 :         if (country_match(eu_op_class_cc, country)) {
     808           6 :                 freq = ieee80211_chan_to_freq_eu(op_class, chan);
     809           6 :                 if (freq > 0)
     810           1 :                         return freq;
     811             :         }
     812             : 
     813        9270 :         if (country_match(jp_op_class_cc, country)) {
     814           0 :                 freq = ieee80211_chan_to_freq_jp(op_class, chan);
     815           0 :                 if (freq > 0)
     816           0 :                         return freq;
     817             :         }
     818             : 
     819        9270 :         if (country_match(cn_op_class_cc, country)) {
     820           0 :                 freq = ieee80211_chan_to_freq_cn(op_class, chan);
     821           0 :                 if (freq > 0)
     822           0 :                         return freq;
     823             :         }
     824             : 
     825        9270 :         return ieee80211_chan_to_freq_global(op_class, chan);
     826             : }
     827             : 
     828             : 
     829        9080 : static int is_11b(u8 rate)
     830             : {
     831        9080 :         return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
     832             : }
     833             : 
     834             : 
     835        1114 : int supp_rates_11b_only(struct ieee802_11_elems *elems)
     836             : {
     837        1114 :         int num_11b = 0, num_others = 0;
     838             :         int i;
     839             : 
     840        1114 :         if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
     841           0 :                 return 0;
     842             : 
     843       10026 :         for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
     844        8912 :                 if (is_11b(elems->supp_rates[i]))
     845         168 :                         num_11b++;
     846             :                 else
     847        8744 :                         num_others++;
     848             :         }
     849             : 
     850        2396 :         for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
     851         168 :              i++) {
     852         168 :                 if (is_11b(elems->ext_supp_rates[i]))
     853           0 :                         num_11b++;
     854             :                 else
     855         168 :                         num_others++;
     856             :         }
     857             : 
     858        1114 :         return num_11b > 0 && num_others == 0;
     859             : }
     860             : 
     861             : 
     862       48769 : const char * fc2str(u16 fc)
     863             : {
     864       48769 :         u16 stype = WLAN_FC_GET_STYPE(fc);
     865             : #define C2S(x) case x: return #x;
     866             : 
     867       48769 :         switch (WLAN_FC_GET_TYPE(fc)) {
     868             :         case WLAN_FC_TYPE_MGMT:
     869       48769 :                 switch (stype) {
     870        3434 :                 C2S(WLAN_FC_STYPE_ASSOC_REQ)
     871        2173 :                 C2S(WLAN_FC_STYPE_ASSOC_RESP)
     872        1568 :                 C2S(WLAN_FC_STYPE_REASSOC_REQ)
     873         302 :                 C2S(WLAN_FC_STYPE_REASSOC_RESP)
     874        5550 :                 C2S(WLAN_FC_STYPE_PROBE_REQ)
     875        2942 :                 C2S(WLAN_FC_STYPE_PROBE_RESP)
     876           0 :                 C2S(WLAN_FC_STYPE_BEACON)
     877           0 :                 C2S(WLAN_FC_STYPE_ATIM)
     878        1272 :                 C2S(WLAN_FC_STYPE_DISASSOC)
     879        6437 :                 C2S(WLAN_FC_STYPE_AUTH)
     880        5851 :                 C2S(WLAN_FC_STYPE_DEAUTH)
     881       19240 :                 C2S(WLAN_FC_STYPE_ACTION)
     882             :                 }
     883           0 :                 break;
     884             :         case WLAN_FC_TYPE_CTRL:
     885           0 :                 switch (stype) {
     886           0 :                 C2S(WLAN_FC_STYPE_PSPOLL)
     887           0 :                 C2S(WLAN_FC_STYPE_RTS)
     888           0 :                 C2S(WLAN_FC_STYPE_CTS)
     889           0 :                 C2S(WLAN_FC_STYPE_ACK)
     890           0 :                 C2S(WLAN_FC_STYPE_CFEND)
     891           0 :                 C2S(WLAN_FC_STYPE_CFENDACK)
     892             :                 }
     893           0 :                 break;
     894             :         case WLAN_FC_TYPE_DATA:
     895           0 :                 switch (stype) {
     896           0 :                 C2S(WLAN_FC_STYPE_DATA)
     897           0 :                 C2S(WLAN_FC_STYPE_DATA_CFACK)
     898           0 :                 C2S(WLAN_FC_STYPE_DATA_CFPOLL)
     899           0 :                 C2S(WLAN_FC_STYPE_DATA_CFACKPOLL)
     900           0 :                 C2S(WLAN_FC_STYPE_NULLFUNC)
     901           0 :                 C2S(WLAN_FC_STYPE_CFACK)
     902           0 :                 C2S(WLAN_FC_STYPE_CFPOLL)
     903           0 :                 C2S(WLAN_FC_STYPE_CFACKPOLL)
     904           0 :                 C2S(WLAN_FC_STYPE_QOS_DATA)
     905           0 :                 C2S(WLAN_FC_STYPE_QOS_DATA_CFACK)
     906           0 :                 C2S(WLAN_FC_STYPE_QOS_DATA_CFPOLL)
     907           0 :                 C2S(WLAN_FC_STYPE_QOS_DATA_CFACKPOLL)
     908           0 :                 C2S(WLAN_FC_STYPE_QOS_NULL)
     909           0 :                 C2S(WLAN_FC_STYPE_QOS_CFPOLL)
     910           0 :                 C2S(WLAN_FC_STYPE_QOS_CFACKPOLL)
     911             :                 }
     912           0 :                 break;
     913             :         }
     914           0 :         return "WLAN_FC_TYPE_UNKNOWN";
     915             : #undef C2S
     916             : }

Generated by: LCOV version 1.10