1928750b6Schristos /*
2928750b6Schristos  * wpa_supplicant - MBO
3928750b6Schristos  *
4928750b6Schristos  * Copyright(c) 2015 Intel Deutschland GmbH
5928750b6Schristos  * Contact Information:
6928750b6Schristos  * Intel Linux Wireless <ilw@linux.intel.com>
7928750b6Schristos  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
8928750b6Schristos  *
9928750b6Schristos  * This software may be distributed under the terms of the BSD license.
10928750b6Schristos  * See README for more details.
11928750b6Schristos  */
12928750b6Schristos 
13928750b6Schristos #include "utils/includes.h"
14928750b6Schristos 
15928750b6Schristos #include "utils/common.h"
16928750b6Schristos #include "common/ieee802_11_defs.h"
17928750b6Schristos #include "common/gas.h"
18928750b6Schristos #include "config.h"
19928750b6Schristos #include "wpa_supplicant_i.h"
20928750b6Schristos #include "driver_i.h"
21928750b6Schristos #include "bss.h"
22928750b6Schristos #include "scan.h"
23928750b6Schristos 
24928750b6Schristos /* type + length + oui + oui type */
25928750b6Schristos #define MBO_IE_HEADER 6
26928750b6Schristos 
27928750b6Schristos 
wpas_mbo_validate_non_pref_chan(u8 oper_class,u8 chan,u8 reason)28928750b6Schristos static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
29928750b6Schristos {
30928750b6Schristos 	if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
31928750b6Schristos 		return -1;
32928750b6Schristos 
33928750b6Schristos 	/* Only checking the validity of the channel and oper_class */
34928750b6Schristos 	if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
35928750b6Schristos 		return -1;
36928750b6Schristos 
37928750b6Schristos 	return 0;
38928750b6Schristos }
39928750b6Schristos 
40928750b6Schristos 
mbo_attr_from_mbo_ie(const u8 * mbo_ie,enum mbo_attr_id attr)41ebb5671cSchristos const u8 * mbo_attr_from_mbo_ie(const u8 *mbo_ie, enum mbo_attr_id attr)
42ebb5671cSchristos {
43ebb5671cSchristos 	const u8 *mbo;
44ebb5671cSchristos 	u8 ie_len = mbo_ie[1];
45ebb5671cSchristos 
46ebb5671cSchristos 	if (ie_len < MBO_IE_HEADER - 2)
47ebb5671cSchristos 		return NULL;
48ebb5671cSchristos 	mbo = mbo_ie + MBO_IE_HEADER;
49ebb5671cSchristos 
50ebb5671cSchristos 	return get_ie(mbo, 2 + ie_len - MBO_IE_HEADER, attr);
51ebb5671cSchristos }
52ebb5671cSchristos 
53ebb5671cSchristos 
mbo_get_attr_from_ies(const u8 * ies,size_t ies_len,enum mbo_attr_id attr)54*0d69f216Schristos const u8 * mbo_get_attr_from_ies(const u8 *ies, size_t ies_len,
55*0d69f216Schristos 				 enum mbo_attr_id attr)
56*0d69f216Schristos {
57*0d69f216Schristos 	const u8 *mbo_ie;
58*0d69f216Schristos 
59*0d69f216Schristos 	mbo_ie = get_vendor_ie(ies, ies_len, MBO_IE_VENDOR_TYPE);
60*0d69f216Schristos 	if (!mbo_ie)
61*0d69f216Schristos 		return NULL;
62*0d69f216Schristos 
63*0d69f216Schristos 	return mbo_attr_from_mbo_ie(mbo_ie, attr);
64*0d69f216Schristos }
65*0d69f216Schristos 
66*0d69f216Schristos 
wpas_mbo_get_bss_attr(struct wpa_bss * bss,enum mbo_attr_id attr)67928750b6Schristos const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
68928750b6Schristos {
69928750b6Schristos 	const u8 *mbo, *end;
70928750b6Schristos 
71928750b6Schristos 	if (!bss)
72928750b6Schristos 		return NULL;
73928750b6Schristos 
74928750b6Schristos 	mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
75928750b6Schristos 	if (!mbo)
76928750b6Schristos 		return NULL;
77928750b6Schristos 
78928750b6Schristos 	end = mbo + 2 + mbo[1];
79928750b6Schristos 	mbo += MBO_IE_HEADER;
80928750b6Schristos 
81928750b6Schristos 	return get_ie(mbo, end - mbo, attr);
82928750b6Schristos }
83928750b6Schristos 
84928750b6Schristos 
wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)85928750b6Schristos static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
86928750b6Schristos 					     struct wpabuf *mbo,
87928750b6Schristos 					     u8 start, u8 end)
88928750b6Schristos {
89928750b6Schristos 	u8 i;
90928750b6Schristos 
91928750b6Schristos 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
92928750b6Schristos 
93928750b6Schristos 	for (i = start; i < end; i++)
94928750b6Schristos 		wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
95928750b6Schristos 
96928750b6Schristos 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
97928750b6Schristos 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
98928750b6Schristos }
99928750b6Schristos 
100928750b6Schristos 
wpas_mbo_non_pref_chan_attr_hdr(struct wpabuf * mbo,size_t size)101*0d69f216Schristos static void wpas_mbo_non_pref_chan_attr_hdr(struct wpabuf *mbo, size_t size)
102*0d69f216Schristos {
103*0d69f216Schristos 	wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
104*0d69f216Schristos 	wpabuf_put_u8(mbo, size); /* Length */
105*0d69f216Schristos }
106*0d69f216Schristos 
107*0d69f216Schristos 
wpas_mbo_non_pref_chan_attr(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)108928750b6Schristos static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
109928750b6Schristos 					struct wpabuf *mbo, u8 start, u8 end)
110928750b6Schristos {
111928750b6Schristos 	size_t size = end - start + 3;
112928750b6Schristos 
113928750b6Schristos 	if (size + 2 > wpabuf_tailroom(mbo))
114928750b6Schristos 		return;
115928750b6Schristos 
116*0d69f216Schristos 	wpas_mbo_non_pref_chan_attr_hdr(mbo, size);
117928750b6Schristos 	wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
118928750b6Schristos }
119928750b6Schristos 
120928750b6Schristos 
wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf * mbo,u8 len)121928750b6Schristos static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
122928750b6Schristos {
123928750b6Schristos 	wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
124928750b6Schristos 	wpabuf_put_u8(mbo, len); /* Length */
125928750b6Schristos 	wpabuf_put_be24(mbo, OUI_WFA);
126928750b6Schristos 	wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
127928750b6Schristos }
128928750b6Schristos 
129928750b6Schristos 
wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)130928750b6Schristos static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
131928750b6Schristos 					      struct wpabuf *mbo, u8 start,
132928750b6Schristos 					      u8 end)
133928750b6Schristos {
134928750b6Schristos 	size_t size = end - start + 7;
135928750b6Schristos 
136928750b6Schristos 	if (size + 2 > wpabuf_tailroom(mbo))
137928750b6Schristos 		return;
138928750b6Schristos 
139928750b6Schristos 	wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
140928750b6Schristos 	wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
141928750b6Schristos }
142928750b6Schristos 
143928750b6Schristos 
wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,int subelement)144928750b6Schristos static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
145928750b6Schristos 					 struct wpabuf *mbo, int subelement)
146928750b6Schristos {
147928750b6Schristos 	u8 i, start = 0;
148928750b6Schristos 	struct wpa_mbo_non_pref_channel *start_pref;
149928750b6Schristos 
150928750b6Schristos 	if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
151928750b6Schristos 		if (subelement)
152928750b6Schristos 			wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
153*0d69f216Schristos 		else
154*0d69f216Schristos 			wpas_mbo_non_pref_chan_attr_hdr(mbo, 0);
155928750b6Schristos 		return;
156928750b6Schristos 	}
157928750b6Schristos 	start_pref = &wpa_s->non_pref_chan[0];
158928750b6Schristos 
159928750b6Schristos 	for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
160928750b6Schristos 		struct wpa_mbo_non_pref_channel *non_pref = NULL;
161928750b6Schristos 
162928750b6Schristos 		if (i < wpa_s->non_pref_chan_num)
163928750b6Schristos 			non_pref = &wpa_s->non_pref_chan[i];
164928750b6Schristos 		if (!non_pref ||
165928750b6Schristos 		    non_pref->oper_class != start_pref->oper_class ||
166928750b6Schristos 		    non_pref->reason != start_pref->reason ||
167928750b6Schristos 		    non_pref->preference != start_pref->preference) {
168928750b6Schristos 			if (subelement)
169928750b6Schristos 				wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
170928750b6Schristos 								  start, i);
171928750b6Schristos 			else
172928750b6Schristos 				wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
173928750b6Schristos 							    i);
174928750b6Schristos 
175928750b6Schristos 			if (!non_pref)
176928750b6Schristos 				return;
177928750b6Schristos 
178928750b6Schristos 			start = i;
179928750b6Schristos 			start_pref = non_pref;
180928750b6Schristos 		}
181928750b6Schristos 	}
182928750b6Schristos }
183928750b6Schristos 
184928750b6Schristos 
wpas_mbo_ie(struct wpa_supplicant * wpa_s,u8 * buf,size_t len,int add_oce_capa)185ebb5671cSchristos int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len,
186ebb5671cSchristos 		int add_oce_capa)
187928750b6Schristos {
188928750b6Schristos 	struct wpabuf *mbo;
189928750b6Schristos 	int res;
190928750b6Schristos 
191ebb5671cSchristos 	if (len < MBO_IE_HEADER + 3 + 7 +
192ebb5671cSchristos 	    ((wpa_s->enable_oce & OCE_STA) ? 3 : 0))
193928750b6Schristos 		return 0;
194928750b6Schristos 
195928750b6Schristos 	/* Leave room for the MBO IE header */
196928750b6Schristos 	mbo = wpabuf_alloc(len - MBO_IE_HEADER);
197928750b6Schristos 	if (!mbo)
198928750b6Schristos 		return 0;
199928750b6Schristos 
200928750b6Schristos 	/* Add non-preferred channels attribute */
201928750b6Schristos 	wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
202928750b6Schristos 
203928750b6Schristos 	/*
204928750b6Schristos 	 * Send cellular capabilities attribute even if AP does not advertise
205928750b6Schristos 	 * cellular capabilities.
206928750b6Schristos 	 */
207928750b6Schristos 	wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
208928750b6Schristos 	wpabuf_put_u8(mbo, 1);
209928750b6Schristos 	wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
210928750b6Schristos 
211ebb5671cSchristos 	/* Add OCE capability indication attribute if OCE is enabled */
212ebb5671cSchristos 	if ((wpa_s->enable_oce & OCE_STA) && add_oce_capa) {
213ebb5671cSchristos 		wpabuf_put_u8(mbo, OCE_ATTR_ID_CAPA_IND);
214ebb5671cSchristos 		wpabuf_put_u8(mbo, 1);
215ebb5671cSchristos 		wpabuf_put_u8(mbo, OCE_RELEASE);
216ebb5671cSchristos 	}
217ebb5671cSchristos 
218928750b6Schristos 	res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
219928750b6Schristos 	if (!res)
220ebb5671cSchristos 		wpa_printf(MSG_ERROR, "Failed to add MBO/OCE IE");
221928750b6Schristos 
222928750b6Schristos 	wpabuf_free(mbo);
223928750b6Schristos 	return res;
224928750b6Schristos }
225928750b6Schristos 
226928750b6Schristos 
wpas_mbo_send_wnm_notification(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)227928750b6Schristos static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
228928750b6Schristos 					   const u8 *data, size_t len)
229928750b6Schristos {
230928750b6Schristos 	struct wpabuf *buf;
231928750b6Schristos 	int res;
232928750b6Schristos 
233928750b6Schristos 	/*
234928750b6Schristos 	 * Send WNM-Notification Request frame only in case of a change in
235928750b6Schristos 	 * non-preferred channels list during association, if the AP supports
236928750b6Schristos 	 * MBO.
237928750b6Schristos 	 */
238928750b6Schristos 	if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
239928750b6Schristos 	    !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
240928750b6Schristos 		return;
241928750b6Schristos 
242928750b6Schristos 	buf = wpabuf_alloc(4 + len);
243928750b6Schristos 	if (!buf)
244928750b6Schristos 		return;
245928750b6Schristos 
246928750b6Schristos 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
247928750b6Schristos 	wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
248928750b6Schristos 	wpa_s->mbo_wnm_token++;
249928750b6Schristos 	if (wpa_s->mbo_wnm_token == 0)
250928750b6Schristos 		wpa_s->mbo_wnm_token++;
251928750b6Schristos 	wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
252928750b6Schristos 	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
253928750b6Schristos 
254928750b6Schristos 	wpabuf_put_data(buf, data, len);
255928750b6Schristos 
256928750b6Schristos 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
257928750b6Schristos 				  wpa_s->own_addr, wpa_s->bssid,
258928750b6Schristos 				  wpabuf_head(buf), wpabuf_len(buf), 0);
259928750b6Schristos 	if (res < 0)
260928750b6Schristos 		wpa_printf(MSG_DEBUG,
261928750b6Schristos 			   "Failed to send WNM-Notification Request frame with non-preferred channel list");
262928750b6Schristos 
263928750b6Schristos 	wpabuf_free(buf);
264928750b6Schristos }
265928750b6Schristos 
266928750b6Schristos 
wpas_mbo_non_pref_chan_changed(struct wpa_supplicant * wpa_s)267928750b6Schristos static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
268928750b6Schristos {
269928750b6Schristos 	struct wpabuf *buf;
270928750b6Schristos 
271928750b6Schristos 	buf = wpabuf_alloc(512);
272928750b6Schristos 	if (!buf)
273928750b6Schristos 		return;
274928750b6Schristos 
275928750b6Schristos 	wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
276928750b6Schristos 	wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
277928750b6Schristos 				       wpabuf_len(buf));
278*0d69f216Schristos 	wpas_update_mbo_connect_params(wpa_s);
279928750b6Schristos 	wpabuf_free(buf);
280928750b6Schristos }
281928750b6Schristos 
282928750b6Schristos 
wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel * a,struct wpa_mbo_non_pref_channel * b)283928750b6Schristos static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
284928750b6Schristos 				   struct wpa_mbo_non_pref_channel *b)
285928750b6Schristos {
286928750b6Schristos 	return a->oper_class == b->oper_class && a->chan == b->chan;
287928750b6Schristos }
288928750b6Schristos 
289928750b6Schristos 
290928750b6Schristos /*
291928750b6Schristos  * wpa_non_pref_chan_cmp - Compare two channels for sorting
292928750b6Schristos  *
293928750b6Schristos  * In MBO IE non-preferred channel subelement we can put many channels in an
294928750b6Schristos  * attribute if they are in the same operating class and have the same
295928750b6Schristos  * preference and reason. To make it easy for the functions that build
296928750b6Schristos  * the IE attributes and WNM Request subelements, save the channels sorted
297928750b6Schristos  * by their oper_class and reason.
298928750b6Schristos  */
wpa_non_pref_chan_cmp(const void * _a,const void * _b)299928750b6Schristos static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
300928750b6Schristos {
301928750b6Schristos 	const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
302928750b6Schristos 
303928750b6Schristos 	if (a->oper_class != b->oper_class)
304*0d69f216Schristos 		return (int) a->oper_class - (int) b->oper_class;
305928750b6Schristos 	if (a->reason != b->reason)
306*0d69f216Schristos 		return (int) a->reason - (int) b->reason;
307*0d69f216Schristos 	return (int) a->preference - (int) b->preference;
308928750b6Schristos }
309928750b6Schristos 
310928750b6Schristos 
wpas_mbo_update_non_pref_chan(struct wpa_supplicant * wpa_s,const char * non_pref_chan)311928750b6Schristos int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
312928750b6Schristos 				  const char *non_pref_chan)
313928750b6Schristos {
314928750b6Schristos 	char *cmd, *token, *context = NULL;
315928750b6Schristos 	struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
316928750b6Schristos 	size_t num = 0, size = 0;
317928750b6Schristos 	unsigned i;
318928750b6Schristos 
319928750b6Schristos 	wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
320928750b6Schristos 		   non_pref_chan ? non_pref_chan : "N/A");
321928750b6Schristos 
322928750b6Schristos 	/*
323ebb5671cSchristos 	 * The shortest channel configuration is 7 characters - 3 colons and
324ebb5671cSchristos 	 * 4 values.
325928750b6Schristos 	 */
326ebb5671cSchristos 	if (!non_pref_chan || os_strlen(non_pref_chan) < 7)
327928750b6Schristos 		goto update;
328928750b6Schristos 
329928750b6Schristos 	cmd = os_strdup(non_pref_chan);
330928750b6Schristos 	if (!cmd)
331928750b6Schristos 		return -1;
332928750b6Schristos 
333928750b6Schristos 	while ((token = str_token(cmd, " ", &context))) {
334928750b6Schristos 		struct wpa_mbo_non_pref_channel *chan;
335928750b6Schristos 		int ret;
336928750b6Schristos 		unsigned int _oper_class;
337928750b6Schristos 		unsigned int _chan;
338928750b6Schristos 		unsigned int _preference;
339928750b6Schristos 		unsigned int _reason;
340928750b6Schristos 
341928750b6Schristos 		if (num == size) {
342928750b6Schristos 			size = size ? size * 2 : 1;
343928750b6Schristos 			tmp_chans = os_realloc_array(chans, size,
344928750b6Schristos 						     sizeof(*chans));
345928750b6Schristos 			if (!tmp_chans) {
346928750b6Schristos 				wpa_printf(MSG_ERROR,
347928750b6Schristos 					   "Couldn't reallocate non_pref_chan");
348928750b6Schristos 				goto fail;
349928750b6Schristos 			}
350928750b6Schristos 			chans = tmp_chans;
351928750b6Schristos 		}
352928750b6Schristos 
353928750b6Schristos 		chan = &chans[num];
354928750b6Schristos 
355928750b6Schristos 		ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
356928750b6Schristos 			     &_chan, &_preference, &_reason);
357928750b6Schristos 		if (ret != 4 ||
358928750b6Schristos 		    _oper_class > 255 || _chan > 255 ||
359928750b6Schristos 		    _preference > 255 || _reason > 65535 ) {
360928750b6Schristos 			wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
361928750b6Schristos 				   token);
362928750b6Schristos 			goto fail;
363928750b6Schristos 		}
364928750b6Schristos 		chan->oper_class = _oper_class;
365928750b6Schristos 		chan->chan = _chan;
366928750b6Schristos 		chan->preference = _preference;
367928750b6Schristos 		chan->reason = _reason;
368928750b6Schristos 
369928750b6Schristos 		if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
370928750b6Schristos 						    chan->chan, chan->reason)) {
371928750b6Schristos 			wpa_printf(MSG_ERROR,
372928750b6Schristos 				   "Invalid non_pref_chan: oper class %d chan %d reason %d",
373928750b6Schristos 				   chan->oper_class, chan->chan, chan->reason);
374928750b6Schristos 			goto fail;
375928750b6Schristos 		}
376928750b6Schristos 
377928750b6Schristos 		for (i = 0; i < num; i++)
378928750b6Schristos 			if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
379928750b6Schristos 				break;
380928750b6Schristos 		if (i != num) {
381928750b6Schristos 			wpa_printf(MSG_ERROR,
382928750b6Schristos 				   "oper class %d chan %d is duplicated",
383928750b6Schristos 				   chan->oper_class, chan->chan);
384928750b6Schristos 			goto fail;
385928750b6Schristos 		}
386928750b6Schristos 
387928750b6Schristos 		num++;
388928750b6Schristos 	}
389928750b6Schristos 
390928750b6Schristos 	os_free(cmd);
391928750b6Schristos 
392928750b6Schristos 	if (chans) {
393928750b6Schristos 		qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
394928750b6Schristos 		      wpa_non_pref_chan_cmp);
395928750b6Schristos 	}
396928750b6Schristos 
397928750b6Schristos update:
398928750b6Schristos 	os_free(wpa_s->non_pref_chan);
399928750b6Schristos 	wpa_s->non_pref_chan = chans;
400928750b6Schristos 	wpa_s->non_pref_chan_num = num;
401928750b6Schristos 	wpas_mbo_non_pref_chan_changed(wpa_s);
402928750b6Schristos 
403928750b6Schristos 	return 0;
404928750b6Schristos 
405928750b6Schristos fail:
406928750b6Schristos 	os_free(chans);
407928750b6Schristos 	os_free(cmd);
408928750b6Schristos 	return -1;
409928750b6Schristos }
410928750b6Schristos 
411928750b6Schristos 
wpas_mbo_scan_ie(struct wpa_supplicant * wpa_s,struct wpabuf * ie)412928750b6Schristos void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
413928750b6Schristos {
414ebb5671cSchristos 	u8 *len;
415ebb5671cSchristos 
416928750b6Schristos 	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
417ebb5671cSchristos 	len = wpabuf_put(ie, 1);
418ebb5671cSchristos 
419928750b6Schristos 	wpabuf_put_be24(ie, OUI_WFA);
420928750b6Schristos 	wpabuf_put_u8(ie, MBO_OUI_TYPE);
421928750b6Schristos 
422928750b6Schristos 	wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
423928750b6Schristos 	wpabuf_put_u8(ie, 1);
424928750b6Schristos 	wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
425ebb5671cSchristos 	if (wpa_s->enable_oce & OCE_STA) {
426ebb5671cSchristos 		wpabuf_put_u8(ie, OCE_ATTR_ID_CAPA_IND);
427ebb5671cSchristos 		wpabuf_put_u8(ie, 1);
428ebb5671cSchristos 		wpabuf_put_u8(ie, OCE_RELEASE);
429928750b6Schristos 	}
430ebb5671cSchristos 	*len = (u8 *) wpabuf_put(ie, 0) - len - 1;
431928750b6Schristos }
432928750b6Schristos 
433928750b6Schristos 
wpas_mbo_ie_trans_req(struct wpa_supplicant * wpa_s,const u8 * mbo_ie,size_t len)434928750b6Schristos void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
435928750b6Schristos 			   size_t len)
436928750b6Schristos {
437ebb5671cSchristos 	const u8 *pos, *cell_pref = NULL;
438928750b6Schristos 	u8 id, elen;
439928750b6Schristos 	u16 disallowed_sec = 0;
440928750b6Schristos 
441928750b6Schristos 	if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
442928750b6Schristos 	    mbo_ie[3] != MBO_OUI_TYPE)
443928750b6Schristos 		return;
444928750b6Schristos 
445928750b6Schristos 	pos = mbo_ie + 4;
446928750b6Schristos 	len -= 4;
447928750b6Schristos 
448928750b6Schristos 	while (len >= 2) {
449928750b6Schristos 		id = *pos++;
450928750b6Schristos 		elen = *pos++;
451928750b6Schristos 		len -= 2;
452928750b6Schristos 
453928750b6Schristos 		if (elen > len)
454928750b6Schristos 			goto fail;
455928750b6Schristos 
456928750b6Schristos 		switch (id) {
457928750b6Schristos 		case MBO_ATTR_ID_CELL_DATA_PREF:
458928750b6Schristos 			if (elen != 1)
459928750b6Schristos 				goto fail;
460928750b6Schristos 
461928750b6Schristos 			if (wpa_s->conf->mbo_cell_capa ==
462928750b6Schristos 			    MBO_CELL_CAPA_AVAILABLE)
463928750b6Schristos 				cell_pref = pos;
464928750b6Schristos 			else
465928750b6Schristos 				wpa_printf(MSG_DEBUG,
466928750b6Schristos 					   "MBO: Station does not support Cellular data connection");
467928750b6Schristos 			break;
468928750b6Schristos 		case MBO_ATTR_ID_TRANSITION_REASON:
469928750b6Schristos 			if (elen != 1)
470928750b6Schristos 				goto fail;
471928750b6Schristos 
472ebb5671cSchristos 			wpa_s->wnm_mbo_trans_reason_present = 1;
473ebb5671cSchristos 			wpa_s->wnm_mbo_transition_reason = *pos;
474928750b6Schristos 			break;
475928750b6Schristos 		case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
476928750b6Schristos 			if (elen != 2)
477928750b6Schristos 				goto fail;
478928750b6Schristos 
479928750b6Schristos 			if (wpa_s->wnm_mode &
480928750b6Schristos 			    WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
481928750b6Schristos 				wpa_printf(MSG_DEBUG,
482928750b6Schristos 					   "MBO: Unexpected association retry delay, BSS is terminating");
483928750b6Schristos 				goto fail;
484928750b6Schristos 			} else if (wpa_s->wnm_mode &
485928750b6Schristos 				   WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
486928750b6Schristos 				disallowed_sec = WPA_GET_LE16(pos);
487ebb5671cSchristos 				wpa_printf(MSG_DEBUG,
488ebb5671cSchristos 					   "MBO: Association retry delay: %u",
489ebb5671cSchristos 					   disallowed_sec);
490928750b6Schristos 			} else {
491928750b6Schristos 				wpa_printf(MSG_DEBUG,
492928750b6Schristos 					   "MBO: Association retry delay attribute not in disassoc imminent mode");
493928750b6Schristos 			}
494928750b6Schristos 
495928750b6Schristos 			break;
496928750b6Schristos 		case MBO_ATTR_ID_AP_CAPA_IND:
497928750b6Schristos 		case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
498928750b6Schristos 		case MBO_ATTR_ID_CELL_DATA_CAPA:
499928750b6Schristos 		case MBO_ATTR_ID_ASSOC_DISALLOW:
500928750b6Schristos 		case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
501928750b6Schristos 			wpa_printf(MSG_DEBUG,
502928750b6Schristos 				   "MBO: Attribute %d should not be included in BTM Request frame",
503928750b6Schristos 				   id);
504928750b6Schristos 			break;
505928750b6Schristos 		default:
506928750b6Schristos 			wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
507928750b6Schristos 				   id);
508928750b6Schristos 			return;
509928750b6Schristos 		}
510928750b6Schristos 
511928750b6Schristos 		pos += elen;
512928750b6Schristos 		len -= elen;
513928750b6Schristos 	}
514928750b6Schristos 
515928750b6Schristos 	if (cell_pref)
516928750b6Schristos 		wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
517928750b6Schristos 			*cell_pref);
518928750b6Schristos 
519ebb5671cSchristos 	if (wpa_s->wnm_mbo_trans_reason_present)
520928750b6Schristos 		wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
521ebb5671cSchristos 			wpa_s->wnm_mbo_transition_reason);
522928750b6Schristos 
523928750b6Schristos 	if (disallowed_sec && wpa_s->current_bss)
524928750b6Schristos 		wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
525*0d69f216Schristos 				     disallowed_sec, 0);
526928750b6Schristos 
527928750b6Schristos 	return;
528928750b6Schristos fail:
529928750b6Schristos 	wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
530928750b6Schristos 		   id, elen, len);
531928750b6Schristos }
532928750b6Schristos 
533928750b6Schristos 
wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant * wpa_s,u8 * pos,size_t len,enum mbo_transition_reject_reason reason)534928750b6Schristos size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
535928750b6Schristos 				    size_t len,
536928750b6Schristos 				    enum mbo_transition_reject_reason reason)
537928750b6Schristos {
538928750b6Schristos 	u8 reject_attr[3];
539928750b6Schristos 
540928750b6Schristos 	reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
541928750b6Schristos 	reject_attr[1] = 1;
542928750b6Schristos 	reject_attr[2] = reason;
543928750b6Schristos 
544928750b6Schristos 	return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
545928750b6Schristos }
546928750b6Schristos 
547928750b6Schristos 
wpas_mbo_update_cell_capa(struct wpa_supplicant * wpa_s,u8 mbo_cell_capa)548928750b6Schristos void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
549928750b6Schristos {
550928750b6Schristos 	u8 cell_capa[7];
551928750b6Schristos 
552928750b6Schristos 	if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
553928750b6Schristos 		wpa_printf(MSG_DEBUG,
554928750b6Schristos 			   "MBO: Cellular capability already set to %u",
555928750b6Schristos 			   mbo_cell_capa);
556928750b6Schristos 		return;
557928750b6Schristos 	}
558928750b6Schristos 
559928750b6Schristos 	wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
560928750b6Schristos 
561928750b6Schristos 	cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
562928750b6Schristos 	cell_capa[1] = 5; /* Length */
563928750b6Schristos 	WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
564928750b6Schristos 	cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
565928750b6Schristos 	cell_capa[6] = mbo_cell_capa;
566928750b6Schristos 
567928750b6Schristos 	wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
568928750b6Schristos 	wpa_supplicant_set_default_scan_ies(wpa_s);
569*0d69f216Schristos 	wpas_update_mbo_connect_params(wpa_s);
570928750b6Schristos }
571928750b6Schristos 
572928750b6Schristos 
mbo_build_anqp_buf(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,u32 mbo_subtypes)573928750b6Schristos struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
574ebb5671cSchristos 				   struct wpa_bss *bss, u32 mbo_subtypes)
575928750b6Schristos {
576928750b6Schristos 	struct wpabuf *anqp_buf;
577928750b6Schristos 	u8 *len_pos;
578ebb5671cSchristos 	u8 i;
579928750b6Schristos 
580928750b6Schristos 	if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
581928750b6Schristos 		wpa_printf(MSG_INFO, "MBO: " MACSTR
582928750b6Schristos 			   " does not support MBO - cannot request MBO ANQP elements from it",
583928750b6Schristos 			   MAC2STR(bss->bssid));
584928750b6Schristos 		return NULL;
585928750b6Schristos 	}
586928750b6Schristos 
587ebb5671cSchristos 	/* Allocate size for the maximum case - all MBO subtypes are set */
588ebb5671cSchristos 	anqp_buf = wpabuf_alloc(9 + MAX_MBO_ANQP_SUBTYPE);
589928750b6Schristos 	if (!anqp_buf)
590928750b6Schristos 		return NULL;
591928750b6Schristos 
592928750b6Schristos 	len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
593928750b6Schristos 	wpabuf_put_be24(anqp_buf, OUI_WFA);
594928750b6Schristos 	wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
595928750b6Schristos 
596ebb5671cSchristos 	wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_QUERY_LIST);
597ebb5671cSchristos 
598ebb5671cSchristos 	/* The first valid MBO subtype is 1 */
599ebb5671cSchristos 	for (i = 1; i <= MAX_MBO_ANQP_SUBTYPE; i++) {
600ebb5671cSchristos 		if (mbo_subtypes & BIT(i))
601ebb5671cSchristos 			wpabuf_put_u8(anqp_buf, i);
602ebb5671cSchristos 	}
603ebb5671cSchristos 
604928750b6Schristos 	gas_anqp_set_element_len(anqp_buf, len_pos);
605928750b6Schristos 
606928750b6Schristos 	return anqp_buf;
607928750b6Schristos }
608ebb5671cSchristos 
609ebb5671cSchristos 
mbo_parse_rx_anqp_resp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 * sa,const u8 * data,size_t slen)610ebb5671cSchristos void mbo_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
611ebb5671cSchristos 			    struct wpa_bss *bss, const u8 *sa,
612ebb5671cSchristos 			    const u8 *data, size_t slen)
613ebb5671cSchristos {
614ebb5671cSchristos 	const u8 *pos = data;
615ebb5671cSchristos 	u8 subtype;
616ebb5671cSchristos 
617ebb5671cSchristos 	if (slen < 1)
618ebb5671cSchristos 		return;
619ebb5671cSchristos 
620ebb5671cSchristos 	subtype = *pos++;
621ebb5671cSchristos 	slen--;
622ebb5671cSchristos 
623ebb5671cSchristos 	switch (subtype) {
624ebb5671cSchristos 	case MBO_ANQP_SUBTYPE_CELL_CONN_PREF:
625ebb5671cSchristos 		if (slen < 1)
626ebb5671cSchristos 			break;
627ebb5671cSchristos 		wpa_msg(wpa_s, MSG_INFO, RX_MBO_ANQP MACSTR
628ebb5671cSchristos 			" cell_conn_pref=%u", MAC2STR(sa), *pos);
629ebb5671cSchristos 		break;
630ebb5671cSchristos 	default:
631ebb5671cSchristos 		wpa_printf(MSG_DEBUG, "MBO: Unsupported ANQP subtype %u",
632ebb5671cSchristos 			   subtype);
633ebb5671cSchristos 		break;
634ebb5671cSchristos 	}
635ebb5671cSchristos }
636