vpnengine/sit/src/vpnconnstarter.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003-2009 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 "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:   VPN connection starter
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <commsdat.h>
       
    20 #include <cmmanagerext.h>
       
    21 
       
    22 #if defined(__PFX_MIP4__)
       
    23 #include <FeatMgr.h>
       
    24 #endif
       
    25 
       
    26 #include "vpnconnstarter.h"
       
    27 #include "ikepolparser.h"
       
    28 #include "ipsecpolparser.h"
       
    29 #include "log.h"
       
    30 
       
    31 // Task handling states
       
    32 enum TConnStarterState
       
    33     {
       
    34     KStateStartRealIap,
       
    35     KStateRealIapConnected,
       
    36     KStateGetVpnPolicy,
       
    37     KStateActivateKmd,
       
    38     KStateAfterActivateKmd,
       
    39     KStateLoadIpsecPolicy,
       
    40     KStateAfterLoadIpsecPolicy,
       
    41     KStateActivateIpsecPolicy,
       
    42     KStateAfterActivateIpsecPolicy
       
    43     };
       
    44 
       
    45 
       
    46 CVpnConnStarter* CVpnConnStarter::NewL(MTaskHandlerManager* aManager,
       
    47                                        const TTaskArrivedEventData& aTaskInfo)
       
    48     {
       
    49     LOG(Log::Printf(_L("CVpnConnStarter::NewL - begin\n")));
       
    50     CVpnConnStarter* self = new (ELeave) CVpnConnStarter(aManager, aTaskInfo);
       
    51     CleanupStack::PushL(self);
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop(); // self
       
    54     LOG(Log::Printf(_L("CVpnConnStarter::NewL - end\n")));
       
    55     return self;
       
    56     }
       
    57 
       
    58 CVpnConnStarter::CVpnConnStarter(MTaskHandlerManager* aManager,
       
    59                                  const TTaskArrivedEventData& aTaskInfo)
       
    60     : CTaskHandler(aManager, aTaskInfo, EStartVpnConnCancelEvent, &iEventSpecDes),
       
    61       iVpnAddressInfoDes(iVpnAddressInfo),
       
    62       iIfInfoDes(iIfInfo)
       
    63     {
       
    64     iIfInfo.iRealIapId = 0;
       
    65     iIfInfo.iRealNetId = 0;
       
    66     }
       
    67 
       
    68 void CVpnConnStarter::ConstructL()
       
    69     {
       
    70     CActiveScheduler::Add(this);
       
    71     User::LeaveIfError(iEventMediator.Connect());
       
    72     User::LeaveIfError(iVpnServ.Connect());
       
    73     User::LeaveIfError(iKmdServ.Connect());
       
    74     User::LeaveIfError(iPolicyServ.Connect());
       
    75 #if defined(__PFX_MIP4__)
       
    76     FeatureManager::InitializeLibL();
       
    77 #endif
       
    78     }
       
    79     
       
    80 CVpnConnStarter::~CVpnConnStarter()
       
    81     {
       
    82     LOG(Log::Printf(_L("CVpnConnStarter::~CVpnConnStarter\n")));
       
    83     Cancel();
       
    84     iEventMediator.Close();
       
    85     iVpnServ.Close();
       
    86     iKmdServ.Close();
       
    87     iPolicyServ.Close();
       
    88     
       
    89     delete iIkePolicyData;
       
    90     delete iIpsecPolicyData;
       
    91     delete iCustomTs;
       
    92     
       
    93     RELEASE_EVENT_LOGGER;
       
    94 #if defined(__PFX_MIP4__)    
       
    95     FeatureManager::UnInitializeLib();  
       
    96 #endif    
       
    97     }
       
    98 
       
    99 void CVpnConnStarter::StartTaskHandling()
       
   100     {
       
   101     // Read input parameters. 
       
   102     // Changing the content of iEventSpecDes is not allowed
       
   103     iVpnIapId = iEventSpecDes().iIfInfo.iVpnIapId;
       
   104     GotoState(KStateStartRealIap);
       
   105     }
       
   106 
       
   107 void CVpnConnStarter::ChangeStateL()
       
   108     {
       
   109     switch (NextState())
       
   110         {
       
   111         case KStateStartRealIap:
       
   112             StateStartRealIap();
       
   113             break;
       
   114 
       
   115         case KStateRealIapConnected:
       
   116             StateRealIapConnectedL();
       
   117             break;            
       
   118 
       
   119         case KStateGetVpnPolicy:
       
   120             StateGetVpnPolicyL();
       
   121             break;
       
   122 
       
   123         case KStateLoadIpsecPolicy:
       
   124             StateLoadIpsecPolicy();
       
   125             break;
       
   126 
       
   127         case KStateAfterLoadIpsecPolicy:
       
   128             StateAfterLoadIpsecPolicyL();
       
   129             break;
       
   130 
       
   131         case KStateActivateIpsecPolicy:
       
   132             StateActivateIpsecPolicy();
       
   133             break;
       
   134 
       
   135         case KStateAfterActivateIpsecPolicy:
       
   136             StateAfterActivateIpsecPolicyL();
       
   137             break;
       
   138 
       
   139         case KStateActivateKmd:
       
   140             StateActivateKmdL();
       
   141             break;
       
   142 
       
   143         case KStateAfterActivateKmd:
       
   144             StateAfterActivateKmdL();
       
   145             break;
       
   146 
       
   147         default:
       
   148             User::Panic(KSitName, EPanicInvalidTaskHandlerState);
       
   149         }    
       
   150     }
       
   151 
       
   152 void CVpnConnStarter::CancelOngoingOperation()
       
   153     {
       
   154     LOG_EVENT(R_VPN_MSG_VPN_IAP_ACT_CANCEL, NULL, 0, 0);
       
   155     
       
   156     switch (CurrState())
       
   157         {
       
   158         case KStateStartRealIap:
       
   159             DEB(LOG(Log::Printf(_L("Canceling start real connection\n") ));)             
       
   160             iKmdServ.CancelStartRealConnection();            
       
   161             break;
       
   162         case KStateRealIapConnected:
       
   163         case KStateGetVpnPolicy:
       
   164         case KStateAfterActivateKmd:
       
   165         case KStateAfterLoadIpsecPolicy:
       
   166         case KStateAfterActivateIpsecPolicy:
       
   167             // There's no ongoing external request to cancel
       
   168             break;
       
   169             
       
   170         case KStateActivateKmd:
       
   171             DEB(LOG(Log::Printf(_L("Canceling KMD activate\n") ));)             
       
   172             iKmdServ.CancelActivate();
       
   173             break;
       
   174 
       
   175         case KStateLoadIpsecPolicy:
       
   176             DEB(LOG(Log::Printf(_L("Canceling Ipsec policy load\n") ));)
       
   177             iPolicyServ.CancelLoad();
       
   178             break;
       
   179 
       
   180         case KStateActivateIpsecPolicy:
       
   181             DEB(LOG(Log::Printf(_L("Canceling Ipsec policy activate\n") ));)
       
   182             iPolicyServ.CancelActivate();
       
   183             break;
       
   184 
       
   185         default:
       
   186             User::Panic(KSitName, EPanicInvalidTaskHandlerState);
       
   187         }
       
   188 
       
   189     RollbackProcess();
       
   190     }
       
   191     
       
   192 void CVpnConnStarter::StateStartRealIap()
       
   193     {       
       
   194     SetCurrState(KStateStartRealIap);
       
   195     
       
   196     DEB(LOG(Log::Printf(_L("StateStartRealIap, VPN IAP Id:%d\n"), 
       
   197         iVpnIapId));)                           
       
   198     iKmdServ.StartRealConnection(iVpnIapId, iIfInfoDes, iStatus);
       
   199     SetNextState(KStateRealIapConnected);
       
   200     SetActive();
       
   201     }
       
   202 
       
   203 void CVpnConnStarter::StateRealIapConnectedL()
       
   204     {
       
   205     SetCurrState(KStateRealIapConnected);
       
   206     DEB(LOG(Log::Printf(_L("StateRealIapConnected:%d\n"), iStatus.Int()));)
       
   207     User::LeaveIfError( iStatus.Int() );
       
   208     INIT_EVENT_LOGGER(iEventMediator, 
       
   209     iVpnIapId, iIfInfoDes().iRealIapId);
       
   210     LOG_EVENT(R_VPN_MSG_VPN_IAP_ACT_START, this, 0, 0);    
       
   211     GotoState(KStateGetVpnPolicy);    
       
   212     }
       
   213       
       
   214 void CVpnConnStarter::StateGetVpnPolicyL()
       
   215     {
       
   216     SetCurrState(KStateGetVpnPolicy);
       
   217     
       
   218     HBufC8* vpnPolicyData = NULL;
       
   219     DEB(LOG(Log::Printf(_L("Starting to get VPN policy: %S\n"), 
       
   220         &(iEventSpecDes().iPolicyId) ));)   
       
   221     // Get VPN policy data
       
   222     User::LeaveIfError(
       
   223         iVpnServ.GetPolicyData(iEventSpecDes().iPolicyId, vpnPolicyData));
       
   224     CleanupStack::PushL(vpnPolicyData);
       
   225 
       
   226     // Convert to 16 bit
       
   227     HBufC16* vpnPolicyData16 = CSit::To16BitL(*vpnPolicyData);
       
   228     CleanupStack::PushL(vpnPolicyData16);
       
   229 
       
   230     // Buffer containing Nokia NAT-T specific traffic selectors
       
   231     // if specified in IKE policy
       
   232     delete iCustomTs;
       
   233     iCustomTs = NULL;
       
   234     iCustomTs = HBufC8::NewL(256);
       
   235     
       
   236     // Extract IKE and IPSec policies from the VPN policy
       
   237     // Also stores custom Traffic Selectors if needed
       
   238     GetIkePolicyFromVpnPolicyL(*vpnPolicyData16);
       
   239     DEB(LOG(Log::Printf(_L("IKE policy parsing succeeded\n")));)
       
   240             
       
   241     GetIpsecPolicyFromVpnPolicyL(*vpnPolicyData16);
       
   242     DEB(LOG(Log::Printf(_L("Ipsec policy parsing succeeded\n")));)
       
   243     
       
   244     // Insert stored traffic selectors to IPsec policy
       
   245     InsertCustomTrafficSelectorsL();
       
   246             
       
   247     // Custom traffic selectors are no longer needed since
       
   248     // they were given to IPsec in above
       
   249     delete iCustomTs;
       
   250     iCustomTs = NULL;
       
   251     
       
   252     CleanupStack::PopAndDestroy(2); // vpnPolicyData16, vpnPolicyData
       
   253     
       
   254     GotoState(KStateLoadIpsecPolicy);   
       
   255     }
       
   256             
       
   257 void CVpnConnStarter::StateActivateKmdL()
       
   258     {
       
   259     SetCurrState(KStateActivateKmd);
       
   260 
       
   261     DEB(LOG(Log::Printf(_L("Starting KMD activate\n")));)                           
       
   262     iKmdServ.Activate(iVpnIapId,
       
   263                       iEventSpecDes().iIfInfo.iVPNIfName,
       
   264                       *iIkePolicyData,
       
   265                       iVpnAddressInfoDes,
       
   266                       iStatus);
       
   267     
       
   268     SetNextState(KStateAfterActivateKmd);
       
   269     SetActive();
       
   270     }
       
   271 
       
   272 void CVpnConnStarter::StateAfterActivateKmdL()
       
   273     {
       
   274     SetCurrState(KStateAfterActivateKmd);
       
   275 
       
   276     DEB(LOG(Log::Printf(
       
   277         _L("KMD activate completed, status = %d, VPN IAP Id = %d\n"), 
       
   278         iStatus.Int(), iVpnIapId));)
       
   279     // Leave if KMD activation failed
       
   280     User::LeaveIfError(iStatus.Int());
       
   281 
       
   282     LOG_EVENT(R_VPN_MSG_VPN_IAP_ACTIVATED, 
       
   283         &(iVpnAddressInfo.iVPNIfAddr), iStatus.Int(), 0);
       
   284     
       
   285     TaskComplete(KErrNone);
       
   286     }
       
   287     
       
   288 void CVpnConnStarter::StateLoadIpsecPolicy()
       
   289     {
       
   290     SetCurrState(KStateLoadIpsecPolicy);
       
   291 
       
   292     DEB(LOG(Log::Printf(_L("Starting to load Ipsec policy\n")));)       
       
   293     LoadIpsecPolicy();
       
   294     
       
   295     SetNextState(KStateAfterLoadIpsecPolicy);
       
   296     SetActive();
       
   297     }
       
   298 
       
   299 void CVpnConnStarter::StateAfterLoadIpsecPolicyL()
       
   300     {
       
   301     SetCurrState(KStateAfterLoadIpsecPolicy);
       
   302     DEB(LOG(Log::Printf(
       
   303         _L("Ipsec policy load completed, status =  %d\n"), iStatus.Int()));)
       
   304     // Log some debug data if policy loading failed
       
   305     if (iStatus.Int() == ESelectorConflict)
       
   306         {
       
   307         DEB(TPolicyNameInfo conflictInfo;)
       
   308         DEB(TInt err = iPolicyServ.GetDebugInfo(conflictInfo, KConflictingPolicyInfo));
       
   309 
       
   310         DEB(if (!err))
       
   311             DEB(LOG(Log::Printf(_L("Additional debug info: %S\n"), &conflictInfo));)
       
   312         }
       
   313 
       
   314     if (iStatus.Int() == EParsingError)
       
   315         {
       
   316         DEB(typedef TBuf<200> TParsingErrorInfo;)
       
   317         DEB(TParsingErrorInfo errorInfo;)
       
   318         DEB(TInt err = iPolicyServ.GetDebugInfo(errorInfo, KParsingErrorInfo));
       
   319 
       
   320         DEB(if (!err))
       
   321             DEB(LOG(Log::Printf(_L("Additional debug info: %S\n"), &errorInfo));)
       
   322         }
       
   323 
       
   324     // Leave if policy loading failed
       
   325     User::LeaveIfError(iStatus.Int());
       
   326     
       
   327     GotoState(KStateActivateIpsecPolicy);
       
   328     }
       
   329     
       
   330 void CVpnConnStarter::StateActivateIpsecPolicy()
       
   331     {
       
   332     SetCurrState(KStateActivateIpsecPolicy);
       
   333 
       
   334     DEB(LOG(Log::Printf(_L("Activating Ipsec policy, handle = %d\n"), iIpsecPolicyHandleDes()));)           
       
   335     iPolicyServ.ActivatePolicy(iIpsecPolicyHandleDes(), iStatus);
       
   336 
       
   337     SetNextState(KStateAfterActivateIpsecPolicy);
       
   338     SetActive();
       
   339     }
       
   340 
       
   341 void CVpnConnStarter::StateAfterActivateIpsecPolicyL()
       
   342     {
       
   343     DEB(LOG(Log::Printf(_L("Ipsec policy activated, status = %d\n"), iStatus.Int()));)
       
   344 
       
   345     User::LeaveIfError(iStatus.Int());
       
   346 
       
   347     GotoState(KStateActivateKmd);
       
   348     }
       
   349 
       
   350 void CVpnConnStarter::ReportResult(TInt aStatus)
       
   351     {
       
   352     DEB(LOG(Log::Printf(_L("CVpnConnStarter::ReportResult\n")));)
       
   353     // Report the startup result with associated
       
   354     // information to the Event Mediator
       
   355     TStartVpnConnEventData eventData;
       
   356 
       
   357     eventData.iTaskStatus = aStatus;
       
   358 
       
   359     if (aStatus == KErrNone)
       
   360         {
       
   361         eventData.iIkePolicyHandle.iHandle = iVpnIapId;
       
   362         eventData.iIpsecPolicyHandle = iIpsecPolicyHandleDes();
       
   363         eventData.iVpnAddressInfo = iVpnAddressInfo; 
       
   364         eventData.iIapId = iIfInfo.iRealIapId;
       
   365         eventData.iNetId = iIfInfo.iRealNetId;
       
   366         DEB(LOG(Log::Printf(_L("Iap:%d Net:%d\n"), 
       
   367             eventData.iIapId, eventData.iNetId));) 
       
   368         }
       
   369     
       
   370     TPckg<TStartVpnConnEventData> eventDataDes(eventData);
       
   371 
       
   372     ReportEvent(EStartVpnConnEvent, iEventSpecDes, eventDataDes);
       
   373 
       
   374     LOG_EVENT(R_VPN_MSG_VPN_IAP_ACT_END, 
       
   375         &(iEventSpecDes().iPolicyId), aStatus, 0);
       
   376     }
       
   377 
       
   378 TInt CVpnConnStarter::RunError(TInt aError)
       
   379     {
       
   380     LOG_EVENT(R_VPN_MSG_VPN_IAP_ACT_FAILED, NULL, aError, 0);
       
   381             
       
   382     RollbackProcess();
       
   383     TaskComplete(aError);
       
   384     return KErrNone;
       
   385     }
       
   386 
       
   387 void CVpnConnStarter::RollbackProcess()
       
   388     {
       
   389     // If the VPN connection activation process
       
   390     // fails at some point, we may need to reverse
       
   391     // some of the actions that we've done:
       
   392     // - If the KMD has been activated (the IKE policy has been loaded),
       
   393     //   deactivate KMD (unload IKE policy)
       
   394     // - If the IPSec policy has been loaded and activated,
       
   395     //   unload the policy
       
   396     // No actions are needed regarding the real IAP connection
       
   397     // as it will be closed by the system in the lack of users.
       
   398 
       
   399     DEB(LOG(Log::Printf(_L("Deactivating IKE policy due VPN activation error, VPN IAP Id =  %d\n"),iVpnIapId));)
       
   400     iKmdServ.StopVpnConnection(iVpnIapId, TKmdStopConnection::ENormal);
       
   401 
       
   402     if (iIpsecPolicyHandleDes().iHandle != 0)
       
   403         {
       
   404         DEB(LOG(Log::Printf(_L("Unloading Ipsec policy due VPN activation error, VPN IAP Id =  %d\n"),iVpnIapId));)            
       
   405         TRequestStatus status;
       
   406         iPolicyServ.UnloadPolicy(iIpsecPolicyHandleDes(), status);
       
   407         User::WaitForRequest(status);
       
   408         }
       
   409     }
       
   410 
       
   411 void CVpnConnStarter::ResolveIpsecFQDNL(CSecPolBundleList* aSecPolBundleList)
       
   412     {
       
   413     if (aSecPolBundleList)
       
   414         {
       
   415         for (TInt i(0); i<aSecPolBundleList->Count(); ++i)
       
   416             {
       
   417             CSecpolBundleItem* item = aSecPolBundleList->At(i);
       
   418             HBufC* dnsname = item->iTunnelEpFQDN;
       
   419             
       
   420             TInetAddr result = ResolveFQDNL(*dnsname);
       
   421             // 39 for ipv6
       
   422             TBuf<39> ipAddress;
       
   423             result.Output( ipAddress );       
       
   424             item->iTunnel.Input(ipAddress);
       
   425             DEB(LOG(Log::Printf(_L("Resolved:%S\n"), &ipAddress));)                         
       
   426             }
       
   427         }
       
   428     }
       
   429 
       
   430 void CVpnConnStarter::ResolveIkeFQDNL(CIkeData* aIkeData)
       
   431     {
       
   432     ASSERT(aIkeData);
       
   433     if (aIkeData->iFQDNAddr)
       
   434         {
       
   435         TInetAddr result = ResolveFQDNL(aIkeData->iFQDNAddr->GetData());
       
   436         // 39 for ipv6
       
   437         TBuf<39> ipAddress;
       
   438         result.Output( ipAddress );       
       
   439         aIkeData->iAddr.Input(ipAddress);
       
   440         DEB(LOG(Log::Printf(_L("Resolved:%S\n"), &ipAddress));) 
       
   441         }
       
   442     }
       
   443 
       
   444 TInetAddr CVpnConnStarter::ResolveFQDNL(const TDesC& aAddress)
       
   445     {
       
   446     DEB(LOG(Log::Printf(_L("ResolveFQDNL:%S\n"), &aAddress));)     
       
   447     TRequestStatus status;
       
   448     TNameEntry result;
       
   449     iKmdServ.ResolveAddress(iVpnIapId, aAddress, result, status);
       
   450     User::WaitForRequest(status);
       
   451     User::LeaveIfError(status.Int());
       
   452     TInetAddr address(result().iAddr);
       
   453     return address;    
       
   454     }
       
   455 
       
   456 void CVpnConnStarter::FormCustomTrafficSelectorsL(CIkeData *aIkeData)
       
   457 	{
       
   458 	// Add Nokia NAT-T specific traffic selectors to IPsec
       
   459     // custom traffic selector buffer
       
   460     if (aIkeData->iUseNatProbing)
       
   461     	{
       
   462     	// Default port for Nokia NAT-T
       
   463     	TInt port(KNokiaNattDefaultPort);
       
   464     	if (aIkeData->iEspUdpPort)
       
   465     		port = aIkeData->iEspUdpPort;
       
   466     		
       
   467     	TBuf8<39> addr, mask;
       
   468     	TBuf<39> buffer;
       
   469     	aIkeData->iAddr.Output(buffer);
       
   470     	addr.Copy(buffer);
       
   471     	aIkeData->iMask.Output(buffer);
       
   472     	mask.Copy(buffer);
       
   473     	
       
   474     	HBufC8 *tsBuffer = HBufC8::NewLC(128);
       
   475     	tsBuffer->Des().Format(_L8("remote %S %S local_port %d = { }\n"),
       
   476     			&addr, &mask, port);
       
   477     	DEB(LOG(Log::Printf(_L8("Custom TS:%S\n"), tsBuffer));) 
       
   478     	// Make sure that TS fits in the ts buffer
       
   479     	if (iCustomTs->Des().MaxLength() < iCustomTs->Length() + tsBuffer->Length())
       
   480     		{
       
   481     		iCustomTs = iCustomTs->ReAllocL(iCustomTs->Length() + tsBuffer->Length());
       
   482     		}
       
   483     	iCustomTs->Des().Append(*tsBuffer);
       
   484     	CleanupStack::PopAndDestroy(tsBuffer);
       
   485     	}
       
   486 	}
       
   487 
       
   488 void CVpnConnStarter::InsertCustomTrafficSelectorsL()
       
   489 	{
       
   490 	_LIT8(KSearchTerm, "[POLICY]\n");
       
   491 	TInt pos = iIpsecPolicyData->Find(KSearchTerm);
       
   492 	if (pos != KErrNotFound)
       
   493 		{
       
   494 		HBufC8 *newIpsecPolicyData = HBufC8::NewL(
       
   495 				iIpsecPolicyData->Length() + iCustomTs->Length());
       
   496 		pos += KSearchTerm().Length();
       
   497 		
       
   498 		// Add data to "[POLICY]\n"
       
   499 		newIpsecPolicyData->Des().Append(iIpsecPolicyData->Des().Left(pos));
       
   500 		
       
   501 		// Add custom traffic selectors
       
   502 		newIpsecPolicyData->Des().Append(*iCustomTs);
       
   503 		
       
   504 		// Add rest of the original data
       
   505 		newIpsecPolicyData->Des().Append(iIpsecPolicyData->Des().Right(
       
   506 				iIpsecPolicyData->Length() - pos));
       
   507 		
       
   508 		delete iIpsecPolicyData;
       
   509 		iIpsecPolicyData = newIpsecPolicyData;
       
   510 		
       
   511 		DEB(LOG(Log::Write(*iIpsecPolicyData)));
       
   512 		}
       
   513 	}
       
   514 
       
   515 void CVpnConnStarter::GetIkePolicyFromVpnPolicyL(const TDesC& aVpnPolicy)
       
   516     {
       
   517     CIkeDataArray* ikeList = CIkeDataArray::NewL(1);
       
   518     CleanupStack::PushL(ikeList);
       
   519     
       
   520     TIkeParser ikeParser(aVpnPolicy);
       
   521     ikeParser.ParseIKESectionsL(ikeList);
       
   522 
       
   523     iIkePolicyData = HBufC8::NewL(KIkePolicyBufferSizeIncrement);
       
   524     const TInt maxRetrans(8);
       
   525     const TInt maxTraceFileSize(16);
       
   526     User::LeaveIfError(ikeParser.GeneralInfoWrite(ikeList->iMaxLifetimeSec, 
       
   527                                                   ikeList->iMaxLifetimeKB,
       
   528                                                   maxRetrans,
       
   529                                                   maxTraceFileSize, 
       
   530                                                   iIkePolicyData));
       
   531 
       
   532     for (TInt i(0); i < ikeList->Count(); i++)
       
   533         {
       
   534         CIkeData* ikeData = ikeList->At(i);
       
   535         ResolveIkeFQDNL(ikeData);
       
   536         FormCustomTrafficSelectorsL(ikeData);
       
   537         User::LeaveIfError(ikeParser.MainWrite(ikeData, iIkePolicyData));
       
   538         }
       
   539 
       
   540     CleanupStack::PopAndDestroy(ikeList);
       
   541     }
       
   542 
       
   543 void CVpnConnStarter::GetIpsecPolicyFromVpnPolicyL(const TDesC& aVpnPolicy)
       
   544     {
       
   545     CIpSecurityPiece* pieceData  = new (ELeave) CIpSecurityPiece;
       
   546     CleanupStack::PushL(pieceData);
       
   547     pieceData->ConstructL();
       
   548 
       
   549     TIpSecParser parser(aVpnPolicy);
       
   550 
       
   551     TInt ret = parser.ParseAndIgnoreIKEL(pieceData);
       
   552     
       
   553     if (ret != KErrNone)
       
   554         {
       
   555         HBufC* errorInfo = HBufC16::NewL(200);
       
   556         errorInfo->Des().Copy(pieceData->iErrorInfo);
       
   557         LOG(TPtr ptr = errorInfo->Des(); Log::Printf(_L("Parsing error info: %S\n"), &ptr));
       
   558         delete errorInfo;
       
   559         CleanupStack::PopAndDestroy(); // pieceData
       
   560         User::Leave(ret);
       
   561         }
       
   562 
       
   563     ResolveIpsecFQDNL(pieceData->FQDNAddressListL());
       
   564     
       
   565     iIpsecPolicyData = HBufC8::NewL(KPolicyBufferSizeIncrement);
       
   566     ret = parser.Write(pieceData, iIpsecPolicyData);
       
   567     
       
   568     if (ret != KErrNone)
       
   569        {
       
   570         LOG(Log::Printf(_L("Ipsec policy write error: %d\n"), ret));        
       
   571         CleanupStack::PopAndDestroy(); // pieceData
       
   572         User::Leave(ret);
       
   573        }
       
   574 
       
   575     CleanupStack::PopAndDestroy(); // pieceData
       
   576     }
       
   577 
       
   578 void CVpnConnStarter::LoadIpsecPolicy()
       
   579     {
       
   580     // Specify network IDs for tunnel end-point and selector definitions
       
   581     iZoneInfoSetDes().iSelectorZone.iScope = KScopeNetwork;
       
   582     iZoneInfoSetDes().iSelectorZone.iId = iEventSpecDes().iIfInfo.iVpnNetId;
       
   583     iZoneInfoSetDes().iEndPointZone.iScope = KScopeNetwork;
       
   584     iZoneInfoSetDes().iEndPointZone.iId = iIfInfo.iRealNetId;
       
   585     
       
   586     LOG(Log::Printf(_L("CVpnConnStarter::LoadIpsecPolicy - VPN NET ID=%d, Real NET ID=%d\n"),
       
   587             iEventSpecDes().iIfInfo.iVpnNetId, iIfInfo.iRealNetId ));
       
   588 
       
   589 #if defined(__PFX_MIP4__)
       
   590 
       
   591     if (IsMip4FeaturePresent())
       
   592         {
       
   593         LOG(Log::Printf(_L("CVpnConnStarter::LoadIpsecPolicy - MIPv4 present, adding MIPv4 bypass selectors\n")));
       
   594         iProcessingFlags = KAddIkeBypassSelectors | KAddMip4BypassSelectors;
       
   595         }
       
   596     else
       
   597         {
       
   598         LOG(Log::Printf(_L("CVpnConnStarter::LoadIpsecPolicy - MIPv4 NOT present, NOT adding MIPv4 bypass selectors\n")));
       
   599         iProcessingFlags = KAddIkeBypassSelectors;
       
   600         }
       
   601     
       
   602 #else
       
   603     
       
   604     LOG(Log::Printf(_L("CVpnConnStarter::LoadIpsecPolicy - MIPv4 NOT present, NOT adding MIPv4 bypass selectors\n")));
       
   605     iProcessingFlags = KAddIkeBypassSelectors;
       
   606     
       
   607 #endif // __PFX_MIP4__
       
   608 
       
   609     // Check from commdb wether "real" interface uses DHCP
       
   610     // and if it does, KAddDhcpBypassSelectors is added to
       
   611     // the processing flags 
       
   612     
       
   613     TBool dhcpEnabled = EFalse;
       
   614     TRAPD(err, IsDhcpEnabledL(dhcpEnabled));
       
   615     
       
   616     if (!err && dhcpEnabled)
       
   617         {
       
   618         iProcessingFlags |= KAddDhcpBypassSelectors;
       
   619         }
       
   620     
       
   621     iPolicyServ.LoadPolicy(*iIpsecPolicyData, iIpsecPolicyHandleDes,
       
   622                            iStatus, iZoneInfoSetDes, iProcessingFlags);
       
   623     }
       
   624 
       
   625 void CVpnConnStarter::IsDhcpEnabledL(TBool& aDhcpEnabled)
       
   626     {
       
   627     using namespace CMManager;
       
   628     
       
   629     RCmManagerExt cmManagerExt;
       
   630     cmManagerExt.OpenL();
       
   631     CleanupClosePushL(cmManagerExt);
       
   632     
       
   633     RCmConnectionMethodExt connectionMethod = cmManagerExt.ConnectionMethodL( iIfInfo.iRealIapId );
       
   634     CleanupClosePushL(connectionMethod);
       
   635     
       
   636     aDhcpEnabled = connectionMethod.GetBoolAttributeL(ECmIPAddFromServer);
       
   637 	if (!aDhcpEnabled)
       
   638 	    {
       
   639         aDhcpEnabled = connectionMethod.GetBoolAttributeL(ECmIPDNSAddrFromServer);
       
   640 		}
       
   641 
       
   642     CleanupStack::PopAndDestroy(2);
       
   643     }
       
   644 
       
   645 #if defined(__PFX_MIP4__)
       
   646     
       
   647 TBool CVpnConnStarter::IsMip4FeaturePresent(void)
       
   648     {
       
   649     return FeatureManager::FeatureSupported(KFeatureIdMIPv4);
       
   650     }
       
   651 
       
   652 #endif // __PFX_MIP4__