wlan_bearer/wlanldd/wlan_common/umac_common/src/umacaddbroadcastwepkey.cpp
changeset 0 c40eb8fe8501
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Implementation of WlanAddBroadcastWepKey class
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: 6 %
       
    20 */
       
    21 
       
    22 #include "config.h"
       
    23 #include "umacaddbroadcastwepkey.h"
       
    24 #include "UmacContextImpl.h"
       
    25 #include "UmacWsaAddKey.h"
       
    26 #include "UmacWsaWriteMib.h"
       
    27 #include "UmacWsaKeyIndexMapper.h"
       
    28 
       
    29 #ifndef NDEBUG
       
    30 const TInt8 WlanAddBroadcastWepKey::iName[] 
       
    31     = "wsacomplex-addbroadcastwepkey";
       
    32 
       
    33 const TUint8 WlanAddBroadcastWepKey::iStateName
       
    34     [ESTATEMAX][KMaxStateStringLength] = 
       
    35     {
       
    36         {"EINIT"}, 
       
    37         {"EADDGROUPKEY"}, 
       
    38         {"EWRITEMIB"},
       
    39         {"EADDPAIRWISEKEY"},
       
    40         {"EFINIT"}
       
    41     };
       
    42 
       
    43 const TUint8 WlanAddBroadcastWepKey::iEventName
       
    44     [EEVENTMAX][KMaxEventStringLength] = 
       
    45     {
       
    46         {"ESTATEENTRY"}, {"ETXCOMPLETE"}, {"EABORT"}
       
    47     };
       
    48 #endif // !NDEBUG
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // 
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void WlanAddBroadcastWepKey::Set( 
       
    57     const TMacAddress& aMac,
       
    58     TUint32 aKeyIndex,             
       
    59     TBool aUseAsDefaulKey,                
       
    60     TBool aUseAsPairwiseKey,
       
    61     TUint32 aKeyLength,                      
       
    62     const TUint8 aKey[KMaxWEPKeyLength] )
       
    63     {
       
    64     ((aUseAsPairwiseKey) ? (iFlags |= KUseAsPairwiseKey) 
       
    65         : (iFlags &= ~KUseAsPairwiseKey));
       
    66     ((aUseAsDefaulKey) ? (iFlags |= KUseAsDefaultKey) 
       
    67         : (iFlags &= ~KUseAsDefaultKey));
       
    68 
       
    69     iMacAddr = aMac;
       
    70     iKeyIndex = aKeyIndex;
       
    71     iKeyLength = aKeyLength;                       
       
    72     iKey = aKey;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // 
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void WlanAddBroadcastWepKey::Entry( 
       
    80     WlanContextImpl& aCtxImpl )
       
    81     {
       
    82     if ( aCtxImpl.WsaCmdActive() )
       
    83         {
       
    84         // sanity checking code
       
    85         OsAssert( (TUint8*)("UMAC * panic"), (TUint8*)(WLAN_FILE), __LINE__ );
       
    86         }
       
    87 
       
    88     if ( iState != EINIT )
       
    89         {
       
    90         // this is NOT the start of the the FSM actions
       
    91         // note that we send the ETXCOMPLETE event as the states
       
    92         // that wait for it are the only ones that can be interrupted
       
    93         // as they are asynchronous operations by nature
       
    94         // and wait for corresponding WHA completion method
       
    95         Fsm( aCtxImpl, ETXCOMPLETE );
       
    96         }
       
    97     else
       
    98         {
       
    99         // this is the start of the the FSM actions
       
   100         Fsm( aCtxImpl, ESTATEENTRY );
       
   101         }
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // 
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void WlanAddBroadcastWepKey::Exit( 
       
   109     WlanContextImpl& aCtxImpl)
       
   110     {
       
   111     iState = EINIT;
       
   112     // make sure we don't leak memory
       
   113     // don't worry about the key material as the memory associated
       
   114     // to it is released at command completion time
       
   115     os_free( iMemory );
       
   116     iMemory = NULL;
       
   117 
       
   118     // complete client request
       
   119     Dot11History().AddDefaultBroadcastWepKeyComplete( aCtxImpl );
       
   120     }
       
   121 
       
   122 #ifndef NDEBUG 
       
   123 // -----------------------------------------------------------------------------
       
   124 // 
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 const TInt8* WlanAddBroadcastWepKey::GetStateName( 
       
   128     TUint8& aLength ) const
       
   129     {
       
   130     aLength = sizeof( iName );
       
   131     return iName;
       
   132     }
       
   133 #endif
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // 
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void WlanAddBroadcastWepKey::ChangeInternalState( 
       
   140     WlanContextImpl& aCtxImpl, 
       
   141     TState aNewState )
       
   142     {
       
   143     iState = aNewState;
       
   144     Fsm( aCtxImpl, ESTATEENTRY );
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // 
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void WlanAddBroadcastWepKey::Fsm( 
       
   152     WlanContextImpl& aCtxImpl, 
       
   153     TEvent aEvent )
       
   154     {
       
   155     OsTracePrint( KUmacDetails, 
       
   156         (TUint8*)("UMAC: WlanAddBroadcastWepKey::Fsm: FSM EVENT") );
       
   157 #ifndef NDEBUG
       
   158     OsTracePrint( KUmacDetails, (TUint8*)("UMAC: event:"));
       
   159     OsTracePrint( KUmacDetails, iEventName[aEvent] );
       
   160     OsTracePrint( KUmacDetails, (TUint8*)("UMAC: state:"));
       
   161     OsTracePrint( KUmacDetails, iStateName[iState] );
       
   162 #endif // !NDEBUG
       
   163 
       
   164     switch ( aEvent )
       
   165         {
       
   166         case ESTATEENTRY:
       
   167             OnStateEntryEvent( aCtxImpl );
       
   168             break;
       
   169         case ETXCOMPLETE:
       
   170             OnTxCompleteEvent( aCtxImpl );
       
   171             break;
       
   172         case EABORT:
       
   173             OnAbortEvent( aCtxImpl );
       
   174             break;
       
   175         default:
       
   176             // catch internal FSM programming error
       
   177 #ifndef NDEBUG
       
   178             OsTracePrint( KErrorLevel, (TUint8*)("UMAC: event:"));
       
   179             OsTracePrint( KErrorLevel, iEventName[aEvent] );                
       
   180 #endif // !NDEBUG
       
   181             OsAssert( (TUint8*)("UMAC: panic"), 
       
   182                 (TUint8*)(WLAN_FILE), __LINE__ );
       
   183             break;
       
   184         }
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // 
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void WlanAddBroadcastWepKey::OnStateEntryEvent( 
       
   192     WlanContextImpl& aCtxImpl )
       
   193     {
       
   194     const TUint KAllocLen( 100 ); 
       
   195 
       
   196     switch ( iState )
       
   197         {
       
   198         case EINIT:
       
   199             iMemory = os_alloc( KAllocLen );
       
   200             if ( iMemory )
       
   201                 {
       
   202                 // start the FSM traversal
       
   203                 ChangeInternalState( aCtxImpl, 
       
   204                     EADDGROUPKEY );            
       
   205                 }
       
   206             else
       
   207                 {
       
   208                 // allocation failure
       
   209                 Fsm( aCtxImpl, EABORT );
       
   210                 }
       
   211             break;
       
   212         case EADDGROUPKEY:
       
   213             AddGroupKey( aCtxImpl );
       
   214             break;
       
   215         case EWRITEMIB:
       
   216             WriteMib( aCtxImpl );
       
   217             break;
       
   218         case EADDPAIRWISEKEY:
       
   219             AddPairwiseKey( aCtxImpl );
       
   220             break;
       
   221         case EFINIT:
       
   222             // fsm execution complete traverse back to history state
       
   223             // after signalling completion in the Exit method
       
   224             TraverseToHistoryState( aCtxImpl );
       
   225             break;
       
   226         default:
       
   227             // catch internal FSM programming error
       
   228 #ifndef NDEBUG
       
   229             OsTracePrint( KErrorLevel, (TUint8*)("UMAC: state:"));
       
   230             OsTracePrint( KErrorLevel, iStateName[iState] );
       
   231 #endif // !NDEBUG
       
   232             OsAssert( (TUint8*)("UMAC: panic"), 
       
   233                 (TUint8*)(WLAN_FILE), __LINE__ );
       
   234             break;
       
   235         }
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // 
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void WlanAddBroadcastWepKey::OnTxCompleteEvent( 
       
   243     WlanContextImpl& aCtxImpl )
       
   244     {
       
   245     switch ( iState )
       
   246         {
       
   247         case EADDGROUPKEY:
       
   248             if ( UseAsDefaultKey() )
       
   249                 {
       
   250                 ChangeInternalState( aCtxImpl, EWRITEMIB );
       
   251                 }
       
   252             else if ( UseAsPairwiseKey() )
       
   253                 {
       
   254                 ChangeInternalState( aCtxImpl, EADDPAIRWISEKEY );
       
   255                 }
       
   256             else
       
   257                 {
       
   258                 // not to be used as default key or PTK
       
   259                 // we can end this fsm
       
   260                 ChangeInternalState( aCtxImpl, EFINIT );
       
   261                 }
       
   262             break;
       
   263         case EWRITEMIB:
       
   264             if ( UseAsPairwiseKey() )
       
   265                 {
       
   266                 ChangeInternalState( aCtxImpl, EADDPAIRWISEKEY );
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 // not to be used as PTK
       
   271                 // we can end this fsm
       
   272                 ChangeInternalState( aCtxImpl, EFINIT );
       
   273                 }
       
   274             break;
       
   275         case EADDPAIRWISEKEY:
       
   276             ChangeInternalState( aCtxImpl, EFINIT );
       
   277             break;
       
   278         default:
       
   279             // catch internal FSM programming error
       
   280 #ifndef NDEBUG
       
   281             OsTracePrint( KErrorLevel, (TUint8*)("UMAC: state:"));
       
   282             OsTracePrint( KErrorLevel, iStateName[iState] );
       
   283 #endif // !NDEBUG
       
   284             OsAssert( (TUint8*)("UMAC: panic"), 
       
   285                 (TUint8*)(WLAN_FILE), __LINE__ );
       
   286             break;
       
   287         }
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // WlanAddBroadcastWepKey::OnAbortEvent
       
   292 // simulate macnotresponding error
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void WlanAddBroadcastWepKey::OnAbortEvent( 
       
   296     WlanContextImpl& aCtxImpl )
       
   297     {
       
   298     OsTracePrint( KWarningLevel, 
       
   299         (TUint8*)("UMAC: WlanAddBroadcastWepKey::OnAbortEvent") );
       
   300 
       
   301     DoErrorIndication( aCtxImpl, WHA::KErrorMacNotResponding );
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // 
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void WlanAddBroadcastWepKey::AddGroupKey( 
       
   309     WlanContextImpl& aCtxImpl )
       
   310     {
       
   311     // store info of default WEP GTK insertion
       
   312     aCtxImpl.GroupKeyType( WHA::EWepGroupKey );
       
   313 
       
   314     WHA::SWepGroupKey* key_cnxt = static_cast<WHA::SWepGroupKey*>(iMemory);
       
   315 
       
   316     key_cnxt->iKeyId = static_cast<WHA::TPrivacyKeyId>(iKeyIndex);
       
   317     key_cnxt->iKeyLengthInBytes = iKeyLength; 
       
   318     os_memcpy( key_cnxt->iKey, iKey, key_cnxt->iKeyLengthInBytes );
       
   319 
       
   320     WlanWsaAddKey& wha_cmd( aCtxImpl.WsaAddKey() );
       
   321     wha_cmd.Set( aCtxImpl, 
       
   322             WHA::EWepGroupKey,
       
   323             key_cnxt,
       
   324             WlanWsaKeyIndexMapper::Extract( 
       
   325             WHA::EWepGroupKey, key_cnxt->iKeyId ) 
       
   326             );
       
   327     
       
   328     // change global state: entry procedure triggers action
       
   329     ChangeState( aCtxImpl, 
       
   330         *this,              // prev state
       
   331         wha_cmd             // next state
       
   332         );                           
       
   333     }
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // 
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 void WlanAddBroadcastWepKey::AddPairwiseKey( 
       
   340     WlanContextImpl& aCtxImpl )
       
   341     {
       
   342     WHA::SWepPairwiseKey* key_cnxt( 
       
   343         static_cast<WHA::SWepPairwiseKey*>(iMemory) );
       
   344 
       
   345     os_memcpy( 
       
   346         &(key_cnxt->iMacAddr), 
       
   347         iMacAddr.iMacAddress,
       
   348         sizeof(key_cnxt->iMacAddr) );
       
   349     key_cnxt->iKeyLengthInBytes = iKeyLength; 
       
   350     os_memcpy( key_cnxt->iKey, iKey, key_cnxt->iKeyLengthInBytes );
       
   351     
       
   352     WlanWsaAddKey& wha_cmd( aCtxImpl.WsaAddKey() );    
       
   353     wha_cmd.Set( aCtxImpl, 
       
   354         WHA::EWepPairWiseKey,
       
   355         key_cnxt,
       
   356         WlanWsaKeyIndexMapper::Extract( WHA::EWepPairWiseKey ) );
       
   357 
       
   358     // change global state: entry procedure triggers action
       
   359     ChangeState( aCtxImpl, 
       
   360         *this,              // prev state
       
   361         wha_cmd             // next state
       
   362         );                           
       
   363     }
       
   364 
       
   365 // -----------------------------------------------------------------------------
       
   366 // 
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 void WlanAddBroadcastWepKey::WriteMib( 
       
   370     WlanContextImpl& aCtxImpl )
       
   371     {        
       
   372     // allocate memory for the mib to write
       
   373     WHA::Sdot11WepDefaultKeyId* mib( 
       
   374         static_cast<WHA::Sdot11WepDefaultKeyId*>(iMemory) ); 
       
   375    
       
   376     mib->iDot11WepDefaultKeyId = static_cast<WHA::TPrivacyKeyId>(iKeyIndex);
       
   377         
       
   378     WlanWsaWriteMib& wha_cmd = aCtxImpl.WsaWriteMib();
       
   379         
       
   380     wha_cmd.Set( 
       
   381         aCtxImpl, WHA::KMibDot11WepDefaultKeyId, sizeof(*mib), mib );
       
   382         
       
   383     // change global state: entry procedure triggers action
       
   384     ChangeState( aCtxImpl, 
       
   385         *this,              // prev state
       
   386         wha_cmd             // next state
       
   387         );       
       
   388     }