|
1 // Copyright (c) 2004-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 /** |
|
17 @file ramod.cpp |
|
18 @internalComponent |
|
19 */ |
|
20 |
|
21 |
|
22 #include <in_chk.h> |
|
23 #include <icmp6_hdr.h> |
|
24 #include <in_sock.h> |
|
25 #include <in_bind.h> |
|
26 #include "in6_opt.h" |
|
27 |
|
28 #include "ramod.h" |
|
29 #include "HookLog.h" |
|
30 |
|
31 |
|
32 Cramod* Cramod::NewL() |
|
33 { |
|
34 Cramod* self = new(ELeave) Cramod(); |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(); |
|
37 CleanupStack::Pop(); |
|
38 return self; |
|
39 } |
|
40 |
|
41 |
|
42 |
|
43 void Cramod::ConstructL() |
|
44 { |
|
45 } |
|
46 |
|
47 |
|
48 Cramod::Cramod() |
|
49 { |
|
50 } |
|
51 |
|
52 |
|
53 |
|
54 Cramod::~Cramod() |
|
55 { |
|
56 } |
|
57 |
|
58 |
|
59 |
|
60 void Cramod::BindL(CProtocolBase* aProtocol, TUint aId) |
|
61 { |
|
62 // Do sanity checks |
|
63 // |
|
64 if ((aId != KProtocolInet6Ip) || (aProtocol == this)) |
|
65 { |
|
66 User::Leave(KErrArgument); |
|
67 } |
|
68 |
|
69 TUint ourId; |
|
70 { |
|
71 TServerProtocolDesc info; |
|
72 Identify(&info); |
|
73 ourId = info.iProtocol; |
|
74 } |
|
75 |
|
76 if (aId == ourId) |
|
77 { |
|
78 User::Leave(KErrArgument); |
|
79 } |
|
80 |
|
81 if ( iProtocolIPv6 != NULL ) |
|
82 { |
|
83 if ( iProtocolIPv6 == aProtocol ) |
|
84 { |
|
85 // We don't need to bind to the same protocol twice. |
|
86 // |
|
87 return; |
|
88 } |
|
89 else |
|
90 { |
|
91 // We don't want to bind to a different protocol either... |
|
92 // |
|
93 User::Leave(KErrAlreadyExists); |
|
94 } |
|
95 } |
|
96 |
|
97 iProtocolIPv6 = (CProtocolInet6Binder*) aProtocol; |
|
98 |
|
99 RegisterHooksL(); |
|
100 } |
|
101 |
|
102 |
|
103 |
|
104 void Cramod::Unbind(CProtocolBase* aProtocol, TUint /* aId */) |
|
105 { |
|
106 if (iProtocolIPv6 != aProtocol) |
|
107 { |
|
108 return; |
|
109 } |
|
110 |
|
111 UnregisterHooks(); |
|
112 |
|
113 iProtocolIPv6 = 0; |
|
114 } |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 void Cramod::RegisterHooksL(void) |
|
120 /** |
|
121 * Registers the hook to catch IP stack events |
|
122 */ |
|
123 { |
|
124 if(!iProtocolIPv6) |
|
125 { |
|
126 User::Leave(KErrNotReady); |
|
127 } |
|
128 |
|
129 // bind to IP for incoming packets |
|
130 iProtocolIPv6->BindL((CProtocolBase*)this, BindHookFor(KProtocolInet6Icmp)); |
|
131 } |
|
132 |
|
133 |
|
134 |
|
135 void Cramod::UnregisterHooks(void) |
|
136 /** |
|
137 * Detaches this hook from the running of the stack. |
|
138 * This is so IPEN doesn't slow down the stack while it isn't |
|
139 * needed by any clients. |
|
140 * |
|
141 * e.g. without this mechanism, every incoming packet would pass through |
|
142 * Cramod::ApplyL pointlessly. |
|
143 * |
|
144 */ |
|
145 { |
|
146 if(!iProtocolIPv6) |
|
147 { |
|
148 return; |
|
149 } |
|
150 // If IPv6 exists, unbind the hook. Covers both in/out hooks (I hope) |
|
151 iProtocolIPv6->Unbind(this); |
|
152 } |
|
153 |
|
154 |
|
155 |
|
156 void Cramod::FillIdentification(TServerProtocolDesc& anEntry) |
|
157 /** |
|
158 * Fills in an existing protocol description structure with details of this protocol. |
|
159 * |
|
160 * @param aProtocolDesc: reference to the structure to be filled in |
|
161 */ |
|
162 { |
|
163 anEntry.iName=_S("ramod"); |
|
164 anEntry.iAddrFamily=KAfInet; //KAfExain; |
|
165 anEntry.iSockType=KSockDatagram; |
|
166 anEntry.iProtocol=KMyProtocolId; |
|
167 anEntry.iVersion=TVersion(1, 0, 0); |
|
168 anEntry.iByteOrder=EBigEndian; |
|
169 anEntry.iServiceInfo=KSIDatagram | KSIConnectionLess; |
|
170 anEntry.iNamingServices=0; |
|
171 anEntry.iSecurity=KSocketNoSecurity; |
|
172 anEntry.iMessageSize=0xffff; |
|
173 anEntry.iServiceTypeInfo=0; // 1 for ability to create sockets |
|
174 anEntry.iNumSockets=KUnlimitedSockets; |
|
175 } |
|
176 |
|
177 |
|
178 void Cramod::Identify(TServerProtocolDesc* aProtocolDesc) const |
|
179 /** |
|
180 * Fills in an existing protocol description structure with details of this protocol. |
|
181 * |
|
182 * @param aProtocolDesc: pointer to the structure to be filled in |
|
183 */ |
|
184 { |
|
185 FillIdentification(*aProtocolDesc); |
|
186 } |
|
187 |
|
188 |
|
189 |
|
190 /** |
|
191 * Incoming packet |
|
192 */ |
|
193 TInt Cramod::ApplyL(RMBufHookPacket& aPacket, RMBufRecvInfo& aInfo) |
|
194 { |
|
195 |
|
196 LOG ( |
|
197 _LIT(KHookNotifyStr,"Cramod::ApplyL hit with protocol: %d"); |
|
198 HookLog::Printf( KHookNotifyStr , aInfo.iProtocol ); |
|
199 ) |
|
200 |
|
201 if(aInfo.iProtocol == static_cast<TInt>(KProtocolInet6Icmp)) |
|
202 { |
|
203 |
|
204 LOG ( |
|
205 _LIT(KHookNotifyStr," ICMPv6 packet detected"); |
|
206 HookLog::Printf( KHookNotifyStr ); |
|
207 ) |
|
208 |
|
209 /** |
|
210 * Could switch on ICMP type here, in case the hook needs to catch |
|
211 * other ICMP packet types.. |
|
212 */ |
|
213 TInet6Checksum<TInet6HeaderICMP_RouterAdv> icmp(aPacket,aInfo.iOffset); |
|
214 |
|
215 if(icmp.iHdr) |
|
216 { |
|
217 if (icmp.iHdr->Type() == KInet6ICMP_RouterAdv && |
|
218 icmp.VerifyChecksum( aPacket, |
|
219 aInfo.iProtocol == static_cast<TInt>(KProtocolInet6Icmp) ? &aInfo : NULL, |
|
220 aInfo.iOffset) ) |
|
221 { |
|
222 #ifdef RAMOD1 |
|
223 // RA received from LAN, modfiy the M and O flag here, clear 'M' flag and set 'O' flag |
|
224 icmp.iHdr->SetFlags(64); |
|
225 icmp.ComputeChecksum( aPacket, |
|
226 aInfo.iProtocol == static_cast<TInt> (KProtocolInet6Icmp) ? &aInfo : NULL, |
|
227 aInfo.iOffset ); |
|
228 #elif RAMOD2 |
|
229 // RA received from LAN, modfiy the M and O flag here, set both 'M' and 'O' flags as false |
|
230 icmp.iHdr->SetFlags(0); |
|
231 icmp.ComputeChecksum( aPacket, |
|
232 aInfo.iProtocol == static_cast<TInt> (KProtocolInet6Icmp) ? &aInfo : NULL, |
|
233 aInfo.iOffset ); |
|
234 |
|
235 #elif RAMOD3 |
|
236 // RA received, modfiy the M and O flag here, invalidate checksum by changing flag, Don't compute checksum, packets will be dropped |
|
237 icmp.iHdr->SetFlags(0); |
|
238 #endif |
|
239 } |
|
240 } |
|
241 |
|
242 } |
|
243 |
|
244 return KIp6Hook_PASS; // ensure we don't affect the control flow |
|
245 } |
|
246 |
|
247 |
|
248 |