hostap_info.c 14 KB

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