PageRenderTime 1393ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/ipw2x00/libipw_wx.c

https://github.com/Mengqi/linux-2.6
C | 769 lines | 587 code | 104 blank | 78 comment | 156 complexity | 6a2e2e4a4522fdc0f1c0ce2dc533f9c8 MD5 | raw file
  1. /******************************************************************************
  2. Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
  3. Portions of this file are based on the WEP enablement code provided by the
  4. Host AP project hostap-drivers v0.1.3
  5. Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. <j@w1.fi>
  7. Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
  8. This program is free software; you can redistribute it and/or modify it
  9. under the terms of version 2 of the GNU General Public License as
  10. published by the Free Software Foundation.
  11. This program is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc., 59
  17. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. The full GNU General Public License is included in this distribution in the
  19. file called LICENSE.
  20. Contact Information:
  21. Intel Linux Wireless <ilw@linux.intel.com>
  22. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  23. ******************************************************************************/
  24. #include <linux/hardirq.h>
  25. #include <linux/kmod.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/jiffies.h>
  29. #include <net/lib80211.h>
  30. #include <linux/wireless.h>
  31. #include "libipw.h"
  32. static const char *libipw_modes[] = {
  33. "?", "a", "b", "ab", "g", "ag", "bg", "abg"
  34. };
  35. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  36. {
  37. unsigned long end = jiffies;
  38. if (end >= start)
  39. return jiffies_to_msecs(end - start);
  40. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  41. }
  42. #define MAX_CUSTOM_LEN 64
  43. static char *libipw_translate_scan(struct libipw_device *ieee,
  44. char *start, char *stop,
  45. struct libipw_network *network,
  46. struct iw_request_info *info)
  47. {
  48. char custom[MAX_CUSTOM_LEN];
  49. char *p;
  50. struct iw_event iwe;
  51. int i, j;
  52. char *current_val; /* For rates */
  53. u8 rate;
  54. /* First entry *MUST* be the AP MAC address */
  55. iwe.cmd = SIOCGIWAP;
  56. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  57. memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
  58. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
  59. /* Remaining entries will be displayed in the order we provide them */
  60. /* Add the ESSID */
  61. iwe.cmd = SIOCGIWESSID;
  62. iwe.u.data.flags = 1;
  63. iwe.u.data.length = min(network->ssid_len, (u8) 32);
  64. start = iwe_stream_add_point(info, start, stop,
  65. &iwe, network->ssid);
  66. /* Add the protocol name */
  67. iwe.cmd = SIOCGIWNAME;
  68. snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s",
  69. libipw_modes[network->mode]);
  70. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
  71. /* Add mode */
  72. iwe.cmd = SIOCGIWMODE;
  73. if (network->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
  74. if (network->capability & WLAN_CAPABILITY_ESS)
  75. iwe.u.mode = IW_MODE_MASTER;
  76. else
  77. iwe.u.mode = IW_MODE_ADHOC;
  78. start = iwe_stream_add_event(info, start, stop,
  79. &iwe, IW_EV_UINT_LEN);
  80. }
  81. /* Add channel and frequency */
  82. /* Note : userspace automatically computes channel using iwrange */
  83. iwe.cmd = SIOCGIWFREQ;
  84. iwe.u.freq.m = libipw_channel_to_freq(ieee, network->channel);
  85. iwe.u.freq.e = 6;
  86. iwe.u.freq.i = 0;
  87. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
  88. /* Add encryption capability */
  89. iwe.cmd = SIOCGIWENCODE;
  90. if (network->capability & WLAN_CAPABILITY_PRIVACY)
  91. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  92. else
  93. iwe.u.data.flags = IW_ENCODE_DISABLED;
  94. iwe.u.data.length = 0;
  95. start = iwe_stream_add_point(info, start, stop,
  96. &iwe, network->ssid);
  97. /* Add basic and extended rates */
  98. /* Rate : stuffing multiple values in a single event require a bit
  99. * more of magic - Jean II */
  100. current_val = start + iwe_stream_lcp_len(info);
  101. iwe.cmd = SIOCGIWRATE;
  102. /* Those two flags are ignored... */
  103. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  104. for (i = 0, j = 0; i < network->rates_len;) {
  105. if (j < network->rates_ex_len &&
  106. ((network->rates_ex[j] & 0x7F) <
  107. (network->rates[i] & 0x7F)))
  108. rate = network->rates_ex[j++] & 0x7F;
  109. else
  110. rate = network->rates[i++] & 0x7F;
  111. /* Bit rate given in 500 kb/s units (+ 0x80) */
  112. iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
  113. /* Add new value to event */
  114. current_val = iwe_stream_add_value(info, start, current_val,
  115. stop, &iwe, IW_EV_PARAM_LEN);
  116. }
  117. for (; j < network->rates_ex_len; j++) {
  118. rate = network->rates_ex[j] & 0x7F;
  119. /* Bit rate given in 500 kb/s units (+ 0x80) */
  120. iwe.u.bitrate.value = ((rate & 0x7f) * 500000);
  121. /* Add new value to event */
  122. current_val = iwe_stream_add_value(info, start, current_val,
  123. stop, &iwe, IW_EV_PARAM_LEN);
  124. }
  125. /* Check if we added any rate */
  126. if ((current_val - start) > iwe_stream_lcp_len(info))
  127. start = current_val;
  128. /* Add quality statistics */
  129. iwe.cmd = IWEVQUAL;
  130. iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
  131. IW_QUAL_NOISE_UPDATED;
  132. if (!(network->stats.mask & LIBIPW_STATMASK_RSSI)) {
  133. iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID |
  134. IW_QUAL_LEVEL_INVALID;
  135. iwe.u.qual.qual = 0;
  136. } else {
  137. if (ieee->perfect_rssi == ieee->worst_rssi)
  138. iwe.u.qual.qual = 100;
  139. else
  140. iwe.u.qual.qual =
  141. (100 *
  142. (ieee->perfect_rssi - ieee->worst_rssi) *
  143. (ieee->perfect_rssi - ieee->worst_rssi) -
  144. (ieee->perfect_rssi - network->stats.rssi) *
  145. (15 * (ieee->perfect_rssi - ieee->worst_rssi) +
  146. 62 * (ieee->perfect_rssi -
  147. network->stats.rssi))) /
  148. ((ieee->perfect_rssi -
  149. ieee->worst_rssi) * (ieee->perfect_rssi -
  150. ieee->worst_rssi));
  151. if (iwe.u.qual.qual > 100)
  152. iwe.u.qual.qual = 100;
  153. else if (iwe.u.qual.qual < 1)
  154. iwe.u.qual.qual = 0;
  155. }
  156. if (!(network->stats.mask & LIBIPW_STATMASK_NOISE)) {
  157. iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
  158. iwe.u.qual.noise = 0;
  159. } else {
  160. iwe.u.qual.noise = network->stats.noise;
  161. }
  162. if (!(network->stats.mask & LIBIPW_STATMASK_SIGNAL)) {
  163. iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
  164. iwe.u.qual.level = 0;
  165. } else {
  166. iwe.u.qual.level = network->stats.signal;
  167. }
  168. start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
  169. iwe.cmd = IWEVCUSTOM;
  170. p = custom;
  171. iwe.u.data.length = p - custom;
  172. if (iwe.u.data.length)
  173. start = iwe_stream_add_point(info, start, stop, &iwe, custom);
  174. memset(&iwe, 0, sizeof(iwe));
  175. if (network->wpa_ie_len) {
  176. char buf[MAX_WPA_IE_LEN];
  177. memcpy(buf, network->wpa_ie, network->wpa_ie_len);
  178. iwe.cmd = IWEVGENIE;
  179. iwe.u.data.length = network->wpa_ie_len;
  180. start = iwe_stream_add_point(info, start, stop, &iwe, buf);
  181. }
  182. memset(&iwe, 0, sizeof(iwe));
  183. if (network->rsn_ie_len) {
  184. char buf[MAX_WPA_IE_LEN];
  185. memcpy(buf, network->rsn_ie, network->rsn_ie_len);
  186. iwe.cmd = IWEVGENIE;
  187. iwe.u.data.length = network->rsn_ie_len;
  188. start = iwe_stream_add_point(info, start, stop, &iwe, buf);
  189. }
  190. /* Add EXTRA: Age to display seconds since last beacon/probe response
  191. * for given network. */
  192. iwe.cmd = IWEVCUSTOM;
  193. p = custom;
  194. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  195. " Last beacon: %ums ago",
  196. elapsed_jiffies_msecs(network->last_scanned));
  197. iwe.u.data.length = p - custom;
  198. if (iwe.u.data.length)
  199. start = iwe_stream_add_point(info, start, stop, &iwe, custom);
  200. /* Add spectrum management information */
  201. iwe.cmd = -1;
  202. p = custom;
  203. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Channel flags: ");
  204. if (libipw_get_channel_flags(ieee, network->channel) &
  205. LIBIPW_CH_INVALID) {
  206. iwe.cmd = IWEVCUSTOM;
  207. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), "INVALID ");
  208. }
  209. if (libipw_get_channel_flags(ieee, network->channel) &
  210. LIBIPW_CH_RADAR_DETECT) {
  211. iwe.cmd = IWEVCUSTOM;
  212. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), "DFS ");
  213. }
  214. if (iwe.cmd == IWEVCUSTOM) {
  215. iwe.u.data.length = p - custom;
  216. start = iwe_stream_add_point(info, start, stop, &iwe, custom);
  217. }
  218. return start;
  219. }
  220. #define SCAN_ITEM_SIZE 128
  221. int libipw_wx_get_scan(struct libipw_device *ieee,
  222. struct iw_request_info *info,
  223. union iwreq_data *wrqu, char *extra)
  224. {
  225. struct libipw_network *network;
  226. unsigned long flags;
  227. int err = 0;
  228. char *ev = extra;
  229. char *stop = ev + wrqu->data.length;
  230. int i = 0;
  231. DECLARE_SSID_BUF(ssid);
  232. LIBIPW_DEBUG_WX("Getting scan\n");
  233. spin_lock_irqsave(&ieee->lock, flags);
  234. list_for_each_entry(network, &ieee->network_list, list) {
  235. i++;
  236. if (stop - ev < SCAN_ITEM_SIZE) {
  237. err = -E2BIG;
  238. break;
  239. }
  240. if (ieee->scan_age == 0 ||
  241. time_after(network->last_scanned + ieee->scan_age, jiffies))
  242. ev = libipw_translate_scan(ieee, ev, stop, network,
  243. info);
  244. else {
  245. LIBIPW_DEBUG_SCAN("Not showing network '%s ("
  246. "%pM)' due to age (%ums).\n",
  247. print_ssid(ssid, network->ssid,
  248. network->ssid_len),
  249. network->bssid,
  250. elapsed_jiffies_msecs(
  251. network->last_scanned));
  252. }
  253. }
  254. spin_unlock_irqrestore(&ieee->lock, flags);
  255. wrqu->data.length = ev - extra;
  256. wrqu->data.flags = 0;
  257. LIBIPW_DEBUG_WX("exit: %d networks returned.\n", i);
  258. return err;
  259. }
  260. int libipw_wx_set_encode(struct libipw_device *ieee,
  261. struct iw_request_info *info,
  262. union iwreq_data *wrqu, char *keybuf)
  263. {
  264. struct iw_point *erq = &(wrqu->encoding);
  265. struct net_device *dev = ieee->dev;
  266. struct libipw_security sec = {
  267. .flags = 0
  268. };
  269. int i, key, key_provided, len;
  270. struct lib80211_crypt_data **crypt;
  271. int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
  272. DECLARE_SSID_BUF(ssid);
  273. LIBIPW_DEBUG_WX("SET_ENCODE\n");
  274. key = erq->flags & IW_ENCODE_INDEX;
  275. if (key) {
  276. if (key > WEP_KEYS)
  277. return -EINVAL;
  278. key--;
  279. key_provided = 1;
  280. } else {
  281. key_provided = 0;
  282. key = ieee->crypt_info.tx_keyidx;
  283. }
  284. LIBIPW_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
  285. "provided" : "default");
  286. crypt = &ieee->crypt_info.crypt[key];
  287. if (erq->flags & IW_ENCODE_DISABLED) {
  288. if (key_provided && *crypt) {
  289. LIBIPW_DEBUG_WX("Disabling encryption on key %d.\n",
  290. key);
  291. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  292. } else
  293. LIBIPW_DEBUG_WX("Disabling encryption.\n");
  294. /* Check all the keys to see if any are still configured,
  295. * and if no key index was provided, de-init them all */
  296. for (i = 0; i < WEP_KEYS; i++) {
  297. if (ieee->crypt_info.crypt[i] != NULL) {
  298. if (key_provided)
  299. break;
  300. lib80211_crypt_delayed_deinit(&ieee->crypt_info,
  301. &ieee->crypt_info.crypt[i]);
  302. }
  303. }
  304. if (i == WEP_KEYS) {
  305. sec.enabled = 0;
  306. sec.encrypt = 0;
  307. sec.level = SEC_LEVEL_0;
  308. sec.flags |= SEC_ENABLED | SEC_LEVEL | SEC_ENCRYPT;
  309. }
  310. goto done;
  311. }
  312. sec.enabled = 1;
  313. sec.encrypt = 1;
  314. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  315. if (*crypt != NULL && (*crypt)->ops != NULL &&
  316. strcmp((*crypt)->ops->name, "WEP") != 0) {
  317. /* changing to use WEP; deinit previously used algorithm
  318. * on this key */
  319. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  320. }
  321. if (*crypt == NULL && host_crypto) {
  322. struct lib80211_crypt_data *new_crypt;
  323. /* take WEP into use */
  324. new_crypt = kzalloc(sizeof(struct lib80211_crypt_data),
  325. GFP_KERNEL);
  326. if (new_crypt == NULL)
  327. return -ENOMEM;
  328. new_crypt->ops = lib80211_get_crypto_ops("WEP");
  329. if (!new_crypt->ops) {
  330. request_module("lib80211_crypt_wep");
  331. new_crypt->ops = lib80211_get_crypto_ops("WEP");
  332. }
  333. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  334. new_crypt->priv = new_crypt->ops->init(key);
  335. if (!new_crypt->ops || !new_crypt->priv) {
  336. kfree(new_crypt);
  337. new_crypt = NULL;
  338. printk(KERN_WARNING "%s: could not initialize WEP: "
  339. "load module lib80211_crypt_wep\n", dev->name);
  340. return -EOPNOTSUPP;
  341. }
  342. *crypt = new_crypt;
  343. }
  344. /* If a new key was provided, set it up */
  345. if (erq->length > 0) {
  346. len = erq->length <= 5 ? 5 : 13;
  347. memcpy(sec.keys[key], keybuf, erq->length);
  348. if (len > erq->length)
  349. memset(sec.keys[key] + erq->length, 0,
  350. len - erq->length);
  351. LIBIPW_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
  352. key, print_ssid(ssid, sec.keys[key], len),
  353. erq->length, len);
  354. sec.key_sizes[key] = len;
  355. if (*crypt)
  356. (*crypt)->ops->set_key(sec.keys[key], len, NULL,
  357. (*crypt)->priv);
  358. sec.flags |= (1 << key);
  359. /* This ensures a key will be activated if no key is
  360. * explicitly set */
  361. if (key == sec.active_key)
  362. sec.flags |= SEC_ACTIVE_KEY;
  363. } else {
  364. if (host_crypto) {
  365. len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
  366. NULL, (*crypt)->priv);
  367. if (len == 0) {
  368. /* Set a default key of all 0 */
  369. LIBIPW_DEBUG_WX("Setting key %d to all "
  370. "zero.\n", key);
  371. memset(sec.keys[key], 0, 13);
  372. (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
  373. (*crypt)->priv);
  374. sec.key_sizes[key] = 13;
  375. sec.flags |= (1 << key);
  376. }
  377. }
  378. /* No key data - just set the default TX key index */
  379. if (key_provided) {
  380. LIBIPW_DEBUG_WX("Setting key %d to default Tx "
  381. "key.\n", key);
  382. ieee->crypt_info.tx_keyidx = key;
  383. sec.active_key = key;
  384. sec.flags |= SEC_ACTIVE_KEY;
  385. }
  386. }
  387. if (erq->flags & (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED)) {
  388. ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
  389. sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
  390. WLAN_AUTH_SHARED_KEY;
  391. sec.flags |= SEC_AUTH_MODE;
  392. LIBIPW_DEBUG_WX("Auth: %s\n",
  393. sec.auth_mode == WLAN_AUTH_OPEN ?
  394. "OPEN" : "SHARED KEY");
  395. }
  396. /* For now we just support WEP, so only set that security level...
  397. * TODO: When WPA is added this is one place that needs to change */
  398. sec.flags |= SEC_LEVEL;
  399. sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
  400. sec.encode_alg[key] = SEC_ALG_WEP;
  401. done:
  402. if (ieee->set_security)
  403. ieee->set_security(dev, &sec);
  404. /* Do not reset port if card is in Managed mode since resetting will
  405. * generate new IEEE 802.11 authentication which may end up in looping
  406. * with IEEE 802.1X. If your hardware requires a reset after WEP
  407. * configuration (for example... Prism2), implement the reset_port in
  408. * the callbacks structures used to initialize the 802.11 stack. */
  409. if (ieee->reset_on_keychange &&
  410. ieee->iw_mode != IW_MODE_INFRA &&
  411. ieee->reset_port && ieee->reset_port(dev)) {
  412. printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
  413. return -EINVAL;
  414. }
  415. return 0;
  416. }
  417. int libipw_wx_get_encode(struct libipw_device *ieee,
  418. struct iw_request_info *info,
  419. union iwreq_data *wrqu, char *keybuf)
  420. {
  421. struct iw_point *erq = &(wrqu->encoding);
  422. int len, key;
  423. struct lib80211_crypt_data *crypt;
  424. struct libipw_security *sec = &ieee->sec;
  425. LIBIPW_DEBUG_WX("GET_ENCODE\n");
  426. key = erq->flags & IW_ENCODE_INDEX;
  427. if (key) {
  428. if (key > WEP_KEYS)
  429. return -EINVAL;
  430. key--;
  431. } else
  432. key = ieee->crypt_info.tx_keyidx;
  433. crypt = ieee->crypt_info.crypt[key];
  434. erq->flags = key + 1;
  435. if (!sec->enabled) {
  436. erq->length = 0;
  437. erq->flags |= IW_ENCODE_DISABLED;
  438. return 0;
  439. }
  440. len = sec->key_sizes[key];
  441. memcpy(keybuf, sec->keys[key], len);
  442. erq->length = len;
  443. erq->flags |= IW_ENCODE_ENABLED;
  444. if (ieee->open_wep)
  445. erq->flags |= IW_ENCODE_OPEN;
  446. else
  447. erq->flags |= IW_ENCODE_RESTRICTED;
  448. return 0;
  449. }
  450. int libipw_wx_set_encodeext(struct libipw_device *ieee,
  451. struct iw_request_info *info,
  452. union iwreq_data *wrqu, char *extra)
  453. {
  454. struct net_device *dev = ieee->dev;
  455. struct iw_point *encoding = &wrqu->encoding;
  456. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  457. int i, idx, ret = 0;
  458. int group_key = 0;
  459. const char *alg, *module;
  460. struct lib80211_crypto_ops *ops;
  461. struct lib80211_crypt_data **crypt;
  462. struct libipw_security sec = {
  463. .flags = 0,
  464. };
  465. idx = encoding->flags & IW_ENCODE_INDEX;
  466. if (idx) {
  467. if (idx < 1 || idx > WEP_KEYS)
  468. return -EINVAL;
  469. idx--;
  470. } else
  471. idx = ieee->crypt_info.tx_keyidx;
  472. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
  473. crypt = &ieee->crypt_info.crypt[idx];
  474. group_key = 1;
  475. } else {
  476. /* some Cisco APs use idx>0 for unicast in dynamic WEP */
  477. if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
  478. return -EINVAL;
  479. if (ieee->iw_mode == IW_MODE_INFRA)
  480. crypt = &ieee->crypt_info.crypt[idx];
  481. else
  482. return -EINVAL;
  483. }
  484. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  485. if ((encoding->flags & IW_ENCODE_DISABLED) ||
  486. ext->alg == IW_ENCODE_ALG_NONE) {
  487. if (*crypt)
  488. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  489. for (i = 0; i < WEP_KEYS; i++)
  490. if (ieee->crypt_info.crypt[i] != NULL)
  491. break;
  492. if (i == WEP_KEYS) {
  493. sec.enabled = 0;
  494. sec.encrypt = 0;
  495. sec.level = SEC_LEVEL_0;
  496. sec.flags |= SEC_LEVEL;
  497. }
  498. goto done;
  499. }
  500. sec.enabled = 1;
  501. sec.encrypt = 1;
  502. if (group_key ? !ieee->host_mc_decrypt :
  503. !(ieee->host_encrypt || ieee->host_decrypt ||
  504. ieee->host_encrypt_msdu))
  505. goto skip_host_crypt;
  506. switch (ext->alg) {
  507. case IW_ENCODE_ALG_WEP:
  508. alg = "WEP";
  509. module = "lib80211_crypt_wep";
  510. break;
  511. case IW_ENCODE_ALG_TKIP:
  512. alg = "TKIP";
  513. module = "lib80211_crypt_tkip";
  514. break;
  515. case IW_ENCODE_ALG_CCMP:
  516. alg = "CCMP";
  517. module = "lib80211_crypt_ccmp";
  518. break;
  519. default:
  520. LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
  521. dev->name, ext->alg);
  522. ret = -EINVAL;
  523. goto done;
  524. }
  525. ops = lib80211_get_crypto_ops(alg);
  526. if (ops == NULL) {
  527. request_module(module);
  528. ops = lib80211_get_crypto_ops(alg);
  529. }
  530. if (ops == NULL) {
  531. LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
  532. dev->name, ext->alg);
  533. ret = -EINVAL;
  534. goto done;
  535. }
  536. if (*crypt == NULL || (*crypt)->ops != ops) {
  537. struct lib80211_crypt_data *new_crypt;
  538. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  539. new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
  540. if (new_crypt == NULL) {
  541. ret = -ENOMEM;
  542. goto done;
  543. }
  544. new_crypt->ops = ops;
  545. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  546. new_crypt->priv = new_crypt->ops->init(idx);
  547. if (new_crypt->priv == NULL) {
  548. kfree(new_crypt);
  549. ret = -EINVAL;
  550. goto done;
  551. }
  552. *crypt = new_crypt;
  553. }
  554. if (ext->key_len > 0 && (*crypt)->ops->set_key &&
  555. (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
  556. (*crypt)->priv) < 0) {
  557. LIBIPW_DEBUG_WX("%s: key setting failed\n", dev->name);
  558. ret = -EINVAL;
  559. goto done;
  560. }
  561. skip_host_crypt:
  562. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
  563. ieee->crypt_info.tx_keyidx = idx;
  564. sec.active_key = idx;
  565. sec.flags |= SEC_ACTIVE_KEY;
  566. }
  567. if (ext->alg != IW_ENCODE_ALG_NONE) {
  568. memcpy(sec.keys[idx], ext->key, ext->key_len);
  569. sec.key_sizes[idx] = ext->key_len;
  570. sec.flags |= (1 << idx);
  571. if (ext->alg == IW_ENCODE_ALG_WEP) {
  572. sec.encode_alg[idx] = SEC_ALG_WEP;
  573. sec.flags |= SEC_LEVEL;
  574. sec.level = SEC_LEVEL_1;
  575. } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
  576. sec.encode_alg[idx] = SEC_ALG_TKIP;
  577. sec.flags |= SEC_LEVEL;
  578. sec.level = SEC_LEVEL_2;
  579. } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
  580. sec.encode_alg[idx] = SEC_ALG_CCMP;
  581. sec.flags |= SEC_LEVEL;
  582. sec.level = SEC_LEVEL_3;
  583. }
  584. /* Don't set sec level for group keys. */
  585. if (group_key)
  586. sec.flags &= ~SEC_LEVEL;
  587. }
  588. done:
  589. if (ieee->set_security)
  590. ieee->set_security(ieee->dev, &sec);
  591. /*
  592. * Do not reset port if card is in Managed mode since resetting will
  593. * generate new IEEE 802.11 authentication which may end up in looping
  594. * with IEEE 802.1X. If your hardware requires a reset after WEP
  595. * configuration (for example... Prism2), implement the reset_port in
  596. * the callbacks structures used to initialize the 802.11 stack.
  597. */
  598. if (ieee->reset_on_keychange &&
  599. ieee->iw_mode != IW_MODE_INFRA &&
  600. ieee->reset_port && ieee->reset_port(dev)) {
  601. LIBIPW_DEBUG_WX("%s: reset_port failed\n", dev->name);
  602. return -EINVAL;
  603. }
  604. return ret;
  605. }
  606. int libipw_wx_get_encodeext(struct libipw_device *ieee,
  607. struct iw_request_info *info,
  608. union iwreq_data *wrqu, char *extra)
  609. {
  610. struct iw_point *encoding = &wrqu->encoding;
  611. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  612. struct libipw_security *sec = &ieee->sec;
  613. int idx, max_key_len;
  614. max_key_len = encoding->length - sizeof(*ext);
  615. if (max_key_len < 0)
  616. return -EINVAL;
  617. idx = encoding->flags & IW_ENCODE_INDEX;
  618. if (idx) {
  619. if (idx < 1 || idx > WEP_KEYS)
  620. return -EINVAL;
  621. idx--;
  622. } else
  623. idx = ieee->crypt_info.tx_keyidx;
  624. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) &&
  625. ext->alg != IW_ENCODE_ALG_WEP)
  626. if (idx != 0 || ieee->iw_mode != IW_MODE_INFRA)
  627. return -EINVAL;
  628. encoding->flags = idx + 1;
  629. memset(ext, 0, sizeof(*ext));
  630. if (!sec->enabled) {
  631. ext->alg = IW_ENCODE_ALG_NONE;
  632. ext->key_len = 0;
  633. encoding->flags |= IW_ENCODE_DISABLED;
  634. } else {
  635. if (sec->encode_alg[idx] == SEC_ALG_WEP)
  636. ext->alg = IW_ENCODE_ALG_WEP;
  637. else if (sec->encode_alg[idx] == SEC_ALG_TKIP)
  638. ext->alg = IW_ENCODE_ALG_TKIP;
  639. else if (sec->encode_alg[idx] == SEC_ALG_CCMP)
  640. ext->alg = IW_ENCODE_ALG_CCMP;
  641. else
  642. return -EINVAL;
  643. ext->key_len = sec->key_sizes[idx];
  644. memcpy(ext->key, sec->keys[idx], ext->key_len);
  645. encoding->flags |= IW_ENCODE_ENABLED;
  646. if (ext->key_len &&
  647. (ext->alg == IW_ENCODE_ALG_TKIP ||
  648. ext->alg == IW_ENCODE_ALG_CCMP))
  649. ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
  650. }
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(libipw_wx_set_encodeext);
  654. EXPORT_SYMBOL(libipw_wx_get_encodeext);
  655. EXPORT_SYMBOL(libipw_wx_get_scan);
  656. EXPORT_SYMBOL(libipw_wx_set_encode);
  657. EXPORT_SYMBOL(libipw_wx_get_encode);