Home | History | Annotate | Line # | Download | only in wpa_supplicant
wnm_sta.c revision 1.3.6.1
      1      1.1  christos /*
      2      1.1  christos  * wpa_supplicant - WNM
      3      1.2  christos  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
      4      1.1  christos  *
      5      1.1  christos  * This software may be distributed under the terms of the BSD license.
      6      1.1  christos  * See README for more details.
      7      1.1  christos  */
      8      1.1  christos 
      9      1.1  christos #include "utils/includes.h"
     10      1.1  christos 
     11      1.1  christos #include "utils/common.h"
     12      1.1  christos #include "common/ieee802_11_defs.h"
     13      1.2  christos #include "common/ieee802_11_common.h"
     14      1.2  christos #include "common/wpa_ctrl.h"
     15      1.1  christos #include "rsn_supp/wpa.h"
     16      1.1  christos #include "wpa_supplicant_i.h"
     17      1.1  christos #include "driver_i.h"
     18      1.1  christos #include "scan.h"
     19      1.2  christos #include "ctrl_iface.h"
     20      1.2  christos #include "bss.h"
     21      1.2  christos #include "wnm_sta.h"
     22      1.2  christos #include "hs20_supplicant.h"
     23      1.1  christos 
     24      1.1  christos #define MAX_TFS_IE_LEN  1024
     25      1.2  christos #define WNM_MAX_NEIGHBOR_REPORT 10
     26      1.1  christos 
     27      1.3  christos #define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
     28      1.1  christos 
     29      1.1  christos /* get the TFS IE from driver */
     30      1.1  christos static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
     31      1.1  christos 				   u16 *buf_len, enum wnm_oper oper)
     32      1.1  christos {
     33      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
     34      1.1  christos 
     35      1.1  christos 	return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
     36      1.1  christos }
     37      1.1  christos 
     38      1.1  christos 
     39      1.1  christos /* set the TFS IE to driver */
     40      1.1  christos static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
     41      1.3  christos 				   const u8 *addr, const u8 *buf, u16 buf_len,
     42      1.1  christos 				   enum wnm_oper oper)
     43      1.1  christos {
     44      1.3  christos 	u16 len = buf_len;
     45      1.3  christos 
     46      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
     47      1.1  christos 
     48      1.3  christos 	return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
     49      1.1  christos }
     50      1.1  christos 
     51      1.1  christos 
     52      1.1  christos /* MLME-SLEEPMODE.request */
     53      1.1  christos int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
     54      1.1  christos 				 u8 action, u16 intval, struct wpabuf *tfs_req)
     55      1.1  christos {
     56      1.1  christos 	struct ieee80211_mgmt *mgmt;
     57      1.1  christos 	int res;
     58      1.1  christos 	size_t len;
     59      1.1  christos 	struct wnm_sleep_element *wnmsleep_ie;
     60      1.1  christos 	u8 *wnmtfs_ie;
     61      1.1  christos 	u8 wnmsleep_ie_len;
     62      1.1  christos 	u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
     63      1.1  christos 	enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
     64      1.1  christos 		WNM_SLEEP_TFS_REQ_IE_NONE;
     65      1.1  christos 
     66      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
     67      1.1  christos 		   "action=%s to " MACSTR,
     68      1.1  christos 		   action == 0 ? "enter" : "exit",
     69      1.1  christos 		   MAC2STR(wpa_s->bssid));
     70      1.1  christos 
     71      1.1  christos 	/* WNM-Sleep Mode IE */
     72      1.1  christos 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
     73      1.1  christos 	wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
     74      1.1  christos 	if (wnmsleep_ie == NULL)
     75      1.1  christos 		return -1;
     76      1.1  christos 	wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
     77      1.1  christos 	wnmsleep_ie->len = wnmsleep_ie_len - 2;
     78      1.1  christos 	wnmsleep_ie->action_type = action;
     79      1.1  christos 	wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
     80      1.1  christos 	wnmsleep_ie->intval = host_to_le16(intval);
     81      1.1  christos 	wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
     82      1.1  christos 		    (u8 *) wnmsleep_ie, wnmsleep_ie_len);
     83      1.1  christos 
     84      1.1  christos 	/* TFS IE(s) */
     85      1.1  christos 	if (tfs_req) {
     86      1.1  christos 		wnmtfs_ie_len = wpabuf_len(tfs_req);
     87      1.1  christos 		wnmtfs_ie = os_malloc(wnmtfs_ie_len);
     88      1.1  christos 		if (wnmtfs_ie == NULL) {
     89      1.1  christos 			os_free(wnmsleep_ie);
     90      1.1  christos 			return -1;
     91      1.1  christos 		}
     92      1.1  christos 		os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
     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.3.6.1    martin 			   "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.3.6.1    martin 	wpa_s->wnmsleep_used = 0;
    303  1.3.6.1    martin 
    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.2  christos }
    342      1.2  christos 
    343      1.2  christos 
    344      1.2  christos static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
    345      1.2  christos 					   u8 id, u8 elen, const u8 *pos)
    346      1.2  christos {
    347      1.2  christos 	switch (id) {
    348      1.2  christos 	case WNM_NEIGHBOR_TSF:
    349      1.2  christos 		if (elen < 2 + 2) {
    350      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
    351      1.2  christos 			break;
    352      1.2  christos 		}
    353      1.2  christos 		rep->tsf_offset = WPA_GET_LE16(pos);
    354      1.2  christos 		rep->beacon_int = WPA_GET_LE16(pos + 2);
    355      1.2  christos 		rep->tsf_present = 1;
    356      1.2  christos 		break;
    357      1.2  christos 	case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
    358      1.2  christos 		if (elen < 2) {
    359      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
    360      1.2  christos 				   "country string");
    361      1.2  christos 			break;
    362      1.2  christos 		}
    363      1.2  christos 		os_memcpy(rep->country, pos, 2);
    364      1.2  christos 		rep->country_present = 1;
    365      1.2  christos 		break;
    366      1.2  christos 	case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
    367      1.2  christos 		if (elen < 1) {
    368      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
    369      1.2  christos 				   "candidate");
    370      1.2  christos 			break;
    371      1.2  christos 		}
    372      1.2  christos 		rep->preference = pos[0];
    373      1.2  christos 		rep->preference_present = 1;
    374      1.2  christos 		break;
    375      1.2  christos 	case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
    376      1.2  christos 		rep->bss_term_tsf = WPA_GET_LE64(pos);
    377      1.2  christos 		rep->bss_term_dur = WPA_GET_LE16(pos + 8);
    378      1.2  christos 		rep->bss_term_present = 1;
    379      1.2  christos 		break;
    380      1.2  christos 	case WNM_NEIGHBOR_BEARING:
    381      1.2  christos 		if (elen < 8) {
    382      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
    383      1.2  christos 				   "bearing");
    384      1.2  christos 			break;
    385      1.2  christos 		}
    386      1.2  christos 		rep->bearing = WPA_GET_LE16(pos);
    387      1.2  christos 		rep->distance = WPA_GET_LE32(pos + 2);
    388      1.2  christos 		rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
    389      1.2  christos 		rep->bearing_present = 1;
    390      1.2  christos 		break;
    391      1.2  christos 	case WNM_NEIGHBOR_MEASUREMENT_PILOT:
    392      1.2  christos 		if (elen < 1) {
    393      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
    394      1.2  christos 				   "pilot");
    395      1.2  christos 			break;
    396      1.2  christos 		}
    397      1.2  christos 		os_free(rep->meas_pilot);
    398      1.2  christos 		rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
    399      1.2  christos 		if (rep->meas_pilot == NULL)
    400      1.2  christos 			break;
    401      1.2  christos 		rep->meas_pilot->measurement_pilot = pos[0];
    402      1.2  christos 		rep->meas_pilot->subelem_len = elen - 1;
    403      1.2  christos 		os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
    404      1.2  christos 		break;
    405      1.2  christos 	case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
    406      1.2  christos 		if (elen < 5) {
    407      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
    408      1.2  christos 				   "capabilities");
    409      1.2  christos 			break;
    410      1.2  christos 		}
    411      1.2  christos 		os_memcpy(rep->rm_capab, pos, 5);
    412      1.2  christos 		rep->rm_capab_present = 1;
    413      1.2  christos 		break;
    414      1.2  christos 	case WNM_NEIGHBOR_MULTIPLE_BSSID:
    415      1.2  christos 		if (elen < 1) {
    416      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
    417      1.2  christos 			break;
    418      1.2  christos 		}
    419      1.2  christos 		os_free(rep->mul_bssid);
    420      1.2  christos 		rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
    421      1.2  christos 		if (rep->mul_bssid == NULL)
    422      1.2  christos 			break;
    423      1.2  christos 		rep->mul_bssid->max_bssid_indicator = pos[0];
    424      1.2  christos 		rep->mul_bssid->subelem_len = elen - 1;
    425      1.2  christos 		os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
    426      1.2  christos 		break;
    427      1.2  christos 	}
    428      1.2  christos }
    429      1.2  christos 
    430      1.2  christos 
    431      1.2  christos static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
    432      1.2  christos {
    433      1.2  christos 	struct wpa_bss *bss = wpa_s->current_bss;
    434      1.2  christos 	const char *country = NULL;
    435      1.3  christos 	int freq;
    436      1.2  christos 
    437      1.2  christos 	if (bss) {
    438      1.2  christos 		const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
    439      1.2  christos 
    440      1.2  christos 		if (elem && elem[1] >= 2)
    441      1.2  christos 			country = (const char *) (elem + 2);
    442      1.2  christos 	}
    443      1.2  christos 
    444      1.3  christos 	freq = ieee80211_chan_to_freq(country, op_class, chan);
    445      1.3  christos 	if (freq <= 0 && op_class == 0) {
    446      1.3  christos 		/*
    447      1.3  christos 		 * Some APs do not advertise correct operating class
    448      1.3  christos 		 * information. Try to determine the most likely operating
    449      1.3  christos 		 * frequency based on the channel number.
    450      1.3  christos 		 */
    451      1.3  christos 		if (chan >= 1 && chan <= 13)
    452      1.3  christos 			freq = 2407 + chan * 5;
    453      1.3  christos 		else if (chan == 14)
    454      1.3  christos 			freq = 2484;
    455      1.3  christos 		else if (chan >= 36 && chan <= 169)
    456      1.3  christos 			freq = 5000 + chan * 5;
    457      1.3  christos 	}
    458      1.3  christos 	return freq;
    459      1.2  christos }
    460      1.2  christos 
    461      1.2  christos 
    462      1.2  christos static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
    463      1.2  christos 				      const u8 *pos, u8 len,
    464      1.2  christos 				      struct neighbor_report *rep)
    465      1.2  christos {
    466      1.2  christos 	u8 left = len;
    467      1.2  christos 
    468      1.2  christos 	if (left < 13) {
    469      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
    470      1.2  christos 		return;
    471      1.2  christos 	}
    472      1.2  christos 
    473      1.2  christos 	os_memcpy(rep->bssid, pos, ETH_ALEN);
    474      1.2  christos 	rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
    475      1.2  christos 	rep->regulatory_class = *(pos + 10);
    476      1.2  christos 	rep->channel_number = *(pos + 11);
    477      1.2  christos 	rep->phy_type = *(pos + 12);
    478      1.2  christos 
    479      1.2  christos 	pos += 13;
    480      1.2  christos 	left -= 13;
    481      1.2  christos 
    482      1.2  christos 	while (left >= 2) {
    483      1.2  christos 		u8 id, elen;
    484      1.2  christos 
    485      1.2  christos 		id = *pos++;
    486      1.2  christos 		elen = *pos++;
    487      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
    488      1.2  christos 		left -= 2;
    489      1.2  christos 		if (elen > left) {
    490      1.2  christos 			wpa_printf(MSG_DEBUG,
    491      1.2  christos 				   "WNM: Truncated neighbor report subelement");
    492      1.2  christos 			break;
    493      1.2  christos 		}
    494      1.2  christos 		wnm_parse_neighbor_report_elem(rep, id, elen, pos);
    495      1.2  christos 		left -= elen;
    496      1.2  christos 		pos += elen;
    497      1.2  christos 	}
    498      1.2  christos 
    499      1.2  christos 	rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
    500      1.2  christos 				     rep->channel_number);
    501      1.2  christos }
    502      1.2  christos 
    503      1.2  christos 
    504      1.2  christos static struct wpa_bss *
    505      1.3  christos compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs)
    506      1.2  christos {
    507      1.2  christos 
    508      1.2  christos 	u8 i;
    509      1.2  christos 	struct wpa_bss *bss = wpa_s->current_bss;
    510      1.2  christos 	struct wpa_bss *target;
    511      1.2  christos 
    512      1.2  christos 	if (!bss)
    513      1.3  christos 		return NULL;
    514      1.2  christos 
    515      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
    516      1.2  christos 		   MAC2STR(wpa_s->bssid), bss->level);
    517      1.2  christos 
    518      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    519      1.2  christos 		struct neighbor_report *nei;
    520      1.2  christos 
    521      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
    522      1.2  christos 		if (nei->preference_present && nei->preference == 0) {
    523      1.2  christos 			wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
    524      1.2  christos 				   MAC2STR(nei->bssid));
    525      1.2  christos 			continue;
    526      1.2  christos 		}
    527      1.2  christos 
    528      1.2  christos 		target = wpa_bss_get_bssid(wpa_s, nei->bssid);
    529      1.2  christos 		if (!target) {
    530      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    531      1.2  christos 				   " (pref %d) not found in scan results",
    532      1.2  christos 				   MAC2STR(nei->bssid),
    533      1.2  christos 				   nei->preference_present ? nei->preference :
    534      1.2  christos 				   -1);
    535      1.2  christos 			continue;
    536      1.2  christos 		}
    537      1.2  christos 
    538      1.3  christos 		if (age_secs) {
    539      1.3  christos 			struct os_reltime now;
    540      1.3  christos 
    541      1.3  christos 			if (os_get_reltime(&now) == 0 &&
    542      1.3  christos 			    os_reltime_expired(&now, &target->last_update,
    543      1.3  christos 					       age_secs)) {
    544      1.3  christos 				wpa_printf(MSG_DEBUG,
    545      1.3  christos 					   "Candidate BSS is more than %ld seconds old",
    546      1.3  christos 					   age_secs);
    547      1.3  christos 				continue;
    548      1.3  christos 			}
    549      1.3  christos 		}
    550      1.3  christos 
    551      1.2  christos 		if (bss->ssid_len != target->ssid_len ||
    552      1.2  christos 		    os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
    553      1.2  christos 			/*
    554      1.2  christos 			 * TODO: Could consider allowing transition to another
    555      1.2  christos 			 * ESS if PMF was enabled for the association.
    556      1.2  christos 			 */
    557      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    558      1.2  christos 				   " (pref %d) in different ESS",
    559      1.2  christos 				   MAC2STR(nei->bssid),
    560      1.2  christos 				   nei->preference_present ? nei->preference :
    561      1.2  christos 				   -1);
    562      1.2  christos 			continue;
    563      1.2  christos 		}
    564      1.2  christos 
    565      1.3  christos 		if (wpa_s->current_ssid &&
    566      1.3  christos 		    !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
    567      1.3  christos 					1)) {
    568      1.3  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    569      1.3  christos 				   " (pref %d) does not match the current network profile",
    570      1.3  christos 				   MAC2STR(nei->bssid),
    571      1.3  christos 				   nei->preference_present ? nei->preference :
    572      1.3  christos 				   -1);
    573      1.3  christos 			continue;
    574      1.3  christos 		}
    575      1.3  christos 
    576      1.3  christos 		if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
    577      1.3  christos 			wpa_printf(MSG_DEBUG,
    578      1.3  christos 				   "MBO: Candidate BSS " MACSTR
    579      1.3  christos 				   " retry delay is not over yet",
    580      1.3  christos 				   MAC2STR(nei->bssid));
    581      1.3  christos 			continue;
    582      1.3  christos 		}
    583      1.3  christos 
    584      1.2  christos 		if (target->level < bss->level && target->level < -80) {
    585      1.2  christos 			wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
    586      1.2  christos 				   " (pref %d) does not have sufficient signal level (%d)",
    587      1.2  christos 				   MAC2STR(nei->bssid),
    588      1.2  christos 				   nei->preference_present ? nei->preference :
    589      1.2  christos 				   -1,
    590      1.2  christos 				   target->level);
    591      1.2  christos 			continue;
    592      1.2  christos 		}
    593      1.2  christos 
    594      1.2  christos 		wpa_printf(MSG_DEBUG,
    595      1.2  christos 			   "WNM: Found an acceptable preferred transition candidate BSS "
    596      1.2  christos 			   MACSTR " (RSSI %d)",
    597      1.2  christos 			   MAC2STR(nei->bssid), target->level);
    598      1.2  christos 		return target;
    599      1.2  christos 	}
    600      1.2  christos 
    601      1.2  christos 	return NULL;
    602      1.2  christos }
    603      1.2  christos 
    604      1.2  christos 
    605      1.3  christos static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
    606      1.3  christos {
    607      1.3  christos 	const u8 *ie_a, *ie_b;
    608      1.3  christos 
    609      1.3  christos 	if (!a || !b)
    610      1.3  christos 		return 0;
    611      1.3  christos 
    612      1.3  christos 	ie_a = wpa_bss_get_ie(a, eid);
    613      1.3  christos 	ie_b = wpa_bss_get_ie(b, eid);
    614      1.3  christos 
    615      1.3  christos 	if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
    616      1.3  christos 		return 0;
    617      1.3  christos 
    618      1.3  christos 	return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
    619      1.3  christos }
    620      1.3  christos 
    621      1.3  christos 
    622      1.3  christos static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
    623      1.3  christos {
    624      1.3  christos 	u32 info = 0;
    625      1.3  christos 
    626      1.3  christos 	info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
    627      1.3  christos 
    628      1.3  christos 	/*
    629      1.3  christos 	 * Leave the security and key scope bits unset to indicate that the
    630      1.3  christos 	 * security information is not available.
    631      1.3  christos 	 */
    632      1.3  christos 
    633      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
    634      1.3  christos 		info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
    635      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_QOS)
    636      1.3  christos 		info |= NEI_REP_BSSID_INFO_QOS;
    637      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_APSD)
    638      1.3  christos 		info |= NEI_REP_BSSID_INFO_APSD;
    639      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
    640      1.3  christos 		info |= NEI_REP_BSSID_INFO_RM;
    641      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
    642      1.3  christos 		info |= NEI_REP_BSSID_INFO_DELAYED_BA;
    643      1.3  christos 	if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
    644      1.3  christos 		info |= NEI_REP_BSSID_INFO_IMM_BA;
    645      1.3  christos 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
    646      1.3  christos 		info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
    647      1.3  christos 	if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
    648      1.3  christos 		info |= NEI_REP_BSSID_INFO_HT;
    649      1.3  christos 
    650      1.3  christos 	return info;
    651      1.3  christos }
    652      1.3  christos 
    653      1.3  christos 
    654      1.3  christos static int wnm_add_nei_rep(u8 *buf, size_t len, const u8 *bssid, u32 bss_info,
    655      1.3  christos 			   u8 op_class, u8 chan, u8 phy_type, u8 pref)
    656      1.3  christos {
    657      1.3  christos 	u8 *pos = buf;
    658      1.3  christos 
    659      1.3  christos 	if (len < 18) {
    660      1.3  christos 		wpa_printf(MSG_DEBUG,
    661      1.3  christos 			   "WNM: Not enough room for Neighbor Report element");
    662      1.3  christos 		return -1;
    663      1.3  christos 	}
    664      1.3  christos 
    665      1.3  christos 	*pos++ = WLAN_EID_NEIGHBOR_REPORT;
    666      1.3  christos 	/* length: 13 for basic neighbor report + 3 for preference subelement */
    667      1.3  christos 	*pos++ = 16;
    668      1.3  christos 	os_memcpy(pos, bssid, ETH_ALEN);
    669      1.3  christos 	pos += ETH_ALEN;
    670      1.3  christos 	WPA_PUT_LE32(pos, bss_info);
    671      1.3  christos 	pos += 4;
    672      1.3  christos 	*pos++ = op_class;
    673      1.3  christos 	*pos++ = chan;
    674      1.3  christos 	*pos++ = phy_type;
    675      1.3  christos 	*pos++ = WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE;
    676      1.3  christos 	*pos++ = 1;
    677      1.3  christos 	*pos++ = pref;
    678      1.3  christos 	return pos - buf;
    679      1.3  christos }
    680      1.3  christos 
    681      1.3  christos 
    682      1.3  christos static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
    683      1.3  christos 			       struct wpa_bss *bss, u8 *buf, size_t len,
    684      1.3  christos 			       u8 pref)
    685      1.3  christos {
    686      1.3  christos 	const u8 *ie;
    687      1.3  christos 	u8 op_class, chan;
    688      1.3  christos 	int sec_chan = 0, vht = 0;
    689      1.3  christos 	enum phy_type phy_type;
    690      1.3  christos 	u32 info;
    691      1.3  christos 	struct ieee80211_ht_operation *ht_oper = NULL;
    692      1.3  christos 	struct ieee80211_vht_operation *vht_oper = NULL;
    693      1.3  christos 
    694      1.3  christos 	ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
    695      1.3  christos 	if (ie && ie[1] >= 2) {
    696      1.3  christos 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
    697      1.3  christos 
    698      1.3  christos 		if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
    699      1.3  christos 			sec_chan = 1;
    700      1.3  christos 		else if (ht_oper->ht_param &
    701      1.3  christos 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
    702      1.3  christos 			sec_chan = -1;
    703      1.3  christos 	}
    704      1.3  christos 
    705      1.3  christos 	ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
    706      1.3  christos 	if (ie && ie[1] >= 1) {
    707      1.3  christos 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
    708      1.3  christos 
    709      1.3  christos 		if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
    710      1.3  christos 		    vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
    711      1.3  christos 		    vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
    712      1.3  christos 			vht = vht_oper->vht_op_info_chwidth;
    713      1.3  christos 	}
    714      1.3  christos 
    715      1.3  christos 	if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
    716      1.3  christos 					  &chan) == NUM_HOSTAPD_MODES) {
    717      1.3  christos 		wpa_printf(MSG_DEBUG,
    718      1.3  christos 			   "WNM: Cannot determine operating class and channel");
    719      1.3  christos 		return -2;
    720      1.3  christos 	}
    721      1.3  christos 
    722      1.3  christos 	phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
    723      1.3  christos 					  (vht_oper != NULL));
    724      1.3  christos 	if (phy_type == PHY_TYPE_UNSPECIFIED) {
    725      1.3  christos 		wpa_printf(MSG_DEBUG,
    726      1.3  christos 			   "WNM: Cannot determine BSS phy type for Neighbor Report");
    727      1.3  christos 		return -2;
    728      1.3  christos 	}
    729      1.3  christos 
    730      1.3  christos 	info = wnm_get_bss_info(wpa_s, bss);
    731      1.3  christos 
    732      1.3  christos 	return wnm_add_nei_rep(buf, len, bss->bssid, info, op_class, chan,
    733      1.3  christos 			       phy_type, pref);
    734      1.3  christos }
    735      1.3  christos 
    736      1.3  christos 
    737      1.3  christos static int wnm_add_cand_list(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
    738      1.3  christos {
    739      1.3  christos 	u8 *pos = buf;
    740      1.3  christos 	unsigned int i, pref = 255;
    741      1.3  christos 	struct os_reltime now;
    742      1.3  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    743      1.3  christos 
    744      1.3  christos 	if (!ssid)
    745      1.3  christos 		return 0;
    746      1.3  christos 
    747      1.3  christos 	/*
    748      1.3  christos 	 * TODO: Define when scan results are no longer valid for the candidate
    749      1.3  christos 	 * list.
    750      1.3  christos 	 */
    751      1.3  christos 	os_get_reltime(&now);
    752      1.3  christos 	if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
    753      1.3  christos 		return 0;
    754      1.3  christos 
    755      1.3  christos 	wpa_printf(MSG_DEBUG,
    756      1.3  christos 		   "WNM: Add candidate list to BSS Transition Management Response frame");
    757      1.3  christos 	for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
    758      1.3  christos 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
    759      1.3  christos 		int res;
    760      1.3  christos 
    761      1.3  christos 		if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1)) {
    762      1.3  christos 			res = wnm_nei_rep_add_bss(wpa_s, bss, pos, len, pref--);
    763      1.3  christos 			if (res == -2)
    764      1.3  christos 				continue; /* could not build entry for BSS */
    765      1.3  christos 			if (res < 0)
    766      1.3  christos 				break; /* no more room for candidates */
    767      1.3  christos 			if (pref == 1)
    768      1.3  christos 				break;
    769      1.3  christos 
    770      1.3  christos 			pos += res;
    771      1.3  christos 			len -= res;
    772      1.3  christos 		}
    773      1.3  christos 	}
    774      1.3  christos 
    775      1.3  christos 	wpa_hexdump(MSG_DEBUG,
    776      1.3  christos 		    "WNM: BSS Transition Management Response candidate list",
    777      1.3  christos 		    buf, pos - buf);
    778      1.3  christos 
    779      1.3  christos 	return pos - buf;
    780      1.3  christos }
    781      1.3  christos 
    782      1.3  christos 
    783      1.2  christos static void wnm_send_bss_transition_mgmt_resp(
    784      1.2  christos 	struct wpa_supplicant *wpa_s, u8 dialog_token,
    785      1.2  christos 	enum bss_trans_mgmt_status_code status, u8 delay,
    786      1.2  christos 	const u8 *target_bssid)
    787      1.1  christos {
    788      1.3  christos 	u8 buf[2000], *pos;
    789      1.1  christos 	struct ieee80211_mgmt *mgmt;
    790      1.1  christos 	size_t len;
    791      1.2  christos 	int res;
    792      1.1  christos 
    793      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
    794      1.1  christos 		   "to " MACSTR " dialog_token=%u status=%u delay=%d",
    795      1.1  christos 		   MAC2STR(wpa_s->bssid), dialog_token, status, delay);
    796      1.2  christos 	if (!wpa_s->current_bss) {
    797      1.2  christos 		wpa_printf(MSG_DEBUG,
    798      1.2  christos 			   "WNM: Current BSS not known - drop response");
    799      1.2  christos 		return;
    800      1.2  christos 	}
    801      1.1  christos 
    802      1.1  christos 	mgmt = (struct ieee80211_mgmt *) buf;
    803      1.1  christos 	os_memset(&buf, 0, sizeof(buf));
    804      1.1  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
    805      1.1  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
    806      1.1  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
    807      1.1  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    808      1.1  christos 					   WLAN_FC_STYPE_ACTION);
    809      1.1  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
    810      1.1  christos 	mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
    811      1.1  christos 	mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
    812      1.1  christos 	mgmt->u.action.u.bss_tm_resp.status_code = status;
    813      1.1  christos 	mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
    814      1.1  christos 	pos = mgmt->u.action.u.bss_tm_resp.variable;
    815      1.1  christos 	if (target_bssid) {
    816      1.1  christos 		os_memcpy(pos, target_bssid, ETH_ALEN);
    817      1.1  christos 		pos += ETH_ALEN;
    818      1.2  christos 	} else if (status == WNM_BSS_TM_ACCEPT) {
    819      1.2  christos 		/*
    820      1.2  christos 		 * P802.11-REVmc clarifies that the Target BSSID field is always
    821      1.2  christos 		 * present when status code is zero, so use a fake value here if
    822      1.2  christos 		 * no BSSID is yet known.
    823      1.2  christos 		 */
    824      1.2  christos 		os_memset(pos, 0, ETH_ALEN);
    825      1.2  christos 		pos += ETH_ALEN;
    826      1.1  christos 	}
    827      1.1  christos 
    828      1.3  christos 	if (status == WNM_BSS_TM_ACCEPT)
    829      1.3  christos 		pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
    830      1.3  christos 
    831      1.3  christos #ifdef CONFIG_MBO
    832      1.3  christos 	if (status != WNM_BSS_TM_ACCEPT) {
    833      1.3  christos 		pos += wpas_mbo_ie_bss_trans_reject(
    834      1.3  christos 			wpa_s, pos, buf + sizeof(buf) - pos,
    835      1.3  christos 			MBO_TRANSITION_REJECT_REASON_UNSPECIFIED);
    836      1.3  christos 	}
    837      1.3  christos #endif /* CONFIG_MBO */
    838      1.3  christos 
    839      1.1  christos 	len = pos - (u8 *) &mgmt->u.action.category;
    840      1.1  christos 
    841      1.2  christos 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    842      1.2  christos 				  wpa_s->own_addr, wpa_s->bssid,
    843      1.2  christos 				  &mgmt->u.action.category, len, 0);
    844      1.2  christos 	if (res < 0) {
    845      1.2  christos 		wpa_printf(MSG_DEBUG,
    846      1.2  christos 			   "WNM: Failed to send BSS Transition Management Response");
    847      1.2  christos 	}
    848      1.2  christos }
    849      1.2  christos 
    850      1.2  christos 
    851      1.3  christos static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
    852      1.3  christos 			       struct wpa_bss *bss, struct wpa_ssid *ssid,
    853      1.3  christos 			       int after_new_scan)
    854      1.3  christos {
    855      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
    856      1.3  christos 		"WNM: Transition to BSS " MACSTR
    857      1.3  christos 		" based on BSS Transition Management Request (old BSSID "
    858      1.3  christos 		MACSTR " after_new_scan=%d)",
    859      1.3  christos 		MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
    860      1.3  christos 
    861      1.3  christos 	/* Send the BSS Management Response - Accept */
    862      1.3  christos 	if (wpa_s->wnm_reply) {
    863      1.3  christos 		wpa_s->wnm_reply = 0;
    864      1.3  christos 		wpa_printf(MSG_DEBUG,
    865      1.3  christos 			   "WNM: Sending successful BSS Transition Management Response");
    866      1.3  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
    867      1.3  christos 						  wpa_s->wnm_dialog_token,
    868      1.3  christos 						  WNM_BSS_TM_ACCEPT,
    869      1.3  christos 						  0, bss->bssid);
    870      1.3  christos 	}
    871      1.3  christos 
    872      1.3  christos 	if (bss == wpa_s->current_bss) {
    873      1.3  christos 		wpa_printf(MSG_DEBUG,
    874      1.3  christos 			   "WNM: Already associated with the preferred candidate");
    875      1.3  christos 		wnm_deallocate_memory(wpa_s);
    876      1.3  christos 		return;
    877      1.3  christos 	}
    878      1.3  christos 
    879      1.3  christos 	wpa_s->reassociate = 1;
    880      1.3  christos 	wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
    881      1.3  christos 	wpa_supplicant_connect(wpa_s, bss, ssid);
    882      1.3  christos 	wnm_deallocate_memory(wpa_s);
    883      1.3  christos }
    884      1.3  christos 
    885      1.3  christos 
    886      1.2  christos int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
    887      1.2  christos {
    888      1.2  christos 	struct wpa_bss *bss;
    889      1.2  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    890      1.2  christos 	enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
    891      1.2  christos 
    892      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
    893      1.2  christos 		return 0;
    894      1.2  christos 
    895      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
    896      1.3  christos 		"WNM: Process scan results for BSS Transition Management");
    897      1.2  christos 	if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
    898      1.2  christos 			      &wpa_s->scan_trigger_time)) {
    899      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
    900      1.2  christos 		wnm_deallocate_memory(wpa_s);
    901      1.2  christos 		return 0;
    902      1.2  christos 	}
    903      1.2  christos 
    904      1.2  christos 	if (!wpa_s->current_bss ||
    905      1.2  christos 	    os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
    906      1.2  christos 		      ETH_ALEN) != 0) {
    907      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
    908      1.2  christos 		return 0;
    909      1.2  christos 	}
    910      1.2  christos 
    911      1.2  christos 	/* Compare the Neighbor Report and scan results */
    912      1.3  christos 	bss = compare_scan_neighbor_results(wpa_s, 0);
    913      1.2  christos 	if (!bss) {
    914      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
    915      1.2  christos 		status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
    916      1.2  christos 		goto send_bss_resp_fail;
    917      1.2  christos 	}
    918      1.2  christos 
    919      1.2  christos 	/* Associate to the network */
    920      1.3  christos 	wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
    921      1.2  christos 	return 1;
    922      1.2  christos 
    923      1.2  christos send_bss_resp_fail:
    924      1.2  christos 	if (!reply_on_fail)
    925      1.2  christos 		return 0;
    926      1.2  christos 
    927      1.2  christos 	/* Send reject response for all the failures */
    928      1.2  christos 
    929      1.2  christos 	if (wpa_s->wnm_reply) {
    930      1.2  christos 		wpa_s->wnm_reply = 0;
    931      1.2  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
    932      1.2  christos 						  wpa_s->wnm_dialog_token,
    933      1.2  christos 						  status, 0, NULL);
    934      1.2  christos 	}
    935      1.2  christos 	wnm_deallocate_memory(wpa_s);
    936      1.2  christos 
    937      1.2  christos 	return 0;
    938      1.2  christos }
    939      1.2  christos 
    940      1.2  christos 
    941      1.2  christos static int cand_pref_compar(const void *a, const void *b)
    942      1.2  christos {
    943      1.2  christos 	const struct neighbor_report *aa = a;
    944      1.2  christos 	const struct neighbor_report *bb = b;
    945      1.2  christos 
    946      1.2  christos 	if (!aa->preference_present && !bb->preference_present)
    947      1.2  christos 		return 0;
    948      1.2  christos 	if (!aa->preference_present)
    949      1.2  christos 		return 1;
    950      1.2  christos 	if (!bb->preference_present)
    951      1.2  christos 		return -1;
    952      1.2  christos 	if (bb->preference > aa->preference)
    953      1.2  christos 		return 1;
    954      1.2  christos 	if (bb->preference < aa->preference)
    955      1.2  christos 		return -1;
    956      1.2  christos 	return 0;
    957      1.2  christos }
    958      1.2  christos 
    959      1.2  christos 
    960      1.2  christos static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
    961      1.2  christos {
    962      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
    963      1.2  christos 		return;
    964      1.2  christos 	qsort(wpa_s->wnm_neighbor_report_elements,
    965      1.2  christos 	      wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
    966      1.2  christos 	      cand_pref_compar);
    967      1.2  christos }
    968      1.2  christos 
    969      1.2  christos 
    970      1.2  christos static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
    971      1.2  christos {
    972      1.2  christos 	unsigned int i;
    973      1.2  christos 
    974      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
    975      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
    976      1.2  christos 		return;
    977      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    978      1.2  christos 		struct neighbor_report *nei;
    979      1.2  christos 
    980      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
    981      1.2  christos 		wpa_printf(MSG_DEBUG, "%u: " MACSTR
    982      1.2  christos 			   " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
    983      1.2  christos 			   i, MAC2STR(nei->bssid), nei->bssid_info,
    984      1.2  christos 			   nei->regulatory_class,
    985      1.2  christos 			   nei->channel_number, nei->phy_type,
    986      1.2  christos 			   nei->preference_present ? nei->preference : -1,
    987      1.2  christos 			   nei->freq);
    988      1.2  christos 	}
    989      1.2  christos }
    990      1.2  christos 
    991      1.2  christos 
    992      1.2  christos static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
    993      1.2  christos {
    994      1.2  christos 	unsigned int i;
    995      1.2  christos 
    996      1.2  christos 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
    997      1.2  christos 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
    998      1.2  christos 		int j;
    999      1.2  christos 
   1000      1.2  christos 		for (j = 0; j < mode->num_channels; j++) {
   1001      1.2  christos 			struct hostapd_channel_data *chan;
   1002      1.2  christos 
   1003      1.2  christos 			chan = &mode->channels[j];
   1004      1.2  christos 			if (chan->freq == freq &&
   1005      1.2  christos 			    !(chan->flag & HOSTAPD_CHAN_DISABLED))
   1006      1.2  christos 				return 1;
   1007      1.2  christos 		}
   1008      1.2  christos 	}
   1009      1.2  christos 
   1010      1.2  christos 	return 0;
   1011      1.2  christos }
   1012      1.2  christos 
   1013      1.2  christos 
   1014      1.2  christos static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
   1015      1.2  christos {
   1016      1.2  christos 	int *freqs;
   1017      1.2  christos 	int num_freqs = 0;
   1018      1.2  christos 	unsigned int i;
   1019      1.2  christos 
   1020      1.2  christos 	if (!wpa_s->wnm_neighbor_report_elements)
   1021      1.2  christos 		return;
   1022      1.2  christos 
   1023      1.2  christos 	if (wpa_s->hw.modes == NULL)
   1024      1.2  christos 		return;
   1025      1.2  christos 
   1026      1.2  christos 	os_free(wpa_s->next_scan_freqs);
   1027      1.2  christos 	wpa_s->next_scan_freqs = NULL;
   1028      1.2  christos 
   1029      1.2  christos 	freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
   1030      1.2  christos 	if (freqs == NULL)
   1031      1.2  christos 		return;
   1032      1.2  christos 
   1033      1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
   1034      1.2  christos 		struct neighbor_report *nei;
   1035      1.2  christos 
   1036      1.2  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
   1037      1.2  christos 		if (nei->freq <= 0) {
   1038      1.2  christos 			wpa_printf(MSG_DEBUG,
   1039      1.2  christos 				   "WNM: Unknown neighbor operating frequency for "
   1040      1.2  christos 				   MACSTR " - scan all channels",
   1041      1.2  christos 				   MAC2STR(nei->bssid));
   1042      1.2  christos 			os_free(freqs);
   1043      1.2  christos 			return;
   1044      1.2  christos 		}
   1045      1.2  christos 		if (chan_supported(wpa_s, nei->freq))
   1046      1.2  christos 			add_freq(freqs, &num_freqs, nei->freq);
   1047      1.2  christos 	}
   1048      1.2  christos 
   1049      1.2  christos 	if (num_freqs == 0) {
   1050      1.2  christos 		os_free(freqs);
   1051      1.2  christos 		return;
   1052      1.2  christos 	}
   1053      1.2  christos 
   1054      1.2  christos 	wpa_printf(MSG_DEBUG,
   1055      1.2  christos 		   "WNM: Scan %d frequencies based on transition candidate list",
   1056      1.2  christos 		   num_freqs);
   1057      1.2  christos 	wpa_s->next_scan_freqs = freqs;
   1058      1.1  christos }
   1059      1.1  christos 
   1060      1.1  christos 
   1061      1.3  christos static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
   1062      1.3  christos {
   1063      1.3  christos 	struct wpa_scan_results *scan_res;
   1064      1.3  christos 	struct wpa_bss *bss;
   1065      1.3  christos 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   1066      1.3  christos 	u8 i, found = 0;
   1067      1.3  christos 	size_t j;
   1068      1.3  christos 
   1069      1.3  christos 	wpa_dbg(wpa_s, MSG_DEBUG,
   1070      1.3  christos 		"WNM: Fetch current scan results from the driver for checking transition candidates");
   1071      1.3  christos 	scan_res = wpa_drv_get_scan_results2(wpa_s);
   1072      1.3  christos 	if (!scan_res) {
   1073      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
   1074      1.3  christos 		return 0;
   1075      1.3  christos 	}
   1076      1.3  christos 
   1077      1.3  christos 	if (scan_res->fetch_time.sec == 0)
   1078      1.3  christos 		os_get_reltime(&scan_res->fetch_time);
   1079      1.3  christos 
   1080      1.3  christos 	filter_scan_res(wpa_s, scan_res);
   1081      1.3  christos 
   1082      1.3  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
   1083      1.3  christos 		struct neighbor_report *nei;
   1084      1.3  christos 
   1085      1.3  christos 		nei = &wpa_s->wnm_neighbor_report_elements[i];
   1086      1.3  christos 		if (nei->preference_present && nei->preference == 0)
   1087      1.3  christos 			continue;
   1088      1.3  christos 
   1089      1.3  christos 		for (j = 0; j < scan_res->num; j++) {
   1090      1.3  christos 			struct wpa_scan_res *res;
   1091      1.3  christos 			const u8 *ssid_ie;
   1092      1.3  christos 
   1093      1.3  christos 			res = scan_res->res[j];
   1094      1.3  christos 			if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
   1095      1.3  christos 			    res->age > WNM_SCAN_RESULT_AGE * 1000)
   1096      1.3  christos 				continue;
   1097      1.3  christos 			bss = wpa_s->current_bss;
   1098      1.3  christos 			ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
   1099      1.3  christos 			if (bss && ssid_ie &&
   1100      1.3  christos 			    (bss->ssid_len != ssid_ie[1] ||
   1101      1.3  christos 			     os_memcmp(bss->ssid, ssid_ie + 2,
   1102      1.3  christos 				       bss->ssid_len) != 0))
   1103      1.3  christos 				continue;
   1104      1.3  christos 
   1105      1.3  christos 			/* Potential candidate found */
   1106      1.3  christos 			found = 1;
   1107      1.3  christos 			scan_snr(res);
   1108      1.3  christos 			scan_est_throughput(wpa_s, res);
   1109      1.3  christos 			wpa_bss_update_scan_res(wpa_s, res,
   1110      1.3  christos 						&scan_res->fetch_time);
   1111      1.3  christos 		}
   1112      1.3  christos 	}
   1113      1.3  christos 
   1114      1.3  christos 	wpa_scan_results_free(scan_res);
   1115      1.3  christos 	if (!found) {
   1116      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG,
   1117      1.3  christos 			"WNM: No transition candidate matches existing scan results");
   1118      1.3  christos 		return 0;
   1119      1.3  christos 	}
   1120      1.3  christos 
   1121      1.3  christos 	bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE);
   1122      1.3  christos 	if (!bss) {
   1123      1.3  christos 		wpa_dbg(wpa_s, MSG_DEBUG,
   1124      1.3  christos 			"WNM: Comparison of scan results against transition candidates did not find matches");
   1125      1.3  christos 		return 0;
   1126      1.3  christos 	}
   1127      1.3  christos 
   1128      1.3  christos 	/* Associate to the network */
   1129      1.3  christos 	wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
   1130      1.3  christos 	return 1;
   1131      1.3  christos }
   1132      1.3  christos 
   1133      1.3  christos 
   1134      1.1  christos static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
   1135      1.1  christos 					     const u8 *pos, const u8 *end,
   1136      1.1  christos 					     int reply)
   1137      1.1  christos {
   1138      1.2  christos 	unsigned int beacon_int;
   1139      1.2  christos 	u8 valid_int;
   1140      1.3  christos #ifdef CONFIG_MBO
   1141      1.3  christos 	const u8 *vendor;
   1142      1.3  christos #endif /* CONFIG_MBO */
   1143      1.1  christos 
   1144      1.3  christos 	if (end - pos < 5)
   1145      1.1  christos 		return;
   1146      1.1  christos 
   1147      1.2  christos 	if (wpa_s->current_bss)
   1148      1.2  christos 		beacon_int = wpa_s->current_bss->beacon_int;
   1149      1.2  christos 	else
   1150      1.2  christos 		beacon_int = 100; /* best guess */
   1151      1.2  christos 
   1152      1.2  christos 	wpa_s->wnm_dialog_token = pos[0];
   1153      1.2  christos 	wpa_s->wnm_mode = pos[1];
   1154      1.2  christos 	wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
   1155      1.2  christos 	valid_int = pos[4];
   1156      1.2  christos 	wpa_s->wnm_reply = reply;
   1157      1.1  christos 
   1158      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
   1159      1.1  christos 		   "dialog_token=%u request_mode=0x%x "
   1160      1.1  christos 		   "disassoc_timer=%u validity_interval=%u",
   1161      1.2  christos 		   wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
   1162      1.2  christos 		   wpa_s->wnm_dissoc_timer, valid_int);
   1163      1.2  christos 
   1164      1.3  christos #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
   1165      1.3  christos 	if (wpa_s->reject_btm_req_reason) {
   1166      1.3  christos 		wpa_printf(MSG_INFO,
   1167      1.3  christos 			   "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
   1168      1.3  christos 			   wpa_s->reject_btm_req_reason);
   1169      1.3  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
   1170      1.3  christos 						  wpa_s->wnm_dialog_token,
   1171      1.3  christos 						  wpa_s->reject_btm_req_reason,
   1172      1.3  christos 						  0, NULL);
   1173      1.3  christos 		return;
   1174      1.3  christos 	}
   1175      1.3  christos #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
   1176      1.3  christos 
   1177      1.1  christos 	pos += 5;
   1178      1.2  christos 
   1179      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
   1180      1.3  christos 		if (end - pos < 12) {
   1181      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
   1182      1.2  christos 			return;
   1183      1.2  christos 		}
   1184      1.2  christos 		os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
   1185      1.1  christos 		pos += 12; /* BSS Termination Duration */
   1186      1.2  christos 	}
   1187      1.2  christos 
   1188      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
   1189      1.1  christos 		char url[256];
   1190      1.2  christos 
   1191      1.3  christos 		if (end - pos < 1 || 1 + pos[0] > end - pos) {
   1192      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
   1193      1.1  christos 				   "Management Request (URL)");
   1194      1.1  christos 			return;
   1195      1.1  christos 		}
   1196      1.1  christos 		os_memcpy(url, pos + 1, pos[0]);
   1197      1.1  christos 		url[pos[0]] = '\0';
   1198      1.2  christos 		pos += 1 + pos[0];
   1199      1.2  christos 
   1200      1.2  christos 		wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
   1201      1.2  christos 			wpa_sm_pmf_enabled(wpa_s->wpa),
   1202      1.2  christos 			wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
   1203      1.1  christos 	}
   1204      1.1  christos 
   1205      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
   1206      1.1  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
   1207      1.2  christos 			"Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
   1208      1.2  christos 		if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
   1209      1.1  christos 			/* TODO: mark current BSS less preferred for
   1210      1.1  christos 			 * selection */
   1211      1.1  christos 			wpa_printf(MSG_DEBUG, "Trying to find another BSS");
   1212      1.1  christos 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   1213      1.1  christos 		}
   1214      1.1  christos 	}
   1215      1.1  christos 
   1216      1.3  christos #ifdef CONFIG_MBO
   1217      1.3  christos 	vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
   1218      1.3  christos 	if (vendor)
   1219      1.3  christos 		wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
   1220      1.3  christos #endif /* CONFIG_MBO */
   1221      1.3  christos 
   1222      1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
   1223      1.2  christos 		unsigned int valid_ms;
   1224      1.2  christos 
   1225      1.2  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
   1226      1.2  christos 		wnm_deallocate_memory(wpa_s);
   1227      1.2  christos 		wpa_s->wnm_neighbor_report_elements = os_calloc(
   1228      1.2  christos 			WNM_MAX_NEIGHBOR_REPORT,
   1229      1.2  christos 			sizeof(struct neighbor_report));
   1230      1.2  christos 		if (wpa_s->wnm_neighbor_report_elements == NULL)
   1231      1.2  christos 			return;
   1232      1.2  christos 
   1233      1.3  christos 		while (end - pos >= 2 &&
   1234      1.2  christos 		       wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
   1235      1.2  christos 		{
   1236      1.2  christos 			u8 tag = *pos++;
   1237      1.2  christos 			u8 len = *pos++;
   1238      1.2  christos 
   1239      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
   1240      1.2  christos 				   tag);
   1241      1.3  christos 			if (len > end - pos) {
   1242      1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: Truncated request");
   1243      1.2  christos 				return;
   1244      1.2  christos 			}
   1245      1.2  christos 			if (tag == WLAN_EID_NEIGHBOR_REPORT) {
   1246      1.2  christos 				struct neighbor_report *rep;
   1247      1.2  christos 				rep = &wpa_s->wnm_neighbor_report_elements[
   1248      1.2  christos 					wpa_s->wnm_num_neighbor_report];
   1249      1.2  christos 				wnm_parse_neighbor_report(wpa_s, pos, len, rep);
   1250      1.3  christos 				wpa_s->wnm_num_neighbor_report++;
   1251      1.2  christos 			}
   1252      1.2  christos 
   1253      1.2  christos 			pos += len;
   1254      1.2  christos 		}
   1255      1.3  christos 
   1256      1.3  christos 		if (!wpa_s->wnm_num_neighbor_report) {
   1257      1.3  christos 			wpa_printf(MSG_DEBUG,
   1258      1.3  christos 				   "WNM: Candidate list included bit is set, but no candidates found");
   1259      1.3  christos 			wnm_send_bss_transition_mgmt_resp(
   1260      1.3  christos 				wpa_s, wpa_s->wnm_dialog_token,
   1261      1.3  christos 				WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
   1262      1.3  christos 				0, NULL);
   1263      1.3  christos 			return;
   1264      1.3  christos 		}
   1265      1.3  christos 
   1266      1.2  christos 		wnm_sort_cand_list(wpa_s);
   1267      1.2  christos 		wnm_dump_cand_list(wpa_s);
   1268      1.2  christos 		valid_ms = valid_int * beacon_int * 128 / 125;
   1269      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
   1270      1.2  christos 			   valid_ms);
   1271      1.2  christos 		os_get_reltime(&wpa_s->wnm_cand_valid_until);
   1272      1.2  christos 		wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
   1273      1.2  christos 		wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
   1274      1.2  christos 		wpa_s->wnm_cand_valid_until.sec +=
   1275      1.2  christos 			wpa_s->wnm_cand_valid_until.usec / 1000000;
   1276      1.2  christos 		wpa_s->wnm_cand_valid_until.usec %= 1000000;
   1277      1.2  christos 		os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
   1278      1.2  christos 
   1279      1.3  christos 		/*
   1280      1.3  christos 		 * Fetch the latest scan results from the kernel and check for
   1281      1.3  christos 		 * candidates based on those results first. This can help in
   1282      1.3  christos 		 * finding more up-to-date information should the driver has
   1283      1.3  christos 		 * done some internal scanning operations after the last scan
   1284      1.3  christos 		 * result update in wpa_supplicant.
   1285      1.3  christos 		 */
   1286      1.3  christos 		if (wnm_fetch_scan_results(wpa_s) > 0)
   1287      1.3  christos 			return;
   1288      1.3  christos 
   1289      1.3  christos 		/*
   1290      1.3  christos 		 * Try to use previously received scan results, if they are
   1291      1.3  christos 		 * recent enough to use for a connection.
   1292      1.3  christos 		 */
   1293      1.2  christos 		if (wpa_s->last_scan_res_used > 0) {
   1294      1.2  christos 			struct os_reltime now;
   1295      1.2  christos 
   1296      1.2  christos 			os_get_reltime(&now);
   1297      1.2  christos 			if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
   1298      1.2  christos 				wpa_printf(MSG_DEBUG,
   1299      1.2  christos 					   "WNM: Try to use recent scan results");
   1300      1.2  christos 				if (wnm_scan_process(wpa_s, 0) > 0)
   1301      1.2  christos 					return;
   1302      1.2  christos 				wpa_printf(MSG_DEBUG,
   1303      1.2  christos 					   "WNM: No match in previous scan results - try a new scan");
   1304      1.2  christos 			}
   1305      1.2  christos 		}
   1306      1.2  christos 
   1307      1.2  christos 		wnm_set_scan_freqs(wpa_s);
   1308      1.3  christos 		if (wpa_s->wnm_num_neighbor_report == 1) {
   1309      1.3  christos 			os_memcpy(wpa_s->next_scan_bssid,
   1310      1.3  christos 				  wpa_s->wnm_neighbor_report_elements[0].bssid,
   1311      1.3  christos 				  ETH_ALEN);
   1312      1.3  christos 			wpa_printf(MSG_DEBUG,
   1313      1.3  christos 				   "WNM: Scan only for a specific BSSID since there is only a single candidate "
   1314      1.3  christos 				   MACSTR, MAC2STR(wpa_s->next_scan_bssid));
   1315      1.3  christos 		}
   1316      1.2  christos 		wpa_supplicant_req_scan(wpa_s, 0, 0);
   1317      1.2  christos 	} else if (reply) {
   1318      1.2  christos 		enum bss_trans_mgmt_status_code status;
   1319      1.2  christos 		if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
   1320      1.2  christos 			status = WNM_BSS_TM_ACCEPT;
   1321      1.2  christos 		else {
   1322      1.2  christos 			wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
   1323      1.2  christos 			status = WNM_BSS_TM_REJECT_UNSPECIFIED;
   1324      1.2  christos 		}
   1325      1.2  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
   1326      1.2  christos 						  wpa_s->wnm_dialog_token,
   1327      1.2  christos 						  status, 0, NULL);
   1328      1.2  christos 	}
   1329      1.2  christos }
   1330      1.2  christos 
   1331      1.2  christos 
   1332      1.2  christos int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
   1333      1.3  christos 				       u8 query_reason, int cand_list)
   1334      1.2  christos {
   1335      1.3  christos 	u8 buf[2000], *pos;
   1336      1.2  christos 	struct ieee80211_mgmt *mgmt;
   1337      1.2  christos 	size_t len;
   1338      1.2  christos 	int ret;
   1339      1.2  christos 
   1340      1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
   1341      1.3  christos 		   MACSTR " query_reason=%u%s",
   1342      1.3  christos 		   MAC2STR(wpa_s->bssid), query_reason,
   1343      1.3  christos 		   cand_list ? " candidate list" : "");
   1344      1.2  christos 
   1345      1.2  christos 	mgmt = (struct ieee80211_mgmt *) buf;
   1346      1.2  christos 	os_memset(&buf, 0, sizeof(buf));
   1347      1.2  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
   1348      1.2  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
   1349      1.2  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
   1350      1.2  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
   1351      1.2  christos 					   WLAN_FC_STYPE_ACTION);
   1352      1.2  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
   1353      1.2  christos 	mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
   1354      1.2  christos 	mgmt->u.action.u.bss_tm_query.dialog_token = 1;
   1355      1.2  christos 	mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
   1356      1.2  christos 	pos = mgmt->u.action.u.bss_tm_query.variable;
   1357      1.2  christos 
   1358      1.3  christos 	if (cand_list)
   1359      1.3  christos 		pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
   1360      1.3  christos 
   1361      1.2  christos 	len = pos - (u8 *) &mgmt->u.action.category;
   1362      1.2  christos 
   1363      1.2  christos 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
   1364      1.2  christos 				  wpa_s->own_addr, wpa_s->bssid,
   1365      1.2  christos 				  &mgmt->u.action.category, len, 0);
   1366      1.2  christos 
   1367      1.2  christos 	return ret;
   1368      1.2  christos }
   1369      1.2  christos 
   1370      1.2  christos 
   1371      1.2  christos static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
   1372      1.2  christos 					    const u8 *sa, const u8 *data,
   1373      1.2  christos 					    int len)
   1374      1.2  christos {
   1375      1.2  christos 	const u8 *pos, *end, *next;
   1376      1.2  christos 	u8 ie, ie_len;
   1377      1.2  christos 
   1378      1.2  christos 	pos = data;
   1379      1.2  christos 	end = data + len;
   1380      1.2  christos 
   1381      1.3  christos 	while (end - pos > 1) {
   1382      1.2  christos 		ie = *pos++;
   1383      1.2  christos 		ie_len = *pos++;
   1384      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
   1385      1.2  christos 			   ie, ie_len);
   1386      1.2  christos 		if (ie_len > end - pos) {
   1387      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
   1388      1.2  christos 				   "subelement");
   1389      1.2  christos 			break;
   1390      1.2  christos 		}
   1391      1.2  christos 		next = pos + ie_len;
   1392      1.2  christos 		if (ie_len < 4) {
   1393      1.2  christos 			pos = next;
   1394      1.2  christos 			continue;
   1395      1.2  christos 		}
   1396      1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
   1397      1.2  christos 			   WPA_GET_BE24(pos), pos[3]);
   1398      1.2  christos 
   1399      1.2  christos #ifdef CONFIG_HS20
   1400      1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
   1401      1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
   1402      1.2  christos 		    pos[3] == HS20_WNM_SUB_REM_NEEDED) {
   1403      1.2  christos 			/* Subscription Remediation subelement */
   1404      1.2  christos 			const u8 *ie_end;
   1405      1.2  christos 			u8 url_len;
   1406      1.2  christos 			char *url;
   1407      1.2  christos 			u8 osu_method;
   1408      1.2  christos 
   1409      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
   1410      1.2  christos 				   "subelement");
   1411      1.2  christos 			ie_end = pos + ie_len;
   1412      1.2  christos 			pos += 4;
   1413      1.2  christos 			url_len = *pos++;
   1414      1.2  christos 			if (url_len == 0) {
   1415      1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
   1416      1.2  christos 				url = NULL;
   1417      1.2  christos 				osu_method = 1;
   1418      1.2  christos 			} else {
   1419      1.3  christos 				if (url_len + 1 > ie_end - pos) {
   1420      1.2  christos 					wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
   1421      1.2  christos 						   url_len,
   1422      1.2  christos 						   (int) (ie_end - pos));
   1423      1.2  christos 					break;
   1424      1.2  christos 				}
   1425      1.2  christos 				url = os_malloc(url_len + 1);
   1426      1.2  christos 				if (url == NULL)
   1427      1.2  christos 					break;
   1428      1.2  christos 				os_memcpy(url, pos, url_len);
   1429      1.2  christos 				url[url_len] = '\0';
   1430      1.2  christos 				osu_method = pos[url_len];
   1431      1.2  christos 			}
   1432      1.2  christos 			hs20_rx_subscription_remediation(wpa_s, url,
   1433      1.2  christos 							 osu_method);
   1434      1.2  christos 			os_free(url);
   1435      1.2  christos 			pos = next;
   1436      1.2  christos 			continue;
   1437      1.2  christos 		}
   1438      1.2  christos 
   1439      1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
   1440      1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
   1441      1.2  christos 		    pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
   1442      1.2  christos 			const u8 *ie_end;
   1443      1.2  christos 			u8 url_len;
   1444      1.2  christos 			char *url;
   1445      1.2  christos 			u8 code;
   1446      1.2  christos 			u16 reauth_delay;
   1447      1.2  christos 
   1448      1.2  christos 			ie_end = pos + ie_len;
   1449      1.2  christos 			pos += 4;
   1450      1.2  christos 			code = *pos++;
   1451      1.2  christos 			reauth_delay = WPA_GET_LE16(pos);
   1452      1.2  christos 			pos += 2;
   1453      1.2  christos 			url_len = *pos++;
   1454      1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
   1455      1.2  christos 				   "Imminent - Reason Code %u   "
   1456      1.2  christos 				   "Re-Auth Delay %u  URL Length %u",
   1457      1.2  christos 				   code, reauth_delay, url_len);
   1458      1.3  christos 			if (url_len > ie_end - pos)
   1459      1.2  christos 				break;
   1460      1.2  christos 			url = os_malloc(url_len + 1);
   1461      1.2  christos 			if (url == NULL)
   1462      1.2  christos 				break;
   1463      1.2  christos 			os_memcpy(url, pos, url_len);
   1464      1.2  christos 			url[url_len] = '\0';
   1465      1.2  christos 			hs20_rx_deauth_imminent_notice(wpa_s, code,
   1466      1.2  christos 						       reauth_delay, url);
   1467      1.2  christos 			os_free(url);
   1468      1.2  christos 			pos = next;
   1469      1.2  christos 			continue;
   1470      1.2  christos 		}
   1471      1.2  christos #endif /* CONFIG_HS20 */
   1472      1.2  christos 
   1473      1.2  christos 		pos = next;
   1474      1.2  christos 	}
   1475      1.2  christos }
   1476      1.2  christos 
   1477      1.2  christos 
   1478      1.2  christos static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
   1479      1.2  christos 					const u8 *sa, const u8 *frm, int len)
   1480      1.2  christos {
   1481      1.2  christos 	const u8 *pos, *end;
   1482      1.2  christos 	u8 dialog_token, type;
   1483      1.2  christos 
   1484      1.2  christos 	/* Dialog Token [1] | Type [1] | Subelements */
   1485      1.2  christos 
   1486      1.2  christos 	if (len < 2 || sa == NULL)
   1487      1.2  christos 		return;
   1488      1.2  christos 	end = frm + len;
   1489      1.2  christos 	pos = frm;
   1490      1.2  christos 	dialog_token = *pos++;
   1491      1.2  christos 	type = *pos++;
   1492      1.2  christos 
   1493      1.2  christos 	wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
   1494      1.2  christos 		"(dialog_token %u type %u sa " MACSTR ")",
   1495      1.2  christos 		dialog_token, type, MAC2STR(sa));
   1496      1.2  christos 	wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
   1497      1.2  christos 		    pos, end - pos);
   1498      1.2  christos 
   1499      1.2  christos 	if (wpa_s->wpa_state != WPA_COMPLETED ||
   1500      1.2  christos 	    os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
   1501      1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
   1502      1.2  christos 			"from our AP - ignore it");
   1503      1.2  christos 		return;
   1504      1.2  christos 	}
   1505      1.2  christos 
   1506      1.2  christos 	switch (type) {
   1507      1.2  christos 	case 1:
   1508      1.2  christos 		ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
   1509      1.2  christos 		break;
   1510      1.2  christos 	default:
   1511      1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
   1512      1.2  christos 			"WNM-Notification type %u", type);
   1513      1.2  christos 		break;
   1514      1.1  christos 	}
   1515      1.1  christos }
   1516      1.1  christos 
   1517      1.1  christos 
   1518      1.1  christos void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
   1519      1.2  christos 			      const struct ieee80211_mgmt *mgmt, size_t len)
   1520      1.1  christos {
   1521      1.1  christos 	const u8 *pos, *end;
   1522      1.1  christos 	u8 act;
   1523      1.1  christos 
   1524      1.2  christos 	if (len < IEEE80211_HDRLEN + 2)
   1525      1.1  christos 		return;
   1526      1.1  christos 
   1527      1.2  christos 	pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
   1528      1.1  christos 	act = *pos++;
   1529      1.2  christos 	end = ((const u8 *) mgmt) + len;
   1530      1.1  christos 
   1531      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
   1532      1.2  christos 		   act, MAC2STR(mgmt->sa));
   1533      1.1  christos 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
   1534      1.2  christos 	    os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
   1535      1.1  christos 		wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
   1536      1.1  christos 			   "frame");
   1537      1.1  christos 		return;
   1538      1.1  christos 	}
   1539      1.1  christos 
   1540      1.1  christos 	switch (act) {
   1541      1.1  christos 	case WNM_BSS_TRANS_MGMT_REQ:
   1542      1.1  christos 		ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
   1543      1.2  christos 						 !(mgmt->da[0] & 0x01));
   1544      1.1  christos 		break;
   1545      1.1  christos 	case WNM_SLEEP_MODE_RESP:
   1546      1.2  christos 		ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
   1547      1.2  christos 		break;
   1548      1.2  christos 	case WNM_NOTIFICATION_REQ:
   1549      1.2  christos 		ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
   1550      1.1  christos 		break;
   1551      1.1  christos 	default:
   1552      1.2  christos 		wpa_printf(MSG_ERROR, "WNM: Unknown request");
   1553      1.1  christos 		break;
   1554      1.1  christos 	}
   1555      1.1  christos }
   1556