d8c2f177c8e50e46c31a6c707e707136c1ad48b6
[reactos.git] / reactos / lib / drivers / ip / network / arp.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: datalink/arp.c
5 * PURPOSE: Address Resolution Protocol routines
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 PNDIS_PACKET PrepareARPPacket(
14 PIP_INTERFACE IF,
15 USHORT HardwareType,
16 USHORT ProtocolType,
17 UCHAR LinkAddressLength,
18 UCHAR ProtoAddressLength,
19 PVOID SenderLinkAddress,
20 PVOID SenderProtoAddress,
21 PVOID TargetLinkAddress,
22 PVOID TargetProtoAddress,
23 USHORT Opcode)
24 /*
25 * FUNCTION: Prepares an ARP packet
26 * ARGUMENTS:
27 * HardwareType = Hardware type (in network byte order)
28 * ProtocolType = Protocol type (in network byte order)
29 * LinkAddressLength = Length of link address fields
30 * ProtoAddressLength = Length of protocol address fields
31 * SenderLinkAddress = Sender's link address
32 * SenderProtoAddress = Sender's protocol address
33 * TargetLinkAddress = Target's link address (NULL if don't care)
34 * TargetProtoAddress = Target's protocol address
35 * Opcode = ARP opcode (in network byte order)
36 * RETURNS:
37 * Pointer to NDIS packet, NULL if there is not enough free resources
38 */
39 {
40 PNDIS_PACKET NdisPacket;
41 NDIS_STATUS NdisStatus;
42 PARP_HEADER Header;
43 PVOID DataBuffer;
44 ULONG Size, Contig;
45
46 TI_DbgPrint(DEBUG_ARP, ("Called.\n"));
47
48 /* Prepare ARP packet */
49 Size = sizeof(ARP_HEADER) +
50 2 * LinkAddressLength + /* Hardware address length */
51 2 * ProtoAddressLength; /* Protocol address length */
52 Size = MAX(Size, IF->MinFrameSize - IF->HeaderSize);
53
54 NdisStatus = AllocatePacketWithBuffer( &NdisPacket, NULL, Size );
55 if( !NT_SUCCESS(NdisStatus) ) return NULL;
56
57 GetDataPtr( NdisPacket, 0, (PCHAR *)&DataBuffer, (PUINT)&Contig );
58 ASSERT(DataBuffer);
59
60 RtlZeroMemory(DataBuffer, Size);
61 Header = (PARP_HEADER)((ULONG_PTR)DataBuffer);
62 Header->HWType = HardwareType;
63 Header->ProtoType = ProtocolType;
64 Header->HWAddrLen = LinkAddressLength;
65 Header->ProtoAddrLen = ProtoAddressLength;
66 Header->Opcode = Opcode; /* Already swapped */
67 DataBuffer = (PVOID)((ULONG_PTR)Header + sizeof(ARP_HEADER));
68
69 /* Our hardware address */
70 RtlCopyMemory(DataBuffer, SenderLinkAddress, LinkAddressLength);
71 DataBuffer = (PVOID)((ULONG_PTR)DataBuffer + LinkAddressLength);
72
73 /* Our protocol address */
74 RtlCopyMemory(DataBuffer, SenderProtoAddress, ProtoAddressLength);
75
76 if (TargetLinkAddress) {
77 DataBuffer = (PVOID)((ULONG_PTR)DataBuffer + ProtoAddressLength);
78 /* Target hardware address */
79 RtlCopyMemory(DataBuffer, TargetLinkAddress, LinkAddressLength);
80 DataBuffer = (PVOID)((ULONG_PTR)DataBuffer + LinkAddressLength);
81 } else
82 /* Don't care about target hardware address */
83 DataBuffer = (PVOID)((ULONG_PTR)DataBuffer + ProtoAddressLength + LinkAddressLength);
84
85 /* Target protocol address */
86 RtlCopyMemory(DataBuffer, TargetProtoAddress, ProtoAddressLength);
87
88 return NdisPacket;
89 }
90
91
92 VOID ARPTransmitComplete(
93 PVOID Context,
94 PNDIS_PACKET NdisPacket,
95 NDIS_STATUS NdisStatus)
96 /*
97 * FUNCTION: ARP request transmit completion handler
98 * ARGUMENTS:
99 * Context = Pointer to context information (IP_INTERFACE)
100 * Packet = Pointer to NDIS packet that was sent
101 * NdisStatus = NDIS status of operation
102 * NOTES:
103 * This routine is called when an ARP request has been sent
104 */
105 {
106 TI_DbgPrint(DEBUG_ARP, ("Called.\n"));
107 FreeNdisPacket(NdisPacket);
108 }
109
110
111 BOOLEAN ARPTransmit(PIP_ADDRESS Address, PIP_INTERFACE Interface)
112 /*
113 * FUNCTION: Creates an ARP request and transmits it on a network
114 * ARGUMENTS:
115 * Address = Pointer to IP address to resolve
116 * RETURNS:
117 * TRUE if the request was successfully sent, FALSE if not
118 */
119 {
120 PNDIS_PACKET NdisPacket;
121 UCHAR ProtoAddrLen;
122 USHORT ProtoType;
123
124 TI_DbgPrint(DEBUG_ARP, ("Called.\n"));
125
126 /* If Address is NULL then the caller wants an
127 * gratuitous ARP packet sent */
128 if (!Address)
129 Address = &Interface->Unicast;
130
131 switch (Address->Type) {
132 case IP_ADDRESS_V4:
133 ProtoType = (USHORT)ETYPE_IPv4; /* IPv4 */
134 ProtoAddrLen = 4; /* Length of IPv4 address */
135 break;
136 case IP_ADDRESS_V6:
137 ProtoType = (USHORT)ETYPE_IPv6; /* IPv6 */
138 ProtoAddrLen = 16; /* Length of IPv6 address */
139 break;
140 default:
141 TI_DbgPrint(DEBUG_ARP,("Bad Address Type %x\n", Address->Type));
142 KeBugCheck(0);
143 /* Should not happen */
144 return FALSE;
145 }
146
147 NdisPacket = PrepareARPPacket(
148 Interface,
149 WN2H(0x0001), /* FIXME: Ethernet only */
150 ProtoType, /* Protocol type */
151 (UCHAR)Interface->AddressLength, /* Hardware address length */
152 (UCHAR)ProtoAddrLen, /* Protocol address length */
153 Interface->Address, /* Sender's (local) hardware address */
154 &Interface->Unicast.Address.IPv4Address,/* Sender's (local) protocol address */
155 NULL, /* Don't care */
156 &Address->Address.IPv4Address, /* Target's (remote) protocol address */
157 ARP_OPCODE_REQUEST); /* ARP request */
158
159 if( !NdisPacket ) return FALSE;
160
161 ASSERT_KM_POINTER(NdisPacket);
162 ASSERT_KM_POINTER(PC(NdisPacket));
163 PC(NdisPacket)->DLComplete = ARPTransmitComplete;
164
165 TI_DbgPrint(DEBUG_ARP,("Sending ARP Packet\n"));
166
167 (*Interface->Transmit)(Interface->Context, NdisPacket,
168 0, NULL, LAN_PROTO_ARP);
169
170 return TRUE;
171 }
172
173
174 VOID ARPReceive(
175 PVOID Context,
176 PIP_PACKET Packet)
177 /*
178 * FUNCTION: Receives an ARP packet
179 * ARGUMENTS:
180 * Context = Pointer to context information (IP_INTERFACE)
181 * Packet = Pointer to packet
182 */
183 {
184 PARP_HEADER Header;
185 IP_ADDRESS Address;
186 PVOID SenderHWAddress;
187 PVOID SenderProtoAddress;
188 PNEIGHBOR_CACHE_ENTRY NCE;
189 PNDIS_PACKET NdisPacket;
190 PIP_INTERFACE Interface = (PIP_INTERFACE)Context;
191
192 TI_DbgPrint(DEBUG_ARP, ("Called.\n"));
193
194 Header = (PARP_HEADER)Packet->Header;
195
196 /* FIXME: Ethernet only */
197 if (WN2H(Header->HWType) != 1) {
198 TI_DbgPrint(DEBUG_ARP, ("Unknown ARP hardware type (0x%X).\n", WN2H(Header->HWType)));
199 return;
200 }
201
202 /* Check protocol type */
203 if (Header->ProtoType != ETYPE_IPv4) {
204 TI_DbgPrint(DEBUG_ARP, ("Unknown ARP protocol type (0x%X).\n", WN2H(Header->ProtoType)));
205 return;
206 }
207
208 SenderHWAddress = (PVOID)((ULONG_PTR)Header + sizeof(ARP_HEADER));
209 SenderProtoAddress = (PVOID)((ULONG_PTR)SenderHWAddress + Header->HWAddrLen);
210
211 /* Check if we know the sender */
212
213 AddrInitIPv4(&Address, *((PULONG)SenderProtoAddress));
214
215 NCE = NBLocateNeighbor(&Address);
216 if (NCE) {
217 /* We know the sender. Update the hardware address
218 and state in our neighbor address cache */
219 NBUpdateNeighbor(NCE, SenderHWAddress, 0);
220 } else {
221 /* The packet had our protocol address as target. The sender
222 may want to communicate with us soon, so add his address
223 to our address cache */
224 NCE = NBAddNeighbor(Interface, &Address, SenderHWAddress,
225 Header->HWAddrLen, 0, ARP_TIMEOUT);
226 }
227
228 if (Header->Opcode != ARP_OPCODE_REQUEST)
229 return;
230
231 /* This is a request for our address. Swap the addresses and
232 send an ARP reply back to the sender */
233 NdisPacket = PrepareARPPacket(
234 Interface,
235 Header->HWType, /* Hardware type */
236 Header->ProtoType, /* Protocol type */
237 (UCHAR)Interface->AddressLength, /* Hardware address length */
238 (UCHAR)Header->ProtoAddrLen, /* Protocol address length */
239 Interface->Address, /* Sender's (local) hardware address */
240 &Interface->Unicast.Address.IPv4Address,/* Sender's (local) protocol address */
241 SenderHWAddress, /* Target's (remote) hardware address */
242 SenderProtoAddress, /* Target's (remote) protocol address */
243 ARP_OPCODE_REPLY); /* ARP reply */
244 if (NdisPacket) {
245 PC(NdisPacket)->DLComplete = ARPTransmitComplete;
246 (*Interface->Transmit)(Interface->Context,
247 NdisPacket,
248 0,
249 SenderHWAddress,
250 LAN_PROTO_ARP);
251 }
252
253 Packet->Free(Packet);
254 }
255
256 /* EOF */