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.

510 lines
10 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. routing\ip\rtrmgr\close.c
  5. Abstract:
  6. Function related to shutdown
  7. Revision History:
  8. Gurdeep Singh Pall 6/14/95 Created
  9. --*/
  10. #include "allinc.h"
  11. VOID
  12. ReinstallOldRoutes(
  13. );
  14. VOID
  15. RouterManagerCleanup(
  16. VOID
  17. )
  18. /*++
  19. Routine Description
  20. The main cleanup function
  21. Locks
  22. None
  23. Arguments
  24. None
  25. Return Value
  26. None
  27. --*/
  28. {
  29. HANDLE hRtmHandle;
  30. DWORD i;
  31. TraceEnter("RouterManagerCleanup");
  32. DeleteAllInterfaces();
  33. UnloadRoutingProtocols();
  34. UnInitHashTables();
  35. CloseIPDriver();
  36. CloseMcastDriver();
  37. MIBCleanup();
  38. #ifdef KSL_IPINIP
  39. CloseIpIpKey();
  40. #endif //KSL_IPINIP
  41. if (!RouterRoleLanOnly)
  42. {
  43. //
  44. // WAN related cleanups
  45. //
  46. CloseWanArp() ;
  47. if (g_bEnableNetbtBcastFrowarding)
  48. {
  49. RestoreNetbtBcastForwardingMode();
  50. g_bEnableNetbtBcastFrowarding = FALSE;
  51. }
  52. }
  53. if(g_hMprConfig isnot NULL)
  54. {
  55. MprConfigServerDisconnect(g_hMprConfig);
  56. g_hMprConfig = NULL;
  57. }
  58. MgmDeInitialize ();
  59. if (g_hNotification isnot NULL)
  60. {
  61. RtmDeregisterFromChangeNotification(g_hLocalRoute,
  62. g_hNotification);
  63. g_hNotification = NULL;
  64. }
  65. if (g_hDefaultRouteNotification isnot NULL)
  66. {
  67. RtmDeregisterFromChangeNotification(g_hNetMgmtRoute,
  68. g_hDefaultRouteNotification);
  69. g_hDefaultRouteNotification = NULL;
  70. }
  71. // Cleanup and deregister all RTM registrations
  72. for(i = 0;
  73. i < sizeof(g_rgRtmHandles)/sizeof(RTM_HANDLE_INFO);
  74. i++)
  75. {
  76. hRtmHandle = g_rgRtmHandles[i].hRouteHandle;
  77. if (hRtmHandle isnot NULL)
  78. {
  79. // Delete all routes added by this regn
  80. DeleteRtmRoutes(hRtmHandle, 0, TRUE);
  81. // Delete all nexthops added by this regn
  82. DeleteRtmNexthops(hRtmHandle, 0, TRUE);
  83. // Deregister this registration from RTM
  84. RtmDeregisterEntity(hRtmHandle);
  85. g_rgRtmHandles[i].dwProtoId = 0;
  86. g_rgRtmHandles[i].hRouteHandle = NULL;
  87. }
  88. }
  89. // Null out the aliases to the above regn handles
  90. g_hLocalRoute = NULL;
  91. g_hAutoStaticRoute = NULL;
  92. g_hStaticRoute = NULL;
  93. g_hNonDodRoute = NULL;
  94. g_hNetMgmtRoute = NULL;
  95. //
  96. // When last entity deregisters, the route table is automatically deleted
  97. //
  98. //
  99. // Close handles used for notification
  100. //
  101. if(g_hDemandDialEvent isnot NULL)
  102. {
  103. CloseHandle(g_hDemandDialEvent) ;
  104. g_hDemandDialEvent = NULL;
  105. }
  106. #ifdef KSL_IPINIP
  107. if(g_hIpInIpEvent isnot NULL)
  108. {
  109. CloseHandle(g_hIpInIpEvent);
  110. g_hIpInIpEvent = NULL;
  111. }
  112. #endif //KSL_IPINIP
  113. if(g_hSetForwardingEvent isnot NULL)
  114. {
  115. CloseHandle(g_hSetForwardingEvent);
  116. g_hSetForwardingEvent = NULL;
  117. }
  118. if(g_hForwardingChangeEvent isnot NULL)
  119. {
  120. CloseHandle(g_hForwardingChangeEvent);
  121. g_hForwardingChangeEvent = NULL;
  122. }
  123. if(g_hStackChangeEvent isnot NULL)
  124. {
  125. CloseHandle(g_hStackChangeEvent);
  126. g_hStackChangeEvent = NULL;
  127. }
  128. if(g_hRoutingProtocolEvent isnot NULL)
  129. {
  130. CloseHandle(g_hRoutingProtocolEvent) ;
  131. g_hRoutingProtocolEvent = NULL;
  132. }
  133. if(g_hStopRouterEvent isnot NULL)
  134. {
  135. CloseHandle(g_hStopRouterEvent) ;
  136. g_hStopRouterEvent = NULL;
  137. }
  138. if(g_hRtrDiscSocketEvent isnot NULL)
  139. {
  140. CloseHandle(g_hRtrDiscSocketEvent);
  141. g_hRtrDiscSocketEvent = NULL;
  142. }
  143. if(g_hMcMiscSocketEvent isnot NULL)
  144. {
  145. CloseHandle(g_hMcMiscSocketEvent);
  146. g_hMcMiscSocketEvent = NULL;
  147. }
  148. if(g_hRtrDiscTimer isnot NULL)
  149. {
  150. CloseHandle(g_hRtrDiscTimer);
  151. g_hRtrDiscTimer = NULL;
  152. }
  153. for(i = 0; i < NUM_MCAST_IRPS; i++)
  154. {
  155. if(g_hMcastEvents[i] isnot NULL)
  156. {
  157. CloseHandle(g_hMcastEvents[i]);
  158. g_hMcastEvents[i] = NULL;
  159. }
  160. }
  161. for(i = 0; i < NUM_ROUTE_CHANGE_IRPS; i++)
  162. {
  163. if(g_hRouteChangeEvents[i] isnot NULL)
  164. {
  165. CloseHandle(g_hRouteChangeEvents[i]);
  166. g_hRouteChangeEvents[i] = NULL;
  167. }
  168. }
  169. if(WSACleanup() isnot NO_ERROR)
  170. {
  171. Trace1(ERR,
  172. "RouterManagerCleanup: WSACleanup returned %d",
  173. WSAGetLastError());
  174. }
  175. for(i = 0; i < NUM_LOCKS; i++)
  176. {
  177. RtlDeleteResource(&g_LockTable[i]);
  178. }
  179. //
  180. // reinstall all the routes that existed before we started. The memory
  181. // was from route heap so will get freed
  182. //
  183. if(!IsListEmpty(&g_leStackRoutesToRestore))
  184. {
  185. Sleep(0);
  186. Trace0(GLOBAL, "Reinstalling stack routes");
  187. ReinstallOldRoutes();
  188. }
  189. //
  190. // This cleans out the interface structures, since they are all
  191. // allocated from this heap
  192. //
  193. if(IPRouterHeap isnot NULL)
  194. {
  195. HeapDestroy (IPRouterHeap) ;
  196. IPRouterHeap = NULL;
  197. }
  198. Trace0(GLOBAL, "IP Router Manager cleanup done");
  199. TraceLeave("RouterManagerCleanup");
  200. TraceDeregister (TraceHandle) ;
  201. }
  202. VOID
  203. ReinstallOldRoutes(
  204. )
  205. {
  206. DWORD dwResult;
  207. PROUTE_LIST_ENTRY prl;
  208. TraceEnter("ReinstallOldRoutes");
  209. while (!IsListEmpty(&g_leStackRoutesToRestore))
  210. {
  211. prl = (PROUTE_LIST_ENTRY) RemoveHeadList(
  212. &g_leStackRoutesToRestore
  213. );
  214. Trace2(
  215. ROUTE, "%d.%d.%d.%d/%d.%d.%d.%d",
  216. PRINT_IPADDR( prl->mibRoute.dwForwardDest ),
  217. PRINT_IPADDR( prl->mibRoute.dwForwardMask )
  218. );
  219. dwResult = SetIpForwardEntryToStack(&(prl->mibRoute));
  220. if (dwResult isnot NO_ERROR)
  221. {
  222. Trace2(ERR,
  223. "ReinstallOldRoutes: Failed to add route to %x from "
  224. " init table. Error %x",
  225. prl->mibRoute.dwForwardDest,
  226. dwResult);
  227. }
  228. }
  229. TraceLeave("ReinstallOldRoutes");
  230. }
  231. VOID
  232. MIBCleanup(
  233. VOID
  234. )
  235. {
  236. TraceEnter("MIBCleanup");
  237. if(g_hIfHeap isnot NULL)
  238. {
  239. HeapDestroy(g_hIfHeap);
  240. g_hIfHeap = NULL;
  241. }
  242. if(g_hUdpHeap isnot NULL)
  243. {
  244. HeapDestroy(g_hUdpHeap);
  245. g_hUdpHeap = NULL;
  246. }
  247. if(g_hIpAddrHeap isnot NULL)
  248. {
  249. HeapDestroy(g_hIpAddrHeap);
  250. g_hIpAddrHeap = NULL;
  251. }
  252. if(g_hIpForwardHeap isnot NULL)
  253. {
  254. HeapDestroy(g_hIpForwardHeap);
  255. g_hIpForwardHeap = NULL;
  256. }
  257. if(g_hIpNetHeap isnot NULL)
  258. {
  259. HeapDestroy(g_hIpNetHeap);
  260. g_hIpNetHeap = NULL;
  261. }
  262. TraceLeave("MIBCleanup");
  263. }
  264. //* UnloadRoutingProtocols()
  265. //
  266. // Function: 1. Calls stopprotocol for each routing protocol
  267. // 2. Waits for protocols to stop
  268. // 3. Unloads the routing protocol dlls.
  269. //
  270. // Returns: Nothing.
  271. //*
  272. VOID
  273. UnloadRoutingProtocols()
  274. {
  275. PLIST_ENTRY currentlist ;
  276. PPROTO_CB protptr ;
  277. TraceEnter("UnloadRoutingProtocols");
  278. while (!IsListEmpty(&g_leProtoCbList))
  279. {
  280. currentlist = RemoveHeadList(&g_leProtoCbList);
  281. protptr = CONTAINING_RECORD (currentlist, PROTO_CB, leList) ;
  282. //
  283. // relenquish CPU to enable DLL threads to finish
  284. //
  285. Sleep(0);
  286. FreeLibrary (protptr->hiHInstance) ; // unload dll
  287. HeapFree (IPRouterHeap, 0, protptr) ; // free cb
  288. }
  289. TraceLeave("UnloadRoutingProtocols");
  290. }
  291. VOID
  292. CloseIPDriver(
  293. VOID
  294. )
  295. {
  296. TraceEnter("CloseIPDriver");
  297. if(g_hIpDevice isnot NULL)
  298. {
  299. CloseHandle(g_hIpDevice) ;
  300. }
  301. if (g_hIpRouteChangeDevice isnot NULL)
  302. {
  303. CloseHandle(g_hIpRouteChangeDevice);
  304. }
  305. TraceLeave("CloseIPDriver");
  306. }
  307. VOID
  308. CloseMcastDriver(
  309. VOID
  310. )
  311. {
  312. TraceEnter("CloseMcastDriver");
  313. if(g_hMcastDevice isnot NULL)
  314. {
  315. CloseHandle(g_hMcastDevice);
  316. }
  317. TraceLeave("CloseMcastDriver");
  318. }
  319. DWORD
  320. StopDriverAndCloseHandle(
  321. PCHAR pszServiceName,
  322. HANDLE hDevice
  323. )
  324. {
  325. NTSTATUS status;
  326. UNICODE_STRING nameString;
  327. IO_STATUS_BLOCK ioStatusBlock;
  328. OBJECT_ATTRIBUTES objectAttributes;
  329. SC_HANDLE schSCManager, schService;
  330. DWORD dwErr;
  331. SERVICE_STATUS ssStatus;
  332. TraceEnter("StopDriverAndCloseHandle");
  333. if(hDevice isnot NULL)
  334. {
  335. CloseHandle(hDevice);
  336. }
  337. schSCManager = OpenSCManager(NULL,
  338. NULL,
  339. SC_MANAGER_ALL_ACCESS);
  340. if(schSCManager is NULL)
  341. {
  342. dwErr = GetLastError();
  343. Trace2(ERR,
  344. "StopDriver: Error %d opening service controller for %s",
  345. dwErr,
  346. pszServiceName);
  347. TraceLeave("StopDriver");
  348. return dwErr;
  349. }
  350. schService = OpenService(schSCManager,
  351. pszServiceName,
  352. SERVICE_ALL_ACCESS);
  353. if(schService is NULL)
  354. {
  355. dwErr = GetLastError();
  356. Trace2(ERR,
  357. "StopDriver: Error %d opening %s",
  358. dwErr,
  359. pszServiceName);
  360. CloseServiceHandle(schSCManager);
  361. TraceLeave("StopDriver");
  362. return dwErr;
  363. }
  364. if(!ControlService(schService,
  365. SERVICE_CONTROL_STOP,
  366. &ssStatus))
  367. {
  368. dwErr = GetLastError();
  369. Trace2(ERR,
  370. "StopDriver: Error %d stopping %s",
  371. dwErr,
  372. pszServiceName);
  373. TraceLeave("StopDriver");
  374. return dwErr;
  375. }
  376. TraceLeave("StopDriver");
  377. return NO_ERROR ;
  378. }