hostap_main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * Host AP (software wireless LAN access point) driver for
  3. * Intersil Prism2/2.5/3 - hostap.o module, common routines
  4. *
  5. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. * <j@w1.fi>
  7. * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation. See README and COPYING for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/delay.h>
  20. #include <linux/random.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/kmod.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/wireless.h>
  25. #include <linux/etherdevice.h>
  26. #include <net/iw_handler.h>
  27. #include <net/ieee80211.h>
  28. #include <net/ieee80211_crypt.h>
  29. #include <asm/uaccess.h>
  30. #include "hostap_wlan.h"
  31. #include "hostap_80211.h"
  32. #include "hostap_ap.h"
  33. #include "hostap.h"
  34. MODULE_AUTHOR("Jouni Malinen");
  35. MODULE_DESCRIPTION("Host AP common routines");
  36. MODULE_LICENSE("GPL");
  37. #define TX_TIMEOUT (2 * HZ)
  38. #define PRISM2_MAX_FRAME_SIZE 2304
  39. #define PRISM2_MIN_MTU 256
  40. /* FIX: */
  41. #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
  42. struct net_device * hostap_add_interface(struct local_info *local,
  43. int type, int rtnl_locked,
  44. const char *prefix,
  45. const char *name)
  46. {
  47. struct net_device *dev, *mdev;
  48. struct hostap_interface *iface;
  49. int ret;
  50. dev = alloc_etherdev(sizeof(struct hostap_interface));
  51. if (dev == NULL)
  52. return NULL;
  53. iface = netdev_priv(dev);
  54. iface->dev = dev;
  55. iface->local = local;
  56. iface->type = type;
  57. list_add(&iface->list, &local->hostap_interfaces);
  58. mdev = local->dev;
  59. memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
  60. dev->base_addr = mdev->base_addr;
  61. dev->irq = mdev->irq;
  62. dev->mem_start = mdev->mem_start;
  63. dev->mem_end = mdev->mem_end;
  64. hostap_setup_dev(dev, local, 0);
  65. dev->destructor = free_netdev;
  66. sprintf(dev->name, "%s%s", prefix, name);
  67. if (!rtnl_locked)
  68. rtnl_lock();
  69. ret = 0;
  70. if (strchr(dev->name, '%'))
  71. ret = dev_alloc_name(dev, dev->name);
  72. SET_NETDEV_DEV(dev, mdev->dev.parent);
  73. if (ret >= 0)
  74. ret = register_netdevice(dev);
  75. if (!rtnl_locked)
  76. rtnl_unlock();
  77. if (ret < 0) {
  78. printk(KERN_WARNING "%s: failed to add new netdevice!\n",
  79. dev->name);
  80. free_netdev(dev);
  81. return NULL;
  82. }
  83. printk(KERN_DEBUG "%s: registered netdevice %s\n",
  84. mdev->name, dev->name);
  85. return dev;
  86. }
  87. void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
  88. int remove_from_list)
  89. {
  90. struct hostap_interface *iface;
  91. if (!dev)
  92. return;
  93. iface = netdev_priv(dev);
  94. if (remove_from_list) {
  95. list_del(&iface->list);
  96. }
  97. if (dev == iface->local->ddev)
  98. iface->local->ddev = NULL;
  99. else if (dev == iface->local->apdev)
  100. iface->local->apdev = NULL;
  101. else if (dev == iface->local->stadev)
  102. iface->local->stadev = NULL;
  103. if (rtnl_locked)
  104. unregister_netdevice(dev);
  105. else
  106. unregister_netdev(dev);
  107. /* dev->destructor = free_netdev() will free the device data, including
  108. * private data, when removing the device */
  109. }
  110. static inline int prism2_wds_special_addr(u8 *addr)
  111. {
  112. if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
  113. return 0;
  114. return 1;
  115. }
  116. int prism2_wds_add(local_info_t *local, u8 *remote_addr,
  117. int rtnl_locked)
  118. {
  119. struct net_device *dev;
  120. struct list_head *ptr;
  121. struct hostap_interface *iface, *empty, *match;
  122. empty = match = NULL;
  123. read_lock_bh(&local->iface_lock);
  124. list_for_each(ptr, &local->hostap_interfaces) {
  125. iface = list_entry(ptr, struct hostap_interface, list);
  126. if (iface->type != HOSTAP_INTERFACE_WDS)
  127. continue;
  128. if (prism2_wds_special_addr(iface->u.wds.remote_addr))
  129. empty = iface;
  130. else if (memcmp(iface->u.wds.remote_addr, remote_addr,
  131. ETH_ALEN) == 0) {
  132. match = iface;
  133. break;
  134. }
  135. }
  136. if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
  137. /* take pre-allocated entry into use */
  138. memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
  139. read_unlock_bh(&local->iface_lock);
  140. printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
  141. local->dev->name, empty->dev->name);
  142. return 0;
  143. }
  144. read_unlock_bh(&local->iface_lock);
  145. if (!prism2_wds_special_addr(remote_addr)) {
  146. if (match)
  147. return -EEXIST;
  148. hostap_add_sta(local->ap, remote_addr);
  149. }
  150. if (local->wds_connections >= local->wds_max_connections)
  151. return -ENOBUFS;
  152. /* verify that there is room for wds# postfix in the interface name */
  153. if (strlen(local->dev->name) > IFNAMSIZ - 5) {
  154. printk(KERN_DEBUG "'%s' too long base device name\n",
  155. local->dev->name);
  156. return -EINVAL;
  157. }
  158. dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
  159. local->ddev->name, "wds%d");
  160. if (dev == NULL)
  161. return -ENOMEM;
  162. iface = netdev_priv(dev);
  163. memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
  164. local->wds_connections++;
  165. return 0;
  166. }
  167. int prism2_wds_del(local_info_t *local, u8 *remote_addr,
  168. int rtnl_locked, int do_not_remove)
  169. {
  170. unsigned long flags;
  171. struct list_head *ptr;
  172. struct hostap_interface *iface, *selected = NULL;
  173. write_lock_irqsave(&local->iface_lock, flags);
  174. list_for_each(ptr, &local->hostap_interfaces) {
  175. iface = list_entry(ptr, struct hostap_interface, list);
  176. if (iface->type != HOSTAP_INTERFACE_WDS)
  177. continue;
  178. if (memcmp(iface->u.wds.remote_addr, remote_addr,
  179. ETH_ALEN) == 0) {
  180. selected = iface;
  181. break;
  182. }
  183. }
  184. if (selected && !do_not_remove)
  185. list_del(&selected->list);
  186. write_unlock_irqrestore(&local->iface_lock, flags);
  187. if (selected) {
  188. if (do_not_remove)
  189. memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
  190. else {
  191. hostap_remove_interface(selected->dev, rtnl_locked, 0);
  192. local->wds_connections--;
  193. }
  194. }
  195. return selected ? 0 : -ENODEV;
  196. }
  197. u16 hostap_tx_callback_register(local_info_t *local,
  198. void (*func)(struct sk_buff *, int ok, void *),
  199. void *data)
  200. {
  201. unsigned long flags;
  202. struct hostap_tx_callback_info *entry;
  203. entry = kmalloc(sizeof(*entry),
  204. GFP_ATOMIC);
  205. if (entry == NULL)
  206. return 0;
  207. entry->func = func;
  208. entry->data = data;
  209. spin_lock_irqsave(&local->lock, flags);
  210. entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
  211. entry->next = local->tx_callback;
  212. local->tx_callback = entry;
  213. spin_unlock_irqrestore(&local->lock, flags);
  214. return entry->idx;
  215. }
  216. int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
  217. {
  218. unsigned long flags;
  219. struct hostap_tx_callback_info *cb, *prev = NULL;
  220. spin_lock_irqsave(&local->lock, flags);
  221. cb = local->tx_callback;
  222. while (cb != NULL && cb->idx != idx) {
  223. prev = cb;
  224. cb = cb->next;
  225. }
  226. if (cb) {
  227. if (prev == NULL)
  228. local->tx_callback = cb->next;
  229. else
  230. prev->next = cb->next;
  231. kfree(cb);
  232. }
  233. spin_unlock_irqrestore(&local->lock, flags);
  234. return cb ? 0 : -1;
  235. }
  236. /* val is in host byte order */
  237. int hostap_set_word(struct net_device *dev, int rid, u16 val)
  238. {
  239. struct hostap_interface *iface;
  240. u16 tmp = cpu_to_le16(val);
  241. iface = netdev_priv(dev);
  242. return iface->local->func->set_rid(dev, rid, &tmp, 2);
  243. }
  244. int hostap_set_string(struct net_device *dev, int rid, const char *val)
  245. {
  246. struct hostap_interface *iface;
  247. char buf[MAX_SSID_LEN + 2];
  248. int len;
  249. iface = netdev_priv(dev);
  250. len = strlen(val);
  251. if (len > MAX_SSID_LEN)
  252. return -1;
  253. memset(buf, 0, sizeof(buf));
  254. buf[0] = len; /* little endian 16 bit word */
  255. memcpy(buf + 2, val, len);
  256. return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
  257. }
  258. u16 hostap_get_porttype(local_info_t *local)
  259. {
  260. if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
  261. return HFA384X_PORTTYPE_PSEUDO_IBSS;
  262. if (local->iw_mode == IW_MODE_ADHOC)
  263. return HFA384X_PORTTYPE_IBSS;
  264. if (local->iw_mode == IW_MODE_INFRA)
  265. return HFA384X_PORTTYPE_BSS;
  266. if (local->iw_mode == IW_MODE_REPEAT)
  267. return HFA384X_PORTTYPE_WDS;
  268. if (local->iw_mode == IW_MODE_MONITOR)
  269. return HFA384X_PORTTYPE_PSEUDO_IBSS;
  270. return HFA384X_PORTTYPE_HOSTAP;
  271. }
  272. int hostap_set_encryption(local_info_t *local)
  273. {
  274. u16 val, old_val;
  275. int i, keylen, len, idx;
  276. char keybuf[WEP_KEY_LEN + 1];
  277. enum { NONE, WEP, OTHER } encrypt_type;
  278. idx = local->tx_keyidx;
  279. if (local->crypt[idx] == NULL || local->crypt[idx]->ops == NULL)
  280. encrypt_type = NONE;
  281. else if (strcmp(local->crypt[idx]->ops->name, "WEP") == 0)
  282. encrypt_type = WEP;
  283. else
  284. encrypt_type = OTHER;
  285. if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
  286. 1) < 0) {
  287. printk(KERN_DEBUG "Could not read current WEP flags.\n");
  288. goto fail;
  289. }
  290. le16_to_cpus(&val);
  291. old_val = val;
  292. if (encrypt_type != NONE || local->privacy_invoked)
  293. val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
  294. else
  295. val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
  296. if (local->open_wep || encrypt_type == NONE ||
  297. ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
  298. val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
  299. else
  300. val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
  301. if ((encrypt_type != NONE || local->privacy_invoked) &&
  302. (encrypt_type == OTHER || local->host_encrypt))
  303. val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
  304. else
  305. val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
  306. if ((encrypt_type != NONE || local->privacy_invoked) &&
  307. (encrypt_type == OTHER || local->host_decrypt))
  308. val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
  309. else
  310. val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
  311. if (val != old_val &&
  312. hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
  313. printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
  314. val);
  315. goto fail;
  316. }
  317. if (encrypt_type != WEP)
  318. return 0;
  319. /* 104-bit support seems to require that all the keys are set to the
  320. * same keylen */
  321. keylen = 6; /* first 5 octets */
  322. len = local->crypt[idx]->ops->get_key(keybuf, sizeof(keybuf),
  323. NULL, local->crypt[idx]->priv);
  324. if (idx >= 0 && idx < WEP_KEYS && len > 5)
  325. keylen = WEP_KEY_LEN + 1; /* first 13 octets */
  326. for (i = 0; i < WEP_KEYS; i++) {
  327. memset(keybuf, 0, sizeof(keybuf));
  328. if (local->crypt[i]) {
  329. (void) local->crypt[i]->ops->get_key(
  330. keybuf, sizeof(keybuf),
  331. NULL, local->crypt[i]->priv);
  332. }
  333. if (local->func->set_rid(local->dev,
  334. HFA384X_RID_CNFDEFAULTKEY0 + i,
  335. keybuf, keylen)) {
  336. printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
  337. i, keylen);
  338. goto fail;
  339. }
  340. }
  341. if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
  342. printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
  343. goto fail;
  344. }
  345. return 0;
  346. fail:
  347. printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
  348. return -1;
  349. }
  350. int hostap_set_antsel(local_info_t *local)
  351. {
  352. u16 val;
  353. int ret = 0;
  354. if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
  355. local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
  356. HFA386X_CR_TX_CONFIGURE,
  357. NULL, &val) == 0) {
  358. val &= ~(BIT(2) | BIT(1));
  359. switch (local->antsel_tx) {
  360. case HOSTAP_ANTSEL_DIVERSITY:
  361. val |= BIT(1);
  362. break;
  363. case HOSTAP_ANTSEL_LOW:
  364. break;
  365. case HOSTAP_ANTSEL_HIGH:
  366. val |= BIT(2);
  367. break;
  368. }
  369. if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
  370. HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
  371. printk(KERN_INFO "%s: setting TX AntSel failed\n",
  372. local->dev->name);
  373. ret = -1;
  374. }
  375. }
  376. if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
  377. local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
  378. HFA386X_CR_RX_CONFIGURE,
  379. NULL, &val) == 0) {
  380. val &= ~(BIT(1) | BIT(0));
  381. switch (local->antsel_rx) {
  382. case HOSTAP_ANTSEL_DIVERSITY:
  383. break;
  384. case HOSTAP_ANTSEL_LOW:
  385. val |= BIT(0);
  386. break;
  387. case HOSTAP_ANTSEL_HIGH:
  388. val |= BIT(0) | BIT(1);
  389. break;
  390. }
  391. if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
  392. HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
  393. printk(KERN_INFO "%s: setting RX AntSel failed\n",
  394. local->dev->name);
  395. ret = -1;
  396. }
  397. }
  398. return ret;
  399. }
  400. int hostap_set_roaming(local_info_t *local)
  401. {
  402. u16 val;
  403. switch (local->host_roaming) {
  404. case 1:
  405. val = HFA384X_ROAMING_HOST;
  406. break;
  407. case 2:
  408. val = HFA384X_ROAMING_DISABLED;
  409. break;
  410. case 0:
  411. default:
  412. val = HFA384X_ROAMING_FIRMWARE;
  413. break;
  414. }
  415. return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
  416. }
  417. int hostap_set_auth_algs(local_info_t *local)
  418. {
  419. int val = local->auth_algs;
  420. /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
  421. * set to include both Open and Shared Key flags. It tries to use
  422. * Shared Key authentication in that case even if WEP keys are not
  423. * configured.. STA f/w v0.7.6 is able to handle such configuration,
  424. * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
  425. if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
  426. val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
  427. val = PRISM2_AUTH_OPEN;
  428. if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
  429. printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
  430. "failed\n", local->dev->name, local->auth_algs);
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
  436. {
  437. u16 status, fc;
  438. status = __le16_to_cpu(rx->status);
  439. printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
  440. "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
  441. "jiffies=%ld\n",
  442. name, status, (status >> 8) & 0x07, status >> 13, status & 1,
  443. rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
  444. fc = __le16_to_cpu(rx->frame_control);
  445. printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
  446. "data_len=%d%s%s\n",
  447. fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
  448. __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
  449. __le16_to_cpu(rx->data_len),
  450. fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
  451. fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
  452. printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
  453. MACSTR "\n",
  454. MAC2STR(rx->addr1), MAC2STR(rx->addr2), MAC2STR(rx->addr3),
  455. MAC2STR(rx->addr4));
  456. printk(KERN_DEBUG " dst=" MACSTR " src=" MACSTR " len=%d\n",
  457. MAC2STR(rx->dst_addr), MAC2STR(rx->src_addr),
  458. __be16_to_cpu(rx->len));
  459. }
  460. void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
  461. {
  462. u16 fc;
  463. printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
  464. "tx_control=0x%04x; jiffies=%ld\n",
  465. name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
  466. __le16_to_cpu(tx->tx_control), jiffies);
  467. fc = __le16_to_cpu(tx->frame_control);
  468. printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
  469. "data_len=%d%s%s\n",
  470. fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
  471. __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
  472. __le16_to_cpu(tx->data_len),
  473. fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
  474. fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
  475. printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
  476. MACSTR "\n",
  477. MAC2STR(tx->addr1), MAC2STR(tx->addr2), MAC2STR(tx->addr3),
  478. MAC2STR(tx->addr4));
  479. printk(KERN_DEBUG " dst=" MACSTR " src=" MACSTR " len=%d\n",
  480. MAC2STR(tx->dst_addr), MAC2STR(tx->src_addr),
  481. __be16_to_cpu(tx->len));
  482. }
  483. int hostap_80211_header_parse(struct sk_buff *skb, unsigned char *haddr)
  484. {
  485. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  486. return ETH_ALEN;
  487. }
  488. int hostap_80211_prism_header_parse(struct sk_buff *skb, unsigned char *haddr)
  489. {
  490. const unsigned char *mac = skb_mac_header(skb);
  491. if (*(u32 *)mac == LWNG_CAP_DID_BASE) {
  492. memcpy(haddr, mac + sizeof(struct linux_wlan_ng_prism_hdr) + 10,
  493. ETH_ALEN); /* addr2 */
  494. } else { /* (*(u32 *)mac == htonl(LWNG_CAPHDR_VERSION)) */
  495. memcpy(haddr, mac + sizeof(struct linux_wlan_ng_cap_hdr) + 10,
  496. ETH_ALEN); /* addr2 */
  497. }
  498. return ETH_ALEN;
  499. }
  500. int hostap_80211_get_hdrlen(u16 fc)
  501. {
  502. int hdrlen = 24;
  503. switch (WLAN_FC_GET_TYPE(fc)) {
  504. case IEEE80211_FTYPE_DATA:
  505. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  506. hdrlen = 30; /* Addr4 */
  507. break;
  508. case IEEE80211_FTYPE_CTL:
  509. switch (WLAN_FC_GET_STYPE(fc)) {
  510. case IEEE80211_STYPE_CTS:
  511. case IEEE80211_STYPE_ACK:
  512. hdrlen = 10;
  513. break;
  514. default:
  515. hdrlen = 16;
  516. break;
  517. }
  518. break;
  519. }
  520. return hdrlen;
  521. }
  522. struct net_device_stats *hostap_get_stats(struct net_device *dev)
  523. {
  524. struct hostap_interface *iface;
  525. iface = netdev_priv(dev);
  526. return &iface->stats;
  527. }
  528. static int prism2_close(struct net_device *dev)
  529. {
  530. struct hostap_interface *iface;
  531. local_info_t *local;
  532. PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
  533. iface = netdev_priv(dev);
  534. local = iface->local;
  535. if (dev == local->ddev) {
  536. prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
  537. }
  538. #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  539. if (!local->hostapd && dev == local->dev &&
  540. (!local->func->card_present || local->func->card_present(local)) &&
  541. local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
  542. hostap_deauth_all_stas(dev, local->ap, 1);
  543. #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  544. if (dev == local->dev) {
  545. local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
  546. }
  547. if (netif_running(dev)) {
  548. netif_stop_queue(dev);
  549. netif_device_detach(dev);
  550. }
  551. flush_scheduled_work();
  552. module_put(local->hw_module);
  553. local->num_dev_open--;
  554. if (dev != local->dev && local->dev->flags & IFF_UP &&
  555. local->master_dev_auto_open && local->num_dev_open == 1) {
  556. /* Close master radio interface automatically if it was also
  557. * opened automatically and we are now closing the last
  558. * remaining non-master device. */
  559. dev_close(local->dev);
  560. }
  561. return 0;
  562. }
  563. static int prism2_open(struct net_device *dev)
  564. {
  565. struct hostap_interface *iface;
  566. local_info_t *local;
  567. PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
  568. iface = netdev_priv(dev);
  569. local = iface->local;
  570. if (local->no_pri) {
  571. printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
  572. "f/w\n", dev->name);
  573. return 1;
  574. }
  575. if ((local->func->card_present && !local->func->card_present(local)) ||
  576. local->hw_downloading)
  577. return -ENODEV;
  578. if (!try_module_get(local->hw_module))
  579. return -ENODEV;
  580. local->num_dev_open++;
  581. if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
  582. printk(KERN_WARNING "%s: could not enable MAC port\n",
  583. dev->name);
  584. prism2_close(dev);
  585. return 1;
  586. }
  587. if (!local->dev_enabled)
  588. prism2_callback(local, PRISM2_CALLBACK_ENABLE);
  589. local->dev_enabled = 1;
  590. if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
  591. /* Master radio interface is needed for all operation, so open
  592. * it automatically when any virtual net_device is opened. */
  593. local->master_dev_auto_open = 1;
  594. dev_open(local->dev);
  595. }
  596. netif_device_attach(dev);
  597. netif_start_queue(dev);
  598. return 0;
  599. }
  600. static int prism2_set_mac_address(struct net_device *dev, void *p)
  601. {
  602. struct hostap_interface *iface;
  603. local_info_t *local;
  604. struct list_head *ptr;
  605. struct sockaddr *addr = p;
  606. iface = netdev_priv(dev);
  607. local = iface->local;
  608. if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
  609. ETH_ALEN) < 0 || local->func->reset_port(dev))
  610. return -EINVAL;
  611. read_lock_bh(&local->iface_lock);
  612. list_for_each(ptr, &local->hostap_interfaces) {
  613. iface = list_entry(ptr, struct hostap_interface, list);
  614. memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
  615. }
  616. memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
  617. read_unlock_bh(&local->iface_lock);
  618. return 0;
  619. }
  620. /* TODO: to be further implemented as soon as Prism2 fully supports
  621. * GroupAddresses and correct documentation is available */
  622. void hostap_set_multicast_list_queue(struct work_struct *work)
  623. {
  624. local_info_t *local =
  625. container_of(work, local_info_t, set_multicast_list_queue);
  626. struct net_device *dev = local->dev;
  627. struct hostap_interface *iface;
  628. iface = netdev_priv(dev);
  629. if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
  630. local->is_promisc)) {
  631. printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
  632. dev->name, local->is_promisc ? "en" : "dis");
  633. }
  634. }
  635. static void hostap_set_multicast_list(struct net_device *dev)
  636. {
  637. #if 0
  638. /* FIX: promiscuous mode seems to be causing a lot of problems with
  639. * some station firmware versions (FCSErr frames, invalid MACPort, etc.
  640. * corrupted incoming frames). This code is now commented out while the
  641. * problems are investigated. */
  642. struct hostap_interface *iface;
  643. local_info_t *local;
  644. iface = netdev_priv(dev);
  645. local = iface->local;
  646. if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
  647. local->is_promisc = 1;
  648. } else {
  649. local->is_promisc = 0;
  650. }
  651. schedule_work(&local->set_multicast_list_queue);
  652. #endif
  653. }
  654. static int prism2_change_mtu(struct net_device *dev, int new_mtu)
  655. {
  656. if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
  657. return -EINVAL;
  658. dev->mtu = new_mtu;
  659. return 0;
  660. }
  661. static void prism2_tx_timeout(struct net_device *dev)
  662. {
  663. struct hostap_interface *iface;
  664. local_info_t *local;
  665. struct hfa384x_regs regs;
  666. iface = netdev_priv(dev);
  667. local = iface->local;
  668. printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
  669. netif_stop_queue(local->dev);
  670. local->func->read_regs(dev, &regs);
  671. printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
  672. "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
  673. dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
  674. regs.swsupport0);
  675. local->func->schedule_reset(local);
  676. }
  677. void hostap_setup_dev(struct net_device *dev, local_info_t *local,
  678. int main_dev)
  679. {
  680. struct hostap_interface *iface;
  681. iface = netdev_priv(dev);
  682. ether_setup(dev);
  683. /* kernel callbacks */
  684. dev->get_stats = hostap_get_stats;
  685. if (iface) {
  686. /* Currently, we point to the proper spy_data only on
  687. * the main_dev. This could be fixed. Jean II */
  688. iface->wireless_data.spy_data = &iface->spy_data;
  689. dev->wireless_data = &iface->wireless_data;
  690. }
  691. dev->wireless_handlers =
  692. (struct iw_handler_def *) &hostap_iw_handler_def;
  693. dev->do_ioctl = hostap_ioctl;
  694. dev->open = prism2_open;
  695. dev->stop = prism2_close;
  696. dev->hard_start_xmit = hostap_data_start_xmit;
  697. dev->set_mac_address = prism2_set_mac_address;
  698. dev->set_multicast_list = hostap_set_multicast_list;
  699. dev->change_mtu = prism2_change_mtu;
  700. dev->tx_timeout = prism2_tx_timeout;
  701. dev->watchdog_timeo = TX_TIMEOUT;
  702. dev->mtu = local->mtu;
  703. if (!main_dev) {
  704. /* use main radio device queue */
  705. dev->tx_queue_len = 0;
  706. }
  707. SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
  708. netif_stop_queue(dev);
  709. }
  710. static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
  711. {
  712. struct net_device *dev = local->dev;
  713. if (local->apdev)
  714. return -EEXIST;
  715. printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
  716. local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
  717. rtnl_locked, local->ddev->name,
  718. "ap");
  719. if (local->apdev == NULL)
  720. return -ENOMEM;
  721. local->apdev->hard_start_xmit = hostap_mgmt_start_xmit;
  722. local->apdev->type = ARPHRD_IEEE80211;
  723. local->apdev->hard_header_parse = hostap_80211_header_parse;
  724. return 0;
  725. }
  726. static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
  727. {
  728. struct net_device *dev = local->dev;
  729. printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
  730. hostap_remove_interface(local->apdev, rtnl_locked, 1);
  731. local->apdev = NULL;
  732. return 0;
  733. }
  734. static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
  735. {
  736. struct net_device *dev = local->dev;
  737. if (local->stadev)
  738. return -EEXIST;
  739. printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
  740. local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
  741. rtnl_locked, local->ddev->name,
  742. "sta");
  743. if (local->stadev == NULL)
  744. return -ENOMEM;
  745. return 0;
  746. }
  747. static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
  748. {
  749. struct net_device *dev = local->dev;
  750. printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
  751. hostap_remove_interface(local->stadev, rtnl_locked, 1);
  752. local->stadev = NULL;
  753. return 0;
  754. }
  755. int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
  756. {
  757. int ret;
  758. if (val < 0 || val > 1)
  759. return -EINVAL;
  760. if (local->hostapd == val)
  761. return 0;
  762. if (val) {
  763. ret = hostap_enable_hostapd(local, rtnl_locked);
  764. if (ret == 0)
  765. local->hostapd = 1;
  766. } else {
  767. local->hostapd = 0;
  768. ret = hostap_disable_hostapd(local, rtnl_locked);
  769. if (ret != 0)
  770. local->hostapd = 1;
  771. }
  772. return ret;
  773. }
  774. int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
  775. {
  776. int ret;
  777. if (val < 0 || val > 1)
  778. return -EINVAL;
  779. if (local->hostapd_sta == val)
  780. return 0;
  781. if (val) {
  782. ret = hostap_enable_hostapd_sta(local, rtnl_locked);
  783. if (ret == 0)
  784. local->hostapd_sta = 1;
  785. } else {
  786. local->hostapd_sta = 0;
  787. ret = hostap_disable_hostapd_sta(local, rtnl_locked);
  788. if (ret != 0)
  789. local->hostapd_sta = 1;
  790. }
  791. return ret;
  792. }
  793. int prism2_update_comms_qual(struct net_device *dev)
  794. {
  795. struct hostap_interface *iface;
  796. local_info_t *local;
  797. int ret = 0;
  798. struct hfa384x_comms_quality sq;
  799. iface = netdev_priv(dev);
  800. local = iface->local;
  801. if (!local->sta_fw_ver)
  802. ret = -1;
  803. else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
  804. if (local->func->get_rid(local->dev,
  805. HFA384X_RID_DBMCOMMSQUALITY,
  806. &sq, sizeof(sq), 1) >= 0) {
  807. local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
  808. local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
  809. local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
  810. local->last_comms_qual_update = jiffies;
  811. } else
  812. ret = -1;
  813. } else {
  814. if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
  815. &sq, sizeof(sq), 1) >= 0) {
  816. local->comms_qual = le16_to_cpu(sq.comm_qual);
  817. local->avg_signal = HFA384X_LEVEL_TO_dBm(
  818. le16_to_cpu(sq.signal_level));
  819. local->avg_noise = HFA384X_LEVEL_TO_dBm(
  820. le16_to_cpu(sq.noise_level));
  821. local->last_comms_qual_update = jiffies;
  822. } else
  823. ret = -1;
  824. }
  825. return ret;
  826. }
  827. int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
  828. u8 *body, size_t bodylen)
  829. {
  830. struct sk_buff *skb;
  831. struct hostap_ieee80211_mgmt *mgmt;
  832. struct hostap_skb_tx_data *meta;
  833. struct net_device *dev = local->dev;
  834. skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
  835. if (skb == NULL)
  836. return -ENOMEM;
  837. mgmt = (struct hostap_ieee80211_mgmt *)
  838. skb_put(skb, IEEE80211_MGMT_HDR_LEN);
  839. memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
  840. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  841. memcpy(mgmt->da, dst, ETH_ALEN);
  842. memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
  843. memcpy(mgmt->bssid, dst, ETH_ALEN);
  844. if (body)
  845. memcpy(skb_put(skb, bodylen), body, bodylen);
  846. meta = (struct hostap_skb_tx_data *) skb->cb;
  847. memset(meta, 0, sizeof(*meta));
  848. meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
  849. meta->iface = netdev_priv(dev);
  850. skb->dev = dev;
  851. skb_reset_mac_header(skb);
  852. skb_reset_network_header(skb);
  853. dev_queue_xmit(skb);
  854. return 0;
  855. }
  856. int prism2_sta_deauth(local_info_t *local, u16 reason)
  857. {
  858. union iwreq_data wrqu;
  859. int ret;
  860. if (local->iw_mode != IW_MODE_INFRA ||
  861. memcmp(local->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 ||
  862. memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
  863. return 0;
  864. reason = cpu_to_le16(reason);
  865. ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
  866. (u8 *) &reason, 2);
  867. memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
  868. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  869. return ret;
  870. }
  871. struct proc_dir_entry *hostap_proc;
  872. static int __init hostap_init(void)
  873. {
  874. if (proc_net != NULL) {
  875. hostap_proc = proc_mkdir("hostap", proc_net);
  876. if (!hostap_proc)
  877. printk(KERN_WARNING "Failed to mkdir "
  878. "/proc/net/hostap\n");
  879. } else
  880. hostap_proc = NULL;
  881. return 0;
  882. }
  883. static void __exit hostap_exit(void)
  884. {
  885. if (hostap_proc != NULL) {
  886. hostap_proc = NULL;
  887. remove_proc_entry("hostap", proc_net);
  888. }
  889. }
  890. EXPORT_SYMBOL(hostap_set_word);
  891. EXPORT_SYMBOL(hostap_set_string);
  892. EXPORT_SYMBOL(hostap_get_porttype);
  893. EXPORT_SYMBOL(hostap_set_encryption);
  894. EXPORT_SYMBOL(hostap_set_antsel);
  895. EXPORT_SYMBOL(hostap_set_roaming);
  896. EXPORT_SYMBOL(hostap_set_auth_algs);
  897. EXPORT_SYMBOL(hostap_dump_rx_header);
  898. EXPORT_SYMBOL(hostap_dump_tx_header);
  899. EXPORT_SYMBOL(hostap_80211_header_parse);
  900. EXPORT_SYMBOL(hostap_80211_get_hdrlen);
  901. EXPORT_SYMBOL(hostap_get_stats);
  902. EXPORT_SYMBOL(hostap_setup_dev);
  903. EXPORT_SYMBOL(hostap_set_multicast_list_queue);
  904. EXPORT_SYMBOL(hostap_set_hostapd);
  905. EXPORT_SYMBOL(hostap_set_hostapd_sta);
  906. EXPORT_SYMBOL(hostap_add_interface);
  907. EXPORT_SYMBOL(hostap_remove_interface);
  908. EXPORT_SYMBOL(prism2_update_comms_qual);
  909. module_init(hostap_init);
  910. module_exit(hostap_exit);