Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /******************************************************************************
0003  * ieee80211.c
0004  *
0005  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
0006  * Linux device driver for RTL8192SU
0007  *
0008  * Modifications for inclusion into the Linux staging tree are
0009  * Copyright(c) 2010 Larry Finger. All rights reserved.
0010  *
0011  * Contact information:
0012  * WLAN FAE <wlanfae@realtek.com>.
0013  * Larry Finger <Larry.Finger@lwfinger.net>
0014  *
0015  ******************************************************************************/
0016 
0017 #define _IEEE80211_C
0018 
0019 #include "drv_types.h"
0020 #include "ieee80211.h"
0021 #include "wifi.h"
0022 #include "osdep_service.h"
0023 #include "wlan_bssdef.h"
0024 
0025 static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
0026 static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
0027 static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
0028 static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
0029 static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
0030 static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
0031 
0032 static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
0033 static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
0034 static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
0035 static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
0036 static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
0037 
0038 /*-----------------------------------------------------------
0039  * for adhoc-master to generate ie and provide supported-rate to fw
0040  *-----------------------------------------------------------
0041  */
0042 
0043 static u8 WIFI_CCKRATES[] =  {
0044     (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
0045     (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
0046     (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
0047     (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
0048 };
0049 
0050 static u8 WIFI_OFDMRATES[] = {
0051     (IEEE80211_OFDM_RATE_6MB),
0052     (IEEE80211_OFDM_RATE_9MB),
0053     (IEEE80211_OFDM_RATE_12MB),
0054     (IEEE80211_OFDM_RATE_18MB),
0055     (IEEE80211_OFDM_RATE_24MB),
0056     (IEEE80211_OFDM_RATE_36MB),
0057     (IEEE80211_OFDM_RATE_48MB),
0058     (IEEE80211_OFDM_RATE_54MB)
0059 };
0060 
0061 uint r8712_is_cckrates_included(u8 *rate)
0062 {
0063     u32 i = 0;
0064 
0065     while (rate[i] != 0) {
0066         if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
0067             (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
0068             return true;
0069         i++;
0070     }
0071     return false;
0072 }
0073 
0074 uint r8712_is_cckratesonly_included(u8 *rate)
0075 {
0076     u32 i = 0;
0077 
0078     while (rate[i] != 0) {
0079         if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
0080             (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
0081             return false;
0082         i++;
0083     }
0084     return true;
0085 }
0086 
0087 /* r8712_set_ie will update frame length */
0088 u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
0089 {
0090     *pbuf = (u8)index;
0091     *(pbuf + 1) = (u8)len;
0092     if (len > 0)
0093         memcpy((void *)(pbuf + 2), (void *)source, len);
0094     *frlen = *frlen + (len + 2);
0095     return pbuf + len + 2;
0096 }
0097 
0098 /* ---------------------------------------------------------------------------
0099  * index: the information element id index, limit is the limit for search
0100  * ---------------------------------------------------------------------------
0101  */
0102 u8 *r8712_get_ie(u8 *pbuf, sint index, uint *len, sint limit)
0103 {
0104     sint tmp, i;
0105     u8 *p;
0106 
0107     if (limit < 1)
0108         return NULL;
0109     p = pbuf;
0110     i = 0;
0111     *len = 0;
0112     while (1) {
0113         if (*p == index) {
0114             *len = *(p + 1);
0115             return p;
0116         }
0117         tmp = *(p + 1);
0118         p += (tmp + 2);
0119         i += (tmp + 2);
0120         if (i >= limit)
0121             break;
0122     }
0123     return NULL;
0124 }
0125 
0126 static void set_supported_rate(u8 *rates, uint mode)
0127 {
0128     memset(rates, 0, NDIS_802_11_LENGTH_RATES_EX);
0129     switch (mode) {
0130     case WIRELESS_11B:
0131         memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
0132         break;
0133     case WIRELESS_11G:
0134     case WIRELESS_11A:
0135         memcpy(rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
0136         break;
0137     case WIRELESS_11BG:
0138         memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
0139         memcpy(rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
0140                IEEE80211_NUM_OFDM_RATESLEN);
0141         break;
0142     }
0143 }
0144 
0145 static uint r8712_get_rateset_len(u8 *rateset)
0146 {
0147     uint i = 0;
0148 
0149     while (1) {
0150         if ((rateset[i]) == 0)
0151             break;
0152         if (i > 12)
0153             break;
0154         i++;
0155     }
0156     return i;
0157 }
0158 
0159 int r8712_generate_ie(struct registry_priv *registrypriv)
0160 {
0161     int rate_len;
0162     uint sz = 0;
0163     struct wlan_bssid_ex *dev_network = &registrypriv->dev_network;
0164     u8 *ie = dev_network->IEs;
0165     u16 beacon_period = (u16)dev_network->Configuration.BeaconPeriod;
0166 
0167     /*timestamp will be inserted by hardware*/
0168     sz += 8;
0169     ie += sz;
0170     /*beacon interval : 2bytes*/
0171     *(__le16 *)ie = cpu_to_le16(beacon_period);
0172     sz += 2;
0173     ie += 2;
0174     /*capability info*/
0175     *(u16 *)ie = 0;
0176     *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_IBSS);
0177     if (registrypriv->preamble == PREAMBLE_SHORT)
0178         *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
0179     if (dev_network->Privacy)
0180         *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
0181     sz += 2;
0182     ie += 2;
0183     /*SSID*/
0184     ie = r8712_set_ie(ie, WLAN_EID_SSID, dev_network->Ssid.SsidLength,
0185               dev_network->Ssid.Ssid, &sz);
0186     /*supported rates*/
0187     set_supported_rate(dev_network->rates, registrypriv->wireless_mode);
0188     rate_len = r8712_get_rateset_len(dev_network->rates);
0189     if (rate_len > 8) {
0190         ie = r8712_set_ie(ie, WLAN_EID_SUPP_RATES, 8,
0191                   dev_network->rates, &sz);
0192         ie = r8712_set_ie(ie, WLAN_EID_EXT_SUPP_RATES, (rate_len - 8),
0193                   (dev_network->rates + 8), &sz);
0194     } else {
0195         ie = r8712_set_ie(ie, WLAN_EID_SUPP_RATES,
0196                   rate_len, dev_network->rates, &sz);
0197     }
0198     /*DS parameter set*/
0199     ie = r8712_set_ie(ie, WLAN_EID_DS_PARAMS, 1,
0200               (u8 *)&dev_network->Configuration.DSConfig, &sz);
0201     /*IBSS Parameter Set*/
0202     ie = r8712_set_ie(ie, WLAN_EID_IBSS_PARAMS, 2,
0203               (u8 *)&dev_network->Configuration.ATIMWindow, &sz);
0204     return sz;
0205 }
0206 
0207 unsigned char *r8712_get_wpa_ie(unsigned char *ie, uint *wpa_ie_len, int limit)
0208 {
0209     u32 len;
0210     u16 val16;
0211     unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
0212     u8 *buf = ie;
0213 
0214     while (1) {
0215         buf = r8712_get_ie(buf, _WPA_IE_ID_, &len, limit);
0216         if (buf) {
0217             /*check if oui matches...*/
0218             if (memcmp((buf + 2), wpa_oui_type,
0219                    sizeof(wpa_oui_type)))
0220                 goto check_next_ie;
0221             /*check version...*/
0222             memcpy((u8 *)&val16, (buf + 6), sizeof(val16));
0223             le16_to_cpus(&val16);
0224             if (val16 != 0x0001)
0225                 goto check_next_ie;
0226             *wpa_ie_len = *(buf + 1);
0227             return buf;
0228         }
0229         *wpa_ie_len = 0;
0230         return NULL;
0231 check_next_ie:
0232         limit = limit - (buf - ie) - 2 - len;
0233         if (limit <= 0)
0234             break;
0235         buf += (2 + len);
0236     }
0237     *wpa_ie_len = 0;
0238     return NULL;
0239 }
0240 
0241 unsigned char *r8712_get_wpa2_ie(unsigned char *pie, uint *rsn_ie_len,
0242                  int limit)
0243 {
0244     return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
0245 }
0246 
0247 static int r8712_get_wpa_cipher_suite(u8 *s)
0248 {
0249     if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
0250         return WPA_CIPHER_NONE;
0251     if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
0252         return WPA_CIPHER_WEP40;
0253     if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
0254         return WPA_CIPHER_TKIP;
0255     if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
0256         return WPA_CIPHER_CCMP;
0257     if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
0258         return WPA_CIPHER_WEP104;
0259     return 0;
0260 }
0261 
0262 static int r8712_get_wpa2_cipher_suite(u8 *s)
0263 {
0264     if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
0265         return WPA_CIPHER_NONE;
0266     if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
0267         return WPA_CIPHER_WEP40;
0268     if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
0269         return WPA_CIPHER_TKIP;
0270     if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
0271         return WPA_CIPHER_CCMP;
0272     if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
0273         return WPA_CIPHER_WEP104;
0274     return 0;
0275 }
0276 
0277 int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
0278                int *pairwise_cipher)
0279 {
0280     int i;
0281     int left, count;
0282     u8 *pos;
0283 
0284     if (wpa_ie_len <= 0) {
0285         /* No WPA IE - fail silently */
0286         return -EINVAL;
0287     }
0288     if ((*wpa_ie != _WPA_IE_ID_) ||
0289         (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
0290         (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
0291         return -EINVAL;
0292     pos = wpa_ie;
0293     pos += 8;
0294     left = wpa_ie_len - 8;
0295     /*group_cipher*/
0296     if (left >= WPA_SELECTOR_LEN) {
0297         *group_cipher = r8712_get_wpa_cipher_suite(pos);
0298         pos += WPA_SELECTOR_LEN;
0299         left -= WPA_SELECTOR_LEN;
0300     } else if (left > 0) {
0301         return -EINVAL;
0302     }
0303     /*pairwise_cipher*/
0304     if (left >= 2) {
0305         count = le16_to_cpu(*(__le16 *)pos);
0306         pos += 2;
0307         left -= 2;
0308         if (count == 0 || left < count * WPA_SELECTOR_LEN)
0309             return -EINVAL;
0310         for (i = 0; i < count; i++) {
0311             *pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
0312             pos += WPA_SELECTOR_LEN;
0313             left -= WPA_SELECTOR_LEN;
0314         }
0315     } else if (left == 1) {
0316         return -EINVAL;
0317     }
0318     return 0;
0319 }
0320 
0321 int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
0322             int *pairwise_cipher)
0323 {
0324     int i;
0325     int left, count;
0326     u8 *pos;
0327 
0328     if (rsn_ie_len <= 0) {
0329         /* No RSN IE - fail silently */
0330         return -EINVAL;
0331     }
0332     if ((*rsn_ie != _WPA2_IE_ID_) ||
0333         (*(rsn_ie + 1) != (u8)(rsn_ie_len - 2)))
0334         return -EINVAL;
0335     pos = rsn_ie;
0336     pos += 4;
0337     left = rsn_ie_len - 4;
0338     /*group_cipher*/
0339     if (left >= RSN_SELECTOR_LEN) {
0340         *group_cipher = r8712_get_wpa2_cipher_suite(pos);
0341         pos += RSN_SELECTOR_LEN;
0342         left -= RSN_SELECTOR_LEN;
0343     } else if (left > 0) {
0344         return -EINVAL;
0345     }
0346     /*pairwise_cipher*/
0347     if (left >= 2) {
0348         count = le16_to_cpu(*(__le16 *)pos);
0349         pos += 2;
0350         left -= 2;
0351         if (count == 0 || left < count * RSN_SELECTOR_LEN)
0352             return -EINVAL;
0353         for (i = 0; i < count; i++) {
0354             *pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
0355             pos += RSN_SELECTOR_LEN;
0356             left -= RSN_SELECTOR_LEN;
0357         }
0358     } else if (left == 1) {
0359         return -EINVAL;
0360     }
0361     return 0;
0362 }
0363 
0364 int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
0365              u8 *wpa_ie, u16 *wpa_len)
0366 {
0367     u8 authmode;
0368     u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
0369     uint cnt;
0370 
0371     /*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
0372     cnt = _TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_;
0373     while (cnt < in_len) {
0374         authmode = in_ie[cnt];
0375         if ((authmode == _WPA_IE_ID_) &&
0376             (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
0377             memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
0378             *wpa_len = in_ie[cnt + 1] + 2;
0379             cnt += in_ie[cnt + 1] + 2;  /*get next */
0380         } else {
0381             if (authmode == _WPA2_IE_ID_) {
0382                 memcpy(rsn_ie, &in_ie[cnt],
0383                        in_ie[cnt + 1] + 2);
0384                 *rsn_len = in_ie[cnt + 1] + 2;
0385                 cnt += in_ie[cnt + 1] + 2;  /*get next*/
0386             } else {
0387                 cnt += in_ie[cnt + 1] + 2;   /*get next*/
0388             }
0389         }
0390     }
0391     return *rsn_len + *wpa_len;
0392 }
0393 
0394 int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
0395 {
0396     int match;
0397     uint cnt;
0398     u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
0399 
0400     cnt = 12;
0401     match = false;
0402     while (cnt < in_len) {
0403         eid = in_ie[cnt];
0404         if ((eid == _WPA_IE_ID_) &&
0405             (!memcmp(&in_ie[cnt + 2], wps_oui, 4))) {
0406             memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
0407             *wps_ielen = in_ie[cnt + 1] + 2;
0408             cnt += in_ie[cnt + 1] + 2;
0409             match = true;
0410             break;
0411         }
0412         cnt += in_ie[cnt + 1] + 2; /* goto next */
0413     }
0414     return match;
0415 }