hostap_info.c 14 KB

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