c859ac7d2a8639cb853c224ae4b74590b7379556
[reactos.git] / sdk / lib / drivers / ip / network / loopback.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: datalink/loopback.c
5 * PURPOSE: Loopback adapter
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 PIP_INTERFACE Loopback = NULL;
14
15 VOID LoopPassiveWorker(
16 PVOID Context)
17 {
18 PIP_PACKET IPPacket = Context;
19
20 /* IPReceive() takes care of the NDIS packet */
21 IPReceive(Loopback, IPPacket);
22
23 ExFreePool(IPPacket);
24 }
25
26 VOID LoopTransmit(
27 PVOID Context,
28 PNDIS_PACKET NdisPacket,
29 UINT Offset,
30 PVOID LinkAddress,
31 USHORT Type)
32 /*
33 * FUNCTION: Transmits a packet
34 * ARGUMENTS:
35 * Context = Pointer to context information (NULL)
36 * NdisPacket = Pointer to NDIS packet to send
37 * Offset = Offset in packet where packet data starts
38 * LinkAddress = Pointer to link address
39 * Type = LAN protocol type
40 */
41 {
42 PCHAR PacketBuffer;
43 UINT PacketLength;
44 PNDIS_PACKET XmitPacket;
45 NDIS_STATUS NdisStatus;
46 PIP_PACKET IPPacket;
47
48 ASSERT_KM_POINTER(NdisPacket);
49 ASSERT_KM_POINTER(PC(NdisPacket));
50 ASSERT_KM_POINTER(PC(NdisPacket)->DLComplete);
51
52 if (Type != LAN_PROTO_IPv4)
53 {
54 TI_DbgPrint(MAX_TRACE, ("Received unsupported protocol %u\n", Type));
55 PC(NdisPacket)->DLComplete(PC(NdisPacket)->Context, NdisPacket, NDIS_STATUS_NOT_SUPPORTED);
56 return;
57 }
58
59 TI_DbgPrint(MAX_TRACE, ("Called (NdisPacket = %x)\n", NdisPacket));
60
61 GetDataPtr( NdisPacket, 0, &PacketBuffer, &PacketLength );
62
63 NdisStatus = AllocatePacketWithBuffer
64 ( &XmitPacket, PacketBuffer, PacketLength );
65
66 if( NT_SUCCESS(NdisStatus) ) {
67 IPPacket = ExAllocatePool(NonPagedPool, sizeof(IP_PACKET));
68 if (IPPacket)
69 {
70 IPInitializePacket(IPPacket, 0);
71
72 IPPacket->NdisPacket = XmitPacket;
73
74 GetDataPtr(IPPacket->NdisPacket,
75 0,
76 (PCHAR*)&IPPacket->Header,
77 &IPPacket->TotalSize);
78
79 IPPacket->MappedHeader = TRUE;
80
81 if (!ChewCreate(LoopPassiveWorker, IPPacket))
82 {
83 IPPacket->Free(IPPacket);
84 ExFreePool(IPPacket);
85 NdisStatus = NDIS_STATUS_RESOURCES;
86 }
87 }
88 else
89 NdisStatus = NDIS_STATUS_RESOURCES;
90 }
91
92 (PC(NdisPacket)->DLComplete)
93 ( PC(NdisPacket)->Context, NdisPacket, NdisStatus );
94 }
95
96 NDIS_STATUS LoopRegisterAdapter(
97 PNDIS_STRING AdapterName,
98 PLAN_ADAPTER *Adapter)
99 /*
100 * FUNCTION: Registers loopback adapter with the network layer
101 * ARGUMENTS:
102 * AdapterName = Unused
103 * Adapter = Unused
104 * RETURNS:
105 * Status of operation
106 */
107 {
108 LLIP_BIND_INFO BindInfo;
109
110 TI_DbgPrint(MID_TRACE, ("Called.\n"));
111
112 /* Bind the adapter to network (IP) layer */
113 BindInfo.Context = NULL;
114 BindInfo.HeaderSize = 0;
115 BindInfo.MinFrameSize = 0;
116 BindInfo.Address = NULL;
117 BindInfo.AddressLength = 0;
118 BindInfo.Transmit = LoopTransmit;
119
120 Loopback = IPCreateInterface(&BindInfo);
121 if (!Loopback) return NDIS_STATUS_RESOURCES;
122
123 Loopback->MTU = 16384;
124
125 Loopback->Name.Buffer = L"Loopback";
126 Loopback->Name.MaximumLength = Loopback->Name.Length =
127 wcslen(Loopback->Name.Buffer) * sizeof(WCHAR);
128
129 AddrInitIPv4(&Loopback->Unicast, LOOPBACK_ADDRESS_IPv4);
130 AddrInitIPv4(&Loopback->Netmask, LOOPBACK_ADDRMASK_IPv4);
131 AddrInitIPv4(&Loopback->Broadcast, LOOPBACK_BCASTADDR_IPv4);
132
133 IPRegisterInterface(Loopback);
134
135 IPAddInterfaceRoute(Loopback);
136
137 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
138
139 return NDIS_STATUS_SUCCESS;
140 }
141
142
143 NDIS_STATUS LoopUnregisterAdapter(
144 PLAN_ADAPTER Adapter)
145 /*
146 * FUNCTION: Unregisters loopback adapter with the network layer
147 * ARGUMENTS:
148 * Adapter = Unused
149 * RETURNS:
150 * Status of operation
151 * NOTES:
152 * Does not care wether we have registered loopback adapter
153 */
154 {
155 TI_DbgPrint(MID_TRACE, ("Called.\n"));
156
157 if (Loopback != NULL)
158 {
159 IPUnregisterInterface(Loopback);
160 IPDestroyInterface(Loopback);
161 Loopback = NULL;
162 }
163
164 TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
165
166 return NDIS_STATUS_SUCCESS;
167 }