hostap_info.c 14 KB

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