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