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"
18 #include "wpa_supplicant_i.h"
22 /* type + length + oui + oui type */
23 #define MBO_IE_HEADER 6
26 static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
28 if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
31 /* Only checking the validity of the channel and oper_class */
32 if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
39 static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
45 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
47 for (i = start; i < end; i++)
48 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);
56 static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
57 struct wpabuf *mbo, u8 start, u8 end)
59 size_t size = end - start + 4;
61 if (size + 2 > wpabuf_tailroom(mbo))
64 wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
65 wpabuf_put_u8(mbo, size); /* Length */
67 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
71 static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
73 wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
74 wpabuf_put_u8(mbo, len); /* Length */
75 wpabuf_put_be24(mbo, OUI_WFA);
76 wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
80 static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
81 struct wpabuf *mbo, u8 start,
84 size_t size = end - start + 8;
86 if (size + 2 > wpabuf_tailroom(mbo))
89 wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
90 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
94 static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
95 struct wpabuf *mbo, int subelement)
98 struct wpa_mbo_non_pref_channel *start_pref;
100 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
102 wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
105 start_pref = &wpa_s->non_pref_chan[0];
107 for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
108 struct wpa_mbo_non_pref_channel *non_pref = NULL;
110 if (i < wpa_s->non_pref_chan_num)
111 non_pref = &wpa_s->non_pref_chan[i];
113 non_pref->oper_class != start_pref->oper_class ||
114 non_pref->reason != start_pref->reason ||
115 non_pref->reason_detail != start_pref->reason_detail ||
116 non_pref->preference != start_pref->preference) {
118 wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
121 wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
128 start_pref = non_pref;
134 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
139 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num ||
140 len < MBO_IE_HEADER + 7)
143 /* Leave room for the MBO IE header */
144 mbo = wpabuf_alloc(len - MBO_IE_HEADER);
148 /* Add non-preferred channels attribute */
149 wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
151 res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
153 wpa_printf(MSG_ERROR, "Failed to add MBO IE");
160 static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s)
166 * Send WNM-Notification Request frame only in case of a change in
167 * non-preferred channels list during association, if the AP supports
170 if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
171 !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
174 buf = wpabuf_alloc(512);
178 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
179 wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
180 wpa_s->mbo_wnm_token++;
181 if (wpa_s->mbo_wnm_token == 0)
182 wpa_s->mbo_wnm_token++;
183 wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
184 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
186 wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
188 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
189 wpa_s->own_addr, wpa_s->bssid,
190 wpabuf_head(buf), wpabuf_len(buf), 0);
192 wpa_printf(MSG_DEBUG,
193 "Failed to send WNM-Notification Request frame with non-preferred channel list");
199 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
200 struct wpa_mbo_non_pref_channel *b)
202 return a->oper_class == b->oper_class && a->chan == b->chan;
207 * wpa_non_pref_chan_cmp - Compare two channels for sorting
209 * In MBO IE non-preferred channel subelement we can put many channels in an
210 * attribute if they are in the same operating class and have the same
211 * preference, reason, and reason detail. To make it easy for the functions that
212 * build the IE attributes and WNM Request subelements, save the channels sorted
213 * by their oper_class, reason, and reason_detail.
215 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
217 const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
219 if (a->oper_class != b->oper_class)
220 return a->oper_class - b->oper_class;
221 if (a->reason != b->reason)
222 return a->reason - b->reason;
223 if (a->reason_detail != b->reason_detail)
224 return a->reason_detail - b->reason_detail;
225 return a->preference - b->preference;
229 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
230 const char *non_pref_chan)
232 char *cmd, *token, *context = NULL;
233 struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
234 size_t num = 0, size = 0;
237 wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
238 non_pref_chan ? non_pref_chan : "N/A");
241 * The shortest channel configuration is 10 characters - commas, 3
242 * colons, and 4 values that one of them (oper_class) is 2 digits or
245 if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
248 cmd = os_strdup(non_pref_chan);
252 while ((token = str_token(cmd, " ", &context))) {
253 struct wpa_mbo_non_pref_channel *chan;
255 unsigned int _oper_class;
257 unsigned int _preference;
258 unsigned int _reason;
259 unsigned int _reason_detail;
262 size = size ? size * 2 : 1;
263 tmp_chans = os_realloc_array(chans, size,
266 wpa_printf(MSG_ERROR,
267 "Couldn't reallocate non_pref_chan");
275 ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
276 &_chan, &_preference, &_reason,
278 if ((ret != 4 && ret != 5) ||
279 _oper_class > 255 || _chan > 255 ||
280 _preference > 255 || _reason > 65535 ||
281 (ret == 5 && _reason_detail > 255)) {
282 wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
286 chan->oper_class = _oper_class;
288 chan->preference = _preference;
289 chan->reason = _reason;
290 chan->reason_detail = ret == 4 ? 0 : _reason_detail;
292 if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
293 chan->chan, chan->reason)) {
294 wpa_printf(MSG_ERROR,
295 "Invalid non_pref_chan: oper class %d chan %d reason %d",
296 chan->oper_class, chan->chan, chan->reason);
300 for (i = 0; i < num; i++)
301 if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
304 wpa_printf(MSG_ERROR,
305 "oper class %d chan %d is duplicated",
306 chan->oper_class, chan->chan);
316 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
317 wpa_non_pref_chan_cmp);
321 os_free(wpa_s->non_pref_chan);
322 wpa_s->non_pref_chan = chans;
323 wpa_s->non_pref_chan_num = num;
324 wpas_mbo_send_wnm_notification(wpa_s);