Merge tag 'platform-drivers-x86-v5.12-1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / staging / rtl8192e / rtllib_wx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2004 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are based on the WEP enablement code provided by the
6  * Host AP project hostap-drivers v0.1.3
7  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
8  * <jkmaline@cc.hut.fi>
9  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
10  *
11  * Contact Information:
12  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
13  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
14  */
15 #include <linux/wireless.h>
16 #include <linux/kmod.h>
17 #include <linux/module.h>
18 #include <linux/etherdevice.h>
19 #include "rtllib.h"
20 struct modes_unit {
21         char *mode_string;
22         int mode_size;
23 };
24 static struct modes_unit rtllib_modes[] = {
25         {"a", 1},
26         {"b", 1},
27         {"g", 1},
28         {"?", 1},
29         {"N-24G", 5},
30         {"N-5G", 4},
31 };
32
33 #define MAX_CUSTOM_LEN 64
34 static inline char *rtl819x_translate_scan(struct rtllib_device *ieee,
35                                            char *start, char *stop,
36                                            struct rtllib_network *network,
37                                            struct iw_request_info *info)
38 {
39         char custom[MAX_CUSTOM_LEN];
40         char proto_name[IFNAMSIZ];
41         char *pname = proto_name;
42         char *p;
43         struct iw_event iwe;
44         int i, j;
45         u16 max_rate, rate;
46         static u8       EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};
47
48         /* First entry *MUST* be the AP MAC address */
49         iwe.cmd = SIOCGIWAP;
50         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
51         ether_addr_copy(iwe.u.ap_addr.sa_data, network->bssid);
52         start = iwe_stream_add_event_rsl(info, start, stop,
53                                          &iwe, IW_EV_ADDR_LEN);
54         /* Remaining entries will be displayed in the order we provide them */
55
56         /* Add the ESSID */
57         iwe.cmd = SIOCGIWESSID;
58         iwe.u.data.flags = 1;
59         if (network->ssid_len > 0) {
60                 iwe.u.data.length = min_t(u8, network->ssid_len, 32);
61                 start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
62                                                  network->ssid);
63         } else if (network->hidden_ssid_len == 0) {
64                 iwe.u.data.length = sizeof("<hidden>");
65                 start = iwe_stream_add_point_rsl(info, start, stop,
66                                                  &iwe, "<hidden>");
67         } else {
68                 iwe.u.data.length = min_t(u8, network->hidden_ssid_len, 32);
69                 start = iwe_stream_add_point_rsl(info, start, stop, &iwe,
70                                                  network->hidden_ssid);
71         }
72         /* Add the protocol name */
73         iwe.cmd = SIOCGIWNAME;
74         for (i = 0; i < ARRAY_SIZE(rtllib_modes); i++) {
75                 if (network->mode&(1<<i)) {
76                         sprintf(pname, rtllib_modes[i].mode_string,
77                                 rtllib_modes[i].mode_size);
78                         pname += rtllib_modes[i].mode_size;
79                 }
80         }
81         *pname = '\0';
82         snprintf(iwe.u.name, IFNAMSIZ, "IEEE802.11%s", proto_name);
83         start = iwe_stream_add_event_rsl(info, start, stop,
84                                          &iwe, IW_EV_CHAR_LEN);
85         /* Add mode */
86         iwe.cmd = SIOCGIWMODE;
87         if (network->capability &
88             (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
89                 if (network->capability & WLAN_CAPABILITY_ESS)
90                         iwe.u.mode = IW_MODE_MASTER;
91                 else
92                         iwe.u.mode = IW_MODE_ADHOC;
93                 start = iwe_stream_add_event_rsl(info, start, stop,
94                                                  &iwe, IW_EV_UINT_LEN);
95         }
96
97         /* Add frequency/channel */
98         iwe.cmd = SIOCGIWFREQ;
99         iwe.u.freq.m = network->channel;
100         iwe.u.freq.e = 0;
101         iwe.u.freq.i = 0;
102         start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
103                                          IW_EV_FREQ_LEN);
104
105         /* Add encryption capability */
106         iwe.cmd = SIOCGIWENCODE;
107         if (network->capability & WLAN_CAPABILITY_PRIVACY)
108                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
109         else
110                 iwe.u.data.flags = IW_ENCODE_DISABLED;
111         iwe.u.data.length = 0;
112         start = iwe_stream_add_point_rsl(info, start, stop,
113                                          &iwe, network->ssid);
114         /* Add basic and extended rates */
115         max_rate = 0;
116         p = custom;
117         p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
118         for (i = 0, j = 0; i < network->rates_len;) {
119                 if (j < network->rates_ex_len &&
120                     ((network->rates_ex[j] & 0x7F) <
121                      (network->rates[i] & 0x7F)))
122                         rate = network->rates_ex[j++] & 0x7F;
123                 else
124                         rate = network->rates[i++] & 0x7F;
125                 if (rate > max_rate)
126                         max_rate = rate;
127                 p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
128                               "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
129         }
130         for (; j < network->rates_ex_len; j++) {
131                 rate = network->rates_ex[j] & 0x7F;
132                 p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
133                               "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
134                 if (rate > max_rate)
135                         max_rate = rate;
136         }
137
138         if (network->mode >= IEEE_N_24G) {
139                 struct ht_capab_ele *ht_cap = NULL;
140                 bool is40M = false, isShortGI = false;
141                 u8 max_mcs = 0;
142
143                 if (!memcmp(network->bssht.bdHTCapBuf, EWC11NHTCap, 4))
144                         ht_cap = (struct ht_capab_ele *)
145                                  &network->bssht.bdHTCapBuf[4];
146                 else
147                         ht_cap = (struct ht_capab_ele *)
148                                  &network->bssht.bdHTCapBuf[0];
149                 is40M = (ht_cap->ChlWidth) ? 1 : 0;
150                 isShortGI = (ht_cap->ChlWidth) ?
151                                 ((ht_cap->ShortGI40Mhz) ? 1 : 0) :
152                                 ((ht_cap->ShortGI20Mhz) ? 1 : 0);
153
154                 max_mcs = HTGetHighestMCSRate(ieee, ht_cap->MCS,
155                                               MCS_FILTER_ALL);
156                 rate = MCS_DATA_RATE[is40M][isShortGI][max_mcs & 0x7f];
157                 if (rate > max_rate)
158                         max_rate = rate;
159         }
160         iwe.cmd = SIOCGIWRATE;
161         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
162         iwe.u.bitrate.value = max_rate * 500000;
163         start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
164                                      IW_EV_PARAM_LEN);
165         iwe.cmd = IWEVCUSTOM;
166         iwe.u.data.length = p - custom;
167         if (iwe.u.data.length)
168                 start = iwe_stream_add_point_rsl(info, start, stop,
169                                                  &iwe, custom);
170         /* Add quality statistics */
171         /* TODO: Fix these values... */
172         iwe.cmd = IWEVQUAL;
173         iwe.u.qual.qual = network->stats.signal;
174         iwe.u.qual.level = network->stats.rssi;
175         iwe.u.qual.noise = network->stats.noise;
176         iwe.u.qual.updated = network->stats.mask & RTLLIB_STATMASK_WEMASK;
177         if (!(network->stats.mask & RTLLIB_STATMASK_RSSI))
178                 iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
179         if (!(network->stats.mask & RTLLIB_STATMASK_NOISE))
180                 iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
181         if (!(network->stats.mask & RTLLIB_STATMASK_SIGNAL))
182                 iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
183         iwe.u.qual.updated = 7;
184         start = iwe_stream_add_event_rsl(info, start, stop, &iwe,
185                                          IW_EV_QUAL_LEN);
186
187         iwe.cmd = IWEVCUSTOM;
188         p = custom;
189         iwe.u.data.length = p - custom;
190         if (iwe.u.data.length)
191                 start = iwe_stream_add_point_rsl(info, start, stop,
192                                                  &iwe, custom);
193
194         memset(&iwe, 0, sizeof(iwe));
195         if (network->wpa_ie_len) {
196                 char buf[MAX_WPA_IE_LEN];
197
198                 memcpy(buf, network->wpa_ie, network->wpa_ie_len);
199                 iwe.cmd = IWEVGENIE;
200                 iwe.u.data.length = network->wpa_ie_len;
201                 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
202         }
203         memset(&iwe, 0, sizeof(iwe));
204         if (network->rsn_ie_len) {
205                 char buf[MAX_WPA_IE_LEN];
206
207                 memcpy(buf, network->rsn_ie, network->rsn_ie_len);
208                 iwe.cmd = IWEVGENIE;
209                 iwe.u.data.length = network->rsn_ie_len;
210                 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
211         }
212
213         /* add info for WZC */
214         memset(&iwe, 0, sizeof(iwe));
215         if (network->wzc_ie_len) {
216                 char buf[MAX_WZC_IE_LEN];
217
218                 memcpy(buf, network->wzc_ie, network->wzc_ie_len);
219                 iwe.cmd = IWEVGENIE;
220                 iwe.u.data.length = network->wzc_ie_len;
221                 start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
222         }
223
224         /* Add EXTRA: Age to display seconds since last beacon/probe response
225          * for given network.
226          */
227         iwe.cmd = IWEVCUSTOM;
228         p = custom;
229         p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
230                       " Last beacon: %lums ago",
231                       (jiffies - network->last_scanned) / (HZ / 100));
232         iwe.u.data.length = p - custom;
233         if (iwe.u.data.length)
234                 start = iwe_stream_add_point_rsl(info, start, stop,
235                                                  &iwe, custom);
236
237         return start;
238 }
239
240 int rtllib_wx_get_scan(struct rtllib_device *ieee,
241                           struct iw_request_info *info,
242                           union iwreq_data *wrqu, char *extra)
243 {
244         struct rtllib_network *network;
245         unsigned long flags;
246
247         char *ev = extra;
248         char *stop = ev + wrqu->data.length;
249         int i = 0;
250         int err = 0;
251
252         netdev_dbg(ieee->dev, "Getting scan\n");
253         mutex_lock(&ieee->wx_mutex);
254         spin_lock_irqsave(&ieee->lock, flags);
255
256         list_for_each_entry(network, &ieee->network_list, list) {
257                 i++;
258                 if ((stop - ev) < 200) {
259                         err = -E2BIG;
260                         break;
261                 }
262                 if (ieee->scan_age == 0 ||
263                     time_after(network->last_scanned + ieee->scan_age, jiffies))
264                         ev = rtl819x_translate_scan(ieee, ev, stop, network,
265                                                     info);
266                 else
267                         netdev_dbg(ieee->dev,
268                                    "Network '%s ( %pM)' hidden due to age (%lums).\n",
269                                    escape_essid(network->ssid,
270                                                 network->ssid_len),
271                                    network->bssid,
272                                    (jiffies - network->last_scanned) /
273                                    (HZ / 100));
274         }
275
276         spin_unlock_irqrestore(&ieee->lock, flags);
277         mutex_unlock(&ieee->wx_mutex);
278         wrqu->data.length = ev -  extra;
279         wrqu->data.flags = 0;
280
281         netdev_dbg(ieee->dev, "%s(): %d networks returned.\n", __func__, i);
282
283         return err;
284 }
285 EXPORT_SYMBOL(rtllib_wx_get_scan);
286
287 int rtllib_wx_set_encode(struct rtllib_device *ieee,
288                             struct iw_request_info *info,
289                             union iwreq_data *wrqu, char *keybuf)
290 {
291         struct iw_point *erq = &(wrqu->encoding);
292         struct net_device *dev = ieee->dev;
293         struct rtllib_security sec = {
294                 .flags = 0
295         };
296         int i, key, key_provided, len;
297         struct lib80211_crypt_data **crypt;
298
299         netdev_dbg(ieee->dev, "%s()\n", __func__);
300
301         key = erq->flags & IW_ENCODE_INDEX;
302         if (key) {
303                 if (key > NUM_WEP_KEYS)
304                         return -EINVAL;
305                 key--;
306                 key_provided = 1;
307         } else {
308                 key_provided = 0;
309                 key = ieee->crypt_info.tx_keyidx;
310         }
311
312         netdev_dbg(ieee->dev, "Key: %d [%s]\n", key, key_provided ?
313                            "provided" : "default");
314         crypt = &ieee->crypt_info.crypt[key];
315         if (erq->flags & IW_ENCODE_DISABLED) {
316                 if (key_provided && *crypt) {
317                         netdev_dbg(ieee->dev,
318                                    "Disabling encryption on key %d.\n", key);
319                         lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
320                 } else
321                         netdev_dbg(ieee->dev, "Disabling encryption.\n");
322
323                 /* Check all the keys to see if any are still configured,
324                  * and if no key index was provided, de-init them all
325                  */
326                 for (i = 0; i < NUM_WEP_KEYS; i++) {
327                         if (ieee->crypt_info.crypt[i] != NULL) {
328                                 if (key_provided)
329                                         break;
330                                 lib80211_crypt_delayed_deinit(&ieee->crypt_info,
331                                                     &ieee->crypt_info.crypt[i]);
332                         }
333                 }
334
335                 if (i == NUM_WEP_KEYS) {
336                         sec.enabled = 0;
337                         sec.level = SEC_LEVEL_0;
338                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
339                 }
340
341                 goto done;
342         }
343
344         sec.enabled = 1;
345         sec.flags |= SEC_ENABLED;
346
347         if (*crypt != NULL && (*crypt)->ops != NULL &&
348             strcmp((*crypt)->ops->name, "R-WEP") != 0) {
349                 /* changing to use WEP; deinit previously used algorithm
350                  * on this key
351                  */
352                 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
353         }
354
355         if (*crypt == NULL) {
356                 struct lib80211_crypt_data *new_crypt;
357
358                 /* take WEP into use */
359                 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
360                 if (new_crypt == NULL)
361                         return -ENOMEM;
362                 new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
363                 if (!new_crypt->ops) {
364                         request_module("rtllib_crypt_wep");
365                         new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
366                 }
367
368                 if (new_crypt->ops)
369                         new_crypt->priv = new_crypt->ops->init(key);
370
371                 if (!new_crypt->ops || !new_crypt->priv) {
372                         kfree(new_crypt);
373                         new_crypt = NULL;
374
375                         netdev_warn(dev,
376                                     "%s: could not initialize WEP: load module rtllib_crypt_wep\n",
377                                     dev->name);
378                         return -EOPNOTSUPP;
379                 }
380                 *crypt = new_crypt;
381         }
382
383         /* If a new key was provided, set it up */
384         if (erq->length > 0) {
385                 len = erq->length <= 5 ? 5 : 13;
386                 memcpy(sec.keys[key], keybuf, erq->length);
387                 if (len > erq->length)
388                         memset(sec.keys[key] + erq->length, 0,
389                                len - erq->length);
390                 netdev_dbg(ieee->dev, "Setting key %d to '%s' (%d:%d bytes)\n",
391                            key, escape_essid(sec.keys[key], len), erq->length,
392                            len);
393                 sec.key_sizes[key] = len;
394                 (*crypt)->ops->set_key(sec.keys[key], len, NULL,
395                                        (*crypt)->priv);
396                 sec.flags |= (1 << key);
397                 /* This ensures a key will be activated if no key is
398                  * explicitly set
399                  */
400                 if (key == sec.active_key)
401                         sec.flags |= SEC_ACTIVE_KEY;
402                 ieee->crypt_info.tx_keyidx = key;
403
404         } else {
405                 len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
406                                              NULL, (*crypt)->priv);
407                 if (len == 0) {
408                         /* Set a default key of all 0 */
409                         netdev_info(ieee->dev, "Setting key %d to all zero.\n",
410                                            key);
411
412                         memset(sec.keys[key], 0, 13);
413                         (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
414                                                (*crypt)->priv);
415                         sec.key_sizes[key] = 13;
416                         sec.flags |= (1 << key);
417                 }
418
419                 /* No key data - just set the default TX key index */
420                 if (key_provided) {
421                         netdev_dbg(ieee->dev,
422                                    "Setting key %d as default Tx key.\n", key);
423                         ieee->crypt_info.tx_keyidx = key;
424                         sec.active_key = key;
425                         sec.flags |= SEC_ACTIVE_KEY;
426                 }
427         }
428  done:
429         ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
430         ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
431                           WLAN_AUTH_SHARED_KEY;
432         sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
433         sec.flags |= SEC_AUTH_MODE;
434         netdev_dbg(ieee->dev, "Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
435                            "OPEN" : "SHARED KEY");
436
437         /* For now we just support WEP, so only set that security level...
438          * TODO: When WPA is added this is one place that needs to change
439          */
440         sec.flags |= SEC_LEVEL;
441         sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
442
443         if (ieee->set_security)
444                 ieee->set_security(dev, &sec);
445
446         /* Do not reset port if card is in Managed mode since resetting will
447          * generate new IEEE 802.11 authentication which may end up in looping
448          * with IEEE 802.1X.  If your hardware requires a reset after WEP
449          * configuration (for example... Prism2), implement the reset_port in
450          * the callbacks structures used to initialize the 802.11 stack.
451          */
452         if (ieee->reset_on_keychange &&
453             ieee->iw_mode != IW_MODE_INFRA &&
454             ieee->reset_port && ieee->reset_port(dev)) {
455                 netdev_dbg(dev, "%s: reset_port failed\n", dev->name);
456                 return -EINVAL;
457         }
458         return 0;
459 }
460 EXPORT_SYMBOL(rtllib_wx_set_encode);
461
462 int rtllib_wx_get_encode(struct rtllib_device *ieee,
463                             struct iw_request_info *info,
464                             union iwreq_data *wrqu, char *keybuf)
465 {
466         struct iw_point *erq = &(wrqu->encoding);
467         int len, key;
468         struct lib80211_crypt_data *crypt;
469
470         netdev_dbg(ieee->dev, "%s()\n", __func__);
471
472         if (ieee->iw_mode == IW_MODE_MONITOR)
473                 return -1;
474
475         key = erq->flags & IW_ENCODE_INDEX;
476         if (key) {
477                 if (key > NUM_WEP_KEYS)
478                         return -EINVAL;
479                 key--;
480         } else {
481                 key = ieee->crypt_info.tx_keyidx;
482         }
483         crypt = ieee->crypt_info.crypt[key];
484
485         erq->flags = key + 1;
486
487         if (crypt == NULL || crypt->ops == NULL) {
488                 erq->length = 0;
489                 erq->flags |= IW_ENCODE_DISABLED;
490                 return 0;
491         }
492         len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
493
494         erq->length = max(len, 0);
495
496         erq->flags |= IW_ENCODE_ENABLED;
497
498         if (ieee->open_wep)
499                 erq->flags |= IW_ENCODE_OPEN;
500         else
501                 erq->flags |= IW_ENCODE_RESTRICTED;
502
503         return 0;
504 }
505 EXPORT_SYMBOL(rtllib_wx_get_encode);
506
507 int rtllib_wx_set_encode_ext(struct rtllib_device *ieee,
508                                struct iw_request_info *info,
509                                union iwreq_data *wrqu, char *extra)
510 {
511         int ret = 0;
512         struct net_device *dev = ieee->dev;
513         struct iw_point *encoding = &wrqu->encoding;
514         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
515         int i, idx;
516         int group_key = 0;
517         const char *alg, *module;
518         struct lib80211_crypto_ops *ops;
519         struct lib80211_crypt_data **crypt;
520
521         struct rtllib_security sec = {
522                 .flags = 0,
523         };
524         idx = encoding->flags & IW_ENCODE_INDEX;
525         if (idx) {
526                 if (idx < 1 || idx > NUM_WEP_KEYS)
527                         return -EINVAL;
528                 idx--;
529         } else {
530                 idx = ieee->crypt_info.tx_keyidx;
531         }
532         if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
533                 crypt = &ieee->crypt_info.crypt[idx];
534                 group_key = 1;
535         } else {
536                 /* some Cisco APs use idx>0 for unicast in dynamic WEP */
537                 if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
538                         return -EINVAL;
539                 if (ieee->iw_mode == IW_MODE_INFRA)
540                         crypt = &ieee->crypt_info.crypt[idx];
541                 else
542                         return -EINVAL;
543         }
544
545         sec.flags |= SEC_ENABLED;
546         if ((encoding->flags & IW_ENCODE_DISABLED) ||
547             ext->alg == IW_ENCODE_ALG_NONE) {
548                 if (*crypt)
549                         lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
550
551                 for (i = 0; i < NUM_WEP_KEYS; i++) {
552                         if (ieee->crypt_info.crypt[i] != NULL)
553                                 break;
554                 }
555                 if (i == NUM_WEP_KEYS) {
556                         sec.enabled = 0;
557                         sec.level = SEC_LEVEL_0;
558                         sec.flags |= SEC_LEVEL;
559                 }
560                 goto done;
561         }
562
563         sec.enabled = 1;
564         switch (ext->alg) {
565         case IW_ENCODE_ALG_WEP:
566                 alg = "R-WEP";
567                 module = "rtllib_crypt_wep";
568                 break;
569         case IW_ENCODE_ALG_TKIP:
570                 alg = "R-TKIP";
571                 module = "rtllib_crypt_tkip";
572                 break;
573         case IW_ENCODE_ALG_CCMP:
574                 alg = "R-CCMP";
575                 module = "rtllib_crypt_ccmp";
576                 break;
577         default:
578                 netdev_dbg(ieee->dev, "Unknown crypto alg %d\n", ext->alg);
579                 ret = -EINVAL;
580                 goto done;
581         }
582         netdev_dbg(dev, "alg name:%s\n", alg);
583
584         ops = lib80211_get_crypto_ops(alg);
585         if (ops == NULL) {
586                 char tempbuf[100];
587
588                 memset(tempbuf, 0x00, 100);
589                 sprintf(tempbuf, "%s", module);
590                 request_module("%s", tempbuf);
591                 ops = lib80211_get_crypto_ops(alg);
592         }
593         if (ops == NULL) {
594                 netdev_info(dev, "========>unknown crypto alg %d\n", ext->alg);
595                 ret = -EINVAL;
596                 goto done;
597         }
598
599         if (*crypt == NULL || (*crypt)->ops != ops) {
600                 struct lib80211_crypt_data *new_crypt;
601
602                 lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
603
604                 new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
605                 if (new_crypt == NULL) {
606                         ret = -ENOMEM;
607                         goto done;
608                 }
609                 new_crypt->ops = ops;
610                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
611                         new_crypt->priv = new_crypt->ops->init(idx);
612
613                 if (new_crypt->priv == NULL) {
614                         kfree(new_crypt);
615                         ret = -EINVAL;
616                         goto done;
617                 }
618                 *crypt = new_crypt;
619
620         }
621
622         if (ext->key_len > 0 && (*crypt)->ops->set_key &&
623             (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
624                                    (*crypt)->priv) < 0) {
625                 netdev_info(dev, "key setting failed\n");
626                 ret = -EINVAL;
627                 goto done;
628         }
629         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
630                 ieee->crypt_info.tx_keyidx = idx;
631                 sec.active_key = idx;
632                 sec.flags |= SEC_ACTIVE_KEY;
633         }
634         if (ext->alg != IW_ENCODE_ALG_NONE) {
635                 sec.key_sizes[idx] = ext->key_len;
636                 sec.flags |= (1 << idx);
637                 if (ext->alg == IW_ENCODE_ALG_WEP) {
638                         sec.flags |= SEC_LEVEL;
639                         sec.level = SEC_LEVEL_1;
640                 } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
641                         sec.flags |= SEC_LEVEL;
642                         sec.level = SEC_LEVEL_2;
643                 } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
644                         sec.flags |= SEC_LEVEL;
645                         sec.level = SEC_LEVEL_3;
646                 }
647                 /* Don't set sec level for group keys. */
648                 if (group_key)
649                         sec.flags &= ~SEC_LEVEL;
650         }
651 done:
652         if (ieee->set_security)
653                 ieee->set_security(ieee->dev, &sec);
654
655         if (ieee->reset_on_keychange &&
656             ieee->iw_mode != IW_MODE_INFRA &&
657             ieee->reset_port && ieee->reset_port(dev)) {
658                 netdev_dbg(ieee->dev, "Port reset failed\n");
659                 return -EINVAL;
660         }
661         return ret;
662 }
663 EXPORT_SYMBOL(rtllib_wx_set_encode_ext);
664
665 int rtllib_wx_set_mlme(struct rtllib_device *ieee,
666                                struct iw_request_info *info,
667                                union iwreq_data *wrqu, char *extra)
668 {
669         u8 i = 0;
670         bool deauth = false;
671         struct iw_mlme *mlme = (struct iw_mlme *) extra;
672
673         if (ieee->state != RTLLIB_LINKED)
674                 return -ENOLINK;
675
676         mutex_lock(&ieee->wx_mutex);
677
678         switch (mlme->cmd) {
679         case IW_MLME_DEAUTH:
680                 deauth = true;
681                 fallthrough;
682         case IW_MLME_DISASSOC:
683                 if (deauth)
684                         netdev_info(ieee->dev, "disauth packet !\n");
685                 else
686                         netdev_info(ieee->dev, "dis associate packet!\n");
687
688                 ieee->cannot_notify = true;
689
690                 SendDisassociation(ieee, deauth, mlme->reason_code);
691                 rtllib_disassociate(ieee);
692
693                 ieee->wap_set = 0;
694                 for (i = 0; i < 6; i++)
695                         ieee->current_network.bssid[i] = 0x55;
696
697                 ieee->ssid_set = 0;
698                 ieee->current_network.ssid[0] = '\0';
699                 ieee->current_network.ssid_len = 0;
700                 break;
701         default:
702                 mutex_unlock(&ieee->wx_mutex);
703                 return -EOPNOTSUPP;
704         }
705
706         mutex_unlock(&ieee->wx_mutex);
707
708         return 0;
709 }
710 EXPORT_SYMBOL(rtllib_wx_set_mlme);
711
712 int rtllib_wx_set_auth(struct rtllib_device *ieee,
713                                struct iw_request_info *info,
714                                struct iw_param *data, char *extra)
715 {
716         switch (data->flags & IW_AUTH_INDEX) {
717         case IW_AUTH_WPA_VERSION:
718                 break;
719         case IW_AUTH_CIPHER_PAIRWISE:
720         case IW_AUTH_CIPHER_GROUP:
721         case IW_AUTH_KEY_MGMT:
722                 /* Host AP driver does not use these parameters and allows
723                  * wpa_supplicant to control them internally.
724                  */
725                 break;
726         case IW_AUTH_TKIP_COUNTERMEASURES:
727                 ieee->tkip_countermeasures = data->value;
728                 break;
729         case IW_AUTH_DROP_UNENCRYPTED:
730                 ieee->drop_unencrypted = data->value;
731                 break;
732
733         case IW_AUTH_80211_AUTH_ALG:
734                 if (data->value & IW_AUTH_ALG_SHARED_KEY) {
735                         ieee->open_wep = 0;
736                         ieee->auth_mode = 1;
737                 } else if (data->value & IW_AUTH_ALG_OPEN_SYSTEM) {
738                         ieee->open_wep = 1;
739                         ieee->auth_mode = 0;
740                 } else if (data->value & IW_AUTH_ALG_LEAP) {
741                         ieee->open_wep = 1;
742                         ieee->auth_mode = 2;
743                 } else
744                         return -EINVAL;
745                 break;
746
747         case IW_AUTH_WPA_ENABLED:
748                 ieee->wpa_enabled = (data->value) ? 1 : 0;
749                 break;
750
751         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
752                 ieee->ieee802_1x = data->value;
753                 break;
754         case IW_AUTH_PRIVACY_INVOKED:
755                 ieee->privacy_invoked = data->value;
756                 break;
757         default:
758                 return -EOPNOTSUPP;
759         }
760         return 0;
761 }
762 EXPORT_SYMBOL(rtllib_wx_set_auth);
763
764 int rtllib_wx_set_gen_ie(struct rtllib_device *ieee, u8 *ie, size_t len)
765 {
766         u8 *buf;
767         u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
768
769         if (len > MAX_WPA_IE_LEN || (len && ie == NULL))
770                 return -EINVAL;
771
772         if (len) {
773                 eid = ie[0];
774                 if ((eid == MFIE_TYPE_GENERIC) && (!memcmp(&ie[2],
775                      wps_oui, 4))) {
776
777                         ieee->wps_ie_len = min_t(size_t, len, MAX_WZC_IE_LEN);
778                         buf = kmemdup(ie, ieee->wps_ie_len, GFP_KERNEL);
779                         if (buf == NULL)
780                                 return -ENOMEM;
781                         ieee->wps_ie = buf;
782                         return 0;
783                 }
784         }
785         ieee->wps_ie_len = 0;
786         kfree(ieee->wps_ie);
787         ieee->wps_ie = NULL;
788         if (len) {
789                 if (len != ie[1]+2)
790                         return -EINVAL;
791                 buf = kmemdup(ie, len, GFP_KERNEL);
792                 if (buf == NULL)
793                         return -ENOMEM;
794                 kfree(ieee->wpa_ie);
795                 ieee->wpa_ie = buf;
796                 ieee->wpa_ie_len = len;
797         } else {
798                 kfree(ieee->wpa_ie);
799                 ieee->wpa_ie = NULL;
800                 ieee->wpa_ie_len = 0;
801         }
802         return 0;
803 }
804 EXPORT_SYMBOL(rtllib_wx_set_gen_ie);