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