5b7c706b8febc1145829b214643ba1bc81ff29a4
[reactos.git] / drivers / network / tcpip / tcpip / ninfo.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/ninfo.c
5 * PURPOSE: Network information
6 * PROGRAMMERS: Art Yerkes
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 #define IP_ROUTE_TYPE_ADD 3
14 #define IP_ROUTE_TYPE_DEL 2
15
16 /* Get IPRouteEntry s for each of the routes in the system */
17 TDI_STATUS InfoTdiQueryGetRouteTable( PIP_INTERFACE IF, PNDIS_BUFFER Buffer, PUINT BufferSize ) {
18 TDI_STATUS Status;
19 KIRQL OldIrql;
20 UINT RtCount = CountFIBs(IF);
21 UINT Size = sizeof( IPROUTE_ENTRY ) * RtCount;
22 PFIB_ENTRY RCache, RCacheCur;
23 PIPROUTE_ENTRY RouteEntries, RtCurrent;
24 UINT i;
25
26 TI_DbgPrint(DEBUG_INFO, ("Called, routes = %d\n",
27 RtCount));
28
29 if (RtCount == 0)
30 return InfoCopyOut(NULL, 0, NULL, BufferSize);
31
32 RouteEntries = ExAllocatePoolWithTag( NonPagedPool, Size, ROUTE_ENTRY_TAG );
33 RtCurrent = RouteEntries;
34
35 RCache = ExAllocatePoolWithTag( NonPagedPool, sizeof( FIB_ENTRY ) * RtCount, FIB_TAG );
36 RCacheCur = RCache;
37
38 if( !RCache || !RouteEntries ) {
39 if( RCache ) ExFreePoolWithTag( RCache, FIB_TAG );
40 if( RouteEntries ) ExFreePoolWithTag( RouteEntries, ROUTE_ENTRY_TAG );
41 return TDI_NO_RESOURCES;
42 }
43
44 RtlZeroMemory( RouteEntries, Size );
45
46 RtCount = CopyFIBs( IF, RCache );
47
48 while( RtCurrent < RouteEntries + RtCount ) {
49 ASSERT(RCacheCur->Router);
50
51 RtlCopyMemory( &RtCurrent->Dest,
52 &RCacheCur->NetworkAddress.Address,
53 sizeof(RtCurrent->Dest) );
54 RtlCopyMemory( &RtCurrent->Mask,
55 &RCacheCur->Netmask.Address,
56 sizeof(RtCurrent->Mask) );
57 RtlCopyMemory( &RtCurrent->Gw,
58 &RCacheCur->Router->Address.Address,
59 sizeof(RtCurrent->Gw) );
60
61 RtCurrent->Metric1 = RCacheCur->Metric;
62 RtCurrent->Type = TDI_ADDRESS_TYPE_IP;
63
64 TI_DbgPrint
65 (DEBUG_INFO,
66 ("%d: NA %08x NM %08x GW %08x MT %x\n",
67 RtCurrent - RouteEntries,
68 RtCurrent->Dest,
69 RtCurrent->Mask,
70 RtCurrent->Gw,
71 RtCurrent->Metric1 ));
72
73 TcpipAcquireSpinLock(&EntityListLock, &OldIrql);
74 for (i = 0; i < EntityCount; i++)
75 if (EntityList[i].context == IF)
76 break;
77
78 if (i < EntityCount)
79 RtCurrent->Index = EntityList[i].tei_instance;
80 else
81 RtCurrent->Index = 0;
82
83 TcpipReleaseSpinLock(&EntityListLock, OldIrql);
84
85 RtCurrent++; RCacheCur++;
86 }
87
88 Status = InfoCopyOut( (PCHAR)RouteEntries, Size, Buffer, BufferSize );
89
90 ExFreePoolWithTag( RouteEntries, ROUTE_ENTRY_TAG );
91 ExFreePoolWithTag( RCache, FIB_TAG );
92
93 TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
94
95 return Status;
96 }
97
98 TDI_STATUS InfoTdiQueryGetAddrTable(TDIEntityID ID,
99 PNDIS_BUFFER Buffer,
100 PUINT BufferSize)
101 {
102 KIRQL OldIrql;
103 PIPADDR_ENTRY IPEntry;
104 PIP_INTERFACE CurrentIF;
105 UINT i;
106
107 TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
108
109
110 TcpipAcquireSpinLock(&EntityListLock, &OldIrql);
111
112 for (i = 0; i < EntityCount; i++)
113 {
114 if (EntityList[i].tei_entity == ID.tei_entity &&
115 EntityList[i].tei_instance == ID.tei_instance)
116 break;
117 }
118
119 if (i == EntityCount)
120 {
121 TcpipReleaseSpinLock(&EntityListLock, OldIrql);
122 return TDI_INVALID_PARAMETER;
123 }
124
125 IPEntry = ExAllocatePoolWithTag(NonPagedPool, sizeof(IPADDR_ENTRY), IP_ADDRESS_TAG);
126 if (!IPEntry)
127 {
128 TcpipReleaseSpinLock(&EntityListLock, OldIrql);
129 return TDI_NO_RESOURCES;
130 }
131
132 CurrentIF = EntityList[i].context;
133
134 IPEntry->Index = CurrentIF->Index;
135 GetInterfaceIPv4Address(CurrentIF,
136 ADE_UNICAST,
137 &IPEntry->Addr);
138 GetInterfaceIPv4Address(CurrentIF,
139 ADE_ADDRMASK,
140 &IPEntry->Mask);
141 GetInterfaceIPv4Address(CurrentIF,
142 ADE_BROADCAST,
143 &IPEntry->BcastAddr);
144
145 TcpipReleaseSpinLock(&EntityListLock, OldIrql);
146
147 InfoCopyOut((PCHAR)IPEntry, sizeof(IPADDR_ENTRY),
148 Buffer, BufferSize);
149
150 ExFreePoolWithTag(IPEntry, IP_ADDRESS_TAG);
151
152 return TDI_SUCCESS;
153 }
154
155 TDI_STATUS InfoTdiQueryGetIPSnmpInfo( TDIEntityID ID,
156 PIP_INTERFACE IF,
157 PNDIS_BUFFER Buffer,
158 PUINT BufferSize ) {
159 IPSNMPInfo SnmpInfo;
160 UINT IfCount = CountInterfaces();
161 UINT RouteCount = CountFIBs(IF);
162 TDI_STATUS Status = TDI_INVALID_REQUEST;
163
164 TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
165
166 RtlZeroMemory(&SnmpInfo, sizeof(SnmpInfo));
167
168 SnmpInfo.ipsi_numif = IfCount;
169 SnmpInfo.ipsi_numaddr = 1;
170 SnmpInfo.ipsi_numroutes = RouteCount;
171
172 Status = InfoCopyOut( (PCHAR)&SnmpInfo, sizeof(SnmpInfo),
173 Buffer, BufferSize );
174
175 TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
176
177 return Status;
178 }
179
180 #define ntohs(n) ((((n) & 0xff) << 8) | (((n) & 0xff00) >> 8))
181
182 TDI_STATUS InfoTdiQueryGetConnectionTcpTable(PADDRESS_FILE AddrFile,
183 PNDIS_BUFFER Buffer,
184 PUINT BufferSize,
185 BOOLEAN Extended)
186 {
187 SIZE_T Size;
188 MIB_TCPROW_OWNER_PID TcpRow;
189 TDI_STATUS Status = TDI_INVALID_REQUEST;
190
191 TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
192
193 TcpRow.dwOwningPid = (DWORD)AddrFile->ProcessId;
194 if (Extended)
195 {
196 Size = sizeof(MIB_TCPROW_OWNER_PID);
197 }
198 else
199 {
200 Size = sizeof(MIB_TCPROW);
201 }
202
203 if (AddrFile->Listener != NULL)
204 {
205 PADDRESS_FILE EndPoint;
206
207 EndPoint = AddrFile->Listener->AddressFile;
208
209 TcpRow.dwState = MIB_TCP_STATE_LISTEN;
210 TcpRow.dwLocalAddr = AddrFile->Address.Address.IPv4Address;
211 TcpRow.dwLocalPort = AddrFile->Port;
212 TcpRow.dwRemoteAddr = EndPoint->Address.Address.IPv4Address;
213 TcpRow.dwRemotePort = EndPoint->Port;
214
215 Status = TDI_SUCCESS;
216 }
217 else if (AddrFile->Connection != NULL &&
218 AddrFile->Connection->SocketContext != NULL)
219 {
220 TA_IP_ADDRESS EndPoint;
221
222 Status = TCPGetSockAddress(AddrFile->Connection, (PTRANSPORT_ADDRESS)&EndPoint, FALSE);
223 if (NT_SUCCESS(Status))
224 {
225 ASSERT(EndPoint.TAAddressCount >= 1);
226 ASSERT(EndPoint.Address[0].AddressLength == TDI_ADDRESS_LENGTH_IP);
227 TcpRow.dwLocalAddr = EndPoint.Address[0].Address[0].in_addr;
228 TcpRow.dwLocalPort = ntohs(EndPoint.Address[0].Address[0].sin_port);
229
230 Status = TCPGetSockAddress(AddrFile->Connection, (PTRANSPORT_ADDRESS)&EndPoint, TRUE);
231 if (NT_SUCCESS(Status))
232 {
233 ASSERT(EndPoint.TAAddressCount >= 1);
234 ASSERT(EndPoint.Address[0].AddressLength == TDI_ADDRESS_LENGTH_IP);
235 TcpRow.dwRemoteAddr = EndPoint.Address[0].Address[0].in_addr;
236 TcpRow.dwRemotePort = ntohs(EndPoint.Address[0].Address[0].sin_port);
237
238 Status = TCPGetSocketStatus(AddrFile->Connection, &TcpRow.dwState);
239 ASSERT(NT_SUCCESS(Status));
240 }
241 }
242 }
243
244 if (NT_SUCCESS(Status))
245 {
246 Status = InfoCopyOut( (PCHAR)&TcpRow, Size,
247 Buffer, BufferSize );
248 }
249
250 TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
251
252 return Status;
253 }
254
255 TDI_STATUS InfoTdiQueryGetConnectionUdpTable(PADDRESS_FILE AddrFile,
256 PNDIS_BUFFER Buffer,
257 PUINT BufferSize,
258 BOOLEAN Extended)
259 {
260 MIB_UDPROW_OWNER_PID UdpRow;
261 TDI_STATUS Status = TDI_INVALID_REQUEST;
262
263 TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
264
265 UdpRow.dwLocalAddr = AddrFile->Address.Address.IPv4Address;
266 UdpRow.dwLocalPort = AddrFile->Port;
267 UdpRow.dwOwningPid = (DWORD)AddrFile->ProcessId;
268
269 Status = InfoCopyOut( (PCHAR)&UdpRow,
270 (Extended ? sizeof(MIB_UDPROW_OWNER_PID) : sizeof(MIB_UDPROW)),
271 Buffer, BufferSize );
272
273 TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
274
275 return Status;
276 }
277
278 TDI_STATUS InfoTdiSetRoute(PIP_INTERFACE IF, PVOID Buffer, UINT BufferSize)
279 {
280 IP_ADDRESS Address, Netmask, Router;
281 PIPROUTE_ENTRY Route = Buffer;
282
283 AddrInitIPv4( &Address, Route->Dest );
284 AddrInitIPv4( &Netmask, Route->Mask );
285 AddrInitIPv4( &Router, Route->Gw );
286
287 if (!Buffer || BufferSize < sizeof(IPROUTE_ENTRY))
288 return TDI_INVALID_PARAMETER;
289
290 if (IF == Loopback)
291 {
292 DbgPrint("Failing attempt to add route to loopback adapter\n");
293 return TDI_INVALID_PARAMETER;
294 }
295
296 if( Route->Type == IP_ROUTE_TYPE_ADD ) { /* Add the route */
297 TI_DbgPrint(DEBUG_INFO,("Adding route (%s)\n", A2S(&Address)));
298 if (!RouterCreateRoute( &Address, &Netmask, &Router,
299 IF, Route->Metric1))
300 return TDI_NO_RESOURCES;
301
302 return TDI_SUCCESS;
303 } else if( Route->Type == IP_ROUTE_TYPE_DEL ) {
304 TI_DbgPrint(DEBUG_INFO,("Removing route (%s)\n", A2S(&Address)));
305 if (NT_SUCCESS(RouterRemoveRoute( &Address, &Router )))
306 return TDI_SUCCESS;
307 else
308 return TDI_INVALID_PARAMETER;
309 }
310
311 return TDI_INVALID_REQUEST;
312 }