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.

337 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. routing\netsh\ip\ipmon\ipmon.c
  5. Abstract:
  6. IP Command dispatcher.
  7. Revision History:
  8. Anand Mahalingam 7/10/98 Created
  9. --*/
  10. #include "precomp.h"
  11. const GUID g_IpGuid = IPMONTR_GUID;
  12. #define IP_HELPER_VERSION 1
  13. DWORD g_dwNumTableEntries = 0; // 6;
  14. //
  15. // The table of Add, Delete, Set and Show Commands for IP RTR MGR
  16. // To add a command to one of the command groups, just add the
  17. // CMD_ENTRY to the correct table. To add a new cmd group, create its
  18. // cmd table and then add the group entry to group table
  19. //
  20. //
  21. // The commands are prefix-matched with the command-line, in sequential
  22. // order. So a command like 'ADD INTERFACE FILTER' must come before
  23. // the command 'ADD INTERFACE' in the table. Likewise,
  24. // a command like 'ADD ROUTE' must come before the command
  25. // 'ADD ROUTEXXXX' in the table.
  26. //
  27. CMD_ENTRY g_IpAddCmdTable[] = {
  28. CREATE_CMD_ENTRY(IP_ADD_BOUNDARY, HandleIpAddBoundary),
  29. CREATE_CMD_ENTRY(IP_ADD_IF_FILTER, HandleIpAddIfFilter),
  30. CREATE_CMD_ENTRY(IP_ADD_INTERFACE, HandleIpAddInterface),
  31. #ifdef KSL_IPINIP
  32. //CREATE_CMD_ENTRY(IP_ADD_IPIPTUNNEL, HandleIpAddIpIpTunnel),
  33. #endif //KSL_IPINIP
  34. CREATE_CMD_ENTRY(IP_ADD_PROTOPREF, HandleIpAddRoutePref),
  35. CREATE_CMD_ENTRY(IP_ADD_SCOPE, HandleIpAddScope),
  36. CREATE_CMD_ENTRY(IP_ADD_RTMROUTE, HandleIpAddRtmRoute),
  37. CREATE_CMD_ENTRY(IP_ADD_PERSISTENTROUTE,HandleIpAddPersistentRoute),
  38. };
  39. CMD_ENTRY g_IpDelCmdTable[] = {
  40. CREATE_CMD_ENTRY(IP_DEL_BOUNDARY, HandleIpDelBoundary),
  41. CREATE_CMD_ENTRY(IP_DEL_IF_FILTER, HandleIpDelIfFilter),
  42. CREATE_CMD_ENTRY(IP_DEL_INTERFACE, HandleIpDelInterface),
  43. CREATE_CMD_ENTRY(IP_DEL_PROTOPREF, HandleIpDelRoutePref),
  44. CREATE_CMD_ENTRY(IP_DEL_SCOPE, HandleIpDelScope),
  45. CREATE_CMD_ENTRY(IP_DEL_RTMROUTE, HandleIpDelRtmRoute),
  46. CREATE_CMD_ENTRY(IP_DEL_PERSISTENTROUTE,HandleIpDelPersistentRoute),
  47. };
  48. CMD_ENTRY g_IpSetCmdTable[] = {
  49. CREATE_CMD_ENTRY(IP_SET_IF_FILTER, HandleIpSetIfFilter),
  50. CREATE_CMD_ENTRY(IP_SET_INTERFACE, HandleIpSetInterface),
  51. #ifdef KSL_IPINIP
  52. //CREATE_CMD_ENTRY(IP_SET_IPIPTUNNEL, HandleIpSetIpIpTunnel),
  53. #endif //KSL_IPINIP
  54. CREATE_CMD_ENTRY(IP_SET_LOGLEVEL, HandleIpSetLogLevel),
  55. CREATE_CMD_ENTRY(IP_SET_PROTOPREF, HandleIpSetRoutePref),
  56. CREATE_CMD_ENTRY(IP_SET_SCOPE, HandleIpSetScope),
  57. CREATE_CMD_ENTRY(IP_SET_RTMROUTE, HandleIpSetRtmRoute),
  58. CREATE_CMD_ENTRY(IP_SET_PERSISTENTROUTE,HandleIpSetPersistentRoute)
  59. };
  60. CMD_ENTRY g_IpShowCmdTable[] = {
  61. CREATE_CMD_ENTRY(IP_SHOW_BOUNDARY, HandleIpShowBoundary),
  62. CREATE_CMD_ENTRY(IPMIB_SHOW_BOUNDARY, HandleIpMibShowObject),
  63. CREATE_CMD_ENTRY(IPMIB_SHOW_RTMDEST, HandleIpShowRtmDestinations),
  64. CREATE_CMD_ENTRY(IP_SHOW_IF_FILTER, HandleIpShowIfFilter),
  65. CREATE_CMD_ENTRY(IP_SHOW_INTERFACE, HandleIpShowInterface),
  66. // CREATE_CMD_ENTRY(IPMIB_SHOW_IPFORWARD, HandleIpMibShowObject),
  67. CREATE_CMD_ENTRY(IP_SHOW_LOGLEVEL, HandleIpShowLogLevel),
  68. CREATE_CMD_ENTRY(IPMIB_SHOW_MFE, HandleIpMibShowObject),
  69. CREATE_CMD_ENTRY(IPMIB_SHOW_MFESTATS, HandleIpMibShowObject),
  70. CREATE_CMD_ENTRY(IP_SHOW_PROTOCOL, HandleIpShowProtocol),
  71. CREATE_CMD_ENTRY(IPMIB_SHOW_RTMROUTE, HandleIpShowRtmRoutes),
  72. CREATE_CMD_ENTRY(IP_SHOW_PROTOPREF, HandleIpShowRoutePref),
  73. CREATE_CMD_ENTRY(IP_SHOW_SCOPE, HandleIpShowScope),
  74. CREATE_CMD_ENTRY(IP_SHOW_PERSISTENTROUTE,HandleIpShowPersistentRoute)
  75. };
  76. CMD_GROUP_ENTRY g_IpCmdGroups[] =
  77. {
  78. CREATE_CMD_GROUP_ENTRY(GROUP_ADD, g_IpAddCmdTable),
  79. CREATE_CMD_GROUP_ENTRY(GROUP_DELETE, g_IpDelCmdTable),
  80. CREATE_CMD_GROUP_ENTRY(GROUP_SET, g_IpSetCmdTable),
  81. CREATE_CMD_GROUP_ENTRY(GROUP_SHOW, g_IpShowCmdTable),
  82. };
  83. ULONG g_ulNumGroups = sizeof(g_IpCmdGroups)/sizeof(CMD_GROUP_ENTRY);
  84. CMD_ENTRY g_IpCmds[] =
  85. {
  86. // CREATE_CMD_ENTRY(IP_INSTALL, HandleIpInstall),
  87. CREATE_CMD_ENTRY(IP_RESET, HandleIpReset),
  88. CREATE_CMD_ENTRY_EX(IP_UPDATE, HandleIpUpdate, 0),
  89. // CREATE_CMD_ENTRY(IP_UNINSTALL, HandleIpUninstall),
  90. };
  91. ULONG g_ulNumTopCmds = sizeof(g_IpCmds)/sizeof(CMD_ENTRY);
  92. BOOL g_bIpDirty = FALSE;
  93. HANDLE g_hModule;
  94. HANDLE g_hMprConfig = NULL;
  95. HANDLE g_hMprAdmin = NULL;
  96. HANDLE g_hMIBServer = NULL;
  97. BOOL g_bCommit;
  98. DWORD g_dwNumTableEntries;
  99. TRANSPORT_INFO g_tiTransport;
  100. LIST_ENTRY g_leIfListHead;
  101. NS_CONTEXT_CONNECT_FN IpConnect;
  102. ULONG g_ulInitCount;
  103. DWORD
  104. WINAPI
  105. IpUnInit(
  106. IN DWORD dwReserved
  107. );
  108. static DWORD ParentVersion = 0;
  109. DWORD
  110. WINAPI
  111. IpStartHelper(
  112. IN CONST GUID *pguidParent,
  113. IN DWORD dwVersion
  114. )
  115. {
  116. DWORD dwErr;
  117. NS_CONTEXT_ATTRIBUTES attMyAttributes;
  118. PNS_PRIV_CONTEXT_ATTRIBUTES pNsPrivContextAttributes;
  119. pNsPrivContextAttributes = MALLOC(sizeof(NS_PRIV_CONTEXT_ATTRIBUTES));
  120. if (!pNsPrivContextAttributes)
  121. {
  122. return ERROR_NOT_ENOUGH_MEMORY;
  123. }
  124. ParentVersion = dwVersion;
  125. ZeroMemory(&attMyAttributes, sizeof(attMyAttributes));
  126. ZeroMemory(pNsPrivContextAttributes, sizeof(NS_PRIV_CONTEXT_ATTRIBUTES));
  127. attMyAttributes.pwszContext = L"ip";
  128. attMyAttributes.guidHelper = g_IpGuid;
  129. attMyAttributes.dwVersion = 1;
  130. attMyAttributes.dwFlags = 0;
  131. attMyAttributes.ulNumTopCmds = g_ulNumTopCmds;
  132. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_IpCmds;
  133. attMyAttributes.ulNumGroups = g_ulNumGroups;
  134. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_IpCmdGroups;
  135. attMyAttributes.pfnCommitFn = IpCommit;
  136. attMyAttributes.pfnDumpFn = IpDump;
  137. attMyAttributes.pfnConnectFn = IpConnect;
  138. pNsPrivContextAttributes->pfnEntryFn = NULL;
  139. pNsPrivContextAttributes->pfnSubEntryFn = IpSubEntry;
  140. attMyAttributes.pReserved = pNsPrivContextAttributes;
  141. dwErr = RegisterContext( &attMyAttributes );
  142. return dwErr;
  143. }
  144. DWORD WINAPI
  145. IpSubEntry(
  146. IN const NS_CONTEXT_ATTRIBUTES *pSubContext,
  147. IN LPCWSTR pwszMachine,
  148. IN OUT LPWSTR *ppwcArguments,
  149. IN DWORD dwArgCount,
  150. IN DWORD dwFlags,
  151. IN LPCVOID pvData,
  152. OUT LPWSTR pwcNewContext
  153. )
  154. {
  155. PNS_PRIV_CONTEXT_ATTRIBUTES pNsPrivContextAttributes = pSubContext->pReserved;
  156. if ( (!pNsPrivContextAttributes) || (!pNsPrivContextAttributes->pfnEntryFn) )
  157. {
  158. return GenericMonitor(pSubContext,
  159. pwszMachine,
  160. ppwcArguments,
  161. dwArgCount,
  162. dwFlags,
  163. g_hMIBServer,
  164. pwcNewContext );
  165. }
  166. return (*pNsPrivContextAttributes->pfnEntryFn)( pwszMachine,
  167. ppwcArguments,
  168. dwArgCount,
  169. dwFlags,
  170. g_hMIBServer,
  171. pwcNewContext );
  172. }
  173. DWORD
  174. WINAPI
  175. InitHelperDll(
  176. IN DWORD dwNetshVersion,
  177. OUT PNS_DLL_ATTRIBUTES pDllTable
  178. )
  179. {
  180. WORD wVersion = MAKEWORD(1,1);
  181. WSADATA wsaData;
  182. DWORD dwErr;
  183. NS_HELPER_ATTRIBUTES attMyAttributes;
  184. //
  185. // See if this is the first time we are being called
  186. //
  187. if(InterlockedIncrement(&g_ulInitCount) != 1)
  188. {
  189. return NO_ERROR;
  190. }
  191. dwErr = WSAStartup(wVersion,&wsaData);
  192. if(dwErr isnot NO_ERROR)
  193. {
  194. return dwErr;
  195. }
  196. //
  197. // Initialize interface list and the Transport Info Block
  198. //
  199. InitializeListHead(&g_leIfListHead);
  200. g_tiTransport.bValid = FALSE;
  201. g_tiTransport.pibhInfo = NULL;
  202. g_bCommit = TRUE;
  203. pDllTable->dwVersion = NETSH_VERSION_50;
  204. pDllTable->pfnStopFn = StopHelperDll;
  205. // Register helpers
  206. // We have 2 helpers (ROUTING, IP)
  207. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  208. attMyAttributes.guidHelper = g_RoutingGuid;
  209. attMyAttributes.dwVersion = IP_HELPER_VERSION;
  210. attMyAttributes.pfnStart = RoutingStartHelper;
  211. attMyAttributes.pfnStop = NULL;
  212. RegisterHelper( &g_NetshGuid, &attMyAttributes );
  213. attMyAttributes.guidHelper = g_IpGuid;
  214. attMyAttributes.dwVersion = IP_HELPER_VERSION;
  215. attMyAttributes.pfnStart = IpStartHelper;
  216. attMyAttributes.pfnStop = NULL;
  217. RegisterHelper( &g_RoutingGuid, &attMyAttributes );
  218. return NO_ERROR;
  219. }
  220. DWORD
  221. WINAPI
  222. StopHelperDll(
  223. IN DWORD dwReserved
  224. )
  225. {
  226. if(InterlockedDecrement(&g_ulInitCount) isnot 0)
  227. {
  228. return NO_ERROR;
  229. }
  230. IpCommit(NETSH_FLUSH);
  231. return NO_ERROR;
  232. }
  233. BOOL
  234. WINAPI
  235. IpDllEntry(
  236. HINSTANCE hInstDll,
  237. DWORD fdwReason,
  238. LPVOID pReserved
  239. )
  240. {
  241. switch (fdwReason)
  242. {
  243. case DLL_PROCESS_ATTACH:
  244. {
  245. g_hModule = hInstDll;
  246. DisableThreadLibraryCalls(hInstDll);
  247. break;
  248. }
  249. case DLL_PROCESS_DETACH:
  250. {
  251. break;
  252. }
  253. default:
  254. {
  255. break;
  256. }
  257. }
  258. return TRUE;
  259. }
  260. DWORD WINAPI
  261. IpConnect(
  262. IN LPCWSTR pwszRouter
  263. )
  264. {
  265. // If context info is dirty, reregister it
  266. if (g_bIpDirty)
  267. {
  268. IpStartHelper(NULL, ParentVersion);
  269. }
  270. return NO_ERROR;
  271. }