hostap_info.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 " MACSTR "\n",
  176. dev->name, MAC2STR(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=" MACSTR " channel=%d\n",
  192. dev->name, MAC2STR(req.bssid), le16_to_cpu(req.channel));
  193. if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
  194. sizeof(req))) {
  195. printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
  196. }
  197. local->last_join_time = jiffies;
  198. }
  199. static void hostap_report_scan_complete(local_info_t *local)
  200. {
  201. union iwreq_data wrqu;
  202. /* Inform user space about new scan results (just empty event,
  203. * SIOCGIWSCAN can be used to fetch data */
  204. wrqu.data.length = 0;
  205. wrqu.data.flags = 0;
  206. wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
  207. /* Allow SIOCGIWSCAN handling to occur since we have received
  208. * scanning result */
  209. local->scan_timestamp = 0;
  210. }
  211. /* Called only as a tasklet (software IRQ) */
  212. static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
  213. int left)
  214. {
  215. u16 *pos;
  216. int new_count, i;
  217. unsigned long flags;
  218. struct hfa384x_scan_result *res;
  219. struct hfa384x_hostscan_result *results, *prev;
  220. if (left < 4) {
  221. printk(KERN_DEBUG "%s: invalid scanresult info frame "
  222. "length %d\n", local->dev->name, left);
  223. return;
  224. }
  225. pos = (u16 *) buf;
  226. pos++;
  227. pos++;
  228. left -= 4;
  229. new_count = left / sizeof(struct hfa384x_scan_result);
  230. results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
  231. GFP_ATOMIC);
  232. if (results == NULL)
  233. return;
  234. /* Convert to hostscan result format. */
  235. res = (struct hfa384x_scan_result *) pos;
  236. for (i = 0; i < new_count; i++) {
  237. memcpy(&results[i], &res[i],
  238. sizeof(struct hfa384x_scan_result));
  239. results[i].atim = 0;
  240. }
  241. spin_lock_irqsave(&local->lock, flags);
  242. local->last_scan_type = PRISM2_SCAN;
  243. prev = local->last_scan_results;
  244. local->last_scan_results = results;
  245. local->last_scan_results_count = new_count;
  246. spin_unlock_irqrestore(&local->lock, flags);
  247. kfree(prev);
  248. hostap_report_scan_complete(local);
  249. /* Perform rest of ScanResults handling later in scheduled task */
  250. set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
  251. schedule_work(&local->info_queue);
  252. }
  253. /* Called only as a tasklet (software IRQ) */
  254. static void prism2_info_hostscanresults(local_info_t *local,
  255. unsigned char *buf, int left)
  256. {
  257. int i, result_size, copy_len, new_count;
  258. struct hfa384x_hostscan_result *results, *prev;
  259. unsigned long flags;
  260. u16 *pos;
  261. u8 *ptr;
  262. wake_up_interruptible(&local->hostscan_wq);
  263. if (left < 4) {
  264. printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
  265. "length %d\n", local->dev->name, left);
  266. return;
  267. }
  268. pos = (u16 *) buf;
  269. copy_len = result_size = le16_to_cpu(*pos);
  270. if (result_size == 0) {
  271. printk(KERN_DEBUG "%s: invalid result_size (0) in "
  272. "hostscanresults\n", local->dev->name);
  273. return;
  274. }
  275. if (copy_len > sizeof(struct hfa384x_hostscan_result))
  276. copy_len = sizeof(struct hfa384x_hostscan_result);
  277. pos++;
  278. pos++;
  279. left -= 4;
  280. ptr = (u8 *) pos;
  281. new_count = left / result_size;
  282. results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
  283. GFP_ATOMIC);
  284. if (results == NULL)
  285. return;
  286. memset(results, 0, new_count * sizeof(struct hfa384x_hostscan_result));
  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 (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, info->len, info->type);
  337. PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
  338. for (i = 0; i < (left < 100 ? left : 100); i++)
  339. PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
  340. PDEBUG2(DEBUG_EXTRA, "\n");
  341. break;
  342. #endif /* PRISM2_NO_DEBUG */
  343. }
  344. }
  345. #ifndef PRISM2_NO_STATION_MODES
  346. static void handle_info_queue_linkstatus(local_info_t *local)
  347. {
  348. int val = local->prev_link_status;
  349. int connected;
  350. union iwreq_data wrqu;
  351. connected =
  352. val == HFA384X_LINKSTATUS_CONNECTED ||
  353. val == HFA384X_LINKSTATUS_AP_CHANGE ||
  354. val == HFA384X_LINKSTATUS_AP_IN_RANGE;
  355. if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
  356. local->bssid, ETH_ALEN, 1) < 0) {
  357. printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
  358. "LinkStatus event\n", local->dev->name);
  359. } else {
  360. PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=" MACSTR "\n",
  361. local->dev->name,
  362. MAC2STR((unsigned char *) local->bssid));
  363. if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
  364. hostap_add_sta(local->ap, local->bssid);
  365. }
  366. /* Get BSSID if we have a valid AP address */
  367. if (connected) {
  368. netif_carrier_on(local->dev);
  369. netif_carrier_on(local->ddev);
  370. memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
  371. } else {
  372. netif_carrier_off(local->dev);
  373. netif_carrier_off(local->ddev);
  374. memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
  375. }
  376. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  377. /*
  378. * Filter out sequential disconnect events in order not to cause a
  379. * flood of SIOCGIWAP events that have a race condition with EAPOL
  380. * frames and can confuse wpa_supplicant about the current association
  381. * status.
  382. */
  383. if (connected || local->prev_linkstatus_connected)
  384. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  385. local->prev_linkstatus_connected = connected;
  386. }
  387. static void handle_info_queue_scanresults(local_info_t *local)
  388. {
  389. if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
  390. prism2_host_roaming(local);
  391. if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
  392. memcmp(local->preferred_ap, "\x00\x00\x00\x00\x00\x00",
  393. ETH_ALEN) != 0) {
  394. /*
  395. * Firmware seems to be getting into odd state in host_roaming
  396. * mode 2 when hostscan is used without join command, so try
  397. * to fix this by re-joining the current AP. This does not
  398. * actually trigger a new association if the current AP is
  399. * still in the scan results.
  400. */
  401. prism2_host_roaming(local);
  402. }
  403. }
  404. /* Called only as scheduled task after receiving info frames (used to avoid
  405. * pending too much time in HW IRQ handler). */
  406. static void handle_info_queue(void *data)
  407. {
  408. local_info_t *local = (local_info_t *) data;
  409. if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
  410. &local->pending_info))
  411. handle_info_queue_linkstatus(local);
  412. if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
  413. &local->pending_info))
  414. handle_info_queue_scanresults(local);
  415. }
  416. #endif /* PRISM2_NO_STATION_MODES */
  417. void hostap_info_init(local_info_t *local)
  418. {
  419. skb_queue_head_init(&local->info_list);
  420. #ifndef PRISM2_NO_STATION_MODES
  421. INIT_WORK(&local->info_queue, handle_info_queue, local);
  422. #endif /* PRISM2_NO_STATION_MODES */
  423. }
  424. EXPORT_SYMBOL(hostap_info_init);
  425. EXPORT_SYMBOL(hostap_info_process);