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 "common/gas.h"
18 #include "rsn_supp/wpa.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "driver_i.h"
22 #include "bss.h"
23 #include "scan.h"
24 
25 /* type + length + oui + oui type */
26 #define MBO_IE_HEADER 6
27 
28 
wpas_mbo_validate_non_pref_chan(u8 oper_class,u8 chan,u8 reason)29 static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
30 {
31 	if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
32 		return -1;
33 
34 	/* Only checking the validity of the channel and oper_class */
35 	if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
36 		return -1;
37 
38 	return 0;
39 }
40 
41 
mbo_attr_from_mbo_ie(const u8 * mbo_ie,enum mbo_attr_id attr)42 const u8 * mbo_attr_from_mbo_ie(const u8 *mbo_ie, enum mbo_attr_id attr)
43 {
44 	const u8 *mbo;
45 	u8 ie_len = mbo_ie[1];
46 
47 	if (ie_len < MBO_IE_HEADER - 2)
48 		return NULL;
49 	mbo = mbo_ie + MBO_IE_HEADER;
50 
51 	return get_ie(mbo, 2 + ie_len - MBO_IE_HEADER, attr);
52 }
53 
54 
mbo_get_attr_from_ies(const u8 * ies,size_t ies_len,enum mbo_attr_id attr)55 const u8 * mbo_get_attr_from_ies(const u8 *ies, size_t ies_len,
56 				 enum mbo_attr_id attr)
57 {
58 	const u8 *mbo_ie;
59 
60 	mbo_ie = get_vendor_ie(ies, ies_len, MBO_IE_VENDOR_TYPE);
61 	if (!mbo_ie)
62 		return NULL;
63 
64 	return mbo_attr_from_mbo_ie(mbo_ie, attr);
65 }
66 
67 
wpas_mbo_get_bss_attr(struct wpa_bss * bss,enum mbo_attr_id attr)68 const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
69 {
70 	const u8 *mbo, *end;
71 
72 	if (!bss)
73 		return NULL;
74 
75 	mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
76 	if (!mbo)
77 		return NULL;
78 
79 	end = mbo + 2 + mbo[1];
80 	mbo += MBO_IE_HEADER;
81 
82 	return get_ie(mbo, end - mbo, attr);
83 }
84 
85 
wpas_mbo_check_pmf(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid)86 void wpas_mbo_check_pmf(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
87 			struct wpa_ssid *ssid)
88 {
89 	const u8 *rsne, *mbo, *oce;
90 	struct wpa_ie_data ie;
91 
92 	wpa_s->disable_mbo_oce = 0;
93 	if (!bss)
94 		return;
95 	mbo = wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND);
96 	oce = wpas_mbo_get_bss_attr(bss, OCE_ATTR_ID_CAPA_IND);
97 	if (!mbo && !oce)
98 		return;
99 	if (oce && oce[1] >= 1 && (oce[2] & OCE_IS_STA_CFON))
100 		return; /* STA-CFON is not required to enable PMF */
101 	rsne = wpa_bss_get_ie(bss, WLAN_EID_RSN);
102 	if (!rsne || wpa_parse_wpa_ie(rsne, 2 + rsne[1], &ie) < 0)
103 		return; /* AP is not using RSN */
104 
105 	if (!(ie.capabilities & WPA_CAPABILITY_MFPC))
106 		wpa_s->disable_mbo_oce = 1; /* AP uses RSN without PMF */
107 	if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION)
108 		wpa_s->disable_mbo_oce = 1; /* STA uses RSN without PMF */
109 	if (wpa_s->disable_mbo_oce)
110 		wpa_printf(MSG_INFO,
111 			   "MBO: Disable MBO/OCE due to misbehaving AP not having enabled PMF");
112 }
113 
114 
wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)115 static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
116 					     struct wpabuf *mbo,
117 					     u8 start, u8 end)
118 {
119 	u8 i;
120 
121 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
122 
123 	for (i = start; i < end; i++)
124 		wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
125 
126 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
127 	wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
128 }
129 
130 
wpas_mbo_non_pref_chan_attr_hdr(struct wpabuf * mbo,size_t size)131 static void wpas_mbo_non_pref_chan_attr_hdr(struct wpabuf *mbo, size_t size)
132 {
133 	wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
134 	wpabuf_put_u8(mbo, size); /* Length */
135 }
136 
137 
wpas_mbo_non_pref_chan_attr(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)138 static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
139 					struct wpabuf *mbo, u8 start, u8 end)
140 {
141 	size_t size = end - start + 3;
142 
143 	if (size + 2 > wpabuf_tailroom(mbo))
144 		return;
145 
146 	wpas_mbo_non_pref_chan_attr_hdr(mbo, size);
147 	wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
148 }
149 
150 
wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf * mbo,u8 len)151 static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
152 {
153 	wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
154 	wpabuf_put_u8(mbo, len); /* Length */
155 	wpabuf_put_be24(mbo, OUI_WFA);
156 	wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
157 }
158 
159 
wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,u8 start,u8 end)160 static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
161 					      struct wpabuf *mbo, u8 start,
162 					      u8 end)
163 {
164 	size_t size = end - start + 7;
165 
166 	if (size + 2 > wpabuf_tailroom(mbo))
167 		return;
168 
169 	wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
170 	wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
171 }
172 
173 
wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant * wpa_s,struct wpabuf * mbo,int subelement)174 static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
175 					 struct wpabuf *mbo, int subelement)
176 {
177 	u8 i, start = 0;
178 	struct wpa_mbo_non_pref_channel *start_pref;
179 
180 	if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
181 		if (subelement)
182 			wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
183 		else
184 			wpas_mbo_non_pref_chan_attr_hdr(mbo, 0);
185 		return;
186 	}
187 	start_pref = &wpa_s->non_pref_chan[0];
188 
189 	for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
190 		struct wpa_mbo_non_pref_channel *non_pref = NULL;
191 
192 		if (i < wpa_s->non_pref_chan_num)
193 			non_pref = &wpa_s->non_pref_chan[i];
194 		if (!non_pref ||
195 		    non_pref->oper_class != start_pref->oper_class ||
196 		    non_pref->reason != start_pref->reason ||
197 		    non_pref->preference != start_pref->preference) {
198 			if (subelement)
199 				wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
200 								  start, i);
201 			else
202 				wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
203 							    i);
204 
205 			if (!non_pref)
206 				return;
207 
208 			start = i;
209 			start_pref = non_pref;
210 		}
211 	}
212 }
213 
214 
wpas_mbo_ie(struct wpa_supplicant * wpa_s,u8 * buf,size_t len,int add_oce_capa)215 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len,
216 		int add_oce_capa)
217 {
218 	struct wpabuf *mbo;
219 	int res;
220 
221 	if (len < MBO_IE_HEADER + 3 + 7 +
222 	    ((wpa_s->enable_oce & OCE_STA) ? 3 : 0))
223 		return 0;
224 
225 	/* Leave room for the MBO IE header */
226 	mbo = wpabuf_alloc(len - MBO_IE_HEADER);
227 	if (!mbo)
228 		return 0;
229 
230 	/* Add non-preferred channels attribute */
231 	wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
232 
233 	/*
234 	 * Send cellular capabilities attribute even if AP does not advertise
235 	 * cellular capabilities.
236 	 */
237 	wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
238 	wpabuf_put_u8(mbo, 1);
239 	wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
240 
241 	/* Add OCE capability indication attribute if OCE is enabled */
242 	if ((wpa_s->enable_oce & OCE_STA) && add_oce_capa) {
243 		wpabuf_put_u8(mbo, OCE_ATTR_ID_CAPA_IND);
244 		wpabuf_put_u8(mbo, 1);
245 		wpabuf_put_u8(mbo, OCE_RELEASE);
246 	}
247 
248 	res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
249 	if (!res)
250 		wpa_printf(MSG_ERROR, "Failed to add MBO/OCE IE");
251 
252 	wpabuf_free(mbo);
253 	return res;
254 }
255 
256 
257 #ifdef CONFIG_WNM
wpas_mbo_send_wnm_notification(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)258 static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
259 					   const u8 *data, size_t len)
260 {
261 	struct wpabuf *buf;
262 	int res;
263 
264 	/*
265 	 * Send WNM-Notification Request frame only in case of a change in
266 	 * non-preferred channels list during association, if the AP supports
267 	 * MBO.
268 	 */
269 	if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
270 	    !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
271 		return;
272 
273 	buf = wpabuf_alloc(4 + len);
274 	if (!buf)
275 		return;
276 
277 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
278 	wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
279 	wpa_s->mbo_wnm_token++;
280 	if (wpa_s->mbo_wnm_token == 0)
281 		wpa_s->mbo_wnm_token++;
282 	wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
283 	wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
284 
285 	wpabuf_put_data(buf, data, len);
286 
287 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
288 				  wpa_s->own_addr, wpa_s->bssid,
289 				  wpabuf_head(buf), wpabuf_len(buf), 0);
290 	if (res < 0)
291 		wpa_printf(MSG_DEBUG,
292 			   "Failed to send WNM-Notification Request frame with non-preferred channel list");
293 
294 	wpabuf_free(buf);
295 }
296 #endif /* CONFIG_WNM */
297 
298 
wpas_mbo_non_pref_chan_changed(struct wpa_supplicant * wpa_s)299 static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
300 {
301 	struct wpabuf *buf;
302 
303 	buf = wpabuf_alloc(512);
304 	if (!buf)
305 		return;
306 
307 	wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
308 #ifdef CONFIG_WNM
309 	wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
310 				       wpabuf_len(buf));
311 #endif /* CONFIG_WNM */
312 	wpas_update_mbo_connect_params(wpa_s);
313 	wpabuf_free(buf);
314 }
315 
316 
wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel * a,struct wpa_mbo_non_pref_channel * b)317 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
318 				   struct wpa_mbo_non_pref_channel *b)
319 {
320 	return a->oper_class == b->oper_class && a->chan == b->chan;
321 }
322 
323 
324 /*
325  * wpa_non_pref_chan_cmp - Compare two channels for sorting
326  *
327  * In MBO IE non-preferred channel subelement we can put many channels in an
328  * attribute if they are in the same operating class and have the same
329  * preference and reason. To make it easy for the functions that build
330  * the IE attributes and WNM Request subelements, save the channels sorted
331  * by their oper_class and reason.
332  */
wpa_non_pref_chan_cmp(const void * _a,const void * _b)333 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
334 {
335 	const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
336 
337 	if (a->oper_class != b->oper_class)
338 		return (int) a->oper_class - (int) b->oper_class;
339 	if (a->reason != b->reason)
340 		return (int) a->reason - (int) b->reason;
341 	return (int) a->preference - (int) b->preference;
342 }
343 
344 
wpas_mbo_update_non_pref_chan(struct wpa_supplicant * wpa_s,const char * non_pref_chan)345 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
346 				  const char *non_pref_chan)
347 {
348 	char *cmd, *token, *context = NULL;
349 	struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
350 	size_t num = 0, size = 0;
351 	unsigned i;
352 
353 	wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
354 		   non_pref_chan ? non_pref_chan : "N/A");
355 
356 	/*
357 	 * The shortest channel configuration is 7 characters - 3 colons and
358 	 * 4 values.
359 	 */
360 	if (!non_pref_chan || os_strlen(non_pref_chan) < 7)
361 		goto update;
362 
363 	cmd = os_strdup(non_pref_chan);
364 	if (!cmd)
365 		return -1;
366 
367 	while ((token = str_token(cmd, " ", &context))) {
368 		struct wpa_mbo_non_pref_channel *chan;
369 		int ret;
370 		unsigned int _oper_class;
371 		unsigned int _chan;
372 		unsigned int _preference;
373 		unsigned int _reason;
374 
375 		if (num == size) {
376 			size = size ? size * 2 : 1;
377 			tmp_chans = os_realloc_array(chans, size,
378 						     sizeof(*chans));
379 			if (!tmp_chans) {
380 				wpa_printf(MSG_ERROR,
381 					   "Couldn't reallocate non_pref_chan");
382 				goto fail;
383 			}
384 			chans = tmp_chans;
385 		}
386 
387 		chan = &chans[num];
388 
389 		ret = sscanf(token, "%u:%u:%u:%u", &_oper_class,
390 			     &_chan, &_preference, &_reason);
391 		if (ret != 4 ||
392 		    _oper_class > 255 || _chan > 255 ||
393 		    _preference > 255 || _reason > 65535 ) {
394 			wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
395 				   token);
396 			goto fail;
397 		}
398 		chan->oper_class = _oper_class;
399 		chan->chan = _chan;
400 		chan->preference = _preference;
401 		chan->reason = _reason;
402 
403 		if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
404 						    chan->chan, chan->reason)) {
405 			wpa_printf(MSG_ERROR,
406 				   "Invalid non_pref_chan: oper class %d chan %d reason %d",
407 				   chan->oper_class, chan->chan, chan->reason);
408 			goto fail;
409 		}
410 
411 		for (i = 0; i < num; i++)
412 			if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
413 				break;
414 		if (i != num) {
415 			wpa_printf(MSG_ERROR,
416 				   "oper class %d chan %d is duplicated",
417 				   chan->oper_class, chan->chan);
418 			goto fail;
419 		}
420 
421 		num++;
422 	}
423 
424 	os_free(cmd);
425 
426 	if (chans) {
427 		qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
428 		      wpa_non_pref_chan_cmp);
429 	}
430 
431 update:
432 	os_free(wpa_s->non_pref_chan);
433 	wpa_s->non_pref_chan = chans;
434 	wpa_s->non_pref_chan_num = num;
435 	wpas_mbo_non_pref_chan_changed(wpa_s);
436 
437 	return 0;
438 
439 fail:
440 	os_free(chans);
441 	os_free(cmd);
442 	return -1;
443 }
444 
445 
wpas_mbo_scan_ie(struct wpa_supplicant * wpa_s,struct wpabuf * ie)446 void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
447 {
448 	u8 *len;
449 
450 	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
451 	len = wpabuf_put(ie, 1);
452 
453 	wpabuf_put_be24(ie, OUI_WFA);
454 	wpabuf_put_u8(ie, MBO_OUI_TYPE);
455 
456 	wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
457 	wpabuf_put_u8(ie, 1);
458 	wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
459 	if (wpa_s->enable_oce & OCE_STA) {
460 		wpabuf_put_u8(ie, OCE_ATTR_ID_CAPA_IND);
461 		wpabuf_put_u8(ie, 1);
462 		wpabuf_put_u8(ie, OCE_RELEASE);
463 	}
464 	*len = (u8 *) wpabuf_put(ie, 0) - len - 1;
465 }
466 
467 
wpas_mbo_ie_trans_req(struct wpa_supplicant * wpa_s,const u8 * mbo_ie,size_t len)468 void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
469 			   size_t len)
470 {
471 	const u8 *pos, *cell_pref = NULL;
472 	u8 id, elen;
473 	u16 disallowed_sec = 0;
474 
475 	if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
476 	    mbo_ie[3] != MBO_OUI_TYPE)
477 		return;
478 
479 	pos = mbo_ie + 4;
480 	len -= 4;
481 
482 	while (len >= 2) {
483 		id = *pos++;
484 		elen = *pos++;
485 		len -= 2;
486 
487 		if (elen > len)
488 			goto fail;
489 
490 		switch (id) {
491 		case MBO_ATTR_ID_CELL_DATA_PREF:
492 			if (elen != 1)
493 				goto fail;
494 
495 			if (wpa_s->conf->mbo_cell_capa ==
496 			    MBO_CELL_CAPA_AVAILABLE)
497 				cell_pref = pos;
498 			else
499 				wpa_printf(MSG_DEBUG,
500 					   "MBO: Station does not support Cellular data connection");
501 			break;
502 #ifdef CONFIG_WNM
503 		case MBO_ATTR_ID_TRANSITION_REASON:
504 			if (elen != 1)
505 				goto fail;
506 
507 			wpa_s->wnm_mbo_trans_reason_present = 1;
508 			wpa_s->wnm_mbo_transition_reason = *pos;
509 			break;
510 		case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
511 			if (elen != 2)
512 				goto fail;
513 
514 			if (wpa_s->wnm_mode &
515 			    WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
516 				wpa_printf(MSG_DEBUG,
517 					   "MBO: Unexpected association retry delay, BSS is terminating");
518 				goto fail;
519 			} else if (wpa_s->wnm_mode &
520 				   WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
521 				disallowed_sec = WPA_GET_LE16(pos);
522 				wpa_printf(MSG_DEBUG,
523 					   "MBO: Association retry delay: %u",
524 					   disallowed_sec);
525 			} else {
526 				wpa_printf(MSG_DEBUG,
527 					   "MBO: Association retry delay attribute not in disassoc imminent mode");
528 			}
529 
530 			break;
531 #endif /* CONFIG_WNM */
532 		case MBO_ATTR_ID_AP_CAPA_IND:
533 		case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
534 		case MBO_ATTR_ID_CELL_DATA_CAPA:
535 		case MBO_ATTR_ID_ASSOC_DISALLOW:
536 		case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
537 			wpa_printf(MSG_DEBUG,
538 				   "MBO: Attribute %d should not be included in BTM Request frame",
539 				   id);
540 			break;
541 		default:
542 			wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
543 				   id);
544 			return;
545 		}
546 
547 		pos += elen;
548 		len -= elen;
549 	}
550 
551 	if (cell_pref)
552 		wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
553 			*cell_pref);
554 
555 #ifdef CONFIG_WNM
556 	if (wpa_s->wnm_mbo_trans_reason_present)
557 		wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
558 			wpa_s->wnm_mbo_transition_reason);
559 #endif
560 
561 	if (disallowed_sec && wpa_s->current_bss)
562 		wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
563 				     disallowed_sec, 0);
564 
565 	return;
566 fail:
567 	wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
568 		   id, elen, len);
569 }
570 
571 
wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant * wpa_s,u8 * pos,size_t len,enum mbo_transition_reject_reason reason)572 size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
573 				    size_t len,
574 				    enum mbo_transition_reject_reason reason)
575 {
576 	u8 reject_attr[3];
577 
578 	reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
579 	reject_attr[1] = 1;
580 	reject_attr[2] = reason;
581 
582 	return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
583 }
584 
585 
wpas_mbo_update_cell_capa(struct wpa_supplicant * wpa_s,u8 mbo_cell_capa)586 void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
587 {
588 	u8 cell_capa[7];
589 
590 	if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
591 		wpa_printf(MSG_DEBUG,
592 			   "MBO: Cellular capability already set to %u",
593 			   mbo_cell_capa);
594 		return;
595 	}
596 
597 	wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
598 
599 	cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
600 	cell_capa[1] = 5; /* Length */
601 	WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
602 	cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
603 	cell_capa[6] = mbo_cell_capa;
604 
605 #ifdef CONFIG_WNM
606 	wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
607 #endif /* CONFIG_WNM */
608 	wpa_supplicant_set_default_scan_ies(wpa_s);
609 	wpas_update_mbo_connect_params(wpa_s);
610 }
611 
612 #ifdef CONFIG_GAS
mbo_build_anqp_buf(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,u32 mbo_subtypes)613 struct wpabuf * mbo_build_anqp_buf(struct wpa_supplicant *wpa_s,
614 				   struct wpa_bss *bss, u32 mbo_subtypes)
615 {
616 	struct wpabuf *anqp_buf;
617 	u8 *len_pos;
618 	u8 i;
619 
620 	if (!wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
621 		wpa_printf(MSG_INFO, "MBO: " MACSTR
622 			   " does not support MBO - cannot request MBO ANQP elements from it",
623 			   MAC2STR(bss->bssid));
624 		return NULL;
625 	}
626 
627 	/* Allocate size for the maximum case - all MBO subtypes are set */
628 	anqp_buf = wpabuf_alloc(9 + MAX_MBO_ANQP_SUBTYPE);
629 	if (!anqp_buf)
630 		return NULL;
631 
632 	len_pos = gas_anqp_add_element(anqp_buf, ANQP_VENDOR_SPECIFIC);
633 	wpabuf_put_be24(anqp_buf, OUI_WFA);
634 	wpabuf_put_u8(anqp_buf, MBO_ANQP_OUI_TYPE);
635 
636 	wpabuf_put_u8(anqp_buf, MBO_ANQP_SUBTYPE_QUERY_LIST);
637 
638 	/* The first valid MBO subtype is 1 */
639 	for (i = 1; i <= MAX_MBO_ANQP_SUBTYPE; i++) {
640 		if (mbo_subtypes & BIT(i))
641 			wpabuf_put_u8(anqp_buf, i);
642 	}
643 
644 	gas_anqp_set_element_len(anqp_buf, len_pos);
645 
646 	return anqp_buf;
647 }
648 #endif /* CONFIG_GAS */
649 
mbo_parse_rx_anqp_resp(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 * sa,const u8 * data,size_t slen)650 void mbo_parse_rx_anqp_resp(struct wpa_supplicant *wpa_s,
651 			    struct wpa_bss *bss, const u8 *sa,
652 			    const u8 *data, size_t slen)
653 {
654 	const u8 *pos = data;
655 	u8 subtype;
656 
657 	if (slen < 1)
658 		return;
659 
660 	subtype = *pos++;
661 	slen--;
662 
663 	switch (subtype) {
664 	case MBO_ANQP_SUBTYPE_CELL_CONN_PREF:
665 		if (slen < 1)
666 			break;
667 		wpa_msg(wpa_s, MSG_INFO, RX_MBO_ANQP MACSTR
668 			" cell_conn_pref=%u", MAC2STR(sa), *pos);
669 		break;
670 	default:
671 		wpa_printf(MSG_DEBUG, "MBO: Unsupported ANQP subtype %u",
672 			   subtype);
673 		break;
674 	}
675 }
676