hostap.c 30 KB

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