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/*============================ TYPES =========================================*/
28
29/*
30 * Raw WiFi radio instance embedded in vsf_wifi_t.
31 *
32 * The public vsf_wifi_radio.h header only forward-declares this type; the
33 * full definition is private so the layout can carry internal adapter state.
34 */
38 void *rx_param;
40
41 /* Internal adapter state: stores the user `done` callback when a raw
42 * radio operation is delegated to a vsf_wifi_done_t-style hook. */
44};
45
46/*
47 * The wifi layer is chip-agnostic but knows about the optional register-bus
48 * helper used by register-based chips (e.g. RT2X00). reg_bus may be NULL for
49 * chips that use a command/event-based bus (e.g. MediaTek mt76). The bus
50 * driver embeds vsf_wifi_t inside a larger struct (e.g. vk_usbh_wifi_t) and
51 * recovers its outer instance via vsf_container_of().
52 */
53struct vsf_wifi_t {
58
59 /* Optional network-stack backend (lwIP netdrv adapter, etc.) bound at
60 * runtime via vsf_wifi_netdrv_attach(). When netif_ops != NULL the core
61 * routes decrypted RX data frames and link up/down events here instead
62 * of the weak vsf_wifi_on_* hooks. */
65
68 bool bss_wmm; /* current BSS supports WMM/QoS (set on assoc) */
69 bool disconnecting; /* set in fini() to gate stale callbacks */
70 uint8_t mac[6]; /* populated by chip-driver EEPROM stage; */
71 /* zero until firmware_load chain finishes */
72
73 /* ---- Scan state (wifi-driven hop scheduler) ---- */
79#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
81 /* Read-poll spacing timer. Logically independent of scan_timer:
82 * read_poll runs during firmware_load (is_ready == false) while scan
83 * runs after on_ready, but they could in principle overlap if a
84 * future chip op uses run_read_poll mid-flight, so we keep two. */
86#endif
87
88 /* ---- MLME connection state (OPEN-system auth + association) ----
89 *
90 * Driven by the wifi layer (vsf_wifi.c). mlme_state walks
91 * IDLE -> AUTH -> ASSOC -> RUN; each handshake step arms mlme_timer and
92 * retransmits up to mlme_retry times before giving up. */
93 uint8_t mlme_state; /* WIFI_MLME_xxx */
94 uint8_t mlme_bssid[6]; /* target AP BSSID */
95 uint8_t mlme_ssid[33]; /* target SSID (for assoc-req SSID IE) */
97 uint8_t mlme_channel; /* target channel */
98 uint8_t connect_bw; /* WIFI_BW_xxx for next connect/set_channel*/
99 uint8_t mlme_retry; /* retransmits left for current step */
100
101 /* Deferred hardware key install. When the chip crypto backend reports
102 * VSF_ERR_NOT_AVAILABLE because a previous register script is still in
103 * flight, the handshake is retried automatically once that script
104 * completes. This avoids a race between the async connect script and the
105 * 4-way handshake on register-based chips such as RT5572. */
106 struct {
110 uint16_t mlme_aid; /* association id from assoc-resp */
111#if VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED
113#endif
114
115#if VSF_WIFI_USE_WPA == ENABLED
116 /* ---- WPA2-PSK security context ----
117 *
118 * wpa_auth is captured by vsf_wifi_set_auth_mode(); auth_mode == OPEN
119 * means no RSN (the assoc-req carries no RSN IE and no 4-way handshake
120 * runs). wpa_rsn_ie holds the STA RSN IE bytes sent in the assoc-req so
121 * the 4-way handshake M2 can echo them unchanged. Handshake-specific
122 * fields (PTK / nonces / GTK / replay / substate) are added by the WPA
123 * state machine. */
127
128 /* 4-way handshake working state (vsf_wifi_wpa.c). The PMK lives in
129 * wpa_auth.psk; PTK = KCK(16)|KEK(16)|TK(16) is derived on M1. anonce /
130 * snonce feed the PRF; gtk holds the group key unwrapped from M3.
131 * replay echoes the AP's EAPOL-Key replay counter; ptk_valid gates the
132 * CCMP data path until the handshake installs keys. */
141
142 /* CCMP data path (vsf_wifi.c). wpa_tx_pn is the 48-bit packet-number
143 * counter (little-endian, pn[0] = LSB) advanced once per encrypted TX
144 * MPDU; it seeds the CCMP header and CCM nonce. wpa_hw_crypto is set
145 * when a chip crypto_ops backend installed the keys, in which case the
146 * software CCMP encap/decap is bypassed (the chip does it in-line). */
149#endif
150
151 /* ---- Script / blob dispatcher state ----
152 *
153 * Only one outstanding script or blob is allowed per wifi (the reg_bus
154 * concurrency contract enforces the same limit at the bus level). The
155 * dispatcher walks the op array via the chained __script_step_done /
156 * __blob_step_done callbacks, each of which feeds the next op to
157 * reg_bus.
158 */
162 union {
163 struct {
168 struct {
169 const uint8_t *data;
171 uint32_t offset; /* used by the reg_write fallback path */
175 struct {
177 uint16_t retry_left; /* attempts remaining after
178 * the current in-flight read */
181 uint32_t last_val; /* read landing zone */
184 } s;
185
186 /*
187 * Backend-level chain slot. When a chip op needs to issue several
188 * scripts back-to-back (firmware_load = run_blob + post-fw script),
189 * it stashes the *outer* `done` here, then internally chains via local
190 * helpers. Only one chain may be in flight at a time — same constraint
191 * as the script dispatcher itself.
192 */
194
195 /* BSS Max Idle Period from assoc-resp IE 90 (units of 1000 TU).
196 * 0 means the AP did not advertise it. */
198
199#if (VSF_WIFI_CFG_KEEPALIVE_PERIOD_MS > 0) \
200 && (VSF_KERNEL_CFG_SUPPORT_CALLBACK_TIMER == ENABLED)
201 /* 802.11 null-data-frame keepalive state. Started on link-up and stopped
202 * on link-down / fini to keep the AP from deauthenticating an idle STA. */
203 vsf_callback_timer_t keepalive_timer;
204 uint32_t keepalive_period_ms;
205#endif
206
208
209 /* Chip-driver private data pointer. Set by the bus driver after
210 * vsf_wifi_init() for command/event-based chips that need per-instance
211 * state beyond the generic wifi struct (e.g. MediaTek mt76). The generic
212 * layer never touches this field. */
214
215 /* Chip-specific bus operations.
216 *
217 * This abstracts the *communication semantics* between the chip driver and
218 * the host, not the underlying physical bus. Register-based chips (e.g.
219 * Ralink RT28xx) use reg_bus above: the chip driver only knows "read/write
220 * a register", and the bus driver implements that with USB ep0 vendor
221 * requests, SDIO CMD53, etc.
222 *
223 * Command/event-based chips (e.g. MediaTek mt76) store a chip-defined
224 * vtable here (cast by the chip driver to the appropriate type). The
225 * operations are named after what the *chip* does (send MCU command,
226 * submit TX frame, start RX, ...), so the same chip driver can plug into
227 * different physical buses without modification.
228 *
229 * Design rule: bus_ops MUST NOT contain physical-bus-specific primitives
230 * such as USB ep0 vendor_request. If a chip genuinely needs a bus-specific
231 * control primitive, define a chip-private vtable whose FIRST member is
232 * vsf_wifi_reg_bus_t (when register semantics exist) or another chip-defined
233 * base, and place the private primitive after the shared part. The bus
234 * driver supplies the private vtable; the chip driver casts bus_ops to the
235 * chip-private type. */
236 const void *bus_ops;
237
238 /* ---- Raw WiFi radio state ----
239 *
240 * When raw_radio_active is true the instance is in raw-radio mode and the
241 * standard MLME/scan/WPA state machine is gated out. Received frames are
242 * delivered to raw_radio.rx_cb instead of the normal netif/weak hooks. */
245};
246
247#if VSF_WIFI_USE_WPA == ENABLED
248/* CCMP-encrypt a plaintext data MPDU using a caller-supplied 48-bit PN
249 * counter (little-endian, pn[0] = LSB). If `pn` is NULL the function falls
250 * back to wifi->wpa_tx_pn. Returns the encrypted MPDU length, or 0 on
251 * failure. Used by chip drivers that need to software-encrypt specific
252 * frames (e.g. MT76 broadcast/multicast) while leaving unicast traffic to
253 * the hardware crypto backend. */
255 const uint8_t *frame, uint16_t len, uint8_t *out, uint16_t cap,
256 uint8_t pn[6]);
257#endif
258
259#endif // VSF_USE_WIFI
260#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:160
Definition vsf_wifi.h:304
Definition vsf_wifi.h:399
Definition vsf_wifi_radio.h:59
Definition vsf_wifi_priv.h:35
const vsf_wifi_radio_ops_t * ops
Definition vsf_wifi_priv.h:36
vsf_wifi_radio_done_t adapter_done
Definition vsf_wifi_priv.h:43
vsf_wifi_t * wifi
Definition vsf_wifi_priv.h:39
void * rx_param
Definition vsf_wifi_priv.h:38
vsf_wifi_radio_rx_cb_t rx_cb
Definition vsf_wifi_priv.h:37
Definition vsf_wifi.h:214
Definition vsf_wifi.h:175
Definition vsf_wifi_priv.h:53
uint8_t wpa_rsn_ie_len
Definition vsf_wifi_priv.h:126
vsf_callback_timer_t scan_timer
Definition vsf_wifi_priv.h:80
bool scanning
Definition vsf_wifi_priv.h:74
bool ptk_pending
Definition vsf_wifi_priv.h:107
uint16_t bss_max_idle_period
Definition vsf_wifi_priv.h:197
const vsf_wifi_reg_bus_t * reg_bus
Definition vsf_wifi_priv.h:55
uint8_t mlme_ssid_len
Definition vsf_wifi_priv.h:96
const vsf_wifi_chip_drv_t * drv
Definition vsf_wifi_priv.h:54
struct vsf_wifi_t::@338::@341 read_poll
uint8_t scan_channel_idx
Definition vsf_wifi_priv.h:75
uint32_t len
Definition vsf_wifi_priv.h:170
uint32_t reg
Definition vsf_wifi_priv.h:176
uint8_t scan_channels[VSF_WIFI_CFG_SCAN_MAX_CHANNELS]
Definition vsf_wifi_priv.h:77
const void * bus_ops
Definition vsf_wifi_priv.h:236
bool wpa_hw_crypto
Definition vsf_wifi_priv.h:148
uint8_t scan_num_channels
Definition vsf_wifi_priv.h:76
bool script_busy
Definition vsf_wifi_priv.h:159
void * netif_param
Definition vsf_wifi_priv.h:64
bool script_is_blob
Definition vsf_wifi_priv.h:160
uint8_t wpa_rsn_ie[24]
Definition vsf_wifi_priv.h:125
const vsf_wifi_reg_op_t * ops
Definition vsf_wifi_priv.h:164
vsf_wifi_radio_t raw_radio
Definition vsf_wifi_priv.h:244
struct vsf_wifi_t::@337 key_install
uint8_t wpa_anonce[32]
Definition vsf_wifi_priv.h:134
void * chip_priv
Definition vsf_wifi_priv.h:213
vsf_wifi_reg_op_t scratch_ops[VSF_WIFI_CFG_SCRATCH_OPS]
Definition vsf_wifi_priv.h:207
uint16_t idx
Definition vsf_wifi_priv.h:166
uint16_t retry_left
Definition vsf_wifi_priv.h:177
bool disconnecting
Definition vsf_wifi_priv.h:69
uint8_t wpa_snonce[32]
Definition vsf_wifi_priv.h:135
uint8_t channel
Definition vsf_wifi_priv.h:66
uint16_t chunk_size
Definition vsf_wifi_priv.h:173
uint32_t last_val
Definition vsf_wifi_priv.h:181
uint16_t mlme_aid
Definition vsf_wifi_priv.h:110
uint8_t mlme_ssid[33]
Definition vsf_wifi_priv.h:95
uint16_t count
Definition vsf_wifi_priv.h:165
vsf_wifi_attach_fail_t attach_fail
Definition vsf_wifi_priv.h:57
uint8_t mac[6]
Definition vsf_wifi_priv.h:70
uint8_t mlme_bssid[6]
Definition vsf_wifi_priv.h:94
uint8_t mlme_state
Definition vsf_wifi_priv.h:93
vsf_callback_timer_t mlme_timer
Definition vsf_wifi_priv.h:112
uint8_t mlme_channel
Definition vsf_wifi_priv.h:97
uint8_t wpa_gtk_keyidx
Definition vsf_wifi_priv.h:138
uint8_t wpa_tx_pn[6]
Definition vsf_wifi_priv.h:147
uint8_t connect_bw
Definition vsf_wifi_priv.h:98
uint8_t mlme_retry
Definition vsf_wifi_priv.h:99
uint16_t reserved
Definition vsf_wifi_priv.h:180
bool bss_wmm
Definition vsf_wifi_priv.h:68
vsf_wifi_done_t script_done
Definition vsf_wifi_priv.h:161
uint8_t wpa_ptk[48]
Definition vsf_wifi_priv.h:133
const vsf_wifi_netif_ops_t * netif_ops
Definition vsf_wifi_priv.h:63
uint32_t base_reg
Definition vsf_wifi_priv.h:172
vsf_wifi_auth_cfg_t wpa_auth
Definition vsf_wifi_priv.h:124
vsf_wifi_done_t backend_chain_done
Definition vsf_wifi_priv.h:193
uint16_t scan_dwell_ms
Definition vsf_wifi_priv.h:78
struct vsf_wifi_t::@338::@339 script
vsf_callback_timer_t read_poll_timer
Definition vsf_wifi_priv.h:85
vsf_eda_t * post_eda
Definition vsf_wifi_priv.h:56
const uint8_t * data
Definition vsf_wifi_priv.h:169
uint8_t wpa_replay[8]
Definition vsf_wifi_priv.h:139
uint8_t wpa_gtk[32]
Definition vsf_wifi_priv.h:136
bool gtk_pending
Definition vsf_wifi_priv.h:108
uint8_t wpa_gtk_len
Definition vsf_wifi_priv.h:137
bool raw_radio_active
Definition vsf_wifi_priv.h:243
struct vsf_wifi_t::@338::@340 blob
vsf_wifi_reg_match_fn_t match
Definition vsf_wifi_priv.h:182
uint16_t interval_ms
Definition vsf_wifi_priv.h:179
bool is_ready
Definition vsf_wifi_priv.h:67
bool wpa_ptk_valid
Definition vsf_wifi_priv.h:140
union vsf_wifi_t::@338 s
uint32_t offset
Definition vsf_wifi_priv.h:171
bool(* vsf_wifi_reg_match_fn_t)(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:528
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:929
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@ceb53fd