ieee80211_wx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /******************************************************************************
  2. Copyright(c) 2004 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 <net/ieee80211.h>
  27. #include <linux/wireless.h>
  28. static const char *ieee80211_modes[] = {
  29. "?", "a", "b", "ab", "g", "ag", "bg", "abg"
  30. };
  31. #define MAX_CUSTOM_LEN 64
  32. static inline char *ipw2100_translate_scan(struct ieee80211_device *ieee,
  33. char *start, char *stop,
  34. struct ieee80211_network *network)
  35. {
  36. char custom[MAX_CUSTOM_LEN];
  37. char *p;
  38. struct iw_event iwe;
  39. int i, j;
  40. u8 max_rate, rate;
  41. /* First entry *MUST* be the AP MAC address */
  42. iwe.cmd = SIOCGIWAP;
  43. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  44. memcpy(iwe.u.ap_addr.sa_data, network->bssid, ETH_ALEN);
  45. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  46. /* Remaining entries will be displayed in the order we provide them */
  47. /* Add the ESSID */
  48. iwe.cmd = SIOCGIWESSID;
  49. iwe.u.data.flags = 1;
  50. if (network->flags & NETWORK_EMPTY_ESSID) {
  51. iwe.u.data.length = sizeof("<hidden>");
  52. start = iwe_stream_add_point(start, stop, &iwe, "<hidden>");
  53. } else {
  54. iwe.u.data.length = min(network->ssid_len, (u8) 32);
  55. start = iwe_stream_add_point(start, stop, &iwe, network->ssid);
  56. }
  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(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(start, stop, &iwe, IW_EV_UINT_LEN);
  70. }
  71. /* Add frequency/channel */
  72. iwe.cmd = SIOCGIWFREQ;
  73. /* iwe.u.freq.m = ieee80211_frequency(network->channel, network->mode);
  74. iwe.u.freq.e = 3; */
  75. iwe.u.freq.m = network->channel;
  76. iwe.u.freq.e = 0;
  77. iwe.u.freq.i = 0;
  78. start = iwe_stream_add_event(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(start, stop, &iwe, network->ssid);
  87. /* Add basic and extended rates */
  88. max_rate = 0;
  89. p = custom;
  90. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
  91. for (i = 0, j = 0; i < network->rates_len;) {
  92. if (j < network->rates_ex_len &&
  93. ((network->rates_ex[j] & 0x7F) <
  94. (network->rates[i] & 0x7F)))
  95. rate = network->rates_ex[j++] & 0x7F;
  96. else
  97. rate = network->rates[i++] & 0x7F;
  98. if (rate > max_rate)
  99. max_rate = rate;
  100. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  101. "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
  102. }
  103. for (; j < network->rates_ex_len; j++) {
  104. rate = network->rates_ex[j] & 0x7F;
  105. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  106. "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
  107. if (rate > max_rate)
  108. max_rate = rate;
  109. }
  110. iwe.cmd = SIOCGIWRATE;
  111. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  112. iwe.u.bitrate.value = max_rate * 500000;
  113. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_PARAM_LEN);
  114. iwe.cmd = IWEVCUSTOM;
  115. iwe.u.data.length = p - custom;
  116. if (iwe.u.data.length)
  117. start = iwe_stream_add_point(start, stop, &iwe, custom);
  118. /* Add quality statistics */
  119. /* TODO: Fix these values... */
  120. iwe.cmd = IWEVQUAL;
  121. iwe.u.qual.qual = network->stats.signal;
  122. iwe.u.qual.level = network->stats.rssi;
  123. iwe.u.qual.noise = network->stats.noise;
  124. iwe.u.qual.updated = network->stats.mask & IEEE80211_STATMASK_WEMASK;
  125. if (!(network->stats.mask & IEEE80211_STATMASK_RSSI))
  126. iwe.u.qual.updated |= IW_QUAL_LEVEL_INVALID;
  127. if (!(network->stats.mask & IEEE80211_STATMASK_NOISE))
  128. iwe.u.qual.updated |= IW_QUAL_NOISE_INVALID;
  129. if (!(network->stats.mask & IEEE80211_STATMASK_SIGNAL))
  130. iwe.u.qual.updated |= IW_QUAL_QUAL_INVALID;
  131. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  132. iwe.cmd = IWEVCUSTOM;
  133. p = custom;
  134. iwe.u.data.length = p - custom;
  135. if (iwe.u.data.length)
  136. start = iwe_stream_add_point(start, stop, &iwe, custom);
  137. if (ieee->wpa_enabled && network->wpa_ie_len) {
  138. char buf[MAX_WPA_IE_LEN * 2 + 30];
  139. u8 *p = buf;
  140. p += sprintf(p, "wpa_ie=");
  141. for (i = 0; i < network->wpa_ie_len; i++) {
  142. p += sprintf(p, "%02x", network->wpa_ie[i]);
  143. }
  144. memset(&iwe, 0, sizeof(iwe));
  145. iwe.cmd = IWEVCUSTOM;
  146. iwe.u.data.length = strlen(buf);
  147. start = iwe_stream_add_point(start, stop, &iwe, buf);
  148. }
  149. if (ieee->wpa_enabled && network->rsn_ie_len) {
  150. char buf[MAX_WPA_IE_LEN * 2 + 30];
  151. u8 *p = buf;
  152. p += sprintf(p, "rsn_ie=");
  153. for (i = 0; i < network->rsn_ie_len; i++) {
  154. p += sprintf(p, "%02x", network->rsn_ie[i]);
  155. }
  156. memset(&iwe, 0, sizeof(iwe));
  157. iwe.cmd = IWEVCUSTOM;
  158. iwe.u.data.length = strlen(buf);
  159. start = iwe_stream_add_point(start, stop, &iwe, buf);
  160. }
  161. /* Add EXTRA: Age to display seconds since last beacon/probe response
  162. * for given network. */
  163. iwe.cmd = IWEVCUSTOM;
  164. p = custom;
  165. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  166. " Last beacon: %lums ago",
  167. (jiffies - network->last_scanned) / (HZ / 100));
  168. iwe.u.data.length = p - custom;
  169. if (iwe.u.data.length)
  170. start = iwe_stream_add_point(start, stop, &iwe, custom);
  171. return start;
  172. }
  173. int ieee80211_wx_get_scan(struct ieee80211_device *ieee,
  174. struct iw_request_info *info,
  175. union iwreq_data *wrqu, char *extra)
  176. {
  177. struct ieee80211_network *network;
  178. unsigned long flags;
  179. char *ev = extra;
  180. char *stop = ev + IW_SCAN_MAX_DATA;
  181. int i = 0;
  182. IEEE80211_DEBUG_WX("Getting scan\n");
  183. spin_lock_irqsave(&ieee->lock, flags);
  184. list_for_each_entry(network, &ieee->network_list, list) {
  185. i++;
  186. if (ieee->scan_age == 0 ||
  187. time_after(network->last_scanned + ieee->scan_age, jiffies))
  188. ev = ipw2100_translate_scan(ieee, ev, stop, network);
  189. else
  190. IEEE80211_DEBUG_SCAN("Not showing network '%s ("
  191. MAC_FMT ")' due to age (%lums).\n",
  192. escape_essid(network->ssid,
  193. network->ssid_len),
  194. MAC_ARG(network->bssid),
  195. (jiffies -
  196. network->last_scanned) / (HZ /
  197. 100));
  198. }
  199. spin_unlock_irqrestore(&ieee->lock, flags);
  200. wrqu->data.length = ev - extra;
  201. wrqu->data.flags = 0;
  202. IEEE80211_DEBUG_WX("exit: %d networks returned.\n", i);
  203. return 0;
  204. }
  205. int ieee80211_wx_set_encode(struct ieee80211_device *ieee,
  206. struct iw_request_info *info,
  207. union iwreq_data *wrqu, char *keybuf)
  208. {
  209. struct iw_point *erq = &(wrqu->encoding);
  210. struct net_device *dev = ieee->dev;
  211. struct ieee80211_security sec = {
  212. .flags = 0
  213. };
  214. int i, key, key_provided, len;
  215. struct ieee80211_crypt_data **crypt;
  216. IEEE80211_DEBUG_WX("SET_ENCODE\n");
  217. key = erq->flags & IW_ENCODE_INDEX;
  218. if (key) {
  219. if (key > WEP_KEYS)
  220. return -EINVAL;
  221. key--;
  222. key_provided = 1;
  223. } else {
  224. key_provided = 0;
  225. key = ieee->tx_keyidx;
  226. }
  227. IEEE80211_DEBUG_WX("Key: %d [%s]\n", key, key_provided ?
  228. "provided" : "default");
  229. crypt = &ieee->crypt[key];
  230. if (erq->flags & IW_ENCODE_DISABLED) {
  231. if (key_provided && *crypt) {
  232. IEEE80211_DEBUG_WX("Disabling encryption on key %d.\n",
  233. key);
  234. ieee80211_crypt_delayed_deinit(ieee, crypt);
  235. } else
  236. IEEE80211_DEBUG_WX("Disabling encryption.\n");
  237. /* Check all the keys to see if any are still configured,
  238. * and if no key index was provided, de-init them all */
  239. for (i = 0; i < WEP_KEYS; i++) {
  240. if (ieee->crypt[i] != NULL) {
  241. if (key_provided)
  242. break;
  243. ieee80211_crypt_delayed_deinit(ieee,
  244. &ieee->crypt[i]);
  245. }
  246. }
  247. if (i == WEP_KEYS) {
  248. sec.enabled = 0;
  249. sec.level = SEC_LEVEL_0;
  250. sec.flags |= SEC_ENABLED | SEC_LEVEL;
  251. }
  252. goto done;
  253. }
  254. sec.enabled = 1;
  255. sec.flags |= SEC_ENABLED;
  256. if (*crypt != NULL && (*crypt)->ops != NULL &&
  257. strcmp((*crypt)->ops->name, "WEP") != 0) {
  258. /* changing to use WEP; deinit previously used algorithm
  259. * on this key */
  260. ieee80211_crypt_delayed_deinit(ieee, crypt);
  261. }
  262. if (*crypt == NULL) {
  263. struct ieee80211_crypt_data *new_crypt;
  264. /* take WEP into use */
  265. new_crypt = kmalloc(sizeof(struct ieee80211_crypt_data),
  266. GFP_KERNEL);
  267. if (new_crypt == NULL)
  268. return -ENOMEM;
  269. memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
  270. new_crypt->ops = ieee80211_get_crypto_ops("WEP");
  271. if (!new_crypt->ops) {
  272. request_module("ieee80211_crypt_wep");
  273. new_crypt->ops = ieee80211_get_crypto_ops("WEP");
  274. }
  275. if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
  276. new_crypt->priv = new_crypt->ops->init(key);
  277. if (!new_crypt->ops || !new_crypt->priv) {
  278. kfree(new_crypt);
  279. new_crypt = NULL;
  280. printk(KERN_WARNING "%s: could not initialize WEP: "
  281. "load module ieee80211_crypt_wep\n", dev->name);
  282. return -EOPNOTSUPP;
  283. }
  284. *crypt = new_crypt;
  285. }
  286. /* If a new key was provided, set it up */
  287. if (erq->length > 0) {
  288. len = erq->length <= 5 ? 5 : 13;
  289. memcpy(sec.keys[key], keybuf, erq->length);
  290. if (len > erq->length)
  291. memset(sec.keys[key] + erq->length, 0,
  292. len - erq->length);
  293. IEEE80211_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
  294. key, escape_essid(sec.keys[key], len),
  295. erq->length, len);
  296. sec.key_sizes[key] = len;
  297. (*crypt)->ops->set_key(sec.keys[key], len, NULL,
  298. (*crypt)->priv);
  299. sec.flags |= (1 << key);
  300. /* This ensures a key will be activated if no key is
  301. * explicitely set */
  302. if (key == sec.active_key)
  303. sec.flags |= SEC_ACTIVE_KEY;
  304. } else {
  305. len = (*crypt)->ops->get_key(sec.keys[key], WEP_KEY_LEN,
  306. NULL, (*crypt)->priv);
  307. if (len == 0) {
  308. /* Set a default key of all 0 */
  309. IEEE80211_DEBUG_WX("Setting key %d to all zero.\n",
  310. key);
  311. memset(sec.keys[key], 0, 13);
  312. (*crypt)->ops->set_key(sec.keys[key], 13, NULL,
  313. (*crypt)->priv);
  314. sec.key_sizes[key] = 13;
  315. sec.flags |= (1 << key);
  316. }
  317. /* No key data - just set the default TX key index */
  318. if (key_provided) {
  319. IEEE80211_DEBUG_WX
  320. ("Setting key %d to default Tx key.\n", key);
  321. ieee->tx_keyidx = key;
  322. sec.active_key = key;
  323. sec.flags |= SEC_ACTIVE_KEY;
  324. }
  325. }
  326. done:
  327. ieee->open_wep = !(erq->flags & IW_ENCODE_RESTRICTED);
  328. sec.auth_mode = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
  329. sec.flags |= SEC_AUTH_MODE;
  330. IEEE80211_DEBUG_WX("Auth: %s\n", sec.auth_mode == WLAN_AUTH_OPEN ?
  331. "OPEN" : "SHARED KEY");
  332. /* For now we just support WEP, so only set that security level...
  333. * TODO: When WPA is added this is one place that needs to change */
  334. sec.flags |= SEC_LEVEL;
  335. sec.level = SEC_LEVEL_1; /* 40 and 104 bit WEP */
  336. if (ieee->set_security)
  337. ieee->set_security(dev, &sec);
  338. /* Do not reset port if card is in Managed mode since resetting will
  339. * generate new IEEE 802.11 authentication which may end up in looping
  340. * with IEEE 802.1X. If your hardware requires a reset after WEP
  341. * configuration (for example... Prism2), implement the reset_port in
  342. * the callbacks structures used to initialize the 802.11 stack. */
  343. if (ieee->reset_on_keychange &&
  344. ieee->iw_mode != IW_MODE_INFRA &&
  345. ieee->reset_port && ieee->reset_port(dev)) {
  346. printk(KERN_DEBUG "%s: reset_port failed\n", dev->name);
  347. return -EINVAL;
  348. }
  349. return 0;
  350. }
  351. int ieee80211_wx_get_encode(struct ieee80211_device *ieee,
  352. struct iw_request_info *info,
  353. union iwreq_data *wrqu, char *keybuf)
  354. {
  355. struct iw_point *erq = &(wrqu->encoding);
  356. int len, key;
  357. struct ieee80211_crypt_data *crypt;
  358. IEEE80211_DEBUG_WX("GET_ENCODE\n");
  359. key = erq->flags & IW_ENCODE_INDEX;
  360. if (key) {
  361. if (key > WEP_KEYS)
  362. return -EINVAL;
  363. key--;
  364. } else
  365. key = ieee->tx_keyidx;
  366. crypt = ieee->crypt[key];
  367. erq->flags = key + 1;
  368. if (crypt == NULL || crypt->ops == NULL) {
  369. erq->length = 0;
  370. erq->flags |= IW_ENCODE_DISABLED;
  371. return 0;
  372. }
  373. if (strcmp(crypt->ops->name, "WEP") != 0) {
  374. /* only WEP is supported with wireless extensions, so just
  375. * report that encryption is used */
  376. erq->length = 0;
  377. erq->flags |= IW_ENCODE_ENABLED;
  378. return 0;
  379. }
  380. len = crypt->ops->get_key(keybuf, WEP_KEY_LEN, NULL, crypt->priv);
  381. erq->length = (len >= 0 ? len : 0);
  382. erq->flags |= IW_ENCODE_ENABLED;
  383. if (ieee->open_wep)
  384. erq->flags |= IW_ENCODE_OPEN;
  385. else
  386. erq->flags |= IW_ENCODE_RESTRICTED;
  387. return 0;
  388. }
  389. EXPORT_SYMBOL(ieee80211_wx_get_scan);
  390. EXPORT_SYMBOL(ieee80211_wx_set_encode);
  391. EXPORT_SYMBOL(ieee80211_wx_get_encode);