mbo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * wpa_supplicant - MBO
  3. *
  4. * Copyright(c) 2015 Intel Deutschland GmbH
  5. * Contact Information:
  6. * Intel Linux Wireless <ilw@linux.intel.com>
  7. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  8. *
  9. * This software may be distributed under the terms of the BSD license.
  10. * See README for more details.
  11. */
  12. #include "utils/includes.h"
  13. #include "utils/common.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "common/gas.h"
  16. #include "config.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "driver_i.h"
  19. #include "bss.h"
  20. #include "scan.h"
  21. /* type + length + oui + oui type */
  22. #define MBO_IE_HEADER 6
  23. static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
  24. {
  25. if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
  26. return -1;
  27. /* Only checking the validity of the channel and oper_class */
  28. if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
  29. return -1;
  30. return 0;
  31. }
  32. const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
  33. {
  34. const u8 *mbo, *end;
  35. if (!bss)
  36. return NULL;
  37. mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
  38. if (!mbo)
  39. return NULL;
  40. end = mbo + 2 + mbo[1];
  41. mbo += MBO_IE_HEADER;
  42. return get_ie(mbo, end - mbo, attr);
  43. }
  44. static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
  45. struct wpabuf *mbo,
  46. u8 start, u8 end)
  47. {
  48. u8 i;
  49. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
  50. for (i = start; i < end; i++)
  51. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
  52. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
  53. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
  54. }
  55. static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
  56. struct wpabuf *mbo, u8 start, u8 end)
  57. {
  58. size_t size = end - start + 3;
  59. if (size + 2 > wpabuf_tailroom(mbo))
  60. return;
  61. wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
  62. wpabuf_put_u8(mbo, size); /* Length */
  63. wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
  64. }
  65. static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
  66. {
  67. wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
  68. wpabuf_put_u8(mbo, len); /* Length */
  69. wpabuf_put_be24(mbo, OUI_WFA);
  70. wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
  71. }
  72. static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
  73. struct wpabuf *mbo, u8 start,
  74. u8 end)
  75. {
  76. size_t size = end - start + 7;
  77. if (size + 2 > wpabuf_tailroom(mbo))
  78. return;
  79. wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
  80. wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
  81. }
  82. static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
  83. struct wpabuf *mbo, int subelement)
  84. {
  85. u8 i, start = 0;
  86. struct wpa_mbo_non_pref_channel *start_pref;
  87. if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
  88. if (subelement)
  89. wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
  90. return;
  91. }
  92. start_pref = &wpa_s->non_pref_chan[0];
  93. for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
  94. struct wpa_mbo_non_pref_channel *non_pref = NULL;
  95. if (i < wpa_s->non_pref_chan_num)
  96. non_pref = &wpa_s->non_pref_chan[i];
  97. if (!non_pref ||
  98. non_pref->oper_class != start_pref->oper_class ||
  99. non_pref->reason != start_pref->reason ||
  100. non_pref->preference != start_pref->preference) {
  101. if (subelement)
  102. wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
  103. start, i);
  104. else
  105. wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
  106. i);
  107. if (!non_pref)
  108. return;
  109. start = i;
  110. start_pref = non_pref;
  111. }
  112. }
  113. }
  114. int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
  115. {
  116. struct wpabuf *mbo;
  117. int res;
  118. if (len < MBO_IE_HEADER + 3 + 7 +
  119. ((wpa_s->enable_oce & OCE_STA) ? 3 : 0))
  120. return 0;
  121. /* Leave room for the MBO IE header */
  122. mbo = wpabuf_alloc(len - MBO_IE_HEADER);
  123. if (!mbo)
  124. return 0;
  125. /* Add non-preferred channels attribute */
  126. wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
  127. /*
  128. * Send cellular capabilities attribute even if AP does not advertise
  129. * cellular capabilities.
  130. */
  131. wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
  132. wpabuf_put_u8(mbo, 1);
  133. wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
  134. /* Add OCE capability indication attribute if OCE is enabled */
  135. if (wpa_s->enable_oce & OCE_STA) {
  136. wpabuf_put_u8(mbo, OCE_ATTR_ID_CAPA_IND);
  137. wpabuf_put_u8(mbo, 1);
  138. wpabuf_put_u8(mbo, OCE_RELEASE);
  139. }
  140. res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
  141. if (!res)
  142. wpa_printf(MSG_ERROR, "Failed to add MBO/OCE IE");
  143. wpabuf_free(mbo);
  144. return res;
  145. }
  146. static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
  147. const u8 *data, size_t len)
  148. {
  149. struct wpabuf *buf;
  150. int res;
  151. /*
  152. * Send WNM-Notification Request frame only in case of a change in
  153. * non-preferred channels list during association, if the AP supports
  154. * MBO.
  155. */
  156. if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
  157. !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
  158. return;
  159. buf = wpabuf_alloc(4 + len);
  160. if (!buf)
  161. return;
  162. wpabuf_put_u8(buf, WLAN_ACTION_WNM);
  163. wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
  164. wpa_s->mbo_wnm_token++;
  165. if (wpa_s->mbo_wnm_token == 0)
  166. wpa_s->mbo_wnm_token++;
  167. wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
  168. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
  169. wpabuf_put_data(buf, data, len);
  170. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  171. wpa_s->own_addr, wpa_s->bssid,
  172. wpabuf_head(buf), wpabuf_len(buf), 0);
  173. if (res < 0)
  174. wpa_printf(MSG_DEBUG,
  175. "Failed to send WNM-Notification Request frame with non-preferred channel list");
  176. wpabuf_free(buf);
  177. }
  178. static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
  179. {
  180. struct wpabuf *buf;
  181. buf = wpabuf_alloc(512);
  182. if (!buf)
  183. return;
  184. wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
  185. wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
  186. wpabuf_len(buf));
  187. wpabuf_free(buf);
  188. }
  189. static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
  190. struct wpa_mbo_non_pref_channel *b)
  191. {
  192. return a->oper_class == b->oper_class && a->chan == b->chan;
  193. }
  194. /*
  195. * wpa_non_pref_chan_cmp - Compare two channels for sorting
  196. *
  197. * In MBO IE non-preferred channel subelement we can put many channels in an
  198. * attribute if they are in the same operating class and have the same
  199. * preference and reason. To make it easy for the functions that build
  200. * the IE attributes and WNM Request subelements, save the channels sorted
  201. * by their oper_class and reason.
  202. */
  203. static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
  204. {
  205. const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
  206. if (a->oper_class != b->oper_class)
  207. return a->oper_class - b->oper_class;
  208. if (a->reason != b->reason)
  209. return a->reason - b->reason;
  210. return a->preference - b->preference;
  211. }
  212. int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
  213. const char *non_pref_chan)
  214. {
  215. char *cmd, *token, *context = NULL;
  216. struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
  217. size_t num = 0, size = 0;
  218. unsigned i;
  219. wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
  220. non_pref_chan ? non_pref_chan : "N/A");
  221. /*
  222. * The shortest channel configuration is 7 characters - 3 colons and
  223. * 4 values.
  224. */
  225. if (!non_pref_chan || os_strlen(non_pref_chan) < 7)
  226. goto update;
  227. cmd = os_strdup(non_pref_chan);
  228. if (!cmd)
  229. return -1;
  230. while ((token = str_token(cmd, " ", &context))) {
  231. struct wpa_mbo_non_pref_channel *chan;
  232. int ret;
  233. unsigned int _oper_class;
  234. unsigned int _chan;
  235. unsigned int _preference;
  236. unsigned int _reason;
  237. if (num == size) {
  238. size = size ? size * 2 : 1;
  239. tmp_chans = os_realloc_array(chans, size,
  240. sizeof(*chans));
  241. if (!tmp_chans) {
  242. wpa_printf(MSG_ERROR,
  243. "Couldn't reallocate non_pref_chan");
  244. goto fail;
  245. }
  246. chans = tmp_chans;
  247. }
  248. chan = &chans[num];
  249. ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
  250. &_chan, &_preference, &_reason);
  251. if (ret != 4 ||
  252. _oper_class > 255 || _chan > 255 ||
  253. _preference > 255 || _reason > 65535 ) {
  254. wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
  255. token);
  256. goto fail;
  257. }
  258. chan->oper_class = _oper_class;
  259. chan->chan = _chan;
  260. chan->preference = _preference;
  261. chan->reason = _reason;
  262. if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
  263. chan->chan, chan->reason)) {
  264. wpa_printf(MSG_ERROR,
  265. "Invalid non_pref_chan: oper class %d chan %d reason %d",
  266. chan->oper_class, chan->chan, chan->reason);
  267. goto fail;
  268. }
  269. for (i = 0; i < num; i++)
  270. if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
  271. break;
  272. if (i != num) {
  273. wpa_printf(MSG_ERROR,
  274. "oper class %d chan %d is duplicated",
  275. chan->oper_class, chan->chan);
  276. goto fail;
  277. }
  278. num++;
  279. }
  280. os_free(cmd);
  281. if (chans) {
  282. qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
  283. wpa_non_pref_chan_cmp);
  284. }
  285. update:
  286. os_free(wpa_s->non_pref_chan);
  287. wpa_s->non_pref_chan = chans;
  288. wpa_s->non_pref_chan_num = num;
  289. wpas_mbo_non_pref_chan_changed(wpa_s);
  290. return 0;
  291. fail:
  292. os_free(chans);
  293. os_free(cmd);
  294. return -1;
  295. }
  296. void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
  297. {
  298. u8 *len;
  299. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  300. len = wpabuf_put(ie, 1);
  301. wpabuf_put_be24(ie, OUI_WFA);
  302. wpabuf_put_u8(ie, MBO_OUI_TYPE);
  303. wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
  304. wpabuf_put_u8(ie, 1);
  305. wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
  306. if (wpa_s->enable_oce & OCE_STA) {
  307. wpabuf_put_u8(ie, OCE_ATTR_ID_CAPA_IND);
  308. wpabuf_put_u8(ie, 1);
  309. wpabuf_put_u8(ie, OCE_RELEASE);
  310. }
  311. *len = (u8 *) wpabuf_put(ie, 0) - len - 1;
  312. }
  313. void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
  314. size_t len)
  315. {
  316. const u8 *pos, *cell_pref = NULL;
  317. u8 id, elen;
  318. u16 disallowed_sec = 0;
  319. if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
  320. mbo_ie[3] != MBO_OUI_TYPE)
  321. return;
  322. pos = mbo_ie + 4;
  323. len -= 4;
  324. while (len >= 2) {
  325. id = *pos++;
  326. elen = *pos++;
  327. len -= 2;
  328. if (elen > len)
  329. goto fail;
  330. switch (id) {
  331. case MBO_ATTR_ID_CELL_DATA_PREF:
  332. if (elen != 1)
  333. goto fail;
  334. if (wpa_s->conf->mbo_cell_capa ==
  335. MBO_CELL_CAPA_AVAILABLE)
  336. cell_pref = pos;
  337. else
  338. wpa_printf(MSG_DEBUG,
  339. "MBO: Station does not support Cellular data connection");
  340. break;
  341. case MBO_ATTR_ID_TRANSITION_REASON:
  342. if (elen != 1)
  343. goto fail;
  344. wpa_s->wnm_mbo_trans_reason_present = 1;
  345. wpa_s->wnm_mbo_transition_reason = *pos;
  346. break;
  347. case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
  348. if (elen != 2)
  349. goto fail;
  350. if (wpa_s->wnm_mode &
  351. WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  352. wpa_printf(MSG_DEBUG,
  353. "MBO: Unexpected association retry delay, BSS is terminating");
  354. goto fail;
  355. } else if (wpa_s->wnm_mode &
  356. WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  357. disallowed_sec = WPA_GET_LE16(pos);
  358. wpa_printf(MSG_DEBUG,
  359. "MBO: Association retry delay: %u",
  360. disallowed_sec);
  361. } else {
  362. wpa_printf(MSG_DEBUG,
  363. "MBO: Association retry delay attribute not in disassoc imminent mode");
  364. }
  365. break;
  366. case MBO_ATTR_ID_AP_CAPA_IND:
  367. case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
  368. case MBO_ATTR_ID_CELL_DATA_CAPA:
  369. case MBO_ATTR_ID_ASSOC_DISALLOW:
  370. case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
  371. wpa_printf(MSG_DEBUG,
  372. "MBO: Attribute %d should not be included in BTM Request frame",
  373. id);
  374. break;
  375. default:
  376. wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
  377. id);
  378. return;
  379. }
  380. pos += elen;
  381. len -= elen;
  382. }
  383. if (cell_pref)
  384. wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
  385. *cell_pref);
  386. if (wpa_s->wnm_mbo_trans_reason_present)
  387. wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
  388. wpa_s->wnm_mbo_transition_reason);
  389. if (disallowed_sec && wpa_s->current_bss)
  390. wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
  391. disallowed_sec);
  392. return;
  393. fail:
  394. wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
  395. id, elen, len);
  396. }
  397. size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
  398. size_t len,
  399. enum mbo_transition_reject_reason reason)
  400. {
  401. u8 reject_attr[3];
  402. reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
  403. reject_attr[1] = 1;
  404. reject_attr[2] = reason;
  405. return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
  406. }
  407. void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
  408. {
  409. u8 cell_capa[7];
  410. if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
  411. wpa_printf(MSG_DEBUG,
  412. "MBO: Cellular capability already set to %u",
  413. mbo_cell_capa);
  414. return;
  415. }
  416. wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
  417. cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
  418. cell_capa[1] = 5; /* Length */
  419. WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
  420. cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
  421. cell_capa[6] = mbo_cell_capa;
  422. wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
  423. wpa_supplicant_set_default_scan_ies(wpa_s);
  424. }
  425. struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
  426. struct wpa_bss *bss, u32 mbo_subtypes)
  427. {
  428. struct wpabuf *anqp_buf;
  429. u8 *len_pos;
  430. u8 i;
  431. if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
  432. wpa_printf(MSG_INFO, "MBO: " MACSTR
  433. " does not support MBO - cannot request MBO ANQP elements from it",
  434. MAC2STR(bss->bssid));
  435. return NULL;
  436. }
  437. /* Allocate size for the maximum case - all MBO subtypes are set */
  438. anqp_buf = wpabuf_alloc(9 + MAX_MBO_ANQP_SUBTYPE);
  439. if (!anqp_buf)
  440. return NULL;
  441. len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
  442. wpabuf_put_be24(anqp_buf, OUI_WFA);
  443. wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
  444. wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_QUERY_LIST);
  445. /* The first valid MBO subtype is 1 */
  446. for (i = 1; i <= MAX_MBO_ANQP_SUBTYPE; i++) {
  447. if (mbo_subtypes & BIT(i))
  448. wpabuf_put_u8(anqp_buf, i);
  449. }
  450. gas_anqp_set_element_len(anqp_buf, len_pos);
  451. return anqp_buf;
  452. }
  453. void mbo_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
  454. struct wpa_bss *bss, const u8 *sa,
  455. const u8 *data, size_t slen)
  456. {
  457. const u8 *pos = data;
  458. u8 subtype;
  459. if (slen < 1)
  460. return;
  461. subtype = *pos++;
  462. slen--;
  463. switch (subtype) {
  464. case MBO_ANQP_SUBTYPE_CELL_CONN_PREF:
  465. if (slen < 1)
  466. break;
  467. wpa_msg(wpa_s, MSG_INFO, RX_MBO_ANQP MACSTR
  468. " cell_conn_pref=%u", MAC2STR(sa), *pos);
  469. break;
  470. default:
  471. wpa_printf(MSG_DEBUG, "MBO: Unsupported ANQP subtype %u",
  472. subtype);
  473. break;
  474. }
  475. }