libipw_wx.c 21 KB

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