ieee80211_wx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. <jkmaline@cc.hut.fi>
  7. Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.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/ieee80211.h>
  28. #include <linux/wireless.h>
  29. static const char *ieee80211_modes[] = {
  30. "?", "a", "b", "ab", "g", "ag", "bg", "abg"
  31. };
  32. #define MAX_CUSTOM_LEN 64
  33. static char *ieee80211_translate_scan(struct ieee80211_device *ieee,
  34. char *start, char *stop,
  35. struct ieee80211_network *network)
  36. {
  37. char custom[MAX_CUSTOM_LEN];
  38. char *p;
  39. struct iw_event iwe;
  40. int i, j;
  41. u8 max_rate, rate;
  42. /* First entry *MUST* be the AP MAC address */
  43. iwe.cmd = SIOCGIWAP;
  44. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  45. memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
  46. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  47. /* Remaining entries will be displayed in the order we provide them */
  48. /* Add the ESSID */
  49. iwe.cmd = SIOCGIWESSID;
  50. iwe.u.data.flags = 1;
  51. if (network->flags & NETWORK_EMPTY_ESSID) {
  52. iwe.u.data.length = sizeof("<hidden>");
  53. start = iwe_stream_add_point(start, stop, &iwe, "<hidden>");
  54. } else {
  55. iwe.u.data.length = min(network->ssid_len, (u8) 32);
  56. start = iwe_stream_add_point(start, stop, &iwe, network->ssid);
  57. }
  58. /* Add the protocol name */
  59. iwe.cmd = SIOCGIWNAME;
  60. snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11%s",
  61. ieee80211_modes[network->mode]);
  62. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_CHAR_LEN);
  63. /* Add mode */
  64. iwe.cmd = SIOCGIWMODE;
  65. if (network->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
  66. if (network->capability & WLAN_CAPABILITY_ESS)
  67. iwe.u.mode = IW_MODE_MASTER;
  68. else
  69. iwe.u.mode = IW_MODE_ADHOC;
  70. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  71. }
  72. /* Add frequency/channel */
  73. iwe.cmd = SIOCGIWFREQ;
  74. /* iwe.u.freq.m = ieee80211_frequency(network->channel, network->mode);
  75. iwe.u.freq.e = 3; */
  76. iwe.u.freq.m = network->channel;
  77. iwe.u.freq.e = 0;
  78. iwe.u.freq.i = 0;
  79. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  80. /* Add encryption capability */
  81. iwe.cmd = SIOCGIWENCODE;
  82. if (network->capability & WLAN_CAPABILITY_PRIVACY)
  83. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  84. else
  85. iwe.u.data.flags = IW_ENCODE_DISABLED;
  86. iwe.u.data.length = 0;
  87. start = iwe_stream_add_point(start, stop, &iwe, network->ssid);
  88. /* Add basic and extended rates */
  89. max_rate = 0;
  90. p = custom;
  91. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
  92. for (i = 0, j = 0; i < network->rates_len;) {
  93. if (j < network->rates_ex_len &&
  94. ((network->rates_ex[j] & 0x7F) <
  95. (network->rates[i] & 0x7F)))
  96. rate = network->rates_ex[j++] & 0x7F;
  97. else
  98. rate = network->rates[i++] & 0x7F;
  99. if (rate > max_rate)
  100. max_rate = rate;
  101. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  102. "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
  103. }
  104. for (; j < network->rates_ex_len; j++) {
  105. rate = network->rates_ex[j] & 0x7F;
  106. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  107. "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
  108. if (rate > max_rate)
  109. max_rate = rate;
  110. }
  111. iwe.cmd = SIOCGIWRATE;
  112. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  113. iwe.u.bitrate.value = max_rate * 500000;
  114. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_PARAM_LEN);
  115. iwe.cmd = IWEVCUSTOM;
  116. iwe.u.data.length = p - custom;
  117. if (iwe.u.data.length)
  118. start = iwe_stream_add_point(start, stop, &iwe, custom);
  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(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(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(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(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(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(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. IEEE80211_DEBUG_WX("Getting scan\n");
  223. spin_lock_irqsave(&ieee->lock, flags);
  224. list_for_each_entry(network, &ieee->network_list, list) {
  225. i++;
  226. if (stop - ev < SCAN_ITEM_SIZE) {
  227. err = -E2BIG;
  228. break;
  229. }
  230. if (ieee->scan_age == 0 ||
  231. time_after(network->last_scanned + ieee->scan_age, jiffies))
  232. ev = ieee80211_translate_scan(ieee, ev, stop, network);
  233. else
  234. IEEE80211_DEBUG_SCAN("Not showing network '%s ("
  235. MAC_FMT ")' due to age (%dms).\n",
  236. escape_essid(network->ssid,
  237. network->ssid_len),
  238. MAC_ARG(network->bssid),
  239. jiffies_to_msecs(jiffies -
  240. network->
  241. last_scanned));
  242. }
  243. spin_unlock_irqrestore(&ieee->lock, flags);
  244. wrqu->data.length = ev - extra;
  245. wrqu->data.flags = 0;
  246. IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
  247. return err;
  248. }
  249. int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
  250. struct iw_request_info *info,
  251. union iwreq_data *wrqu, char *keybuf)
  252. {
  253. struct iw_point *erq = &(wrqu->encoding);
  254. struct net_device *dev = ieee->dev;
  255. struct ieee80211_security sec = {
  256. .flags = 0
  257. };
  258. int i, key, key_provided, len;
  259. struct ieee80211_crypt_data **crypt;
  260. int host_crypto = ieee->host_encrypt || ieee->host_decrypt || ieee->host_build_iv;
  261. IEEE80211_DEBUG_WX("SET_ENCODE\n");
  262. key = erq->flags & IW_ENCODE_INDEX;
  263. if (key) {
  264. if (key > WEP_KEYS)
  265. return -EINVAL;
  266. key--;
  267. key_provided = 1;
  268. } else {
  269. key_provided = 0;
  270. key = ieee->tx_keyidx;
  271. }
  272. IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
  273. "provided" : "default");
  274. crypt = &ieee->crypt[key];
  275. if (erq->flags & IW_ENCODE_DISABLED) {
  276. if (key_provided && *crypt) {
  277. IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
  278. key);
  279. ieee80211_crypt_delayed_deinit(ieee, crypt);
  280. } else
  281. IEEE80211_DEBUG_WX("Disabling encryption.\n");
  282. /* Check all the keys to see if any are still configured,
  283. * and if no key index was provided, de-init them all */
  284. for (i = 0; i < WEP_KEYS; i++) {
  285. if (ieee->crypt[i] != NULL) {
  286. if (key_provided)
  287. break;
  288. ieee80211_crypt_delayed_deinit(ieee,
  289. &ieee->crypt[i]);
  290. }
  291. }
  292. if (i == WEP_KEYS) {
  293. sec.enabled = 0;
  294. sec.encrypt = 0;
  295. sec.level = SEC_LEVEL_0;
  296. sec.flags |= SEC_ENABLED | SEC_LEVEL | SEC_ENCRYPT;
  297. }
  298. goto done;
  299. }
  300. sec.enabled = 1;
  301. sec.encrypt = 1;
  302. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  303. if (*crypt != NULL && (*crypt)->ops != NULL &&
  304. strcmp((*crypt)->ops->name, "WEP") != 0) {
  305. /* changing to use WEP; deinit previously used algorithm
  306. * on this key */
  307. ieee80211_crypt_delayed_deinit(ieee, crypt);
  308. }
  309. if (*crypt == NULL && host_crypto) {
  310. struct ieee80211_crypt_data *new_crypt;
  311. /* take WEP into use */
  312. new_crypt = kmalloc(sizeof(struct ieee80211_crypt_data),
  313. GFP_KERNEL);
  314. if (new_crypt == NULL)
  315. return -ENOMEM;
  316. memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
  317. new_crypt->ops = ieee80211_get_crypto_ops("WEP");
  318. if (!new_crypt->ops) {
  319. request_module("ieee80211_crypt_wep");
  320. new_crypt->ops = ieee80211_get_crypto_ops("WEP");
  321. }
  322. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  323. new_crypt->priv = new_crypt->ops->init(key);
  324. if (!new_crypt->ops || !new_crypt->priv) {
  325. kfree(new_crypt);
  326. new_crypt = NULL;
  327. printk(KERN_WARNING "%s: could not initialize WEP: "
  328. "load module ieee80211_crypt_wep\n", dev->name);
  329. return -EOPNOTSUPP;
  330. }
  331. *crypt = new_crypt;
  332. }
  333. /* If a new key was provided, set it up */
  334. if (erq->length > 0) {
  335. len = erq->length <= 5 ? 5 : 13;
  336. memcpy(sec.keys[key], keybuf, erq->length);
  337. if (len > erq->length)
  338. memset(sec.keys[key] + erq->length, 0,
  339. len - erq->length);
  340. IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
  341. key, escape_essid(sec.keys[key], len),
  342. erq->length, len);
  343. sec.key_sizes[key] = len;
  344. if (*crypt)
  345. (*crypt)->ops->set_key(sec.keys[key], len, NULL,
  346. (*crypt)->priv);
  347. sec.flags |= (1 << key);
  348. /* This ensures a key will be activated if no key is
  349. * explicitely set */
  350. if (key == sec.active_key)
  351. sec.flags |= SEC_ACTIVE_KEY;
  352. } else {
  353. if (host_crypto) {
  354. len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
  355. NULL, (*crypt)->priv);
  356. if (len == 0) {
  357. /* Set a default key of all 0 */
  358. IEEE80211_DEBUG_WX("Setting key %d to all "
  359. "zero.\n", key);
  360. memset(sec.keys[key], 0, 13);
  361. (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
  362. (*crypt)->priv);
  363. sec.key_sizes[key] = 13;
  364. sec.flags |= (1 << key);
  365. }
  366. }
  367. /* No key data - just set the default TX key index */
  368. if (key_provided) {
  369. IEEE80211_DEBUG_WX("Setting key %d to default Tx "
  370. "key.\n", key);
  371. ieee->tx_keyidx = key;
  372. sec.active_key = key;
  373. sec.flags |= SEC_ACTIVE_KEY;
  374. }
  375. }
  376. if (erq->flags & (IW_ENCODE_OPEN | IW_ENCODE_RESTRICTED)) {
  377. ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
  378. sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN :
  379. WLAN_AUTH_SHARED_KEY;
  380. sec.flags |= SEC_AUTH_MODE;
  381. IEEE80211_DEBUG_WX("Auth: %s\n",
  382. sec.auth_mode == WLAN_AUTH_OPEN ?
  383. "OPEN" : "SHARED KEY");
  384. }
  385. /* For now we just support WEP, so only set that security level...
  386. * TODO: When WPA is added this is one place that needs to change */
  387. sec.flags |= SEC_LEVEL;
  388. sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
  389. sec.encode_alg[key] = SEC_ALG_WEP;
  390. done:
  391. if (ieee->set_security)
  392. ieee->set_security(dev, &sec);
  393. /* Do not reset port if card is in Managed mode since resetting will
  394. * generate new IEEE 802.11 authentication which may end up in looping
  395. * with IEEE 802.1X. If your hardware requires a reset after WEP
  396. * configuration (for example... Prism2), implement the reset_port in
  397. * the callbacks structures used to initialize the 802.11 stack. */
  398. if (ieee->reset_on_keychange &&
  399. ieee->iw_mode != IW_MODE_INFRA &&
  400. ieee->reset_port && ieee->reset_port(dev)) {
  401. printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
  407. struct iw_request_info *info,
  408. union iwreq_data *wrqu, char *keybuf)
  409. {
  410. struct iw_point *erq = &(wrqu->encoding);
  411. int len, key;
  412. struct ieee80211_crypt_data *crypt;
  413. struct ieee80211_security *sec = &ieee->sec;
  414. IEEE80211_DEBUG_WX("GET_ENCODE\n");
  415. key = erq->flags & IW_ENCODE_INDEX;
  416. if (key) {
  417. if (key > WEP_KEYS)
  418. return -EINVAL;
  419. key--;
  420. } else
  421. key = ieee->tx_keyidx;
  422. crypt = ieee->crypt[key];
  423. erq->flags = key + 1;
  424. if (!sec->enabled) {
  425. erq->length = 0;
  426. erq->flags |= IW_ENCODE_DISABLED;
  427. return 0;
  428. }
  429. len = sec->key_sizes[key];
  430. memcpy(keybuf, sec->keys[key], len);
  431. erq->length = (len >= 0 ? len : 0);
  432. erq->flags |= IW_ENCODE_ENABLED;
  433. if (ieee->open_wep)
  434. erq->flags |= IW_ENCODE_OPEN;
  435. else
  436. erq->flags |= IW_ENCODE_RESTRICTED;
  437. return 0;
  438. }
  439. int ieee80211_wx_set_encodeext(struct ieee80211_device *ieee,
  440. struct iw_request_info *info,
  441. union iwreq_data *wrqu, char *extra)
  442. {
  443. struct net_device *dev = ieee->dev;
  444. struct iw_point *encoding = &wrqu->encoding;
  445. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  446. int i, idx, ret = 0;
  447. int group_key = 0;
  448. const char *alg, *module;
  449. struct ieee80211_crypto_ops *ops;
  450. struct ieee80211_crypt_data **crypt;
  451. struct ieee80211_security sec = {
  452. .flags = 0,
  453. };
  454. idx = encoding->flags & IW_ENCODE_INDEX;
  455. if (idx) {
  456. if (idx < 1 || idx > WEP_KEYS)
  457. return -EINVAL;
  458. idx--;
  459. } else
  460. idx = ieee->tx_keyidx;
  461. if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
  462. crypt = &ieee->crypt[idx];
  463. group_key = 1;
  464. } else {
  465. /* some Cisco APs use idx>0 for unicast in dynamic WEP */
  466. if (idx != 0 && ext->alg != IW_ENCODE_ALG_WEP)
  467. return -EINVAL;
  468. if (ieee->iw_mode == IW_MODE_INFRA)
  469. crypt = &ieee->crypt[idx];
  470. else
  471. return -EINVAL;
  472. }
  473. sec.flags |= SEC_ENABLED | SEC_ENCRYPT;
  474. if ((encoding->flags & IW_ENCODE_DISABLED) ||
  475. ext->alg == IW_ENCODE_ALG_NONE) {
  476. if (*crypt)
  477. ieee80211_crypt_delayed_deinit(ieee, crypt);
  478. for (i = 0; i < WEP_KEYS; i++)
  479. if (ieee->crypt[i] != NULL)
  480. break;
  481. if (i == WEP_KEYS) {
  482. sec.enabled = 0;
  483. sec.encrypt = 0;
  484. sec.level = SEC_LEVEL_0;
  485. sec.flags |= SEC_LEVEL;
  486. }
  487. goto done;
  488. }
  489. sec.enabled = 1;
  490. sec.encrypt = 1;
  491. if (group_key ? !ieee->host_mc_decrypt :
  492. !(ieee->host_encrypt || ieee->host_decrypt ||
  493. ieee->host_encrypt_msdu))
  494. goto skip_host_crypt;
  495. switch (ext->alg) {
  496. case IW_ENCODE_ALG_WEP:
  497. alg = "WEP";
  498. module = "ieee80211_crypt_wep";
  499. break;
  500. case IW_ENCODE_ALG_TKIP:
  501. alg = "TKIP";
  502. module = "ieee80211_crypt_tkip";
  503. break;
  504. case IW_ENCODE_ALG_CCMP:
  505. alg = "CCMP";
  506. module = "ieee80211_crypt_ccmp";
  507. break;
  508. default:
  509. IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
  510. dev->name, ext->alg);
  511. ret = -EINVAL;
  512. goto done;
  513. }
  514. ops = ieee80211_get_crypto_ops(alg);
  515. if (ops == NULL) {
  516. request_module(module);
  517. ops = ieee80211_get_crypto_ops(alg);
  518. }
  519. if (ops == NULL) {
  520. IEEE80211_DEBUG_WX("%s: unknown crypto alg %d\n",
  521. dev->name, ext->alg);
  522. ret = -EINVAL;
  523. goto done;
  524. }
  525. if (*crypt == NULL || (*crypt)->ops != ops) {
  526. struct ieee80211_crypt_data *new_crypt;
  527. ieee80211_crypt_delayed_deinit(ieee, crypt);
  528. new_crypt = (struct ieee80211_crypt_data *)
  529. kmalloc(sizeof(*new_crypt), GFP_KERNEL);
  530. if (new_crypt == NULL) {
  531. ret = -ENOMEM;
  532. goto done;
  533. }
  534. memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
  535. new_crypt->ops = ops;
  536. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  537. new_crypt->priv = new_crypt->ops->init(idx);
  538. if (new_crypt->priv == NULL) {
  539. kfree(new_crypt);
  540. ret = -EINVAL;
  541. goto done;
  542. }
  543. *crypt = new_crypt;
  544. }
  545. if (ext->key_len > 0 && (*crypt)->ops->set_key &&
  546. (*crypt)->ops->set_key(ext->key, ext->key_len, ext->rx_seq,
  547. (*crypt)->priv) < 0) {
  548. IEEE80211_DEBUG_WX("%s: key setting failed\n", dev->name);
  549. ret = -EINVAL;
  550. goto done;
  551. }
  552. skip_host_crypt:
  553. if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
  554. ieee->tx_keyidx = idx;
  555. sec.active_key = idx;
  556. sec.flags |= SEC_ACTIVE_KEY;
  557. }
  558. if (ext->alg != IW_ENCODE_ALG_NONE) {
  559. memcpy(sec.keys[idx], ext->key, ext->key_len);
  560. sec.key_sizes[idx] = ext->key_len;
  561. sec.flags |= (1 << idx);
  562. if (ext->alg == IW_ENCODE_ALG_WEP) {
  563. sec.encode_alg[idx] = SEC_ALG_WEP;
  564. sec.flags |= SEC_LEVEL;
  565. sec.level = SEC_LEVEL_1;
  566. } else if (ext->alg == IW_ENCODE_ALG_TKIP) {
  567. sec.encode_alg[idx] = SEC_ALG_TKIP;
  568. sec.flags |= SEC_LEVEL;
  569. sec.level = SEC_LEVEL_2;
  570. } else if (ext->alg == IW_ENCODE_ALG_CCMP) {
  571. sec.encode_alg[idx] = SEC_ALG_CCMP;
  572. sec.flags |= SEC_LEVEL;
  573. sec.level = SEC_LEVEL_3;
  574. }
  575. /* Don't set sec level for group keys. */
  576. if (group_key)
  577. sec.flags &= ~SEC_LEVEL;
  578. }
  579. done:
  580. if (ieee->set_security)
  581. ieee->set_security(ieee->dev, &sec);
  582. /*
  583. * Do not reset port if card is in Managed mode since resetting will
  584. * generate new IEEE 802.11 authentication which may end up in looping
  585. * with IEEE 802.1X. If your hardware requires a reset after WEP
  586. * configuration (for example... Prism2), implement the reset_port in
  587. * the callbacks structures used to initialize the 802.11 stack.
  588. */
  589. if (ieee->reset_on_keychange &&
  590. ieee->iw_mode != IW_MODE_INFRA &&
  591. ieee->reset_port && ieee->reset_port(dev)) {
  592. IEEE80211_DEBUG_WX("%s: reset_port failed\n", dev->name);
  593. return -EINVAL;
  594. }
  595. return ret;
  596. }
  597. int ieee80211_wx_get_encodeext(struct ieee80211_device *ieee,
  598. struct iw_request_info *info,
  599. union iwreq_data *wrqu, char *extra)
  600. {
  601. struct iw_point *encoding = &wrqu->encoding;
  602. struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
  603. struct ieee80211_security *sec = &ieee->sec;
  604. int idx, max_key_len;
  605. max_key_len = encoding->length - sizeof(*ext);
  606. if (max_key_len < 0)
  607. return -EINVAL;
  608. idx = encoding->flags & IW_ENCODE_INDEX;
  609. if (idx) {
  610. if (idx < 1 || idx > WEP_KEYS)
  611. return -EINVAL;
  612. idx--;
  613. } else
  614. idx = ieee->tx_keyidx;
  615. if (!ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY &&
  616. ext->alg != IW_ENCODE_ALG_WEP)
  617. if (idx != 0 || ieee->iw_mode != IW_MODE_INFRA)
  618. return -EINVAL;
  619. encoding->flags = idx + 1;
  620. memset(ext, 0, sizeof(*ext));
  621. if (!sec->enabled) {
  622. ext->alg = IW_ENCODE_ALG_NONE;
  623. ext->key_len = 0;
  624. encoding->flags |= IW_ENCODE_DISABLED;
  625. } else {
  626. if (sec->encode_alg[idx] == SEC_ALG_WEP)
  627. ext->alg = IW_ENCODE_ALG_WEP;
  628. else if (sec->encode_alg[idx] == SEC_ALG_TKIP)
  629. ext->alg = IW_ENCODE_ALG_TKIP;
  630. else if (sec->encode_alg[idx] == SEC_ALG_CCMP)
  631. ext->alg = IW_ENCODE_ALG_CCMP;
  632. else
  633. return -EINVAL;
  634. ext->key_len = sec->key_sizes[idx];
  635. memcpy(ext->key, sec->keys[idx], ext->key_len);
  636. encoding->flags |= IW_ENCODE_ENABLED;
  637. if (ext->key_len &&
  638. (ext->alg == IW_ENCODE_ALG_TKIP ||
  639. ext->alg == IW_ENCODE_ALG_CCMP))
  640. ext->ext_flags |= IW_ENCODE_EXT_TX_SEQ_VALID;
  641. }
  642. return 0;
  643. }
  644. int ieee80211_wx_set_auth(struct net_device *dev,
  645. struct iw_request_info *info,
  646. union iwreq_data *wrqu,
  647. char *extra)
  648. {
  649. struct ieee80211_device *ieee = netdev_priv(dev);
  650. unsigned long flags;
  651. int err = 0;
  652. spin_lock_irqsave(&ieee->lock, flags);
  653. switch (wrqu->param.flags & IW_AUTH_INDEX) {
  654. case IW_AUTH_WPA_VERSION:
  655. case IW_AUTH_CIPHER_PAIRWISE:
  656. case IW_AUTH_CIPHER_GROUP:
  657. case IW_AUTH_KEY_MGMT:
  658. /*
  659. * Host AP driver does not use these parameters and allows
  660. * wpa_supplicant to control them internally.
  661. */
  662. break;
  663. case IW_AUTH_TKIP_COUNTERMEASURES:
  664. break; /* FIXME */
  665. case IW_AUTH_DROP_UNENCRYPTED:
  666. ieee->drop_unencrypted = !!wrqu->param.value;
  667. break;
  668. case IW_AUTH_80211_AUTH_ALG:
  669. break; /* FIXME */
  670. case IW_AUTH_WPA_ENABLED:
  671. ieee->privacy_invoked = ieee->wpa_enabled = !!wrqu->param.value;
  672. break;
  673. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  674. ieee->ieee802_1x = !!wrqu->param.value;
  675. break;
  676. case IW_AUTH_PRIVACY_INVOKED:
  677. ieee->privacy_invoked = !!wrqu->param.value;
  678. break;
  679. default:
  680. err = -EOPNOTSUPP;
  681. break;
  682. }
  683. spin_unlock_irqrestore(&ieee->lock, flags);
  684. return err;
  685. }
  686. int ieee80211_wx_get_auth(struct net_device *dev,
  687. struct iw_request_info *info,
  688. union iwreq_data *wrqu,
  689. char *extra)
  690. {
  691. struct ieee80211_device *ieee = netdev_priv(dev);
  692. unsigned long flags;
  693. int err = 0;
  694. spin_lock_irqsave(&ieee->lock, flags);
  695. switch (wrqu->param.flags & IW_AUTH_INDEX) {
  696. case IW_AUTH_WPA_VERSION:
  697. case IW_AUTH_CIPHER_PAIRWISE:
  698. case IW_AUTH_CIPHER_GROUP:
  699. case IW_AUTH_KEY_MGMT:
  700. case IW_AUTH_TKIP_COUNTERMEASURES: /* FIXME */
  701. case IW_AUTH_80211_AUTH_ALG: /* FIXME */
  702. /*
  703. * Host AP driver does not use these parameters and allows
  704. * wpa_supplicant to control them internally.
  705. */
  706. err = -EOPNOTSUPP;
  707. break;
  708. case IW_AUTH_DROP_UNENCRYPTED:
  709. wrqu->param.value = ieee->drop_unencrypted;
  710. break;
  711. case IW_AUTH_WPA_ENABLED:
  712. wrqu->param.value = ieee->wpa_enabled;
  713. break;
  714. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  715. wrqu->param.value = ieee->ieee802_1x;
  716. break;
  717. default:
  718. err = -EOPNOTSUPP;
  719. break;
  720. }
  721. spin_unlock_irqrestore(&ieee->lock, flags);
  722. return err;
  723. }
  724. EXPORT_SYMBOL(ieee80211_wx_set_encodeext);
  725. EXPORT_SYMBOL(ieee80211_wx_get_encodeext);
  726. EXPORT_SYMBOL(ieee80211_wx_get_scan);
  727. EXPORT_SYMBOL(ieee80211_wx_set_encode);
  728. EXPORT_SYMBOL(ieee80211_wx_get_encode);
  729. EXPORT_SYMBOL_GPL(ieee80211_wx_set_auth);
  730. EXPORT_SYMBOL_GPL(ieee80211_wx_get_auth);