Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003 
0004   Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
0005 
0006   Portions of this file are based on the WEP enablement code provided by the
0007   Host AP project hostap-drivers v0.1.3
0008   Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
0009   <j@w1.fi>
0010   Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
0011 
0012 
0013   Contact Information:
0014   Intel Linux Wireless <ilw@linux.intel.com>
0015   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
0016 
0017 ******************************************************************************/
0018 
0019 #include <linux/hardirq.h>
0020 #include <linux/kmod.h>
0021 #include <linux/slab.h>
0022 #include <linux/module.h>
0023 #include <linux/jiffies.h>
0024 
0025 #include <net/lib80211.h>
0026 #include <linux/wireless.h>
0027 
0028 #include "libipw.h"
0029 
0030 static const char *libipw_modes[] = {
0031     "?", "a", "b", "ab", "g", "ag", "bg", "abg"
0032 };
0033 
0034 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
0035 {
0036     unsigned long end = jiffies;
0037 
0038     if (end >= start)
0039         return jiffies_to_msecs(end - start);
0040 
0041     return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
0042 }
0043 
0044 #define MAX_CUSTOM_LEN 64
0045 static char *libipw_translate_scan(struct libipw_device *ieee,
0046                       char *start, char *stop,
0047                       struct libipw_network *network,
0048                       struct iw_request_info *info)
0049 {
0050     char custom[MAX_CUSTOM_LEN];
0051     char *p;
0052     struct iw_event iwe;
0053     int i, j;
0054     char *current_val;  /* For rates */
0055     u8 rate;
0056 
0057     /* First entry *MUST* be the AP MAC address */
0058     iwe.cmd = SIOCGIWAP;
0059     iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
0060     memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
0061     start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
0062 
0063     /* Remaining entries will be displayed in the order we provide them */
0064 
0065     /* Add the ESSID */
0066     iwe.cmd = SIOCGIWESSID;
0067     iwe.u.data.flags = 1;
0068     iwe.u.data.length = min(network->ssid_len, (u8) 32);
0069     start = iwe_stream_add_point(info, start, stop,
0070                      &iwe, network->ssid);
0071 
0072     /* Add the protocol name */
0073     iwe.cmd = SIOCGIWNAME;
0074     snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s",
0075          libipw_modes[network->mode]);
0076     start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
0077 
0078     /* Add mode */
0079     iwe.cmd = SIOCGIWMODE;
0080     if (network->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
0081         if (network->capability & WLAN_CAPABILITY_ESS)
0082             iwe.u.mode = IW_MODE_MASTER;
0083         else
0084             iwe.u.mode = IW_MODE_ADHOC;
0085 
0086         start = iwe_stream_add_event(info, start, stop,
0087                          &iwe, IW_EV_UINT_LEN);
0088     }
0089 
0090     /* Add channel and frequency */
0091     /* Note : userspace automatically computes channel using iwrange */
0092     iwe.cmd = SIOCGIWFREQ;
0093     iwe.u.freq.m = libipw_channel_to_freq(ieee, network->channel);
0094     iwe.u.freq.e = 6;
0095     iwe.u.freq.i = 0;
0096     start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
0097 
0098     /* Add encryption capability */
0099     iwe.cmd = SIOCGIWENCODE;
0100     if (network->capability & WLAN_CAPABILITY_PRIVACY)
0101         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
0102     else
0103         iwe.u.data.flags = IW_ENCODE_DISABLED;
0104     iwe.u.data.length = 0;
0105     start = iwe_stream_add_point(info, start, stop,
0106                      &iwe, network->ssid);
0107 
0108     /* Add basic and extended rates */
0109     /* Rate : stuffing multiple values in a single event require a bit
0110      * more of magic - Jean II */
0111     current_val = start + iwe_stream_lcp_len(info);
0112     iwe.cmd = SIOCGIWRATE;
0113     /* Those two flags are ignored... */
0114     iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
0115 
0116     for (i = 0, j = 0; i < network->rates_len;) {
0117         if (j < network->rates_ex_len &&
0118             ((network->rates_ex[j] & 0x7F) <
0119              (network->rates[i] & 0x7F)))
0120             rate = network->rates_ex[j++] & 0x7F;
0121         else
0122             rate = network->rates[i++] & 0x7F;
0123         /* Bit rate given in 500 kb/s units (+ 0x80) */
0124         iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
0125         /* Add new value to event */
0126         current_val = iwe_stream_add_value(info, start, current_val,
0127                            stop, &iwe, IW_EV_PARAM_LEN);
0128     }
0129     for (; j < network->rates_ex_len; j++) {
0130         rate = network->rates_ex[j] & 0x7F;
0131         /* Bit rate given in 500 kb/s units (+ 0x80) */
0132         iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
0133         /* Add new value to event */
0134         current_val = iwe_stream_add_value(info, start, current_val,
0135                            stop, &iwe, IW_EV_PARAM_LEN);
0136     }
0137     /* Check if we added any rate */
0138     if ((current_val - start) > iwe_stream_lcp_len(info))
0139         start = current_val;
0140 
0141     /* Add quality statistics */
0142     iwe.cmd = IWEVQUAL;
0143     iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
0144         IW_QUAL_NOISE_UPDATED;
0145 
0146     if (!(network->stats.mask & LIBIPW_STATMASK_RSSI)) {
0147         iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID |
0148             IW_QUAL_LEVEL_INVALID;
0149         iwe.u.qual.qual = 0;
0150     } else {
0151         if (ieee->perfect_rssi == ieee->worst_rssi)
0152             iwe.u.qual.qual = 100;
0153         else
0154             iwe.u.qual.qual =
0155                 (100 *
0156                  (ieee->perfect_rssi - ieee->worst_rssi) *
0157                  (ieee->perfect_rssi - ieee->worst_rssi) -
0158                  (ieee->perfect_rssi - network->stats.rssi) *
0159                  (15 * (ieee->perfect_rssi - ieee->worst_rssi) +
0160                   62 * (ieee->perfect_rssi -
0161                     network->stats.rssi))) /
0162                 ((ieee->perfect_rssi -
0163                   ieee->worst_rssi) * (ieee->perfect_rssi -
0164                            ieee->worst_rssi));
0165         if (iwe.u.qual.qual > 100)
0166             iwe.u.qual.qual = 100;
0167         else if (iwe.u.qual.qual < 1)
0168             iwe.u.qual.qual = 0;
0169     }
0170 
0171     if (!(network->stats.mask & LIBIPW_STATMASK_NOISE)) {
0172         iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
0173         iwe.u.qual.noise = 0;
0174     } else {
0175         iwe.u.qual.noise = network->stats.noise;
0176     }
0177 
0178     if (!(network->stats.mask & LIBIPW_STATMASK_SIGNAL)) {
0179         iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
0180         iwe.u.qual.level = 0;
0181     } else {
0182         iwe.u.qual.level = network->stats.signal;
0183     }
0184 
0185     start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
0186 
0187     iwe.cmd = IWEVCUSTOM;
0188     p = custom;
0189 
0190     iwe.u.data.length = p - custom;
0191     if (iwe.u.data.length)
0192         start = iwe_stream_add_point(info, start, stop, &iwe, custom);
0193 
0194     memset(&iwe, 0, sizeof(iwe));
0195     if (network->wpa_ie_len) {
0196         char buf[MAX_WPA_IE_LEN];
0197         memcpy(buf, network->wpa_ie, network->wpa_ie_len);
0198         iwe.cmd = IWEVGENIE;
0199         iwe.u.data.length = network->wpa_ie_len;
0200         start = iwe_stream_add_point(info, start, stop, &iwe, buf);
0201     }
0202 
0203     memset(&iwe, 0, sizeof(iwe));
0204     if (network->rsn_ie_len) {
0205         char buf[MAX_WPA_IE_LEN];
0206         memcpy(buf, network->rsn_ie, network->rsn_ie_len);
0207         iwe.cmd = IWEVGENIE;
0208         iwe.u.data.length = network->rsn_ie_len;
0209         start = iwe_stream_add_point(info, start, stop, &iwe, buf);
0210     }
0211 
0212     /* Add EXTRA: Age to display seconds since last beacon/probe response
0213      * for given network. */
0214     iwe.cmd = IWEVCUSTOM;
0215     p = custom;
0216     p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
0217               " Last beacon: %ums ago",
0218               elapsed_jiffies_msecs(network->last_scanned));
0219     iwe.u.data.length = p - custom;
0220     if (iwe.u.data.length)
0221         start = iwe_stream_add_point(info, start, stop, &iwe, custom);
0222 
0223     /* Add spectrum management information */
0224     iwe.cmd = -1;
0225     p = custom;
0226     p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), " Channel flags: ");
0227 
0228     if (libipw_get_channel_flags(ieee, network->channel) &
0229         LIBIPW_CH_INVALID) {
0230         iwe.cmd = IWEVCUSTOM;
0231         p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), "INVALID ");
0232     }
0233 
0234     if (libipw_get_channel_flags(ieee, network->channel) &
0235         LIBIPW_CH_RADAR_DETECT) {
0236         iwe.cmd = IWEVCUSTOM;
0237         p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom), "DFS ");
0238     }
0239 
0240     if (iwe.cmd == IWEVCUSTOM) {
0241         iwe.u.data.length = p - custom;
0242         start = iwe_stream_add_point(info, start, stop, &iwe, custom);
0243     }
0244 
0245     return start;
0246 }
0247 
0248 #define SCAN_ITEM_SIZE 128
0249 
0250 int libipw_wx_get_scan(struct libipw_device *ieee,
0251               struct iw_request_info *info,
0252               union iwreq_data *wrqu, char *extra)
0253 {
0254     struct libipw_network *network;
0255     unsigned long flags;
0256     int err = 0;
0257 
0258     char *ev = extra;
0259     char *stop = ev + wrqu->data.length;
0260     int i = 0;
0261 
0262     LIBIPW_DEBUG_WX("Getting scan\n");
0263 
0264     spin_lock_irqsave(&ieee->lock, flags);
0265 
0266     list_for_each_entry(network, &ieee->network_list, list) {
0267         i++;
0268         if (stop - ev < SCAN_ITEM_SIZE) {
0269             err = -E2BIG;
0270             break;
0271         }
0272 
0273         if (ieee->scan_age == 0 ||
0274             time_after(network->last_scanned + ieee->scan_age, jiffies))
0275             ev = libipw_translate_scan(ieee, ev, stop, network,
0276                               info);
0277         else {
0278             LIBIPW_DEBUG_SCAN("Not showing network '%*pE (%pM)' due to age (%ums).\n",
0279                       network->ssid_len, network->ssid,
0280                       network->bssid,
0281                       elapsed_jiffies_msecs(
0282                                    network->last_scanned));
0283         }
0284     }
0285 
0286     spin_unlock_irqrestore(&ieee->lock, flags);
0287 
0288     wrqu->data.length = ev - extra;
0289     wrqu->data.flags = 0;
0290 
0291     LIBIPW_DEBUG_WX("exit: %d networks returned.\n", i);
0292 
0293     return err;
0294 }
0295 
0296 int libipw_wx_set_encode(struct libipw_device *ieee,
0297                 struct iw_request_info *info,
0298                 union iwreq_data *wrqu, char *keybuf)
0299 {
0300     struct iw_point *erq = &(wrqu->encoding);
0301     struct net_device *dev = ieee->dev;
0302     struct libipw_security sec = {
0303         .flags = 0
0304     };
0305     int i, key, key_provided, len;
0306     struct lib80211_crypt_data **crypt;
0307     int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
0308 
0309     LIBIPW_DEBUG_WX("SET_ENCODE\n");
0310 
0311     key = erq->flags & IW_ENCODE_INDEX;
0312     if (key) {
0313         if (key > WEP_KEYS)
0314             return -EINVAL;
0315         key--;
0316         key_provided = 1;
0317     } else {
0318         key_provided = 0;
0319         key = ieee->crypt_info.tx_keyidx;
0320     }
0321 
0322     LIBIPW_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
0323                "provided" : "default");
0324 
0325     crypt = &ieee->crypt_info.crypt[key];
0326 
0327     if (erq->flags & IW_ENCODE_DISABLED) {
0328         if (key_provided && *crypt) {
0329             LIBIPW_DEBUG_WX("Disabling encryption on key %d.\n",
0330                        key);
0331             lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
0332         } else
0333             LIBIPW_DEBUG_WX("Disabling encryption.\n");
0334 
0335         /* Check all the keys to see if any are still configured,
0336          * and if no key index was provided, de-init them all */
0337         for (i = 0; i < WEP_KEYS; i++) {
0338             if (ieee->crypt_info.crypt[i] != NULL) {
0339                 if (key_provided)
0340                     break;
0341                 lib80211_crypt_delayed_deinit(&ieee->crypt_info,
0342                                    &ieee->crypt_info.crypt[i]);
0343             }
0344         }
0345 
0346         if (i == WEP_KEYS) {
0347             sec.enabled = 0;
0348             sec.encrypt = 0;
0349             sec.level = SEC_LEVEL_0;
0350             sec.flags |= SEC_ENABLED | SEC_LEVEL | SEC_ENCRYPT;
0351         }
0352 
0353         goto done;
0354     }
0355 
0356     sec.enabled = 1;
0357     sec.encrypt = 1;
0358     sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
0359 
0360     if (*crypt != NULL && (*crypt)->ops != NULL &&
0361         strcmp((*crypt)->ops->name, "WEP") != 0) {
0362         /* changing to use WEP; deinit previously used algorithm
0363          * on this key */
0364         lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
0365     }
0366 
0367     if (*crypt == NULL && host_crypto) {
0368         struct lib80211_crypt_data *new_crypt;
0369 
0370         /* take WEP into use */
0371         new_crypt = kzalloc(sizeof(struct lib80211_crypt_data),
0372                     GFP_KERNEL);
0373         if (new_crypt == NULL)
0374             return -ENOMEM;
0375         new_crypt->ops = lib80211_get_crypto_ops("WEP");
0376         if (!new_crypt->ops) {
0377             request_module("lib80211_crypt_wep");
0378             new_crypt->ops = lib80211_get_crypto_ops("WEP");
0379         }
0380 
0381         if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
0382             new_crypt->priv = new_crypt->ops->init(key);
0383 
0384         if (!new_crypt->ops || !new_crypt->priv) {
0385             kfree(new_crypt);
0386             new_crypt = NULL;
0387 
0388             printk(KERN_WARNING "%s: could not initialize WEP: "
0389                    "load module lib80211_crypt_wep\n", dev->name);
0390             return -EOPNOTSUPP;
0391         }
0392         *crypt = new_crypt;
0393     }
0394 
0395     /* If a new key was provided, set it up */
0396     if (erq->length > 0) {
0397         len = erq->length <= 5 ? 5 : 13;
0398         memcpy(sec.keys[key], keybuf, erq->length);
0399         if (len > erq->length)
0400             memset(sec.keys[key] + erq->length, 0,
0401                    len - erq->length);
0402         LIBIPW_DEBUG_WX("Setting key %d to '%*pE' (%d:%d bytes)\n",
0403                    key, len, sec.keys[key],
0404                    erq->length, len);
0405         sec.key_sizes[key] = len;
0406         if (*crypt)
0407             (*crypt)->ops->set_key(sec.keys[key], len, NULL,
0408                            (*crypt)->priv);
0409         sec.flags |= (1 << key);
0410         /* This ensures a key will be activated if no key is
0411          * explicitly set */
0412         if (key == sec.active_key)
0413             sec.flags |= SEC_ACTIVE_KEY;
0414 
0415     } else {
0416         if (host_crypto) {
0417             len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
0418                              NULL, (*crypt)->priv);
0419             if (len == 0) {
0420                 /* Set a default key of all 0 */
0421                 LIBIPW_DEBUG_WX("Setting key %d to all "
0422                            "zero.\n", key);
0423                 memset(sec.keys[key], 0, 13);
0424                 (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
0425                                (*crypt)->priv);
0426                 sec.key_sizes[key] = 13;
0427                 sec.flags |= (1 << key);
0428             }
0429         }
0430         /* No key data - just set the default TX key index */
0431         if (key_provided) {
0432             LIBIPW_DEBUG_WX("Setting key %d to default Tx "
0433                        "key.\n", key);
0434             ieee->crypt_info.tx_keyidx = key;
0435             sec.active_key = key;
0436             sec.flags |= SEC_ACTIVE_KEY;
0437         }
0438     }
0439     if (erq->flags & (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED)) {
0440         ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
0441         sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
0442             WLAN_AUTH_SHARED_KEY;
0443         sec.flags |= SEC_AUTH_MODE;
0444         LIBIPW_DEBUG_WX("Auth: %s\n",
0445                    sec.auth_mode == WLAN_AUTH_OPEN ?
0446                    "OPEN" : "SHARED KEY");
0447     }
0448 
0449     /* For now we just support WEP, so only set that security level...
0450      * TODO: When WPA is added this is one place that needs to change */
0451     sec.flags |= SEC_LEVEL;
0452     sec.level = SEC_LEVEL_1;    /* 40 and 104 bit WEP */
0453     sec.encode_alg[key] = SEC_ALG_WEP;
0454 
0455       done:
0456     if (ieee->set_security)
0457         ieee->set_security(dev, &sec);
0458 
0459     return 0;
0460 }
0461 
0462 int libipw_wx_get_encode(struct libipw_device *ieee,
0463                 struct iw_request_info *info,
0464                 union iwreq_data *wrqu, char *keybuf)
0465 {
0466     struct iw_point *erq = &(wrqu->encoding);
0467     int len, key;
0468     struct libipw_security *sec = &ieee->sec;
0469 
0470     LIBIPW_DEBUG_WX("GET_ENCODE\n");
0471 
0472     key = erq->flags & IW_ENCODE_INDEX;
0473     if (key) {
0474         if (key > WEP_KEYS)
0475             return -EINVAL;
0476         key--;
0477     } else
0478         key = ieee->crypt_info.tx_keyidx;
0479 
0480     erq->flags = key + 1;
0481 
0482     if (!sec->enabled) {
0483         erq->length = 0;
0484         erq->flags |= IW_ENCODE_DISABLED;
0485         return 0;
0486     }
0487 
0488     len = sec->key_sizes[key];
0489     memcpy(keybuf, sec->keys[key], len);
0490 
0491     erq->length = len;
0492     erq->flags |= IW_ENCODE_ENABLED;
0493 
0494     if (ieee->open_wep)
0495         erq->flags |= IW_ENCODE_OPEN;
0496     else
0497         erq->flags |= IW_ENCODE_RESTRICTED;
0498 
0499     return 0;
0500 }
0501 
0502 int libipw_wx_set_encodeext(struct libipw_device *ieee,
0503                    struct iw_request_info *info,
0504                    union iwreq_data *wrqu, char *extra)
0505 {
0506     struct net_device *dev = ieee->dev;
0507     struct iw_point *encoding = &wrqu->encoding;
0508     struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
0509     int i, idx, ret = 0;
0510     int group_key = 0;
0511     const char *alg, *module;
0512     struct lib80211_crypto_ops *ops;
0513     struct lib80211_crypt_data **crypt;
0514 
0515     struct libipw_security sec = {
0516         .flags = 0,
0517     };
0518 
0519     idx = encoding->flags & IW_ENCODE_INDEX;
0520     if (idx) {
0521         if (idx < 1 || idx > WEP_KEYS)
0522             return -EINVAL;
0523         idx--;
0524     } else
0525         idx = ieee->crypt_info.tx_keyidx;
0526 
0527     if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
0528         crypt = &ieee->crypt_info.crypt[idx];
0529         group_key = 1;
0530     } else {
0531         /* some Cisco APs use idx>0 for unicast in dynamic WEP */
0532         if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
0533             return -EINVAL;
0534         if (ieee->iw_mode == IW_MODE_INFRA)
0535             crypt = &ieee->crypt_info.crypt[idx];
0536         else
0537             return -EINVAL;
0538     }
0539 
0540     sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
0541     if ((encoding->flags & IW_ENCODE_DISABLED) ||
0542         ext->alg == IW_ENCODE_ALG_NONE) {
0543         if (*crypt)
0544             lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
0545 
0546         for (i = 0; i < WEP_KEYS; i++)
0547             if (ieee->crypt_info.crypt[i] != NULL)
0548                 break;
0549 
0550         if (i == WEP_KEYS) {
0551             sec.enabled = 0;
0552             sec.encrypt = 0;
0553             sec.level = SEC_LEVEL_0;
0554             sec.flags |= SEC_LEVEL;
0555         }
0556         goto done;
0557     }
0558 
0559     sec.enabled = 1;
0560     sec.encrypt = 1;
0561 
0562     if (group_key ? !ieee->host_mc_decrypt :
0563         !(ieee->host_encrypt || ieee->host_decrypt ||
0564           ieee->host_encrypt_msdu))
0565         goto skip_host_crypt;
0566 
0567     switch (ext->alg) {
0568     case IW_ENCODE_ALG_WEP:
0569         alg = "WEP";
0570         module = "lib80211_crypt_wep";
0571         break;
0572     case IW_ENCODE_ALG_TKIP:
0573         alg = "TKIP";
0574         module = "lib80211_crypt_tkip";
0575         break;
0576     case IW_ENCODE_ALG_CCMP:
0577         alg = "CCMP";
0578         module = "lib80211_crypt_ccmp";
0579         break;
0580     default:
0581         LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
0582                    dev->name, ext->alg);
0583         ret = -EINVAL;
0584         goto done;
0585     }
0586 
0587     ops = lib80211_get_crypto_ops(alg);
0588     if (ops == NULL) {
0589         request_module(module);
0590         ops = lib80211_get_crypto_ops(alg);
0591     }
0592     if (ops == NULL) {
0593         LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
0594                    dev->name, ext->alg);
0595         ret = -EINVAL;
0596         goto done;
0597     }
0598 
0599     if (*crypt == NULL || (*crypt)->ops != ops) {
0600         struct lib80211_crypt_data *new_crypt;
0601 
0602         lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
0603 
0604         new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
0605         if (new_crypt == NULL) {
0606             ret = -ENOMEM;
0607             goto done;
0608         }
0609         new_crypt->ops = ops;
0610         if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
0611             new_crypt->priv = new_crypt->ops->init(idx);
0612         if (new_crypt->priv == NULL) {
0613             kfree(new_crypt);
0614             ret = -EINVAL;
0615             goto done;
0616         }
0617         *crypt = new_crypt;
0618     }
0619 
0620     if (ext->key_len > 0 && (*crypt)->ops->set_key &&
0621         (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
0622                    (*crypt)->priv) < 0) {
0623         LIBIPW_DEBUG_WX("%s: key setting failed\n", dev->name);
0624         ret = -EINVAL;
0625         goto done;
0626     }
0627 
0628       skip_host_crypt:
0629     if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
0630         ieee->crypt_info.tx_keyidx = idx;
0631         sec.active_key = idx;
0632         sec.flags |= SEC_ACTIVE_KEY;
0633     }
0634 
0635     if (ext->alg != IW_ENCODE_ALG_NONE) {
0636         int key_len = clamp_val(ext->key_len, 0, SCM_KEY_LEN);
0637 
0638         memcpy(sec.keys[idx], ext->key, key_len);
0639         sec.key_sizes[idx] = key_len;
0640         sec.flags |= (1 << idx);
0641         if (ext->alg == IW_ENCODE_ALG_WEP) {
0642             sec.encode_alg[idx] = SEC_ALG_WEP;
0643             sec.flags |= SEC_LEVEL;
0644             sec.level = SEC_LEVEL_1;
0645         } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
0646             sec.encode_alg[idx] = SEC_ALG_TKIP;
0647             sec.flags |= SEC_LEVEL;
0648             sec.level = SEC_LEVEL_2;
0649         } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
0650             sec.encode_alg[idx] = SEC_ALG_CCMP;
0651             sec.flags |= SEC_LEVEL;
0652             sec.level = SEC_LEVEL_3;
0653         }
0654         /* Don't set sec level for group keys. */
0655         if (group_key)
0656             sec.flags &= ~SEC_LEVEL;
0657     }
0658       done:
0659     if (ieee->set_security)
0660         ieee->set_security(dev, &sec);
0661 
0662     return ret;
0663 }
0664 
0665 int libipw_wx_get_encodeext(struct libipw_device *ieee,
0666                    struct iw_request_info *info,
0667                    union iwreq_data *wrqu, char *extra)
0668 {
0669     struct iw_point *encoding = &wrqu->encoding;
0670     struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
0671     struct libipw_security *sec = &ieee->sec;
0672     int idx, max_key_len;
0673 
0674     max_key_len = encoding->length - sizeof(*ext);
0675     if (max_key_len < 0)
0676         return -EINVAL;
0677 
0678     idx = encoding->flags & IW_ENCODE_INDEX;
0679     if (idx) {
0680         if (idx < 1 || idx > WEP_KEYS)
0681             return -EINVAL;
0682         idx--;
0683     } else
0684         idx = ieee->crypt_info.tx_keyidx;
0685 
0686     if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) &&
0687         ext->alg != IW_ENCODE_ALG_WEP)
0688         if (idx != 0 || ieee->iw_mode != IW_MODE_INFRA)
0689             return -EINVAL;
0690 
0691     encoding->flags = idx + 1;
0692     memset(ext, 0, sizeof(*ext));
0693 
0694     if (!sec->enabled) {
0695         ext->alg = IW_ENCODE_ALG_NONE;
0696         ext->key_len = 0;
0697         encoding->flags |= IW_ENCODE_DISABLED;
0698     } else {
0699         if (sec->encode_alg[idx] == SEC_ALG_WEP)
0700             ext->alg = IW_ENCODE_ALG_WEP;
0701         else if (sec->encode_alg[idx] == SEC_ALG_TKIP)
0702             ext->alg = IW_ENCODE_ALG_TKIP;
0703         else if (sec->encode_alg[idx] == SEC_ALG_CCMP)
0704             ext->alg = IW_ENCODE_ALG_CCMP;
0705         else
0706             return -EINVAL;
0707 
0708         ext->key_len = sec->key_sizes[idx];
0709         memcpy(ext->key, sec->keys[idx], ext->key_len);
0710         encoding->flags |= IW_ENCODE_ENABLED;
0711         if (ext->key_len &&
0712             (ext->alg == IW_ENCODE_ALG_TKIP ||
0713              ext->alg == IW_ENCODE_ALG_CCMP))
0714             ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
0715 
0716     }
0717 
0718     return 0;
0719 }
0720 
0721 EXPORT_SYMBOL(libipw_wx_set_encodeext);
0722 EXPORT_SYMBOL(libipw_wx_get_encodeext);
0723 
0724 EXPORT_SYMBOL(libipw_wx_get_scan);
0725 EXPORT_SYMBOL(libipw_wx_set_encode);
0726 EXPORT_SYMBOL(libipw_wx_get_encode);