mbo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 "config.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "driver_i.h"
  18. #include "bss.h"
  19. /* type + length + oui + oui type */
  20. #define MBO_IE_HEADER 6
  21. static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
  22. {
  23. if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
  24. return -1;
  25. /* Only checking the validity of the channel and oper_class */
  26. if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
  27. return -1;
  28. return 0;
  29. }
  30. const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
  31. {
  32. const u8 *mbo, *end;
  33. if (!bss)
  34. return NULL;
  35. mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
  36. if (!mbo)
  37. return NULL;
  38. end = mbo + 2 + mbo[1];
  39. mbo += MBO_IE_HEADER;
  40. return get_ie(mbo, end - mbo, attr);
  41. }
  42. static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
  43. struct wpabuf *mbo,
  44. u8 start, u8 end)
  45. {
  46. u8 i;
  47. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
  48. for (i = start; i < end; i++)
  49. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
  50. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
  51. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
  52. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason_detail);
  53. }
  54. static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
  55. struct wpabuf *mbo, u8 start, u8 end)
  56. {
  57. size_t size = end - start + 4;
  58. if (size + 2 > wpabuf_tailroom(mbo))
  59. return;
  60. wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
  61. wpabuf_put_u8(mbo, size); /* Length */
  62. wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
  63. }
  64. static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
  65. {
  66. wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
  67. wpabuf_put_u8(mbo, len); /* Length */
  68. wpabuf_put_be24(mbo, OUI_WFA);
  69. wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
  70. }
  71. static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
  72. struct wpabuf *mbo, u8 start,
  73. u8 end)
  74. {
  75. size_t size = end - start + 8;
  76. if (size + 2 > wpabuf_tailroom(mbo))
  77. return;
  78. wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
  79. wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
  80. }
  81. static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
  82. struct wpabuf *mbo, int subelement)
  83. {
  84. u8 i, start = 0;
  85. struct wpa_mbo_non_pref_channel *start_pref;
  86. if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
  87. if (subelement)
  88. wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
  89. return;
  90. }
  91. start_pref = &wpa_s->non_pref_chan[0];
  92. for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
  93. struct wpa_mbo_non_pref_channel *non_pref = NULL;
  94. if (i < wpa_s->non_pref_chan_num)
  95. non_pref = &wpa_s->non_pref_chan[i];
  96. if (!non_pref ||
  97. non_pref->oper_class != start_pref->oper_class ||
  98. non_pref->reason != start_pref->reason ||
  99. non_pref->reason_detail != start_pref->reason_detail ||
  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, reason, and reason detail. To make it easy for the functions that
  193. * build the IE attributes and WNM Request subelements, save the channels sorted
  194. * by their oper_class, reason, and reason_detail.
  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. if (a->reason_detail != b->reason_detail)
  204. return a->reason_detail - b->reason_detail;
  205. return a->preference - b->preference;
  206. }
  207. int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
  208. const char *non_pref_chan)
  209. {
  210. char *cmd, *token, *context = NULL;
  211. struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
  212. size_t num = 0, size = 0;
  213. unsigned i;
  214. wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
  215. non_pref_chan ? non_pref_chan : "N/A");
  216. /*
  217. * The shortest channel configuration is 10 characters - commas, 3
  218. * colons, and 4 values that one of them (oper_class) is 2 digits or
  219. * more.
  220. */
  221. if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
  222. goto update;
  223. cmd = os_strdup(non_pref_chan);
  224. if (!cmd)
  225. return -1;
  226. while ((token = str_token(cmd, " ", &context))) {
  227. struct wpa_mbo_non_pref_channel *chan;
  228. int ret;
  229. unsigned int _oper_class;
  230. unsigned int _chan;
  231. unsigned int _preference;
  232. unsigned int _reason;
  233. unsigned int _reason_detail;
  234. if (num == size) {
  235. size = size ? size * 2 : 1;
  236. tmp_chans = os_realloc_array(chans, size,
  237. sizeof(*chans));
  238. if (!tmp_chans) {
  239. wpa_printf(MSG_ERROR,
  240. "Couldn't reallocate non_pref_chan");
  241. goto fail;
  242. }
  243. chans = tmp_chans;
  244. }
  245. chan = &chans[num];
  246. ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
  247. &_chan, &_preference, &_reason,
  248. &_reason_detail);
  249. if ((ret != 4 && ret != 5) ||
  250. _oper_class > 255 || _chan > 255 ||
  251. _preference > 255 || _reason > 65535 ||
  252. (ret == 5 && _reason_detail > 255)) {
  253. wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
  254. token);
  255. goto fail;
  256. }
  257. chan->oper_class = _oper_class;
  258. chan->chan = _chan;
  259. chan->preference = _preference;
  260. chan->reason = _reason;
  261. chan->reason_detail = ret == 4 ? 0 : _reason_detail;
  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. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  299. wpabuf_put_u8(ie, 7);
  300. wpabuf_put_be24(ie, OUI_WFA);
  301. wpabuf_put_u8(ie, MBO_OUI_TYPE);
  302. wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
  303. wpabuf_put_u8(ie, 1);
  304. wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
  305. }
  306. enum chan_allowed {
  307. NOT_ALLOWED, ALLOWED
  308. };
  309. static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
  310. unsigned int *flags)
  311. {
  312. int i;
  313. for (i = 0; i < mode->num_channels; i++) {
  314. if (mode->channels[i].chan == chan)
  315. break;
  316. }
  317. if (i == mode->num_channels ||
  318. (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
  319. return NOT_ALLOWED;
  320. if (flags)
  321. *flags = mode->channels[i].flag;
  322. return ALLOWED;
  323. }
  324. static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  325. {
  326. u8 center_channels[] = {42, 58, 106, 122, 138, 155};
  327. size_t i;
  328. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  329. return 0;
  330. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  331. /*
  332. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  333. * so the center channel is 6 channels away from the start/end.
  334. */
  335. if (channel >= center_channels[i] - 6 &&
  336. channel <= center_channels[i] + 6)
  337. return center_channels[i];
  338. }
  339. return 0;
  340. }
  341. static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  342. {
  343. u8 center_chan;
  344. unsigned int i;
  345. center_chan = get_center_80mhz(mode, channel);
  346. if (!center_chan)
  347. return NOT_ALLOWED;
  348. /* check all the channels are available */
  349. for (i = 0; i < 4; i++) {
  350. unsigned int flags;
  351. u8 adj_chan = center_chan - 6 + i * 4;
  352. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  353. return NOT_ALLOWED;
  354. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
  355. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
  356. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
  357. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
  358. return NOT_ALLOWED;
  359. }
  360. return ALLOWED;
  361. }
  362. static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
  363. {
  364. u8 center_channels[] = { 50, 114 };
  365. unsigned int i;
  366. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  367. return 0;
  368. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  369. /*
  370. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  371. * so the center channel is 14 channels away from the start/end.
  372. */
  373. if (channel >= center_channels[i] - 14 &&
  374. channel <= center_channels[i] + 14)
  375. return center_channels[i];
  376. }
  377. return 0;
  378. }
  379. static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
  380. u8 channel)
  381. {
  382. u8 center_chan;
  383. unsigned int i;
  384. center_chan = get_center_160mhz(mode, channel);
  385. if (!center_chan)
  386. return NOT_ALLOWED;
  387. /* Check all the channels are available */
  388. for (i = 0; i < 8; i++) {
  389. unsigned int flags;
  390. u8 adj_chan = center_chan - 14 + i * 4;
  391. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  392. return NOT_ALLOWED;
  393. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
  394. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
  395. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
  396. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
  397. (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
  398. (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
  399. (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
  400. (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
  401. return NOT_ALLOWED;
  402. }
  403. return ALLOWED;
  404. }
  405. enum chan_allowed verify_channel(struct hostapd_hw_modes *mode, u8 channel,
  406. u8 bw)
  407. {
  408. unsigned int flag = 0;
  409. enum chan_allowed res, res2;
  410. res2 = res = allow_channel(mode, channel, &flag);
  411. if (bw == BW40MINUS) {
  412. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  413. return NOT_ALLOWED;
  414. res2 = allow_channel(mode, channel - 4, NULL);
  415. } else if (bw == BW40PLUS) {
  416. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  417. return NOT_ALLOWED;
  418. res2 = allow_channel(mode, channel + 4, NULL);
  419. } else if (bw == BW80) {
  420. res2 = verify_80mhz(mode, channel);
  421. } else if (bw == BW160) {
  422. res2 = verify_160mhz(mode, channel);
  423. }
  424. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  425. return NOT_ALLOWED;
  426. return ALLOWED;
  427. }
  428. static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
  429. const struct oper_class_map *op_class)
  430. {
  431. int chan;
  432. size_t i;
  433. struct hostapd_hw_modes *mode;
  434. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
  435. if (!mode)
  436. return 0;
  437. if (op_class->op_class == 128 || op_class->op_class == 130) {
  438. u8 channels[] = { 42, 58, 106, 122, 138, 155 };
  439. for (i = 0; i < ARRAY_SIZE(channels); i++) {
  440. if (verify_channel(mode, channels[i], op_class->bw) ==
  441. NOT_ALLOWED)
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. if (op_class->op_class == 129) {
  447. if (verify_channel(mode, 50, op_class->bw) == NOT_ALLOWED ||
  448. verify_channel(mode, 114, op_class->bw) == NOT_ALLOWED)
  449. return 0;
  450. return 1;
  451. }
  452. for (chan = op_class->min_chan; chan <= op_class->max_chan;
  453. chan += op_class->inc) {
  454. if (verify_channel(mode, chan, op_class->bw) == NOT_ALLOWED)
  455. return 0;
  456. }
  457. return 1;
  458. }
  459. int wpas_mbo_supp_op_class_ie(struct wpa_supplicant *wpa_s, int freq, u8 *pos,
  460. size_t len)
  461. {
  462. struct wpabuf *buf;
  463. u8 op, current, chan;
  464. u8 *ie_len;
  465. int res;
  466. /*
  467. * Assume 20 MHz channel for now.
  468. * TODO: Use the secondary channel and VHT channel width that will be
  469. * used after association.
  470. */
  471. if (ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  472. &current, &chan) == NUM_HOSTAPD_MODES)
  473. return 0;
  474. /*
  475. * Need 3 bytes for EID, length, and current operating class, plus
  476. * 1 byte for every other supported operating class.
  477. */
  478. buf = wpabuf_alloc(global_op_class_size + 3);
  479. if (!buf)
  480. return 0;
  481. wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
  482. /* Will set the length later, putting a placeholder */
  483. ie_len = wpabuf_put(buf, 1);
  484. wpabuf_put_u8(buf, current);
  485. for (op = 0; global_op_class[op].op_class; op++) {
  486. if (wpas_op_class_supported(wpa_s, &global_op_class[op]))
  487. wpabuf_put_u8(buf, global_op_class[op].op_class);
  488. }
  489. *ie_len = wpabuf_len(buf) - 2;
  490. if (*ie_len < 2 || wpabuf_len(buf) > len) {
  491. wpa_printf(MSG_ERROR,
  492. "Failed to add supported operating classes IE");
  493. res = 0;
  494. } else {
  495. os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
  496. res = wpabuf_len(buf);
  497. wpa_hexdump_buf(MSG_DEBUG,
  498. "MBO: Added supported operating classes IE",
  499. buf);
  500. }
  501. wpabuf_free(buf);
  502. return res;
  503. }
  504. void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
  505. size_t len)
  506. {
  507. const u8 *pos, *cell_pref = NULL, *reason = NULL;
  508. u8 id, elen;
  509. u16 disallowed_sec = 0;
  510. if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
  511. mbo_ie[3] != MBO_OUI_TYPE)
  512. return;
  513. pos = mbo_ie + 4;
  514. len -= 4;
  515. while (len >= 2) {
  516. id = *pos++;
  517. elen = *pos++;
  518. len -= 2;
  519. if (elen > len)
  520. goto fail;
  521. switch (id) {
  522. case MBO_ATTR_ID_CELL_DATA_PREF:
  523. if (elen != 1)
  524. goto fail;
  525. if (wpa_s->conf->mbo_cell_capa ==
  526. MBO_CELL_CAPA_AVAILABLE)
  527. cell_pref = pos;
  528. else
  529. wpa_printf(MSG_DEBUG,
  530. "MBO: Station does not support Cellular data connection");
  531. break;
  532. case MBO_ATTR_ID_TRANSITION_REASON:
  533. if (elen != 1)
  534. goto fail;
  535. reason = pos;
  536. break;
  537. case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
  538. if (elen != 2)
  539. goto fail;
  540. if (wpa_s->wnm_mode &
  541. WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  542. wpa_printf(MSG_DEBUG,
  543. "MBO: Unexpected association retry delay, BSS is terminating");
  544. goto fail;
  545. } else if (wpa_s->wnm_mode &
  546. WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  547. disallowed_sec = WPA_GET_LE16(pos);
  548. } else {
  549. wpa_printf(MSG_DEBUG,
  550. "MBO: Association retry delay attribute not in disassoc imminent mode");
  551. }
  552. break;
  553. case MBO_ATTR_ID_AP_CAPA_IND:
  554. case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
  555. case MBO_ATTR_ID_CELL_DATA_CAPA:
  556. case MBO_ATTR_ID_ASSOC_DISALLOW:
  557. case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
  558. wpa_printf(MSG_DEBUG,
  559. "MBO: Attribute %d should not be included in BTM Request frame",
  560. id);
  561. break;
  562. default:
  563. wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
  564. id);
  565. return;
  566. }
  567. pos += elen;
  568. len -= elen;
  569. }
  570. if (cell_pref)
  571. wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
  572. *cell_pref);
  573. if (reason)
  574. wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
  575. *reason);
  576. if (disallowed_sec && wpa_s->current_bss)
  577. wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
  578. disallowed_sec);
  579. return;
  580. fail:
  581. wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
  582. id, elen, len);
  583. }
  584. size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
  585. size_t len,
  586. enum mbo_transition_reject_reason reason)
  587. {
  588. u8 reject_attr[3];
  589. reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
  590. reject_attr[1] = 1;
  591. reject_attr[2] = reason;
  592. return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
  593. }
  594. void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
  595. {
  596. u8 cell_capa[7];
  597. if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
  598. wpa_printf(MSG_DEBUG,
  599. "MBO: Cellular capability already set to %u",
  600. mbo_cell_capa);
  601. return;
  602. }
  603. wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
  604. cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
  605. cell_capa[1] = 5; /* Length */
  606. WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
  607. cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
  608. cell_capa[6] = mbo_cell_capa;
  609. wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
  610. }