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