Link to, and adjust types for, the PCSC framework included with OSX
[mech_eap.git] / wpa_supplicant / mbo.c
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 #include "scan.h"
22
23 /* type + length + oui + oui type */
24 #define MBO_IE_HEADER 6
25
26
27 static int wpas_mbo_validate_non_pref_chan(u8 oper_class, u8 chan, u8 reason)
28 {
29         if (reason > MBO_NON_PREF_CHAN_REASON_INT_INTERFERENCE)
30                 return -1;
31
32         /* Only checking the validity of the channel and oper_class */
33         if (ieee80211_chan_to_freq(NULL, oper_class, chan) == -1)
34                 return -1;
35
36         return 0;
37 }
38
39
40 const u8 * wpas_mbo_get_bss_attr(struct wpa_bss *bss, enum mbo_attr_id attr)
41 {
42         const u8 *mbo, *end;
43
44         if (!bss)
45                 return NULL;
46
47         mbo = wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE);
48         if (!mbo)
49                 return NULL;
50
51         end = mbo + 2 + mbo[1];
52         mbo += MBO_IE_HEADER;
53
54         return get_ie(mbo, end - mbo, attr);
55 }
56
57
58 static void wpas_mbo_non_pref_chan_attr_body(struct wpa_supplicant *wpa_s,
59                                              struct wpabuf *mbo,
60                                              u8 start, u8 end)
61 {
62         u8 i;
63
64         wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].oper_class);
65
66         for (i = start; i < end; i++)
67                 wpabuf_put_u8(mbo, wpa_s->non_pref_chan[i].chan);
68
69         wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].preference);
70         wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason);
71         wpabuf_put_u8(mbo, wpa_s->non_pref_chan[start].reason_detail);
72 }
73
74
75 static void wpas_mbo_non_pref_chan_attr(struct wpa_supplicant *wpa_s,
76                                         struct wpabuf *mbo, u8 start, u8 end)
77 {
78         size_t size = end - start + 4;
79
80         if (size + 2 > wpabuf_tailroom(mbo))
81                 return;
82
83         wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
84         wpabuf_put_u8(mbo, size); /* Length */
85
86         wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
87 }
88
89
90 static void wpas_mbo_non_pref_chan_subelem_hdr(struct wpabuf *mbo, u8 len)
91 {
92         wpabuf_put_u8(mbo, WLAN_EID_VENDOR_SPECIFIC);
93         wpabuf_put_u8(mbo, len); /* Length */
94         wpabuf_put_be24(mbo, OUI_WFA);
95         wpabuf_put_u8(mbo, MBO_ATTR_ID_NON_PREF_CHAN_REPORT);
96 }
97
98
99 static void wpas_mbo_non_pref_chan_subelement(struct wpa_supplicant *wpa_s,
100                                               struct wpabuf *mbo, u8 start,
101                                               u8 end)
102 {
103         size_t size = end - start + 8;
104
105         if (size + 2 > wpabuf_tailroom(mbo))
106                 return;
107
108         wpas_mbo_non_pref_chan_subelem_hdr(mbo, size);
109         wpas_mbo_non_pref_chan_attr_body(wpa_s, mbo, start, end);
110 }
111
112
113 static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
114                                          struct wpabuf *mbo, int subelement)
115 {
116         u8 i, start = 0;
117         struct wpa_mbo_non_pref_channel *start_pref;
118
119         if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num) {
120                 if (subelement)
121                         wpas_mbo_non_pref_chan_subelem_hdr(mbo, 4);
122                 return;
123         }
124         start_pref = &wpa_s->non_pref_chan[0];
125
126         for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
127                 struct wpa_mbo_non_pref_channel *non_pref = NULL;
128
129                 if (i < wpa_s->non_pref_chan_num)
130                         non_pref = &wpa_s->non_pref_chan[i];
131                 if (!non_pref ||
132                     non_pref->oper_class != start_pref->oper_class ||
133                     non_pref->reason != start_pref->reason ||
134                     non_pref->reason_detail != start_pref->reason_detail ||
135                     non_pref->preference != start_pref->preference) {
136                         if (subelement)
137                                 wpas_mbo_non_pref_chan_subelement(wpa_s, mbo,
138                                                                   start, i);
139                         else
140                                 wpas_mbo_non_pref_chan_attr(wpa_s, mbo, start,
141                                                             i);
142
143                         if (!non_pref)
144                                 return;
145
146                         start = i;
147                         start_pref = non_pref;
148                 }
149         }
150 }
151
152
153 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
154 {
155         struct wpabuf *mbo;
156         int res;
157
158         if (len < MBO_IE_HEADER + 3 + 7)
159                 return 0;
160
161         /* Leave room for the MBO IE header */
162         mbo = wpabuf_alloc(len - MBO_IE_HEADER);
163         if (!mbo)
164                 return 0;
165
166         /* Add non-preferred channels attribute */
167         wpas_mbo_non_pref_chan_attrs(wpa_s, mbo, 0);
168
169         /*
170          * Send cellular capabilities attribute even if AP does not advertise
171          * cellular capabilities.
172          */
173         wpabuf_put_u8(mbo, MBO_ATTR_ID_CELL_DATA_CAPA);
174         wpabuf_put_u8(mbo, 1);
175         wpabuf_put_u8(mbo, wpa_s->conf->mbo_cell_capa);
176
177         res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
178         if (!res)
179                 wpa_printf(MSG_ERROR, "Failed to add MBO IE");
180
181         wpabuf_free(mbo);
182         return res;
183 }
184
185
186 static void wpas_mbo_send_wnm_notification(struct wpa_supplicant *wpa_s,
187                                            const u8 *data, size_t len)
188 {
189         struct wpabuf *buf;
190         int res;
191
192         /*
193          * Send WNM-Notification Request frame only in case of a change in
194          * non-preferred channels list during association, if the AP supports
195          * MBO.
196          */
197         if (wpa_s->wpa_state != WPA_COMPLETED || !wpa_s->current_bss ||
198             !wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE))
199                 return;
200
201         buf = wpabuf_alloc(4 + len);
202         if (!buf)
203                 return;
204
205         wpabuf_put_u8(buf, WLAN_ACTION_WNM);
206         wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
207         wpa_s->mbo_wnm_token++;
208         if (wpa_s->mbo_wnm_token == 0)
209                 wpa_s->mbo_wnm_token++;
210         wpabuf_put_u8(buf, wpa_s->mbo_wnm_token);
211         wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC); /* Type */
212
213         wpabuf_put_data(buf, data, len);
214
215         res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
216                                   wpa_s->own_addr, wpa_s->bssid,
217                                   wpabuf_head(buf), wpabuf_len(buf), 0);
218         if (res < 0)
219                 wpa_printf(MSG_DEBUG,
220                            "Failed to send WNM-Notification Request frame with non-preferred channel list");
221
222         wpabuf_free(buf);
223 }
224
225
226 static void wpas_mbo_non_pref_chan_changed(struct wpa_supplicant *wpa_s)
227 {
228         struct wpabuf *buf;
229
230         buf = wpabuf_alloc(512);
231         if (!buf)
232                 return;
233
234         wpas_mbo_non_pref_chan_attrs(wpa_s, buf, 1);
235         wpas_mbo_send_wnm_notification(wpa_s, wpabuf_head_u8(buf),
236                                        wpabuf_len(buf));
237         wpabuf_free(buf);
238 }
239
240
241 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
242                                    struct wpa_mbo_non_pref_channel *b)
243 {
244         return a->oper_class == b->oper_class && a->chan == b->chan;
245 }
246
247
248 /*
249  * wpa_non_pref_chan_cmp - Compare two channels for sorting
250  *
251  * In MBO IE non-preferred channel subelement we can put many channels in an
252  * attribute if they are in the same operating class and have the same
253  * preference, reason, and reason detail. To make it easy for the functions that
254  * build the IE attributes and WNM Request subelements, save the channels sorted
255  * by their oper_class, reason, and reason_detail.
256  */
257 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
258 {
259         const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
260
261         if (a->oper_class != b->oper_class)
262                 return a->oper_class - b->oper_class;
263         if (a->reason != b->reason)
264                 return a->reason - b->reason;
265         if (a->reason_detail != b->reason_detail)
266                 return a->reason_detail - b->reason_detail;
267         return a->preference - b->preference;
268 }
269
270
271 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
272                                   const char *non_pref_chan)
273 {
274         char *cmd, *token, *context = NULL;
275         struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
276         size_t num = 0, size = 0;
277         unsigned i;
278
279         wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
280                    non_pref_chan ? non_pref_chan : "N/A");
281
282         /*
283          * The shortest channel configuration is 10 characters - commas, 3
284          * colons, and 4 values that one of them (oper_class) is 2 digits or
285          * more.
286          */
287         if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
288                 goto update;
289
290         cmd = os_strdup(non_pref_chan);
291         if (!cmd)
292                 return -1;
293
294         while ((token = str_token(cmd, " ", &context))) {
295                 struct wpa_mbo_non_pref_channel *chan;
296                 int ret;
297                 unsigned int _oper_class;
298                 unsigned int _chan;
299                 unsigned int _preference;
300                 unsigned int _reason;
301                 unsigned int _reason_detail;
302
303                 if (num == size) {
304                         size = size ? size * 2 : 1;
305                         tmp_chans = os_realloc_array(chans, size,
306                                                      sizeof(*chans));
307                         if (!tmp_chans) {
308                                 wpa_printf(MSG_ERROR,
309                                            "Couldn't reallocate non_pref_chan");
310                                 goto fail;
311                         }
312                         chans = tmp_chans;
313                 }
314
315                 chan = &chans[num];
316
317                 ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
318                              &_chan, &_preference, &_reason,
319                              &_reason_detail);
320                 if ((ret != 4 && ret != 5) ||
321                     _oper_class > 255 || _chan > 255 ||
322                     _preference > 255 || _reason > 65535 ||
323                     (ret == 5 && _reason_detail > 255)) {
324                         wpa_printf(MSG_ERROR, "Invalid non-pref chan input %s",
325                                    token);
326                         goto fail;
327                 }
328                 chan->oper_class = _oper_class;
329                 chan->chan = _chan;
330                 chan->preference = _preference;
331                 chan->reason = _reason;
332                 chan->reason_detail = ret == 4 ? 0 : _reason_detail;
333
334                 if (wpas_mbo_validate_non_pref_chan(chan->oper_class,
335                                                     chan->chan, chan->reason)) {
336                         wpa_printf(MSG_ERROR,
337                                    "Invalid non_pref_chan: oper class %d chan %d reason %d",
338                                    chan->oper_class, chan->chan, chan->reason);
339                         goto fail;
340                 }
341
342                 for (i = 0; i < num; i++)
343                         if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
344                                 break;
345                 if (i != num) {
346                         wpa_printf(MSG_ERROR,
347                                    "oper class %d chan %d is duplicated",
348                                    chan->oper_class, chan->chan);
349                         goto fail;
350                 }
351
352                 num++;
353         }
354
355         os_free(cmd);
356
357         if (chans) {
358                 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
359                       wpa_non_pref_chan_cmp);
360         }
361
362 update:
363         os_free(wpa_s->non_pref_chan);
364         wpa_s->non_pref_chan = chans;
365         wpa_s->non_pref_chan_num = num;
366         wpas_mbo_non_pref_chan_changed(wpa_s);
367
368         return 0;
369
370 fail:
371         os_free(chans);
372         os_free(cmd);
373         return -1;
374 }
375
376
377 void wpas_mbo_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ie)
378 {
379         wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
380         wpabuf_put_u8(ie, 7);
381         wpabuf_put_be24(ie, OUI_WFA);
382         wpabuf_put_u8(ie, MBO_OUI_TYPE);
383
384         wpabuf_put_u8(ie, MBO_ATTR_ID_CELL_DATA_CAPA);
385         wpabuf_put_u8(ie, 1);
386         wpabuf_put_u8(ie, wpa_s->conf->mbo_cell_capa);
387 }
388
389
390 enum chan_allowed {
391         NOT_ALLOWED, ALLOWED
392 };
393
394 static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
395                                        unsigned int *flags)
396 {
397         int i;
398
399         for (i = 0; i < mode->num_channels; i++) {
400                 if (mode->channels[i].chan == chan)
401                         break;
402         }
403
404         if (i == mode->num_channels ||
405             (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
406                 return NOT_ALLOWED;
407
408         if (flags)
409                 *flags = mode->channels[i].flag;
410
411         return ALLOWED;
412 }
413
414
415 static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
416 {
417         u8 center_channels[] = {42, 58, 106, 122, 138, 155};
418         size_t i;
419
420         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
421                 return 0;
422
423         for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
424                 /*
425                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
426                  * so the center channel is 6 channels away from the start/end.
427                  */
428                 if (channel >= center_channels[i] - 6 &&
429                     channel <= center_channels[i] + 6)
430                         return center_channels[i];
431         }
432
433         return 0;
434 }
435
436
437 static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
438 {
439         u8 center_chan;
440         unsigned int i;
441
442         center_chan = get_center_80mhz(mode, channel);
443         if (!center_chan)
444                 return NOT_ALLOWED;
445
446         /* check all the channels are available */
447         for (i = 0; i < 4; i++) {
448                 unsigned int flags;
449                 u8 adj_chan = center_chan - 6 + i * 4;
450
451                 if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
452                         return NOT_ALLOWED;
453
454                 if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
455                     (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
456                     (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
457                     (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
458                         return NOT_ALLOWED;
459         }
460
461         return ALLOWED;
462 }
463
464
465 static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
466 {
467         u8 center_channels[] = { 50, 114 };
468         unsigned int i;
469
470         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
471                 return 0;
472
473         for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
474                 /*
475                  * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
476                  * so the center channel is 14 channels away from the start/end.
477                  */
478                 if (channel >= center_channels[i] - 14 &&
479                     channel <= center_channels[i] + 14)
480                         return center_channels[i];
481         }
482
483         return 0;
484 }
485
486
487 static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
488                                        u8 channel)
489 {
490         u8 center_chan;
491         unsigned int i;
492
493         center_chan = get_center_160mhz(mode, channel);
494         if (!center_chan)
495                 return NOT_ALLOWED;
496
497         /* Check all the channels are available */
498         for (i = 0; i < 8; i++) {
499                 unsigned int flags;
500                 u8 adj_chan = center_chan - 14 + i * 4;
501
502                 if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
503                         return NOT_ALLOWED;
504
505                 if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
506                     (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
507                     (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
508                     (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
509                     (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
510                     (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
511                     (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
512                     (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
513                         return NOT_ALLOWED;
514         }
515
516         return ALLOWED;
517 }
518
519
520 static enum chan_allowed verify_channel(struct hostapd_hw_modes *mode,
521                                         u8 channel, u8 bw)
522 {
523         unsigned int flag = 0;
524         enum chan_allowed res, res2;
525
526         res2 = res = allow_channel(mode, channel, &flag);
527         if (bw == BW40MINUS) {
528                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
529                         return NOT_ALLOWED;
530                 res2 = allow_channel(mode, channel - 4, NULL);
531         } else if (bw == BW40PLUS) {
532                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
533                         return NOT_ALLOWED;
534                 res2 = allow_channel(mode, channel + 4, NULL);
535         } else if (bw == BW80) {
536                 /*
537                  * channel is a center channel and as such, not necessarily a
538                  * valid 20 MHz channels. Override earlier allow_channel()
539                  * result and use only the 80 MHz specific version.
540                  */
541                 res2 = res = verify_80mhz(mode, channel);
542         } else if (bw == BW160) {
543                 /*
544                  * channel is a center channel and as such, not necessarily a
545                  * valid 20 MHz channels. Override earlier allow_channel()
546                  * result and use only the 160 MHz specific version.
547                  */
548                 res2 = res = verify_160mhz(mode, channel);
549         } else if (bw == BW80P80) {
550                 /*
551                  * channel is a center channel and as such, not necessarily a
552                  * valid 20 MHz channels. Override earlier allow_channel()
553                  * result and use only the 80 MHz specific version.
554                  */
555                 res2 = res = verify_80mhz(mode, channel);
556         }
557
558         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
559                 return NOT_ALLOWED;
560
561         return ALLOWED;
562 }
563
564
565 static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
566                                    const struct oper_class_map *op_class)
567 {
568         int chan;
569         size_t i;
570         struct hostapd_hw_modes *mode;
571         int found;
572
573         mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
574         if (!mode)
575                 return 0;
576
577         if (op_class->op_class == 128) {
578                 u8 channels[] = { 42, 58, 106, 122, 138, 155 };
579
580                 for (i = 0; i < ARRAY_SIZE(channels); i++) {
581                         if (verify_channel(mode, channels[i], op_class->bw) ==
582                             ALLOWED)
583                                 return 1;
584                 }
585
586                 return 0;
587         }
588
589         if (op_class->op_class == 129) {
590                 /* Check if either 160 MHz channels is allowed */
591                 return verify_channel(mode, 50, op_class->bw) == ALLOWED ||
592                         verify_channel(mode, 114, op_class->bw) == ALLOWED;
593         }
594
595         if (op_class->op_class == 130) {
596                 /* Need at least two non-contiguous 80 MHz segments */
597                 found = 0;
598
599                 if (verify_channel(mode, 42, op_class->bw) == ALLOWED ||
600                     verify_channel(mode, 58, op_class->bw) == ALLOWED)
601                         found++;
602                 if (verify_channel(mode, 106, op_class->bw) == ALLOWED ||
603                     verify_channel(mode, 122, op_class->bw) == ALLOWED ||
604                     verify_channel(mode, 138, op_class->bw) == ALLOWED)
605                         found++;
606                 if (verify_channel(mode, 106, op_class->bw) == ALLOWED &&
607                     verify_channel(mode, 138, op_class->bw) == ALLOWED)
608                         found++;
609                 if (verify_channel(mode, 155, op_class->bw) == ALLOWED)
610                         found++;
611
612                 if (found >= 2)
613                         return 1;
614
615                 return 0;
616         }
617
618         found = 0;
619         for (chan = op_class->min_chan; chan <= op_class->max_chan;
620              chan += op_class->inc) {
621                 if (verify_channel(mode, chan, op_class->bw) == ALLOWED) {
622                         found = 1;
623                         break;
624                 }
625         }
626
627         return found;
628 }
629
630
631 int wpas_mbo_supp_op_class_ie(struct wpa_supplicant *wpa_s, int freq, u8 *pos,
632                               size_t len)
633 {
634         struct wpabuf *buf;
635         u8 op, current, chan;
636         u8 *ie_len;
637         int res;
638
639         /*
640          * Assume 20 MHz channel for now.
641          * TODO: Use the secondary channel and VHT channel width that will be
642          * used after association.
643          */
644         if (ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
645                                           &current, &chan) == NUM_HOSTAPD_MODES)
646                 return 0;
647
648         /*
649          * Need 3 bytes for EID, length, and current operating class, plus
650          * 1 byte for every other supported operating class.
651          */
652         buf = wpabuf_alloc(global_op_class_size + 3);
653         if (!buf)
654                 return 0;
655
656         wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
657         /* Will set the length later, putting a placeholder */
658         ie_len = wpabuf_put(buf, 1);
659         wpabuf_put_u8(buf, current);
660
661         for (op = 0; global_op_class[op].op_class; op++) {
662                 if (wpas_op_class_supported(wpa_s, &global_op_class[op]))
663                         wpabuf_put_u8(buf, global_op_class[op].op_class);
664         }
665
666         *ie_len = wpabuf_len(buf) - 2;
667         if (*ie_len < 2 || wpabuf_len(buf) > len) {
668                 wpa_printf(MSG_ERROR,
669                            "Failed to add supported operating classes IE");
670                 res = 0;
671         } else {
672                 os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
673                 res = wpabuf_len(buf);
674                 wpa_hexdump_buf(MSG_DEBUG,
675                                 "MBO: Added supported operating classes IE",
676                                 buf);
677         }
678
679         wpabuf_free(buf);
680         return res;
681 }
682
683
684 void wpas_mbo_ie_trans_req(struct wpa_supplicant *wpa_s, const u8 *mbo_ie,
685                            size_t len)
686 {
687         const u8 *pos, *cell_pref = NULL, *reason = NULL;
688         u8 id, elen;
689         u16 disallowed_sec = 0;
690
691         if (len <= 4 || WPA_GET_BE24(mbo_ie) != OUI_WFA ||
692             mbo_ie[3] != MBO_OUI_TYPE)
693                 return;
694
695         pos = mbo_ie + 4;
696         len -= 4;
697
698         while (len >= 2) {
699                 id = *pos++;
700                 elen = *pos++;
701                 len -= 2;
702
703                 if (elen > len)
704                         goto fail;
705
706                 switch (id) {
707                 case MBO_ATTR_ID_CELL_DATA_PREF:
708                         if (elen != 1)
709                                 goto fail;
710
711                         if (wpa_s->conf->mbo_cell_capa ==
712                             MBO_CELL_CAPA_AVAILABLE)
713                                 cell_pref = pos;
714                         else
715                                 wpa_printf(MSG_DEBUG,
716                                            "MBO: Station does not support Cellular data connection");
717                         break;
718                 case MBO_ATTR_ID_TRANSITION_REASON:
719                         if (elen != 1)
720                                 goto fail;
721
722                         reason = pos;
723                         break;
724                 case MBO_ATTR_ID_ASSOC_RETRY_DELAY:
725                         if (elen != 2)
726                                 goto fail;
727
728                         if (wpa_s->wnm_mode &
729                             WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
730                                 wpa_printf(MSG_DEBUG,
731                                            "MBO: Unexpected association retry delay, BSS is terminating");
732                                 goto fail;
733                         } else if (wpa_s->wnm_mode &
734                                    WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
735                                 disallowed_sec = WPA_GET_LE16(pos);
736                         } else {
737                                 wpa_printf(MSG_DEBUG,
738                                            "MBO: Association retry delay attribute not in disassoc imminent mode");
739                         }
740
741                         break;
742                 case MBO_ATTR_ID_AP_CAPA_IND:
743                 case MBO_ATTR_ID_NON_PREF_CHAN_REPORT:
744                 case MBO_ATTR_ID_CELL_DATA_CAPA:
745                 case MBO_ATTR_ID_ASSOC_DISALLOW:
746                 case MBO_ATTR_ID_TRANSITION_REJECT_REASON:
747                         wpa_printf(MSG_DEBUG,
748                                    "MBO: Attribute %d should not be included in BTM Request frame",
749                                    id);
750                         break;
751                 default:
752                         wpa_printf(MSG_DEBUG, "MBO: Unknown attribute id %u",
753                                    id);
754                         return;
755                 }
756
757                 pos += elen;
758                 len -= elen;
759         }
760
761         if (cell_pref)
762                 wpa_msg(wpa_s, MSG_INFO, MBO_CELL_PREFERENCE "preference=%u",
763                         *cell_pref);
764
765         if (reason)
766                 wpa_msg(wpa_s, MSG_INFO, MBO_TRANSITION_REASON "reason=%u",
767                         *reason);
768
769         if (disallowed_sec && wpa_s->current_bss)
770                 wpa_bss_tmp_disallow(wpa_s, wpa_s->current_bss->bssid,
771                                      disallowed_sec);
772
773         return;
774 fail:
775         wpa_printf(MSG_DEBUG, "MBO IE parsing failed (id=%u len=%u left=%zu)",
776                    id, elen, len);
777 }
778
779
780 size_t wpas_mbo_ie_bss_trans_reject(struct wpa_supplicant *wpa_s, u8 *pos,
781                                     size_t len,
782                                     enum mbo_transition_reject_reason reason)
783 {
784         u8 reject_attr[3];
785
786         reject_attr[0] = MBO_ATTR_ID_TRANSITION_REJECT_REASON;
787         reject_attr[1] = 1;
788         reject_attr[2] = reason;
789
790         return mbo_add_ie(pos, len, reject_attr, sizeof(reject_attr));
791 }
792
793
794 void wpas_mbo_update_cell_capa(struct wpa_supplicant *wpa_s, u8 mbo_cell_capa)
795 {
796         u8 cell_capa[7];
797
798         if (wpa_s->conf->mbo_cell_capa == mbo_cell_capa) {
799                 wpa_printf(MSG_DEBUG,
800                            "MBO: Cellular capability already set to %u",
801                            mbo_cell_capa);
802                 return;
803         }
804
805         wpa_s->conf->mbo_cell_capa = mbo_cell_capa;
806
807         cell_capa[0] = WLAN_EID_VENDOR_SPECIFIC;
808         cell_capa[1] = 5; /* Length */
809         WPA_PUT_BE24(cell_capa + 2, OUI_WFA);
810         cell_capa[5] = MBO_ATTR_ID_CELL_DATA_CAPA;
811         cell_capa[6] = mbo_cell_capa;
812
813         wpas_mbo_send_wnm_notification(wpa_s, cell_capa, 7);
814         wpa_supplicant_set_default_scan_ies(wpa_s);
815 }