wnm_sta.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /*
  2. * wpa_supplicant - WNM
  3. * Copyright (c) 2011-2013, 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 "common/ieee802_11_defs.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "rsn_supp/wpa.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "driver_i.h"
  16. #include "scan.h"
  17. #include "ctrl_iface.h"
  18. #include "bss.h"
  19. #include "wnm_sta.h"
  20. #include "hs20_supplicant.h"
  21. #define MAX_TFS_IE_LEN 1024
  22. #define WNM_MAX_NEIGHBOR_REPORT 10
  23. /* get the TFS IE from driver */
  24. static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
  25. u16 *buf_len, enum wnm_oper oper)
  26. {
  27. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  28. return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
  29. }
  30. /* set the TFS IE to driver */
  31. static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
  32. const u8 *addr, const u8 *buf, u16 buf_len,
  33. enum wnm_oper oper)
  34. {
  35. u16 len = buf_len;
  36. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  37. return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
  38. }
  39. /* MLME-SLEEPMODE.request */
  40. int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
  41. u8 action, u16 intval, struct wpabuf *tfs_req)
  42. {
  43. struct ieee80211_mgmt *mgmt;
  44. int res;
  45. size_t len;
  46. struct wnm_sleep_element *wnmsleep_ie;
  47. u8 *wnmtfs_ie;
  48. u8 wnmsleep_ie_len;
  49. u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
  50. enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
  51. WNM_SLEEP_TFS_REQ_IE_NONE;
  52. wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
  53. "action=%s to " MACSTR,
  54. action == 0 ? "enter" : "exit",
  55. MAC2STR(wpa_s->bssid));
  56. /* WNM-Sleep Mode IE */
  57. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  58. wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
  59. if (wnmsleep_ie == NULL)
  60. return -1;
  61. wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
  62. wnmsleep_ie->len = wnmsleep_ie_len - 2;
  63. wnmsleep_ie->action_type = action;
  64. wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
  65. wnmsleep_ie->intval = host_to_le16(intval);
  66. wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
  67. (u8 *) wnmsleep_ie, wnmsleep_ie_len);
  68. /* TFS IE(s) */
  69. if (tfs_req) {
  70. wnmtfs_ie_len = wpabuf_len(tfs_req);
  71. wnmtfs_ie = os_malloc(wnmtfs_ie_len);
  72. if (wnmtfs_ie == NULL) {
  73. os_free(wnmsleep_ie);
  74. return -1;
  75. }
  76. os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
  77. } else {
  78. wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
  79. if (wnmtfs_ie == NULL) {
  80. os_free(wnmsleep_ie);
  81. return -1;
  82. }
  83. if (ieee80211_11_get_tfs_ie(wpa_s, 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. wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
  91. (u8 *) wnmtfs_ie, wnmtfs_ie_len);
  92. mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
  93. if (mgmt == NULL) {
  94. wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
  95. "WNM-Sleep Request action frame");
  96. os_free(wnmsleep_ie);
  97. os_free(wnmtfs_ie);
  98. return -1;
  99. }
  100. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  101. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  102. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  103. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  104. WLAN_FC_STYPE_ACTION);
  105. mgmt->u.action.category = WLAN_ACTION_WNM;
  106. mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
  107. mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
  108. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
  109. wnmsleep_ie_len);
  110. /* copy TFS IE here */
  111. if (wnmtfs_ie_len > 0) {
  112. os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
  113. wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
  114. }
  115. len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
  116. wnmtfs_ie_len;
  117. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  118. wpa_s->own_addr, wpa_s->bssid,
  119. &mgmt->u.action.category, len, 0);
  120. if (res < 0)
  121. wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
  122. "(action=%d, intval=%d)", action, intval);
  123. else
  124. wpa_s->wnmsleep_used = 1;
  125. os_free(wnmsleep_ie);
  126. os_free(wnmtfs_ie);
  127. os_free(mgmt);
  128. return res;
  129. }
  130. static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
  131. const u8 *tfsresp_ie_start,
  132. const u8 *tfsresp_ie_end)
  133. {
  134. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
  135. wpa_s->bssid, NULL, NULL);
  136. /* remove GTK/IGTK ?? */
  137. /* set the TFS Resp IE(s) */
  138. if (tfsresp_ie_start && tfsresp_ie_end &&
  139. tfsresp_ie_end - tfsresp_ie_start >= 0) {
  140. u16 tfsresp_ie_len;
  141. tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
  142. tfsresp_ie_start;
  143. wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
  144. /* pass the TFS Resp IE(s) to driver for processing */
  145. if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
  146. tfsresp_ie_start,
  147. tfsresp_ie_len,
  148. WNM_SLEEP_TFS_RESP_IE_SET))
  149. wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
  150. }
  151. }
  152. static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
  153. const u8 *frm, u16 key_len_total)
  154. {
  155. u8 *ptr, *end;
  156. u8 gtk_len;
  157. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
  158. NULL, NULL);
  159. /* Install GTK/IGTK */
  160. /* point to key data field */
  161. ptr = (u8 *) frm + 1 + 2;
  162. end = ptr + key_len_total;
  163. wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
  164. if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
  165. wpa_msg(wpa_s, MSG_INFO,
  166. "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
  167. return;
  168. }
  169. while (end - ptr > 1) {
  170. if (2 + ptr[1] > end - ptr) {
  171. wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
  172. "length");
  173. if (end > ptr) {
  174. wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
  175. ptr, end - ptr);
  176. }
  177. break;
  178. }
  179. if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
  180. if (ptr[1] < 11 + 5) {
  181. wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
  182. "subelem");
  183. break;
  184. }
  185. gtk_len = *(ptr + 4);
  186. if (ptr[1] < 11 + gtk_len ||
  187. gtk_len < 5 || gtk_len > 32) {
  188. wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
  189. "subelem");
  190. break;
  191. }
  192. wpa_wnmsleep_install_key(
  193. wpa_s->wpa,
  194. WNM_SLEEP_SUBELEM_GTK,
  195. ptr);
  196. ptr += 13 + gtk_len;
  197. #ifdef CONFIG_IEEE80211W
  198. } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
  199. if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
  200. wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
  201. "subelem");
  202. break;
  203. }
  204. wpa_wnmsleep_install_key(wpa_s->wpa,
  205. WNM_SLEEP_SUBELEM_IGTK, ptr);
  206. ptr += 10 + WPA_IGTK_LEN;
  207. #endif /* CONFIG_IEEE80211W */
  208. } else
  209. break; /* skip the loop */
  210. }
  211. }
  212. static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
  213. const u8 *frm, int len)
  214. {
  215. /*
  216. * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
  217. * WNM-Sleep Mode IE | TFS Response IE
  218. */
  219. const u8 *pos = frm; /* point to payload after the action field */
  220. u16 key_len_total;
  221. struct wnm_sleep_element *wnmsleep_ie = NULL;
  222. /* multiple TFS Resp IE (assuming consecutive) */
  223. const u8 *tfsresp_ie_start = NULL;
  224. const u8 *tfsresp_ie_end = NULL;
  225. size_t left;
  226. if (!wpa_s->wnmsleep_used) {
  227. wpa_printf(MSG_DEBUG,
  228. "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode has not been used in this association");
  229. return;
  230. }
  231. if (len < 3)
  232. return;
  233. key_len_total = WPA_GET_LE16(frm + 1);
  234. wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
  235. frm[0], key_len_total);
  236. left = len - 3;
  237. if (key_len_total > left) {
  238. wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
  239. return;
  240. }
  241. pos += 3 + key_len_total;
  242. while (pos - frm + 1 < len) {
  243. u8 ie_len = *(pos + 1);
  244. if (2 + ie_len > frm + len - pos) {
  245. wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
  246. break;
  247. }
  248. wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
  249. if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
  250. wnmsleep_ie = (struct wnm_sleep_element *) pos;
  251. else if (*pos == WLAN_EID_TFS_RESP) {
  252. if (!tfsresp_ie_start)
  253. tfsresp_ie_start = pos;
  254. tfsresp_ie_end = pos;
  255. } else
  256. wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
  257. pos += ie_len + 2;
  258. }
  259. if (!wnmsleep_ie) {
  260. wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
  261. return;
  262. }
  263. if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
  264. wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
  265. wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
  266. "frame (action=%d, intval=%d)",
  267. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  268. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
  269. wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
  270. tfsresp_ie_end);
  271. } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
  272. wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
  273. }
  274. } else {
  275. wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
  276. "(action=%d, intval=%d)",
  277. wnmsleep_ie->action_type, wnmsleep_ie->intval);
  278. if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
  279. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
  280. wpa_s->bssid, NULL, NULL);
  281. else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
  282. wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
  283. wpa_s->bssid, NULL, NULL);
  284. }
  285. }
  286. void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
  287. {
  288. int i;
  289. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  290. os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
  291. os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
  292. }
  293. wpa_s->wnm_num_neighbor_report = 0;
  294. os_free(wpa_s->wnm_neighbor_report_elements);
  295. wpa_s->wnm_neighbor_report_elements = NULL;
  296. }
  297. static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
  298. u8 id, u8 elen, const u8 *pos)
  299. {
  300. switch (id) {
  301. case WNM_NEIGHBOR_TSF:
  302. if (elen < 2 + 2) {
  303. wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
  304. break;
  305. }
  306. rep->tsf_offset = WPA_GET_LE16(pos);
  307. rep->beacon_int = WPA_GET_LE16(pos + 2);
  308. rep->tsf_present = 1;
  309. break;
  310. case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
  311. if (elen < 2) {
  312. wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
  313. "country string");
  314. break;
  315. }
  316. os_memcpy(rep->country, pos, 2);
  317. rep->country_present = 1;
  318. break;
  319. case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
  320. if (elen < 1) {
  321. wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
  322. "candidate");
  323. break;
  324. }
  325. rep->preference = pos[0];
  326. rep->preference_present = 1;
  327. break;
  328. case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
  329. rep->bss_term_tsf = WPA_GET_LE64(pos);
  330. rep->bss_term_dur = WPA_GET_LE16(pos + 8);
  331. rep->bss_term_present = 1;
  332. break;
  333. case WNM_NEIGHBOR_BEARING:
  334. if (elen < 8) {
  335. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
  336. "bearing");
  337. break;
  338. }
  339. rep->bearing = WPA_GET_LE16(pos);
  340. rep->distance = WPA_GET_LE32(pos + 2);
  341. rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
  342. rep->bearing_present = 1;
  343. break;
  344. case WNM_NEIGHBOR_MEASUREMENT_PILOT:
  345. if (elen < 1) {
  346. wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
  347. "pilot");
  348. break;
  349. }
  350. os_free(rep->meas_pilot);
  351. rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
  352. if (rep->meas_pilot == NULL)
  353. break;
  354. rep->meas_pilot->measurement_pilot = pos[0];
  355. rep->meas_pilot->subelem_len = elen - 1;
  356. os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
  357. break;
  358. case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
  359. if (elen < 5) {
  360. wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
  361. "capabilities");
  362. break;
  363. }
  364. os_memcpy(rep->rm_capab, pos, 5);
  365. rep->rm_capab_present = 1;
  366. break;
  367. case WNM_NEIGHBOR_MULTIPLE_BSSID:
  368. if (elen < 1) {
  369. wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
  370. break;
  371. }
  372. os_free(rep->mul_bssid);
  373. rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
  374. if (rep->mul_bssid == NULL)
  375. break;
  376. rep->mul_bssid->max_bssid_indicator = pos[0];
  377. rep->mul_bssid->subelem_len = elen - 1;
  378. os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
  379. break;
  380. }
  381. }
  382. static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
  383. {
  384. struct wpa_bss *bss = wpa_s->current_bss;
  385. const char *country = NULL;
  386. int freq;
  387. if (bss) {
  388. const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
  389. if (elem && elem[1] >= 2)
  390. country = (const char *) (elem + 2);
  391. }
  392. freq = ieee80211_chan_to_freq(country, op_class, chan);
  393. if (freq <= 0 && op_class == 0) {
  394. /*
  395. * Some APs do not advertise correct operating class
  396. * information. Try to determine the most likely operating
  397. * frequency based on the channel number.
  398. */
  399. if (chan >= 1 && chan <= 13)
  400. freq = 2407 + chan * 5;
  401. else if (chan == 14)
  402. freq = 2484;
  403. else if (chan >= 36 && chan <= 169)
  404. freq = 5000 + chan * 5;
  405. }
  406. return freq;
  407. }
  408. static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
  409. const u8 *pos, u8 len,
  410. struct neighbor_report *rep)
  411. {
  412. u8 left = len;
  413. if (left < 13) {
  414. wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
  415. return;
  416. }
  417. os_memcpy(rep->bssid, pos, ETH_ALEN);
  418. rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
  419. rep->regulatory_class = *(pos + 10);
  420. rep->channel_number = *(pos + 11);
  421. rep->phy_type = *(pos + 12);
  422. pos += 13;
  423. left -= 13;
  424. while (left >= 2) {
  425. u8 id, elen;
  426. id = *pos++;
  427. elen = *pos++;
  428. wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
  429. left -= 2;
  430. if (elen > left) {
  431. wpa_printf(MSG_DEBUG,
  432. "WNM: Truncated neighbor report subelement");
  433. break;
  434. }
  435. wnm_parse_neighbor_report_elem(rep, id, elen, pos);
  436. left -= elen;
  437. pos += elen;
  438. }
  439. rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
  440. rep->channel_number);
  441. }
  442. static struct wpa_bss *
  443. compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
  444. {
  445. u8 i;
  446. struct wpa_bss *bss = wpa_s->current_bss;
  447. struct wpa_bss *target;
  448. if (!bss)
  449. return 0;
  450. wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
  451. MAC2STR(wpa_s->bssid), bss->level);
  452. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  453. struct neighbor_report *nei;
  454. nei = &wpa_s->wnm_neighbor_report_elements[i];
  455. if (nei->preference_present && nei->preference == 0) {
  456. wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
  457. MAC2STR(nei->bssid));
  458. continue;
  459. }
  460. target = wpa_bss_get_bssid(wpa_s, nei->bssid);
  461. if (!target) {
  462. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  463. " (pref %d) not found in scan results",
  464. MAC2STR(nei->bssid),
  465. nei->preference_present ? nei->preference :
  466. -1);
  467. continue;
  468. }
  469. if (bss->ssid_len != target->ssid_len ||
  470. os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
  471. /*
  472. * TODO: Could consider allowing transition to another
  473. * ESS if PMF was enabled for the association.
  474. */
  475. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  476. " (pref %d) in different ESS",
  477. MAC2STR(nei->bssid),
  478. nei->preference_present ? nei->preference :
  479. -1);
  480. continue;
  481. }
  482. if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
  483. wpa_printf(MSG_DEBUG,
  484. "MBO: Candidate BSS " MACSTR
  485. " retry delay is not over yet",
  486. MAC2STR(nei->bssid));
  487. continue;
  488. }
  489. if (target->level < bss->level && target->level < -80) {
  490. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  491. " (pref %d) does not have sufficient signal level (%d)",
  492. MAC2STR(nei->bssid),
  493. nei->preference_present ? nei->preference :
  494. -1,
  495. target->level);
  496. continue;
  497. }
  498. wpa_printf(MSG_DEBUG,
  499. "WNM: Found an acceptable preferred transition candidate BSS "
  500. MACSTR " (RSSI %d)",
  501. MAC2STR(nei->bssid), target->level);
  502. return target;
  503. }
  504. return NULL;
  505. }
  506. static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
  507. {
  508. const u8 *ie_a, *ie_b;
  509. if (!a || !b)
  510. return 0;
  511. ie_a = wpa_bss_get_ie(a, eid);
  512. ie_b = wpa_bss_get_ie(b, eid);
  513. if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
  514. return 0;
  515. return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
  516. }
  517. static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  518. {
  519. u32 info = 0;
  520. info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
  521. /*
  522. * Leave the security and key scope bits unset to indicate that the
  523. * security information is not available.
  524. */
  525. if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
  526. info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
  527. if (bss->caps & WLAN_CAPABILITY_QOS)
  528. info |= NEI_REP_BSSID_INFO_QOS;
  529. if (bss->caps & WLAN_CAPABILITY_APSD)
  530. info |= NEI_REP_BSSID_INFO_APSD;
  531. if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
  532. info |= NEI_REP_BSSID_INFO_RM;
  533. if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
  534. info |= NEI_REP_BSSID_INFO_DELAYED_BA;
  535. if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
  536. info |= NEI_REP_BSSID_INFO_IMM_BA;
  537. if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
  538. info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
  539. if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
  540. info |= NEI_REP_BSSID_INFO_HT;
  541. return info;
  542. }
  543. static int wnm_add_nei_rep(u8 *buf, size_t len, const u8 *bssid, u32 bss_info,
  544. u8 op_class, u8 chan, u8 phy_type, u8 pref)
  545. {
  546. u8 *pos = buf;
  547. if (len < 18) {
  548. wpa_printf(MSG_DEBUG,
  549. "WNM: Not enough room for Neighbor Report element");
  550. return -1;
  551. }
  552. *pos++ = WLAN_EID_NEIGHBOR_REPORT;
  553. /* length: 13 for basic neighbor report + 3 for preference subelement */
  554. *pos++ = 16;
  555. os_memcpy(pos, bssid, ETH_ALEN);
  556. pos += ETH_ALEN;
  557. WPA_PUT_LE32(pos, bss_info);
  558. pos += 4;
  559. *pos++ = op_class;
  560. *pos++ = chan;
  561. *pos++ = phy_type;
  562. *pos++ = WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE;
  563. *pos++ = 1;
  564. *pos++ = pref;
  565. return pos - buf;
  566. }
  567. static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
  568. struct wpa_bss *bss, u8 *buf, size_t len,
  569. u8 pref)
  570. {
  571. const u8 *ie;
  572. u8 op_class, chan;
  573. int sec_chan = 0, vht = 0;
  574. enum phy_type phy_type;
  575. u32 info;
  576. struct ieee80211_ht_operation *ht_oper = NULL;
  577. struct ieee80211_vht_operation *vht_oper = NULL;
  578. ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
  579. if (ie && ie[1] >= 2) {
  580. ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
  581. if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  582. sec_chan = 1;
  583. else if (ht_oper->ht_param &
  584. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  585. sec_chan = -1;
  586. }
  587. ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
  588. if (ie && ie[1] >= 1) {
  589. vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
  590. if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
  591. vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
  592. vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
  593. vht = vht_oper->vht_op_info_chwidth;
  594. }
  595. if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
  596. &chan) == NUM_HOSTAPD_MODES) {
  597. wpa_printf(MSG_DEBUG,
  598. "WNM: Cannot determine operating class and channel");
  599. return -2;
  600. }
  601. phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
  602. (vht_oper != NULL));
  603. if (phy_type == PHY_TYPE_UNSPECIFIED) {
  604. wpa_printf(MSG_DEBUG,
  605. "WNM: Cannot determine BSS phy type for Neighbor Report");
  606. return -2;
  607. }
  608. info = wnm_get_bss_info(wpa_s, bss);
  609. return wnm_add_nei_rep(buf, len, bss->bssid, info, op_class, chan,
  610. phy_type, pref);
  611. }
  612. static int wnm_add_cand_list(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
  613. {
  614. u8 *pos = buf;
  615. unsigned int i, pref = 255;
  616. struct os_reltime now;
  617. struct wpa_ssid *ssid = wpa_s->current_ssid;
  618. if (!ssid)
  619. return 0;
  620. /*
  621. * TODO: Define when scan results are no longer valid for the candidate
  622. * list.
  623. */
  624. os_get_reltime(&now);
  625. if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
  626. return 0;
  627. wpa_printf(MSG_DEBUG,
  628. "WNM: Add candidate list to BSS Transition Management Response frame");
  629. for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
  630. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  631. int res;
  632. if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1)) {
  633. res = wnm_nei_rep_add_bss(wpa_s, bss, pos, len, pref--);
  634. if (res == -2)
  635. continue; /* could not build entry for BSS */
  636. if (res < 0)
  637. break; /* no more room for candidates */
  638. if (pref == 1)
  639. break;
  640. pos += res;
  641. len -= res;
  642. }
  643. }
  644. wpa_hexdump(MSG_DEBUG,
  645. "WNM: BSS Transition Management Response candidate list",
  646. buf, pos - buf);
  647. return pos - buf;
  648. }
  649. static void wnm_send_bss_transition_mgmt_resp(
  650. struct wpa_supplicant *wpa_s, u8 dialog_token,
  651. enum bss_trans_mgmt_status_code status, u8 delay,
  652. const u8 *target_bssid)
  653. {
  654. u8 buf[2000], *pos;
  655. struct ieee80211_mgmt *mgmt;
  656. size_t len;
  657. int res;
  658. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
  659. "to " MACSTR " dialog_token=%u status=%u delay=%d",
  660. MAC2STR(wpa_s->bssid), dialog_token, status, delay);
  661. if (!wpa_s->current_bss) {
  662. wpa_printf(MSG_DEBUG,
  663. "WNM: Current BSS not known - drop response");
  664. return;
  665. }
  666. mgmt = (struct ieee80211_mgmt *) buf;
  667. os_memset(&buf, 0, sizeof(buf));
  668. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  669. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  670. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  671. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  672. WLAN_FC_STYPE_ACTION);
  673. mgmt->u.action.category = WLAN_ACTION_WNM;
  674. mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
  675. mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
  676. mgmt->u.action.u.bss_tm_resp.status_code = status;
  677. mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
  678. pos = mgmt->u.action.u.bss_tm_resp.variable;
  679. if (target_bssid) {
  680. os_memcpy(pos, target_bssid, ETH_ALEN);
  681. pos += ETH_ALEN;
  682. } else if (status == WNM_BSS_TM_ACCEPT) {
  683. /*
  684. * P802.11-REVmc clarifies that the Target BSSID field is always
  685. * present when status code is zero, so use a fake value here if
  686. * no BSSID is yet known.
  687. */
  688. os_memset(pos, 0, ETH_ALEN);
  689. pos += ETH_ALEN;
  690. }
  691. if (status == WNM_BSS_TM_ACCEPT)
  692. pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
  693. #ifdef CONFIG_MBO
  694. if (status != WNM_BSS_TM_ACCEPT) {
  695. pos += wpas_mbo_ie_bss_trans_reject(
  696. wpa_s, pos, buf + sizeof(buf) - pos,
  697. MBO_TRANSITION_REJECT_REASON_UNSPECIFIED);
  698. }
  699. #endif /* CONFIG_MBO */
  700. len = pos - (u8 *) &mgmt->u.action.category;
  701. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  702. wpa_s->own_addr, wpa_s->bssid,
  703. &mgmt->u.action.category, len, 0);
  704. if (res < 0) {
  705. wpa_printf(MSG_DEBUG,
  706. "WNM: Failed to send BSS Transition Management Response");
  707. }
  708. }
  709. int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
  710. {
  711. struct wpa_bss *bss;
  712. struct wpa_ssid *ssid = wpa_s->current_ssid;
  713. enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  714. if (!wpa_s->wnm_neighbor_report_elements)
  715. return 0;
  716. if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
  717. &wpa_s->scan_trigger_time)) {
  718. wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
  719. wnm_deallocate_memory(wpa_s);
  720. return 0;
  721. }
  722. if (!wpa_s->current_bss ||
  723. os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
  724. ETH_ALEN) != 0) {
  725. wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
  726. return 0;
  727. }
  728. /* Compare the Neighbor Report and scan results */
  729. bss = compare_scan_neighbor_results(wpa_s);
  730. if (!bss) {
  731. wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
  732. status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
  733. goto send_bss_resp_fail;
  734. }
  735. /* Associate to the network */
  736. /* Send the BSS Management Response - Accept */
  737. if (wpa_s->wnm_reply) {
  738. wpa_s->wnm_reply = 0;
  739. wnm_send_bss_transition_mgmt_resp(wpa_s,
  740. wpa_s->wnm_dialog_token,
  741. WNM_BSS_TM_ACCEPT,
  742. 0, bss->bssid);
  743. }
  744. if (bss == wpa_s->current_bss) {
  745. wpa_printf(MSG_DEBUG,
  746. "WNM: Already associated with the preferred candidate");
  747. wnm_deallocate_memory(wpa_s);
  748. return 1;
  749. }
  750. wpa_s->reassociate = 1;
  751. wpa_supplicant_connect(wpa_s, bss, ssid);
  752. wnm_deallocate_memory(wpa_s);
  753. return 1;
  754. send_bss_resp_fail:
  755. if (!reply_on_fail)
  756. return 0;
  757. /* Send reject response for all the failures */
  758. if (wpa_s->wnm_reply) {
  759. wpa_s->wnm_reply = 0;
  760. wnm_send_bss_transition_mgmt_resp(wpa_s,
  761. wpa_s->wnm_dialog_token,
  762. status, 0, NULL);
  763. }
  764. wnm_deallocate_memory(wpa_s);
  765. return 0;
  766. }
  767. static int cand_pref_compar(const void *a, const void *b)
  768. {
  769. const struct neighbor_report *aa = a;
  770. const struct neighbor_report *bb = b;
  771. if (!aa->preference_present && !bb->preference_present)
  772. return 0;
  773. if (!aa->preference_present)
  774. return 1;
  775. if (!bb->preference_present)
  776. return -1;
  777. if (bb->preference > aa->preference)
  778. return 1;
  779. if (bb->preference < aa->preference)
  780. return -1;
  781. return 0;
  782. }
  783. static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
  784. {
  785. if (!wpa_s->wnm_neighbor_report_elements)
  786. return;
  787. qsort(wpa_s->wnm_neighbor_report_elements,
  788. wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
  789. cand_pref_compar);
  790. }
  791. static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
  792. {
  793. unsigned int i;
  794. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
  795. if (!wpa_s->wnm_neighbor_report_elements)
  796. return;
  797. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  798. struct neighbor_report *nei;
  799. nei = &wpa_s->wnm_neighbor_report_elements[i];
  800. wpa_printf(MSG_DEBUG, "%u: " MACSTR
  801. " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
  802. i, MAC2STR(nei->bssid), nei->bssid_info,
  803. nei->regulatory_class,
  804. nei->channel_number, nei->phy_type,
  805. nei->preference_present ? nei->preference : -1,
  806. nei->freq);
  807. }
  808. }
  809. static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
  810. {
  811. unsigned int i;
  812. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  813. struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
  814. int j;
  815. for (j = 0; j < mode->num_channels; j++) {
  816. struct hostapd_channel_data *chan;
  817. chan = &mode->channels[j];
  818. if (chan->freq == freq &&
  819. !(chan->flag & HOSTAPD_CHAN_DISABLED))
  820. return 1;
  821. }
  822. }
  823. return 0;
  824. }
  825. static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
  826. {
  827. int *freqs;
  828. int num_freqs = 0;
  829. unsigned int i;
  830. if (!wpa_s->wnm_neighbor_report_elements)
  831. return;
  832. if (wpa_s->hw.modes == NULL)
  833. return;
  834. os_free(wpa_s->next_scan_freqs);
  835. wpa_s->next_scan_freqs = NULL;
  836. freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
  837. if (freqs == NULL)
  838. return;
  839. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  840. struct neighbor_report *nei;
  841. nei = &wpa_s->wnm_neighbor_report_elements[i];
  842. if (nei->freq <= 0) {
  843. wpa_printf(MSG_DEBUG,
  844. "WNM: Unknown neighbor operating frequency for "
  845. MACSTR " - scan all channels",
  846. MAC2STR(nei->bssid));
  847. os_free(freqs);
  848. return;
  849. }
  850. if (chan_supported(wpa_s, nei->freq))
  851. add_freq(freqs, &num_freqs, nei->freq);
  852. }
  853. if (num_freqs == 0) {
  854. os_free(freqs);
  855. return;
  856. }
  857. wpa_printf(MSG_DEBUG,
  858. "WNM: Scan %d frequencies based on transition candidate list",
  859. num_freqs);
  860. wpa_s->next_scan_freqs = freqs;
  861. }
  862. static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
  863. const u8 *pos, const u8 *end,
  864. int reply)
  865. {
  866. unsigned int beacon_int;
  867. u8 valid_int;
  868. #ifdef CONFIG_MBO
  869. const u8 *vendor;
  870. #endif /* CONFIG_MBO */
  871. if (end - pos < 5)
  872. return;
  873. if (wpa_s->current_bss)
  874. beacon_int = wpa_s->current_bss->beacon_int;
  875. else
  876. beacon_int = 100; /* best guess */
  877. wpa_s->wnm_dialog_token = pos[0];
  878. wpa_s->wnm_mode = pos[1];
  879. wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
  880. valid_int = pos[4];
  881. wpa_s->wnm_reply = reply;
  882. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
  883. "dialog_token=%u request_mode=0x%x "
  884. "disassoc_timer=%u validity_interval=%u",
  885. wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
  886. wpa_s->wnm_dissoc_timer, valid_int);
  887. pos += 5;
  888. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  889. if (end - pos < 12) {
  890. wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
  891. return;
  892. }
  893. os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
  894. pos += 12; /* BSS Termination Duration */
  895. }
  896. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
  897. char url[256];
  898. if (end - pos < 1 || 1 + pos[0] > end - pos) {
  899. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
  900. "Management Request (URL)");
  901. return;
  902. }
  903. os_memcpy(url, pos + 1, pos[0]);
  904. url[pos[0]] = '\0';
  905. pos += 1 + pos[0];
  906. wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
  907. wpa_sm_pmf_enabled(wpa_s->wpa),
  908. wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
  909. }
  910. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  911. wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
  912. "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
  913. if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
  914. /* TODO: mark current BSS less preferred for
  915. * selection */
  916. wpa_printf(MSG_DEBUG, "Trying to find another BSS");
  917. wpa_supplicant_req_scan(wpa_s, 0, 0);
  918. }
  919. }
  920. #ifdef CONFIG_MBO
  921. vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
  922. if (vendor)
  923. wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
  924. #endif /* CONFIG_MBO */
  925. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
  926. unsigned int valid_ms;
  927. wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
  928. wnm_deallocate_memory(wpa_s);
  929. wpa_s->wnm_neighbor_report_elements = os_calloc(
  930. WNM_MAX_NEIGHBOR_REPORT,
  931. sizeof(struct neighbor_report));
  932. if (wpa_s->wnm_neighbor_report_elements == NULL)
  933. return;
  934. while (end - pos >= 2 &&
  935. wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
  936. {
  937. u8 tag = *pos++;
  938. u8 len = *pos++;
  939. wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
  940. tag);
  941. if (len > end - pos) {
  942. wpa_printf(MSG_DEBUG, "WNM: Truncated request");
  943. return;
  944. }
  945. if (tag == WLAN_EID_NEIGHBOR_REPORT) {
  946. struct neighbor_report *rep;
  947. rep = &wpa_s->wnm_neighbor_report_elements[
  948. wpa_s->wnm_num_neighbor_report];
  949. wnm_parse_neighbor_report(wpa_s, pos, len, rep);
  950. wpa_s->wnm_num_neighbor_report++;
  951. }
  952. pos += len;
  953. }
  954. if (!wpa_s->wnm_num_neighbor_report) {
  955. wpa_printf(MSG_DEBUG,
  956. "WNM: Candidate list included bit is set, but no candidates found");
  957. wnm_send_bss_transition_mgmt_resp(
  958. wpa_s, wpa_s->wnm_dialog_token,
  959. WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
  960. 0, NULL);
  961. return;
  962. }
  963. wnm_sort_cand_list(wpa_s);
  964. wnm_dump_cand_list(wpa_s);
  965. valid_ms = valid_int * beacon_int * 128 / 125;
  966. wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
  967. valid_ms);
  968. os_get_reltime(&wpa_s->wnm_cand_valid_until);
  969. wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
  970. wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
  971. wpa_s->wnm_cand_valid_until.sec +=
  972. wpa_s->wnm_cand_valid_until.usec / 1000000;
  973. wpa_s->wnm_cand_valid_until.usec %= 1000000;
  974. os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
  975. if (wpa_s->last_scan_res_used > 0) {
  976. struct os_reltime now;
  977. os_get_reltime(&now);
  978. if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
  979. wpa_printf(MSG_DEBUG,
  980. "WNM: Try to use recent scan results");
  981. if (wnm_scan_process(wpa_s, 0) > 0)
  982. return;
  983. wpa_printf(MSG_DEBUG,
  984. "WNM: No match in previous scan results - try a new scan");
  985. }
  986. }
  987. wnm_set_scan_freqs(wpa_s);
  988. if (wpa_s->wnm_num_neighbor_report == 1) {
  989. os_memcpy(wpa_s->next_scan_bssid,
  990. wpa_s->wnm_neighbor_report_elements[0].bssid,
  991. ETH_ALEN);
  992. wpa_printf(MSG_DEBUG,
  993. "WNM: Scan only for a specific BSSID since there is only a single candidate "
  994. MACSTR, MAC2STR(wpa_s->next_scan_bssid));
  995. }
  996. wpa_supplicant_req_scan(wpa_s, 0, 0);
  997. } else if (reply) {
  998. enum bss_trans_mgmt_status_code status;
  999. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
  1000. status = WNM_BSS_TM_ACCEPT;
  1001. else {
  1002. wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
  1003. status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  1004. }
  1005. wnm_send_bss_transition_mgmt_resp(wpa_s,
  1006. wpa_s->wnm_dialog_token,
  1007. status, 0, NULL);
  1008. }
  1009. }
  1010. int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
  1011. u8 query_reason, int cand_list)
  1012. {
  1013. u8 buf[2000], *pos;
  1014. struct ieee80211_mgmt *mgmt;
  1015. size_t len;
  1016. int ret;
  1017. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
  1018. MACSTR " query_reason=%u%s",
  1019. MAC2STR(wpa_s->bssid), query_reason,
  1020. cand_list ? " candidate list" : "");
  1021. mgmt = (struct ieee80211_mgmt *) buf;
  1022. os_memset(&buf, 0, sizeof(buf));
  1023. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  1024. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  1025. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  1026. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1027. WLAN_FC_STYPE_ACTION);
  1028. mgmt->u.action.category = WLAN_ACTION_WNM;
  1029. mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
  1030. mgmt->u.action.u.bss_tm_query.dialog_token = 1;
  1031. mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
  1032. pos = mgmt->u.action.u.bss_tm_query.variable;
  1033. if (cand_list)
  1034. pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
  1035. len = pos - (u8 *) &mgmt->u.action.category;
  1036. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  1037. wpa_s->own_addr, wpa_s->bssid,
  1038. &mgmt->u.action.category, len, 0);
  1039. return ret;
  1040. }
  1041. static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
  1042. const u8 *sa, const u8 *data,
  1043. int len)
  1044. {
  1045. const u8 *pos, *end, *next;
  1046. u8 ie, ie_len;
  1047. pos = data;
  1048. end = data + len;
  1049. while (end - pos > 1) {
  1050. ie = *pos++;
  1051. ie_len = *pos++;
  1052. wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
  1053. ie, ie_len);
  1054. if (ie_len > end - pos) {
  1055. wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
  1056. "subelement");
  1057. break;
  1058. }
  1059. next = pos + ie_len;
  1060. if (ie_len < 4) {
  1061. pos = next;
  1062. continue;
  1063. }
  1064. wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
  1065. WPA_GET_BE24(pos), pos[3]);
  1066. #ifdef CONFIG_HS20
  1067. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
  1068. WPA_GET_BE24(pos) == OUI_WFA &&
  1069. pos[3] == HS20_WNM_SUB_REM_NEEDED) {
  1070. /* Subscription Remediation subelement */
  1071. const u8 *ie_end;
  1072. u8 url_len;
  1073. char *url;
  1074. u8 osu_method;
  1075. wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
  1076. "subelement");
  1077. ie_end = pos + ie_len;
  1078. pos += 4;
  1079. url_len = *pos++;
  1080. if (url_len == 0) {
  1081. wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
  1082. url = NULL;
  1083. osu_method = 1;
  1084. } else {
  1085. if (url_len + 1 > ie_end - pos) {
  1086. wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
  1087. url_len,
  1088. (int) (ie_end - pos));
  1089. break;
  1090. }
  1091. url = os_malloc(url_len + 1);
  1092. if (url == NULL)
  1093. break;
  1094. os_memcpy(url, pos, url_len);
  1095. url[url_len] = '\0';
  1096. osu_method = pos[url_len];
  1097. }
  1098. hs20_rx_subscription_remediation(wpa_s, url,
  1099. osu_method);
  1100. os_free(url);
  1101. pos = next;
  1102. continue;
  1103. }
  1104. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
  1105. WPA_GET_BE24(pos) == OUI_WFA &&
  1106. pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
  1107. const u8 *ie_end;
  1108. u8 url_len;
  1109. char *url;
  1110. u8 code;
  1111. u16 reauth_delay;
  1112. ie_end = pos + ie_len;
  1113. pos += 4;
  1114. code = *pos++;
  1115. reauth_delay = WPA_GET_LE16(pos);
  1116. pos += 2;
  1117. url_len = *pos++;
  1118. wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
  1119. "Imminent - Reason Code %u "
  1120. "Re-Auth Delay %u URL Length %u",
  1121. code, reauth_delay, url_len);
  1122. if (url_len > ie_end - pos)
  1123. break;
  1124. url = os_malloc(url_len + 1);
  1125. if (url == NULL)
  1126. break;
  1127. os_memcpy(url, pos, url_len);
  1128. url[url_len] = '\0';
  1129. hs20_rx_deauth_imminent_notice(wpa_s, code,
  1130. reauth_delay, url);
  1131. os_free(url);
  1132. pos = next;
  1133. continue;
  1134. }
  1135. #endif /* CONFIG_HS20 */
  1136. pos = next;
  1137. }
  1138. }
  1139. static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
  1140. const u8 *sa, const u8 *frm, int len)
  1141. {
  1142. const u8 *pos, *end;
  1143. u8 dialog_token, type;
  1144. /* Dialog Token [1] | Type [1] | Subelements */
  1145. if (len < 2 || sa == NULL)
  1146. return;
  1147. end = frm + len;
  1148. pos = frm;
  1149. dialog_token = *pos++;
  1150. type = *pos++;
  1151. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
  1152. "(dialog_token %u type %u sa " MACSTR ")",
  1153. dialog_token, type, MAC2STR(sa));
  1154. wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
  1155. pos, end - pos);
  1156. if (wpa_s->wpa_state != WPA_COMPLETED ||
  1157. os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
  1158. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
  1159. "from our AP - ignore it");
  1160. return;
  1161. }
  1162. switch (type) {
  1163. case 1:
  1164. ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
  1165. break;
  1166. default:
  1167. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
  1168. "WNM-Notification type %u", type);
  1169. break;
  1170. }
  1171. }
  1172. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  1173. const struct ieee80211_mgmt *mgmt, size_t len)
  1174. {
  1175. const u8 *pos, *end;
  1176. u8 act;
  1177. if (len < IEEE80211_HDRLEN + 2)
  1178. return;
  1179. pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
  1180. act = *pos++;
  1181. end = ((const u8 *) mgmt) + len;
  1182. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  1183. act, MAC2STR(mgmt->sa));
  1184. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  1185. os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  1186. wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
  1187. "frame");
  1188. return;
  1189. }
  1190. switch (act) {
  1191. case WNM_BSS_TRANS_MGMT_REQ:
  1192. ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
  1193. !(mgmt->da[0] & 0x01));
  1194. break;
  1195. case WNM_SLEEP_MODE_RESP:
  1196. ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
  1197. break;
  1198. case WNM_NOTIFICATION_REQ:
  1199. ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
  1200. break;
  1201. default:
  1202. wpa_printf(MSG_ERROR, "WNM: Unknown request");
  1203. break;
  1204. }
  1205. }