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