xref: /freebsd/contrib/wpa/wpa_supplicant/wnm_sta.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*
2  * wpa_supplicant - WNM
3  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/wpa_ctrl.h"
15 #include "rsn_supp/wpa.h"
16 #include "wpa_supplicant_i.h"
17 #include "driver_i.h"
18 #include "scan.h"
19 #include "ctrl_iface.h"
20 #include "bss.h"
21 #include "wnm_sta.h"
22 #include "hs20_supplicant.h"
23 
24 #define MAX_TFS_IE_LEN  1024
25 #define WNM_MAX_NEIGHBOR_REPORT 10
26 
27 
28 /* get the TFS IE from driver */
29 static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
30 				   u16 *buf_len, enum wnm_oper oper)
31 {
32 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
33 
34 	return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
35 }
36 
37 
38 /* set the TFS IE to driver */
39 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
40 				   const u8 *addr, u8 *buf, u16 *buf_len,
41 				   enum wnm_oper oper)
42 {
43 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
44 
45 	return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
46 }
47 
48 
49 /* MLME-SLEEPMODE.request */
50 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
51 				 u8 action, u16 intval, struct wpabuf *tfs_req)
52 {
53 	struct ieee80211_mgmt *mgmt;
54 	int res;
55 	size_t len;
56 	struct wnm_sleep_element *wnmsleep_ie;
57 	u8 *wnmtfs_ie;
58 	u8 wnmsleep_ie_len;
59 	u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
60 	enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
61 		WNM_SLEEP_TFS_REQ_IE_NONE;
62 
63 	wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
64 		   "action=%s to " MACSTR,
65 		   action == 0 ? "enter" : "exit",
66 		   MAC2STR(wpa_s->bssid));
67 
68 	/* WNM-Sleep Mode IE */
69 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
70 	wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
71 	if (wnmsleep_ie == NULL)
72 		return -1;
73 	wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
74 	wnmsleep_ie->len = wnmsleep_ie_len - 2;
75 	wnmsleep_ie->action_type = action;
76 	wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
77 	wnmsleep_ie->intval = host_to_le16(intval);
78 	wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
79 		    (u8 *) wnmsleep_ie, wnmsleep_ie_len);
80 
81 	/* TFS IE(s) */
82 	if (tfs_req) {
83 		wnmtfs_ie_len = wpabuf_len(tfs_req);
84 		wnmtfs_ie = os_malloc(wnmtfs_ie_len);
85 		if (wnmtfs_ie == NULL) {
86 			os_free(wnmsleep_ie);
87 			return -1;
88 		}
89 		os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
90 	} else {
91 		wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
92 		if (wnmtfs_ie == NULL) {
93 			os_free(wnmsleep_ie);
94 			return -1;
95 		}
96 		if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
97 					    tfs_oper)) {
98 			wnmtfs_ie_len = 0;
99 			os_free(wnmtfs_ie);
100 			wnmtfs_ie = NULL;
101 		}
102 	}
103 	wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
104 		    (u8 *) wnmtfs_ie, wnmtfs_ie_len);
105 
106 	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
107 	if (mgmt == NULL) {
108 		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
109 			   "WNM-Sleep Request action frame");
110 		os_free(wnmsleep_ie);
111 		os_free(wnmtfs_ie);
112 		return -1;
113 	}
114 
115 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
116 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
117 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
118 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
119 					   WLAN_FC_STYPE_ACTION);
120 	mgmt->u.action.category = WLAN_ACTION_WNM;
121 	mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
122 	mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
123 	os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
124 		  wnmsleep_ie_len);
125 	/* copy TFS IE here */
126 	if (wnmtfs_ie_len > 0) {
127 		os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
128 			  wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
129 	}
130 
131 	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
132 		wnmtfs_ie_len;
133 
134 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
135 				  wpa_s->own_addr, wpa_s->bssid,
136 				  &mgmt->u.action.category, len, 0);
137 	if (res < 0)
138 		wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
139 			   "(action=%d, intval=%d)", action, intval);
140 	else
141 		wpa_s->wnmsleep_used = 1;
142 
143 	os_free(wnmsleep_ie);
144 	os_free(wnmtfs_ie);
145 	os_free(mgmt);
146 
147 	return res;
148 }
149 
150 
151 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
152 					 u8 *tfsresp_ie_start,
153 					 u8 *tfsresp_ie_end)
154 {
155 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
156 			 wpa_s->bssid, NULL, NULL);
157 	/* remove GTK/IGTK ?? */
158 
159 	/* set the TFS Resp IE(s) */
160 	if (tfsresp_ie_start && tfsresp_ie_end &&
161 	    tfsresp_ie_end - tfsresp_ie_start >= 0) {
162 		u16 tfsresp_ie_len;
163 		tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
164 			tfsresp_ie_start;
165 		wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
166 		/* pass the TFS Resp IE(s) to driver for processing */
167 		if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
168 					    tfsresp_ie_start,
169 					    &tfsresp_ie_len,
170 					    WNM_SLEEP_TFS_RESP_IE_SET))
171 			wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
172 	}
173 }
174 
175 
176 static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
177 					const u8 *frm, u16 key_len_total)
178 {
179 	u8 *ptr, *end;
180 	u8 gtk_len;
181 
182 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,  wpa_s->bssid,
183 			 NULL, NULL);
184 
185 	/* Install GTK/IGTK */
186 
187 	/* point to key data field */
188 	ptr = (u8 *) frm + 1 + 2;
189 	end = ptr + key_len_total;
190 	wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
191 
192 	if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
193 		wpa_msg(wpa_s, MSG_INFO,
194 			"WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
195 		return;
196 	}
197 
198 	while (ptr + 1 < end) {
199 		if (ptr + 2 + ptr[1] > end) {
200 			wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
201 				   "length");
202 			if (end > ptr) {
203 				wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
204 					    ptr, end - ptr);
205 			}
206 			break;
207 		}
208 		if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
209 			if (ptr[1] < 11 + 5) {
210 				wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
211 					   "subelem");
212 				break;
213 			}
214 			gtk_len = *(ptr + 4);
215 			if (ptr[1] < 11 + gtk_len ||
216 			    gtk_len < 5 || gtk_len > 32) {
217 				wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
218 					   "subelem");
219 				break;
220 			}
221 			wpa_wnmsleep_install_key(
222 				wpa_s->wpa,
223 				WNM_SLEEP_SUBELEM_GTK,
224 				ptr);
225 			ptr += 13 + gtk_len;
226 #ifdef CONFIG_IEEE80211W
227 		} else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
228 			if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
229 				wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
230 					   "subelem");
231 				break;
232 			}
233 			wpa_wnmsleep_install_key(wpa_s->wpa,
234 						 WNM_SLEEP_SUBELEM_IGTK, ptr);
235 			ptr += 10 + WPA_IGTK_LEN;
236 #endif /* CONFIG_IEEE80211W */
237 		} else
238 			break; /* skip the loop */
239 	}
240 }
241 
242 
243 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
244 					const u8 *frm, int len)
245 {
246 	/*
247 	 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
248 	 * WNM-Sleep Mode IE | TFS Response IE
249 	 */
250 	u8 *pos = (u8 *) frm; /* point to payload after the action field */
251 	u16 key_len_total;
252 	struct wnm_sleep_element *wnmsleep_ie = NULL;
253 	/* multiple TFS Resp IE (assuming consecutive) */
254 	u8 *tfsresp_ie_start = NULL;
255 	u8 *tfsresp_ie_end = NULL;
256 	size_t left;
257 
258 	if (!wpa_s->wnmsleep_used) {
259 		wpa_printf(MSG_DEBUG,
260 			   "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
261 		return;
262 	}
263 
264 	if (len < 3)
265 		return;
266 	key_len_total = WPA_GET_LE16(frm + 1);
267 
268 	wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
269 		   frm[0], key_len_total);
270 	left = len - 3;
271 	if (key_len_total > left) {
272 		wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
273 		return;
274 	}
275 	pos += 3 + key_len_total;
276 	while (pos - frm < len) {
277 		u8 ie_len = *(pos + 1);
278 		if (pos + 2 + ie_len > frm + len) {
279 			wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
280 			break;
281 		}
282 		wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
283 		if (*pos == WLAN_EID_WNMSLEEP)
284 			wnmsleep_ie = (struct wnm_sleep_element *) pos;
285 		else if (*pos == WLAN_EID_TFS_RESP) {
286 			if (!tfsresp_ie_start)
287 				tfsresp_ie_start = pos;
288 			tfsresp_ie_end = pos;
289 		} else
290 			wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
291 		pos += ie_len + 2;
292 	}
293 
294 	if (!wnmsleep_ie) {
295 		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
296 		return;
297 	}
298 
299 	wpa_s->wnmsleep_used = 0;
300 
301 	if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
302 	    wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
303 		wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
304 			   "frame (action=%d, intval=%d)",
305 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
306 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
307 			wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
308 						     tfsresp_ie_end);
309 		} else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
310 			wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
311 		}
312 	} else {
313 		wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
314 			   "(action=%d, intval=%d)",
315 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
316 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
317 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
318 					 wpa_s->bssid, NULL, NULL);
319 		else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
320 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
321 					 wpa_s->bssid, NULL, NULL);
322 	}
323 }
324 
325 
326 void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
327 {
328 	int i;
329 
330 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
331 		os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
332 		os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
333 	}
334 
335 	wpa_s->wnm_num_neighbor_report = 0;
336 	os_free(wpa_s->wnm_neighbor_report_elements);
337 	wpa_s->wnm_neighbor_report_elements = NULL;
338 }
339 
340 
341 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
342 					   u8 id, u8 elen, const u8 *pos)
343 {
344 	switch (id) {
345 	case WNM_NEIGHBOR_TSF:
346 		if (elen < 2 + 2) {
347 			wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
348 			break;
349 		}
350 		rep->tsf_offset = WPA_GET_LE16(pos);
351 		rep->beacon_int = WPA_GET_LE16(pos + 2);
352 		rep->tsf_present = 1;
353 		break;
354 	case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
355 		if (elen < 2) {
356 			wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
357 				   "country string");
358 			break;
359 		}
360 		os_memcpy(rep->country, pos, 2);
361 		rep->country_present = 1;
362 		break;
363 	case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
364 		if (elen < 1) {
365 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
366 				   "candidate");
367 			break;
368 		}
369 		rep->preference = pos[0];
370 		rep->preference_present = 1;
371 		break;
372 	case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
373 		rep->bss_term_tsf = WPA_GET_LE64(pos);
374 		rep->bss_term_dur = WPA_GET_LE16(pos + 8);
375 		rep->bss_term_present = 1;
376 		break;
377 	case WNM_NEIGHBOR_BEARING:
378 		if (elen < 8) {
379 			wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
380 				   "bearing");
381 			break;
382 		}
383 		rep->bearing = WPA_GET_LE16(pos);
384 		rep->distance = WPA_GET_LE32(pos + 2);
385 		rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
386 		rep->bearing_present = 1;
387 		break;
388 	case WNM_NEIGHBOR_MEASUREMENT_PILOT:
389 		if (elen < 1) {
390 			wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
391 				   "pilot");
392 			break;
393 		}
394 		os_free(rep->meas_pilot);
395 		rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
396 		if (rep->meas_pilot == NULL)
397 			break;
398 		rep->meas_pilot->measurement_pilot = pos[0];
399 		rep->meas_pilot->subelem_len = elen - 1;
400 		os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
401 		break;
402 	case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
403 		if (elen < 5) {
404 			wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
405 				   "capabilities");
406 			break;
407 		}
408 		os_memcpy(rep->rm_capab, pos, 5);
409 		rep->rm_capab_present = 1;
410 		break;
411 	case WNM_NEIGHBOR_MULTIPLE_BSSID:
412 		if (elen < 1) {
413 			wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
414 			break;
415 		}
416 		os_free(rep->mul_bssid);
417 		rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
418 		if (rep->mul_bssid == NULL)
419 			break;
420 		rep->mul_bssid->max_bssid_indicator = pos[0];
421 		rep->mul_bssid->subelem_len = elen - 1;
422 		os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
423 		break;
424 	}
425 }
426 
427 
428 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
429 {
430 	struct wpa_bss *bss = wpa_s->current_bss;
431 	const char *country = NULL;
432 
433 	if (bss) {
434 		const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
435 
436 		if (elem && elem[1] >= 2)
437 			country = (const char *) (elem + 2);
438 	}
439 
440 	return ieee80211_chan_to_freq(country, op_class, chan);
441 }
442 
443 
444 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
445 				      const u8 *pos, u8 len,
446 				      struct neighbor_report *rep)
447 {
448 	u8 left = len;
449 
450 	if (left < 13) {
451 		wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
452 		return;
453 	}
454 
455 	os_memcpy(rep->bssid, pos, ETH_ALEN);
456 	rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
457 	rep->regulatory_class = *(pos + 10);
458 	rep->channel_number = *(pos + 11);
459 	rep->phy_type = *(pos + 12);
460 
461 	pos += 13;
462 	left -= 13;
463 
464 	while (left >= 2) {
465 		u8 id, elen;
466 
467 		id = *pos++;
468 		elen = *pos++;
469 		wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
470 		left -= 2;
471 		if (elen > left) {
472 			wpa_printf(MSG_DEBUG,
473 				   "WNM: Truncated neighbor report subelement");
474 			break;
475 		}
476 		wnm_parse_neighbor_report_elem(rep, id, elen, pos);
477 		left -= elen;
478 		pos += elen;
479 	}
480 
481 	rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
482 				     rep->channel_number);
483 }
484 
485 
486 static struct wpa_bss *
487 compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
488 {
489 
490 	u8 i;
491 	struct wpa_bss *bss = wpa_s->current_bss;
492 	struct wpa_bss *target;
493 
494 	if (!bss)
495 		return 0;
496 
497 	wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
498 		   MAC2STR(wpa_s->bssid), bss->level);
499 
500 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
501 		struct neighbor_report *nei;
502 
503 		nei = &wpa_s->wnm_neighbor_report_elements[i];
504 		if (nei->preference_present && nei->preference == 0) {
505 			wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
506 				   MAC2STR(nei->bssid));
507 			continue;
508 		}
509 
510 		target = wpa_bss_get_bssid(wpa_s, nei->bssid);
511 		if (!target) {
512 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
513 				   " (pref %d) not found in scan results",
514 				   MAC2STR(nei->bssid),
515 				   nei->preference_present ? nei->preference :
516 				   -1);
517 			continue;
518 		}
519 
520 		if (bss->ssid_len != target->ssid_len ||
521 		    os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
522 			/*
523 			 * TODO: Could consider allowing transition to another
524 			 * ESS if PMF was enabled for the association.
525 			 */
526 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
527 				   " (pref %d) in different ESS",
528 				   MAC2STR(nei->bssid),
529 				   nei->preference_present ? nei->preference :
530 				   -1);
531 			continue;
532 		}
533 
534 		if (target->level < bss->level && target->level < -80) {
535 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
536 				   " (pref %d) does not have sufficient signal level (%d)",
537 				   MAC2STR(nei->bssid),
538 				   nei->preference_present ? nei->preference :
539 				   -1,
540 				   target->level);
541 			continue;
542 		}
543 
544 		wpa_printf(MSG_DEBUG,
545 			   "WNM: Found an acceptable preferred transition candidate BSS "
546 			   MACSTR " (RSSI %d)",
547 			   MAC2STR(nei->bssid), target->level);
548 		return target;
549 	}
550 
551 	return NULL;
552 }
553 
554 
555 static void wnm_send_bss_transition_mgmt_resp(
556 	struct wpa_supplicant *wpa_s, u8 dialog_token,
557 	enum bss_trans_mgmt_status_code status, u8 delay,
558 	const u8 *target_bssid)
559 {
560 	u8 buf[1000], *pos;
561 	struct ieee80211_mgmt *mgmt;
562 	size_t len;
563 	int res;
564 
565 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
566 		   "to " MACSTR " dialog_token=%u status=%u delay=%d",
567 		   MAC2STR(wpa_s->bssid), dialog_token, status, delay);
568 	if (!wpa_s->current_bss) {
569 		wpa_printf(MSG_DEBUG,
570 			   "WNM: Current BSS not known - drop response");
571 		return;
572 	}
573 
574 	mgmt = (struct ieee80211_mgmt *) buf;
575 	os_memset(&buf, 0, sizeof(buf));
576 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
577 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
578 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
579 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
580 					   WLAN_FC_STYPE_ACTION);
581 	mgmt->u.action.category = WLAN_ACTION_WNM;
582 	mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
583 	mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
584 	mgmt->u.action.u.bss_tm_resp.status_code = status;
585 	mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
586 	pos = mgmt->u.action.u.bss_tm_resp.variable;
587 	if (target_bssid) {
588 		os_memcpy(pos, target_bssid, ETH_ALEN);
589 		pos += ETH_ALEN;
590 	} else if (status == WNM_BSS_TM_ACCEPT) {
591 		/*
592 		 * P802.11-REVmc clarifies that the Target BSSID field is always
593 		 * present when status code is zero, so use a fake value here if
594 		 * no BSSID is yet known.
595 		 */
596 		os_memset(pos, 0, ETH_ALEN);
597 		pos += ETH_ALEN;
598 	}
599 
600 	len = pos - (u8 *) &mgmt->u.action.category;
601 
602 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
603 				  wpa_s->own_addr, wpa_s->bssid,
604 				  &mgmt->u.action.category, len, 0);
605 	if (res < 0) {
606 		wpa_printf(MSG_DEBUG,
607 			   "WNM: Failed to send BSS Transition Management Response");
608 	}
609 }
610 
611 
612 int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
613 {
614 	struct wpa_bss *bss;
615 	struct wpa_ssid *ssid = wpa_s->current_ssid;
616 	enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
617 
618 	if (!wpa_s->wnm_neighbor_report_elements)
619 		return 0;
620 
621 	if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
622 			      &wpa_s->scan_trigger_time)) {
623 		wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
624 		wnm_deallocate_memory(wpa_s);
625 		return 0;
626 	}
627 
628 	if (!wpa_s->current_bss ||
629 	    os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
630 		      ETH_ALEN) != 0) {
631 		wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
632 		return 0;
633 	}
634 
635 	/* Compare the Neighbor Report and scan results */
636 	bss = compare_scan_neighbor_results(wpa_s);
637 	if (!bss) {
638 		wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
639 		status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
640 		goto send_bss_resp_fail;
641 	}
642 
643 	/* Associate to the network */
644 	/* Send the BSS Management Response - Accept */
645 	if (wpa_s->wnm_reply) {
646 		wpa_s->wnm_reply = 0;
647 		wnm_send_bss_transition_mgmt_resp(wpa_s,
648 						  wpa_s->wnm_dialog_token,
649 						  WNM_BSS_TM_ACCEPT,
650 						  0, bss->bssid);
651 	}
652 
653 	if (bss == wpa_s->current_bss) {
654 		wpa_printf(MSG_DEBUG,
655 			   "WNM: Already associated with the preferred candidate");
656 		return 1;
657 	}
658 
659 	wpa_s->reassociate = 1;
660 	wpa_supplicant_connect(wpa_s, bss, ssid);
661 	wnm_deallocate_memory(wpa_s);
662 	return 1;
663 
664 send_bss_resp_fail:
665 	if (!reply_on_fail)
666 		return 0;
667 
668 	/* Send reject response for all the failures */
669 
670 	if (wpa_s->wnm_reply) {
671 		wpa_s->wnm_reply = 0;
672 		wnm_send_bss_transition_mgmt_resp(wpa_s,
673 						  wpa_s->wnm_dialog_token,
674 						  status, 0, NULL);
675 	}
676 	wnm_deallocate_memory(wpa_s);
677 
678 	return 0;
679 }
680 
681 
682 static int cand_pref_compar(const void *a, const void *b)
683 {
684 	const struct neighbor_report *aa = a;
685 	const struct neighbor_report *bb = b;
686 
687 	if (!aa->preference_present && !bb->preference_present)
688 		return 0;
689 	if (!aa->preference_present)
690 		return 1;
691 	if (!bb->preference_present)
692 		return -1;
693 	if (bb->preference > aa->preference)
694 		return 1;
695 	if (bb->preference < aa->preference)
696 		return -1;
697 	return 0;
698 }
699 
700 
701 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
702 {
703 	if (!wpa_s->wnm_neighbor_report_elements)
704 		return;
705 	qsort(wpa_s->wnm_neighbor_report_elements,
706 	      wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
707 	      cand_pref_compar);
708 }
709 
710 
711 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
712 {
713 	unsigned int i;
714 
715 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
716 	if (!wpa_s->wnm_neighbor_report_elements)
717 		return;
718 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
719 		struct neighbor_report *nei;
720 
721 		nei = &wpa_s->wnm_neighbor_report_elements[i];
722 		wpa_printf(MSG_DEBUG, "%u: " MACSTR
723 			   " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
724 			   i, MAC2STR(nei->bssid), nei->bssid_info,
725 			   nei->regulatory_class,
726 			   nei->channel_number, nei->phy_type,
727 			   nei->preference_present ? nei->preference : -1,
728 			   nei->freq);
729 	}
730 }
731 
732 
733 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
734 {
735 	unsigned int i;
736 
737 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
738 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
739 		int j;
740 
741 		for (j = 0; j < mode->num_channels; j++) {
742 			struct hostapd_channel_data *chan;
743 
744 			chan = &mode->channels[j];
745 			if (chan->freq == freq &&
746 			    !(chan->flag & HOSTAPD_CHAN_DISABLED))
747 				return 1;
748 		}
749 	}
750 
751 	return 0;
752 }
753 
754 
755 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
756 {
757 	int *freqs;
758 	int num_freqs = 0;
759 	unsigned int i;
760 
761 	if (!wpa_s->wnm_neighbor_report_elements)
762 		return;
763 
764 	if (wpa_s->hw.modes == NULL)
765 		return;
766 
767 	os_free(wpa_s->next_scan_freqs);
768 	wpa_s->next_scan_freqs = NULL;
769 
770 	freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
771 	if (freqs == NULL)
772 		return;
773 
774 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
775 		struct neighbor_report *nei;
776 
777 		nei = &wpa_s->wnm_neighbor_report_elements[i];
778 		if (nei->freq <= 0) {
779 			wpa_printf(MSG_DEBUG,
780 				   "WNM: Unknown neighbor operating frequency for "
781 				   MACSTR " - scan all channels",
782 				   MAC2STR(nei->bssid));
783 			os_free(freqs);
784 			return;
785 		}
786 		if (chan_supported(wpa_s, nei->freq))
787 			add_freq(freqs, &num_freqs, nei->freq);
788 	}
789 
790 	if (num_freqs == 0) {
791 		os_free(freqs);
792 		return;
793 	}
794 
795 	wpa_printf(MSG_DEBUG,
796 		   "WNM: Scan %d frequencies based on transition candidate list",
797 		   num_freqs);
798 	wpa_s->next_scan_freqs = freqs;
799 }
800 
801 
802 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
803 					     const u8 *pos, const u8 *end,
804 					     int reply)
805 {
806 	unsigned int beacon_int;
807 	u8 valid_int;
808 
809 	if (pos + 5 > end)
810 		return;
811 
812 	if (wpa_s->current_bss)
813 		beacon_int = wpa_s->current_bss->beacon_int;
814 	else
815 		beacon_int = 100; /* best guess */
816 
817 	wpa_s->wnm_dialog_token = pos[0];
818 	wpa_s->wnm_mode = pos[1];
819 	wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
820 	valid_int = pos[4];
821 	wpa_s->wnm_reply = reply;
822 
823 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
824 		   "dialog_token=%u request_mode=0x%x "
825 		   "disassoc_timer=%u validity_interval=%u",
826 		   wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
827 		   wpa_s->wnm_dissoc_timer, valid_int);
828 
829 	pos += 5;
830 
831 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
832 		if (pos + 12 > end) {
833 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
834 			return;
835 		}
836 		os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
837 		pos += 12; /* BSS Termination Duration */
838 	}
839 
840 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
841 		char url[256];
842 
843 		if (pos + 1 > end || pos + 1 + pos[0] > end) {
844 			wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
845 				   "Management Request (URL)");
846 			return;
847 		}
848 		os_memcpy(url, pos + 1, pos[0]);
849 		url[pos[0]] = '\0';
850 		pos += 1 + pos[0];
851 
852 		wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
853 			wpa_sm_pmf_enabled(wpa_s->wpa),
854 			wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
855 	}
856 
857 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
858 		wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
859 			"Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
860 		if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
861 			/* TODO: mark current BSS less preferred for
862 			 * selection */
863 			wpa_printf(MSG_DEBUG, "Trying to find another BSS");
864 			wpa_supplicant_req_scan(wpa_s, 0, 0);
865 		}
866 	}
867 
868 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
869 		unsigned int valid_ms;
870 
871 		wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
872 		wnm_deallocate_memory(wpa_s);
873 		wpa_s->wnm_neighbor_report_elements = os_calloc(
874 			WNM_MAX_NEIGHBOR_REPORT,
875 			sizeof(struct neighbor_report));
876 		if (wpa_s->wnm_neighbor_report_elements == NULL)
877 			return;
878 
879 		while (pos + 2 <= end &&
880 		       wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
881 		{
882 			u8 tag = *pos++;
883 			u8 len = *pos++;
884 
885 			wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
886 				   tag);
887 			if (pos + len > end) {
888 				wpa_printf(MSG_DEBUG, "WNM: Truncated request");
889 				return;
890 			}
891 			if (tag == WLAN_EID_NEIGHBOR_REPORT) {
892 				struct neighbor_report *rep;
893 				rep = &wpa_s->wnm_neighbor_report_elements[
894 					wpa_s->wnm_num_neighbor_report];
895 				wnm_parse_neighbor_report(wpa_s, pos, len, rep);
896 			}
897 
898 			pos += len;
899 			wpa_s->wnm_num_neighbor_report++;
900 		}
901 		wnm_sort_cand_list(wpa_s);
902 		wnm_dump_cand_list(wpa_s);
903 		valid_ms = valid_int * beacon_int * 128 / 125;
904 		wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
905 			   valid_ms);
906 		os_get_reltime(&wpa_s->wnm_cand_valid_until);
907 		wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
908 		wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
909 		wpa_s->wnm_cand_valid_until.sec +=
910 			wpa_s->wnm_cand_valid_until.usec / 1000000;
911 		wpa_s->wnm_cand_valid_until.usec %= 1000000;
912 		os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
913 
914 		if (wpa_s->last_scan_res_used > 0) {
915 			struct os_reltime now;
916 
917 			os_get_reltime(&now);
918 			if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
919 				wpa_printf(MSG_DEBUG,
920 					   "WNM: Try to use recent scan results");
921 				if (wnm_scan_process(wpa_s, 0) > 0)
922 					return;
923 				wpa_printf(MSG_DEBUG,
924 					   "WNM: No match in previous scan results - try a new scan");
925 			}
926 		}
927 
928 		wnm_set_scan_freqs(wpa_s);
929 		wpa_supplicant_req_scan(wpa_s, 0, 0);
930 	} else if (reply) {
931 		enum bss_trans_mgmt_status_code status;
932 		if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
933 			status = WNM_BSS_TM_ACCEPT;
934 		else {
935 			wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
936 			status = WNM_BSS_TM_REJECT_UNSPECIFIED;
937 		}
938 		wnm_send_bss_transition_mgmt_resp(wpa_s,
939 						  wpa_s->wnm_dialog_token,
940 						  status, 0, NULL);
941 	}
942 }
943 
944 
945 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
946 				       u8 query_reason)
947 {
948 	u8 buf[1000], *pos;
949 	struct ieee80211_mgmt *mgmt;
950 	size_t len;
951 	int ret;
952 
953 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
954 		   MACSTR " query_reason=%u",
955 		   MAC2STR(wpa_s->bssid), query_reason);
956 
957 	mgmt = (struct ieee80211_mgmt *) buf;
958 	os_memset(&buf, 0, sizeof(buf));
959 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
960 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
961 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
962 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
963 					   WLAN_FC_STYPE_ACTION);
964 	mgmt->u.action.category = WLAN_ACTION_WNM;
965 	mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
966 	mgmt->u.action.u.bss_tm_query.dialog_token = 1;
967 	mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
968 	pos = mgmt->u.action.u.bss_tm_query.variable;
969 
970 	len = pos - (u8 *) &mgmt->u.action.category;
971 
972 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
973 				  wpa_s->own_addr, wpa_s->bssid,
974 				  &mgmt->u.action.category, len, 0);
975 
976 	return ret;
977 }
978 
979 
980 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
981 					    const u8 *sa, const u8 *data,
982 					    int len)
983 {
984 	const u8 *pos, *end, *next;
985 	u8 ie, ie_len;
986 
987 	pos = data;
988 	end = data + len;
989 
990 	while (pos + 1 < end) {
991 		ie = *pos++;
992 		ie_len = *pos++;
993 		wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
994 			   ie, ie_len);
995 		if (ie_len > end - pos) {
996 			wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
997 				   "subelement");
998 			break;
999 		}
1000 		next = pos + ie_len;
1001 		if (ie_len < 4) {
1002 			pos = next;
1003 			continue;
1004 		}
1005 		wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1006 			   WPA_GET_BE24(pos), pos[3]);
1007 
1008 #ifdef CONFIG_HS20
1009 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1010 		    WPA_GET_BE24(pos) == OUI_WFA &&
1011 		    pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1012 			/* Subscription Remediation subelement */
1013 			const u8 *ie_end;
1014 			u8 url_len;
1015 			char *url;
1016 			u8 osu_method;
1017 
1018 			wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1019 				   "subelement");
1020 			ie_end = pos + ie_len;
1021 			pos += 4;
1022 			url_len = *pos++;
1023 			if (url_len == 0) {
1024 				wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1025 				url = NULL;
1026 				osu_method = 1;
1027 			} else {
1028 				if (pos + url_len + 1 > ie_end) {
1029 					wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1030 						   url_len,
1031 						   (int) (ie_end - pos));
1032 					break;
1033 				}
1034 				url = os_malloc(url_len + 1);
1035 				if (url == NULL)
1036 					break;
1037 				os_memcpy(url, pos, url_len);
1038 				url[url_len] = '\0';
1039 				osu_method = pos[url_len];
1040 			}
1041 			hs20_rx_subscription_remediation(wpa_s, url,
1042 							 osu_method);
1043 			os_free(url);
1044 			pos = next;
1045 			continue;
1046 		}
1047 
1048 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1049 		    WPA_GET_BE24(pos) == OUI_WFA &&
1050 		    pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1051 			const u8 *ie_end;
1052 			u8 url_len;
1053 			char *url;
1054 			u8 code;
1055 			u16 reauth_delay;
1056 
1057 			ie_end = pos + ie_len;
1058 			pos += 4;
1059 			code = *pos++;
1060 			reauth_delay = WPA_GET_LE16(pos);
1061 			pos += 2;
1062 			url_len = *pos++;
1063 			wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1064 				   "Imminent - Reason Code %u   "
1065 				   "Re-Auth Delay %u  URL Length %u",
1066 				   code, reauth_delay, url_len);
1067 			if (pos + url_len > ie_end)
1068 				break;
1069 			url = os_malloc(url_len + 1);
1070 			if (url == NULL)
1071 				break;
1072 			os_memcpy(url, pos, url_len);
1073 			url[url_len] = '\0';
1074 			hs20_rx_deauth_imminent_notice(wpa_s, code,
1075 						       reauth_delay, url);
1076 			os_free(url);
1077 			pos = next;
1078 			continue;
1079 		}
1080 #endif /* CONFIG_HS20 */
1081 
1082 		pos = next;
1083 	}
1084 }
1085 
1086 
1087 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1088 					const u8 *sa, const u8 *frm, int len)
1089 {
1090 	const u8 *pos, *end;
1091 	u8 dialog_token, type;
1092 
1093 	/* Dialog Token [1] | Type [1] | Subelements */
1094 
1095 	if (len < 2 || sa == NULL)
1096 		return;
1097 	end = frm + len;
1098 	pos = frm;
1099 	dialog_token = *pos++;
1100 	type = *pos++;
1101 
1102 	wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1103 		"(dialog_token %u type %u sa " MACSTR ")",
1104 		dialog_token, type, MAC2STR(sa));
1105 	wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1106 		    pos, end - pos);
1107 
1108 	if (wpa_s->wpa_state != WPA_COMPLETED ||
1109 	    os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1110 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1111 			"from our AP - ignore it");
1112 		return;
1113 	}
1114 
1115 	switch (type) {
1116 	case 1:
1117 		ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1118 		break;
1119 	default:
1120 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1121 			"WNM-Notification type %u", type);
1122 		break;
1123 	}
1124 }
1125 
1126 
1127 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1128 			      const struct ieee80211_mgmt *mgmt, size_t len)
1129 {
1130 	const u8 *pos, *end;
1131 	u8 act;
1132 
1133 	if (len < IEEE80211_HDRLEN + 2)
1134 		return;
1135 
1136 	pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
1137 	act = *pos++;
1138 	end = ((const u8 *) mgmt) + len;
1139 
1140 	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1141 		   act, MAC2STR(mgmt->sa));
1142 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
1143 	    os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1144 		wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1145 			   "frame");
1146 		return;
1147 	}
1148 
1149 	switch (act) {
1150 	case WNM_BSS_TRANS_MGMT_REQ:
1151 		ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
1152 						 !(mgmt->da[0] & 0x01));
1153 		break;
1154 	case WNM_SLEEP_MODE_RESP:
1155 		ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
1156 		break;
1157 	case WNM_NOTIFICATION_REQ:
1158 		ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1159 		break;
1160 	default:
1161 		wpa_printf(MSG_ERROR, "WNM: Unknown request");
1162 		break;
1163 	}
1164 }
1165