Added networking code from Casper Hornstrup
[reactos.git] / reactos / drivers / net / tcpip / include / ip.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: include/ip.h
5 * PURPOSE: Internet Protocol related definitions
6 */
7 #ifndef __IP_H
8 #define __IP_H
9
10 /* Raw IPv4 style address */
11 typedef ULONG IPv4_RAW_ADDRESS;
12 typedef IPv4_RAW_ADDRESS *PIPv4_RAW_ADDRESS;
13
14 /* Raw IPv6 style address */
15 typedef USHORT IPv6_RAW_ADDRESS[8];
16 typedef IPv6_RAW_ADDRESS *PIPv6_RAW_ADDRESS;
17
18 /* IP style address */
19 typedef struct IP_ADDRESS {
20 ULONG RefCount; /* Number of references to this address */
21 UCHAR Type; /* Type of IP address */
22 union {
23 IPv4_RAW_ADDRESS IPv4Address; /* IPv4 address */
24 PIPv6_RAW_ADDRESS IPv6Address; /* IPv6 address */
25 } Address;
26 } IP_ADDRESS, *PIP_ADDRESS;
27
28 /* IP type constants */
29 #define IP_ADDRESS_V4 0x00 /* IPv4 style address */
30 #define IP_ADDRESS_V6 0x01 /* IPv6 style address */
31
32
33 /* IPv4 header format */
34 typedef struct IPv4_HEADER {
35 UCHAR VerIHL; /* 4-bit version, 4-bit Internet Header Length */
36 UCHAR Tos; /* Type of Service */
37 USHORT TotalLength; /* Total Length */
38 USHORT Id; /* Identification */
39 USHORT FlagsFragOfs; /* 3-bit Flags, 13-bit Fragment Offset */
40 UCHAR Ttl; /* Time to Live */
41 UCHAR Protocol; /* Protocol */
42 USHORT Checksum; /* Header Checksum */
43 IPv4_RAW_ADDRESS SrcAddr; /* Source Address */
44 IPv4_RAW_ADDRESS DstAddr; /* Destination Address */
45 } IPv4_HEADER, *PIPv4_HEADER;
46
47 #define IPv4_FRAGOFS_MASK 0x1FFF
48 #define IPv4_MF_MASK 0x2000
49 #define IPv4_MAX_HEADER_SIZE 60
50
51 /* Packet completion handler prototype */
52 typedef VOID (*PACKET_COMPLETION_ROUTINE)(
53 PVOID Context,
54 PNDIS_PACKET NdisPacket,
55 NDIS_STATUS NdisStatus);
56
57 /* Structure for an IP packet */
58 typedef struct IP_PACKET {
59 ULONG RefCount; /* Reference count for this object */
60 UCHAR Type; /* Type of IP packet (see IP_ADDRESS_xx above) */
61 PVOID Header; /* Pointer to IP header for this packet */
62 UINT HeaderSize; /* Size of IP header */
63 PVOID Data; /* Current pointer into packet data */
64 UINT TotalSize; /* Total amount of data in packet (IP header and data) */
65 UINT ContigSize; /* Number of contiguous bytes left in current buffer */
66 UINT Position; /* Current logical offset into packet */
67 PNDIS_PACKET NdisPacket; /* Pointer to NDIS packet */
68 IP_ADDRESS SrcAddr; /* Source address */
69 IP_ADDRESS DstAddr; /* Destination address */
70 } IP_PACKET, *PIP_PACKET;
71
72 /* Packet context */
73 typedef struct PACKET_CONTEXT {
74 PACKET_COMPLETION_ROUTINE Complete; /* Transport level completion handler */
75 PVOID Context; /* Context information for handler */
76 PACKET_COMPLETION_ROUTINE DLComplete; /* Data link level completion handler. Also
77 used to link to next packet in a queue */
78 UINT DLOffset; /* Offset where data (IP header) starts */
79 } PACKET_CONTEXT, *PPACKET_CONTEXT;
80
81 /* The ProtocolReserved field is structured as a PACKET_CONTEXT */
82 #define PC(Packet) ((PPACKET_CONTEXT)(&Packet->ProtocolReserved))
83
84
85 /* Address information a.k.a ADE */
86 typedef struct _ADDRESS_ENTRY {
87 LIST_ENTRY ListEntry; /* Entry on list */
88 ULONG RefCount; /* Reference count */
89 struct _NET_TABLE_ENTRY *NTE; /* NTE associated with this address */
90 UCHAR Type; /* Address type */
91 PIP_ADDRESS Address; /* Pointer to address identifying this entry */
92 } ADDRESS_ENTRY, *PADDRESS_ENTRY;
93
94 /* Values for address type */
95 #define ADE_UNICAST 0x01
96 #define ADE_MULTICAST 0x02
97 #define ADE_ADDRMASK 0x03
98
99 /* There is one NTE for each source (unicast) address assigned to an interface */
100 typedef struct _NET_TABLE_ENTRY {
101 LIST_ENTRY IFListEntry; /* Entry on interface list */
102 LIST_ENTRY NTListEntry; /* Entry on net table list */
103 struct _IP_INTERFACE *Interface; /* Pointer to interface on this net */
104 struct _PREFIX_LIST_ENTRY *PLE; /* Pointer to prefix list entry for this net */
105 ULONG RefCount; /* Reference count */
106 PIP_ADDRESS Address; /* Pointer to unicast address for this net */
107 } NET_TABLE_ENTRY, *PNET_TABLE_ENTRY;
108
109
110 /* Link layer transmit prototype */
111 typedef VOID (*LL_TRANSMIT_ROUTINE)(
112 PVOID Context,
113 PNDIS_PACKET NdisPacket,
114 UINT Offset,
115 PVOID LinkAddress,
116 USHORT Type);
117
118 /* Link layer to IP binding information */
119 typedef struct _LLIP_BIND_INFO {
120 PVOID Context; /* Pointer to link layer context information */
121 UINT HeaderSize; /* Size of link level header */
122 UINT MinFrameSize; /* Minimum frame size in bytes */
123 UINT MTU; /* Maximum transmission unit */
124 PUCHAR Address; /* Pointer to interface address */
125 UINT AddressLength; /* Length of address in bytes */
126 LL_TRANSMIT_ROUTINE Transmit; /* Transmit function for this interface */
127 } LLIP_BIND_INFO, *PLLIP_BIND_INFO;
128
129
130 /* Information about an IP interface */
131 typedef struct _IP_INTERFACE {
132 LIST_ENTRY ListEntry; /* Entry on list */
133 ULONG RefCount; /* Reference count */
134 KSPIN_LOCK Lock; /* Spin lock for this object */
135 LIST_ENTRY NTEListHead; /* List of NTEs on this interface */
136 LIST_ENTRY ADEListHead; /* List of ADEs on this interface */
137 PVOID Context; /* Pointer to link layer context information */
138 UINT HeaderSize; /* Size of link level header */
139 UINT MinFrameSize; /* Minimum frame size in bytes */
140 UINT MTU; /* Maximum transmission unit */
141 PUCHAR Address; /* Pointer to interface address */
142 UINT AddressLength; /* Length of address in bytes */
143 LL_TRANSMIT_ROUTINE Transmit; /* Pointer to transmit function */
144 } IP_INTERFACE, *PIP_INTERFACE;
145
146
147 /* Prefix List Entry */
148 typedef struct _PREFIX_LIST_ENTRY {
149 LIST_ENTRY ListEntry; /* Entry on list */
150 ULONG RefCount; /* Reference count */
151 PIP_INTERFACE Interface; /* Pointer to interface */
152 PIP_ADDRESS Prefix; /* Pointer to prefix */
153 UINT PrefixLength; /* Length of prefix */
154 } PREFIX_LIST_ENTRY, *PPREFIX_LIST_ENTRY;
155
156
157 #define IP_PROTOCOL_TABLE_SIZE 0x100
158
159 typedef VOID (*IP_PROTOCOL_HANDLER)(
160 PNET_TABLE_ENTRY NTE,
161 PIP_PACKET IPPacket);
162
163 /* Loopback adapter address information (network byte order) */
164 #define LOOPBACK_ADDRESS_IPv4 ((IPv4_RAW_ADDRESS)DH2N(0x7F000001))
165 #define LOOPBACK_BCASTADDR_IPv4 ((IPv4_RAW_ADDRESS)DH2N(0x7F0000FF))
166 #define LOOPBACK_ADDRMASK_IPv4 ((IPv4_RAW_ADDRESS)DH2N(0xFFFFFF00))
167
168 /* Protocol definitions */
169 #define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
170 #define IPPROTO_IGMP 2 /* Internet Group Management Protocol */
171 #define IPPROTO_TCP 6 /* Transmission Control Protocol */
172 #define IPPROTO_UDP 17 /* User Datagram Protocol */
173
174 /* Timeout timer constants */
175 #define IP_TICKS_SECOND 2 /* Two ticks per second */
176 #define IP_TIMEOUT (1000 / IP_TICKS_SECOND) /* Timeout in milliseconds */
177
178
179 extern LIST_ENTRY InterfaceListHead;
180 extern KSPIN_LOCK InterfaceListLock;
181 extern LIST_ENTRY NetTableListHead;
182 extern KSPIN_LOCK NetTableListLock;
183 extern LIST_ENTRY PrefixListHead;
184 extern KSPIN_LOCK PrefixListLock;
185 extern UINT MaxLLHeaderSize;
186 extern UINT MinLLFrameSize;
187
188
189 PNET_TABLE_ENTRY IPCreateNTE(
190 PIP_INTERFACE IF,
191 PIP_ADDRESS Address,
192 UINT PrefixLength);
193
194 PIP_INTERFACE IPCreateInterface(
195 PLLIP_BIND_INFO BindInfo);
196
197 VOID IPDestroyInterface(
198 PIP_INTERFACE IF);
199
200 BOOLEAN IPRegisterInterface(
201 PIP_INTERFACE IF);
202
203 VOID IPUnregisterInterface(
204 PIP_INTERFACE IF);
205
206 PNET_TABLE_ENTRY IPLocateNTEOnInterface(
207 PIP_INTERFACE IF,
208 PIP_ADDRESS Address,
209 PUINT AddressType);
210
211 PNET_TABLE_ENTRY IPLocateNTE(
212 PIP_ADDRESS Address,
213 PUINT AddressType);
214
215 PADDRESS_ENTRY IPLocateADE(
216 PIP_ADDRESS Address,
217 UINT AddressType);
218
219 PADDRESS_ENTRY IPGetDefaultADE(
220 UINT AddressType);
221
222 VOID IPTimeout(
223 PKDPC Dpc,
224 PVOID DeferredContext,
225 PVOID SystemArgument1,
226 PVOID SystemArgument2);
227
228 VOID IPDispatchProtocol(
229 PNET_TABLE_ENTRY NTE,
230 PIP_PACKET IPPacket);
231
232 VOID IPRegisterProtocol(
233 UINT ProtocolNumber,
234 IP_PROTOCOL_HANDLER Handler);
235
236 NTSTATUS IPStartup(
237 PDRIVER_OBJECT DriverObject,
238 PUNICODE_STRING RegistryPath);
239
240 NTSTATUS IPShutdown(
241 VOID);
242
243 #endif /* __IP_H */
244
245 /* EOF */