LCOV - code coverage report
Current view: top level - wpa_supplicant - offchannel.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401872338 Lines: 116 157 73.9 %
Date: 2014-06-04 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.10