hostap_main.c 28 KB

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