一、先了解一下状态机的基本定义,hostapd里有多个状态机,这里是状态机的定义模板
/**
* SM_STATE - Declaration of a state machine function
* @machine: State machine name
* @state: State machine state
*
* This macro is used to declare a state machine function. It is used in place
* of a C function definition to declare functions to be run when the state is
* entered by calling SM_ENTER or SM_ENTER_GLOBAL.
*/
#define SM_STATE(machine, state) \
static void sm_ ## machine ## _ ## state ## _Enter(STATE_MACHINE_DATA *sm, \
int global)
//状态机的状态函数模板
/**
* SM_ENTRY - State machine function entry point
* @machine: State machine name
* @state: State machine state
*
* This macro is used inside each state machine function declared with
* SM_STATE. SM_ENTRY should be in the beginning of the function body, but
* after declaration of possible local variables. This macro prints debug
* information about state transition and update the state machine state.
*/
#define SM_ENTRY(machine, state) \
if (!global || sm->machine ## _state != machine ## _ ## state) { \
sm->changed = true; \
wpa_printf(MSG_DEBUG, STATE_MACHINE_DEBUG_PREFIX ": " #machine \
" entering state " #state); \
} \
sm->machine ## _state = machine ## _ ## state;
//相当于状态机切换后的入口日志
/**
* SM_ENTER - Enter a new state machine state
* @machine: State machine name
* @state: State machine state
*
* This macro expands to a function call to a state machine function defined
* with SM_STATE macro. SM_ENTER is used in a state machine step function to
* move the state machine to a new state.
*/
#define SM_ENTER(machine, state) \
sm_ ## machine ## _ ## state ## _Enter(sm, 0)
//状态机转换调用函数
/**
* SM_STEP - Declaration of a state machine step function
* @machine: State machine name
*
* This macro is used to declare a state machine step function. It is used in
* place of a C function definition to declare a function that is used to move
* state machine to a new state based on state variables. This function uses
* SM_ENTER and SM_ENTER_GLOBAL macros to enter new state.
*/
#define SM_STEP(machine) \
static void sm_ ## machine ## _Step(STATE_MACHINE_DATA *sm)
//状态机的实现
/**
* SM_STEP_RUN - Call the state machine step function
* @machine: State machine name
*
* This macro expands to a function call to a state machine step function
* defined with SM_STEP macro.
*/
#define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
//状态机的启动函数
二、wpa ptk状态机的状态
enum {
WPA_PTK_INITIALIZE, WPA_PTK_DISCONNECT, WPA_PTK_DISCONNECTED,
WPA_PTK_AUTHENTICATION, WPA_PTK_AUTHENTICATION2,
WPA_PTK_INITPMK, WPA_PTK_INITPSK, WPA_PTK_PTKSTART,
WPA_PTK_PTKCALCNEGOTIATING, WPA_PTK_PTKCALCNEGOTIATING2,
WPA_PTK_PTKINITNEGOTIATING, WPA_PTK_PTKINITDONE
} wpa_ptk_state;
SM_STEP(WPA_PTK) //状态转移函数
wpa_auth_sm_event
WPA_ASSOC
wpa_sm_step //首次调用
hostapd_new_assoc_sta
wpa_auth_sta_associated //触发init和auth req
AUTHENTICATION2 //生成ANonce
PTKSTART //发送1/4