wlan_bearer/wlannwif/src/CLanIp4Bearer.cpp
changeset 0 c40eb8fe8501
child 3 6524e815f76f
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Implements IPv4 bearer
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: 12 %
       
    20 */
       
    21 
       
    22 #include <in_sock.h> // Header is retained, but in_sock.h is modified for ipv6
       
    23 #include <in_pkt.h>
       
    24 #include <in6_if.h>
       
    25 #include <nifvar.h>
       
    26 #include <comms-infras/connectionsettings.h>
       
    27 #include "CLanIp4Bearer.h"
       
    28 #include "arp_hdr.h"
       
    29 #include <commdb.h>
       
    30 #include <cdblen.h>
       
    31 #include "WlanProto.h"
       
    32 #include <comms-infras/es_protbinder.h>
       
    33 #include "WlanProvision.h"
       
    34 
       
    35 // From Wlan
       
    36 #include "am_debug.h"
       
    37 #include "rwlmserver.h"
       
    38 
       
    39 // Avoiding a warning in armv5 urel
       
    40 #ifdef _DEBUG
       
    41 _LIT(KCLanIp4BearerName,"EthWlan4");
       
    42 #endif /* _DEBUG */
       
    43 
       
    44 using namespace ESock;
       
    45 
       
    46 /**
       
    47 Required ConstructL() method.  Performs the following:
       
    48 "	Sets a unique name for this binder instance in the iIfName field - "wlan[0x%x]" where "%x"
       
    49     is the hexadecimal address of the "this" object.
       
    50 "	Reads the IP Interface Settings using ReadIPInterfaceSettingsL().
       
    51 */
       
    52 void CLanIp4Bearer::ConstructL()
       
    53 {
       
    54     DEBUG("CLanIp4Bearer::ConstructL()");
       
    55  
       
    56 	iIfName.Format(_L("wlan[0x%08x]"), this);
       
    57 	iBroadcastConfigured = EFalse;
       
    58 	iArpMsgNext = EFalse;
       
    59 	// ReadCommDbLanSettingsL() moved from here to point when provisioning information received
       
    60 
       
    61 }
       
    62 
       
    63 TInt CLanIp4Bearer::Control(TUint aLevel, TUint aName, TDes8& aOption)
       
    64 	{
       
    65     DEBUG("CLanIp4Bearer::Control()");
       
    66 	
       
    67 	if ((aLevel==KCOLInterface) || (aLevel==KSOLInterface))
       
    68 		{
       
    69 		switch (aName)
       
    70 			{
       
    71 			case KSoIfHardwareAddr:
       
    72 				{
       
    73 			    typedef TPckgBuf<TSoIfHardwareAddr> THwAddr;
       
    74 			    THwAddr& theHwAddrRef = static_cast<THwAddr&>(aOption);
       
    75 			    theHwAddrRef().iHardwareAddr.SetFamily(1);
       
    76 			    theHwAddrRef().iHardwareAddr.SetPort(KArpHarwareType_ETHERNET);
       
    77 			    theHwAddrRef().iHardwareAddr.SetLength(sizeof(SSockAddr));
       
    78 			    theHwAddrRef().iHardwareAddr.Append(Link()->MacAddress());
       
    79 			    return KErrNone;
       
    80 	         	}
       
    81 		default:
       
    82 			break;
       
    83 			}
       
    84 		}
       
    85 	
       
    86 	else if (aLevel==KSolInetIp)
       
    87 		{
       
    88 		switch (aName)
       
    89 			{
       
    90 			case KSoIp6LeaveGroup:
       
    91 			case KSoIp6JoinGroup:
       
    92 			    {
       
    93 			    			    
       
    94 			    // Convert multicast IPv4 address to multicast MAC address
       
    95 			    TIp6Mreq& opt = *(TIp6Mreq*)aOption.Ptr();
       
    96 			    TUint8* tmp = opt.iAddr.u.iAddr8;
       
    97 			    TMacAddress mac;
       
    98 			    mac.iMacAddress[0] = 0x01;
       
    99 			    mac.iMacAddress[1] = 0x00;
       
   100 			    mac.iMacAddress[2] = 0x5E;
       
   101 			    mac.iMacAddress[3] = tmp[13];
       
   102 			    mac.iMacAddress[3] &= 0x7F; // Most significant bit is set zero
       
   103 			    mac.iMacAddress[4] = tmp[14];
       
   104 			    mac.iMacAddress[5] = tmp[15];  
       
   105 			    
       
   106 			    TIp6Addr addr6 = opt.iAddr;
       
   107 				TInetAddr addr(addr6, 0);
       
   108 				TBuf<39> aa;
       
   109 				addr.Output(aa);
       
   110 			    			    
       
   111 			    if(aName == KSoIp6JoinGroup)
       
   112 	        		DEBUG1("CLanIp4Bearer::Control() - joining multicast address:%S", &aa);
       
   113 			    else
       
   114 	        		DEBUG1("CLanIp4Bearer::Control() - leaving multicast address:%S", &aa);
       
   115 			    
       
   116 			    TInt ret = KErrNone;
       
   117 			    RWLMServer wlmServer;
       
   118 			    ret = wlmServer.Connect();
       
   119 			    if( ret )
       
   120 			        {
       
   121 	            	DEBUG1("CLanIp4Bearer::Control()- ERROR connecting RWlmServer: %d", ret);
       
   122 	            	return ret;
       
   123 			        }
       
   124 			    else
       
   125 			    	{
       
   126 			    	ret = wlmServer.ConfigureMulticast( aName, mac );
       
   127 				    wlmServer.Close();
       
   128 				    return KErrNone;
       
   129 					}
       
   130 			    }
       
   131 			default:
       
   132 				break;
       
   133 			}
       
   134 		}
       
   135 	
       
   136 	return KErrNotSupported;
       
   137 	}
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // CLanIp4Bearer::Send
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 MLowerDataSender::TSendResult CLanIp4Bearer::Send(RMBufChain& aPdu)
       
   144 	{
       
   145 	DEBUG("CLanIp4Bearer::Send()");
       
   146 	// Call down from the Protocol
       
   147 	// Real Protocol data is in the second MBuf of the RMBufChain
       
   148 
       
   149 	RMBufPktInfo* info = RMBufPacket::PeekInfoInChain(aPdu);
       
   150 	TUint32 family = info->iDstAddr.Family();
       
   151 	__ASSERT_DEBUG(family!=KAfInet6, User::Panic(KCLanIp4BearerName, 0));
       
   152 
       
   153 	TBuf8<KMACByteLength> destAddr;
       
   154 	const TUint8 etherBroadcastAddr[KMACByteLength] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
       
   155 
       
   156 	if(family == KAfInet)
       
   157 		{
       
   158 		// Only broadcast and multicast packets come here with address family KAfInet
       
   159 		TInetAddr dest(info->iDstAddr);
       
   160 		if( dest.IsMulticast() )
       
   161 			{
       
   162 			// multicast
       
   163 			const TUint8 IpV4MulticastPrefix[KMACByteLength / 2] = {0x01, 0x00, 0x5e};
       
   164 			destAddr.Copy(IpV4MulticastPrefix, KMACByteLength / 2);
       
   165 
       
   166 			TUint32 mCastAdrrEnd = dest.Address() & 0x007fffff;
       
   167 			BigEndian::Put32(reinterpret_cast<TUint8*>(&mCastAdrrEnd), mCastAdrrEnd);
       
   168 			destAddr.Append(reinterpret_cast<TUint8*>(&mCastAdrrEnd) + 1, KMACByteLength / 2);
       
   169 			}
       
   170         else
       
   171             {
       
   172             // broadcast
       
   173             destAddr.Copy(etherBroadcastAddr, KMACByteLength);
       
   174             }
       
   175 		}
       
   176 	else if(family == 1)
       
   177 		{
       
   178 		// Address family == 1, ethernet address is found from info
       
   179 		destAddr.Copy(info->iDstAddr.Mid(sizeof(SSockAddr), KMACByteLength));
       
   180 		}
       
   181 	else
       
   182 		{
       
   183 		DEBUG1( "CLanIp4Bearer::Send() - unsupported family type: %u",
       
   184 		    family );
       
   185 
       
   186 		aPdu.Free();
       
   187 		return static_cast<MLowerDataSender::TSendResult>(1);
       
   188 		}
       
   189 
       
   190 	TInt ret = Link()->FrameSend(aPdu, destAddr, (TUint)info->iProtocol == KProtocolArp ? KArpFrameType : KIPFrameType);
       
   191 
       
   192 	return static_cast<MLowerDataSender::TSendResult>(ret);
       
   193 }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CLanIp4Bearer::StartSending
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CLanIp4Bearer::StartSending(CProtocolBase* aProtocol)
       
   200 {
       
   201 	DEBUG("CLanIp4Bearer::StartSending()");
       
   202 
       
   203 	CLanxBearer::StartSending(aProtocol);
       
   204 	
       
   205 	if (!iBroadcastConfigured)
       
   206 		{
       
   207 		TMacAddress broadcastmac;
       
   208 	    broadcastmac.iMacAddress[0] = 0xFF;
       
   209 	    broadcastmac.iMacAddress[1] = 0xFF;
       
   210 	    broadcastmac.iMacAddress[2] = 0xFF;
       
   211 	    broadcastmac.iMacAddress[3] = 0xFF;
       
   212 	    broadcastmac.iMacAddress[4] = 0xFF;
       
   213 	    broadcastmac.iMacAddress[5] = 0xFF; 
       
   214 	    
       
   215 	    RWLMServer wlmServer;
       
   216 	    if (wlmServer.Connect() != KErrNone)
       
   217 	    	{
       
   218 	    	DEBUG("CLanIp4Bearer::StartSending(), ERROR connecting to wlan engine");
       
   219 	    	}
       
   220 	    else
       
   221 	    	{
       
   222 	    	DEBUG("CLanIp4Bearer::StartSending(), Configuring umac to receive Broadcast frames");
       
   223 	    	if( wlmServer.ConfigureMulticast( KSoIp6JoinGroup, broadcastmac ) )
       
   224 	    		{
       
   225 	    		DEBUG("CLanIp4Bearer::StartSending(),ERROR calling ConfigureMulticast");
       
   226 	    		}
       
   227 	    	wlmServer.Close();
       
   228 	    	iBroadcastConfigured = ETrue;
       
   229 	    	}	    
       
   230 		}
       
   231 }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CLanIp4Bearer::WantsProtocol
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 TBool CLanIp4Bearer::WantsProtocol(TUint16 aProtocolCode,const TUint8* aPayload)
       
   238 {
       
   239 	DEBUG("CLanIp4Bearer::WantsProtocol()");
       
   240 
       
   241     iArpMsgNext = (aProtocolCode == KArpFrameType);
       
   242 	return ((aProtocolCode==KIPProtocol && (aPayload[0]>>4)==KIP4Protocol) || (aProtocolCode==KArpFrameType))  ? ETrue:EFalse;
       
   243 }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CLanIp4Bearer::Process
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 void CLanIp4Bearer::Process(RMBufChain& aPdu)
       
   250 {
       
   251 	DEBUG("CLanIp4Bearer::Process()");
       
   252 	
       
   253 	if (iUpperReceiver)
       
   254 		{
       
   255 		RMBufPktInfo* info = RMBufPacket::PeekInfoInChain(aPdu);
       
   256 		if (info)
       
   257 			{
       
   258 			info->iProtocol = iArpMsgNext ? KProtocolArp : KProtocolInetIp;
       
   259 			}
       
   260 		iUpperReceiver->Process(aPdu);
       
   261 		}
       
   262 	else
       
   263 		{
       
   264 	  	aPdu.Free();
       
   265 	  	}
       
   266 }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CLanIp4Bearer::ReadCommDbLanSettingsL
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CLanIp4Bearer::ReadCommDbLanSettingsL()
       
   273 {
       
   274 	DEBUG("CLanIp4Bearer::ReadCommDbLanSettingsL()");
       
   275 
       
   276 	ASSERT(iProvision);
       
   277 	
       
   278 	iLocalAddr = iProvision->LocalAddr();
       
   279 	iNetMask = iProvision->NetMask();
       
   280 	iDefGateway = iProvision->DefGateway();
       
   281 	iPrimaryDns = iProvision->PrimaryDns();
       
   282 	iSecondaryDns = iProvision->SecondaryDns();
       
   283 }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CLanIp4Bearer::GetConfig
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TInt CLanIp4Bearer::GetConfig(TBinderConfig& aConfig)
       
   290 	{
       
   291 	DEBUG("CLanIp4Bearer::GetConfig()");
       
   292 	
       
   293 	TBinderConfig4& config = static_cast<TBinderConfig4&>(aConfig);
       
   294 	
       
   295 	config.iFamily = KAfInet;		/* KAfInet / KAfInet6 - selects TBinderConfig4/6 */
       
   296 
       
   297 	config.iInfo.iFeatures = KIfCanBroadcast | KIfCanMulticast | KIfNeedsND;		/* Feature flags */
       
   298 	config.iInfo.iMtu = Link()->Mtu();					/* Maximum transmission unit. */
       
   299 	config.iInfo.iRMtu = Link()->Mtu();					/* Maximum transmission unit for receiving. */
       
   300 	config.iInfo.iSpeedMetric = Link()->SpeedMetric();	/* approximation of the interface speed in Kbps. */
       
   301 	
       
   302 	config.iAddress.SetAddress(iLocalAddr);			/* Interface IP address. */
       
   303 	config.iNetMask.SetAddress(iNetMask);			/* IP netmask. */
       
   304 	config.iBrdAddr.SetAddress(iBroadcastAddr);		/* IP broadcast address. */
       
   305 	config.iDefGate.SetAddress(iDefGateway);		/* IP default gateway or peer address (if known). */
       
   306 	config.iNameSer1.SetAddress(iPrimaryDns);		/* IP primary name server (if any). */
       
   307 	config.iNameSer2.SetAddress(iSecondaryDns);		/* IP secondary name server (if any). */
       
   308 
       
   309 	return KErrNone;
       
   310 	}
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CLanIp4Bearer::ProtocolName
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 const TDesC8& CLanIp4Bearer::ProtocolName() const
       
   317 	{
       
   318 	DEBUG("CLanIp4Bearer::ProtocolName()");
       
   319 	
       
   320 	_LIT8(KProtocolName, "ip");
       
   321 	return KProtocolName();
       
   322 	}
       
   323 	
       
   324 // -----------------------------------------------------------------------------
       
   325 // CLanIp4Bearer::SetProvisionL
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CLanIp4Bearer::SetProvisionL(const Meta::SMetaData* aProvision)
       
   329 	{
       
   330 	DEBUG("CLanIp4Bearer::SetProvisionL()");
       
   331 	
       
   332 	if (iProvision == NULL)
       
   333 		{
       
   334 		iProvision = static_cast<const TLanIp4Provision*>(aProvision);
       
   335 		ReadCommDbLanSettingsL();
       
   336 		}
       
   337 	}
       
   338