hostap_main.c 28 KB

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