bluetoothcommsprofiles/btpan/pannapiphook/src/pannapiphook.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2007-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 //
       
    15 
       
    16 #include <bluetooth/logger.h>
       
    17 #include "pannapiphook.h"
       
    18 #include "RBnepFrame.h"			// For the DHCP / Forwarding RMBufPacket header flags
       
    19 
       
    20 #include <in_sock.h>
       
    21 #include <in_chk.h>
       
    22 #include <udp_hdr.h>
       
    23 #include <es_prot_internal.h>
       
    24 
       
    25 _LIT(KProtocolPanNapIpHookName, "pannapiphook");
       
    26 
       
    27 #ifdef __FLOG_ACTIVE
       
    28 _LIT8(KLogComponent, LOG_COMPONENT_PAN_NAPIPHOOK);
       
    29 #endif
       
    30 
       
    31 #ifdef __FLOG_ACTIVE
       
    32 // Define strings for logging.
       
    33 _LIT(KStringTrue, "True");
       
    34 _LIT(KStringFalse,  "False");
       
    35 _LIT(KStringDropped, "Dropped");
       
    36 _LIT(KStringPassed,  "Passed");
       
    37 #endif
       
    38 
       
    39 
       
    40 static const TUint KDhcpDstPort = 67;
       
    41 
       
    42 // Returning a negative number from the ApplyL() method in an IP hook will make the IP
       
    43 // stack drop the packet. No constant is defined in networking so define it here to
       
    44 // help readability of the code.
       
    45 static const TInt KIp6Hook_DROP = -1;
       
    46 
       
    47 //
       
    48 // CPanNapIpHook
       
    49 //
       
    50 CPanNapIpHook* CPanNapIpHook::NewL()
       
    51 	{
       
    52 	CPanNapIpHook* self = new(ELeave) CPanNapIpHook();
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 CPanNapIpHook::~CPanNapIpHook()
       
    57 	{
       
    58 	Unbind(this,BindHookAll());
       
    59 	CProtocolPosthook::NetworkDetached();
       
    60 	}
       
    61 
       
    62 void CPanNapIpHook::NetworkAttachedL()
       
    63 	{
       
    64 	// This hook is dual purpose and checks both that incoming packets destined for the
       
    65 	// DHCP server and any packets to be forwarded to the uplink have access to do so.
       
    66 	// This is why the hook gets bound in 2 places, once for forwarding packets and once
       
    67 	// for all incoming packets. The logic in ApplyL() determines where the hook has 
       
    68 	// been called from.
       
    69 	NetworkService()->BindL(this, BindForwardHook());
       
    70 	NetworkService()->BindL(this, BindHookAll());
       
    71 	iManager = NetworkService()->Interfacer();
       
    72 	}
       
    73 
       
    74 TInt CPanNapIpHook::ApplyL(RMBufHookPacket &aPacket, RMBufRecvInfo &aInfo)
       
    75 	{
       
    76 	TInt rCode = KIp6Hook_PASS;
       
    77 	
       
    78 	// This hook is for packets coming from BNEP only and performs two purposes depending
       
    79 	// on whether or not the packet is destined for the local host or not.
       
    80 	if(aInfo.iFlags & KBnep)
       
    81 		{
       
    82 		LOG1(_L("Remote device granted access to uplink: %S"), (aInfo.iFlags & KBnepForwardingAllowed) ? &KStringTrue() : &KStringFalse());
       
    83 		
       
    84 		if (iManager->IsForMeAddress(TInetAddr::Cast(aInfo.iDstAddr).Ip6Address(), aInfo.iInterfaceIndex))
       
    85 			{
       
    86 			LOG(_L("Packet destined for local host"));
       
    87 
       
    88 			// Make sure that any packets from BNEP destined for the DHCP Server are processed
       
    89 			// only if it has come from a remote device granted access to the uplink
       
    90 			if(aInfo.iProtocol == KProtocolInetUdp)
       
    91 				{
       
    92 				TInet6Checksum<TInet6HeaderIP4> ipHeader(aPacket);
       
    93 				TInet6Packet<TInet6HeaderUDP> udpHeader(aPacket, ipHeader.iHdr->HeaderLength());
       
    94 				
       
    95 				if(udpHeader.iHdr->DstPort() == KDhcpDstPort)
       
    96 					{
       
    97 					LOG(_L("DHCP packet detected"));
       
    98 					
       
    99 					// DHCP packet. Drop if forwarding flag not set.
       
   100 					if(!(aInfo.iFlags & KBnepForwardingAllowed))
       
   101 						{
       
   102 						aPacket.Free();
       
   103 						rCode = KIp6Hook_DROP;  // Drop packet
       
   104 						}
       
   105 					}
       
   106 				}			
       
   107 			}
       
   108 		else
       
   109 			{
       
   110 			LOG(_L("Packet destined to be forwarded"));
       
   111 
       
   112 			// Before forwarding packets from BNEP ensure that they have come from a remote
       
   113 			// device granted access to the uplink
       
   114 			if(!(aInfo.iFlags & KBnepForwardingAllowed))
       
   115 				{
       
   116 				aPacket.Free();
       
   117 				rCode = KIp6Hook_DROP;  // Drop packet
       
   118 				}
       
   119 			}
       
   120 		}
       
   121 		
       
   122 	// Finished with the flags
       
   123 	aInfo.iFlags &= ~KBnep;
       
   124 	aInfo.iFlags &= ~KBnepForwardingAllowed;
       
   125 
       
   126 	LOG1(_L("Packet %S"), (rCode < 0) ? &KStringDropped() : &KStringPassed());
       
   127 
       
   128 	return rCode;
       
   129 	}
       
   130 
       
   131 void CPanNapIpHook::Identify(TServerProtocolDesc* aProtocolDesc) const 
       
   132 	{
       
   133 	Describe(*aProtocolDesc);
       
   134 	}
       
   135 
       
   136 void CPanNapIpHook::Describe(TServerProtocolDesc& aEntry)
       
   137 	{
       
   138 	aEntry.iName = KProtocolPanNapIpHookName;
       
   139 	aEntry.iAddrFamily = KAfInet;
       
   140 	aEntry.iSockType = KSockDatagram;
       
   141 	aEntry.iProtocol = KProtocolPanNapIpHook;
       
   142 	aEntry.iVersion = TVersion(1, 0, 0);
       
   143 	aEntry.iByteOrder = EBigEndian;
       
   144 	aEntry.iServiceInfo = KSIDatagram | KSIConnectionLess;
       
   145 	aEntry.iNamingServices = 0;
       
   146 	aEntry.iSecurity = KSocketNoSecurity;
       
   147 	aEntry.iMessageSize = 0xffff;
       
   148 	aEntry.iServiceTypeInfo = 0;
       
   149 	aEntry.iNumSockets = KUnlimitedSockets;
       
   150 	aEntry.iServiceTypeInfo = ESocketSupport | EInterface;
       
   151 	}
       
   152 
       
   153 //
       
   154 // CPanNapIpHookFamily
       
   155 //	
       
   156 CPanNapIpHookFamily::CPanNapIpHookFamily()
       
   157 	{
       
   158 	__DECLARE_NAME(_S("CPanNapIpHookFamily"));
       
   159 	}
       
   160 
       
   161 CPanNapIpHookFamily::~CPanNapIpHookFamily()
       
   162 	{
       
   163 	}
       
   164 
       
   165 TInt CPanNapIpHookFamily::Install()
       
   166 	{
       
   167 	return KErrNone;
       
   168 	}
       
   169 
       
   170 TInt CPanNapIpHookFamily::Remove()
       
   171 	{
       
   172 	return KErrNone;
       
   173 	}
       
   174 
       
   175 TUint CPanNapIpHookFamily::ProtocolList(TServerProtocolDesc *& aProtocolList)
       
   176 	{
       
   177 	aProtocolList = new TServerProtocolDesc[1];
       
   178 	
       
   179 	if (!aProtocolList)
       
   180 		{
       
   181 		return 0;
       
   182 		}
       
   183 		
       
   184 	CPanNapIpHook::Describe(aProtocolList[0]);
       
   185 	return 1;
       
   186 	}
       
   187 
       
   188 CProtocolBase* CPanNapIpHookFamily::NewProtocolL(TUint /*aSockType*/, TUint aProtocol)
       
   189 	{
       
   190 	if (aProtocol != KProtocolPanNapIpHook)
       
   191 		{
       
   192 		User::Leave(KErrNotSupported);
       
   193 		}
       
   194 	return CPanNapIpHook::NewL();
       
   195 	}
       
   196 
       
   197 EXPORT_C CProtocolFamilyBase* Install(void)
       
   198 	{
       
   199 	CPanNapIpHookFamily* protocol = new CPanNapIpHookFamily();
       
   200 	return protocol;
       
   201 	}