FST: Fix FST Action frame length validation
[mech_eap.git] / src / fst / fst_session.c
1 /*
2  * FST module - FST Session 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
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/defs.h"
14 #include "fst/fst_internal.h"
15 #include "fst/fst_defs.h"
16 #include "fst/fst_ctrl_iface.h"
17 #ifdef CONFIG_FST_TEST
18 #include "fst/fst_ctrl_defs.h"
19 #endif /* CONFIG_FST_TEST */
20
21 #define US_80211_TU 1024
22
23 #define US_TO_TU(m) ((m) * / US_80211_TU)
24 #define TU_TO_US(m) ((m) * US_80211_TU)
25
26 #define FST_LLT_SWITCH_IMMEDIATELY 0
27
28 #define fst_printf_session(s, level, format, ...) \
29         fst_printf((level), "%u (0x%08x): [" MACSTR "," MACSTR "] :" format, \
30                    (s)->id, (s)->data.fsts_id, \
31                    MAC2STR((s)->data.old_peer_addr), \
32                    MAC2STR((s)->data.new_peer_addr), \
33                    ##__VA_ARGS__)
34
35 #define fst_printf_siface(s, iface, level, format, ...) \
36         fst_printf_session((s), (level), "%s: " format, \
37                            fst_iface_get_name(iface), ##__VA_ARGS__)
38
39 #define fst_printf_sframe(s, is_old, level, format, ...) \
40         fst_printf_siface((s), \
41                 (is_old) ? (s)->data.old_iface : (s)->data.new_iface, \
42                 (level), format, ##__VA_ARGS__)
43
44 #define FST_LLT_MS_DEFAULT 50
45 #define FST_ACTION_MAX_SUPPORTED   FST_ACTION_ON_CHANNEL_TUNNEL
46
47 const char * const fst_action_names[] = {
48         [FST_ACTION_SETUP_REQUEST]     = "Setup Request",
49         [FST_ACTION_SETUP_RESPONSE]    = "Setup Response",
50         [FST_ACTION_TEAR_DOWN]         = "Tear Down",
51         [FST_ACTION_ACK_REQUEST]       = "Ack Request",
52         [FST_ACTION_ACK_RESPONSE]      = "Ack Response",
53         [FST_ACTION_ON_CHANNEL_TUNNEL] = "On Channel Tunnel",
54 };
55
56 struct fst_session {
57         struct {
58                 /* Session configuration that can be zeroed on reset */
59                 u8 old_peer_addr[ETH_ALEN];
60                 u8 new_peer_addr[ETH_ALEN];
61                 struct fst_iface *new_iface;
62                 struct fst_iface *old_iface;
63                 u32 llt_ms;
64                 u8 pending_setup_req_dlgt;
65                 u32 fsts_id; /* FSTS ID, see spec, 8.4.2.147
66                               * Session Transition element */
67         } data;
68         /* Session object internal fields which won't be zeroed on reset */
69         struct dl_list global_sessions_lentry;
70         u32 id; /* Session object ID used to identify
71                  * specific session object */
72         struct fst_group *group;
73         enum fst_session_state state;
74         Boolean stt_armed;
75 };
76
77 static struct dl_list global_sessions_list;
78 static u32 global_session_id = 0;
79
80 #define foreach_fst_session(s) \
81         dl_list_for_each((s), &global_sessions_list, \
82                          struct fst_session, global_sessions_lentry)
83
84 #define foreach_fst_session_safe(s, temp) \
85         dl_list_for_each_safe((s), (temp), &global_sessions_list, \
86                               struct fst_session, global_sessions_lentry)
87
88
89 static void fst_session_global_inc_id(void)
90 {
91         global_session_id++;
92         if (global_session_id == FST_INVALID_SESSION_ID)
93                 global_session_id++;
94 }
95
96
97 int fst_session_global_init(void)
98 {
99         dl_list_init(&global_sessions_list);
100         return 0;
101 }
102
103
104 void fst_session_global_deinit(void)
105 {
106         WPA_ASSERT(dl_list_empty(&global_sessions_list));
107 }
108
109
110 static inline void fst_session_notify_ctrl(struct fst_session *s,
111                                            enum fst_event_type event_type,
112                                            union fst_event_extra *extra)
113 {
114         foreach_fst_ctrl_call(on_event, event_type, NULL, s, extra);
115 }
116
117
118 static void fst_session_set_state(struct fst_session *s,
119                                   enum fst_session_state state,
120                                   union fst_session_state_switch_extra *extra)
121 {
122         if (s->state != state) {
123                 union fst_event_extra evext = {
124                         .session_state = {
125                                 .old_state = s->state,
126                                 .new_state = state,
127                         },
128                 };
129
130                 if (extra)
131                         evext.session_state.extra = *extra;
132                 fst_session_notify_ctrl(s, EVENT_FST_SESSION_STATE_CHANGED,
133                                         &evext);
134                 fst_printf_session(s, MSG_INFO, "State: %s => %s",
135                                    fst_session_state_name(s->state),
136                                    fst_session_state_name(state));
137                 s->state = state;
138         }
139 }
140
141
142 static u32 fst_find_free_session_id(void)
143 {
144         u32 i, id = FST_INVALID_SESSION_ID;
145         struct fst_session *s;
146
147         for (i = 0; i < (u32) -1; i++) {
148                 Boolean in_use = FALSE;
149
150                 foreach_fst_session(s) {
151                         if (s->id == global_session_id) {
152                                 fst_session_global_inc_id();
153                                 in_use = TRUE;
154                                 break;
155                         }
156                 }
157                 if (!in_use) {
158                         id = global_session_id;
159                         fst_session_global_inc_id();
160                         break;
161                 }
162         }
163
164         return id;
165 }
166
167
168 static void fst_session_timeout_handler(void *eloop_data, void *user_ctx)
169 {
170         struct fst_session *s = user_ctx;
171         union fst_session_state_switch_extra extra = {
172                 .to_initial = {
173                         .reason = REASON_STT,
174                 },
175         };
176
177         fst_printf_session(s, MSG_WARNING, "Session State Timeout");
178         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &extra);
179 }
180
181
182 static void fst_session_stt_arm(struct fst_session *s)
183 {
184         eloop_register_timeout(0, TU_TO_US(FST_DEFAULT_SESSION_TIMEOUT_TU),
185                                fst_session_timeout_handler, NULL, s);
186         s->stt_armed = TRUE;
187 }
188
189
190 static void fst_session_stt_disarm(struct fst_session *s)
191 {
192         if (s->stt_armed) {
193                 eloop_cancel_timeout(fst_session_timeout_handler, NULL, s);
194                 s->stt_armed = FALSE;
195         }
196 }
197
198
199 static Boolean fst_session_is_in_transition(struct fst_session *s)
200 {
201         /* See spec, 10.32.2.2  Transitioning between states */
202         return s->stt_armed;
203 }
204
205
206 static int fst_session_is_in_progress(struct fst_session *s)
207 {
208         return s->state != FST_SESSION_STATE_INITIAL;
209 }
210
211
212 static int fst_session_is_ready_pending(struct fst_session *s)
213 {
214         return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
215                 fst_session_is_in_transition(s);
216 }
217
218
219 static int fst_session_is_ready(struct fst_session *s)
220 {
221         return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
222                 !fst_session_is_in_transition(s);
223 }
224
225
226 static int fst_session_is_switch_requested(struct fst_session *s)
227 {
228         return s->state == FST_SESSION_STATE_TRANSITION_DONE &&
229                 fst_session_is_in_transition(s);
230 }
231
232
233 static struct fst_session *
234 fst_find_session_in_progress(const u8 *peer_addr, struct fst_group *g)
235 {
236         struct fst_session *s;
237
238         foreach_fst_session(s) {
239                 if (s->group == g &&
240                     (os_memcmp(s->data.old_peer_addr, peer_addr,
241                                ETH_ALEN) == 0 ||
242                      os_memcmp(s->data.new_peer_addr, peer_addr,
243                                ETH_ALEN) == 0) &&
244                     fst_session_is_in_progress(s))
245                         return s;
246         }
247
248         return NULL;
249 }
250
251
252 static void fst_session_reset_ex(struct fst_session *s, enum fst_reason reason)
253 {
254         union fst_session_state_switch_extra evext = {
255                 .to_initial = {
256                         .reason = reason,
257                 },
258         };
259
260         if (s->state == FST_SESSION_STATE_SETUP_COMPLETION ||
261             s->state == FST_SESSION_STATE_TRANSITION_DONE)
262                 fst_session_tear_down_setup(s);
263         fst_session_stt_disarm(s);
264         os_memset(&s->data, 0, sizeof(s->data));
265         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
266 }
267
268
269 static int fst_session_send_action(struct fst_session *s, Boolean old_iface,
270                                    const void *payload, size_t size,
271                                    const struct wpabuf *extra_buf)
272 {
273         size_t len;
274         int res;
275         struct wpabuf *buf;
276         u8 action;
277         struct fst_iface *iface =
278                 old_iface ? s->data.old_iface : s->data.new_iface;
279
280         WPA_ASSERT(payload != NULL);
281         WPA_ASSERT(size != 0);
282
283         action = *(const u8 *) payload;
284
285         WPA_ASSERT(action <= FST_ACTION_MAX_SUPPORTED);
286
287         if (!iface) {
288                 fst_printf_session(s, MSG_ERROR,
289                                    "no %s interface for FST Action '%s' sending",
290                                    old_iface ? "old" : "new",
291                                    fst_action_names[action]);
292                 return -1;
293         }
294
295         len = sizeof(u8) /* category */ + size;
296         if (extra_buf)
297                 len += wpabuf_size(extra_buf);
298
299         buf = wpabuf_alloc(len);
300         if (!buf) {
301                 fst_printf_session(s, MSG_ERROR,
302                                    "cannot allocate buffer of %zu bytes for FST Action '%s' sending",
303                                    len, fst_action_names[action]);
304                 return -1;
305         }
306
307         wpabuf_put_u8(buf, WLAN_ACTION_FST);
308         wpabuf_put_data(buf, payload, size);
309         if (extra_buf)
310                 wpabuf_put_buf(buf, extra_buf);
311
312         res = fst_iface_send_action(iface,
313                                     old_iface ? s->data.old_peer_addr :
314                                     s->data.new_peer_addr, buf);
315         if (res < 0)
316                 fst_printf_siface(s, iface, MSG_ERROR,
317                                   "failed to send FST Action '%s'",
318                                   fst_action_names[action]);
319         else
320                 fst_printf_siface(s, iface, MSG_DEBUG, "FST Action '%s' sent",
321                                   fst_action_names[action]);
322         wpabuf_free(buf);
323
324         return res;
325 }
326
327
328 static int fst_session_send_tear_down(struct fst_session *s)
329 {
330         struct fst_tear_down td;
331         int res;
332
333         if (!fst_session_is_in_progress(s)) {
334                 fst_printf_session(s, MSG_ERROR, "No FST setup to tear down");
335                 return -1;
336         }
337
338         WPA_ASSERT(s->data.old_iface != NULL);
339         WPA_ASSERT(s->data.new_iface != NULL);
340
341         os_memset(&td, 0, sizeof(td));
342
343         td.action = FST_ACTION_TEAR_DOWN;
344         td.fsts_id = host_to_le32(s->data.fsts_id);
345
346         res = fst_session_send_action(s, TRUE, &td, sizeof(td), NULL);
347         if (!res)
348                 fst_printf_sframe(s, TRUE, MSG_INFO, "FST TearDown sent");
349         else
350                 fst_printf_sframe(s, TRUE, MSG_ERROR,
351                                   "failed to send FST TearDown");
352
353         return res;
354 }
355
356
357 static void fst_session_handle_setup_request(struct fst_iface *iface,
358                                              const struct ieee80211_mgmt *mgmt,
359                                              size_t frame_len)
360 {
361         struct fst_session *s;
362         const struct fst_setup_req *req =
363                 (const struct fst_setup_req *) &mgmt->u.action.u.fst_action;
364         struct fst_iface *new_iface = NULL;
365         struct fst_group *g;
366         u8 new_iface_peer_addr[ETH_ALEN];
367         struct wpabuf *peer_mbies;
368         size_t plen;
369
370         if (frame_len < IEEE80211_HDRLEN + 1 + sizeof(*req))  {
371                 fst_printf_iface(iface, MSG_WARNING,
372                                  "FST Request dropped: too short (%zu < %zu)",
373                                  frame_len,
374                                  IEEE80211_HDRLEN + 1 + sizeof(*req));
375                 return;
376         }
377         plen = frame_len - IEEE80211_HDRLEN - 1;
378
379         if (req->stie.new_band_id == req->stie.old_band_id) {
380                 fst_printf_iface(iface, MSG_WARNING,
381                                  "FST Request dropped: new and old band IDs are the same");
382                 return;
383         }
384
385         g = fst_iface_get_group(iface);
386
387         if (plen > sizeof(*req)) {
388                 fst_iface_update_mb_ie(iface, mgmt->sa, (const u8 *) (req + 1),
389                                        plen - sizeof(*req));
390                 fst_printf_iface(iface, MSG_INFO,
391                                  "FST Request: MB IEs updated for " MACSTR,
392                                  MAC2STR(mgmt->sa));
393         }
394
395         peer_mbies = fst_iface_get_peer_mb_ie(iface, mgmt->sa);
396         if (peer_mbies) {
397                 new_iface = fst_group_get_new_iface_by_stie_and_mbie(
398                         g, wpabuf_head(peer_mbies), wpabuf_len(peer_mbies),
399                         &req->stie, new_iface_peer_addr);
400                 if (new_iface)
401                         fst_printf_iface(iface, MSG_INFO,
402                                          "FST Request: new iface (%s:" MACSTR
403                                          ") found by MB IEs",
404                                          fst_iface_get_name(new_iface),
405                                          MAC2STR(new_iface_peer_addr));
406         }
407
408         if (!new_iface) {
409                 new_iface = fst_group_find_new_iface_by_stie(
410                         g, iface, mgmt->sa, &req->stie,
411                         new_iface_peer_addr);
412                 if (new_iface)
413                         fst_printf_iface(iface, MSG_INFO,
414                                          "FST Request: new iface (%s:" MACSTR
415                                          ") found by others",
416                                          fst_iface_get_name(new_iface),
417                                          MAC2STR(new_iface_peer_addr));
418         }
419
420         if (!new_iface) {
421                 fst_printf_iface(iface, MSG_WARNING,
422                                  "FST Request dropped: new iface not found");
423                 return;
424         }
425
426         s = fst_find_session_in_progress(mgmt->sa, g);
427         if (s) {
428                 union fst_session_state_switch_extra evext = {
429                         .to_initial = {
430                                 .reason = REASON_SETUP,
431                         },
432                 };
433
434                 /*
435                  * 10.32.2.2  Transitioning between states:
436                  * Upon receipt of an FST Setup Request frame, the responder
437                  * shall respond with an FST Setup Response frame unless it has
438                  * a pending FST Setup Request frame addressed to the initiator
439                  * and the responder has a numerically larger MAC address than
440                  * the initiator’s MAC address, in which case, the responder
441                  * shall delete the received FST Setup Request.
442                  */
443                 if (os_memcmp(mgmt->da, mgmt->sa, ETH_ALEN) > 0) {
444                         fst_printf_session(s, MSG_WARNING,
445                                            "FST Request dropped due to MAC comparison (our MAC is "
446                                            MACSTR ")",
447                                            MAC2STR(mgmt->da));
448                         return;
449                 }
450
451                 if (!fst_session_is_ready_pending(s)) {
452                         fst_printf_session(s, MSG_WARNING,
453                                            "FST Request from " MACSTR
454                                            " dropped due to inappropriate state %s",
455                                            MAC2STR(mgmt->da),
456                                            fst_session_state_name(s->state));
457                         return;
458                 }
459
460
461                 /*
462                  * If FST Setup Request arrived with the same FSTS ID as one we
463                  * initialized before, it means the other side either didn't
464                  * receive our FST Request or skipped it for some reason (for
465                  * example, due to numerical MAC comparison).
466                  *
467                  * In this case, there's no need to tear down the session.
468                  * Moreover, as FSTS ID is the same, the other side will
469                  * associate this tear down with the session it initiated that
470                  * will break the sync.
471                  */
472                 if (le_to_host32(req->stie.fsts_id) != s->data.fsts_id)
473                         fst_session_send_tear_down(s);
474                 else
475                         fst_printf_session(s, MSG_WARNING,
476                                            "Skipping TearDown as the FST request has the same FSTS ID as initiated");
477                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
478                 fst_session_stt_disarm(s);
479                 fst_printf_session(s, MSG_WARNING, "reset due to FST request");
480         }
481
482         s = fst_session_create(g);
483         if (!s) {
484                 fst_printf(MSG_WARNING,
485                            "FST Request dropped: cannot create session for %s and %s",
486                            fst_iface_get_name(iface),
487                            fst_iface_get_name(new_iface));
488                 return;
489         }
490
491         fst_session_set_iface(s, iface, TRUE);
492         fst_session_set_peer_addr(s, mgmt->sa, TRUE);
493         fst_session_set_iface(s, new_iface, FALSE);
494         fst_session_set_peer_addr(s, new_iface_peer_addr, FALSE);
495         fst_session_set_llt(s, FST_LLT_VAL_TO_MS(le_to_host32(req->llt)));
496         s->data.pending_setup_req_dlgt = req->dialog_token;
497         s->data.fsts_id = le_to_host32(req->stie.fsts_id);
498
499         fst_session_stt_arm(s);
500
501         fst_session_notify_ctrl(s, EVENT_FST_SETUP, NULL);
502
503         fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION, NULL);
504 }
505
506
507 static void fst_session_handle_setup_response(struct fst_session *s,
508                                               struct fst_iface *iface,
509                                               const struct ieee80211_mgmt *mgmt,
510                                               size_t frame_len)
511 {
512         const struct fst_setup_res *res =
513                 (const struct fst_setup_res *) &mgmt->u.action.u.fst_action;
514         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
515         enum hostapd_hw_mode hw_mode;
516         u8 channel;
517         union fst_session_state_switch_extra evext = {
518                 .to_initial = {0},
519         };
520
521         if (iface != s->data.old_iface) {
522                 fst_printf_session(s, MSG_WARNING,
523                                    "FST Response dropped: %s is not the old iface",
524                                    fst_iface_get_name(iface));
525                 return;
526         }
527
528         if (!fst_session_is_ready_pending(s)) {
529                 fst_printf_session(s, MSG_WARNING,
530                                    "FST Response dropped due to wrong state: %s",
531                                    fst_session_state_name(s->state));
532                 return;
533         }
534
535         if (plen < sizeof(*res)) {
536                 fst_printf_session(s, MSG_WARNING,
537                                    "Too short FST Response dropped");
538                 return;
539         }
540
541         if (res->dialog_token != s->data.pending_setup_req_dlgt)  {
542                 fst_printf_session(s, MSG_WARNING,
543                                    "FST Response dropped due to wrong dialog token (%u != %u)",
544                                    s->data.pending_setup_req_dlgt,
545                                    res->dialog_token);
546                 return;
547         }
548
549         if (res->status_code == WLAN_STATUS_SUCCESS &&
550             le_to_host32(res->stie.fsts_id) != s->data.fsts_id) {
551                 fst_printf_session(s, MSG_WARNING,
552                                    "FST Response dropped due to wrong FST Session ID (%u)",
553                                    le_to_host32(res->stie.fsts_id));
554                 return;
555         }
556
557         fst_session_stt_disarm(s);
558
559         if (res->status_code != WLAN_STATUS_SUCCESS) {
560                 /*
561                  * 10.32.2.2  Transitioning between states
562                  * The initiator shall set the STT to the value of the
563                  * FSTSessionTimeOut field at ... and at each ACK frame sent in
564                  * response to a received FST Setup Response with the Status
565                  * Code field equal to PENDING_ADMITTING_FST_SESSION or
566                  * PENDING_GAP_IN_BA_WINDOW.
567                  */
568                 evext.to_initial.reason = REASON_REJECT;
569                 evext.to_initial.reject_code = res->status_code;
570                 evext.to_initial.initiator = FST_INITIATOR_REMOTE;
571                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
572                 fst_printf_session(s, MSG_WARNING,
573                                    "FST Setup rejected by remote side with status %u",
574                                    res->status_code);
575                 return;
576         }
577
578         fst_iface_get_channel_info(s->data.new_iface, &hw_mode, &channel);
579
580         if (fst_hw_mode_to_band(hw_mode) != res->stie.new_band_id) {
581                 evext.to_initial.reason = REASON_ERROR_PARAMS;
582                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
583                 fst_printf_session(s, MSG_WARNING,
584                                    "invalid FST Setup parameters");
585                 fst_session_tear_down_setup(s);
586                 return;
587         }
588
589         fst_printf_session(s, MSG_INFO,
590                            "%s: FST Setup established for %s (llt=%u)",
591                            fst_iface_get_name(s->data.old_iface),
592                            fst_iface_get_name(s->data.new_iface),
593                            s->data.llt_ms);
594
595         fst_session_notify_ctrl(s, EVENT_FST_ESTABLISHED, NULL);
596
597         if (s->data.llt_ms == FST_LLT_SWITCH_IMMEDIATELY)
598                 fst_session_initiate_switch(s);
599 }
600
601
602 static void fst_session_handle_tear_down(struct fst_session *s,
603                                          struct fst_iface *iface,
604                                          const struct ieee80211_mgmt *mgmt,
605                                          size_t frame_len)
606 {
607         const struct fst_tear_down *td =
608                 (const struct fst_tear_down *) &mgmt->u.action.u.fst_action;
609         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
610         union fst_session_state_switch_extra evext = {
611                 .to_initial = {
612                         .reason = REASON_TEARDOWN,
613                         .initiator = FST_INITIATOR_REMOTE,
614                 },
615         };
616
617         if (!fst_session_is_in_progress(s)) {
618                 fst_printf_session(s, MSG_WARNING, "no FST Setup to tear down");
619                 return;
620         }
621
622         if (plen < sizeof(*td)) {
623                 fst_printf_session(s, MSG_WARNING,
624                                    "Too short FST Tear Down dropped");
625                 return;
626         }
627
628         if (le_to_host32(td->fsts_id) != s->data.fsts_id) {
629                 fst_printf_siface(s, iface, MSG_WARNING,
630                                   "tear down for wrong FST Setup ID (%u)",
631                                   le_to_host32(td->fsts_id));
632                 return;
633         }
634
635         fst_session_stt_disarm(s);
636
637         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
638 }
639
640
641 static void fst_session_handle_ack_request(struct fst_session *s,
642                                            struct fst_iface *iface,
643                                            const struct ieee80211_mgmt *mgmt,
644                                            size_t frame_len)
645 {
646         const struct fst_ack_req *req =
647                 (const struct fst_ack_req *) &mgmt->u.action.u.fst_action;
648         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
649         struct fst_ack_res res;
650         union fst_session_state_switch_extra evext = {
651                 .to_initial = {
652                         .reason = REASON_SWITCH,
653                         .initiator = FST_INITIATOR_REMOTE,
654                 },
655         };
656
657         if (!fst_session_is_ready(s) && !fst_session_is_switch_requested(s)) {
658                 fst_printf_siface(s, iface, MSG_ERROR,
659                                   "cannot initiate switch due to wrong session state (%s)",
660                                   fst_session_state_name(s->state));
661                 return;
662         }
663
664         WPA_ASSERT(s->data.new_iface != NULL);
665
666         if (iface != s->data.new_iface) {
667                 fst_printf_siface(s, iface, MSG_ERROR,
668                                   "Ack received on wrong interface");
669                 return;
670         }
671
672         if (plen < sizeof(*req)) {
673                 fst_printf_session(s, MSG_WARNING,
674                                    "Too short FST Ack Request dropped");
675                 return;
676         }
677
678         if (le_to_host32(req->fsts_id) != s->data.fsts_id) {
679                 fst_printf_siface(s, iface, MSG_WARNING,
680                                   "Ack for wrong FST Setup ID (%u)",
681                                   le_to_host32(req->fsts_id));
682                 return;
683         }
684
685         os_memset(&res, 0, sizeof(res));
686
687         res.action = FST_ACTION_ACK_RESPONSE;
688         res.dialog_token = req->dialog_token;
689         res.fsts_id = req->fsts_id;
690
691         if (!fst_session_send_action(s, FALSE, &res, sizeof(res), NULL)) {
692                 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Response sent");
693                 fst_session_stt_disarm(s);
694                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
695                                       NULL);
696                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED,
697                                       NULL);
698                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
699         }
700 }
701
702
703 static void
704 fst_session_handle_ack_response(struct fst_session *s,
705                                 struct fst_iface *iface,
706                                 const struct ieee80211_mgmt *mgmt,
707                                 size_t frame_len)
708 {
709         const struct fst_ack_res *res =
710                 (const struct fst_ack_res *) &mgmt->u.action.u.fst_action;
711         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
712         union fst_session_state_switch_extra evext = {
713                 .to_initial = {
714                         .reason = REASON_SWITCH,
715                         .initiator = FST_INITIATOR_LOCAL,
716                 },
717         };
718
719         if (!fst_session_is_switch_requested(s)) {
720                 fst_printf_siface(s, iface, MSG_ERROR,
721                                   "Ack Response in inappropriate session state (%s)",
722                                   fst_session_state_name(s->state));
723                 return;
724         }
725
726         WPA_ASSERT(s->data.new_iface != NULL);
727
728         if (iface != s->data.new_iface) {
729                 fst_printf_siface(s, iface, MSG_ERROR,
730                                   "Ack response received on wrong interface");
731                 return;
732         }
733
734         if (plen < sizeof(*res)) {
735                 fst_printf_session(s, MSG_WARNING,
736                                    "Too short FST Ack Response dropped");
737                 return;
738         }
739
740         if (le_to_host32(res->fsts_id) != s->data.fsts_id) {
741                 fst_printf_siface(s, iface, MSG_ERROR,
742                                   "Ack response for wrong FST Setup ID (%u)",
743                                   le_to_host32(res->fsts_id));
744                 return;
745         }
746
747         fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED, NULL);
748         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
749
750         fst_session_stt_disarm(s);
751 }
752
753
754 struct fst_session * fst_session_create(struct fst_group *g)
755 {
756         struct fst_session *s;
757         u32 id;
758
759         WPA_ASSERT(!is_zero_ether_addr(own_addr));
760
761         id = fst_find_free_session_id();
762         if (id == FST_INVALID_SESSION_ID) {
763                 fst_printf(MSG_ERROR, "Cannot assign new session ID");
764                 return NULL;
765         }
766
767         s = os_zalloc(sizeof(*s));
768         if (!s) {
769                 fst_printf(MSG_ERROR, "Cannot allocate new session object");
770                 return NULL;
771         }
772
773         s->id = id;
774         s->group = g;
775         s->state = FST_SESSION_STATE_INITIAL;
776
777         s->data.llt_ms = FST_LLT_MS_DEFAULT;
778
779         fst_printf(MSG_INFO, "Session %u created", s->id);
780
781         dl_list_add_tail(&global_sessions_list, &s->global_sessions_lentry);
782
783         foreach_fst_ctrl_call(on_session_added, s);
784
785         return s;
786 }
787
788
789 void fst_session_set_iface(struct fst_session *s, struct fst_iface *iface,
790                            Boolean is_old)
791 {
792         if (is_old)
793                 s->data.old_iface = iface;
794         else
795                 s->data.new_iface = iface;
796
797 }
798
799
800 void fst_session_set_llt(struct fst_session *s, u32 llt)
801 {
802         s->data.llt_ms = llt;
803 }
804
805
806 void fst_session_set_peer_addr(struct fst_session *s, const u8 *addr,
807                                Boolean is_old)
808 {
809         u8 *a = is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
810
811         os_memcpy(a, addr, ETH_ALEN);
812 }
813
814
815 int fst_session_initiate_setup(struct fst_session *s)
816 {
817         struct fst_setup_req req;
818         int res;
819         u32 fsts_id;
820         u8 dialog_token;
821         struct fst_session *_s;
822
823         if (fst_session_is_in_progress(s)) {
824                 fst_printf_session(s, MSG_ERROR, "Session in progress");
825                 return -EINVAL;
826         }
827
828         if (is_zero_ether_addr(s->data.old_peer_addr)) {
829                 fst_printf_session(s, MSG_ERROR, "No old peer MAC address");
830                 return -EINVAL;
831         }
832
833         if (is_zero_ether_addr(s->data.new_peer_addr)) {
834                 fst_printf_session(s, MSG_ERROR, "No new peer MAC address");
835                 return -EINVAL;
836         }
837
838         if (!s->data.old_iface) {
839                 fst_printf_session(s, MSG_ERROR, "No old interface defined");
840                 return -EINVAL;
841         }
842
843         if (!s->data.new_iface) {
844                 fst_printf_session(s, MSG_ERROR, "No new interface defined");
845                 return -EINVAL;
846         }
847
848         if (s->data.new_iface == s->data.old_iface) {
849                 fst_printf_session(s, MSG_ERROR,
850                                    "Same interface set as old and new");
851                 return -EINVAL;
852         }
853
854         if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr)) {
855                 fst_printf_session(s, MSG_ERROR,
856                                    "The preset old peer address is not connected");
857                 return -EINVAL;
858         }
859
860         if (!fst_iface_is_connected(s->data.new_iface, s->data.new_peer_addr)) {
861                 fst_printf_session(s, MSG_ERROR,
862                                    "The preset new peer address is not connected");
863                 return -EINVAL;
864         }
865
866         _s = fst_find_session_in_progress(s->data.old_peer_addr, s->group);
867         if (_s) {
868                 fst_printf_session(s, MSG_ERROR,
869                                    "There is another session in progress (old): %u",
870                                    _s->id);
871                 return -EINVAL;
872         }
873
874         _s = fst_find_session_in_progress(s->data.new_peer_addr, s->group);
875         if (_s) {
876                 fst_printf_session(s, MSG_ERROR,
877                                    "There is another session in progress (new): %u",
878                                    _s->id);
879                 return -EINVAL;
880         }
881
882         dialog_token = fst_group_assign_dialog_token(s->group);
883         fsts_id = fst_group_assign_fsts_id(s->group);
884
885         os_memset(&req, 0, sizeof(req));
886
887         fst_printf_siface(s, s->data.old_iface, MSG_INFO,
888                 "initiating FST setup for %s (llt=%u ms)",
889                 fst_iface_get_name(s->data.new_iface), s->data.llt_ms);
890
891         req.action = FST_ACTION_SETUP_REQUEST;
892         req.dialog_token = dialog_token;
893         req.llt = host_to_le32(FST_LLT_MS_TO_VAL(s->data.llt_ms));
894         /* 8.4.2.147 Session Transition element */
895         req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
896         req.stie.length = sizeof(req.stie);
897         req.stie.fsts_id = host_to_le32(fsts_id);
898         req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
899
900         req.stie.new_band_id = fst_iface_get_band_id(s->data.new_iface);
901         req.stie.new_band_op = 1;
902         req.stie.new_band_setup = 0;
903
904         req.stie.old_band_id = fst_iface_get_band_id(s->data.old_iface);
905         req.stie.old_band_op = 1;
906         req.stie.old_band_setup = 0;
907
908         res = fst_session_send_action(s, TRUE, &req, sizeof(req),
909                                       fst_iface_get_mbie(s->data.old_iface));
910         if (!res) {
911                 s->data.fsts_id = fsts_id;
912                 s->data.pending_setup_req_dlgt = dialog_token;
913                 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Request sent");
914                 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION,
915                                       NULL);
916
917                 fst_session_stt_arm(s);
918         }
919
920         return res;
921 }
922
923
924 int fst_session_respond(struct fst_session *s, u8 status_code)
925 {
926         struct fst_setup_res res;
927         enum hostapd_hw_mode hw_mode;
928         u8 channel;
929
930         if (!fst_session_is_ready_pending(s)) {
931                 fst_printf_session(s, MSG_ERROR, "incorrect state: %s",
932                                    fst_session_state_name(s->state));
933                 return -EINVAL;
934         }
935
936         if (is_zero_ether_addr(s->data.old_peer_addr)) {
937                 fst_printf_session(s, MSG_ERROR, "No peer MAC address");
938                 return -EINVAL;
939         }
940
941         if (!s->data.old_iface) {
942                 fst_printf_session(s, MSG_ERROR, "No old interface defined");
943                 return -EINVAL;
944         }
945
946         if (!s->data.new_iface) {
947                 fst_printf_session(s, MSG_ERROR, "No new interface defined");
948                 return -EINVAL;
949         }
950
951         if (s->data.new_iface == s->data.old_iface) {
952                 fst_printf_session(s, MSG_ERROR,
953                                    "Same interface set as old and new");
954                 return -EINVAL;
955         }
956
957         if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr)) {
958                 fst_printf_session(s, MSG_ERROR,
959                                    "The preset peer address is not in the peer list");
960                 return -EINVAL;
961         }
962
963         fst_session_stt_disarm(s);
964
965         os_memset(&res, 0, sizeof(res));
966
967         res.action = FST_ACTION_SETUP_RESPONSE;
968         res.dialog_token = s->data.pending_setup_req_dlgt;
969         res.status_code = status_code;
970
971         if (status_code == WLAN_STATUS_SUCCESS) {
972                 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
973                 res.stie.length = sizeof(res.stie);
974                 res.stie.fsts_id = s->data.fsts_id;
975                 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
976
977                 fst_iface_get_channel_info(s->data.new_iface, &hw_mode,
978                                            &channel);
979                 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
980                 res.stie.new_band_op = 1;
981                 res.stie.new_band_setup = 0;
982
983                 fst_iface_get_channel_info(s->data.old_iface, &hw_mode,
984                                            &channel);
985                 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
986                 res.stie.old_band_op = 1;
987                 res.stie.old_band_setup = 0;
988
989                 fst_printf_session(s, MSG_INFO,
990                                    "%s: FST Setup Request accepted for %s (llt=%u)",
991                                    fst_iface_get_name(s->data.old_iface),
992                                    fst_iface_get_name(s->data.new_iface),
993                                    s->data.llt_ms);
994         } else {
995                 fst_printf_session(s, MSG_WARNING,
996                                    "%s: FST Setup Request rejected with code %d",
997                                    fst_iface_get_name(s->data.old_iface),
998                                    status_code);
999         }
1000
1001         if (fst_session_send_action(s, TRUE, &res, sizeof(res),
1002                                     fst_iface_get_mbie(s->data.old_iface))) {
1003                 fst_printf_sframe(s, TRUE, MSG_ERROR,
1004                                   "cannot send FST Setup Response with code %d",
1005                                   status_code);
1006                 return -EINVAL;
1007         }
1008
1009         fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Response sent");
1010
1011         if (status_code != WLAN_STATUS_SUCCESS) {
1012                 union fst_session_state_switch_extra evext = {
1013                         .to_initial = {
1014                                 .reason = REASON_REJECT,
1015                                 .reject_code = status_code,
1016                                 .initiator = FST_INITIATOR_LOCAL,
1017                         },
1018                 };
1019                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1020         }
1021
1022         return 0;
1023 }
1024
1025
1026 int fst_session_initiate_switch(struct fst_session *s)
1027 {
1028         struct fst_ack_req req;
1029         int res;
1030         u8 dialog_token;
1031
1032         if (!fst_session_is_ready(s)) {
1033                 fst_printf_session(s, MSG_ERROR,
1034                                    "cannot initiate switch due to wrong setup state (%d)",
1035                                    s->state);
1036                 return -1;
1037         }
1038
1039         dialog_token = fst_group_assign_dialog_token(s->group);
1040
1041         WPA_ASSERT(s->data.new_iface != NULL);
1042         WPA_ASSERT(s->data.old_iface != NULL);
1043
1044         fst_printf_session(s, MSG_INFO, "initiating FST switch: %s => %s",
1045                            fst_iface_get_name(s->data.old_iface),
1046                            fst_iface_get_name(s->data.new_iface));
1047
1048         os_memset(&req, 0, sizeof(req));
1049
1050         req.action = FST_ACTION_ACK_REQUEST;
1051         req.dialog_token = dialog_token;
1052         req.fsts_id = host_to_le32(s->data.fsts_id);
1053
1054         res = fst_session_send_action(s, FALSE, &req, sizeof(req), NULL);
1055         if (!res) {
1056                 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Request sent");
1057                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
1058                                       NULL);
1059                 fst_session_stt_arm(s);
1060         } else {
1061                 fst_printf_sframe(s, FALSE, MSG_ERROR,
1062                                   "Cannot send FST Ack Request");
1063         }
1064
1065         return res;
1066 }
1067
1068
1069 void fst_session_handle_action(struct fst_session *s,
1070                                struct fst_iface *iface,
1071                                const struct ieee80211_mgmt *mgmt,
1072                                size_t frame_len)
1073 {
1074         switch (mgmt->u.action.u.fst_action.action) {
1075         case FST_ACTION_SETUP_REQUEST:
1076                 WPA_ASSERT(0);
1077                 break;
1078         case FST_ACTION_SETUP_RESPONSE:
1079                 fst_session_handle_setup_response(s, iface, mgmt, frame_len);
1080                 break;
1081         case FST_ACTION_TEAR_DOWN:
1082                 fst_session_handle_tear_down(s, iface, mgmt, frame_len);
1083                 break;
1084         case FST_ACTION_ACK_REQUEST:
1085                 fst_session_handle_ack_request(s, iface, mgmt, frame_len);
1086                 break;
1087         case FST_ACTION_ACK_RESPONSE:
1088                 fst_session_handle_ack_response(s, iface, mgmt, frame_len);
1089                 break;
1090         case FST_ACTION_ON_CHANNEL_TUNNEL:
1091         default:
1092                 fst_printf_sframe(s, FALSE, MSG_ERROR,
1093                                   "Unsupported FST Action frame");
1094                 break;
1095         }
1096 }
1097
1098
1099 int fst_session_tear_down_setup(struct fst_session *s)
1100 {
1101         int res;
1102         union fst_session_state_switch_extra evext = {
1103                 .to_initial = {
1104                         .reason = REASON_TEARDOWN,
1105                         .initiator = FST_INITIATOR_LOCAL,
1106                 },
1107         };
1108
1109         res = fst_session_send_tear_down(s);
1110
1111         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1112
1113         return res;
1114 }
1115
1116
1117 void fst_session_reset(struct fst_session *s)
1118 {
1119         fst_session_reset_ex(s, REASON_RESET);
1120 }
1121
1122
1123 void fst_session_delete(struct fst_session *s)
1124 {
1125         fst_printf(MSG_INFO, "Session %u deleted", s->id);
1126         dl_list_del(&s->global_sessions_lentry);
1127         foreach_fst_ctrl_call(on_session_removed, s);
1128         os_free(s);
1129 }
1130
1131
1132 struct fst_group * fst_session_get_group(struct fst_session *s)
1133 {
1134         return s->group;
1135 }
1136
1137
1138 struct fst_iface * fst_session_get_iface(struct fst_session *s, Boolean is_old)
1139 {
1140         return is_old ? s->data.old_iface : s->data.new_iface;
1141 }
1142
1143
1144 u32 fst_session_get_id(struct fst_session *s)
1145 {
1146         return s->id;
1147 }
1148
1149
1150 const u8 * fst_session_get_peer_addr(struct fst_session *s, Boolean is_old)
1151 {
1152         return is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
1153 }
1154
1155
1156 u32 fst_session_get_llt(struct fst_session *s)
1157 {
1158         return s->data.llt_ms;
1159 }
1160
1161
1162 enum fst_session_state fst_session_get_state(struct fst_session *s)
1163 {
1164         return s->state;
1165 }
1166
1167
1168 struct fst_session * fst_session_get_by_id(u32 id)
1169 {
1170         struct fst_session *s;
1171
1172         foreach_fst_session(s) {
1173                 if (id == s->id)
1174                         return s;
1175         }
1176
1177         return NULL;
1178 }
1179
1180
1181 void fst_session_enum(struct fst_group *g, fst_session_enum_clb clb, void *ctx)
1182 {
1183         struct fst_session *s;
1184
1185         foreach_fst_session(s) {
1186                 if (!g || s->group == g)
1187                         clb(s->group, s, ctx);
1188         }
1189 }
1190
1191
1192 void fst_session_on_action_rx(struct fst_iface *iface,
1193                               const struct ieee80211_mgmt *mgmt,
1194                               size_t len)
1195 {
1196         struct fst_session *s;
1197
1198         if (len < IEEE80211_HDRLEN + 2 ||
1199             mgmt->u.action.category != WLAN_ACTION_FST) {
1200                 fst_printf_iface(iface, MSG_ERROR,
1201                                  "invalid Action frame received");
1202                 return;
1203         }
1204
1205         if (mgmt->u.action.u.fst_action.action <= FST_ACTION_MAX_SUPPORTED) {
1206                 fst_printf_iface(iface, MSG_DEBUG,
1207                                  "FST Action '%s' received!",
1208                                  fst_action_names[mgmt->u.action.u.fst_action.action]);
1209         } else {
1210                 fst_printf_iface(iface, MSG_WARNING,
1211                                  "unknown FST Action (%u) received!",
1212                                  mgmt->u.action.u.fst_action.action);
1213                 return;
1214         }
1215
1216         if (mgmt->u.action.u.fst_action.action == FST_ACTION_SETUP_REQUEST) {
1217                 fst_session_handle_setup_request(iface, mgmt, len);
1218                 return;
1219         }
1220
1221         s = fst_find_session_in_progress(mgmt->sa, fst_iface_get_group(iface));
1222         if (s) {
1223                 fst_session_handle_action(s, iface, mgmt, len);
1224         } else {
1225                 fst_printf_iface(iface, MSG_WARNING,
1226                                  "FST Action '%s' dropped: no session in progress found",
1227                                  fst_action_names[mgmt->u.action.u.fst_action.action]);
1228         }
1229 }
1230
1231
1232 int fst_session_set_str_ifname(struct fst_session *s, const char *ifname,
1233                                Boolean is_old)
1234 {
1235         struct fst_group *g = fst_session_get_group(s);
1236         struct fst_iface *i;
1237
1238         i = fst_group_get_iface_by_name(g, ifname);
1239         if (!i) {
1240                 fst_printf_session(s, MSG_WARNING,
1241                                    "Cannot set iface %s: no such iface within group '%s'",
1242                                    ifname, fst_group_get_id(g));
1243                 return -1;
1244         }
1245
1246         fst_session_set_iface(s, i, is_old);
1247
1248         return 0;
1249 }
1250
1251
1252 int fst_session_set_str_peer_addr(struct fst_session *s, const char *mac,
1253                                   Boolean is_old)
1254 {
1255         u8 peer_addr[ETH_ALEN];
1256         int res = fst_read_peer_addr(mac, peer_addr);
1257
1258         if (res)
1259                 return res;
1260
1261         fst_session_set_peer_addr(s, peer_addr, is_old);
1262
1263         return 0;
1264 }
1265
1266
1267 int fst_session_set_str_llt(struct fst_session *s, const char *llt_str)
1268 {
1269         char *endp;
1270         long int llt = strtol(llt_str, &endp, 0);
1271
1272         if (*endp || llt < 0 || (unsigned long int) llt > FST_MAX_LLT_MS) {
1273                 fst_printf_session(s, MSG_WARNING,
1274                                    "Cannot set llt %s: Invalid llt value (1..%u expected)",
1275                                    llt_str, FST_MAX_LLT_MS);
1276                 return -1;
1277         }
1278         fst_session_set_llt(s, (u32) llt);
1279
1280         return 0;
1281 }
1282
1283
1284 void fst_session_global_on_iface_detached(struct fst_iface *iface)
1285 {
1286         struct fst_session *s;
1287
1288         foreach_fst_session(s) {
1289                 if (fst_session_is_in_progress(s) &&
1290                     (s->data.new_iface == iface ||
1291                      s->data.old_iface == iface))
1292                         fst_session_reset_ex(s, REASON_DETACH_IFACE);
1293         }
1294 }
1295
1296
1297 struct fst_session * fst_session_global_get_first_by_group(struct fst_group *g)
1298 {
1299         struct fst_session *s;
1300
1301         foreach_fst_session(s) {
1302                 if (s->group == g)
1303                         return s;
1304         }
1305
1306         return NULL;
1307 }
1308
1309
1310 #ifdef CONFIG_FST_TEST
1311
1312 static int get_group_fill_session(struct fst_group **g, struct fst_session *s)
1313 {
1314         const u8 *old_addr, *new_addr;
1315         struct fst_get_peer_ctx *ctx;
1316
1317         os_memset(s, 0, sizeof(*s));
1318         *g = dl_list_first(&fst_global_groups_list,
1319                            struct fst_group, global_groups_lentry);
1320         if (!*g)
1321                 return EINVAL;
1322
1323         s->data.new_iface = dl_list_first(&(*g)->ifaces, struct fst_iface,
1324                                           group_lentry);
1325         if (!s->data.new_iface)
1326                 return EINVAL;
1327
1328         s->data.old_iface = dl_list_entry(s->data.new_iface->group_lentry.next,
1329                                           struct fst_iface, group_lentry);
1330         if (!s->data.old_iface)
1331                 return EINVAL;
1332
1333         old_addr = fst_iface_get_peer_first(s->data.old_iface, &ctx, TRUE);
1334         if (!old_addr)
1335                 return EINVAL;
1336
1337         new_addr = fst_iface_get_peer_first(s->data.new_iface, &ctx, TRUE);
1338         if (!new_addr)
1339                 return EINVAL;
1340
1341         os_memcpy(s->data.old_peer_addr, old_addr, ETH_ALEN);
1342         os_memcpy(s->data.new_peer_addr, new_addr, ETH_ALEN);
1343
1344         return 0;
1345 }
1346
1347
1348 #define FST_MAX_COMMAND_WORD_NAME_LENGTH 16
1349
1350 int fst_test_req_send_fst_request(const char *params)
1351 {
1352         int fsts_id;
1353         Boolean is_valid;
1354         char *endp;
1355         struct fst_setup_req req;
1356         struct fst_session s;
1357         struct fst_group *g;
1358         enum hostapd_hw_mode hw_mode;
1359         u8 channel;
1360         char additional_param[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1361
1362         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1363         if (!is_valid)
1364                 return EINVAL;
1365
1366         if (get_group_fill_session(&g, &s))
1367                 return EINVAL;
1368
1369         req.action = FST_ACTION_SETUP_REQUEST;
1370         req.dialog_token = g->dialog_token;
1371         req.llt = host_to_le32(FST_LLT_MS_DEFAULT);
1372         /* 8.4.2.147 Session Transition element */
1373         req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1374         req.stie.length = sizeof(req.stie);
1375         req.stie.fsts_id = host_to_le32(fsts_id);
1376         req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1377
1378         fst_iface_get_channel_info(s.data.new_iface, &hw_mode, &channel);
1379         req.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1380         req.stie.new_band_op = 1;
1381         req.stie.new_band_setup = 0;
1382
1383         fst_iface_get_channel_info(s.data.old_iface, &hw_mode, &channel);
1384         req.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1385         req.stie.old_band_op = 1;
1386         req.stie.old_band_setup = 0;
1387
1388         if (!fst_read_next_text_param(endp, additional_param,
1389                                        sizeof(additional_param), &endp)) {
1390                 if (!os_strcasecmp(additional_param, FST_CTR_PVAL_BAD_NEW_BAND))
1391                         req.stie.new_band_id = req.stie.old_band_id;
1392         }
1393
1394         return fst_session_send_action(&s, TRUE, &req, sizeof(req),
1395                                        s.data.old_iface->mb_ie);
1396 }
1397
1398
1399 int fst_test_req_send_fst_response(const char *params)
1400 {
1401         int fsts_id;
1402         Boolean is_valid;
1403         char *endp;
1404         struct fst_setup_res res;
1405         struct fst_session s;
1406         struct fst_group *g;
1407         enum hostapd_hw_mode hw_mode;
1408         u8 status_code;
1409         u8 channel;
1410         char response[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1411         struct fst_session *_s;
1412
1413         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1414         if (!is_valid)
1415                 return EINVAL;
1416
1417         if (get_group_fill_session(&g, &s))
1418                 return EINVAL;
1419
1420         status_code = WLAN_STATUS_SUCCESS;
1421         if (!fst_read_next_text_param(endp, response, sizeof(response),
1422                                       &endp)) {
1423                 if (!os_strcasecmp(response, FST_CS_PVAL_RESPONSE_REJECT))
1424                         status_code = WLAN_STATUS_PENDING_ADMITTING_FST_SESSION;
1425         }
1426
1427         os_memset(&res, 0, sizeof(res));
1428
1429         res.action = FST_ACTION_SETUP_RESPONSE;
1430         /*
1431          * If some session has just received an FST Setup Request, then
1432          * use the correct dialog token copied from this request.
1433          */
1434         _s = fst_find_session_in_progress(fst_session_get_peer_addr(&s, TRUE),
1435                                           g);
1436         res.dialog_token = (_s && fst_session_is_ready_pending(_s)) ?
1437                 _s->data.pending_setup_req_dlgt : g->dialog_token;
1438         res.status_code  = status_code;
1439
1440         if (res.status_code == WLAN_STATUS_SUCCESS) {
1441                 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1442                 res.stie.length = sizeof(res.stie);
1443                 res.stie.fsts_id = fsts_id;
1444                 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1445
1446                 fst_iface_get_channel_info(s.data.new_iface, &hw_mode,
1447                                             &channel);
1448                 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1449                 res.stie.new_band_op = 1;
1450                 res.stie.new_band_setup = 0;
1451
1452                 fst_iface_get_channel_info(s.data.old_iface, &hw_mode,
1453                                            &channel);
1454                 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1455                 res.stie.old_band_op = 1;
1456                 res.stie.old_band_setup = 0;
1457         }
1458
1459         if (!fst_read_next_text_param(endp, response, sizeof(response),
1460                                       &endp)) {
1461                 if (!os_strcasecmp(response, FST_CTR_PVAL_BAD_NEW_BAND))
1462                         res.stie.new_band_id = res.stie.old_band_id;
1463         }
1464
1465         return fst_session_send_action(&s, TRUE, &res, sizeof(res),
1466                                        s.data.old_iface->mb_ie);
1467 }
1468
1469
1470 int fst_test_req_send_ack_request(const char *params)
1471 {
1472         int fsts_id;
1473         Boolean is_valid;
1474         char *endp;
1475         struct fst_ack_req req;
1476         struct fst_session s;
1477         struct fst_group *g;
1478
1479         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1480         if (!is_valid)
1481                 return EINVAL;
1482
1483         if (get_group_fill_session(&g, &s))
1484                 return EINVAL;
1485
1486         os_memset(&req, 0, sizeof(req));
1487         req.action = FST_ACTION_ACK_REQUEST;
1488         req.dialog_token = g->dialog_token;
1489         req.fsts_id = fsts_id;
1490
1491         return fst_session_send_action(&s, FALSE, &req, sizeof(req), NULL);
1492 }
1493
1494
1495 int fst_test_req_send_ack_response(const char *params)
1496 {
1497         int fsts_id;
1498         Boolean is_valid;
1499         char *endp;
1500         struct fst_ack_res res;
1501         struct fst_session s;
1502         struct fst_group *g;
1503
1504         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1505         if (!is_valid)
1506                 return EINVAL;
1507
1508         if (get_group_fill_session(&g, &s))
1509                 return EINVAL;
1510
1511         os_memset(&res, 0, sizeof(res));
1512         res.action = FST_ACTION_ACK_RESPONSE;
1513         res.dialog_token = g->dialog_token;
1514         res.fsts_id = fsts_id;
1515
1516         return fst_session_send_action(&s, FALSE, &res, sizeof(res), NULL);
1517 }
1518
1519
1520 int fst_test_req_send_tear_down(const char *params)
1521 {
1522         int fsts_id;
1523         Boolean is_valid;
1524         char *endp;
1525         struct fst_tear_down td;
1526         struct fst_session s;
1527         struct fst_group *g;
1528
1529         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1530         if (!is_valid)
1531                 return EINVAL;
1532
1533         if (get_group_fill_session(&g, &s))
1534                 return EINVAL;
1535
1536         os_memset(&td, 0, sizeof(td));
1537         td.action = FST_ACTION_TEAR_DOWN;
1538         td.fsts_id = fsts_id;
1539
1540         return fst_session_send_action(&s, TRUE, &td, sizeof(td), NULL);
1541 }
1542
1543
1544 u32 fst_test_req_get_fsts_id(const char *params)
1545 {
1546         int sid;
1547         Boolean is_valid;
1548         char *endp;
1549         struct fst_session *s;
1550
1551         sid = fst_read_next_int_param(params, &is_valid, &endp);
1552         if (!is_valid)
1553                 return FST_FSTS_ID_NOT_FOUND;
1554
1555         s = fst_session_get_by_id(sid);
1556         if (!s)
1557                 return FST_FSTS_ID_NOT_FOUND;
1558
1559         return s->data.fsts_id;
1560 }
1561
1562
1563 int fst_test_req_get_local_mbies(const char *request, char *buf, size_t buflen)
1564 {
1565         char *endp;
1566         char ifname[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1567         struct fst_group *g;
1568         struct fst_iface *iface;
1569         int ret;
1570
1571         if (fst_read_next_text_param(request, ifname, sizeof(ifname), &endp) ||
1572             !*ifname)
1573                 goto problem;
1574         g = dl_list_first(&fst_global_groups_list, struct fst_group,
1575                           global_groups_lentry);
1576         if (!g)
1577                 goto problem;
1578         iface = fst_group_get_iface_by_name(g, ifname);
1579         if (!iface || !iface->mb_ie)
1580                 goto problem;
1581         ret = print_mb_ies(iface->mb_ie, buf, buflen);
1582         if ((size_t) ret != wpabuf_len(iface->mb_ie) * 2)
1583                 fst_printf(MSG_WARNING, "MB IEs copied only partially");
1584         return ret;
1585
1586 problem:
1587         return os_snprintf(buf, buflen, "FAIL\n");
1588 }
1589
1590 #endif /* CONFIG_FST_TEST */