offchannel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * wpa_supplicant - Off-channel Action frame TX/RX
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. * Copyright (c) 2011, Qualcomm Atheros
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "utils/eloop.h"
  12. #include "wpa_supplicant_i.h"
  13. #include "driver_i.h"
  14. #include "offchannel.h"
  15. static struct wpa_supplicant *
  16. wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
  17. {
  18. struct wpa_supplicant *iface;
  19. if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
  20. return wpa_s;
  21. /*
  22. * Try to find a group interface that matches with the source address.
  23. */
  24. iface = wpa_s->global->ifaces;
  25. while (iface) {
  26. if (os_memcmp(src, iface->own_addr, ETH_ALEN) == 0)
  27. break;
  28. iface = iface->next;
  29. }
  30. if (iface) {
  31. wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
  32. "instead of interface %s for Action TX",
  33. iface->ifname, wpa_s->ifname);
  34. return iface;
  35. }
  36. return wpa_s;
  37. }
  38. static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
  39. {
  40. struct wpa_supplicant *wpa_s = eloop_ctx;
  41. struct wpa_supplicant *iface;
  42. int res;
  43. int without_roc;
  44. without_roc = wpa_s->pending_action_without_roc;
  45. wpa_s->pending_action_without_roc = 0;
  46. wpa_printf(MSG_DEBUG,
  47. "Off-channel: Send Action callback (without_roc=%d pending_action_tx=%p pending_action_tx_done=%d)",
  48. without_roc, wpa_s->pending_action_tx,
  49. !!wpa_s->pending_action_tx_done);
  50. if (wpa_s->pending_action_tx == NULL || wpa_s->pending_action_tx_done)
  51. return;
  52. /*
  53. * This call is likely going to be on the P2P device instance if the
  54. * driver uses a separate interface for that purpose. However, some
  55. * Action frames are actually sent within a P2P Group and when that is
  56. * the case, we need to follow power saving (e.g., GO buffering the
  57. * frame for a client in PS mode or a client following the advertised
  58. * NoA from its GO). To make that easier for the driver, select the
  59. * correct group interface here.
  60. */
  61. iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
  62. if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
  63. wpa_s->pending_action_freq != 0 &&
  64. wpa_s->pending_action_freq != iface->assoc_freq) {
  65. wpa_printf(MSG_DEBUG, "Off-channel: Pending Action frame TX "
  66. "waiting for another freq=%u (off_channel_freq=%u "
  67. "assoc_freq=%u)",
  68. wpa_s->pending_action_freq,
  69. wpa_s->off_channel_freq,
  70. iface->assoc_freq);
  71. if (without_roc && wpa_s->off_channel_freq == 0) {
  72. unsigned int duration = 200;
  73. /*
  74. * We may get here if wpas_send_action() found us to be
  75. * on the correct channel, but remain-on-channel cancel
  76. * event was received before getting here.
  77. */
  78. wpa_printf(MSG_DEBUG, "Off-channel: Schedule "
  79. "remain-on-channel to send Action frame");
  80. #ifdef CONFIG_TESTING_OPTIONS
  81. if (wpa_s->extra_roc_dur) {
  82. wpa_printf(MSG_DEBUG,
  83. "TESTING: Increase ROC duration %u -> %u",
  84. duration,
  85. duration + wpa_s->extra_roc_dur);
  86. duration += wpa_s->extra_roc_dur;
  87. }
  88. #endif /* CONFIG_TESTING_OPTIONS */
  89. if (wpa_drv_remain_on_channel(
  90. wpa_s, wpa_s->pending_action_freq,
  91. duration) < 0) {
  92. wpa_printf(MSG_DEBUG, "Off-channel: Failed to "
  93. "request driver to remain on "
  94. "channel (%u MHz) for Action Frame "
  95. "TX", wpa_s->pending_action_freq);
  96. } else {
  97. wpa_s->off_channel_freq = 0;
  98. wpa_s->roc_waiting_drv_freq =
  99. wpa_s->pending_action_freq;
  100. }
  101. }
  102. return;
  103. }
  104. wpa_printf(MSG_DEBUG, "Off-channel: Sending pending Action frame to "
  105. MACSTR " using interface %s",
  106. MAC2STR(wpa_s->pending_action_dst), iface->ifname);
  107. res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, 0,
  108. wpa_s->pending_action_dst,
  109. wpa_s->pending_action_src,
  110. wpa_s->pending_action_bssid,
  111. wpabuf_head(wpa_s->pending_action_tx),
  112. wpabuf_len(wpa_s->pending_action_tx),
  113. wpa_s->pending_action_no_cck);
  114. if (res) {
  115. wpa_printf(MSG_DEBUG, "Off-channel: Failed to send the "
  116. "pending Action frame");
  117. /*
  118. * Use fake TX status event to allow state machines to
  119. * continue.
  120. */
  121. offchannel_send_action_tx_status(
  122. wpa_s, wpa_s->pending_action_dst,
  123. wpabuf_head(wpa_s->pending_action_tx),
  124. wpabuf_len(wpa_s->pending_action_tx),
  125. OFFCHANNEL_SEND_ACTION_FAILED);
  126. }
  127. }
  128. /**
  129. * offchannel_send_action_tx_status - TX status callback
  130. * @wpa_s: Pointer to wpa_supplicant data
  131. * @dst: Destination MAC address of the transmitted Action frame
  132. * @data: Transmitted frame payload
  133. * @data_len: Length of @data in bytes
  134. * @result: TX status
  135. *
  136. * This function is called whenever the driver indicates a TX status event for
  137. * a frame sent by offchannel_send_action() using wpa_drv_send_action().
  138. */
  139. void offchannel_send_action_tx_status(
  140. struct wpa_supplicant *wpa_s, const u8 *dst, const u8 *data,
  141. size_t data_len, enum offchannel_send_action_result result)
  142. {
  143. if (wpa_s->pending_action_tx == NULL) {
  144. wpa_printf(MSG_DEBUG, "Off-channel: Ignore Action TX status - "
  145. "no pending operation");
  146. return;
  147. }
  148. if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
  149. wpa_printf(MSG_DEBUG, "Off-channel: Ignore Action TX status - "
  150. "unknown destination address");
  151. return;
  152. }
  153. /* Accept report only if the contents of the frame matches */
  154. if (data_len - wpabuf_len(wpa_s->pending_action_tx) != 24 ||
  155. os_memcmp(data + 24, wpabuf_head(wpa_s->pending_action_tx),
  156. wpabuf_len(wpa_s->pending_action_tx)) != 0) {
  157. wpa_printf(MSG_DEBUG, "Off-channel: Ignore Action TX status - "
  158. "mismatching contents with pending frame");
  159. wpa_hexdump(MSG_MSGDUMP, "TX status frame data",
  160. data, data_len);
  161. wpa_hexdump_buf(MSG_MSGDUMP, "Pending TX frame",
  162. wpa_s->pending_action_tx);
  163. return;
  164. }
  165. wpa_printf(MSG_DEBUG, "Off-channel: Delete matching pending action frame");
  166. wpabuf_free(wpa_s->pending_action_tx);
  167. wpa_s->pending_action_tx = NULL;
  168. wpa_printf(MSG_DEBUG, "Off-channel: TX status result=%d cb=%p",
  169. result, wpa_s->pending_action_tx_status_cb);
  170. if (wpa_s->pending_action_tx_status_cb) {
  171. wpa_s->pending_action_tx_status_cb(
  172. wpa_s, wpa_s->pending_action_freq,
  173. wpa_s->pending_action_dst, wpa_s->pending_action_src,
  174. wpa_s->pending_action_bssid,
  175. data, data_len, result);
  176. }
  177. }
  178. /**
  179. * offchannel_send_action - Request off-channel Action frame TX
  180. * @wpa_s: Pointer to wpa_supplicant data
  181. * @freq: The frequency in MHz indicating the channel on which the frame is to
  182. * transmitted or 0 for the current channel (only if associated)
  183. * @dst: Action frame destination MAC address
  184. * @src: Action frame source MAC address
  185. * @bssid: Action frame BSSID
  186. * @buf: Frame to transmit starting from the Category field
  187. * @len: Length of @buf in bytes
  188. * @wait_time: Wait time for response in milliseconds
  189. * @tx_cb: Callback function for indicating TX status or %NULL for now callback
  190. * @no_cck: Whether CCK rates are to be disallowed for TX rate selection
  191. * Returns: 0 on success or -1 on failure
  192. *
  193. * This function is used to request an Action frame to be transmitted on the
  194. * current operating channel or on another channel (off-channel). The actual
  195. * frame transmission will be delayed until the driver is ready on the specified
  196. * channel. The @wait_time parameter can be used to request the driver to remain
  197. * awake on the channel to wait for a response.
  198. */
  199. int offchannel_send_action(struct wpa_supplicant *wpa_s, unsigned int freq,
  200. const u8 *dst, const u8 *src, const u8 *bssid,
  201. const u8 *buf, size_t len, unsigned int wait_time,
  202. void (*tx_cb)(struct wpa_supplicant *wpa_s,
  203. unsigned int freq, const u8 *dst,
  204. const u8 *src, const u8 *bssid,
  205. const u8 *data, size_t data_len,
  206. enum offchannel_send_action_result
  207. result),
  208. int no_cck)
  209. {
  210. wpa_printf(MSG_DEBUG, "Off-channel: Send action frame: freq=%d dst="
  211. MACSTR " src=" MACSTR " bssid=" MACSTR " len=%d",
  212. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  213. (int) len);
  214. wpa_s->pending_action_tx_status_cb = tx_cb;
  215. if (wpa_s->pending_action_tx) {
  216. wpa_printf(MSG_DEBUG, "Off-channel: Dropped pending Action "
  217. "frame TX to " MACSTR,
  218. MAC2STR(wpa_s->pending_action_dst));
  219. wpabuf_free(wpa_s->pending_action_tx);
  220. }
  221. wpa_s->pending_action_tx_done = 0;
  222. wpa_s->pending_action_tx = wpabuf_alloc(len);
  223. if (wpa_s->pending_action_tx == NULL) {
  224. wpa_printf(MSG_DEBUG, "Off-channel: Failed to allocate Action "
  225. "frame TX buffer (len=%llu)",
  226. (unsigned long long) len);
  227. return -1;
  228. }
  229. wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
  230. os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
  231. os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
  232. os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
  233. wpa_s->pending_action_freq = freq;
  234. wpa_s->pending_action_no_cck = no_cck;
  235. if (freq != 0 && wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
  236. struct wpa_supplicant *iface;
  237. int ret;
  238. iface = wpas_get_tx_interface(wpa_s, src);
  239. wpa_s->action_tx_wait_time = wait_time;
  240. ret = wpa_drv_send_action(
  241. iface, wpa_s->pending_action_freq,
  242. wait_time, wpa_s->pending_action_dst,
  243. wpa_s->pending_action_src, wpa_s->pending_action_bssid,
  244. wpabuf_head(wpa_s->pending_action_tx),
  245. wpabuf_len(wpa_s->pending_action_tx),
  246. wpa_s->pending_action_no_cck);
  247. if (ret == 0)
  248. wpa_s->pending_action_tx_done = 1;
  249. return ret;
  250. }
  251. if (freq) {
  252. struct wpa_supplicant *tx_iface;
  253. tx_iface = wpas_get_tx_interface(wpa_s, src);
  254. if (tx_iface->assoc_freq == freq) {
  255. wpa_printf(MSG_DEBUG, "Off-channel: Already on "
  256. "requested channel (TX interface operating "
  257. "channel)");
  258. freq = 0;
  259. }
  260. }
  261. if (wpa_s->off_channel_freq == freq || freq == 0) {
  262. wpa_printf(MSG_DEBUG, "Off-channel: Already on requested "
  263. "channel; send Action frame immediately");
  264. /* TODO: Would there ever be need to extend the current
  265. * duration on the channel? */
  266. wpa_s->pending_action_without_roc = 1;
  267. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  268. eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
  269. return 0;
  270. }
  271. wpa_s->pending_action_without_roc = 0;
  272. if (wpa_s->roc_waiting_drv_freq == freq) {
  273. wpa_printf(MSG_DEBUG, "Off-channel: Already waiting for "
  274. "driver to get to frequency %u MHz; continue "
  275. "waiting to send the Action frame", freq);
  276. return 0;
  277. }
  278. wpa_printf(MSG_DEBUG, "Off-channel: Schedule Action frame to be "
  279. "transmitted once the driver gets to the requested "
  280. "channel");
  281. if (wait_time > wpa_s->max_remain_on_chan)
  282. wait_time = wpa_s->max_remain_on_chan;
  283. else if (wait_time == 0)
  284. wait_time = 20;
  285. #ifdef CONFIG_TESTING_OPTIONS
  286. if (wpa_s->extra_roc_dur) {
  287. wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
  288. wait_time, wait_time + wpa_s->extra_roc_dur);
  289. wait_time += wpa_s->extra_roc_dur;
  290. }
  291. #endif /* CONFIG_TESTING_OPTIONS */
  292. if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
  293. wpa_printf(MSG_DEBUG, "Off-channel: Failed to request driver "
  294. "to remain on channel (%u MHz) for Action "
  295. "Frame TX", freq);
  296. return -1;
  297. }
  298. wpa_s->off_channel_freq = 0;
  299. wpa_s->roc_waiting_drv_freq = freq;
  300. return 0;
  301. }
  302. /**
  303. * offchannel_send_send_action_done - Notify completion of Action frame sequence
  304. * @wpa_s: Pointer to wpa_supplicant data
  305. *
  306. * This function can be used to cancel a wait for additional response frames on
  307. * the channel that was used with offchannel_send_action().
  308. */
  309. void offchannel_send_action_done(struct wpa_supplicant *wpa_s)
  310. {
  311. wpa_printf(MSG_DEBUG, "Off-channel: Action frame sequence done "
  312. "notification");
  313. wpabuf_free(wpa_s->pending_action_tx);
  314. wpa_s->pending_action_tx = NULL;
  315. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX &&
  316. wpa_s->action_tx_wait_time)
  317. wpa_drv_send_action_cancel_wait(wpa_s);
  318. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  319. wpa_drv_cancel_remain_on_channel(wpa_s);
  320. wpa_s->off_channel_freq = 0;
  321. wpa_s->roc_waiting_drv_freq = 0;
  322. }
  323. }
  324. /**
  325. * offchannel_remain_on_channel_cb - Remain-on-channel callback function
  326. * @wpa_s: Pointer to wpa_supplicant data
  327. * @freq: Frequency (in MHz) of the selected channel
  328. * @duration: Duration of the remain-on-channel operation in milliseconds
  329. *
  330. * This function is called whenever the driver notifies beginning of a
  331. * remain-on-channel operation.
  332. */
  333. void offchannel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  334. unsigned int freq, unsigned int duration)
  335. {
  336. wpa_s->roc_waiting_drv_freq = 0;
  337. wpa_s->off_channel_freq = freq;
  338. wpas_send_action_cb(wpa_s, NULL);
  339. }
  340. /**
  341. * offchannel_cancel_remain_on_channel_cb - Remain-on-channel stopped callback
  342. * @wpa_s: Pointer to wpa_supplicant data
  343. * @freq: Frequency (in MHz) of the selected channel
  344. *
  345. * This function is called whenever the driver notifies termination of a
  346. * remain-on-channel operation.
  347. */
  348. void offchannel_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  349. unsigned int freq)
  350. {
  351. wpa_s->off_channel_freq = 0;
  352. }
  353. /**
  354. * offchannel_pending_action_tx - Check whether there is a pending Action TX
  355. * @wpa_s: Pointer to wpa_supplicant data
  356. * Returns: Pointer to pending frame or %NULL if no pending operation
  357. *
  358. * This function can be used to check whether there is a pending Action frame TX
  359. * operation. The returned pointer should be used only for checking whether it
  360. * is %NULL (no pending frame) or to print the pointer value in debug
  361. * information (i.e., the pointer should not be dereferenced).
  362. */
  363. const void * offchannel_pending_action_tx(struct wpa_supplicant *wpa_s)
  364. {
  365. return wpa_s->pending_action_tx;
  366. }
  367. /**
  368. * offchannel_clear_pending_action_tx - Clear pending Action frame TX
  369. * @wpa_s: Pointer to wpa_supplicant data
  370. */
  371. void offchannel_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  372. {
  373. wpabuf_free(wpa_s->pending_action_tx);
  374. wpa_s->pending_action_tx = NULL;
  375. }
  376. /**
  377. * offchannel_deinit - Deinit off-channel operations
  378. * @wpa_s: Pointer to wpa_supplicant data
  379. *
  380. * This function is used to free up any allocated resources for off-channel
  381. * operations.
  382. */
  383. void offchannel_deinit(struct wpa_supplicant *wpa_s)
  384. {
  385. offchannel_clear_pending_action_tx(wpa_s);
  386. eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
  387. }