2 * FST module - FST Session implementation
3 * Copyright (c) 2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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 */
21 #define US_80211_TU 1024
23 #define US_TO_TU(m) ((m) * / US_80211_TU)
24 #define TU_TO_US(m) ((m) * US_80211_TU)
26 #define FST_LLT_SWITCH_IMMEDIATELY 0
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), \
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__)
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__)
44 #define FST_LLT_MS_DEFAULT 50
45 #define FST_ACTION_MAX_SUPPORTED FST_ACTION_ON_CHANNEL_TUNNEL
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",
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;
64 u8 pending_setup_req_dlgt;
65 u32 fsts_id; /* FSTS ID, see spec, 8.4.2.147
66 * Session Transition element */
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;
77 static struct dl_list global_sessions_list;
78 static u32 global_session_id = 0;
80 #define foreach_fst_session(s) \
81 dl_list_for_each((s), &global_sessions_list, \
82 struct fst_session, global_sessions_lentry)
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)
89 static void fst_session_global_inc_id(void)
92 if (global_session_id == FST_INVALID_SESSION_ID)
97 int fst_session_global_init(void)
99 dl_list_init(&global_sessions_list);
104 void fst_session_global_deinit(void)
106 WPA_ASSERT(dl_list_empty(&global_sessions_list));
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)
114 foreach_fst_ctrl_call(on_event, event_type, NULL, s, extra);
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)
122 if (s->state != state) {
123 union fst_event_extra evext = {
125 .old_state = s->state,
131 evext.session_state.extra = *extra;
132 fst_session_notify_ctrl(s, EVENT_FST_SESSION_STATE_CHANGED,
134 fst_printf_session(s, MSG_INFO, "State: %s => %s",
135 fst_session_state_name(s->state),
136 fst_session_state_name(state));
142 static u32 fst_find_free_session_id(void)
144 u32 i, id = FST_INVALID_SESSION_ID;
145 struct fst_session *s;
147 for (i = 0; i < (u32) -1; i++) {
148 Boolean in_use = FALSE;
150 foreach_fst_session(s) {
151 if (s->id == global_session_id) {
152 fst_session_global_inc_id();
158 id = global_session_id;
159 fst_session_global_inc_id();
168 static void fst_session_timeout_handler(void *eloop_data, void *user_ctx)
170 struct fst_session *s = user_ctx;
171 union fst_session_state_switch_extra extra = {
173 .reason = REASON_STT,
177 fst_printf_session(s, MSG_WARNING, "Session State Timeout");
178 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &extra);
182 static void fst_session_stt_arm(struct fst_session *s)
184 eloop_register_timeout(0, TU_TO_US(FST_DEFAULT_SESSION_TIMEOUT_TU),
185 fst_session_timeout_handler, NULL, s);
190 static void fst_session_stt_disarm(struct fst_session *s)
193 eloop_cancel_timeout(fst_session_timeout_handler, NULL, s);
194 s->stt_armed = FALSE;
199 static Boolean fst_session_is_in_transition(struct fst_session *s)
201 /* See spec, 10.32.2.2 Transitioning between states */
206 static int fst_session_is_in_progress(struct fst_session *s)
208 return s->state != FST_SESSION_STATE_INITIAL;
212 static int fst_session_is_ready_pending(struct fst_session *s)
214 return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
215 fst_session_is_in_transition(s);
219 static int fst_session_is_ready(struct fst_session *s)
221 return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
222 !fst_session_is_in_transition(s);
226 static int fst_session_is_switch_requested(struct fst_session *s)
228 return s->state == FST_SESSION_STATE_TRANSITION_DONE &&
229 fst_session_is_in_transition(s);
233 static struct fst_session *
234 fst_find_session_in_progress(const u8 *peer_addr, struct fst_group *g)
236 struct fst_session *s;
238 foreach_fst_session(s) {
240 (os_memcmp(s->data.old_peer_addr, peer_addr,
242 os_memcmp(s->data.new_peer_addr, peer_addr,
244 fst_session_is_in_progress(s))
252 static void fst_session_reset_ex(struct fst_session *s, enum fst_reason reason)
254 union fst_session_state_switch_extra evext = {
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);
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)
277 struct fst_iface *iface =
278 old_iface ? s->data.old_iface : s->data.new_iface;
280 WPA_ASSERT(payload != NULL);
281 WPA_ASSERT(size != 0);
283 action = *(const u8 *) payload;
285 WPA_ASSERT(action <= FST_ACTION_MAX_SUPPORTED);
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]);
295 len = sizeof(u8) /* category */ + size;
297 len += wpabuf_size(extra_buf);
299 buf = wpabuf_alloc(len);
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]);
307 wpabuf_put_u8(buf, WLAN_ACTION_FST);
308 wpabuf_put_data(buf, payload, size);
310 wpabuf_put_buf(buf, extra_buf);
312 res = fst_iface_send_action(iface,
313 old_iface ? s->data.old_peer_addr :
314 s->data.new_peer_addr, buf);
316 fst_printf_siface(s, iface, MSG_ERROR,
317 "failed to send FST Action '%s'",
318 fst_action_names[action]);
320 fst_printf_siface(s, iface, MSG_DEBUG, "FST Action '%s' sent",
321 fst_action_names[action]);
328 static int fst_session_send_tear_down(struct fst_session *s)
330 struct fst_tear_down td;
333 if (!fst_session_is_in_progress(s)) {
334 fst_printf_session(s, MSG_ERROR, "No FST setup to tear down");
338 WPA_ASSERT(s->data.old_iface != NULL);
339 WPA_ASSERT(s->data.new_iface != NULL);
341 os_memset(&td, 0, sizeof(td));
343 td.action = FST_ACTION_TEAR_DOWN;
344 td.fsts_id = host_to_le32(s->data.fsts_id);
346 res = fst_session_send_action(s, TRUE, &td, sizeof(td), NULL);
348 fst_printf_sframe(s, TRUE, MSG_INFO, "FST TearDown sent");
350 fst_printf_sframe(s, TRUE, MSG_ERROR,
351 "failed to send FST TearDown");
357 static void fst_session_handle_setup_request(struct fst_iface *iface,
358 const struct ieee80211_mgmt *mgmt,
361 struct fst_session *s;
362 const struct fst_setup_req *req;
363 struct fst_iface *new_iface = NULL;
365 u8 new_iface_peer_addr[ETH_ALEN];
366 const struct wpabuf *peer_mbies;
369 if (frame_len < IEEE80211_HDRLEN + 1 + sizeof(*req)) {
370 fst_printf_iface(iface, MSG_WARNING,
371 "FST Request dropped: too short (%zu < %zu)",
373 IEEE80211_HDRLEN + 1 + sizeof(*req));
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");
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");
392 g = fst_iface_get_group(iface);
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,
402 peer_mbies = fst_iface_get_peer_mb_ie(iface, mgmt->sa);
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);
408 fst_printf_iface(iface, MSG_INFO,
409 "FST Request: new iface (%s:" MACSTR
411 fst_iface_get_name(new_iface),
412 MAC2STR(new_iface_peer_addr));
416 new_iface = fst_group_find_new_iface_by_stie(
417 g, iface, mgmt->sa, &req->stie,
418 new_iface_peer_addr);
420 fst_printf_iface(iface, MSG_INFO,
421 "FST Request: new iface (%s:" MACSTR
423 fst_iface_get_name(new_iface),
424 MAC2STR(new_iface_peer_addr));
428 fst_printf_iface(iface, MSG_WARNING,
429 "FST Request dropped: new iface not found");
433 s = fst_find_session_in_progress(mgmt->sa, g);
435 union fst_session_state_switch_extra evext = {
437 .reason = REASON_SETUP,
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.
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 "
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",
463 fst_session_state_name(s->state));
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).
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.
479 if (le_to_host32(req->stie.fsts_id) != s->data.fsts_id)
480 fst_session_send_tear_down(s);
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");
489 s = fst_session_create(g);
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));
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);
506 fst_session_stt_arm(s);
508 fst_session_notify_ctrl(s, EVENT_FST_SETUP, NULL);
510 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION, NULL);
514 static void fst_session_handle_setup_response(struct fst_session *s,
515 struct fst_iface *iface,
516 const struct ieee80211_mgmt *mgmt,
519 const struct fst_setup_res *res;
520 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
521 enum hostapd_hw_mode hw_mode;
523 union fst_session_state_switch_extra evext = {
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));
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));
541 if (plen < sizeof(*res)) {
542 fst_printf_session(s, MSG_WARNING,
543 "Too short FST Response dropped");
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");
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,
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));
571 fst_session_stt_disarm(s);
573 if (res->status_code != WLAN_STATUS_SUCCESS) {
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.
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",
592 fst_iface_get_channel_info(s->data.new_iface, &hw_mode, &channel);
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);
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),
609 fst_session_notify_ctrl(s, EVENT_FST_ESTABLISHED, NULL);
611 if (s->data.llt_ms == FST_LLT_SWITCH_IMMEDIATELY)
612 fst_session_initiate_switch(s);
616 static void fst_session_handle_tear_down(struct fst_session *s,
617 struct fst_iface *iface,
618 const struct ieee80211_mgmt *mgmt,
621 const struct fst_tear_down *td;
622 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
623 union fst_session_state_switch_extra evext = {
625 .reason = REASON_TEARDOWN,
626 .initiator = FST_INITIATOR_REMOTE,
630 if (plen < sizeof(*td)) {
631 fst_printf_session(s, MSG_WARNING,
632 "Too short FST Tear Down dropped");
635 td = (const struct fst_tear_down *)
636 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
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));
645 fst_session_stt_disarm(s);
647 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
651 static void fst_session_handle_ack_request(struct fst_session *s,
652 struct fst_iface *iface,
653 const struct ieee80211_mgmt *mgmt,
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 = {
661 .reason = REASON_SWITCH,
662 .initiator = FST_INITIATOR_REMOTE,
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));
673 WPA_ASSERT(s->data.new_iface != NULL);
675 if (iface != s->data.new_iface) {
676 fst_printf_siface(s, iface, MSG_ERROR,
677 "Ack received on wrong interface");
681 if (plen < sizeof(*req)) {
682 fst_printf_session(s, MSG_WARNING,
683 "Too short FST Ack Request dropped");
686 req = (const struct fst_ack_req *)
687 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
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));
696 os_memset(&res, 0, sizeof(res));
698 res.action = FST_ACTION_ACK_RESPONSE;
699 res.dialog_token = req->dialog_token;
700 res.fsts_id = req->fsts_id;
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,
707 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED,
709 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
715 fst_session_handle_ack_response(struct fst_session *s,
716 struct fst_iface *iface,
717 const struct ieee80211_mgmt *mgmt,
720 const struct fst_ack_res *res;
721 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
722 union fst_session_state_switch_extra evext = {
724 .reason = REASON_SWITCH,
725 .initiator = FST_INITIATOR_LOCAL,
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));
736 WPA_ASSERT(s->data.new_iface != NULL);
738 if (iface != s->data.new_iface) {
739 fst_printf_siface(s, iface, MSG_ERROR,
740 "Ack response received on wrong interface");
744 if (plen < sizeof(*res)) {
745 fst_printf_session(s, MSG_WARNING,
746 "Too short FST Ack Response dropped");
749 res = (const struct fst_ack_res *)
750 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
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));
759 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED, NULL);
760 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
762 fst_session_stt_disarm(s);
766 struct fst_session * fst_session_create(struct fst_group *g)
768 struct fst_session *s;
771 WPA_ASSERT(!is_zero_ether_addr(own_addr));
773 id = fst_find_free_session_id();
774 if (id == FST_INVALID_SESSION_ID) {
775 fst_printf(MSG_ERROR, "Cannot assign new session ID");
779 s = os_zalloc(sizeof(*s));
781 fst_printf(MSG_ERROR, "Cannot allocate new session object");
787 s->state = FST_SESSION_STATE_INITIAL;
789 s->data.llt_ms = FST_LLT_MS_DEFAULT;
791 fst_printf(MSG_INFO, "Session %u created", s->id);
793 dl_list_add_tail(&global_sessions_list, &s->global_sessions_lentry);
795 foreach_fst_ctrl_call(on_session_added, s);
801 void fst_session_set_iface(struct fst_session *s, struct fst_iface *iface,
805 s->data.old_iface = iface;
807 s->data.new_iface = iface;
812 void fst_session_set_llt(struct fst_session *s, u32 llt)
814 s->data.llt_ms = llt;
818 void fst_session_set_peer_addr(struct fst_session *s, const u8 *addr,
821 u8 *a = is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
823 os_memcpy(a, addr, ETH_ALEN);
827 int fst_session_initiate_setup(struct fst_session *s)
829 struct fst_setup_req req;
833 struct fst_session *_s;
835 if (fst_session_is_in_progress(s)) {
836 fst_printf_session(s, MSG_ERROR, "Session in progress");
840 if (is_zero_ether_addr(s->data.old_peer_addr)) {
841 fst_printf_session(s, MSG_ERROR, "No old peer MAC address");
845 if (is_zero_ether_addr(s->data.new_peer_addr)) {
846 fst_printf_session(s, MSG_ERROR, "No new peer MAC address");
850 if (!s->data.old_iface) {
851 fst_printf_session(s, MSG_ERROR, "No old interface defined");
855 if (!s->data.new_iface) {
856 fst_printf_session(s, MSG_ERROR, "No new interface defined");
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");
866 if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr)) {
867 fst_printf_session(s, MSG_ERROR,
868 "The preset old peer address is not connected");
872 if (!fst_iface_is_connected(s->data.new_iface, s->data.new_peer_addr)) {
873 fst_printf_session(s, MSG_ERROR,
874 "The preset new peer address is not connected");
878 _s = fst_find_session_in_progress(s->data.old_peer_addr, s->group);
880 fst_printf_session(s, MSG_ERROR,
881 "There is another session in progress (old): %u",
886 _s = fst_find_session_in_progress(s->data.new_peer_addr, s->group);
888 fst_printf_session(s, MSG_ERROR,
889 "There is another session in progress (new): %u",
894 dialog_token = fst_group_assign_dialog_token(s->group);
895 fsts_id = fst_group_assign_fsts_id(s->group);
897 os_memset(&req, 0, sizeof(req));
899 fst_printf_siface(s, s->data.old_iface, MSG_INFO,
900 "initiating FST setup for %s (llt=%u ms)",
901 fst_iface_get_name(s->data.new_iface), s->data.llt_ms);
903 req.action = FST_ACTION_SETUP_REQUEST;
904 req.dialog_token = dialog_token;
905 req.llt = host_to_le32(FST_LLT_MS_TO_VAL(s->data.llt_ms));
906 /* 8.4.2.147 Session Transition element */
907 req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
908 req.stie.length = sizeof(req.stie) - 2;
909 req.stie.fsts_id = host_to_le32(fsts_id);
910 req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
912 req.stie.new_band_id = fst_iface_get_band_id(s->data.new_iface);
913 req.stie.new_band_op = 1;
914 req.stie.new_band_setup = 0;
916 req.stie.old_band_id = fst_iface_get_band_id(s->data.old_iface);
917 req.stie.old_band_op = 1;
918 req.stie.old_band_setup = 0;
920 res = fst_session_send_action(s, TRUE, &req, sizeof(req),
921 fst_iface_get_mbie(s->data.old_iface));
923 s->data.fsts_id = fsts_id;
924 s->data.pending_setup_req_dlgt = dialog_token;
925 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Request sent");
926 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION,
929 fst_session_stt_arm(s);
936 int fst_session_respond(struct fst_session *s, u8 status_code)
938 struct fst_setup_res res;
939 enum hostapd_hw_mode hw_mode;
942 if (!fst_session_is_ready_pending(s)) {
943 fst_printf_session(s, MSG_ERROR, "incorrect state: %s",
944 fst_session_state_name(s->state));
948 if (is_zero_ether_addr(s->data.old_peer_addr)) {
949 fst_printf_session(s, MSG_ERROR, "No peer MAC address");
953 if (!s->data.old_iface) {
954 fst_printf_session(s, MSG_ERROR, "No old interface defined");
958 if (!s->data.new_iface) {
959 fst_printf_session(s, MSG_ERROR, "No new interface defined");
963 if (s->data.new_iface == s->data.old_iface) {
964 fst_printf_session(s, MSG_ERROR,
965 "Same interface set as old and new");
969 if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr)) {
970 fst_printf_session(s, MSG_ERROR,
971 "The preset peer address is not in the peer list");
975 fst_session_stt_disarm(s);
977 os_memset(&res, 0, sizeof(res));
979 res.action = FST_ACTION_SETUP_RESPONSE;
980 res.dialog_token = s->data.pending_setup_req_dlgt;
981 res.status_code = status_code;
983 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
984 res.stie.length = sizeof(res.stie) - 2;
986 if (status_code == WLAN_STATUS_SUCCESS) {
987 res.stie.fsts_id = s->data.fsts_id;
988 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
990 fst_iface_get_channel_info(s->data.new_iface, &hw_mode,
992 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
993 res.stie.new_band_op = 1;
994 res.stie.new_band_setup = 0;
996 fst_iface_get_channel_info(s->data.old_iface, &hw_mode,
998 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
999 res.stie.old_band_op = 1;
1000 res.stie.old_band_setup = 0;
1002 fst_printf_session(s, MSG_INFO,
1003 "%s: FST Setup Request accepted for %s (llt=%u)",
1004 fst_iface_get_name(s->data.old_iface),
1005 fst_iface_get_name(s->data.new_iface),
1008 fst_printf_session(s, MSG_WARNING,
1009 "%s: FST Setup Request rejected with code %d",
1010 fst_iface_get_name(s->data.old_iface),
1014 if (fst_session_send_action(s, TRUE, &res, sizeof(res),
1015 fst_iface_get_mbie(s->data.old_iface))) {
1016 fst_printf_sframe(s, TRUE, MSG_ERROR,
1017 "cannot send FST Setup Response with code %d",
1022 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Response sent");
1024 if (status_code != WLAN_STATUS_SUCCESS) {
1025 union fst_session_state_switch_extra evext = {
1027 .reason = REASON_REJECT,
1028 .reject_code = status_code,
1029 .initiator = FST_INITIATOR_LOCAL,
1032 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1039 int fst_session_initiate_switch(struct fst_session *s)
1041 struct fst_ack_req req;
1045 if (!fst_session_is_ready(s)) {
1046 fst_printf_session(s, MSG_ERROR,
1047 "cannot initiate switch due to wrong setup state (%d)",
1052 dialog_token = fst_group_assign_dialog_token(s->group);
1054 WPA_ASSERT(s->data.new_iface != NULL);
1055 WPA_ASSERT(s->data.old_iface != NULL);
1057 fst_printf_session(s, MSG_INFO, "initiating FST switch: %s => %s",
1058 fst_iface_get_name(s->data.old_iface),
1059 fst_iface_get_name(s->data.new_iface));
1061 os_memset(&req, 0, sizeof(req));
1063 req.action = FST_ACTION_ACK_REQUEST;
1064 req.dialog_token = dialog_token;
1065 req.fsts_id = host_to_le32(s->data.fsts_id);
1067 res = fst_session_send_action(s, FALSE, &req, sizeof(req), NULL);
1069 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Request sent");
1070 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
1072 fst_session_stt_arm(s);
1074 fst_printf_sframe(s, FALSE, MSG_ERROR,
1075 "Cannot send FST Ack Request");
1082 void fst_session_handle_action(struct fst_session *s,
1083 struct fst_iface *iface,
1084 const struct ieee80211_mgmt *mgmt,
1087 switch (mgmt->u.action.u.fst_action.action) {
1088 case FST_ACTION_SETUP_REQUEST:
1091 case FST_ACTION_SETUP_RESPONSE:
1092 fst_session_handle_setup_response(s, iface, mgmt, frame_len);
1094 case FST_ACTION_TEAR_DOWN:
1095 fst_session_handle_tear_down(s, iface, mgmt, frame_len);
1097 case FST_ACTION_ACK_REQUEST:
1098 fst_session_handle_ack_request(s, iface, mgmt, frame_len);
1100 case FST_ACTION_ACK_RESPONSE:
1101 fst_session_handle_ack_response(s, iface, mgmt, frame_len);
1103 case FST_ACTION_ON_CHANNEL_TUNNEL:
1105 fst_printf_sframe(s, FALSE, MSG_ERROR,
1106 "Unsupported FST Action frame");
1112 int fst_session_tear_down_setup(struct fst_session *s)
1115 union fst_session_state_switch_extra evext = {
1117 .reason = REASON_TEARDOWN,
1118 .initiator = FST_INITIATOR_LOCAL,
1122 res = fst_session_send_tear_down(s);
1124 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1130 void fst_session_reset(struct fst_session *s)
1132 fst_session_reset_ex(s, REASON_RESET);
1136 void fst_session_delete(struct fst_session *s)
1138 fst_printf(MSG_INFO, "Session %u deleted", s->id);
1139 dl_list_del(&s->global_sessions_lentry);
1140 foreach_fst_ctrl_call(on_session_removed, s);
1145 struct fst_group * fst_session_get_group(struct fst_session *s)
1151 struct fst_iface * fst_session_get_iface(struct fst_session *s, Boolean is_old)
1153 return is_old ? s->data.old_iface : s->data.new_iface;
1157 u32 fst_session_get_id(struct fst_session *s)
1163 const u8 * fst_session_get_peer_addr(struct fst_session *s, Boolean is_old)
1165 return is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
1169 u32 fst_session_get_llt(struct fst_session *s)
1171 return s->data.llt_ms;
1175 enum fst_session_state fst_session_get_state(struct fst_session *s)
1181 struct fst_session * fst_session_get_by_id(u32 id)
1183 struct fst_session *s;
1185 foreach_fst_session(s) {
1194 void fst_session_enum(struct fst_group *g, fst_session_enum_clb clb, void *ctx)
1196 struct fst_session *s;
1198 foreach_fst_session(s) {
1199 if (!g || s->group == g)
1200 clb(s->group, s, ctx);
1205 void fst_session_on_action_rx(struct fst_iface *iface,
1206 const struct ieee80211_mgmt *mgmt,
1209 struct fst_session *s;
1211 if (len < IEEE80211_HDRLEN + 2 ||
1212 mgmt->u.action.category != WLAN_ACTION_FST) {
1213 fst_printf_iface(iface, MSG_ERROR,
1214 "invalid Action frame received");
1218 if (mgmt->u.action.u.fst_action.action <= FST_ACTION_MAX_SUPPORTED) {
1219 fst_printf_iface(iface, MSG_DEBUG,
1220 "FST Action '%s' received!",
1221 fst_action_names[mgmt->u.action.u.fst_action.action]);
1223 fst_printf_iface(iface, MSG_WARNING,
1224 "unknown FST Action (%u) received!",
1225 mgmt->u.action.u.fst_action.action);
1229 if (mgmt->u.action.u.fst_action.action == FST_ACTION_SETUP_REQUEST) {
1230 fst_session_handle_setup_request(iface, mgmt, len);
1234 s = fst_find_session_in_progress(mgmt->sa, fst_iface_get_group(iface));
1236 fst_session_handle_action(s, iface, mgmt, len);
1238 fst_printf_iface(iface, MSG_WARNING,
1239 "FST Action '%s' dropped: no session in progress found",
1240 fst_action_names[mgmt->u.action.u.fst_action.action]);
1245 int fst_session_set_str_ifname(struct fst_session *s, const char *ifname,
1248 struct fst_group *g = fst_session_get_group(s);
1249 struct fst_iface *i;
1251 i = fst_group_get_iface_by_name(g, ifname);
1253 fst_printf_session(s, MSG_WARNING,
1254 "Cannot set iface %s: no such iface within group '%s'",
1255 ifname, fst_group_get_id(g));
1259 fst_session_set_iface(s, i, is_old);
1265 int fst_session_set_str_peer_addr(struct fst_session *s, const char *mac,
1268 u8 peer_addr[ETH_ALEN];
1269 int res = fst_read_peer_addr(mac, peer_addr);
1274 fst_session_set_peer_addr(s, peer_addr, is_old);
1280 int fst_session_set_str_llt(struct fst_session *s, const char *llt_str)
1283 long int llt = strtol(llt_str, &endp, 0);
1285 if (*endp || llt < 0 || (unsigned long int) llt > FST_MAX_LLT_MS) {
1286 fst_printf_session(s, MSG_WARNING,
1287 "Cannot set llt %s: Invalid llt value (1..%u expected)",
1288 llt_str, FST_MAX_LLT_MS);
1291 fst_session_set_llt(s, (u32) llt);
1297 void fst_session_global_on_iface_detached(struct fst_iface *iface)
1299 struct fst_session *s;
1301 foreach_fst_session(s) {
1302 if (fst_session_is_in_progress(s) &&
1303 (s->data.new_iface == iface ||
1304 s->data.old_iface == iface))
1305 fst_session_reset_ex(s, REASON_DETACH_IFACE);
1310 struct fst_session * fst_session_global_get_first_by_group(struct fst_group *g)
1312 struct fst_session *s;
1314 foreach_fst_session(s) {
1323 #ifdef CONFIG_FST_TEST
1325 static int get_group_fill_session(struct fst_group **g, struct fst_session *s)
1327 const u8 *old_addr, *new_addr;
1328 struct fst_get_peer_ctx *ctx;
1330 os_memset(s, 0, sizeof(*s));
1331 foreach_fst_group(*g) {
1332 s->data.new_iface = fst_group_first_iface(*g);
1333 if (s->data.new_iface)
1336 if (!s->data.new_iface)
1339 s->data.old_iface = dl_list_entry(s->data.new_iface->group_lentry.next,
1340 struct fst_iface, group_lentry);
1341 if (!s->data.old_iface)
1344 old_addr = fst_iface_get_peer_first(s->data.old_iface, &ctx, TRUE);
1348 new_addr = fst_iface_get_peer_first(s->data.new_iface, &ctx, TRUE);
1352 os_memcpy(s->data.old_peer_addr, old_addr, ETH_ALEN);
1353 os_memcpy(s->data.new_peer_addr, new_addr, ETH_ALEN);
1359 #define FST_MAX_COMMAND_WORD_NAME_LENGTH 16
1361 int fst_test_req_send_fst_request(const char *params)
1366 struct fst_setup_req req;
1367 struct fst_session s;
1368 struct fst_group *g;
1369 enum hostapd_hw_mode hw_mode;
1371 char additional_param[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1373 if (params[0] != ' ')
1376 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1380 if (get_group_fill_session(&g, &s))
1383 req.action = FST_ACTION_SETUP_REQUEST;
1384 req.dialog_token = g->dialog_token;
1385 req.llt = host_to_le32(FST_LLT_MS_DEFAULT);
1386 /* 8.4.2.147 Session Transition element */
1387 req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1388 req.stie.length = sizeof(req.stie) - 2;
1389 req.stie.fsts_id = host_to_le32(fsts_id);
1390 req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1392 fst_iface_get_channel_info(s.data.new_iface, &hw_mode, &channel);
1393 req.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1394 req.stie.new_band_op = 1;
1395 req.stie.new_band_setup = 0;
1397 fst_iface_get_channel_info(s.data.old_iface, &hw_mode, &channel);
1398 req.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1399 req.stie.old_band_op = 1;
1400 req.stie.old_band_setup = 0;
1402 if (!fst_read_next_text_param(endp, additional_param,
1403 sizeof(additional_param), &endp)) {
1404 if (!os_strcasecmp(additional_param, FST_CTR_PVAL_BAD_NEW_BAND))
1405 req.stie.new_band_id = req.stie.old_band_id;
1408 return fst_session_send_action(&s, TRUE, &req, sizeof(req),
1409 s.data.old_iface->mb_ie);
1413 int fst_test_req_send_fst_response(const char *params)
1418 struct fst_setup_res res;
1419 struct fst_session s;
1420 struct fst_group *g;
1421 enum hostapd_hw_mode hw_mode;
1424 char response[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1425 struct fst_session *_s;
1427 if (params[0] != ' ')
1430 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1434 if (get_group_fill_session(&g, &s))
1437 status_code = WLAN_STATUS_SUCCESS;
1438 if (!fst_read_next_text_param(endp, response, sizeof(response),
1440 if (!os_strcasecmp(response, FST_CS_PVAL_RESPONSE_REJECT))
1441 status_code = WLAN_STATUS_PENDING_ADMITTING_FST_SESSION;
1444 os_memset(&res, 0, sizeof(res));
1446 res.action = FST_ACTION_SETUP_RESPONSE;
1448 * If some session has just received an FST Setup Request, then
1449 * use the correct dialog token copied from this request.
1451 _s = fst_find_session_in_progress(fst_session_get_peer_addr(&s, TRUE),
1453 res.dialog_token = (_s && fst_session_is_ready_pending(_s)) ?
1454 _s->data.pending_setup_req_dlgt : g->dialog_token;
1455 res.status_code = status_code;
1457 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1458 res.stie.length = sizeof(res.stie) - 2;
1460 if (res.status_code == WLAN_STATUS_SUCCESS) {
1461 res.stie.fsts_id = fsts_id;
1462 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1464 fst_iface_get_channel_info(s.data.new_iface, &hw_mode,
1466 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1467 res.stie.new_band_op = 1;
1468 res.stie.new_band_setup = 0;
1470 fst_iface_get_channel_info(s.data.old_iface, &hw_mode,
1472 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1473 res.stie.old_band_op = 1;
1474 res.stie.old_band_setup = 0;
1477 if (!fst_read_next_text_param(endp, response, sizeof(response),
1479 if (!os_strcasecmp(response, FST_CTR_PVAL_BAD_NEW_BAND))
1480 res.stie.new_band_id = res.stie.old_band_id;
1483 return fst_session_send_action(&s, TRUE, &res, sizeof(res),
1484 s.data.old_iface->mb_ie);
1488 int fst_test_req_send_ack_request(const char *params)
1493 struct fst_ack_req req;
1494 struct fst_session s;
1495 struct fst_group *g;
1497 if (params[0] != ' ')
1500 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1504 if (get_group_fill_session(&g, &s))
1507 os_memset(&req, 0, sizeof(req));
1508 req.action = FST_ACTION_ACK_REQUEST;
1509 req.dialog_token = g->dialog_token;
1510 req.fsts_id = fsts_id;
1512 return fst_session_send_action(&s, FALSE, &req, sizeof(req), NULL);
1516 int fst_test_req_send_ack_response(const char *params)
1521 struct fst_ack_res res;
1522 struct fst_session s;
1523 struct fst_group *g;
1525 if (params[0] != ' ')
1528 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1532 if (get_group_fill_session(&g, &s))
1535 os_memset(&res, 0, sizeof(res));
1536 res.action = FST_ACTION_ACK_RESPONSE;
1537 res.dialog_token = g->dialog_token;
1538 res.fsts_id = fsts_id;
1540 return fst_session_send_action(&s, FALSE, &res, sizeof(res), NULL);
1544 int fst_test_req_send_tear_down(const char *params)
1549 struct fst_tear_down td;
1550 struct fst_session s;
1551 struct fst_group *g;
1553 if (params[0] != ' ')
1556 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1560 if (get_group_fill_session(&g, &s))
1563 os_memset(&td, 0, sizeof(td));
1564 td.action = FST_ACTION_TEAR_DOWN;
1565 td.fsts_id = fsts_id;
1567 return fst_session_send_action(&s, TRUE, &td, sizeof(td), NULL);
1571 u32 fst_test_req_get_fsts_id(const char *params)
1576 struct fst_session *s;
1578 if (params[0] != ' ')
1579 return FST_FSTS_ID_NOT_FOUND;
1581 sid = fst_read_next_int_param(params, &is_valid, &endp);
1583 return FST_FSTS_ID_NOT_FOUND;
1585 s = fst_session_get_by_id(sid);
1587 return FST_FSTS_ID_NOT_FOUND;
1589 return s->data.fsts_id;
1593 int fst_test_req_get_local_mbies(const char *request, char *buf, size_t buflen)
1596 char ifname[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1597 struct fst_group *g;
1598 struct fst_iface *iface;
1600 if (request[0] != ' ')
1603 if (fst_read_next_text_param(request, ifname, sizeof(ifname), &endp) ||
1606 g = dl_list_first(&fst_global_groups_list, struct fst_group,
1607 global_groups_lentry);
1610 iface = fst_group_get_iface_by_name(g, ifname);
1611 if (!iface || !iface->mb_ie)
1613 return wpa_snprintf_hex(buf, buflen, wpabuf_head(iface->mb_ie),
1614 wpabuf_len(iface->mb_ie));
1617 return os_snprintf(buf, buflen, "FAIL\n");
1620 #endif /* CONFIG_FST_TEST */