libipw_wx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. LIBIPW_DEBUG_WX("Getting scan\n");
  232. spin_lock_irqsave(&ieee->lock, flags);
  233. list_for_each_entry(network, &ieee->network_list, list) {
  234. i++;
  235. if (stop - ev < SCAN_ITEM_SIZE) {
  236. err = -E2BIG;
  237. break;
  238. }
  239. if (ieee->scan_age == 0 ||
  240. time_after(network->last_scanned + ieee->scan_age, jiffies))
  241. ev = libipw_translate_scan(ieee, ev, stop, network,
  242. info);
  243. else {
  244. LIBIPW_DEBUG_SCAN("Not showing network '%*pE (%pM)' due to age (%ums).\n",
  245. network->ssid_len, network->ssid,
  246. network->bssid,
  247. elapsed_jiffies_msecs(
  248. network->last_scanned));
  249. }
  250. }
  251. spin_unlock_irqrestore(&ieee->lock, flags);
  252. wrqu->data.length = ev - extra;
  253. wrqu->data.flags = 0;
  254. LIBIPW_DEBUG_WX("exit: %d networks returned.\n", i);
  255. return err;
  256. }
  257. int libipw_wx_set_encode(struct libipw_device *ieee,
  258. struct iw_request_info *info,
  259. union iwreq_data *wrqu, char *keybuf)
  260. {
  261. struct iw_point *erq = &(wrqu->encoding);
  262. struct net_device *dev = ieee->dev;
  263. struct libipw_security sec = {
  264. .flags = 0
  265. };
  266. int i, key, key_provided, len;
  267. struct lib80211_crypt_data **crypt;
  268. int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
  269. LIBIPW_DEBUG_WX("SET_ENCODE\n");
  270. key = erq->flags & IW_ENCODE_INDEX;
  271. if (key) {
  272. if (key > WEP_KEYS)
  273. return -EINVAL;
  274. key--;
  275. key_provided = 1;
  276. } else {
  277. key_provided = 0;
  278. key = ieee->crypt_info.tx_keyidx;
  279. }
  280. LIBIPW_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
  281. "provided" : "default");
  282. crypt = &ieee->crypt_info.crypt[key];
  283. if (erq->flags & IW_ENCODE_DISABLED) {
  284. if (key_provided && *crypt) {
  285. LIBIPW_DEBUG_WX("Disabling encryption on key %d.\n",
  286. key);
  287. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  288. } else
  289. LIBIPW_DEBUG_WX("Disabling encryption.\n");
  290. /* Check all the keys to see if any are still configured,
  291. * and if no key index was provided, de-init them all */
  292. for (i = 0; i < WEP_KEYS; i++) {
  293. if (ieee->crypt_info.crypt[i] != NULL) {
  294. if (key_provided)
  295. break;
  296. lib80211_crypt_delayed_deinit(&ieee->crypt_info,
  297. &ieee->crypt_info.crypt[i]);
  298. }
  299. }
  300. if (i == WEP_KEYS) {
  301. sec.enabled = 0;
  302. sec.encrypt = 0;
  303. sec.level = SEC_LEVEL_0;
  304. sec.flags |= SEC_ENABLED | SEC_LEVEL | SEC_ENCRYPT;
  305. }
  306. goto done;
  307. }
  308. sec.enabled = 1;
  309. sec.encrypt = 1;
  310. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  311. if (*crypt != NULL && (*crypt)->ops != NULL &&
  312. strcmp((*crypt)->ops->name, "WEP") != 0) {
  313. /* changing to use WEP; deinit previously used algorithm
  314. * on this key */
  315. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  316. }
  317. if (*crypt == NULL && host_crypto) {
  318. struct lib80211_crypt_data *new_crypt;
  319. /* take WEP into use */
  320. new_crypt = kzalloc(sizeof(struct lib80211_crypt_data),
  321. GFP_KERNEL);
  322. if (new_crypt == NULL)
  323. return -ENOMEM;
  324. new_crypt->ops = lib80211_get_crypto_ops("WEP");
  325. if (!new_crypt->ops) {
  326. request_module("lib80211_crypt_wep");
  327. new_crypt->ops = lib80211_get_crypto_ops("WEP");
  328. }
  329. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  330. new_crypt->priv = new_crypt->ops->init(key);
  331. if (!new_crypt->ops || !new_crypt->priv) {
  332. kfree(new_crypt);
  333. new_crypt = NULL;
  334. printk(KERN_WARNING "%s: could not initialize WEP: "
  335. "load module lib80211_crypt_wep\n", dev->name);
  336. return -EOPNOTSUPP;
  337. }
  338. *crypt = new_crypt;
  339. }
  340. /* If a new key was provided, set it up */
  341. if (erq->length > 0) {
  342. len = erq->length <= 5 ? 5 : 13;
  343. memcpy(sec.keys[key], keybuf, erq->length);
  344. if (len > erq->length)
  345. memset(sec.keys[key] + erq->length, 0,
  346. len - erq->length);
  347. LIBIPW_DEBUG_WX("Setting key %d to '%*pE' (%d:%d bytes)\n",
  348. key, len, sec.keys[key],
  349. erq->length, len);
  350. sec.key_sizes[key] = len;
  351. if (*crypt)
  352. (*crypt)->ops->set_key(sec.keys[key], len, NULL,
  353. (*crypt)->priv);
  354. sec.flags |= (1 << key);
  355. /* This ensures a key will be activated if no key is
  356. * explicitly set */
  357. if (key == sec.active_key)
  358. sec.flags |= SEC_ACTIVE_KEY;
  359. } else {
  360. if (host_crypto) {
  361. len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
  362. NULL, (*crypt)->priv);
  363. if (len == 0) {
  364. /* Set a default key of all 0 */
  365. LIBIPW_DEBUG_WX("Setting key %d to all "
  366. "zero.\n", key);
  367. memset(sec.keys[key], 0, 13);
  368. (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
  369. (*crypt)->priv);
  370. sec.key_sizes[key] = 13;
  371. sec.flags |= (1 << key);
  372. }
  373. }
  374. /* No key data - just set the default TX key index */
  375. if (key_provided) {
  376. LIBIPW_DEBUG_WX("Setting key %d to default Tx "
  377. "key.\n", key);
  378. ieee->crypt_info.tx_keyidx = key;
  379. sec.active_key = key;
  380. sec.flags |= SEC_ACTIVE_KEY;
  381. }
  382. }
  383. if (erq->flags & (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED)) {
  384. ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
  385. sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
  386. WLAN_AUTH_SHARED_KEY;
  387. sec.flags |= SEC_AUTH_MODE;
  388. LIBIPW_DEBUG_WX("Auth: %s\n",
  389. sec.auth_mode == WLAN_AUTH_OPEN ?
  390. "OPEN" : "SHARED KEY");
  391. }
  392. /* For now we just support WEP, so only set that security level...
  393. * TODO: When WPA is added this is one place that needs to change */
  394. sec.flags |= SEC_LEVEL;
  395. sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
  396. sec.encode_alg[key] = SEC_ALG_WEP;
  397. done:
  398. if (ieee->set_security)
  399. ieee->set_security(dev, &sec);
  400. return 0;
  401. }
  402. int libipw_wx_get_encode(struct libipw_device *ieee,
  403. struct iw_request_info *info,
  404. union iwreq_data *wrqu, char *keybuf)
  405. {
  406. struct iw_point *erq = &(wrqu->encoding);
  407. int len, key;
  408. struct lib80211_crypt_data *crypt;
  409. struct libipw_security *sec = &ieee->sec;
  410. LIBIPW_DEBUG_WX("GET_ENCODE\n");
  411. key = erq->flags & IW_ENCODE_INDEX;
  412. if (key) {
  413. if (key > WEP_KEYS)
  414. return -EINVAL;
  415. key--;
  416. } else
  417. key = ieee->crypt_info.tx_keyidx;
  418. crypt = ieee->crypt_info.crypt[key];
  419. erq->flags = key + 1;
  420. if (!sec->enabled) {
  421. erq->length = 0;
  422. erq->flags |= IW_ENCODE_DISABLED;
  423. return 0;
  424. }
  425. len = sec->key_sizes[key];
  426. memcpy(keybuf, sec->keys[key], len);
  427. erq->length = len;
  428. erq->flags |= IW_ENCODE_ENABLED;
  429. if (ieee->open_wep)
  430. erq->flags |= IW_ENCODE_OPEN;
  431. else
  432. erq->flags |= IW_ENCODE_RESTRICTED;
  433. return 0;
  434. }
  435. int libipw_wx_set_encodeext(struct libipw_device *ieee,
  436. struct iw_request_info *info,
  437. union iwreq_data *wrqu, char *extra)
  438. {
  439. struct net_device *dev = ieee->dev;
  440. struct iw_point *encoding = &wrqu->encoding;
  441. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  442. int i, idx, ret = 0;
  443. int group_key = 0;
  444. const char *alg, *module;
  445. struct lib80211_crypto_ops *ops;
  446. struct lib80211_crypt_data **crypt;
  447. struct libipw_security sec = {
  448. .flags = 0,
  449. };
  450. idx = encoding->flags & IW_ENCODE_INDEX;
  451. if (idx) {
  452. if (idx < 1 || idx > WEP_KEYS)
  453. return -EINVAL;
  454. idx--;
  455. } else
  456. idx = ieee->crypt_info.tx_keyidx;
  457. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
  458. crypt = &ieee->crypt_info.crypt[idx];
  459. group_key = 1;
  460. } else {
  461. /* some Cisco APs use idx>0 for unicast in dynamic WEP */
  462. if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
  463. return -EINVAL;
  464. if (ieee->iw_mode == IW_MODE_INFRA)
  465. crypt = &ieee->crypt_info.crypt[idx];
  466. else
  467. return -EINVAL;
  468. }
  469. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  470. if ((encoding->flags & IW_ENCODE_DISABLED) ||
  471. ext->alg == IW_ENCODE_ALG_NONE) {
  472. if (*crypt)
  473. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  474. for (i = 0; i < WEP_KEYS; i++)
  475. if (ieee->crypt_info.crypt[i] != NULL)
  476. break;
  477. if (i == WEP_KEYS) {
  478. sec.enabled = 0;
  479. sec.encrypt = 0;
  480. sec.level = SEC_LEVEL_0;
  481. sec.flags |= SEC_LEVEL;
  482. }
  483. goto done;
  484. }
  485. sec.enabled = 1;
  486. sec.encrypt = 1;
  487. if (group_key ? !ieee->host_mc_decrypt :
  488. !(ieee->host_encrypt || ieee->host_decrypt ||
  489. ieee->host_encrypt_msdu))
  490. goto skip_host_crypt;
  491. switch (ext->alg) {
  492. case IW_ENCODE_ALG_WEP:
  493. alg = "WEP";
  494. module = "lib80211_crypt_wep";
  495. break;
  496. case IW_ENCODE_ALG_TKIP:
  497. alg = "TKIP";
  498. module = "lib80211_crypt_tkip";
  499. break;
  500. case IW_ENCODE_ALG_CCMP:
  501. alg = "CCMP";
  502. module = "lib80211_crypt_ccmp";
  503. break;
  504. default:
  505. LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
  506. dev->name, ext->alg);
  507. ret = -EINVAL;
  508. goto done;
  509. }
  510. ops = lib80211_get_crypto_ops(alg);
  511. if (ops == NULL) {
  512. request_module(module);
  513. ops = lib80211_get_crypto_ops(alg);
  514. }
  515. if (ops == NULL) {
  516. LIBIPW_DEBUG_WX("%s: unknown crypto alg %d\n",
  517. dev->name, ext->alg);
  518. ret = -EINVAL;
  519. goto done;
  520. }
  521. if (*crypt == NULL || (*crypt)->ops != ops) {
  522. struct lib80211_crypt_data *new_crypt;
  523. lib80211_crypt_delayed_deinit(&ieee->crypt_info, crypt);
  524. new_crypt = kzalloc(sizeof(*new_crypt), GFP_KERNEL);
  525. if (new_crypt == NULL) {
  526. ret = -ENOMEM;
  527. goto done;
  528. }
  529. new_crypt->ops = ops;
  530. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  531. new_crypt->priv = new_crypt->ops->init(idx);
  532. if (new_crypt->priv == NULL) {
  533. kfree(new_crypt);
  534. ret = -EINVAL;
  535. goto done;
  536. }
  537. *crypt = new_crypt;
  538. }
  539. if (ext->key_len > 0 && (*crypt)->ops->set_key &&
  540. (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
  541. (*crypt)->priv) < 0) {
  542. LIBIPW_DEBUG_WX("%s: key setting failed\n", dev->name);
  543. ret = -EINVAL;
  544. goto done;
  545. }
  546. skip_host_crypt:
  547. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
  548. ieee->crypt_info.tx_keyidx = idx;
  549. sec.active_key = idx;
  550. sec.flags |= SEC_ACTIVE_KEY;
  551. }
  552. if (ext->alg != IW_ENCODE_ALG_NONE) {
  553. memcpy(sec.keys[idx], ext->key, ext->key_len);
  554. sec.key_sizes[idx] = ext->key_len;
  555. sec.flags |= (1 << idx);
  556. if (ext->alg == IW_ENCODE_ALG_WEP) {
  557. sec.encode_alg[idx] = SEC_ALG_WEP;
  558. sec.flags |= SEC_LEVEL;
  559. sec.level = SEC_LEVEL_1;
  560. } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
  561. sec.encode_alg[idx] = SEC_ALG_TKIP;
  562. sec.flags |= SEC_LEVEL;
  563. sec.level = SEC_LEVEL_2;
  564. } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
  565. sec.encode_alg[idx] = SEC_ALG_CCMP;
  566. sec.flags |= SEC_LEVEL;
  567. sec.level = SEC_LEVEL_3;
  568. }
  569. /* Don't set sec level for group keys. */
  570. if (group_key)
  571. sec.flags &= ~SEC_LEVEL;
  572. }
  573. done:
  574. if (ieee->set_security)
  575. ieee->set_security(dev, &sec);
  576. return ret;
  577. }
  578. int libipw_wx_get_encodeext(struct libipw_device *ieee,
  579. struct iw_request_info *info,
  580. union iwreq_data *wrqu, char *extra)
  581. {
  582. struct iw_point *encoding = &wrqu->encoding;
  583. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  584. struct libipw_security *sec = &ieee->sec;
  585. int idx, max_key_len;
  586. max_key_len = encoding->length - sizeof(*ext);
  587. if (max_key_len < 0)
  588. return -EINVAL;
  589. idx = encoding->flags & IW_ENCODE_INDEX;
  590. if (idx) {
  591. if (idx < 1 || idx > WEP_KEYS)
  592. return -EINVAL;
  593. idx--;
  594. } else
  595. idx = ieee->crypt_info.tx_keyidx;
  596. if (!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) &&
  597. ext->alg != IW_ENCODE_ALG_WEP)
  598. if (idx != 0 || ieee->iw_mode != IW_MODE_INFRA)
  599. return -EINVAL;
  600. encoding->flags = idx + 1;
  601. memset(ext, 0, sizeof(*ext));
  602. if (!sec->enabled) {
  603. ext->alg = IW_ENCODE_ALG_NONE;
  604. ext->key_len = 0;
  605. encoding->flags |= IW_ENCODE_DISABLED;
  606. } else {
  607. if (sec->encode_alg[idx] == SEC_ALG_WEP)
  608. ext->alg = IW_ENCODE_ALG_WEP;
  609. else if (sec->encode_alg[idx] == SEC_ALG_TKIP)
  610. ext->alg = IW_ENCODE_ALG_TKIP;
  611. else if (sec->encode_alg[idx] == SEC_ALG_CCMP)
  612. ext->alg = IW_ENCODE_ALG_CCMP;
  613. else
  614. return -EINVAL;
  615. ext->key_len = sec->key_sizes[idx];
  616. memcpy(ext->key, sec->keys[idx], ext->key_len);
  617. encoding->flags |= IW_ENCODE_ENABLED;
  618. if (ext->key_len &&
  619. (ext->alg == IW_ENCODE_ALG_TKIP ||
  620. ext->alg == IW_ENCODE_ALG_CCMP))
  621. ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
  622. }
  623. return 0;
  624. }
  625. EXPORT_SYMBOL(libipw_wx_set_encodeext);
  626. EXPORT_SYMBOL(libipw_wx_get_encodeext);
  627. EXPORT_SYMBOL(libipw_wx_get_scan);
  628. EXPORT_SYMBOL(libipw_wx_set_encode);
  629. EXPORT_SYMBOL(libipw_wx_get_encode);