]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/mbo.c
MBO: Add cellular capability to MBO IE
[thirdparty/hostap.git] / wpa_supplicant / mbo.c
CommitLineData
92c6e2e3
DS
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
13#include "utils/includes.h"
14
15#include "utils/common.h"
16#include "common/ieee802_11_defs.h"
17#include "config.h"
18#include "wpa_supplicant_i.h"
19#include "driver_i.h"
20#include "bss.h"
21
22/* type + length + oui + oui type */
23#define MBO_IE_HEADER 6
24
25
26static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
27{
28 if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
29 return -1;
30
31 /* Only checking the validity of the channel and oper_class */
32 if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
33 return -1;
34
35 return 0;
36}
37
38
39static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
40 struct wpabuf *mbo,
41 u8 start, u8 end)
42{
43 u8 i;
44
45 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
46
47 for (i = start; i < end; i++)
48 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
49
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);
53}
54
55
56static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
57 struct wpabuf *mbo, u8 start, u8 end)
58{
59 size_t size = end - start + 4;
60
61 if (size + 2 > wpabuf_tailroom(mbo))
62 return;
63
64 wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
65 wpabuf_put_u8(mbo, size); /* Length */
66
67 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
68}
69
70
2d5b8614
DS
71static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
72{
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);
77}
78
79
80static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
81 struct wpabuf *mbo, u8 start,
82 u8 end)
83{
84 size_t size = end - start + 8;
85
86 if (size + 2 > wpabuf_tailroom(mbo))
87 return;
88
89 wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
90 wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
91}
92
92c6e2e3 93
2d5b8614
DS
94static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
95 struct wpabuf *mbo, int subelement)
92c6e2e3
DS
96{
97 u8 i, start = 0;
98 struct wpa_mbo_non_pref_channel *start_pref;
99
2d5b8614
DS
100 if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
101 if (subelement)
102 wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
92c6e2e3 103 return;
2d5b8614 104 }
92c6e2e3
DS
105 start_pref = &wpa_s->non_pref_chan[0];
106
107 for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
108 struct wpa_mbo_non_pref_channel *non_pref = NULL;
109
110 if (i < wpa_s->non_pref_chan_num)
111 non_pref = &wpa_s->non_pref_chan[i];
112 if (!non_pref ||
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) {
2d5b8614
DS
117 if (subelement)
118 wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
119 start, i);
120 else
121 wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
122 i);
92c6e2e3
DS
123
124 if (!non_pref)
125 return;
126
127 start = i;
128 start_pref = non_pref;
129 }
130 }
131}
132
133
134int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
135{
136 struct wpabuf *mbo;
137 int res;
138
c5d193d7 139 if (len < MBO_IE_HEADER + 3 + 7)
92c6e2e3
DS
140 return 0;
141
142 /* Leave room for the MBO IE header */
143 mbo = wpabuf_alloc(len - MBO_IE_HEADER);
144 if (!mbo)
145 return 0;
146
147 /* Add non-preferred channels attribute */
2d5b8614 148 wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
92c6e2e3 149
c5d193d7
DS
150 /*
151 * Send cellular capabilities attribute even if AP does not advertise
152 * cellular capabilities.
153 */
154 wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
155 wpabuf_put_u8(mbo, 1);
156 wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
157
92c6e2e3
DS
158 res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
159 if (!res)
160 wpa_printf(MSG_ERROR, "Failed to add MBO IE");
161
162 wpabuf_free(mbo);
163 return res;
164}
165
166
2d5b8614
DS
167static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s)
168{
169 struct wpabuf *buf;
170 int res;
171
172 /*
173 * Send WNM-Notification Request frame only in case of a change in
174 * non-preferred channels list during association, if the AP supports
175 * MBO.
176 */
177 if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
178 !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
179 return;
180
181 buf = wpabuf_alloc(512);
182 if (!buf)
183 return;
184
185 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
186 wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
187 wpa_s->mbo_wnm_token++;
188 if (wpa_s->mbo_wnm_token == 0)
189 wpa_s->mbo_wnm_token++;
190 wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
191 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
192
193 wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
194
195 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
196 wpa_s->own_addr, wpa_s->bssid,
197 wpabuf_head(buf), wpabuf_len(buf), 0);
198 if (res < 0)
199 wpa_printf(MSG_DEBUG,
200 "Failed to send WNM-Notification Request frame with non-preferred channel list");
201
202 wpabuf_free(buf);
203}
204
205
92c6e2e3
DS
206static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
207 struct wpa_mbo_non_pref_channel *b)
208{
209 return a->oper_class == b->oper_class && a->chan == b->chan;
210}
211
212
213/*
214 * wpa_non_pref_chan_cmp - Compare two channels for sorting
215 *
216 * In MBO IE non-preferred channel subelement we can put many channels in an
217 * attribute if they are in the same operating class and have the same
218 * preference, reason, and reason detail. To make it easy for the functions that
219 * build the IE attributes and WNM Request subelements, save the channels sorted
220 * by their oper_class, reason, and reason_detail.
221 */
222static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
223{
224 const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
225
226 if (a->oper_class != b->oper_class)
227 return a->oper_class - b->oper_class;
228 if (a->reason != b->reason)
229 return a->reason - b->reason;
230 if (a->reason_detail != b->reason_detail)
231 return a->reason_detail - b->reason_detail;
232 return a->preference - b->preference;
233}
234
235
236int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
237 const char *non_pref_chan)
238{
239 char *cmd, *token, *context = NULL;
240 struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
241 size_t num = 0, size = 0;
242 unsigned i;
243
244 wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
245 non_pref_chan ? non_pref_chan : "N/A");
246
247 /*
248 * The shortest channel configuration is 10 characters - commas, 3
249 * colons, and 4 values that one of them (oper_class) is 2 digits or
250 * more.
251 */
252 if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
253 goto update;
254
255 cmd = os_strdup(non_pref_chan);
256 if (!cmd)
257 return -1;
258
259 while ((token = str_token(cmd, " ", &context))) {
260 struct wpa_mbo_non_pref_channel *chan;
261 int ret;
262 unsigned int _oper_class;
263 unsigned int _chan;
264 unsigned int _preference;
265 unsigned int _reason;
266 unsigned int _reason_detail;
267
268 if (num == size) {
269 size = size ? size * 2 : 1;
270 tmp_chans = os_realloc_array(chans, size,
271 sizeof(*chans));
272 if (!tmp_chans) {
273 wpa_printf(MSG_ERROR,
274 "Couldn't reallocate non_pref_chan");
275 goto fail;
276 }
277 chans = tmp_chans;
278 }
279
280 chan = &chans[num];
281
282 ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
283 &_chan, &_preference, &_reason,
284 &_reason_detail);
285 if ((ret != 4 && ret != 5) ||
286 _oper_class > 255 || _chan > 255 ||
287 _preference > 255 || _reason > 65535 ||
288 (ret == 5 && _reason_detail > 255)) {
289 wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
290 token);
291 goto fail;
292 }
293 chan->oper_class = _oper_class;
294 chan->chan = _chan;
295 chan->preference = _preference;
296 chan->reason = _reason;
297 chan->reason_detail = ret == 4 ? 0 : _reason_detail;
298
299 if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
300 chan->chan, chan->reason)) {
301 wpa_printf(MSG_ERROR,
302 "Invalid non_pref_chan: oper class %d chan %d reason %d",
303 chan->oper_class, chan->chan, chan->reason);
304 goto fail;
305 }
306
307 for (i = 0; i < num; i++)
308 if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
309 break;
310 if (i != num) {
311 wpa_printf(MSG_ERROR,
312 "oper class %d chan %d is duplicated",
313 chan->oper_class, chan->chan);
314 goto fail;
315 }
316
317 num++;
318 }
319
320 os_free(cmd);
321
322 if (chans) {
323 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
324 wpa_non_pref_chan_cmp);
325 }
326
327update:
328 os_free(wpa_s->non_pref_chan);
329 wpa_s->non_pref_chan = chans;
330 wpa_s->non_pref_chan_num = num;
2d5b8614 331 wpas_mbo_send_wnm_notification(wpa_s);
92c6e2e3
DS
332
333 return 0;
334
335fail:
336 os_free(chans);
337 os_free(cmd);
338 return -1;
339}
c5d193d7
DS
340
341
342void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
343{
344 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
345 wpabuf_put_u8(ie, 7);
346 wpabuf_put_be24(ie, OUI_WFA);
347 wpabuf_put_u8(ie, MBO_OUI_TYPE);
348
349 wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
350 wpabuf_put_u8(ie, 1);
351 wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
352}