wnm_sta.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. #define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
  24. /* get the TFS IE from driver */
  25. static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
  26. u16 *buf_len, enum wnm_oper oper)
  27. {
  28. wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
  29. return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
  30. }
  31. /* set the TFS IE to driver */
  32. static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
  33. const u8 *addr, const u8 *buf, u16 buf_len,
  34. enum wnm_oper oper)
  35. {
  36. u16 len = buf_len;
  37. wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
  38. return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
  39. }
  40. /* MLME-SLEEPMODE.request */
  41. int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
  42. u8 action, u16 intval, struct wpabuf *tfs_req)
  43. {
  44. struct ieee80211_mgmt *mgmt;
  45. int res;
  46. size_t len;
  47. struct wnm_sleep_element *wnmsleep_ie;
  48. u8 *wnmtfs_ie;
  49. u8 wnmsleep_ie_len;
  50. u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
  51. enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
  52. WNM_SLEEP_TFS_REQ_IE_NONE;
  53. wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
  54. "action=%s to " MACSTR,
  55. action == 0 ? "enter" : "exit",
  56. MAC2STR(wpa_s->bssid));
  57. /* WNM-Sleep Mode IE */
  58. wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
  59. wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
  60. if (wnmsleep_ie == NULL)
  61. return -1;
  62. wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
  63. wnmsleep_ie->len = wnmsleep_ie_len - 2;
  64. wnmsleep_ie->action_type = action;
  65. wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
  66. wnmsleep_ie->intval = host_to_le16(intval);
  67. wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
  68. (u8 *) wnmsleep_ie, wnmsleep_ie_len);
  69. /* TFS IE(s) */
  70. if (tfs_req) {
  71. wnmtfs_ie_len = wpabuf_len(tfs_req);
  72. wnmtfs_ie = os_malloc(wnmtfs_ie_len);
  73. if (wnmtfs_ie == NULL) {
  74. os_free(wnmsleep_ie);
  75. return -1;
  76. }
  77. os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
  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 struct wpa_bss *
  444. compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs)
  445. {
  446. u8 i;
  447. struct wpa_bss *bss = wpa_s->current_bss;
  448. struct wpa_bss *target;
  449. if (!bss)
  450. return NULL;
  451. wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
  452. MAC2STR(wpa_s->bssid), bss->level);
  453. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  454. struct neighbor_report *nei;
  455. nei = &wpa_s->wnm_neighbor_report_elements[i];
  456. if (nei->preference_present && nei->preference == 0) {
  457. wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
  458. MAC2STR(nei->bssid));
  459. continue;
  460. }
  461. target = wpa_bss_get_bssid(wpa_s, nei->bssid);
  462. if (!target) {
  463. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  464. " (pref %d) not found in scan results",
  465. MAC2STR(nei->bssid),
  466. nei->preference_present ? nei->preference :
  467. -1);
  468. continue;
  469. }
  470. if (age_secs) {
  471. struct os_reltime now;
  472. if (os_get_reltime(&now) == 0 &&
  473. os_reltime_expired(&now, &target->last_update,
  474. age_secs)) {
  475. wpa_printf(MSG_DEBUG,
  476. "Candidate BSS is more than %ld seconds old",
  477. age_secs);
  478. continue;
  479. }
  480. }
  481. if (bss->ssid_len != target->ssid_len ||
  482. os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
  483. /*
  484. * TODO: Could consider allowing transition to another
  485. * ESS if PMF was enabled for the association.
  486. */
  487. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  488. " (pref %d) in different ESS",
  489. MAC2STR(nei->bssid),
  490. nei->preference_present ? nei->preference :
  491. -1);
  492. continue;
  493. }
  494. if (wpa_s->current_ssid &&
  495. !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid,
  496. 1)) {
  497. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  498. " (pref %d) does not match the current network profile",
  499. MAC2STR(nei->bssid),
  500. nei->preference_present ? nei->preference :
  501. -1);
  502. continue;
  503. }
  504. if (wpa_is_bss_tmp_disallowed(wpa_s, target->bssid)) {
  505. wpa_printf(MSG_DEBUG,
  506. "MBO: Candidate BSS " MACSTR
  507. " retry delay is not over yet",
  508. MAC2STR(nei->bssid));
  509. continue;
  510. }
  511. if (target->level < bss->level && target->level < -80) {
  512. wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
  513. " (pref %d) does not have sufficient signal level (%d)",
  514. MAC2STR(nei->bssid),
  515. nei->preference_present ? nei->preference :
  516. -1,
  517. target->level);
  518. continue;
  519. }
  520. wpa_printf(MSG_DEBUG,
  521. "WNM: Found an acceptable preferred transition candidate BSS "
  522. MACSTR " (RSSI %d)",
  523. MAC2STR(nei->bssid), target->level);
  524. return target;
  525. }
  526. return NULL;
  527. }
  528. static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
  529. {
  530. const u8 *ie_a, *ie_b;
  531. if (!a || !b)
  532. return 0;
  533. ie_a = wpa_bss_get_ie(a, eid);
  534. ie_b = wpa_bss_get_ie(b, eid);
  535. if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
  536. return 0;
  537. return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
  538. }
  539. static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  540. {
  541. u32 info = 0;
  542. info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
  543. /*
  544. * Leave the security and key scope bits unset to indicate that the
  545. * security information is not available.
  546. */
  547. if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
  548. info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
  549. if (bss->caps & WLAN_CAPABILITY_QOS)
  550. info |= NEI_REP_BSSID_INFO_QOS;
  551. if (bss->caps & WLAN_CAPABILITY_APSD)
  552. info |= NEI_REP_BSSID_INFO_APSD;
  553. if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
  554. info |= NEI_REP_BSSID_INFO_RM;
  555. if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
  556. info |= NEI_REP_BSSID_INFO_DELAYED_BA;
  557. if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
  558. info |= NEI_REP_BSSID_INFO_IMM_BA;
  559. if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
  560. info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
  561. if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
  562. info |= NEI_REP_BSSID_INFO_HT;
  563. return info;
  564. }
  565. static int wnm_add_nei_rep(u8 *buf, size_t len, const u8 *bssid, u32 bss_info,
  566. u8 op_class, u8 chan, u8 phy_type, u8 pref)
  567. {
  568. u8 *pos = buf;
  569. if (len < 18) {
  570. wpa_printf(MSG_DEBUG,
  571. "WNM: Not enough room for Neighbor Report element");
  572. return -1;
  573. }
  574. *pos++ = WLAN_EID_NEIGHBOR_REPORT;
  575. /* length: 13 for basic neighbor report + 3 for preference subelement */
  576. *pos++ = 16;
  577. os_memcpy(pos, bssid, ETH_ALEN);
  578. pos += ETH_ALEN;
  579. WPA_PUT_LE32(pos, bss_info);
  580. pos += 4;
  581. *pos++ = op_class;
  582. *pos++ = chan;
  583. *pos++ = phy_type;
  584. *pos++ = WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE;
  585. *pos++ = 1;
  586. *pos++ = pref;
  587. return pos - buf;
  588. }
  589. static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
  590. struct wpa_bss *bss, u8 *buf, size_t len,
  591. u8 pref)
  592. {
  593. const u8 *ie;
  594. u8 op_class, chan;
  595. int sec_chan = 0, vht = 0;
  596. enum phy_type phy_type;
  597. u32 info;
  598. struct ieee80211_ht_operation *ht_oper = NULL;
  599. struct ieee80211_vht_operation *vht_oper = NULL;
  600. ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
  601. if (ie && ie[1] >= 2) {
  602. ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
  603. if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
  604. sec_chan = 1;
  605. else if (ht_oper->ht_param &
  606. HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
  607. sec_chan = -1;
  608. }
  609. ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
  610. if (ie && ie[1] >= 1) {
  611. vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
  612. if (vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80MHZ ||
  613. vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_160MHZ ||
  614. vht_oper->vht_op_info_chwidth == VHT_CHANWIDTH_80P80MHZ)
  615. vht = vht_oper->vht_op_info_chwidth;
  616. }
  617. if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class,
  618. &chan) == NUM_HOSTAPD_MODES) {
  619. wpa_printf(MSG_DEBUG,
  620. "WNM: Cannot determine operating class and channel");
  621. return -2;
  622. }
  623. phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
  624. (vht_oper != NULL));
  625. if (phy_type == PHY_TYPE_UNSPECIFIED) {
  626. wpa_printf(MSG_DEBUG,
  627. "WNM: Cannot determine BSS phy type for Neighbor Report");
  628. return -2;
  629. }
  630. info = wnm_get_bss_info(wpa_s, bss);
  631. return wnm_add_nei_rep(buf, len, bss->bssid, info, op_class, chan,
  632. phy_type, pref);
  633. }
  634. static int wnm_add_cand_list(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
  635. {
  636. u8 *pos = buf;
  637. unsigned int i, pref = 255;
  638. struct os_reltime now;
  639. struct wpa_ssid *ssid = wpa_s->current_ssid;
  640. if (!ssid)
  641. return 0;
  642. /*
  643. * TODO: Define when scan results are no longer valid for the candidate
  644. * list.
  645. */
  646. os_get_reltime(&now);
  647. if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
  648. return 0;
  649. wpa_printf(MSG_DEBUG,
  650. "WNM: Add candidate list to BSS Transition Management Response frame");
  651. for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
  652. struct wpa_bss *bss = wpa_s->last_scan_res[i];
  653. int res;
  654. if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1)) {
  655. res = wnm_nei_rep_add_bss(wpa_s, bss, pos, len, pref--);
  656. if (res == -2)
  657. continue; /* could not build entry for BSS */
  658. if (res < 0)
  659. break; /* no more room for candidates */
  660. if (pref == 1)
  661. break;
  662. pos += res;
  663. len -= res;
  664. }
  665. }
  666. wpa_hexdump(MSG_DEBUG,
  667. "WNM: BSS Transition Management Response candidate list",
  668. buf, pos - buf);
  669. return pos - buf;
  670. }
  671. static void wnm_send_bss_transition_mgmt_resp(
  672. struct wpa_supplicant *wpa_s, u8 dialog_token,
  673. enum bss_trans_mgmt_status_code status, u8 delay,
  674. const u8 *target_bssid)
  675. {
  676. u8 buf[2000], *pos;
  677. struct ieee80211_mgmt *mgmt;
  678. size_t len;
  679. int res;
  680. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
  681. "to " MACSTR " dialog_token=%u status=%u delay=%d",
  682. MAC2STR(wpa_s->bssid), dialog_token, status, delay);
  683. if (!wpa_s->current_bss) {
  684. wpa_printf(MSG_DEBUG,
  685. "WNM: Current BSS not known - drop response");
  686. return;
  687. }
  688. mgmt = (struct ieee80211_mgmt *) buf;
  689. os_memset(&buf, 0, sizeof(buf));
  690. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  691. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  692. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  693. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  694. WLAN_FC_STYPE_ACTION);
  695. mgmt->u.action.category = WLAN_ACTION_WNM;
  696. mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
  697. mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
  698. mgmt->u.action.u.bss_tm_resp.status_code = status;
  699. mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
  700. pos = mgmt->u.action.u.bss_tm_resp.variable;
  701. if (target_bssid) {
  702. os_memcpy(pos, target_bssid, ETH_ALEN);
  703. pos += ETH_ALEN;
  704. } else if (status == WNM_BSS_TM_ACCEPT) {
  705. /*
  706. * P802.11-REVmc clarifies that the Target BSSID field is always
  707. * present when status code is zero, so use a fake value here if
  708. * no BSSID is yet known.
  709. */
  710. os_memset(pos, 0, ETH_ALEN);
  711. pos += ETH_ALEN;
  712. }
  713. if (status == WNM_BSS_TM_ACCEPT)
  714. pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
  715. #ifdef CONFIG_MBO
  716. if (status != WNM_BSS_TM_ACCEPT) {
  717. pos += wpas_mbo_ie_bss_trans_reject(
  718. wpa_s, pos, buf + sizeof(buf) - pos,
  719. MBO_TRANSITION_REJECT_REASON_UNSPECIFIED);
  720. }
  721. #endif /* CONFIG_MBO */
  722. len = pos - (u8 *) &mgmt->u.action.category;
  723. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  724. wpa_s->own_addr, wpa_s->bssid,
  725. &mgmt->u.action.category, len, 0);
  726. if (res < 0) {
  727. wpa_printf(MSG_DEBUG,
  728. "WNM: Failed to send BSS Transition Management Response");
  729. }
  730. }
  731. static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
  732. struct wpa_bss *bss, struct wpa_ssid *ssid,
  733. int after_new_scan)
  734. {
  735. wpa_dbg(wpa_s, MSG_DEBUG,
  736. "WNM: Transition to BSS " MACSTR
  737. " based on BSS Transition Management Request (old BSSID "
  738. MACSTR " after_new_scan=%d)",
  739. MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
  740. /* Send the BSS Management Response - Accept */
  741. if (wpa_s->wnm_reply) {
  742. wpa_s->wnm_reply = 0;
  743. wpa_printf(MSG_DEBUG,
  744. "WNM: Sending successful BSS Transition Management Response");
  745. wnm_send_bss_transition_mgmt_resp(wpa_s,
  746. wpa_s->wnm_dialog_token,
  747. WNM_BSS_TM_ACCEPT,
  748. 0, bss->bssid);
  749. }
  750. if (bss == wpa_s->current_bss) {
  751. wpa_printf(MSG_DEBUG,
  752. "WNM: Already associated with the preferred candidate");
  753. wnm_deallocate_memory(wpa_s);
  754. return;
  755. }
  756. wpa_s->reassociate = 1;
  757. wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
  758. wpa_supplicant_connect(wpa_s, bss, ssid);
  759. wnm_deallocate_memory(wpa_s);
  760. }
  761. int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
  762. {
  763. struct wpa_bss *bss;
  764. struct wpa_ssid *ssid = wpa_s->current_ssid;
  765. enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  766. if (!wpa_s->wnm_neighbor_report_elements)
  767. return 0;
  768. wpa_dbg(wpa_s, MSG_DEBUG,
  769. "WNM: Process scan results for BSS Transition Management");
  770. if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
  771. &wpa_s->scan_trigger_time)) {
  772. wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
  773. wnm_deallocate_memory(wpa_s);
  774. return 0;
  775. }
  776. if (!wpa_s->current_bss ||
  777. os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
  778. ETH_ALEN) != 0) {
  779. wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
  780. return 0;
  781. }
  782. /* Compare the Neighbor Report and scan results */
  783. bss = compare_scan_neighbor_results(wpa_s, 0);
  784. if (!bss) {
  785. wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
  786. status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
  787. goto send_bss_resp_fail;
  788. }
  789. /* Associate to the network */
  790. wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
  791. return 1;
  792. send_bss_resp_fail:
  793. if (!reply_on_fail)
  794. return 0;
  795. /* Send reject response for all the failures */
  796. if (wpa_s->wnm_reply) {
  797. wpa_s->wnm_reply = 0;
  798. wnm_send_bss_transition_mgmt_resp(wpa_s,
  799. wpa_s->wnm_dialog_token,
  800. status, 0, NULL);
  801. }
  802. wnm_deallocate_memory(wpa_s);
  803. return 0;
  804. }
  805. static int cand_pref_compar(const void *a, const void *b)
  806. {
  807. const struct neighbor_report *aa = a;
  808. const struct neighbor_report *bb = b;
  809. if (!aa->preference_present && !bb->preference_present)
  810. return 0;
  811. if (!aa->preference_present)
  812. return 1;
  813. if (!bb->preference_present)
  814. return -1;
  815. if (bb->preference > aa->preference)
  816. return 1;
  817. if (bb->preference < aa->preference)
  818. return -1;
  819. return 0;
  820. }
  821. static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
  822. {
  823. if (!wpa_s->wnm_neighbor_report_elements)
  824. return;
  825. qsort(wpa_s->wnm_neighbor_report_elements,
  826. wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
  827. cand_pref_compar);
  828. }
  829. static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
  830. {
  831. unsigned int i;
  832. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
  833. if (!wpa_s->wnm_neighbor_report_elements)
  834. return;
  835. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  836. struct neighbor_report *nei;
  837. nei = &wpa_s->wnm_neighbor_report_elements[i];
  838. wpa_printf(MSG_DEBUG, "%u: " MACSTR
  839. " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
  840. i, MAC2STR(nei->bssid), nei->bssid_info,
  841. nei->regulatory_class,
  842. nei->channel_number, nei->phy_type,
  843. nei->preference_present ? nei->preference : -1,
  844. nei->freq);
  845. }
  846. }
  847. static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
  848. {
  849. unsigned int i;
  850. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  851. struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
  852. int j;
  853. for (j = 0; j < mode->num_channels; j++) {
  854. struct hostapd_channel_data *chan;
  855. chan = &mode->channels[j];
  856. if (chan->freq == freq &&
  857. !(chan->flag & HOSTAPD_CHAN_DISABLED))
  858. return 1;
  859. }
  860. }
  861. return 0;
  862. }
  863. static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
  864. {
  865. int *freqs;
  866. int num_freqs = 0;
  867. unsigned int i;
  868. if (!wpa_s->wnm_neighbor_report_elements)
  869. return;
  870. if (wpa_s->hw.modes == NULL)
  871. return;
  872. os_free(wpa_s->next_scan_freqs);
  873. wpa_s->next_scan_freqs = NULL;
  874. freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
  875. if (freqs == NULL)
  876. return;
  877. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  878. struct neighbor_report *nei;
  879. nei = &wpa_s->wnm_neighbor_report_elements[i];
  880. if (nei->freq <= 0) {
  881. wpa_printf(MSG_DEBUG,
  882. "WNM: Unknown neighbor operating frequency for "
  883. MACSTR " - scan all channels",
  884. MAC2STR(nei->bssid));
  885. os_free(freqs);
  886. return;
  887. }
  888. if (chan_supported(wpa_s, nei->freq))
  889. add_freq(freqs, &num_freqs, nei->freq);
  890. }
  891. if (num_freqs == 0) {
  892. os_free(freqs);
  893. return;
  894. }
  895. wpa_printf(MSG_DEBUG,
  896. "WNM: Scan %d frequencies based on transition candidate list",
  897. num_freqs);
  898. wpa_s->next_scan_freqs = freqs;
  899. }
  900. static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
  901. {
  902. struct wpa_scan_results *scan_res;
  903. struct wpa_bss *bss;
  904. struct wpa_ssid *ssid = wpa_s->current_ssid;
  905. u8 i, found = 0;
  906. size_t j;
  907. wpa_dbg(wpa_s, MSG_DEBUG,
  908. "WNM: Fetch current scan results from the driver for checking transition candidates");
  909. scan_res = wpa_drv_get_scan_results2(wpa_s);
  910. if (!scan_res) {
  911. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
  912. return 0;
  913. }
  914. if (scan_res->fetch_time.sec == 0)
  915. os_get_reltime(&scan_res->fetch_time);
  916. filter_scan_res(wpa_s, scan_res);
  917. for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
  918. struct neighbor_report *nei;
  919. nei = &wpa_s->wnm_neighbor_report_elements[i];
  920. if (nei->preference_present && nei->preference == 0)
  921. continue;
  922. for (j = 0; j < scan_res->num; j++) {
  923. struct wpa_scan_res *res;
  924. const u8 *ssid_ie;
  925. res = scan_res->res[j];
  926. if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
  927. res->age > WNM_SCAN_RESULT_AGE * 1000)
  928. continue;
  929. bss = wpa_s->current_bss;
  930. ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  931. if (bss && ssid_ie &&
  932. (bss->ssid_len != ssid_ie[1] ||
  933. os_memcmp(bss->ssid, ssid_ie + 2,
  934. bss->ssid_len) != 0))
  935. continue;
  936. /* Potential candidate found */
  937. found = 1;
  938. scan_snr(res);
  939. scan_est_throughput(wpa_s, res);
  940. wpa_bss_update_scan_res(wpa_s, res,
  941. &scan_res->fetch_time);
  942. }
  943. }
  944. wpa_scan_results_free(scan_res);
  945. if (!found) {
  946. wpa_dbg(wpa_s, MSG_DEBUG,
  947. "WNM: No transition candidate matches existing scan results");
  948. return 0;
  949. }
  950. bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE);
  951. if (!bss) {
  952. wpa_dbg(wpa_s, MSG_DEBUG,
  953. "WNM: Comparison of scan results against transition candidates did not find matches");
  954. return 0;
  955. }
  956. /* Associate to the network */
  957. wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
  958. return 1;
  959. }
  960. static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
  961. const u8 *pos, const u8 *end,
  962. int reply)
  963. {
  964. unsigned int beacon_int;
  965. u8 valid_int;
  966. #ifdef CONFIG_MBO
  967. const u8 *vendor;
  968. #endif /* CONFIG_MBO */
  969. if (end - pos < 5)
  970. return;
  971. if (wpa_s->current_bss)
  972. beacon_int = wpa_s->current_bss->beacon_int;
  973. else
  974. beacon_int = 100; /* best guess */
  975. wpa_s->wnm_dialog_token = pos[0];
  976. wpa_s->wnm_mode = pos[1];
  977. wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
  978. valid_int = pos[4];
  979. wpa_s->wnm_reply = reply;
  980. wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
  981. "dialog_token=%u request_mode=0x%x "
  982. "disassoc_timer=%u validity_interval=%u",
  983. wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
  984. wpa_s->wnm_dissoc_timer, valid_int);
  985. pos += 5;
  986. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  987. if (end - pos < 12) {
  988. wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
  989. return;
  990. }
  991. os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
  992. pos += 12; /* BSS Termination Duration */
  993. }
  994. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
  995. char url[256];
  996. if (end - pos < 1 || 1 + pos[0] > end - pos) {
  997. wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
  998. "Management Request (URL)");
  999. return;
  1000. }
  1001. os_memcpy(url, pos + 1, pos[0]);
  1002. url[pos[0]] = '\0';
  1003. pos += 1 + pos[0];
  1004. wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
  1005. wpa_sm_pmf_enabled(wpa_s->wpa),
  1006. wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
  1007. }
  1008. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  1009. wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
  1010. "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
  1011. if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
  1012. /* TODO: mark current BSS less preferred for
  1013. * selection */
  1014. wpa_printf(MSG_DEBUG, "Trying to find another BSS");
  1015. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1016. }
  1017. }
  1018. #ifdef CONFIG_MBO
  1019. vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
  1020. if (vendor)
  1021. wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
  1022. #endif /* CONFIG_MBO */
  1023. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
  1024. unsigned int valid_ms;
  1025. wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
  1026. wnm_deallocate_memory(wpa_s);
  1027. wpa_s->wnm_neighbor_report_elements = os_calloc(
  1028. WNM_MAX_NEIGHBOR_REPORT,
  1029. sizeof(struct neighbor_report));
  1030. if (wpa_s->wnm_neighbor_report_elements == NULL)
  1031. return;
  1032. while (end - pos >= 2 &&
  1033. wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
  1034. {
  1035. u8 tag = *pos++;
  1036. u8 len = *pos++;
  1037. wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
  1038. tag);
  1039. if (len > end - pos) {
  1040. wpa_printf(MSG_DEBUG, "WNM: Truncated request");
  1041. return;
  1042. }
  1043. if (tag == WLAN_EID_NEIGHBOR_REPORT) {
  1044. struct neighbor_report *rep;
  1045. rep = &wpa_s->wnm_neighbor_report_elements[
  1046. wpa_s->wnm_num_neighbor_report];
  1047. wnm_parse_neighbor_report(wpa_s, pos, len, rep);
  1048. wpa_s->wnm_num_neighbor_report++;
  1049. }
  1050. pos += len;
  1051. }
  1052. if (!wpa_s->wnm_num_neighbor_report) {
  1053. wpa_printf(MSG_DEBUG,
  1054. "WNM: Candidate list included bit is set, but no candidates found");
  1055. wnm_send_bss_transition_mgmt_resp(
  1056. wpa_s, wpa_s->wnm_dialog_token,
  1057. WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
  1058. 0, NULL);
  1059. return;
  1060. }
  1061. wnm_sort_cand_list(wpa_s);
  1062. wnm_dump_cand_list(wpa_s);
  1063. valid_ms = valid_int * beacon_int * 128 / 125;
  1064. wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
  1065. valid_ms);
  1066. os_get_reltime(&wpa_s->wnm_cand_valid_until);
  1067. wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
  1068. wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
  1069. wpa_s->wnm_cand_valid_until.sec +=
  1070. wpa_s->wnm_cand_valid_until.usec / 1000000;
  1071. wpa_s->wnm_cand_valid_until.usec %= 1000000;
  1072. os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
  1073. /*
  1074. * Fetch the latest scan results from the kernel and check for
  1075. * candidates based on those results first. This can help in
  1076. * finding more up-to-date information should the driver has
  1077. * done some internal scanning operations after the last scan
  1078. * result update in wpa_supplicant.
  1079. */
  1080. if (wnm_fetch_scan_results(wpa_s) > 0)
  1081. return;
  1082. /*
  1083. * Try to use previously received scan results, if they are
  1084. * recent enough to use for a connection.
  1085. */
  1086. if (wpa_s->last_scan_res_used > 0) {
  1087. struct os_reltime now;
  1088. os_get_reltime(&now);
  1089. if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
  1090. wpa_printf(MSG_DEBUG,
  1091. "WNM: Try to use recent scan results");
  1092. if (wnm_scan_process(wpa_s, 0) > 0)
  1093. return;
  1094. wpa_printf(MSG_DEBUG,
  1095. "WNM: No match in previous scan results - try a new scan");
  1096. }
  1097. }
  1098. wnm_set_scan_freqs(wpa_s);
  1099. if (wpa_s->wnm_num_neighbor_report == 1) {
  1100. os_memcpy(wpa_s->next_scan_bssid,
  1101. wpa_s->wnm_neighbor_report_elements[0].bssid,
  1102. ETH_ALEN);
  1103. wpa_printf(MSG_DEBUG,
  1104. "WNM: Scan only for a specific BSSID since there is only a single candidate "
  1105. MACSTR, MAC2STR(wpa_s->next_scan_bssid));
  1106. }
  1107. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1108. } else if (reply) {
  1109. enum bss_trans_mgmt_status_code status;
  1110. if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
  1111. status = WNM_BSS_TM_ACCEPT;
  1112. else {
  1113. wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
  1114. status = WNM_BSS_TM_REJECT_UNSPECIFIED;
  1115. }
  1116. wnm_send_bss_transition_mgmt_resp(wpa_s,
  1117. wpa_s->wnm_dialog_token,
  1118. status, 0, NULL);
  1119. }
  1120. }
  1121. int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
  1122. u8 query_reason, int cand_list)
  1123. {
  1124. u8 buf[2000], *pos;
  1125. struct ieee80211_mgmt *mgmt;
  1126. size_t len;
  1127. int ret;
  1128. wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
  1129. MACSTR " query_reason=%u%s",
  1130. MAC2STR(wpa_s->bssid), query_reason,
  1131. cand_list ? " candidate list" : "");
  1132. mgmt = (struct ieee80211_mgmt *) buf;
  1133. os_memset(&buf, 0, sizeof(buf));
  1134. os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
  1135. os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
  1136. os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
  1137. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  1138. WLAN_FC_STYPE_ACTION);
  1139. mgmt->u.action.category = WLAN_ACTION_WNM;
  1140. mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
  1141. mgmt->u.action.u.bss_tm_query.dialog_token = 1;
  1142. mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
  1143. pos = mgmt->u.action.u.bss_tm_query.variable;
  1144. if (cand_list)
  1145. pos += wnm_add_cand_list(wpa_s, pos, buf + sizeof(buf) - pos);
  1146. len = pos - (u8 *) &mgmt->u.action.category;
  1147. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  1148. wpa_s->own_addr, wpa_s->bssid,
  1149. &mgmt->u.action.category, len, 0);
  1150. return ret;
  1151. }
  1152. static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
  1153. const u8 *sa, const u8 *data,
  1154. int len)
  1155. {
  1156. const u8 *pos, *end, *next;
  1157. u8 ie, ie_len;
  1158. pos = data;
  1159. end = data + len;
  1160. while (end - pos > 1) {
  1161. ie = *pos++;
  1162. ie_len = *pos++;
  1163. wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
  1164. ie, ie_len);
  1165. if (ie_len > end - pos) {
  1166. wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
  1167. "subelement");
  1168. break;
  1169. }
  1170. next = pos + ie_len;
  1171. if (ie_len < 4) {
  1172. pos = next;
  1173. continue;
  1174. }
  1175. wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
  1176. WPA_GET_BE24(pos), pos[3]);
  1177. #ifdef CONFIG_HS20
  1178. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
  1179. WPA_GET_BE24(pos) == OUI_WFA &&
  1180. pos[3] == HS20_WNM_SUB_REM_NEEDED) {
  1181. /* Subscription Remediation subelement */
  1182. const u8 *ie_end;
  1183. u8 url_len;
  1184. char *url;
  1185. u8 osu_method;
  1186. wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
  1187. "subelement");
  1188. ie_end = pos + ie_len;
  1189. pos += 4;
  1190. url_len = *pos++;
  1191. if (url_len == 0) {
  1192. wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
  1193. url = NULL;
  1194. osu_method = 1;
  1195. } else {
  1196. if (url_len + 1 > ie_end - pos) {
  1197. wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
  1198. url_len,
  1199. (int) (ie_end - pos));
  1200. break;
  1201. }
  1202. url = os_malloc(url_len + 1);
  1203. if (url == NULL)
  1204. break;
  1205. os_memcpy(url, pos, url_len);
  1206. url[url_len] = '\0';
  1207. osu_method = pos[url_len];
  1208. }
  1209. hs20_rx_subscription_remediation(wpa_s, url,
  1210. osu_method);
  1211. os_free(url);
  1212. pos = next;
  1213. continue;
  1214. }
  1215. if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
  1216. WPA_GET_BE24(pos) == OUI_WFA &&
  1217. pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
  1218. const u8 *ie_end;
  1219. u8 url_len;
  1220. char *url;
  1221. u8 code;
  1222. u16 reauth_delay;
  1223. ie_end = pos + ie_len;
  1224. pos += 4;
  1225. code = *pos++;
  1226. reauth_delay = WPA_GET_LE16(pos);
  1227. pos += 2;
  1228. url_len = *pos++;
  1229. wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
  1230. "Imminent - Reason Code %u "
  1231. "Re-Auth Delay %u URL Length %u",
  1232. code, reauth_delay, url_len);
  1233. if (url_len > ie_end - pos)
  1234. break;
  1235. url = os_malloc(url_len + 1);
  1236. if (url == NULL)
  1237. break;
  1238. os_memcpy(url, pos, url_len);
  1239. url[url_len] = '\0';
  1240. hs20_rx_deauth_imminent_notice(wpa_s, code,
  1241. reauth_delay, url);
  1242. os_free(url);
  1243. pos = next;
  1244. continue;
  1245. }
  1246. #endif /* CONFIG_HS20 */
  1247. pos = next;
  1248. }
  1249. }
  1250. static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
  1251. const u8 *sa, const u8 *frm, int len)
  1252. {
  1253. const u8 *pos, *end;
  1254. u8 dialog_token, type;
  1255. /* Dialog Token [1] | Type [1] | Subelements */
  1256. if (len < 2 || sa == NULL)
  1257. return;
  1258. end = frm + len;
  1259. pos = frm;
  1260. dialog_token = *pos++;
  1261. type = *pos++;
  1262. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
  1263. "(dialog_token %u type %u sa " MACSTR ")",
  1264. dialog_token, type, MAC2STR(sa));
  1265. wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
  1266. pos, end - pos);
  1267. if (wpa_s->wpa_state != WPA_COMPLETED ||
  1268. os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
  1269. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
  1270. "from our AP - ignore it");
  1271. return;
  1272. }
  1273. switch (type) {
  1274. case 1:
  1275. ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
  1276. break;
  1277. default:
  1278. wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
  1279. "WNM-Notification type %u", type);
  1280. break;
  1281. }
  1282. }
  1283. void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
  1284. const struct ieee80211_mgmt *mgmt, size_t len)
  1285. {
  1286. const u8 *pos, *end;
  1287. u8 act;
  1288. if (len < IEEE80211_HDRLEN + 2)
  1289. return;
  1290. pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
  1291. act = *pos++;
  1292. end = ((const u8 *) mgmt) + len;
  1293. wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
  1294. act, MAC2STR(mgmt->sa));
  1295. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  1296. os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
  1297. wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
  1298. "frame");
  1299. return;
  1300. }
  1301. switch (act) {
  1302. case WNM_BSS_TRANS_MGMT_REQ:
  1303. ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
  1304. !(mgmt->da[0] & 0x01));
  1305. break;
  1306. case WNM_SLEEP_MODE_RESP:
  1307. ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
  1308. break;
  1309. case WNM_NOTIFICATION_REQ:
  1310. ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
  1311. break;
  1312. default:
  1313. wpa_printf(MSG_ERROR, "WNM: Unknown request");
  1314. break;
  1315. }
  1316. }