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