hostap.c 30 KB

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