wpa.c
来自「台湾RALink公司的 rt2570无线 802.11g 网卡的 驱动的源代码 」· C语言 代码 · 共 1,350 行 · 第 1/4 页
C
1,350 行
/*
***************************************************************************
* Ralink Tech Inc.
* 4F, No. 2 Technology 5th Rd.
* Science-based Industrial Park
* Hsin-chu, Taiwan, R.O.C.
*
* (c) Copyright 2002-2004, Ralink Technology, Inc.
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
************************************************************************
Module Name:
wpa.c
Abstract:
Revision History:
Who When What
-------- ---------- ----------------------------------------------
Name Date Modification logs
Jan Lee 2005-06-01 Release
*/
#include "rt_config.h"
UCHAR CipherWpaPskTkip[] = {
0xDD, 0x16, // RSN IE
0x00, 0x50, 0xf2, 0x01, // oui
0x01, 0x00, // Version
0x00, 0x50, 0xf2, 0x02, // Multicast
0x01, 0x00, // Number of unicast
0x00, 0x50, 0xf2, 0x02, // unicast
0x01, 0x00, // number of authentication method
0x00, 0x50, 0xf2, 0x02 // authentication
};
UCHAR CipherWpaPskTkipLen = (sizeof(CipherWpaPskTkip) / sizeof(UCHAR));
UCHAR CipherWpaPskAes[] = {
0xDD, 0x16, // RSN IE
0x00, 0x50, 0xf2, 0x01, // oui
0x01, 0x00, // Version
0x00, 0x50, 0xf2, 0x04, // Multicast
0x01, 0x00, // Number of unicast
0x00, 0x50, 0xf2, 0x04, // unicast
0x01, 0x00, // number of authentication method
0x00, 0x50, 0xf2, 0x02 // authentication
};
UCHAR CipherWpaPskAesLen = (sizeof(CipherWpaPskAes) / sizeof(UCHAR));
/*
========================================================================
Routine Description:
Classify WPA EAP message type
Arguments:
EAPType Value of EAP message type
MsgType Internal Message definition for MLME state machine
Return Value:
TRUE Found appropriate message type
FALSE No appropriate message type
Note:
All these constants are defined in wpa.h
For supplicant, there is only EAPOL Key message avaliable
========================================================================
*/
BOOLEAN WpaMsgTypeSubst(
IN UCHAR EAPType,
OUT ULONG *MsgType)
{
switch (EAPType)
{
case EAPPacket:
*MsgType = EAP_MSG_TYPE_EAPPacket;
break;
case EAPOLStart:
*MsgType = EAP_MSG_TYPE_EAPOLStart;
break;
case EAPOLLogoff:
*MsgType = EAP_MSG_TYPE_EAPOLLogoff;
break;
case EAPOLKey:
*MsgType = EAP_MSG_TYPE_EAPOLKey;
break;
case EAPOLASFAlert:
*MsgType = EAP_MSG_TYPE_EAPOLASFAlert;
break;
default:
DBGPRINT(RT_DEBUG_ERROR, "WpaMsgTypeSubst : return FALSE; \n");
return FALSE;
}
return TRUE;
}
/*
==========================================================================
Description:
association state machine init, including state transition and timer init
Parameters:
S - pointer to the association state machine
==========================================================================
*/
VOID WpaPskStateMachineInit(
IN PRT2570ADAPTER pAd,
IN STATE_MACHINE *S,
OUT STATE_MACHINE_FUNC Trans[])
{
StateMachineInit(S, (STATE_MACHINE_FUNC*)Trans, MAX_WPA_PSK_STATE, MAX_WPA_PSK_MSG, (STATE_MACHINE_FUNC)Drop, WPA_PSK_IDLE, WPA_MACHINE_BASE);
StateMachineSetAction(S, WPA_PSK_IDLE, EAP_MSG_TYPE_EAPOLKey, (STATE_MACHINE_FUNC)WpaEAPOLKeyAction);
}
/*
==========================================================================
Description:
This is state machine function.
When receiving EAPOL packets which is for 802.1x key management.
Use both in WPA, and WPAPSK case.
In this function, further dispatch to different functions according to the received packet. 3 categories are :
1. normal 4-way pairwisekey and 2-way groupkey handshake
2. MIC error (Countermeasures attack) report packet from STA.
3. Request for pairwise/group key update from STA
Return:
==========================================================================
*/
VOID WpaEAPOLKeyAction(
IN PRT2570ADAPTER pAdapter,
IN MLME_QUEUE_ELEM *Elem)
{
INT MsgType;
PKEY_DESCRIPTER pKeyDesc;
PHEADER_802_11 pHeader; //red
DBGPRINT(RT_DEBUG_TRACE, "-----> WpaEAPOLKeyAction\n");
pHeader = (PHEADER_802_11) &Elem->Msg[0];//red
// Get 802.11 header first
if( (pAdapter->PortCfg.CipherAlg == CIPHER_TKIP || pAdapter->PortCfg.CipherAlg == CIPHER_AES) && pHeader->Controlhead.Frame.Wep)
pKeyDesc = (PKEY_DESCRIPTER) &Elem->Msg[(LENGTH_802_11 + LENGTH_802_1_H + 8 + LENGTH_EAPOL_H)];
else
pKeyDesc = (PKEY_DESCRIPTER) &Elem->Msg[(LENGTH_802_11 + LENGTH_802_1_H + LENGTH_EAPOL_H)];
// Sanity check, this should only happen in WPA-PSK mode
if (pAdapter->PortCfg.AuthMode != Ndis802_11AuthModeWPAPSK)
return;
// 0. Debug print all bit information
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Key Description Version %d\n", pKeyDesc->KeyInfo.KeyDescVer);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Key Type %d\n", pKeyDesc->KeyInfo.KeyType);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Key Index %d\n", pKeyDesc->KeyInfo.KeyIndex);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Install %d\n", pKeyDesc->KeyInfo.Install);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Key Ack %d\n", pKeyDesc->KeyInfo.KeyAck);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Key MIC %d\n", pKeyDesc->KeyInfo.KeyMic);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Secure %d\n", pKeyDesc->KeyInfo.Secure);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Error %d\n", pKeyDesc->KeyInfo.Error);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo Request %d\n", pKeyDesc->KeyInfo.Request);
DBGPRINT(RT_DEBUG_INFO, "KeyInfo DL %d\n", pKeyDesc->KeyInfo.DL);
// 1. Check EAPOL frame version and type
if( (pAdapter->PortCfg.CipherAlg == CIPHER_TKIP || pAdapter->PortCfg.CipherAlg == CIPHER_AES) && pHeader->Controlhead.Frame.Wep)//red
{
if ((Elem->Msg[LENGTH_802_11+LENGTH_802_1_H + 8] != EAPOL_VER) || (pKeyDesc->Type != RSN_KEY_DESC))
{
DBGPRINT(RT_DEBUG_ERROR, " Key descripter does not match with WPA rule \n");
return;
}
}
else
{
if ((Elem->Msg[LENGTH_802_11+LENGTH_802_1_H] != EAPOL_VER) || (pKeyDesc->Type != RSN_KEY_DESC))
{
DBGPRINT(RT_DEBUG_ERROR, " Key descripter does not match with WPA rule \n");
return;
}
}
// 2.Check Version for AES & TKIP
if ((pAdapter->PortCfg.WepStatus == Ndis802_11Encryption3Enabled) && (pKeyDesc->KeyInfo.KeyDescVer != DESC_TYPE_AES))
{
DBGPRINT(RT_DEBUG_ERROR, " Key descripter version not match AES \n");
return;
}
else if ((pAdapter->PortCfg.WepStatus == Ndis802_11Encryption2Enabled) && (pKeyDesc->KeyInfo.KeyDescVer != DESC_TYPE_TKIP))
{
DBGPRINT(RT_DEBUG_ERROR, " Key descripter version not match TKIP \n");
return;
}
// First validate replay counter, only accept message with larger replay counter
if (RTMPCompareMemory(pKeyDesc->ReplayCounter, pAdapter->PortCfg.ReplayCounter, LEN_KEY_DESC_REPLAY) != 1)
return;
// Classify message Type, either pairwise message 1, 3, or group message 1 for supplicant
MsgType = EAPOL_MSG_INVALID;
if ((pKeyDesc->KeyInfo.KeyType == 1) &&
(pKeyDesc->KeyInfo.KeyIndex == 0) &&
(pKeyDesc->KeyInfo.KeyAck == 1) &&
(pKeyDesc->KeyInfo.KeyMic == 0) &&
(pKeyDesc->KeyInfo.Secure == 0) &&
(pKeyDesc->KeyInfo.Error == 0) &&
(pKeyDesc->KeyInfo.Request == 0))
{
MsgType = EAPOL_PAIR_MSG_1;
DBGPRINT(RT_DEBUG_ERROR, "Receive EAPOL Key Pairwise Message 1\n");
}
else if ((pKeyDesc->KeyInfo.KeyType == 1) &&
(pKeyDesc->KeyInfo.KeyIndex == 0) &&
(pKeyDesc->KeyInfo.KeyAck == 1) &&
(pKeyDesc->KeyInfo.KeyMic == 1) &&
(pKeyDesc->KeyInfo.Secure == 0) &&
(pKeyDesc->KeyInfo.Error == 0) &&
(pKeyDesc->KeyInfo.Request == 0))
{
MsgType = EAPOL_PAIR_MSG_3;
DBGPRINT(RT_DEBUG_ERROR, "Receive EAPOL Key Pairwise Message 3\n");
}
else if ((pKeyDesc->KeyInfo.KeyType == 0) &&
(pKeyDesc->KeyInfo.KeyIndex != 0) &&
(pKeyDesc->KeyInfo.KeyAck == 1) &&
(pKeyDesc->KeyInfo.KeyMic == 1) &&
(pKeyDesc->KeyInfo.Secure == 1) &&
(pKeyDesc->KeyInfo.Error == 0) &&
(pKeyDesc->KeyInfo.Request == 0))
{
MsgType = EAPOL_GROUP_MSG_1;
DBGPRINT(RT_DEBUG_ERROR, "Receive EAPOL Key Group Message 1\n");
}
// We will assume link is up (assoc suceess and port not secured).
// All state has to be able to process message from previous state
switch (pAdapter->PortCfg.WpaState)
{
case SS_START:
if (MsgType == EAPOL_PAIR_MSG_1)
{
WpaPairMsg1Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_WAIT_MSG_3;
}
break;
case SS_WAIT_MSG_3:
if (MsgType == EAPOL_PAIR_MSG_1)
{
WpaPairMsg1Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_WAIT_MSG_3;
}
else if (MsgType == EAPOL_PAIR_MSG_3)
{
WpaPairMsg3Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_WAIT_GROUP;
}
break;
case SS_WAIT_GROUP: // When doing group key exchange
case SS_FINISH: // This happened when update group key
if (MsgType == EAPOL_PAIR_MSG_1)
{
WpaPairMsg1Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_WAIT_MSG_3;
// Reset port secured variable
pAdapter->PortCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
}
else if (MsgType == EAPOL_PAIR_MSG_3)
{
WpaPairMsg3Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_WAIT_GROUP;
// Reset port secured variable
pAdapter->PortCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
}
else if (MsgType == EAPOL_GROUP_MSG_1)
{
WpaGroupMsg1Action(pAdapter, Elem);
pAdapter->PortCfg.WpaState = SS_FINISH;
}
break;
default:
break;
}
DBGPRINT(RT_DEBUG_TRACE, "<----- WpaEAPOLKeyAction\n");
}
/*
========================================================================
Routine Description:
Process Pairwise key 4-way handshaking
Arguments:
pAdapter Pointer to our adapter
Elem Message body
Return Value:
None
Note:
========================================================================
*/
VOID WpaPairMsg1Action(
IN PRT2570ADAPTER pAdapter,
IN MLME_QUEUE_ELEM *Elem)
{
PHEADER_802_11 pHeader;
UCHAR PTK[80];
UCHAR *OutBuffer = NULL;
HEADER_802_11 Header_802_11;
NDIS_STATUS NStatus;
UCHAR AckRate = RATE_2;
USHORT AckDuration = 0;
ULONG FrameLen = 0;
UCHAR EAPHEAD[8] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00,0x88,0x8e};
PEAPOL_PACKET pMsg1;
EAPOL_PACKET Packet;
UCHAR Mic[16];
DBGPRINT(RT_DEBUG_ERROR, "WpaPairMsg1Action ----->\n");
pHeader = (PHEADER_802_11) Elem->Msg;
// Save Data Length to pDesc for receiving packet, then put in outgoing frame Data Len fields.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?