1 /*
2  * hostapd - WNM
3  * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/ocv.h"
16 #include "ap/hostapd.h"
17 #include "ap/sta_info.h"
18 #include "ap/ap_config.h"
19 #include "ap/ap_drv_ops.h"
20 #include "ap/wpa_auth.h"
21 #include "mbo_ap.h"
22 #include "wnm_ap.h"
23 
24 #define MAX_TFS_IE_LEN  1024
25 
26 
27 /* get the TFS IE from driver */
ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr, u8 *buf, u16 *buf_len, enum wnm_oper oper)28 static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
29 				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
30 {
31 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
32 
33 	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
34 }
35 
36 
37 /* set the TFS IE to driver */
ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr, u8 *buf, u16 *buf_len, enum wnm_oper oper)38 static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
39 				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
40 {
41 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
42 
43 	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
44 }
45 
46 
47 /* MLME-SLEEPMODE.response */
ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd, const u8 *addr, u8 dialog_token, u8 action_type, u16 intval)48 static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
49 					 const u8 *addr, u8 dialog_token,
50 					 u8 action_type, u16 intval)
51 {
52 	struct ieee80211_mgmt *mgmt;
53 	int res;
54 	size_t len;
55 	size_t gtk_elem_len = 0;
56 	size_t igtk_elem_len = 0;
57 	size_t bigtk_elem_len = 0;
58 	struct wnm_sleep_element wnmsleep_ie;
59 	u8 *wnmtfs_ie, *oci_ie;
60 	u8 wnmsleep_ie_len, oci_ie_len;
61 	u16 wnmtfs_ie_len;
62 	u8 *pos;
63 	struct sta_info *sta;
64 	enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
65 		WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
66 
67 	sta = ap_get_sta(hapd, addr);
68 	if (sta == NULL) {
69 		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
70 		return -EINVAL;
71 	}
72 
73 	/* WNM-Sleep Mode IE */
74 	os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
75 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
76 	wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
77 	wnmsleep_ie.len = wnmsleep_ie_len - 2;
78 	wnmsleep_ie.action_type = action_type;
79 	wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
80 	wnmsleep_ie.intval = host_to_le16(intval);
81 
82 	/* TFS IE(s) */
83 	wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
84 	if (wnmtfs_ie == NULL)
85 		return -1;
86 	if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
87 				    tfs_oper)) {
88 		wnmtfs_ie_len = 0;
89 		os_free(wnmtfs_ie);
90 		wnmtfs_ie = NULL;
91 	}
92 
93 	oci_ie = NULL;
94 	oci_ie_len = 0;
95 #ifdef CONFIG_OCV
96 	if (action_type == WNM_SLEEP_MODE_EXIT &&
97 	    wpa_auth_uses_ocv(sta->wpa_sm)) {
98 		struct wpa_channel_info ci;
99 
100 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
101 			wpa_printf(MSG_WARNING,
102 				   "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
103 			os_free(wnmtfs_ie);
104 			return -1;
105 		}
106 #ifdef CONFIG_TESTING_OPTIONS
107 		if (hapd->conf->oci_freq_override_wnm_sleep) {
108 			wpa_printf(MSG_INFO,
109 				   "TEST: Override OCI frequency %d -> %u MHz",
110 				   ci.frequency,
111 				   hapd->conf->oci_freq_override_wnm_sleep);
112 			ci.frequency = hapd->conf->oci_freq_override_wnm_sleep;
113 		}
114 #endif /* CONFIG_TESTING_OPTIONS */
115 
116 		oci_ie_len = OCV_OCI_EXTENDED_LEN;
117 		oci_ie = os_zalloc(oci_ie_len);
118 		if (!oci_ie) {
119 			wpa_printf(MSG_WARNING,
120 				   "Failed to allocate buffer for OCI element in WNM-Sleep Mode frame");
121 			os_free(wnmtfs_ie);
122 			return -1;
123 		}
124 
125 		if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
126 			os_free(wnmtfs_ie);
127 			os_free(oci_ie);
128 			return -1;
129 		}
130 	}
131 #endif /* CONFIG_OCV */
132 
133 #define MAX_GTK_SUBELEM_LEN 45
134 #define MAX_IGTK_SUBELEM_LEN 26
135 #define MAX_BIGTK_SUBELEM_LEN 26
136 	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
137 			 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN +
138 			 MAX_BIGTK_SUBELEM_LEN +
139 			 oci_ie_len);
140 	if (mgmt == NULL) {
141 		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
142 			   "WNM-Sleep Response action frame");
143 		res = -1;
144 		goto fail;
145 	}
146 	os_memcpy(mgmt->da, addr, ETH_ALEN);
147 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
148 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
149 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
150 					   WLAN_FC_STYPE_ACTION);
151 	mgmt->u.action.category = WLAN_ACTION_WNM;
152 	mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
153 	mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
154 	pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
155 	/* add key data if MFP is enabled */
156 	if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
157 	    hapd->conf->wnm_sleep_mode_no_keys ||
158 	    action_type != WNM_SLEEP_MODE_EXIT) {
159 		mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
160 	} else {
161 		gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
162 		pos += gtk_elem_len;
163 		wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
164 			   (int) gtk_elem_len);
165 		res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
166 		if (res < 0)
167 			goto fail;
168 		igtk_elem_len = res;
169 		pos += igtk_elem_len;
170 		wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
171 			   (int) igtk_elem_len);
172 		if (hapd->conf->beacon_prot &&
173 		    (hapd->iface->drv_flags &
174 		     WPA_DRIVER_FLAGS_BEACON_PROTECTION)) {
175 			res = wpa_wnmsleep_bigtk_subelem(sta->wpa_sm, pos);
176 			if (res < 0)
177 				goto fail;
178 			bigtk_elem_len = res;
179 			pos += bigtk_elem_len;
180 			wpa_printf(MSG_DEBUG, "Pass 4 bigtk_len = %d",
181 				   (int) bigtk_elem_len);
182 		}
183 
184 		WPA_PUT_LE16((u8 *)
185 			     &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
186 			     gtk_elem_len + igtk_elem_len + bigtk_elem_len);
187 	}
188 	os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
189 	/* copy TFS IE here */
190 	pos += wnmsleep_ie_len;
191 	if (wnmtfs_ie) {
192 		os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
193 		pos += wnmtfs_ie_len;
194 	}
195 #ifdef CONFIG_OCV
196 	/* copy OCV OCI here */
197 	if (oci_ie_len > 0)
198 		os_memcpy(pos, oci_ie, oci_ie_len);
199 #endif /* CONFIG_OCV */
200 
201 	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
202 		igtk_elem_len + bigtk_elem_len +
203 		wnmsleep_ie_len + wnmtfs_ie_len + oci_ie_len;
204 
205 	/* In driver, response frame should be forced to sent when STA is in
206 	 * PS mode */
207 	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
208 				      mgmt->da, &mgmt->u.action.category, len);
209 
210 	if (!res) {
211 		wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
212 			   "frame");
213 
214 		/* when entering wnmsleep
215 		 * 1. pause the node in driver
216 		 * 2. mark the node so that AP won't update GTK/IGTK/BIGTK
217 		 * during WNM Sleep
218 		 */
219 		if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
220 		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
221 			sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
222 			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
223 					     addr, NULL, NULL);
224 			wpa_set_wnmsleep(sta->wpa_sm, 1);
225 		}
226 		/* when exiting wnmsleep
227 		 * 1. unmark the node
228 		 * 2. start GTK/IGTK/BIGTK update if MFP is not used
229 		 * 3. unpause the node in driver
230 		 */
231 		if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
232 		     wnmsleep_ie.status ==
233 		     WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
234 		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
235 			sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
236 			wpa_set_wnmsleep(sta->wpa_sm, 0);
237 			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
238 					     addr, NULL, NULL);
239 			if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
240 			    hapd->conf->wnm_sleep_mode_no_keys)
241 				wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
242 		}
243 	} else
244 		wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
245 
246 #undef MAX_GTK_SUBELEM_LEN
247 #undef MAX_IGTK_SUBELEM_LEN
248 #undef MAX_BIGTK_SUBELEM_LEN
249 fail:
250 	os_free(wnmtfs_ie);
251 	os_free(oci_ie);
252 	os_free(mgmt);
253 	return res;
254 }
255 
256 
ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd, const u8 *addr, const u8 *frm, int len)257 static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
258 				       const u8 *addr, const u8 *frm, int len)
259 {
260 	/* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
261 	const u8 *pos = frm;
262 	u8 dialog_token;
263 	struct wnm_sleep_element *wnmsleep_ie = NULL;
264 	/* multiple TFS Req IE (assuming consecutive) */
265 	u8 *tfsreq_ie_start = NULL;
266 	u8 *tfsreq_ie_end = NULL;
267 	u16 tfsreq_ie_len = 0;
268 #ifdef CONFIG_OCV
269 	struct sta_info *sta;
270 	const u8 *oci_ie = NULL;
271 	u8 oci_ie_len = 0;
272 #endif /* CONFIG_OCV */
273 
274 	if (!hapd->conf->wnm_sleep_mode) {
275 		wpa_printf(MSG_DEBUG, "Ignore WNM-Sleep Mode Request from "
276 			   MACSTR_SEC " since WNM-Sleep Mode is disabled",
277 			   MAC2STR_SEC(addr));
278 		return;
279 	}
280 
281 	if (len < 1) {
282 		wpa_printf(MSG_DEBUG,
283 			   "WNM: Ignore too short WNM-Sleep Mode Request from "
284 			   MACSTR_SEC, MAC2STR_SEC(addr));
285 		return;
286 	}
287 
288 	dialog_token = *pos++;
289 	while (pos + 1 < frm + len) {
290 		u8 ie_len = pos[1];
291 		if (pos + 2 + ie_len > frm + len)
292 			break;
293 		if (*pos == WLAN_EID_WNMSLEEP &&
294 		    ie_len >= (int) sizeof(*wnmsleep_ie) - 2)
295 			wnmsleep_ie = (struct wnm_sleep_element *) pos;
296 		else if (*pos == WLAN_EID_TFS_REQ) {
297 			if (!tfsreq_ie_start)
298 				tfsreq_ie_start = (u8 *) pos;
299 			tfsreq_ie_end = (u8 *) pos;
300 #ifdef CONFIG_OCV
301 		} else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
302 			   pos[2] == WLAN_EID_EXT_OCV_OCI) {
303 			oci_ie = pos + 3;
304 			oci_ie_len = ie_len - 1;
305 #endif /* CONFIG_OCV */
306 		} else
307 			wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
308 				   *pos);
309 		pos += ie_len + 2;
310 	}
311 
312 	if (!wnmsleep_ie) {
313 		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
314 		return;
315 	}
316 
317 #ifdef CONFIG_OCV
318 	sta = ap_get_sta(hapd, addr);
319 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
320 	    sta && wpa_auth_uses_ocv(sta->wpa_sm)) {
321 		struct wpa_channel_info ci;
322 
323 		if (hostapd_drv_channel_info(hapd, &ci) != 0) {
324 			wpa_printf(MSG_WARNING,
325 				   "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
326 			return;
327 		}
328 
329 		if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
330 					 channel_width_to_int(ci.chanwidth),
331 					 ci.seg1_idx) != OCI_SUCCESS) {
332 			wpa_msg(hapd, MSG_WARNING, "WNM: OCV failed: %s",
333 				ocv_errorstr);
334 			return;
335 		}
336 	}
337 #endif /* CONFIG_OCV */
338 
339 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
340 	    tfsreq_ie_start && tfsreq_ie_end &&
341 	    tfsreq_ie_end - tfsreq_ie_start >= 0) {
342 		tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
343 			tfsreq_ie_start;
344 		wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
345 		/* pass the TFS Req IE(s) to driver for processing */
346 		if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
347 					    &tfsreq_ie_len,
348 					    WNM_SLEEP_TFS_REQ_IE_SET))
349 			wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
350 	}
351 
352 	ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
353 				      wnmsleep_ie->action_type,
354 				      le_to_host16(wnmsleep_ie->intval));
355 
356 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
357 		/* clear the tfs after sending the resp frame */
358 		ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
359 					&tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
360 	}
361 }
362 
363 
ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd, const u8 *addr, u8 dialog_token)364 static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
365 						  const u8 *addr,
366 						  u8 dialog_token)
367 {
368 	struct ieee80211_mgmt *mgmt;
369 	size_t len;
370 	u8 *pos;
371 	int res;
372 
373 	mgmt = os_zalloc(sizeof(*mgmt));
374 	if (mgmt == NULL)
375 		return -1;
376 	os_memcpy(mgmt->da, addr, ETH_ALEN);
377 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
378 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
379 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
380 					   WLAN_FC_STYPE_ACTION);
381 	mgmt->u.action.category = WLAN_ACTION_WNM;
382 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
383 	mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
384 	mgmt->u.action.u.bss_tm_req.req_mode = 0;
385 	mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
386 	mgmt->u.action.u.bss_tm_req.validity_interval = 1;
387 	pos = mgmt->u.action.u.bss_tm_req.variable;
388 
389 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
390 		   MACSTR_SEC " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
391 		   "validity_interval=%u",
392 		   MAC2STR_SEC(addr), dialog_token,
393 		   mgmt->u.action.u.bss_tm_req.req_mode,
394 		   le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
395 		   mgmt->u.action.u.bss_tm_req.validity_interval);
396 
397 	len = pos - &mgmt->u.action.category;
398 	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
399 				      mgmt->da, &mgmt->u.action.category, len);
400 	os_free(mgmt);
401 	return res;
402 }
403 
404 
ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd, const u8 *addr, const u8 *frm, size_t len)405 static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
406 					       const u8 *addr, const u8 *frm,
407 					       size_t len)
408 {
409 	u8 dialog_token, reason;
410 	const u8 *pos, *end;
411 	int enabled = hapd->conf->bss_transition;
412 
413 #ifdef CONFIG_MBO
414 	if (hapd->conf->mbo_enabled)
415 		enabled = 1;
416 #endif /* CONFIG_MBO */
417 	if (!enabled) {
418 		wpa_printf(MSG_DEBUG,
419 			   "Ignore BSS Transition Management Query from "
420 			   MACSTR_SEC
421 			   " since BSS Transition Management is disabled",
422 			   MAC2STR_SEC(addr));
423 		return;
424 	}
425 
426 	if (len < 2) {
427 		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
428 			   MACSTR_SEC, MAC2STR_SEC(addr));
429 		return;
430 	}
431 
432 	pos = frm;
433 	end = pos + len;
434 	dialog_token = *pos++;
435 	reason = *pos++;
436 
437 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
438 		   MACSTR_SEC " dialog_token=%u reason=%u",
439 		   MAC2STR_SEC(addr), dialog_token, reason);
440 
441 	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
442 		    pos, end - pos);
443 
444 	ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token);
445 }
446 
447 
ap_sta_reset_steer_flag_timer(void *eloop_ctx, void *timeout_ctx)448 void ap_sta_reset_steer_flag_timer(void *eloop_ctx, void *timeout_ctx)
449 {
450 	struct hostapd_data *hapd = eloop_ctx;
451 	struct sta_info *sta = timeout_ctx;
452 
453 	if (sta->agreed_to_steer) {
454 		wpa_printf(MSG_DEBUG, "%s: Reset steering flag for STA " MACSTR_SEC,
455 			   hapd->conf->iface, MAC2STR_SEC(sta->addr));
456 		sta->agreed_to_steer = 0;
457 	}
458 }
459 
460 
ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd, const u8 *addr, const u8 *frm, size_t len)461 static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
462 					      const u8 *addr, const u8 *frm,
463 					      size_t len)
464 {
465 	u8 dialog_token, status_code, bss_termination_delay;
466 	const u8 *pos, *end;
467 	int enabled = hapd->conf->bss_transition;
468 	struct sta_info *sta;
469 
470 #ifdef CONFIG_MBO
471 	if (hapd->conf->mbo_enabled)
472 		enabled = 1;
473 #endif /* CONFIG_MBO */
474 	if (!enabled) {
475 		wpa_printf(MSG_DEBUG,
476 			   "Ignore BSS Transition Management Response from "
477 			   MACSTR_SEC
478 			   " since BSS Transition Management is disabled",
479 			   MAC2STR_SEC(addr));
480 		return;
481 	}
482 
483 	if (len < 3) {
484 		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
485 			   MACSTR_SEC, MAC2STR_SEC(addr));
486 		return;
487 	}
488 
489 	pos = frm;
490 	end = pos + len;
491 	dialog_token = *pos++;
492 	status_code = *pos++;
493 	bss_termination_delay = *pos++;
494 
495 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
496 		   MACSTR_SEC " dialog_token=%u status_code=%u "
497 		   "bss_termination_delay=%u", MAC2STR_SEC(addr), dialog_token,
498 		   status_code, bss_termination_delay);
499 
500 	sta = ap_get_sta(hapd, addr);
501 	if (!sta) {
502 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
503 			   " not found for received BSS TM Response",
504 			   MAC2STR_SEC(addr));
505 		return;
506 	}
507 
508 	if (status_code == WNM_BSS_TM_ACCEPT) {
509 		if (end - pos < ETH_ALEN) {
510 			wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
511 			return;
512 		}
513 		sta->agreed_to_steer = 1;
514 		eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
515 		eloop_register_timeout(2, 0, ap_sta_reset_steer_flag_timer,
516 				       hapd, sta);
517 		wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR_SEC,
518 			   MAC2STR_SEC(pos));
519 		wpa_msg_only_for_cb(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
520 			" status_code=%u bss_termination_delay=%u target_bssid="
521 			MACSTR,
522 			MAC2STR(addr), status_code, bss_termination_delay,
523 			MAC2STR(pos));
524 		wpa_printf(MSG_INFO, BSS_TM_RESP MACSTR_SEC
525 			" status_code=%u bss_termination_delay=%u target_bssid="
526 			MACSTR_SEC,
527 			MAC2STR_SEC(addr), status_code, bss_termination_delay,
528 			MAC2STR_SEC(pos));
529 		pos += ETH_ALEN;
530 	} else {
531 		sta->agreed_to_steer = 0;
532 		wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
533 			" status_code=%u bss_termination_delay=%u",
534 			MAC2STR(addr), status_code, bss_termination_delay);
535 	}
536 
537 	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
538 		    pos, end - pos);
539 }
540 
541 
wnm_beacon_protection_failure(struct hostapd_data *hapd, const u8 *addr)542 static void wnm_beacon_protection_failure(struct hostapd_data *hapd,
543 					  const u8 *addr)
544 {
545 	struct sta_info *sta;
546 
547 	if (!hapd->conf->beacon_prot ||
548 	    !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION))
549 		return;
550 
551 	sta = ap_get_sta(hapd, addr);
552 	if (!sta || !(sta->flags & WLAN_STA_AUTHORIZED)) {
553 		wpa_printf(MSG_DEBUG, "Station " MACSTR_SEC
554 			   " not found for received WNM-Notification Request",
555 			   MAC2STR_SEC(addr));
556 		return;
557 	}
558 
559 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
560 		       HOSTAPD_LEVEL_INFO,
561 		       "Beacon protection failure reported");
562 	wpa_msg(hapd->msg_ctx, MSG_INFO, WPA_EVENT_UNPROT_BEACON "reporter="
563 		MACSTR, MAC2STR(addr));
564 }
565 
566 
ieee802_11_rx_wnm_notification_req(struct hostapd_data *hapd, const u8 *addr, const u8 *buf, size_t len)567 static void ieee802_11_rx_wnm_notification_req(struct hostapd_data *hapd,
568 					       const u8 *addr, const u8 *buf,
569 					       size_t len)
570 {
571 	u8 dialog_token, type;
572 
573 	if (len < 2)
574 		return;
575 	dialog_token = *buf++;
576 	type = *buf++;
577 	len -= 2;
578 
579 	wpa_printf(MSG_DEBUG,
580 		   "WNM: Received WNM Notification Request frame from "
581 		   MACSTR_SEC " (dialog_token=%u type=%u)",
582 		   MAC2STR_SEC(addr), dialog_token, type);
583 	wpa_hexdump(MSG_MSGDUMP, "WNM: Notification Request subelements",
584 		    buf, len);
585 	switch (type) {
586 	case WNM_NOTIF_TYPE_BEACON_PROTECTION_FAILURE:
587 		wnm_beacon_protection_failure(hapd, addr);
588 		break;
589 	case WNM_NOTIF_TYPE_VENDOR_SPECIFIC:
590 		mbo_ap_wnm_notification_req(hapd, addr, buf, len);
591 		break;
592 	}
593 }
594 
595 
ieee802_11_rx_wnm_coloc_intf_report(struct hostapd_data *hapd, const u8 *addr, const u8 *buf, size_t len)596 static void ieee802_11_rx_wnm_coloc_intf_report(struct hostapd_data *hapd,
597 						const u8 *addr, const u8 *buf,
598 						size_t len)
599 {
600 	u8 dialog_token;
601 	char *hex;
602 	size_t hex_len;
603 
604 	if (!hapd->conf->coloc_intf_reporting) {
605 		wpa_printf(MSG_DEBUG,
606 			   "WNM: Ignore unexpected Collocated Interference Report from "
607 			   MACSTR_SEC, MAC2STR_SEC(addr));
608 		return;
609 	}
610 
611 	if (len < 1) {
612 		wpa_printf(MSG_DEBUG,
613 			   "WNM: Ignore too short Collocated Interference Report from "
614 			   MACSTR_SEC, MAC2STR_SEC(addr));
615 		return;
616 	}
617 	dialog_token = *buf++;
618 	len--;
619 
620 	wpa_printf(MSG_DEBUG,
621 		   "WNM: Received Collocated Interference Report frame from "
622 		   MACSTR_SEC " (dialog_token=%u)",
623 		   MAC2STR_SEC(addr), dialog_token);
624 	wpa_hexdump(MSG_MSGDUMP, "WNM: Collocated Interference Report Elements",
625 		    buf, len);
626 
627 	hex_len = 2 * len + 1;
628 	hex = os_malloc(hex_len);
629 	if (!hex)
630 		return;
631 	wpa_snprintf_hex(hex, hex_len, buf, len);
632 	wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO, COLOC_INTF_REPORT MACSTR " %d %s",
633 		MAC2STR(addr), dialog_token, hex);
634 	os_free(hex);
635 }
636 
637 
ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd, const struct ieee80211_mgmt *mgmt, size_t len)638 int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
639 				const struct ieee80211_mgmt *mgmt, size_t len)
640 {
641 	u8 action;
642 	const u8 *payload;
643 	size_t plen;
644 
645 	if (len < IEEE80211_HDRLEN + 2)
646 		return -1;
647 
648 	payload = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
649 	action = *payload++;
650 	plen = len - IEEE80211_HDRLEN - 2;
651 
652 	switch (action) {
653 	case WNM_BSS_TRANS_MGMT_QUERY:
654 		ieee802_11_rx_bss_trans_mgmt_query(hapd, mgmt->sa, payload,
655 						   plen);
656 		return 0;
657 	case WNM_BSS_TRANS_MGMT_RESP:
658 		ieee802_11_rx_bss_trans_mgmt_resp(hapd, mgmt->sa, payload,
659 						  plen);
660 		return 0;
661 	case WNM_SLEEP_MODE_REQ:
662 		ieee802_11_rx_wnmsleep_req(hapd, mgmt->sa, payload, plen);
663 		return 0;
664 	case WNM_NOTIFICATION_REQ:
665 		ieee802_11_rx_wnm_notification_req(hapd, mgmt->sa, payload,
666 						   plen);
667 		return 0;
668 	case WNM_COLLOCATED_INTERFERENCE_REPORT:
669 		ieee802_11_rx_wnm_coloc_intf_report(hapd, mgmt->sa, payload,
670 						    plen);
671 		return 0;
672 	}
673 
674 	wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR_SEC,
675 		   action, MAC2STR_SEC(mgmt->sa));
676 	return -1;
677 }
678 
679 
wnm_send_disassoc_imminent(struct hostapd_data *hapd, struct sta_info *sta, int disassoc_timer)680 int wnm_send_disassoc_imminent(struct hostapd_data *hapd,
681 			       struct sta_info *sta, int disassoc_timer)
682 {
683 	u8 buf[1000], *pos;
684 	struct ieee80211_mgmt *mgmt;
685 
686 	os_memset(buf, 0, sizeof(buf));
687 	mgmt = (struct ieee80211_mgmt *) buf;
688 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
689 					   WLAN_FC_STYPE_ACTION);
690 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
691 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
692 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
693 	mgmt->u.action.category = WLAN_ACTION_WNM;
694 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
695 	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
696 	mgmt->u.action.u.bss_tm_req.req_mode =
697 		WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
698 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
699 		host_to_le16(disassoc_timer);
700 	mgmt->u.action.u.bss_tm_req.validity_interval = 0;
701 
702 	pos = mgmt->u.action.u.bss_tm_req.variable;
703 
704 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request frame to indicate imminent disassociation (disassoc_timer=%d) to "
705 		   MACSTR_SEC, disassoc_timer, MAC2STR_SEC(sta->addr));
706 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
707 		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
708 			   "Management Request frame");
709 		return -1;
710 	}
711 
712 	return 0;
713 }
714 
715 
set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta, int disassoc_timer)716 static void set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta,
717 			       int disassoc_timer)
718 {
719 	int timeout, beacon_int;
720 
721 	/*
722 	 * Prevent STA from reconnecting using cached PMKSA to force
723 	 * full authentication with the authentication server (which may
724 	 * decide to reject the connection),
725 	 */
726 	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
727 
728 	beacon_int = hapd->iconf->beacon_int;
729 	if (beacon_int < 1)
730 		beacon_int = 100; /* best guess */
731 	/* Calculate timeout in ms based on beacon_int in TU */
732 	timeout = disassoc_timer * beacon_int * 128 / 125;
733 	wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR_SEC
734 		   " set to %d ms", MAC2STR_SEC(sta->addr), timeout);
735 
736 	sta->timeout_next = STA_DISASSOC_FROM_CLI;
737 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
738 	eloop_register_timeout(timeout / 1000,
739 			       timeout % 1000 * 1000,
740 			       ap_handle_timer, hapd, sta);
741 }
742 
743 
wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd, struct sta_info *sta, const char *url, int disassoc_timer)744 int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
745 				   struct sta_info *sta, const char *url,
746 				   int disassoc_timer)
747 {
748 	u8 buf[1000], *pos;
749 	struct ieee80211_mgmt *mgmt;
750 	size_t url_len;
751 
752 	os_memset(buf, 0, sizeof(buf));
753 	mgmt = (struct ieee80211_mgmt *) buf;
754 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
755 					   WLAN_FC_STYPE_ACTION);
756 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
757 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
758 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
759 	mgmt->u.action.category = WLAN_ACTION_WNM;
760 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
761 	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
762 	mgmt->u.action.u.bss_tm_req.req_mode =
763 		WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
764 		WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
765 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
766 		host_to_le16(disassoc_timer);
767 	mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
768 
769 	pos = mgmt->u.action.u.bss_tm_req.variable;
770 
771 	/* Session Information URL */
772 	url_len = os_strlen(url);
773 	if (url_len > 255)
774 		return -1;
775 	*pos++ = url_len;
776 	os_memcpy(pos, url, url_len);
777 	pos += url_len;
778 
779 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
780 		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
781 			   "Management Request frame");
782 		return -1;
783 	}
784 
785 	if (disassoc_timer) {
786 		/* send disassociation frame after time-out */
787 		set_disassoc_timer(hapd, sta, disassoc_timer);
788 	}
789 
790 	return 0;
791 }
792 
793 
wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta, u8 req_mode, int disassoc_timer, u8 valid_int, const u8 *bss_term_dur, u8 dialog_token, const char *url, const u8 *nei_rep, size_t nei_rep_len, const u8 *mbo_attrs, size_t mbo_len)794 int wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta,
795 			u8 req_mode, int disassoc_timer, u8 valid_int,
796 			const u8 *bss_term_dur, u8 dialog_token,
797 			const char *url, const u8 *nei_rep, size_t nei_rep_len,
798 			const u8 *mbo_attrs, size_t mbo_len)
799 {
800 	u8 *buf, *pos;
801 	struct ieee80211_mgmt *mgmt;
802 	size_t url_len;
803 
804 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
805 		   MACSTR_SEC
806 		   " req_mode=0x%x disassoc_timer=%d valid_int=0x%x dialog_token=%u",
807 		   MAC2STR_SEC(sta->addr), req_mode, disassoc_timer, valid_int,
808 		   dialog_token);
809 	buf = os_zalloc(1000 + nei_rep_len + mbo_len);
810 	if (buf == NULL)
811 		return -1;
812 	mgmt = (struct ieee80211_mgmt *) buf;
813 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
814 					   WLAN_FC_STYPE_ACTION);
815 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
816 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
817 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
818 	mgmt->u.action.category = WLAN_ACTION_WNM;
819 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
820 	mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
821 	mgmt->u.action.u.bss_tm_req.req_mode = req_mode;
822 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
823 		host_to_le16(disassoc_timer);
824 	mgmt->u.action.u.bss_tm_req.validity_interval = valid_int;
825 
826 	pos = mgmt->u.action.u.bss_tm_req.variable;
827 
828 	if ((req_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) &&
829 	    bss_term_dur) {
830 		os_memcpy(pos, bss_term_dur, 12);
831 		pos += 12;
832 	}
833 
834 	if (url) {
835 		/* Session Information URL */
836 		url_len = os_strlen(url);
837 		if (url_len > 255) {
838 			os_free(buf);
839 			return -1;
840 		}
841 
842 		*pos++ = url_len;
843 		os_memcpy(pos, url, url_len);
844 		pos += url_len;
845 	}
846 
847 	if (nei_rep) {
848 		os_memcpy(pos, nei_rep, nei_rep_len);
849 		pos += nei_rep_len;
850 	}
851 
852 	if (mbo_len > 0) {
853 		pos += mbo_add_ie(pos, buf + sizeof(buf) - pos, mbo_attrs,
854 				  mbo_len);
855 	}
856 
857 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
858 		wpa_printf(MSG_DEBUG,
859 			   "Failed to send BSS Transition Management Request frame");
860 		os_free(buf);
861 		return -1;
862 	}
863 	os_free(buf);
864 
865 	if (disassoc_timer) {
866 		/* send disassociation frame after time-out */
867 		set_disassoc_timer(hapd, sta, disassoc_timer);
868 	}
869 
870 	return 0;
871 }
872 
873 
wnm_send_coloc_intf_req(struct hostapd_data *hapd, struct sta_info *sta, unsigned int auto_report, unsigned int timeout)874 int wnm_send_coloc_intf_req(struct hostapd_data *hapd, struct sta_info *sta,
875 			    unsigned int auto_report, unsigned int timeout)
876 {
877 	u8 buf[100], *pos;
878 	struct ieee80211_mgmt *mgmt;
879 	u8 dialog_token = 1;
880 
881 	if (auto_report > 3 || timeout > 63)
882 		return -1;
883 	os_memset(buf, 0, sizeof(buf));
884 	mgmt = (struct ieee80211_mgmt *) buf;
885 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
886 					   WLAN_FC_STYPE_ACTION);
887 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
888 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
889 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
890 	mgmt->u.action.category = WLAN_ACTION_WNM;
891 	mgmt->u.action.u.coloc_intf_req.action =
892 		WNM_COLLOCATED_INTERFERENCE_REQ;
893 	mgmt->u.action.u.coloc_intf_req.dialog_token = dialog_token;
894 	mgmt->u.action.u.coloc_intf_req.req_info = auto_report | (timeout << 2);
895 	pos = &mgmt->u.action.u.coloc_intf_req.req_info;
896 	pos++;
897 
898 	wpa_printf(MSG_DEBUG, "WNM: Sending Collocated Interference Request to "
899 		   MACSTR_SEC " (dialog_token=%u auto_report=%u timeout=%u)",
900 		   MAC2STR_SEC(sta->addr), dialog_token, auto_report, timeout);
901 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0, NULL, 0, 0) < 0) {
902 		wpa_printf(MSG_DEBUG,
903 			   "WNM: Failed to send Collocated Interference Request frame");
904 		return -1;
905 	}
906 
907 	return 0;
908 }
909