Home | History | Annotate | Line # | Download | only in wpa_supplicant
wnm_sta.c revision 1.5.2.1
      1      1.1  christos /*
      2      1.1  christos  * wpa_supplicant - WNM
      3      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.2  christos #include "common/ieee802_11_common.h"
     14      1.2  christos #include "common/wpa_ctrl.h"
     15  1.5.2.1    martin #include "common/ocv.h"
     16      1.1  christos #include "rsn_supp/wpa.h"
     17      1.5  christos #include "config.h"
     18      1.1  christos #include "wpa_supplicant_i.h"
     19      1.1  christos #include "driver_i.h"
     20      1.1  christos #include "scan.h"
     21      1.2  christos #include "ctrl_iface.h"
     22      1.2  christos #include "bss.h"
     23      1.2  christos #include "wnm_sta.h"
     24  1.5.2.1    martin #include "notify.h"
     25      1.2  christos #include "hs20_supplicant.h"
     26      1.1  christos 
     27      1.1  christos #define MAX_TFS_IE_LEN  1024
     28      1.2  christos #define WNM_MAX_NEIGHBOR_REPORT 10
     29      1.1  christos 
     30      1.3  christos #define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
     31      1.1  christos 
     32      1.1  christos /* get the TFS IE from driver */
     33      1.1  christos static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
     34      1.1  christos 				   u16 *buf_len, enum wnm_oper oper)
     35      1.1  christos {
     36      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
     37      1.1  christos 
     38      1.1  christos 	return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
     39      1.1  christos }
     40      1.1  christos 
     41      1.1  christos 
     42      1.1  christos /* set the TFS IE to driver */
     43      1.1  christos static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
     44      1.3  christos 				   const u8 *addr, const u8 *buf, u16 buf_len,
     45      1.1  christos 				   enum wnm_oper oper)
     46      1.1  christos {
     47      1.3  christos 	u16 len = buf_len;
     48      1.3  christos 
     49      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
     50      1.1  christos 
     51      1.3  christos 	return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
     52      1.1  christos }
     53      1.1  christos 
     54      1.1  christos 
     55      1.1  christos /* MLME-SLEEPMODE.request */
     56      1.1  christos int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
     57      1.1  christos 				 u8 action, u16 intval, struct wpabuf *tfs_req)
     58      1.1  christos {
     59      1.1  christos 	struct ieee80211_mgmt *mgmt;
     60      1.1  christos 	int res;
     61      1.1  christos 	size_t len;
     62      1.1  christos 	struct wnm_sleep_element *wnmsleep_ie;
     63  1.5.2.1    martin 	u8 *wnmtfs_ie, *oci_ie;
     64  1.5.2.1    martin 	u8 wnmsleep_ie_len, oci_ie_len;
     65      1.1  christos 	u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
     66      1.1  christos 	enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
     67      1.1  christos 		WNM_SLEEP_TFS_REQ_IE_NONE;
     68      1.1  christos 
     69      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
     70      1.1  christos 		   "action=%s to " MACSTR,
     71      1.1  christos 		   action == 0 ? "enter" : "exit",
     72      1.1  christos 		   MAC2STR(wpa_s->bssid));
     73      1.1  christos 
     74      1.1  christos 	/* WNM-Sleep Mode IE */
     75      1.1  christos 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
     76      1.1  christos 	wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
     77      1.1  christos 	if (wnmsleep_ie == NULL)
     78      1.1  christos 		return -1;
     79      1.1  christos 	wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
     80      1.1  christos 	wnmsleep_ie->len = wnmsleep_ie_len - 2;
     81      1.1  christos 	wnmsleep_ie->action_type = action;
     82      1.1  christos 	wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
     83      1.1  christos 	wnmsleep_ie->intval = host_to_le16(intval);
     84      1.1  christos 	wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
     85      1.1  christos 		    (u8 *) wnmsleep_ie, wnmsleep_ie_len);
     86      1.1  christos 
     87      1.1  christos 	/* TFS IE(s) */
     88      1.1  christos 	if (tfs_req) {
     89      1.1  christos 		wnmtfs_ie_len = wpabuf_len(tfs_req);
     90      1.5  christos 		wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
     91      1.1  christos 		if (wnmtfs_ie == NULL) {
     92      1.1  christos 			os_free(wnmsleep_ie);
     93      1.1  christos 			return -1;
     94      1.1  christos 		}
     95      1.1  christos 	} else {
     96      1.1  christos 		wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
     97      1.1  christos 		if (wnmtfs_ie == NULL) {
     98      1.1  christos 			os_free(wnmsleep_ie);
     99      1.1  christos 			return -1;
    100      1.1  christos 		}
    101      1.1  christos 		if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
    102      1.1  christos 					    tfs_oper)) {
    103      1.1  christos 			wnmtfs_ie_len = 0;
    104      1.1  christos 			os_free(wnmtfs_ie);
    105      1.1  christos 			wnmtfs_ie = NULL;
    106      1.1  christos 		}
    107      1.1  christos 	}
    108      1.1  christos 	wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
    109      1.1  christos 		    (u8 *) wnmtfs_ie, wnmtfs_ie_len);
    110      1.1  christos 
    111  1.5.2.1    martin 	oci_ie = NULL;
    112  1.5.2.1    martin 	oci_ie_len = 0;
    113  1.5.2.1    martin #ifdef CONFIG_OCV
    114  1.5.2.1    martin 	if (action == WNM_SLEEP_MODE_EXIT && wpa_sm_ocv_enabled(wpa_s->wpa)) {
    115  1.5.2.1    martin 		struct wpa_channel_info ci;
    116  1.5.2.1    martin 
    117  1.5.2.1    martin 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
    118  1.5.2.1    martin 			wpa_printf(MSG_WARNING,
    119  1.5.2.1    martin 				   "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
    120  1.5.2.1    martin 			os_free(wnmsleep_ie);
    121  1.5.2.1    martin 			os_free(wnmtfs_ie);
    122  1.5.2.1    martin 			return -1;
    123  1.5.2.1    martin 		}
    124  1.5.2.1    martin 
    125  1.5.2.1    martin 		oci_ie_len = OCV_OCI_EXTENDED_LEN;
    126  1.5.2.1    martin 		oci_ie = os_zalloc(oci_ie_len);
    127  1.5.2.1    martin 		if (!oci_ie) {
    128  1.5.2.1    martin 			wpa_printf(MSG_WARNING,
    129  1.5.2.1    martin 				   "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
    130  1.5.2.1    martin 			os_free(wnmsleep_ie);
    131  1.5.2.1    martin 			os_free(wnmtfs_ie);
    132  1.5.2.1    martin 			return -1;
    133  1.5.2.1    martin 		}
    134  1.5.2.1    martin 
    135  1.5.2.1    martin 		if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
    136  1.5.2.1    martin 			os_free(wnmsleep_ie);
    137  1.5.2.1    martin 			os_free(wnmtfs_ie);
    138  1.5.2.1    martin 			os_free(oci_ie);
    139  1.5.2.1    martin 			return -1;
    140  1.5.2.1    martin 		}
    141  1.5.2.1    martin 	}
    142  1.5.2.1    martin #endif /* CONFIG_OCV */
    143  1.5.2.1    martin 
    144  1.5.2.1    martin 	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
    145  1.5.2.1    martin 			 oci_ie_len);
    146      1.1  christos 	if (mgmt == NULL) {
    147      1.1  christos 		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
    148      1.1  christos 			   "WNM-Sleep Request action frame");
    149      1.1  christos 		os_free(wnmsleep_ie);
    150      1.1  christos 		os_free(wnmtfs_ie);
    151      1.1  christos 		return -1;
    152      1.1  christos 	}
    153      1.1  christos 
    154      1.1  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
    155      1.1  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
    156      1.1  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
    157      1.1  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    158      1.1  christos 					   WLAN_FC_STYPE_ACTION);
    159      1.1  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
    160      1.1  christos 	mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
    161      1.1  christos 	mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
    162      1.1  christos 	os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
    163      1.1  christos 		  wnmsleep_ie_len);
    164      1.1  christos 	/* copy TFS IE here */
    165      1.1  christos 	if (wnmtfs_ie_len > 0) {
    166      1.1  christos 		os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
    167      1.1  christos 			  wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
    168      1.1  christos 	}
    169      1.1  christos 
    170  1.5.2.1    martin #ifdef CONFIG_OCV
    171  1.5.2.1    martin 	/* copy OCV OCI here */
    172  1.5.2.1    martin 	if (oci_ie_len > 0) {
    173  1.5.2.1    martin 		os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
    174  1.5.2.1    martin 			  wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
    175  1.5.2.1    martin 	}
    176  1.5.2.1    martin #endif /* CONFIG_OCV */
    177  1.5.2.1    martin 
    178      1.1  christos 	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
    179  1.5.2.1    martin 		wnmtfs_ie_len + oci_ie_len;
    180      1.1  christos 
    181      1.1  christos 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    182      1.1  christos 				  wpa_s->own_addr, wpa_s->bssid,
    183      1.1  christos 				  &mgmt->u.action.category, len, 0);
    184      1.1  christos 	if (res < 0)
    185      1.1  christos 		wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
    186      1.1  christos 			   "(action=%d, intval=%d)", action, intval);
    187      1.3  christos 	else
    188      1.3  christos 		wpa_s->wnmsleep_used = 1;
    189      1.1  christos 
    190      1.1  christos 	os_free(wnmsleep_ie);
    191      1.1  christos 	os_free(wnmtfs_ie);
    192  1.5.2.1    martin 	os_free(oci_ie);
    193      1.1  christos 	os_free(mgmt);
    194      1.1  christos 
    195      1.1  christos 	return res;
    196      1.1  christos }
    197      1.1  christos 
    198      1.1  christos 
    199      1.1  christos static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
    200      1.3  christos 					 const u8 *tfsresp_ie_start,
    201      1.3  christos 					 const u8 *tfsresp_ie_end)
    202      1.1  christos {
    203      1.1  christos 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
    204      1.1  christos 			 wpa_s->bssid, NULL, NULL);
    205      1.1  christos 	/* remove GTK/IGTK ?? */
    206      1.1  christos 
    207      1.1  christos 	/* set the TFS Resp IE(s) */
    208      1.1  christos 	if (tfsresp_ie_start && tfsresp_ie_end &&
    209      1.1  christos 	    tfsresp_ie_end - tfsresp_ie_start >= 0) {
    210      1.1  christos 		u16 tfsresp_ie_len;
    211      1.1  christos 		tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
    212      1.1  christos 			tfsresp_ie_start;
    213      1.1  christos 		wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
    214      1.1  christos 		/* pass the TFS Resp IE(s) to driver for processing */
    215      1.1  christos 		if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
    216      1.1  christos 					    tfsresp_ie_start,
    217      1.3  christos 					    tfsresp_ie_len,
    218      1.1  christos 					    WNM_SLEEP_TFS_RESP_IE_SET))
    219      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
    220      1.1  christos 	}
    221      1.1  christos }
    222      1.1  christos 
    223      1.1  christos 
    224      1.1  christos static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
    225      1.1  christos 					const u8 *frm, u16 key_len_total)
    226      1.1  christos {
    227      1.1  christos 	u8 *ptr, *end;
    228      1.1  christos 	u8 gtk_len;
    229      1.1  christos 
    230      1.1  christos 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,  wpa_s->bssid,
    231      1.1  christos 			 NULL, NULL);
    232      1.1  christos 
    233      1.1  christos 	/* Install GTK/IGTK */
    234      1.1  christos 
    235      1.1  christos 	/* point to key data field */
    236      1.2  christos 	ptr = (u8 *) frm + 1 + 2;
    237      1.1  christos 	end = ptr + key_len_total;
    238      1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
    239      1.1  christos 
    240      1.2  christos 	if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
    241      1.2  christos 		wpa_msg(wpa_s, MSG_INFO,
    242      1.2  christos 			"WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
    243      1.2  christos 		return;
    244      1.2  christos 	}
    245      1.2  christos 
    246      1.3  christos 	while (end - ptr > 1) {
    247      1.3  christos 		if (2 + ptr[1] > end - ptr) {
    248      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
    249      1.1  christos 				   "length");
    250      1.1  christos 			if (end > ptr) {
    251      1.1  christos 				wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
    252      1.1  christos 					    ptr, end - ptr);
    253      1.1  christos 			}
    254      1.1  christos 			break;
    255      1.1  christos 		}
    256      1.1  christos 		if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
    257      1.1  christos 			if (ptr[1] < 11 + 5) {
    258      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
    259      1.1  christos 					   "subelem");
    260      1.1  christos 				break;
    261      1.1  christos 			}
    262      1.1  christos 			gtk_len = *(ptr + 4);
    263      1.1  christos 			if (ptr[1] < 11 + gtk_len ||
    264      1.1  christos 			    gtk_len < 5 || gtk_len > 32) {
    265      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
    266      1.1  christos 					   "subelem");
    267      1.1  christos 				break;
    268      1.1  christos 			}
    269      1.1  christos 			wpa_wnmsleep_install_key(
    270      1.1  christos 				wpa_s->wpa,
    271      1.1  christos 				WNM_SLEEP_SUBELEM_GTK,
    272      1.1  christos 				ptr);
    273      1.1  christos 			ptr += 13 + gtk_len;
    274      1.1  christos #ifdef CONFIG_IEEE80211W
    275      1.1  christos 		} else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
    276      1.1  christos 			if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
    277      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
    278      1.1  christos 					   "subelem");
    279      1.1  christos 				break;
    280      1.1  christos 			}
    281      1.1  christos 			wpa_wnmsleep_install_key(wpa_s->wpa,
    282      1.1  christos 						 WNM_SLEEP_SUBELEM_IGTK, ptr);
    283      1.1  christos 			ptr += 10 + WPA_IGTK_LEN;
    284      1.1  christos #endif /* CONFIG_IEEE80211W */
    285      1.1  christos 		} else
    286      1.1  christos 			break; /* skip the loop */
    287      1.1  christos 	}
    288      1.1  christos }
    289      1.1  christos 
    290      1.1  christos 
    291      1.1  christos static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
    292      1.1  christos 					const u8 *frm, int len)
    293      1.1  christos {
    294      1.1  christos 	/*
    295      1.2  christos 	 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
    296      1.1  christos 	 * WNM-Sleep Mode IE | TFS Response IE
    297      1.1  christos 	 */
    298      1.3  christos 	const u8 *pos = frm; /* point to payload after the action field */
    299      1.2  christos 	u16 key_len_total;
    300      1.1  christos 	struct wnm_sleep_element *wnmsleep_ie = NULL;
    301      1.1  christos 	/* multiple TFS Resp IE (assuming consecutive) */
    302      1.3  christos 	const u8 *tfsresp_ie_start = NULL;
    303      1.3  christos 	const u8 *tfsresp_ie_end = NULL;
    304  1.5.2.1    martin #ifdef CONFIG_OCV
    305  1.5.2.1    martin 	const u8 *oci_ie = NULL;
    306  1.5.2.1    martin 	u8 oci_ie_len = 0;
    307  1.5.2.1    martin #endif /* CONFIG_OCV */
    308      1.2  christos 	size_t left;
    309      1.1  christos 
    310      1.3  christos 	if (!wpa_s->wnmsleep_used) {
    311      1.3  christos 		wpa_printf(MSG_DEBUG,
    312      1.4       spz 			   "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
    313      1.3  christos 		return;
    314      1.3  christos 	}
    315      1.3  christos 
    316      1.2  christos 	if (len < 3)
    317      1.2  christos 		return;
    318      1.2  christos 	key_len_total = WPA_GET_LE16(frm + 1);
    319      1.2  christos 
    320      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
    321      1.2  christos 		   frm[0], key_len_total);
    322      1.2  christos 	left = len - 3;
    323      1.2  christos 	if (key_len_total > left) {
    324      1.1  christos 		wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
    325      1.1  christos 		return;
    326      1.1  christos 	}
    327      1.2  christos 	pos += 3 + key_len_total;
    328      1.3  christos 	while (pos - frm + 1 < len) {
    329      1.1  christos 		u8 ie_len = *(pos + 1);
    330      1.3  christos 		if (2 + ie_len > frm + len - pos) {
    331      1.1  christos 			wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
    332      1.1  christos 			break;
    333      1.1  christos 		}
    334      1.1  christos 		wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
    335      1.3  christos 		if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
    336      1.1  christos 			wnmsleep_ie = (struct wnm_sleep_element *) pos;
    337      1.1  christos 		else if (*pos == WLAN_EID_TFS_RESP) {
    338      1.1  christos 			if (!tfsresp_ie_start)
    339      1.1  christos 				tfsresp_ie_start = pos;
    340      1.1  christos 			tfsresp_ie_end = pos;
    341  1.5.2.1    martin #ifdef CONFIG_OCV
    342  1.5.2.1    martin 		} else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
    343  1.5.2.1    martin 			   pos[2] == WLAN_EID_EXT_OCV_OCI) {
    344  1.5.2.1    martin 			oci_ie = pos + 3;
    345  1.5.2.1    martin 			oci_ie_len = ie_len - 1;
    346  1.5.2.1    martin #endif /* CONFIG_OCV */
    347      1.1  christos 		} else
    348      1.1  christos 			wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
    349      1.1  christos 		pos += ie_len + 2;
    350      1.1  christos 	}
    351      1.1  christos 
    352      1.1  christos 	if (!wnmsleep_ie) {
    353      1.1  christos 		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
    354      1.1  christos 		return;
    355      1.1  christos 	}
    356      1.1  christos 
    357  1.5.2.1    martin #ifdef CONFIG_OCV
    358  1.5.2.1    martin 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
    359  1.5.2.1    martin 	    wpa_sm_ocv_enabled(wpa_s->wpa)) {
    360  1.5.2.1    martin 		struct wpa_channel_info ci;
    361  1.5.2.1    martin 
    362  1.5.2.1    martin 		if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
    363  1.5.2.1    martin 			wpa_msg(wpa_s, MSG_WARNING,
    364  1.5.2.1    martin 				"Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
    365  1.5.2.1    martin 			return;
    366  1.5.2.1    martin 		}
    367  1.5.2.1    martin 
    368  1.5.2.1    martin 		if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
    369  1.5.2.1    martin 					 channel_width_to_int(ci.chanwidth),
    370  1.5.2.1    martin 					 ci.seg1_idx) != 0) {
    371  1.5.2.1    martin 			wpa_msg(wpa_s, MSG_WARNING, "WNM: %s", ocv_errorstr);
    372  1.5.2.1    martin 			return;
    373  1.5.2.1    martin 		}
    374  1.5.2.1    martin 	}
    375  1.5.2.1    martin #endif /* CONFIG_OCV */
    376  1.5.2.1    martin 
    377      1.4       spz 	wpa_s->wnmsleep_used = 0;
    378      1.4       spz 
    379      1.1  christos 	if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
    380      1.1  christos 	    wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
    381      1.1  christos 		wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
    382      1.1  christos 			   "frame (action=%d, intval=%d)",
    383      1.1  christos 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
    384      1.1  christos 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
    385      1.1  christos 			wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
    386      1.1  christos 						     tfsresp_ie_end);
    387      1.1  christos 		} else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
    388      1.1  christos 			wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
    389      1.1  christos 		}
    390      1.1  christos 	} else {
    391      1.1  christos 		wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
    392      1.1  christos 			   "(action=%d, intval=%d)",
    393      1.1  christos 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
    394      1.1  christos 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
    395      1.1  christos 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
    396      1.1  christos 					 wpa_s->bssid, NULL, NULL);
    397      1.1  christos 		else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
    398      1.1  christos 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
    399      1.1  christos 					 wpa_s->bssid, NULL, NULL);
    400      1.1  christos 	}
    401      1.1  christos }
    402      1.1  christos 
    403      1.1  christos 
    404      1.2  christos void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
    405      1.2  christos {
    406      1.2  christos 	int i;
    407      1.2  christos 
    408      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    409      1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
    410      1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
    411      1.2  christos 	}
    412      1.2  christos 
    413      1.2  christos 	wpa_s->wnm_num_neighbor_report = 0;
    414      1.2  christos 	os_free(wpa_s->wnm_neighbor_report_elements);
    415      1.2  christos 	wpa_s->wnm_neighbor_report_elements = NULL;
    416      1.5  christos 
    417      1.5  christos 	wpabuf_free(wpa_s->coloc_intf_elems);
    418      1.5  christos 	wpa_s->coloc_intf_elems = NULL;
    419      1.2  christos }
    420      1.2  christos 
    421      1.2  christos 
    422      1.2  christos static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
    423      1.2  christos 					   u8 id, u8 elen, const u8 *pos)
    424      1.2  christos {
    425      1.2  christos 	switch (id) {
    426      1.2  christos 	case WNM_NEIGHBOR_TSF:
    427      1.2  christos 		if (elen < 2 + 2) {
    428      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
    429      1.2  christos 			break;
    430      1.2  christos 		}
    431      1.2  christos 		rep->tsf_offset = WPA_GET_LE16(pos);
    432      1.2  christos 		rep->beacon_int = WPA_GET_LE16(pos + 2);
    433      1.2  christos 		rep->tsf_present = 1;
    434      1.2  christos 		break;
    435      1.2  christos 	case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
    436      1.2  christos 		if (elen < 2) {
    437      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
    438      1.2  christos 				   "country string");
    439      1.2  christos 			break;
    440      1.2  christos 		}
    441      1.2  christos 		os_memcpy(rep->country, pos, 2);
    442      1.2  christos 		rep->country_present = 1;
    443      1.2  christos 		break;
    444      1.2  christos 	case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
    445      1.2  christos 		if (elen < 1) {
    446      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
    447      1.2  christos 				   "candidate");
    448      1.2  christos 			break;
    449      1.2  christos 		}
    450      1.2  christos 		rep->preference = pos[0];
    451      1.2  christos 		rep->preference_present = 1;
    452      1.2  christos 		break;
    453      1.2  christos 	case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
    454  1.5.2.1    martin 		if (elen < 10) {
    455  1.5.2.1    martin 			wpa_printf(MSG_DEBUG,
    456  1.5.2.1    martin 				   "WNM: Too short BSS termination duration");
    457  1.5.2.1    martin 			break;
    458  1.5.2.1    martin 		}
    459      1.2  christos 		rep->bss_term_tsf = WPA_GET_LE64(pos);
    460      1.2  christos 		rep->bss_term_dur = WPA_GET_LE16(pos + 8);
    461      1.2  christos 		rep->bss_term_present = 1;
    462      1.2  christos 		break;
    463      1.2  christos 	case WNM_NEIGHBOR_BEARING:
    464      1.2  christos 		if (elen < 8) {
    465      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
    466      1.2  christos 				   "bearing");
    467      1.2  christos 			break;
    468      1.2  christos 		}
    469      1.2  christos 		rep->bearing = WPA_GET_LE16(pos);
    470      1.2  christos 		rep->distance = WPA_GET_LE32(pos + 2);
    471      1.2  christos 		rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
    472      1.2  christos 		rep->bearing_present = 1;
    473      1.2  christos 		break;
    474      1.2  christos 	case WNM_NEIGHBOR_MEASUREMENT_PILOT:
    475      1.2  christos 		if (elen < 1) {
    476      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
    477      1.2  christos 				   "pilot");
    478      1.2  christos 			break;
    479      1.2  christos 		}
    480      1.2  christos 		os_free(rep->meas_pilot);
    481      1.2  christos 		rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
    482      1.2  christos 		if (rep->meas_pilot == NULL)
    483      1.2  christos 			break;
    484      1.2  christos 		rep->meas_pilot->measurement_pilot = pos[0];
    485      1.2  christos 		rep->meas_pilot->subelem_len = elen - 1;
    486      1.2  christos 		os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
    487      1.2  christos 		break;
    488      1.2  christos 	case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
    489      1.2  christos 		if (elen < 5) {
    490      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
    491      1.2  christos 				   "capabilities");
    492      1.2  christos 			break;
    493      1.2  christos 		}
    494      1.2  christos 		os_memcpy(rep->rm_capab, pos, 5);
    495      1.2  christos 		rep->rm_capab_present = 1;
    496      1.2  christos 		break;
    497      1.2  christos 	case WNM_NEIGHBOR_MULTIPLE_BSSID:
    498      1.2  christos 		if (elen < 1) {
    499      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
    500      1.2  christos 			break;
    501      1.2  christos 		}
    502      1.2  christos 		os_free(rep->mul_bssid);
    503      1.2  christos 		rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
    504      1.2  christos 		if (rep->mul_bssid == NULL)
    505      1.2  christos 			break;
    506      1.2  christos 		rep->mul_bssid->max_bssid_indicator = pos[0];
    507      1.2  christos 		rep->mul_bssid->subelem_len = elen - 1;
    508      1.2  christos 		os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
    509      1.2  christos 		break;
    510      1.2  christos 	}
    511      1.2  christos }
    512      1.2  christos 
    513      1.2  christos 
    514      1.2  christos static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
    515      1.2  christos {
    516      1.2  christos 	struct wpa_bss *bss = wpa_s->current_bss;
    517      1.2  christos 	const char *country = NULL;
    518      1.3  christos 	int freq;
    519      1.2  christos 
    520      1.2  christos 	if (bss) {
    521      1.2  christos 		const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
    522      1.2  christos 
    523      1.2  christos 		if (elem && elem[1] >= 2)
    524      1.2  christos 			country = (const char *) (elem + 2);
    525      1.2  christos 	}
    526      1.2  christos 
    527      1.3  christos 	freq = ieee80211_chan_to_freq(country, op_class, chan);
    528      1.3  christos 	if (freq <= 0 && op_class == 0) {
    529      1.3  christos 		/*
    530      1.3  christos 		 * Some APs do not advertise correct operating class
    531      1.3  christos 		 * information. Try to determine the most likely operating
    532      1.3  christos 		 * frequency based on the channel number.
    533      1.3  christos 		 */
    534      1.3  christos 		if (chan >= 1 && chan <= 13)
    535      1.3  christos 			freq = 2407 + chan * 5;
    536      1.3  christos 		else if (chan == 14)
    537      1.3  christos 			freq = 2484;
    538      1.3  christos 		else if (chan >= 36 && chan <= 169)
    539      1.3  christos 			freq = 5000 + chan * 5;
    540      1.3  christos 	}
    541      1.3  christos 	return freq;
    542      1.2  christos }
    543      1.2  christos 
    544      1.2  christos 
    545      1.2  christos static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
    546      1.2  christos 				      const u8 *pos, u8 len,
    547      1.2  christos 				      struct neighbor_report *rep)
    548      1.2  christos {
    549      1.2  christos 	u8 left = len;
    550      1.2  christos 
    551      1.2  christos 	if (left < 13) {
    552      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
    553      1.2  christos 		return;
    554      1.2  christos 	}
    555      1.2  christos 
    556      1.2  christos 	os_memcpy(rep->bssid, pos, ETH_ALEN);
    557      1.2  christos 	rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
    558      1.2  christos 	rep->regulatory_class = *(pos + 10);
    559      1.2  christos 	rep->channel_number = *(pos + 11);
    560      1.2  christos 	rep->phy_type = *(pos + 12);
    561      1.2  christos 
    562      1.2  christos 	pos += 13;
    563      1.2  christos 	left -= 13;
    564      1.2  christos 
    565      1.2  christos 	while (left >= 2) {
    566      1.2  christos 		u8 id, elen;
    567      1.2  christos 
    568      1.2  christos 		id = *pos++;
    569      1.2  christos 		elen = *pos++;
    570      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
    571      1.2  christos 		left -= 2;
    572      1.2  christos 		if (elen > left) {
    573      1.2  christos 			wpa_printf(MSG_DEBUG,
    574      1.2  christos 				   "WNM: Truncated neighbor report subelement");
    575      1.2  christos 			break;
    576      1.2  christos 		}
    577      1.2  christos 		wnm_parse_neighbor_report_elem(rep, id, elen, pos);
    578      1.2  christos 		left -= elen;
    579      1.2  christos 		pos += elen;
    580      1.2  christos 	}
    581      1.2  christos 
    582      1.2  christos 	rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
    583      1.2  christos 				     rep->channel_number);
    584      1.2  christos }
    585      1.2  christos 
    586      1.2  christos 
    587      1.5  christos static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
    588      1.5  christos {
    589      1.5  christos 	unsigned int i;
    590      1.5  christos 
    591      1.5  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
    592      1.5  christos 		wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
    593      1.5  christos }
    594      1.5  christos 
    595      1.5  christos 
    596      1.5  christos static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
    597      1.5  christos {
    598      1.5  christos 	unsigned int i;
    599      1.5  christos 	struct neighbor_report *nei;
    600      1.5  christos 
    601      1.5  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    602      1.5  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
    603      1.5  christos 		if (nei->acceptable)
    604      1.5  christos 			return wpa_bss_get_bssid(wpa_s, nei->bssid);
    605      1.5  christos 	}
    606      1.5  christos 
    607      1.5  christos 	return NULL;
    608      1.5  christos }
    609      1.5  christos 
    610      1.5  christos 
    611      1.5  christos #ifdef CONFIG_MBO
    612      1.2  christos static struct wpa_bss *
    613      1.5  christos get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
    614      1.5  christos 			     enum mbo_transition_reject_reason *reason)
    615      1.2  christos {
    616      1.5  christos 	struct wpa_bss *target = NULL;
    617      1.5  christos 	struct wpa_bss_trans_info params;
    618      1.5  christos 	struct wpa_bss_candidate_info *info = NULL;
    619      1.5  christos 	struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
    620      1.5  christos 	u8 *first_candidate_bssid = NULL, *pos;
    621      1.5  christos 	unsigned int i;
    622      1.5  christos 
    623      1.5  christos 	params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
    624      1.5  christos 	params.n_candidates = 0;
    625      1.5  christos 	params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
    626      1.5  christos 	if (!params.bssid)
    627      1.5  christos 		return NULL;
    628      1.5  christos 
    629      1.5  christos 	pos = params.bssid;
    630      1.5  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
    631      1.5  christos 		if (nei->is_first)
    632      1.5  christos 			first_candidate_bssid = nei->bssid;
    633      1.5  christos 		if (!nei->acceptable)
    634      1.5  christos 			continue;
    635      1.5  christos 		os_memcpy(pos, nei->bssid, ETH_ALEN);
    636      1.5  christos 		pos += ETH_ALEN;
    637      1.5  christos 		params.n_candidates++;
    638      1.5  christos 	}
    639      1.5  christos 
    640      1.5  christos 	if (!params.n_candidates)
    641      1.5  christos 		goto end;
    642      1.5  christos 
    643      1.5  christos 	info = wpa_drv_get_bss_trans_status(wpa_s, &params);
    644      1.5  christos 	if (!info) {
    645      1.5  christos 		/* If failed to get candidate BSS transition status from driver,
    646      1.5  christos 		 * get the first acceptable candidate from wpa_supplicant.
    647      1.5  christos 		 */
    648      1.5  christos 		target = wpa_bss_get_bssid(wpa_s, params.bssid);
    649      1.5  christos 		goto end;
    650      1.5  christos 	}
    651      1.5  christos 
    652      1.5  christos 	/* Get the first acceptable candidate from driver */
    653      1.5  christos 	for (i = 0; i < info->num; i++) {
    654      1.5  christos 		if (info->candidates[i].is_accept) {
    655      1.5  christos 			target = wpa_bss_get_bssid(wpa_s,
    656      1.5  christos 						   info->candidates[i].bssid);
    657      1.5  christos 			goto end;
    658      1.5  christos 		}
    659      1.5  christos 	}
    660      1.5  christos 
    661      1.5  christos 	/* If Disassociation Imminent is set and driver rejects all the
    662      1.5  christos 	 * candidate select first acceptable candidate which has
    663      1.5  christos 	 * rssi > disassoc_imminent_rssi_threshold
    664      1.5  christos 	 */
    665      1.5  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
    666      1.5  christos 		for (i = 0; i < info->num; i++) {
    667      1.5  christos 			target = wpa_bss_get_bssid(wpa_s,
    668      1.5  christos 						   info->candidates[i].bssid);
    669      1.5  christos 			if (target &&
    670      1.5  christos 			    (target->level <
    671      1.5  christos 			     wpa_s->conf->disassoc_imminent_rssi_threshold))
    672      1.5  christos 				continue;
    673      1.5  christos 			goto end;
    674      1.5  christos 		}
    675      1.5  christos 	}
    676      1.5  christos 
    677      1.5  christos 	/* While sending BTM reject use reason code of the first candidate
    678      1.5  christos 	 * received in BTM request frame
    679      1.5  christos 	 */
    680      1.5  christos 	if (reason) {
    681      1.5  christos 		for (i = 0; i < info->num; i++) {
    682      1.5  christos 			if (first_candidate_bssid &&
    683      1.5  christos 			    os_memcmp(first_candidate_bssid,
    684      1.5  christos 				      info->candidates[i].bssid, ETH_ALEN) == 0)
    685      1.5  christos 			{
    686      1.5  christos 				*reason = info->candidates[i].reject_reason;
    687      1.5  christos 				break;
    688      1.5  christos 			}
    689      1.5  christos 		}
    690      1.5  christos 	}
    691      1.5  christos 
    692      1.5  christos 	target = NULL;
    693      1.5  christos 
    694      1.5  christos end:
    695      1.5  christos 	os_free(params.bssid);
    696      1.5  christos 	if (info) {
    697      1.5  christos 		os_free(info->candidates);
    698      1.5  christos 		os_free(info);
    699      1.5  christos 	}
    700      1.5  christos 	return target;
    701      1.5  christos }
    702      1.5  christos #endif /* CONFIG_MBO */
    703      1.5  christos 
    704      1.2  christos 
    705      1.5  christos static struct wpa_bss *
    706      1.5  christos compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
    707      1.5  christos 			      enum mbo_transition_reject_reason *reason)
    708      1.5  christos {
    709      1.2  christos 	u8 i;
    710      1.2  christos 	struct wpa_bss *bss = wpa_s->current_bss;
    711      1.2  christos 	struct wpa_bss *target;
    712      1.2  christos 
    713      1.2  christos 	if (!bss)
    714      1.3  christos 		return NULL;
    715      1.2  christos 
    716      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
    717      1.2  christos 		   MAC2STR(wpa_s->bssid), bss->level);
    718      1.2  christos 
    719      1.5  christos 	wnm_clear_acceptable(wpa_s);
    720      1.5  christos 
    721      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    722      1.2  christos 		struct neighbor_report *nei;
    723      1.2  christos 
    724      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
    725      1.2  christos 		if (nei->preference_present && nei->preference == 0) {
    726      1.2  christos 			wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
    727      1.2  christos 				   MAC2STR(nei->bssid));
    728      1.2  christos 			continue;
    729      1.2  christos 		}
    730      1.2  christos 
    731      1.2  christos 		target = wpa_bss_get_bssid(wpa_s, nei->bssid);
    732      1.2  christos 		if (!target) {
    733      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    734      1.2  christos 				   " (pref %d) not found in scan results",
    735      1.2  christos 				   MAC2STR(nei->bssid),
    736      1.2  christos 				   nei->preference_present ? nei->preference :
    737      1.2  christos 				   -1);
    738      1.2  christos 			continue;
    739      1.2  christos 		}
    740      1.2  christos 
    741      1.3  christos 		if (age_secs) {
    742      1.3  christos 			struct os_reltime now;
    743      1.3  christos 
    744      1.3  christos 			if (os_get_reltime(&now) == 0 &&
    745      1.3  christos 			    os_reltime_expired(&now, &target->last_update,
    746      1.3  christos 					       age_secs)) {
    747      1.3  christos 				wpa_printf(MSG_DEBUG,
    748      1.3  christos 					   "Candidate BSS is more than %ld seconds old",
    749      1.3  christos 					   age_secs);
    750      1.3  christos 				continue;
    751      1.3  christos 			}
    752      1.3  christos 		}
    753      1.3  christos 
    754      1.2  christos 		if (bss->ssid_len != target->ssid_len ||
    755      1.2  christos 		    os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
    756      1.2  christos 			/*
    757      1.2  christos 			 * TODO: Could consider allowing transition to another
    758      1.2  christos 			 * ESS if PMF was enabled for the association.
    759      1.2  christos 			 */
    760      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    761      1.2  christos 				   " (pref %d) in different ESS",
    762      1.2  christos 				   MAC2STR(nei->bssid),
    763      1.2  christos 				   nei->preference_present ? nei->preference :
    764      1.2  christos 				   -1);
    765      1.2  christos 			continue;
    766      1.2  christos 		}
    767      1.2  christos 
    768      1.3  christos 		if (wpa_s->current_ssid &&
    769      1.3  christos 		    !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
    770      1.5  christos 					1, 0)) {
    771      1.3  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    772      1.3  christos 				   " (pref %d) does not match the current network profile",
    773      1.3  christos 				   MAC2STR(nei->bssid),
    774      1.3  christos 				   nei->preference_present ? nei->preference :
    775      1.3  christos 				   -1);
    776      1.3  christos 			continue;
    777      1.3  christos 		}
    778      1.3  christos 
    779  1.5.2.1    martin 		if (wpa_is_bss_tmp_disallowed(wpa_s, target)) {
    780      1.3  christos 			wpa_printf(MSG_DEBUG,
    781      1.3  christos 				   "MBO: Candidate BSS " MACSTR
    782      1.3  christos 				   " retry delay is not over yet",
    783      1.3  christos 				   MAC2STR(nei->bssid));
    784      1.3  christos 			continue;
    785      1.3  christos 		}
    786      1.3  christos 
    787      1.2  christos 		if (target->level < bss->level && target->level < -80) {
    788      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    789      1.2  christos 				   " (pref %d) does not have sufficient signal level (%d)",
    790      1.2  christos 				   MAC2STR(nei->bssid),
    791      1.2  christos 				   nei->preference_present ? nei->preference :
    792      1.2  christos 				   -1,
    793      1.2  christos 				   target->level);
    794      1.2  christos 			continue;
    795      1.2  christos 		}
    796      1.2  christos 
    797      1.5  christos 		nei->acceptable = 1;
    798      1.5  christos 	}
    799      1.5  christos 
    800      1.5  christos #ifdef CONFIG_MBO
    801      1.5  christos 	if (wpa_s->wnm_mbo_trans_reason_present)
    802      1.5  christos 		target = get_mbo_transition_candidate(wpa_s, reason);
    803      1.5  christos 	else
    804      1.5  christos 		target = get_first_acceptable(wpa_s);
    805      1.5  christos #else /* CONFIG_MBO */
    806      1.5  christos 	target = get_first_acceptable(wpa_s);
    807      1.5  christos #endif /* CONFIG_MBO */
    808      1.5  christos 
    809      1.5  christos 	if (target) {
    810      1.2  christos 		wpa_printf(MSG_DEBUG,
    811      1.2  christos 			   "WNM: Found an acceptable preferred transition candidate BSS "
    812      1.2  christos 			   MACSTR " (RSSI %d)",
    813      1.5  christos 			   MAC2STR(target->bssid), target->level);
    814      1.2  christos 	}
    815      1.2  christos 
    816      1.5  christos 	return target;
    817      1.2  christos }
    818      1.2  christos 
    819      1.2  christos 
    820      1.3  christos static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
    821      1.3  christos {
    822      1.3  christos 	const u8 *ie_a, *ie_b;
    823      1.3  christos 
    824      1.3  christos 	if (!a || !b)
    825      1.3  christos 		return 0;
    826      1.3  christos 
    827      1.3  christos 	ie_a = wpa_bss_get_ie(a, eid);
    828      1.3  christos 	ie_b = wpa_bss_get_ie(b, eid);
    829      1.3  christos 
    830      1.3  christos 	if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
    831      1.3  christos 		return 0;
    832      1.3  christos 
    833      1.3  christos 	return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
    834      1.3  christos }
    835      1.3  christos 
    836      1.3  christos 
    837      1.3  christos static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
    838      1.3  christos {
    839      1.3  christos 	u32 info = 0;
    840      1.3  christos 
    841      1.3  christos 	info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
    842      1.3  christos 
    843      1.3  christos 	/*
    844      1.3  christos 	 * Leave the security and key scope bits unset to indicate that the
    845      1.3  christos 	 * security information is not available.
    846      1.3  christos 	 */
    847      1.3  christos 
    848      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
    849      1.3  christos 		info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
    850      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_QOS)
    851      1.3  christos 		info |= NEI_REP_BSSID_INFO_QOS;
    852      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_APSD)
    853      1.3  christos 		info |= NEI_REP_BSSID_INFO_APSD;
    854      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
    855      1.3  christos 		info |= NEI_REP_BSSID_INFO_RM;
    856      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
    857      1.3  christos 		info |= NEI_REP_BSSID_INFO_DELAYED_BA;
    858      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
    859      1.3  christos 		info |= NEI_REP_BSSID_INFO_IMM_BA;
    860      1.3  christos 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
    861      1.3  christos 		info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
    862      1.3  christos 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
    863      1.3  christos 		info |= NEI_REP_BSSID_INFO_HT;
    864      1.3  christos 
    865      1.3  christos 	return info;
    866      1.3  christos }
    867      1.3  christos 
    868      1.3  christos 
    869      1.5  christos static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
    870      1.5  christos 			   u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
    871      1.5  christos 			   u8 pref)
    872      1.3  christos {
    873      1.5  christos 	if (wpabuf_len(*buf) + 18 >
    874      1.5  christos 	    IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
    875      1.5  christos 		wpa_printf(MSG_DEBUG,
    876      1.5  christos 			   "WNM: No room in frame for Neighbor Report element");
    877      1.5  christos 		return -1;
    878      1.5  christos 	}
    879      1.3  christos 
    880      1.5  christos 	if (wpabuf_resize(buf, 18) < 0) {
    881      1.3  christos 		wpa_printf(MSG_DEBUG,
    882      1.5  christos 			   "WNM: Failed to allocate memory for Neighbor Report element");
    883      1.3  christos 		return -1;
    884      1.3  christos 	}
    885      1.3  christos 
    886      1.5  christos 	wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
    887      1.3  christos 	/* length: 13 for basic neighbor report + 3 for preference subelement */
    888      1.5  christos 	wpabuf_put_u8(*buf, 16);
    889      1.5  christos 	wpabuf_put_data(*buf, bssid, ETH_ALEN);
    890      1.5  christos 	wpabuf_put_le32(*buf, bss_info);
    891      1.5  christos 	wpabuf_put_u8(*buf, op_class);
    892      1.5  christos 	wpabuf_put_u8(*buf, chan);
    893      1.5  christos 	wpabuf_put_u8(*buf, phy_type);
    894      1.5  christos 	wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
    895      1.5  christos 	wpabuf_put_u8(*buf, 1);
    896      1.5  christos 	wpabuf_put_u8(*buf, pref);
    897      1.5  christos 	return 0;
    898      1.3  christos }
    899      1.3  christos 
    900      1.3  christos 
    901      1.3  christos static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
    902      1.5  christos 			       struct wpa_bss *bss, struct wpabuf **buf,
    903      1.3  christos 			       u8 pref)
    904      1.3  christos {
    905      1.3  christos 	const u8 *ie;
    906      1.3  christos 	u8 op_class, chan;
    907      1.3  christos 	int sec_chan = 0, vht = 0;
    908      1.3  christos 	enum phy_type phy_type;
    909      1.3  christos 	u32 info;
    910      1.3  christos 	struct ieee80211_ht_operation *ht_oper = NULL;
    911      1.3  christos 	struct ieee80211_vht_operation *vht_oper = NULL;
    912      1.3  christos 
    913      1.3  christos 	ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
    914      1.3  christos 	if (ie && ie[1] >= 2) {
    915      1.3  christos 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
    916      1.3  christos 
    917      1.3  christos 		if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
    918      1.3  christos 			sec_chan = 1;
    919      1.3  christos 		else if (ht_oper->ht_param &
    920      1.3  christos 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
    921      1.3  christos 			sec_chan = -1;
    922      1.3  christos 	}
    923      1.3  christos 
    924      1.3  christos 	ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
    925      1.3  christos 	if (ie && ie[1] >= 1) {
    926      1.3  christos 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
    927      1.3  christos 
    928  1.5.2.1    martin 		if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
    929  1.5.2.1    martin 		    vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
    930  1.5.2.1    martin 		    vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
    931      1.3  christos 			vht = vht_oper->vht_op_info_chwidth;
    932      1.3  christos 	}
    933      1.3  christos 
    934      1.3  christos 	if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
    935      1.3  christos 					  &chan) == NUM_HOSTAPD_MODES) {
    936      1.3  christos 		wpa_printf(MSG_DEBUG,
    937      1.3  christos 			   "WNM: Cannot determine operating class and channel");
    938      1.3  christos 		return -2;
    939      1.3  christos 	}
    940      1.3  christos 
    941      1.3  christos 	phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
    942      1.3  christos 					  (vht_oper != NULL));
    943      1.3  christos 	if (phy_type == PHY_TYPE_UNSPECIFIED) {
    944      1.3  christos 		wpa_printf(MSG_DEBUG,
    945      1.3  christos 			   "WNM: Cannot determine BSS phy type for Neighbor Report");
    946      1.3  christos 		return -2;
    947      1.3  christos 	}
    948      1.3  christos 
    949      1.3  christos 	info = wnm_get_bss_info(wpa_s, bss);
    950      1.3  christos 
    951      1.5  christos 	return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
    952      1.5  christos 			       pref);
    953      1.3  christos }
    954      1.3  christos 
    955      1.3  christos 
    956      1.5  christos static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
    957      1.3  christos {
    958      1.3  christos 	unsigned int i, pref = 255;
    959      1.3  christos 	struct os_reltime now;
    960      1.3  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    961      1.3  christos 
    962      1.3  christos 	if (!ssid)
    963      1.5  christos 		return;
    964      1.3  christos 
    965      1.3  christos 	/*
    966      1.3  christos 	 * TODO: Define when scan results are no longer valid for the candidate
    967      1.3  christos 	 * list.
    968      1.3  christos 	 */
    969      1.3  christos 	os_get_reltime(&now);
    970      1.3  christos 	if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
    971      1.5  christos 		return;
    972      1.3  christos 
    973      1.3  christos 	wpa_printf(MSG_DEBUG,
    974      1.3  christos 		   "WNM: Add candidate list to BSS Transition Management Response frame");
    975      1.3  christos 	for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
    976      1.3  christos 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
    977      1.3  christos 		int res;
    978      1.3  christos 
    979      1.5  christos 		if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
    980      1.5  christos 			res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
    981      1.3  christos 			if (res == -2)
    982      1.3  christos 				continue; /* could not build entry for BSS */
    983      1.3  christos 			if (res < 0)
    984      1.3  christos 				break; /* no more room for candidates */
    985      1.3  christos 			if (pref == 1)
    986      1.3  christos 				break;
    987      1.3  christos 		}
    988      1.3  christos 	}
    989      1.3  christos 
    990      1.5  christos 	wpa_hexdump_buf(MSG_DEBUG,
    991      1.5  christos 			"WNM: BSS Transition Management Response candidate list",
    992      1.5  christos 			*buf);
    993      1.5  christos }
    994      1.3  christos 
    995      1.3  christos 
    996      1.5  christos #define BTM_RESP_MIN_SIZE	5 + ETH_ALEN
    997      1.3  christos 
    998      1.2  christos static void wnm_send_bss_transition_mgmt_resp(
    999      1.2  christos 	struct wpa_supplicant *wpa_s, u8 dialog_token,
   1000      1.5  christos 	enum bss_trans_mgmt_status_code status,
   1001      1.5  christos 	enum mbo_transition_reject_reason reason,
   1002      1.5  christos 	u8 delay, const u8 *target_bssid)
   1003      1.1  christos {
   1004      1.5  christos 	struct wpabuf *buf;
   1005      1.2  christos 	int res;
   1006      1.1  christos 
   1007      1.5  christos 	wpa_printf(MSG_DEBUG,
   1008      1.5  christos 		   "WNM: Send BSS Transition Management Response to " MACSTR
   1009      1.5  christos 		   " dialog_token=%u status=%u reason=%u delay=%d",
   1010      1.5  christos 		   MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
   1011      1.2  christos 	if (!wpa_s->current_bss) {
   1012      1.2  christos 		wpa_printf(MSG_DEBUG,
   1013      1.2  christos 			   "WNM: Current BSS not known - drop response");
   1014      1.2  christos 		return;
   1015      1.2  christos 	}
   1016      1.1  christos 
   1017      1.5  christos 	buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
   1018      1.5  christos 	if (!buf) {
   1019      1.5  christos 		wpa_printf(MSG_DEBUG,
   1020      1.5  christos 			   "WNM: Failed to allocate memory for BTM response");
   1021      1.5  christos 		return;
   1022      1.5  christos 	}
   1023      1.5  christos 
   1024  1.5.2.1    martin 	wpa_s->bss_tm_status = status;
   1025  1.5.2.1    martin 	wpas_notify_bss_tm_status(wpa_s);
   1026  1.5.2.1    martin 
   1027      1.5  christos 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
   1028      1.5  christos 	wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
   1029      1.5  christos 	wpabuf_put_u8(buf, dialog_token);
   1030      1.5  christos 	wpabuf_put_u8(buf, status);
   1031      1.5  christos 	wpabuf_put_u8(buf, delay);
   1032      1.1  christos 	if (target_bssid) {
   1033      1.5  christos 		wpabuf_put_data(buf, target_bssid, ETH_ALEN);
   1034      1.2  christos 	} else if (status == WNM_BSS_TM_ACCEPT) {
   1035      1.2  christos 		/*
   1036      1.2  christos 		 * P802.11-REVmc clarifies that the Target BSSID field is always
   1037      1.2  christos 		 * present when status code is zero, so use a fake value here if
   1038      1.2  christos 		 * no BSSID is yet known.
   1039      1.2  christos 		 */
   1040      1.5  christos 		wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
   1041      1.1  christos 	}
   1042      1.1  christos 
   1043      1.3  christos 	if (status == WNM_BSS_TM_ACCEPT)
   1044      1.5  christos 		wnm_add_cand_list(wpa_s, &buf);
   1045      1.3  christos 
   1046      1.3  christos #ifdef CONFIG_MBO
   1047      1.5  christos 	if (status != WNM_BSS_TM_ACCEPT &&
   1048      1.5  christos 	    wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
   1049      1.5  christos 		u8 mbo[10];
   1050      1.5  christos 		size_t ret;
   1051      1.5  christos 
   1052      1.5  christos 		ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
   1053      1.5  christos 						   reason);
   1054      1.5  christos 		if (ret) {
   1055      1.5  christos 			if (wpabuf_resize(&buf, ret) < 0) {
   1056      1.5  christos 				wpabuf_free(buf);
   1057      1.5  christos 				wpa_printf(MSG_DEBUG,
   1058      1.5  christos 					   "WNM: Failed to allocate memory for MBO IE");
   1059      1.5  christos 				return;
   1060      1.5  christos 			}
   1061      1.5  christos 
   1062      1.5  christos 			wpabuf_put_data(buf, mbo, ret);
   1063      1.5  christos 		}
   1064      1.3  christos 	}
   1065      1.3  christos #endif /* CONFIG_MBO */
   1066      1.3  christos 
   1067      1.2  christos 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
   1068      1.2  christos 				  wpa_s->own_addr, wpa_s->bssid,
   1069      1.5  christos 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
   1070      1.2  christos 	if (res < 0) {
   1071      1.2  christos 		wpa_printf(MSG_DEBUG,
   1072      1.2  christos 			   "WNM: Failed to send BSS Transition Management Response");
   1073      1.2  christos 	}
   1074      1.5  christos 
   1075      1.5  christos 	wpabuf_free(buf);
   1076      1.2  christos }
   1077      1.2  christos 
   1078      1.2  christos 
   1079      1.3  christos static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
   1080      1.3  christos 			       struct wpa_bss *bss, struct wpa_ssid *ssid,
   1081      1.3  christos 			       int after_new_scan)
   1082      1.3  christos {
   1083      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
   1084      1.3  christos 		"WNM: Transition to BSS " MACSTR
   1085      1.3  christos 		" based on BSS Transition Management Request (old BSSID "
   1086      1.3  christos 		MACSTR " after_new_scan=%d)",
   1087      1.3  christos 		MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
   1088      1.3  christos 
   1089      1.3  christos 	/* Send the BSS Management Response - Accept */
   1090      1.3  christos 	if (wpa_s->wnm_reply) {
   1091      1.3  christos 		wpa_s->wnm_reply = 0;
   1092      1.3  christos 		wpa_printf(MSG_DEBUG,
   1093      1.3  christos 			   "WNM: Sending successful BSS Transition Management Response");
   1094      1.5  christos 		wnm_send_bss_transition_mgmt_resp(
   1095      1.5  christos 			wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
   1096      1.5  christos 			MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
   1097      1.5  christos 			bss->bssid);
   1098      1.3  christos 	}
   1099      1.3  christos 
   1100      1.3  christos 	if (bss == wpa_s->current_bss) {
   1101      1.3  christos 		wpa_printf(MSG_DEBUG,
   1102      1.3  christos 			   "WNM: Already associated with the preferred candidate");
   1103      1.3  christos 		wnm_deallocate_memory(wpa_s);
   1104      1.3  christos 		return;
   1105      1.3  christos 	}
   1106      1.3  christos 
   1107      1.3  christos 	wpa_s->reassociate = 1;
   1108      1.3  christos 	wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
   1109      1.3  christos 	wpa_supplicant_connect(wpa_s, bss, ssid);
   1110      1.3  christos 	wnm_deallocate_memory(wpa_s);
   1111      1.3  christos }
   1112      1.3  christos 
   1113      1.3  christos 
   1114      1.2  christos int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
   1115      1.2  christos {
   1116      1.2  christos 	struct wpa_bss *bss;
   1117      1.2  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   1118      1.2  christos 	enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
   1119      1.5  christos 	enum mbo_transition_reject_reason reason =
   1120      1.5  christos 		MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
   1121      1.2  christos 
   1122      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
   1123      1.2  christos 		return 0;
   1124      1.2  christos 
   1125      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
   1126      1.3  christos 		"WNM: Process scan results for BSS Transition Management");
   1127      1.2  christos 	if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
   1128      1.2  christos 			      &wpa_s->scan_trigger_time)) {
   1129      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
   1130      1.2  christos 		wnm_deallocate_memory(wpa_s);
   1131      1.2  christos 		return 0;
   1132      1.2  christos 	}
   1133      1.2  christos 
   1134      1.2  christos 	if (!wpa_s->current_bss ||
   1135      1.2  christos 	    os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
   1136      1.2  christos 		      ETH_ALEN) != 0) {
   1137      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
   1138      1.2  christos 		return 0;
   1139      1.2  christos 	}
   1140      1.2  christos 
   1141      1.2  christos 	/* Compare the Neighbor Report and scan results */
   1142      1.5  christos 	bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
   1143      1.2  christos 	if (!bss) {
   1144      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
   1145      1.2  christos 		status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
   1146      1.2  christos 		goto send_bss_resp_fail;
   1147      1.2  christos 	}
   1148      1.2  christos 
   1149      1.2  christos 	/* Associate to the network */
   1150      1.3  christos 	wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
   1151      1.2  christos 	return 1;
   1152      1.2  christos 
   1153      1.2  christos send_bss_resp_fail:
   1154      1.2  christos 	if (!reply_on_fail)
   1155      1.2  christos 		return 0;
   1156      1.2  christos 
   1157      1.2  christos 	/* Send reject response for all the failures */
   1158      1.2  christos 
   1159      1.2  christos 	if (wpa_s->wnm_reply) {
   1160      1.2  christos 		wpa_s->wnm_reply = 0;
   1161      1.2  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
   1162      1.2  christos 						  wpa_s->wnm_dialog_token,
   1163      1.5  christos 						  status, reason, 0, NULL);
   1164      1.2  christos 	}
   1165      1.2  christos 	wnm_deallocate_memory(wpa_s);
   1166      1.2  christos 
   1167      1.2  christos 	return 0;
   1168      1.2  christos }
   1169      1.2  christos 
   1170      1.2  christos 
   1171      1.2  christos static int cand_pref_compar(const void *a, const void *b)
   1172      1.2  christos {
   1173      1.2  christos 	const struct neighbor_report *aa = a;
   1174      1.2  christos 	const struct neighbor_report *bb = b;
   1175      1.2  christos 
   1176      1.2  christos 	if (!aa->preference_present && !bb->preference_present)
   1177      1.2  christos 		return 0;
   1178      1.2  christos 	if (!aa->preference_present)
   1179      1.2  christos 		return 1;
   1180      1.2  christos 	if (!bb->preference_present)
   1181      1.2  christos 		return -1;
   1182      1.2  christos 	if (bb->preference > aa->preference)
   1183      1.2  christos 		return 1;
   1184      1.2  christos 	if (bb->preference < aa->preference)
   1185      1.2  christos 		return -1;
   1186      1.2  christos 	return 0;
   1187      1.2  christos }
   1188      1.2  christos 
   1189      1.2  christos 
   1190      1.2  christos static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
   1191      1.2  christos {
   1192      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
   1193      1.2  christos 		return;
   1194      1.2  christos 	qsort(wpa_s->wnm_neighbor_report_elements,
   1195      1.2  christos 	      wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
   1196      1.2  christos 	      cand_pref_compar);
   1197      1.2  christos }
   1198      1.2  christos 
   1199      1.2  christos 
   1200      1.2  christos static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
   1201      1.2  christos {
   1202      1.2  christos 	unsigned int i;
   1203      1.2  christos 
   1204      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
   1205      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
   1206      1.2  christos 		return;
   1207      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
   1208      1.2  christos 		struct neighbor_report *nei;
   1209      1.2  christos 
   1210      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
   1211      1.2  christos 		wpa_printf(MSG_DEBUG, "%u: " MACSTR
   1212      1.2  christos 			   " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
   1213      1.2  christos 			   i, MAC2STR(nei->bssid), nei->bssid_info,
   1214      1.2  christos 			   nei->regulatory_class,
   1215      1.2  christos 			   nei->channel_number, nei->phy_type,
   1216      1.2  christos 			   nei->preference_present ? nei->preference : -1,
   1217      1.2  christos 			   nei->freq);
   1218      1.2  christos 	}
   1219      1.2  christos }
   1220      1.2  christos 
   1221      1.2  christos 
   1222      1.2  christos static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
   1223      1.2  christos {
   1224      1.2  christos 	unsigned int i;
   1225      1.2  christos 
   1226      1.2  christos 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
   1227      1.2  christos 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
   1228      1.2  christos 		int j;
   1229      1.2  christos 
   1230      1.2  christos 		for (j = 0; j < mode->num_channels; j++) {
   1231      1.2  christos 			struct hostapd_channel_data *chan;
   1232      1.2  christos 
   1233      1.2  christos 			chan = &mode->channels[j];
   1234      1.2  christos 			if (chan->freq == freq &&
   1235      1.2  christos 			    !(chan->flag & HOSTAPD_CHAN_DISABLED))
   1236      1.2  christos 				return 1;
   1237      1.2  christos 		}
   1238      1.2  christos 	}
   1239      1.2  christos 
   1240      1.2  christos 	return 0;
   1241      1.2  christos }
   1242      1.2  christos 
   1243      1.2  christos 
   1244      1.2  christos static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
   1245      1.2  christos {
   1246      1.2  christos 	int *freqs;
   1247      1.2  christos 	int num_freqs = 0;
   1248      1.2  christos 	unsigned int i;
   1249      1.2  christos 
   1250      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
   1251      1.2  christos 		return;
   1252      1.2  christos 
   1253      1.2  christos 	if (wpa_s->hw.modes == NULL)
   1254      1.2  christos 		return;
   1255      1.2  christos 
   1256      1.2  christos 	os_free(wpa_s->next_scan_freqs);
   1257      1.2  christos 	wpa_s->next_scan_freqs = NULL;
   1258      1.2  christos 
   1259      1.2  christos 	freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
   1260      1.2  christos 	if (freqs == NULL)
   1261      1.2  christos 		return;
   1262      1.2  christos 
   1263      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
   1264      1.2  christos 		struct neighbor_report *nei;
   1265      1.2  christos 
   1266      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
   1267      1.2  christos 		if (nei->freq <= 0) {
   1268      1.2  christos 			wpa_printf(MSG_DEBUG,
   1269      1.2  christos 				   "WNM: Unknown neighbor operating frequency for "
   1270      1.2  christos 				   MACSTR " - scan all channels",
   1271      1.2  christos 				   MAC2STR(nei->bssid));
   1272      1.2  christos 			os_free(freqs);
   1273      1.2  christos 			return;
   1274      1.2  christos 		}
   1275      1.2  christos 		if (chan_supported(wpa_s, nei->freq))
   1276      1.2  christos 			add_freq(freqs, &num_freqs, nei->freq);
   1277      1.2  christos 	}
   1278      1.2  christos 
   1279      1.2  christos 	if (num_freqs == 0) {
   1280      1.2  christos 		os_free(freqs);
   1281      1.2  christos 		return;
   1282      1.2  christos 	}
   1283      1.2  christos 
   1284      1.2  christos 	wpa_printf(MSG_DEBUG,
   1285      1.2  christos 		   "WNM: Scan %d frequencies based on transition candidate list",
   1286      1.2  christos 		   num_freqs);
   1287      1.2  christos 	wpa_s->next_scan_freqs = freqs;
   1288      1.1  christos }
   1289      1.1  christos 
   1290      1.1  christos 
   1291      1.3  christos static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
   1292      1.3  christos {
   1293      1.3  christos 	struct wpa_scan_results *scan_res;
   1294      1.3  christos 	struct wpa_bss *bss;
   1295      1.3  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   1296      1.3  christos 	u8 i, found = 0;
   1297      1.3  christos 	size_t j;
   1298      1.3  christos 
   1299      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
   1300      1.3  christos 		"WNM: Fetch current scan results from the driver for checking transition candidates");
   1301      1.3  christos 	scan_res = wpa_drv_get_scan_results2(wpa_s);
   1302      1.3  christos 	if (!scan_res) {
   1303      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
   1304      1.3  christos 		return 0;
   1305      1.3  christos 	}
   1306      1.3  christos 
   1307      1.3  christos 	if (scan_res->fetch_time.sec == 0)
   1308      1.3  christos 		os_get_reltime(&scan_res->fetch_time);
   1309      1.3  christos 
   1310      1.3  christos 	filter_scan_res(wpa_s, scan_res);
   1311      1.3  christos 
   1312      1.3  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
   1313      1.3  christos 		struct neighbor_report *nei;
   1314      1.3  christos 
   1315      1.3  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
   1316      1.3  christos 		if (nei->preference_present && nei->preference == 0)
   1317      1.3  christos 			continue;
   1318      1.3  christos 
   1319      1.3  christos 		for (j = 0; j < scan_res->num; j++) {
   1320      1.3  christos 			struct wpa_scan_res *res;
   1321      1.3  christos 			const u8 *ssid_ie;
   1322      1.3  christos 
   1323      1.3  christos 			res = scan_res->res[j];
   1324      1.3  christos 			if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
   1325      1.3  christos 			    res->age > WNM_SCAN_RESULT_AGE * 1000)
   1326      1.3  christos 				continue;
   1327      1.3  christos 			bss = wpa_s->current_bss;
   1328      1.3  christos 			ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
   1329      1.3  christos 			if (bss && ssid_ie &&
   1330      1.3  christos 			    (bss->ssid_len != ssid_ie[1] ||
   1331      1.3  christos 			     os_memcmp(bss->ssid, ssid_ie + 2,
   1332      1.3  christos 				       bss->ssid_len) != 0))
   1333      1.3  christos 				continue;
   1334      1.3  christos 
   1335      1.3  christos 			/* Potential candidate found */
   1336      1.3  christos 			found = 1;
   1337      1.3  christos 			scan_snr(res);
   1338      1.3  christos 			scan_est_throughput(wpa_s, res);
   1339      1.3  christos 			wpa_bss_update_scan_res(wpa_s, res,
   1340      1.3  christos 						&scan_res->fetch_time);
   1341      1.3  christos 		}
   1342      1.3  christos 	}
   1343      1.3  christos 
   1344      1.3  christos 	wpa_scan_results_free(scan_res);
   1345      1.3  christos 	if (!found) {
   1346      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG,
   1347      1.3  christos 			"WNM: No transition candidate matches existing scan results");
   1348      1.3  christos 		return 0;
   1349      1.3  christos 	}
   1350      1.3  christos 
   1351      1.5  christos 	bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
   1352      1.3  christos 	if (!bss) {
   1353      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG,
   1354      1.3  christos 			"WNM: Comparison of scan results against transition candidates did not find matches");
   1355      1.3  christos 		return 0;
   1356      1.3  christos 	}
   1357      1.3  christos 
   1358      1.3  christos 	/* Associate to the network */
   1359      1.3  christos 	wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
   1360      1.3  christos 	return 1;
   1361      1.3  christos }
   1362      1.3  christos 
   1363      1.3  christos 
   1364      1.1  christos static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
   1365      1.1  christos 					     const u8 *pos, const u8 *end,
   1366      1.1  christos 					     int reply)
   1367      1.1  christos {
   1368      1.2  christos 	unsigned int beacon_int;
   1369      1.2  christos 	u8 valid_int;
   1370      1.3  christos #ifdef CONFIG_MBO
   1371      1.3  christos 	const u8 *vendor;
   1372      1.3  christos #endif /* CONFIG_MBO */
   1373      1.1  christos 
   1374  1.5.2.1    martin 	if (wpa_s->conf->disable_btm)
   1375  1.5.2.1    martin 		return;
   1376  1.5.2.1    martin 
   1377      1.3  christos 	if (end - pos < 5)
   1378      1.1  christos 		return;
   1379      1.1  christos 
   1380      1.5  christos #ifdef CONFIG_MBO
   1381      1.5  christos 	wpa_s->wnm_mbo_trans_reason_present = 0;
   1382      1.5  christos 	wpa_s->wnm_mbo_transition_reason = 0;
   1383      1.5  christos #endif /* CONFIG_MBO */
   1384      1.5  christos 
   1385      1.2  christos 	if (wpa_s->current_bss)
   1386      1.2  christos 		beacon_int = wpa_s->current_bss->beacon_int;
   1387      1.2  christos 	else
   1388      1.2  christos 		beacon_int = 100; /* best guess */
   1389      1.2  christos 
   1390      1.2  christos 	wpa_s->wnm_dialog_token = pos[0];
   1391      1.2  christos 	wpa_s->wnm_mode = pos[1];
   1392      1.2  christos 	wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
   1393      1.2  christos 	valid_int = pos[4];
   1394      1.2  christos 	wpa_s->wnm_reply = reply;
   1395      1.1  christos 
   1396      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
   1397      1.1  christos 		   "dialog_token=%u request_mode=0x%x "
   1398      1.1  christos 		   "disassoc_timer=%u validity_interval=%u",
   1399      1.2  christos 		   wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
   1400      1.2  christos 		   wpa_s->wnm_dissoc_timer, valid_int);
   1401      1.2  christos 
   1402      1.3  christos #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
   1403      1.3  christos 	if (wpa_s->reject_btm_req_reason) {
   1404      1.3  christos 		wpa_printf(MSG_INFO,
   1405      1.3  christos 			   "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
   1406      1.3  christos 			   wpa_s->reject_btm_req_reason);
   1407      1.5  christos 		wnm_send_bss_transition_mgmt_resp(
   1408      1.5  christos 			wpa_s, wpa_s->wnm_dialog_token,
   1409      1.5  christos 			wpa_s->reject_btm_req_reason,
   1410      1.5  christos 			MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
   1411      1.3  christos 		return;
   1412      1.3  christos 	}
   1413      1.3  christos #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
   1414      1.3  christos 
   1415      1.1  christos 	pos += 5;
   1416      1.2  christos 
   1417      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
   1418      1.3  christos 		if (end - pos < 12) {
   1419      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
   1420      1.2  christos 			return;
   1421      1.2  christos 		}
   1422      1.2  christos 		os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
   1423      1.1  christos 		pos += 12; /* BSS Termination Duration */
   1424      1.2  christos 	}
   1425      1.2  christos 
   1426      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
   1427      1.1  christos 		char url[256];
   1428      1.2  christos 
   1429      1.3  christos 		if (end - pos < 1 || 1 + pos[0] > end - pos) {
   1430      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
   1431      1.1  christos 				   "Management Request (URL)");
   1432      1.1  christos 			return;
   1433      1.1  christos 		}
   1434      1.1  christos 		os_memcpy(url, pos + 1, pos[0]);
   1435      1.1  christos 		url[pos[0]] = '\0';
   1436      1.2  christos 		pos += 1 + pos[0];
   1437      1.2  christos 
   1438      1.2  christos 		wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
   1439      1.2  christos 			wpa_sm_pmf_enabled(wpa_s->wpa),
   1440      1.2  christos 			wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
   1441      1.1  christos 	}
   1442      1.1  christos 
   1443      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
   1444      1.1  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
   1445      1.2  christos 			"Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
   1446      1.2  christos 		if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
   1447      1.1  christos 			/* TODO: mark current BSS less preferred for
   1448      1.1  christos 			 * selection */
   1449      1.1  christos 			wpa_printf(MSG_DEBUG, "Trying to find another BSS");
   1450      1.1  christos 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   1451      1.1  christos 		}
   1452      1.1  christos 	}
   1453      1.1  christos 
   1454      1.3  christos #ifdef CONFIG_MBO
   1455      1.3  christos 	vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
   1456      1.3  christos 	if (vendor)
   1457      1.3  christos 		wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
   1458      1.3  christos #endif /* CONFIG_MBO */
   1459      1.3  christos 
   1460      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
   1461      1.2  christos 		unsigned int valid_ms;
   1462      1.2  christos 
   1463      1.2  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
   1464      1.2  christos 		wnm_deallocate_memory(wpa_s);
   1465      1.2  christos 		wpa_s->wnm_neighbor_report_elements = os_calloc(
   1466      1.2  christos 			WNM_MAX_NEIGHBOR_REPORT,
   1467      1.2  christos 			sizeof(struct neighbor_report));
   1468      1.2  christos 		if (wpa_s->wnm_neighbor_report_elements == NULL)
   1469      1.2  christos 			return;
   1470      1.2  christos 
   1471      1.3  christos 		while (end - pos >= 2 &&
   1472      1.2  christos 		       wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
   1473      1.2  christos 		{
   1474      1.2  christos 			u8 tag = *pos++;
   1475      1.2  christos 			u8 len = *pos++;
   1476      1.2  christos 
   1477      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
   1478      1.2  christos 				   tag);
   1479      1.3  christos 			if (len > end - pos) {
   1480      1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: Truncated request");
   1481      1.2  christos 				return;
   1482      1.2  christos 			}
   1483      1.2  christos 			if (tag == WLAN_EID_NEIGHBOR_REPORT) {
   1484      1.2  christos 				struct neighbor_report *rep;
   1485      1.2  christos 				rep = &wpa_s->wnm_neighbor_report_elements[
   1486      1.2  christos 					wpa_s->wnm_num_neighbor_report];
   1487      1.2  christos 				wnm_parse_neighbor_report(wpa_s, pos, len, rep);
   1488      1.3  christos 				wpa_s->wnm_num_neighbor_report++;
   1489      1.5  christos #ifdef CONFIG_MBO
   1490      1.5  christos 				if (wpa_s->wnm_mbo_trans_reason_present &&
   1491      1.5  christos 				    wpa_s->wnm_num_neighbor_report == 1) {
   1492      1.5  christos 					rep->is_first = 1;
   1493      1.5  christos 					wpa_printf(MSG_DEBUG,
   1494      1.5  christos 						   "WNM: First transition candidate is "
   1495      1.5  christos 						   MACSTR, MAC2STR(rep->bssid));
   1496      1.5  christos 				}
   1497      1.5  christos #endif /* CONFIG_MBO */
   1498      1.2  christos 			}
   1499      1.2  christos 
   1500      1.2  christos 			pos += len;
   1501      1.2  christos 		}
   1502      1.3  christos 
   1503      1.3  christos 		if (!wpa_s->wnm_num_neighbor_report) {
   1504      1.3  christos 			wpa_printf(MSG_DEBUG,
   1505      1.3  christos 				   "WNM: Candidate list included bit is set, but no candidates found");
   1506      1.3  christos 			wnm_send_bss_transition_mgmt_resp(
   1507      1.3  christos 				wpa_s, wpa_s->wnm_dialog_token,
   1508      1.3  christos 				WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
   1509      1.5  christos 				MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
   1510      1.5  christos 				NULL);
   1511      1.3  christos 			return;
   1512      1.3  christos 		}
   1513      1.3  christos 
   1514      1.2  christos 		wnm_sort_cand_list(wpa_s);
   1515      1.2  christos 		wnm_dump_cand_list(wpa_s);
   1516      1.2  christos 		valid_ms = valid_int * beacon_int * 128 / 125;
   1517      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
   1518      1.2  christos 			   valid_ms);
   1519      1.2  christos 		os_get_reltime(&wpa_s->wnm_cand_valid_until);
   1520      1.2  christos 		wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
   1521      1.2  christos 		wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
   1522      1.2  christos 		wpa_s->wnm_cand_valid_until.sec +=
   1523      1.2  christos 			wpa_s->wnm_cand_valid_until.usec / 1000000;
   1524      1.2  christos 		wpa_s->wnm_cand_valid_until.usec %= 1000000;
   1525      1.2  christos 		os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
   1526      1.2  christos 
   1527      1.3  christos 		/*
   1528      1.3  christos 		 * Fetch the latest scan results from the kernel and check for
   1529      1.3  christos 		 * candidates based on those results first. This can help in
   1530      1.3  christos 		 * finding more up-to-date information should the driver has
   1531      1.3  christos 		 * done some internal scanning operations after the last scan
   1532      1.3  christos 		 * result update in wpa_supplicant.
   1533      1.3  christos 		 */
   1534      1.3  christos 		if (wnm_fetch_scan_results(wpa_s) > 0)
   1535      1.3  christos 			return;
   1536      1.3  christos 
   1537      1.3  christos 		/*
   1538      1.3  christos 		 * Try to use previously received scan results, if they are
   1539      1.3  christos 		 * recent enough to use for a connection.
   1540      1.3  christos 		 */
   1541      1.2  christos 		if (wpa_s->last_scan_res_used > 0) {
   1542      1.2  christos 			struct os_reltime now;
   1543      1.2  christos 
   1544      1.2  christos 			os_get_reltime(&now);
   1545      1.2  christos 			if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
   1546      1.2  christos 				wpa_printf(MSG_DEBUG,
   1547      1.2  christos 					   "WNM: Try to use recent scan results");
   1548      1.2  christos 				if (wnm_scan_process(wpa_s, 0) > 0)
   1549      1.2  christos 					return;
   1550      1.2  christos 				wpa_printf(MSG_DEBUG,
   1551      1.2  christos 					   "WNM: No match in previous scan results - try a new scan");
   1552      1.2  christos 			}
   1553      1.2  christos 		}
   1554      1.2  christos 
   1555      1.2  christos 		wnm_set_scan_freqs(wpa_s);
   1556      1.3  christos 		if (wpa_s->wnm_num_neighbor_report == 1) {
   1557      1.3  christos 			os_memcpy(wpa_s->next_scan_bssid,
   1558      1.3  christos 				  wpa_s->wnm_neighbor_report_elements[0].bssid,
   1559      1.3  christos 				  ETH_ALEN);
   1560      1.3  christos 			wpa_printf(MSG_DEBUG,
   1561      1.3  christos 				   "WNM: Scan only for a specific BSSID since there is only a single candidate "
   1562      1.3  christos 				   MACSTR, MAC2STR(wpa_s->next_scan_bssid));
   1563      1.3  christos 		}
   1564      1.2  christos 		wpa_supplicant_req_scan(wpa_s, 0, 0);
   1565      1.2  christos 	} else if (reply) {
   1566      1.2  christos 		enum bss_trans_mgmt_status_code status;
   1567      1.2  christos 		if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
   1568      1.2  christos 			status = WNM_BSS_TM_ACCEPT;
   1569      1.2  christos 		else {
   1570      1.2  christos 			wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
   1571      1.2  christos 			status = WNM_BSS_TM_REJECT_UNSPECIFIED;
   1572      1.2  christos 		}
   1573      1.5  christos 		wnm_send_bss_transition_mgmt_resp(
   1574      1.5  christos 			wpa_s, wpa_s->wnm_dialog_token, status,
   1575      1.5  christos 			MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
   1576      1.2  christos 	}
   1577      1.2  christos }
   1578      1.2  christos 
   1579      1.2  christos 
   1580      1.5  christos #define BTM_QUERY_MIN_SIZE	4
   1581      1.5  christos 
   1582      1.2  christos int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
   1583      1.5  christos 				       u8 query_reason,
   1584      1.5  christos 				       const char *btm_candidates,
   1585      1.5  christos 				       int cand_list)
   1586      1.2  christos {
   1587      1.5  christos 	struct wpabuf *buf;
   1588      1.2  christos 	int ret;
   1589      1.2  christos 
   1590      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
   1591      1.3  christos 		   MACSTR " query_reason=%u%s",
   1592      1.3  christos 		   MAC2STR(wpa_s->bssid), query_reason,
   1593      1.3  christos 		   cand_list ? " candidate list" : "");
   1594      1.2  christos 
   1595      1.5  christos 	buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
   1596      1.5  christos 	if (!buf)
   1597      1.5  christos 		return -1;
   1598      1.5  christos 
   1599      1.5  christos 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
   1600      1.5  christos 	wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
   1601      1.5  christos 	wpabuf_put_u8(buf, 1);
   1602      1.5  christos 	wpabuf_put_u8(buf, query_reason);
   1603      1.2  christos 
   1604      1.3  christos 	if (cand_list)
   1605      1.5  christos 		wnm_add_cand_list(wpa_s, &buf);
   1606      1.5  christos 
   1607      1.5  christos 	if (btm_candidates) {
   1608      1.5  christos 		const size_t max_len = 1000;
   1609      1.5  christos 
   1610      1.5  christos 		ret = wpabuf_resize(&buf, max_len);
   1611      1.5  christos 		if (ret < 0) {
   1612      1.5  christos 			wpabuf_free(buf);
   1613      1.5  christos 			return ret;
   1614      1.5  christos 		}
   1615      1.5  christos 
   1616      1.5  christos 		ret = ieee802_11_parse_candidate_list(btm_candidates,
   1617      1.5  christos 						      wpabuf_put(buf, 0),
   1618      1.5  christos 						      max_len);
   1619      1.5  christos 		if (ret < 0) {
   1620      1.5  christos 			wpabuf_free(buf);
   1621      1.5  christos 			return ret;
   1622      1.5  christos 		}
   1623      1.3  christos 
   1624      1.5  christos 		wpabuf_put(buf, ret);
   1625      1.5  christos 	}
   1626      1.2  christos 
   1627      1.2  christos 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
   1628      1.2  christos 				  wpa_s->own_addr, wpa_s->bssid,
   1629      1.5  christos 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
   1630      1.2  christos 
   1631      1.5  christos 	wpabuf_free(buf);
   1632      1.2  christos 	return ret;
   1633      1.2  christos }
   1634      1.2  christos 
   1635      1.2  christos 
   1636      1.2  christos static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
   1637      1.2  christos 					    const u8 *sa, const u8 *data,
   1638      1.2  christos 					    int len)
   1639      1.2  christos {
   1640      1.2  christos 	const u8 *pos, *end, *next;
   1641      1.2  christos 	u8 ie, ie_len;
   1642      1.2  christos 
   1643      1.2  christos 	pos = data;
   1644      1.2  christos 	end = data + len;
   1645      1.2  christos 
   1646      1.3  christos 	while (end - pos > 1) {
   1647      1.2  christos 		ie = *pos++;
   1648      1.2  christos 		ie_len = *pos++;
   1649      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
   1650      1.2  christos 			   ie, ie_len);
   1651      1.2  christos 		if (ie_len > end - pos) {
   1652      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
   1653      1.2  christos 				   "subelement");
   1654      1.2  christos 			break;
   1655      1.2  christos 		}
   1656      1.2  christos 		next = pos + ie_len;
   1657      1.2  christos 		if (ie_len < 4) {
   1658      1.2  christos 			pos = next;
   1659      1.2  christos 			continue;
   1660      1.2  christos 		}
   1661      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
   1662      1.2  christos 			   WPA_GET_BE24(pos), pos[3]);
   1663      1.2  christos 
   1664      1.2  christos #ifdef CONFIG_HS20
   1665      1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
   1666      1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
   1667      1.2  christos 		    pos[3] == HS20_WNM_SUB_REM_NEEDED) {
   1668      1.2  christos 			/* Subscription Remediation subelement */
   1669      1.2  christos 			const u8 *ie_end;
   1670      1.2  christos 			u8 url_len;
   1671      1.2  christos 			char *url;
   1672      1.2  christos 			u8 osu_method;
   1673      1.2  christos 
   1674      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
   1675      1.2  christos 				   "subelement");
   1676      1.2  christos 			ie_end = pos + ie_len;
   1677      1.2  christos 			pos += 4;
   1678      1.2  christos 			url_len = *pos++;
   1679      1.2  christos 			if (url_len == 0) {
   1680      1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
   1681      1.2  christos 				url = NULL;
   1682      1.2  christos 				osu_method = 1;
   1683      1.2  christos 			} else {
   1684      1.3  christos 				if (url_len + 1 > ie_end - pos) {
   1685      1.2  christos 					wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
   1686      1.2  christos 						   url_len,
   1687      1.2  christos 						   (int) (ie_end - pos));
   1688      1.2  christos 					break;
   1689      1.2  christos 				}
   1690      1.2  christos 				url = os_malloc(url_len + 1);
   1691      1.2  christos 				if (url == NULL)
   1692      1.2  christos 					break;
   1693      1.2  christos 				os_memcpy(url, pos, url_len);
   1694      1.2  christos 				url[url_len] = '\0';
   1695      1.2  christos 				osu_method = pos[url_len];
   1696      1.2  christos 			}
   1697      1.2  christos 			hs20_rx_subscription_remediation(wpa_s, url,
   1698      1.2  christos 							 osu_method);
   1699      1.2  christos 			os_free(url);
   1700      1.2  christos 			pos = next;
   1701      1.2  christos 			continue;
   1702      1.2  christos 		}
   1703      1.2  christos 
   1704      1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
   1705      1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
   1706      1.2  christos 		    pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
   1707      1.2  christos 			const u8 *ie_end;
   1708      1.2  christos 			u8 url_len;
   1709      1.2  christos 			char *url;
   1710      1.2  christos 			u8 code;
   1711      1.2  christos 			u16 reauth_delay;
   1712      1.2  christos 
   1713      1.2  christos 			ie_end = pos + ie_len;
   1714      1.2  christos 			pos += 4;
   1715      1.2  christos 			code = *pos++;
   1716      1.2  christos 			reauth_delay = WPA_GET_LE16(pos);
   1717      1.2  christos 			pos += 2;
   1718      1.2  christos 			url_len = *pos++;
   1719      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
   1720      1.2  christos 				   "Imminent - Reason Code %u   "
   1721      1.2  christos 				   "Re-Auth Delay %u  URL Length %u",
   1722      1.2  christos 				   code, reauth_delay, url_len);
   1723      1.3  christos 			if (url_len > ie_end - pos)
   1724      1.2  christos 				break;
   1725      1.2  christos 			url = os_malloc(url_len + 1);
   1726      1.2  christos 			if (url == NULL)
   1727      1.2  christos 				break;
   1728      1.2  christos 			os_memcpy(url, pos, url_len);
   1729      1.2  christos 			url[url_len] = '\0';
   1730      1.2  christos 			hs20_rx_deauth_imminent_notice(wpa_s, code,
   1731      1.2  christos 						       reauth_delay, url);
   1732      1.2  christos 			os_free(url);
   1733      1.2  christos 			pos = next;
   1734      1.2  christos 			continue;
   1735      1.2  christos 		}
   1736      1.5  christos 
   1737      1.5  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
   1738      1.5  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
   1739      1.5  christos 		    pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
   1740      1.5  christos 			const u8 *ie_end;
   1741      1.5  christos 			u8 url_len;
   1742      1.5  christos 			char *url;
   1743      1.5  christos 
   1744      1.5  christos 			ie_end = pos + ie_len;
   1745      1.5  christos 			pos += 4;
   1746      1.5  christos 			url_len = *pos++;
   1747      1.5  christos 			wpa_printf(MSG_DEBUG,
   1748      1.5  christos 				   "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
   1749      1.5  christos 				   url_len);
   1750      1.5  christos 			if (url_len > ie_end - pos)
   1751      1.5  christos 				break;
   1752      1.5  christos 			url = os_malloc(url_len + 1);
   1753      1.5  christos 			if (!url)
   1754      1.5  christos 				break;
   1755      1.5  christos 			os_memcpy(url, pos, url_len);
   1756      1.5  christos 			url[url_len] = '\0';
   1757      1.5  christos 			hs20_rx_t_c_acceptance(wpa_s, url);
   1758      1.5  christos 			os_free(url);
   1759      1.5  christos 			pos = next;
   1760      1.5  christos 			continue;
   1761      1.5  christos 		}
   1762      1.2  christos #endif /* CONFIG_HS20 */
   1763      1.2  christos 
   1764      1.2  christos 		pos = next;
   1765      1.2  christos 	}
   1766      1.2  christos }
   1767      1.2  christos 
   1768      1.2  christos 
   1769      1.2  christos static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
   1770      1.2  christos 					const u8 *sa, const u8 *frm, int len)
   1771      1.2  christos {
   1772      1.2  christos 	const u8 *pos, *end;
   1773      1.2  christos 	u8 dialog_token, type;
   1774      1.2  christos 
   1775      1.2  christos 	/* Dialog Token [1] | Type [1] | Subelements */
   1776      1.2  christos 
   1777      1.2  christos 	if (len < 2 || sa == NULL)
   1778      1.2  christos 		return;
   1779      1.2  christos 	end = frm + len;
   1780      1.2  christos 	pos = frm;
   1781      1.2  christos 	dialog_token = *pos++;
   1782      1.2  christos 	type = *pos++;
   1783      1.2  christos 
   1784      1.2  christos 	wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
   1785      1.2  christos 		"(dialog_token %u type %u sa " MACSTR ")",
   1786      1.2  christos 		dialog_token, type, MAC2STR(sa));
   1787      1.2  christos 	wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
   1788      1.2  christos 		    pos, end - pos);
   1789      1.2  christos 
   1790      1.2  christos 	if (wpa_s->wpa_state != WPA_COMPLETED ||
   1791      1.2  christos 	    os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
   1792      1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
   1793      1.2  christos 			"from our AP - ignore it");
   1794      1.2  christos 		return;
   1795      1.2  christos 	}
   1796      1.2  christos 
   1797      1.2  christos 	switch (type) {
   1798      1.2  christos 	case 1:
   1799      1.2  christos 		ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
   1800      1.2  christos 		break;
   1801      1.2  christos 	default:
   1802      1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
   1803      1.2  christos 			"WNM-Notification type %u", type);
   1804      1.2  christos 		break;
   1805      1.1  christos 	}
   1806      1.1  christos }
   1807      1.1  christos 
   1808      1.1  christos 
   1809      1.5  christos static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
   1810      1.5  christos 					     const u8 *sa, const u8 *frm,
   1811      1.5  christos 					     int len)
   1812      1.5  christos {
   1813      1.5  christos 	u8 dialog_token, req_info, auto_report, timeout;
   1814      1.5  christos 
   1815      1.5  christos 	if (!wpa_s->conf->coloc_intf_reporting)
   1816      1.5  christos 		return;
   1817      1.5  christos 
   1818      1.5  christos 	/* Dialog Token [1] | Request Info [1] */
   1819      1.5  christos 
   1820      1.5  christos 	if (len < 2)
   1821      1.5  christos 		return;
   1822      1.5  christos 	dialog_token = frm[0];
   1823      1.5  christos 	req_info = frm[1];
   1824      1.5  christos 	auto_report = req_info & 0x03;
   1825      1.5  christos 	timeout = req_info >> 2;
   1826      1.5  christos 
   1827      1.5  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
   1828      1.5  christos 		"WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
   1829      1.5  christos 		dialog_token, auto_report, timeout, MAC2STR(sa));
   1830      1.5  christos 
   1831      1.5  christos 	if (dialog_token == 0)
   1832      1.5  christos 		return; /* only nonzero values are used for request */
   1833      1.5  christos 
   1834      1.5  christos 	if (wpa_s->wpa_state != WPA_COMPLETED ||
   1835      1.5  christos 	    os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
   1836      1.5  christos 		wpa_dbg(wpa_s, MSG_DEBUG,
   1837      1.5  christos 			"WNM: Collocated Interference Request frame not from current AP - ignore it");
   1838      1.5  christos 		return;
   1839      1.5  christos 	}
   1840      1.5  christos 
   1841      1.5  christos 	wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
   1842      1.5  christos 		dialog_token, auto_report, timeout);
   1843      1.5  christos 	wpa_s->coloc_intf_dialog_token = dialog_token;
   1844      1.5  christos 	wpa_s->coloc_intf_auto_report = auto_report;
   1845      1.5  christos 	wpa_s->coloc_intf_timeout = timeout;
   1846      1.5  christos }
   1847      1.5  christos 
   1848      1.5  christos 
   1849      1.1  christos void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
   1850      1.2  christos 			      const struct ieee80211_mgmt *mgmt, size_t len)
   1851      1.1  christos {
   1852      1.1  christos 	const u8 *pos, *end;
   1853      1.1  christos 	u8 act;
   1854      1.1  christos 
   1855      1.2  christos 	if (len < IEEE80211_HDRLEN + 2)
   1856      1.1  christos 		return;
   1857      1.1  christos 
   1858      1.2  christos 	pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
   1859      1.1  christos 	act = *pos++;
   1860      1.2  christos 	end = ((const u8 *) mgmt) + len;
   1861      1.1  christos 
   1862      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
   1863      1.2  christos 		   act, MAC2STR(mgmt->sa));
   1864      1.1  christos 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
   1865      1.2  christos 	    os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
   1866      1.1  christos 		wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
   1867      1.1  christos 			   "frame");
   1868      1.1  christos 		return;
   1869      1.1  christos 	}
   1870      1.1  christos 
   1871      1.1  christos 	switch (act) {
   1872      1.1  christos 	case WNM_BSS_TRANS_MGMT_REQ:
   1873      1.1  christos 		ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
   1874      1.2  christos 						 !(mgmt->da[0] & 0x01));
   1875      1.1  christos 		break;
   1876      1.1  christos 	case WNM_SLEEP_MODE_RESP:
   1877      1.2  christos 		ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
   1878      1.2  christos 		break;
   1879      1.2  christos 	case WNM_NOTIFICATION_REQ:
   1880      1.2  christos 		ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
   1881      1.1  christos 		break;
   1882      1.5  christos 	case WNM_COLLOCATED_INTERFERENCE_REQ:
   1883      1.5  christos 		ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
   1884      1.5  christos 						 end - pos);
   1885      1.5  christos 		break;
   1886      1.1  christos 	default:
   1887      1.2  christos 		wpa_printf(MSG_ERROR, "WNM: Unknown request");
   1888      1.1  christos 		break;
   1889      1.1  christos 	}
   1890      1.1  christos }
   1891      1.5  christos 
   1892      1.5  christos 
   1893      1.5  christos int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
   1894      1.5  christos 			       const struct wpabuf *elems)
   1895      1.5  christos {
   1896      1.5  christos 	struct wpabuf *buf;
   1897      1.5  christos 	int ret;
   1898      1.5  christos 
   1899      1.5  christos 	if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
   1900      1.5  christos 		return -1;
   1901      1.5  christos 
   1902      1.5  christos 	wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
   1903      1.5  christos 		   MACSTR " (dialog token %u)",
   1904      1.5  christos 		   MAC2STR(wpa_s->bssid), dialog_token);
   1905      1.5  christos 
   1906      1.5  christos 	buf = wpabuf_alloc(3 + wpabuf_len(elems));
   1907      1.5  christos 	if (!buf)
   1908      1.5  christos 		return -1;
   1909      1.5  christos 
   1910      1.5  christos 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
   1911      1.5  christos 	wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
   1912      1.5  christos 	wpabuf_put_u8(buf, dialog_token);
   1913      1.5  christos 	wpabuf_put_buf(buf, elems);
   1914      1.5  christos 
   1915      1.5  christos 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
   1916      1.5  christos 				  wpa_s->own_addr, wpa_s->bssid,
   1917      1.5  christos 				  wpabuf_head_u8(buf), wpabuf_len(buf), 0);
   1918      1.5  christos 	wpabuf_free(buf);
   1919      1.5  christos 	return ret;
   1920      1.5  christos }
   1921      1.5  christos 
   1922      1.5  christos 
   1923      1.5  christos void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
   1924      1.5  christos 			      struct wpabuf *elems)
   1925      1.5  christos {
   1926      1.5  christos 	wpabuf_free(wpa_s->coloc_intf_elems);
   1927      1.5  christos 	if (elems && wpabuf_len(elems) == 0) {
   1928      1.5  christos 		wpabuf_free(elems);
   1929      1.5  christos 		elems = NULL;
   1930      1.5  christos 	}
   1931      1.5  christos 	wpa_s->coloc_intf_elems = elems;
   1932      1.5  christos 
   1933      1.5  christos 	if (wpa_s->conf->coloc_intf_reporting && wpa_s->coloc_intf_elems &&
   1934      1.5  christos 	    wpa_s->coloc_intf_dialog_token &&
   1935      1.5  christos 	    (wpa_s->coloc_intf_auto_report == 1 ||
   1936      1.5  christos 	     wpa_s->coloc_intf_auto_report == 3)) {
   1937      1.5  christos 		/* TODO: Check that there has not been less than
   1938      1.5  christos 		 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
   1939      1.5  christos 		 */
   1940      1.5  christos 		wnm_send_coloc_intf_report(wpa_s,
   1941      1.5  christos 					   wpa_s->coloc_intf_dialog_token,
   1942      1.5  christos 					   wpa_s->coloc_intf_elems);
   1943      1.5  christos 	}
   1944      1.5  christos }
   1945      1.5  christos 
   1946      1.5  christos 
   1947      1.5  christos void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
   1948      1.5  christos {
   1949      1.5  christos #ifdef CONFIG_WNM
   1950      1.5  christos 	wpa_s->coloc_intf_dialog_token = 0;
   1951      1.5  christos 	wpa_s->coloc_intf_auto_report = 0;
   1952      1.5  christos #endif /* CONFIG_WNM */
   1953      1.5  christos }
   1954