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