wnm_sta.c 36 KB

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