hostap_main.c 30 KB

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