VSF Documented
vsf_wifi_priv.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifndef __VSF_WIFI_PRIV_H__
19#define __VSF_WIFI_PRIV_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "./vsf_wifi.h"
24
25#if VSF_USE_WIFI == ENABLED
26
27#ifndef VSF_WIFI_CFG_CCMP_BUF_SIZE
28# define VSF_WIFI_CFG_CCMP_BUF_SIZE 1600
29#endif
30
31/*============================ TYPES =========================================*/
32
33/*
34 * Raw WiFi radio instance embedded in vsf_wifi_t.
35 *
36 * The public vsf_wifi_radio.h header only forward-declares this type; the
37 * full definition is private so the layout can carry internal adapter state.
38 */
42 void *rx_param;
44
45 /* Internal adapter state: stores the user `done` callback when a raw
46 * radio operation is delegated to a vsf_wifi_done_t-style hook. */
48};
49
50/*
51 * The wifi layer is chip-agnostic but knows about the optional register-bus
52 * helper used by register-based chips (e.g. RT2X00). reg_bus may be NULL for
53 * chips that use a command/event-based bus (e.g. MediaTek mt76). The bus
54 * driver embeds vsf_wifi_t inside a larger struct (e.g. vk_usbh_wifi_t) and
55 * recovers its outer instance via vsf_container_of().
56 */
57struct vsf_wifi_t {
62
63 /* Optional network-stack backend (lwIP netdrv adapter, etc.) bound at
64 * runtime via vsf_wifi_netdrv_attach(). When netif_ops != NULL the core
65 * routes decrypted RX data frames and link up/down events here instead
66 * of the weak vsf_wifi_on_* hooks. */
69
72 bool bss_wmm; /* current BSS supports WMM/QoS (set on assoc) */
73 bool disconnecting; /* set in fini() to gate stale callbacks */
74 uint8_t mac[6]; /* populated by chip-driver EEPROM stage; */
75 /* zero until firmware_load chain finishes */
76
77 /* ---- Scan state (wifi-driven hop scheduler) ---- */
83#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
85 /* Read-poll spacing timer. Logically independent of scan_timer:
86 * read_poll runs during firmware_load (is_ready == false) while scan
87 * runs after on_ready, but they could in principle overlap if a
88 * future chip op uses run_read_poll mid-flight, so we keep two. */
90#endif
91
92 /* ---- MLME connection state (OPEN-system auth + association) ----
93 *
94 * Driven by the wifi layer (vsf_wifi.c). mlme_state walks
95 * IDLE -> AUTH -> ASSOC -> RUN; each handshake step arms mlme_timer and
96 * retransmits up to mlme_retry times before giving up. */
97 uint8_t mlme_state; /* WIFI_MLME_xxx */
98 uint8_t mlme_bssid[6]; /* target AP BSSID */
99 uint8_t mlme_ssid[33]; /* target SSID (for assoc-req SSID IE) */
101 uint8_t mlme_channel; /* target channel */
102 uint8_t connect_bw; /* WIFI_BW_xxx for next connect/set_channel*/
103 uint8_t mlme_retry; /* retransmits left for current step */
104
105 /* Deferred hardware key install. When the chip crypto backend reports
106 * VSF_ERR_NOT_AVAILABLE because a previous register script is still in
107 * flight, the handshake is retried automatically once that script
108 * completes. This avoids a race between the async connect script and the
109 * 4-way handshake on register-based chips such as RT5572. */
110 struct {
114 uint16_t mlme_aid; /* association id from assoc-resp */
115
116 /* Software TX sequence-number allocator. Chip backends that raise the
117 * TXWI "no sequence" bit (RT5572 et al.) never overwrite frame[22..23],
118 * so every software-built data frame (EAPOL, lwIP data via netdrv,
119 * keepalive nulls) must be given a fresh number here; frames repeating
120 * seq=0 are silently discarded by AP duplicate detection, which looks
121 * to the AP exactly like total uplink silence. */
123#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
125#endif
126
127#if VSF_WIFI_USE_WPA == ENABLED
128 /* ---- WPA2-PSK security context ----
129 *
130 * wpa_auth is captured by vsf_wifi_set_auth_mode(); auth_mode == OPEN
131 * means no RSN (the assoc-req carries no RSN IE and no 4-way handshake
132 * runs). wpa_rsn_ie holds the STA RSN IE bytes sent in the assoc-req so
133 * the 4-way handshake M2 can echo them unchanged. Handshake-specific
134 * fields (PTK / nonces / GTK / replay / substate) are added by the WPA
135 * state machine. */
139
140 /* 4-way handshake working state (vsf_wifi_wpa.c). The PMK lives in
141 * wpa_auth.psk; PTK = KCK(16)|KEK(16)|TK(16) is derived on M1. anonce /
142 * snonce feed the PRF; gtk holds the group key unwrapped from M3.
143 * replay echoes the AP's EAPOL-Key replay counter; ptk_valid gates the
144 * CCMP data path until the handshake installs keys. */
153
154 /* CCMP data path (vsf_wifi.c). wpa_tx_pn is the 48-bit packet-number
155 * counter (little-endian, pn[0] = LSB) advanced once per encrypted TX
156 * MPDU; it seeds the CCMP header and CCM nonce. wpa_hw_crypto is set
157 * when a chip crypto_ops backend installed the keys, in which case the
158 * software CCMP encap/decap is bypassed (the chip does it in-line). */
161
162 /* Auto-reconnect (see VSF_WIFI_CFG_AUTO_RECONNECT): armed when a linked
163 * session is torn down by an over-the-air deauth/disassoc; re-issues
164 * connect to the same target after a delay. mlme_linked tracks whether
165 * the torn-down session had actually reached link-up (only then is
166 * auto-reconnect eligible). */
170#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
172#endif
173
174 /* Software CCMP working buffers. Placed in the instance instead of local
175 * static arrays so the code remains re-entrant when multiple wifi instances
176 * exist or when an async path could overlap. */
179#if VSF_WIFI_CFG_CCMP_TX_SELF_VERIFY == ENABLED
182#endif
184#endif
185
186 /* ---- Script / blob dispatcher state ----
187 *
188 * Only one outstanding script or blob is allowed per wifi (the reg_bus
189 * concurrency contract enforces the same limit at the bus level). The
190 * dispatcher walks the op array via the chained __script_step_done /
191 * __blob_step_done callbacks, each of which feeds the next op to
192 * reg_bus.
193 */
197 union {
198 struct {
203 struct {
204 const uint8_t *data;
206 uint32_t offset; /* used by the reg_write fallback path */
210 struct {
212 uint16_t retry_left; /* attempts remaining after
213 * the current in-flight read */
216 uint32_t last_val; /* read landing zone */
220 } s;
221
222 /*
223 * Backend-level chain slot. When a chip op needs to issue several
224 * scripts back-to-back (firmware_load = run_blob + post-fw script),
225 * it stashes the *outer* `done` here, then internally chains via local
226 * helpers. Only one chain may be in flight at a time — same constraint
227 * as the script dispatcher itself.
228 */
230
231 /* BSS Max Idle Period from assoc-resp IE 90 (units of 1000 TU).
232 * 0 means the AP did not advertise it. */
234
235#if (VSF_WIFI_CFG_KEEPALIVE_PERIOD_MS > 0) \
236 && (VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED)
237 /* 802.11 null-data-frame keepalive state. Started on link-up and stopped
238 * on link-down / fini to keep the AP from deauthenticating an idle STA. */
239 vsf_callback_timer_t keepalive_timer;
240 uint32_t keepalive_period_ms;
241#endif
242
244
245 /* Chip-driver private data pointer. Set by the bus driver after
246 * vsf_wifi_init() for command/event-based chips that need per-instance
247 * state beyond the generic wifi struct (e.g. MediaTek mt76). The generic
248 * layer never touches this field. */
250
251 /* Chip-specific bus operations.
252 *
253 * This abstracts the *communication semantics* between the chip driver and
254 * the host, not the underlying physical bus. Register-based chips (e.g.
255 * Ralink RT28xx) use reg_bus above: the chip driver only knows "read/write
256 * a register", and the bus driver implements that with USB ep0 vendor
257 * requests, SDIO CMD53, etc.
258 *
259 * Command/event-based chips (e.g. MediaTek mt76) store a chip-defined
260 * vtable here (cast by the chip driver to the appropriate type). The
261 * operations are named after what the *chip* does (send MCU command,
262 * submit TX frame, start RX, ...), so the same chip driver can plug into
263 * different physical buses without modification.
264 *
265 * Design rule: bus_ops MUST NOT contain physical-bus-specific primitives
266 * such as USB ep0 vendor_request. If a chip genuinely needs a bus-specific
267 * control primitive, define a chip-private vtable whose FIRST member is
268 * vsf_wifi_reg_bus_t (when register semantics exist) or another chip-defined
269 * base, and place the private primitive after the shared part. The bus
270 * driver supplies the private vtable; the chip driver casts bus_ops to the
271 * chip-private type. */
272 const void *bus_ops;
273
274 /* ---- Raw WiFi radio state ----
275 *
276 * When raw_radio_active is true the instance is in raw-radio mode and the
277 * standard MLME/scan/WPA state machine is gated out. Received frames are
278 * delivered to raw_radio.rx_cb instead of the normal netif/weak hooks. */
281};
282
283/* Allocate a fresh 12-bit 802.11 sequence number for an outbound data
284 * frame (low nibble = fragment index stays 0; wraps at 4096). */
285static inline uint16_t __vsf_wifi_next_tx_seq(vsf_wifi_t *wifi)
286{
287 return (uint16_t)((wifi->tx_seq++ & 0x0FFF) << 4);
288}
289
290#if VSF_WIFI_USE_WPA == ENABLED
291/* CCMP-encrypt a plaintext data MPDU using a caller-supplied 48-bit PN
292 * counter (little-endian, pn[0] = LSB). If `pn` is NULL the function falls
293 * back to wifi->wpa_tx_pn. Returns the encrypted MPDU length, or 0 on
294 * failure. Used by chip drivers that need to software-encrypt specific
295 * frames (e.g. MT76 broadcast/multicast) while leaving unicast traffic to
296 * the hardware crypto backend. */
298 const uint8_t *frame, uint16_t len, uint8_t *out, uint16_t cap,
299 uint8_t pn[6]);
300#endif
301
302#endif // VSF_USE_WIFI
303#endif // __VSF_WIFI_PRIV_H__
Task-independent timer; when it expires the kernel task invokes the user callback.
Definition vsf_eda.h:1937
The event-driven task (TCB); every VSF task is an eda at the bottom. Tasks share stacks and only occu...
Definition vsf_eda.h:1838
unsigned short uint16_t
Definition stdint.h:7
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Definition vsf_wifi.h:161
Definition vsf_wifi.h:314
Definition vsf_wifi.h:432
Definition vsf_wifi_radio.h:59
Definition vsf_wifi_priv.h:39
const vsf_wifi_radio_ops_t * ops
Definition vsf_wifi_priv.h:40
vsf_wifi_radio_done_t adapter_done
Definition vsf_wifi_priv.h:47
vsf_wifi_t * wifi
Definition vsf_wifi_priv.h:43
void * rx_param
Definition vsf_wifi_priv.h:42
vsf_wifi_radio_rx_cb_t rx_cb
Definition vsf_wifi_priv.h:41
Definition vsf_wifi.h:215
Definition vsf_wifi.h:176
Definition vsf_wifi_priv.h:57
uint8_t wpa_rsn_ie_len
Definition vsf_wifi_priv.h:138
vsf_callback_timer_t scan_timer
Definition vsf_wifi_priv.h:84
bool scanning
Definition vsf_wifi_priv.h:78
bool ptk_pending
Definition vsf_wifi_priv.h:111
uint16_t bss_max_idle_period
Definition vsf_wifi_priv.h:233
const vsf_wifi_reg_bus_t * reg_bus
Definition vsf_wifi_priv.h:59
uint8_t mlme_ssid_len
Definition vsf_wifi_priv.h:100
const vsf_wifi_chip_drv_t * drv
Definition vsf_wifi_priv.h:58
uint8_t scan_channel_idx
Definition vsf_wifi_priv.h:79
uint32_t len
Definition vsf_wifi_priv.h:205
uint32_t reg
Definition vsf_wifi_priv.h:211
uint8_t scan_channels[VSF_WIFI_CFG_SCAN_MAX_CHANNELS]
Definition vsf_wifi_priv.h:81
const void * bus_ops
Definition vsf_wifi_priv.h:272
bool wpa_hw_crypto
Definition vsf_wifi_priv.h:160
uint8_t scan_num_channels
Definition vsf_wifi_priv.h:80
bool script_busy
Definition vsf_wifi_priv.h:194
void * netif_param
Definition vsf_wifi_priv.h:68
bool script_is_blob
Definition vsf_wifi_priv.h:195
uint8_t wpa_rsn_ie[24]
Definition vsf_wifi_priv.h:137
const vsf_wifi_reg_op_t * ops
Definition vsf_wifi_priv.h:199
vsf_wifi_radio_t raw_radio
Definition vsf_wifi_priv.h:280
union vsf_wifi_t::@352 s
vsf_callback_timer_t reconnect_timer
Definition vsf_wifi_priv.h:171
uint8_t wpa_anonce[32]
Definition vsf_wifi_priv.h:146
void * chip_priv
Definition vsf_wifi_priv.h:249
vsf_wifi_reg_op_t scratch_ops[VSF_WIFI_CFG_SCRATCH_OPS]
Definition vsf_wifi_priv.h:243
uint16_t idx
Definition vsf_wifi_priv.h:201
uint16_t retry_left
Definition vsf_wifi_priv.h:212
bool disconnecting
Definition vsf_wifi_priv.h:73
uint8_t wpa_snonce[32]
Definition vsf_wifi_priv.h:147
uint8_t channel
Definition vsf_wifi_priv.h:70
uint16_t chunk_size
Definition vsf_wifi_priv.h:208
uint32_t last_val
Definition vsf_wifi_priv.h:216
uint16_t mlme_aid
Definition vsf_wifi_priv.h:114
uint8_t mlme_ssid[33]
Definition vsf_wifi_priv.h:99
uint16_t count
Definition vsf_wifi_priv.h:200
vsf_wifi_attach_fail_t attach_fail
Definition vsf_wifi_priv.h:61
uint8_t mac[6]
Definition vsf_wifi_priv.h:74
void * match_ctx
Definition vsf_wifi_priv.h:218
bool auto_reconnect
Definition vsf_wifi_priv.h:167
uint8_t mlme_bssid[6]
Definition vsf_wifi_priv.h:98
uint8_t mlme_state
Definition vsf_wifi_priv.h:97
vsf_callback_timer_t mlme_timer
Definition vsf_wifi_priv.h:124
uint8_t mlme_channel
Definition vsf_wifi_priv.h:101
uint8_t wpa_gtk_keyidx
Definition vsf_wifi_priv.h:150
struct vsf_wifi_t::@352::@354 blob
uint8_t wpa_tx_pn[6]
Definition vsf_wifi_priv.h:159
uint8_t connect_bw
Definition vsf_wifi_priv.h:102
uint8_t mlme_retry
Definition vsf_wifi_priv.h:103
uint16_t reserved
Definition vsf_wifi_priv.h:215
bool bss_wmm
Definition vsf_wifi_priv.h:72
vsf_wifi_done_t script_done
Definition vsf_wifi_priv.h:196
uint8_t wpa_ptk[48]
Definition vsf_wifi_priv.h:145
uint32_t wpa_ccmp_verify_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:180
const vsf_wifi_netif_ops_t * netif_ops
Definition vsf_wifi_priv.h:67
uint32_t base_reg
Definition vsf_wifi_priv.h:207
vsf_wifi_auth_cfg_t wpa_auth
Definition vsf_wifi_priv.h:136
uint16_t tx_seq
Definition vsf_wifi_priv.h:122
vsf_wifi_done_t backend_chain_done
Definition vsf_wifi_priv.h:229
uint16_t scan_dwell_ms
Definition vsf_wifi_priv.h:82
bool mlme_linked
Definition vsf_wifi_priv.h:168
uint8_t wpa_ccmp_fail_cnt
Definition vsf_wifi_priv.h:183
struct vsf_wifi_t::@352::@355 read_poll
vsf_callback_timer_t read_poll_timer
Definition vsf_wifi_priv.h:89
vsf_eda_t * post_eda
Definition vsf_wifi_priv.h:60
const uint8_t * data
Definition vsf_wifi_priv.h:204
uint8_t wpa_replay[8]
Definition vsf_wifi_priv.h:151
uint8_t wpa_gtk[32]
Definition vsf_wifi_priv.h:148
struct vsf_wifi_t::@351 key_install
bool gtk_pending
Definition vsf_wifi_priv.h:112
uint8_t wpa_gtk_len
Definition vsf_wifi_priv.h:149
uint32_t wpa_ccmp_tx_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:177
uint8_t wpa_ccmp_verify_cnt
Definition vsf_wifi_priv.h:181
bool raw_radio_active
Definition vsf_wifi_priv.h:279
struct vsf_wifi_t::@352::@353 script
vsf_wifi_reg_match_fn_t match
Definition vsf_wifi_priv.h:217
uint16_t interval_ms
Definition vsf_wifi_priv.h:214
bool is_ready
Definition vsf_wifi_priv.h:71
bool wpa_ptk_valid
Definition vsf_wifi_priv.h:152
uint32_t offset
Definition vsf_wifi_priv.h:206
uint32_t wpa_ccmp_rx_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:178
uint8_t auto_reconnect_retries
Definition vsf_wifi_priv.h:169
bool(* vsf_wifi_reg_match_fn_t)(void *ctx, uint32_t val)
Definition vsf_wifi.h:130
#define VSF_WIFI_CFG_SCRATCH_OPS
Definition vsf_wifi.h:105
#define VSF_WIFI_CFG_SCAN_MAX_CHANNELS
Definition vsf_wifi.h:110
void(* vsf_wifi_done_t)(vsf_wifi_t *wifi, vsf_err_t err)
Definition vsf_wifi.h:123
void(* vsf_wifi_attach_fail_t)(vsf_wifi_t *wifi, vsf_err_t err)
Definition vsf_wifi.h:569
#define VSF_WIFI_CFG_CCMP_BUF_SIZE
Definition vsf_wifi_priv.h:28
uint16_t vsf_wifi_ccmp_encap_with_pn(vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len, uint8_t *out, uint16_t cap, uint8_t pn[6])
Definition vsf_wifi.c:966
void(* vsf_wifi_radio_rx_cb_t)(vsf_wifi_radio_t *radio, void *param, uint8_t *frame, uint16_t len)
Definition vsf_wifi_radio.h:42
void(* vsf_wifi_radio_done_t)(vsf_wifi_radio_t *radio, vsf_err_t err)
Definition vsf_wifi_radio.h:46
Generated from commit: vsfteam/vsf@a5104db