offchannel.c 16 KB

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