hostap_info.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_array(new_count,
  238. sizeof(struct hfa384x_hostscan_result),
  239. GFP_ATOMIC);
  240. if (results == NULL)
  241. return;
  242. /* Convert to hostscan result format. */
  243. res = (struct hfa384x_scan_result *) pos;
  244. for (i = 0; i < new_count; i++) {
  245. memcpy(&results[i], &res[i],
  246. sizeof(struct hfa384x_scan_result));
  247. results[i].atim = 0;
  248. }
  249. spin_lock_irqsave(&local->lock, flags);
  250. local->last_scan_type = PRISM2_SCAN;
  251. prev = local->last_scan_results;
  252. local->last_scan_results = results;
  253. local->last_scan_results_count = new_count;
  254. spin_unlock_irqrestore(&local->lock, flags);
  255. kfree(prev);
  256. hostap_report_scan_complete(local);
  257. /* Perform rest of ScanResults handling later in scheduled task */
  258. set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
  259. schedule_work(&local->info_queue);
  260. }
  261. /* Called only as a tasklet (software IRQ) */
  262. static void prism2_info_hostscanresults(local_info_t *local,
  263. unsigned char *buf, int left)
  264. {
  265. int i, result_size, copy_len, new_count;
  266. struct hfa384x_hostscan_result *results, *prev;
  267. unsigned long flags;
  268. __le16 *pos;
  269. u8 *ptr;
  270. wake_up_interruptible(&local->hostscan_wq);
  271. if (left < 4) {
  272. printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
  273. "length %d\n", local->dev->name, left);
  274. return;
  275. }
  276. pos = (__le16 *) buf;
  277. copy_len = result_size = le16_to_cpu(*pos);
  278. if (result_size == 0) {
  279. printk(KERN_DEBUG "%s: invalid result_size (0) in "
  280. "hostscanresults\n", local->dev->name);
  281. return;
  282. }
  283. if (copy_len > sizeof(struct hfa384x_hostscan_result))
  284. copy_len = sizeof(struct hfa384x_hostscan_result);
  285. pos++;
  286. pos++;
  287. left -= 4;
  288. ptr = (u8 *) pos;
  289. new_count = left / result_size;
  290. results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
  291. GFP_ATOMIC);
  292. if (results == NULL)
  293. return;
  294. for (i = 0; i < new_count; i++) {
  295. memcpy(&results[i], ptr, copy_len);
  296. ptr += result_size;
  297. left -= result_size;
  298. }
  299. if (left) {
  300. printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
  301. local->dev->name, left, result_size);
  302. }
  303. spin_lock_irqsave(&local->lock, flags);
  304. local->last_scan_type = PRISM2_HOSTSCAN;
  305. prev = local->last_scan_results;
  306. local->last_scan_results = results;
  307. local->last_scan_results_count = new_count;
  308. spin_unlock_irqrestore(&local->lock, flags);
  309. kfree(prev);
  310. hostap_report_scan_complete(local);
  311. }
  312. #endif /* PRISM2_NO_STATION_MODES */
  313. /* Called only as a tasklet (software IRQ) */
  314. void hostap_info_process(local_info_t *local, struct sk_buff *skb)
  315. {
  316. struct hfa384x_info_frame *info;
  317. unsigned char *buf;
  318. int left;
  319. #ifndef PRISM2_NO_DEBUG
  320. int i;
  321. #endif /* PRISM2_NO_DEBUG */
  322. info = (struct hfa384x_info_frame *) skb->data;
  323. buf = skb->data + sizeof(*info);
  324. left = skb->len - sizeof(*info);
  325. switch (le16_to_cpu(info->type)) {
  326. case HFA384X_INFO_COMMTALLIES:
  327. prism2_info_commtallies(local, buf, left);
  328. break;
  329. #ifndef PRISM2_NO_STATION_MODES
  330. case HFA384X_INFO_LINKSTATUS:
  331. prism2_info_linkstatus(local, buf, left);
  332. break;
  333. case HFA384X_INFO_SCANRESULTS:
  334. prism2_info_scanresults(local, buf, left);
  335. break;
  336. case HFA384X_INFO_HOSTSCANRESULTS:
  337. prism2_info_hostscanresults(local, buf, left);
  338. break;
  339. #endif /* PRISM2_NO_STATION_MODES */
  340. #ifndef PRISM2_NO_DEBUG
  341. default:
  342. PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
  343. local->dev->name, le16_to_cpu(info->len),
  344. le16_to_cpu(info->type));
  345. PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
  346. for (i = 0; i < (left < 100 ? left : 100); i++)
  347. PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
  348. PDEBUG2(DEBUG_EXTRA, "\n");
  349. break;
  350. #endif /* PRISM2_NO_DEBUG */
  351. }
  352. }
  353. #ifndef PRISM2_NO_STATION_MODES
  354. static void handle_info_queue_linkstatus(local_info_t *local)
  355. {
  356. int val = local->prev_link_status;
  357. int connected;
  358. union iwreq_data wrqu;
  359. connected =
  360. val == HFA384X_LINKSTATUS_CONNECTED ||
  361. val == HFA384X_LINKSTATUS_AP_CHANGE ||
  362. val == HFA384X_LINKSTATUS_AP_IN_RANGE;
  363. if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
  364. local->bssid, ETH_ALEN, 1) < 0) {
  365. printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
  366. "LinkStatus event\n", local->dev->name);
  367. } else {
  368. PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
  369. local->dev->name,
  370. (unsigned char *) local->bssid);
  371. if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
  372. hostap_add_sta(local->ap, local->bssid);
  373. }
  374. /* Get BSSID if we have a valid AP address */
  375. if (connected) {
  376. netif_carrier_on(local->dev);
  377. netif_carrier_on(local->ddev);
  378. memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
  379. } else {
  380. netif_carrier_off(local->dev);
  381. netif_carrier_off(local->ddev);
  382. eth_zero_addr(wrqu.ap_addr.sa_data);
  383. }
  384. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  385. /*
  386. * Filter out sequential disconnect events in order not to cause a
  387. * flood of SIOCGIWAP events that have a race condition with EAPOL
  388. * frames and can confuse wpa_supplicant about the current association
  389. * status.
  390. */
  391. if (connected || local->prev_linkstatus_connected)
  392. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  393. local->prev_linkstatus_connected = connected;
  394. }
  395. static void handle_info_queue_scanresults(local_info_t *local)
  396. {
  397. if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
  398. prism2_host_roaming(local);
  399. if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
  400. !is_zero_ether_addr(local->preferred_ap)) {
  401. /*
  402. * Firmware seems to be getting into odd state in host_roaming
  403. * mode 2 when hostscan is used without join command, so try
  404. * to fix this by re-joining the current AP. This does not
  405. * actually trigger a new association if the current AP is
  406. * still in the scan results.
  407. */
  408. prism2_host_roaming(local);
  409. }
  410. }
  411. /* Called only as scheduled task after receiving info frames (used to avoid
  412. * pending too much time in HW IRQ handler). */
  413. static void handle_info_queue(struct work_struct *work)
  414. {
  415. local_info_t *local = container_of(work, local_info_t, info_queue);
  416. if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
  417. &local->pending_info))
  418. handle_info_queue_linkstatus(local);
  419. if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
  420. &local->pending_info))
  421. handle_info_queue_scanresults(local);
  422. }
  423. #endif /* PRISM2_NO_STATION_MODES */
  424. void hostap_info_init(local_info_t *local)
  425. {
  426. skb_queue_head_init(&local->info_list);
  427. #ifndef PRISM2_NO_STATION_MODES
  428. INIT_WORK(&local->info_queue, handle_info_queue);
  429. #endif /* PRISM2_NO_STATION_MODES */
  430. }
  431. EXPORT_SYMBOL(hostap_info_init);
  432. EXPORT_SYMBOL(hostap_info_process);