FST: Fix STA MB IEs creation
[mech_eap.git] / src / fst / fst_group.c
1 /*
2  * FST module - FST group object implementation
3  * Copyright (c) 2014, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "common/defs.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "drivers/driver.h"
15 #include "fst/fst_internal.h"
16 #include "fst/fst_defs.h"
17
18
19 struct dl_list fst_global_groups_list;
20
21
22 static void fst_dump_mb_ies(const char *group_id, const char *ifname,
23                             struct wpabuf *mbies)
24 {
25         const u8 *p = wpabuf_head(mbies);
26         size_t s = wpabuf_len(mbies);
27
28         while (s >= 2) {
29                 const struct multi_band_ie *mbie =
30                         (const struct multi_band_ie *) p;
31                 WPA_ASSERT(mbie->eid == WLAN_EID_MULTI_BAND);
32                 WPA_ASSERT(2 + mbie->len >= sizeof(*mbie));
33
34                 fst_printf(MSG_WARNING,
35                            "%s: %s: mb_ctrl=%u band_id=%u op_class=%u chan=%u bssid="
36                            MACSTR
37                            " beacon_int=%u tsf_offs=[%u %u %u %u %u %u %u %u] mb_cc=0x%02x tmout=%u",
38                            group_id, ifname,
39                            mbie->mb_ctrl, mbie->band_id, mbie->op_class,
40                            mbie->chan, MAC2STR(mbie->bssid), mbie->beacon_int,
41                            mbie->tsf_offs[0], mbie->tsf_offs[1],
42                            mbie->tsf_offs[2], mbie->tsf_offs[3],
43                            mbie->tsf_offs[4], mbie->tsf_offs[5],
44                            mbie->tsf_offs[6], mbie->tsf_offs[7],
45                            mbie->mb_connection_capability,
46                            mbie->fst_session_tmout);
47
48                 p += 2 + mbie->len;
49                 s -= 2 + mbie->len;
50         }
51 }
52
53
54 static void fst_fill_mb_ie(struct wpabuf *buf, const u8 *bssid,
55                            const u8 *own_addr, enum mb_band_id band, u8 channel)
56 {
57         struct multi_band_ie *mbie;
58         size_t len = sizeof(*mbie);
59
60         if (own_addr)
61                 len += ETH_ALEN;
62
63         mbie = wpabuf_put(buf, len);
64
65         os_memset(mbie, 0, len);
66
67         mbie->eid = WLAN_EID_MULTI_BAND;
68         mbie->len = len - 2;
69 #ifdef HOSTAPD
70         mbie->mb_ctrl = MB_STA_ROLE_AP;
71         mbie->mb_connection_capability = MB_CONNECTION_CAPABILITY_AP;
72 #else /* HOSTAPD */
73         mbie->mb_ctrl = MB_STA_ROLE_NON_PCP_NON_AP;
74         mbie->mb_connection_capability = 0;
75 #endif /* HOSTAPD */
76         if (bssid)
77                 os_memcpy(mbie->bssid, bssid, ETH_ALEN);
78         mbie->band_id = band;
79         mbie->op_class = 0;  /* means all */
80         mbie->chan = channel;
81         mbie->fst_session_tmout = FST_DEFAULT_SESSION_TIMEOUT_TU;
82
83         if (own_addr) {
84                 mbie->mb_ctrl |= MB_CTRL_STA_MAC_PRESENT;
85                 os_memcpy(&mbie[1], own_addr, ETH_ALEN);
86         }
87 }
88
89
90 static unsigned fst_fill_iface_mb_ies(struct fst_iface *f, struct wpabuf *buf)
91 {
92         const  u8 *bssid;
93
94         bssid = fst_iface_get_bssid(f);
95         if (bssid) {
96                 enum hostapd_hw_mode hw_mode;
97                 u8 channel;
98
99                 if (buf) {
100                         fst_iface_get_channel_info(f, &hw_mode, &channel);
101                         fst_fill_mb_ie(buf, bssid, fst_iface_get_addr(f),
102                                        fst_hw_mode_to_band(hw_mode), channel);
103                 }
104                 return 1;
105         } else {
106                 unsigned bands[MB_BAND_ID_WIFI_60GHZ + 1] = {};
107                 struct hostapd_hw_modes *modes;
108                 enum mb_band_id b;
109                 int num_modes = fst_iface_get_hw_modes(f, &modes);
110                 int ret = 0;
111
112                 while (num_modes--) {
113                         b = fst_hw_mode_to_band(modes->mode);
114                         modes++;
115                         if (b >= ARRAY_SIZE(bands) || bands[b]++)
116                                 continue;
117                         ret++;
118                         if (buf)
119                                 fst_fill_mb_ie(buf, NULL, fst_iface_get_addr(f),
120                                                b, MB_STA_CHANNEL_ALL);
121                 }
122                 return ret;
123         }
124 }
125
126
127 static struct wpabuf * fst_group_create_mb_ie(struct fst_group *g,
128                                               struct fst_iface *i)
129 {
130         struct wpabuf *buf;
131         struct fst_iface *f;
132         unsigned int nof_mbies = 0;
133         unsigned int nof_ifaces_added = 0;
134
135         foreach_fst_group_iface(g, f) {
136                 if (f == i)
137                         continue;
138                 nof_mbies += fst_fill_iface_mb_ies(f, NULL);
139         }
140
141         buf = wpabuf_alloc(nof_mbies *
142                            (sizeof(struct multi_band_ie) + ETH_ALEN));
143         if (!buf) {
144                 fst_printf_iface(i, MSG_ERROR,
145                                  "cannot allocate mem for %u MB IEs",
146                                  nof_mbies);
147                 return NULL;
148         }
149
150         /* The list is sorted in descending order by priorities, so MB IEs will
151          * be arranged in the same order, as required by spec (see corresponding
152          * comment in.fst_attach().
153          */
154         foreach_fst_group_iface(g, f) {
155                 if (f == i)
156                         continue;
157
158                 fst_fill_iface_mb_ies(f, buf);
159                 ++nof_ifaces_added;
160
161                 fst_printf_iface(i, MSG_DEBUG, "added to MB IE");
162         }
163
164         if (!nof_ifaces_added) {
165                 wpabuf_free(buf);
166                 buf = NULL;
167                 fst_printf_iface(i, MSG_INFO,
168                                  "cannot add MB IE: no backup ifaces");
169         } else {
170                 fst_dump_mb_ies(fst_group_get_id(g), fst_iface_get_name(i),
171                                 buf);
172         }
173
174         return buf;
175 }
176
177
178 static const u8 * fst_mbie_get_peer_addr(const struct multi_band_ie *mbie)
179 {
180         const u8 *peer_addr = NULL;
181
182         switch (MB_CTRL_ROLE(mbie->mb_ctrl)) {
183         case MB_STA_ROLE_AP:
184                 peer_addr = mbie->bssid;
185                 break;
186         case MB_STA_ROLE_NON_PCP_NON_AP:
187                 if (mbie->mb_ctrl & MB_CTRL_STA_MAC_PRESENT &&
188                     (size_t) 2 + mbie->len >= sizeof(*mbie) + ETH_ALEN)
189                         peer_addr = (const u8 *) &mbie[1];
190                 break;
191         default:
192                 break;
193         }
194
195         return peer_addr;
196 }
197
198
199 static struct fst_iface *
200 fst_group_get_new_iface_by_mbie_and_band_id(struct fst_group *g,
201                                             const u8 *mb_ies_buff,
202                                             size_t mb_ies_size,
203                                             u8 band_id,
204                                             u8 *iface_peer_addr)
205 {
206         while (mb_ies_size >= 2) {
207                 const struct multi_band_ie *mbie =
208                         (const struct multi_band_ie *) mb_ies_buff;
209
210                 if (mbie->eid != WLAN_EID_MULTI_BAND ||
211                     (size_t) 2 + mbie->len < sizeof(*mbie))
212                         break;
213
214                 if (mbie->band_id == band_id) {
215                         struct fst_iface *iface;
216
217                         foreach_fst_group_iface(g, iface) {
218                                 const u8 *peer_addr =
219                                         fst_mbie_get_peer_addr(mbie);
220
221                                 if (peer_addr &&
222                                     fst_iface_is_connected(iface, peer_addr) &&
223                                     band_id == fst_iface_get_band_id(iface)) {
224                                         os_memcpy(iface_peer_addr, peer_addr,
225                                                   ETH_ALEN);
226                                         return iface;
227                                 }
228                         }
229                         break;
230                 }
231
232                 mb_ies_buff += 2 + mbie->len;
233                 mb_ies_size -= 2 + mbie->len;
234         }
235
236         return NULL;
237 }
238
239
240 struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g,
241                                                const char *ifname)
242 {
243         struct fst_iface *f;
244
245         foreach_fst_group_iface(g, f) {
246                 const char *in = fst_iface_get_name(f);
247
248                 if (os_strncmp(in, ifname, os_strlen(in)) == 0)
249                         return f;
250         }
251
252         return NULL;
253 }
254
255
256 u8 fst_group_assign_dialog_token(struct fst_group *g)
257 {
258         g->dialog_token++;
259         if (g->dialog_token == 0)
260                 g->dialog_token++;
261         return g->dialog_token;
262 }
263
264
265 u32 fst_group_assign_fsts_id(struct fst_group *g)
266 {
267         g->fsts_id++;
268         return g->fsts_id;
269 }
270
271
272 static Boolean
273 fst_group_does_iface_appear_in_other_mbies(struct fst_group *g,
274                                            struct fst_iface *iface,
275                                            struct fst_iface *other,
276                                            u8 *peer_addr)
277 {
278         struct fst_get_peer_ctx *ctx;
279         const u8 *addr;
280         const u8 *iface_addr;
281         enum mb_band_id  iface_band_id;
282
283         WPA_ASSERT(g == fst_iface_get_group(iface));
284         WPA_ASSERT(g == fst_iface_get_group(other));
285
286         iface_addr = fst_iface_get_addr(iface);
287         iface_band_id = fst_iface_get_band_id(iface);
288
289         addr = fst_iface_get_peer_first(other, &ctx, TRUE);
290         for (; addr; addr = fst_iface_get_peer_next(other, &ctx, TRUE)) {
291                 const struct wpabuf *mbies;
292                 u8 other_iface_peer_addr[ETH_ALEN];
293                 struct fst_iface *other_new_iface;
294
295                 mbies = fst_iface_get_peer_mb_ie(other, addr);
296                 if (!mbies)
297                         continue;
298
299                 other_new_iface = fst_group_get_new_iface_by_mbie_and_band_id(
300                         g, wpabuf_head(mbies), wpabuf_len(mbies),
301                         iface_band_id, other_iface_peer_addr);
302                 if (other_new_iface == iface &&
303                     os_memcmp(iface_addr, other_iface_peer_addr,
304                               ETH_ALEN) != 0) {
305                         os_memcpy(peer_addr, addr, ETH_ALEN);
306                         return TRUE;
307                 }
308         }
309
310         return FALSE;
311 }
312
313
314 struct fst_iface *
315 fst_group_find_new_iface_by_stie(struct fst_group *g,
316                                  struct fst_iface *iface,
317                                  const u8 *peer_addr,
318                                  const struct session_transition_ie *stie,
319                                  u8 *iface_peer_addr)
320 {
321         struct fst_iface *i;
322
323         foreach_fst_group_iface(g, i) {
324                 if (i == iface ||
325                     stie->new_band_id != fst_iface_get_band_id(i))
326                         continue;
327                 if (fst_group_does_iface_appear_in_other_mbies(g, iface, i,
328                         iface_peer_addr))
329                         return i;
330                 break;
331         }
332         return NULL;
333 }
334
335
336 struct fst_iface *
337 fst_group_get_new_iface_by_stie_and_mbie(
338         struct fst_group *g, const u8 *mb_ies_buff, size_t mb_ies_size,
339         const struct session_transition_ie *stie, u8 *iface_peer_addr)
340 {
341         return fst_group_get_new_iface_by_mbie_and_band_id(
342                 g, mb_ies_buff, mb_ies_size, stie->new_band_id,
343                 iface_peer_addr);
344 }
345
346
347 struct fst_group * fst_group_create(const char *group_id)
348 {
349         struct fst_group *g;
350
351         g = os_zalloc(sizeof(*g));
352         if (g == NULL) {
353                 fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id);
354                 return NULL;
355         }
356
357         dl_list_init(&g->ifaces);
358         os_strlcpy(g->group_id, group_id, sizeof(g->group_id));
359
360         dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry);
361         fst_printf_group(g, MSG_DEBUG, "instance created");
362
363         foreach_fst_ctrl_call(on_group_created, g);
364
365         return g;
366 }
367
368
369 void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i)
370 {
371         struct dl_list *list = &g->ifaces;
372         struct fst_iface *f;
373
374         /*
375          * Add new interface to the list.
376          * The list is sorted in descending order by priority to allow
377          * multiple MB IEs creation according to the spec (see 10.32 Multi-band
378          * operation, 10.32.1 General), as they should be ordered according to
379          * priorities.
380          */
381         foreach_fst_group_iface(g, f) {
382                 if (fst_iface_get_priority(f) < fst_iface_get_priority(i))
383                         break;
384                 list = &f->group_lentry;
385         }
386         dl_list_add(list, &i->group_lentry);
387 }
388
389
390 void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i)
391 {
392         dl_list_del(&i->group_lentry);
393 }
394
395
396 void fst_group_delete(struct fst_group *group)
397 {
398         struct fst_session *s;
399
400         dl_list_del(&group->global_groups_lentry);
401         WPA_ASSERT(dl_list_empty(&group->ifaces));
402         foreach_fst_ctrl_call(on_group_deleted, group);
403         fst_printf_group(group, MSG_DEBUG, "instance deleted");
404         while ((s = fst_session_global_get_first_by_group(group)) != NULL)
405                 fst_session_delete(s);
406         os_free(group);
407 }
408
409
410 Boolean fst_group_delete_if_empty(struct fst_group *group)
411 {
412         Boolean is_empty = !fst_group_has_ifaces(group) &&
413                 !fst_session_global_get_first_by_group(group);
414
415         if (is_empty)
416                 fst_group_delete(group);
417
418         return is_empty;
419 }
420
421
422 void fst_group_update_ie(struct fst_group *g)
423 {
424         struct fst_iface *i;
425
426         foreach_fst_group_iface(g, i) {
427                 struct wpabuf *mbie = fst_group_create_mb_ie(g, i);
428
429                 if (!mbie)
430                         fst_printf_iface(i, MSG_WARNING, "cannot create MB IE");
431
432                 fst_iface_attach_mbie(i, mbie);
433                 fst_iface_set_ies(i, mbie);
434                 fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie);
435         }
436 }