Leaked source code of windows server 2003
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.

250 lines
12 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // This file contains the definitions of TDI address objects and related
  14. // constants and structures.
  15. //
  16. #define ao_signature 0x20204F41 // 'AO '.
  17. #define WILDCARD_PORT 0 // 0 means assign a port.
  18. #define MIN_USER_PORT 1025 // Minimum value for a wildcard port.
  19. #define MAX_USER_PORT 5000 // Maximim value for a user port.
  20. #define NUM_USER_PORTS (uint)(MaxUserPort - MIN_USER_PORT + 1)
  21. #define NETBT_SESSION_PORT 139
  22. typedef struct AddrObj AddrObj;
  23. // Datagram transport-specific send function.
  24. typedef void (*DGSendProc)(AddrObj *SrcAO, void *SendReq);
  25. //
  26. // Definition of the structure of an address object. Each object represents
  27. // a local address, and the IP portion may be a wildcard.
  28. //
  29. typedef struct AddrObj {
  30. #if DBG
  31. ulong ao_sig;
  32. #endif
  33. struct AddrObj *ao_next; // Next address object in chain.
  34. KSPIN_LOCK ao_lock; // Lock for this object.
  35. struct AORequest *ao_request; // Pointer to pending request.
  36. Queue ao_sendq; // Queue of sends waiting for transmit.
  37. Queue ao_pendq; // Linkage for pending queue.
  38. Queue ao_rcvq; // Receive queue.
  39. IPv6Addr ao_addr; // IP address for this address object.
  40. ulong ao_scope_id; // Scope ID of IP address (0 if none).
  41. ushort ao_port; // Local port for this address object.
  42. uchar ao_prot; // Protocol for this AO.
  43. uchar ao_index; // Index into table of this AO.
  44. ulong ao_flags; // Flags for this object.
  45. uint ao_listencnt; // Number of listening connections.
  46. ushort ao_usecnt; // Count of 'uses' on AO.
  47. ushort ao_inst; // 'Instance' number of this AO.
  48. int ao_ucast_hops; // Hop count for unicast packets.
  49. uint ao_mcast_if; // Our multicast source interface.
  50. int ao_mcast_hops; // Hop count for multicast packets.
  51. int ao_mcast_loop; // The multicast loopback state.
  52. int ao_protect; // Protection level.
  53. Queue ao_activeq; // Queue of active connections.
  54. Queue ao_idleq; // Queue of inactive (no TCB) connctns.
  55. Queue ao_listenq; // Queue of listening connections.
  56. WORK_QUEUE_ITEM ao_workitem; // Work-queue item to use for this AO.
  57. PConnectEvent ao_connect; // Connect event handle.
  58. PVOID ao_conncontext; // Receive DG context.
  59. PDisconnectEvent ao_disconnect; // Disconnect event routine.
  60. PVOID ao_disconncontext; // Disconnect event context.
  61. PErrorEvent ao_error; // Error event routine.
  62. PVOID ao_errcontext; // Error event context.
  63. PRcvEvent ao_rcv; // Receive event handler.
  64. PVOID ao_rcvcontext; // Receive context.
  65. PRcvDGEvent ao_rcvdg; // Receive DG event handler.
  66. PVOID ao_rcvdgcontext; // Receive DG context.
  67. PRcvEvent ao_exprcv; // Expedited receive event handler.
  68. PVOID ao_exprcvcontext; // Expedited receive context.
  69. struct AOMCastAddr *ao_mcastlist; // List of active multicast addresses.
  70. DGSendProc ao_dgsend; // Datagram transport send function.
  71. PTDI_IND_ERROR_EX ao_errorex; // Error event routine.
  72. PVOID ao_errorexcontext; // Error event context.
  73. ushort ao_maxdgsize; // maximum user datagram size.
  74. ushort ao_udp_cksum_cover; // UDP-Lite checksum coverage.
  75. ulong ao_owningpid; // Process ID of owner.
  76. uint* ao_iflist; // List of enabled interfaces.
  77. PSECURITY_DESCRIPTOR ao_sd; // SD for port-sharing access checks.
  78. } AddrObj;
  79. #define AO_SENTDATA_FLAG 0x00040000 // AO has had at least one send done.
  80. #define AO_RCV_HOPLIMIT_FLAG 0x00020000 // Receive HopLimit in aux data.
  81. // 0x00010000 Reserved for AO_WINSET_FLAG.
  82. // 0x00008000 Reserved for AO_SCALE_CWIN_FLAG.
  83. // 0x00004000 Reserved for AO_DEFERRED_FLAG.
  84. #define AO_PKTINFO_FLAG 0x00002000 // Packet info structure passed in
  85. // control info (WSARecvMsg).
  86. #define AO_SHARE_FLAG 0x00001000 // AddrObj can be shared.
  87. // 0x00000800 Reserved for AO_CONNUDP_FLAG.
  88. #define AO_HDRINCL_FLAG 0x00000400 // User provides IP header on raw send.
  89. #define AO_RAW_FLAG 0x00000200 // AO is for a raw endpoint.
  90. #define AO_DHCP_FLAG 0x00000100 // AO is bound to real 0 address.
  91. #define AO_VALID_FLAG 0x00000080 // AddrObj is valid.
  92. #define AO_BUSY_FLAG 0x00000040 // AddrObj is busy (i.e., has it
  93. // exclusive).
  94. #define AO_OOR_FLAG 0x00000020 // AddrObj is out of resources, and on
  95. // either the pending or delayed queue.
  96. #define AO_QUEUED_FLAG 0x00000010 // AddrObj is on the pending queue.
  97. // 0x00000008 Reserved for AO_XSUM_FLAG.
  98. #define AO_SEND_FLAG 0x00000004 // Send is pending.
  99. #define AO_OPTIONS_FLAG 0x00000002 // Option set pending.
  100. #define AO_DELETE_FLAG 0x00000001 // Delete pending.
  101. #define AO_VALID(A) ((A)->ao_flags & AO_VALID_FLAG)
  102. #define SET_AO_INVALID(A) (A)->ao_flags &= ~AO_VALID_FLAG
  103. #define AO_BUSY(A) ((A)->ao_flags & AO_BUSY_FLAG)
  104. #define SET_AO_BUSY(A) (A)->ao_flags |= AO_BUSY_FLAG
  105. #define CLEAR_AO_BUSY(A) (A)->ao_flags &= ~AO_BUSY_FLAG
  106. #define AO_OOR(A) ((A)->ao_flags & AO_OOR_FLAG)
  107. #define SET_AO_OOR(A) (A)->ao_flags |= AO_OOR_FLAG
  108. #define CLEAR_AO_OOR(A) (A)->ao_flags &= ~AO_OOR_FLAG
  109. #define AO_QUEUED(A) ((A)->ao_flags & AO_QUEUED_FLAG)
  110. #define SET_AO_QUEUED(A) (A)->ao_flags |= AO_QUEUED_FLAG
  111. #define CLEAR_AO_QUEUED(A) (A)->ao_flags &= ~AO_QUEUED_FLAG
  112. #define AO_REQUEST(A, f) ((A)->ao_flags & f##_FLAG)
  113. #define SET_AO_REQUEST(A, f) (A)->ao_flags |= f##_FLAG
  114. #define CLEAR_AO_REQUEST(A, f) (A)->ao_flags &= ~f##_FLAG
  115. #define AO_PENDING(A) \
  116. ((A)->ao_flags & (AO_DELETE_FLAG | AO_OPTIONS_FLAG | AO_SEND_FLAG))
  117. #define AO_SHARE(A) ((A)->ao_flags & AO_SHARE_FLAG)
  118. #define SET_AO_SHARE(A) (A)->ao_flags |= AO_SHARE_FLAG
  119. #define CLEAR_AO_SHARE(A) (A)->ao_flags &= ~AO_SHARE_FLAG
  120. #define AO_HDRINCL(A) ((A)->ao_flags & AO_HDRINCL_FLAG)
  121. #define SET_AO_HDRINCL(A) (A)->ao_flags |= AO_HDRINCL_FLAG
  122. #define CLEAR_AO_HDRINCL(A) (A)->ao_flags &= ~AO_HDRINCL_FLAG
  123. #define AO_PKTINFO(A) ((A)->ao_flags & AO_PKTINFO_FLAG)
  124. #define SET_AO_PKTINFO(A) (A)->ao_flags |= AO_PKTINFO_FLAG
  125. #define CLEAR_AO_PKTINFO(A) (A)->ao_flags &= ~AO_PKTINFO_FLAG
  126. #define AO_RCV_HOPLIMIT(A) ((A)->ao_flags & AO_RCV_HOPLIMIT_FLAG)
  127. #define SET_AO_RCV_HOPLIMIT(A) (A)->ao_flags |= AO_RCV_HOPLIMIT_FLAG
  128. #define CLEAR_AO_RCV_HOPLIMIT(A) (A)->ao_flags &= ~AO_RCV_HOPLIMIT_FLAG
  129. #define AO_SENTDATA(A) ((A)->ao_flags & AO_SENTDATA_FLAG)
  130. #define SET_AO_SENTDATA(A) (A)->ao_flags |= AO_SENTDATA_FLAG
  131. #define PROTECTION_LEVEL_UNRESTRICTED 10 // For peer-to-peer apps.
  132. #define PROTECTION_LEVEL_DEFAULT 20 // Default level.
  133. #define PROTECTION_LEVEL_RESTRICTED 30 // For Intranet apps.
  134. //
  135. // Definition of an address object search context. This is a data structure
  136. // used when the address object table is to be read sequentially.
  137. //
  138. typedef struct AOSearchContext {
  139. AddrObj *asc_previous; // Previous AO found.
  140. IPv6Addr asc_local_addr; // Local IP address to be found.
  141. IPv6Addr asc_remote_addr; // Remote IP address to check against.
  142. Interface *asc_interface; // Interface to check against.
  143. uint asc_scope_id; // Scope id for IP address.
  144. ushort asc_port; // Port to be found.
  145. uchar asc_prot; // Protocol.
  146. uchar asc_pad; // Pad to dword boundary.
  147. } AOSearchContext;
  148. //
  149. // Definition of an AO request structure. There structures are used only for
  150. // queuing delete and option set requests.
  151. //
  152. #define aor_signature 0x20524F41
  153. typedef struct AORequest {
  154. #if DBG
  155. ulong aor_sig;
  156. #endif
  157. struct AORequest *aor_next; // Next pointer in chain.
  158. uint aor_id; // ID for the request.
  159. uint aor_length; // Length of buffer.
  160. void *aor_buffer; // Buffer for this request.
  161. RequestCompleteRoutine aor_rtn; // Complete routine for this request.
  162. PVOID aor_context; // Request context;
  163. } AORequest;
  164. typedef struct AOMCastAddr {
  165. struct AOMCastAddr *ama_next; // Next in list.
  166. IPv6Addr ama_addr; // The address.
  167. uint ama_if; // The interface.
  168. } AOMCastAddr;
  169. //
  170. // External declarations for exported functions.
  171. //
  172. extern uint AddrObjTableSize;
  173. extern AddrObj **AddrObjTable;
  174. extern AddrObj *GetAddrObj(IPv6Addr *LocalAddr, IPv6Addr *RemoteAddr,
  175. uint LocalScopeId,
  176. ushort LocalPort, uchar Prot, AddrObj *PreviousAO,
  177. Interface* IF);
  178. extern AddrObj *GetNextAddrObj(AOSearchContext *SearchContext);
  179. extern AddrObj *GetFirstAddrObj(IPv6Addr *LocalAddr, IPv6Addr *RemoteAddr,
  180. uint LocalScopeId,
  181. ushort LocalPort, uchar Prot, Interface *IF,
  182. AOSearchContext *SearchContext);
  183. extern TDI_STATUS TdiOpenAddress(PTDI_REQUEST Request,
  184. TRANSPORT_ADDRESS UNALIGNED *AddrList,
  185. uint Protocol, void *Reuse,
  186. PSECURITY_DESCRIPTOR AddrSD);
  187. extern TDI_STATUS TdiCloseAddress(PTDI_REQUEST Request);
  188. extern TDI_STATUS SetAddrOptions(PTDI_REQUEST Request, uint ID, uint OptLength,
  189. void *Options);
  190. extern TDI_STATUS TdiSetEvent(PVOID Handle, int Type, PVOID Handler,
  191. PVOID Context);
  192. extern uchar GetAddress(TRANSPORT_ADDRESS UNALIGNED *AddrList,
  193. IPv6Addr *Addr, ulong *ScopeId, ushort *Port);
  194. extern int InitAddr(void);
  195. extern void AddrUnload(void);
  196. extern void ProcessAORequests(AddrObj *RequestAO);
  197. extern void DelayDerefAO(AddrObj *RequestAO);
  198. extern void DerefAO(AddrObj *RequestAO);
  199. extern void FreeAORequest(AORequest *FreedRequest);
  200. extern uint ValidateAOContext(void *Context, uint *Valid);
  201. extern uint ReadNextAO(void *Context, void *OutBuf);
  202. extern void InvalidateAddrs(IPv6Addr *Addr, uint ScopeId);
  203. extern uint MCastAddrOnAO(AddrObj *AO, IPv6Addr *Addr);
  204. extern AOMCastAddr *FindAOMCastAddr(AddrObj *AO, IPv6Addr *Addr, uint IFNo, AOMCastAddr **PrevAMA, BOOLEAN Loose);
  205. extern int DoesAOAllowPacket(AddrObj *RcvAO, Interface *IF, IPv6Addr *RemoteAddr);
  206. #define GetBestAddrObj(localaddr, remoteaddr, scope, port, prot, if) \
  207. GetAddrObj(localaddr, remoteaddr, scope, port, prot, NULL, if)
  208. #define REF_AO(a) (a)->ao_usecnt++
  209. #define DELAY_DEREF_AO(a) DelayDerefAO((a))
  210. #define DEREF_AO(a) DerefAO((a))
  211. #define LOCKED_DELAY_DEREF_AO(a) (a)->ao_usecnt--; \
  212. \
  213. if (!(a)->ao_usecnt && !AO_BUSY((a)) && AO_PENDING((a))) { \
  214. SET_AO_BUSY((a)); \
  215. ExQueueWorkItem(&(a)->ao_workitem, CriticalWorkQueue); \
  216. }