telephonyprotocols/rawipnif/src/IPv6Binder.cpp
changeset 0 3553901f7fa8
child 5 7ef16719d8cb
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This file implements the CIPv6Binder class, which handles the transmission
       
    15 // of IPv6 data to and from the TCP/IP stack.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include <etelpckt.h>
       
    24 #include <in_iface.h>
       
    25 #include "RawIPFlow.h"
       
    26 #include "IPv6Binder.h"
       
    27 #include <comms-infras/linkprovision.h>
       
    28 
       
    29 using namespace ESock;
       
    30 #ifdef WCDMA_STUB
       
    31 #include <networking/umtsnifcontrolif.h>
       
    32 #endif
       
    33 
       
    34 #define LOG_IP_ADDRESS(desc,addr) _LOG_L2C5(_L8("    " desc " = %d:%d:%d:%d from context"), \
       
    35 			addr.u.iAddr32[3], addr.u.iAddr32[2], addr.u.iAddr32[1], addr.u.iAddr32[0]);
       
    36 
       
    37 CIPv6Binder::CIPv6Binder(CRawIPFlow& aFlow, CBttLogger* aTheLogger)
       
    38 /**
       
    39  * Constructor
       
    40  */ 
       
    41 	: CBinderBase(aFlow,aTheLogger),
       
    42 	  iTheLogger(aTheLogger),
       
    43 	  iSpeedMetric(KDefaultSpeedMetric)
       
    44 	{
       
    45 	}
       
    46 
       
    47 CIPv6Binder::~CIPv6Binder()
       
    48 /**
       
    49  *	Destructor
       
    50  */
       
    51 	{
       
    52 	}
       
    53 
       
    54 MLowerDataSender* CIPv6Binder::Bind(MUpperDataReceiver* aUpperReceiver, MUpperControl* aUpperControl)
       
    55 /**
       
    56  * Binds TCP/IP protocol to Flow
       
    57  *
       
    58  * @param aUpperReceiver A pointer to Upper layer Receive class
       
    59  * @param aUpperControl A pointer to Upper layer control class
       
    60  */
       
    61 	{
       
    62 	CBinderBase::Bind(aUpperReceiver, aUpperControl); // Call the superclass's method.
       
    63 	return this;
       
    64 	}
       
    65 
       
    66 TInt CIPv6Binder::Control(TUint aLevel, TUint aName, TDes8& /*aOption*/)
       
    67 /**
       
    68  * The main function called by the TCP/IP protocol to control the interface.
       
    69  * Can perform a variety of general IP tasks (such as getting IP config)
       
    70  * and "3G" specific tasks (such as deleting the context).
       
    71  *
       
    72  * @param aLevel The level of the interface to control - always KSOLInterface
       
    73  * @param aName The command to perform
       
    74  * @param aOption Data to be input/output as a result of the command
       
    75  * @return Standard error codes
       
    76  */
       
    77 	{
       
    78 	_LOG_L1C3(_L8("CIPv6Binder::Control [aLevel=%d, aName=%d]"),
       
    79 		aLevel, aName);
       
    80 
       
    81 	if (aLevel == KSOLInterface)
       
    82 		{
       
    83 		switch (aName)
       
    84 			{
       
    85 		case KSoIfHardwareAddr:
       
    86 			// unsupported because we don't have a h/w address
       
    87 			break;
       
    88 
       
    89 		// 3G-specific configuration commands are below this point.
       
    90 #ifdef WCDMA_STUB
       
    91 		case KRegisterEventHandler:
       
    92 			// Raw IP NIF Events are not supported
       
    93 		case KContextSetEvents:
       
    94 			// Raw IP NIF Events are not supported
       
    95 			break;
       
    96 
       
    97 		case KContextCreate:
       
    98 			// We don't support creating new secondary contexts.
       
    99 			break;
       
   100 
       
   101 		case KContextDelete:
       
   102 			// Deletes the primary PDP context. This will shut down the Nif.
       
   103 			return DeleteContext(aOption);
       
   104 
       
   105 		case KContextActivate:
       
   106 			// If the IPv6 interface is up, then the context will already have
       
   107 			// been activated. So this command should fail with 
       
   108 			// KErrAlreadyExists
       
   109 			{
       
   110 			TUint8* ptr = CONST_CAST(TUint8*, aOption.Ptr()); 
       
   111 			TContextParameters* contextParams =
       
   112 				REINTERPRET_CAST(TContextParameters*, ptr);
       
   113 
       
   114 			if (contextParams->iContextInfo.iContextId != 
       
   115 				STATIC_CAST(TInt8, GetFlow().GetBcaController()->Nsapi()))
       
   116 				{
       
   117 				contextParams->iReasonCode = KErrNotFound;
       
   118 				}
       
   119 			else
       
   120 				{
       
   121 				contextParams->iContextInfo.iStatus =
       
   122 					GetFlow().GetContextStatus();
       
   123 				contextParams->iReasonCode = KErrAlreadyExists;
       
   124 				}
       
   125 			return KErrNone;
       
   126 			}
       
   127 
       
   128 		case KNifSetDefaultQoS:
       
   129 		case KContextQoSSet:
       
   130 			// Setting the QoS is meaningless over GPRS, so we just return that
       
   131 			// we don't support these operations.
       
   132 			break;
       
   133 
       
   134 		case KContextTFTModify:
       
   135 			// As we only have one primary context, we don't support anything
       
   136 			// to do with traffic flow templates, which are used by secondary
       
   137 			// contexts.
       
   138 			break;
       
   139 
       
   140 		case KContextModifyActive:
       
   141 			// This command is only valid aftermodifying TFT/QoS parameters. 
       
   142 			// As we don't support any of these operations,
       
   143 			// this command is never valid.
       
   144 			break;
       
   145 #endif
       
   146 		default:
       
   147 			break;
       
   148 			}
       
   149 		}
       
   150 	return KErrNotSupported;
       
   151 	}
       
   152 
       
   153 TInt CIPv6Binder::GetConfig(TBinderConfig& aConfig)
       
   154 	{
       
   155     TBinderConfig6* config = TBinderConfig::Cast<TBinderConfig6>(aConfig);
       
   156     
       
   157    	if(config == NULL)
       
   158    		{
       
   159    		return KErrNotSupported;
       
   160    		}
       
   161 	
       
   162 	config->iFamily = KAfInet6;		/* KAfInet6 - selects TBinderConfig6 */
       
   163 
       
   164 	config->iInfo.iFeatures = KIfCanBroadcast | KIfCanMulticast;		/* Feature flags */
       
   165 	config->iInfo.iMtu = KDefaultMtu;				/* Maximum transmission unit. */
       
   166 	config->iInfo.iRMtu = KDefaultMtu;				/* Maximum transmission unit for receiving. */
       
   167 	config->iInfo.iSpeedMetric = iSpeedMetric;		/* approximation of the interface speed in Kbps. */
       
   168 	
       
   169 	TEui64Addr& localId = TEui64Addr::Cast(config->iLocalId);
       
   170 	localId = iSettings.iLocalIfId;
       
   171 	config->iNameSer1.SetAddress(iSettings.iPrimaryDns);		/* IP primary name server (if any). */
       
   172 	config->iNameSer2.SetAddress(iSettings.iSecondaryDns);	/* IP secondary name server (if any). */
       
   173 
       
   174 	return KErrNone;
       
   175 	}
       
   176 #ifdef WCDMA_STUB
       
   177 
       
   178 TInt CIPv6Binder::DeleteContext(TDes8& aContextParameters)
       
   179 /**
       
   180  * Deletes a context. As the NIF is responsible for one primary context,
       
   181  * this is equivalent to closing down the NIF.
       
   182  *
       
   183  * @param aContextParameters Parameters of the context to delete
       
   184  * @return KErrArgument if an incorrect structure is passed, otherwise KErrNone
       
   185  */
       
   186 	{
       
   187 	_LOG_L1C1(_L8("CIPv6Binder::DeleteContext"));
       
   188 
       
   189 	if (aContextParameters.Length() != sizeof(TContextParameters))
       
   190 		{
       
   191 		return KErrArgument;
       
   192 		}
       
   193 
       
   194 	TUint8* ptr = CONST_CAST(TUint8*, aContextParameters.Ptr());
       
   195 	TContextParameters* params = REINTERPRET_CAST(TContextParameters*, ptr);
       
   196 
       
   197 	if (params->iContextInfo.iContextId != 
       
   198 		STATIC_CAST(TInt8, GetFlow().GetBcaController()->Nsapi()))
       
   199 		{
       
   200 		params->iReasonCode = KErrBadName;
       
   201 		}
       
   202 	else
       
   203 		{
       
   204 		params->iReasonCode = KErrNone; 
       
   205 		GetFlow().Stop(KErrNone, MNifIfNotify::EDisconnect);
       
   206 		}
       
   207 
       
   208 	return KErrNone;
       
   209 	}
       
   210 
       
   211 #endif
       
   212 
       
   213 /**
       
   214  * Called when the context has been activated to set our IP address and get
       
   215  * any other required settings from CommDB.
       
   216  *
       
   217  * @param aConfig The new context config
       
   218  */
       
   219  void CIPv6Binder::UpdateContextConfigL(const TPacketDataConfigBase& aConfig)
       
   220 	{
       
   221 	_LOG_L1C1(_L8("CIPv6Binder::UpdateContextConfig"));
       
   222 
       
   223 	// Get our IP address from the GPRS context config.
       
   224 	TInetAddr address;
       
   225 	
       
   226 	TBuf<RPacketContext::KMaxPDPAddressLength> tempAddr;
       
   227 	
       
   228 	const RPacketContext::TProtocolConfigOptionV2* pco;
       
   229 	TInt rel = const_cast<TPacketDataConfigBase&>(aConfig).ExtensionId();
       
   230 	if (rel == TPacketDataConfigBase::KConfigGPRS) 
       
   231 	    {
       
   232 	    tempAddr.Copy(static_cast<const RPacketContext::TContextConfigGPRS&>(aConfig).iPdpAddress);
       
   233 	    pco = &static_cast<const RPacketContext::TContextConfigGPRS&>(aConfig).iProtocolConfigOption;
       
   234 	    }
       
   235     else
       
   236         {
       
   237         ASSERT(rel == TPacketDataConfigBase::KConfigRel99Rel4 || rel == TPacketDataConfigBase::KConfigRel5);
       
   238 	    tempAddr.Copy(static_cast<const RPacketContext::TContextConfigR99_R4&>(aConfig).iPdpAddress);
       
   239 	    pco = &static_cast<const RPacketContext::TContextConfigR99_R4&>(aConfig).iProtocolConfigOption;
       
   240         }
       
   241 	TInt ret = address.Input(tempAddr);
       
   242 
       
   243 	// We've got our IP address! Let's save it.
       
   244 	if (ret == KErrNone)
       
   245 		{
       
   246 		const TUint8* addrTable = &address.Ip6Address().u.iAddr8[8];
       
   247 		
       
   248 		iSettings.iLocalIfId.SetAddr(addrTable, 8);		
       
   249 		
       
   250 		LOG_IP_ADDRESS("Got local IP address", address.Ip6Address());
       
   251 		}
       
   252 	else
       
   253 		{
       
   254 		_LOG_L2C2(_L8("Couldn't get IP address from GPRS config (err: %d)"),
       
   255 			ret);
       
   256 
       
   257 		// Don't leave on this error: we may still be OK if we read some
       
   258 		// settings from CommDB.
       
   259 		}
       
   260 
       
   261 	// @todo - is this correct. We can only get the DNS addresses
       
   262 	// from the TSY using the iProtocolConfigOption data. Yet a client could
       
   263 	// access those DNS config details without knowing about the state of the
       
   264 	// iSettings.iGetDnsFromServer flag.
       
   265 
       
   266 	if ((iSettings.iGetDnsFromServer) ||
       
   267 		((iSettings.iPrimaryDns.IsUnspecified()) &&
       
   268 		 (iSettings.iSecondaryDns.IsUnspecified())) ) 
       
   269 		{
       
   270 		TBuf<RPacketContext::KMaxPDPAddressLength> tempAddr;
       
   271 		tempAddr.Copy(pco->iDnsAddresses.iPrimaryDns);
       
   272 		ret = address.Input(tempAddr);
       
   273 
       
   274 		if (ret == KErrNone)
       
   275 			{
       
   276 			iSettings.iPrimaryDns = address.Ip6Address();
       
   277 			LOG_IP_ADDRESS("Got primary DNS", iSettings.iPrimaryDns);
       
   278 			}
       
   279 		else
       
   280 			{
       
   281 			_LOG_L2C2(_L8("Couldn't get primary DNS address from GPRS config (err: %d)"),
       
   282 				ret);
       
   283 
       
   284 			// Don't leave on this error: we may still be OK if we read some
       
   285 			// settings from CommDB.
       
   286 			}
       
   287 
       
   288 		tempAddr.Copy(pco->iDnsAddresses.iSecondaryDns);
       
   289 		ret = address.Input(tempAddr);
       
   290 
       
   291 		if (ret == KErrNone)
       
   292 			{
       
   293 			iSettings.iSecondaryDns = address.Ip6Address();
       
   294 			LOG_IP_ADDRESS("Got secondary DNS", iSettings.iPrimaryDns);
       
   295 			}
       
   296 		else
       
   297 			{
       
   298 			_LOG_L2C2(_L8("Couldn't get secondary DNS address from GPRS config (err: %d)"),
       
   299 				ret);
       
   300 
       
   301 			// Don't leave on this error: we may still be OK if we read some
       
   302 			// settings from CommDB.
       
   303 			}
       
   304 		}
       
   305 	else
       
   306 		{
       
   307 		LOG_IP_ADDRESS("Using CommDB DNS address - Primary ", iSettings.iPrimaryDns);
       
   308 		LOG_IP_ADDRESS("                         - Secondary ", iSettings.iSecondaryDns);
       
   309 		}
       
   310 	}
       
   311 
       
   312 void CIPv6Binder::UpdateConnectionSpeed(TUint aConnectionSpeed)
       
   313 /**
       
   314  * Sets the speed metric to return to TCP/IP, based on what the TSY tells us.
       
   315  *
       
   316  * @param aConnectionSpeed Our connection speed
       
   317  */
       
   318 	{
       
   319 	_LOG_L1C1(_L8("CIPv6Binder::UpdateConnectionSpeed"));
       
   320 
       
   321 	iSpeedMetric = aConnectionSpeed;
       
   322 	}
       
   323 
       
   324 
       
   325 MLowerDataSender::TSendResult CIPv6Binder::Send(RMBufChain& aPdu)
       
   326 /**
       
   327  * Called by the protocol to send an outgoing IP packet to the network.
       
   328  *
       
   329  * @param aPdu The outgoing packet
       
   330  * @return Standard error codes
       
   331  */
       
   332 	{
       
   333 	_LOG_L1C1(_L8("CIPv6Binder::Send"));
       
   334 
       
   335 #ifdef __BTT_LOGGING__
       
   336 	LogPacket(aPdu);
       
   337 #endif
       
   338 
       
   339 	// Return <0: an error occurred
       
   340 	// Return  0: no error, but don't send any more packets
       
   341 
       
   342 	return static_cast<MLowerDataSender::TSendResult>(GetFlow().SendPacket(aPdu, NULL, KIp4FrameType));
       
   343 	}
       
   344 
       
   345 TInt CIPv6Binder::Notification(TAgentToNifEventType /*aEvent*/, 
       
   346 	void* /*aInfo*/)
       
   347 /**
       
   348  * The Nif will ignore any notification sent
       
   349  *
       
   350  * @param aEvent Not used
       
   351  * @param aInfo Not used 
       
   352  */
       
   353 	{
       
   354 	_LOG_L1C1(_L8("CIPv6Binder::Notification"));
       
   355 
       
   356 	return KErrNone;
       
   357 	}
       
   358 
       
   359 void CIPv6Binder::StartSending()
       
   360 /**
       
   361  * Indicates to the protocol layer that the NIF is ready to send packets.
       
   362  *
       
   363  * @param aProtocol A pointer to a protocol
       
   364  */
       
   365 	{
       
   366 	_LOG_L1C1(_L8("CIPv6Binder::StartSending()"));
       
   367 
       
   368 	CBinderBase::StartSending();
       
   369 	}
       
   370 
       
   371 TBool CIPv6Binder::WantsProtocol(TUint16 aProtocolCode)
       
   372 /**
       
   373  * Indicates the type of protocol implemented by this class.
       
   374  *
       
   375  * @param aProtocolCode The protocol type
       
   376  */
       
   377 	{
       
   378 	_LOG_L1C2(_L8("CIPv6Binder::WantsProtocol [aProtocolCode=%X]"),
       
   379 		aProtocolCode);
       
   380 
       
   381 #ifdef RAWIP_HEADER_APPENDED_TO_PACKETS
       
   382 	return ((aProtocolCode & 0x00FF) == KIp6FrameType);
       
   383 #else
       
   384 	(void) aProtocolCode;
       
   385 	return ETrue;
       
   386 #endif // RAWIP_HEADER_APPENDED_TO_PACKETS
       
   387 	}
       
   388 
       
   389 void CIPv6Binder::Process(RMBufChain& aPdu)
       
   390 /**
       
   391  * Called when an incoming IP packet has arrived. Send packets up to the
       
   392  * TCP/IP stack.
       
   393  *
       
   394  * @param aPdu The incoming packet
       
   395  */
       
   396 	{
       
   397 	_LOG_L1C1(_L8("CIPv6Binder::Process"));
       
   398 
       
   399 #ifdef __BTT_LOGGING__
       
   400 	LogPacket(aPdu);
       
   401 #endif
       
   402 
       
   403 	// Pass incoming packets up to the protocol, unless it hasn't
       
   404 	// been bound yet.
       
   405 	if (iUpperReceiver)						// ASSERT(iUpperReceiver) ?
       
   406 		{
       
   407 		_LOG_L1C1(_L8("CIPv6Binder: Packet Sent to TCP/IP Protocol!!!"));
       
   408 		iUpperReceiver->Process(aPdu);
       
   409 		}
       
   410 	else 
       
   411 		{
       
   412 		_LOG_L2C1(_L8("WARNING: dumping incoming packet, no protocol bound"));
       
   413 		aPdu.Free();
       
   414 		}
       
   415 	}
       
   416 
       
   417 //
       
   418 // MLowerControl methods
       
   419 //
       
   420 
       
   421 TInt CIPv6Binder::GetName(TDes& aName)
       
   422 /**
       
   423 */
       
   424 	{
       
   425 	WriteIfName(aName);
       
   426 	return KErrNone;
       
   427 	}
       
   428 
       
   429 //
       
   430 // CBinderBase methods
       
   431 //
       
   432 
       
   433 void CIPv6Binder::SetProvision(const CIPConfig& aProvision)
       
   434 /**
       
   435 Set provisioning information for IPv6 binder.
       
   436 
       
   437 Called from RawIP Flow.
       
   438 
       
   439 @param aProvision Provisioning structure from Control side.
       
   440 */
       
   441 	{
       
   442 	iSettings.iPrimaryDns = aProvision.GetIp6NameServer1();
       
   443 	iSettings.iSecondaryDns	= aProvision.GetIp6NameServer2();
       
   444 	iSettings.iGetDnsFromServer = aProvision.GetIp6DNSAddrFromServer();
       
   445 
       
   446 	// Read whether to get IPv4 address from the server
       
   447 	// This is only needed for the integration tests. If it's true then 
       
   448 	// the IPv4 address will be used to build up the IPv6 address. 
       
   449 	iSettings.iGetIpFromServer = aProvision.GetIpAddrFromServer();
       
   450 	
       
   451 	if (iSettings.iGetIpFromServer == EFalse)
       
   452 		{
       
   453 		// Sets the IPv6 Link-local address from IpAddr.
       
   454 		// LocalId is derived from IpAddr and it's further used to set the Link-local
       
   455 		// address elsewhere by adding a prefix (FE80::) in front.
       
   456 		// For IpAddr: 192.168.1.1, link-local address will be FE80::C0A8:101.		
       
   457 		TUint32 ipAddr = aProvision.GetIpAddress();
       
   458 		const TUint8 constantId[8] = { 0, 0, 0, 0, 
       
   459 									ipAddr >> 24, (ipAddr >> 16) & 0xFF, 
       
   460 									(ipAddr >> 8) & 0xFF, ipAddr & 0xFF }; 
       
   461 		iSettings.iLocalIfId.SetAddr(constantId, sizeof (constantId));
       
   462 		}
       
   463 	else
       
   464 		{	
       
   465 		//
       
   466 		// Use the 64 bit id of MARM machines as our interface id
       
   467 		//
       
   468 		TMachineInfoV1Buf machineInfo;	
       
   469 		UserHal::MachineInfo(machineInfo);
       
   470 		iSettings.iLocalIfId.SetAddr(machineInfo().iMachineUniqueId);
       
   471 		iSettings.iLocalIfId.SetUniversalBit(0);
       
   472 		//
       
   473 		// In WINS environment the id is zero which is no-no
       
   474 		//
       
   475 		if (iSettings.iLocalIfId.IsZero())
       
   476 			{
       
   477 				iSettings.iLocalIfId.SetAddrRandomNZ();
       
   478 			}		
       
   479 		}
       
   480 	}
       
   481 
       
   482 #ifdef __BTT_LOGGING__
       
   483 void CIPv6Binder::LogPacket(const RMBufChain& aPacket)
       
   484 /**
       
   485 * Logs packet information into log file.
       
   486 *
       
   487 * @param aPacket The packet 
       
   488 */
       
   489 	{
       
   490 	_LOG_L1C1(_L8("CIPv6Binder::LogPacket"));
       
   491 
       
   492 	TInt mBufLength = aPacket.Length() - aPacket.First()->Length();
       
   493 
       
   494 	_LOG_L3C2(_L8("Analysis of %d byte packet:"), mBufLength);
       
   495 
       
   496 	//Note: All the constants used on this method are a pragmatic guess of the
       
   497 	//IP header fields. The only porpose of this method is logging.
       
   498 
       
   499 	if (mBufLength < 40)
       
   500 		{
       
   501 		_LOG_L3C2(_L8(" -doesn't appear to be a valid IPv6 packet (length=%d)")
       
   502 			, mBufLength);
       
   503 		return;
       
   504 		}
       
   505 
       
   506 	// Get a pointer to the packet's payload.
       
   507 	const TUint8* payloadPtr = aPacket.First()->Next()->Ptr();
       
   508 
       
   509 	if ((payloadPtr[0] & 0xF0) != 0x60)
       
   510 		{
       
   511 		_LOG_L3C2(_L8(" - doesn't appear to be an IPv6 packet (version=0x%X)"),
       
   512 			(payloadPtr[0] & 0xF0) >> 4);
       
   513 		return;
       
   514 		}
       
   515 
       
   516 	_LOG_L3C2(_L8(" - traffic class: 0x%X"), 
       
   517 					((payloadPtr[0] & 0xF) << 4) | ((payloadPtr[1] & 0xF0) >> 4));
       
   518 	_LOG_L3C2(_L8(" - flow label: 0x%X"), 
       
   519 					((payloadPtr[1] & 0x0F) << 16) | (payloadPtr[2] << 8) | payloadPtr[3]);
       
   520 	_LOG_L3C2(_L8(" - payload length: 0x%X"), 
       
   521 					(payloadPtr[4] << 16) | payloadPtr[5]);
       
   522 	_LOG_L3C2(_L8(" - next header: 0x%08X"), payloadPtr[6]);
       
   523 	_LOG_L3C2(_L8(" - hop limit: 0x%08X"), payloadPtr[7]);
       
   524 	}
       
   525 #endif // __BTT_LOGGING__