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.

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