]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/rtl8712/ieee80211.c
Merge tag 'pstore-v4.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[linux.git] / drivers / staging / rtl8712 / ieee80211.c
1 /******************************************************************************
2  * ieee80211.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * Modifications for inclusion into the Linux staging tree are
17  * Copyright(c) 2010 Larry Finger. All rights reserved.
18  *
19  * Contact information:
20  * WLAN FAE <wlanfae@realtek.com>.
21  * Larry Finger <Larry.Finger@lwfinger.net>
22  *
23  ******************************************************************************/
24
25 #define _IEEE80211_C
26
27 #include "drv_types.h"
28 #include "ieee80211.h"
29 #include "wifi.h"
30 #include "osdep_service.h"
31 #include "wlan_bssdef.h"
32
33 static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
34 static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
35 static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
36 static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
37 static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
38 static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
39
40 static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
41 static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
42 static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
43 static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
44 static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
45
46 /*-----------------------------------------------------------
47  * for adhoc-master to generate ie and provide supported-rate to fw
48  *-----------------------------------------------------------
49  */
50
51 static u8 WIFI_CCKRATES[] =  {
52         (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
53         (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
54         (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
55         (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
56 };
57
58 static u8 WIFI_OFDMRATES[] = {
59         (IEEE80211_OFDM_RATE_6MB),
60         (IEEE80211_OFDM_RATE_9MB),
61         (IEEE80211_OFDM_RATE_12MB),
62         (IEEE80211_OFDM_RATE_18MB),
63         (IEEE80211_OFDM_RATE_24MB),
64         (IEEE80211_OFDM_RATE_36MB),
65         (IEEE80211_OFDM_RATE_48MB),
66         (IEEE80211_OFDM_RATE_54MB)
67 };
68
69 uint r8712_is_cckrates_included(u8 *rate)
70 {
71         u32 i = 0;
72
73         while (rate[i] != 0) {
74                 if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
75                     (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
76                         return true;
77                 i++;
78         }
79         return false;
80 }
81
82 uint r8712_is_cckratesonly_included(u8 *rate)
83 {
84         u32 i = 0;
85
86         while (rate[i] != 0) {
87                 if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
88                     (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
89                         return false;
90                 i++;
91         }
92         return true;
93 }
94
95 /* r8712_set_ie will update frame length */
96 u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
97 {
98         *pbuf = (u8)index;
99         *(pbuf + 1) = (u8)len;
100         if (len > 0)
101                 memcpy((void *)(pbuf + 2), (void *)source, len);
102         *frlen = *frlen + (len + 2);
103         return pbuf + len + 2;
104 }
105
106 /* ---------------------------------------------------------------------------
107  * index: the information element id index, limit is the limit for search
108  * ---------------------------------------------------------------------------
109  */
110 u8 *r8712_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
111 {
112         sint tmp, i;
113         u8 *p;
114
115         if (limit < 1)
116                 return NULL;
117         p = pbuf;
118         i = 0;
119         *len = 0;
120         while (1) {
121                 if (*p == index) {
122                         *len = *(p + 1);
123                         return p;
124                 }
125                 tmp = *(p + 1);
126                 p += (tmp + 2);
127                 i += (tmp + 2);
128                 if (i >= limit)
129                         break;
130         }
131         return NULL;
132 }
133
134 static void set_supported_rate(u8 *rates, uint mode)
135 {
136         memset(rates, 0, NDIS_802_11_LENGTH_RATES_EX);
137         switch (mode) {
138         case WIRELESS_11B:
139                 memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
140                 break;
141         case WIRELESS_11G:
142         case WIRELESS_11A:
143                 memcpy(rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
144                 break;
145         case WIRELESS_11BG:
146                 memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
147                 memcpy(rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
148                        IEEE80211_NUM_OFDM_RATESLEN);
149                 break;
150         }
151 }
152
153 static uint r8712_get_rateset_len(u8 *rateset)
154 {
155         uint i = 0;
156
157         while (1) {
158                 if ((rateset[i]) == 0)
159                         break;
160                 if (i > 12)
161                         break;
162                 i++;
163         }
164         return i;
165 }
166
167 int r8712_generate_ie(struct registry_priv *pregistrypriv)
168 {
169         int sz = 0, rate_len;
170         struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
171         u8 *ie = pdev_network->IEs;
172         u16 beaconPeriod = (u16)pdev_network->Configuration.BeaconPeriod;
173
174         /*timestamp will be inserted by hardware*/
175         sz += 8;
176         ie += sz;
177         /*beacon interval : 2bytes*/
178         *(__le16 *)ie = cpu_to_le16(beaconPeriod);
179         sz += 2;
180         ie += 2;
181         /*capability info*/
182         *(u16 *)ie = 0;
183         *(__le16 *)ie |= cpu_to_le16(cap_IBSS);
184         if (pregistrypriv->preamble == PREAMBLE_SHORT)
185                 *(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
186         if (pdev_network->Privacy)
187                 *(__le16 *)ie |= cpu_to_le16(cap_Privacy);
188         sz += 2;
189         ie += 2;
190         /*SSID*/
191         ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
192                           pdev_network->Ssid.Ssid, &sz);
193         /*supported rates*/
194         set_supported_rate(pdev_network->rates, pregistrypriv->wireless_mode);
195         rate_len = r8712_get_rateset_len(pdev_network->rates);
196         if (rate_len > 8) {
197                 ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
198                                   pdev_network->rates, &sz);
199                 ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8),
200                                   (pdev_network->rates + 8), &sz);
201         } else {
202                 ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
203                                   rate_len, pdev_network->rates, &sz);
204         }
205         /*DS parameter set*/
206         ie = r8712_set_ie(ie, _DSSET_IE_, 1,
207                           (u8 *)&pdev_network->Configuration.DSConfig, &sz);
208         /*IBSS Parameter Set*/
209         ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
210                           (u8 *)&pdev_network->Configuration.ATIMWindow, &sz);
211         return sz;
212 }
213
214 unsigned char *r8712_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
215 {
216         int len;
217         u16 val16;
218         unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
219         u8 *pbuf = pie;
220
221         while (1) {
222                 pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
223                 if (pbuf) {
224                         /*check if oui matches...*/
225                         if (memcmp((pbuf + 2), wpa_oui_type,
226                                    sizeof(wpa_oui_type)))
227                                 goto check_next_ie;
228                         /*check version...*/
229                         memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
230                         le16_to_cpus(&val16);
231                         if (val16 != 0x0001)
232                                 goto check_next_ie;
233                         *wpa_ie_len = *(pbuf + 1);
234                         return pbuf;
235                 }
236                 *wpa_ie_len = 0;
237                 return NULL;
238 check_next_ie:
239                 limit = limit - (pbuf - pie) - 2 - len;
240                 if (limit <= 0)
241                         break;
242                 pbuf += (2 + len);
243         }
244         *wpa_ie_len = 0;
245         return NULL;
246 }
247
248 unsigned char *r8712_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
249 {
250         return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
251 }
252
253 static int r8712_get_wpa_cipher_suite(u8 *s)
254 {
255         if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
256                 return WPA_CIPHER_NONE;
257         if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
258                 return WPA_CIPHER_WEP40;
259         if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
260                 return WPA_CIPHER_TKIP;
261         if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
262                 return WPA_CIPHER_CCMP;
263         if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
264                 return WPA_CIPHER_WEP104;
265         return 0;
266 }
267
268 static int r8712_get_wpa2_cipher_suite(u8 *s)
269 {
270         if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
271                 return WPA_CIPHER_NONE;
272         if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
273                 return WPA_CIPHER_WEP40;
274         if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
275                 return WPA_CIPHER_TKIP;
276         if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
277                 return WPA_CIPHER_CCMP;
278         if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
279                 return WPA_CIPHER_WEP104;
280         return 0;
281 }
282
283 int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
284                        int *pairwise_cipher)
285 {
286         int i;
287         int left, count;
288         u8 *pos;
289
290         if (wpa_ie_len <= 0) {
291                 /* No WPA IE - fail silently */
292                 return _FAIL;
293         }
294         if ((*wpa_ie != _WPA_IE_ID_) ||
295             (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
296             (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
297                 return _FAIL;
298         pos = wpa_ie;
299         pos += 8;
300         left = wpa_ie_len - 8;
301         /*group_cipher*/
302         if (left >= WPA_SELECTOR_LEN) {
303                 *group_cipher = r8712_get_wpa_cipher_suite(pos);
304                 pos += WPA_SELECTOR_LEN;
305                 left -= WPA_SELECTOR_LEN;
306         } else if (left > 0) {
307                 return _FAIL;
308         }
309         /*pairwise_cipher*/
310         if (left >= 2) {
311                 count = le16_to_cpu(*(__le16 *)pos);
312                 pos += 2;
313                 left -= 2;
314                 if (count == 0 || left < count * WPA_SELECTOR_LEN)
315                         return _FAIL;
316                 for (i = 0; i < count; i++) {
317                         *pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
318                         pos += WPA_SELECTOR_LEN;
319                         left -= WPA_SELECTOR_LEN;
320                 }
321         } else if (left == 1) {
322                 return _FAIL;
323         }
324         return _SUCCESS;
325 }
326
327 int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
328                         int *pairwise_cipher)
329 {
330         int i;
331         int left, count;
332         u8 *pos;
333
334         if (rsn_ie_len <= 0) {
335                 /* No RSN IE - fail silently */
336                 return _FAIL;
337         }
338         if ((*rsn_ie != _WPA2_IE_ID_) ||
339             (*(rsn_ie + 1) != (u8)(rsn_ie_len - 2)))
340                 return _FAIL;
341         pos = rsn_ie;
342         pos += 4;
343         left = rsn_ie_len - 4;
344         /*group_cipher*/
345         if (left >= RSN_SELECTOR_LEN) {
346                 *group_cipher = r8712_get_wpa2_cipher_suite(pos);
347                 pos += RSN_SELECTOR_LEN;
348                 left -= RSN_SELECTOR_LEN;
349         } else if (left > 0) {
350                 return _FAIL;
351         }
352         /*pairwise_cipher*/
353         if (left >= 2) {
354                 count = le16_to_cpu(*(__le16 *)pos);
355                 pos += 2;
356                 left -= 2;
357                 if (count == 0 || left < count * RSN_SELECTOR_LEN)
358                         return _FAIL;
359                 for (i = 0; i < count; i++) {
360                         *pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
361                         pos += RSN_SELECTOR_LEN;
362                         left -= RSN_SELECTOR_LEN;
363                 }
364         } else if (left == 1) {
365                 return _FAIL;
366         }
367         return _SUCCESS;
368 }
369
370 int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
371                      u8 *wpa_ie, u16 *wpa_len)
372 {
373         u8 authmode;
374         u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
375         uint cnt;
376
377         /*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
378         cnt = _TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_;
379         while (cnt < in_len) {
380                 authmode = in_ie[cnt];
381                 if ((authmode == _WPA_IE_ID_) &&
382                     (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
383                         memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
384                         *wpa_len = in_ie[cnt + 1] + 2;
385                         cnt += in_ie[cnt + 1] + 2;  /*get next */
386                 } else {
387                         if (authmode == _WPA2_IE_ID_) {
388                                 memcpy(rsn_ie, &in_ie[cnt],
389                                        in_ie[cnt + 1] + 2);
390                                 *rsn_len = in_ie[cnt + 1] + 2;
391                                 cnt += in_ie[cnt + 1] + 2;  /*get next*/
392                         } else {
393                                 cnt += in_ie[cnt + 1] + 2;   /*get next*/
394                         }
395                 }
396         }
397         return *rsn_len + *wpa_len;
398 }
399
400 int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
401 {
402         int match;
403         uint cnt;
404         u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
405
406         cnt = 12;
407         match = false;
408         while (cnt < in_len) {
409                 eid = in_ie[cnt];
410                 if ((eid == _WPA_IE_ID_) &&
411                     (!memcmp(&in_ie[cnt + 2], wps_oui, 4))) {
412                         memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
413                         *wps_ielen = in_ie[cnt + 1] + 2;
414                         cnt += in_ie[cnt + 1] + 2;
415                         match = true;
416                         break;
417                 }
418                         cnt += in_ie[cnt + 1] + 2; /* goto next */
419         }
420         return match;
421 }