wnm_sta.c 45 KB

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