hostap_info.c 14 KB

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