MBO: Implement MBO non-preferred channel report in Association Request
[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
22 /* type + length + oui + oui type */
23 #define MBO_IE_HEADER 6
24
25
26 static 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
39 static 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
56 static 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
71 static void wpas_mbo_non_pref_chan_attrs(struct wpa_supplicant *wpa_s,
72                                          struct wpabuf *mbo)
73
74 {
75         u8 i, start = 0;
76         struct wpa_mbo_non_pref_channel *start_pref;
77
78         if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num)
79                 return;
80         start_pref = &wpa_s->non_pref_chan[0];
81
82         for (i = 1; i <= wpa_s->non_pref_chan_num; i++) {
83                 struct wpa_mbo_non_pref_channel *non_pref = NULL;
84
85                 if (i < wpa_s->non_pref_chan_num)
86                         non_pref = &wpa_s->non_pref_chan[i];
87                 if (!non_pref ||
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);
93
94                         if (!non_pref)
95                                 return;
96
97                         start = i;
98                         start_pref = non_pref;
99                 }
100         }
101 }
102
103
104 int wpas_mbo_ie(struct wpa_supplicant *wpa_s, u8 *buf, size_t len)
105 {
106         struct wpabuf *mbo;
107         int res;
108
109         if (!wpa_s->non_pref_chan || !wpa_s->non_pref_chan_num ||
110             len < MBO_IE_HEADER + 7)
111                 return 0;
112
113         /* Leave room for the MBO IE header */
114         mbo = wpabuf_alloc(len - MBO_IE_HEADER);
115         if (!mbo)
116                 return 0;
117
118         /* Add non-preferred channels attribute */
119         wpas_mbo_non_pref_chan_attrs(wpa_s, mbo);
120
121         res = mbo_add_ie(buf, len, wpabuf_head_u8(mbo), wpabuf_len(mbo));
122         if (!res)
123                 wpa_printf(MSG_ERROR, "Failed to add MBO IE");
124
125         wpabuf_free(mbo);
126         return res;
127 }
128
129
130 static int wpa_non_pref_chan_is_eq(struct wpa_mbo_non_pref_channel *a,
131                                    struct wpa_mbo_non_pref_channel *b)
132 {
133         return a->oper_class == b->oper_class && a->chan == b->chan;
134 }
135
136
137 /*
138  * wpa_non_pref_chan_cmp - Compare two channels for sorting
139  *
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.
145  */
146 static int wpa_non_pref_chan_cmp(const void *_a, const void *_b)
147 {
148         const struct wpa_mbo_non_pref_channel *a = _a, *b = _b;
149
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;
157 }
158
159
160 int wpas_mbo_update_non_pref_chan(struct wpa_supplicant *wpa_s,
161                                   const char *non_pref_chan)
162 {
163         char *cmd, *token, *context = NULL;
164         struct wpa_mbo_non_pref_channel *chans = NULL, *tmp_chans;
165         size_t num = 0, size = 0;
166         unsigned i;
167
168         wpa_printf(MSG_DEBUG, "MBO: Update non-preferred channels, non_pref_chan=%s",
169                    non_pref_chan ? non_pref_chan : "N/A");
170
171         /*
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
174          * more.
175          */
176         if (!non_pref_chan || os_strlen(non_pref_chan) < 10)
177                 goto update;
178
179         cmd = os_strdup(non_pref_chan);
180         if (!cmd)
181                 return -1;
182
183         while ((token = str_token(cmd, " ", &context))) {
184                 struct wpa_mbo_non_pref_channel *chan;
185                 int ret;
186                 unsigned int _oper_class;
187                 unsigned int _chan;
188                 unsigned int _preference;
189                 unsigned int _reason;
190                 unsigned int _reason_detail;
191
192                 if (num == size) {
193                         size = size ? size * 2 : 1;
194                         tmp_chans = os_realloc_array(chans, size,
195                                                      sizeof(*chans));
196                         if (!tmp_chans) {
197                                 wpa_printf(MSG_ERROR,
198                                            "Couldn't reallocate non_pref_chan");
199                                 goto fail;
200                         }
201                         chans = tmp_chans;
202                 }
203
204                 chan = &chans[num];
205
206                 ret = sscanf(token, "%u:%u:%u:%u:%u", &_oper_class,
207                              &_chan, &_preference, &_reason,
208                              &_reason_detail);
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",
214                                    token);
215                         goto fail;
216                 }
217                 chan->oper_class = _oper_class;
218                 chan->chan = _chan;
219                 chan->preference = _preference;
220                 chan->reason = _reason;
221                 chan->reason_detail = ret == 4 ? 0 : _reason_detail;
222
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);
228                         goto fail;
229                 }
230
231                 for (i = 0; i < num; i++)
232                         if (wpa_non_pref_chan_is_eq(chan, &chans[i]))
233                                 break;
234                 if (i != num) {
235                         wpa_printf(MSG_ERROR,
236                                    "oper class %d chan %d is duplicated",
237                                    chan->oper_class, chan->chan);
238                         goto fail;
239                 }
240
241                 num++;
242         }
243
244         os_free(cmd);
245
246         if (chans) {
247                 qsort(chans, num, sizeof(struct wpa_mbo_non_pref_channel),
248                       wpa_non_pref_chan_cmp);
249         }
250
251 update:
252         os_free(wpa_s->non_pref_chan);
253         wpa_s->non_pref_chan = chans;
254         wpa_s->non_pref_chan_num = num;
255
256         return 0;
257
258 fail:
259         os_free(chans);
260         os_free(cmd);
261         return -1;
262 }