hostap_main.c 28 KB

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