ieee80211_wx.c 23 KB

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