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#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
117#endif
118
119#if VSF_WIFI_USE_WPA == ENABLED
120 /* ---- WPA2-PSK security context ----
121 *
122 * wpa_auth is captured by vsf_wifi_set_auth_mode(); auth_mode == OPEN
123 * means no RSN (the assoc-req carries no RSN IE and no 4-way handshake
124 * runs). wpa_rsn_ie holds the STA RSN IE bytes sent in the assoc-req so
125 * the 4-way handshake M2 can echo them unchanged. Handshake-specific
126 * fields (PTK / nonces / GTK / replay / substate) are added by the WPA
127 * state machine. */
131
132 /* 4-way handshake working state (vsf_wifi_wpa.c). The PMK lives in
133 * wpa_auth.psk; PTK = KCK(16)|KEK(16)|TK(16) is derived on M1. anonce /
134 * snonce feed the PRF; gtk holds the group key unwrapped from M3.
135 * replay echoes the AP's EAPOL-Key replay counter; ptk_valid gates the
136 * CCMP data path until the handshake installs keys. */
145
146 /* CCMP data path (vsf_wifi.c). wpa_tx_pn is the 48-bit packet-number
147 * counter (little-endian, pn[0] = LSB) advanced once per encrypted TX
148 * MPDU; it seeds the CCMP header and CCM nonce. wpa_hw_crypto is set
149 * when a chip crypto_ops backend installed the keys, in which case the
150 * software CCMP encap/decap is bypassed (the chip does it in-line). */
153
154 /* Software CCMP working buffers. Placed in the instance instead of local
155 * static arrays so the code remains re-entrant when multiple wifi instances
156 * exist or when an async path could overlap. */
159#if VSF_WIFI_CFG_CCMP_TX_SELF_VERIFY == ENABLED
162#endif
164#endif
165
166 /* ---- Script / blob dispatcher state ----
167 *
168 * Only one outstanding script or blob is allowed per wifi (the reg_bus
169 * concurrency contract enforces the same limit at the bus level). The
170 * dispatcher walks the op array via the chained __script_step_done /
171 * __blob_step_done callbacks, each of which feeds the next op to
172 * reg_bus.
173 */
177 union {
178 struct {
183 struct {
184 const uint8_t *data;
186 uint32_t offset; /* used by the reg_write fallback path */
190 struct {
192 uint16_t retry_left; /* attempts remaining after
193 * the current in-flight read */
196 uint32_t last_val; /* read landing zone */
200 } s;
201
202 /*
203 * Backend-level chain slot. When a chip op needs to issue several
204 * scripts back-to-back (firmware_load = run_blob + post-fw script),
205 * it stashes the *outer* `done` here, then internally chains via local
206 * helpers. Only one chain may be in flight at a time — same constraint
207 * as the script dispatcher itself.
208 */
210
211 /* BSS Max Idle Period from assoc-resp IE 90 (units of 1000 TU).
212 * 0 means the AP did not advertise it. */
214
215#if (VSF_WIFI_CFG_KEEPALIVE_PERIOD_MS > 0) \
216 && (VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED)
217 /* 802.11 null-data-frame keepalive state. Started on link-up and stopped
218 * on link-down / fini to keep the AP from deauthenticating an idle STA. */
219 vsf_callback_timer_t keepalive_timer;
220 uint32_t keepalive_period_ms;
221#endif
222
224
225 /* Chip-driver private data pointer. Set by the bus driver after
226 * vsf_wifi_init() for command/event-based chips that need per-instance
227 * state beyond the generic wifi struct (e.g. MediaTek mt76). The generic
228 * layer never touches this field. */
230
231 /* Chip-specific bus operations.
232 *
233 * This abstracts the *communication semantics* between the chip driver and
234 * the host, not the underlying physical bus. Register-based chips (e.g.
235 * Ralink RT28xx) use reg_bus above: the chip driver only knows "read/write
236 * a register", and the bus driver implements that with USB ep0 vendor
237 * requests, SDIO CMD53, etc.
238 *
239 * Command/event-based chips (e.g. MediaTek mt76) store a chip-defined
240 * vtable here (cast by the chip driver to the appropriate type). The
241 * operations are named after what the *chip* does (send MCU command,
242 * submit TX frame, start RX, ...), so the same chip driver can plug into
243 * different physical buses without modification.
244 *
245 * Design rule: bus_ops MUST NOT contain physical-bus-specific primitives
246 * such as USB ep0 vendor_request. If a chip genuinely needs a bus-specific
247 * control primitive, define a chip-private vtable whose FIRST member is
248 * vsf_wifi_reg_bus_t (when register semantics exist) or another chip-defined
249 * base, and place the private primitive after the shared part. The bus
250 * driver supplies the private vtable; the chip driver casts bus_ops to the
251 * chip-private type. */
252 const void *bus_ops;
253
254 /* ---- Raw WiFi radio state ----
255 *
256 * When raw_radio_active is true the instance is in raw-radio mode and the
257 * standard MLME/scan/WPA state machine is gated out. Received frames are
258 * delivered to raw_radio.rx_cb instead of the normal netif/weak hooks. */
261};
262
263#if VSF_WIFI_USE_WPA == ENABLED
264/* CCMP-encrypt a plaintext data MPDU using a caller-supplied 48-bit PN
265 * counter (little-endian, pn[0] = LSB). If `pn` is NULL the function falls
266 * back to wifi->wpa_tx_pn. Returns the encrypted MPDU length, or 0 on
267 * failure. Used by chip drivers that need to software-encrypt specific
268 * frames (e.g. MT76 broadcast/multicast) while leaving unicast traffic to
269 * the hardware crypto backend. */
271 const uint8_t *frame, uint16_t len, uint8_t *out, uint16_t cap,
272 uint8_t pn[6]);
273#endif
274
275#endif // VSF_USE_WIFI
276#endif // __VSF_WIFI_PRIV_H__
Definition vsf_eda.h:851
Definition vsf_eda.h:769
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:312
Definition vsf_wifi.h:430
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:130
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:213
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:185
uint32_t reg
Definition vsf_wifi_priv.h:191
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:252
bool wpa_hw_crypto
Definition vsf_wifi_priv.h:152
uint8_t scan_num_channels
Definition vsf_wifi_priv.h:80
bool script_busy
Definition vsf_wifi_priv.h:174
void * netif_param
Definition vsf_wifi_priv.h:68
bool script_is_blob
Definition vsf_wifi_priv.h:175
uint8_t wpa_rsn_ie[24]
Definition vsf_wifi_priv.h:129
const vsf_wifi_reg_op_t * ops
Definition vsf_wifi_priv.h:179
struct vsf_wifi_t::@349::@351 blob
vsf_wifi_radio_t raw_radio
Definition vsf_wifi_priv.h:260
struct vsf_wifi_t::@348 key_install
uint8_t wpa_anonce[32]
Definition vsf_wifi_priv.h:138
void * chip_priv
Definition vsf_wifi_priv.h:229
vsf_wifi_reg_op_t scratch_ops[VSF_WIFI_CFG_SCRATCH_OPS]
Definition vsf_wifi_priv.h:223
uint16_t idx
Definition vsf_wifi_priv.h:181
uint16_t retry_left
Definition vsf_wifi_priv.h:192
bool disconnecting
Definition vsf_wifi_priv.h:73
uint8_t wpa_snonce[32]
Definition vsf_wifi_priv.h:139
union vsf_wifi_t::@349 s
uint8_t channel
Definition vsf_wifi_priv.h:70
uint16_t chunk_size
Definition vsf_wifi_priv.h:188
uint32_t last_val
Definition vsf_wifi_priv.h:196
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:180
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:198
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:116
uint8_t mlme_channel
Definition vsf_wifi_priv.h:101
uint8_t wpa_gtk_keyidx
Definition vsf_wifi_priv.h:142
uint8_t wpa_tx_pn[6]
Definition vsf_wifi_priv.h:151
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:195
bool bss_wmm
Definition vsf_wifi_priv.h:72
vsf_wifi_done_t script_done
Definition vsf_wifi_priv.h:176
uint8_t wpa_ptk[48]
Definition vsf_wifi_priv.h:137
uint32_t wpa_ccmp_verify_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:160
const vsf_wifi_netif_ops_t * netif_ops
Definition vsf_wifi_priv.h:67
uint32_t base_reg
Definition vsf_wifi_priv.h:187
vsf_wifi_auth_cfg_t wpa_auth
Definition vsf_wifi_priv.h:128
vsf_wifi_done_t backend_chain_done
Definition vsf_wifi_priv.h:209
uint16_t scan_dwell_ms
Definition vsf_wifi_priv.h:82
uint8_t wpa_ccmp_fail_cnt
Definition vsf_wifi_priv.h:163
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:184
uint8_t wpa_replay[8]
Definition vsf_wifi_priv.h:143
uint8_t wpa_gtk[32]
Definition vsf_wifi_priv.h:140
bool gtk_pending
Definition vsf_wifi_priv.h:112
uint8_t wpa_gtk_len
Definition vsf_wifi_priv.h:141
uint32_t wpa_ccmp_tx_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:157
uint8_t wpa_ccmp_verify_cnt
Definition vsf_wifi_priv.h:161
bool raw_radio_active
Definition vsf_wifi_priv.h:259
struct vsf_wifi_t::@349::@350 script
vsf_wifi_reg_match_fn_t match
Definition vsf_wifi_priv.h:197
uint16_t interval_ms
Definition vsf_wifi_priv.h:194
bool is_ready
Definition vsf_wifi_priv.h:71
bool wpa_ptk_valid
Definition vsf_wifi_priv.h:144
uint32_t offset
Definition vsf_wifi_priv.h:186
uint32_t wpa_ccmp_rx_buf[(VSF_WIFI_CFG_CCMP_BUF_SIZE+3)/4]
Definition vsf_wifi_priv.h:158
struct vsf_wifi_t::@349::@352 read_poll
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:559
#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:957
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@5de27dd