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