wnm_ap.c 19 KB

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