xref: /freebsd/contrib/wpa/src/fst/fst_session.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1325151a3SRui Paulo /*
2325151a3SRui Paulo  * FST module - FST Session implementation
3325151a3SRui Paulo  * Copyright (c) 2014, Qualcomm Atheros, Inc.
4325151a3SRui Paulo  *
5325151a3SRui Paulo  * This software may be distributed under the terms of the BSD license.
6325151a3SRui Paulo  * See README for more details.
7325151a3SRui Paulo  */
8325151a3SRui Paulo 
9325151a3SRui Paulo #include "utils/includes.h"
10325151a3SRui Paulo 
11325151a3SRui Paulo #include "utils/common.h"
12325151a3SRui Paulo #include "utils/eloop.h"
13325151a3SRui Paulo #include "common/defs.h"
14325151a3SRui Paulo #include "fst/fst_internal.h"
15325151a3SRui Paulo #include "fst/fst_defs.h"
16325151a3SRui Paulo #include "fst/fst_ctrl_iface.h"
17325151a3SRui Paulo #ifdef CONFIG_FST_TEST
18325151a3SRui Paulo #include "fst/fst_ctrl_defs.h"
19325151a3SRui Paulo #endif /* CONFIG_FST_TEST */
20325151a3SRui Paulo 
21325151a3SRui Paulo #define US_80211_TU 1024
22325151a3SRui Paulo 
23325151a3SRui Paulo #define US_TO_TU(m) ((m) * / US_80211_TU)
24325151a3SRui Paulo #define TU_TO_US(m) ((m) * US_80211_TU)
25325151a3SRui Paulo 
26325151a3SRui Paulo #define FST_LLT_SWITCH_IMMEDIATELY 0
27325151a3SRui Paulo 
28325151a3SRui Paulo #define fst_printf_session(s, level, format, ...) \
29325151a3SRui Paulo 	fst_printf((level), "%u (0x%08x): [" MACSTR "," MACSTR "] :" format, \
30325151a3SRui Paulo 		   (s)->id, (s)->data.fsts_id, \
31325151a3SRui Paulo 		   MAC2STR((s)->data.old_peer_addr), \
32325151a3SRui Paulo 		   MAC2STR((s)->data.new_peer_addr), \
33325151a3SRui Paulo 		   ##__VA_ARGS__)
34325151a3SRui Paulo 
35325151a3SRui Paulo #define fst_printf_siface(s, iface, level, format, ...) \
36325151a3SRui Paulo 	fst_printf_session((s), (level), "%s: " format, \
37325151a3SRui Paulo 			   fst_iface_get_name(iface), ##__VA_ARGS__)
38325151a3SRui Paulo 
39325151a3SRui Paulo #define fst_printf_sframe(s, is_old, level, format, ...) \
40325151a3SRui Paulo 	fst_printf_siface((s), \
41325151a3SRui Paulo 		(is_old) ? (s)->data.old_iface : (s)->data.new_iface, \
42325151a3SRui Paulo 		(level), format, ##__VA_ARGS__)
43325151a3SRui Paulo 
44325151a3SRui Paulo #define FST_LLT_MS_DEFAULT 50
45325151a3SRui Paulo #define FST_ACTION_MAX_SUPPORTED   FST_ACTION_ON_CHANNEL_TUNNEL
46325151a3SRui Paulo 
47780fb4a2SCy Schubert static const char * const fst_action_names[] = {
48325151a3SRui Paulo 	[FST_ACTION_SETUP_REQUEST]     = "Setup Request",
49325151a3SRui Paulo 	[FST_ACTION_SETUP_RESPONSE]    = "Setup Response",
50325151a3SRui Paulo 	[FST_ACTION_TEAR_DOWN]         = "Tear Down",
51325151a3SRui Paulo 	[FST_ACTION_ACK_REQUEST]       = "Ack Request",
52325151a3SRui Paulo 	[FST_ACTION_ACK_RESPONSE]      = "Ack Response",
53325151a3SRui Paulo 	[FST_ACTION_ON_CHANNEL_TUNNEL] = "On Channel Tunnel",
54325151a3SRui Paulo };
55325151a3SRui Paulo 
56325151a3SRui Paulo struct fst_session {
57325151a3SRui Paulo 	struct {
58325151a3SRui Paulo 		/* Session configuration that can be zeroed on reset */
59325151a3SRui Paulo 		u8 old_peer_addr[ETH_ALEN];
60325151a3SRui Paulo 		u8 new_peer_addr[ETH_ALEN];
61325151a3SRui Paulo 		struct fst_iface *new_iface;
62325151a3SRui Paulo 		struct fst_iface *old_iface;
63325151a3SRui Paulo 		u32 llt_ms;
64325151a3SRui Paulo 		u8 pending_setup_req_dlgt;
65325151a3SRui Paulo 		u32 fsts_id; /* FSTS ID, see spec, 8.4.2.147
66325151a3SRui Paulo 			      * Session Transition element */
67325151a3SRui Paulo 	} data;
68325151a3SRui Paulo 	/* Session object internal fields which won't be zeroed on reset */
69325151a3SRui Paulo 	struct dl_list global_sessions_lentry;
70325151a3SRui Paulo 	u32 id; /* Session object ID used to identify
71325151a3SRui Paulo 		 * specific session object */
72325151a3SRui Paulo 	struct fst_group *group;
73325151a3SRui Paulo 	enum fst_session_state state;
74c1d255d3SCy Schubert 	bool stt_armed;
75325151a3SRui Paulo };
76325151a3SRui Paulo 
77325151a3SRui Paulo static struct dl_list global_sessions_list;
78325151a3SRui Paulo static u32 global_session_id = 0;
79325151a3SRui Paulo 
80325151a3SRui Paulo #define foreach_fst_session(s) \
81325151a3SRui Paulo 	dl_list_for_each((s), &global_sessions_list, \
82325151a3SRui Paulo 			 struct fst_session, global_sessions_lentry)
83325151a3SRui Paulo 
84325151a3SRui Paulo #define foreach_fst_session_safe(s, temp) \
85325151a3SRui Paulo 	dl_list_for_each_safe((s), (temp), &global_sessions_list, \
86325151a3SRui Paulo 			      struct fst_session, global_sessions_lentry)
87325151a3SRui Paulo 
88325151a3SRui Paulo 
fst_session_global_inc_id(void)89325151a3SRui Paulo static void fst_session_global_inc_id(void)
90325151a3SRui Paulo {
91325151a3SRui Paulo 	global_session_id++;
92325151a3SRui Paulo 	if (global_session_id == FST_INVALID_SESSION_ID)
93325151a3SRui Paulo 		global_session_id++;
94325151a3SRui Paulo }
95325151a3SRui Paulo 
96325151a3SRui Paulo 
fst_session_global_init(void)97325151a3SRui Paulo int fst_session_global_init(void)
98325151a3SRui Paulo {
99325151a3SRui Paulo 	dl_list_init(&global_sessions_list);
100325151a3SRui Paulo 	return 0;
101325151a3SRui Paulo }
102325151a3SRui Paulo 
103325151a3SRui Paulo 
fst_session_global_deinit(void)104325151a3SRui Paulo void fst_session_global_deinit(void)
105325151a3SRui Paulo {
106325151a3SRui Paulo 	WPA_ASSERT(dl_list_empty(&global_sessions_list));
107325151a3SRui Paulo }
108325151a3SRui Paulo 
109325151a3SRui Paulo 
fst_session_notify_ctrl(struct fst_session * s,enum fst_event_type event_type,union fst_event_extra * extra)110325151a3SRui Paulo static inline void fst_session_notify_ctrl(struct fst_session *s,
111325151a3SRui Paulo 					   enum fst_event_type event_type,
112325151a3SRui Paulo 					   union fst_event_extra *extra)
113325151a3SRui Paulo {
114325151a3SRui Paulo 	foreach_fst_ctrl_call(on_event, event_type, NULL, s, extra);
115325151a3SRui Paulo }
116325151a3SRui Paulo 
117325151a3SRui Paulo 
fst_session_set_state(struct fst_session * s,enum fst_session_state state,union fst_session_state_switch_extra * extra)118325151a3SRui Paulo static void fst_session_set_state(struct fst_session *s,
119325151a3SRui Paulo 				  enum fst_session_state state,
120325151a3SRui Paulo 				  union fst_session_state_switch_extra *extra)
121325151a3SRui Paulo {
122325151a3SRui Paulo 	if (s->state != state) {
123325151a3SRui Paulo 		union fst_event_extra evext = {
124325151a3SRui Paulo 			.session_state = {
125325151a3SRui Paulo 				.old_state = s->state,
126325151a3SRui Paulo 				.new_state = state,
127325151a3SRui Paulo 			},
128325151a3SRui Paulo 		};
129325151a3SRui Paulo 
130325151a3SRui Paulo 		if (extra)
131325151a3SRui Paulo 			evext.session_state.extra = *extra;
132325151a3SRui Paulo 		fst_session_notify_ctrl(s, EVENT_FST_SESSION_STATE_CHANGED,
133325151a3SRui Paulo 					&evext);
134325151a3SRui Paulo 		fst_printf_session(s, MSG_INFO, "State: %s => %s",
135325151a3SRui Paulo 				   fst_session_state_name(s->state),
136325151a3SRui Paulo 				   fst_session_state_name(state));
137325151a3SRui Paulo 		s->state = state;
138325151a3SRui Paulo 	}
139325151a3SRui Paulo }
140325151a3SRui Paulo 
141325151a3SRui Paulo 
fst_find_free_session_id(void)142325151a3SRui Paulo static u32 fst_find_free_session_id(void)
143325151a3SRui Paulo {
144325151a3SRui Paulo 	u32 i, id = FST_INVALID_SESSION_ID;
145325151a3SRui Paulo 	struct fst_session *s;
146325151a3SRui Paulo 
147325151a3SRui Paulo 	for (i = 0; i < (u32) -1; i++) {
148c1d255d3SCy Schubert 		bool in_use = false;
149325151a3SRui Paulo 
150325151a3SRui Paulo 		foreach_fst_session(s) {
151325151a3SRui Paulo 			if (s->id == global_session_id) {
152325151a3SRui Paulo 				fst_session_global_inc_id();
153c1d255d3SCy Schubert 				in_use = true;
154325151a3SRui Paulo 				break;
155325151a3SRui Paulo 			}
156325151a3SRui Paulo 		}
157325151a3SRui Paulo 		if (!in_use) {
158325151a3SRui Paulo 			id = global_session_id;
159325151a3SRui Paulo 			fst_session_global_inc_id();
160325151a3SRui Paulo 			break;
161325151a3SRui Paulo 		}
162325151a3SRui Paulo 	}
163325151a3SRui Paulo 
164325151a3SRui Paulo 	return id;
165325151a3SRui Paulo }
166325151a3SRui Paulo 
167325151a3SRui Paulo 
fst_session_timeout_handler(void * eloop_data,void * user_ctx)168325151a3SRui Paulo static void fst_session_timeout_handler(void *eloop_data, void *user_ctx)
169325151a3SRui Paulo {
170325151a3SRui Paulo 	struct fst_session *s = user_ctx;
171325151a3SRui Paulo 	union fst_session_state_switch_extra extra = {
172325151a3SRui Paulo 		.to_initial = {
173325151a3SRui Paulo 			.reason = REASON_STT,
174325151a3SRui Paulo 		},
175325151a3SRui Paulo 	};
176325151a3SRui Paulo 
177325151a3SRui Paulo 	fst_printf_session(s, MSG_WARNING, "Session State Timeout");
178325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &extra);
179325151a3SRui Paulo }
180325151a3SRui Paulo 
181325151a3SRui Paulo 
fst_session_stt_arm(struct fst_session * s)182325151a3SRui Paulo static void fst_session_stt_arm(struct fst_session *s)
183325151a3SRui Paulo {
184780fb4a2SCy Schubert 	/* Action frames sometimes get delayed. Use relaxed timeout (2*) */
185780fb4a2SCy Schubert 	eloop_register_timeout(0, 2 * TU_TO_US(FST_DEFAULT_SESSION_TIMEOUT_TU),
186325151a3SRui Paulo 			       fst_session_timeout_handler, NULL, s);
187c1d255d3SCy Schubert 	s->stt_armed = true;
188325151a3SRui Paulo }
189325151a3SRui Paulo 
190325151a3SRui Paulo 
fst_session_stt_disarm(struct fst_session * s)191325151a3SRui Paulo static void fst_session_stt_disarm(struct fst_session *s)
192325151a3SRui Paulo {
193325151a3SRui Paulo 	if (s->stt_armed) {
194325151a3SRui Paulo 		eloop_cancel_timeout(fst_session_timeout_handler, NULL, s);
195c1d255d3SCy Schubert 		s->stt_armed = false;
196325151a3SRui Paulo 	}
197325151a3SRui Paulo }
198325151a3SRui Paulo 
199325151a3SRui Paulo 
fst_session_is_in_transition(struct fst_session * s)200c1d255d3SCy Schubert static bool fst_session_is_in_transition(struct fst_session *s)
201325151a3SRui Paulo {
202325151a3SRui Paulo 	/* See spec, 10.32.2.2  Transitioning between states */
203325151a3SRui Paulo 	return s->stt_armed;
204325151a3SRui Paulo }
205325151a3SRui Paulo 
206325151a3SRui Paulo 
fst_session_is_in_progress(struct fst_session * s)207325151a3SRui Paulo static int fst_session_is_in_progress(struct fst_session *s)
208325151a3SRui Paulo {
209325151a3SRui Paulo 	return s->state != FST_SESSION_STATE_INITIAL;
210325151a3SRui Paulo }
211325151a3SRui Paulo 
212325151a3SRui Paulo 
fst_session_is_ready_pending(struct fst_session * s)213325151a3SRui Paulo static int fst_session_is_ready_pending(struct fst_session *s)
214325151a3SRui Paulo {
215325151a3SRui Paulo 	return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
216325151a3SRui Paulo 		fst_session_is_in_transition(s);
217325151a3SRui Paulo }
218325151a3SRui Paulo 
219325151a3SRui Paulo 
fst_session_is_ready(struct fst_session * s)220325151a3SRui Paulo static int fst_session_is_ready(struct fst_session *s)
221325151a3SRui Paulo {
222325151a3SRui Paulo 	return s->state == FST_SESSION_STATE_SETUP_COMPLETION &&
223325151a3SRui Paulo 		!fst_session_is_in_transition(s);
224325151a3SRui Paulo }
225325151a3SRui Paulo 
226325151a3SRui Paulo 
fst_session_is_switch_requested(struct fst_session * s)227325151a3SRui Paulo static int fst_session_is_switch_requested(struct fst_session *s)
228325151a3SRui Paulo {
229325151a3SRui Paulo 	return s->state == FST_SESSION_STATE_TRANSITION_DONE &&
230325151a3SRui Paulo 		fst_session_is_in_transition(s);
231325151a3SRui Paulo }
232325151a3SRui Paulo 
233325151a3SRui Paulo 
234325151a3SRui Paulo static struct fst_session *
fst_find_session_in_progress(const u8 * peer_addr,struct fst_group * g)235325151a3SRui Paulo fst_find_session_in_progress(const u8 *peer_addr, struct fst_group *g)
236325151a3SRui Paulo {
237325151a3SRui Paulo 	struct fst_session *s;
238325151a3SRui Paulo 
239325151a3SRui Paulo 	foreach_fst_session(s) {
240325151a3SRui Paulo 		if (s->group == g &&
241*a90b9d01SCy Schubert 		    (ether_addr_equal(s->data.old_peer_addr, peer_addr) ||
242*a90b9d01SCy Schubert 		     ether_addr_equal(s->data.new_peer_addr, peer_addr)) &&
243325151a3SRui Paulo 		    fst_session_is_in_progress(s))
244325151a3SRui Paulo 			return s;
245325151a3SRui Paulo 	}
246325151a3SRui Paulo 
247325151a3SRui Paulo 	return NULL;
248325151a3SRui Paulo }
249325151a3SRui Paulo 
250325151a3SRui Paulo 
fst_session_reset_ex(struct fst_session * s,enum fst_reason reason)251325151a3SRui Paulo static void fst_session_reset_ex(struct fst_session *s, enum fst_reason reason)
252325151a3SRui Paulo {
253325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
254325151a3SRui Paulo 		.to_initial = {
255325151a3SRui Paulo 			.reason = reason,
256325151a3SRui Paulo 		},
257325151a3SRui Paulo 	};
258325151a3SRui Paulo 
259325151a3SRui Paulo 	if (s->state == FST_SESSION_STATE_SETUP_COMPLETION ||
260325151a3SRui Paulo 	    s->state == FST_SESSION_STATE_TRANSITION_DONE)
261325151a3SRui Paulo 		fst_session_tear_down_setup(s);
262325151a3SRui Paulo 	fst_session_stt_disarm(s);
263325151a3SRui Paulo 	os_memset(&s->data, 0, sizeof(s->data));
264325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
265325151a3SRui Paulo }
266325151a3SRui Paulo 
267325151a3SRui Paulo 
fst_session_send_action(struct fst_session * s,bool old_iface,const void * payload,size_t size,const struct wpabuf * extra_buf)268c1d255d3SCy Schubert static int fst_session_send_action(struct fst_session *s, bool old_iface,
269325151a3SRui Paulo 				   const void *payload, size_t size,
270325151a3SRui Paulo 				   const struct wpabuf *extra_buf)
271325151a3SRui Paulo {
272325151a3SRui Paulo 	size_t len;
273325151a3SRui Paulo 	int res;
274325151a3SRui Paulo 	struct wpabuf *buf;
275325151a3SRui Paulo 	u8 action;
276325151a3SRui Paulo 	struct fst_iface *iface =
277325151a3SRui Paulo 		old_iface ? s->data.old_iface : s->data.new_iface;
278325151a3SRui Paulo 
279325151a3SRui Paulo 	WPA_ASSERT(payload != NULL);
280325151a3SRui Paulo 	WPA_ASSERT(size != 0);
281325151a3SRui Paulo 
282325151a3SRui Paulo 	action = *(const u8 *) payload;
283325151a3SRui Paulo 
284325151a3SRui Paulo 	WPA_ASSERT(action <= FST_ACTION_MAX_SUPPORTED);
285325151a3SRui Paulo 
286325151a3SRui Paulo 	if (!iface) {
287325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
288325151a3SRui Paulo 				   "no %s interface for FST Action '%s' sending",
289325151a3SRui Paulo 				   old_iface ? "old" : "new",
290325151a3SRui Paulo 				   fst_action_names[action]);
291325151a3SRui Paulo 		return -1;
292325151a3SRui Paulo 	}
293325151a3SRui Paulo 
294325151a3SRui Paulo 	len = sizeof(u8) /* category */ + size;
295325151a3SRui Paulo 	if (extra_buf)
296325151a3SRui Paulo 		len += wpabuf_size(extra_buf);
297325151a3SRui Paulo 
298325151a3SRui Paulo 	buf = wpabuf_alloc(len);
299325151a3SRui Paulo 	if (!buf) {
300325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
301325151a3SRui Paulo 				   "cannot allocate buffer of %zu bytes for FST Action '%s' sending",
302325151a3SRui Paulo 				   len, fst_action_names[action]);
303325151a3SRui Paulo 		return -1;
304325151a3SRui Paulo 	}
305325151a3SRui Paulo 
306325151a3SRui Paulo 	wpabuf_put_u8(buf, WLAN_ACTION_FST);
307325151a3SRui Paulo 	wpabuf_put_data(buf, payload, size);
308325151a3SRui Paulo 	if (extra_buf)
309325151a3SRui Paulo 		wpabuf_put_buf(buf, extra_buf);
310325151a3SRui Paulo 
311325151a3SRui Paulo 	res = fst_iface_send_action(iface,
312325151a3SRui Paulo 				    old_iface ? s->data.old_peer_addr :
313325151a3SRui Paulo 				    s->data.new_peer_addr, buf);
314325151a3SRui Paulo 	if (res < 0)
315325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
316325151a3SRui Paulo 				  "failed to send FST Action '%s'",
317325151a3SRui Paulo 				  fst_action_names[action]);
318325151a3SRui Paulo 	else
319325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_DEBUG, "FST Action '%s' sent",
320325151a3SRui Paulo 				  fst_action_names[action]);
321325151a3SRui Paulo 	wpabuf_free(buf);
322325151a3SRui Paulo 
323325151a3SRui Paulo 	return res;
324325151a3SRui Paulo }
325325151a3SRui Paulo 
326325151a3SRui Paulo 
fst_session_send_tear_down(struct fst_session * s)327325151a3SRui Paulo static int fst_session_send_tear_down(struct fst_session *s)
328325151a3SRui Paulo {
329325151a3SRui Paulo 	struct fst_tear_down td;
330325151a3SRui Paulo 	int res;
331325151a3SRui Paulo 
332325151a3SRui Paulo 	if (!fst_session_is_in_progress(s)) {
333325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No FST setup to tear down");
334325151a3SRui Paulo 		return -1;
335325151a3SRui Paulo 	}
336325151a3SRui Paulo 
337325151a3SRui Paulo 	WPA_ASSERT(s->data.old_iface != NULL);
338325151a3SRui Paulo 	WPA_ASSERT(s->data.new_iface != NULL);
339325151a3SRui Paulo 
340325151a3SRui Paulo 	os_memset(&td, 0, sizeof(td));
341325151a3SRui Paulo 
342325151a3SRui Paulo 	td.action = FST_ACTION_TEAR_DOWN;
343325151a3SRui Paulo 	td.fsts_id = host_to_le32(s->data.fsts_id);
344325151a3SRui Paulo 
345c1d255d3SCy Schubert 	res = fst_session_send_action(s, true, &td, sizeof(td), NULL);
346325151a3SRui Paulo 	if (!res)
347c1d255d3SCy Schubert 		fst_printf_sframe(s, true, MSG_INFO, "FST TearDown sent");
348325151a3SRui Paulo 	else
349c1d255d3SCy Schubert 		fst_printf_sframe(s, true, MSG_ERROR,
350325151a3SRui Paulo 				  "failed to send FST TearDown");
351325151a3SRui Paulo 
352325151a3SRui Paulo 	return res;
353325151a3SRui Paulo }
354325151a3SRui Paulo 
355325151a3SRui Paulo 
fst_session_handle_setup_request(struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)356325151a3SRui Paulo static void fst_session_handle_setup_request(struct fst_iface *iface,
357325151a3SRui Paulo 					     const struct ieee80211_mgmt *mgmt,
358325151a3SRui Paulo 					     size_t frame_len)
359325151a3SRui Paulo {
360325151a3SRui Paulo 	struct fst_session *s;
361325151a3SRui Paulo 	const struct fst_setup_req *req;
362325151a3SRui Paulo 	struct fst_iface *new_iface = NULL;
363325151a3SRui Paulo 	struct fst_group *g;
364325151a3SRui Paulo 	u8 new_iface_peer_addr[ETH_ALEN];
365325151a3SRui Paulo 	size_t plen;
366325151a3SRui Paulo 
367325151a3SRui Paulo 	if (frame_len < IEEE80211_HDRLEN + 1 + sizeof(*req))  {
368325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
369325151a3SRui Paulo 				 "FST Request dropped: too short (%zu < %zu)",
370325151a3SRui Paulo 				 frame_len,
371325151a3SRui Paulo 				 IEEE80211_HDRLEN + 1 + sizeof(*req));
372325151a3SRui Paulo 		return;
373325151a3SRui Paulo 	}
374325151a3SRui Paulo 	plen = frame_len - IEEE80211_HDRLEN - 1;
375325151a3SRui Paulo 	req = (const struct fst_setup_req *)
376325151a3SRui Paulo 		(((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
377325151a3SRui Paulo 	if (req->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
378325151a3SRui Paulo 	    req->stie.length < 11) {
379325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
380325151a3SRui Paulo 				 "FST Request dropped: invalid STIE");
381325151a3SRui Paulo 		return;
382325151a3SRui Paulo 	}
383325151a3SRui Paulo 
384325151a3SRui Paulo 	if (req->stie.new_band_id == req->stie.old_band_id) {
385325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
386325151a3SRui Paulo 				 "FST Request dropped: new and old band IDs are the same");
387325151a3SRui Paulo 		return;
388325151a3SRui Paulo 	}
389325151a3SRui Paulo 
390325151a3SRui Paulo 	g = fst_iface_get_group(iface);
391325151a3SRui Paulo 
392325151a3SRui Paulo 	if (plen > sizeof(*req)) {
393325151a3SRui Paulo 		fst_iface_update_mb_ie(iface, mgmt->sa, (const u8 *) (req + 1),
394325151a3SRui Paulo 				       plen - sizeof(*req));
395325151a3SRui Paulo 		fst_printf_iface(iface, MSG_INFO,
396325151a3SRui Paulo 				 "FST Request: MB IEs updated for " MACSTR,
397325151a3SRui Paulo 				 MAC2STR(mgmt->sa));
398325151a3SRui Paulo 	}
399325151a3SRui Paulo 
400780fb4a2SCy Schubert 	new_iface = fst_group_get_peer_other_connection(iface, mgmt->sa,
401780fb4a2SCy Schubert 							req->stie.new_band_id,
402325151a3SRui Paulo 							new_iface_peer_addr);
403325151a3SRui Paulo 	if (!new_iface) {
404325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
405325151a3SRui Paulo 				 "FST Request dropped: new iface not found");
406325151a3SRui Paulo 		return;
407325151a3SRui Paulo 	}
408780fb4a2SCy Schubert 	fst_printf_iface(iface, MSG_INFO,
409780fb4a2SCy Schubert 			 "FST Request: new iface (%s:" MACSTR ") found",
410780fb4a2SCy Schubert 			 fst_iface_get_name(new_iface),
411780fb4a2SCy Schubert 			 MAC2STR(new_iface_peer_addr));
412325151a3SRui Paulo 
413325151a3SRui Paulo 	s = fst_find_session_in_progress(mgmt->sa, g);
414325151a3SRui Paulo 	if (s) {
415325151a3SRui Paulo 		union fst_session_state_switch_extra evext = {
416325151a3SRui Paulo 			.to_initial = {
417325151a3SRui Paulo 				.reason = REASON_SETUP,
418325151a3SRui Paulo 			},
419325151a3SRui Paulo 		};
420325151a3SRui Paulo 
421325151a3SRui Paulo 		/*
422325151a3SRui Paulo 		 * 10.32.2.2  Transitioning between states:
423325151a3SRui Paulo 		 * Upon receipt of an FST Setup Request frame, the responder
424325151a3SRui Paulo 		 * shall respond with an FST Setup Response frame unless it has
425325151a3SRui Paulo 		 * a pending FST Setup Request frame addressed to the initiator
426325151a3SRui Paulo 		 * and the responder has a numerically larger MAC address than
427325151a3SRui Paulo 		 * the initiator’s MAC address, in which case, the responder
428325151a3SRui Paulo 		 * shall delete the received FST Setup Request.
429325151a3SRui Paulo 		 */
430780fb4a2SCy Schubert 		if (fst_session_is_ready_pending(s) &&
431780fb4a2SCy Schubert 		    /* waiting for Setup Response */
432780fb4a2SCy Schubert 		    os_memcmp(mgmt->da, mgmt->sa, ETH_ALEN) > 0) {
433325151a3SRui Paulo 			fst_printf_session(s, MSG_WARNING,
434325151a3SRui Paulo 					   "FST Request dropped due to MAC comparison (our MAC is "
435325151a3SRui Paulo 					   MACSTR ")",
436325151a3SRui Paulo 					   MAC2STR(mgmt->da));
437325151a3SRui Paulo 			return;
438325151a3SRui Paulo 		}
439325151a3SRui Paulo 
440780fb4a2SCy Schubert 		/*
441780fb4a2SCy Schubert 		 * State is SETUP_COMPLETION (either in transition or not) or
442780fb4a2SCy Schubert 		 * TRANSITION_DONE (in transition).
443780fb4a2SCy Schubert 		 * Setup Request arriving in this state could mean:
444780fb4a2SCy Schubert 		 * 1. peer sent it before receiving our Setup Request (race
445780fb4a2SCy Schubert 		 *    condition)
446780fb4a2SCy Schubert 		 * 2. peer didn't receive our Setup Response. Peer is retrying
447780fb4a2SCy Schubert 		 *    after STT timeout
448780fb4a2SCy Schubert 		 * 3. peer's FST state machines are out of sync due to some
449780fb4a2SCy Schubert 		 *    other reason
450780fb4a2SCy Schubert 		 *
451780fb4a2SCy Schubert 		 * We will reset our session and create a new one instead.
452780fb4a2SCy Schubert 		 */
453325151a3SRui Paulo 
454780fb4a2SCy Schubert 		fst_printf_session(s, MSG_WARNING,
455780fb4a2SCy Schubert 			"resetting due to FST request");
456325151a3SRui Paulo 
457325151a3SRui Paulo 		/*
458325151a3SRui Paulo 		 * If FST Setup Request arrived with the same FSTS ID as one we
459780fb4a2SCy Schubert 		 * initialized before, there's no need to tear down the session.
460325151a3SRui Paulo 		 * Moreover, as FSTS ID is the same, the other side will
461325151a3SRui Paulo 		 * associate this tear down with the session it initiated that
462325151a3SRui Paulo 		 * will break the sync.
463325151a3SRui Paulo 		 */
464325151a3SRui Paulo 		if (le_to_host32(req->stie.fsts_id) != s->data.fsts_id)
465325151a3SRui Paulo 			fst_session_send_tear_down(s);
466325151a3SRui Paulo 		else
467325151a3SRui Paulo 			fst_printf_session(s, MSG_WARNING,
468325151a3SRui Paulo 					   "Skipping TearDown as the FST request has the same FSTS ID as initiated");
469325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
470325151a3SRui Paulo 		fst_session_stt_disarm(s);
471325151a3SRui Paulo 	}
472325151a3SRui Paulo 
473325151a3SRui Paulo 	s = fst_session_create(g);
474325151a3SRui Paulo 	if (!s) {
475325151a3SRui Paulo 		fst_printf(MSG_WARNING,
476325151a3SRui Paulo 			   "FST Request dropped: cannot create session for %s and %s",
477325151a3SRui Paulo 			   fst_iface_get_name(iface),
478325151a3SRui Paulo 			   fst_iface_get_name(new_iface));
479325151a3SRui Paulo 		return;
480325151a3SRui Paulo 	}
481325151a3SRui Paulo 
482c1d255d3SCy Schubert 	fst_session_set_iface(s, iface, true);
483c1d255d3SCy Schubert 	fst_session_set_peer_addr(s, mgmt->sa, true);
484c1d255d3SCy Schubert 	fst_session_set_iface(s, new_iface, false);
485c1d255d3SCy Schubert 	fst_session_set_peer_addr(s, new_iface_peer_addr, false);
486325151a3SRui Paulo 	fst_session_set_llt(s, FST_LLT_VAL_TO_MS(le_to_host32(req->llt)));
487325151a3SRui Paulo 	s->data.pending_setup_req_dlgt = req->dialog_token;
488325151a3SRui Paulo 	s->data.fsts_id = le_to_host32(req->stie.fsts_id);
489325151a3SRui Paulo 
490325151a3SRui Paulo 	fst_session_stt_arm(s);
491325151a3SRui Paulo 
492325151a3SRui Paulo 	fst_session_notify_ctrl(s, EVENT_FST_SETUP, NULL);
493325151a3SRui Paulo 
494325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION, NULL);
495325151a3SRui Paulo }
496325151a3SRui Paulo 
497325151a3SRui Paulo 
fst_session_handle_setup_response(struct fst_session * s,struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)498325151a3SRui Paulo static void fst_session_handle_setup_response(struct fst_session *s,
499325151a3SRui Paulo 					      struct fst_iface *iface,
500325151a3SRui Paulo 					      const struct ieee80211_mgmt *mgmt,
501325151a3SRui Paulo 					      size_t frame_len)
502325151a3SRui Paulo {
503325151a3SRui Paulo 	const struct fst_setup_res *res;
504325151a3SRui Paulo 	size_t plen = frame_len - IEEE80211_HDRLEN - 1;
505325151a3SRui Paulo 	enum hostapd_hw_mode hw_mode;
506325151a3SRui Paulo 	u8 channel;
507325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
508780fb4a2SCy Schubert 		.to_initial = {
509780fb4a2SCy Schubert 			.reject_code = 0,
510780fb4a2SCy Schubert 		},
511325151a3SRui Paulo 	};
512325151a3SRui Paulo 
513325151a3SRui Paulo 	if (iface != s->data.old_iface) {
514325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
515325151a3SRui Paulo 				   "FST Response dropped: %s is not the old iface",
516325151a3SRui Paulo 				   fst_iface_get_name(iface));
517325151a3SRui Paulo 		return;
518325151a3SRui Paulo 	}
519325151a3SRui Paulo 
520325151a3SRui Paulo 	if (!fst_session_is_ready_pending(s)) {
521325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
522325151a3SRui Paulo 				   "FST Response dropped due to wrong state: %s",
523325151a3SRui Paulo 				   fst_session_state_name(s->state));
524325151a3SRui Paulo 		return;
525325151a3SRui Paulo 	}
526325151a3SRui Paulo 
527325151a3SRui Paulo 	if (plen < sizeof(*res)) {
528325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
529325151a3SRui Paulo 				   "Too short FST Response dropped");
530325151a3SRui Paulo 		return;
531325151a3SRui Paulo 	}
532325151a3SRui Paulo 	res = (const struct fst_setup_res *)
533325151a3SRui Paulo 		(((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
534325151a3SRui Paulo 	if (res->stie.element_id != WLAN_EID_SESSION_TRANSITION ||
535325151a3SRui Paulo 	    res->stie.length < 11) {
536325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
537325151a3SRui Paulo 				 "FST Response dropped: invalid STIE");
538325151a3SRui Paulo 		return;
539325151a3SRui Paulo 	}
540325151a3SRui Paulo 
541325151a3SRui Paulo 	if (res->dialog_token != s->data.pending_setup_req_dlgt)  {
542325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
543325151a3SRui Paulo 				   "FST Response dropped due to wrong dialog token (%u != %u)",
544325151a3SRui Paulo 				   s->data.pending_setup_req_dlgt,
545325151a3SRui Paulo 				   res->dialog_token);
546325151a3SRui Paulo 		return;
547325151a3SRui Paulo 	}
548325151a3SRui Paulo 
549325151a3SRui Paulo 	if (res->status_code == WLAN_STATUS_SUCCESS &&
550325151a3SRui Paulo 	    le_to_host32(res->stie.fsts_id) != s->data.fsts_id) {
551325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
552325151a3SRui Paulo 				   "FST Response dropped due to wrong FST Session ID (%u)",
553325151a3SRui Paulo 				   le_to_host32(res->stie.fsts_id));
554325151a3SRui Paulo 		return;
555325151a3SRui Paulo 	}
556325151a3SRui Paulo 
557325151a3SRui Paulo 	fst_session_stt_disarm(s);
558325151a3SRui Paulo 
559325151a3SRui Paulo 	if (res->status_code != WLAN_STATUS_SUCCESS) {
560325151a3SRui Paulo 		/*
561325151a3SRui Paulo 		 * 10.32.2.2  Transitioning between states
562325151a3SRui Paulo 		 * The initiator shall set the STT to the value of the
563325151a3SRui Paulo 		 * FSTSessionTimeOut field at ... and at each ACK frame sent in
564325151a3SRui Paulo 		 * response to a received FST Setup Response with the Status
565325151a3SRui Paulo 		 * Code field equal to PENDING_ADMITTING_FST_SESSION or
566325151a3SRui Paulo 		 * PENDING_GAP_IN_BA_WINDOW.
567325151a3SRui Paulo 		 */
568325151a3SRui Paulo 		evext.to_initial.reason = REASON_REJECT;
569325151a3SRui Paulo 		evext.to_initial.reject_code = res->status_code;
570325151a3SRui Paulo 		evext.to_initial.initiator = FST_INITIATOR_REMOTE;
571325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
572325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
573325151a3SRui Paulo 				   "FST Setup rejected by remote side with status %u",
574325151a3SRui Paulo 				   res->status_code);
575325151a3SRui Paulo 		return;
576325151a3SRui Paulo 	}
577325151a3SRui Paulo 
578325151a3SRui Paulo 	fst_iface_get_channel_info(s->data.new_iface, &hw_mode, &channel);
579325151a3SRui Paulo 
580325151a3SRui Paulo 	if (fst_hw_mode_to_band(hw_mode) != res->stie.new_band_id) {
581325151a3SRui Paulo 		evext.to_initial.reason = REASON_ERROR_PARAMS;
582325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
583325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
584325151a3SRui Paulo 				   "invalid FST Setup parameters");
585325151a3SRui Paulo 		fst_session_tear_down_setup(s);
586325151a3SRui Paulo 		return;
587325151a3SRui Paulo 	}
588325151a3SRui Paulo 
589325151a3SRui Paulo 	fst_printf_session(s, MSG_INFO,
590325151a3SRui Paulo 			   "%s: FST Setup established for %s (llt=%u)",
591325151a3SRui Paulo 			   fst_iface_get_name(s->data.old_iface),
592325151a3SRui Paulo 			   fst_iface_get_name(s->data.new_iface),
593325151a3SRui Paulo 			   s->data.llt_ms);
594325151a3SRui Paulo 
595325151a3SRui Paulo 	fst_session_notify_ctrl(s, EVENT_FST_ESTABLISHED, NULL);
596325151a3SRui Paulo 
597325151a3SRui Paulo 	if (s->data.llt_ms == FST_LLT_SWITCH_IMMEDIATELY)
598325151a3SRui Paulo 		fst_session_initiate_switch(s);
599325151a3SRui Paulo }
600325151a3SRui Paulo 
601325151a3SRui Paulo 
fst_session_handle_tear_down(struct fst_session * s,struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)602325151a3SRui Paulo static void fst_session_handle_tear_down(struct fst_session *s,
603325151a3SRui Paulo 					 struct fst_iface *iface,
604325151a3SRui Paulo 					 const struct ieee80211_mgmt *mgmt,
605325151a3SRui Paulo 					 size_t frame_len)
606325151a3SRui Paulo {
607325151a3SRui Paulo 	const struct fst_tear_down *td;
608325151a3SRui Paulo 	size_t plen = frame_len - IEEE80211_HDRLEN - 1;
609325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
610325151a3SRui Paulo 		.to_initial = {
611325151a3SRui Paulo 			.reason = REASON_TEARDOWN,
612325151a3SRui Paulo 			.initiator = FST_INITIATOR_REMOTE,
613325151a3SRui Paulo 		},
614325151a3SRui Paulo 	};
615325151a3SRui Paulo 
616325151a3SRui Paulo 	if (plen < sizeof(*td)) {
617325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
618325151a3SRui Paulo 				   "Too short FST Tear Down dropped");
619325151a3SRui Paulo 		return;
620325151a3SRui Paulo 	}
621325151a3SRui Paulo 	td = (const struct fst_tear_down *)
622325151a3SRui Paulo 		(((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
623325151a3SRui Paulo 
624325151a3SRui Paulo 	if (le_to_host32(td->fsts_id) != s->data.fsts_id) {
625325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_WARNING,
626325151a3SRui Paulo 				  "tear down for wrong FST Setup ID (%u)",
627325151a3SRui Paulo 				  le_to_host32(td->fsts_id));
628325151a3SRui Paulo 		return;
629325151a3SRui Paulo 	}
630325151a3SRui Paulo 
631325151a3SRui Paulo 	fst_session_stt_disarm(s);
632325151a3SRui Paulo 
633325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
634325151a3SRui Paulo }
635325151a3SRui Paulo 
636325151a3SRui Paulo 
fst_session_handle_ack_request(struct fst_session * s,struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)637325151a3SRui Paulo static void fst_session_handle_ack_request(struct fst_session *s,
638325151a3SRui Paulo 					   struct fst_iface *iface,
639325151a3SRui Paulo 					   const struct ieee80211_mgmt *mgmt,
640325151a3SRui Paulo 					   size_t frame_len)
641325151a3SRui Paulo {
642325151a3SRui Paulo 	const struct fst_ack_req *req;
643325151a3SRui Paulo 	size_t plen = frame_len - IEEE80211_HDRLEN - 1;
644325151a3SRui Paulo 	struct fst_ack_res res;
645325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
646325151a3SRui Paulo 		.to_initial = {
647325151a3SRui Paulo 			.reason = REASON_SWITCH,
648325151a3SRui Paulo 			.initiator = FST_INITIATOR_REMOTE,
649325151a3SRui Paulo 		},
650325151a3SRui Paulo 	};
651325151a3SRui Paulo 
652325151a3SRui Paulo 	if (!fst_session_is_ready(s) && !fst_session_is_switch_requested(s)) {
653325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
654325151a3SRui Paulo 				  "cannot initiate switch due to wrong session state (%s)",
655325151a3SRui Paulo 				  fst_session_state_name(s->state));
656325151a3SRui Paulo 		return;
657325151a3SRui Paulo 	}
658325151a3SRui Paulo 
659325151a3SRui Paulo 	WPA_ASSERT(s->data.new_iface != NULL);
660325151a3SRui Paulo 
661325151a3SRui Paulo 	if (iface != s->data.new_iface) {
662325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
663325151a3SRui Paulo 				  "Ack received on wrong interface");
664325151a3SRui Paulo 		return;
665325151a3SRui Paulo 	}
666325151a3SRui Paulo 
667325151a3SRui Paulo 	if (plen < sizeof(*req)) {
668325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
669325151a3SRui Paulo 				   "Too short FST Ack Request dropped");
670325151a3SRui Paulo 		return;
671325151a3SRui Paulo 	}
672325151a3SRui Paulo 	req = (const struct fst_ack_req *)
673325151a3SRui Paulo 		(((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
674325151a3SRui Paulo 
675325151a3SRui Paulo 	if (le_to_host32(req->fsts_id) != s->data.fsts_id) {
676325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_WARNING,
677325151a3SRui Paulo 				  "Ack for wrong FST Setup ID (%u)",
678325151a3SRui Paulo 				  le_to_host32(req->fsts_id));
679325151a3SRui Paulo 		return;
680325151a3SRui Paulo 	}
681325151a3SRui Paulo 
682325151a3SRui Paulo 	os_memset(&res, 0, sizeof(res));
683325151a3SRui Paulo 
684325151a3SRui Paulo 	res.action = FST_ACTION_ACK_RESPONSE;
685325151a3SRui Paulo 	res.dialog_token = req->dialog_token;
686325151a3SRui Paulo 	res.fsts_id = req->fsts_id;
687325151a3SRui Paulo 
688c1d255d3SCy Schubert 	if (!fst_session_send_action(s, false, &res, sizeof(res), NULL)) {
689c1d255d3SCy Schubert 		fst_printf_sframe(s, false, MSG_INFO, "FST Ack Response sent");
690325151a3SRui Paulo 		fst_session_stt_disarm(s);
691325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
692325151a3SRui Paulo 				      NULL);
693325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED,
694325151a3SRui Paulo 				      NULL);
695325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
696325151a3SRui Paulo 	}
697325151a3SRui Paulo }
698325151a3SRui Paulo 
699325151a3SRui Paulo 
700325151a3SRui Paulo static void
fst_session_handle_ack_response(struct fst_session * s,struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)701325151a3SRui Paulo fst_session_handle_ack_response(struct fst_session *s,
702325151a3SRui Paulo 				struct fst_iface *iface,
703325151a3SRui Paulo 				const struct ieee80211_mgmt *mgmt,
704325151a3SRui Paulo 				size_t frame_len)
705325151a3SRui Paulo {
706325151a3SRui Paulo 	const struct fst_ack_res *res;
707325151a3SRui Paulo 	size_t plen = frame_len - IEEE80211_HDRLEN - 1;
708325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
709325151a3SRui Paulo 		.to_initial = {
710325151a3SRui Paulo 			.reason = REASON_SWITCH,
711325151a3SRui Paulo 			.initiator = FST_INITIATOR_LOCAL,
712325151a3SRui Paulo 		},
713325151a3SRui Paulo 	};
714325151a3SRui Paulo 
715325151a3SRui Paulo 	if (!fst_session_is_switch_requested(s)) {
716325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
717325151a3SRui Paulo 				  "Ack Response in inappropriate session state (%s)",
718325151a3SRui Paulo 				  fst_session_state_name(s->state));
719325151a3SRui Paulo 		return;
720325151a3SRui Paulo 	}
721325151a3SRui Paulo 
722325151a3SRui Paulo 	WPA_ASSERT(s->data.new_iface != NULL);
723325151a3SRui Paulo 
724325151a3SRui Paulo 	if (iface != s->data.new_iface) {
725325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
726325151a3SRui Paulo 				  "Ack response received on wrong interface");
727325151a3SRui Paulo 		return;
728325151a3SRui Paulo 	}
729325151a3SRui Paulo 
730325151a3SRui Paulo 	if (plen < sizeof(*res)) {
731325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
732325151a3SRui Paulo 				   "Too short FST Ack Response dropped");
733325151a3SRui Paulo 		return;
734325151a3SRui Paulo 	}
735325151a3SRui Paulo 	res = (const struct fst_ack_res *)
736325151a3SRui Paulo 		(((const u8 *) mgmt) + IEEE80211_HDRLEN + 1);
737325151a3SRui Paulo 
738325151a3SRui Paulo 	if (le_to_host32(res->fsts_id) != s->data.fsts_id) {
739325151a3SRui Paulo 		fst_printf_siface(s, iface, MSG_ERROR,
740325151a3SRui Paulo 				  "Ack response for wrong FST Setup ID (%u)",
741325151a3SRui Paulo 				  le_to_host32(res->fsts_id));
742325151a3SRui Paulo 		return;
743325151a3SRui Paulo 	}
744325151a3SRui Paulo 
745325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_CONFIRMED, NULL);
746325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
747325151a3SRui Paulo 
748325151a3SRui Paulo 	fst_session_stt_disarm(s);
749325151a3SRui Paulo }
750325151a3SRui Paulo 
751325151a3SRui Paulo 
fst_session_create(struct fst_group * g)752325151a3SRui Paulo struct fst_session * fst_session_create(struct fst_group *g)
753325151a3SRui Paulo {
754325151a3SRui Paulo 	struct fst_session *s;
755325151a3SRui Paulo 	u32 id;
756325151a3SRui Paulo 
757325151a3SRui Paulo 	id = fst_find_free_session_id();
758325151a3SRui Paulo 	if (id == FST_INVALID_SESSION_ID) {
759325151a3SRui Paulo 		fst_printf(MSG_ERROR, "Cannot assign new session ID");
760325151a3SRui Paulo 		return NULL;
761325151a3SRui Paulo 	}
762325151a3SRui Paulo 
763325151a3SRui Paulo 	s = os_zalloc(sizeof(*s));
764325151a3SRui Paulo 	if (!s) {
765325151a3SRui Paulo 		fst_printf(MSG_ERROR, "Cannot allocate new session object");
766325151a3SRui Paulo 		return NULL;
767325151a3SRui Paulo 	}
768325151a3SRui Paulo 
769325151a3SRui Paulo 	s->id = id;
770325151a3SRui Paulo 	s->group = g;
771325151a3SRui Paulo 	s->state = FST_SESSION_STATE_INITIAL;
772325151a3SRui Paulo 
773325151a3SRui Paulo 	s->data.llt_ms = FST_LLT_MS_DEFAULT;
774325151a3SRui Paulo 
775325151a3SRui Paulo 	fst_printf(MSG_INFO, "Session %u created", s->id);
776325151a3SRui Paulo 
777325151a3SRui Paulo 	dl_list_add_tail(&global_sessions_list, &s->global_sessions_lentry);
778325151a3SRui Paulo 
779325151a3SRui Paulo 	foreach_fst_ctrl_call(on_session_added, s);
780325151a3SRui Paulo 
781325151a3SRui Paulo 	return s;
782325151a3SRui Paulo }
783325151a3SRui Paulo 
784325151a3SRui Paulo 
fst_session_set_iface(struct fst_session * s,struct fst_iface * iface,bool is_old)785325151a3SRui Paulo void fst_session_set_iface(struct fst_session *s, struct fst_iface *iface,
786c1d255d3SCy Schubert 			   bool is_old)
787325151a3SRui Paulo {
788325151a3SRui Paulo 	if (is_old)
789325151a3SRui Paulo 		s->data.old_iface = iface;
790325151a3SRui Paulo 	else
791325151a3SRui Paulo 		s->data.new_iface = iface;
792325151a3SRui Paulo 
793325151a3SRui Paulo }
794325151a3SRui Paulo 
795325151a3SRui Paulo 
fst_session_set_llt(struct fst_session * s,u32 llt)796325151a3SRui Paulo void fst_session_set_llt(struct fst_session *s, u32 llt)
797325151a3SRui Paulo {
798325151a3SRui Paulo 	s->data.llt_ms = llt;
799325151a3SRui Paulo }
800325151a3SRui Paulo 
801325151a3SRui Paulo 
fst_session_set_peer_addr(struct fst_session * s,const u8 * addr,bool is_old)802325151a3SRui Paulo void fst_session_set_peer_addr(struct fst_session *s, const u8 *addr,
803c1d255d3SCy Schubert 			       bool is_old)
804325151a3SRui Paulo {
805325151a3SRui Paulo 	u8 *a = is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
806325151a3SRui Paulo 
807325151a3SRui Paulo 	os_memcpy(a, addr, ETH_ALEN);
808325151a3SRui Paulo }
809325151a3SRui Paulo 
810325151a3SRui Paulo 
fst_session_initiate_setup(struct fst_session * s)811325151a3SRui Paulo int fst_session_initiate_setup(struct fst_session *s)
812325151a3SRui Paulo {
813325151a3SRui Paulo 	struct fst_setup_req req;
814325151a3SRui Paulo 	int res;
815325151a3SRui Paulo 	u32 fsts_id;
816325151a3SRui Paulo 	u8 dialog_token;
817325151a3SRui Paulo 	struct fst_session *_s;
818325151a3SRui Paulo 
819325151a3SRui Paulo 	if (fst_session_is_in_progress(s)) {
820325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "Session in progress");
821325151a3SRui Paulo 		return -EINVAL;
822325151a3SRui Paulo 	}
823325151a3SRui Paulo 
824325151a3SRui Paulo 	if (is_zero_ether_addr(s->data.old_peer_addr)) {
825325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No old peer MAC address");
826325151a3SRui Paulo 		return -EINVAL;
827325151a3SRui Paulo 	}
828325151a3SRui Paulo 
829325151a3SRui Paulo 	if (is_zero_ether_addr(s->data.new_peer_addr)) {
830325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No new peer MAC address");
831325151a3SRui Paulo 		return -EINVAL;
832325151a3SRui Paulo 	}
833325151a3SRui Paulo 
834325151a3SRui Paulo 	if (!s->data.old_iface) {
835325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No old interface defined");
836325151a3SRui Paulo 		return -EINVAL;
837325151a3SRui Paulo 	}
838325151a3SRui Paulo 
839325151a3SRui Paulo 	if (!s->data.new_iface) {
840325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No new interface defined");
841325151a3SRui Paulo 		return -EINVAL;
842325151a3SRui Paulo 	}
843325151a3SRui Paulo 
844325151a3SRui Paulo 	if (s->data.new_iface == s->data.old_iface) {
845325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
846325151a3SRui Paulo 				   "Same interface set as old and new");
847325151a3SRui Paulo 		return -EINVAL;
848325151a3SRui Paulo 	}
849325151a3SRui Paulo 
850780fb4a2SCy Schubert 	if (!fst_iface_is_connected(s->data.old_iface, s->data.old_peer_addr,
851c1d255d3SCy Schubert 				    false)) {
852325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
853325151a3SRui Paulo 				   "The preset old peer address is not connected");
854325151a3SRui Paulo 		return -EINVAL;
855325151a3SRui Paulo 	}
856325151a3SRui Paulo 
857780fb4a2SCy Schubert 	if (!fst_iface_is_connected(s->data.new_iface, s->data.new_peer_addr,
858c1d255d3SCy Schubert 				    false)) {
859325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
860325151a3SRui Paulo 				   "The preset new peer address is not connected");
861325151a3SRui Paulo 		return -EINVAL;
862325151a3SRui Paulo 	}
863325151a3SRui Paulo 
864325151a3SRui Paulo 	_s = fst_find_session_in_progress(s->data.old_peer_addr, s->group);
865325151a3SRui Paulo 	if (_s) {
866325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
867325151a3SRui Paulo 				   "There is another session in progress (old): %u",
868325151a3SRui Paulo 				   _s->id);
869325151a3SRui Paulo 		return -EINVAL;
870325151a3SRui Paulo 	}
871325151a3SRui Paulo 
872325151a3SRui Paulo 	_s = fst_find_session_in_progress(s->data.new_peer_addr, s->group);
873325151a3SRui Paulo 	if (_s) {
874325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
875325151a3SRui Paulo 				   "There is another session in progress (new): %u",
876325151a3SRui Paulo 				   _s->id);
877325151a3SRui Paulo 		return -EINVAL;
878325151a3SRui Paulo 	}
879325151a3SRui Paulo 
880325151a3SRui Paulo 	dialog_token = fst_group_assign_dialog_token(s->group);
881325151a3SRui Paulo 	fsts_id = fst_group_assign_fsts_id(s->group);
882325151a3SRui Paulo 
883325151a3SRui Paulo 	os_memset(&req, 0, sizeof(req));
884325151a3SRui Paulo 
885325151a3SRui Paulo 	fst_printf_siface(s, s->data.old_iface, MSG_INFO,
886325151a3SRui Paulo 		"initiating FST setup for %s (llt=%u ms)",
887325151a3SRui Paulo 		fst_iface_get_name(s->data.new_iface), s->data.llt_ms);
888325151a3SRui Paulo 
889325151a3SRui Paulo 	req.action = FST_ACTION_SETUP_REQUEST;
890325151a3SRui Paulo 	req.dialog_token = dialog_token;
891325151a3SRui Paulo 	req.llt = host_to_le32(FST_LLT_MS_TO_VAL(s->data.llt_ms));
892325151a3SRui Paulo 	/* 8.4.2.147 Session Transition element */
893325151a3SRui Paulo 	req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
894325151a3SRui Paulo 	req.stie.length = sizeof(req.stie) - 2;
895325151a3SRui Paulo 	req.stie.fsts_id = host_to_le32(fsts_id);
896325151a3SRui Paulo 	req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
897325151a3SRui Paulo 
898325151a3SRui Paulo 	req.stie.new_band_id = fst_iface_get_band_id(s->data.new_iface);
899325151a3SRui Paulo 	req.stie.new_band_op = 1;
900325151a3SRui Paulo 	req.stie.new_band_setup = 0;
901325151a3SRui Paulo 
902325151a3SRui Paulo 	req.stie.old_band_id = fst_iface_get_band_id(s->data.old_iface);
903325151a3SRui Paulo 	req.stie.old_band_op = 1;
904325151a3SRui Paulo 	req.stie.old_band_setup = 0;
905325151a3SRui Paulo 
906c1d255d3SCy Schubert 	res = fst_session_send_action(s, true, &req, sizeof(req),
907325151a3SRui Paulo 				      fst_iface_get_mbie(s->data.old_iface));
908325151a3SRui Paulo 	if (!res) {
909325151a3SRui Paulo 		s->data.fsts_id = fsts_id;
910325151a3SRui Paulo 		s->data.pending_setup_req_dlgt = dialog_token;
911c1d255d3SCy Schubert 		fst_printf_sframe(s, true, MSG_INFO, "FST Setup Request sent");
912325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_SETUP_COMPLETION,
913325151a3SRui Paulo 				      NULL);
914325151a3SRui Paulo 
915325151a3SRui Paulo 		fst_session_stt_arm(s);
916325151a3SRui Paulo 	}
917325151a3SRui Paulo 
918325151a3SRui Paulo 	return res;
919325151a3SRui Paulo }
920325151a3SRui Paulo 
921325151a3SRui Paulo 
fst_session_respond(struct fst_session * s,u8 status_code)922325151a3SRui Paulo int fst_session_respond(struct fst_session *s, u8 status_code)
923325151a3SRui Paulo {
924325151a3SRui Paulo 	struct fst_setup_res res;
925325151a3SRui Paulo 	enum hostapd_hw_mode hw_mode;
926325151a3SRui Paulo 	u8 channel;
927325151a3SRui Paulo 
928325151a3SRui Paulo 	if (!fst_session_is_ready_pending(s)) {
929325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "incorrect state: %s",
930325151a3SRui Paulo 				   fst_session_state_name(s->state));
931325151a3SRui Paulo 		return -EINVAL;
932325151a3SRui Paulo 	}
933325151a3SRui Paulo 
934325151a3SRui Paulo 	if (is_zero_ether_addr(s->data.old_peer_addr)) {
935325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No peer MAC address");
936325151a3SRui Paulo 		return -EINVAL;
937325151a3SRui Paulo 	}
938325151a3SRui Paulo 
939325151a3SRui Paulo 	if (!s->data.old_iface) {
940325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No old interface defined");
941325151a3SRui Paulo 		return -EINVAL;
942325151a3SRui Paulo 	}
943325151a3SRui Paulo 
944325151a3SRui Paulo 	if (!s->data.new_iface) {
945325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR, "No new interface defined");
946325151a3SRui Paulo 		return -EINVAL;
947325151a3SRui Paulo 	}
948325151a3SRui Paulo 
949325151a3SRui Paulo 	if (s->data.new_iface == s->data.old_iface) {
950325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
951325151a3SRui Paulo 				   "Same interface set as old and new");
952325151a3SRui Paulo 		return -EINVAL;
953325151a3SRui Paulo 	}
954325151a3SRui Paulo 
955780fb4a2SCy Schubert 	if (!fst_iface_is_connected(s->data.old_iface,
956c1d255d3SCy Schubert 				    s->data.old_peer_addr, false)) {
957325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
958325151a3SRui Paulo 				   "The preset peer address is not in the peer list");
959325151a3SRui Paulo 		return -EINVAL;
960325151a3SRui Paulo 	}
961325151a3SRui Paulo 
962325151a3SRui Paulo 	fst_session_stt_disarm(s);
963325151a3SRui Paulo 
964325151a3SRui Paulo 	os_memset(&res, 0, sizeof(res));
965325151a3SRui Paulo 
966325151a3SRui Paulo 	res.action = FST_ACTION_SETUP_RESPONSE;
967325151a3SRui Paulo 	res.dialog_token = s->data.pending_setup_req_dlgt;
968325151a3SRui Paulo 	res.status_code = status_code;
969325151a3SRui Paulo 
970325151a3SRui Paulo 	res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
971325151a3SRui Paulo 	res.stie.length = sizeof(res.stie) - 2;
972325151a3SRui Paulo 
973325151a3SRui Paulo 	if (status_code == WLAN_STATUS_SUCCESS) {
974780fb4a2SCy Schubert 		res.stie.fsts_id = host_to_le32(s->data.fsts_id);
975325151a3SRui Paulo 		res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
976325151a3SRui Paulo 
977325151a3SRui Paulo 		fst_iface_get_channel_info(s->data.new_iface, &hw_mode,
978325151a3SRui Paulo 					   &channel);
979325151a3SRui Paulo 		res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
980325151a3SRui Paulo 		res.stie.new_band_op = 1;
981325151a3SRui Paulo 		res.stie.new_band_setup = 0;
982325151a3SRui Paulo 
983325151a3SRui Paulo 		fst_iface_get_channel_info(s->data.old_iface, &hw_mode,
984325151a3SRui Paulo 					   &channel);
985325151a3SRui Paulo 		res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
986325151a3SRui Paulo 		res.stie.old_band_op = 1;
987325151a3SRui Paulo 		res.stie.old_band_setup = 0;
988325151a3SRui Paulo 
989325151a3SRui Paulo 		fst_printf_session(s, MSG_INFO,
990325151a3SRui Paulo 				   "%s: FST Setup Request accepted for %s (llt=%u)",
991325151a3SRui Paulo 				   fst_iface_get_name(s->data.old_iface),
992325151a3SRui Paulo 				   fst_iface_get_name(s->data.new_iface),
993325151a3SRui Paulo 				   s->data.llt_ms);
994325151a3SRui Paulo 	} else {
995325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
996325151a3SRui Paulo 				   "%s: FST Setup Request rejected with code %d",
997325151a3SRui Paulo 				   fst_iface_get_name(s->data.old_iface),
998325151a3SRui Paulo 				   status_code);
999325151a3SRui Paulo 	}
1000325151a3SRui Paulo 
1001c1d255d3SCy Schubert 	if (fst_session_send_action(s, true, &res, sizeof(res),
1002325151a3SRui Paulo 				    fst_iface_get_mbie(s->data.old_iface))) {
1003c1d255d3SCy Schubert 		fst_printf_sframe(s, true, MSG_ERROR,
1004325151a3SRui Paulo 				  "cannot send FST Setup Response with code %d",
1005325151a3SRui Paulo 				  status_code);
1006325151a3SRui Paulo 		return -EINVAL;
1007325151a3SRui Paulo 	}
1008325151a3SRui Paulo 
1009c1d255d3SCy Schubert 	fst_printf_sframe(s, true, MSG_INFO, "FST Setup Response sent");
1010325151a3SRui Paulo 
1011325151a3SRui Paulo 	if (status_code != WLAN_STATUS_SUCCESS) {
1012325151a3SRui Paulo 		union fst_session_state_switch_extra evext = {
1013325151a3SRui Paulo 			.to_initial = {
1014325151a3SRui Paulo 				.reason = REASON_REJECT,
1015325151a3SRui Paulo 				.reject_code = status_code,
1016325151a3SRui Paulo 				.initiator = FST_INITIATOR_LOCAL,
1017325151a3SRui Paulo 			},
1018325151a3SRui Paulo 		};
1019325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1020325151a3SRui Paulo 	}
1021325151a3SRui Paulo 
1022325151a3SRui Paulo 	return 0;
1023325151a3SRui Paulo }
1024325151a3SRui Paulo 
1025325151a3SRui Paulo 
fst_session_initiate_switch(struct fst_session * s)1026325151a3SRui Paulo int fst_session_initiate_switch(struct fst_session *s)
1027325151a3SRui Paulo {
1028325151a3SRui Paulo 	struct fst_ack_req req;
1029325151a3SRui Paulo 	int res;
1030325151a3SRui Paulo 	u8 dialog_token;
1031325151a3SRui Paulo 
1032325151a3SRui Paulo 	if (!fst_session_is_ready(s)) {
1033325151a3SRui Paulo 		fst_printf_session(s, MSG_ERROR,
1034325151a3SRui Paulo 				   "cannot initiate switch due to wrong setup state (%d)",
1035325151a3SRui Paulo 				   s->state);
1036325151a3SRui Paulo 		return -1;
1037325151a3SRui Paulo 	}
1038325151a3SRui Paulo 
1039325151a3SRui Paulo 	dialog_token = fst_group_assign_dialog_token(s->group);
1040325151a3SRui Paulo 
1041325151a3SRui Paulo 	WPA_ASSERT(s->data.new_iface != NULL);
1042325151a3SRui Paulo 	WPA_ASSERT(s->data.old_iface != NULL);
1043325151a3SRui Paulo 
1044325151a3SRui Paulo 	fst_printf_session(s, MSG_INFO, "initiating FST switch: %s => %s",
1045325151a3SRui Paulo 			   fst_iface_get_name(s->data.old_iface),
1046325151a3SRui Paulo 			   fst_iface_get_name(s->data.new_iface));
1047325151a3SRui Paulo 
1048325151a3SRui Paulo 	os_memset(&req, 0, sizeof(req));
1049325151a3SRui Paulo 
1050325151a3SRui Paulo 	req.action = FST_ACTION_ACK_REQUEST;
1051325151a3SRui Paulo 	req.dialog_token = dialog_token;
1052325151a3SRui Paulo 	req.fsts_id = host_to_le32(s->data.fsts_id);
1053325151a3SRui Paulo 
1054c1d255d3SCy Schubert 	res = fst_session_send_action(s, false, &req, sizeof(req), NULL);
1055325151a3SRui Paulo 	if (!res) {
1056c1d255d3SCy Schubert 		fst_printf_sframe(s, false, MSG_INFO, "FST Ack Request sent");
1057325151a3SRui Paulo 		fst_session_set_state(s, FST_SESSION_STATE_TRANSITION_DONE,
1058325151a3SRui Paulo 				      NULL);
1059325151a3SRui Paulo 		fst_session_stt_arm(s);
1060325151a3SRui Paulo 	} else {
1061c1d255d3SCy Schubert 		fst_printf_sframe(s, false, MSG_ERROR,
1062325151a3SRui Paulo 				  "Cannot send FST Ack Request");
1063325151a3SRui Paulo 	}
1064325151a3SRui Paulo 
1065325151a3SRui Paulo 	return res;
1066325151a3SRui Paulo }
1067325151a3SRui Paulo 
1068325151a3SRui Paulo 
fst_session_handle_action(struct fst_session * s,struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t frame_len)1069325151a3SRui Paulo void fst_session_handle_action(struct fst_session *s,
1070325151a3SRui Paulo 			       struct fst_iface *iface,
1071325151a3SRui Paulo 			       const struct ieee80211_mgmt *mgmt,
1072325151a3SRui Paulo 			       size_t frame_len)
1073325151a3SRui Paulo {
1074325151a3SRui Paulo 	switch (mgmt->u.action.u.fst_action.action) {
1075325151a3SRui Paulo 	case FST_ACTION_SETUP_REQUEST:
1076325151a3SRui Paulo 		WPA_ASSERT(0);
1077325151a3SRui Paulo 		break;
1078325151a3SRui Paulo 	case FST_ACTION_SETUP_RESPONSE:
1079325151a3SRui Paulo 		fst_session_handle_setup_response(s, iface, mgmt, frame_len);
1080325151a3SRui Paulo 		break;
1081325151a3SRui Paulo 	case FST_ACTION_TEAR_DOWN:
1082325151a3SRui Paulo 		fst_session_handle_tear_down(s, iface, mgmt, frame_len);
1083325151a3SRui Paulo 		break;
1084325151a3SRui Paulo 	case FST_ACTION_ACK_REQUEST:
1085325151a3SRui Paulo 		fst_session_handle_ack_request(s, iface, mgmt, frame_len);
1086325151a3SRui Paulo 		break;
1087325151a3SRui Paulo 	case FST_ACTION_ACK_RESPONSE:
1088325151a3SRui Paulo 		fst_session_handle_ack_response(s, iface, mgmt, frame_len);
1089325151a3SRui Paulo 		break;
1090325151a3SRui Paulo 	case FST_ACTION_ON_CHANNEL_TUNNEL:
1091325151a3SRui Paulo 	default:
1092c1d255d3SCy Schubert 		fst_printf_sframe(s, false, MSG_ERROR,
1093325151a3SRui Paulo 				  "Unsupported FST Action frame");
1094325151a3SRui Paulo 		break;
1095325151a3SRui Paulo 	}
1096325151a3SRui Paulo }
1097325151a3SRui Paulo 
1098325151a3SRui Paulo 
fst_session_tear_down_setup(struct fst_session * s)1099325151a3SRui Paulo int fst_session_tear_down_setup(struct fst_session *s)
1100325151a3SRui Paulo {
1101325151a3SRui Paulo 	int res;
1102325151a3SRui Paulo 	union fst_session_state_switch_extra evext = {
1103325151a3SRui Paulo 		.to_initial = {
1104325151a3SRui Paulo 			.reason = REASON_TEARDOWN,
1105325151a3SRui Paulo 			.initiator = FST_INITIATOR_LOCAL,
1106325151a3SRui Paulo 		},
1107325151a3SRui Paulo 	};
1108325151a3SRui Paulo 
1109325151a3SRui Paulo 	res = fst_session_send_tear_down(s);
1110325151a3SRui Paulo 
1111325151a3SRui Paulo 	fst_session_set_state(s, FST_SESSION_STATE_INITIAL, &evext);
1112325151a3SRui Paulo 
1113325151a3SRui Paulo 	return res;
1114325151a3SRui Paulo }
1115325151a3SRui Paulo 
1116325151a3SRui Paulo 
fst_session_reset(struct fst_session * s)1117325151a3SRui Paulo void fst_session_reset(struct fst_session *s)
1118325151a3SRui Paulo {
1119325151a3SRui Paulo 	fst_session_reset_ex(s, REASON_RESET);
1120325151a3SRui Paulo }
1121325151a3SRui Paulo 
1122325151a3SRui Paulo 
fst_session_delete(struct fst_session * s)1123325151a3SRui Paulo void fst_session_delete(struct fst_session *s)
1124325151a3SRui Paulo {
1125325151a3SRui Paulo 	fst_printf(MSG_INFO, "Session %u deleted", s->id);
1126325151a3SRui Paulo 	dl_list_del(&s->global_sessions_lentry);
1127325151a3SRui Paulo 	foreach_fst_ctrl_call(on_session_removed, s);
1128325151a3SRui Paulo 	os_free(s);
1129325151a3SRui Paulo }
1130325151a3SRui Paulo 
1131325151a3SRui Paulo 
fst_session_get_group(struct fst_session * s)1132325151a3SRui Paulo struct fst_group * fst_session_get_group(struct fst_session *s)
1133325151a3SRui Paulo {
1134325151a3SRui Paulo 	return s->group;
1135325151a3SRui Paulo }
1136325151a3SRui Paulo 
1137325151a3SRui Paulo 
fst_session_get_iface(struct fst_session * s,bool is_old)1138c1d255d3SCy Schubert struct fst_iface * fst_session_get_iface(struct fst_session *s, bool is_old)
1139325151a3SRui Paulo {
1140325151a3SRui Paulo 	return is_old ? s->data.old_iface : s->data.new_iface;
1141325151a3SRui Paulo }
1142325151a3SRui Paulo 
1143325151a3SRui Paulo 
fst_session_get_id(struct fst_session * s)1144325151a3SRui Paulo u32 fst_session_get_id(struct fst_session *s)
1145325151a3SRui Paulo {
1146325151a3SRui Paulo 	return s->id;
1147325151a3SRui Paulo }
1148325151a3SRui Paulo 
1149325151a3SRui Paulo 
fst_session_get_peer_addr(struct fst_session * s,bool is_old)1150c1d255d3SCy Schubert const u8 * fst_session_get_peer_addr(struct fst_session *s, bool is_old)
1151325151a3SRui Paulo {
1152325151a3SRui Paulo 	return is_old ? s->data.old_peer_addr : s->data.new_peer_addr;
1153325151a3SRui Paulo }
1154325151a3SRui Paulo 
1155325151a3SRui Paulo 
fst_session_get_llt(struct fst_session * s)1156325151a3SRui Paulo u32 fst_session_get_llt(struct fst_session *s)
1157325151a3SRui Paulo {
1158325151a3SRui Paulo 	return s->data.llt_ms;
1159325151a3SRui Paulo }
1160325151a3SRui Paulo 
1161325151a3SRui Paulo 
fst_session_get_state(struct fst_session * s)1162325151a3SRui Paulo enum fst_session_state fst_session_get_state(struct fst_session *s)
1163325151a3SRui Paulo {
1164325151a3SRui Paulo 	return s->state;
1165325151a3SRui Paulo }
1166325151a3SRui Paulo 
1167325151a3SRui Paulo 
fst_session_get_by_id(u32 id)1168325151a3SRui Paulo struct fst_session * fst_session_get_by_id(u32 id)
1169325151a3SRui Paulo {
1170325151a3SRui Paulo 	struct fst_session *s;
1171325151a3SRui Paulo 
1172325151a3SRui Paulo 	foreach_fst_session(s) {
1173325151a3SRui Paulo 		if (id == s->id)
1174325151a3SRui Paulo 			return s;
1175325151a3SRui Paulo 	}
1176325151a3SRui Paulo 
1177325151a3SRui Paulo 	return NULL;
1178325151a3SRui Paulo }
1179325151a3SRui Paulo 
1180325151a3SRui Paulo 
fst_session_enum(struct fst_group * g,fst_session_enum_clb clb,void * ctx)1181325151a3SRui Paulo void fst_session_enum(struct fst_group *g, fst_session_enum_clb clb, void *ctx)
1182325151a3SRui Paulo {
1183325151a3SRui Paulo 	struct fst_session *s;
1184325151a3SRui Paulo 
1185325151a3SRui Paulo 	foreach_fst_session(s) {
1186325151a3SRui Paulo 		if (!g || s->group == g)
1187325151a3SRui Paulo 			clb(s->group, s, ctx);
1188325151a3SRui Paulo 	}
1189325151a3SRui Paulo }
1190325151a3SRui Paulo 
1191325151a3SRui Paulo 
fst_session_on_action_rx(struct fst_iface * iface,const struct ieee80211_mgmt * mgmt,size_t len)1192325151a3SRui Paulo void fst_session_on_action_rx(struct fst_iface *iface,
1193325151a3SRui Paulo 			      const struct ieee80211_mgmt *mgmt,
1194325151a3SRui Paulo 			      size_t len)
1195325151a3SRui Paulo {
1196325151a3SRui Paulo 	struct fst_session *s;
1197325151a3SRui Paulo 
1198325151a3SRui Paulo 	if (len < IEEE80211_HDRLEN + 2 ||
1199325151a3SRui Paulo 	    mgmt->u.action.category != WLAN_ACTION_FST) {
1200325151a3SRui Paulo 		fst_printf_iface(iface, MSG_ERROR,
1201325151a3SRui Paulo 				 "invalid Action frame received");
1202325151a3SRui Paulo 		return;
1203325151a3SRui Paulo 	}
1204325151a3SRui Paulo 
1205325151a3SRui Paulo 	if (mgmt->u.action.u.fst_action.action <= FST_ACTION_MAX_SUPPORTED) {
1206325151a3SRui Paulo 		fst_printf_iface(iface, MSG_DEBUG,
1207325151a3SRui Paulo 				 "FST Action '%s' received!",
1208325151a3SRui Paulo 				 fst_action_names[mgmt->u.action.u.fst_action.action]);
1209325151a3SRui Paulo 	} else {
1210325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
1211325151a3SRui Paulo 				 "unknown FST Action (%u) received!",
1212325151a3SRui Paulo 				 mgmt->u.action.u.fst_action.action);
1213325151a3SRui Paulo 		return;
1214325151a3SRui Paulo 	}
1215325151a3SRui Paulo 
1216325151a3SRui Paulo 	if (mgmt->u.action.u.fst_action.action == FST_ACTION_SETUP_REQUEST) {
1217325151a3SRui Paulo 		fst_session_handle_setup_request(iface, mgmt, len);
1218325151a3SRui Paulo 		return;
1219325151a3SRui Paulo 	}
1220325151a3SRui Paulo 
1221325151a3SRui Paulo 	s = fst_find_session_in_progress(mgmt->sa, fst_iface_get_group(iface));
1222325151a3SRui Paulo 	if (s) {
1223325151a3SRui Paulo 		fst_session_handle_action(s, iface, mgmt, len);
1224325151a3SRui Paulo 	} else {
1225325151a3SRui Paulo 		fst_printf_iface(iface, MSG_WARNING,
1226325151a3SRui Paulo 				 "FST Action '%s' dropped: no session in progress found",
1227325151a3SRui Paulo 				 fst_action_names[mgmt->u.action.u.fst_action.action]);
1228325151a3SRui Paulo 	}
1229325151a3SRui Paulo }
1230325151a3SRui Paulo 
1231325151a3SRui Paulo 
fst_session_set_str_ifname(struct fst_session * s,const char * ifname,bool is_old)1232325151a3SRui Paulo int fst_session_set_str_ifname(struct fst_session *s, const char *ifname,
1233c1d255d3SCy Schubert 			       bool is_old)
1234325151a3SRui Paulo {
1235325151a3SRui Paulo 	struct fst_group *g = fst_session_get_group(s);
1236325151a3SRui Paulo 	struct fst_iface *i;
1237325151a3SRui Paulo 
1238325151a3SRui Paulo 	i = fst_group_get_iface_by_name(g, ifname);
1239325151a3SRui Paulo 	if (!i) {
1240325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
1241325151a3SRui Paulo 				   "Cannot set iface %s: no such iface within group '%s'",
1242325151a3SRui Paulo 				   ifname, fst_group_get_id(g));
1243325151a3SRui Paulo 		return -1;
1244325151a3SRui Paulo 	}
1245325151a3SRui Paulo 
1246325151a3SRui Paulo 	fst_session_set_iface(s, i, is_old);
1247325151a3SRui Paulo 
1248325151a3SRui Paulo 	return 0;
1249325151a3SRui Paulo }
1250325151a3SRui Paulo 
1251325151a3SRui Paulo 
fst_session_set_str_peer_addr(struct fst_session * s,const char * mac,bool is_old)1252325151a3SRui Paulo int fst_session_set_str_peer_addr(struct fst_session *s, const char *mac,
1253c1d255d3SCy Schubert 				  bool is_old)
1254325151a3SRui Paulo {
1255325151a3SRui Paulo 	u8 peer_addr[ETH_ALEN];
1256325151a3SRui Paulo 	int res = fst_read_peer_addr(mac, peer_addr);
1257325151a3SRui Paulo 
1258325151a3SRui Paulo 	if (res)
1259325151a3SRui Paulo 		return res;
1260325151a3SRui Paulo 
1261325151a3SRui Paulo 	fst_session_set_peer_addr(s, peer_addr, is_old);
1262325151a3SRui Paulo 
1263325151a3SRui Paulo 	return 0;
1264325151a3SRui Paulo }
1265325151a3SRui Paulo 
1266325151a3SRui Paulo 
fst_session_set_str_llt(struct fst_session * s,const char * llt_str)1267325151a3SRui Paulo int fst_session_set_str_llt(struct fst_session *s, const char *llt_str)
1268325151a3SRui Paulo {
1269325151a3SRui Paulo 	char *endp;
1270325151a3SRui Paulo 	long int llt = strtol(llt_str, &endp, 0);
1271325151a3SRui Paulo 
1272325151a3SRui Paulo 	if (*endp || llt < 0 || (unsigned long int) llt > FST_MAX_LLT_MS) {
1273325151a3SRui Paulo 		fst_printf_session(s, MSG_WARNING,
1274325151a3SRui Paulo 				   "Cannot set llt %s: Invalid llt value (1..%u expected)",
1275325151a3SRui Paulo 				   llt_str, FST_MAX_LLT_MS);
1276325151a3SRui Paulo 		return -1;
1277325151a3SRui Paulo 	}
1278325151a3SRui Paulo 	fst_session_set_llt(s, (u32) llt);
1279325151a3SRui Paulo 
1280325151a3SRui Paulo 	return 0;
1281325151a3SRui Paulo }
1282325151a3SRui Paulo 
1283325151a3SRui Paulo 
fst_session_global_on_iface_detached(struct fst_iface * iface)1284325151a3SRui Paulo void fst_session_global_on_iface_detached(struct fst_iface *iface)
1285325151a3SRui Paulo {
1286325151a3SRui Paulo 	struct fst_session *s;
1287325151a3SRui Paulo 
1288325151a3SRui Paulo 	foreach_fst_session(s) {
1289325151a3SRui Paulo 		if (fst_session_is_in_progress(s) &&
1290325151a3SRui Paulo 		    (s->data.new_iface == iface ||
1291325151a3SRui Paulo 		     s->data.old_iface == iface))
1292325151a3SRui Paulo 			fst_session_reset_ex(s, REASON_DETACH_IFACE);
1293325151a3SRui Paulo 	}
1294325151a3SRui Paulo }
1295325151a3SRui Paulo 
1296325151a3SRui Paulo 
fst_session_global_get_first_by_group(struct fst_group * g)1297325151a3SRui Paulo struct fst_session * fst_session_global_get_first_by_group(struct fst_group *g)
1298325151a3SRui Paulo {
1299325151a3SRui Paulo 	struct fst_session *s;
1300325151a3SRui Paulo 
1301325151a3SRui Paulo 	foreach_fst_session(s) {
1302325151a3SRui Paulo 		if (s->group == g)
1303325151a3SRui Paulo 			return s;
1304325151a3SRui Paulo 	}
1305325151a3SRui Paulo 
1306325151a3SRui Paulo 	return NULL;
1307325151a3SRui Paulo }
1308325151a3SRui Paulo 
1309325151a3SRui Paulo 
1310325151a3SRui Paulo #ifdef CONFIG_FST_TEST
1311325151a3SRui Paulo 
get_group_fill_session(struct fst_group ** g,struct fst_session * s)1312325151a3SRui Paulo static int get_group_fill_session(struct fst_group **g, struct fst_session *s)
1313325151a3SRui Paulo {
1314325151a3SRui Paulo 	const u8 *old_addr, *new_addr;
1315325151a3SRui Paulo 	struct fst_get_peer_ctx *ctx;
1316325151a3SRui Paulo 
1317325151a3SRui Paulo 	os_memset(s, 0, sizeof(*s));
1318325151a3SRui Paulo 	foreach_fst_group(*g) {
1319325151a3SRui Paulo 		s->data.new_iface = fst_group_first_iface(*g);
1320325151a3SRui Paulo 		if (s->data.new_iface)
1321325151a3SRui Paulo 			break;
1322325151a3SRui Paulo 	}
1323325151a3SRui Paulo 	if (!s->data.new_iface)
1324325151a3SRui Paulo 		return -EINVAL;
1325325151a3SRui Paulo 
1326325151a3SRui Paulo 	s->data.old_iface = dl_list_entry(s->data.new_iface->group_lentry.next,
1327325151a3SRui Paulo 					  struct fst_iface, group_lentry);
1328325151a3SRui Paulo 	if (!s->data.old_iface)
1329325151a3SRui Paulo 		return -EINVAL;
1330325151a3SRui Paulo 
1331c1d255d3SCy Schubert 	old_addr = fst_iface_get_peer_first(s->data.old_iface, &ctx, true);
1332325151a3SRui Paulo 	if (!old_addr)
1333325151a3SRui Paulo 		return -EINVAL;
1334325151a3SRui Paulo 
1335c1d255d3SCy Schubert 	new_addr = fst_iface_get_peer_first(s->data.new_iface, &ctx, true);
1336325151a3SRui Paulo 	if (!new_addr)
1337325151a3SRui Paulo 		return -EINVAL;
1338325151a3SRui Paulo 
1339325151a3SRui Paulo 	os_memcpy(s->data.old_peer_addr, old_addr, ETH_ALEN);
1340325151a3SRui Paulo 	os_memcpy(s->data.new_peer_addr, new_addr, ETH_ALEN);
1341325151a3SRui Paulo 
1342325151a3SRui Paulo 	return 0;
1343325151a3SRui Paulo }
1344325151a3SRui Paulo 
1345325151a3SRui Paulo 
1346325151a3SRui Paulo #define FST_MAX_COMMAND_WORD_NAME_LENGTH 16
1347325151a3SRui Paulo 
fst_test_req_send_fst_request(const char * params)1348325151a3SRui Paulo int fst_test_req_send_fst_request(const char *params)
1349325151a3SRui Paulo {
1350325151a3SRui Paulo 	int fsts_id;
1351c1d255d3SCy Schubert 	bool is_valid;
1352325151a3SRui Paulo 	char *endp;
1353325151a3SRui Paulo 	struct fst_setup_req req;
1354325151a3SRui Paulo 	struct fst_session s;
1355325151a3SRui Paulo 	struct fst_group *g;
1356325151a3SRui Paulo 	enum hostapd_hw_mode hw_mode;
1357325151a3SRui Paulo 	u8 channel;
1358325151a3SRui Paulo 	char additional_param[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1359325151a3SRui Paulo 
1360325151a3SRui Paulo 	if (params[0] != ' ')
1361325151a3SRui Paulo 		return -EINVAL;
1362325151a3SRui Paulo 	params++;
1363325151a3SRui Paulo 	fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1364325151a3SRui Paulo 	if (!is_valid)
1365325151a3SRui Paulo 		return -EINVAL;
1366325151a3SRui Paulo 
1367325151a3SRui Paulo 	if (get_group_fill_session(&g, &s))
1368325151a3SRui Paulo 		return -EINVAL;
1369325151a3SRui Paulo 
1370325151a3SRui Paulo 	req.action = FST_ACTION_SETUP_REQUEST;
1371325151a3SRui Paulo 	req.dialog_token = g->dialog_token;
1372325151a3SRui Paulo 	req.llt = host_to_le32(FST_LLT_MS_DEFAULT);
1373325151a3SRui Paulo 	/* 8.4.2.147 Session Transition element */
1374325151a3SRui Paulo 	req.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1375325151a3SRui Paulo 	req.stie.length = sizeof(req.stie) - 2;
1376325151a3SRui Paulo 	req.stie.fsts_id = host_to_le32(fsts_id);
1377325151a3SRui Paulo 	req.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1378325151a3SRui Paulo 
1379325151a3SRui Paulo 	fst_iface_get_channel_info(s.data.new_iface, &hw_mode, &channel);
1380325151a3SRui Paulo 	req.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1381325151a3SRui Paulo 	req.stie.new_band_op = 1;
1382325151a3SRui Paulo 	req.stie.new_band_setup = 0;
1383325151a3SRui Paulo 
1384325151a3SRui Paulo 	fst_iface_get_channel_info(s.data.old_iface, &hw_mode, &channel);
1385325151a3SRui Paulo 	req.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1386325151a3SRui Paulo 	req.stie.old_band_op = 1;
1387325151a3SRui Paulo 	req.stie.old_band_setup = 0;
1388325151a3SRui Paulo 
1389325151a3SRui Paulo 	if (!fst_read_next_text_param(endp, additional_param,
1390325151a3SRui Paulo 				       sizeof(additional_param), &endp)) {
1391325151a3SRui Paulo 		if (!os_strcasecmp(additional_param, FST_CTR_PVAL_BAD_NEW_BAND))
1392325151a3SRui Paulo 			req.stie.new_band_id = req.stie.old_band_id;
1393325151a3SRui Paulo 	}
1394325151a3SRui Paulo 
1395c1d255d3SCy Schubert 	return fst_session_send_action(&s, true, &req, sizeof(req),
1396325151a3SRui Paulo 				       s.data.old_iface->mb_ie);
1397325151a3SRui Paulo }
1398325151a3SRui Paulo 
1399325151a3SRui Paulo 
fst_test_req_send_fst_response(const char * params)1400325151a3SRui Paulo int fst_test_req_send_fst_response(const char *params)
1401325151a3SRui Paulo {
1402325151a3SRui Paulo 	int fsts_id;
1403c1d255d3SCy Schubert 	bool is_valid;
1404325151a3SRui Paulo 	char *endp;
1405325151a3SRui Paulo 	struct fst_setup_res res;
1406325151a3SRui Paulo 	struct fst_session s;
1407325151a3SRui Paulo 	struct fst_group *g;
1408325151a3SRui Paulo 	enum hostapd_hw_mode hw_mode;
1409325151a3SRui Paulo 	u8 status_code;
1410325151a3SRui Paulo 	u8 channel;
1411325151a3SRui Paulo 	char response[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1412325151a3SRui Paulo 	struct fst_session *_s;
1413325151a3SRui Paulo 
1414325151a3SRui Paulo 	if (params[0] != ' ')
1415325151a3SRui Paulo 		return -EINVAL;
1416325151a3SRui Paulo 	params++;
1417325151a3SRui Paulo 	fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1418325151a3SRui Paulo 	if (!is_valid)
1419325151a3SRui Paulo 		return -EINVAL;
1420325151a3SRui Paulo 
1421325151a3SRui Paulo 	if (get_group_fill_session(&g, &s))
1422325151a3SRui Paulo 		return -EINVAL;
1423325151a3SRui Paulo 
1424325151a3SRui Paulo 	status_code = WLAN_STATUS_SUCCESS;
1425325151a3SRui Paulo 	if (!fst_read_next_text_param(endp, response, sizeof(response),
1426325151a3SRui Paulo 				      &endp)) {
1427325151a3SRui Paulo 		if (!os_strcasecmp(response, FST_CS_PVAL_RESPONSE_REJECT))
1428325151a3SRui Paulo 			status_code = WLAN_STATUS_PENDING_ADMITTING_FST_SESSION;
1429325151a3SRui Paulo 	}
1430325151a3SRui Paulo 
1431325151a3SRui Paulo 	os_memset(&res, 0, sizeof(res));
1432325151a3SRui Paulo 
1433325151a3SRui Paulo 	res.action = FST_ACTION_SETUP_RESPONSE;
1434325151a3SRui Paulo 	/*
1435325151a3SRui Paulo 	 * If some session has just received an FST Setup Request, then
1436325151a3SRui Paulo 	 * use the correct dialog token copied from this request.
1437325151a3SRui Paulo 	 */
1438c1d255d3SCy Schubert 	_s = fst_find_session_in_progress(fst_session_get_peer_addr(&s, true),
1439325151a3SRui Paulo 					  g);
1440325151a3SRui Paulo 	res.dialog_token = (_s && fst_session_is_ready_pending(_s)) ?
1441325151a3SRui Paulo 		_s->data.pending_setup_req_dlgt : g->dialog_token;
1442325151a3SRui Paulo 	res.status_code  = status_code;
1443325151a3SRui Paulo 
1444325151a3SRui Paulo 	res.stie.element_id = WLAN_EID_SESSION_TRANSITION;
1445325151a3SRui Paulo 	res.stie.length = sizeof(res.stie) - 2;
1446325151a3SRui Paulo 
1447325151a3SRui Paulo 	if (res.status_code == WLAN_STATUS_SUCCESS) {
1448780fb4a2SCy Schubert 		res.stie.fsts_id = host_to_le32(fsts_id);
1449325151a3SRui Paulo 		res.stie.session_control = SESSION_CONTROL(SESSION_TYPE_BSS, 0);
1450325151a3SRui Paulo 
1451325151a3SRui Paulo 		fst_iface_get_channel_info(s.data.new_iface, &hw_mode,
1452325151a3SRui Paulo 					    &channel);
1453325151a3SRui Paulo 		res.stie.new_band_id = fst_hw_mode_to_band(hw_mode);
1454325151a3SRui Paulo 		res.stie.new_band_op = 1;
1455325151a3SRui Paulo 		res.stie.new_band_setup = 0;
1456325151a3SRui Paulo 
1457325151a3SRui Paulo 		fst_iface_get_channel_info(s.data.old_iface, &hw_mode,
1458325151a3SRui Paulo 					   &channel);
1459325151a3SRui Paulo 		res.stie.old_band_id = fst_hw_mode_to_band(hw_mode);
1460325151a3SRui Paulo 		res.stie.old_band_op = 1;
1461325151a3SRui Paulo 		res.stie.old_band_setup = 0;
1462325151a3SRui Paulo 	}
1463325151a3SRui Paulo 
1464325151a3SRui Paulo 	if (!fst_read_next_text_param(endp, response, sizeof(response),
1465325151a3SRui Paulo 				      &endp)) {
1466325151a3SRui Paulo 		if (!os_strcasecmp(response, FST_CTR_PVAL_BAD_NEW_BAND))
1467325151a3SRui Paulo 			res.stie.new_band_id = res.stie.old_band_id;
1468325151a3SRui Paulo 	}
1469325151a3SRui Paulo 
1470c1d255d3SCy Schubert 	return fst_session_send_action(&s, true, &res, sizeof(res),
1471325151a3SRui Paulo 				       s.data.old_iface->mb_ie);
1472325151a3SRui Paulo }
1473325151a3SRui Paulo 
1474325151a3SRui Paulo 
fst_test_req_send_ack_request(const char * params)1475325151a3SRui Paulo int fst_test_req_send_ack_request(const char *params)
1476325151a3SRui Paulo {
1477325151a3SRui Paulo 	int fsts_id;
1478c1d255d3SCy Schubert 	bool is_valid;
1479325151a3SRui Paulo 	char *endp;
1480325151a3SRui Paulo 	struct fst_ack_req req;
1481325151a3SRui Paulo 	struct fst_session s;
1482325151a3SRui Paulo 	struct fst_group *g;
1483325151a3SRui Paulo 
1484325151a3SRui Paulo 	if (params[0] != ' ')
1485325151a3SRui Paulo 		return -EINVAL;
1486325151a3SRui Paulo 	params++;
1487325151a3SRui Paulo 	fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1488325151a3SRui Paulo 	if (!is_valid)
1489325151a3SRui Paulo 		return -EINVAL;
1490325151a3SRui Paulo 
1491325151a3SRui Paulo 	if (get_group_fill_session(&g, &s))
1492325151a3SRui Paulo 		return -EINVAL;
1493325151a3SRui Paulo 
1494325151a3SRui Paulo 	os_memset(&req, 0, sizeof(req));
1495325151a3SRui Paulo 	req.action = FST_ACTION_ACK_REQUEST;
1496325151a3SRui Paulo 	req.dialog_token = g->dialog_token;
1497780fb4a2SCy Schubert 	req.fsts_id = host_to_le32(fsts_id);
1498325151a3SRui Paulo 
1499c1d255d3SCy Schubert 	return fst_session_send_action(&s, false, &req, sizeof(req), NULL);
1500325151a3SRui Paulo }
1501325151a3SRui Paulo 
1502325151a3SRui Paulo 
fst_test_req_send_ack_response(const char * params)1503325151a3SRui Paulo int fst_test_req_send_ack_response(const char *params)
1504325151a3SRui Paulo {
1505325151a3SRui Paulo 	int fsts_id;
1506c1d255d3SCy Schubert 	bool is_valid;
1507325151a3SRui Paulo 	char *endp;
1508325151a3SRui Paulo 	struct fst_ack_res res;
1509325151a3SRui Paulo 	struct fst_session s;
1510325151a3SRui Paulo 	struct fst_group *g;
1511325151a3SRui Paulo 
1512325151a3SRui Paulo 	if (params[0] != ' ')
1513325151a3SRui Paulo 		return -EINVAL;
1514325151a3SRui Paulo 	params++;
1515325151a3SRui Paulo 	fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1516325151a3SRui Paulo 	if (!is_valid)
1517325151a3SRui Paulo 		return -EINVAL;
1518325151a3SRui Paulo 
1519325151a3SRui Paulo 	if (get_group_fill_session(&g, &s))
1520325151a3SRui Paulo 		return -EINVAL;
1521325151a3SRui Paulo 
1522325151a3SRui Paulo 	os_memset(&res, 0, sizeof(res));
1523325151a3SRui Paulo 	res.action = FST_ACTION_ACK_RESPONSE;
1524325151a3SRui Paulo 	res.dialog_token = g->dialog_token;
1525780fb4a2SCy Schubert 	res.fsts_id = host_to_le32(fsts_id);
1526325151a3SRui Paulo 
1527c1d255d3SCy Schubert 	return fst_session_send_action(&s, false, &res, sizeof(res), NULL);
1528325151a3SRui Paulo }
1529325151a3SRui Paulo 
1530325151a3SRui Paulo 
fst_test_req_send_tear_down(const char * params)1531325151a3SRui Paulo int fst_test_req_send_tear_down(const char *params)
1532325151a3SRui Paulo {
1533325151a3SRui Paulo 	int fsts_id;
1534c1d255d3SCy Schubert 	bool is_valid;
1535325151a3SRui Paulo 	char *endp;
1536325151a3SRui Paulo 	struct fst_tear_down td;
1537325151a3SRui Paulo 	struct fst_session s;
1538325151a3SRui Paulo 	struct fst_group *g;
1539325151a3SRui Paulo 
1540325151a3SRui Paulo 	if (params[0] != ' ')
1541325151a3SRui Paulo 		return -EINVAL;
1542325151a3SRui Paulo 	params++;
1543325151a3SRui Paulo 	fsts_id = fst_read_next_int_param(params, &is_valid, &endp);
1544325151a3SRui Paulo 	if (!is_valid)
1545325151a3SRui Paulo 		return -EINVAL;
1546325151a3SRui Paulo 
1547325151a3SRui Paulo 	if (get_group_fill_session(&g, &s))
1548325151a3SRui Paulo 		return -EINVAL;
1549325151a3SRui Paulo 
1550325151a3SRui Paulo 	os_memset(&td, 0, sizeof(td));
1551325151a3SRui Paulo 	td.action = FST_ACTION_TEAR_DOWN;
1552780fb4a2SCy Schubert 	td.fsts_id = host_to_le32(fsts_id);
1553325151a3SRui Paulo 
1554c1d255d3SCy Schubert 	return fst_session_send_action(&s, true, &td, sizeof(td), NULL);
1555325151a3SRui Paulo }
1556325151a3SRui Paulo 
1557325151a3SRui Paulo 
fst_test_req_get_fsts_id(const char * params)1558325151a3SRui Paulo u32 fst_test_req_get_fsts_id(const char *params)
1559325151a3SRui Paulo {
1560325151a3SRui Paulo 	int sid;
1561c1d255d3SCy Schubert 	bool is_valid;
1562325151a3SRui Paulo 	char *endp;
1563325151a3SRui Paulo 	struct fst_session *s;
1564325151a3SRui Paulo 
1565325151a3SRui Paulo 	if (params[0] != ' ')
1566325151a3SRui Paulo 		return FST_FSTS_ID_NOT_FOUND;
1567325151a3SRui Paulo 	params++;
1568325151a3SRui Paulo 	sid = fst_read_next_int_param(params, &is_valid, &endp);
1569325151a3SRui Paulo 	if (!is_valid)
1570325151a3SRui Paulo 		return FST_FSTS_ID_NOT_FOUND;
1571325151a3SRui Paulo 
1572325151a3SRui Paulo 	s = fst_session_get_by_id(sid);
1573325151a3SRui Paulo 	if (!s)
1574325151a3SRui Paulo 		return FST_FSTS_ID_NOT_FOUND;
1575325151a3SRui Paulo 
1576325151a3SRui Paulo 	return s->data.fsts_id;
1577325151a3SRui Paulo }
1578325151a3SRui Paulo 
1579325151a3SRui Paulo 
fst_test_req_get_local_mbies(const char * request,char * buf,size_t buflen)1580325151a3SRui Paulo int fst_test_req_get_local_mbies(const char *request, char *buf, size_t buflen)
1581325151a3SRui Paulo {
1582325151a3SRui Paulo 	char *endp;
1583325151a3SRui Paulo 	char ifname[FST_MAX_COMMAND_WORD_NAME_LENGTH];
1584325151a3SRui Paulo 	struct fst_group *g;
1585325151a3SRui Paulo 	struct fst_iface *iface;
1586325151a3SRui Paulo 
1587325151a3SRui Paulo 	if (request[0] != ' ')
1588325151a3SRui Paulo 		return -EINVAL;
1589325151a3SRui Paulo 	request++;
1590325151a3SRui Paulo 	if (fst_read_next_text_param(request, ifname, sizeof(ifname), &endp) ||
1591325151a3SRui Paulo 	    !*ifname)
1592325151a3SRui Paulo 		goto problem;
1593325151a3SRui Paulo 	g = dl_list_first(&fst_global_groups_list, struct fst_group,
1594325151a3SRui Paulo 			  global_groups_lentry);
1595325151a3SRui Paulo 	if (!g)
1596325151a3SRui Paulo 		goto problem;
1597325151a3SRui Paulo 	iface = fst_group_get_iface_by_name(g, ifname);
1598325151a3SRui Paulo 	if (!iface || !iface->mb_ie)
1599325151a3SRui Paulo 		goto problem;
1600325151a3SRui Paulo 	return wpa_snprintf_hex(buf, buflen, wpabuf_head(iface->mb_ie),
1601325151a3SRui Paulo 				wpabuf_len(iface->mb_ie));
1602325151a3SRui Paulo 
1603325151a3SRui Paulo problem:
1604325151a3SRui Paulo 	return os_snprintf(buf, buflen, "FAIL\n");
1605325151a3SRui Paulo }
1606325151a3SRui Paulo 
1607325151a3SRui Paulo #endif /* CONFIG_FST_TEST */
1608