hostap_main.c 28 KB

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