offchannel.c 13 KB

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