hostap.c 30 KB

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