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.

2198 lines
48 KiB

  1. /*++
  2. Copyright (c) 1998, Microsoft Corporation
  3. Module Name:
  4. dhcpif.c
  5. Abstract:
  6. This module contains code for the DHCP allocator's interface management.
  7. Author:
  8. Abolade Gbadegesin (aboladeg) 5-Mar-1998
  9. Revision History:
  10. Raghu Gatta (rgatta) 15-Dec-2000
  11. Added DhcpGetPrivateInterfaceAddress()
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. #include <ipinfo.h>
  16. extern "C" {
  17. #include <iphlpstk.h>
  18. }
  19. //
  20. // LOCAL TYPE DECLARATIONS
  21. //
  22. typedef struct _DHCP_DEFER_READ_CONTEXT {
  23. ULONG Index;
  24. SOCKET Socket;
  25. ULONG DeferralCount;
  26. } DHCP_DEFER_READ_CONTEXT, *PDHCP_DEFER_READ_CONTEXT;
  27. #define DHCP_DEFER_READ_TIMEOUT (5 * 1000)
  28. //
  29. // GLOBAL DATA DEFINITIONS
  30. //
  31. LIST_ENTRY DhcpInterfaceList;
  32. CRITICAL_SECTION DhcpInterfaceLock;
  33. //
  34. // Forward declarations
  35. //
  36. VOID NTAPI
  37. DhcpDeferReadCallbackRoutine(
  38. PVOID Context,
  39. BOOLEAN TimedOut
  40. );
  41. VOID APIENTRY
  42. DhcpDeferReadWorkerRoutine(
  43. PVOID Context
  44. );
  45. ULONG
  46. DhcpActivateInterface(
  47. PDHCP_INTERFACE Interfacep
  48. )
  49. /*++
  50. Routine Description:
  51. This routine is called to activate an interface, when the interface
  52. becomes both enabled and bound.
  53. Activation involves
  54. (a) creating sockets for each binding of the interface
  55. (b) initiating datagram-reads on each created socket
  56. Arguments:
  57. Context - the index of the interface to be activated
  58. Return Value:
  59. ULONG - Win32 status code indicating success or failure.
  60. Environment:
  61. Always invoked locally, with 'Interfacep' referenced by caller and/or
  62. 'DhcpInterfaceLock' held by caller.
  63. --*/
  64. {
  65. ULONG Error;
  66. ULONG i;
  67. BOOLEAN IsNatInterface;
  68. ULONG ScopeNetwork;
  69. ULONG ScopeMask;
  70. PROFILE("DhcpActivateInterface");
  71. //
  72. // Read the scope-network from which addresses are to be assigned
  73. //
  74. EnterCriticalSection(&DhcpGlobalInfoLock);
  75. ScopeNetwork = DhcpGlobalInfo->ScopeNetwork;
  76. ScopeMask = DhcpGlobalInfo->ScopeMask;
  77. LeaveCriticalSection(&DhcpGlobalInfoLock);
  78. //
  79. // (re)take the interface lock for the duration of the routine
  80. //
  81. EnterCriticalSection(&DhcpInterfaceLock);
  82. if (DHCP_INTERFACE_ADMIN_DISABLED(Interfacep)) {
  83. LeaveCriticalSection(&DhcpInterfaceLock);
  84. return NO_ERROR;
  85. }
  86. //
  87. // See whether this is (a) a NAT interface and (b) a boundary interface.
  88. // We never operate on boundary interfaces, and we only operate
  89. // on NAT interfaces.
  90. //
  91. if (NhIsBoundaryInterface(Interfacep->Index, &IsNatInterface)) {
  92. NhTrace(
  93. TRACE_FLAG_DHCP,
  94. "DhcpActivateInterface: ignoring boundary interface %d",
  95. Interfacep->Index
  96. );
  97. NhWarningLog(
  98. IP_AUTO_DHCP_LOG_NAT_INTERFACE_IGNORED,
  99. 0,
  100. "%d",
  101. Interfacep->Index
  102. );
  103. LeaveCriticalSection(&DhcpInterfaceLock);
  104. return NO_ERROR;
  105. }
  106. if (!IsNatInterface) {
  107. NhTrace(
  108. TRACE_FLAG_DHCP,
  109. "DhcpActivateInterface: ignoring non-NAT interface %d",
  110. Interfacep->Index
  111. );
  112. LeaveCriticalSection(&DhcpInterfaceLock);
  113. return NO_ERROR;
  114. }
  115. //
  116. // Create datagram sockets for receiving data on each logical network;
  117. // N.B. We exclude networks other than the scope network.
  118. //
  119. Error = NO_ERROR;
  120. ACQUIRE_LOCK(Interfacep);
  121. for (i = 0; i < Interfacep->BindingCount; i++) {
  122. if ((Interfacep->BindingArray[i].Address & ScopeMask) !=
  123. (ScopeNetwork & ScopeMask)
  124. ) {
  125. NhErrorLog(
  126. IP_AUTO_DHCP_LOG_NON_SCOPE_ADDRESS,
  127. 0,
  128. "%I%I%I",
  129. Interfacep->BindingArray[i].Address,
  130. ScopeNetwork,
  131. ScopeMask
  132. );
  133. continue;
  134. }
  135. Error =
  136. NhCreateDatagramSocket(
  137. Interfacep->BindingArray[i].Address,
  138. DHCP_PORT_SERVER,
  139. &Interfacep->BindingArray[i].Socket
  140. );
  141. if (Error) { break; }
  142. }
  143. //
  144. // If an error occurred, roll back all work done so far and fail.
  145. //
  146. if (Error) {
  147. ULONG FailedAddress = i;
  148. for (; (LONG)i >= 0; i--) {
  149. NhDeleteDatagramSocket(
  150. Interfacep->BindingArray[i].Socket
  151. );
  152. Interfacep->BindingArray[i].Socket = INVALID_SOCKET;
  153. }
  154. NhErrorLog(
  155. IP_AUTO_DHCP_LOG_ACTIVATE_FAILED,
  156. Error,
  157. "%I",
  158. Interfacep->BindingArray[FailedAddress].Address
  159. );
  160. RELEASE_LOCK(Interfacep);
  161. LeaveCriticalSection(&DhcpInterfaceLock);
  162. return Error;
  163. }
  164. //
  165. // Initiate read-operations on each socket
  166. //
  167. for (i = 0; i < Interfacep->BindingCount; i++) {
  168. if ((Interfacep->BindingArray[i].Address & ScopeMask) !=
  169. (ScopeNetwork & ScopeMask)
  170. ) { continue; }
  171. //
  172. // Make a reference to the interface;
  173. // this reference is released in the completion routine
  174. //
  175. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) { continue; }
  176. //
  177. // Initiate the read-operation
  178. //
  179. Error =
  180. NhReadDatagramSocket(
  181. &DhcpComponentReference,
  182. Interfacep->BindingArray[i].Socket,
  183. NULL,
  184. DhcpReadCompletionRoutine,
  185. Interfacep,
  186. UlongToPtr(Interfacep->BindingArray[i].Mask)
  187. );
  188. //
  189. // Drop the reference if a failure occurred
  190. //
  191. if (Error) {
  192. NhErrorLog(
  193. IP_AUTO_DHCP_LOG_RECEIVE_FAILED,
  194. Error,
  195. "%I",
  196. Interfacep->BindingArray[i].Address
  197. );
  198. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  199. //
  200. // Reissue the read-operation later
  201. //
  202. DhcpDeferReadInterface(
  203. Interfacep,
  204. Interfacep->BindingArray[i].Socket
  205. );
  206. Error = NO_ERROR;
  207. }
  208. //
  209. // Now make another reference for the client-request
  210. // with which we detect servers on the network.
  211. //
  212. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) { continue; }
  213. Error =
  214. DhcpWriteClientRequestMessage(
  215. Interfacep,
  216. &Interfacep->BindingArray[i]
  217. );
  218. //
  219. // Drop the reference if a failure occurred
  220. //
  221. if (Error) { DHCP_DEREFERENCE_INTERFACE(Interfacep); Error = NO_ERROR; }
  222. }
  223. //
  224. // cache that this particular interface is a non boundary NAT interface
  225. //
  226. Interfacep->Flags |= DHCP_INTERFACE_FLAG_NAT_NONBOUNDARY;
  227. RELEASE_LOCK(Interfacep);
  228. LeaveCriticalSection(&DhcpInterfaceLock);
  229. return NO_ERROR;
  230. } // DhcpActivateInterface
  231. ULONG
  232. DhcpBindInterface(
  233. ULONG Index,
  234. PIP_ADAPTER_BINDING_INFO BindingInfo
  235. )
  236. /*++
  237. Routine Description:
  238. This routine is invoked to supply the binding for an interface.
  239. It records the binding information received, and if necessary,
  240. it activates the interface.
  241. Arguments:
  242. Index - the index of the interface to be bound
  243. BindingInfo - the binding-information for the interface
  244. Return Value:
  245. ULONG - Win32 status code.
  246. Environment:
  247. Invoked internally in the context of an IP router-manager thread.
  248. (See 'RMDHCP.C').
  249. --*/
  250. {
  251. ULONG Error = NO_ERROR;
  252. ULONG i;
  253. PDHCP_INTERFACE Interfacep;
  254. PROFILE("DhcpBindInterface");
  255. EnterCriticalSection(&DhcpInterfaceLock);
  256. //
  257. // Retrieve the interface to be bound
  258. //
  259. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  260. LeaveCriticalSection(&DhcpInterfaceLock);
  261. NhTrace(
  262. TRACE_FLAG_IF,
  263. "DhcpBindInterface: interface %d not found",
  264. Index
  265. );
  266. return ERROR_NO_SUCH_INTERFACE;
  267. }
  268. //
  269. // Make sure the interface isn't already bound
  270. //
  271. if (DHCP_INTERFACE_BOUND(Interfacep)) {
  272. LeaveCriticalSection(&DhcpInterfaceLock);
  273. NhTrace(
  274. TRACE_FLAG_IF,
  275. "DhcpBindInterface: interface %d is already bound",
  276. Index
  277. );
  278. return ERROR_ADDRESS_ALREADY_ASSOCIATED;
  279. }
  280. //
  281. // Reference the interface
  282. //
  283. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  284. LeaveCriticalSection(&DhcpInterfaceLock);
  285. NhTrace(
  286. TRACE_FLAG_IF,
  287. "DhcpBindInterface: interface %d cannot be referenced",
  288. Index
  289. );
  290. return ERROR_INTERFACE_DISABLED;
  291. }
  292. //
  293. // Update the interface's flags
  294. //
  295. Interfacep->Flags |= DHCP_INTERFACE_FLAG_BOUND;
  296. LeaveCriticalSection(&DhcpInterfaceLock);
  297. ACQUIRE_LOCK(Interfacep);
  298. //
  299. // Allocate space for the binding
  300. //
  301. if (!BindingInfo->AddressCount) {
  302. Interfacep->BindingCount = 0;
  303. Interfacep->BindingArray = NULL;
  304. }
  305. else {
  306. Interfacep->BindingArray =
  307. reinterpret_cast<PDHCP_BINDING>(
  308. NH_ALLOCATE(BindingInfo->AddressCount * sizeof(DHCP_BINDING))
  309. );
  310. if (!Interfacep->BindingArray) {
  311. RELEASE_LOCK(Interfacep);
  312. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  313. NhTrace(
  314. TRACE_FLAG_IF,
  315. "DhcpBindInterface: allocation failed for interface %d binding",
  316. Index
  317. );
  318. NhWarningLog(
  319. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  320. 0,
  321. "%d",
  322. BindingInfo->AddressCount * sizeof(DHCP_BINDING)
  323. );
  324. return ERROR_NOT_ENOUGH_MEMORY;
  325. }
  326. Interfacep->BindingCount = BindingInfo->AddressCount;
  327. }
  328. //
  329. // Copy the binding
  330. //
  331. for (i = 0; i < BindingInfo->AddressCount; i++) {
  332. Interfacep->BindingArray[i].Address = BindingInfo->Address[i].Address;
  333. Interfacep->BindingArray[i].Mask = BindingInfo->Address[i].Mask;
  334. Interfacep->BindingArray[i].Socket = INVALID_SOCKET;
  335. Interfacep->BindingArray[i].ClientSocket = INVALID_SOCKET;
  336. Interfacep->BindingArray[i].TimerPending = FALSE;
  337. }
  338. RELEASE_LOCK(Interfacep);
  339. //
  340. // Activate the interface if necessary
  341. //
  342. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  343. Error = DhcpActivateInterface(Interfacep);
  344. }
  345. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  346. return Error;
  347. } // DhcpBindInterface
  348. VOID
  349. DhcpCleanupInterface(
  350. PDHCP_INTERFACE Interfacep
  351. )
  352. /*++
  353. Routine Description:
  354. This routine is invoked when the very last reference to an interface
  355. is released, and the interface must be destroyed.
  356. Arguments:
  357. Interfacep - the interface to be destroyed
  358. Return Value:
  359. none.
  360. Environment:
  361. Invoked internally from an arbitrary context.
  362. --*/
  363. {
  364. PROFILE("DhcpCleanupInterface");
  365. if (Interfacep->BindingArray) {
  366. NH_FREE(Interfacep->BindingArray);
  367. Interfacep->BindingArray = NULL;
  368. }
  369. DeleteCriticalSection(&Interfacep->Lock);
  370. NH_FREE(Interfacep);
  371. } // DhcpCleanupInterface
  372. ULONG
  373. DhcpConfigureInterface(
  374. ULONG Index,
  375. PIP_AUTO_DHCP_INTERFACE_INFO InterfaceInfo
  376. )
  377. /*++
  378. Routine Description:
  379. This routine is called to set the configuration for an interface.
  380. Arguments:
  381. Index - the interface to be configured
  382. InterfaceInfo - the new configuration
  383. Return Value:
  384. ULONG - Win32 status code
  385. Environment:
  386. Invoked internally in the context of a IP router-manager thread.
  387. (See 'RMDHCP.C').
  388. --*/
  389. {
  390. ULONG Error;
  391. PDHCP_INTERFACE Interfacep;
  392. ULONG NewFlags;
  393. ULONG OldFlags;
  394. PROFILE("DhcpConfigureInterface");
  395. //
  396. // Retrieve the interface to be configured
  397. //
  398. EnterCriticalSection(&DhcpInterfaceLock);
  399. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  400. LeaveCriticalSection(&DhcpInterfaceLock);
  401. NhTrace(
  402. TRACE_FLAG_IF,
  403. "DhcpConfigureInterface: interface %d not found",
  404. Index
  405. );
  406. return ERROR_NO_SUCH_INTERFACE;
  407. }
  408. //
  409. // Reference the interface
  410. //
  411. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  412. LeaveCriticalSection(&DhcpInterfaceLock);
  413. NhTrace(
  414. TRACE_FLAG_IF,
  415. "DhcpConfigureInterface: interface %d cannot be referenced",
  416. Index
  417. );
  418. return ERROR_INTERFACE_DISABLED;
  419. }
  420. LeaveCriticalSection(&DhcpInterfaceLock);
  421. Error = NO_ERROR;
  422. ACQUIRE_LOCK(Interfacep);
  423. //
  424. // Compare the interface's current and new configuration
  425. //
  426. OldFlags = Interfacep->Info.Flags;
  427. NewFlags =
  428. (InterfaceInfo
  429. ? (InterfaceInfo->Flags|DHCP_INTERFACE_FLAG_CONFIGURED) : 0);
  430. Interfacep->Flags &= ~OldFlags;
  431. Interfacep->Flags |= NewFlags;
  432. if (!InterfaceInfo) {
  433. ZeroMemory(&Interfacep->Info, sizeof(IP_AUTO_DHCP_INTERFACE_INFO));
  434. //
  435. // The interface no longer has any information;
  436. // default to being enabled.
  437. //
  438. if (OldFlags & IP_AUTO_DHCP_INTERFACE_FLAG_DISABLED) {
  439. //
  440. // Activate the interface if necessary
  441. //
  442. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  443. RELEASE_LOCK(Interfacep);
  444. Error = DhcpActivateInterface(Interfacep);
  445. ACQUIRE_LOCK(Interfacep);
  446. }
  447. }
  448. }
  449. else {
  450. CopyMemory(
  451. &Interfacep->Info,
  452. InterfaceInfo,
  453. sizeof(IP_AUTO_DHCP_INTERFACE_INFO)
  454. );
  455. //
  456. // Activate or deactivate the interface if its status changed
  457. //
  458. if ((OldFlags & IP_AUTO_DHCP_INTERFACE_FLAG_DISABLED) &&
  459. !(NewFlags & IP_AUTO_DHCP_INTERFACE_FLAG_DISABLED)) {
  460. //
  461. // Activate the interface
  462. //
  463. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  464. RELEASE_LOCK(Interfacep);
  465. Error = DhcpActivateInterface(Interfacep);
  466. ACQUIRE_LOCK(Interfacep);
  467. }
  468. }
  469. else
  470. if (!(OldFlags & IP_AUTO_DHCP_INTERFACE_FLAG_DISABLED) &&
  471. (NewFlags & IP_AUTO_DHCP_INTERFACE_FLAG_DISABLED)) {
  472. //
  473. // Deactivate the interface if necessary
  474. //
  475. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  476. RELEASE_LOCK(Interfacep);
  477. DhcpDeactivateInterface(Interfacep);
  478. ACQUIRE_LOCK(Interfacep);
  479. }
  480. }
  481. }
  482. RELEASE_LOCK(Interfacep);
  483. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  484. return Error;
  485. } // DhcpConfigureInterface
  486. ULONG
  487. DhcpCreateInterface(
  488. ULONG Index,
  489. NET_INTERFACE_TYPE Type,
  490. PIP_AUTO_DHCP_INTERFACE_INFO InterfaceInfo,
  491. OUT PDHCP_INTERFACE* InterfaceCreated
  492. )
  493. /*++
  494. Routine Description:
  495. This routine is invoked by the router-manager to add a new interface
  496. to the DHCP allocator.
  497. Arguments:
  498. Index - the index of the new interface
  499. Type - the media type of the new interface
  500. InterfaceInfo - the interface's configuration
  501. Interfacep - receives the interface created
  502. Return Value:
  503. ULONG - Win32 error code
  504. Environment:
  505. Invoked internally in the context of an IP router-manager thread.
  506. (See 'RMDHCP.C').
  507. --*/
  508. {
  509. PLIST_ENTRY InsertionPoint;
  510. PDHCP_INTERFACE Interfacep;
  511. PROFILE("DhcpCreateInterface");
  512. EnterCriticalSection(&DhcpInterfaceLock);
  513. //
  514. // See if the interface already exists;
  515. // If not, this obtains the insertion point
  516. //
  517. if (DhcpLookupInterface(Index, &InsertionPoint)) {
  518. LeaveCriticalSection(&DhcpInterfaceLock);
  519. NhTrace(
  520. TRACE_FLAG_IF,
  521. "DhcpCreateInterface: duplicate index found for %d",
  522. Index
  523. );
  524. return ERROR_INTERFACE_ALREADY_EXISTS;
  525. }
  526. //
  527. // Allocate a new interface
  528. //
  529. Interfacep = reinterpret_cast<PDHCP_INTERFACE>(
  530. NH_ALLOCATE(sizeof(DHCP_INTERFACE))
  531. );
  532. if (!Interfacep) {
  533. LeaveCriticalSection(&DhcpInterfaceLock);
  534. NhTrace(
  535. TRACE_FLAG_IF, "DhcpCreateInterface: error allocating interface"
  536. );
  537. NhWarningLog(
  538. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  539. 0,
  540. "%d",
  541. sizeof(DHCP_INTERFACE)
  542. );
  543. return ERROR_NOT_ENOUGH_MEMORY;
  544. }
  545. //
  546. // Initialize the new interface
  547. //
  548. ZeroMemory(Interfacep, sizeof(*Interfacep));
  549. __try {
  550. InitializeCriticalSection(&Interfacep->Lock);
  551. }
  552. __except(EXCEPTION_EXECUTE_HANDLER) {
  553. LeaveCriticalSection(&DhcpInterfaceLock);
  554. NH_FREE(Interfacep);
  555. return GetExceptionCode();
  556. }
  557. Interfacep->Index = Index;
  558. Interfacep->Type = Type;
  559. if (InterfaceInfo) {
  560. Interfacep->Flags = InterfaceInfo->Flags|DHCP_INTERFACE_FLAG_CONFIGURED;
  561. CopyMemory(&Interfacep->Info, InterfaceInfo, sizeof(*InterfaceInfo));
  562. }
  563. Interfacep->ReferenceCount = 1;
  564. InsertTailList(InsertionPoint, &Interfacep->Link);
  565. LeaveCriticalSection(&DhcpInterfaceLock);
  566. if (InterfaceCreated) { *InterfaceCreated = Interfacep; }
  567. return NO_ERROR;
  568. } // DhcpCreateInterface
  569. VOID
  570. DhcpDeactivateInterface(
  571. PDHCP_INTERFACE Interfacep
  572. )
  573. /*++
  574. Routine Description:
  575. This routine is called to deactivate an interface.
  576. It closes all sockets on the interface's bindings (if any).
  577. Arguments:
  578. Interfacep - the interface to be deactivated
  579. Return Value:
  580. none.
  581. Environment:
  582. Always invoked locally, with 'Interfacep' referenced by caller and/or
  583. 'DhcpInterfaceLock' held by caller.
  584. --*/
  585. {
  586. ULONG i;
  587. PROFILE("DhcpDeactivateInterface");
  588. //
  589. // Stop all network I/O on the interface's logical networks
  590. //
  591. ACQUIRE_LOCK(Interfacep);
  592. for (i = 0; i < Interfacep->BindingCount; i++) {
  593. if (Interfacep->BindingArray[i].Socket != INVALID_SOCKET) {
  594. NhDeleteDatagramSocket(Interfacep->BindingArray[i].Socket);
  595. Interfacep->BindingArray[i].Socket = INVALID_SOCKET;
  596. }
  597. if (Interfacep->BindingArray[i].ClientSocket != INVALID_SOCKET) {
  598. NhDeleteDatagramSocket(Interfacep->BindingArray[i].ClientSocket);
  599. Interfacep->BindingArray[i].ClientSocket = INVALID_SOCKET;
  600. }
  601. }
  602. //
  603. // clear interface status as a non boundary NAT interface
  604. //
  605. Interfacep->Flags &= ~DHCP_INTERFACE_FLAG_NAT_NONBOUNDARY;
  606. RELEASE_LOCK(Interfacep);
  607. } // DhcpDeactivateInterface
  608. VOID NTAPI
  609. DhcpDeferReadCallbackRoutine(
  610. PVOID Context,
  611. BOOLEAN TimedOut
  612. )
  613. /*++
  614. Routine Description:
  615. This routine is invoked to re-issue a deferred read when the countdown
  616. for the deferral completes.
  617. Arguments:
  618. Context - holds information identifying the interface and socket
  619. TimedOut - indicates whether the countdown completed
  620. Return Value:
  621. none.
  622. Environment:
  623. Invoked with an outstanding reference to the component on our behalf.
  624. --*/
  625. {
  626. PDHCP_DEFER_READ_CONTEXT Contextp;
  627. ULONG Error;
  628. ULONG i;
  629. PDHCP_INTERFACE Interfacep;
  630. NTSTATUS status;
  631. PROFILE("DhcpDeferReadCallbackRoutine");
  632. Contextp = (PDHCP_DEFER_READ_CONTEXT)Context;
  633. //
  634. // Find the interface on which the read was deferred
  635. //
  636. EnterCriticalSection(&DhcpInterfaceLock);
  637. Interfacep = DhcpLookupInterface(Contextp->Index, NULL);
  638. if (!Interfacep ||
  639. !DHCP_INTERFACE_ACTIVE(Interfacep) ||
  640. !DHCP_REFERENCE_INTERFACE(Interfacep)) {
  641. LeaveCriticalSection(&DhcpInterfaceLock);
  642. NH_FREE(Contextp);
  643. DEREFERENCE_DHCP();
  644. return;
  645. }
  646. LeaveCriticalSection(&DhcpInterfaceLock);
  647. ACQUIRE_LOCK(Interfacep);
  648. //
  649. // Search for the socket on which to reissue the read
  650. //
  651. for (i = 0; i < Interfacep->BindingCount; i++) {
  652. if (Interfacep->BindingArray[i].Socket != Contextp->Socket) {continue;}
  653. //
  654. // This is the binding on which to reissue the read.
  655. // If no pending timer is recorded, assume a rebind occurred, and quit.
  656. //
  657. if (!Interfacep->BindingArray[i].TimerPending) { break; }
  658. Interfacep->BindingArray[i].TimerPending = FALSE;
  659. Error =
  660. NhReadDatagramSocket(
  661. &DhcpComponentReference,
  662. Interfacep->BindingArray[i].Socket,
  663. NULL,
  664. DhcpReadCompletionRoutine,
  665. Interfacep,
  666. UlongToPtr(Interfacep->BindingArray[i].Mask)
  667. );
  668. RELEASE_LOCK(Interfacep);
  669. if (!Error) {
  670. NH_FREE(Contextp);
  671. DEREFERENCE_DHCP();
  672. return;
  673. }
  674. //
  675. // An error occurred; we'll have to retry later.
  676. // we queue a work item which sets the timer.
  677. //
  678. NhTrace(
  679. TRACE_FLAG_DHCP,
  680. "DhcpDeferReadCallbackRoutine: error %d reading interface %d",
  681. Error,
  682. Interfacep->Index
  683. );
  684. //
  685. // Reference the component on behalf of the work-item
  686. //
  687. if (REFERENCE_DHCP()) {
  688. //
  689. // Queue a work-item, reusing the deferral context
  690. //
  691. status =
  692. RtlQueueWorkItem(
  693. DhcpDeferReadWorkerRoutine,
  694. Contextp,
  695. WT_EXECUTEINIOTHREAD
  696. );
  697. if (NT_SUCCESS(status)) {
  698. Contextp = NULL;
  699. }
  700. else {
  701. NH_FREE(Contextp);
  702. NhTrace(
  703. TRACE_FLAG_DHCP,
  704. "DhcpDeferReadCallbackRoutine: error %d deferring %d",
  705. Error,
  706. Interfacep->Index
  707. );
  708. DEREFERENCE_DHCP();
  709. }
  710. }
  711. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  712. DEREFERENCE_DHCP();
  713. return;
  714. }
  715. //
  716. // The interface was not found; never mind.
  717. //
  718. RELEASE_LOCK(Interfacep);
  719. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  720. NH_FREE(Contextp);
  721. DEREFERENCE_DHCP();
  722. } // DhcpDeferReadCallbackRoutine
  723. VOID
  724. DhcpDeferReadInterface(
  725. PDHCP_INTERFACE Interfacep,
  726. SOCKET Socket
  727. )
  728. /*++
  729. Routine Description:
  730. This routine is invoked to defer a read-request on an interface,
  731. typically if an attempt to post a read failed.
  732. Arguments:
  733. Interfacep - the interface on which to defer the request
  734. Socket - the socket on which to defer the request
  735. Return Value:
  736. none.
  737. Environment:
  738. Invoked with 'Interfacep' referenced and locked by the caller.
  739. The caller may release the reference upon return.
  740. --*/
  741. {
  742. PDHCP_DEFER_READ_CONTEXT Contextp;
  743. ULONG i;
  744. NTSTATUS status;
  745. PROFILE("DhcpDeferReadInterface");
  746. //
  747. // Find the binding for the given socket.
  748. //
  749. status = STATUS_SUCCESS;
  750. for (i = 0; i < Interfacep->BindingCount; i++) {
  751. if (Interfacep->BindingArray[i].Socket != Socket) { continue; }
  752. //
  753. // This is the binding. If there is already a timer for it,
  754. // then just return silently.
  755. //
  756. if (Interfacep->BindingArray[i].TimerPending) {
  757. status = STATUS_UNSUCCESSFUL;
  758. break;
  759. }
  760. //
  761. // Allocate a context block for the deferral.
  762. //
  763. Contextp =
  764. (PDHCP_DEFER_READ_CONTEXT)
  765. NH_ALLOCATE(sizeof(DHCP_DEFER_READ_CONTEXT));
  766. if (!Contextp) {
  767. NhTrace(
  768. TRACE_FLAG_DHCP,
  769. "DhcpDeferReadInterface: cannot allocate deferral context"
  770. );
  771. status = STATUS_NO_MEMORY;
  772. break;
  773. }
  774. Contextp->Index = Interfacep->Index;
  775. Contextp->Socket = Socket;
  776. Contextp->DeferralCount = 1;
  777. //
  778. // Install a timer to re-issue the read request
  779. //
  780. status =
  781. NhSetTimer(
  782. &DhcpComponentReference,
  783. NULL,
  784. DhcpDeferReadCallbackRoutine,
  785. Contextp,
  786. DHCP_DEFER_READ_TIMEOUT
  787. );
  788. if (NT_SUCCESS(status)) {
  789. Interfacep->BindingArray[i].TimerPending = TRUE;
  790. }
  791. else {
  792. NH_FREE(Contextp);
  793. NhTrace(
  794. TRACE_FLAG_DHCP,
  795. "DhcpDeferReadInterface: status %08x setting deferral timer",
  796. status
  797. );
  798. }
  799. break;
  800. }
  801. if (i >= Interfacep->BindingCount) { status = STATUS_UNSUCCESSFUL; }
  802. } // DhcpDeferReadInterface
  803. VOID APIENTRY
  804. DhcpDeferReadWorkerRoutine(
  805. PVOID Context
  806. )
  807. /*++
  808. Routine Description:
  809. This routine is invoked to set a timer for reissuing a deferred read.
  810. Arguments:
  811. Context - contains the context for the timer.
  812. Return Value:
  813. none.
  814. Environment:
  815. Invoked with an outstanding reference to the module made on our behalf.
  816. --*/
  817. {
  818. PDHCP_DEFER_READ_CONTEXT Contextp;
  819. ULONG i;
  820. PDHCP_INTERFACE Interfacep;
  821. NTSTATUS status;
  822. PROFILE("DhcpDeferReadWorkerRoutine");
  823. Contextp = (PDHCP_DEFER_READ_CONTEXT)Context;
  824. ++Contextp->DeferralCount;
  825. //
  826. // Find the interface on which the read was deferred
  827. //
  828. EnterCriticalSection(&DhcpInterfaceLock);
  829. Interfacep = DhcpLookupInterface(Contextp->Index, NULL);
  830. if (!Interfacep ||
  831. !DHCP_INTERFACE_ACTIVE(Interfacep) ||
  832. !DHCP_REFERENCE_INTERFACE(Interfacep)) {
  833. LeaveCriticalSection(&DhcpInterfaceLock);
  834. NH_FREE(Contextp);
  835. DEREFERENCE_DHCP();
  836. return;
  837. }
  838. LeaveCriticalSection(&DhcpInterfaceLock);
  839. ACQUIRE_LOCK(Interfacep);
  840. //
  841. // Search for the binding on which to set the timer
  842. //
  843. for (i = 0; i < Interfacep->BindingCount; i++) {
  844. if (Interfacep->BindingArray[i].Socket != Contextp->Socket) {continue;}
  845. //
  846. // This is the binding on which to reissue the read.
  847. // If a timer is already pending, assume a rebind occurred, and quit.
  848. //
  849. if (Interfacep->BindingArray[i].TimerPending) { break; }
  850. //
  851. // Install a timer to re-issue the read request,
  852. // reusing the deferral context.
  853. //
  854. status =
  855. NhSetTimer(
  856. &DhcpComponentReference,
  857. NULL,
  858. DhcpDeferReadCallbackRoutine,
  859. Contextp,
  860. DHCP_DEFER_READ_TIMEOUT
  861. );
  862. if (NT_SUCCESS(status)) {
  863. Contextp = NULL;
  864. Interfacep->BindingArray[i].TimerPending = TRUE;
  865. }
  866. else {
  867. NhTrace(
  868. TRACE_FLAG_DHCP,
  869. "DhcpDeferReadWorkerRoutine: status %08x setting timer",
  870. status
  871. );
  872. }
  873. }
  874. RELEASE_LOCK(Interfacep);
  875. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  876. if (Contextp) { NH_FREE(Contextp); }
  877. DEREFERENCE_DHCP();
  878. } // DhcpDeferReadWorkerRoutine
  879. ULONG
  880. DhcpDeleteInterface(
  881. ULONG Index
  882. )
  883. /*++
  884. Routine Description:
  885. This routine is called to delete an interface.
  886. It drops the reference count on the interface so that the last
  887. dereferencer will delete the interface, and sets the 'deleted' flag
  888. so that further references to the interface will fail.
  889. Arguments:
  890. Index - the index of the interface to be deleted
  891. Return Value:
  892. ULONG - Win32 status code.
  893. Environment:
  894. Invoked internally in the context of an IP router-manager thread.
  895. (See 'RMDHCP.C').
  896. --*/
  897. {
  898. PDHCP_INTERFACE Interfacep;
  899. PROFILE("DhcpDeleteInterface");
  900. //
  901. // Retrieve the interface to be deleted
  902. //
  903. EnterCriticalSection(&DhcpInterfaceLock);
  904. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  905. LeaveCriticalSection(&DhcpInterfaceLock);
  906. NhTrace(
  907. TRACE_FLAG_IF,
  908. "DhcpDeleteInterface: interface %d not found",
  909. Index
  910. );
  911. return ERROR_NO_SUCH_INTERFACE;
  912. }
  913. //
  914. // Make sure it isn't already deleted
  915. //
  916. if (DHCP_INTERFACE_DELETED(Interfacep)) {
  917. LeaveCriticalSection(&DhcpInterfaceLock);
  918. NhTrace(
  919. TRACE_FLAG_IF,
  920. "DhcpDeleteInterface: interface %d already deleted",
  921. Index
  922. );
  923. return ERROR_NO_SUCH_INTERFACE;
  924. }
  925. //
  926. // Deactivate the interface
  927. //
  928. DhcpDeactivateInterface(Interfacep);
  929. //
  930. // Mark the interface as deleted and take it off the interface list
  931. //
  932. Interfacep->Flags |= DHCP_INTERFACE_FLAG_DELETED;
  933. Interfacep->Flags &= ~DHCP_INTERFACE_FLAG_ENABLED;
  934. RemoveEntryList(&Interfacep->Link);
  935. //
  936. // Drop the reference count; if it is non-zero,
  937. // the deletion will complete later.
  938. //
  939. if (--Interfacep->ReferenceCount) {
  940. LeaveCriticalSection(&DhcpInterfaceLock);
  941. NhTrace(
  942. TRACE_FLAG_IF,
  943. "DhcpDeleteInterface: interface %d deletion pending",
  944. Index
  945. );
  946. return NO_ERROR;
  947. }
  948. //
  949. // The reference count is zero, so perform final cleanup
  950. //
  951. DhcpCleanupInterface(Interfacep);
  952. LeaveCriticalSection(&DhcpInterfaceLock);
  953. return NO_ERROR;
  954. } // DhcpDeleteInterface
  955. ULONG
  956. DhcpDisableInterface(
  957. ULONG Index
  958. )
  959. /*++
  960. Routine Description:
  961. This routine is called to disable I/O on an interface.
  962. If the interface is active, it is deactivated.
  963. Arguments:
  964. Index - the index of the interface to be disabled.
  965. Return Value:
  966. none.
  967. Environment:
  968. Invoked internally in the context of an IP router-manager thread.
  969. (See 'RMDHCP.C').
  970. --*/
  971. {
  972. PDHCP_INTERFACE Interfacep;
  973. PROFILE("DhcpDisableInterface");
  974. //
  975. // Retrieve the interface to be disabled
  976. //
  977. EnterCriticalSection(&DhcpInterfaceLock);
  978. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  979. LeaveCriticalSection(&DhcpInterfaceLock);
  980. NhTrace(
  981. TRACE_FLAG_IF,
  982. "DhcpDisableInterface: interface %d not found",
  983. Index
  984. );
  985. return ERROR_NO_SUCH_INTERFACE;
  986. }
  987. //
  988. // Make sure the interface is not already disabled
  989. //
  990. if (!DHCP_INTERFACE_ENABLED(Interfacep)) {
  991. LeaveCriticalSection(&DhcpInterfaceLock);
  992. NhTrace(
  993. TRACE_FLAG_IF,
  994. "DhcpDisableInterface: interface %d already disabled",
  995. Index
  996. );
  997. return ERROR_INTERFACE_DISABLED;
  998. }
  999. //
  1000. // Reference the interface
  1001. //
  1002. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1003. LeaveCriticalSection(&DhcpInterfaceLock);
  1004. NhTrace(
  1005. TRACE_FLAG_IF,
  1006. "DhcpDisableInterface: interface %d cannot be referenced",
  1007. Index
  1008. );
  1009. return ERROR_INTERFACE_DISABLED;
  1010. }
  1011. //
  1012. // Clear the 'enabled' flag
  1013. //
  1014. Interfacep->Flags &= ~DHCP_INTERFACE_FLAG_ENABLED;
  1015. //
  1016. // Deactivate the interface, if necessary
  1017. //
  1018. if (DHCP_INTERFACE_BOUND(Interfacep)) {
  1019. DhcpDeactivateInterface(Interfacep);
  1020. }
  1021. LeaveCriticalSection(&DhcpInterfaceLock);
  1022. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1023. return NO_ERROR;
  1024. } // DhcpDisableInterface
  1025. ULONG
  1026. DhcpEnableInterface(
  1027. ULONG Index
  1028. )
  1029. /*++
  1030. Routine Description:
  1031. This routine is called to enable I/O on an interface.
  1032. If the interface is already bound, this enabling activates it.
  1033. Arguments:
  1034. Index - the index of the interfaec to be enabled
  1035. Return Value:
  1036. ULONG - Win32 status code.
  1037. Environment:
  1038. Invoked internally in the context of an IP router-manager thread.
  1039. (See 'RMDHCP.C').
  1040. --*/
  1041. {
  1042. ULONG Error = NO_ERROR;
  1043. PDHCP_INTERFACE Interfacep;
  1044. PROFILE("DhcpEnableInterface");
  1045. //
  1046. // Retrieve the interface to be enabled
  1047. //
  1048. EnterCriticalSection(&DhcpInterfaceLock);
  1049. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  1050. LeaveCriticalSection(&DhcpInterfaceLock);
  1051. NhTrace(
  1052. TRACE_FLAG_IF,
  1053. "DhcpEnableInterface: interface %d not found",
  1054. Index
  1055. );
  1056. return ERROR_NO_SUCH_INTERFACE;
  1057. }
  1058. //
  1059. // Make sure the interface is not already enabled
  1060. //
  1061. if (DHCP_INTERFACE_ENABLED(Interfacep)) {
  1062. LeaveCriticalSection(&DhcpInterfaceLock);
  1063. NhTrace(
  1064. TRACE_FLAG_IF,
  1065. "DhcpEnableInterface: interface %d already enabled",
  1066. Index
  1067. );
  1068. return ERROR_INTERFACE_ALREADY_EXISTS;
  1069. }
  1070. //
  1071. // Reference the interface
  1072. //
  1073. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1074. LeaveCriticalSection(&DhcpInterfaceLock);
  1075. NhTrace(
  1076. TRACE_FLAG_IF,
  1077. "DhcpEnableInterface: interface %d cannot be referenced",
  1078. Index
  1079. );
  1080. return ERROR_INTERFACE_DISABLED;
  1081. }
  1082. //
  1083. // Set the 'enabled' flag
  1084. //
  1085. Interfacep->Flags |= DHCP_INTERFACE_FLAG_ENABLED;
  1086. //
  1087. // Activate the interface, if necessary
  1088. //
  1089. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  1090. Error = DhcpActivateInterface(Interfacep);
  1091. }
  1092. LeaveCriticalSection(&DhcpInterfaceLock);
  1093. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1094. return Error;
  1095. } // DhcpEnableInterface
  1096. ULONG
  1097. DhcpInitializeInterfaceManagement(
  1098. VOID
  1099. )
  1100. /*++
  1101. Routine Description:
  1102. This routine is called to initialize the interface-management module.
  1103. Arguments:
  1104. none.
  1105. Return Value:
  1106. ULONG - Win32 status code.
  1107. Environment:
  1108. Invoked internally in the context of an IP router-manager thread.
  1109. (See 'RMDHCP.C').
  1110. --*/
  1111. {
  1112. ULONG Error = NO_ERROR;
  1113. PROFILE("DhcpInitializeInterfaceManagement");
  1114. InitializeListHead(&DhcpInterfaceList);
  1115. __try {
  1116. InitializeCriticalSection(&DhcpInterfaceLock);
  1117. }
  1118. __except(EXCEPTION_EXECUTE_HANDLER) {
  1119. NhTrace(
  1120. TRACE_FLAG_IF,
  1121. "DhcpInitializeInterfaceManagement: exception %d creating lock",
  1122. Error = GetExceptionCode()
  1123. );
  1124. }
  1125. return Error;
  1126. } // DhcpInitializeInterfaceManagement
  1127. BOOLEAN
  1128. DhcpIsLocalHardwareAddress(
  1129. PUCHAR HardwareAddress,
  1130. ULONG HardwareAddressLength
  1131. )
  1132. /*++
  1133. Routine Description:
  1134. This routine is invoked to determine whether the given hardware address
  1135. is for a local interface.
  1136. Arguments:
  1137. HardwareAddress - the hardware address to find
  1138. HardwareAddressLength - the length of the hardware address in bytes
  1139. Return Value:
  1140. BOOLEAN - TRUE if the address is found, FALSE otherwise
  1141. --*/
  1142. {
  1143. ULONG Error;
  1144. ULONG i;
  1145. PMIB_IFTABLE Table;
  1146. //
  1147. // if the hardware address length is zero, assume external address
  1148. //
  1149. if (!HardwareAddressLength)
  1150. {
  1151. return FALSE;
  1152. }
  1153. Error =
  1154. AllocateAndGetIfTableFromStack(
  1155. &Table, FALSE, GetProcessHeap(), 0, FALSE
  1156. );
  1157. if (Error) {
  1158. NhTrace(
  1159. TRACE_FLAG_IF,
  1160. "DhcpIsLocalHardwareAddress: GetIfTableFromStack=%d", Error
  1161. );
  1162. return FALSE;
  1163. }
  1164. for (i = 0; i < Table->dwNumEntries; i++) {
  1165. if (Table->table[i].dwPhysAddrLen == HardwareAddressLength &&
  1166. memcmp(
  1167. Table->table[i].bPhysAddr,
  1168. HardwareAddress,
  1169. HardwareAddressLength
  1170. ) == 0) {
  1171. HeapFree(GetProcessHeap(), 0, Table);
  1172. return TRUE;
  1173. }
  1174. }
  1175. HeapFree(GetProcessHeap(), 0, Table);
  1176. return FALSE;
  1177. } // DhcpIsLocalHardwareAddress
  1178. PDHCP_INTERFACE
  1179. DhcpLookupInterface(
  1180. ULONG Index,
  1181. OUT PLIST_ENTRY* InsertionPoint OPTIONAL
  1182. )
  1183. /*++
  1184. Routine Description:
  1185. This routine is called to retrieve an interface given its index.
  1186. Arguments:
  1187. Index - the index of the interface to be retrieved
  1188. InsertionPoint - if the interface is not found, optionally receives
  1189. the point where the interface would be inserted in the interface list
  1190. Return Value:
  1191. PDHCP_INTERFACE - the interface, if found; otherwise, NULL.
  1192. Environment:
  1193. Invoked internally from an arbitrary context, with 'DhcpInterfaceLock'
  1194. held by caller.
  1195. --*/
  1196. {
  1197. PDHCP_INTERFACE Interfacep;
  1198. PLIST_ENTRY Link;
  1199. PROFILE("DhcpLookupInterface");
  1200. for (Link = DhcpInterfaceList.Flink;
  1201. Link != &DhcpInterfaceList;
  1202. Link = Link->Flink
  1203. ) {
  1204. Interfacep = CONTAINING_RECORD(Link, DHCP_INTERFACE, Link);
  1205. if (Index > Interfacep->Index) { continue; }
  1206. else
  1207. if (Index < Interfacep->Index) { break; }
  1208. return Interfacep;
  1209. }
  1210. if (InsertionPoint) { *InsertionPoint = Link; }
  1211. return NULL;
  1212. } // DhcpLookupInterface
  1213. ULONG
  1214. DhcpQueryInterface(
  1215. ULONG Index,
  1216. PVOID InterfaceInfo,
  1217. PULONG InterfaceInfoSize
  1218. )
  1219. /*++
  1220. Routine Description:
  1221. This routine is invoked to retrieve the configuration for an interface.
  1222. Arguments:
  1223. Index - the interface to be queried
  1224. InterfaceInfo - receives the retrieved information
  1225. InterfaceInfoSize - receives the (required) size of the information
  1226. Return Value:
  1227. ULONG - Win32 status code.
  1228. --*/
  1229. {
  1230. PDHCP_INTERFACE Interfacep;
  1231. PROFILE("DhcpQueryInterface");
  1232. //
  1233. // Check the caller's buffer size
  1234. //
  1235. if (!InterfaceInfoSize) { return ERROR_INVALID_PARAMETER; }
  1236. //
  1237. // Retrieve the interface to be configured
  1238. //
  1239. EnterCriticalSection(&DhcpInterfaceLock);
  1240. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  1241. LeaveCriticalSection(&DhcpInterfaceLock);
  1242. NhTrace(
  1243. TRACE_FLAG_IF,
  1244. "DhcpQueryInterface: interface %d not found",
  1245. Index
  1246. );
  1247. return ERROR_NO_SUCH_INTERFACE;
  1248. }
  1249. //
  1250. // Reference the interface
  1251. //
  1252. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1253. LeaveCriticalSection(&DhcpInterfaceLock);
  1254. NhTrace(
  1255. TRACE_FLAG_IF,
  1256. "DhcpQueryInterface: interface %d cannot be referenced",
  1257. Index
  1258. );
  1259. return ERROR_INTERFACE_DISABLED;
  1260. }
  1261. //
  1262. // See if there is any explicit config on this interface
  1263. //
  1264. if (!DHCP_INTERFACE_CONFIGURED(Interfacep)) {
  1265. LeaveCriticalSection(&DhcpInterfaceLock);
  1266. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1267. NhTrace(
  1268. TRACE_FLAG_IF,
  1269. "DhcpQueryInterface: interface %d has no configuration",
  1270. Index
  1271. );
  1272. *InterfaceInfoSize = 0;
  1273. return NO_ERROR;
  1274. }
  1275. if (*InterfaceInfoSize < sizeof(IP_AUTO_DHCP_INTERFACE_INFO)) {
  1276. LeaveCriticalSection(&DhcpInterfaceLock);
  1277. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1278. *InterfaceInfoSize = sizeof(IP_AUTO_DHCP_INTERFACE_INFO);
  1279. return ERROR_INSUFFICIENT_BUFFER;
  1280. }
  1281. //
  1282. // Copy the requested data
  1283. //
  1284. CopyMemory(
  1285. InterfaceInfo,
  1286. &Interfacep->Info,
  1287. sizeof(IP_AUTO_DHCP_INTERFACE_INFO)
  1288. );
  1289. *InterfaceInfoSize = sizeof(IP_AUTO_DHCP_INTERFACE_INFO);
  1290. LeaveCriticalSection(&DhcpInterfaceLock);
  1291. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1292. return NO_ERROR;
  1293. } // DhcpQueryInterface
  1294. VOID
  1295. DhcpReactivateEveryInterface(
  1296. VOID
  1297. )
  1298. /*++
  1299. Routine Description:
  1300. This routine is called to reactivate all activate interfaces
  1301. when a change occurs to the global DHCP configuration.
  1302. Thus if, for instance, the scope network has been changed and is now
  1303. either valid or invalid, during deactivation all sockets are closed,
  1304. and during reactivation they are or are not reopened as appropriate.
  1305. depending on the validity or invalidity of the new configuration.
  1306. Arguments:
  1307. none.
  1308. Return Value:
  1309. none.
  1310. Environment:
  1311. Invoked from a router-manager thread with no locks held.
  1312. --*/
  1313. {
  1314. PDHCP_INTERFACE Interfacep;
  1315. PLIST_ENTRY Link;
  1316. PROFILE("DhcpReactivateEveryInterface");
  1317. EnterCriticalSection(&DhcpInterfaceLock);
  1318. for (Link = DhcpInterfaceList.Flink; Link != &DhcpInterfaceList;
  1319. Link = Link->Flink) {
  1320. Interfacep = CONTAINING_RECORD(Link, DHCP_INTERFACE, Link);
  1321. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) { continue; }
  1322. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  1323. DhcpDeactivateInterface(Interfacep);
  1324. DhcpActivateInterface(Interfacep);
  1325. }
  1326. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1327. }
  1328. LeaveCriticalSection(&DhcpInterfaceLock);
  1329. } // DhcpReactivateEveryInterface
  1330. VOID
  1331. DhcpShutdownInterfaceManagement(
  1332. VOID
  1333. )
  1334. /*++
  1335. Routine Description:
  1336. This routine is called to shutdown the interface-management module.
  1337. Arguments:
  1338. none.
  1339. Return Value:
  1340. none.
  1341. Environment:
  1342. Invoked internally in an arbitrary thread context,
  1343. after all references to all interfaces have been released.
  1344. --*/
  1345. {
  1346. PDHCP_INTERFACE Interfacep;
  1347. PLIST_ENTRY Link;
  1348. PROFILE("DhcpShutdownInterfaceManagement");
  1349. while (!IsListEmpty(&DhcpInterfaceList)) {
  1350. Link = RemoveHeadList(&DhcpInterfaceList);
  1351. Interfacep = CONTAINING_RECORD(Link, DHCP_INTERFACE, Link);
  1352. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  1353. DhcpDeactivateInterface(Interfacep);
  1354. }
  1355. DhcpCleanupInterface(Interfacep);
  1356. }
  1357. DeleteCriticalSection(&DhcpInterfaceLock);
  1358. } // DhcpShutdownInterfaceManagement
  1359. VOID
  1360. DhcpSignalNatInterface(
  1361. ULONG Index,
  1362. BOOLEAN Boundary
  1363. )
  1364. /*++
  1365. Routine Description:
  1366. This routine is invoked upon reconfiguration of a NAT interface.
  1367. Note that this routine may be invoked even when the DHCP allocator
  1368. is neither installed nor running; it operates as expected,
  1369. since the interface list and lock are always initialized.
  1370. Arguments:
  1371. Index - the reconfigured interface
  1372. Boundary - indicates whether the interface is now a boundary interface
  1373. Return Value:
  1374. none.
  1375. Environment:
  1376. Invoked from an arbitrary context.
  1377. --*/
  1378. {
  1379. PDHCP_INTERFACE Interfacep;
  1380. PROFILE("DhcpSignalNatInterface");
  1381. EnterCriticalSection(&DhcpGlobalInfoLock);
  1382. if (!DhcpGlobalInfo) {
  1383. LeaveCriticalSection(&DhcpGlobalInfoLock);
  1384. return;
  1385. }
  1386. LeaveCriticalSection(&DhcpGlobalInfoLock);
  1387. EnterCriticalSection(&DhcpInterfaceLock);
  1388. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  1389. LeaveCriticalSection(&DhcpInterfaceLock);
  1390. return;
  1391. }
  1392. DhcpDeactivateInterface(Interfacep);
  1393. if (DHCP_INTERFACE_ACTIVE(Interfacep)) {
  1394. DhcpActivateInterface(Interfacep);
  1395. }
  1396. LeaveCriticalSection(&DhcpInterfaceLock);
  1397. } // DhcpSignalNatInterface
  1398. ULONG
  1399. DhcpUnbindInterface(
  1400. ULONG Index
  1401. )
  1402. /*++
  1403. Routine Description:
  1404. This routine is invoked to revoke the binding on an interface.
  1405. This involves deactivating the interface if it is active.
  1406. Arguments:
  1407. Index - the index of the interface to be unbound
  1408. Return Value:
  1409. none.
  1410. Environment:
  1411. Invoked internally in the context of an IP router-manager thread.
  1412. (See 'RMDHCP.C').
  1413. --*/
  1414. {
  1415. PDHCP_INTERFACE Interfacep;
  1416. PROFILE("DhcpUnbindInterface");
  1417. //
  1418. // Retrieve the interface to be unbound
  1419. //
  1420. EnterCriticalSection(&DhcpInterfaceLock);
  1421. if (!(Interfacep = DhcpLookupInterface(Index, NULL))) {
  1422. LeaveCriticalSection(&DhcpInterfaceLock);
  1423. NhTrace(
  1424. TRACE_FLAG_IF,
  1425. "DhcpUnbindInterface: interface %d not found",
  1426. Index
  1427. );
  1428. return ERROR_NO_SUCH_INTERFACE;
  1429. }
  1430. //
  1431. // Make sure the interface is not already unbound
  1432. //
  1433. if (!DHCP_INTERFACE_BOUND(Interfacep)) {
  1434. LeaveCriticalSection(&DhcpInterfaceLock);
  1435. NhTrace(
  1436. TRACE_FLAG_IF,
  1437. "DhcpUnbindInterface: interface %d already unbound",
  1438. Index
  1439. );
  1440. return ERROR_ADDRESS_NOT_ASSOCIATED;
  1441. }
  1442. //
  1443. // Reference the interface
  1444. //
  1445. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1446. LeaveCriticalSection(&DhcpInterfaceLock);
  1447. NhTrace(
  1448. TRACE_FLAG_IF,
  1449. "DhcpUnbindInterface: interface %d cannot be referenced",
  1450. Index
  1451. );
  1452. return ERROR_INTERFACE_DISABLED;
  1453. }
  1454. //
  1455. // Clear the 'bound' flag
  1456. //
  1457. Interfacep->Flags &= ~DHCP_INTERFACE_FLAG_BOUND;
  1458. //
  1459. // Deactivate the interface, if necessary
  1460. //
  1461. if (DHCP_INTERFACE_ENABLED(Interfacep)) {
  1462. DhcpDeactivateInterface(Interfacep);
  1463. }
  1464. LeaveCriticalSection(&DhcpInterfaceLock);
  1465. //
  1466. // Destroy the interface's binding
  1467. //
  1468. ACQUIRE_LOCK(Interfacep);
  1469. NH_FREE(Interfacep->BindingArray);
  1470. Interfacep->BindingArray = NULL;
  1471. Interfacep->BindingCount = 0;
  1472. RELEASE_LOCK(Interfacep);
  1473. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1474. return NO_ERROR;
  1475. } // DhcpUnbindInterface
  1476. ULONG
  1477. DhcpGetPrivateInterfaceAddress(
  1478. VOID
  1479. )
  1480. /*++
  1481. Routine Description:
  1482. This routine is invoked to return the IP address on which DHCP
  1483. has been enabled (and which matches the scope net and mask).
  1484. Arguments:
  1485. none.
  1486. Return Value:
  1487. Bound IP address if an address is found (else 0).
  1488. Environment:
  1489. Invoked from an arbitrary context.
  1490. --*/
  1491. {
  1492. PROFILE("DhcpGetPrivateInterfaceAddress");
  1493. ULONG ipAddr = 0;
  1494. ULONG ulRet = NO_ERROR;
  1495. //
  1496. // Find out the interface on which we are enabled and
  1497. // return the primary IP address to which we are bound.
  1498. // (Try to match the scope to the IP address.)
  1499. //
  1500. PDHCP_INTERFACE Interfacep = NULL;
  1501. PLIST_ENTRY Link;
  1502. ULONG i;
  1503. //
  1504. // Get Scope information from DHCP Global Info
  1505. //
  1506. ULONG ScopeNetwork = 0;
  1507. ULONG ScopeMask = 0;
  1508. EnterCriticalSection(&DhcpGlobalInfoLock);
  1509. //
  1510. // Check to see if we have been initialized
  1511. //
  1512. if (!DhcpGlobalInfo)
  1513. {
  1514. LeaveCriticalSection(&DhcpGlobalInfoLock);
  1515. return ipAddr;
  1516. }
  1517. ScopeNetwork = DhcpGlobalInfo->ScopeNetwork;
  1518. ScopeMask = DhcpGlobalInfo->ScopeMask;
  1519. LeaveCriticalSection(&DhcpGlobalInfoLock);
  1520. EnterCriticalSection(&DhcpInterfaceLock);
  1521. if (ScopeNetwork && ScopeMask)
  1522. {
  1523. ULONG NetAddress = ScopeNetwork & ScopeMask;
  1524. //
  1525. // Search & Retrieve the interface to be configured
  1526. //
  1527. for (Link = DhcpInterfaceList.Flink;
  1528. Link != &DhcpInterfaceList;
  1529. Link = Link->Flink
  1530. )
  1531. {
  1532. Interfacep = CONTAINING_RECORD(Link, DHCP_INTERFACE, Link);
  1533. ACQUIRE_LOCK(Interfacep);
  1534. for (i = 0; i < Interfacep->BindingCount; i++)
  1535. {
  1536. NhTrace(
  1537. TRACE_FLAG_DNS,
  1538. "DhcpGetPrivateInterfaceAddress: IP address %s (Index %d)",
  1539. INET_NTOA(Interfacep->BindingArray[i].Address),
  1540. Interfacep->Index
  1541. );
  1542. if (NetAddress == (Interfacep->BindingArray[i].Address &
  1543. Interfacep->BindingArray[i].Mask))
  1544. {
  1545. ipAddr = Interfacep->BindingArray[i].Address;
  1546. break;
  1547. }
  1548. }
  1549. RELEASE_LOCK(Interfacep);
  1550. if (ipAddr)
  1551. {
  1552. LeaveCriticalSection(&DhcpInterfaceLock);
  1553. NhTrace(
  1554. TRACE_FLAG_DNS,
  1555. "DhcpGetPrivateInterfaceAddress: Dhcp private interface IP address %s (Index %d)",
  1556. INET_NTOA(ipAddr),
  1557. Interfacep->Index
  1558. );
  1559. return ipAddr;
  1560. }
  1561. }
  1562. }
  1563. if (!(Interfacep = DhcpLookupInterface(0, NULL)))
  1564. {
  1565. LeaveCriticalSection(&DhcpInterfaceLock);
  1566. NhTrace(
  1567. TRACE_FLAG_DNS,
  1568. "DhcpGetPrivateInterfaceAddress: interface index 0 (default) not found"
  1569. );
  1570. return 0;
  1571. }
  1572. ACQUIRE_LOCK(Interfacep);
  1573. if (Interfacep->BindingCount)
  1574. {
  1575. //
  1576. // simply take the first address available
  1577. //
  1578. ipAddr = Interfacep->BindingArray[0].Address;
  1579. }
  1580. RELEASE_LOCK(Interfacep);
  1581. LeaveCriticalSection(&DhcpInterfaceLock);
  1582. NhTrace(
  1583. TRACE_FLAG_DNS,
  1584. "DhcpGetPrivateInterfaceAddress: Dhcp private interface IP address %s (Index 0)",
  1585. INET_NTOA(ipAddr)
  1586. );
  1587. return ipAddr;
  1588. } // DhcpGetPrivateInterfaceAddress