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 static 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 /* Action frames sometimes get delayed. Use relaxed timeout (2*) */
185 eloop_register_timeout(0, 2 * TU_TO_US(FST_DEFAULT_SESSION_TIMEOUT_TU),
186 fst_session_timeout_handler, NULL, s);
191 static void fst_session_stt_disarm(struct fst_session *s)
194 eloop_cancel_timeout(fst_session_timeout_handler, NULL, s);
195 s->stt_armed = FALSE;
200 static Boolean fst_session_is_in_transition(struct fst_session *s)
202 /* See spec, 10.32.2.2 Transitioning between states */
207 static int fst_session_is_in_progress(struct fst_session *s)
209 return s->state != FST_SESSION_STATE_INITIAL;
213 static int fst_session_is_ready_pending(struct fst_session *s)
215 return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
216 fst_session_is_in_transition(s);
220 static int fst_session_is_ready(struct fst_session *s)
222 return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
223 !fst_session_is_in_transition(s);
227 static int fst_session_is_switch_requested(struct fst_session *s)
229 return s->state == FST_SESSION_STATE_TRANSITION_DONE &&
230 fst_session_is_in_transition(s);
234 static struct fst_session *
235 fst_find_session_in_progress(const u8 *peer_addr, struct fst_group *g)
237 struct fst_session *s;
239 foreach_fst_session(s) {
241 (os_memcmp(s->data.old_peer_addr, peer_addr,
243 os_memcmp(s->data.new_peer_addr, peer_addr,
245 fst_session_is_in_progress(s))
253 static void fst_session_reset_ex(struct fst_session *s, enum fst_reason reason)
255 union fst_session_state_switch_extra evext = {
261 if (s->state == FST_SESSION_STATE_SETUP_COMPLETION ||
262 s->state == FST_SESSION_STATE_TRANSITION_DONE)
263 fst_session_tear_down_setup(s);
264 fst_session_stt_disarm(s);
265 os_memset(&s->data, 0, sizeof(s->data));
266 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
270 static int fst_session_send_action(struct fst_session *s, Boolean old_iface,
271 const void *payload, size_t size,
272 const struct wpabuf *extra_buf)
278 struct fst_iface *iface =
279 old_iface ? s->data.old_iface : s->data.new_iface;
281 WPA_ASSERT(payload != NULL);
282 WPA_ASSERT(size != 0);
284 action = *(const u8 *) payload;
286 WPA_ASSERT(action <= FST_ACTION_MAX_SUPPORTED);
289 fst_printf_session(s, MSG_ERROR,
290 "no %s interface for FST Action '%s' sending",
291 old_iface ? "old" : "new",
292 fst_action_names[action]);
296 len = sizeof(u8) /* category */ + size;
298 len += wpabuf_size(extra_buf);
300 buf = wpabuf_alloc(len);
302 fst_printf_session(s, MSG_ERROR,
303 "cannot allocate buffer of %zu bytes for FST Action '%s' sending",
304 len, fst_action_names[action]);
308 wpabuf_put_u8(buf, WLAN_ACTION_FST);
309 wpabuf_put_data(buf, payload, size);
311 wpabuf_put_buf(buf, extra_buf);
313 res = fst_iface_send_action(iface,
314 old_iface ? s->data.old_peer_addr :
315 s->data.new_peer_addr, buf);
317 fst_printf_siface(s, iface, MSG_ERROR,
318 "failed to send FST Action '%s'",
319 fst_action_names[action]);
321 fst_printf_siface(s, iface, MSG_DEBUG, "FST Action '%s' sent",
322 fst_action_names[action]);
329 static int fst_session_send_tear_down(struct fst_session *s)
331 struct fst_tear_down td;
334 if (!fst_session_is_in_progress(s)) {
335 fst_printf_session(s, MSG_ERROR, "No FST setup to tear down");
339 WPA_ASSERT(s->data.old_iface != NULL);
340 WPA_ASSERT(s->data.new_iface != NULL);
342 os_memset(&td, 0, sizeof(td));
344 td.action = FST_ACTION_TEAR_DOWN;
345 td.fsts_id = host_to_le32(s->data.fsts_id);
347 res = fst_session_send_action(s, TRUE, &td, sizeof(td), NULL);
349 fst_printf_sframe(s, TRUE, MSG_INFO, "FST TearDown sent");
351 fst_printf_sframe(s, TRUE, MSG_ERROR,
352 "failed to send FST TearDown");
358 static void fst_session_handle_setup_request(struct fst_iface *iface,
359 const struct ieee80211_mgmt *mgmt,
362 struct fst_session *s;
363 const struct fst_setup_req *req;
364 struct fst_iface *new_iface = NULL;
366 u8 new_iface_peer_addr[ETH_ALEN];
367 const struct wpabuf *peer_mbies;
370 if (frame_len < IEEE80211_HDRLEN + 1 + sizeof(*req)) {
371 fst_printf_iface(iface, MSG_WARNING,
372 "FST Request dropped: too short (%zu < %zu)",
374 IEEE80211_HDRLEN + 1 + sizeof(*req));
377 plen = frame_len - IEEE80211_HDRLEN - 1;
378 req = (const struct fst_setup_req *)
379 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
380 if (req->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
381 req->stie.length < 11) {
382 fst_printf_iface(iface, MSG_WARNING,
383 "FST Request dropped: invalid STIE");
387 if (req->stie.new_band_id == req->stie.old_band_id) {
388 fst_printf_iface(iface, MSG_WARNING,
389 "FST Request dropped: new and old band IDs are the same");
393 g = fst_iface_get_group(iface);
395 if (plen > sizeof(*req)) {
396 fst_iface_update_mb_ie(iface, mgmt->sa, (const u8 *) (req + 1),
397 plen - sizeof(*req));
398 fst_printf_iface(iface, MSG_INFO,
399 "FST Request: MB IEs updated for " MACSTR,
403 peer_mbies = fst_iface_get_peer_mb_ie(iface, mgmt->sa);
405 new_iface = fst_group_get_new_iface_by_stie_and_mbie(
406 g, wpabuf_head(peer_mbies), wpabuf_len(peer_mbies),
407 &req->stie, new_iface_peer_addr);
409 fst_printf_iface(iface, MSG_INFO,
410 "FST Request: new iface (%s:" MACSTR
412 fst_iface_get_name(new_iface),
413 MAC2STR(new_iface_peer_addr));
417 new_iface = fst_group_find_new_iface_by_stie(
418 g, iface, mgmt->sa, &req->stie,
419 new_iface_peer_addr);
421 fst_printf_iface(iface, MSG_INFO,
422 "FST Request: new iface (%s:" MACSTR
424 fst_iface_get_name(new_iface),
425 MAC2STR(new_iface_peer_addr));
429 fst_printf_iface(iface, MSG_WARNING,
430 "FST Request dropped: new iface not found");
434 s = fst_find_session_in_progress(mgmt->sa, g);
436 union fst_session_state_switch_extra evext = {
438 .reason = REASON_SETUP,
443 * 10.32.2.2 Transitioning between states:
444 * Upon receipt of an FST Setup Request frame, the responder
445 * shall respond with an FST Setup Response frame unless it has
446 * a pending FST Setup Request frame addressed to the initiator
447 * and the responder has a numerically larger MAC address than
448 * the initiator’s MAC address, in which case, the responder
449 * shall delete the received FST Setup Request.
451 if (fst_session_is_ready_pending(s) &&
452 /* waiting for Setup Response */
453 os_memcmp(mgmt->da, mgmt->sa, ETH_ALEN) > 0) {
454 fst_printf_session(s, MSG_WARNING,
455 "FST Request dropped due to MAC comparison (our MAC is "
462 * State is SETUP_COMPLETION (either in transition or not) or
463 * TRANSITION_DONE (in transition).
464 * Setup Request arriving in this state could mean:
465 * 1. peer sent it before receiving our Setup Request (race
467 * 2. peer didn't receive our Setup Response. Peer is retrying
469 * 3. peer's FST state machines are out of sync due to some
472 * We will reset our session and create a new one instead.
475 fst_printf_session(s, MSG_WARNING,
476 "resetting due to FST request");
479 * If FST Setup Request arrived with the same FSTS ID as one we
480 * initialized before, there's no need to tear down the session.
481 * Moreover, as FSTS ID is the same, the other side will
482 * associate this tear down with the session it initiated that
483 * will break the sync.
485 if (le_to_host32(req->stie.fsts_id) != s->data.fsts_id)
486 fst_session_send_tear_down(s);
488 fst_printf_session(s, MSG_WARNING,
489 "Skipping TearDown as the FST request has the same FSTS ID as initiated");
490 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
491 fst_session_stt_disarm(s);
494 s = fst_session_create(g);
496 fst_printf(MSG_WARNING,
497 "FST Request dropped: cannot create session for %s and %s",
498 fst_iface_get_name(iface),
499 fst_iface_get_name(new_iface));
503 fst_session_set_iface(s, iface, TRUE);
504 fst_session_set_peer_addr(s, mgmt->sa, TRUE);
505 fst_session_set_iface(s, new_iface, FALSE);
506 fst_session_set_peer_addr(s, new_iface_peer_addr, FALSE);
507 fst_session_set_llt(s, FST_LLT_VAL_TO_MS(le_to_host32(req->llt)));
508 s->data.pending_setup_req_dlgt = req->dialog_token;
509 s->data.fsts_id = le_to_host32(req->stie.fsts_id);
511 fst_session_stt_arm(s);
513 fst_session_notify_ctrl(s, EVENT_FST_SETUP, NULL);
515 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION, NULL);
519 static void fst_session_handle_setup_response(struct fst_session *s,
520 struct fst_iface *iface,
521 const struct ieee80211_mgmt *mgmt,
524 const struct fst_setup_res *res;
525 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
526 enum hostapd_hw_mode hw_mode;
528 union fst_session_state_switch_extra evext = {
534 if (iface != s->data.old_iface) {
535 fst_printf_session(s, MSG_WARNING,
536 "FST Response dropped: %s is not the old iface",
537 fst_iface_get_name(iface));
541 if (!fst_session_is_ready_pending(s)) {
542 fst_printf_session(s, MSG_WARNING,
543 "FST Response dropped due to wrong state: %s",
544 fst_session_state_name(s->state));
548 if (plen < sizeof(*res)) {
549 fst_printf_session(s, MSG_WARNING,
550 "Too short FST Response dropped");
553 res = (const struct fst_setup_res *)
554 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
555 if (res->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
556 res->stie.length < 11) {
557 fst_printf_iface(iface, MSG_WARNING,
558 "FST Response dropped: invalid STIE");
562 if (res->dialog_token != s->data.pending_setup_req_dlgt) {
563 fst_printf_session(s, MSG_WARNING,
564 "FST Response dropped due to wrong dialog token (%u != %u)",
565 s->data.pending_setup_req_dlgt,
570 if (res->status_code == WLAN_STATUS_SUCCESS &&
571 le_to_host32(res->stie.fsts_id) != s->data.fsts_id) {
572 fst_printf_session(s, MSG_WARNING,
573 "FST Response dropped due to wrong FST Session ID (%u)",
574 le_to_host32(res->stie.fsts_id));
578 fst_session_stt_disarm(s);
580 if (res->status_code != WLAN_STATUS_SUCCESS) {
582 * 10.32.2.2 Transitioning between states
583 * The initiator shall set the STT to the value of the
584 * FSTSessionTimeOut field at ... and at each ACK frame sent in
585 * response to a received FST Setup Response with the Status
586 * Code field equal to PENDING_ADMITTING_FST_SESSION or
587 * PENDING_GAP_IN_BA_WINDOW.
589 evext.to_initial.reason = REASON_REJECT;
590 evext.to_initial.reject_code = res->status_code;
591 evext.to_initial.initiator = FST_INITIATOR_REMOTE;
592 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
593 fst_printf_session(s, MSG_WARNING,
594 "FST Setup rejected by remote side with status %u",
599 fst_iface_get_channel_info(s->data.new_iface, &hw_mode, &channel);
601 if (fst_hw_mode_to_band(hw_mode) != res->stie.new_band_id) {
602 evext.to_initial.reason = REASON_ERROR_PARAMS;
603 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
604 fst_printf_session(s, MSG_WARNING,
605 "invalid FST Setup parameters");
606 fst_session_tear_down_setup(s);
610 fst_printf_session(s, MSG_INFO,
611 "%s: FST Setup established for %s (llt=%u)",
612 fst_iface_get_name(s->data.old_iface),
613 fst_iface_get_name(s->data.new_iface),
616 fst_session_notify_ctrl(s, EVENT_FST_ESTABLISHED, NULL);
618 if (s->data.llt_ms == FST_LLT_SWITCH_IMMEDIATELY)
619 fst_session_initiate_switch(s);
623 static void fst_session_handle_tear_down(struct fst_session *s,
624 struct fst_iface *iface,
625 const struct ieee80211_mgmt *mgmt,
628 const struct fst_tear_down *td;
629 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
630 union fst_session_state_switch_extra evext = {
632 .reason = REASON_TEARDOWN,
633 .initiator = FST_INITIATOR_REMOTE,
637 if (plen < sizeof(*td)) {
638 fst_printf_session(s, MSG_WARNING,
639 "Too short FST Tear Down dropped");
642 td = (const struct fst_tear_down *)
643 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
645 if (le_to_host32(td->fsts_id) != s->data.fsts_id) {
646 fst_printf_siface(s, iface, MSG_WARNING,
647 "tear down for wrong FST Setup ID (%u)",
648 le_to_host32(td->fsts_id));
652 fst_session_stt_disarm(s);
654 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
658 static void fst_session_handle_ack_request(struct fst_session *s,
659 struct fst_iface *iface,
660 const struct ieee80211_mgmt *mgmt,
663 const struct fst_ack_req *req;
664 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
665 struct fst_ack_res res;
666 union fst_session_state_switch_extra evext = {
668 .reason = REASON_SWITCH,
669 .initiator = FST_INITIATOR_REMOTE,
673 if (!fst_session_is_ready(s) && !fst_session_is_switch_requested(s)) {
674 fst_printf_siface(s, iface, MSG_ERROR,
675 "cannot initiate switch due to wrong session state (%s)",
676 fst_session_state_name(s->state));
680 WPA_ASSERT(s->data.new_iface != NULL);
682 if (iface != s->data.new_iface) {
683 fst_printf_siface(s, iface, MSG_ERROR,
684 "Ack received on wrong interface");
688 if (plen < sizeof(*req)) {
689 fst_printf_session(s, MSG_WARNING,
690 "Too short FST Ack Request dropped");
693 req = (const struct fst_ack_req *)
694 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
696 if (le_to_host32(req->fsts_id) != s->data.fsts_id) {
697 fst_printf_siface(s, iface, MSG_WARNING,
698 "Ack for wrong FST Setup ID (%u)",
699 le_to_host32(req->fsts_id));
703 os_memset(&res, 0, sizeof(res));
705 res.action = FST_ACTION_ACK_RESPONSE;
706 res.dialog_token = req->dialog_token;
707 res.fsts_id = req->fsts_id;
709 if (!fst_session_send_action(s, FALSE, &res, sizeof(res), NULL)) {
710 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Response sent");
711 fst_session_stt_disarm(s);
712 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
714 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED,
716 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
722 fst_session_handle_ack_response(struct fst_session *s,
723 struct fst_iface *iface,
724 const struct ieee80211_mgmt *mgmt,
727 const struct fst_ack_res *res;
728 size_t plen = frame_len - IEEE80211_HDRLEN - 1;
729 union fst_session_state_switch_extra evext = {
731 .reason = REASON_SWITCH,
732 .initiator = FST_INITIATOR_LOCAL,
736 if (!fst_session_is_switch_requested(s)) {
737 fst_printf_siface(s, iface, MSG_ERROR,
738 "Ack Response in inappropriate session state (%s)",
739 fst_session_state_name(s->state));
743 WPA_ASSERT(s->data.new_iface != NULL);
745 if (iface != s->data.new_iface) {
746 fst_printf_siface(s, iface, MSG_ERROR,
747 "Ack response received on wrong interface");
751 if (plen < sizeof(*res)) {
752 fst_printf_session(s, MSG_WARNING,
753 "Too short FST Ack Response dropped");
756 res = (const struct fst_ack_res *)
757 (((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
759 if (le_to_host32(res->fsts_id) != s->data.fsts_id) {
760 fst_printf_siface(s, iface, MSG_ERROR,
761 "Ack response for wrong FST Setup ID (%u)",
762 le_to_host32(res->fsts_id));
766 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED, NULL);
767 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
769 fst_session_stt_disarm(s);
773 struct fst_session * fst_session_create(struct fst_group *g)
775 struct fst_session *s;
778 WPA_ASSERT(!is_zero_ether_addr(own_addr));
780 id = fst_find_free_session_id();
781 if (id == FST_INVALID_SESSION_ID) {
782 fst_printf(MSG_ERROR, "Cannot assign new session ID");
786 s = os_zalloc(sizeof(*s));
788 fst_printf(MSG_ERROR, "Cannot allocate new session object");
794 s->state = FST_SESSION_STATE_INITIAL;
796 s->data.llt_ms = FST_LLT_MS_DEFAULT;
798 fst_printf(MSG_INFO, "Session %u created", s->id);
800 dl_list_add_tail(&global_sessions_list, &s->global_sessions_lentry);
802 foreach_fst_ctrl_call(on_session_added, s);
808 void fst_session_set_iface(struct fst_session *s, struct fst_iface *iface,
812 s->data.old_iface = iface;
814 s->data.new_iface = iface;
819 void fst_session_set_llt(struct fst_session *s, u32 llt)
821 s->data.llt_ms = llt;
825 void fst_session_set_peer_addr(struct fst_session *s, const u8 *addr,
828 u8 *a = is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
830 os_memcpy(a, addr, ETH_ALEN);
834 int fst_session_initiate_setup(struct fst_session *s)
836 struct fst_setup_req req;
840 struct fst_session *_s;
842 if (fst_session_is_in_progress(s)) {
843 fst_printf_session(s, MSG_ERROR, "Session in progress");
847 if (is_zero_ether_addr(s->data.old_peer_addr)) {
848 fst_printf_session(s, MSG_ERROR, "No old peer MAC address");
852 if (is_zero_ether_addr(s->data.new_peer_addr)) {
853 fst_printf_session(s, MSG_ERROR, "No new peer MAC address");
857 if (!s->data.old_iface) {
858 fst_printf_session(s, MSG_ERROR, "No old interface defined");
862 if (!s->data.new_iface) {
863 fst_printf_session(s, MSG_ERROR, "No new interface defined");
867 if (s->data.new_iface == s->data.old_iface) {
868 fst_printf_session(s, MSG_ERROR,
869 "Same interface set as old and new");
873 if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr,
875 fst_printf_session(s, MSG_ERROR,
876 "The preset old peer address is not connected");
880 if (!fst_iface_is_connected(s->data.new_iface, s->data.new_peer_addr,
882 fst_printf_session(s, MSG_ERROR,
883 "The preset new peer address is not connected");
887 _s = fst_find_session_in_progress(s->data.old_peer_addr, s->group);
889 fst_printf_session(s, MSG_ERROR,
890 "There is another session in progress (old): %u",
895 _s = fst_find_session_in_progress(s->data.new_peer_addr, s->group);
897 fst_printf_session(s, MSG_ERROR,
898 "There is another session in progress (new): %u",
903 dialog_token = fst_group_assign_dialog_token(s->group);
904 fsts_id = fst_group_assign_fsts_id(s->group);
906 os_memset(&req, 0, sizeof(req));
908 fst_printf_siface(s, s->data.old_iface, MSG_INFO,
909 "initiating FST setup for %s (llt=%u ms)",
910 fst_iface_get_name(s->data.new_iface), s->data.llt_ms);
912 req.action = FST_ACTION_SETUP_REQUEST;
913 req.dialog_token = dialog_token;
914 req.llt = host_to_le32(FST_LLT_MS_TO_VAL(s->data.llt_ms));
915 /* 8.4.2.147 Session Transition element */
916 req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
917 req.stie.length = sizeof(req.stie) - 2;
918 req.stie.fsts_id = host_to_le32(fsts_id);
919 req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
921 req.stie.new_band_id = fst_iface_get_band_id(s->data.new_iface);
922 req.stie.new_band_op = 1;
923 req.stie.new_band_setup = 0;
925 req.stie.old_band_id = fst_iface_get_band_id(s->data.old_iface);
926 req.stie.old_band_op = 1;
927 req.stie.old_band_setup = 0;
929 res = fst_session_send_action(s, TRUE, &req, sizeof(req),
930 fst_iface_get_mbie(s->data.old_iface));
932 s->data.fsts_id = fsts_id;
933 s->data.pending_setup_req_dlgt = dialog_token;
934 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Request sent");
935 fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION,
938 fst_session_stt_arm(s);
945 int fst_session_respond(struct fst_session *s, u8 status_code)
947 struct fst_setup_res res;
948 enum hostapd_hw_mode hw_mode;
951 if (!fst_session_is_ready_pending(s)) {
952 fst_printf_session(s, MSG_ERROR, "incorrect state: %s",
953 fst_session_state_name(s->state));
957 if (is_zero_ether_addr(s->data.old_peer_addr)) {
958 fst_printf_session(s, MSG_ERROR, "No peer MAC address");
962 if (!s->data.old_iface) {
963 fst_printf_session(s, MSG_ERROR, "No old interface defined");
967 if (!s->data.new_iface) {
968 fst_printf_session(s, MSG_ERROR, "No new interface defined");
972 if (s->data.new_iface == s->data.old_iface) {
973 fst_printf_session(s, MSG_ERROR,
974 "Same interface set as old and new");
978 if (!fst_iface_is_connected(s->data.old_iface,
979 s->data.old_peer_addr, FALSE)) {
980 fst_printf_session(s, MSG_ERROR,
981 "The preset peer address is not in the peer list");
985 fst_session_stt_disarm(s);
987 os_memset(&res, 0, sizeof(res));
989 res.action = FST_ACTION_SETUP_RESPONSE;
990 res.dialog_token = s->data.pending_setup_req_dlgt;
991 res.status_code = status_code;
993 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
994 res.stie.length = sizeof(res.stie) - 2;
996 if (status_code == WLAN_STATUS_SUCCESS) {
997 res.stie.fsts_id = host_to_le32(s->data.fsts_id);
998 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1000 fst_iface_get_channel_info(s->data.new_iface, &hw_mode,
1002 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1003 res.stie.new_band_op = 1;
1004 res.stie.new_band_setup = 0;
1006 fst_iface_get_channel_info(s->data.old_iface, &hw_mode,
1008 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1009 res.stie.old_band_op = 1;
1010 res.stie.old_band_setup = 0;
1012 fst_printf_session(s, MSG_INFO,
1013 "%s: FST Setup Request accepted for %s (llt=%u)",
1014 fst_iface_get_name(s->data.old_iface),
1015 fst_iface_get_name(s->data.new_iface),
1018 fst_printf_session(s, MSG_WARNING,
1019 "%s: FST Setup Request rejected with code %d",
1020 fst_iface_get_name(s->data.old_iface),
1024 if (fst_session_send_action(s, TRUE, &res, sizeof(res),
1025 fst_iface_get_mbie(s->data.old_iface))) {
1026 fst_printf_sframe(s, TRUE, MSG_ERROR,
1027 "cannot send FST Setup Response with code %d",
1032 fst_printf_sframe(s, TRUE, MSG_INFO, "FST Setup Response sent");
1034 if (status_code != WLAN_STATUS_SUCCESS) {
1035 union fst_session_state_switch_extra evext = {
1037 .reason = REASON_REJECT,
1038 .reject_code = status_code,
1039 .initiator = FST_INITIATOR_LOCAL,
1042 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1049 int fst_session_initiate_switch(struct fst_session *s)
1051 struct fst_ack_req req;
1055 if (!fst_session_is_ready(s)) {
1056 fst_printf_session(s, MSG_ERROR,
1057 "cannot initiate switch due to wrong setup state (%d)",
1062 dialog_token = fst_group_assign_dialog_token(s->group);
1064 WPA_ASSERT(s->data.new_iface != NULL);
1065 WPA_ASSERT(s->data.old_iface != NULL);
1067 fst_printf_session(s, MSG_INFO, "initiating FST switch: %s => %s",
1068 fst_iface_get_name(s->data.old_iface),
1069 fst_iface_get_name(s->data.new_iface));
1071 os_memset(&req, 0, sizeof(req));
1073 req.action = FST_ACTION_ACK_REQUEST;
1074 req.dialog_token = dialog_token;
1075 req.fsts_id = host_to_le32(s->data.fsts_id);
1077 res = fst_session_send_action(s, FALSE, &req, sizeof(req), NULL);
1079 fst_printf_sframe(s, FALSE, MSG_INFO, "FST Ack Request sent");
1080 fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
1082 fst_session_stt_arm(s);
1084 fst_printf_sframe(s, FALSE, MSG_ERROR,
1085 "Cannot send FST Ack Request");
1092 void fst_session_handle_action(struct fst_session *s,
1093 struct fst_iface *iface,
1094 const struct ieee80211_mgmt *mgmt,
1097 switch (mgmt->u.action.u.fst_action.action) {
1098 case FST_ACTION_SETUP_REQUEST:
1101 case FST_ACTION_SETUP_RESPONSE:
1102 fst_session_handle_setup_response(s, iface, mgmt, frame_len);
1104 case FST_ACTION_TEAR_DOWN:
1105 fst_session_handle_tear_down(s, iface, mgmt, frame_len);
1107 case FST_ACTION_ACK_REQUEST:
1108 fst_session_handle_ack_request(s, iface, mgmt, frame_len);
1110 case FST_ACTION_ACK_RESPONSE:
1111 fst_session_handle_ack_response(s, iface, mgmt, frame_len);
1113 case FST_ACTION_ON_CHANNEL_TUNNEL:
1115 fst_printf_sframe(s, FALSE, MSG_ERROR,
1116 "Unsupported FST Action frame");
1122 int fst_session_tear_down_setup(struct fst_session *s)
1125 union fst_session_state_switch_extra evext = {
1127 .reason = REASON_TEARDOWN,
1128 .initiator = FST_INITIATOR_LOCAL,
1132 res = fst_session_send_tear_down(s);
1134 fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1140 void fst_session_reset(struct fst_session *s)
1142 fst_session_reset_ex(s, REASON_RESET);
1146 void fst_session_delete(struct fst_session *s)
1148 fst_printf(MSG_INFO, "Session %u deleted", s->id);
1149 dl_list_del(&s->global_sessions_lentry);
1150 foreach_fst_ctrl_call(on_session_removed, s);
1155 struct fst_group * fst_session_get_group(struct fst_session *s)
1161 struct fst_iface * fst_session_get_iface(struct fst_session *s, Boolean is_old)
1163 return is_old ? s->data.old_iface : s->data.new_iface;
1167 u32 fst_session_get_id(struct fst_session *s)
1173 const u8 * fst_session_get_peer_addr(struct fst_session *s, Boolean is_old)
1175 return is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
1179 u32 fst_session_get_llt(struct fst_session *s)
1181 return s->data.llt_ms;
1185 enum fst_session_state fst_session_get_state(struct fst_session *s)
1191 struct fst_session * fst_session_get_by_id(u32 id)
1193 struct fst_session *s;
1195 foreach_fst_session(s) {
1204 void fst_session_enum(struct fst_group *g, fst_session_enum_clb clb, void *ctx)
1206 struct fst_session *s;
1208 foreach_fst_session(s) {
1209 if (!g || s->group == g)
1210 clb(s->group, s, ctx);
1215 void fst_session_on_action_rx(struct fst_iface *iface,
1216 const struct ieee80211_mgmt *mgmt,
1219 struct fst_session *s;
1221 if (len < IEEE80211_HDRLEN + 2 ||
1222 mgmt->u.action.category != WLAN_ACTION_FST) {
1223 fst_printf_iface(iface, MSG_ERROR,
1224 "invalid Action frame received");
1228 if (mgmt->u.action.u.fst_action.action <= FST_ACTION_MAX_SUPPORTED) {
1229 fst_printf_iface(iface, MSG_DEBUG,
1230 "FST Action '%s' received!",
1231 fst_action_names[mgmt->u.action.u.fst_action.action]);
1233 fst_printf_iface(iface, MSG_WARNING,
1234 "unknown FST Action (%u) received!",
1235 mgmt->u.action.u.fst_action.action);
1239 if (mgmt->u.action.u.fst_action.action == FST_ACTION_SETUP_REQUEST) {
1240 fst_session_handle_setup_request(iface, mgmt, len);
1244 s = fst_find_session_in_progress(mgmt->sa, fst_iface_get_group(iface));
1246 fst_session_handle_action(s, iface, mgmt, len);
1248 fst_printf_iface(iface, MSG_WARNING,
1249 "FST Action '%s' dropped: no session in progress found",
1250 fst_action_names[mgmt->u.action.u.fst_action.action]);
1255 int fst_session_set_str_ifname(struct fst_session *s, const char *ifname,
1258 struct fst_group *g = fst_session_get_group(s);
1259 struct fst_iface *i;
1261 i = fst_group_get_iface_by_name(g, ifname);
1263 fst_printf_session(s, MSG_WARNING,
1264 "Cannot set iface %s: no such iface within group '%s'",
1265 ifname, fst_group_get_id(g));
1269 fst_session_set_iface(s, i, is_old);
1275 int fst_session_set_str_peer_addr(struct fst_session *s, const char *mac,
1278 u8 peer_addr[ETH_ALEN];
1279 int res = fst_read_peer_addr(mac, peer_addr);
1284 fst_session_set_peer_addr(s, peer_addr, is_old);
1290 int fst_session_set_str_llt(struct fst_session *s, const char *llt_str)
1293 long int llt = strtol(llt_str, &endp, 0);
1295 if (*endp || llt < 0 || (unsigned long int) llt > FST_MAX_LLT_MS) {
1296 fst_printf_session(s, MSG_WARNING,
1297 "Cannot set llt %s: Invalid llt value (1..%u expected)",
1298 llt_str, FST_MAX_LLT_MS);
1301 fst_session_set_llt(s, (u32) llt);
1307 void fst_session_global_on_iface_detached(struct fst_iface *iface)
1309 struct fst_session *s;
1311 foreach_fst_session(s) {
1312 if (fst_session_is_in_progress(s) &&
1313 (s->data.new_iface == iface ||
1314 s->data.old_iface == iface))
1315 fst_session_reset_ex(s, REASON_DETACH_IFACE);
1320 struct fst_session * fst_session_global_get_first_by_group(struct fst_group *g)
1322 struct fst_session *s;
1324 foreach_fst_session(s) {
1333 #ifdef CONFIG_FST_TEST
1335 static int get_group_fill_session(struct fst_group **g, struct fst_session *s)
1337 const u8 *old_addr, *new_addr;
1338 struct fst_get_peer_ctx *ctx;
1340 os_memset(s, 0, sizeof(*s));
1341 foreach_fst_group(*g) {
1342 s->data.new_iface = fst_group_first_iface(*g);
1343 if (s->data.new_iface)
1346 if (!s->data.new_iface)
1349 s->data.old_iface = dl_list_entry(s->data.new_iface->group_lentry.next,
1350 struct fst_iface, group_lentry);
1351 if (!s->data.old_iface)
1354 old_addr = fst_iface_get_peer_first(s->data.old_iface, &ctx, TRUE);
1358 new_addr = fst_iface_get_peer_first(s->data.new_iface, &ctx, TRUE);
1362 os_memcpy(s->data.old_peer_addr, old_addr, ETH_ALEN);
1363 os_memcpy(s->data.new_peer_addr, new_addr, ETH_ALEN);
1369 #define FST_MAX_COMMAND_WORD_NAME_LENGTH 16
1371 int fst_test_req_send_fst_request(const char *params)
1376 struct fst_setup_req req;
1377 struct fst_session s;
1378 struct fst_group *g;
1379 enum hostapd_hw_mode hw_mode;
1381 char additional_param[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1383 if (params[0] != ' ')
1386 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1390 if (get_group_fill_session(&g, &s))
1393 req.action = FST_ACTION_SETUP_REQUEST;
1394 req.dialog_token = g->dialog_token;
1395 req.llt = host_to_le32(FST_LLT_MS_DEFAULT);
1396 /* 8.4.2.147 Session Transition element */
1397 req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1398 req.stie.length = sizeof(req.stie) - 2;
1399 req.stie.fsts_id = host_to_le32(fsts_id);
1400 req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1402 fst_iface_get_channel_info(s.data.new_iface, &hw_mode, &channel);
1403 req.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1404 req.stie.new_band_op = 1;
1405 req.stie.new_band_setup = 0;
1407 fst_iface_get_channel_info(s.data.old_iface, &hw_mode, &channel);
1408 req.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1409 req.stie.old_band_op = 1;
1410 req.stie.old_band_setup = 0;
1412 if (!fst_read_next_text_param(endp, additional_param,
1413 sizeof(additional_param), &endp)) {
1414 if (!os_strcasecmp(additional_param, FST_CTR_PVAL_BAD_NEW_BAND))
1415 req.stie.new_band_id = req.stie.old_band_id;
1418 return fst_session_send_action(&s, TRUE, &req, sizeof(req),
1419 s.data.old_iface->mb_ie);
1423 int fst_test_req_send_fst_response(const char *params)
1428 struct fst_setup_res res;
1429 struct fst_session s;
1430 struct fst_group *g;
1431 enum hostapd_hw_mode hw_mode;
1434 char response[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1435 struct fst_session *_s;
1437 if (params[0] != ' ')
1440 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1444 if (get_group_fill_session(&g, &s))
1447 status_code = WLAN_STATUS_SUCCESS;
1448 if (!fst_read_next_text_param(endp, response, sizeof(response),
1450 if (!os_strcasecmp(response, FST_CS_PVAL_RESPONSE_REJECT))
1451 status_code = WLAN_STATUS_PENDING_ADMITTING_FST_SESSION;
1454 os_memset(&res, 0, sizeof(res));
1456 res.action = FST_ACTION_SETUP_RESPONSE;
1458 * If some session has just received an FST Setup Request, then
1459 * use the correct dialog token copied from this request.
1461 _s = fst_find_session_in_progress(fst_session_get_peer_addr(&s, TRUE),
1463 res.dialog_token = (_s && fst_session_is_ready_pending(_s)) ?
1464 _s->data.pending_setup_req_dlgt : g->dialog_token;
1465 res.status_code = status_code;
1467 res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1468 res.stie.length = sizeof(res.stie) - 2;
1470 if (res.status_code == WLAN_STATUS_SUCCESS) {
1471 res.stie.fsts_id = host_to_le32(fsts_id);
1472 res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1474 fst_iface_get_channel_info(s.data.new_iface, &hw_mode,
1476 res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1477 res.stie.new_band_op = 1;
1478 res.stie.new_band_setup = 0;
1480 fst_iface_get_channel_info(s.data.old_iface, &hw_mode,
1482 res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1483 res.stie.old_band_op = 1;
1484 res.stie.old_band_setup = 0;
1487 if (!fst_read_next_text_param(endp, response, sizeof(response),
1489 if (!os_strcasecmp(response, FST_CTR_PVAL_BAD_NEW_BAND))
1490 res.stie.new_band_id = res.stie.old_band_id;
1493 return fst_session_send_action(&s, TRUE, &res, sizeof(res),
1494 s.data.old_iface->mb_ie);
1498 int fst_test_req_send_ack_request(const char *params)
1503 struct fst_ack_req req;
1504 struct fst_session s;
1505 struct fst_group *g;
1507 if (params[0] != ' ')
1510 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1514 if (get_group_fill_session(&g, &s))
1517 os_memset(&req, 0, sizeof(req));
1518 req.action = FST_ACTION_ACK_REQUEST;
1519 req.dialog_token = g->dialog_token;
1520 req.fsts_id = host_to_le32(fsts_id);
1522 return fst_session_send_action(&s, FALSE, &req, sizeof(req), NULL);
1526 int fst_test_req_send_ack_response(const char *params)
1531 struct fst_ack_res res;
1532 struct fst_session s;
1533 struct fst_group *g;
1535 if (params[0] != ' ')
1538 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1542 if (get_group_fill_session(&g, &s))
1545 os_memset(&res, 0, sizeof(res));
1546 res.action = FST_ACTION_ACK_RESPONSE;
1547 res.dialog_token = g->dialog_token;
1548 res.fsts_id = host_to_le32(fsts_id);
1550 return fst_session_send_action(&s, FALSE, &res, sizeof(res), NULL);
1554 int fst_test_req_send_tear_down(const char *params)
1559 struct fst_tear_down td;
1560 struct fst_session s;
1561 struct fst_group *g;
1563 if (params[0] != ' ')
1566 fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1570 if (get_group_fill_session(&g, &s))
1573 os_memset(&td, 0, sizeof(td));
1574 td.action = FST_ACTION_TEAR_DOWN;
1575 td.fsts_id = host_to_le32(fsts_id);
1577 return fst_session_send_action(&s, TRUE, &td, sizeof(td), NULL);
1581 u32 fst_test_req_get_fsts_id(const char *params)
1586 struct fst_session *s;
1588 if (params[0] != ' ')
1589 return FST_FSTS_ID_NOT_FOUND;
1591 sid = fst_read_next_int_param(params, &is_valid, &endp);
1593 return FST_FSTS_ID_NOT_FOUND;
1595 s = fst_session_get_by_id(sid);
1597 return FST_FSTS_ID_NOT_FOUND;
1599 return s->data.fsts_id;
1603 int fst_test_req_get_local_mbies(const char *request, char *buf, size_t buflen)
1606 char ifname[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1607 struct fst_group *g;
1608 struct fst_iface *iface;
1610 if (request[0] != ' ')
1613 if (fst_read_next_text_param(request, ifname, sizeof(ifname), &endp) ||
1616 g = dl_list_first(&fst_global_groups_list, struct fst_group,
1617 global_groups_lentry);
1620 iface = fst_group_get_iface_by_name(g, ifname);
1621 if (!iface || !iface->mb_ie)
1623 return wpa_snprintf_hex(buf, buflen, wpabuf_head(iface->mb_ie),
1624 wpabuf_len(iface->mb_ie));
1627 return os_snprintf(buf, buflen, "FAIL\n");
1630 #endif /* CONFIG_FST_TEST */