hostap_main.c 29 KB

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