cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

rtllib_wx.c (21318B)


      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"
     20struct modes_unit {
     21	char *mode_string;
     22	int mode_size;
     23};
     24static 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
     34static 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.bd_ht_cap_buf, EWC11NHTCap, 4))
    144			ht_cap = (struct ht_capab_ele *)
    145				 &network->bssht.bd_ht_cap_buf[4];
    146		else
    147			ht_cap = (struct ht_capab_ele *)
    148				 &network->bssht.bd_ht_cap_buf[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, IW_EV_PARAM_LEN);
    164	iwe.cmd = IWEVCUSTOM;
    165	iwe.u.data.length = p - custom;
    166	if (iwe.u.data.length)
    167		start = iwe_stream_add_point_rsl(info, start, stop,
    168						 &iwe, custom);
    169	/* Add quality statistics */
    170	/* TODO: Fix these values... */
    171	iwe.cmd = IWEVQUAL;
    172	iwe.u.qual.qual = network->stats.signal;
    173	iwe.u.qual.level = network->stats.rssi;
    174	iwe.u.qual.noise = network->stats.noise;
    175	iwe.u.qual.updated = network->stats.mask & RTLLIB_STATMASK_WEMASK;
    176	if (!(network->stats.mask & RTLLIB_STATMASK_RSSI))
    177		iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
    178	if (!(network->stats.mask & RTLLIB_STATMASK_NOISE))
    179		iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
    180	if (!(network->stats.mask & RTLLIB_STATMASK_SIGNAL))
    181		iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
    182	iwe.u.qual.updated = 7;
    183	start = iwe_stream_add_event_rsl(info, start, stop, &iwe, IW_EV_QUAL_LEN);
    184
    185	iwe.cmd = IWEVCUSTOM;
    186	p = custom;
    187	iwe.u.data.length = p - custom;
    188	if (iwe.u.data.length)
    189		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, custom);
    190
    191	memset(&iwe, 0, sizeof(iwe));
    192	if (network->wpa_ie_len) {
    193		char buf[MAX_WPA_IE_LEN];
    194
    195		memcpy(buf, network->wpa_ie, network->wpa_ie_len);
    196		iwe.cmd = IWEVGENIE;
    197		iwe.u.data.length = network->wpa_ie_len;
    198		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
    199	}
    200	memset(&iwe, 0, sizeof(iwe));
    201	if (network->rsn_ie_len) {
    202		char buf[MAX_WPA_IE_LEN];
    203
    204		memcpy(buf, network->rsn_ie, network->rsn_ie_len);
    205		iwe.cmd = IWEVGENIE;
    206		iwe.u.data.length = network->rsn_ie_len;
    207		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
    208	}
    209
    210	/* add info for WZC */
    211	memset(&iwe, 0, sizeof(iwe));
    212	if (network->wzc_ie_len) {
    213		char buf[MAX_WZC_IE_LEN];
    214
    215		memcpy(buf, network->wzc_ie, network->wzc_ie_len);
    216		iwe.cmd = IWEVGENIE;
    217		iwe.u.data.length = network->wzc_ie_len;
    218		start = iwe_stream_add_point_rsl(info, start, stop, &iwe, buf);
    219	}
    220
    221	/* Add EXTRA: Age to display seconds since last beacon/probe response
    222	 * for given network.
    223	 */
    224	iwe.cmd = IWEVCUSTOM;
    225	p = custom;
    226	p += scnprintf(p, MAX_CUSTOM_LEN - (p - custom),
    227		      " Last beacon: %lums ago",
    228		      (jiffies - network->last_scanned) / (HZ / 100));
    229	iwe.u.data.length = p - custom;
    230	if (iwe.u.data.length)
    231		start = iwe_stream_add_point_rsl(info, start, stop,
    232						 &iwe, custom);
    233
    234	return start;
    235}
    236
    237int rtllib_wx_get_scan(struct rtllib_device *ieee,
    238		       struct iw_request_info *info,
    239		       union iwreq_data *wrqu, char *extra)
    240{
    241	struct rtllib_network *network;
    242	unsigned long flags;
    243
    244	char *ev = extra;
    245	char *stop = ev + wrqu->data.length;
    246	int i = 0;
    247	int err = 0;
    248
    249	netdev_dbg(ieee->dev, "Getting scan\n");
    250	mutex_lock(&ieee->wx_mutex);
    251	spin_lock_irqsave(&ieee->lock, flags);
    252
    253	list_for_each_entry(network, &ieee->network_list, list) {
    254		i++;
    255		if ((stop - ev) < 200) {
    256			err = -E2BIG;
    257			break;
    258		}
    259		if (ieee->scan_age == 0 ||
    260		    time_after(network->last_scanned + ieee->scan_age, jiffies))
    261			ev = rtl819x_translate_scan(ieee, ev, stop, network,
    262						    info);
    263		else
    264			netdev_dbg(ieee->dev,
    265				   "Network '%s ( %pM)' hidden due to age (%lums).\n",
    266				   escape_essid(network->ssid,
    267						network->ssid_len),
    268				   network->bssid,
    269				   (jiffies - network->last_scanned) /
    270				   (HZ / 100));
    271	}
    272
    273	spin_unlock_irqrestore(&ieee->lock, flags);
    274	mutex_unlock(&ieee->wx_mutex);
    275	wrqu->data.length = ev -  extra;
    276	wrqu->data.flags = 0;
    277
    278	netdev_dbg(ieee->dev, "%s(): %d networks returned.\n", __func__, i);
    279
    280	return err;
    281}
    282EXPORT_SYMBOL(rtllib_wx_get_scan);
    283
    284int rtllib_wx_set_encode(struct rtllib_device *ieee,
    285			 struct iw_request_info *info,
    286			 union iwreq_data *wrqu, char *keybuf)
    287{
    288	struct iw_point *erq = &(wrqu->encoding);
    289	struct net_device *dev = ieee->dev;
    290	struct rtllib_security sec = {
    291		.flags = 0
    292	};
    293	int i, key, key_provided, len;
    294	struct lib80211_crypt_data **crypt;
    295
    296	key = erq->flags & IW_ENCODE_INDEX;
    297	if (key) {
    298		if (key > NUM_WEP_KEYS)
    299			return -EINVAL;
    300		key--;
    301		key_provided = 1;
    302	} else {
    303		key_provided = 0;
    304		key = ieee->crypt_info.tx_keyidx;
    305	}
    306
    307	netdev_dbg(ieee->dev, "Key: %d [%s]\n", key, key_provided ?
    308			   "provided" : "default");
    309	crypt = &ieee->crypt_info.crypt[key];
    310	if (erq->flags & IW_ENCODE_DISABLED) {
    311		if (key_provided && *crypt) {
    312			netdev_dbg(ieee->dev,
    313				   "Disabling encryption on key %d.\n", key);
    314			lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
    315		} else
    316			netdev_dbg(ieee->dev, "Disabling encryption.\n");
    317
    318		/* Check all the keys to see if any are still configured,
    319		 * and if no key index was provided, de-init them all
    320		 */
    321		for (i = 0; i < NUM_WEP_KEYS; i++) {
    322			if (ieee->crypt_info.crypt[i]) {
    323				if (key_provided)
    324					break;
    325				lib80211_crypt_delayed_deinit(&ieee->crypt_info,
    326							      &ieee->crypt_info.crypt[i]);
    327			}
    328		}
    329
    330		if (i == NUM_WEP_KEYS) {
    331			sec.enabled = 0;
    332			sec.level = SEC_LEVEL_0;
    333			sec.flags |= SEC_ENABLED | SEC_LEVEL;
    334		}
    335
    336		goto done;
    337	}
    338
    339	sec.enabled = 1;
    340	sec.flags |= SEC_ENABLED;
    341
    342	if (*crypt && (*crypt)->ops &&
    343	    strcmp((*crypt)->ops->name, "R-WEP") != 0) {
    344		/* changing to use WEP; deinit previously used algorithm
    345		 * on this key
    346		 */
    347		lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
    348	}
    349
    350	if (!*crypt) {
    351		struct lib80211_crypt_data *new_crypt;
    352
    353		/* take WEP into use */
    354		new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
    355		if (!new_crypt)
    356			return -ENOMEM;
    357		new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
    358		if (!new_crypt->ops) {
    359			request_module("rtllib_crypt_wep");
    360			new_crypt->ops = lib80211_get_crypto_ops("R-WEP");
    361		}
    362
    363		if (new_crypt->ops)
    364			new_crypt->priv = new_crypt->ops->init(key);
    365
    366		if (!new_crypt->ops || !new_crypt->priv) {
    367			kfree(new_crypt);
    368			new_crypt = NULL;
    369
    370			netdev_warn(dev,
    371				    "%s: could not initialize WEP: load module rtllib_crypt_wep\n",
    372				    dev->name);
    373			return -EOPNOTSUPP;
    374		}
    375		*crypt = new_crypt;
    376	}
    377
    378	/* If a new key was provided, set it up */
    379	if (erq->length > 0) {
    380		len = erq->length <= 5 ? 5 : 13;
    381		memcpy(sec.keys[key], keybuf, erq->length);
    382		if (len > erq->length)
    383			memset(sec.keys[key] + erq->length, 0,
    384			       len - erq->length);
    385		netdev_dbg(ieee->dev, "Setting key %d to '%s' (%d:%d bytes)\n",
    386			   key, escape_essid(sec.keys[key], len), erq->length,
    387			   len);
    388		sec.key_sizes[key] = len;
    389		(*crypt)->ops->set_key(sec.keys[key], len, NULL,
    390				       (*crypt)->priv);
    391		sec.flags |= (1 << key);
    392		/* This ensures a key will be activated if no key is
    393		 * explicitly set
    394		 */
    395		if (key == sec.active_key)
    396			sec.flags |= SEC_ACTIVE_KEY;
    397		ieee->crypt_info.tx_keyidx = key;
    398
    399	} else {
    400		len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
    401					     NULL, (*crypt)->priv);
    402		if (len == 0) {
    403			/* Set a default key of all 0 */
    404			netdev_info(ieee->dev, "Setting key %d to all zero.\n", key);
    405
    406			memset(sec.keys[key], 0, 13);
    407			(*crypt)->ops->set_key(sec.keys[key], 13, NULL,
    408					       (*crypt)->priv);
    409			sec.key_sizes[key] = 13;
    410			sec.flags |= (1 << key);
    411		}
    412
    413		/* No key data - just set the default TX key index */
    414		if (key_provided) {
    415			netdev_dbg(ieee->dev,
    416				   "Setting key %d as default Tx key.\n", key);
    417			ieee->crypt_info.tx_keyidx = key;
    418			sec.active_key = key;
    419			sec.flags |= SEC_ACTIVE_KEY;
    420		}
    421	}
    422 done:
    423	ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
    424	ieee->auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
    425			  WLAN_AUTH_SHARED_KEY;
    426	sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
    427	sec.flags |= SEC_AUTH_MODE;
    428	netdev_dbg(ieee->dev, "Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
    429			   "OPEN" : "SHARED KEY");
    430
    431	/* For now we just support WEP, so only set that security level...
    432	 * TODO: When WPA is added this is one place that needs to change
    433	 */
    434	sec.flags |= SEC_LEVEL;
    435	sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
    436
    437	if (ieee->set_security)
    438		ieee->set_security(dev, &sec);
    439
    440	/* Do not reset port if card is in Managed mode since resetting will
    441	 * generate new IEEE 802.11 authentication which may end up in looping
    442	 * with IEEE 802.1X.  If your hardware requires a reset after WEP
    443	 * configuration (for example... Prism2), implement the reset_port in
    444	 * the callbacks structures used to initialize the 802.11 stack.
    445	 */
    446	if (ieee->reset_on_keychange &&
    447	    ieee->iw_mode != IW_MODE_INFRA &&
    448	    ieee->reset_port && ieee->reset_port(dev)) {
    449		netdev_dbg(dev, "%s: reset_port failed\n", dev->name);
    450		return -EINVAL;
    451	}
    452	return 0;
    453}
    454EXPORT_SYMBOL(rtllib_wx_set_encode);
    455
    456int rtllib_wx_get_encode(struct rtllib_device *ieee,
    457			 struct iw_request_info *info,
    458			 union iwreq_data *wrqu, char *keybuf)
    459{
    460	struct iw_point *erq = &(wrqu->encoding);
    461	int len, key;
    462	struct lib80211_crypt_data *crypt;
    463
    464	if (ieee->iw_mode == IW_MODE_MONITOR)
    465		return -1;
    466
    467	key = erq->flags & IW_ENCODE_INDEX;
    468	if (key) {
    469		if (key > NUM_WEP_KEYS)
    470			return -EINVAL;
    471		key--;
    472	} else {
    473		key = ieee->crypt_info.tx_keyidx;
    474	}
    475	crypt = ieee->crypt_info.crypt[key];
    476
    477	erq->flags = key + 1;
    478
    479	if (!crypt || !crypt->ops) {
    480		erq->length = 0;
    481		erq->flags |= IW_ENCODE_DISABLED;
    482		return 0;
    483	}
    484	len = crypt->ops->get_key(keybuf, SCM_KEY_LEN, NULL, crypt->priv);
    485
    486	erq->length = max(len, 0);
    487
    488	erq->flags |= IW_ENCODE_ENABLED;
    489
    490	if (ieee->open_wep)
    491		erq->flags |= IW_ENCODE_OPEN;
    492	else
    493		erq->flags |= IW_ENCODE_RESTRICTED;
    494
    495	return 0;
    496}
    497EXPORT_SYMBOL(rtllib_wx_get_encode);
    498
    499int rtllib_wx_set_encode_ext(struct rtllib_device *ieee,
    500			     struct iw_request_info *info,
    501			     union iwreq_data *wrqu, char *extra)
    502{
    503	int ret = 0;
    504	struct net_device *dev = ieee->dev;
    505	struct iw_point *encoding = &wrqu->encoding;
    506	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
    507	int i, idx;
    508	int group_key = 0;
    509	const char *alg, *module;
    510	struct lib80211_crypto_ops *ops;
    511	struct lib80211_crypt_data **crypt;
    512
    513	struct rtllib_security sec = {
    514		.flags = 0,
    515	};
    516	idx = encoding->flags & IW_ENCODE_INDEX;
    517	if (idx) {
    518		if (idx < 1 || idx > NUM_WEP_KEYS)
    519			return -EINVAL;
    520		idx--;
    521	} else {
    522		idx = ieee->crypt_info.tx_keyidx;
    523	}
    524	if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
    525		crypt = &ieee->crypt_info.crypt[idx];
    526		group_key = 1;
    527	} else {
    528		/* some Cisco APs use idx>0 for unicast in dynamic WEP */
    529		if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
    530			return -EINVAL;
    531		if (ieee->iw_mode == IW_MODE_INFRA)
    532			crypt = &ieee->crypt_info.crypt[idx];
    533		else
    534			return -EINVAL;
    535	}
    536
    537	sec.flags |= SEC_ENABLED;
    538	if ((encoding->flags & IW_ENCODE_DISABLED) ||
    539	    ext->alg == IW_ENCODE_ALG_NONE) {
    540		if (*crypt)
    541			lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
    542
    543		for (i = 0; i < NUM_WEP_KEYS; i++) {
    544			if (ieee->crypt_info.crypt[i])
    545				break;
    546		}
    547		if (i == NUM_WEP_KEYS) {
    548			sec.enabled = 0;
    549			sec.level = SEC_LEVEL_0;
    550			sec.flags |= SEC_LEVEL;
    551		}
    552		goto done;
    553	}
    554
    555	sec.enabled = 1;
    556	switch (ext->alg) {
    557	case IW_ENCODE_ALG_WEP:
    558		alg = "R-WEP";
    559		module = "rtllib_crypt_wep";
    560		break;
    561	case IW_ENCODE_ALG_TKIP:
    562		alg = "R-TKIP";
    563		module = "rtllib_crypt_tkip";
    564		break;
    565	case IW_ENCODE_ALG_CCMP:
    566		alg = "R-CCMP";
    567		module = "rtllib_crypt_ccmp";
    568		break;
    569	default:
    570		netdev_dbg(ieee->dev, "Unknown crypto alg %d\n", ext->alg);
    571		ret = -EINVAL;
    572		goto done;
    573	}
    574	netdev_dbg(dev, "alg name:%s\n", alg);
    575
    576	ops = lib80211_get_crypto_ops(alg);
    577	if (!ops) {
    578		char tempbuf[100];
    579
    580		memset(tempbuf, 0x00, 100);
    581		sprintf(tempbuf, "%s", module);
    582		request_module("%s", tempbuf);
    583		ops = lib80211_get_crypto_ops(alg);
    584	}
    585	if (!ops) {
    586		netdev_info(dev, "========>unknown crypto alg %d\n", ext->alg);
    587		ret = -EINVAL;
    588		goto done;
    589	}
    590
    591	if (!*crypt || (*crypt)->ops != ops) {
    592		struct lib80211_crypt_data *new_crypt;
    593
    594		lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
    595
    596		new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
    597		if (!new_crypt) {
    598			ret = -ENOMEM;
    599			goto done;
    600		}
    601		new_crypt->ops = ops;
    602		if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
    603			new_crypt->priv = new_crypt->ops->init(idx);
    604
    605		if (!new_crypt->priv) {
    606			kfree(new_crypt);
    607			ret = -EINVAL;
    608			goto done;
    609		}
    610		*crypt = new_crypt;
    611
    612	}
    613
    614	if (ext->key_len > 0 && (*crypt)->ops->set_key &&
    615	    (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
    616				   (*crypt)->priv) < 0) {
    617		netdev_info(dev, "key setting failed\n");
    618		ret = -EINVAL;
    619		goto done;
    620	}
    621	if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
    622		ieee->crypt_info.tx_keyidx = idx;
    623		sec.active_key = idx;
    624		sec.flags |= SEC_ACTIVE_KEY;
    625	}
    626	if (ext->alg != IW_ENCODE_ALG_NONE) {
    627		sec.key_sizes[idx] = ext->key_len;
    628		sec.flags |= (1 << idx);
    629		if (ext->alg == IW_ENCODE_ALG_WEP) {
    630			sec.flags |= SEC_LEVEL;
    631			sec.level = SEC_LEVEL_1;
    632		} else if (ext->alg == IW_ENCODE_ALG_TKIP) {
    633			sec.flags |= SEC_LEVEL;
    634			sec.level = SEC_LEVEL_2;
    635		} else if (ext->alg == IW_ENCODE_ALG_CCMP) {
    636			sec.flags |= SEC_LEVEL;
    637			sec.level = SEC_LEVEL_3;
    638		}
    639		/* Don't set sec level for group keys. */
    640		if (group_key)
    641			sec.flags &= ~SEC_LEVEL;
    642	}
    643done:
    644	if (ieee->set_security)
    645		ieee->set_security(ieee->dev, &sec);
    646
    647	if (ieee->reset_on_keychange &&
    648	    ieee->iw_mode != IW_MODE_INFRA &&
    649	    ieee->reset_port && ieee->reset_port(dev)) {
    650		netdev_dbg(ieee->dev, "Port reset failed\n");
    651		return -EINVAL;
    652	}
    653	return ret;
    654}
    655EXPORT_SYMBOL(rtllib_wx_set_encode_ext);
    656
    657int rtllib_wx_set_mlme(struct rtllib_device *ieee,
    658		       struct iw_request_info *info,
    659		       union iwreq_data *wrqu, char *extra)
    660{
    661	u8 i = 0;
    662	bool deauth = false;
    663	struct iw_mlme *mlme = (struct iw_mlme *)extra;
    664
    665	if (ieee->state != RTLLIB_LINKED)
    666		return -ENOLINK;
    667
    668	mutex_lock(&ieee->wx_mutex);
    669
    670	switch (mlme->cmd) {
    671	case IW_MLME_DEAUTH:
    672		deauth = true;
    673		fallthrough;
    674	case IW_MLME_DISASSOC:
    675		if (deauth)
    676			netdev_info(ieee->dev, "disauth packet !\n");
    677		else
    678			netdev_info(ieee->dev, "dis associate packet!\n");
    679
    680		ieee->cannot_notify = true;
    681
    682		SendDisassociation(ieee, deauth, mlme->reason_code);
    683		rtllib_disassociate(ieee);
    684
    685		ieee->wap_set = 0;
    686		for (i = 0; i < 6; i++)
    687			ieee->current_network.bssid[i] = 0x55;
    688
    689		ieee->ssid_set = 0;
    690		ieee->current_network.ssid[0] = '\0';
    691		ieee->current_network.ssid_len = 0;
    692		break;
    693	default:
    694		mutex_unlock(&ieee->wx_mutex);
    695		return -EOPNOTSUPP;
    696	}
    697
    698	mutex_unlock(&ieee->wx_mutex);
    699
    700	return 0;
    701}
    702EXPORT_SYMBOL(rtllib_wx_set_mlme);
    703
    704int rtllib_wx_set_auth(struct rtllib_device *ieee,
    705		       struct iw_request_info *info,
    706		       struct iw_param *data, char *extra)
    707{
    708	switch (data->flags & IW_AUTH_INDEX) {
    709	case IW_AUTH_WPA_VERSION:
    710		break;
    711	case IW_AUTH_CIPHER_PAIRWISE:
    712	case IW_AUTH_CIPHER_GROUP:
    713	case IW_AUTH_KEY_MGMT:
    714		/* Host AP driver does not use these parameters and allows
    715		 * wpa_supplicant to control them internally.
    716		 */
    717		break;
    718	case IW_AUTH_TKIP_COUNTERMEASURES:
    719		ieee->tkip_countermeasures = data->value;
    720		break;
    721	case IW_AUTH_DROP_UNENCRYPTED:
    722		ieee->drop_unencrypted = data->value;
    723		break;
    724
    725	case IW_AUTH_80211_AUTH_ALG:
    726		if (data->value & IW_AUTH_ALG_SHARED_KEY) {
    727			ieee->open_wep = 0;
    728			ieee->auth_mode = 1;
    729		} else if (data->value & IW_AUTH_ALG_OPEN_SYSTEM) {
    730			ieee->open_wep = 1;
    731			ieee->auth_mode = 0;
    732		} else if (data->value & IW_AUTH_ALG_LEAP) {
    733			ieee->open_wep = 1;
    734			ieee->auth_mode = 2;
    735		} else
    736			return -EINVAL;
    737		break;
    738
    739	case IW_AUTH_WPA_ENABLED:
    740		ieee->wpa_enabled = (data->value) ? 1 : 0;
    741		break;
    742
    743	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
    744		ieee->ieee802_1x = data->value;
    745		break;
    746	case IW_AUTH_PRIVACY_INVOKED:
    747		ieee->privacy_invoked = data->value;
    748		break;
    749	default:
    750		return -EOPNOTSUPP;
    751	}
    752	return 0;
    753}
    754EXPORT_SYMBOL(rtllib_wx_set_auth);
    755
    756int rtllib_wx_set_gen_ie(struct rtllib_device *ieee, u8 *ie, size_t len)
    757{
    758	u8 *buf;
    759	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
    760
    761	if (len > MAX_WPA_IE_LEN || (len && !ie))
    762		return -EINVAL;
    763
    764	if (len) {
    765		eid = ie[0];
    766		if ((eid == MFIE_TYPE_GENERIC) && (!memcmp(&ie[2], wps_oui, 4))) {
    767			ieee->wps_ie_len = min_t(size_t, len, MAX_WZC_IE_LEN);
    768			buf = kmemdup(ie, ieee->wps_ie_len, GFP_KERNEL);
    769			if (!buf)
    770				return -ENOMEM;
    771			ieee->wps_ie = buf;
    772			return 0;
    773		}
    774	}
    775	ieee->wps_ie_len = 0;
    776	kfree(ieee->wps_ie);
    777	ieee->wps_ie = NULL;
    778	if (len) {
    779		if (len != ie[1]+2)
    780			return -EINVAL;
    781		buf = kmemdup(ie, len, GFP_KERNEL);
    782		if (!buf)
    783			return -ENOMEM;
    784		kfree(ieee->wpa_ie);
    785		ieee->wpa_ie = buf;
    786		ieee->wpa_ie_len = len;
    787	} else {
    788		kfree(ieee->wpa_ie);
    789		ieee->wpa_ie = NULL;
    790		ieee->wpa_ie_len = 0;
    791	}
    792	return 0;
    793}
    794EXPORT_SYMBOL(rtllib_wx_set_gen_ie);