offchannel.c 14 KB

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