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