Home | History | Annotate | Download | only in wpa_supplicant
      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 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
     22 {
     23 	struct wpa_supplicant *iface;
     24 
     25 	if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
     26 		return wpa_s;
     27 
     28 	/*
     29 	 * Try to find a group interface that matches with the source address.
     30 	 */
     31 	iface = wpa_s->global->ifaces;
     32 	while (iface) {
     33 		if (os_memcmp(wpa_s->pending_action_src,
     34 			      iface->own_addr, ETH_ALEN) == 0)
     35 			break;
     36 		iface = iface->next;
     37 	}
     38 	if (iface) {
     39 		wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
     40 			   "instead of interface %s for Action TX",
     41 			   iface->ifname, wpa_s->ifname);
     42 		return iface;
     43 	}
     44 
     45 	return wpa_s;
     46 }
     47 
     48 
     49 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
     50 {
     51 	struct wpa_supplicant *wpa_s = eloop_ctx;
     52 	struct wpa_supplicant *iface;
     53 	int res;
     54 	int without_roc;
     55 
     56 	without_roc = wpa_s->pending_action_without_roc;
     57 	wpa_s->pending_action_without_roc = 0;
     58 	wpa_printf(MSG_DEBUG, "Off-channel: Send Action callback "
     59 		   "(without_roc=%d pending_action_tx=%p)",
     60 		   without_roc, wpa_s->pending_action_tx);
     61 
     62 	if (wpa_s->pending_action_tx == NULL)
     63 		return;
     64 
     65 	/*
     66 	 * This call is likely going to be on the P2P device instance if the
     67 	 * driver uses a separate interface for that purpose. However, some
     68 	 * Action frames are actually sent within a P2P Group and when that is
     69 	 * the case, we need to follow power saving (e.g., GO buffering the
     70 	 * frame for a client in PS mode or a client following the advertised
     71 	 * NoA from its GO). To make that easier for the driver, select the
     72 	 * correct group interface here.
     73 	 */
     74 	iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
     75 
     76 	if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
     77 	    wpa_s->pending_action_freq != 0 &&
     78 	    wpa_s->pending_action_freq != iface->assoc_freq) {
     79 		wpa_printf(MSG_DEBUG, "Off-channel: Pending Action frame TX "
     80 			   "waiting for another freq=%u (off_channel_freq=%u "
     81 			   "assoc_freq=%u)",
     82 			   wpa_s->pending_action_freq,
     83 			   wpa_s->off_channel_freq,
     84 			   iface->assoc_freq);
     85 		if (without_roc && wpa_s->off_channel_freq == 0) {
     86 			/*
     87 			 * We may get here if wpas_send_action() found us to be
     88 			 * on the correct channel, but remain-on-channel cancel
     89 			 * event was received before getting here.
     90 			 */
     91 			wpa_printf(MSG_DEBUG, "Off-channel: Schedule "
     92 				   "remain-on-channel to send Action frame");
     93 			if (wpa_drv_remain_on_channel(
     94 				    wpa_s, wpa_s->pending_action_freq, 200) <
     95 			    0) {
     96 				wpa_printf(MSG_DEBUG, "Off-channel: Failed to "
     97 					   "request driver to remain on "
     98 					   "channel (%u MHz) for Action Frame "
     99 					   "TX", wpa_s->pending_action_freq);
    100 			} else {
    101 				wpa_s->off_channel_freq = 0;
    102 				wpa_s->roc_waiting_drv_freq =
    103 					wpa_s->pending_action_freq;
    104 			}
    105 		}
    106 		return;
    107 	}
    108 
    109 	wpa_printf(MSG_DEBUG, "Off-channel: Sending pending Action frame to "
    110 		   MACSTR " using interface %s",
    111 		   MAC2STR(wpa_s->pending_action_dst), iface->ifname);
    112 	res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, 0,
    113 				  wpa_s->pending_action_dst,
    114 				  wpa_s->pending_action_src,
    115 				  wpa_s->pending_action_bssid,
    116 				  wpabuf_head(wpa_s->pending_action_tx),
    117 				  wpabuf_len(wpa_s->pending_action_tx),
    118 				  wpa_s->pending_action_no_cck);
    119 	if (res) {
    120 		wpa_printf(MSG_DEBUG, "Off-channel: Failed to send the "
    121 			   "pending Action frame");
    122 		/*
    123 		 * Use fake TX status event to allow state machines to
    124 		 * continue.
    125 		 */
    126 		offchannel_send_action_tx_status(
    127 			wpa_s, wpa_s->pending_action_dst,
    128 			wpabuf_head(wpa_s->pending_action_tx),
    129 			wpabuf_len(wpa_s->pending_action_tx),
    130 			OFFCHANNEL_SEND_ACTION_FAILED);
    131 	}
    132 }
    133 
    134 
    135 void offchannel_send_action_tx_status(
    136 	struct wpa_supplicant *wpa_s, const u8 *dst, const u8 *data,
    137 	size_t data_len, enum offchannel_send_action_result result)
    138 {
    139 	if (wpa_s->pending_action_tx == NULL) {
    140 		wpa_printf(MSG_DEBUG, "Off-channel: Ignore Action TX status - "
    141 			   "no pending operation");
    142 		return;
    143 	}
    144 
    145 	if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
    146 		wpa_printf(MSG_DEBUG, "Off-channel: Ignore Action TX status - "
    147 			   "unknown destination address");
    148 		return;
    149 	}
    150 
    151 	wpabuf_free(wpa_s->pending_action_tx);
    152 	wpa_s->pending_action_tx = NULL;
    153 
    154 	wpa_printf(MSG_DEBUG, "Off-channel: TX status result=%d cb=%p",
    155 		   result, wpa_s->pending_action_tx_status_cb);
    156 
    157 	if (wpa_s->pending_action_tx_status_cb) {
    158 		wpa_s->pending_action_tx_status_cb(
    159 			wpa_s, wpa_s->pending_action_freq,
    160 			wpa_s->pending_action_dst, wpa_s->pending_action_src,
    161 			wpa_s->pending_action_bssid,
    162 			data, data_len, result);
    163 	}
    164 }
    165 
    166 
    167 int offchannel_send_action(struct wpa_supplicant *wpa_s, unsigned int freq,
    168 			   const u8 *dst, const u8 *src, const u8 *bssid,
    169 			   const u8 *buf, size_t len, unsigned int wait_time,
    170 			   void (*tx_cb)(struct wpa_supplicant *wpa_s,
    171 					 unsigned int freq, const u8 *dst,
    172 					 const u8 *src, const u8 *bssid,
    173 					 const u8 *data, size_t data_len,
    174 					 enum offchannel_send_action_result
    175 					 result),
    176 			   int no_cck)
    177 {
    178 	wpa_printf(MSG_DEBUG, "Off-channel: Send action frame: freq=%d dst="
    179 		   MACSTR " src=" MACSTR " bssid=" MACSTR " len=%d",
    180 		   freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
    181 		   (int) len);
    182 
    183 	wpa_s->pending_action_tx_status_cb = tx_cb;
    184 
    185 	if (wpa_s->pending_action_tx) {
    186 		wpa_printf(MSG_DEBUG, "Off-channel: Dropped pending Action "
    187 			   "frame TX to " MACSTR,
    188 			   MAC2STR(wpa_s->pending_action_dst));
    189 		wpabuf_free(wpa_s->pending_action_tx);
    190 	}
    191 	wpa_s->pending_action_tx = wpabuf_alloc(len);
    192 	if (wpa_s->pending_action_tx == NULL) {
    193 		wpa_printf(MSG_DEBUG, "Off-channel: Failed to allocate Action "
    194 			   "frame TX buffer (len=%llu)",
    195 			   (unsigned long long) len);
    196 		return -1;
    197 	}
    198 	wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
    199 	os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
    200 	os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
    201 	os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
    202 	wpa_s->pending_action_freq = freq;
    203 	wpa_s->pending_action_no_cck = no_cck;
    204 
    205 	if (freq != 0 && wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
    206 		struct wpa_supplicant *iface;
    207 
    208 		iface = wpas_get_tx_interface(wpa_s,
    209 					      wpa_s->pending_action_src);
    210 		wpa_s->action_tx_wait_time = wait_time;
    211 
    212 		return wpa_drv_send_action(
    213 			iface, wpa_s->pending_action_freq,
    214 			wait_time, wpa_s->pending_action_dst,
    215 			wpa_s->pending_action_src, wpa_s->pending_action_bssid,
    216 			wpabuf_head(wpa_s->pending_action_tx),
    217 			wpabuf_len(wpa_s->pending_action_tx),
    218 			wpa_s->pending_action_no_cck);
    219 	}
    220 
    221 	if (freq) {
    222 		struct wpa_supplicant *tx_iface;
    223 		tx_iface = wpas_get_tx_interface(wpa_s, src);
    224 		if (tx_iface->assoc_freq == freq) {
    225 			wpa_printf(MSG_DEBUG, "Off-channel: Already on "
    226 				   "requested channel (TX interface operating "
    227 				   "channel)");
    228 			freq = 0;
    229 		}
    230 	}
    231 
    232 	if (wpa_s->off_channel_freq == freq || freq == 0) {
    233 		wpa_printf(MSG_DEBUG, "Off-channel: Already on requested "
    234 			   "channel; send Action frame immediately");
    235 		/* TODO: Would there ever be need to extend the current
    236 		 * duration on the channel? */
    237 		wpa_s->pending_action_without_roc = 1;
    238 		eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
    239 		eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
    240 		return 0;
    241 	}
    242 	wpa_s->pending_action_without_roc = 0;
    243 
    244 	if (wpa_s->roc_waiting_drv_freq == freq) {
    245 		wpa_printf(MSG_DEBUG, "Off-channel: Already waiting for "
    246 			   "driver to get to frequency %u MHz; continue "
    247 			   "waiting to send the Action frame", freq);
    248 		return 0;
    249 	}
    250 
    251 	wpa_printf(MSG_DEBUG, "Off-channel: Schedule Action frame to be "
    252 		   "transmitted once the driver gets to the requested "
    253 		   "channel");
    254 	if (wait_time > wpa_s->max_remain_on_chan)
    255 		wait_time = wpa_s->max_remain_on_chan;
    256 	if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
    257 		wpa_printf(MSG_DEBUG, "Off-channel: Failed to request driver "
    258 			   "to remain on channel (%u MHz) for Action "
    259 			   "Frame TX", freq);
    260 		return -1;
    261 	}
    262 	wpa_s->off_channel_freq = 0;
    263 	wpa_s->roc_waiting_drv_freq = freq;
    264 
    265 	return 0;
    266 }
    267 
    268 
    269 void offchannel_send_action_done(struct wpa_supplicant *wpa_s)
    270 {
    271 	wpa_printf(MSG_DEBUG, "Off-channel: Action frame sequence done "
    272 		   "notification");
    273 	wpabuf_free(wpa_s->pending_action_tx);
    274 	wpa_s->pending_action_tx = NULL;
    275 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX &&
    276 	    wpa_s->action_tx_wait_time)
    277 		wpa_drv_send_action_cancel_wait(wpa_s);
    278 
    279 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    280 		wpa_drv_cancel_remain_on_channel(wpa_s);
    281 		wpa_s->off_channel_freq = 0;
    282 		wpa_s->roc_waiting_drv_freq = 0;
    283 	}
    284 }
    285 
    286 
    287 void offchannel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    288 				     unsigned int freq, unsigned int duration)
    289 {
    290 	wpa_s->roc_waiting_drv_freq = 0;
    291 	wpa_s->off_channel_freq = freq;
    292 	wpas_send_action_cb(wpa_s, NULL);
    293 }
    294 
    295 
    296 void offchannel_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    297 					    unsigned int freq)
    298 {
    299 	wpa_s->off_channel_freq = 0;
    300 }
    301 
    302 
    303 void offchannel_deinit(struct wpa_supplicant *wpa_s)
    304 {
    305 	wpabuf_free(wpa_s->pending_action_tx);
    306 	wpa_s->pending_action_tx = NULL;
    307 	eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
    308 }
    309