xref: /freebsd/contrib/wpa/src/ap/wnm_ap.c (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
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 "ap/hostapd.h"
16 #include "ap/sta_info.h"
17 #include "ap/ap_config.h"
18 #include "ap/ap_drv_ops.h"
19 #include "ap/wpa_auth.h"
20 #include "wnm_ap.h"
21 
22 #define MAX_TFS_IE_LEN  1024
23 
24 
25 /* get the TFS IE from driver */
26 static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
27 				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
28 {
29 	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
30 
31 	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
32 }
33 
34 
35 /* set the TFS IE to driver */
36 static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
37 				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
38 {
39 	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
40 
41 	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
42 }
43 
44 
45 /* MLME-SLEEPMODE.response */
46 static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
47 					 const u8 *addr, u8 dialog_token,
48 					 u8 action_type, u16 intval)
49 {
50 	struct ieee80211_mgmt *mgmt;
51 	int res;
52 	size_t len;
53 	size_t gtk_elem_len = 0;
54 	size_t igtk_elem_len = 0;
55 	struct wnm_sleep_element wnmsleep_ie;
56 	u8 *wnmtfs_ie;
57 	u8 wnmsleep_ie_len;
58 	u16 wnmtfs_ie_len;
59 	u8 *pos;
60 	struct sta_info *sta;
61 	enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
62 		WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
63 
64 	sta = ap_get_sta(hapd, addr);
65 	if (sta == NULL) {
66 		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
67 		return -EINVAL;
68 	}
69 
70 	/* WNM-Sleep Mode IE */
71 	os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
72 	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
73 	wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
74 	wnmsleep_ie.len = wnmsleep_ie_len - 2;
75 	wnmsleep_ie.action_type = action_type;
76 	wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
77 	wnmsleep_ie.intval = host_to_le16(intval);
78 
79 	/* TFS IE(s) */
80 	wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
81 	if (wnmtfs_ie == NULL)
82 		return -1;
83 	if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
84 				    tfs_oper)) {
85 		wnmtfs_ie_len = 0;
86 		os_free(wnmtfs_ie);
87 		wnmtfs_ie = NULL;
88 	}
89 
90 #define MAX_GTK_SUBELEM_LEN 45
91 #define MAX_IGTK_SUBELEM_LEN 26
92 	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
93 			 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN);
94 	if (mgmt == NULL) {
95 		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
96 			   "WNM-Sleep Response action frame");
97 		return -1;
98 	}
99 	os_memcpy(mgmt->da, addr, ETH_ALEN);
100 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
101 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
102 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
103 					   WLAN_FC_STYPE_ACTION);
104 	mgmt->u.action.category = WLAN_ACTION_WNM;
105 	mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
106 	mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
107 	pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
108 	/* add key data if MFP is enabled */
109 	if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
110 	    action_type != WNM_SLEEP_MODE_EXIT) {
111 		mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
112 	} else {
113 		gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
114 		pos += gtk_elem_len;
115 		wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
116 			   (int) gtk_elem_len);
117 #ifdef CONFIG_IEEE80211W
118 		res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
119 		if (res < 0) {
120 			os_free(wnmtfs_ie);
121 			os_free(mgmt);
122 			return -1;
123 		}
124 		igtk_elem_len = res;
125 		pos += igtk_elem_len;
126 		wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
127 			   (int) igtk_elem_len);
128 #endif /* CONFIG_IEEE80211W */
129 
130 		WPA_PUT_LE16((u8 *)
131 			     &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
132 			     gtk_elem_len + igtk_elem_len);
133 	}
134 	os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
135 	/* copy TFS IE here */
136 	pos += wnmsleep_ie_len;
137 	if (wnmtfs_ie)
138 		os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
139 
140 	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
141 		igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
142 
143 	/* In driver, response frame should be forced to sent when STA is in
144 	 * PS mode */
145 	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
146 				      mgmt->da, &mgmt->u.action.category, len);
147 
148 	if (!res) {
149 		wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
150 			   "frame");
151 
152 		/* when entering wnmsleep
153 		 * 1. pause the node in driver
154 		 * 2. mark the node so that AP won't update GTK/IGTK during
155 		 * WNM Sleep
156 		 */
157 		if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
158 		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
159 			sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
160 			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
161 					     addr, NULL, NULL);
162 			wpa_set_wnmsleep(sta->wpa_sm, 1);
163 		}
164 		/* when exiting wnmsleep
165 		 * 1. unmark the node
166 		 * 2. start GTK/IGTK update if MFP is not used
167 		 * 3. unpause the node in driver
168 		 */
169 		if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
170 		     wnmsleep_ie.status ==
171 		     WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
172 		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
173 			sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
174 			wpa_set_wnmsleep(sta->wpa_sm, 0);
175 			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
176 					     addr, NULL, NULL);
177 			if (!wpa_auth_uses_mfp(sta->wpa_sm))
178 				wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
179 		}
180 	} else
181 		wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
182 
183 #undef MAX_GTK_SUBELEM_LEN
184 #undef MAX_IGTK_SUBELEM_LEN
185 	os_free(wnmtfs_ie);
186 	os_free(mgmt);
187 	return res;
188 }
189 
190 
191 static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
192 				       const u8 *addr, const u8 *frm, int len)
193 {
194 	/* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
195 	const u8 *pos = frm;
196 	u8 dialog_token;
197 	struct wnm_sleep_element *wnmsleep_ie = NULL;
198 	/* multiple TFS Req IE (assuming consecutive) */
199 	u8 *tfsreq_ie_start = NULL;
200 	u8 *tfsreq_ie_end = NULL;
201 	u16 tfsreq_ie_len = 0;
202 
203 	dialog_token = *pos++;
204 	while (pos + 1 < frm + len) {
205 		u8 ie_len = pos[1];
206 		if (pos + 2 + ie_len > frm + len)
207 			break;
208 		if (*pos == WLAN_EID_WNMSLEEP)
209 			wnmsleep_ie = (struct wnm_sleep_element *) pos;
210 		else if (*pos == WLAN_EID_TFS_REQ) {
211 			if (!tfsreq_ie_start)
212 				tfsreq_ie_start = (u8 *) pos;
213 			tfsreq_ie_end = (u8 *) pos;
214 		} else
215 			wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
216 				   *pos);
217 		pos += ie_len + 2;
218 	}
219 
220 	if (!wnmsleep_ie) {
221 		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
222 		return;
223 	}
224 
225 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
226 	    tfsreq_ie_start && tfsreq_ie_end &&
227 	    tfsreq_ie_end - tfsreq_ie_start >= 0) {
228 		tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
229 			tfsreq_ie_start;
230 		wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
231 		/* pass the TFS Req IE(s) to driver for processing */
232 		if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
233 					    &tfsreq_ie_len,
234 					    WNM_SLEEP_TFS_REQ_IE_SET))
235 			wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
236 	}
237 
238 	ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
239 				      wnmsleep_ie->action_type,
240 				      le_to_host16(wnmsleep_ie->intval));
241 
242 	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
243 		/* clear the tfs after sending the resp frame */
244 		ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
245 					&tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
246 	}
247 }
248 
249 
250 static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
251 						  const u8 *addr,
252 						  u8 dialog_token,
253 						  const char *url)
254 {
255 	struct ieee80211_mgmt *mgmt;
256 	size_t url_len, len;
257 	u8 *pos;
258 	int res;
259 
260 	if (url)
261 		url_len = os_strlen(url);
262 	else
263 		url_len = 0;
264 
265 	mgmt = os_zalloc(sizeof(*mgmt) + (url_len ? 1 + url_len : 0));
266 	if (mgmt == NULL)
267 		return -1;
268 	os_memcpy(mgmt->da, addr, ETH_ALEN);
269 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
270 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
271 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
272 					   WLAN_FC_STYPE_ACTION);
273 	mgmt->u.action.category = WLAN_ACTION_WNM;
274 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
275 	mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
276 	mgmt->u.action.u.bss_tm_req.req_mode = 0;
277 	mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
278 	mgmt->u.action.u.bss_tm_req.validity_interval = 1;
279 	pos = mgmt->u.action.u.bss_tm_req.variable;
280 	if (url) {
281 		*pos++ += url_len;
282 		os_memcpy(pos, url, url_len);
283 		pos += url_len;
284 	}
285 
286 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
287 		   MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
288 		   "validity_interval=%u",
289 		   MAC2STR(addr), dialog_token,
290 		   mgmt->u.action.u.bss_tm_req.req_mode,
291 		   le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
292 		   mgmt->u.action.u.bss_tm_req.validity_interval);
293 
294 	len = pos - &mgmt->u.action.category;
295 	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
296 				      mgmt->da, &mgmt->u.action.category, len);
297 	os_free(mgmt);
298 	return res;
299 }
300 
301 
302 static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
303 					       const u8 *addr, const u8 *frm,
304 					       size_t len)
305 {
306 	u8 dialog_token, reason;
307 	const u8 *pos, *end;
308 
309 	if (len < 2) {
310 		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
311 			   MACSTR, MAC2STR(addr));
312 		return;
313 	}
314 
315 	pos = frm;
316 	end = pos + len;
317 	dialog_token = *pos++;
318 	reason = *pos++;
319 
320 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
321 		   MACSTR " dialog_token=%u reason=%u",
322 		   MAC2STR(addr), dialog_token, reason);
323 
324 	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
325 		    pos, end - pos);
326 
327 	ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token, NULL);
328 }
329 
330 
331 static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
332 					      const u8 *addr, const u8 *frm,
333 					      size_t len)
334 {
335 	u8 dialog_token, status_code, bss_termination_delay;
336 	const u8 *pos, *end;
337 
338 	if (len < 3) {
339 		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
340 			   MACSTR, MAC2STR(addr));
341 		return;
342 	}
343 
344 	pos = frm;
345 	end = pos + len;
346 	dialog_token = *pos++;
347 	status_code = *pos++;
348 	bss_termination_delay = *pos++;
349 
350 	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
351 		   MACSTR " dialog_token=%u status_code=%u "
352 		   "bss_termination_delay=%u", MAC2STR(addr), dialog_token,
353 		   status_code, bss_termination_delay);
354 
355 	if (status_code == WNM_BSS_TM_ACCEPT) {
356 		if (end - pos < ETH_ALEN) {
357 			wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
358 			return;
359 		}
360 		wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR,
361 			   MAC2STR(pos));
362 		wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
363 			" status_code=%u bss_termination_delay=%u target_bssid="
364 			MACSTR,
365 			MAC2STR(addr), status_code, bss_termination_delay,
366 			MAC2STR(pos));
367 		pos += ETH_ALEN;
368 	} else {
369 		wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
370 			" status_code=%u bss_termination_delay=%u",
371 			MAC2STR(addr), status_code, bss_termination_delay);
372 	}
373 
374 	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
375 		    pos, end - pos);
376 }
377 
378 
379 int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
380 				const struct ieee80211_mgmt *mgmt, size_t len)
381 {
382 	u8 action;
383 	const u8 *payload;
384 	size_t plen;
385 
386 	if (len < IEEE80211_HDRLEN + 2)
387 		return -1;
388 
389 	payload = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
390 	action = *payload++;
391 	plen = len - IEEE80211_HDRLEN - 2;
392 
393 	switch (action) {
394 	case WNM_BSS_TRANS_MGMT_QUERY:
395 		ieee802_11_rx_bss_trans_mgmt_query(hapd, mgmt->sa, payload,
396 						   plen);
397 		return 0;
398 	case WNM_BSS_TRANS_MGMT_RESP:
399 		ieee802_11_rx_bss_trans_mgmt_resp(hapd, mgmt->sa, payload,
400 						  plen);
401 		return 0;
402 	case WNM_SLEEP_MODE_REQ:
403 		ieee802_11_rx_wnmsleep_req(hapd, mgmt->sa, payload, plen);
404 		return 0;
405 	}
406 
407 	wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
408 		   action, MAC2STR(mgmt->sa));
409 	return -1;
410 }
411 
412 
413 int wnm_send_disassoc_imminent(struct hostapd_data *hapd,
414 			       struct sta_info *sta, int disassoc_timer)
415 {
416 	u8 buf[1000], *pos;
417 	struct ieee80211_mgmt *mgmt;
418 
419 	os_memset(buf, 0, sizeof(buf));
420 	mgmt = (struct ieee80211_mgmt *) buf;
421 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
422 					   WLAN_FC_STYPE_ACTION);
423 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
424 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
425 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
426 	mgmt->u.action.category = WLAN_ACTION_WNM;
427 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
428 	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
429 	mgmt->u.action.u.bss_tm_req.req_mode =
430 		WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
431 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
432 		host_to_le16(disassoc_timer);
433 	mgmt->u.action.u.bss_tm_req.validity_interval = 0;
434 
435 	pos = mgmt->u.action.u.bss_tm_req.variable;
436 
437 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request frame to indicate imminent disassociation (disassoc_timer=%d) to "
438 		   MACSTR, disassoc_timer, MAC2STR(sta->addr));
439 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
440 		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
441 			   "Management Request frame");
442 		return -1;
443 	}
444 
445 	return 0;
446 }
447 
448 
449 static void set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta,
450 			       int disassoc_timer)
451 {
452 	int timeout, beacon_int;
453 
454 	/*
455 	 * Prevent STA from reconnecting using cached PMKSA to force
456 	 * full authentication with the authentication server (which may
457 	 * decide to reject the connection),
458 	 */
459 	wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
460 
461 	beacon_int = hapd->iconf->beacon_int;
462 	if (beacon_int < 1)
463 		beacon_int = 100; /* best guess */
464 	/* Calculate timeout in ms based on beacon_int in TU */
465 	timeout = disassoc_timer * beacon_int * 128 / 125;
466 	wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
467 		   " set to %d ms", MAC2STR(sta->addr), timeout);
468 
469 	sta->timeout_next = STA_DISASSOC_FROM_CLI;
470 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
471 	eloop_register_timeout(timeout / 1000,
472 			       timeout % 1000 * 1000,
473 			       ap_handle_timer, hapd, sta);
474 }
475 
476 
477 int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
478 				   struct sta_info *sta, const char *url,
479 				   int disassoc_timer)
480 {
481 	u8 buf[1000], *pos;
482 	struct ieee80211_mgmt *mgmt;
483 	size_t url_len;
484 
485 	os_memset(buf, 0, sizeof(buf));
486 	mgmt = (struct ieee80211_mgmt *) buf;
487 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
488 					   WLAN_FC_STYPE_ACTION);
489 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
490 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
491 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
492 	mgmt->u.action.category = WLAN_ACTION_WNM;
493 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
494 	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
495 	mgmt->u.action.u.bss_tm_req.req_mode =
496 		WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
497 		WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
498 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
499 		host_to_le16(disassoc_timer);
500 	mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
501 
502 	pos = mgmt->u.action.u.bss_tm_req.variable;
503 
504 	/* Session Information URL */
505 	url_len = os_strlen(url);
506 	if (url_len > 255)
507 		return -1;
508 	*pos++ = url_len;
509 	os_memcpy(pos, url, url_len);
510 	pos += url_len;
511 
512 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
513 		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
514 			   "Management Request frame");
515 		return -1;
516 	}
517 
518 	if (disassoc_timer) {
519 		/* send disassociation frame after time-out */
520 		set_disassoc_timer(hapd, sta, disassoc_timer);
521 	}
522 
523 	return 0;
524 }
525 
526 
527 int wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta,
528 			u8 req_mode, int disassoc_timer, u8 valid_int,
529 			const u8 *bss_term_dur, const char *url,
530 			const u8 *nei_rep, size_t nei_rep_len)
531 {
532 	u8 *buf, *pos;
533 	struct ieee80211_mgmt *mgmt;
534 	size_t url_len;
535 
536 	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
537 		   MACSTR " req_mode=0x%x disassoc_timer=%d valid_int=0x%x",
538 		   MAC2STR(sta->addr), req_mode, disassoc_timer, valid_int);
539 	buf = os_zalloc(1000 + nei_rep_len);
540 	if (buf == NULL)
541 		return -1;
542 	mgmt = (struct ieee80211_mgmt *) buf;
543 	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
544 					   WLAN_FC_STYPE_ACTION);
545 	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
546 	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
547 	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
548 	mgmt->u.action.category = WLAN_ACTION_WNM;
549 	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
550 	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
551 	mgmt->u.action.u.bss_tm_req.req_mode = req_mode;
552 	mgmt->u.action.u.bss_tm_req.disassoc_timer =
553 		host_to_le16(disassoc_timer);
554 	mgmt->u.action.u.bss_tm_req.validity_interval = valid_int;
555 
556 	pos = mgmt->u.action.u.bss_tm_req.variable;
557 
558 	if ((req_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) &&
559 	    bss_term_dur) {
560 		os_memcpy(pos, bss_term_dur, 12);
561 		pos += 12;
562 	}
563 
564 	if (url) {
565 		/* Session Information URL */
566 		url_len = os_strlen(url);
567 		if (url_len > 255) {
568 			os_free(buf);
569 			return -1;
570 		}
571 
572 		*pos++ = url_len;
573 		os_memcpy(pos, url, url_len);
574 		pos += url_len;
575 	}
576 
577 	if (nei_rep) {
578 		os_memcpy(pos, nei_rep, nei_rep_len);
579 		pos += nei_rep_len;
580 	}
581 
582 	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
583 		wpa_printf(MSG_DEBUG,
584 			   "Failed to send BSS Transition Management Request frame");
585 		os_free(buf);
586 		return -1;
587 	}
588 	os_free(buf);
589 
590 	if (disassoc_timer) {
591 		/* send disassociation frame after time-out */
592 		set_disassoc_timer(hapd, sta, disassoc_timer);
593 	}
594 
595 	return 0;
596 }
597