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_attrs(struct wpa_supplicant *wpa_s,
76 struct wpa_mbo_non_pref_channel *start_pref;
78 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num)
80 start_pref = &wpa_s->non_pref_chan[0];
82 for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
83 struct wpa_mbo_non_pref_channel *non_pref = NULL;
85 if (i < wpa_s->non_pref_chan_num)
86 non_pref = &wpa_s->non_pref_chan[i];
88 non_pref->oper_class != start_pref->oper_class ||
89 non_pref->reason != start_pref->reason ||
90 non_pref->reason_detail != start_pref->reason_detail ||
91 non_pref->preference != start_pref->preference) {
92 wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start, i);
98 start_pref = non_pref;
104 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
109 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num ||
110 len < MBO_IE_HEADER + 7)
113 /* Leave room for the MBO IE header */
114 mbo = wpabuf_alloc(len - MBO_IE_HEADER);
118 /* Add non-preferred channels attribute */
119 wpas_mbo_non_pref_chan_attrs(wpa_s, mbo);
121 res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
123 wpa_printf(MSG_ERROR, "Failed to add MBO IE");
130 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
131 struct wpa_mbo_non_pref_channel *b)
133 return a->oper_class == b->oper_class && a->chan == b->chan;
138 * wpa_non_pref_chan_cmp - Compare two channels for sorting
140 * In MBO IE non-preferred channel subelement we can put many channels in an
141 * attribute if they are in the same operating class and have the same
142 * preference, reason, and reason detail. To make it easy for the functions that
143 * build the IE attributes and WNM Request subelements, save the channels sorted
144 * by their oper_class, reason, and reason_detail.
146 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
148 const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
150 if (a->oper_class != b->oper_class)
151 return a->oper_class - b->oper_class;
152 if (a->reason != b->reason)
153 return a->reason - b->reason;
154 if (a->reason_detail != b->reason_detail)
155 return a->reason_detail - b->reason_detail;
156 return a->preference - b->preference;
160 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
161 const char *non_pref_chan)
163 char *cmd, *token, *context = NULL;
164 struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
165 size_t num = 0, size = 0;
168 wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
169 non_pref_chan ? non_pref_chan : "N/A");
172 * The shortest channel configuration is 10 characters - commas, 3
173 * colons, and 4 values that one of them (oper_class) is 2 digits or
176 if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
179 cmd = os_strdup(non_pref_chan);
183 while ((token = str_token(cmd, " ", &context))) {
184 struct wpa_mbo_non_pref_channel *chan;
186 unsigned int _oper_class;
188 unsigned int _preference;
189 unsigned int _reason;
190 unsigned int _reason_detail;
193 size = size ? size * 2 : 1;
194 tmp_chans = os_realloc_array(chans, size,
197 wpa_printf(MSG_ERROR,
198 "Couldn't reallocate non_pref_chan");
206 ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
207 &_chan, &_preference, &_reason,
209 if ((ret != 4 && ret != 5) ||
210 _oper_class > 255 || _chan > 255 ||
211 _preference > 255 || _reason > 65535 ||
212 (ret == 5 && _reason_detail > 255)) {
213 wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
217 chan->oper_class = _oper_class;
219 chan->preference = _preference;
220 chan->reason = _reason;
221 chan->reason_detail = ret == 4 ? 0 : _reason_detail;
223 if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
224 chan->chan, chan->reason)) {
225 wpa_printf(MSG_ERROR,
226 "Invalid non_pref_chan: oper class %d chan %d reason %d",
227 chan->oper_class, chan->chan, chan->reason);
231 for (i = 0; i < num; i++)
232 if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
235 wpa_printf(MSG_ERROR,
236 "oper class %d chan %d is duplicated",
237 chan->oper_class, chan->chan);
247 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
248 wpa_non_pref_chan_cmp);
252 os_free(wpa_s->non_pref_chan);
253 wpa_s->non_pref_chan = chans;
254 wpa_s->non_pref_chan_num = num;