hostap_main.c 28 KB

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