mbo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. #include "scan.h"
  20. /* type + length + oui + oui type */
  21. #define MBO_IE_HEADER 6
  22. static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
  23. {
  24. if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
  25. return -1;
  26. /* Only checking the validity of the channel and oper_class */
  27. if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
  28. return -1;
  29. return 0;
  30. }
  31. const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
  32. {
  33. const u8 *mbo, *end;
  34. if (!bss)
  35. return NULL;
  36. mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
  37. if (!mbo)
  38. return NULL;
  39. end = mbo + 2 + mbo[1];
  40. mbo += MBO_IE_HEADER;
  41. return get_ie(mbo, end - mbo, attr);
  42. }
  43. static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
  44. struct wpabuf *mbo,
  45. u8 start, u8 end)
  46. {
  47. u8 i;
  48. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
  49. for (i = start; i < end; i++)
  50. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
  51. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
  52. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
  53. wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason_detail);
  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 + 4;
  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 + 8;
  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->reason_detail != start_pref->reason_detail ||
  101. non_pref->preference != start_pref->preference) {
  102. if (subelement)
  103. wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
  104. start, i);
  105. else
  106. wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
  107. i);
  108. if (!non_pref)
  109. return;
  110. start = i;
  111. start_pref = non_pref;
  112. }
  113. }
  114. }
  115. int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
  116. {
  117. struct wpabuf *mbo;
  118. int res;
  119. if (len < MBO_IE_HEADER + 3 + 7)
  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. res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
  135. if (!res)
  136. wpa_printf(MSG_ERROR, "Failed to add MBO IE");
  137. wpabuf_free(mbo);
  138. return res;
  139. }
  140. static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
  141. const u8 *data, size_t len)
  142. {
  143. struct wpabuf *buf;
  144. int res;
  145. /*
  146. * Send WNM-Notification Request frame only in case of a change in
  147. * non-preferred channels list during association, if the AP supports
  148. * MBO.
  149. */
  150. if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
  151. !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
  152. return;
  153. buf = wpabuf_alloc(4 + len);
  154. if (!buf)
  155. return;
  156. wpabuf_put_u8(buf, WLAN_ACTION_WNM);
  157. wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
  158. wpa_s->mbo_wnm_token++;
  159. if (wpa_s->mbo_wnm_token == 0)
  160. wpa_s->mbo_wnm_token++;
  161. wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
  162. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
  163. wpabuf_put_data(buf, data, len);
  164. res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  165. wpa_s->own_addr, wpa_s->bssid,
  166. wpabuf_head(buf), wpabuf_len(buf), 0);
  167. if (res < 0)
  168. wpa_printf(MSG_DEBUG,
  169. "Failed to send WNM-Notification Request frame with non-preferred channel list");
  170. wpabuf_free(buf);
  171. }
  172. static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
  173. {
  174. struct wpabuf *buf;
  175. buf = wpabuf_alloc(512);
  176. if (!buf)
  177. return;
  178. wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
  179. wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
  180. wpabuf_len(buf));
  181. wpabuf_free(buf);
  182. }
  183. static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
  184. struct wpa_mbo_non_pref_channel *b)
  185. {
  186. return a->oper_class == b->oper_class && a->chan == b->chan;
  187. }
  188. /*
  189. * wpa_non_pref_chan_cmp - Compare two channels for sorting
  190. *
  191. * In MBO IE non-preferred channel subelement we can put many channels in an
  192. * attribute if they are in the same operating class and have the same
  193. * preference, reason, and reason detail. To make it easy for the functions that
  194. * build the IE attributes and WNM Request subelements, save the channels sorted
  195. * by their oper_class, reason, and reason_detail.
  196. */
  197. static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
  198. {
  199. const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
  200. if (a->oper_class != b->oper_class)
  201. return a->oper_class - b->oper_class;
  202. if (a->reason != b->reason)
  203. return a->reason - b->reason;
  204. if (a->reason_detail != b->reason_detail)
  205. return a->reason_detail - b->reason_detail;
  206. return a->preference - b->preference;
  207. }
  208. int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
  209. const char *non_pref_chan)
  210. {
  211. char *cmd, *token, *context = NULL;
  212. struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
  213. size_t num = 0, size = 0;
  214. unsigned i;
  215. wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
  216. non_pref_chan ? non_pref_chan : "N/A");
  217. /*
  218. * The shortest channel configuration is 10 characters - commas, 3
  219. * colons, and 4 values that one of them (oper_class) is 2 digits or
  220. * more.
  221. */
  222. if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
  223. goto update;
  224. cmd = os_strdup(non_pref_chan);
  225. if (!cmd)
  226. return -1;
  227. while ((token = str_token(cmd, " ", &context))) {
  228. struct wpa_mbo_non_pref_channel *chan;
  229. int ret;
  230. unsigned int _oper_class;
  231. unsigned int _chan;
  232. unsigned int _preference;
  233. unsigned int _reason;
  234. unsigned int _reason_detail;
  235. if (num == size) {
  236. size = size ? size * 2 : 1;
  237. tmp_chans = os_realloc_array(chans, size,
  238. sizeof(*chans));
  239. if (!tmp_chans) {
  240. wpa_printf(MSG_ERROR,
  241. "Couldn't reallocate non_pref_chan");
  242. goto fail;
  243. }
  244. chans = tmp_chans;
  245. }
  246. chan = &chans[num];
  247. ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
  248. &_chan, &_preference, &_reason,
  249. &_reason_detail);
  250. if ((ret != 4 && ret != 5) ||
  251. _oper_class > 255 || _chan > 255 ||
  252. _preference > 255 || _reason > 65535 ||
  253. (ret == 5 && _reason_detail > 255)) {
  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. chan->reason_detail = ret == 4 ? 0 : _reason_detail;
  263. if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
  264. chan->chan, chan->reason)) {
  265. wpa_printf(MSG_ERROR,
  266. "Invalid non_pref_chan: oper class %d chan %d reason %d",
  267. chan->oper_class, chan->chan, chan->reason);
  268. goto fail;
  269. }
  270. for (i = 0; i < num; i++)
  271. if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
  272. break;
  273. if (i != num) {
  274. wpa_printf(MSG_ERROR,
  275. "oper class %d chan %d is duplicated",
  276. chan->oper_class, chan->chan);
  277. goto fail;
  278. }
  279. num++;
  280. }
  281. os_free(cmd);
  282. if (chans) {
  283. qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
  284. wpa_non_pref_chan_cmp);
  285. }
  286. update:
  287. os_free(wpa_s->non_pref_chan);
  288. wpa_s->non_pref_chan = chans;
  289. wpa_s->non_pref_chan_num = num;
  290. wpas_mbo_non_pref_chan_changed(wpa_s);
  291. return 0;
  292. fail:
  293. os_free(chans);
  294. os_free(cmd);
  295. return -1;
  296. }
  297. void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
  298. {
  299. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  300. wpabuf_put_u8(ie, 7);
  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. }
  307. enum chan_allowed {
  308. NOT_ALLOWED, ALLOWED
  309. };
  310. static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
  311. unsigned int *flags)
  312. {
  313. int i;
  314. for (i = 0; i < mode->num_channels; i++) {
  315. if (mode->channels[i].chan == chan)
  316. break;
  317. }
  318. if (i == mode->num_channels ||
  319. (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
  320. return NOT_ALLOWED;
  321. if (flags)
  322. *flags = mode->channels[i].flag;
  323. return ALLOWED;
  324. }
  325. static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  326. {
  327. u8 center_channels[] = {42, 58, 106, 122, 138, 155};
  328. size_t i;
  329. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  330. return 0;
  331. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  332. /*
  333. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  334. * so the center channel is 6 channels away from the start/end.
  335. */
  336. if (channel >= center_channels[i] - 6 &&
  337. channel <= center_channels[i] + 6)
  338. return center_channels[i];
  339. }
  340. return 0;
  341. }
  342. static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  343. {
  344. u8 center_chan;
  345. unsigned int i;
  346. center_chan = get_center_80mhz(mode, channel);
  347. if (!center_chan)
  348. return NOT_ALLOWED;
  349. /* check all the channels are available */
  350. for (i = 0; i < 4; i++) {
  351. unsigned int flags;
  352. u8 adj_chan = center_chan - 6 + i * 4;
  353. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  354. return NOT_ALLOWED;
  355. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
  356. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
  357. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
  358. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
  359. return NOT_ALLOWED;
  360. }
  361. return ALLOWED;
  362. }
  363. static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
  364. {
  365. u8 center_channels[] = { 50, 114 };
  366. unsigned int i;
  367. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  368. return 0;
  369. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  370. /*
  371. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  372. * so the center channel is 14 channels away from the start/end.
  373. */
  374. if (channel >= center_channels[i] - 14 &&
  375. channel <= center_channels[i] + 14)
  376. return center_channels[i];
  377. }
  378. return 0;
  379. }
  380. static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
  381. u8 channel)
  382. {
  383. u8 center_chan;
  384. unsigned int i;
  385. center_chan = get_center_160mhz(mode, channel);
  386. if (!center_chan)
  387. return NOT_ALLOWED;
  388. /* Check all the channels are available */
  389. for (i = 0; i < 8; i++) {
  390. unsigned int flags;
  391. u8 adj_chan = center_chan - 14 + i * 4;
  392. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  393. return NOT_ALLOWED;
  394. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
  395. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
  396. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
  397. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
  398. (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
  399. (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
  400. (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
  401. (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
  402. return NOT_ALLOWED;
  403. }
  404. return ALLOWED;
  405. }
  406. static enum chan_allowed verify_channel(struct hostapd_hw_modes *mode,
  407. u8 channel, u8 bw)
  408. {
  409. unsigned int flag = 0;
  410. enum chan_allowed res, res2;
  411. res2 = res = allow_channel(mode, channel, &flag);
  412. if (bw == BW40MINUS) {
  413. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  414. return NOT_ALLOWED;
  415. res2 = allow_channel(mode, channel - 4, NULL);
  416. } else if (bw == BW40PLUS) {
  417. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  418. return NOT_ALLOWED;
  419. res2 = allow_channel(mode, channel + 4, NULL);
  420. } else if (bw == BW80) {
  421. /*
  422. * channel is a center channel and as such, not necessarily a
  423. * valid 20 MHz channels. Override earlier allow_channel()
  424. * result and use only the 80 MHz specific version.
  425. */
  426. res2 = res = verify_80mhz(mode, channel);
  427. } else if (bw == BW160) {
  428. /*
  429. * channel is a center channel and as such, not necessarily a
  430. * valid 20 MHz channels. Override earlier allow_channel()
  431. * result and use only the 160 MHz specific version.
  432. */
  433. res2 = res = verify_160mhz(mode, channel);
  434. } else if (bw == BW80P80) {
  435. /*
  436. * channel is a center channel and as such, not necessarily a
  437. * valid 20 MHz channels. Override earlier allow_channel()
  438. * result and use only the 80 MHz specific version.
  439. */
  440. res2 = res = verify_80mhz(mode, channel);
  441. }
  442. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  443. return NOT_ALLOWED;
  444. return ALLOWED;
  445. }
  446. static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
  447. const struct oper_class_map *op_class)
  448. {
  449. int chan;
  450. size_t i;
  451. struct hostapd_hw_modes *mode;
  452. int found;
  453. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
  454. if (!mode)
  455. return 0;
  456. if (op_class->op_class == 128) {
  457. u8 channels[] = { 42, 58, 106, 122, 138, 155 };
  458. for (i = 0; i < ARRAY_SIZE(channels); i++) {
  459. if (verify_channel(mode, channels[i], op_class->bw) ==
  460. ALLOWED)
  461. return 1;
  462. }
  463. return 0;
  464. }
  465. if (op_class->op_class == 129) {
  466. /* Check if either 160 MHz channels is allowed */
  467. return verify_channel(mode, 50, op_class->bw) == ALLOWED ||
  468. verify_channel(mode, 114, op_class->bw) == ALLOWED;
  469. }
  470. if (op_class->op_class == 130) {
  471. /* Need at least two non-contiguous 80 MHz segments */
  472. found = 0;
  473. if (verify_channel(mode, 42, op_class->bw) == ALLOWED ||
  474. verify_channel(mode, 58, op_class->bw) == ALLOWED)
  475. found++;
  476. if (verify_channel(mode, 106, op_class->bw) == ALLOWED ||
  477. verify_channel(mode, 122, op_class->bw) == ALLOWED ||
  478. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  479. found++;
  480. if (verify_channel(mode, 106, op_class->bw) == ALLOWED &&
  481. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  482. found++;
  483. if (verify_channel(mode, 155, op_class->bw) == ALLOWED)
  484. found++;
  485. if (found >= 2)
  486. return 1;
  487. return 0;
  488. }
  489. found = 0;
  490. for (chan = op_class->min_chan; chan <= op_class->max_chan;
  491. chan += op_class->inc) {
  492. if (verify_channel(mode, chan, op_class->bw) == ALLOWED) {
  493. found = 1;
  494. break;
  495. }
  496. }
  497. return found;
  498. }
  499. int wpas_mbo_supp_op_class_ie(struct wpa_supplicant *wpa_s, int freq, u8 *pos,
  500. size_t len)
  501. {
  502. struct wpabuf *buf;
  503. u8 op, current, chan;
  504. u8 *ie_len;
  505. int res;
  506. /*
  507. * Assume 20 MHz channel for now.
  508. * TODO: Use the secondary channel and VHT channel width that will be
  509. * used after association.
  510. */
  511. if (ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  512. &current, &chan) == NUM_HOSTAPD_MODES)
  513. return 0;
  514. /*
  515. * Need 3 bytes for EID, length, and current operating class, plus
  516. * 1 byte for every other supported operating class.
  517. */
  518. buf = wpabuf_alloc(global_op_class_size + 3);
  519. if (!buf)
  520. return 0;
  521. wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
  522. /* Will set the length later, putting a placeholder */
  523. ie_len = wpabuf_put(buf, 1);
  524. wpabuf_put_u8(buf, current);
  525. for (op = 0; global_op_class[op].op_class; op++) {
  526. if (wpas_op_class_supported(wpa_s, &global_op_class[op]))
  527. wpabuf_put_u8(buf, global_op_class[op].op_class);
  528. }
  529. *ie_len = wpabuf_len(buf) - 2;
  530. if (*ie_len < 2 || wpabuf_len(buf) > len) {
  531. wpa_printf(MSG_ERROR,
  532. "Failed to add supported operating classes IE");
  533. res = 0;
  534. } else {
  535. os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
  536. res = wpabuf_len(buf);
  537. wpa_hexdump_buf(MSG_DEBUG,
  538. "MBO: Added supported operating classes IE",
  539. buf);
  540. }
  541. wpabuf_free(buf);
  542. return res;
  543. }
  544. void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
  545. size_t len)
  546. {
  547. const u8 *pos, *cell_pref = NULL, *reason = NULL;
  548. u8 id, elen;
  549. u16 disallowed_sec = 0;
  550. if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
  551. mbo_ie[3] != MBO_OUI_TYPE)
  552. return;
  553. pos = mbo_ie + 4;
  554. len -= 4;
  555. while (len >= 2) {
  556. id = *pos++;
  557. elen = *pos++;
  558. len -= 2;
  559. if (elen > len)
  560. goto fail;
  561. switch (id) {
  562. case MBO_ATTR_ID_CELL_DATA_PREF:
  563. if (elen != 1)
  564. goto fail;
  565. if (wpa_s->conf->mbo_cell_capa ==
  566. MBO_CELL_CAPA_AVAILABLE)
  567. cell_pref = pos;
  568. else
  569. wpa_printf(MSG_DEBUG,
  570. "MBO: Station does not support Cellular data connection");
  571. break;
  572. case MBO_ATTR_ID_TRANSITION_REASON:
  573. if (elen != 1)
  574. goto fail;
  575. reason = pos;
  576. break;
  577. case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
  578. if (elen != 2)
  579. goto fail;
  580. if (wpa_s->wnm_mode &
  581. WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  582. wpa_printf(MSG_DEBUG,
  583. "MBO: Unexpected association retry delay, BSS is terminating");
  584. goto fail;
  585. } else if (wpa_s->wnm_mode &
  586. WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  587. disallowed_sec = WPA_GET_LE16(pos);
  588. } else {
  589. wpa_printf(MSG_DEBUG,
  590. "MBO: Association retry delay attribute not in disassoc imminent mode");
  591. }
  592. break;
  593. case MBO_ATTR_ID_AP_CAPA_IND:
  594. case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
  595. case MBO_ATTR_ID_CELL_DATA_CAPA:
  596. case MBO_ATTR_ID_ASSOC_DISALLOW:
  597. case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
  598. wpa_printf(MSG_DEBUG,
  599. "MBO: Attribute %d should not be included in BTM Request frame",
  600. id);
  601. break;
  602. default:
  603. wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
  604. id);
  605. return;
  606. }
  607. pos += elen;
  608. len -= elen;
  609. }
  610. if (cell_pref)
  611. wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
  612. *cell_pref);
  613. if (reason)
  614. wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
  615. *reason);
  616. if (disallowed_sec && wpa_s->current_bss)
  617. wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
  618. disallowed_sec);
  619. return;
  620. fail:
  621. wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
  622. id, elen, len);
  623. }
  624. size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
  625. size_t len,
  626. enum mbo_transition_reject_reason reason)
  627. {
  628. u8 reject_attr[3];
  629. reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
  630. reject_attr[1] = 1;
  631. reject_attr[2] = reason;
  632. return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
  633. }
  634. void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
  635. {
  636. u8 cell_capa[7];
  637. if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
  638. wpa_printf(MSG_DEBUG,
  639. "MBO: Cellular capability already set to %u",
  640. mbo_cell_capa);
  641. return;
  642. }
  643. wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
  644. cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
  645. cell_capa[1] = 5; /* Length */
  646. WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
  647. cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
  648. cell_capa[6] = mbo_cell_capa;
  649. wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
  650. wpa_supplicant_set_default_scan_ies(wpa_s);
  651. }