Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

653 lines
15 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. ipinip\ipinip.h
  5. Abstract:
  6. Main header file for the IP in IP encapsulation driver
  7. Revision History:
  8. --*/
  9. #ifndef __WANARP_WANARP_H__
  10. #define __WANARP_WANARP_H___
  11. //
  12. // Symbolic link into DOS space
  13. //
  14. #define WIN32_WANARP_SYMBOLIC_LINK L"\\DosDevices\\WanArp"
  15. //
  16. // ARP name (for IP). Also goes into LLInterface
  17. //
  18. #define WANARP_ARP_NAME WANARP_SERVICE_NAME_W
  19. //
  20. // Name for NDIS. NDIS requires us to have a TCPIP_ in front of the name
  21. //
  22. #define WANARP_NDIS_NAME L"TCPIP_WANARP"
  23. //
  24. // Need to prefix all our device names with this when we give name to
  25. // IP. We assume that the name we cat is of type \<Name>
  26. //
  27. #define TCPIP_IF_PREFIX L"\\DEVICE"
  28. //
  29. // The prefix for the registry key
  30. //
  31. #define TCPIP_REG_PREFIX L"\\Tcpip\\Parameters\\Interfaces\\"
  32. //
  33. // Length of a GUID
  34. //
  35. #define GUID_STR_LEN (38)
  36. //
  37. // We look like an 802.x ARP interface
  38. //
  39. #define ARP_802_ADDR_LENGTH 6
  40. //
  41. // Macro for building a 802.3 hw address given an index.
  42. // We do this since our adapters do not have a real net card associated with
  43. // them
  44. //
  45. #define HW_NAME_SEED "\0SExx\0"
  46. #define BuildHardwareAddrFromIndex(addr,index) { \
  47. RtlCopyMemory(addr, HW_NAME_SEED, 6); \
  48. addr[3] = (uchar) index >> 8; \
  49. addr[4] = (uchar) index; \
  50. }
  51. //
  52. // The default speed and MTU. We change the MTU when we get a better estimate
  53. // but the speed remains the same
  54. //
  55. #define WANARP_DEFAULT_MTU 1500
  56. #define WANARP_DEFAULT_SPEED 28000
  57. #define MEDIA_802_3 0 // index of media
  58. #define WANARP_LOOKAHEAD_SIZE 128 // A reasonable lookahead size
  59. #define MIN_ETYPE 0x600 // Minimum valid Ethertype
  60. #define ARP_ETYPE_IP 0x800 // Standard ETYPE
  61. //
  62. // Max number of packets a connection can have pending
  63. //
  64. #define WANARP_MAX_PENDING_PACKETS 32
  65. //
  66. // Initial size of packet pool
  67. //
  68. #define WAN_PACKET_POOL_COUNT 64
  69. //
  70. // Max outstanding packets:
  71. // Allow 64 connections to be maxed out pending, and 64 others to have
  72. // 2 packets outstanding
  73. //
  74. #define WAN_PACKET_POOL_OVERFLOW ((WANARP_MAX_PENDING_PACKETS * 64) + (2 * 64))
  75. #define CompareUnicodeStrings(S1,S2) \
  76. (((S1)->Length == (S2)->Length) && \
  77. (RtlCompareMemory((S1)->Buffer, \
  78. (S2)->Buffer, \
  79. (S2)->Length) == (S2)->Length))
  80. #define MIN(a,b) ((a) < (b)?a:b)
  81. //
  82. // Functions which are called at passive but acquire spinlocks
  83. //
  84. #define PASSIVE_ENTRY() PAGED_CODE()
  85. #define PASSIVE_EXIT() PAGED_CODE()
  86. //
  87. // Nifty macro for printing IP Addresses
  88. //
  89. #define PRINT_IPADDR(x) \
  90. ((x)&0x000000FF),(((x)&0x0000FF00)>>8),(((x)&0x00FF0000)>>16),(((x)&0xFF000000)>>24)
  91. //
  92. // 0.0.0.0 is an invalid address
  93. //
  94. #define INVALID_IP_ADDRESS 0x00000000
  95. #define IsUnicastAddr(X) ((DWORD)((X) & 0x000000F0) < (DWORD)(0x000000E0))
  96. #define IsClassDAddr(X) (((X) & 0x000000F0) == 0x000000E0)
  97. //
  98. // IPv4 header
  99. //
  100. #include <packon.h>
  101. typedef struct _IP_HEADER
  102. {
  103. BYTE byVerLen; // Version and length.
  104. BYTE byTos; // Type of service.
  105. WORD wLength; // Total length of datagram.
  106. WORD wId; // Identification.
  107. WORD wFlagOff; // Flags and fragment offset.
  108. BYTE byTtl; // Time to live.
  109. BYTE byProtocol; // Protocol.
  110. WORD wXSum; // Header checksum.
  111. DWORD dwSrc; // Source address.
  112. DWORD dwDest; // Destination address.
  113. }IP_HEADER, *PIP_HEADER;
  114. #define LengthOfIpHeader(X) (ULONG)((((X)->byVerLen) & 0x0F)<<2)
  115. typedef struct _ETH_HEADER
  116. {
  117. //
  118. // 6 byte destination address
  119. //
  120. BYTE rgbyDestAddr[ARP_802_ADDR_LENGTH];
  121. //
  122. // 6 byte source address
  123. //
  124. BYTE rgbySourceAddr[ARP_802_ADDR_LENGTH];
  125. //
  126. // 2 byte type
  127. //
  128. WORD wType;
  129. }ETH_HEADER, *PETH_HEADER;
  130. #include <packoff.h>
  131. #define CS_DISCONNECTING 0x00
  132. #define CS_CONNECTING 0x01
  133. #define CS_CONNECTED 0x02
  134. //
  135. // The CONN_ENTRY structure defines a connection. There is only one
  136. // CONN_ENTRY for each dial out or router connection. However, on the
  137. // server adapter, we can have multiple CONN_ENYTRYs - one for each dial
  138. // in client. The fields of a CONN_ENTRY are all READ-ONLY except
  139. // for ulSpeed, ulMtu, pAdapter and byState.
  140. // REFCOUNTS: A CONN_ENTRY is refcounted once when the connection is
  141. // created (LinkUp) and then once for every send. It is deref'ed
  142. // on every SendComplete and then again on CloseLink.
  143. //
  144. typedef struct _CONN_ENTRY
  145. {
  146. //
  147. // Back pointer to owning adapter
  148. //
  149. struct _ADAPTER *pAdapter;
  150. //
  151. // Connection information
  152. //
  153. DWORD dwLocalAddr;
  154. DWORD dwLocalMask;
  155. DWORD dwRemoteAddr;
  156. //
  157. // IP's context for this link. Only used for DU_CALLIN connections
  158. //
  159. PVOID pvIpLinkContext;
  160. //
  161. // Pointer to the lock to use when locking this connection.
  162. // For clients, this is a pointer to rlLock, while for others
  163. // this points to the adapter's lock entry
  164. //
  165. PRT_LOCK prlLock;
  166. //
  167. // The lock for this entry. Only used for DU_CALLIN
  168. //
  169. RT_LOCK rlLock;
  170. //
  171. // Refcount
  172. //
  173. LONG lRefCount;
  174. //
  175. // The MTU and speed for this connection
  176. //
  177. ULONG ulMtu;
  178. ULONG ulSpeed;
  179. //
  180. // The usage (CALLIN, CALLOUT or ROUTER) for the connection
  181. //
  182. DIAL_USAGE duUsage;
  183. //
  184. // The slot index in the connection table
  185. //
  186. ULONG ulSlotIndex;
  187. //
  188. // Precanned header for this connection
  189. //
  190. ETH_HEADER ehHeader;
  191. //
  192. // Flag which determines whether to filter netbios packets or not
  193. //
  194. BOOLEAN bFilterNetBios;
  195. BYTE byState;
  196. }CONN_ENTRY, *PCONN_ENTRY;
  197. #include "ref.h"
  198. #define InitConnEntryRefCount(p) InitStructureRefCount("ConnEntry", (p), 0)
  199. #define ReferenceConnEntry(p) ReferenceStructure("ConnEntry", (p))
  200. #define DereferenceConnEntry(p) DereferenceStructure("ConnEntry", (p) ,WanpDeleteConnEntry)
  201. #define AS_FREE 0x00
  202. #define AS_REMOVING 0x01
  203. #define AS_ADDING 0x02
  204. #define AS_ADDED 0x03
  205. #define AS_UNMAPPING 0x04
  206. #define AS_MAPPING 0x05
  207. #define AS_MAPPED 0x06
  208. //
  209. // There are two conditions where an adapter is in AS_MAPPED state yet has
  210. // no connection entry:
  211. // (i) A Server Adapter is mapped, but has a connection table
  212. // (ii) If a demand dial attempt finds an added adapter, it maps it but
  213. // doesnt create the CONN_ENTRY till LinkUp
  214. //
  215. //
  216. // REFCOUNTS: An ADAPTER is referenced once on creation since it lies on
  217. // a list and once when it added to IP. It is referenced when it
  218. // is mapped to an interface since the interface has a pointer to it.
  219. // It is also referenced once for each connection.
  220. // ADAPTERs are dereferenced when they are unmapped from an interface (at
  221. // linkdown or connection failure). They are deref'ed when a CONN_ENTRY
  222. // is finally cleaned out (not at linkdown - rather when the CONN_ENTRY's
  223. // ref goes to 0). They are deref'ed when they are removed from the
  224. // list to be deleted. We also deref them when we get a CloseAdapter from
  225. // IP
  226. //
  227. typedef struct _ADAPTER
  228. {
  229. //
  230. // Link in the list of adapters on this machine
  231. //
  232. LIST_ENTRY leAdapterLink;
  233. //
  234. // The connection entry for this adapter. Not used for the Server
  235. // Adapter since it has many connections on it
  236. //
  237. PCONN_ENTRY pConnEntry;
  238. //
  239. // Name of the binding
  240. //
  241. UNICODE_STRING usConfigKey;
  242. //
  243. // Name of the device
  244. //
  245. UNICODE_STRING usDeviceNameW;
  246. #if DBG
  247. //
  248. // Same thing, only in asciiz so that we can print it easily
  249. //
  250. ANSI_STRING asDeviceNameA;
  251. #endif
  252. //
  253. // Lock that protects the adapter
  254. //
  255. RT_LOCK rlLock;
  256. //
  257. // The reference count for the structure
  258. // Keep this and the lock together to make the cache happy
  259. //
  260. LONG lRefCount;
  261. //
  262. // The index given to us by IP
  263. //
  264. DWORD dwAdapterIndex;
  265. #if DBG
  266. DWORD dwRequestedIndex;
  267. #endif
  268. //
  269. // TDI entity magic
  270. //
  271. DWORD dwIfInstance;
  272. DWORD dwATInstance;
  273. //
  274. // The state of this adapter
  275. //
  276. BYTE byState;
  277. //
  278. // The Guid for the adapter
  279. //
  280. GUID Guid;
  281. //
  282. // IP's context for this adapter
  283. //
  284. PVOID pvIpContext;
  285. //
  286. // The interface that is adapter is mapped to
  287. //
  288. struct _UMODE_INTERFACE *pInterface;
  289. //
  290. // The pending packet queue length
  291. //
  292. ULONG ulQueueLen;
  293. //
  294. // Queue of pending packets
  295. //
  296. LIST_ENTRY lePendingPktList;
  297. //
  298. // Queue of header buffers for the pending packets
  299. //
  300. LIST_ENTRY lePendingHdrList;
  301. //
  302. // The next two members are used to synchronize state changes for
  303. // the adapter. There are two kinds of notifications needed. When a
  304. // thread is modifying the state using functions which are completed
  305. // asynchronously, it needs to wait for the completion routine to run
  306. // The completion routine uses the pkeChangeEvent to notify the original
  307. // thread.
  308. // Also while this change is in progress, other threads may be interested
  309. // in getting access to the data structure once the state has been
  310. // modified. They add WAN_EVENT_NODE to the EventList and the original
  311. // thread then goes about notifying each of the waiters
  312. //
  313. PKEVENT pkeChangeEvent;
  314. LIST_ENTRY leEventList;
  315. BYTE rgbyHardwareAddr[ARP_802_ADDR_LENGTH];
  316. }ADAPTER, *PADAPTER;
  317. #define InitAdapterRefCount(p) InitStructureRefCount("Adapter", (p), 1)
  318. #define ReferenceAdapter(p) ReferenceStructure("Adapter", (p))
  319. #define DereferenceAdapter(p) DereferenceStructure("Adapter", (p), WanpDeleteAdapter)
  320. typedef struct _UMODE_INTERFACE
  321. {
  322. //
  323. // Link on the list of interfaces
  324. //
  325. LIST_ENTRY leIfLink;
  326. //
  327. // Pointer to adapter when mapped
  328. //
  329. PADAPTER pAdapter;
  330. //
  331. // The (user mode) interface index
  332. //
  333. DWORD dwIfIndex;
  334. //
  335. // The reserved index for this interface
  336. //
  337. DWORD dwRsvdAdapterIndex;
  338. //
  339. // The lock for the interface
  340. //
  341. RT_LOCK rlLock;
  342. //
  343. // The reference count for the structure
  344. // Keep this and the lock together to make the cache happy
  345. //
  346. LONG lRefCount;
  347. //
  348. // The GUID for the interface. This is setup at add interface time
  349. // for router interfaces and at lineup for callouts
  350. //
  351. GUID Guid;
  352. //
  353. // The usage (CALLIN, CALLOUT or ROUTER)
  354. //
  355. DIAL_USAGE duUsage;
  356. //
  357. // Count of packets pending. Used to cap the max number of packets
  358. // copied when a connection is being brought up
  359. //
  360. ULONG ulPacketsPending;
  361. //
  362. // The admin and operational states.
  363. //
  364. DWORD dwAdminState;
  365. DWORD dwOperState;
  366. //
  367. // Last time the state changed. We dont do anything with this right now
  368. //
  369. DWORD dwLastChange;
  370. //
  371. // Sundry MIB-II statistics for the interface
  372. //
  373. ULONG ulInOctets;
  374. ULONG ulInUniPkts;
  375. ULONG ulInNonUniPkts;
  376. ULONG ulInDiscards;
  377. ULONG ulInErrors;
  378. ULONG ulInUnknownProto;
  379. ULONG ulOutOctets;
  380. ULONG ulOutUniPkts;
  381. ULONG ulOutNonUniPkts;
  382. ULONG ulOutDiscards;
  383. ULONG ulOutErrors;
  384. }UMODE_INTERFACE, *PUMODE_INTERFACE;
  385. #define InitInterfaceRefCount(p) InitStructureRefCount("Interface", (p), 1)
  386. #define ReferenceInterface(p) ReferenceStructure("Interface", (p))
  387. #define DereferenceInterface(p) DereferenceStructure("Interface", (p), WanpDeleteInterface)
  388. typedef struct _ADDRESS_CONTEXT
  389. {
  390. //
  391. // The next RCE in the chain
  392. //
  393. RouteCacheEntry *pNextRce;
  394. PCONN_ENTRY pOwningConn;
  395. }ADDRESS_CONTEXT, *PADDRESS_CONTEXT;
  396. //
  397. // Context for an asynchronous NdisRequest
  398. //
  399. typedef
  400. VOID
  401. (* PFNWANARP_REQUEST_COMPLETION_HANDLER)(
  402. NDIS_HANDLE nhHandle,
  403. struct _WANARP_NDIS_REQUEST_CONTEXT *pRequestContext,
  404. NDIS_STATUS nsStatus
  405. );
  406. #pragma warning(disable:4201)
  407. typedef struct _WANARP_NDIS_REQUEST_CONTEXT
  408. {
  409. //
  410. // The request sent to NDIS
  411. // Ndis returns a pointer to this in our completion routine; we
  412. // use CONTAINING_RECORD to get a pointer to the context structure
  413. //
  414. NDIS_REQUEST NdisRequest;
  415. //
  416. // The completion routine to call when NDIS is done processing the
  417. // request. If NULL, then we stop
  418. //
  419. PFNWANARP_REQUEST_COMPLETION_HANDLER pfnCompletionRoutine;
  420. union
  421. {
  422. BYTE rgbyProtocolId[ARP_802_ADDR_LENGTH];
  423. ULONG ulLookahead;
  424. ULONG ulPacketFilter;
  425. TRANSPORT_HEADER_OFFSET TransportHeaderOffset;
  426. };
  427. }WANARP_NDIS_REQUEST_CONTEXT, *PWANARP_NDIS_REQUEST_CONTEXT;
  428. #pragma warning(default:4201)
  429. //
  430. // Our resource (which doesnt allow recursive access)
  431. //
  432. typedef struct _WAN_RESOURCE
  433. {
  434. //
  435. // Number of people waiting on the resource ( + 1 if one is using
  436. // the resource)
  437. //
  438. LONG lWaitCount;
  439. KEVENT keEvent;
  440. }WAN_RESOURCE, *PWAN_RESOURCE;
  441. //
  442. // List of events
  443. //
  444. typedef struct _WAN_EVENT_NODE
  445. {
  446. LIST_ENTRY leEventLink;
  447. KEVENT keEvent;
  448. }WAN_EVENT_NODE, *PWAN_EVENT_NODE;
  449. //
  450. // Define alignment macros to align structure sizes and pointers up and down.
  451. //
  452. #define ALIGN_DOWN(length, type) \
  453. ((ULONG)(length) & ~(sizeof(type) - 1))
  454. #define ALIGN_UP(length, type) \
  455. (ALIGN_DOWN(((ULONG)(length) + sizeof(type) - 1), type))
  456. #define ALIGN_DOWN_POINTER(address, type) \
  457. ((PVOID)((ULONG_PTR)(address) & ~((ULONG_PTR)sizeof(type) - 1)))
  458. #define ALIGN_UP_POINTER(address, type) \
  459. (ALIGN_DOWN_POINTER(((ULONG_PTR)(address) + sizeof(type) - 1), type))
  460. #endif // __WANARP_WANARP_H__