Home | History | Annotate | Line # | Download | only in wpa_supplicant
wnm_sta.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos  * wpa_supplicant - WNM
      3  1.1.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.1.1.2  christos #include "common/wpa_ctrl.h"
     14      1.1  christos #include "rsn_supp/wpa.h"
     15      1.1  christos #include "wpa_supplicant_i.h"
     16      1.1  christos #include "driver_i.h"
     17      1.1  christos #include "scan.h"
     18  1.1.1.2  christos #include "ctrl_iface.h"
     19  1.1.1.2  christos #include "bss.h"
     20  1.1.1.2  christos #include "wnm_sta.h"
     21  1.1.1.2  christos #include "hs20_supplicant.h"
     22      1.1  christos 
     23      1.1  christos #define MAX_TFS_IE_LEN  1024
     24  1.1.1.2  christos #define WNM_MAX_NEIGHBOR_REPORT 10
     25      1.1  christos 
     26      1.1  christos 
     27      1.1  christos /* get the TFS IE from driver */
     28      1.1  christos static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
     29      1.1  christos 				   u16 *buf_len, enum wnm_oper oper)
     30      1.1  christos {
     31      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
     32      1.1  christos 
     33      1.1  christos 	return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
     34      1.1  christos }
     35      1.1  christos 
     36      1.1  christos 
     37      1.1  christos /* set the TFS IE to driver */
     38      1.1  christos static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
     39      1.1  christos 				   const u8 *addr, u8 *buf, u16 *buf_len,
     40      1.1  christos 				   enum wnm_oper oper)
     41      1.1  christos {
     42      1.1  christos 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
     43      1.1  christos 
     44      1.1  christos 	return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
     45      1.1  christos }
     46      1.1  christos 
     47      1.1  christos 
     48      1.1  christos /* MLME-SLEEPMODE.request */
     49      1.1  christos int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
     50      1.1  christos 				 u8 action, u16 intval, struct wpabuf *tfs_req)
     51      1.1  christos {
     52      1.1  christos 	struct ieee80211_mgmt *mgmt;
     53      1.1  christos 	int res;
     54      1.1  christos 	size_t len;
     55      1.1  christos 	struct wnm_sleep_element *wnmsleep_ie;
     56      1.1  christos 	u8 *wnmtfs_ie;
     57      1.1  christos 	u8 wnmsleep_ie_len;
     58      1.1  christos 	u16 wnmtfs_ie_len;  /* possibly multiple IE(s) */
     59      1.1  christos 	enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
     60      1.1  christos 		WNM_SLEEP_TFS_REQ_IE_NONE;
     61      1.1  christos 
     62      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
     63      1.1  christos 		   "action=%s to " MACSTR,
     64      1.1  christos 		   action == 0 ? "enter" : "exit",
     65      1.1  christos 		   MAC2STR(wpa_s->bssid));
     66      1.1  christos 
     67      1.1  christos 	/* WNM-Sleep Mode IE */
     68      1.1  christos 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
     69      1.1  christos 	wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
     70      1.1  christos 	if (wnmsleep_ie == NULL)
     71      1.1  christos 		return -1;
     72      1.1  christos 	wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
     73      1.1  christos 	wnmsleep_ie->len = wnmsleep_ie_len - 2;
     74      1.1  christos 	wnmsleep_ie->action_type = action;
     75      1.1  christos 	wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
     76      1.1  christos 	wnmsleep_ie->intval = host_to_le16(intval);
     77      1.1  christos 	wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
     78      1.1  christos 		    (u8 *) wnmsleep_ie, wnmsleep_ie_len);
     79      1.1  christos 
     80      1.1  christos 	/* TFS IE(s) */
     81      1.1  christos 	if (tfs_req) {
     82      1.1  christos 		wnmtfs_ie_len = wpabuf_len(tfs_req);
     83      1.1  christos 		wnmtfs_ie = os_malloc(wnmtfs_ie_len);
     84      1.1  christos 		if (wnmtfs_ie == NULL) {
     85      1.1  christos 			os_free(wnmsleep_ie);
     86      1.1  christos 			return -1;
     87      1.1  christos 		}
     88      1.1  christos 		os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
     89      1.1  christos 	} else {
     90      1.1  christos 		wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
     91      1.1  christos 		if (wnmtfs_ie == NULL) {
     92      1.1  christos 			os_free(wnmsleep_ie);
     93      1.1  christos 			return -1;
     94      1.1  christos 		}
     95      1.1  christos 		if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
     96      1.1  christos 					    tfs_oper)) {
     97      1.1  christos 			wnmtfs_ie_len = 0;
     98      1.1  christos 			os_free(wnmtfs_ie);
     99      1.1  christos 			wnmtfs_ie = NULL;
    100      1.1  christos 		}
    101      1.1  christos 	}
    102      1.1  christos 	wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
    103      1.1  christos 		    (u8 *) wnmtfs_ie, wnmtfs_ie_len);
    104      1.1  christos 
    105      1.1  christos 	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
    106      1.1  christos 	if (mgmt == NULL) {
    107      1.1  christos 		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
    108      1.1  christos 			   "WNM-Sleep Request action frame");
    109      1.1  christos 		os_free(wnmsleep_ie);
    110      1.1  christos 		os_free(wnmtfs_ie);
    111      1.1  christos 		return -1;
    112      1.1  christos 	}
    113      1.1  christos 
    114      1.1  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
    115      1.1  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
    116      1.1  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
    117      1.1  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    118      1.1  christos 					   WLAN_FC_STYPE_ACTION);
    119      1.1  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
    120      1.1  christos 	mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
    121      1.1  christos 	mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
    122      1.1  christos 	os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
    123      1.1  christos 		  wnmsleep_ie_len);
    124      1.1  christos 	/* copy TFS IE here */
    125      1.1  christos 	if (wnmtfs_ie_len > 0) {
    126      1.1  christos 		os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
    127      1.1  christos 			  wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
    128      1.1  christos 	}
    129      1.1  christos 
    130      1.1  christos 	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
    131      1.1  christos 		wnmtfs_ie_len;
    132      1.1  christos 
    133      1.1  christos 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    134      1.1  christos 				  wpa_s->own_addr, wpa_s->bssid,
    135      1.1  christos 				  &mgmt->u.action.category, len, 0);
    136      1.1  christos 	if (res < 0)
    137      1.1  christos 		wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
    138      1.1  christos 			   "(action=%d, intval=%d)", action, intval);
    139      1.1  christos 
    140      1.1  christos 	os_free(wnmsleep_ie);
    141      1.1  christos 	os_free(wnmtfs_ie);
    142      1.1  christos 	os_free(mgmt);
    143      1.1  christos 
    144      1.1  christos 	return res;
    145      1.1  christos }
    146      1.1  christos 
    147      1.1  christos 
    148      1.1  christos static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
    149      1.1  christos 					 u8 *tfsresp_ie_start,
    150      1.1  christos 					 u8 *tfsresp_ie_end)
    151      1.1  christos {
    152      1.1  christos 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
    153      1.1  christos 			 wpa_s->bssid, NULL, NULL);
    154      1.1  christos 	/* remove GTK/IGTK ?? */
    155      1.1  christos 
    156      1.1  christos 	/* set the TFS Resp IE(s) */
    157      1.1  christos 	if (tfsresp_ie_start && tfsresp_ie_end &&
    158      1.1  christos 	    tfsresp_ie_end - tfsresp_ie_start >= 0) {
    159      1.1  christos 		u16 tfsresp_ie_len;
    160      1.1  christos 		tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
    161      1.1  christos 			tfsresp_ie_start;
    162      1.1  christos 		wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
    163      1.1  christos 		/* pass the TFS Resp IE(s) to driver for processing */
    164      1.1  christos 		if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
    165      1.1  christos 					    tfsresp_ie_start,
    166      1.1  christos 					    &tfsresp_ie_len,
    167      1.1  christos 					    WNM_SLEEP_TFS_RESP_IE_SET))
    168      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
    169      1.1  christos 	}
    170      1.1  christos }
    171      1.1  christos 
    172      1.1  christos 
    173      1.1  christos static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
    174      1.1  christos 					const u8 *frm, u16 key_len_total)
    175      1.1  christos {
    176      1.1  christos 	u8 *ptr, *end;
    177      1.1  christos 	u8 gtk_len;
    178      1.1  christos 
    179      1.1  christos 	wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM,  wpa_s->bssid,
    180      1.1  christos 			 NULL, NULL);
    181      1.1  christos 
    182      1.1  christos 	/* Install GTK/IGTK */
    183      1.1  christos 
    184      1.1  christos 	/* point to key data field */
    185  1.1.1.2  christos 	ptr = (u8 *) frm + 1 + 2;
    186      1.1  christos 	end = ptr + key_len_total;
    187      1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
    188      1.1  christos 
    189      1.1  christos 	while (ptr + 1 < end) {
    190      1.1  christos 		if (ptr + 2 + ptr[1] > end) {
    191      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
    192      1.1  christos 				   "length");
    193      1.1  christos 			if (end > ptr) {
    194      1.1  christos 				wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
    195      1.1  christos 					    ptr, end - ptr);
    196      1.1  christos 			}
    197      1.1  christos 			break;
    198      1.1  christos 		}
    199      1.1  christos 		if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
    200      1.1  christos 			if (ptr[1] < 11 + 5) {
    201      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
    202      1.1  christos 					   "subelem");
    203      1.1  christos 				break;
    204      1.1  christos 			}
    205      1.1  christos 			gtk_len = *(ptr + 4);
    206      1.1  christos 			if (ptr[1] < 11 + gtk_len ||
    207      1.1  christos 			    gtk_len < 5 || gtk_len > 32) {
    208      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
    209      1.1  christos 					   "subelem");
    210      1.1  christos 				break;
    211      1.1  christos 			}
    212      1.1  christos 			wpa_wnmsleep_install_key(
    213      1.1  christos 				wpa_s->wpa,
    214      1.1  christos 				WNM_SLEEP_SUBELEM_GTK,
    215      1.1  christos 				ptr);
    216      1.1  christos 			ptr += 13 + gtk_len;
    217      1.1  christos #ifdef CONFIG_IEEE80211W
    218      1.1  christos 		} else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
    219      1.1  christos 			if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
    220      1.1  christos 				wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
    221      1.1  christos 					   "subelem");
    222      1.1  christos 				break;
    223      1.1  christos 			}
    224      1.1  christos 			wpa_wnmsleep_install_key(wpa_s->wpa,
    225      1.1  christos 						 WNM_SLEEP_SUBELEM_IGTK, ptr);
    226      1.1  christos 			ptr += 10 + WPA_IGTK_LEN;
    227      1.1  christos #endif /* CONFIG_IEEE80211W */
    228      1.1  christos 		} else
    229      1.1  christos 			break; /* skip the loop */
    230      1.1  christos 	}
    231      1.1  christos }
    232      1.1  christos 
    233      1.1  christos 
    234      1.1  christos static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
    235      1.1  christos 					const u8 *frm, int len)
    236      1.1  christos {
    237      1.1  christos 	/*
    238  1.1.1.2  christos 	 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
    239      1.1  christos 	 * WNM-Sleep Mode IE | TFS Response IE
    240      1.1  christos 	 */
    241  1.1.1.2  christos 	u8 *pos = (u8 *) frm; /* point to payload after the action field */
    242  1.1.1.2  christos 	u16 key_len_total;
    243      1.1  christos 	struct wnm_sleep_element *wnmsleep_ie = NULL;
    244      1.1  christos 	/* multiple TFS Resp IE (assuming consecutive) */
    245      1.1  christos 	u8 *tfsresp_ie_start = NULL;
    246      1.1  christos 	u8 *tfsresp_ie_end = NULL;
    247      1.1  christos 
    248  1.1.1.2  christos 	if (len < 3)
    249  1.1.1.2  christos 		return;
    250  1.1.1.2  christos 	key_len_total = WPA_GET_LE16(frm + 1);
    251  1.1.1.2  christos 
    252  1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
    253  1.1.1.2  christos 		   frm[0], key_len_total);
    254  1.1.1.2  christos 	pos += 3 + key_len_total;
    255      1.1  christos 	if (pos > frm + len) {
    256      1.1  christos 		wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
    257      1.1  christos 		return;
    258      1.1  christos 	}
    259      1.1  christos 	while (pos - frm < len) {
    260      1.1  christos 		u8 ie_len = *(pos + 1);
    261      1.1  christos 		if (pos + 2 + ie_len > frm + len) {
    262      1.1  christos 			wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
    263      1.1  christos 			break;
    264      1.1  christos 		}
    265      1.1  christos 		wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
    266      1.1  christos 		if (*pos == WLAN_EID_WNMSLEEP)
    267      1.1  christos 			wnmsleep_ie = (struct wnm_sleep_element *) pos;
    268      1.1  christos 		else if (*pos == WLAN_EID_TFS_RESP) {
    269      1.1  christos 			if (!tfsresp_ie_start)
    270      1.1  christos 				tfsresp_ie_start = pos;
    271      1.1  christos 			tfsresp_ie_end = pos;
    272      1.1  christos 		} else
    273      1.1  christos 			wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
    274      1.1  christos 		pos += ie_len + 2;
    275      1.1  christos 	}
    276      1.1  christos 
    277      1.1  christos 	if (!wnmsleep_ie) {
    278      1.1  christos 		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
    279      1.1  christos 		return;
    280      1.1  christos 	}
    281      1.1  christos 
    282      1.1  christos 	if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
    283      1.1  christos 	    wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
    284      1.1  christos 		wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
    285      1.1  christos 			   "frame (action=%d, intval=%d)",
    286      1.1  christos 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
    287      1.1  christos 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
    288      1.1  christos 			wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
    289      1.1  christos 						     tfsresp_ie_end);
    290      1.1  christos 		} else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
    291      1.1  christos 			wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
    292      1.1  christos 		}
    293      1.1  christos 	} else {
    294      1.1  christos 		wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
    295      1.1  christos 			   "(action=%d, intval=%d)",
    296      1.1  christos 			   wnmsleep_ie->action_type, wnmsleep_ie->intval);
    297      1.1  christos 		if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
    298      1.1  christos 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
    299      1.1  christos 					 wpa_s->bssid, NULL, NULL);
    300      1.1  christos 		else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
    301      1.1  christos 			wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
    302      1.1  christos 					 wpa_s->bssid, NULL, NULL);
    303      1.1  christos 	}
    304      1.1  christos }
    305      1.1  christos 
    306      1.1  christos 
    307  1.1.1.2  christos void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
    308  1.1.1.2  christos {
    309  1.1.1.2  christos 	int i;
    310  1.1.1.2  christos 
    311  1.1.1.2  christos 	for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
    312  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
    313  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
    314  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
    315  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
    316  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
    317  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
    318  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
    319  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
    320  1.1.1.2  christos 	}
    321  1.1.1.2  christos 
    322  1.1.1.2  christos 	wpa_s->wnm_num_neighbor_report = 0;
    323  1.1.1.2  christos 	os_free(wpa_s->wnm_neighbor_report_elements);
    324  1.1.1.2  christos 	wpa_s->wnm_neighbor_report_elements = NULL;
    325  1.1.1.2  christos }
    326  1.1.1.2  christos 
    327  1.1.1.2  christos 
    328  1.1.1.2  christos static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
    329  1.1.1.2  christos 					   u8 id, u8 elen, const u8 *pos)
    330  1.1.1.2  christos {
    331  1.1.1.2  christos 	switch (id) {
    332  1.1.1.2  christos 	case WNM_NEIGHBOR_TSF:
    333  1.1.1.2  christos 		if (elen < 2 + 2) {
    334  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
    335  1.1.1.2  christos 			break;
    336  1.1.1.2  christos 		}
    337  1.1.1.2  christos 		os_free(rep->tsf_info);
    338  1.1.1.2  christos 		rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
    339  1.1.1.2  christos 		if (rep->tsf_info == NULL)
    340  1.1.1.2  christos 			break;
    341  1.1.1.2  christos 		os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
    342  1.1.1.2  christos 		os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
    343  1.1.1.2  christos 		break;
    344  1.1.1.2  christos 	case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
    345  1.1.1.2  christos 		if (elen < 2) {
    346  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
    347  1.1.1.2  christos 				   "country string");
    348  1.1.1.2  christos 			break;
    349  1.1.1.2  christos 		}
    350  1.1.1.2  christos 		os_free(rep->con_coun_str);
    351  1.1.1.2  christos 		rep->con_coun_str =
    352  1.1.1.2  christos 			os_zalloc(sizeof(struct condensed_country_string));
    353  1.1.1.2  christos 		if (rep->con_coun_str == NULL)
    354  1.1.1.2  christos 			break;
    355  1.1.1.2  christos 		os_memcpy(rep->con_coun_str->country_string, pos, 2);
    356  1.1.1.2  christos 		break;
    357  1.1.1.2  christos 	case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
    358  1.1.1.2  christos 		if (elen < 1) {
    359  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
    360  1.1.1.2  christos 				   "candidate");
    361  1.1.1.2  christos 			break;
    362  1.1.1.2  christos 		}
    363  1.1.1.2  christos 		os_free(rep->bss_tran_can);
    364  1.1.1.2  christos 		rep->bss_tran_can =
    365  1.1.1.2  christos 			os_zalloc(sizeof(struct bss_transition_candidate));
    366  1.1.1.2  christos 		if (rep->bss_tran_can == NULL)
    367  1.1.1.2  christos 			break;
    368  1.1.1.2  christos 		rep->bss_tran_can->preference = pos[0];
    369  1.1.1.2  christos 		break;
    370  1.1.1.2  christos 	case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
    371  1.1.1.2  christos 		if (elen < 10) {
    372  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
    373  1.1.1.2  christos 				   "duration");
    374  1.1.1.2  christos 			break;
    375  1.1.1.2  christos 		}
    376  1.1.1.2  christos 		os_free(rep->bss_term_dur);
    377  1.1.1.2  christos 		rep->bss_term_dur =
    378  1.1.1.2  christos 			os_zalloc(sizeof(struct bss_termination_duration));
    379  1.1.1.2  christos 		if (rep->bss_term_dur == NULL)
    380  1.1.1.2  christos 			break;
    381  1.1.1.2  christos 		os_memcpy(rep->bss_term_dur->duration, pos, 10);
    382  1.1.1.2  christos 		break;
    383  1.1.1.2  christos 	case WNM_NEIGHBOR_BEARING:
    384  1.1.1.2  christos 		if (elen < 8) {
    385  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
    386  1.1.1.2  christos 				   "bearing");
    387  1.1.1.2  christos 			break;
    388  1.1.1.2  christos 		}
    389  1.1.1.2  christos 		os_free(rep->bearing);
    390  1.1.1.2  christos 		rep->bearing = os_zalloc(sizeof(struct bearing));
    391  1.1.1.2  christos 		if (rep->bearing == NULL)
    392  1.1.1.2  christos 			break;
    393  1.1.1.2  christos 		os_memcpy(rep->bearing->bearing, pos, 8);
    394  1.1.1.2  christos 		break;
    395  1.1.1.2  christos 	case WNM_NEIGHBOR_MEASUREMENT_PILOT:
    396  1.1.1.2  christos 		if (elen < 1) {
    397  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
    398  1.1.1.2  christos 				   "pilot");
    399  1.1.1.2  christos 			break;
    400  1.1.1.2  christos 		}
    401  1.1.1.2  christos 		os_free(rep->meas_pilot);
    402  1.1.1.2  christos 		rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
    403  1.1.1.2  christos 		if (rep->meas_pilot == NULL)
    404  1.1.1.2  christos 			break;
    405  1.1.1.2  christos 		rep->meas_pilot->measurement_pilot = pos[0];
    406  1.1.1.2  christos 		rep->meas_pilot->subelem_len = elen - 1;
    407  1.1.1.2  christos 		os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
    408  1.1.1.2  christos 		break;
    409  1.1.1.2  christos 	case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
    410  1.1.1.2  christos 		if (elen < 5) {
    411  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
    412  1.1.1.2  christos 				   "capabilities");
    413  1.1.1.2  christos 			break;
    414  1.1.1.2  christos 		}
    415  1.1.1.2  christos 		os_free(rep->rrm_cap);
    416  1.1.1.2  christos 		rep->rrm_cap =
    417  1.1.1.2  christos 			os_zalloc(sizeof(struct rrm_enabled_capabilities));
    418  1.1.1.2  christos 		if (rep->rrm_cap == NULL)
    419  1.1.1.2  christos 			break;
    420  1.1.1.2  christos 		os_memcpy(rep->rrm_cap->capabilities, pos, 5);
    421  1.1.1.2  christos 		break;
    422  1.1.1.2  christos 	case WNM_NEIGHBOR_MULTIPLE_BSSID:
    423  1.1.1.2  christos 		if (elen < 1) {
    424  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
    425  1.1.1.2  christos 			break;
    426  1.1.1.2  christos 		}
    427  1.1.1.2  christos 		os_free(rep->mul_bssid);
    428  1.1.1.2  christos 		rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
    429  1.1.1.2  christos 		if (rep->mul_bssid == NULL)
    430  1.1.1.2  christos 			break;
    431  1.1.1.2  christos 		rep->mul_bssid->max_bssid_indicator = pos[0];
    432  1.1.1.2  christos 		rep->mul_bssid->subelem_len = elen - 1;
    433  1.1.1.2  christos 		os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
    434  1.1.1.2  christos 		break;
    435  1.1.1.2  christos 	}
    436  1.1.1.2  christos }
    437  1.1.1.2  christos 
    438  1.1.1.2  christos 
    439  1.1.1.2  christos static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
    440  1.1.1.2  christos 				      const u8 *pos, u8 len,
    441  1.1.1.2  christos 				      struct neighbor_report *rep)
    442  1.1.1.2  christos {
    443  1.1.1.2  christos 	u8 left = len;
    444  1.1.1.2  christos 
    445  1.1.1.2  christos 	if (left < 13) {
    446  1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
    447  1.1.1.2  christos 		return;
    448  1.1.1.2  christos 	}
    449  1.1.1.2  christos 
    450  1.1.1.2  christos 	os_memcpy(rep->bssid, pos, ETH_ALEN);
    451  1.1.1.2  christos 	os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
    452  1.1.1.2  christos 	rep->regulatory_class = *(pos + 10);
    453  1.1.1.2  christos 	rep->channel_number = *(pos + 11);
    454  1.1.1.2  christos 	rep->phy_type = *(pos + 12);
    455  1.1.1.2  christos 
    456  1.1.1.2  christos 	pos += 13;
    457  1.1.1.2  christos 	left -= 13;
    458  1.1.1.2  christos 
    459  1.1.1.2  christos 	while (left >= 2) {
    460  1.1.1.2  christos 		u8 id, elen;
    461  1.1.1.2  christos 
    462  1.1.1.2  christos 		id = *pos++;
    463  1.1.1.2  christos 		elen = *pos++;
    464  1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
    465  1.1.1.2  christos 		left -= 2;
    466  1.1.1.2  christos 		if (elen > left) {
    467  1.1.1.2  christos 			wpa_printf(MSG_DEBUG,
    468  1.1.1.2  christos 				   "WNM: Truncated neighbor report subelement");
    469  1.1.1.2  christos 			break;
    470  1.1.1.2  christos 		}
    471  1.1.1.2  christos 		wnm_parse_neighbor_report_elem(rep, id, elen, pos);
    472  1.1.1.2  christos 		left -= elen;
    473  1.1.1.2  christos 		pos += elen;
    474  1.1.1.2  christos 	}
    475  1.1.1.2  christos }
    476  1.1.1.2  christos 
    477  1.1.1.2  christos 
    478  1.1.1.2  christos static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
    479  1.1.1.2  christos 					 struct wpa_scan_results *scan_res,
    480  1.1.1.2  christos 					 struct neighbor_report *neigh_rep,
    481  1.1.1.2  christos 					 u8 num_neigh_rep, u8 *bssid_to_connect)
    482  1.1.1.2  christos {
    483  1.1.1.2  christos 
    484  1.1.1.2  christos 	u8 i, j;
    485  1.1.1.2  christos 
    486  1.1.1.2  christos 	if (scan_res == NULL || num_neigh_rep == 0 || !wpa_s->current_bss)
    487  1.1.1.2  christos 		return 0;
    488  1.1.1.2  christos 
    489  1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
    490  1.1.1.2  christos 		   MAC2STR(wpa_s->bssid), wpa_s->current_bss->level);
    491  1.1.1.2  christos 
    492  1.1.1.2  christos 	for (i = 0; i < num_neigh_rep; i++) {
    493  1.1.1.2  christos 		for (j = 0; j < scan_res->num; j++) {
    494  1.1.1.2  christos 			/* Check for a better RSSI AP */
    495  1.1.1.2  christos 			if (os_memcmp(scan_res->res[j]->bssid,
    496  1.1.1.2  christos 				      neigh_rep[i].bssid, ETH_ALEN) == 0 &&
    497  1.1.1.2  christos 			    scan_res->res[j]->level >
    498  1.1.1.2  christos 			    wpa_s->current_bss->level) {
    499  1.1.1.2  christos 				/* Got a BSSID with better RSSI value */
    500  1.1.1.2  christos 				os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
    501  1.1.1.2  christos 					  ETH_ALEN);
    502  1.1.1.2  christos 				wpa_printf(MSG_DEBUG, "Found a BSS " MACSTR
    503  1.1.1.2  christos 					   " with better scan RSSI %d",
    504  1.1.1.2  christos 					   MAC2STR(scan_res->res[j]->bssid),
    505  1.1.1.2  christos 					   scan_res->res[j]->level);
    506  1.1.1.2  christos 				return 1;
    507  1.1.1.2  christos 			}
    508  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "scan_res[%d] " MACSTR
    509  1.1.1.2  christos 				   " RSSI %d", j,
    510  1.1.1.2  christos 				   MAC2STR(scan_res->res[j]->bssid),
    511  1.1.1.2  christos 				   scan_res->res[j]->level);
    512  1.1.1.2  christos 		}
    513  1.1.1.2  christos 	}
    514  1.1.1.2  christos 
    515  1.1.1.2  christos 	return 0;
    516  1.1.1.2  christos }
    517  1.1.1.2  christos 
    518  1.1.1.2  christos 
    519  1.1.1.2  christos static void wnm_send_bss_transition_mgmt_resp(
    520  1.1.1.2  christos 	struct wpa_supplicant *wpa_s, u8 dialog_token,
    521  1.1.1.2  christos 	enum bss_trans_mgmt_status_code status, u8 delay,
    522  1.1.1.2  christos 	const u8 *target_bssid)
    523      1.1  christos {
    524      1.1  christos 	u8 buf[1000], *pos;
    525      1.1  christos 	struct ieee80211_mgmt *mgmt;
    526      1.1  christos 	size_t len;
    527      1.1  christos 
    528      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
    529      1.1  christos 		   "to " MACSTR " dialog_token=%u status=%u delay=%d",
    530      1.1  christos 		   MAC2STR(wpa_s->bssid), dialog_token, status, delay);
    531      1.1  christos 
    532      1.1  christos 	mgmt = (struct ieee80211_mgmt *) buf;
    533      1.1  christos 	os_memset(&buf, 0, sizeof(buf));
    534      1.1  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
    535      1.1  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
    536      1.1  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
    537      1.1  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    538      1.1  christos 					   WLAN_FC_STYPE_ACTION);
    539      1.1  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
    540      1.1  christos 	mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
    541      1.1  christos 	mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
    542      1.1  christos 	mgmt->u.action.u.bss_tm_resp.status_code = status;
    543      1.1  christos 	mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
    544      1.1  christos 	pos = mgmt->u.action.u.bss_tm_resp.variable;
    545      1.1  christos 	if (target_bssid) {
    546      1.1  christos 		os_memcpy(pos, target_bssid, ETH_ALEN);
    547      1.1  christos 		pos += ETH_ALEN;
    548  1.1.1.2  christos 	} else if (status == WNM_BSS_TM_ACCEPT) {
    549  1.1.1.2  christos 		/*
    550  1.1.1.2  christos 		 * P802.11-REVmc clarifies that the Target BSSID field is always
    551  1.1.1.2  christos 		 * present when status code is zero, so use a fake value here if
    552  1.1.1.2  christos 		 * no BSSID is yet known.
    553  1.1.1.2  christos 		 */
    554  1.1.1.2  christos 		os_memset(pos, 0, ETH_ALEN);
    555  1.1.1.2  christos 		pos += ETH_ALEN;
    556      1.1  christos 	}
    557      1.1  christos 
    558      1.1  christos 	len = pos - (u8 *) &mgmt->u.action.category;
    559      1.1  christos 
    560      1.1  christos 	wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    561      1.1  christos 			    wpa_s->own_addr, wpa_s->bssid,
    562      1.1  christos 			    &mgmt->u.action.category, len, 0);
    563      1.1  christos }
    564      1.1  christos 
    565      1.1  christos 
    566  1.1.1.2  christos void wnm_scan_response(struct wpa_supplicant *wpa_s,
    567  1.1.1.2  christos 		       struct wpa_scan_results *scan_res)
    568  1.1.1.2  christos {
    569  1.1.1.2  christos 	u8 bssid[ETH_ALEN];
    570  1.1.1.2  christos 
    571  1.1.1.2  christos 	if (scan_res == NULL) {
    572  1.1.1.2  christos 		wpa_printf(MSG_ERROR, "Scan result is NULL");
    573  1.1.1.2  christos 		goto send_bss_resp_fail;
    574  1.1.1.2  christos 	}
    575  1.1.1.2  christos 
    576  1.1.1.2  christos 	/* Compare the Neighbor Report and scan results */
    577  1.1.1.2  christos 	if (compare_scan_neighbor_results(wpa_s, scan_res,
    578  1.1.1.2  christos 					  wpa_s->wnm_neighbor_report_elements,
    579  1.1.1.2  christos 					  wpa_s->wnm_num_neighbor_report,
    580  1.1.1.2  christos 					  bssid) == 1) {
    581  1.1.1.2  christos 		/* Associate to the network */
    582  1.1.1.2  christos 		struct wpa_bss *bss;
    583  1.1.1.2  christos 		struct wpa_ssid *ssid = wpa_s->current_ssid;
    584  1.1.1.2  christos 
    585  1.1.1.2  christos 		bss = wpa_bss_get_bssid(wpa_s, bssid);
    586  1.1.1.2  christos 		if (!bss) {
    587  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
    588  1.1.1.2  christos 				   "BSS table");
    589  1.1.1.2  christos 			goto send_bss_resp_fail;
    590  1.1.1.2  christos 		}
    591  1.1.1.2  christos 
    592  1.1.1.2  christos 		/* Send the BSS Management Response - Accept */
    593  1.1.1.2  christos 		if (wpa_s->wnm_reply) {
    594  1.1.1.2  christos 			wnm_send_bss_transition_mgmt_resp(wpa_s,
    595  1.1.1.2  christos 						  wpa_s->wnm_dialog_token,
    596  1.1.1.2  christos 						  WNM_BSS_TM_ACCEPT,
    597  1.1.1.2  christos 						  0, bssid);
    598  1.1.1.2  christos 		}
    599  1.1.1.2  christos 
    600  1.1.1.2  christos 		wpa_s->reassociate = 1;
    601  1.1.1.2  christos 		wpa_supplicant_connect(wpa_s, bss, ssid);
    602  1.1.1.2  christos 		wnm_deallocate_memory(wpa_s);
    603  1.1.1.2  christos 		return;
    604  1.1.1.2  christos 	}
    605  1.1.1.2  christos 
    606  1.1.1.2  christos 	/* Send reject response for all the failures */
    607  1.1.1.2  christos send_bss_resp_fail:
    608  1.1.1.2  christos 	wnm_deallocate_memory(wpa_s);
    609  1.1.1.2  christos 	if (wpa_s->wnm_reply) {
    610  1.1.1.2  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
    611  1.1.1.2  christos 						  wpa_s->wnm_dialog_token,
    612  1.1.1.2  christos 						  WNM_BSS_TM_REJECT_UNSPECIFIED,
    613  1.1.1.2  christos 						  0, NULL);
    614  1.1.1.2  christos 	}
    615  1.1.1.2  christos 	return;
    616  1.1.1.2  christos }
    617  1.1.1.2  christos 
    618  1.1.1.2  christos 
    619      1.1  christos static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
    620      1.1  christos 					     const u8 *pos, const u8 *end,
    621      1.1  christos 					     int reply)
    622      1.1  christos {
    623      1.1  christos 	if (pos + 5 > end)
    624      1.1  christos 		return;
    625      1.1  christos 
    626  1.1.1.2  christos 	wpa_s->wnm_dialog_token = pos[0];
    627  1.1.1.2  christos 	wpa_s->wnm_mode = pos[1];
    628  1.1.1.2  christos 	wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
    629  1.1.1.2  christos 	wpa_s->wnm_validity_interval = pos[4];
    630  1.1.1.2  christos 	wpa_s->wnm_reply = reply;
    631      1.1  christos 
    632      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
    633      1.1  christos 		   "dialog_token=%u request_mode=0x%x "
    634      1.1  christos 		   "disassoc_timer=%u validity_interval=%u",
    635  1.1.1.2  christos 		   wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
    636  1.1.1.2  christos 		   wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
    637  1.1.1.2  christos 
    638      1.1  christos 	pos += 5;
    639  1.1.1.2  christos 
    640  1.1.1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
    641  1.1.1.2  christos 		if (pos + 12 > end) {
    642  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
    643  1.1.1.2  christos 			return;
    644  1.1.1.2  christos 		}
    645  1.1.1.2  christos 		os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
    646      1.1  christos 		pos += 12; /* BSS Termination Duration */
    647  1.1.1.2  christos 	}
    648  1.1.1.2  christos 
    649  1.1.1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
    650      1.1  christos 		char url[256];
    651  1.1.1.2  christos 		unsigned int beacon_int;
    652  1.1.1.2  christos 
    653      1.1  christos 		if (pos + 1 > end || pos + 1 + pos[0] > end) {
    654      1.1  christos 			wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
    655      1.1  christos 				   "Management Request (URL)");
    656      1.1  christos 			return;
    657      1.1  christos 		}
    658      1.1  christos 		os_memcpy(url, pos + 1, pos[0]);
    659      1.1  christos 		url[pos[0]] = '\0';
    660  1.1.1.2  christos 		pos += 1 + pos[0];
    661  1.1.1.2  christos 
    662  1.1.1.2  christos 		if (wpa_s->current_bss)
    663  1.1.1.2  christos 			beacon_int = wpa_s->current_bss->beacon_int;
    664  1.1.1.2  christos 		else
    665  1.1.1.2  christos 			beacon_int = 100; /* best guess */
    666  1.1.1.2  christos 
    667  1.1.1.2  christos 		wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
    668  1.1.1.2  christos 			wpa_sm_pmf_enabled(wpa_s->wpa),
    669  1.1.1.2  christos 			wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
    670      1.1  christos 	}
    671      1.1  christos 
    672  1.1.1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
    673      1.1  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
    674  1.1.1.2  christos 			"Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
    675  1.1.1.2  christos 		if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
    676      1.1  christos 			/* TODO: mark current BSS less preferred for
    677      1.1  christos 			 * selection */
    678      1.1  christos 			wpa_printf(MSG_DEBUG, "Trying to find another BSS");
    679      1.1  christos 			wpa_supplicant_req_scan(wpa_s, 0, 0);
    680      1.1  christos 		}
    681      1.1  christos 	}
    682      1.1  christos 
    683  1.1.1.2  christos 	if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
    684  1.1.1.2  christos 		wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
    685  1.1.1.2  christos 		wpa_s->wnm_num_neighbor_report = 0;
    686  1.1.1.2  christos 		os_free(wpa_s->wnm_neighbor_report_elements);
    687  1.1.1.2  christos 		wpa_s->wnm_neighbor_report_elements = os_zalloc(
    688  1.1.1.2  christos 			WNM_MAX_NEIGHBOR_REPORT *
    689  1.1.1.2  christos 			sizeof(struct neighbor_report));
    690  1.1.1.2  christos 		if (wpa_s->wnm_neighbor_report_elements == NULL)
    691  1.1.1.2  christos 			return;
    692  1.1.1.2  christos 
    693  1.1.1.2  christos 		while (pos + 2 <= end &&
    694  1.1.1.2  christos 		       wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
    695  1.1.1.2  christos 		{
    696  1.1.1.2  christos 			u8 tag = *pos++;
    697  1.1.1.2  christos 			u8 len = *pos++;
    698  1.1.1.2  christos 
    699  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
    700  1.1.1.2  christos 				   tag);
    701  1.1.1.2  christos 			if (pos + len > end) {
    702  1.1.1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: Truncated request");
    703  1.1.1.2  christos 				return;
    704  1.1.1.2  christos 			}
    705  1.1.1.2  christos 			if (tag == WLAN_EID_NEIGHBOR_REPORT) {
    706  1.1.1.2  christos 				struct neighbor_report *rep;
    707  1.1.1.2  christos 				rep = &wpa_s->wnm_neighbor_report_elements[
    708  1.1.1.2  christos 					wpa_s->wnm_num_neighbor_report];
    709  1.1.1.2  christos 				wnm_parse_neighbor_report(wpa_s, pos, len, rep);
    710  1.1.1.2  christos 			}
    711  1.1.1.2  christos 
    712  1.1.1.2  christos 			pos += len;
    713  1.1.1.2  christos 			wpa_s->wnm_num_neighbor_report++;
    714  1.1.1.2  christos 		}
    715  1.1.1.2  christos 
    716  1.1.1.2  christos 		wpa_s->scan_res_handler = wnm_scan_response;
    717  1.1.1.2  christos 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    718  1.1.1.2  christos 	} else if (reply) {
    719  1.1.1.2  christos 		enum bss_trans_mgmt_status_code status;
    720  1.1.1.2  christos 		if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
    721  1.1.1.2  christos 			status = WNM_BSS_TM_ACCEPT;
    722  1.1.1.2  christos 		else {
    723  1.1.1.2  christos 			wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
    724  1.1.1.2  christos 			status = WNM_BSS_TM_REJECT_UNSPECIFIED;
    725  1.1.1.2  christos 		}
    726  1.1.1.2  christos 		wnm_send_bss_transition_mgmt_resp(wpa_s,
    727  1.1.1.2  christos 						  wpa_s->wnm_dialog_token,
    728  1.1.1.2  christos 						  status, 0, NULL);
    729  1.1.1.2  christos 	}
    730  1.1.1.2  christos }
    731  1.1.1.2  christos 
    732  1.1.1.2  christos 
    733  1.1.1.2  christos int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
    734  1.1.1.2  christos 				       u8 query_reason)
    735  1.1.1.2  christos {
    736  1.1.1.2  christos 	u8 buf[1000], *pos;
    737  1.1.1.2  christos 	struct ieee80211_mgmt *mgmt;
    738  1.1.1.2  christos 	size_t len;
    739  1.1.1.2  christos 	int ret;
    740  1.1.1.2  christos 
    741  1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
    742  1.1.1.2  christos 		   MACSTR " query_reason=%u",
    743  1.1.1.2  christos 		   MAC2STR(wpa_s->bssid), query_reason);
    744  1.1.1.2  christos 
    745  1.1.1.2  christos 	mgmt = (struct ieee80211_mgmt *) buf;
    746  1.1.1.2  christos 	os_memset(&buf, 0, sizeof(buf));
    747  1.1.1.2  christos 	os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
    748  1.1.1.2  christos 	os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
    749  1.1.1.2  christos 	os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
    750  1.1.1.2  christos 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    751  1.1.1.2  christos 					   WLAN_FC_STYPE_ACTION);
    752  1.1.1.2  christos 	mgmt->u.action.category = WLAN_ACTION_WNM;
    753  1.1.1.2  christos 	mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
    754  1.1.1.2  christos 	mgmt->u.action.u.bss_tm_query.dialog_token = 1;
    755  1.1.1.2  christos 	mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
    756  1.1.1.2  christos 	pos = mgmt->u.action.u.bss_tm_query.variable;
    757  1.1.1.2  christos 
    758  1.1.1.2  christos 	len = pos - (u8 *) &mgmt->u.action.category;
    759  1.1.1.2  christos 
    760  1.1.1.2  christos 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
    761  1.1.1.2  christos 				  wpa_s->own_addr, wpa_s->bssid,
    762  1.1.1.2  christos 				  &mgmt->u.action.category, len, 0);
    763  1.1.1.2  christos 
    764  1.1.1.2  christos 	return ret;
    765  1.1.1.2  christos }
    766  1.1.1.2  christos 
    767  1.1.1.2  christos 
    768  1.1.1.2  christos static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
    769  1.1.1.2  christos 					    const u8 *sa, const u8 *data,
    770  1.1.1.2  christos 					    int len)
    771  1.1.1.2  christos {
    772  1.1.1.2  christos 	const u8 *pos, *end, *next;
    773  1.1.1.2  christos 	u8 ie, ie_len;
    774  1.1.1.2  christos 
    775  1.1.1.2  christos 	pos = data;
    776  1.1.1.2  christos 	end = data + len;
    777  1.1.1.2  christos 
    778  1.1.1.2  christos 	while (pos + 1 < end) {
    779  1.1.1.2  christos 		ie = *pos++;
    780  1.1.1.2  christos 		ie_len = *pos++;
    781  1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
    782  1.1.1.2  christos 			   ie, ie_len);
    783  1.1.1.2  christos 		if (ie_len > end - pos) {
    784  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
    785  1.1.1.2  christos 				   "subelement");
    786  1.1.1.2  christos 			break;
    787  1.1.1.2  christos 		}
    788  1.1.1.2  christos 		next = pos + ie_len;
    789  1.1.1.2  christos 		if (ie_len < 4) {
    790  1.1.1.2  christos 			pos = next;
    791  1.1.1.2  christos 			continue;
    792  1.1.1.2  christos 		}
    793  1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
    794  1.1.1.2  christos 			   WPA_GET_BE24(pos), pos[3]);
    795  1.1.1.2  christos 
    796  1.1.1.2  christos #ifdef CONFIG_HS20
    797  1.1.1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
    798  1.1.1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
    799  1.1.1.2  christos 		    pos[3] == HS20_WNM_SUB_REM_NEEDED) {
    800  1.1.1.2  christos 			/* Subscription Remediation subelement */
    801  1.1.1.2  christos 			const u8 *ie_end;
    802  1.1.1.2  christos 			u8 url_len;
    803  1.1.1.2  christos 			char *url;
    804  1.1.1.2  christos 			u8 osu_method;
    805  1.1.1.2  christos 
    806  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
    807  1.1.1.2  christos 				   "subelement");
    808  1.1.1.2  christos 			ie_end = pos + ie_len;
    809  1.1.1.2  christos 			pos += 4;
    810  1.1.1.2  christos 			url_len = *pos++;
    811  1.1.1.2  christos 			if (url_len == 0) {
    812  1.1.1.2  christos 				wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
    813  1.1.1.2  christos 				url = NULL;
    814  1.1.1.2  christos 				osu_method = 1;
    815  1.1.1.2  christos 			} else {
    816  1.1.1.2  christos 				if (pos + url_len + 1 > ie_end) {
    817  1.1.1.2  christos 					wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
    818  1.1.1.2  christos 						   url_len,
    819  1.1.1.2  christos 						   (int) (ie_end - pos));
    820  1.1.1.2  christos 					break;
    821  1.1.1.2  christos 				}
    822  1.1.1.2  christos 				url = os_malloc(url_len + 1);
    823  1.1.1.2  christos 				if (url == NULL)
    824  1.1.1.2  christos 					break;
    825  1.1.1.2  christos 				os_memcpy(url, pos, url_len);
    826  1.1.1.2  christos 				url[url_len] = '\0';
    827  1.1.1.2  christos 				osu_method = pos[url_len];
    828  1.1.1.2  christos 			}
    829  1.1.1.2  christos 			hs20_rx_subscription_remediation(wpa_s, url,
    830  1.1.1.2  christos 							 osu_method);
    831  1.1.1.2  christos 			os_free(url);
    832  1.1.1.2  christos 			pos = next;
    833  1.1.1.2  christos 			continue;
    834  1.1.1.2  christos 		}
    835  1.1.1.2  christos 
    836  1.1.1.2  christos 		if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
    837  1.1.1.2  christos 		    WPA_GET_BE24(pos) == OUI_WFA &&
    838  1.1.1.2  christos 		    pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
    839  1.1.1.2  christos 			const u8 *ie_end;
    840  1.1.1.2  christos 			u8 url_len;
    841  1.1.1.2  christos 			char *url;
    842  1.1.1.2  christos 			u8 code;
    843  1.1.1.2  christos 			u16 reauth_delay;
    844  1.1.1.2  christos 
    845  1.1.1.2  christos 			ie_end = pos + ie_len;
    846  1.1.1.2  christos 			pos += 4;
    847  1.1.1.2  christos 			code = *pos++;
    848  1.1.1.2  christos 			reauth_delay = WPA_GET_LE16(pos);
    849  1.1.1.2  christos 			pos += 2;
    850  1.1.1.2  christos 			url_len = *pos++;
    851  1.1.1.2  christos 			wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
    852  1.1.1.2  christos 				   "Imminent - Reason Code %u   "
    853  1.1.1.2  christos 				   "Re-Auth Delay %u  URL Length %u",
    854  1.1.1.2  christos 				   code, reauth_delay, url_len);
    855  1.1.1.2  christos 			if (pos + url_len > ie_end)
    856  1.1.1.2  christos 				break;
    857  1.1.1.2  christos 			url = os_malloc(url_len + 1);
    858  1.1.1.2  christos 			if (url == NULL)
    859  1.1.1.2  christos 				break;
    860  1.1.1.2  christos 			os_memcpy(url, pos, url_len);
    861  1.1.1.2  christos 			url[url_len] = '\0';
    862  1.1.1.2  christos 			hs20_rx_deauth_imminent_notice(wpa_s, code,
    863  1.1.1.2  christos 						       reauth_delay, url);
    864  1.1.1.2  christos 			os_free(url);
    865  1.1.1.2  christos 			pos = next;
    866  1.1.1.2  christos 			continue;
    867  1.1.1.2  christos 		}
    868  1.1.1.2  christos #endif /* CONFIG_HS20 */
    869  1.1.1.2  christos 
    870  1.1.1.2  christos 		pos = next;
    871  1.1.1.2  christos 	}
    872  1.1.1.2  christos }
    873  1.1.1.2  christos 
    874  1.1.1.2  christos 
    875  1.1.1.2  christos static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
    876  1.1.1.2  christos 					const u8 *sa, const u8 *frm, int len)
    877  1.1.1.2  christos {
    878  1.1.1.2  christos 	const u8 *pos, *end;
    879  1.1.1.2  christos 	u8 dialog_token, type;
    880  1.1.1.2  christos 
    881  1.1.1.2  christos 	/* Dialog Token [1] | Type [1] | Subelements */
    882  1.1.1.2  christos 
    883  1.1.1.2  christos 	if (len < 2 || sa == NULL)
    884  1.1.1.2  christos 		return;
    885  1.1.1.2  christos 	end = frm + len;
    886  1.1.1.2  christos 	pos = frm;
    887  1.1.1.2  christos 	dialog_token = *pos++;
    888  1.1.1.2  christos 	type = *pos++;
    889  1.1.1.2  christos 
    890  1.1.1.2  christos 	wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
    891  1.1.1.2  christos 		"(dialog_token %u type %u sa " MACSTR ")",
    892  1.1.1.2  christos 		dialog_token, type, MAC2STR(sa));
    893  1.1.1.2  christos 	wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
    894  1.1.1.2  christos 		    pos, end - pos);
    895  1.1.1.2  christos 
    896  1.1.1.2  christos 	if (wpa_s->wpa_state != WPA_COMPLETED ||
    897  1.1.1.2  christos 	    os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
    898  1.1.1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
    899  1.1.1.2  christos 			"from our AP - ignore it");
    900  1.1.1.2  christos 		return;
    901  1.1.1.2  christos 	}
    902  1.1.1.2  christos 
    903  1.1.1.2  christos 	switch (type) {
    904  1.1.1.2  christos 	case 1:
    905  1.1.1.2  christos 		ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
    906  1.1.1.2  christos 		break;
    907  1.1.1.2  christos 	default:
    908  1.1.1.2  christos 		wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
    909  1.1.1.2  christos 			"WNM-Notification type %u", type);
    910  1.1.1.2  christos 		break;
    911      1.1  christos 	}
    912      1.1  christos }
    913      1.1  christos 
    914      1.1  christos 
    915      1.1  christos void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
    916  1.1.1.2  christos 			      const struct ieee80211_mgmt *mgmt, size_t len)
    917      1.1  christos {
    918      1.1  christos 	const u8 *pos, *end;
    919      1.1  christos 	u8 act;
    920      1.1  christos 
    921  1.1.1.2  christos 	if (len < IEEE80211_HDRLEN + 2)
    922      1.1  christos 		return;
    923      1.1  christos 
    924  1.1.1.2  christos 	pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
    925      1.1  christos 	act = *pos++;
    926  1.1.1.2  christos 	end = ((const u8 *) mgmt) + len;
    927      1.1  christos 
    928      1.1  christos 	wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
    929  1.1.1.2  christos 		   act, MAC2STR(mgmt->sa));
    930      1.1  christos 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
    931  1.1.1.2  christos 	    os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
    932      1.1  christos 		wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
    933      1.1  christos 			   "frame");
    934      1.1  christos 		return;
    935      1.1  christos 	}
    936      1.1  christos 
    937      1.1  christos 	switch (act) {
    938      1.1  christos 	case WNM_BSS_TRANS_MGMT_REQ:
    939      1.1  christos 		ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
    940  1.1.1.2  christos 						 !(mgmt->da[0] & 0x01));
    941      1.1  christos 		break;
    942      1.1  christos 	case WNM_SLEEP_MODE_RESP:
    943  1.1.1.2  christos 		ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
    944  1.1.1.2  christos 		break;
    945  1.1.1.2  christos 	case WNM_NOTIFICATION_REQ:
    946  1.1.1.2  christos 		ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
    947      1.1  christos 		break;
    948      1.1  christos 	default:
    949  1.1.1.2  christos 		wpa_printf(MSG_ERROR, "WNM: Unknown request");
    950      1.1  christos 		break;
    951      1.1  christos 	}
    952      1.1  christos }
    953