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.

1633 lines
36 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. AlgIF.c
  5. Abstract:
  6. This module contains code for the ALG transparent proxy's interface
  7. management.
  8. Author:
  9. Qiang Wang (qiangw) 10-April-2000
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #include <ipnatapi.h>
  15. #include "ALG.h"
  16. //
  17. // GLOBAL DATA DEFINITIONS
  18. //
  19. LIST_ENTRY AlgInterfaceList;
  20. CRITICAL_SECTION AlgInterfaceLock;
  21. ULONG AlgFirewallIfCount;
  22. ULONG
  23. AlgAcceptConnectionInterface(
  24. IN PALG_INTERFACE Interfacep,
  25. IN SOCKET ListeningSocket,
  26. IN SOCKET AcceptedSocket OPTIONAL,
  27. IN PNH_BUFFER Bufferp OPTIONAL,
  28. OUT PHANDLE DynamicRedirectHandlep OPTIONAL
  29. )
  30. /*++
  31. Routine Description:
  32. This routine is called to accept a connection on an interface. It issues
  33. an accept-request on the socket and optionally issues a redirect to cause
  34. ALG control-channel connection-requests to be sent to the listening
  35. socket.
  36. Arguments:
  37. Interfacep - the interface on which to accept a connection
  38. ListeningSocket - the socket on which to listen for a connection
  39. AcceptedSocket - optionally specifies the socket with which to accept
  40. a connection.
  41. Bufferp - optionally supplies a buffer to be used for accept-data
  42. DynamicRedirectHandlep - on output, optionally receives a handle to a
  43. dynamic redirect created for the interface. The dynamic redirect
  44. is only created if the caller specifies this parameter.
  45. Return Value:
  46. ULONG - Win32/Winsock2 status code.
  47. Notes:
  48. Invoked with the interface's lock held by the caller and with a reference
  49. made to the interface on behalf of the accept-completion routine. It is
  50. this routine's responsibility to release that reference in the event of a
  51. failure.
  52. --*/
  53. {
  54. ULONG Address;
  55. ULONG Error;
  56. USHORT Port;
  57. PROFILE("AlgAcceptConnectionInterface");
  58. Error =
  59. NhAcceptStreamSocket(
  60. &AlgComponentReference,
  61. ListeningSocket,
  62. AcceptedSocket,
  63. Bufferp,
  64. AlgAcceptCompletionRoutine,
  65. Interfacep,
  66. (PVOID)ListeningSocket
  67. );
  68. if (Error) {
  69. ALG_DEREFERENCE_INTERFACE(Interfacep);
  70. NhTrace(
  71. TRACE_FLAG_ALG,
  72. "AlgAcceptConnectionInterface: error %d accepting connection",
  73. Error
  74. );
  75. } else if (DynamicRedirectHandlep) {
  76. //
  77. // From here onward, failures do not require us to drop our reference
  78. // to the interface, since it is now guaranteed that the accept-
  79. // completion routine will be invoked.
  80. //
  81. // Create the dynamic redirect which will cause all ALG control
  82. // channel connections to our listening socket.
  83. //
  84. Error = NhQueryLocalEndpointSocket(ListeningSocket, &Address, &Port);
  85. if (Error) {
  86. NhTrace(
  87. TRACE_FLAG_ALG,
  88. "AlgAcceptConnectionInterface: error %d querying endpoint",
  89. Error
  90. );
  91. } else {
  92. //
  93. // Install redirect(s).
  94. //
  95. if (NAT_IFC_PRIVATE(Interfacep->Characteristics) ||
  96. ALG_INTERFACE_MAPPED(Interfacep)) {
  97. ASSERT(!ALG_INTERFACE_MAPPED(Interfacep) ||
  98. NAT_IFC_BOUNDARY(Interfacep->Characteristics));
  99. Error =
  100. NatCreateDynamicAdapterRestrictedPortRedirect(
  101. NatRedirectFlagReceiveOnly,
  102. NAT_PROTOCOL_TCP,
  103. ALG_PORT_CONTROL,
  104. Address,
  105. Port,
  106. Interfacep->AdapterIndex,
  107. 0,
  108. &DynamicRedirectHandlep[0]
  109. );
  110. NhTrace(
  111. TRACE_FLAG_ALG,
  112. "AlgAcceptConnectionInterface:"
  113. " redirect installed for adapter 0x%08x [%d]",
  114. Interfacep->AdapterIndex,
  115. Error
  116. );
  117. }
  118. if (!Error && NAT_IFC_FW(Interfacep->Characteristics)) {
  119. Error =
  120. NatCreateDynamicAdapterRestrictedPortRedirect(
  121. NatRedirectFlagSendOnly,
  122. NAT_PROTOCOL_TCP,
  123. ALG_PORT_CONTROL,
  124. Address,
  125. Port,
  126. Interfacep->AdapterIndex,
  127. 0,
  128. &DynamicRedirectHandlep[1]
  129. );
  130. NhTrace(
  131. TRACE_FLAG_ALG,
  132. "AlgAcceptConnectionInterface:"
  133. " redirect installed for firewalled adapter 0x%08x [%d]",
  134. Interfacep->AdapterIndex,
  135. Error
  136. );
  137. }
  138. }
  139. }
  140. return Error;
  141. } // AlgAcceptConnectionInterface
  142. ULONG
  143. AlgActivateInterface(
  144. PALG_INTERFACE Interfacep
  145. )
  146. /*++
  147. Routine Description:
  148. This routine is called to activate an interface, when the interface
  149. becomes both enabled and bound.
  150. Activation involves
  151. (a) creating sockets for each binding of the interface
  152. (b) initiating connection-acceptance on each created socket
  153. (c) initiating session-redirection for the ALG port, if necessary.
  154. Arguments:
  155. Interfacep - the interface to be activated
  156. Return Value:
  157. ULONG - Win32 status code indicating success or failure.
  158. Notes:
  159. Always invoked locally, with 'Interfacep' referenced by caller and/or
  160. 'AlgInterfaceLock' held by caller.
  161. --*/
  162. {
  163. ULONG Error;
  164. ULONG i;
  165. BOOLEAN IsNatInterface;
  166. PROFILE("AlgActivateInterface");
  167. EnterCriticalSection(&AlgInterfaceLock);
  168. if (ALG_INTERFACE_ADMIN_DISABLED(Interfacep)) {
  169. LeaveCriticalSection(&AlgInterfaceLock);
  170. return NO_ERROR;
  171. }
  172. Interfacep->Characteristics
  173. = NatGetInterfaceCharacteristics(Interfacep->Index);
  174. if (!Interfacep->Characteristics) {
  175. LeaveCriticalSection(&AlgInterfaceLock);
  176. NhTrace(
  177. TRACE_FLAG_ALG,
  178. "AlgActivateInterface: ignoring non-NAT interface %d",
  179. Interfacep->Index
  180. );
  181. return NO_ERROR;
  182. }
  183. if (NAT_IFC_BOUNDARY(Interfacep->Characteristics)) {
  184. for (i = 0; i < Interfacep->BindingCount; i++) {
  185. Error = NatLookupPortMappingAdapter(
  186. Interfacep->AdapterIndex,
  187. NAT_PROTOCOL_TCP,
  188. Interfacep->BindingArray[i].Address,
  189. ALG_PORT_CONTROL,
  190. &Interfacep->PortMapping
  191. );
  192. if (!Error) {
  193. Interfacep->Flags |= ALG_INTERFACE_FLAG_MAPPED;
  194. break;
  195. }
  196. }
  197. if (Error && !NAT_IFC_FW(Interfacep->Characteristics)) {
  198. LeaveCriticalSection(&AlgInterfaceLock);
  199. NhTrace(
  200. TRACE_FLAG_ALG,
  201. "AlgActivateInterface:"
  202. " ignoring non-FW and non-mapped NAT boundary interface %d",
  203. Interfacep->Index
  204. );
  205. NhWarningLog(
  206. IP_ALG_LOG_NAT_INTERFACE_IGNORED,
  207. 0,
  208. "%d",
  209. Interfacep->Index
  210. );
  211. return NO_ERROR;
  212. }
  213. }
  214. if (NAT_IFC_FW(Interfacep->Characteristics)) {
  215. InterlockedIncrement(reinterpret_cast<LPLONG>(&AlgFirewallIfCount));
  216. }
  217. //
  218. // Create stream sockets that listen for connection-requests on each
  219. // logical network, and datagram sockets that process incoming messages.
  220. //
  221. Error = NO_ERROR;
  222. ACQUIRE_LOCK(Interfacep);
  223. for (i = 0; i < Interfacep->BindingCount; i++) {
  224. Error =
  225. NhCreateStreamSocket(
  226. Interfacep->BindingArray[i].Address,
  227. 0,
  228. &Interfacep->BindingArray[i].ListeningSocket
  229. );
  230. if (Error) { break; }
  231. Error = listen(Interfacep->BindingArray[i].ListeningSocket, SOMAXCONN);
  232. if (Error == SOCKET_ERROR) { break; }
  233. }
  234. //
  235. // If an error occurred, roll back all work done so far and fail.
  236. //
  237. if (Error) {
  238. ULONG FailedAddress = i;
  239. for (--i; (LONG)i >= 0; i--) {
  240. NhDeleteStreamSocket(
  241. Interfacep->BindingArray[i].ListeningSocket
  242. );
  243. Interfacep->BindingArray[i].ListeningSocket = INVALID_SOCKET;
  244. }
  245. NhErrorLog(
  246. IP_ALG_LOG_ACTIVATE_FAILED,
  247. Error,
  248. "%I",
  249. Interfacep->BindingArray[FailedAddress].Address
  250. );
  251. RELEASE_LOCK(Interfacep);
  252. LeaveCriticalSection(&AlgInterfaceLock);
  253. return Error;
  254. }
  255. //
  256. // Initiate connection-acceptance and message-redirection on each socket
  257. //
  258. for (i = 0; i < Interfacep->BindingCount; i++) {
  259. if (!ALG_REFERENCE_INTERFACE(Interfacep)) { break; }
  260. Error =
  261. AlgAcceptConnectionInterface(
  262. Interfacep,
  263. Interfacep->BindingArray[i].ListeningSocket,
  264. INVALID_SOCKET,
  265. NULL,
  266. &Interfacep->BindingArray[i].ListeningRedirectHandle[0]
  267. );
  268. if (Error) {
  269. NhErrorLog(
  270. IP_ALG_LOG_ACCEPT_FAILED,
  271. Error,
  272. "%I",
  273. Interfacep->BindingArray[i].Address
  274. );
  275. Error = NO_ERROR;
  276. }
  277. }
  278. RELEASE_LOCK(Interfacep);
  279. LeaveCriticalSection(&AlgInterfaceLock);
  280. return NO_ERROR;
  281. } // AlgActivateInterface
  282. ULONG
  283. AlgBindInterface(
  284. ULONG Index,
  285. PIP_ADAPTER_BINDING_INFO BindingInfo
  286. )
  287. /*++
  288. Routine Description:
  289. This routine is invoked to supply the binding for an interface.
  290. It records the binding information received, and if necessary,
  291. it activates the interface.
  292. Arguments:
  293. Index - the index of the interface to be bound
  294. BindingInfo - the binding-information for the interface
  295. Return Value:
  296. ULONG - Win32 status code.
  297. Notes:
  298. Invoked internally in the context of an IP router-manager thread.
  299. (See 'RMALG.C').
  300. --*/
  301. {
  302. ULONG i;
  303. ULONG Error = NO_ERROR;
  304. PALG_INTERFACE Interfacep;
  305. PROFILE("AlgBindInterface");
  306. EnterCriticalSection(&AlgInterfaceLock);
  307. //
  308. // Retrieve the interface to be bound
  309. //
  310. Interfacep = AlgLookupInterface(Index, NULL);
  311. if (Interfacep == NULL) {
  312. LeaveCriticalSection(&AlgInterfaceLock);
  313. NhTrace(
  314. TRACE_FLAG_IF,
  315. "AlgBindInterface: interface %d not found",
  316. Index
  317. );
  318. return ERROR_NO_SUCH_INTERFACE;
  319. }
  320. //
  321. // Make sure the interface isn't already bound
  322. //
  323. if (ALG_INTERFACE_BOUND(Interfacep)) {
  324. LeaveCriticalSection(&AlgInterfaceLock);
  325. NhTrace(
  326. TRACE_FLAG_IF,
  327. "AlgBindInterface: interface %d is already bound",
  328. Index
  329. );
  330. return ERROR_ADDRESS_ALREADY_ASSOCIATED;
  331. }
  332. //
  333. // Reference the interface
  334. //
  335. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  336. LeaveCriticalSection(&AlgInterfaceLock);
  337. NhTrace(
  338. TRACE_FLAG_IF,
  339. "AlgBindInterface: interface %d cannot be referenced",
  340. Index
  341. );
  342. return ERROR_INTERFACE_DISABLED;
  343. }
  344. //
  345. // Update the interface's flags
  346. //
  347. Interfacep->Flags |= ALG_INTERFACE_FLAG_BOUND;
  348. LeaveCriticalSection(&AlgInterfaceLock);
  349. ACQUIRE_LOCK(Interfacep);
  350. //
  351. // Allocate space for the binding
  352. //
  353. if (!BindingInfo->AddressCount) {
  354. Interfacep->BindingCount = 0;
  355. Interfacep->BindingArray = NULL;
  356. } else {
  357. Interfacep->BindingArray =
  358. reinterpret_cast<PALG_BINDING>(
  359. NH_ALLOCATE(BindingInfo->AddressCount * sizeof(ALG_BINDING))
  360. );
  361. if (!Interfacep->BindingArray) {
  362. RELEASE_LOCK(Interfacep);
  363. ALG_DEREFERENCE_INTERFACE(Interfacep);
  364. NhTrace(
  365. TRACE_FLAG_IF,
  366. "AlgBindInterface: allocation failed for interface %d binding",
  367. Index
  368. );
  369. NhErrorLog(
  370. IP_ALG_LOG_ALLOCATION_FAILED,
  371. 0,
  372. "%d",
  373. BindingInfo->AddressCount * sizeof(ALG_BINDING)
  374. );
  375. return ERROR_NOT_ENOUGH_MEMORY;
  376. }
  377. ZeroMemory(
  378. Interfacep->BindingArray,
  379. BindingInfo->AddressCount * sizeof(ALG_BINDING)
  380. );
  381. Interfacep->BindingCount = BindingInfo->AddressCount;
  382. }
  383. //
  384. // Copy the binding
  385. //
  386. for (i = 0; i < BindingInfo->AddressCount; i++) {
  387. Interfacep->BindingArray[i].Address = BindingInfo->Address[i].Address;
  388. Interfacep->BindingArray[i].Mask = BindingInfo->Address[i].Mask;
  389. Interfacep->BindingArray[i].ListeningSocket = INVALID_SOCKET;
  390. }
  391. //
  392. // Figure out our IP Adapter Index, if we have a valid binding
  393. //
  394. if (Interfacep->BindingCount) {
  395. Interfacep->AdapterIndex =
  396. NhMapAddressToAdapter(BindingInfo->Address[0].Address);
  397. }
  398. RELEASE_LOCK(Interfacep);
  399. //
  400. // Activate the interface if necessary
  401. //
  402. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  403. Error = AlgActivateInterface(Interfacep);
  404. }
  405. ALG_DEREFERENCE_INTERFACE(Interfacep);
  406. return Error;
  407. } // AlgBindInterface
  408. VOID
  409. AlgCleanupInterface(
  410. PALG_INTERFACE Interfacep
  411. )
  412. /*++
  413. Routine Description:
  414. This routine is invoked when the very last reference to an interface
  415. is released, and the interface must be destroyed.
  416. Arguments:
  417. Interfacep - the interface to be destroyed
  418. Return Value:
  419. none.
  420. Notes:
  421. Invoked internally from an arbitrary context, with no references
  422. to the interface.
  423. --*/
  424. {
  425. PROFILE("AlgCleanupInterface");
  426. if (Interfacep->BindingArray) {
  427. NH_FREE(Interfacep->BindingArray);
  428. Interfacep->BindingArray = NULL;
  429. }
  430. NH_FREE(Interfacep);
  431. } // AlgCleanupInterface
  432. ULONG
  433. AlgConfigureInterface(
  434. ULONG Index,
  435. PIP_ALG_INTERFACE_INFO InterfaceInfo
  436. )
  437. /*++
  438. Routine Description:
  439. This routine is called to set the configuration for an interface.
  440. Arguments:
  441. Index - the interface to be configured
  442. InterfaceInfo - the new configuration
  443. Return Value:
  444. ULONG - Win32 status code
  445. Notes:
  446. Invoked internally in the context of a IP router-manager thread.
  447. (See 'RMALG.C').
  448. --*/
  449. {
  450. ULONG Error;
  451. PALG_INTERFACE Interfacep;
  452. ULONG NewFlags;
  453. ULONG OldFlags;
  454. PROFILE("AlgConfigureInterface");
  455. //
  456. // Retrieve the interface to be configured
  457. //
  458. EnterCriticalSection(&AlgInterfaceLock);
  459. Interfacep = AlgLookupInterface(Index, NULL);
  460. if (Interfacep == NULL) {
  461. LeaveCriticalSection(&AlgInterfaceLock);
  462. NhTrace(
  463. TRACE_FLAG_IF,
  464. "AlgConfigureInterface: interface %d not found",
  465. Index
  466. );
  467. return ERROR_NO_SUCH_INTERFACE;
  468. }
  469. //
  470. // Reference the interface
  471. //
  472. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  473. LeaveCriticalSection(&AlgInterfaceLock);
  474. NhTrace(
  475. TRACE_FLAG_IF,
  476. "AlgConfigureInterface: interface %d cannot be referenced",
  477. Index
  478. );
  479. return ERROR_INTERFACE_DISABLED;
  480. }
  481. LeaveCriticalSection(&AlgInterfaceLock);
  482. Error = NO_ERROR;
  483. ACQUIRE_LOCK(Interfacep);
  484. //
  485. // Compare the interface's current and new configuration
  486. //
  487. OldFlags = Interfacep->Info.Flags;
  488. NewFlags =
  489. (InterfaceInfo
  490. ? (InterfaceInfo->Flags|ALG_INTERFACE_FLAG_CONFIGURED) : 0);
  491. Interfacep->Flags &= ~OldFlags;
  492. Interfacep->Flags |= NewFlags;
  493. if (!InterfaceInfo) {
  494. ZeroMemory(&Interfacep->Info, sizeof(*InterfaceInfo));
  495. //
  496. // The interface no longer has any information;
  497. // default to being enabled.
  498. //
  499. if (OldFlags & IP_ALG_INTERFACE_FLAG_DISABLED) {
  500. //
  501. // Activate the interface if necessary
  502. //
  503. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  504. RELEASE_LOCK(Interfacep);
  505. Error = AlgActivateInterface(Interfacep);
  506. ACQUIRE_LOCK(Interfacep);
  507. }
  508. }
  509. } else {
  510. CopyMemory(&Interfacep->Info, InterfaceInfo, sizeof(*InterfaceInfo));
  511. //
  512. // Activate or deactivate the interface if its status changed
  513. //
  514. if ((OldFlags & IP_ALG_INTERFACE_FLAG_DISABLED) &&
  515. !(NewFlags & IP_ALG_INTERFACE_FLAG_DISABLED)) {
  516. //
  517. // Activate the interface
  518. //
  519. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  520. RELEASE_LOCK(Interfacep);
  521. Error = AlgActivateInterface(Interfacep);
  522. ACQUIRE_LOCK(Interfacep);
  523. }
  524. } else if (!(OldFlags & IP_ALG_INTERFACE_FLAG_DISABLED) &&
  525. (NewFlags & IP_ALG_INTERFACE_FLAG_DISABLED)) {
  526. //
  527. // Deactivate the interface if necessary
  528. //
  529. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  530. RELEASE_LOCK(Interfacep);
  531. AlgDeactivateInterface(Interfacep);
  532. ACQUIRE_LOCK(Interfacep);
  533. }
  534. }
  535. }
  536. RELEASE_LOCK(Interfacep);
  537. ALG_DEREFERENCE_INTERFACE(Interfacep);
  538. return Error;
  539. } // AlgConfigureInterface
  540. ULONG
  541. AlgCreateInterface(
  542. ULONG Index,
  543. NET_INTERFACE_TYPE Type,
  544. PIP_ALG_INTERFACE_INFO InterfaceInfo,
  545. OUT PALG_INTERFACE* InterfaceCreated
  546. )
  547. /*++
  548. Routine Description:
  549. This routine is invoked by the router-manager to add a new interface
  550. to the ALG transparent proxy.
  551. Arguments:
  552. Index - the index of the new interface
  553. Type - the media type of the new interface
  554. InterfaceInfo - the interface's configuration
  555. Interfacep - receives the interface created
  556. Return Value:
  557. ULONG - Win32 error code
  558. Notes:
  559. Invoked internally in the context of an IP router-manager thread.
  560. (See 'RMALG.C').
  561. --*/
  562. {
  563. PLIST_ENTRY InsertionPoint;
  564. PALG_INTERFACE Interfacep;
  565. PROFILE("AlgCreateInterface");
  566. EnterCriticalSection(&AlgInterfaceLock);
  567. //
  568. // See if the interface already exists;
  569. // If not, this obtains the insertion point
  570. //
  571. if (AlgLookupInterface(Index, &InsertionPoint)) {
  572. LeaveCriticalSection(&AlgInterfaceLock);
  573. NhTrace(
  574. TRACE_FLAG_IF,
  575. "AlgCreateInterface: duplicate index found for %d",
  576. Index
  577. );
  578. return ERROR_INTERFACE_ALREADY_EXISTS;
  579. }
  580. //
  581. // Allocate a new interface
  582. //
  583. Interfacep =
  584. reinterpret_cast<PALG_INTERFACE>(NH_ALLOCATE(sizeof(ALG_INTERFACE)));
  585. if (!Interfacep) {
  586. LeaveCriticalSection(&AlgInterfaceLock);
  587. NhTrace(
  588. TRACE_FLAG_IF, "AlgCreateInterface: error allocating interface"
  589. );
  590. NhErrorLog(
  591. IP_ALG_LOG_ALLOCATION_FAILED,
  592. 0,
  593. "%d",
  594. sizeof(ALG_INTERFACE)
  595. );
  596. return ERROR_NOT_ENOUGH_MEMORY;
  597. }
  598. //
  599. // Initialize the new interface
  600. //
  601. ZeroMemory(Interfacep, sizeof(*Interfacep));
  602. __try {
  603. InitializeCriticalSection(&Interfacep->Lock);
  604. } __except(EXCEPTION_EXECUTE_HANDLER) {
  605. LeaveCriticalSection(&AlgInterfaceLock);
  606. NH_FREE(Interfacep);
  607. return GetExceptionCode();
  608. }
  609. Interfacep->Index = Index;
  610. Interfacep->Type = Type;
  611. if (InterfaceInfo) {
  612. Interfacep->Flags = InterfaceInfo->Flags|ALG_INTERFACE_FLAG_CONFIGURED;
  613. CopyMemory(&Interfacep->Info, InterfaceInfo, sizeof(*InterfaceInfo));
  614. }
  615. Interfacep->ReferenceCount = 1;
  616. InitializeListHead(&Interfacep->ConnectionList);
  617. InitializeListHead(&Interfacep->EndpointList);
  618. InsertTailList(InsertionPoint, &Interfacep->Link);
  619. LeaveCriticalSection(&AlgInterfaceLock);
  620. if (InterfaceCreated) { *InterfaceCreated = Interfacep; }
  621. return NO_ERROR;
  622. } // AlgCreateInterface
  623. VOID
  624. AlgDeactivateInterface(
  625. PALG_INTERFACE Interfacep
  626. )
  627. /*++
  628. Routine Description:
  629. This routine is called to deactivate an interface.
  630. It closes all sockets on the interface's bindings (if any).
  631. Arguments:
  632. Interfacep - the interface to be deactivated
  633. Return Value:
  634. none.
  635. Notes:
  636. Always invoked locally, with 'Interfacep' referenced by caller and/or
  637. 'AlgInterfaceLock' held by caller.
  638. --*/
  639. {
  640. ULONG i;
  641. ULONG j;
  642. PLIST_ENTRY Link;
  643. PALG_CONNECTION Connectionp;
  644. PROFILE("AlgDeactivateInterface");
  645. ACQUIRE_LOCK(Interfacep);
  646. //
  647. // Stop all network I/O on the interface's logical networks
  648. //
  649. for (i = 0; i < Interfacep->BindingCount; i++) {
  650. if (Interfacep->BindingArray[i].ListeningSocket != INVALID_SOCKET) {
  651. NhDeleteStreamSocket(Interfacep->BindingArray[i].ListeningSocket);
  652. Interfacep->BindingArray[i].ListeningSocket = INVALID_SOCKET;
  653. }
  654. for (j = 0; j < 2; j++) {
  655. if (Interfacep->BindingArray[i].ListeningRedirectHandle[j]) {
  656. NatCancelDynamicPortRedirect(
  657. Interfacep->BindingArray[i].ListeningRedirectHandle[j]
  658. );
  659. Interfacep->BindingArray[i].ListeningRedirectHandle[j] = NULL;
  660. }
  661. }
  662. }
  663. //
  664. // Eliminate all connections
  665. //
  666. while (!IsListEmpty(&Interfacep->ConnectionList)) {
  667. Link = RemoveHeadList(&Interfacep->ConnectionList);
  668. Connectionp = CONTAINING_RECORD(Link, ALG_CONNECTION, Link);
  669. AlgDeleteConnection(Connectionp);
  670. }
  671. ASSERT(IsListEmpty(&Interfacep->EndpointList));
  672. //
  673. // If this interface is firewalled, decrement the global count.
  674. //
  675. if (NAT_IFC_FW(Interfacep->Characteristics)) {
  676. InterlockedDecrement(reinterpret_cast<LPLONG>(&AlgFirewallIfCount));
  677. }
  678. RELEASE_LOCK(Interfacep);
  679. } // AlgDeactivateInterface
  680. ULONG
  681. AlgDeleteInterface(
  682. ULONG Index
  683. )
  684. /*++
  685. Routine Description:
  686. This routine is called to delete an interface.
  687. It drops the reference count on the interface so that the last
  688. dereferencer will delete the interface, and sets the 'deleted' flag
  689. so that further references to the interface will fail.
  690. Arguments:
  691. Index - the index of the interface to be deleted
  692. Return Value:
  693. ULONG - Win32 status code.
  694. Notes:
  695. Invoked internally in the context of an IP router-manager thread.
  696. (See 'RMALG.C').
  697. --*/
  698. {
  699. PALG_INTERFACE Interfacep;
  700. PROFILE("AlgDeleteInterface");
  701. //
  702. // Retrieve the interface to be deleted
  703. //
  704. EnterCriticalSection(&AlgInterfaceLock);
  705. Interfacep = AlgLookupInterface(Index, NULL);
  706. if (Interfacep == NULL)
  707. {
  708. LeaveCriticalSection(&AlgInterfaceLock);
  709. NhTrace(
  710. TRACE_FLAG_IF,
  711. "AlgDeleteInterface: interface %d not found",
  712. Index
  713. );
  714. return ERROR_NO_SUCH_INTERFACE;
  715. }
  716. //
  717. // Deactivate the interface
  718. //
  719. AlgDeactivateInterface(Interfacep);
  720. //
  721. // Mark the interface as deleted and take it off the interface list
  722. //
  723. Interfacep->Flags |= ALG_INTERFACE_FLAG_DELETED;
  724. Interfacep->Flags &= ~ALG_INTERFACE_FLAG_ENABLED;
  725. RemoveEntryList(&Interfacep->Link);
  726. //
  727. // Drop the reference count; if it is non-zero,
  728. // the deletion will complete later.
  729. //
  730. if (--Interfacep->ReferenceCount) {
  731. LeaveCriticalSection(&AlgInterfaceLock);
  732. NhTrace(
  733. TRACE_FLAG_IF,
  734. "AlgDeleteInterface: interface %d deletion pending",
  735. Index
  736. );
  737. return NO_ERROR;
  738. }
  739. //
  740. // The reference count is zero, so perform final cleanup
  741. //
  742. AlgCleanupInterface(Interfacep);
  743. LeaveCriticalSection(&AlgInterfaceLock);
  744. return NO_ERROR;
  745. } // AlgDeleteInterface
  746. ULONG
  747. AlgDisableInterface(
  748. ULONG Index
  749. )
  750. /*++
  751. Routine Description:
  752. This routine is called to disable I/O on an interface.
  753. If the interface is active, it is deactivated.
  754. Arguments:
  755. Index - the index of the interface to be disabled.
  756. Return Value:
  757. none.
  758. Notes:
  759. Invoked internally in the context of an IP router-manager thread.
  760. (See 'RMALG.C').
  761. --*/
  762. {
  763. PALG_INTERFACE Interfacep;
  764. PROFILE("AlgDisableInterface");
  765. //
  766. // Retrieve the interface to be disabled
  767. //
  768. EnterCriticalSection(&AlgInterfaceLock);
  769. Interfacep = AlgLookupInterface(Index, NULL);
  770. if (Interfacep == NULL) {
  771. LeaveCriticalSection(&AlgInterfaceLock);
  772. NhTrace(
  773. TRACE_FLAG_IF,
  774. "AlgDisableInterface: interface %d not found",
  775. Index
  776. );
  777. return ERROR_NO_SUCH_INTERFACE;
  778. }
  779. //
  780. // Make sure the interface is not already disabled
  781. //
  782. if (!ALG_INTERFACE_ENABLED(Interfacep)) {
  783. LeaveCriticalSection(&AlgInterfaceLock);
  784. NhTrace(
  785. TRACE_FLAG_IF,
  786. "AlgDisableInterface: interface %d already disabled",
  787. Index
  788. );
  789. return ERROR_INTERFACE_DISABLED;
  790. }
  791. //
  792. // Reference the interface
  793. //
  794. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  795. LeaveCriticalSection(&AlgInterfaceLock);
  796. NhTrace(
  797. TRACE_FLAG_IF,
  798. "AlgDisableInterface: interface %d cannot be referenced",
  799. Index
  800. );
  801. return ERROR_INTERFACE_DISABLED;
  802. }
  803. //
  804. // Clear the 'enabled' flag
  805. //
  806. Interfacep->Flags &= ~ALG_INTERFACE_FLAG_ENABLED;
  807. //
  808. // Deactivate the interface, if necessary
  809. //
  810. if (ALG_INTERFACE_BOUND(Interfacep)) {
  811. AlgDeactivateInterface(Interfacep);
  812. }
  813. LeaveCriticalSection(&AlgInterfaceLock);
  814. ALG_DEREFERENCE_INTERFACE(Interfacep);
  815. return NO_ERROR;
  816. } // AlgDisableInterface
  817. ULONG
  818. AlgEnableInterface(
  819. ULONG Index
  820. )
  821. /*++
  822. Routine Description:
  823. This routine is called to enable I/O on an interface.
  824. If the interface is already bound, this enabling activates it.
  825. Arguments:
  826. Index - the index of the interfaec to be enabled
  827. Return Value:
  828. ULONG - Win32 status code.
  829. Notes:
  830. Invoked internally in the context of an IP router-manager thread.
  831. (See 'RMALG.C').
  832. --*/
  833. {
  834. ULONG Error = NO_ERROR;
  835. PALG_INTERFACE Interfacep;
  836. PROFILE("AlgEnableInterface");
  837. //
  838. // Retrieve the interface to be enabled
  839. //
  840. EnterCriticalSection(&AlgInterfaceLock);
  841. Interfacep = AlgLookupInterface(Index, NULL);
  842. if (Interfacep == NULL) {
  843. LeaveCriticalSection(&AlgInterfaceLock);
  844. NhTrace(
  845. TRACE_FLAG_IF,
  846. "AlgEnableInterface: interface %d not found",
  847. Index
  848. );
  849. return ERROR_NO_SUCH_INTERFACE;
  850. }
  851. //
  852. // Make sure the interface is not already enabled
  853. //
  854. if (ALG_INTERFACE_ENABLED(Interfacep)) {
  855. LeaveCriticalSection(&AlgInterfaceLock);
  856. NhTrace(
  857. TRACE_FLAG_IF,
  858. "AlgEnableInterface: interface %d already enabled",
  859. Index
  860. );
  861. return ERROR_INTERFACE_ALREADY_EXISTS;
  862. }
  863. //
  864. // Reference the interface
  865. //
  866. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  867. LeaveCriticalSection(&AlgInterfaceLock);
  868. NhTrace(
  869. TRACE_FLAG_IF,
  870. "AlgEnableInterface: interface %d cannot be referenced",
  871. Index
  872. );
  873. return ERROR_INTERFACE_DISABLED;
  874. }
  875. //
  876. // Set the 'enabled' flag
  877. //
  878. Interfacep->Flags |= ALG_INTERFACE_FLAG_ENABLED;
  879. //
  880. // Activate the interface, if necessary
  881. //
  882. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  883. Error = AlgActivateInterface(Interfacep);
  884. }
  885. LeaveCriticalSection(&AlgInterfaceLock);
  886. ALG_DEREFERENCE_INTERFACE(Interfacep);
  887. return Error;
  888. } // AlgEnableInterface
  889. ULONG
  890. AlgInitializeInterfaceManagement(
  891. VOID
  892. )
  893. /*++
  894. Routine Description:
  895. This routine is called to initialize the interface-management module.
  896. Arguments:
  897. none.
  898. Return Value:
  899. ULONG - Win32 status code.
  900. Notes:
  901. Invoked internally in the context of an IP router-manager thread.
  902. (See 'RMALG.C').
  903. --*/
  904. {
  905. ULONG Error = NO_ERROR;
  906. PROFILE("AlgInitializeInterfaceManagement");
  907. InitializeListHead(&AlgInterfaceList);
  908. __try {
  909. InitializeCriticalSection(&AlgInterfaceLock);
  910. } __except(EXCEPTION_EXECUTE_HANDLER) {
  911. NhTrace(
  912. TRACE_FLAG_IF,
  913. "AlgInitializeInterfaceManagement: exception %d creating lock",
  914. Error = GetExceptionCode()
  915. );
  916. }
  917. AlgFirewallIfCount = 0;
  918. return Error;
  919. } // AlgInitializeInterfaceManagement
  920. PALG_INTERFACE
  921. AlgLookupInterface(
  922. ULONG Index,
  923. OUT PLIST_ENTRY* InsertionPoint OPTIONAL
  924. )
  925. /*++
  926. Routine Description:
  927. This routine is called to retrieve an interface given its index.
  928. Arguments:
  929. Index - the index of the interface to be retrieved
  930. InsertionPoint - if the interface is not found, optionally receives
  931. the point where the interface would be inserted in the interface list
  932. Return Value:
  933. PALG_INTERFACE - the interface, if found; otherwise, NULL.
  934. Notes:
  935. Invoked internally from an arbitrary context, with 'AlgInterfaceLock'
  936. held by caller.
  937. --*/
  938. {
  939. PALG_INTERFACE Interfacep;
  940. PLIST_ENTRY Link;
  941. PROFILE("AlgLookupInterface");
  942. for (Link = AlgInterfaceList.Flink; Link != &AlgInterfaceList;
  943. Link = Link->Flink) {
  944. Interfacep = CONTAINING_RECORD(Link, ALG_INTERFACE, Link);
  945. if (Index > Interfacep->Index) {
  946. continue;
  947. } else if (Index < Interfacep->Index) {
  948. break;
  949. }
  950. return Interfacep;
  951. }
  952. if (InsertionPoint) { *InsertionPoint = Link; }
  953. return NULL;
  954. } // AlgLookupInterface
  955. ULONG
  956. AlgQueryInterface(
  957. ULONG Index,
  958. PVOID InterfaceInfo,
  959. PULONG InterfaceInfoSize
  960. )
  961. /*++
  962. Routine Description:
  963. This routine is invoked to retrieve the configuration for an interface.
  964. Arguments:
  965. Index - the interface to be queried
  966. InterfaceInfo - receives the retrieved information
  967. InterfaceInfoSize - receives the (required) size of the information
  968. Return Value:
  969. ULONG - Win32 status code.
  970. --*/
  971. {
  972. PALG_INTERFACE Interfacep;
  973. PROFILE("AlgQueryInterface");
  974. //
  975. // Check the caller's buffer size
  976. //
  977. if (!InterfaceInfoSize) { return ERROR_INVALID_PARAMETER; }
  978. //
  979. // Retrieve the interface to be configured
  980. //
  981. EnterCriticalSection(&AlgInterfaceLock);
  982. Interfacep = AlgLookupInterface(Index, NULL);
  983. if (Interfacep == NULL) {
  984. LeaveCriticalSection(&AlgInterfaceLock);
  985. NhTrace(
  986. TRACE_FLAG_IF,
  987. "AlgQueryInterface: interface %d not found",
  988. Index
  989. );
  990. return ERROR_NO_SUCH_INTERFACE;
  991. }
  992. //
  993. // Reference the interface
  994. //
  995. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  996. LeaveCriticalSection(&AlgInterfaceLock);
  997. NhTrace(
  998. TRACE_FLAG_IF,
  999. "AlgQueryInterface: interface %d cannot be referenced",
  1000. Index
  1001. );
  1002. return ERROR_INTERFACE_DISABLED;
  1003. }
  1004. //
  1005. // See if there is any explicit config on this interface
  1006. //
  1007. if (!ALG_INTERFACE_CONFIGURED(Interfacep)) {
  1008. LeaveCriticalSection(&AlgInterfaceLock);
  1009. ALG_DEREFERENCE_INTERFACE(Interfacep);
  1010. NhTrace(
  1011. TRACE_FLAG_IF,
  1012. "AlgQueryInterface: interface %d has no configuration",
  1013. Index
  1014. );
  1015. *InterfaceInfoSize = 0;
  1016. return NO_ERROR;
  1017. }
  1018. //
  1019. // See if there is enough buffer space
  1020. //
  1021. if (*InterfaceInfoSize < sizeof(IP_ALG_INTERFACE_INFO)) {
  1022. LeaveCriticalSection(&AlgInterfaceLock);
  1023. ALG_DEREFERENCE_INTERFACE(Interfacep);
  1024. *InterfaceInfoSize = sizeof(IP_ALG_INTERFACE_INFO);
  1025. return ERROR_INSUFFICIENT_BUFFER;
  1026. }
  1027. //
  1028. // Copy the requested data
  1029. //
  1030. CopyMemory(
  1031. InterfaceInfo,
  1032. &Interfacep->Info,
  1033. sizeof(IP_ALG_INTERFACE_INFO)
  1034. );
  1035. *InterfaceInfoSize = sizeof(IP_ALG_INTERFACE_INFO);
  1036. LeaveCriticalSection(&AlgInterfaceLock);
  1037. ALG_DEREFERENCE_INTERFACE(Interfacep);
  1038. return NO_ERROR;
  1039. } // AlgQueryInterface
  1040. VOID
  1041. AlgShutdownInterfaceManagement(
  1042. VOID
  1043. )
  1044. /*++
  1045. Routine Description:
  1046. This routine is called to shutdown the interface-management module.
  1047. Arguments:
  1048. none.
  1049. Return Value:
  1050. none.
  1051. Notes:
  1052. Invoked in an arbitrary thread context, after all references
  1053. to all interfaces have been released.
  1054. --*/
  1055. {
  1056. PALG_INTERFACE Interfacep;
  1057. PLIST_ENTRY Link;
  1058. PROFILE("AlgShutdownInterfaceManagement");
  1059. while (!IsListEmpty(&AlgInterfaceList)) {
  1060. Link = RemoveHeadList(&AlgInterfaceList);
  1061. Interfacep = CONTAINING_RECORD(Link, ALG_INTERFACE, Link);
  1062. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  1063. AlgDeactivateInterface(Interfacep);
  1064. }
  1065. AlgCleanupInterface(Interfacep);
  1066. }
  1067. DeleteCriticalSection(&AlgInterfaceLock);
  1068. } // AlgShutdownInterfaceManagement
  1069. VOID
  1070. AlgSignalNatInterface(
  1071. ULONG Index,
  1072. BOOLEAN Boundary
  1073. )
  1074. /*++
  1075. Routine Description:
  1076. This routine is invoked upon reconfiguration of a NAT interface.
  1077. Note that this routine may be invoked even when the ALG transparent
  1078. proxy is neither installed nor running; it operates as expected,
  1079. since the global information and lock are always initialized.
  1080. Upon invocation, the routine activates or deactivates the interface
  1081. depending on whether the NAT is not or is running on the interface,
  1082. respectively.
  1083. Arguments:
  1084. Index - the reconfigured interface
  1085. Boundary - indicates whether the interface is now a boundary interface
  1086. Return Value:
  1087. none.
  1088. Notes:
  1089. Invoked from an arbitrary context.
  1090. --*/
  1091. {
  1092. PALG_INTERFACE Interfacep;
  1093. PROFILE("AlgSignalNatInterface");
  1094. EnterCriticalSection(&AlgGlobalInfoLock);
  1095. if (!AlgGlobalInfo) {
  1096. LeaveCriticalSection(&AlgGlobalInfoLock);
  1097. return;
  1098. }
  1099. LeaveCriticalSection(&AlgGlobalInfoLock);
  1100. EnterCriticalSection(&AlgInterfaceLock);
  1101. Interfacep = AlgLookupInterface(Index, NULL);
  1102. if (Interfacep == NULL) {
  1103. LeaveCriticalSection(&AlgInterfaceLock);
  1104. return;
  1105. }
  1106. AlgDeactivateInterface(Interfacep);
  1107. if (ALG_INTERFACE_ACTIVE(Interfacep)) {
  1108. AlgActivateInterface(Interfacep);
  1109. }
  1110. LeaveCriticalSection(&AlgInterfaceLock);
  1111. } // AlgSignalNatInterface
  1112. ULONG
  1113. AlgUnbindInterface(
  1114. ULONG Index
  1115. )
  1116. /*++
  1117. Routine Description:
  1118. This routine is invoked to revoke the binding on an interface.
  1119. This involves deactivating the interface if it is active.
  1120. Arguments:
  1121. Index - the index of the interface to be unbound
  1122. Return Value:
  1123. none.
  1124. Notes:
  1125. Invoked internally in the context of an IP router-manager thread.
  1126. (See 'RMALG.C').
  1127. --*/
  1128. {
  1129. PALG_INTERFACE Interfacep;
  1130. PROFILE("AlgUnbindInterface");
  1131. //
  1132. // Retrieve the interface to be unbound
  1133. //
  1134. EnterCriticalSection(&AlgInterfaceLock);
  1135. Interfacep = AlgLookupInterface(Index, NULL);
  1136. if (Interfacep == NULL) {
  1137. LeaveCriticalSection(&AlgInterfaceLock);
  1138. NhTrace(
  1139. TRACE_FLAG_IF,
  1140. "AlgUnbindInterface: interface %d not found",
  1141. Index
  1142. );
  1143. return ERROR_NO_SUCH_INTERFACE;
  1144. }
  1145. //
  1146. // Make sure the interface is not already unbound
  1147. //
  1148. if (!ALG_INTERFACE_BOUND(Interfacep)) {
  1149. LeaveCriticalSection(&AlgInterfaceLock);
  1150. NhTrace(
  1151. TRACE_FLAG_IF,
  1152. "AlgUnbindInterface: interface %d already unbound",
  1153. Index
  1154. );
  1155. return ERROR_ADDRESS_NOT_ASSOCIATED;
  1156. }
  1157. //
  1158. // Reference the interface
  1159. //
  1160. if (!ALG_REFERENCE_INTERFACE(Interfacep)) {
  1161. LeaveCriticalSection(&AlgInterfaceLock);
  1162. NhTrace(
  1163. TRACE_FLAG_IF,
  1164. "AlgUnbindInterface: interface %d cannot be referenced",
  1165. Index
  1166. );
  1167. return ERROR_INTERFACE_DISABLED;
  1168. }
  1169. //
  1170. // Clear the 'bound' and 'mapped' flag
  1171. //
  1172. Interfacep->Flags &=
  1173. ~(ALG_INTERFACE_FLAG_BOUND | ALG_INTERFACE_FLAG_MAPPED);
  1174. //
  1175. // Deactivate the interface, if necessary
  1176. //
  1177. if (ALG_INTERFACE_ENABLED(Interfacep)) {
  1178. AlgDeactivateInterface(Interfacep);
  1179. }
  1180. LeaveCriticalSection(&AlgInterfaceLock);
  1181. //
  1182. // Destroy the interface's binding
  1183. //
  1184. ACQUIRE_LOCK(Interfacep);
  1185. NH_FREE(Interfacep->BindingArray);
  1186. Interfacep->BindingArray = NULL;
  1187. Interfacep->BindingCount = 0;
  1188. RELEASE_LOCK(Interfacep);
  1189. ALG_DEREFERENCE_INTERFACE(Interfacep);
  1190. return NO_ERROR;
  1191. } // AlgUnbindInterface