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