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