4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * Intel Linux Wireless <ilw@linux.intel.com>
7 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
13 #include "utils/includes.h"
15 #include "utils/common.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/gas.h"
19 #include "wpa_supplicant_i.h"
24 /* type + length + oui + oui type */
25 #define MBO_IE_HEADER 6
28 static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
30 if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
33 /* Only checking the validity of the channel and oper_class */
34 if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
41 const u8 * mbo_attr_from_mbo_ie(const u8 *mbo_ie, enum mbo_attr_id attr)
44 u8 ie_len = mbo_ie[1];
46 if (ie_len < MBO_IE_HEADER - 2)
48 mbo = mbo_ie + MBO_IE_HEADER;
50 return get_ie(mbo, 2 + ie_len - MBO_IE_HEADER, attr);
54 const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
61 mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
65 end = mbo + 2 + mbo[1];
68 return get_ie(mbo, end - mbo, attr);
72 static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
78 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
80 for (i = start; i < end; i++)
81 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
83 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
84 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
88 static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
89 struct wpabuf *mbo, u8 start, u8 end)
91 size_t size = end - start + 3;
93 if (size + 2 > wpabuf_tailroom(mbo))
96 wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
97 wpabuf_put_u8(mbo, size); /* Length */
99 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
103 static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
105 wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
106 wpabuf_put_u8(mbo, len); /* Length */
107 wpabuf_put_be24(mbo, OUI_WFA);
108 wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
112 static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
113 struct wpabuf *mbo, u8 start,
116 size_t size = end - start + 7;
118 if (size + 2 > wpabuf_tailroom(mbo))
121 wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
122 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
126 static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
127 struct wpabuf *mbo, int subelement)
130 struct wpa_mbo_non_pref_channel *start_pref;
132 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
134 wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
137 start_pref = &wpa_s->non_pref_chan[0];
139 for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
140 struct wpa_mbo_non_pref_channel *non_pref = NULL;
142 if (i < wpa_s->non_pref_chan_num)
143 non_pref = &wpa_s->non_pref_chan[i];
145 non_pref->oper_class != start_pref->oper_class ||
146 non_pref->reason != start_pref->reason ||
147 non_pref->preference != start_pref->preference) {
149 wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
152 wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
159 start_pref = non_pref;
165 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len,
171 if (len < MBO_IE_HEADER + 3 + 7 +
172 ((wpa_s->enable_oce & OCE_STA) ? 3 : 0))
175 /* Leave room for the MBO IE header */
176 mbo = wpabuf_alloc(len - MBO_IE_HEADER);
180 /* Add non-preferred channels attribute */
181 wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
184 * Send cellular capabilities attribute even if AP does not advertise
185 * cellular capabilities.
187 wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
188 wpabuf_put_u8(mbo, 1);
189 wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
191 /* Add OCE capability indication attribute if OCE is enabled */
192 if ((wpa_s->enable_oce & OCE_STA) && add_oce_capa) {
193 wpabuf_put_u8(mbo, OCE_ATTR_ID_CAPA_IND);
194 wpabuf_put_u8(mbo, 1);
195 wpabuf_put_u8(mbo, OCE_RELEASE);
198 res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
200 wpa_printf(MSG_ERROR, "Failed to add MBO/OCE IE");
207 static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
208 const u8 *data, size_t len)
214 * Send WNM-Notification Request frame only in case of a change in
215 * non-preferred channels list during association, if the AP supports
218 if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
219 !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
222 buf = wpabuf_alloc(4 + len);
226 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
227 wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
228 wpa_s->mbo_wnm_token++;
229 if (wpa_s->mbo_wnm_token == 0)
230 wpa_s->mbo_wnm_token++;
231 wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
232 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
234 wpabuf_put_data(buf, data, len);
236 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
237 wpa_s->own_addr, wpa_s->bssid,
238 wpabuf_head(buf), wpabuf_len(buf), 0);
240 wpa_printf(MSG_DEBUG,
241 "Failed to send WNM-Notification Request frame with non-preferred channel list");
247 static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
251 buf = wpabuf_alloc(512);
255 wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
256 wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
262 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
263 struct wpa_mbo_non_pref_channel *b)
265 return a->oper_class == b->oper_class && a->chan == b->chan;
270 * wpa_non_pref_chan_cmp - Compare two channels for sorting
272 * In MBO IE non-preferred channel subelement we can put many channels in an
273 * attribute if they are in the same operating class and have the same
274 * preference and reason. To make it easy for the functions that build
275 * the IE attributes and WNM Request subelements, save the channels sorted
276 * by their oper_class and reason.
278 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
280 const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
282 if (a->oper_class != b->oper_class)
283 return a->oper_class - b->oper_class;
284 if (a->reason != b->reason)
285 return a->reason - b->reason;
286 return a->preference - b->preference;
290 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
291 const char *non_pref_chan)
293 char *cmd, *token, *context = NULL;
294 struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
295 size_t num = 0, size = 0;
298 wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
299 non_pref_chan ? non_pref_chan : "N/A");
302 * The shortest channel configuration is 7 characters - 3 colons and
305 if (!non_pref_chan || os_strlen(non_pref_chan) < 7)
308 cmd = os_strdup(non_pref_chan);
312 while ((token = str_token(cmd, " ", &context))) {
313 struct wpa_mbo_non_pref_channel *chan;
315 unsigned int _oper_class;
317 unsigned int _preference;
318 unsigned int _reason;
321 size = size ? size * 2 : 1;
322 tmp_chans = os_realloc_array(chans, size,
325 wpa_printf(MSG_ERROR,
326 "Couldn't reallocate non_pref_chan");
334 ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
335 &_chan, &_preference, &_reason);
337 _oper_class > 255 || _chan > 255 ||
338 _preference > 255 || _reason > 65535 ) {
339 wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
343 chan->oper_class = _oper_class;
345 chan->preference = _preference;
346 chan->reason = _reason;
348 if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
349 chan->chan, chan->reason)) {
350 wpa_printf(MSG_ERROR,
351 "Invalid non_pref_chan: oper class %d chan %d reason %d",
352 chan->oper_class, chan->chan, chan->reason);
356 for (i = 0; i < num; i++)
357 if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
360 wpa_printf(MSG_ERROR,
361 "oper class %d chan %d is duplicated",
362 chan->oper_class, chan->chan);
372 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
373 wpa_non_pref_chan_cmp);
377 os_free(wpa_s->non_pref_chan);
378 wpa_s->non_pref_chan = chans;
379 wpa_s->non_pref_chan_num = num;
380 wpas_mbo_non_pref_chan_changed(wpa_s);
391 void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
395 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
396 len = wpabuf_put(ie, 1);
398 wpabuf_put_be24(ie, OUI_WFA);
399 wpabuf_put_u8(ie, MBO_OUI_TYPE);
401 wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
402 wpabuf_put_u8(ie, 1);
403 wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
404 if (wpa_s->enable_oce & OCE_STA) {
405 wpabuf_put_u8(ie, OCE_ATTR_ID_CAPA_IND);
406 wpabuf_put_u8(ie, 1);
407 wpabuf_put_u8(ie, OCE_RELEASE);
409 *len = (u8 *) wpabuf_put(ie, 0) - len - 1;
413 void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
416 const u8 *pos, *cell_pref = NULL;
418 u16 disallowed_sec = 0;
420 if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
421 mbo_ie[3] != MBO_OUI_TYPE)
436 case MBO_ATTR_ID_CELL_DATA_PREF:
440 if (wpa_s->conf->mbo_cell_capa ==
441 MBO_CELL_CAPA_AVAILABLE)
444 wpa_printf(MSG_DEBUG,
445 "MBO: Station does not support Cellular data connection");
447 case MBO_ATTR_ID_TRANSITION_REASON:
451 wpa_s->wnm_mbo_trans_reason_present = 1;
452 wpa_s->wnm_mbo_transition_reason = *pos;
454 case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
458 if (wpa_s->wnm_mode &
459 WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
460 wpa_printf(MSG_DEBUG,
461 "MBO: Unexpected association retry delay, BSS is terminating");
463 } else if (wpa_s->wnm_mode &
464 WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
465 disallowed_sec = WPA_GET_LE16(pos);
466 wpa_printf(MSG_DEBUG,
467 "MBO: Association retry delay: %u",
470 wpa_printf(MSG_DEBUG,
471 "MBO: Association retry delay attribute not in disassoc imminent mode");
475 case MBO_ATTR_ID_AP_CAPA_IND:
476 case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
477 case MBO_ATTR_ID_CELL_DATA_CAPA:
478 case MBO_ATTR_ID_ASSOC_DISALLOW:
479 case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
480 wpa_printf(MSG_DEBUG,
481 "MBO: Attribute %d should not be included in BTM Request frame",
485 wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
495 wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
498 if (wpa_s->wnm_mbo_trans_reason_present)
499 wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
500 wpa_s->wnm_mbo_transition_reason);
502 if (disallowed_sec && wpa_s->current_bss)
503 wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
508 wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
513 size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
515 enum mbo_transition_reject_reason reason)
519 reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
521 reject_attr[2] = reason;
523 return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
527 void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
531 if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
532 wpa_printf(MSG_DEBUG,
533 "MBO: Cellular capability already set to %u",
538 wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
540 cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
541 cell_capa[1] = 5; /* Length */
542 WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
543 cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
544 cell_capa[6] = mbo_cell_capa;
546 wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
547 wpa_supplicant_set_default_scan_ies(wpa_s);
551 struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
552 struct wpa_bss *bss, u32 mbo_subtypes)
554 struct wpabuf *anqp_buf;
558 if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
559 wpa_printf(MSG_INFO, "MBO: " MACSTR
560 " does not support MBO - cannot request MBO ANQP elements from it",
561 MAC2STR(bss->bssid));
565 /* Allocate size for the maximum case - all MBO subtypes are set */
566 anqp_buf = wpabuf_alloc(9 + MAX_MBO_ANQP_SUBTYPE);
570 len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
571 wpabuf_put_be24(anqp_buf, OUI_WFA);
572 wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
574 wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_QUERY_LIST);
576 /* The first valid MBO subtype is 1 */
577 for (i = 1; i <= MAX_MBO_ANQP_SUBTYPE; i++) {
578 if (mbo_subtypes & BIT(i))
579 wpabuf_put_u8(anqp_buf, i);
582 gas_anqp_set_element_len(anqp_buf, len_pos);
588 void mbo_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
589 struct wpa_bss *bss, const u8 *sa,
590 const u8 *data, size_t slen)
592 const u8 *pos = data;
602 case MBO_ANQP_SUBTYPE_CELL_CONN_PREF:
605 wpa_msg(wpa_s, MSG_INFO, RX_MBO_ANQP MACSTR
606 " cell_conn_pref=%u", MAC2STR(sa), *pos);
609 wpa_printf(MSG_DEBUG, "MBO: Unsupported ANQP subtype %u",