hostap_info.c 14 KB

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