mbo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. enum chan_allowed {
  301. NOT_ALLOWED, ALLOWED
  302. };
  303. static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
  304. unsigned int *flags)
  305. {
  306. int i;
  307. for (i = 0; i < mode->num_channels; i++) {
  308. if (mode->channels[i].chan == chan)
  309. break;
  310. }
  311. if (i == mode->num_channels ||
  312. (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
  313. return NOT_ALLOWED;
  314. if (flags)
  315. *flags = mode->channels[i].flag;
  316. return ALLOWED;
  317. }
  318. static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  319. {
  320. u8 center_channels[] = {42, 58, 106, 122, 138, 155};
  321. size_t i;
  322. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  323. return 0;
  324. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  325. /*
  326. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  327. * so the center channel is 6 channels away from the start/end.
  328. */
  329. if (channel >= center_channels[i] - 6 &&
  330. channel <= center_channels[i] + 6)
  331. return center_channels[i];
  332. }
  333. return 0;
  334. }
  335. static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  336. {
  337. u8 center_chan;
  338. unsigned int i;
  339. center_chan = get_center_80mhz(mode, channel);
  340. if (!center_chan)
  341. return NOT_ALLOWED;
  342. /* check all the channels are available */
  343. for (i = 0; i < 4; i++) {
  344. unsigned int flags;
  345. u8 adj_chan = center_chan - 6 + i * 4;
  346. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  347. return NOT_ALLOWED;
  348. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
  349. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
  350. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
  351. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
  352. return NOT_ALLOWED;
  353. }
  354. return ALLOWED;
  355. }
  356. static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
  357. {
  358. u8 center_channels[] = { 50, 114 };
  359. unsigned int i;
  360. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  361. return 0;
  362. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  363. /*
  364. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  365. * so the center channel is 14 channels away from the start/end.
  366. */
  367. if (channel >= center_channels[i] - 14 &&
  368. channel <= center_channels[i] + 14)
  369. return center_channels[i];
  370. }
  371. return 0;
  372. }
  373. static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
  374. u8 channel)
  375. {
  376. u8 center_chan;
  377. unsigned int i;
  378. center_chan = get_center_160mhz(mode, channel);
  379. if (!center_chan)
  380. return NOT_ALLOWED;
  381. /* Check all the channels are available */
  382. for (i = 0; i < 8; i++) {
  383. unsigned int flags;
  384. u8 adj_chan = center_chan - 14 + i * 4;
  385. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  386. return NOT_ALLOWED;
  387. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
  388. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
  389. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
  390. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
  391. (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
  392. (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
  393. (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
  394. (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
  395. return NOT_ALLOWED;
  396. }
  397. return ALLOWED;
  398. }
  399. static enum chan_allowed verify_channel(struct hostapd_hw_modes *mode,
  400. u8 channel, u8 bw)
  401. {
  402. unsigned int flag = 0;
  403. enum chan_allowed res, res2;
  404. res2 = res = allow_channel(mode, channel, &flag);
  405. if (bw == BW40MINUS) {
  406. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  407. return NOT_ALLOWED;
  408. res2 = allow_channel(mode, channel - 4, NULL);
  409. } else if (bw == BW40PLUS) {
  410. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  411. return NOT_ALLOWED;
  412. res2 = allow_channel(mode, channel + 4, NULL);
  413. } else if (bw == BW80) {
  414. /*
  415. * channel is a center channel and as such, not necessarily a
  416. * valid 20 MHz channels. Override earlier allow_channel()
  417. * result and use only the 80 MHz specific version.
  418. */
  419. res2 = res = verify_80mhz(mode, channel);
  420. } else if (bw == BW160) {
  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 160 MHz specific version.
  425. */
  426. res2 = res = verify_160mhz(mode, channel);
  427. } else if (bw == BW80P80) {
  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 80 MHz specific version.
  432. */
  433. res2 = res = verify_80mhz(mode, channel);
  434. }
  435. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  436. return NOT_ALLOWED;
  437. return ALLOWED;
  438. }
  439. static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
  440. const struct oper_class_map *op_class)
  441. {
  442. int chan;
  443. size_t i;
  444. struct hostapd_hw_modes *mode;
  445. int found;
  446. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
  447. if (!mode)
  448. return 0;
  449. if (op_class->op_class == 128) {
  450. u8 channels[] = { 42, 58, 106, 122, 138, 155 };
  451. for (i = 0; i < ARRAY_SIZE(channels); i++) {
  452. if (verify_channel(mode, channels[i], op_class->bw) ==
  453. ALLOWED)
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. if (op_class->op_class == 129) {
  459. /* Check if either 160 MHz channels is allowed */
  460. return verify_channel(mode, 50, op_class->bw) == ALLOWED ||
  461. verify_channel(mode, 114, op_class->bw) == ALLOWED;
  462. }
  463. if (op_class->op_class == 130) {
  464. /* Need at least two non-contiguous 80 MHz segments */
  465. found = 0;
  466. if (verify_channel(mode, 42, op_class->bw) == ALLOWED ||
  467. verify_channel(mode, 58, op_class->bw) == ALLOWED)
  468. found++;
  469. if (verify_channel(mode, 106, op_class->bw) == ALLOWED ||
  470. verify_channel(mode, 122, op_class->bw) == ALLOWED ||
  471. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  472. found++;
  473. if (verify_channel(mode, 106, op_class->bw) == ALLOWED &&
  474. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  475. found++;
  476. if (verify_channel(mode, 155, op_class->bw) == ALLOWED)
  477. found++;
  478. if (found >= 2)
  479. return 1;
  480. return 0;
  481. }
  482. found = 0;
  483. for (chan = op_class->min_chan; chan <= op_class->max_chan;
  484. chan += op_class->inc) {
  485. if (verify_channel(mode, chan, op_class->bw) == ALLOWED) {
  486. found = 1;
  487. break;
  488. }
  489. }
  490. return found;
  491. }
  492. int wpas_mbo_supp_op_class_ie(struct wpa_supplicant *wpa_s, int freq, u8 *pos,
  493. size_t len)
  494. {
  495. struct wpabuf *buf;
  496. u8 op, current, chan;
  497. u8 *ie_len;
  498. int res;
  499. /*
  500. * Assume 20 MHz channel for now.
  501. * TODO: Use the secondary channel and VHT channel width that will be
  502. * used after association.
  503. */
  504. if (ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  505. &current, &chan) == NUM_HOSTAPD_MODES)
  506. return 0;
  507. /*
  508. * Need 3 bytes for EID, length, and current operating class, plus
  509. * 1 byte for every other supported operating class.
  510. */
  511. buf = wpabuf_alloc(global_op_class_size + 3);
  512. if (!buf)
  513. return 0;
  514. wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
  515. /* Will set the length later, putting a placeholder */
  516. ie_len = wpabuf_put(buf, 1);
  517. wpabuf_put_u8(buf, current);
  518. for (op = 0; global_op_class[op].op_class; op++) {
  519. if (wpas_op_class_supported(wpa_s, &global_op_class[op]))
  520. wpabuf_put_u8(buf, global_op_class[op].op_class);
  521. }
  522. *ie_len = wpabuf_len(buf) - 2;
  523. if (*ie_len < 2 || wpabuf_len(buf) > len) {
  524. wpa_printf(MSG_ERROR,
  525. "Failed to add supported operating classes IE");
  526. res = 0;
  527. } else {
  528. os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
  529. res = wpabuf_len(buf);
  530. wpa_hexdump_buf(MSG_DEBUG,
  531. "MBO: Added supported operating classes IE",
  532. buf);
  533. }
  534. wpabuf_free(buf);
  535. return res;
  536. }
  537. void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
  538. size_t len)
  539. {
  540. const u8 *pos, *cell_pref = NULL, *reason = NULL;
  541. u8 id, elen;
  542. u16 disallowed_sec = 0;
  543. if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
  544. mbo_ie[3] != MBO_OUI_TYPE)
  545. return;
  546. pos = mbo_ie + 4;
  547. len -= 4;
  548. while (len >= 2) {
  549. id = *pos++;
  550. elen = *pos++;
  551. len -= 2;
  552. if (elen > len)
  553. goto fail;
  554. switch (id) {
  555. case MBO_ATTR_ID_CELL_DATA_PREF:
  556. if (elen != 1)
  557. goto fail;
  558. if (wpa_s->conf->mbo_cell_capa ==
  559. MBO_CELL_CAPA_AVAILABLE)
  560. cell_pref = pos;
  561. else
  562. wpa_printf(MSG_DEBUG,
  563. "MBO: Station does not support Cellular data connection");
  564. break;
  565. case MBO_ATTR_ID_TRANSITION_REASON:
  566. if (elen != 1)
  567. goto fail;
  568. reason = pos;
  569. break;
  570. case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
  571. if (elen != 2)
  572. goto fail;
  573. if (wpa_s->wnm_mode &
  574. WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
  575. wpa_printf(MSG_DEBUG,
  576. "MBO: Unexpected association retry delay, BSS is terminating");
  577. goto fail;
  578. } else if (wpa_s->wnm_mode &
  579. WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
  580. disallowed_sec = WPA_GET_LE16(pos);
  581. } else {
  582. wpa_printf(MSG_DEBUG,
  583. "MBO: Association retry delay attribute not in disassoc imminent mode");
  584. }
  585. break;
  586. case MBO_ATTR_ID_AP_CAPA_IND:
  587. case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
  588. case MBO_ATTR_ID_CELL_DATA_CAPA:
  589. case MBO_ATTR_ID_ASSOC_DISALLOW:
  590. case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
  591. wpa_printf(MSG_DEBUG,
  592. "MBO: Attribute %d should not be included in BTM Request frame",
  593. id);
  594. break;
  595. default:
  596. wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
  597. id);
  598. return;
  599. }
  600. pos += elen;
  601. len -= elen;
  602. }
  603. if (cell_pref)
  604. wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
  605. *cell_pref);
  606. if (reason)
  607. wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
  608. *reason);
  609. if (disallowed_sec && wpa_s->current_bss)
  610. wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
  611. disallowed_sec);
  612. return;
  613. fail:
  614. wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
  615. id, elen, len);
  616. }
  617. size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
  618. size_t len,
  619. enum mbo_transition_reject_reason reason)
  620. {
  621. u8 reject_attr[3];
  622. reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
  623. reject_attr[1] = 1;
  624. reject_attr[2] = reason;
  625. return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
  626. }
  627. void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
  628. {
  629. u8 cell_capa[7];
  630. if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
  631. wpa_printf(MSG_DEBUG,
  632. "MBO: Cellular capability already set to %u",
  633. mbo_cell_capa);
  634. return;
  635. }
  636. wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
  637. cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
  638. cell_capa[1] = 5; /* Length */
  639. WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
  640. cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
  641. cell_capa[6] = mbo_cell_capa;
  642. wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
  643. wpa_supplicant_set_default_scan_ies(wpa_s);
  644. }
  645. struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
  646. struct wpa_bss *bss)
  647. {
  648. struct wpabuf *anqp_buf;
  649. u8 *len_pos;
  650. if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
  651. wpa_printf(MSG_INFO, "MBO: " MACSTR
  652. " does not support MBO - cannot request MBO ANQP elements from it",
  653. MAC2STR(bss->bssid));
  654. return NULL;
  655. }
  656. anqp_buf = wpabuf_alloc(10);
  657. if (!anqp_buf)
  658. return NULL;
  659. len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
  660. wpabuf_put_be24(anqp_buf, OUI_WFA);
  661. wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
  662. wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_CELL_CONN_PREF);
  663. gas_anqp_set_element_len(anqp_buf, len_pos);
  664. return anqp_buf;
  665. }