wnm_sta.c 45 KB

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