hostap_info.c 14 KB

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