mbo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. return 0;
  120. /* Leave room for the MBO IE header */
  121. mbo = wpabuf_alloc(len - MBO_IE_HEADER);
  122. if (!mbo)
  123. return 0;
  124. /* Add non-preferred channels attribute */
  125. wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
  126. /*
  127. * Send cellular capabilities attribute even if AP does not advertise
  128. * cellular capabilities.
  129. */
  130. wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
  131. wpabuf_put_u8(mbo, 1);
  132. wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
  133. res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
  134. if (!res)
  135. wpa_printf(MSG_ERROR, "Failed to add MBO IE");
  136. wpabuf_free(mbo);
  137. return res;
  138. }
  139. static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
  140. const u8 *data, size_t len)
  141. {
  142. struct wpabuf *buf;
  143. int res;
  144. /*
  145. * Send WNM-Notification Request frame only in case of a change in
  146. * non-preferred channels list during association, if the AP supports
  147. * MBO.
  148. */
  149. if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
  150. !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
  151. return;
  152. buf = wpabuf_alloc(4 + len);
  153. if (!buf)
  154. return;
  155. wpabuf_put_u8(buf, WLAN_ACTION_WNM);
  156. wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
  157. wpa_s->mbo_wnm_token++;
  158. if (wpa_s->mbo_wnm_token == 0)
  159. wpa_s->mbo_wnm_token++;
  160. wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
  161. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
  162. wpabuf_put_data(buf, data, len);
  163. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  164. wpa_s->own_addr, wpa_s->bssid,
  165. wpabuf_head(buf), wpabuf_len(buf), 0);
  166. if (res < 0)
  167. wpa_printf(MSG_DEBUG,
  168. "Failed to send WNM-Notification Request frame with non-preferred channel list");
  169. wpabuf_free(buf);
  170. }
  171. static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
  172. {
  173. struct wpabuf *buf;
  174. buf = wpabuf_alloc(512);
  175. if (!buf)
  176. return;
  177. wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
  178. wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
  179. wpabuf_len(buf));
  180. wpabuf_free(buf);
  181. }
  182. static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
  183. struct wpa_mbo_non_pref_channel *b)
  184. {
  185. return a->oper_class == b->oper_class && a->chan == b->chan;
  186. }
  187. /*
  188. * wpa_non_pref_chan_cmp - Compare two channels for sorting
  189. *
  190. * In MBO IE non-preferred channel subelement we can put many channels in an
  191. * attribute if they are in the same operating class and have the same
  192. * preference and reason. To make it easy for the functions that build
  193. * the IE attributes and WNM Request subelements, save the channels sorted
  194. * by their oper_class and reason.
  195. */
  196. static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
  197. {
  198. const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
  199. if (a->oper_class != b->oper_class)
  200. return a->oper_class - b->oper_class;
  201. if (a->reason != b->reason)
  202. return a->reason - b->reason;
  203. return a->preference - b->preference;
  204. }
  205. int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
  206. const char *non_pref_chan)
  207. {
  208. char *cmd, *token, *context = NULL;
  209. struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
  210. size_t num = 0, size = 0;
  211. unsigned i;
  212. wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
  213. non_pref_chan ? non_pref_chan : "N/A");
  214. /*
  215. * The shortest channel configuration is 10 characters - commas, 3
  216. * colons, and 4 values that one of them (oper_class) is 2 digits or
  217. * more.
  218. */
  219. if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
  220. goto update;
  221. cmd = os_strdup(non_pref_chan);
  222. if (!cmd)
  223. return -1;
  224. while ((token = str_token(cmd, " ", &context))) {
  225. struct wpa_mbo_non_pref_channel *chan;
  226. int ret;
  227. unsigned int _oper_class;
  228. unsigned int _chan;
  229. unsigned int _preference;
  230. unsigned int _reason;
  231. if (num == size) {
  232. size = size ? size * 2 : 1;
  233. tmp_chans = os_realloc_array(chans, size,
  234. sizeof(*chans));
  235. if (!tmp_chans) {
  236. wpa_printf(MSG_ERROR,
  237. "Couldn't reallocate non_pref_chan");
  238. goto fail;
  239. }
  240. chans = tmp_chans;
  241. }
  242. chan = &chans[num];
  243. ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
  244. &_chan, &_preference, &_reason);
  245. if (ret != 4 ||
  246. _oper_class > 255 || _chan > 255 ||
  247. _preference > 255 || _reason > 65535 ) {
  248. wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
  249. token);
  250. goto fail;
  251. }
  252. chan->oper_class = _oper_class;
  253. chan->chan = _chan;
  254. chan->preference = _preference;
  255. chan->reason = _reason;
  256. if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
  257. chan->chan, chan->reason)) {
  258. wpa_printf(MSG_ERROR,
  259. "Invalid non_pref_chan: oper class %d chan %d reason %d",
  260. chan->oper_class, chan->chan, chan->reason);
  261. goto fail;
  262. }
  263. for (i = 0; i < num; i++)
  264. if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
  265. break;
  266. if (i != num) {
  267. wpa_printf(MSG_ERROR,
  268. "oper class %d chan %d is duplicated",
  269. chan->oper_class, chan->chan);
  270. goto fail;
  271. }
  272. num++;
  273. }
  274. os_free(cmd);
  275. if (chans) {
  276. qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
  277. wpa_non_pref_chan_cmp);
  278. }
  279. update:
  280. os_free(wpa_s->non_pref_chan);
  281. wpa_s->non_pref_chan = chans;
  282. wpa_s->non_pref_chan_num = num;
  283. wpas_mbo_non_pref_chan_changed(wpa_s);
  284. return 0;
  285. fail:
  286. os_free(chans);
  287. os_free(cmd);
  288. return -1;
  289. }
  290. void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
  291. {
  292. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  293. wpabuf_put_u8(ie, 7);
  294. wpabuf_put_be24(ie, OUI_WFA);
  295. wpabuf_put_u8(ie, MBO_OUI_TYPE);
  296. wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
  297. wpabuf_put_u8(ie, 1);
  298. wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
  299. }
  300. void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
  301. size_t len)
  302. {
  303. const u8 *pos, *cell_pref = NULL, *reason = NULL;
  304. u8 id, elen;
  305. u16 disallowed_sec = 0;
  306. if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
  307. mbo_ie[3] != MBO_OUI_TYPE)
  308. return;
  309. pos = mbo_ie + 4;
  310. len -= 4;
  311. while (len >= 2) {
  312. id = *pos++;
  313. elen = *pos++;
  314. len -= 2;
  315. if (elen > len)
  316. goto fail;
  317. switch (id) {
  318. case MBO_ATTR_ID_CELL_DATA_PREF:
  319. if (elen != 1)
  320. goto fail;
  321. if (wpa_s->conf->mbo_cell_capa ==
  322. MBO_CELL_CAPA_AVAILABLE)
  323. cell_pref = pos;
  324. else
  325. wpa_printf(MSG_DEBUG,
  326. "MBO: Station does not support Cellular data connection");
  327. break;
  328. case MBO_ATTR_ID_TRANSITION_REASON:
  329. if (elen != 1)
  330. goto fail;
  331. reason = pos;
  332. break;
  333. case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
  334. if (elen != 2)
  335. goto fail;
  336. if (wpa_s->wnm_mode &
  337. WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  338. wpa_printf(MSG_DEBUG,
  339. "MBO: Unexpected association retry delay, BSS is terminating");
  340. goto fail;
  341. } else if (wpa_s->wnm_mode &
  342. WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  343. disallowed_sec = WPA_GET_LE16(pos);
  344. } else {
  345. wpa_printf(MSG_DEBUG,
  346. "MBO: Association retry delay attribute not in disassoc imminent mode");
  347. }
  348. break;
  349. case MBO_ATTR_ID_AP_CAPA_IND:
  350. case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
  351. case MBO_ATTR_ID_CELL_DATA_CAPA:
  352. case MBO_ATTR_ID_ASSOC_DISALLOW:
  353. case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
  354. wpa_printf(MSG_DEBUG,
  355. "MBO: Attribute %d should not be included in BTM Request frame",
  356. id);
  357. break;
  358. default:
  359. wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
  360. id);
  361. return;
  362. }
  363. pos += elen;
  364. len -= elen;
  365. }
  366. if (cell_pref)
  367. wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
  368. *cell_pref);
  369. if (reason)
  370. wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
  371. *reason);
  372. if (disallowed_sec && wpa_s->current_bss)
  373. wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
  374. disallowed_sec);
  375. return;
  376. fail:
  377. wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
  378. id, elen, len);
  379. }
  380. size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
  381. size_t len,
  382. enum mbo_transition_reject_reason reason)
  383. {
  384. u8 reject_attr[3];
  385. reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
  386. reject_attr[1] = 1;
  387. reject_attr[2] = reason;
  388. return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
  389. }
  390. void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
  391. {
  392. u8 cell_capa[7];
  393. if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
  394. wpa_printf(MSG_DEBUG,
  395. "MBO: Cellular capability already set to %u",
  396. mbo_cell_capa);
  397. return;
  398. }
  399. wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
  400. cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
  401. cell_capa[1] = 5; /* Length */
  402. WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
  403. cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
  404. cell_capa[6] = mbo_cell_capa;
  405. wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
  406. wpa_supplicant_set_default_scan_ies(wpa_s);
  407. }
  408. struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
  409. struct wpa_bss *bss)
  410. {
  411. struct wpabuf *anqp_buf;
  412. u8 *len_pos;
  413. if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
  414. wpa_printf(MSG_INFO, "MBO: " MACSTR
  415. " does not support MBO - cannot request MBO ANQP elements from it",
  416. MAC2STR(bss->bssid));
  417. return NULL;
  418. }
  419. anqp_buf = wpabuf_alloc(10);
  420. if (!anqp_buf)
  421. return NULL;
  422. len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
  423. wpabuf_put_be24(anqp_buf, OUI_WFA);
  424. wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
  425. wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_CELL_CONN_PREF);
  426. gas_anqp_set_element_len(anqp_buf, len_pos);
  427. return anqp_buf;
  428. }