hostap_info.c 14 KB

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