FST: Fix handling of Rx FST Setup Request when session already exists
[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         struct fst_iface *new_iface = NULL;
364         struct fst_group *g;
365         u8 new_iface_peer_addr[ETH_ALEN];
366         const struct wpabuf *peer_mbies;
367         size_t plen;
368
369         if (frame_len < IEEE80211_HDRLEN + 1 + sizeof(*req))  {
370                 fst_printf_iface(iface, MSG_WARNING,
371                                  "FST Request dropped: too short (%zu < %zu)",
372                                  frame_len,
373                                  IEEE80211_HDRLEN + 1 + sizeof(*req));
374                 return;
375         }
376         plen = frame_len - IEEE80211_HDRLEN - 1;
377         req = (const struct fst_setup_req *)
378                 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
379         if (req->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
380             req->stie.length < 11) {
381                 fst_printf_iface(iface, MSG_WARNING,
382                                  "FST Request dropped: invalid STIE");
383                 return;
384         }
385
386         if (req->stie.new_band_id == req->stie.old_band_id) {
387                 fst_printf_iface(iface, MSG_WARNING,
388                                  "FST Request dropped: new and old band IDs are the same");
389                 return;
390         }
391
392         g = fst_iface_get_group(iface);
393
394         if (plen > sizeof(*req)) {
395                 fst_iface_update_mb_ie(iface, mgmt->sa, (const u8 *) (req + 1),
396                                        plen - sizeof(*req));
397                 fst_printf_iface(iface, MSG_INFO,
398                                  "FST Request: MB IEs updated for " MACSTR,
399                                  MAC2STR(mgmt->sa));
400         }
401
402         peer_mbies = fst_iface_get_peer_mb_ie(iface, mgmt->sa);
403         if (peer_mbies) {
404                 new_iface = fst_group_get_new_iface_by_stie_and_mbie(
405                         g, wpabuf_head(peer_mbies), wpabuf_len(peer_mbies),
406                         &req->stie, new_iface_peer_addr);
407                 if (new_iface)
408                         fst_printf_iface(iface, MSG_INFO,
409                                          "FST Request: new iface (%s:" MACSTR
410                                          ") found by MB IEs",
411                                          fst_iface_get_name(new_iface),
412                                          MAC2STR(new_iface_peer_addr));
413         }
414
415         if (!new_iface) {
416                 new_iface = fst_group_find_new_iface_by_stie(
417                         g, iface, mgmt->sa, &req->stie,
418                         new_iface_peer_addr);
419                 if (new_iface)
420                         fst_printf_iface(iface, MSG_INFO,
421                                          "FST Request: new iface (%s:" MACSTR
422                                          ") found by others",
423                                          fst_iface_get_name(new_iface),
424                                          MAC2STR(new_iface_peer_addr));
425         }
426
427         if (!new_iface) {
428                 fst_printf_iface(iface, MSG_WARNING,
429                                  "FST Request dropped: new iface not found");
430                 return;
431         }
432
433         s = fst_find_session_in_progress(mgmt->sa, g);
434         if (s) {
435                 union fst_session_state_switch_extra evext = {
436                         .to_initial = {
437                                 .reason = REASON_SETUP,
438                         },
439                 };
440
441                 /*
442                  * 10.32.2.2  Transitioning between states:
443                  * Upon receipt of an FST Setup Request frame, the responder
444                  * shall respond with an FST Setup Response frame unless it has
445                  * a pending FST Setup Request frame addressed to the initiator
446                  * and the responder has a numerically larger MAC address than
447                  * the initiator’s MAC address, in which case, the responder
448                  * shall delete the received FST Setup Request.
449                  */
450                 if (fst_session_is_ready_pending(s) &&
451                     /* waiting for Setup Response */
452                     os_memcmp(mgmt->da, mgmt->sa, ETH_ALEN) > 0) {
453                         fst_printf_session(s, MSG_WARNING,
454                                            "FST Request dropped due to MAC comparison (our MAC is "
455                                            MACSTR ")",
456                                            MAC2STR(mgmt->da));
457                         return;
458                 }
459
460                 /*
461                  * State is SETUP_COMPLETION (either in transition or not) or
462                  * TRANSITION_DONE (in transition).
463                  * Setup Request arriving in this state could mean:
464                  * 1. peer sent it before receiving our Setup Request (race
465                  *    condition)
466                  * 2. peer didn't receive our Setup Response. Peer is retrying
467                  *    after STT timeout
468                  * 3. peer's FST state machines are out of sync due to some
469                  *    other reason
470                  *
471                  * We will reset our session and create a new one instead.
472                  */
473
474                 fst_printf_session(s, MSG_WARNING,
475                         "resetting due to FST request");
476
477                 /*
478                  * If FST Setup Request arrived with the same FSTS ID as one we
479                  * initialized before, there's no need to tear down the session.
480                  * Moreover, as FSTS ID is the same, the other side will
481                  * associate this tear down with the session it initiated that
482                  * will break the sync.
483                  */
484                 if (le_to_host32(req->stie.fsts_id) != s->data.fsts_id)
485                         fst_session_send_tear_down(s);
486                 else
487                         fst_printf_session(s, MSG_WARNING,
488                                            "Skipping TearDown as the FST request has the same FSTS ID as initiated");
489                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
490                 fst_session_stt_disarm(s);
491         }
492
493         s = fst_session_create(g);
494         if (!s) {
495                 fst_printf(MSG_WARNING,
496                            "FST Request dropped: cannot create session for %s and %s",
497                            fst_iface_get_name(iface),
498                            fst_iface_get_name(new_iface));
499                 return;
500         }
501
502         fst_session_set_iface(s, iface, TRUE);
503         fst_session_set_peer_addr(s, mgmt->sa, TRUE);
504         fst_session_set_iface(s, new_iface, FALSE);
505         fst_session_set_peer_addr(s, new_iface_peer_addr, FALSE);
506         fst_session_set_llt(s, FST_LLT_VAL_TO_MS(le_to_host32(req->llt)));
507         s->data.pending_setup_req_dlgt = req->dialog_token;
508         s->data.fsts_id = le_to_host32(req->stie.fsts_id);
509
510         fst_session_stt_arm(s);
511
512         fst_session_notify_ctrl(s, EVENT_FST_SETUP, NULL);
513
514         fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION, NULL);
515 }
516
517
518 static void fst_session_handle_setup_response(struct fst_session *s,
519                                               struct fst_iface *iface,
520                                               const struct ieee80211_mgmt *mgmt,
521                                               size_t frame_len)
522 {
523         const struct fst_setup_res *res;
524         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
525         enum hostapd_hw_mode hw_mode;
526         u8 channel;
527         union fst_session_state_switch_extra evext = {
528                 .to_initial = {0},
529         };
530
531         if (iface != s->data.old_iface) {
532                 fst_printf_session(s, MSG_WARNING,
533                                    "FST Response dropped: %s is not the old iface",
534                                    fst_iface_get_name(iface));
535                 return;
536         }
537
538         if (!fst_session_is_ready_pending(s)) {
539                 fst_printf_session(s, MSG_WARNING,
540                                    "FST Response dropped due to wrong state: %s",
541                                    fst_session_state_name(s->state));
542                 return;
543         }
544
545         if (plen < sizeof(*res)) {
546                 fst_printf_session(s, MSG_WARNING,
547                                    "Too short FST Response dropped");
548                 return;
549         }
550         res = (const struct fst_setup_res *)
551                 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
552         if (res->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
553             res->stie.length < 11) {
554                 fst_printf_iface(iface, MSG_WARNING,
555                                  "FST Response dropped: invalid STIE");
556                 return;
557         }
558
559         if (res->dialog_token != s->data.pending_setup_req_dlgt)  {
560                 fst_printf_session(s, MSG_WARNING,
561                                    "FST Response dropped due to wrong dialog token (%u != %u)",
562                                    s->data.pending_setup_req_dlgt,
563                                    res->dialog_token);
564                 return;
565         }
566
567         if (res->status_code == WLAN_STATUS_SUCCESS &&
568             le_to_host32(res->stie.fsts_id) != s->data.fsts_id) {
569                 fst_printf_session(s, MSG_WARNING,
570                                    "FST Response dropped due to wrong FST Session ID (%u)",
571                                    le_to_host32(res->stie.fsts_id));
572                 return;
573         }
574
575         fst_session_stt_disarm(s);
576
577         if (res->status_code != WLAN_STATUS_SUCCESS) {
578                 /*
579                  * 10.32.2.2  Transitioning between states
580                  * The initiator shall set the STT to the value of the
581                  * FSTSessionTimeOut field at ... and at each ACK frame sent in
582                  * response to a received FST Setup Response with the Status
583                  * Code field equal to PENDING_ADMITTING_FST_SESSION or
584                  * PENDING_GAP_IN_BA_WINDOW.
585                  */
586                 evext.to_initial.reason = REASON_REJECT;
587                 evext.to_initial.reject_code = res->status_code;
588                 evext.to_initial.initiator = FST_INITIATOR_REMOTE;
589                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
590                 fst_printf_session(s, MSG_WARNING,
591                                    "FST Setup rejected by remote side with status %u",
592                                    res->status_code);
593                 return;
594         }
595
596         fst_iface_get_channel_info(s->data.new_iface, &hw_mode, &channel);
597
598         if (fst_hw_mode_to_band(hw_mode) != res->stie.new_band_id) {
599                 evext.to_initial.reason = REASON_ERROR_PARAMS;
600                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
601                 fst_printf_session(s, MSG_WARNING,
602                                    "invalid FST Setup parameters");
603                 fst_session_tear_down_setup(s);
604                 return;
605         }
606
607         fst_printf_session(s, MSG_INFO,
608                            "%s: FST Setup established for %s (llt=%u)",
609                            fst_iface_get_name(s->data.old_iface),
610                            fst_iface_get_name(s->data.new_iface),
611                            s->data.llt_ms);
612
613         fst_session_notify_ctrl(s, EVENT_FST_ESTABLISHED, NULL);
614
615         if (s->data.llt_ms == FST_LLT_SWITCH_IMMEDIATELY)
616                 fst_session_initiate_switch(s);
617 }
618
619
620 static void fst_session_handle_tear_down(struct fst_session *s,
621                                          struct fst_iface *iface,
622                                          const struct ieee80211_mgmt *mgmt,
623                                          size_t frame_len)
624 {
625         const struct fst_tear_down *td;
626         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
627         union fst_session_state_switch_extra evext = {
628                 .to_initial = {
629                         .reason = REASON_TEARDOWN,
630                         .initiator = FST_INITIATOR_REMOTE,
631                 },
632         };
633
634         if (plen < sizeof(*td)) {
635                 fst_printf_session(s, MSG_WARNING,
636                                    "Too short FST Tear Down dropped");
637                 return;
638         }
639         td = (const struct fst_tear_down *)
640                 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
641
642         if (le_to_host32(td->fsts_id) != s->data.fsts_id) {
643                 fst_printf_siface(s, iface, MSG_WARNING,
644                                   "tear down for wrong FST Setup ID (%u)",
645                                   le_to_host32(td->fsts_id));
646                 return;
647         }
648
649         fst_session_stt_disarm(s);
650
651         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
652 }
653
654
655 static void fst_session_handle_ack_request(struct fst_session *s,
656                                            struct fst_iface *iface,
657                                            const struct ieee80211_mgmt *mgmt,
658                                            size_t frame_len)
659 {
660         const struct fst_ack_req *req;
661         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
662         struct fst_ack_res res;
663         union fst_session_state_switch_extra evext = {
664                 .to_initial = {
665                         .reason = REASON_SWITCH,
666                         .initiator = FST_INITIATOR_REMOTE,
667                 },
668         };
669
670         if (!fst_session_is_ready(s) && !fst_session_is_switch_requested(s)) {
671                 fst_printf_siface(s, iface, MSG_ERROR,
672                                   "cannot initiate switch due to wrong session state (%s)",
673                                   fst_session_state_name(s->state));
674                 return;
675         }
676
677         WPA_ASSERT(s->data.new_iface != NULL);
678
679         if (iface != s->data.new_iface) {
680                 fst_printf_siface(s, iface, MSG_ERROR,
681                                   "Ack received on wrong interface");
682                 return;
683         }
684
685         if (plen < sizeof(*req)) {
686                 fst_printf_session(s, MSG_WARNING,
687                                    "Too short FST Ack Request dropped");
688                 return;
689         }
690         req = (const struct fst_ack_req *)
691                 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
692
693         if (le_to_host32(req->fsts_id) != s->data.fsts_id) {
694                 fst_printf_siface(s, iface, MSG_WARNING,
695                                   "Ack for wrong FST Setup ID (%u)",
696                                   le_to_host32(req->fsts_id));
697                 return;
698         }
699
700         os_memset(&res, 0, sizeof(res));
701
702         res.action = FST_ACTION_ACK_RESPONSE;
703         res.dialog_token = req->dialog_token;
704         res.fsts_id = req->fsts_id;
705
706         if (!fst_session_send_action(s, FALSE, &res, sizeof(res), NULL)) {
707                 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Response sent");
708                 fst_session_stt_disarm(s);
709                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
710                                       NULL);
711                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED,
712                                       NULL);
713                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
714         }
715 }
716
717
718 static void
719 fst_session_handle_ack_response(struct fst_session *s,
720                                 struct fst_iface *iface,
721                                 const struct ieee80211_mgmt *mgmt,
722                                 size_t frame_len)
723 {
724         const struct fst_ack_res *res;
725         size_t plen = frame_len - IEEE80211_HDRLEN - 1;
726         union fst_session_state_switch_extra evext = {
727                 .to_initial = {
728                         .reason = REASON_SWITCH,
729                         .initiator = FST_INITIATOR_LOCAL,
730                 },
731         };
732
733         if (!fst_session_is_switch_requested(s)) {
734                 fst_printf_siface(s, iface, MSG_ERROR,
735                                   "Ack Response in inappropriate session state (%s)",
736                                   fst_session_state_name(s->state));
737                 return;
738         }
739
740         WPA_ASSERT(s->data.new_iface != NULL);
741
742         if (iface != s->data.new_iface) {
743                 fst_printf_siface(s, iface, MSG_ERROR,
744                                   "Ack response received on wrong interface");
745                 return;
746         }
747
748         if (plen < sizeof(*res)) {
749                 fst_printf_session(s, MSG_WARNING,
750                                    "Too short FST Ack Response dropped");
751                 return;
752         }
753         res = (const struct fst_ack_res *)
754                 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
755
756         if (le_to_host32(res->fsts_id) != s->data.fsts_id) {
757                 fst_printf_siface(s, iface, MSG_ERROR,
758                                   "Ack response for wrong FST Setup ID (%u)",
759                                   le_to_host32(res->fsts_id));
760                 return;
761         }
762
763         fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED, NULL);
764         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
765
766         fst_session_stt_disarm(s);
767 }
768
769
770 struct fst_session * fst_session_create(struct fst_group *g)
771 {
772         struct fst_session *s;
773         u32 id;
774
775         WPA_ASSERT(!is_zero_ether_addr(own_addr));
776
777         id = fst_find_free_session_id();
778         if (id == FST_INVALID_SESSION_ID) {
779                 fst_printf(MSG_ERROR, "Cannot assign new session ID");
780                 return NULL;
781         }
782
783         s = os_zalloc(sizeof(*s));
784         if (!s) {
785                 fst_printf(MSG_ERROR, "Cannot allocate new session object");
786                 return NULL;
787         }
788
789         s->id = id;
790         s->group = g;
791         s->state = FST_SESSION_STATE_INITIAL;
792
793         s->data.llt_ms = FST_LLT_MS_DEFAULT;
794
795         fst_printf(MSG_INFO, "Session %u created", s->id);
796
797         dl_list_add_tail(&global_sessions_list, &s->global_sessions_lentry);
798
799         foreach_fst_ctrl_call(on_session_added, s);
800
801         return s;
802 }
803
804
805 void fst_session_set_iface(struct fst_session *s, struct fst_iface *iface,
806                            Boolean is_old)
807 {
808         if (is_old)
809                 s->data.old_iface = iface;
810         else
811                 s->data.new_iface = iface;
812
813 }
814
815
816 void fst_session_set_llt(struct fst_session *s, u32 llt)
817 {
818         s->data.llt_ms = llt;
819 }
820
821
822 void fst_session_set_peer_addr(struct fst_session *s, const u8 *addr,
823                                Boolean is_old)
824 {
825         u8 *a = is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
826
827         os_memcpy(a, addr, ETH_ALEN);
828 }
829
830
831 int fst_session_initiate_setup(struct fst_session *s)
832 {
833         struct fst_setup_req req;
834         int res;
835         u32 fsts_id;
836         u8 dialog_token;
837         struct fst_session *_s;
838
839         if (fst_session_is_in_progress(s)) {
840                 fst_printf_session(s, MSG_ERROR, "Session in progress");
841                 return -EINVAL;
842         }
843
844         if (is_zero_ether_addr(s->data.old_peer_addr)) {
845                 fst_printf_session(s, MSG_ERROR, "No old peer MAC address");
846                 return -EINVAL;
847         }
848
849         if (is_zero_ether_addr(s->data.new_peer_addr)) {
850                 fst_printf_session(s, MSG_ERROR, "No new peer MAC address");
851                 return -EINVAL;
852         }
853
854         if (!s->data.old_iface) {
855                 fst_printf_session(s, MSG_ERROR, "No old interface defined");
856                 return -EINVAL;
857         }
858
859         if (!s->data.new_iface) {
860                 fst_printf_session(s, MSG_ERROR, "No new interface defined");
861                 return -EINVAL;
862         }
863
864         if (s->data.new_iface == s->data.old_iface) {
865                 fst_printf_session(s, MSG_ERROR,
866                                    "Same interface set as old and new");
867                 return -EINVAL;
868         }
869
870         if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr,
871                                     FALSE)) {
872                 fst_printf_session(s, MSG_ERROR,
873                                    "The preset old peer address is not connected");
874                 return -EINVAL;
875         }
876
877         if (!fst_iface_is_connected(s->data.new_iface, s->data.new_peer_addr,
878                                     FALSE)) {
879                 fst_printf_session(s, MSG_ERROR,
880                                    "The preset new peer address is not connected");
881                 return -EINVAL;
882         }
883
884         _s = fst_find_session_in_progress(s->data.old_peer_addr, s->group);
885         if (_s) {
886                 fst_printf_session(s, MSG_ERROR,
887                                    "There is another session in progress (old): %u",
888                                    _s->id);
889                 return -EINVAL;
890         }
891
892         _s = fst_find_session_in_progress(s->data.new_peer_addr, s->group);
893         if (_s) {
894                 fst_printf_session(s, MSG_ERROR,
895                                    "There is another session in progress (new): %u",
896                                    _s->id);
897                 return -EINVAL;
898         }
899
900         dialog_token = fst_group_assign_dialog_token(s->group);
901         fsts_id = fst_group_assign_fsts_id(s->group);
902
903         os_memset(&req, 0, sizeof(req));
904
905         fst_printf_siface(s, s->data.old_iface, MSG_INFO,
906                 "initiating FST setup for %s (llt=%u ms)",
907                 fst_iface_get_name(s->data.new_iface), s->data.llt_ms);
908
909         req.action = FST_ACTION_SETUP_REQUEST;
910         req.dialog_token = dialog_token;
911         req.llt = host_to_le32(FST_LLT_MS_TO_VAL(s->data.llt_ms));
912         /* 8.4.2.147 Session Transition element */
913         req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
914         req.stie.length = sizeof(req.stie) - 2;
915         req.stie.fsts_id = host_to_le32(fsts_id);
916         req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
917
918         req.stie.new_band_id = fst_iface_get_band_id(s->data.new_iface);
919         req.stie.new_band_op = 1;
920         req.stie.new_band_setup = 0;
921
922         req.stie.old_band_id = fst_iface_get_band_id(s->data.old_iface);
923         req.stie.old_band_op = 1;
924         req.stie.old_band_setup = 0;
925
926         res = fst_session_send_action(s, TRUE, &req, sizeof(req),
927                                       fst_iface_get_mbie(s->data.old_iface));
928         if (!res) {
929                 s->data.fsts_id = fsts_id;
930                 s->data.pending_setup_req_dlgt = dialog_token;
931                 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Request sent");
932                 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION,
933                                       NULL);
934
935                 fst_session_stt_arm(s);
936         }
937
938         return res;
939 }
940
941
942 int fst_session_respond(struct fst_session *s, u8 status_code)
943 {
944         struct fst_setup_res res;
945         enum hostapd_hw_mode hw_mode;
946         u8 channel;
947
948         if (!fst_session_is_ready_pending(s)) {
949                 fst_printf_session(s, MSG_ERROR, "incorrect state: %s",
950                                    fst_session_state_name(s->state));
951                 return -EINVAL;
952         }
953
954         if (is_zero_ether_addr(s->data.old_peer_addr)) {
955                 fst_printf_session(s, MSG_ERROR, "No peer MAC address");
956                 return -EINVAL;
957         }
958
959         if (!s->data.old_iface) {
960                 fst_printf_session(s, MSG_ERROR, "No old interface defined");
961                 return -EINVAL;
962         }
963
964         if (!s->data.new_iface) {
965                 fst_printf_session(s, MSG_ERROR, "No new interface defined");
966                 return -EINVAL;
967         }
968
969         if (s->data.new_iface == s->data.old_iface) {
970                 fst_printf_session(s, MSG_ERROR,
971                                    "Same interface set as old and new");
972                 return -EINVAL;
973         }
974
975         if (!fst_iface_is_connected(s->data.old_iface,
976                                     s->data.old_peer_addr, FALSE)) {
977                 fst_printf_session(s, MSG_ERROR,
978                                    "The preset peer address is not in the peer list");
979                 return -EINVAL;
980         }
981
982         fst_session_stt_disarm(s);
983
984         os_memset(&res, 0, sizeof(res));
985
986         res.action = FST_ACTION_SETUP_RESPONSE;
987         res.dialog_token = s->data.pending_setup_req_dlgt;
988         res.status_code = status_code;
989
990         res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
991         res.stie.length = sizeof(res.stie) - 2;
992
993         if (status_code == WLAN_STATUS_SUCCESS) {
994                 res.stie.fsts_id = s->data.fsts_id;
995                 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
996
997                 fst_iface_get_channel_info(s->data.new_iface, &hw_mode,
998                                            &channel);
999                 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1000                 res.stie.new_band_op = 1;
1001                 res.stie.new_band_setup = 0;
1002
1003                 fst_iface_get_channel_info(s->data.old_iface, &hw_mode,
1004                                            &channel);
1005                 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1006                 res.stie.old_band_op = 1;
1007                 res.stie.old_band_setup = 0;
1008
1009                 fst_printf_session(s, MSG_INFO,
1010                                    "%s: FST Setup Request accepted for %s (llt=%u)",
1011                                    fst_iface_get_name(s->data.old_iface),
1012                                    fst_iface_get_name(s->data.new_iface),
1013                                    s->data.llt_ms);
1014         } else {
1015                 fst_printf_session(s, MSG_WARNING,
1016                                    "%s: FST Setup Request rejected with code %d",
1017                                    fst_iface_get_name(s->data.old_iface),
1018                                    status_code);
1019         }
1020
1021         if (fst_session_send_action(s, TRUE, &res, sizeof(res),
1022                                     fst_iface_get_mbie(s->data.old_iface))) {
1023                 fst_printf_sframe(s, TRUE, MSG_ERROR,
1024                                   "cannot send FST Setup Response with code %d",
1025                                   status_code);
1026                 return -EINVAL;
1027         }
1028
1029         fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Response sent");
1030
1031         if (status_code != WLAN_STATUS_SUCCESS) {
1032                 union fst_session_state_switch_extra evext = {
1033                         .to_initial = {
1034                                 .reason = REASON_REJECT,
1035                                 .reject_code = status_code,
1036                                 .initiator = FST_INITIATOR_LOCAL,
1037                         },
1038                 };
1039                 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1040         }
1041
1042         return 0;
1043 }
1044
1045
1046 int fst_session_initiate_switch(struct fst_session *s)
1047 {
1048         struct fst_ack_req req;
1049         int res;
1050         u8 dialog_token;
1051
1052         if (!fst_session_is_ready(s)) {
1053                 fst_printf_session(s, MSG_ERROR,
1054                                    "cannot initiate switch due to wrong setup state (%d)",
1055                                    s->state);
1056                 return -1;
1057         }
1058
1059         dialog_token = fst_group_assign_dialog_token(s->group);
1060
1061         WPA_ASSERT(s->data.new_iface != NULL);
1062         WPA_ASSERT(s->data.old_iface != NULL);
1063
1064         fst_printf_session(s, MSG_INFO, "initiating FST switch: %s => %s",
1065                            fst_iface_get_name(s->data.old_iface),
1066                            fst_iface_get_name(s->data.new_iface));
1067
1068         os_memset(&req, 0, sizeof(req));
1069
1070         req.action = FST_ACTION_ACK_REQUEST;
1071         req.dialog_token = dialog_token;
1072         req.fsts_id = host_to_le32(s->data.fsts_id);
1073
1074         res = fst_session_send_action(s, FALSE, &req, sizeof(req), NULL);
1075         if (!res) {
1076                 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Request sent");
1077                 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
1078                                       NULL);
1079                 fst_session_stt_arm(s);
1080         } else {
1081                 fst_printf_sframe(s, FALSE, MSG_ERROR,
1082                                   "Cannot send FST Ack Request");
1083         }
1084
1085         return res;
1086 }
1087
1088
1089 void fst_session_handle_action(struct fst_session *s,
1090                                struct fst_iface *iface,
1091                                const struct ieee80211_mgmt *mgmt,
1092                                size_t frame_len)
1093 {
1094         switch (mgmt->u.action.u.fst_action.action) {
1095         case FST_ACTION_SETUP_REQUEST:
1096                 WPA_ASSERT(0);
1097                 break;
1098         case FST_ACTION_SETUP_RESPONSE:
1099                 fst_session_handle_setup_response(s, iface, mgmt, frame_len);
1100                 break;
1101         case FST_ACTION_TEAR_DOWN:
1102                 fst_session_handle_tear_down(s, iface, mgmt, frame_len);
1103                 break;
1104         case FST_ACTION_ACK_REQUEST:
1105                 fst_session_handle_ack_request(s, iface, mgmt, frame_len);
1106                 break;
1107         case FST_ACTION_ACK_RESPONSE:
1108                 fst_session_handle_ack_response(s, iface, mgmt, frame_len);
1109                 break;
1110         case FST_ACTION_ON_CHANNEL_TUNNEL:
1111         default:
1112                 fst_printf_sframe(s, FALSE, MSG_ERROR,
1113                                   "Unsupported FST Action frame");
1114                 break;
1115         }
1116 }
1117
1118
1119 int fst_session_tear_down_setup(struct fst_session *s)
1120 {
1121         int res;
1122         union fst_session_state_switch_extra evext = {
1123                 .to_initial = {
1124                         .reason = REASON_TEARDOWN,
1125                         .initiator = FST_INITIATOR_LOCAL,
1126                 },
1127         };
1128
1129         res = fst_session_send_tear_down(s);
1130
1131         fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1132
1133         return res;
1134 }
1135
1136
1137 void fst_session_reset(struct fst_session *s)
1138 {
1139         fst_session_reset_ex(s, REASON_RESET);
1140 }
1141
1142
1143 void fst_session_delete(struct fst_session *s)
1144 {
1145         fst_printf(MSG_INFO, "Session %u deleted", s->id);
1146         dl_list_del(&s->global_sessions_lentry);
1147         foreach_fst_ctrl_call(on_session_removed, s);
1148         os_free(s);
1149 }
1150
1151
1152 struct fst_group * fst_session_get_group(struct fst_session *s)
1153 {
1154         return s->group;
1155 }
1156
1157
1158 struct fst_iface * fst_session_get_iface(struct fst_session *s, Boolean is_old)
1159 {
1160         return is_old ? s->data.old_iface : s->data.new_iface;
1161 }
1162
1163
1164 u32 fst_session_get_id(struct fst_session *s)
1165 {
1166         return s->id;
1167 }
1168
1169
1170 const u8 * fst_session_get_peer_addr(struct fst_session *s, Boolean is_old)
1171 {
1172         return is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
1173 }
1174
1175
1176 u32 fst_session_get_llt(struct fst_session *s)
1177 {
1178         return s->data.llt_ms;
1179 }
1180
1181
1182 enum fst_session_state fst_session_get_state(struct fst_session *s)
1183 {
1184         return s->state;
1185 }
1186
1187
1188 struct fst_session * fst_session_get_by_id(u32 id)
1189 {
1190         struct fst_session *s;
1191
1192         foreach_fst_session(s) {
1193                 if (id == s->id)
1194                         return s;
1195         }
1196
1197         return NULL;
1198 }
1199
1200
1201 void fst_session_enum(struct fst_group *g, fst_session_enum_clb clb, void *ctx)
1202 {
1203         struct fst_session *s;
1204
1205         foreach_fst_session(s) {
1206                 if (!g || s->group == g)
1207                         clb(s->group, s, ctx);
1208         }
1209 }
1210
1211
1212 void fst_session_on_action_rx(struct fst_iface *iface,
1213                               const struct ieee80211_mgmt *mgmt,
1214                               size_t len)
1215 {
1216         struct fst_session *s;
1217
1218         if (len < IEEE80211_HDRLEN + 2 ||
1219             mgmt->u.action.category != WLAN_ACTION_FST) {
1220                 fst_printf_iface(iface, MSG_ERROR,
1221                                  "invalid Action frame received");
1222                 return;
1223         }
1224
1225         if (mgmt->u.action.u.fst_action.action <= FST_ACTION_MAX_SUPPORTED) {
1226                 fst_printf_iface(iface, MSG_DEBUG,
1227                                  "FST Action '%s' received!",
1228                                  fst_action_names[mgmt->u.action.u.fst_action.action]);
1229         } else {
1230                 fst_printf_iface(iface, MSG_WARNING,
1231                                  "unknown FST Action (%u) received!",
1232                                  mgmt->u.action.u.fst_action.action);
1233                 return;
1234         }
1235
1236         if (mgmt->u.action.u.fst_action.action == FST_ACTION_SETUP_REQUEST) {
1237                 fst_session_handle_setup_request(iface, mgmt, len);
1238                 return;
1239         }
1240
1241         s = fst_find_session_in_progress(mgmt->sa, fst_iface_get_group(iface));
1242         if (s) {
1243                 fst_session_handle_action(s, iface, mgmt, len);
1244         } else {
1245                 fst_printf_iface(iface, MSG_WARNING,
1246                                  "FST Action '%s' dropped: no session in progress found",
1247                                  fst_action_names[mgmt->u.action.u.fst_action.action]);
1248         }
1249 }
1250
1251
1252 int fst_session_set_str_ifname(struct fst_session *s, const char *ifname,
1253                                Boolean is_old)
1254 {
1255         struct fst_group *g = fst_session_get_group(s);
1256         struct fst_iface *i;
1257
1258         i = fst_group_get_iface_by_name(g, ifname);
1259         if (!i) {
1260                 fst_printf_session(s, MSG_WARNING,
1261                                    "Cannot set iface %s: no such iface within group '%s'",
1262                                    ifname, fst_group_get_id(g));
1263                 return -1;
1264         }
1265
1266         fst_session_set_iface(s, i, is_old);
1267
1268         return 0;
1269 }
1270
1271
1272 int fst_session_set_str_peer_addr(struct fst_session *s, const char *mac,
1273                                   Boolean is_old)
1274 {
1275         u8 peer_addr[ETH_ALEN];
1276         int res = fst_read_peer_addr(mac, peer_addr);
1277
1278         if (res)
1279                 return res;
1280
1281         fst_session_set_peer_addr(s, peer_addr, is_old);
1282
1283         return 0;
1284 }
1285
1286
1287 int fst_session_set_str_llt(struct fst_session *s, const char *llt_str)
1288 {
1289         char *endp;
1290         long int llt = strtol(llt_str, &endp, 0);
1291
1292         if (*endp || llt < 0 || (unsigned long int) llt > FST_MAX_LLT_MS) {
1293                 fst_printf_session(s, MSG_WARNING,
1294                                    "Cannot set llt %s: Invalid llt value (1..%u expected)",
1295                                    llt_str, FST_MAX_LLT_MS);
1296                 return -1;
1297         }
1298         fst_session_set_llt(s, (u32) llt);
1299
1300         return 0;
1301 }
1302
1303
1304 void fst_session_global_on_iface_detached(struct fst_iface *iface)
1305 {
1306         struct fst_session *s;
1307
1308         foreach_fst_session(s) {
1309                 if (fst_session_is_in_progress(s) &&
1310                     (s->data.new_iface == iface ||
1311                      s->data.old_iface == iface))
1312                         fst_session_reset_ex(s, REASON_DETACH_IFACE);
1313         }
1314 }
1315
1316
1317 struct fst_session * fst_session_global_get_first_by_group(struct fst_group *g)
1318 {
1319         struct fst_session *s;
1320
1321         foreach_fst_session(s) {
1322                 if (s->group == g)
1323                         return s;
1324         }
1325
1326         return NULL;
1327 }
1328
1329
1330 #ifdef CONFIG_FST_TEST
1331
1332 static int get_group_fill_session(struct fst_group **g, struct fst_session *s)
1333 {
1334         const u8 *old_addr, *new_addr;
1335         struct fst_get_peer_ctx *ctx;
1336
1337         os_memset(s, 0, sizeof(*s));
1338         foreach_fst_group(*g) {
1339                 s->data.new_iface = fst_group_first_iface(*g);
1340                 if (s->data.new_iface)
1341                         break;
1342         }
1343         if (!s->data.new_iface)
1344                 return -EINVAL;
1345
1346         s->data.old_iface = dl_list_entry(s->data.new_iface->group_lentry.next,
1347                                           struct fst_iface, group_lentry);
1348         if (!s->data.old_iface)
1349                 return -EINVAL;
1350
1351         old_addr = fst_iface_get_peer_first(s->data.old_iface, &ctx, TRUE);
1352         if (!old_addr)
1353                 return -EINVAL;
1354
1355         new_addr = fst_iface_get_peer_first(s->data.new_iface, &ctx, TRUE);
1356         if (!new_addr)
1357                 return -EINVAL;
1358
1359         os_memcpy(s->data.old_peer_addr, old_addr, ETH_ALEN);
1360         os_memcpy(s->data.new_peer_addr, new_addr, ETH_ALEN);
1361
1362         return 0;
1363 }
1364
1365
1366 #define FST_MAX_COMMAND_WORD_NAME_LENGTH 16
1367
1368 int fst_test_req_send_fst_request(const char *params)
1369 {
1370         int fsts_id;
1371         Boolean is_valid;
1372         char *endp;
1373         struct fst_setup_req req;
1374         struct fst_session s;
1375         struct fst_group *g;
1376         enum hostapd_hw_mode hw_mode;
1377         u8 channel;
1378         char additional_param[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1379
1380         if (params[0] != ' ')
1381                 return -EINVAL;
1382         params++;
1383         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1384         if (!is_valid)
1385                 return -EINVAL;
1386
1387         if (get_group_fill_session(&g, &s))
1388                 return -EINVAL;
1389
1390         req.action = FST_ACTION_SETUP_REQUEST;
1391         req.dialog_token = g->dialog_token;
1392         req.llt = host_to_le32(FST_LLT_MS_DEFAULT);
1393         /* 8.4.2.147 Session Transition element */
1394         req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1395         req.stie.length = sizeof(req.stie) - 2;
1396         req.stie.fsts_id = host_to_le32(fsts_id);
1397         req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1398
1399         fst_iface_get_channel_info(s.data.new_iface, &hw_mode, &channel);
1400         req.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1401         req.stie.new_band_op = 1;
1402         req.stie.new_band_setup = 0;
1403
1404         fst_iface_get_channel_info(s.data.old_iface, &hw_mode, &channel);
1405         req.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1406         req.stie.old_band_op = 1;
1407         req.stie.old_band_setup = 0;
1408
1409         if (!fst_read_next_text_param(endp, additional_param,
1410                                        sizeof(additional_param), &endp)) {
1411                 if (!os_strcasecmp(additional_param, FST_CTR_PVAL_BAD_NEW_BAND))
1412                         req.stie.new_band_id = req.stie.old_band_id;
1413         }
1414
1415         return fst_session_send_action(&s, TRUE, &req, sizeof(req),
1416                                        s.data.old_iface->mb_ie);
1417 }
1418
1419
1420 int fst_test_req_send_fst_response(const char *params)
1421 {
1422         int fsts_id;
1423         Boolean is_valid;
1424         char *endp;
1425         struct fst_setup_res res;
1426         struct fst_session s;
1427         struct fst_group *g;
1428         enum hostapd_hw_mode hw_mode;
1429         u8 status_code;
1430         u8 channel;
1431         char response[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1432         struct fst_session *_s;
1433
1434         if (params[0] != ' ')
1435                 return -EINVAL;
1436         params++;
1437         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1438         if (!is_valid)
1439                 return -EINVAL;
1440
1441         if (get_group_fill_session(&g, &s))
1442                 return -EINVAL;
1443
1444         status_code = WLAN_STATUS_SUCCESS;
1445         if (!fst_read_next_text_param(endp, response, sizeof(response),
1446                                       &endp)) {
1447                 if (!os_strcasecmp(response, FST_CS_PVAL_RESPONSE_REJECT))
1448                         status_code = WLAN_STATUS_PENDING_ADMITTING_FST_SESSION;
1449         }
1450
1451         os_memset(&res, 0, sizeof(res));
1452
1453         res.action = FST_ACTION_SETUP_RESPONSE;
1454         /*
1455          * If some session has just received an FST Setup Request, then
1456          * use the correct dialog token copied from this request.
1457          */
1458         _s = fst_find_session_in_progress(fst_session_get_peer_addr(&s, TRUE),
1459                                           g);
1460         res.dialog_token = (_s && fst_session_is_ready_pending(_s)) ?
1461                 _s->data.pending_setup_req_dlgt : g->dialog_token;
1462         res.status_code  = status_code;
1463
1464         res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1465         res.stie.length = sizeof(res.stie) - 2;
1466
1467         if (res.status_code == WLAN_STATUS_SUCCESS) {
1468                 res.stie.fsts_id = fsts_id;
1469                 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1470
1471                 fst_iface_get_channel_info(s.data.new_iface, &hw_mode,
1472                                             &channel);
1473                 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1474                 res.stie.new_band_op = 1;
1475                 res.stie.new_band_setup = 0;
1476
1477                 fst_iface_get_channel_info(s.data.old_iface, &hw_mode,
1478                                            &channel);
1479                 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1480                 res.stie.old_band_op = 1;
1481                 res.stie.old_band_setup = 0;
1482         }
1483
1484         if (!fst_read_next_text_param(endp, response, sizeof(response),
1485                                       &endp)) {
1486                 if (!os_strcasecmp(response, FST_CTR_PVAL_BAD_NEW_BAND))
1487                         res.stie.new_band_id = res.stie.old_band_id;
1488         }
1489
1490         return fst_session_send_action(&s, TRUE, &res, sizeof(res),
1491                                        s.data.old_iface->mb_ie);
1492 }
1493
1494
1495 int fst_test_req_send_ack_request(const char *params)
1496 {
1497         int fsts_id;
1498         Boolean is_valid;
1499         char *endp;
1500         struct fst_ack_req req;
1501         struct fst_session s;
1502         struct fst_group *g;
1503
1504         if (params[0] != ' ')
1505                 return -EINVAL;
1506         params++;
1507         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1508         if (!is_valid)
1509                 return -EINVAL;
1510
1511         if (get_group_fill_session(&g, &s))
1512                 return -EINVAL;
1513
1514         os_memset(&req, 0, sizeof(req));
1515         req.action = FST_ACTION_ACK_REQUEST;
1516         req.dialog_token = g->dialog_token;
1517         req.fsts_id = fsts_id;
1518
1519         return fst_session_send_action(&s, FALSE, &req, sizeof(req), NULL);
1520 }
1521
1522
1523 int fst_test_req_send_ack_response(const char *params)
1524 {
1525         int fsts_id;
1526         Boolean is_valid;
1527         char *endp;
1528         struct fst_ack_res res;
1529         struct fst_session s;
1530         struct fst_group *g;
1531
1532         if (params[0] != ' ')
1533                 return -EINVAL;
1534         params++;
1535         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1536         if (!is_valid)
1537                 return -EINVAL;
1538
1539         if (get_group_fill_session(&g, &s))
1540                 return -EINVAL;
1541
1542         os_memset(&res, 0, sizeof(res));
1543         res.action = FST_ACTION_ACK_RESPONSE;
1544         res.dialog_token = g->dialog_token;
1545         res.fsts_id = fsts_id;
1546
1547         return fst_session_send_action(&s, FALSE, &res, sizeof(res), NULL);
1548 }
1549
1550
1551 int fst_test_req_send_tear_down(const char *params)
1552 {
1553         int fsts_id;
1554         Boolean is_valid;
1555         char *endp;
1556         struct fst_tear_down td;
1557         struct fst_session s;
1558         struct fst_group *g;
1559
1560         if (params[0] != ' ')
1561                 return -EINVAL;
1562         params++;
1563         fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1564         if (!is_valid)
1565                 return -EINVAL;
1566
1567         if (get_group_fill_session(&g, &s))
1568                 return -EINVAL;
1569
1570         os_memset(&td, 0, sizeof(td));
1571         td.action = FST_ACTION_TEAR_DOWN;
1572         td.fsts_id = fsts_id;
1573
1574         return fst_session_send_action(&s, TRUE, &td, sizeof(td), NULL);
1575 }
1576
1577
1578 u32 fst_test_req_get_fsts_id(const char *params)
1579 {
1580         int sid;
1581         Boolean is_valid;
1582         char *endp;
1583         struct fst_session *s;
1584
1585         if (params[0] != ' ')
1586                 return FST_FSTS_ID_NOT_FOUND;
1587         params++;
1588         sid = fst_read_next_int_param(params, &is_valid, &endp);
1589         if (!is_valid)
1590                 return FST_FSTS_ID_NOT_FOUND;
1591
1592         s = fst_session_get_by_id(sid);
1593         if (!s)
1594                 return FST_FSTS_ID_NOT_FOUND;
1595
1596         return s->data.fsts_id;
1597 }
1598
1599
1600 int fst_test_req_get_local_mbies(const char *request, char *buf, size_t buflen)
1601 {
1602         char *endp;
1603         char ifname[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1604         struct fst_group *g;
1605         struct fst_iface *iface;
1606
1607         if (request[0] != ' ')
1608                 return -EINVAL;
1609         request++;
1610         if (fst_read_next_text_param(request, ifname, sizeof(ifname), &endp) ||
1611             !*ifname)
1612                 goto problem;
1613         g = dl_list_first(&fst_global_groups_list, struct fst_group,
1614                           global_groups_lentry);
1615         if (!g)
1616                 goto problem;
1617         iface = fst_group_get_iface_by_name(g, ifname);
1618         if (!iface || !iface->mb_ie)
1619                 goto problem;
1620         return wpa_snprintf_hex(buf, buflen, wpabuf_head(iface->mb_ie),
1621                                 wpabuf_len(iface->mb_ie));
1622
1623 problem:
1624         return os_snprintf(buf, buflen, "FAIL\n");
1625 }
1626
1627 #endif /* CONFIG_FST_TEST */