hostap_info.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Host AP driver Info Frame processing (part of hostap.o module) */
  2. #include <linux/if_arp.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include <linux/export.h>
  6. #include <linux/etherdevice.h>
  7. #include "hostap_wlan.h"
  8. #include "hostap.h"
  9. #include "hostap_ap.h"
  10. /* Called only as a tasklet (software IRQ) */
  11. static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
  12. int left)
  13. {
  14. struct hfa384x_comm_tallies *tallies;
  15. if (left < sizeof(struct hfa384x_comm_tallies)) {
  16. printk(KERN_DEBUG "%s: too short (len=%d) commtallies "
  17. "info frame\n", local->dev->name, left);
  18. return;
  19. }
  20. tallies = (struct hfa384x_comm_tallies *) buf;
  21. #define ADD_COMM_TALLIES(name) \
  22. local->comm_tallies.name += le16_to_cpu(tallies->name)
  23. ADD_COMM_TALLIES(tx_unicast_frames);
  24. ADD_COMM_TALLIES(tx_multicast_frames);
  25. ADD_COMM_TALLIES(tx_fragments);
  26. ADD_COMM_TALLIES(tx_unicast_octets);
  27. ADD_COMM_TALLIES(tx_multicast_octets);
  28. ADD_COMM_TALLIES(tx_deferred_transmissions);
  29. ADD_COMM_TALLIES(tx_single_retry_frames);
  30. ADD_COMM_TALLIES(tx_multiple_retry_frames);
  31. ADD_COMM_TALLIES(tx_retry_limit_exceeded);
  32. ADD_COMM_TALLIES(tx_discards);
  33. ADD_COMM_TALLIES(rx_unicast_frames);
  34. ADD_COMM_TALLIES(rx_multicast_frames);
  35. ADD_COMM_TALLIES(rx_fragments);
  36. ADD_COMM_TALLIES(rx_unicast_octets);
  37. ADD_COMM_TALLIES(rx_multicast_octets);
  38. ADD_COMM_TALLIES(rx_fcs_errors);
  39. ADD_COMM_TALLIES(rx_discards_no_buffer);
  40. ADD_COMM_TALLIES(tx_discards_wrong_sa);
  41. ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
  42. ADD_COMM_TALLIES(rx_message_in_msg_fragments);
  43. ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
  44. #undef ADD_COMM_TALLIES
  45. }
  46. /* Called only as a tasklet (software IRQ) */
  47. static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
  48. int left)
  49. {
  50. struct hfa384x_comm_tallies32 *tallies;
  51. if (left < sizeof(struct hfa384x_comm_tallies32)) {
  52. printk(KERN_DEBUG "%s: too short (len=%d) commtallies32 "
  53. "info frame\n", local->dev->name, left);
  54. return;
  55. }
  56. tallies = (struct hfa384x_comm_tallies32 *) buf;
  57. #define ADD_COMM_TALLIES(name) \
  58. local->comm_tallies.name += le32_to_cpu(tallies->name)
  59. ADD_COMM_TALLIES(tx_unicast_frames);
  60. ADD_COMM_TALLIES(tx_multicast_frames);
  61. ADD_COMM_TALLIES(tx_fragments);
  62. ADD_COMM_TALLIES(tx_unicast_octets);
  63. ADD_COMM_TALLIES(tx_multicast_octets);
  64. ADD_COMM_TALLIES(tx_deferred_transmissions);
  65. ADD_COMM_TALLIES(tx_single_retry_frames);
  66. ADD_COMM_TALLIES(tx_multiple_retry_frames);
  67. ADD_COMM_TALLIES(tx_retry_limit_exceeded);
  68. ADD_COMM_TALLIES(tx_discards);
  69. ADD_COMM_TALLIES(rx_unicast_frames);
  70. ADD_COMM_TALLIES(rx_multicast_frames);
  71. ADD_COMM_TALLIES(rx_fragments);
  72. ADD_COMM_TALLIES(rx_unicast_octets);
  73. ADD_COMM_TALLIES(rx_multicast_octets);
  74. ADD_COMM_TALLIES(rx_fcs_errors);
  75. ADD_COMM_TALLIES(rx_discards_no_buffer);
  76. ADD_COMM_TALLIES(tx_discards_wrong_sa);
  77. ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
  78. ADD_COMM_TALLIES(rx_message_in_msg_fragments);
  79. ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
  80. #undef ADD_COMM_TALLIES
  81. }
  82. /* Called only as a tasklet (software IRQ) */
  83. static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
  84. int left)
  85. {
  86. if (local->tallies32)
  87. prism2_info_commtallies32(local, buf, left);
  88. else
  89. prism2_info_commtallies16(local, buf, left);
  90. }
  91. #ifndef PRISM2_NO_STATION_MODES
  92. #ifndef PRISM2_NO_DEBUG
  93. static const char* hfa384x_linkstatus_str(u16 linkstatus)
  94. {
  95. switch (linkstatus) {
  96. case HFA384X_LINKSTATUS_CONNECTED:
  97. return "Connected";
  98. case HFA384X_LINKSTATUS_DISCONNECTED:
  99. return "Disconnected";
  100. case HFA384X_LINKSTATUS_AP_CHANGE:
  101. return "Access point change";
  102. case HFA384X_LINKSTATUS_AP_OUT_OF_RANGE:
  103. return "Access point out of range";
  104. case HFA384X_LINKSTATUS_AP_IN_RANGE:
  105. return "Access point in range";
  106. case HFA384X_LINKSTATUS_ASSOC_FAILED:
  107. return "Association failed";
  108. default:
  109. return "Unknown";
  110. }
  111. }
  112. #endif /* PRISM2_NO_DEBUG */
  113. /* Called only as a tasklet (software IRQ) */
  114. static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
  115. int left)
  116. {
  117. u16 val;
  118. int non_sta_mode;
  119. /* Alloc new JoinRequests to occur since LinkStatus for the previous
  120. * has been received */
  121. local->last_join_time = 0;
  122. if (left != 2) {
  123. printk(KERN_DEBUG "%s: invalid linkstatus info frame "
  124. "length %d\n", local->dev->name, left);
  125. return;
  126. }
  127. non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
  128. local->iw_mode == IW_MODE_REPEAT ||
  129. local->iw_mode == IW_MODE_MONITOR;
  130. val = buf[0] | (buf[1] << 8);
  131. if (!non_sta_mode || val != HFA384X_LINKSTATUS_DISCONNECTED) {
  132. PDEBUG(DEBUG_EXTRA, "%s: LinkStatus=%d (%s)\n",
  133. local->dev->name, val, hfa384x_linkstatus_str(val));
  134. }
  135. if (non_sta_mode) {
  136. netif_carrier_on(local->dev);
  137. netif_carrier_on(local->ddev);
  138. return;
  139. }
  140. /* Get current BSSID later in scheduled task */
  141. set_bit(PRISM2_INFO_PENDING_LINKSTATUS, &local->pending_info);
  142. local->prev_link_status = val;
  143. schedule_work(&local->info_queue);
  144. }
  145. static void prism2_host_roaming(local_info_t *local)
  146. {
  147. struct hfa384x_join_request req;
  148. struct net_device *dev = local->dev;
  149. struct hfa384x_hostscan_result *selected, *entry;
  150. int i;
  151. unsigned long flags;
  152. if (local->last_join_time &&
  153. time_before(jiffies, local->last_join_time + 10 * HZ)) {
  154. PDEBUG(DEBUG_EXTRA, "%s: last join request has not yet been "
  155. "completed - waiting for it before issuing new one\n",
  156. dev->name);
  157. return;
  158. }
  159. /* ScanResults are sorted: first ESS results in decreasing signal
  160. * quality then IBSS results in similar order.
  161. * Trivial roaming policy: just select the first entry.
  162. * This could probably be improved by adding hysteresis to limit
  163. * number of handoffs, etc.
  164. *
  165. * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
  166. * ScanResults */
  167. spin_lock_irqsave(&local->lock, flags);
  168. if (local->last_scan_results == NULL ||
  169. local->last_scan_results_count == 0) {
  170. spin_unlock_irqrestore(&local->lock, flags);
  171. PDEBUG(DEBUG_EXTRA, "%s: no scan results for host roaming\n",
  172. dev->name);
  173. return;
  174. }
  175. selected = &local->last_scan_results[0];
  176. if (local->preferred_ap[0] || local->preferred_ap[1] ||
  177. local->preferred_ap[2] || local->preferred_ap[3] ||
  178. local->preferred_ap[4] || local->preferred_ap[5]) {
  179. /* Try to find preferred AP */
  180. PDEBUG(DEBUG_EXTRA, "%s: Preferred AP BSSID %pM\n",
  181. dev->name, local->preferred_ap);
  182. for (i = 0; i < local->last_scan_results_count; i++) {
  183. entry = &local->last_scan_results[i];
  184. if (memcmp(local->preferred_ap, entry->bssid, 6) == 0)
  185. {
  186. PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
  187. "selection\n", dev->name);
  188. selected = entry;
  189. break;
  190. }
  191. }
  192. }
  193. memcpy(req.bssid, selected->bssid, ETH_ALEN);
  194. req.channel = selected->chid;
  195. spin_unlock_irqrestore(&local->lock, flags);
  196. PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
  197. " channel=%d\n",
  198. dev->name, req.bssid, le16_to_cpu(req.channel));
  199. if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
  200. sizeof(req))) {
  201. printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
  202. }
  203. local->last_join_time = jiffies;
  204. }
  205. static void hostap_report_scan_complete(local_info_t *local)
  206. {
  207. union iwreq_data wrqu;
  208. /* Inform user space about new scan results (just empty event,
  209. * SIOCGIWSCAN can be used to fetch data */
  210. wrqu.data.length = 0;
  211. wrqu.data.flags = 0;
  212. wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
  213. /* Allow SIOCGIWSCAN handling to occur since we have received
  214. * scanning result */
  215. local->scan_timestamp = 0;
  216. }
  217. /* Called only as a tasklet (software IRQ) */
  218. static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
  219. int left)
  220. {
  221. u16 *pos;
  222. int new_count, i;
  223. unsigned long flags;
  224. struct hfa384x_scan_result *res;
  225. struct hfa384x_hostscan_result *results, *prev;
  226. if (left < 4) {
  227. printk(KERN_DEBUG "%s: invalid scanresult info frame "
  228. "length %d\n", local->dev->name, left);
  229. return;
  230. }
  231. pos = (u16 *) buf;
  232. pos++;
  233. pos++;
  234. left -= 4;
  235. new_count = left / sizeof(struct hfa384x_scan_result);
  236. results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
  237. GFP_ATOMIC);
  238. if (results == NULL)
  239. return;
  240. /* Convert to hostscan result format. */
  241. res = (struct hfa384x_scan_result *) pos;
  242. for (i = 0; i < new_count; i++) {
  243. memcpy(&results[i], &res[i],
  244. sizeof(struct hfa384x_scan_result));
  245. results[i].atim = 0;
  246. }
  247. spin_lock_irqsave(&local->lock, flags);
  248. local->last_scan_type = PRISM2_SCAN;
  249. prev = local->last_scan_results;
  250. local->last_scan_results = results;
  251. local->last_scan_results_count = new_count;
  252. spin_unlock_irqrestore(&local->lock, flags);
  253. kfree(prev);
  254. hostap_report_scan_complete(local);
  255. /* Perform rest of ScanResults handling later in scheduled task */
  256. set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
  257. schedule_work(&local->info_queue);
  258. }
  259. /* Called only as a tasklet (software IRQ) */
  260. static void prism2_info_hostscanresults(local_info_t *local,
  261. unsigned char *buf, int left)
  262. {
  263. int i, result_size, copy_len, new_count;
  264. struct hfa384x_hostscan_result *results, *prev;
  265. unsigned long flags;
  266. __le16 *pos;
  267. u8 *ptr;
  268. wake_up_interruptible(&local->hostscan_wq);
  269. if (left < 4) {
  270. printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
  271. "length %d\n", local->dev->name, left);
  272. return;
  273. }
  274. pos = (__le16 *) buf;
  275. copy_len = result_size = le16_to_cpu(*pos);
  276. if (result_size == 0) {
  277. printk(KERN_DEBUG "%s: invalid result_size (0) in "
  278. "hostscanresults\n", local->dev->name);
  279. return;
  280. }
  281. if (copy_len > sizeof(struct hfa384x_hostscan_result))
  282. copy_len = sizeof(struct hfa384x_hostscan_result);
  283. pos++;
  284. pos++;
  285. left -= 4;
  286. ptr = (u8 *) pos;
  287. new_count = left / result_size;
  288. results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
  289. GFP_ATOMIC);
  290. if (results == NULL)
  291. return;
  292. for (i = 0; i < new_count; i++) {
  293. memcpy(&results[i], ptr, copy_len);
  294. ptr += result_size;
  295. left -= result_size;
  296. }
  297. if (left) {
  298. printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
  299. local->dev->name, left, result_size);
  300. }
  301. spin_lock_irqsave(&local->lock, flags);
  302. local->last_scan_type = PRISM2_HOSTSCAN;
  303. prev = local->last_scan_results;
  304. local->last_scan_results = results;
  305. local->last_scan_results_count = new_count;
  306. spin_unlock_irqrestore(&local->lock, flags);
  307. kfree(prev);
  308. hostap_report_scan_complete(local);
  309. }
  310. #endif /* PRISM2_NO_STATION_MODES */
  311. /* Called only as a tasklet (software IRQ) */
  312. void hostap_info_process(local_info_t *local, struct sk_buff *skb)
  313. {
  314. struct hfa384x_info_frame *info;
  315. unsigned char *buf;
  316. int left;
  317. #ifndef PRISM2_NO_DEBUG
  318. int i;
  319. #endif /* PRISM2_NO_DEBUG */
  320. info = (struct hfa384x_info_frame *) skb->data;
  321. buf = skb->data + sizeof(*info);
  322. left = skb->len - sizeof(*info);
  323. switch (le16_to_cpu(info->type)) {
  324. case HFA384X_INFO_COMMTALLIES:
  325. prism2_info_commtallies(local, buf, left);
  326. break;
  327. #ifndef PRISM2_NO_STATION_MODES
  328. case HFA384X_INFO_LINKSTATUS:
  329. prism2_info_linkstatus(local, buf, left);
  330. break;
  331. case HFA384X_INFO_SCANRESULTS:
  332. prism2_info_scanresults(local, buf, left);
  333. break;
  334. case HFA384X_INFO_HOSTSCANRESULTS:
  335. prism2_info_hostscanresults(local, buf, left);
  336. break;
  337. #endif /* PRISM2_NO_STATION_MODES */
  338. #ifndef PRISM2_NO_DEBUG
  339. default:
  340. PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
  341. local->dev->name, le16_to_cpu(info->len),
  342. le16_to_cpu(info->type));
  343. PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
  344. for (i = 0; i < (left < 100 ? left : 100); i++)
  345. PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
  346. PDEBUG2(DEBUG_EXTRA, "\n");
  347. break;
  348. #endif /* PRISM2_NO_DEBUG */
  349. }
  350. }
  351. #ifndef PRISM2_NO_STATION_MODES
  352. static void handle_info_queue_linkstatus(local_info_t *local)
  353. {
  354. int val = local->prev_link_status;
  355. int connected;
  356. union iwreq_data wrqu;
  357. connected =
  358. val == HFA384X_LINKSTATUS_CONNECTED ||
  359. val == HFA384X_LINKSTATUS_AP_CHANGE ||
  360. val == HFA384X_LINKSTATUS_AP_IN_RANGE;
  361. if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
  362. local->bssid, ETH_ALEN, 1) < 0) {
  363. printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
  364. "LinkStatus event\n", local->dev->name);
  365. } else {
  366. PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
  367. local->dev->name,
  368. (unsigned char *) local->bssid);
  369. if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
  370. hostap_add_sta(local->ap, local->bssid);
  371. }
  372. /* Get BSSID if we have a valid AP address */
  373. if (connected) {
  374. netif_carrier_on(local->dev);
  375. netif_carrier_on(local->ddev);
  376. memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
  377. } else {
  378. netif_carrier_off(local->dev);
  379. netif_carrier_off(local->ddev);
  380. eth_zero_addr(wrqu.ap_addr.sa_data);
  381. }
  382. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  383. /*
  384. * Filter out sequential disconnect events in order not to cause a
  385. * flood of SIOCGIWAP events that have a race condition with EAPOL
  386. * frames and can confuse wpa_supplicant about the current association
  387. * status.
  388. */
  389. if (connected || local->prev_linkstatus_connected)
  390. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  391. local->prev_linkstatus_connected = connected;
  392. }
  393. static void handle_info_queue_scanresults(local_info_t *local)
  394. {
  395. if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
  396. prism2_host_roaming(local);
  397. if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
  398. !is_zero_ether_addr(local->preferred_ap)) {
  399. /*
  400. * Firmware seems to be getting into odd state in host_roaming
  401. * mode 2 when hostscan is used without join command, so try
  402. * to fix this by re-joining the current AP. This does not
  403. * actually trigger a new association if the current AP is
  404. * still in the scan results.
  405. */
  406. prism2_host_roaming(local);
  407. }
  408. }
  409. /* Called only as scheduled task after receiving info frames (used to avoid
  410. * pending too much time in HW IRQ handler). */
  411. static void handle_info_queue(struct work_struct *work)
  412. {
  413. local_info_t *local = container_of(work, local_info_t, info_queue);
  414. if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
  415. &local->pending_info))
  416. handle_info_queue_linkstatus(local);
  417. if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
  418. &local->pending_info))
  419. handle_info_queue_scanresults(local);
  420. }
  421. #endif /* PRISM2_NO_STATION_MODES */
  422. void hostap_info_init(local_info_t *local)
  423. {
  424. skb_queue_head_init(&local->info_list);
  425. #ifndef PRISM2_NO_STATION_MODES
  426. INIT_WORK(&local->info_queue, handle_info_queue);
  427. #endif /* PRISM2_NO_STATION_MODES */
  428. }
  429. EXPORT_SYMBOL(hostap_info_init);
  430. EXPORT_SYMBOL(hostap_info_process);