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.

1949 lines
52 KiB

  1. /*++
  2. Copyright (c) 1998, Microsoft Corporation
  3. Module Name:
  4. dhcpmsg.c
  5. Abstract:
  6. This module contains declarations related to the DHCP allocator's
  7. message-processing.
  8. Author:
  9. Abolade Gbadegesin (aboladeg) 6-Mar-1998
  10. Revision History:
  11. Raghu Gatta (rgatta) 15-Dec-2000
  12. + Changed manner in which the option DHCP_TAG_DOMAIN_NAME is
  13. added in DhcpBuildReplyMessage().
  14. + Inform DNS component via DnsUpdate() in DhcpProcessRequestMessage().
  15. Raghu Gatta (rgatta) 20-Apr-2001
  16. + IP/1394 support changes
  17. --*/
  18. #include "precomp.h"
  19. #pragma hdrstop
  20. //
  21. // EXTERNAL DECLARATIONS
  22. //
  23. extern PIP_DNS_PROXY_GLOBAL_INFO DnsGlobalInfo;
  24. extern PWCHAR DnsICSDomainSuffix;
  25. extern CRITICAL_SECTION DnsGlobalInfoLock;
  26. //
  27. // FORWARD DECLARATIONS
  28. //
  29. VOID
  30. DhcpAppendOptionToMessage(
  31. DHCP_OPTION UNALIGNED** Optionp,
  32. UCHAR Tag,
  33. UCHAR Length,
  34. UCHAR Option[]
  35. );
  36. VOID
  37. DhcpBuildReplyMessage(
  38. PDHCP_INTERFACE Interfacep,
  39. PNH_BUFFER Bufferp,
  40. DHCP_OPTION UNALIGNED** Option,
  41. UCHAR MessageType,
  42. BOOLEAN DynamicDns,
  43. DHCP_OPTION UNALIGNED* OptionArray[]
  44. );
  45. ULONG
  46. DhcpExtractOptionsFromMessage(
  47. PDHCP_HEADER Headerp,
  48. ULONG MessageSize,
  49. DHCP_OPTION UNALIGNED* OptionArray[]
  50. );
  51. VOID
  52. DnsUpdate(
  53. CHAR *pszName,
  54. ULONG len,
  55. ULONG ulAddress
  56. );
  57. VOID
  58. DhcpAppendOptionToMessage(
  59. DHCP_OPTION UNALIGNED** Optionp,
  60. UCHAR Tag,
  61. UCHAR Length,
  62. UCHAR Option[]
  63. )
  64. /*++
  65. Routine Description:
  66. This routine is invoked to append an option to a DHCP message.
  67. Arguments:
  68. Optionp - on input, the point at which to append the option;
  69. on output, the point at which to append the next option.
  70. Tag - the option tag
  71. Length - the option length
  72. Option - the option's data
  73. Return Value:
  74. none.
  75. --*/
  76. {
  77. PROFILE("DhcpAppendOptionToMessage");
  78. (*Optionp)->Tag = Tag;
  79. if (!Length) {
  80. *Optionp = (DHCP_OPTION UNALIGNED *)((PUCHAR)*Optionp + 1);
  81. } else {
  82. (*Optionp)->Length = Length;
  83. CopyMemory((*Optionp)->Option, Option, Length);
  84. *Optionp = (DHCP_OPTION UNALIGNED *)((PUCHAR)*Optionp + Length + 2);
  85. }
  86. } // DhcpAppendOptionToMessage
  87. VOID
  88. DhcpBuildReplyMessage(
  89. PDHCP_INTERFACE Interfacep,
  90. PNH_BUFFER Bufferp,
  91. DHCP_OPTION UNALIGNED** Option,
  92. UCHAR MessageType,
  93. BOOLEAN DynamicDns,
  94. DHCP_OPTION UNALIGNED* OptionArray[]
  95. )
  96. /*++
  97. Routine Description:
  98. This routine is called to construct the options portion
  99. of a reply message.
  100. Arguments:
  101. Interfacep - the interface on which the reply will be sent
  102. Bufferp - the buffer containing the reply
  103. Option - the start of the options portion on input;
  104. on output, the end of the message
  105. MessageType - the type of message to be sent
  106. DynamicDns - indicates whether to include the 'dynamic-dns' option.
  107. OptionArray - options extracted from message
  108. Return Value:
  109. none.
  110. Environment:
  111. Invoked with 'Interfacep' referenced by the caller.
  112. --*/
  113. {
  114. ULONG Address;
  115. ULONG SubnetMask;
  116. ULONG i;
  117. //
  118. // Obtain the address and mask for the endpoint
  119. //
  120. Address = NhQueryAddressSocket(Bufferp->Socket);
  121. SubnetMask = PtrToUlong(Bufferp->Context2);
  122. if (MessageType == DHCP_MESSAGE_BOOTP) {
  123. ((PDHCP_HEADER)Bufferp->Buffer)->BootstrapServerAddress = Address;
  124. } else {
  125. //
  126. // Always begin with the 'message-type' option.
  127. //
  128. DhcpAppendOptionToMessage(
  129. Option,
  130. DHCP_TAG_MESSAGE_TYPE,
  131. 1,
  132. &MessageType
  133. );
  134. //
  135. // Provide our address as the server-identifier
  136. //
  137. DhcpAppendOptionToMessage(
  138. Option,
  139. DHCP_TAG_SERVER_IDENTIFIER,
  140. 4,
  141. (PUCHAR)&Address
  142. );
  143. }
  144. if (MessageType != DHCP_MESSAGE_NAK) {
  145. PCHAR DomainName;
  146. ULONG dnSize;
  147. ULONG LeaseTime;
  148. UCHAR NbtNodeType = DHCP_NBT_NODE_TYPE_M;
  149. ULONG RebindingTime;
  150. ULONG RenewalTime;
  151. EnterCriticalSection(&DhcpGlobalInfoLock);
  152. LeaseTime = DhcpGlobalInfo->LeaseTime * 60;
  153. LeaveCriticalSection(&DhcpGlobalInfoLock);
  154. RebindingTime = (LeaseTime * 3) / 4;
  155. RenewalTime = LeaseTime / 2;
  156. if (RenewalTime > DHCP_MAXIMUM_RENEWAL_TIME) {
  157. RenewalTime = DHCP_MAXIMUM_RENEWAL_TIME;
  158. }
  159. LeaseTime = htonl(LeaseTime);
  160. RebindingTime = htonl(RebindingTime);
  161. RenewalTime = htonl(RenewalTime);
  162. DhcpAppendOptionToMessage(
  163. Option,
  164. DHCP_TAG_SUBNET_MASK,
  165. 4,
  166. (PUCHAR)&SubnetMask
  167. );
  168. DhcpAppendOptionToMessage(
  169. Option,
  170. DHCP_TAG_ROUTER,
  171. 4,
  172. (PUCHAR)&Address
  173. );
  174. ////
  175. //// RFC 2132 9.14 : server treats client identifier as an opaque object
  176. //// append the client identifier if present in received message
  177. ////
  178. //if (OptionArray[DhcpOptionClientIdentifier])
  179. //{
  180. // DhcpAppendOptionToMessage(
  181. // Option,
  182. // DHCP_TAG_CLIENT_IDENTIFIER,
  183. // OptionArray[DhcpOptionClientIdentifier]->Length,
  184. // (PUCHAR)OptionArray[DhcpOptionClientIdentifier]->Option
  185. // );
  186. //}
  187. if (MessageType != DHCP_MESSAGE_BOOTP) {
  188. //specify the DNS server in the message if DNS proxy is enabled
  189. //or DNS server is running on local host
  190. if (NhIsDnsProxyEnabled() || !NoLocalDns) {
  191. DhcpAppendOptionToMessage(
  192. Option,
  193. DHCP_TAG_DNS_SERVER,
  194. 4,
  195. (PUCHAR)&Address
  196. );
  197. }
  198. if (NhIsWinsProxyEnabled()) {
  199. DhcpAppendOptionToMessage(
  200. Option,
  201. DHCP_TAG_WINS_SERVER,
  202. 4,
  203. (PUCHAR)&Address
  204. );
  205. }
  206. DhcpAppendOptionToMessage(
  207. Option,
  208. DHCP_TAG_RENEWAL_TIME,
  209. 4,
  210. (PUCHAR)&RenewalTime
  211. );
  212. DhcpAppendOptionToMessage(
  213. Option,
  214. DHCP_TAG_REBINDING_TIME,
  215. 4,
  216. (PUCHAR)&RebindingTime
  217. );
  218. DhcpAppendOptionToMessage(
  219. Option,
  220. DHCP_TAG_LEASE_TIME,
  221. 4,
  222. (PUCHAR)&LeaseTime
  223. );
  224. DhcpAppendOptionToMessage(
  225. Option,
  226. DHCP_TAG_NBT_NODE_TYPE,
  227. 1,
  228. &NbtNodeType
  229. );
  230. if (DynamicDns) {
  231. UCHAR DynamicDns[3] = { 0x03, 0, 0 };
  232. DhcpAppendOptionToMessage(
  233. Option,
  234. DHCP_TAG_DYNAMIC_DNS,
  235. sizeof(DynamicDns),
  236. DynamicDns
  237. );
  238. }
  239. //if (NhpStopDnsEvent && DnsICSDomainSuffix)
  240. if (DnsGlobalInfo && DnsICSDomainSuffix)
  241. {
  242. EnterCriticalSection(&DnsGlobalInfoLock);
  243. dnSize = wcstombs(NULL, DnsICSDomainSuffix, 0);
  244. DomainName = reinterpret_cast<PCHAR>(NH_ALLOCATE(dnSize + 1));
  245. if (DomainName)
  246. {
  247. wcstombs(DomainName, DnsICSDomainSuffix, (dnSize + 1));
  248. }
  249. LeaveCriticalSection(&DnsGlobalInfoLock);
  250. }
  251. else
  252. //
  253. // at this point we have no DNS enabled
  254. // so we default to old behaviour
  255. //
  256. {
  257. DomainName = NhQuerySharedConnectionDomainName();
  258. }
  259. if (DomainName)
  260. {
  261. //
  262. // We include the terminating nul in the domain name
  263. // even though the RFC says we should not, because
  264. // the DHCP server does so.
  265. //
  266. DhcpAppendOptionToMessage(
  267. Option,
  268. DHCP_TAG_DOMAIN_NAME,
  269. (UCHAR)(lstrlenA(DomainName) + 1),
  270. (PUCHAR)DomainName
  271. );
  272. NH_FREE(DomainName);
  273. }
  274. }
  275. }
  276. DhcpAppendOptionToMessage(
  277. Option,
  278. DHCP_TAG_END,
  279. 0,
  280. NULL
  281. );
  282. } // DhcpBuildReplyMessage
  283. ULONG
  284. DhcpExtractOptionsFromMessage(
  285. PDHCP_HEADER Headerp,
  286. ULONG MessageSize,
  287. DHCP_OPTION UNALIGNED* OptionArray[]
  288. )
  289. /*++
  290. Routine Description:
  291. This routine is invoked to parse the options contained in a DHCP message.
  292. Pointers to each option are stored in the given option array.
  293. Arguments:
  294. Headerp - the header of the DHCP message to be parsed
  295. MessageSize - the size of the message to be parsed
  296. OptionArray - receives the parsed options
  297. Return Value:
  298. ULONG - Win32 status code.
  299. --*/
  300. {
  301. DHCP_OPTION UNALIGNED* Index;
  302. DHCP_OPTION UNALIGNED* End;
  303. PROFILE("DhcpExtractOptionsFromMessage");
  304. //
  305. // Initialize the option array to be empty
  306. //
  307. ZeroMemory(OptionArray, DhcpOptionCount * sizeof(PDHCP_OPTION));
  308. //
  309. // Check that the message is large enough to hold options
  310. //
  311. if (MessageSize < sizeof(DHCP_HEADER)) {
  312. NhTrace(
  313. TRACE_FLAG_DHCP,
  314. "DhcpExtractOptionsFromMessage: message size %d too small",
  315. MessageSize
  316. );
  317. NhWarningLog(
  318. IP_AUTO_DHCP_LOG_MESSAGE_TOO_SMALL,
  319. 0,
  320. ""
  321. );
  322. return ERROR_INVALID_DATA;
  323. }
  324. //
  325. // Ensure that the magic cookie is present; if not, there are no options.
  326. //
  327. if (MessageSize < (sizeof(DHCP_HEADER) + sizeof(DHCP_FOOTER)) ||
  328. *(ULONG UNALIGNED*)Headerp->Footer[0].Cookie != DHCP_MAGIC_COOKIE) {
  329. return NO_ERROR;
  330. }
  331. //
  332. // Parse the message's options, if any
  333. //
  334. End = (PDHCP_OPTION)((PUCHAR)Headerp + MessageSize);
  335. Index = (PDHCP_OPTION)&Headerp->Footer[1];
  336. while (Index < End && Index->Tag != DHCP_TAG_END) {
  337. if ((DHCP_TAG_PAD != Index->Tag) &&
  338. (End < (PDHCP_OPTION)(Index->Option + Index->Length))) {
  339. NhTrace(
  340. TRACE_FLAG_DHCP,
  341. "DhcpExtractOptionsFromMessage: option truncated at %d bytes",
  342. MessageSize
  343. );
  344. NhWarningLog(
  345. IP_AUTO_DHCP_LOG_INVALID_FORMAT,
  346. 0,
  347. ""
  348. );
  349. return ERROR_INVALID_DATA;
  350. }
  351. switch (Index->Tag) {
  352. case DHCP_TAG_PAD:
  353. NhTrace(TRACE_FLAG_DHCP, "Pad");
  354. break;
  355. case DHCP_TAG_CLIENT_IDENTIFIER:
  356. NhTrace(TRACE_FLAG_DHCP, "ClientIdentifier");
  357. OptionArray[DhcpOptionClientIdentifier] = Index; break;
  358. case DHCP_TAG_MESSAGE_TYPE:
  359. NhTrace(TRACE_FLAG_DHCP, "MessageType");
  360. if (Index->Length < 1) { break; }
  361. OptionArray[DhcpOptionMessageType] = Index; break;
  362. case DHCP_TAG_REQUESTED_ADDRESS:
  363. NhTrace(TRACE_FLAG_DHCP, "RequestedAddress");
  364. if (Index->Length < 4) { break; }
  365. OptionArray[DhcpOptionRequestedAddress] = Index; break;
  366. case DHCP_TAG_PARAMETER_REQUEST_LIST:
  367. NhTrace(TRACE_FLAG_DHCP, "ParameterRequestList");
  368. if (Index->Length < 1) { break; }
  369. OptionArray[DhcpOptionParameterRequestList] = Index; break;
  370. case DHCP_TAG_ERROR_MESSAGE:
  371. NhTrace(TRACE_FLAG_DHCP, "ErrorMessage");
  372. if (Index->Length < 1) { break; }
  373. OptionArray[DhcpOptionErrorMessage] = Index; break;
  374. case DHCP_TAG_DYNAMIC_DNS:
  375. NhTrace(TRACE_FLAG_DHCP, "DynamicDns");
  376. if (Index->Length < 1) { break; }
  377. OptionArray[DhcpOptionDynamicDns] = Index; break;
  378. case DHCP_TAG_HOST_NAME:
  379. NhTrace(TRACE_FLAG_DHCP, "HostName");
  380. if (Index->Length < 1) { break; }
  381. OptionArray[DhcpOptionHostName] = Index; break;
  382. }
  383. if (DHCP_TAG_PAD != Index->Tag) {
  384. Index = (PDHCP_OPTION)(Index->Option + Index->Length);
  385. }
  386. else {
  387. Index = (PDHCP_OPTION)((PUCHAR)Index + 1);
  388. }
  389. }
  390. if (Index->Tag != DHCP_TAG_END) {
  391. NhTrace(
  392. TRACE_FLAG_DHCP,
  393. "DhcpExtractOptionsFromMessage: message truncated to %d bytes",
  394. MessageSize
  395. );
  396. NhWarningLog(
  397. IP_AUTO_DHCP_LOG_INVALID_FORMAT,
  398. 0,
  399. ""
  400. );
  401. return ERROR_INVALID_DATA;
  402. }
  403. return NO_ERROR;
  404. } // DhcpExtractOptionsFromMessage
  405. VOID
  406. DhcpProcessBootpMessage(
  407. PDHCP_INTERFACE Interfacep,
  408. PNH_BUFFER Bufferp,
  409. DHCP_OPTION UNALIGNED* OptionArray[]
  410. )
  411. /*++
  412. Routine Description:
  413. This routine is called to process a received BOOTP message.
  414. Arguments:
  415. Interfacep - the interface on which the message was received
  416. Bufferp - the buffer containing the message
  417. OptionArray - options extracted from the message
  418. Return Value:
  419. none.
  420. Environment:
  421. Invoked with 'Interfacep' referenced by the caller.
  422. --*/
  423. {
  424. ULONG AssignedAddress;
  425. ULONG Error;
  426. UCHAR ExistingAddress[MAX_HARDWARE_ADDRESS_LENGTH];
  427. ULONG ExistingAddressLength;
  428. PDHCP_HEADER Headerp;
  429. ULONG MessageLength;
  430. PDHCP_HEADER Offerp;
  431. DHCP_OPTION UNALIGNED* Option;
  432. ULONG ReplyAddress;
  433. USHORT ReplyPort;
  434. PNH_BUFFER Replyp;
  435. ULONG ScopeNetwork;
  436. ULONG ScopeMask;
  437. BOOLEAN bIsLocal = FALSE;
  438. PROFILE("DhcpProcessBootpMessage");
  439. ZeroMemory(ExistingAddress, sizeof(ExistingAddress));
  440. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  441. if (!Headerp->ClientAddress) {
  442. AssignedAddress = 0;
  443. } else {
  444. //
  445. // Validate the address requested by the client
  446. //
  447. AssignedAddress = Headerp->ClientAddress;
  448. EnterCriticalSection(&DhcpGlobalInfoLock);
  449. ScopeNetwork = DhcpGlobalInfo->ScopeNetwork;
  450. ScopeMask = DhcpGlobalInfo->ScopeMask;
  451. LeaveCriticalSection(&DhcpGlobalInfoLock);
  452. if ((AssignedAddress & ~ScopeMask) == 0 ||
  453. (AssignedAddress & ~ScopeMask) == ~ScopeMask ||
  454. (AssignedAddress & ScopeMask) != (ScopeNetwork & ScopeMask)) {
  455. //
  456. // The client is on the wrong subnet, or has an all-zeros
  457. // or all-ones address on the subnet.
  458. // Select a different address for the client.
  459. //
  460. AssignedAddress = 0;
  461. } else if (!DhcpIsUniqueAddress(
  462. AssignedAddress,
  463. &bIsLocal,
  464. ExistingAddress,
  465. &ExistingAddressLength
  466. ) &&
  467. (bIsLocal ||
  468. ((Headerp->HardwareAddressType != 7 && // due to WinXP Bridge bug + WinME Client bug
  469. Headerp->HardwareAddressLength) && // if address length is zero we wont compare
  470. (ExistingAddressLength < Headerp->HardwareAddressLength ||
  471. memcmp(
  472. ExistingAddress,
  473. Headerp->HardwareAddress,
  474. Headerp->HardwareAddressLength
  475. ))))) {
  476. //
  477. // Someone has the requested address, and it's not the requestor.
  478. //
  479. AssignedAddress = 0;
  480. } else if (DhcpIsReservedAddress(AssignedAddress, NULL, 0)) {
  481. //
  482. // The address is reserved for someone else.
  483. //
  484. AssignedAddress = 0;
  485. }
  486. }
  487. if (!AssignedAddress &&
  488. !(AssignedAddress =
  489. DhcpAcquireUniqueAddress(
  490. NULL,
  491. 0,
  492. Headerp->HardwareAddress,
  493. Headerp->HardwareAddressLength
  494. ))) {
  495. NhTrace(
  496. TRACE_FLAG_DHCP,
  497. "DhcpProcessBootpMessage: address-allocation failed"
  498. );
  499. return;
  500. }
  501. //
  502. // Acquire a buffer for the reply we will send back
  503. //
  504. Replyp = NhAcquireBuffer();
  505. if (!Replyp) {
  506. NhTrace(
  507. TRACE_FLAG_DHCP,
  508. "DhcpProcessBootpMessage: buffer-allocation failed"
  509. );
  510. NhErrorLog(
  511. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  512. 0,
  513. "%d",
  514. sizeof(NH_BUFFER)
  515. );
  516. return;
  517. }
  518. //
  519. // Pick up fields from the original buffer;
  520. // the routines setting up the reply will attempt to read these,
  521. // so they are set to the values from the original buffer.
  522. //
  523. Replyp->Socket = Bufferp->Socket;
  524. Replyp->ReadAddress = Bufferp->ReadAddress;
  525. Replyp->WriteAddress = Bufferp->WriteAddress;
  526. Replyp->Context = Bufferp->Context;
  527. Replyp->Context2 = Bufferp->Context2;
  528. Offerp = (PDHCP_HEADER)Replyp->Buffer;
  529. //
  530. // Copy the original header
  531. //
  532. *Offerp = *Headerp;
  533. //
  534. // Set up the offer-header fields
  535. //
  536. Offerp->Operation = BOOTP_OPERATION_REPLY;
  537. Offerp->AssignedAddress = AssignedAddress;
  538. Offerp->ServerHostName[0] = 0;
  539. Offerp->BootFile[0] = 0;
  540. Offerp->SecondsSinceBoot = 0;
  541. *(ULONG UNALIGNED *)Offerp->Footer[0].Cookie = DHCP_MAGIC_COOKIE;
  542. //
  543. // Fill in options
  544. //
  545. Option = (PDHCP_OPTION)&Offerp->Footer[1];
  546. DhcpBuildReplyMessage(
  547. Interfacep,
  548. Replyp,
  549. &Option,
  550. DHCP_MESSAGE_BOOTP,
  551. FALSE,
  552. OptionArray
  553. );
  554. //
  555. // Send the offer to the BOOTP client
  556. //
  557. EnterCriticalSection(&DhcpInterfaceLock);
  558. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  559. LeaveCriticalSection(&DhcpInterfaceLock);
  560. NhReleaseBuffer(Replyp);
  561. } else {
  562. LeaveCriticalSection(&DhcpInterfaceLock);
  563. if (Headerp->RelayAgentAddress) {
  564. ReplyAddress = Headerp->RelayAgentAddress;
  565. ReplyPort = DHCP_PORT_SERVER;
  566. } else {
  567. ReplyAddress = INADDR_BROADCAST;
  568. ReplyPort = DHCP_PORT_CLIENT;
  569. }
  570. MessageLength = (ULONG)((PUCHAR)Option - Replyp->Buffer);
  571. if (MessageLength < sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH) {
  572. MessageLength = sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH;
  573. }
  574. Error =
  575. NhWriteDatagramSocket(
  576. &DhcpComponentReference,
  577. Bufferp->Socket,
  578. ReplyAddress,
  579. ReplyPort,
  580. Replyp,
  581. MessageLength,
  582. DhcpWriteCompletionRoutine,
  583. Interfacep,
  584. Bufferp->Context2
  585. );
  586. if (!Error) {
  587. InterlockedIncrement(
  588. reinterpret_cast<LPLONG>(&DhcpStatistics.BootpOffersSent)
  589. );
  590. } else {
  591. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  592. NhReleaseBuffer(Replyp);
  593. NhTrace(
  594. TRACE_FLAG_DHCP,
  595. "DhcpProcessBootpMessage: error %d sending reply",
  596. Error
  597. );
  598. NhErrorLog(
  599. IP_AUTO_DHCP_LOG_REPLY_FAILED,
  600. Error,
  601. "%I",
  602. NhQueryAddressSocket(Bufferp->Socket)
  603. );
  604. }
  605. }
  606. } // DhcpProcessBootpMessage
  607. VOID
  608. DhcpProcessDiscoverMessage(
  609. PDHCP_INTERFACE Interfacep,
  610. PNH_BUFFER Bufferp,
  611. DHCP_OPTION UNALIGNED* OptionArray[]
  612. )
  613. /*++
  614. Routine Description:
  615. This routine is called to process a received DHCPDISCOVER message.
  616. Arguments:
  617. Interfacep - the interface on which the discover was received
  618. Bufferp - the buffer containing the message
  619. OptionArray - options extracted from the message
  620. Return Value:
  621. none.
  622. Environment:
  623. Invoked with 'Interfacep' referenced by the caller.
  624. --*/
  625. {
  626. ULONG AssignedAddress;
  627. ULONG Error;
  628. UCHAR ExistingAddress[MAX_HARDWARE_ADDRESS_LENGTH];
  629. ULONG ExistingAddressLength;
  630. PDHCP_HEADER Headerp;
  631. ULONG MessageLength;
  632. PDHCP_HEADER Offerp;
  633. DHCP_OPTION UNALIGNED* Option;
  634. ULONG ReplyAddress;
  635. USHORT ReplyPort;
  636. PNH_BUFFER Replyp;
  637. ULONG ScopeNetwork;
  638. ULONG ScopeMask;
  639. BOOLEAN bIsLocal = FALSE;
  640. PROFILE("DhcpProcessDiscoverMessage");
  641. ZeroMemory(ExistingAddress, sizeof(ExistingAddress));
  642. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  643. //
  644. // See if the client is renewing or requesting
  645. //
  646. if (!OptionArray[DhcpOptionRequestedAddress]) {
  647. AssignedAddress = 0;
  648. } else {
  649. //
  650. // Validate the address requested by the client
  651. //
  652. AssignedAddress =
  653. *(ULONG UNALIGNED*)OptionArray[DhcpOptionRequestedAddress]->Option;
  654. EnterCriticalSection(&DhcpGlobalInfoLock);
  655. ScopeNetwork = DhcpGlobalInfo->ScopeNetwork;
  656. ScopeMask = DhcpGlobalInfo->ScopeMask;
  657. LeaveCriticalSection(&DhcpGlobalInfoLock);
  658. if ((AssignedAddress & ~ScopeMask) == 0 ||
  659. (AssignedAddress & ~ScopeMask) == ~ScopeMask ||
  660. (AssignedAddress & ScopeMask) != (ScopeNetwork & ScopeMask)) {
  661. //
  662. // The client is on the wrong subnet, or has an all-zeroes
  663. // or all-ones address on the subnet.
  664. // Select a different address for the client.
  665. //
  666. AssignedAddress = 0;
  667. } else if (!DhcpIsUniqueAddress(
  668. AssignedAddress,
  669. &bIsLocal,
  670. ExistingAddress,
  671. &ExistingAddressLength
  672. ) &&
  673. (bIsLocal ||
  674. ((Headerp->HardwareAddressType != 7 && // due to WinXP Bridge bug + WinME Client bug
  675. Headerp->HardwareAddressLength) && // if address length is zero we wont compare
  676. (ExistingAddressLength < Headerp->HardwareAddressLength ||
  677. memcmp(
  678. ExistingAddress,
  679. Headerp->HardwareAddress,
  680. Headerp->HardwareAddressLength
  681. ))))) {
  682. //
  683. // Someone has the requested address, and it's not the requestor.
  684. //
  685. AssignedAddress = 0;
  686. } else if (OptionArray[DhcpOptionHostName]) {
  687. if (DhcpIsReservedAddress(
  688. AssignedAddress,
  689. reinterpret_cast<PCHAR>(
  690. OptionArray[DhcpOptionHostName]->Option
  691. ),
  692. OptionArray[DhcpOptionHostName]->Length
  693. )) {
  694. //
  695. // The address is reserved for someone else,
  696. // or the client has a different address reserved.
  697. //
  698. AssignedAddress = 0;
  699. }
  700. } else if (DhcpIsReservedAddress(AssignedAddress, NULL, 0)) {
  701. //
  702. // The address is reserved for someone else.
  703. //
  704. AssignedAddress = 0;
  705. }
  706. }
  707. //
  708. // Generate an address for the client if necessary
  709. //
  710. if (!AssignedAddress) {
  711. if (!OptionArray[DhcpOptionHostName]) {
  712. AssignedAddress =
  713. DhcpAcquireUniqueAddress(
  714. NULL,
  715. 0,
  716. Headerp->HardwareAddress,
  717. Headerp->HardwareAddressLength
  718. );
  719. } else {
  720. AssignedAddress =
  721. DhcpAcquireUniqueAddress(
  722. reinterpret_cast<PCHAR>(
  723. OptionArray[DhcpOptionHostName]->Option
  724. ),
  725. OptionArray[DhcpOptionHostName]->Length,
  726. Headerp->HardwareAddress,
  727. Headerp->HardwareAddressLength
  728. );
  729. }
  730. if (!AssignedAddress) {
  731. NhTrace(
  732. TRACE_FLAG_DHCP,
  733. "DhcpProcessDiscoverMessage: address-allocation failed"
  734. );
  735. return;
  736. }
  737. }
  738. //
  739. // Acquire a buffer for the offer we will send back
  740. //
  741. Replyp = NhAcquireBuffer();
  742. if (!Replyp) {
  743. NhTrace(
  744. TRACE_FLAG_DHCP,
  745. "DhcpProcessDiscoverMessage: buffer-allocation failed"
  746. );
  747. NhErrorLog(
  748. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  749. 0,
  750. "%d",
  751. sizeof(NH_BUFFER)
  752. );
  753. return;
  754. }
  755. //
  756. // Pick up fields from the original message
  757. // the routines setting up the reply will attempt to read these,
  758. // so they are set to the values from the original buffer.
  759. //
  760. Replyp->Socket = Bufferp->Socket;
  761. Replyp->ReadAddress = Bufferp->ReadAddress;
  762. Replyp->WriteAddress = Bufferp->WriteAddress;
  763. Replyp->Context = Bufferp->Context;
  764. Replyp->Context2 = Bufferp->Context2;
  765. Offerp = (PDHCP_HEADER)Replyp->Buffer;
  766. //
  767. // Copy the original discover header
  768. //
  769. *Offerp = *Headerp;
  770. //
  771. // IP/1394 support (RFC 2855)
  772. //
  773. if ((IP1394_HTYPE == Offerp->HardwareAddressType) &&
  774. (0 == Offerp->HardwareAddressLength))
  775. {
  776. //
  777. // MUST set client hardware address to zero
  778. //
  779. ZeroMemory(Offerp->HardwareAddress, sizeof(Offerp->HardwareAddress));
  780. }
  781. //
  782. // Set up the offer-header fieldds
  783. //
  784. Offerp->Operation = BOOTP_OPERATION_REPLY;
  785. Offerp->AssignedAddress = AssignedAddress;
  786. Offerp->ServerHostName[0] = 0;
  787. Offerp->BootFile[0] = 0;
  788. Offerp->SecondsSinceBoot = 0;
  789. *(ULONG UNALIGNED *)Offerp->Footer[0].Cookie = DHCP_MAGIC_COOKIE;
  790. //
  791. // Fill in options
  792. //
  793. Option = (PDHCP_OPTION)&Offerp->Footer[1];
  794. DhcpBuildReplyMessage(
  795. Interfacep,
  796. Replyp,
  797. &Option,
  798. DHCP_MESSAGE_OFFER,
  799. (BOOLEAN)(OptionArray[DhcpOptionDynamicDns] ? TRUE : FALSE),
  800. OptionArray
  801. );
  802. //
  803. // Send the offer to the client
  804. //
  805. EnterCriticalSection(&DhcpInterfaceLock);
  806. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  807. LeaveCriticalSection(&DhcpInterfaceLock);
  808. NhReleaseBuffer(Replyp);
  809. } else {
  810. LeaveCriticalSection(&DhcpInterfaceLock);
  811. if (Headerp->RelayAgentAddress) {
  812. ReplyAddress = Headerp->RelayAgentAddress;
  813. ReplyPort = DHCP_PORT_SERVER;
  814. } else {
  815. ReplyAddress = INADDR_BROADCAST;
  816. ReplyPort = DHCP_PORT_CLIENT;
  817. }
  818. MessageLength = (ULONG)((PUCHAR)Option - Replyp->Buffer);
  819. if (MessageLength < sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH) {
  820. MessageLength = sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH;
  821. }
  822. Error =
  823. NhWriteDatagramSocket(
  824. &DhcpComponentReference,
  825. Bufferp->Socket,
  826. ReplyAddress,
  827. ReplyPort,
  828. Replyp,
  829. MessageLength,
  830. DhcpWriteCompletionRoutine,
  831. Interfacep,
  832. Bufferp->Context2
  833. );
  834. if (!Error) {
  835. InterlockedIncrement(
  836. reinterpret_cast<LPLONG>(&DhcpStatistics.OffersSent)
  837. );
  838. } else {
  839. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  840. NhReleaseBuffer(Replyp);
  841. NhTrace(
  842. TRACE_FLAG_DHCP,
  843. "DhcpProcessDiscoverMessage: error %d sending reply",
  844. Error
  845. );
  846. NhErrorLog(
  847. IP_AUTO_DHCP_LOG_REPLY_FAILED,
  848. Error,
  849. "%I",
  850. NhQueryAddressSocket(Bufferp->Socket)
  851. );
  852. }
  853. }
  854. } // DhcpProcessDiscoverMessage
  855. VOID
  856. DhcpProcessInformMessage(
  857. PDHCP_INTERFACE Interfacep,
  858. PNH_BUFFER Bufferp,
  859. DHCP_OPTION UNALIGNED* OptionArray[]
  860. )
  861. /*++
  862. Routine Description:
  863. This routine is called to process a received DHCPINFORM message.
  864. Arguments:
  865. Interfacep - the interface on which the inform was received
  866. Bufferp - the buffer containing the message
  867. OptionArray - options extracted from the message
  868. Return Value:
  869. none.
  870. Environment:
  871. Invoked with 'Interfacep' referenced by the caller.
  872. --*/
  873. {
  874. PDHCP_HEADER Ackp;
  875. ULONG Error;
  876. PDHCP_HEADER Headerp;
  877. ULONG MessageLength;
  878. DHCP_OPTION UNALIGNED* Option;
  879. ULONG ReplyAddress;
  880. USHORT ReplyPort;
  881. PNH_BUFFER Replyp;
  882. PROFILE("DhcpProcessInformMessage");
  883. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  884. //
  885. // Acquire a buffer for the ack we will send back
  886. //
  887. Replyp = NhAcquireBuffer();
  888. if (!Replyp) {
  889. NhTrace(
  890. TRACE_FLAG_DHCP,
  891. "DhcpProcessInformMessage: buffer-allocation failed"
  892. );
  893. NhErrorLog(
  894. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  895. 0,
  896. "%d",
  897. sizeof(NH_BUFFER)
  898. );
  899. return;
  900. }
  901. //
  902. // Pick up fields from the original message
  903. // the routines setting up the reply will attempt to read these,
  904. // so they are set to the values from the original buffer.
  905. //
  906. Replyp->Socket = Bufferp->Socket;
  907. Replyp->ReadAddress = Bufferp->ReadAddress;
  908. Replyp->WriteAddress = Bufferp->WriteAddress;
  909. Replyp->Context = Bufferp->Context;
  910. Replyp->Context2 = Bufferp->Context2;
  911. Ackp = (PDHCP_HEADER)Replyp->Buffer;
  912. //
  913. // Copy the original header
  914. //
  915. *Ackp = *Headerp;
  916. //
  917. // IP/1394 support (RFC 2855)
  918. //
  919. if ((IP1394_HTYPE == Ackp->HardwareAddressType) &&
  920. (0 == Ackp->HardwareAddressLength))
  921. {
  922. //
  923. // MUST set client hardware address to zero
  924. //
  925. ZeroMemory(Ackp->HardwareAddress, sizeof(Ackp->HardwareAddress));
  926. }
  927. //
  928. // Set up the ack-header fieldds
  929. //
  930. Ackp->Operation = BOOTP_OPERATION_REPLY;
  931. Ackp->AssignedAddress = 0;
  932. Ackp->ServerHostName[0] = 0;
  933. Ackp->BootFile[0] = 0;
  934. Ackp->SecondsSinceBoot = 0;
  935. *(ULONG UNALIGNED *)Ackp->Footer[0].Cookie = DHCP_MAGIC_COOKIE;
  936. //
  937. // Fill in options
  938. //
  939. Option = (PDHCP_OPTION)&Ackp->Footer[1];
  940. DhcpBuildReplyMessage(
  941. Interfacep,
  942. Replyp,
  943. &Option,
  944. DHCP_MESSAGE_ACK,
  945. (BOOLEAN)(OptionArray[DhcpOptionDynamicDns] ? TRUE : FALSE),
  946. OptionArray
  947. );
  948. //
  949. // Send the offer to the client
  950. //
  951. EnterCriticalSection(&DhcpInterfaceLock);
  952. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  953. LeaveCriticalSection(&DhcpInterfaceLock);
  954. NhReleaseBuffer(Replyp);
  955. } else {
  956. LeaveCriticalSection(&DhcpInterfaceLock);
  957. if (Headerp->RelayAgentAddress) {
  958. ReplyAddress = Headerp->RelayAgentAddress;
  959. ReplyPort = DHCP_PORT_SERVER;
  960. } else {
  961. ReplyAddress = INADDR_BROADCAST;
  962. ReplyPort = DHCP_PORT_CLIENT;
  963. }
  964. MessageLength = (ULONG)((PUCHAR)Option - Replyp->Buffer);
  965. if (MessageLength < sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH) {
  966. MessageLength = sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH;
  967. }
  968. Error =
  969. NhWriteDatagramSocket(
  970. &DhcpComponentReference,
  971. Bufferp->Socket,
  972. ReplyAddress,
  973. ReplyPort,
  974. Replyp,
  975. MessageLength,
  976. DhcpWriteCompletionRoutine,
  977. Interfacep,
  978. Bufferp->Context2
  979. );
  980. if (!Error) {
  981. InterlockedIncrement(
  982. reinterpret_cast<LPLONG>(&DhcpStatistics.AcksSent)
  983. );
  984. } else {
  985. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  986. NhReleaseBuffer(Replyp);
  987. NhTrace(
  988. TRACE_FLAG_DHCP,
  989. "DhcpProcessInformMessage: error %d sending reply",
  990. Error
  991. );
  992. NhErrorLog(
  993. IP_AUTO_DHCP_LOG_REPLY_FAILED,
  994. Error,
  995. "%I",
  996. NhQueryAddressSocket(Bufferp->Socket)
  997. );
  998. }
  999. }
  1000. } // DhcpProcessInformMessage
  1001. VOID
  1002. DhcpProcessMessage(
  1003. PDHCP_INTERFACE Interfacep,
  1004. PNH_BUFFER Bufferp
  1005. )
  1006. /*++
  1007. Routine Description:
  1008. This routine is invoked to process a DHCP client message.
  1009. Arguments:
  1010. Interfacep - the interface on which the request was received
  1011. Bufferp - the buffer containing the message received
  1012. Return Value:
  1013. none.
  1014. Environment:
  1015. Invoked internally with 'Interfacep' referenced by the caller.
  1016. --*/
  1017. {
  1018. ULONG Error;
  1019. PDHCP_HEADER Headerp;
  1020. UCHAR MessageType;
  1021. PDHCP_OPTION OptionArray[DhcpOptionCount];
  1022. PROFILE("DhcpProcessMessage");
  1023. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  1024. #if DBG
  1025. NhDump(
  1026. TRACE_FLAG_DHCP,
  1027. Bufferp->Buffer,
  1028. Bufferp->BytesTransferred,
  1029. 1
  1030. );
  1031. #endif
  1032. //
  1033. // Extract pointers to each option in the message
  1034. //
  1035. Error =
  1036. DhcpExtractOptionsFromMessage(
  1037. Headerp,
  1038. Bufferp->BytesTransferred,
  1039. OptionArray
  1040. );
  1041. if (Error) {
  1042. InterlockedIncrement(
  1043. reinterpret_cast<LPLONG>(&DhcpStatistics.MessagesIgnored)
  1044. );
  1045. }
  1046. else
  1047. //
  1048. // Look for the message-type;
  1049. // This distinguishes BOOTP from DHCP clients.
  1050. //
  1051. if (!OptionArray[DhcpOptionMessageType]) {
  1052. DhcpProcessBootpMessage(
  1053. Interfacep,
  1054. Bufferp,
  1055. OptionArray
  1056. );
  1057. } else if (Headerp->HardwareAddressLength >
  1058. sizeof(Headerp->HardwareAddress)) {
  1059. NhTrace(
  1060. TRACE_FLAG_DHCP,
  1061. "DhcpProcessMessage: ignoring message since HWAddrLength "
  1062. "is too long"
  1063. );
  1064. InterlockedIncrement(
  1065. reinterpret_cast<LPLONG>(&DhcpStatistics.MessagesIgnored)
  1066. );
  1067. } else if (DhcpIsLocalHardwareAddress(
  1068. Headerp->HardwareAddress, Headerp->HardwareAddressLength)) {
  1069. NhTrace(
  1070. TRACE_FLAG_DHCP,
  1071. "DhcpProcessMessage: ignoring message, from self"
  1072. );
  1073. InterlockedIncrement(
  1074. reinterpret_cast<LPLONG>(&DhcpStatistics.MessagesIgnored)
  1075. );
  1076. } else switch(MessageType = OptionArray[DhcpOptionMessageType]->Option[0]) {
  1077. case DHCP_MESSAGE_DISCOVER: {
  1078. InterlockedIncrement(
  1079. reinterpret_cast<LPLONG>(&DhcpStatistics.DiscoversReceived)
  1080. );
  1081. NhTrace(
  1082. TRACE_FLAG_DHCP,
  1083. "DhcpProcessMessage: received DISCOVER message"
  1084. );
  1085. DhcpProcessDiscoverMessage(
  1086. Interfacep,
  1087. Bufferp,
  1088. OptionArray
  1089. );
  1090. break;
  1091. }
  1092. case DHCP_MESSAGE_REQUEST: {
  1093. InterlockedIncrement(
  1094. reinterpret_cast<LPLONG>(&DhcpStatistics.RequestsReceived)
  1095. );
  1096. NhTrace(
  1097. TRACE_FLAG_DHCP,
  1098. "DhcpProcessMessage: received REQUEST message"
  1099. );
  1100. DhcpProcessRequestMessage(
  1101. Interfacep,
  1102. Bufferp,
  1103. OptionArray
  1104. );
  1105. break;
  1106. }
  1107. case DHCP_MESSAGE_INFORM: {
  1108. InterlockedIncrement(
  1109. reinterpret_cast<LPLONG>(&DhcpStatistics.InformsReceived)
  1110. );
  1111. NhTrace(
  1112. TRACE_FLAG_DHCP,
  1113. "DhcpProcessMessage: received INFORM message"
  1114. );
  1115. DhcpProcessInformMessage(
  1116. Interfacep,
  1117. Bufferp,
  1118. OptionArray
  1119. );
  1120. break;
  1121. }
  1122. case DHCP_MESSAGE_DECLINE: {
  1123. InterlockedIncrement(
  1124. reinterpret_cast<LPLONG>(&DhcpStatistics.DeclinesReceived)
  1125. );
  1126. // log message
  1127. NhTrace(
  1128. TRACE_FLAG_DHCP,
  1129. "DhcpProcessMessage: received DECLINE message"
  1130. );
  1131. break;
  1132. }
  1133. case DHCP_MESSAGE_RELEASE: {
  1134. InterlockedIncrement(
  1135. reinterpret_cast<LPLONG>(&DhcpStatistics.ReleasesReceived)
  1136. );
  1137. NhTrace(
  1138. TRACE_FLAG_DHCP,
  1139. "DhcpProcessMessage: received RELEASE message"
  1140. );
  1141. break;
  1142. }
  1143. default: {
  1144. InterlockedIncrement(
  1145. reinterpret_cast<LPLONG>(&DhcpStatistics.MessagesIgnored)
  1146. );
  1147. NhTrace(
  1148. TRACE_FLAG_DHCP,
  1149. "DhcpProcessMessage: message type %d invalid",
  1150. MessageType
  1151. );
  1152. NhWarningLog(
  1153. IP_AUTO_DHCP_LOG_INVALID_DHCP_MESSAGE_TYPE,
  1154. 0,
  1155. "%d",
  1156. MessageType
  1157. );
  1158. break;
  1159. }
  1160. }
  1161. //
  1162. // Post the buffer for another read
  1163. //
  1164. EnterCriticalSection(&DhcpInterfaceLock);
  1165. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1166. LeaveCriticalSection(&DhcpInterfaceLock);
  1167. NhReleaseBuffer(Bufferp);
  1168. } else {
  1169. LeaveCriticalSection(&DhcpInterfaceLock);
  1170. Error =
  1171. NhReadDatagramSocket(
  1172. &DhcpComponentReference,
  1173. Bufferp->Socket,
  1174. Bufferp,
  1175. DhcpReadCompletionRoutine,
  1176. Bufferp->Context,
  1177. Bufferp->Context2
  1178. );
  1179. if (Error) {
  1180. ACQUIRE_LOCK(Interfacep);
  1181. DhcpDeferReadInterface(Interfacep, Bufferp->Socket);
  1182. RELEASE_LOCK(Interfacep);
  1183. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1184. NhTrace(
  1185. TRACE_FLAG_DHCP,
  1186. "DhcpProcessMessage: error %d reposting read",
  1187. Error
  1188. );
  1189. NhWarningLog(
  1190. IP_AUTO_DHCP_LOG_RECEIVE_FAILED,
  1191. Error,
  1192. "%I",
  1193. NhQueryAddressSocket(Bufferp->Socket)
  1194. );
  1195. NhReleaseBuffer(Bufferp);
  1196. }
  1197. }
  1198. } // DhcpProcessMessage
  1199. VOID
  1200. DhcpProcessRequestMessage(
  1201. PDHCP_INTERFACE Interfacep,
  1202. PNH_BUFFER Bufferp,
  1203. DHCP_OPTION UNALIGNED* OptionArray[]
  1204. )
  1205. /*++
  1206. Routine Description:
  1207. This routine is called to process a request message.
  1208. Arguments:
  1209. Interfacep - the interface on which the request was received
  1210. Bufferp - the buffer containing the message received
  1211. OptionArray - options extracted from the message
  1212. Return Value:
  1213. none.
  1214. Environment:
  1215. Invoked internally with 'Interfacep' referenced by the caller.
  1216. --*/
  1217. {
  1218. ULONG AssignedAddress = 0;
  1219. ULONG Error;
  1220. UCHAR ExistingAddress[MAX_HARDWARE_ADDRESS_LENGTH];
  1221. ULONG ExistingAddressLength;
  1222. PDHCP_HEADER Headerp;
  1223. ULONG MessageLength;
  1224. PDHCP_HEADER Offerp;
  1225. DHCP_OPTION UNALIGNED* Option;
  1226. ULONG ReplyAddress;
  1227. USHORT ReplyPort;
  1228. PNH_BUFFER Replyp;
  1229. UCHAR ReplyType = DHCP_MESSAGE_ACK;
  1230. ULONG ScopeNetwork;
  1231. ULONG ScopeMask;
  1232. BOOLEAN bIsLocal = FALSE;
  1233. PROFILE("DhcpProcessRequestMessage");
  1234. ZeroMemory(ExistingAddress, sizeof(ExistingAddress));
  1235. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  1236. //
  1237. // Validate the address requested by the client
  1238. //
  1239. if (!Headerp->ClientAddress && !OptionArray[DhcpOptionRequestedAddress]) {
  1240. //
  1241. // The client left out the address being requested
  1242. //
  1243. ReplyType = DHCP_MESSAGE_NAK;
  1244. } else {
  1245. //
  1246. // Try to see if the address is in use.
  1247. //
  1248. AssignedAddress =
  1249. Headerp->ClientAddress
  1250. ? Headerp->ClientAddress
  1251. : *(ULONG UNALIGNED*)
  1252. OptionArray[DhcpOptionRequestedAddress]->Option;
  1253. EnterCriticalSection(&DhcpGlobalInfoLock);
  1254. ScopeNetwork = DhcpGlobalInfo->ScopeNetwork;
  1255. ScopeMask = DhcpGlobalInfo->ScopeMask;
  1256. LeaveCriticalSection(&DhcpGlobalInfoLock);
  1257. if ((AssignedAddress & ~ScopeMask) == 0 ||
  1258. (AssignedAddress & ~ScopeMask) == ~ScopeMask ||
  1259. (AssignedAddress & ScopeMask) != (ScopeNetwork & ScopeMask)) {
  1260. //
  1261. // The client is on the wrong subnet, or has an all-ones
  1262. // or all-zeroes address.
  1263. //
  1264. ReplyType = DHCP_MESSAGE_NAK;
  1265. } else if (!DhcpIsUniqueAddress(
  1266. AssignedAddress,
  1267. &bIsLocal,
  1268. ExistingAddress,
  1269. &ExistingAddressLength
  1270. ) &&
  1271. (bIsLocal ||
  1272. ((Headerp->HardwareAddressType != 7 && // due to WinXP Bridge bug + WinME Client bug
  1273. Headerp->HardwareAddressLength) && // if address length is zero we wont compare
  1274. (ExistingAddressLength < Headerp->HardwareAddressLength ||
  1275. memcmp(
  1276. ExistingAddress,
  1277. Headerp->HardwareAddress,
  1278. Headerp->HardwareAddressLength
  1279. ))))) {
  1280. //
  1281. // Someone has the requested address, and it's not the requestor.
  1282. //
  1283. ReplyType = DHCP_MESSAGE_NAK;
  1284. } else if (OptionArray[DhcpOptionHostName]) {
  1285. if (DhcpIsReservedAddress(
  1286. AssignedAddress,
  1287. reinterpret_cast<PCHAR>(
  1288. OptionArray[DhcpOptionHostName]->Option
  1289. ),
  1290. OptionArray[DhcpOptionHostName]->Length
  1291. )) {
  1292. //
  1293. // The address is reserved for someone else,
  1294. // or the client has a different address reserved.
  1295. //
  1296. ReplyType = DHCP_MESSAGE_NAK;
  1297. }
  1298. } else if (DhcpIsReservedAddress(AssignedAddress, NULL, 0)) {
  1299. //
  1300. // The address is reserved for someone else.
  1301. //
  1302. ReplyType = DHCP_MESSAGE_NAK;
  1303. }
  1304. }
  1305. //
  1306. // Acquire a buffer for the reply we will send back
  1307. //
  1308. Replyp = NhAcquireBuffer();
  1309. if (!Replyp) {
  1310. NhTrace(
  1311. TRACE_FLAG_DHCP,
  1312. "DhcpProcessRequestMessage: buffer-allocation failed"
  1313. );
  1314. NhErrorLog(
  1315. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  1316. 0,
  1317. "%d",
  1318. sizeof(NH_BUFFER)
  1319. );
  1320. return;
  1321. }
  1322. //
  1323. // Pick up fields to be used in the reply-buffer
  1324. // the routines setting up the reply will attempt to read these,
  1325. // so they are set to the values from the original buffer.
  1326. //
  1327. Replyp->Socket = Bufferp->Socket;
  1328. Replyp->ReadAddress = Bufferp->ReadAddress;
  1329. Replyp->WriteAddress = Bufferp->WriteAddress;
  1330. Replyp->Context = Bufferp->Context;
  1331. Replyp->Context2 = Bufferp->Context2;
  1332. Offerp = (PDHCP_HEADER)Replyp->Buffer;
  1333. //
  1334. // Copy the original discover header
  1335. //
  1336. *Offerp = *Headerp;
  1337. //
  1338. // IP/1394 support (RFC 2855)
  1339. //
  1340. if ((IP1394_HTYPE == Offerp->HardwareAddressType) &&
  1341. (0 == Offerp->HardwareAddressLength))
  1342. {
  1343. //
  1344. // MUST set client hardware address to zero
  1345. //
  1346. ZeroMemory(Offerp->HardwareAddress, sizeof(Offerp->HardwareAddress));
  1347. }
  1348. //
  1349. // Set up the offer-header fieldds
  1350. //
  1351. Offerp->Operation = BOOTP_OPERATION_REPLY;
  1352. Offerp->AssignedAddress = AssignedAddress;
  1353. Offerp->ServerHostName[0] = 0;
  1354. Offerp->BootFile[0] = 0;
  1355. Offerp->SecondsSinceBoot = 0;
  1356. *(ULONG UNALIGNED *)Offerp->Footer[0].Cookie = DHCP_MAGIC_COOKIE;
  1357. //
  1358. // Fill in options
  1359. //
  1360. Option = (PDHCP_OPTION)&Offerp->Footer[1];
  1361. DhcpBuildReplyMessage(
  1362. Interfacep,
  1363. Replyp,
  1364. &Option,
  1365. ReplyType,
  1366. (BOOLEAN)(OptionArray[DhcpOptionDynamicDns] ? TRUE : FALSE),
  1367. OptionArray
  1368. );
  1369. //
  1370. // NEW LOGIC HERE => tied to DNS
  1371. //
  1372. if (DHCP_MESSAGE_ACK == ReplyType)
  1373. {
  1374. //
  1375. // We perform the equivalent of Dynamic DNS here
  1376. // by informing the DNS component that this client exists
  1377. //
  1378. if (OptionArray[DhcpOptionHostName])
  1379. {
  1380. //
  1381. // check if DNS component is active
  1382. //
  1383. if (REFERENCE_DNS())
  1384. {
  1385. DnsUpdate(
  1386. reinterpret_cast<PCHAR>(OptionArray[DhcpOptionHostName]->Option),
  1387. (ULONG) OptionArray[DhcpOptionHostName]->Length,
  1388. AssignedAddress
  1389. );
  1390. DEREFERENCE_DNS();
  1391. }
  1392. }
  1393. }
  1394. //
  1395. // Send the reply to the client
  1396. //
  1397. EnterCriticalSection(&DhcpInterfaceLock);
  1398. if (!DHCP_REFERENCE_INTERFACE(Interfacep)) {
  1399. LeaveCriticalSection(&DhcpInterfaceLock);
  1400. NhReleaseBuffer(Replyp);
  1401. } else {
  1402. LeaveCriticalSection(&DhcpInterfaceLock);
  1403. if (Headerp->RelayAgentAddress) {
  1404. ReplyAddress = Headerp->RelayAgentAddress;
  1405. ReplyPort = DHCP_PORT_SERVER;
  1406. } else {
  1407. ReplyAddress = INADDR_BROADCAST;
  1408. ReplyPort = DHCP_PORT_CLIENT;
  1409. }
  1410. MessageLength = (ULONG)((PUCHAR)Option - Replyp->Buffer);
  1411. if (MessageLength < sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH) {
  1412. MessageLength = sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH;
  1413. }
  1414. Error =
  1415. NhWriteDatagramSocket(
  1416. &DhcpComponentReference,
  1417. Bufferp->Socket,
  1418. ReplyAddress,
  1419. ReplyPort,
  1420. Replyp,
  1421. MessageLength,
  1422. DhcpWriteCompletionRoutine,
  1423. Interfacep,
  1424. Bufferp->Context2
  1425. );
  1426. if (!Error) {
  1427. InterlockedIncrement(
  1428. (ReplyType == DHCP_MESSAGE_ACK)
  1429. ? reinterpret_cast<LPLONG>(&DhcpStatistics.AcksSent)
  1430. : reinterpret_cast<LPLONG>(&DhcpStatistics.NaksSent)
  1431. );
  1432. } else {
  1433. DHCP_DEREFERENCE_INTERFACE(Interfacep);
  1434. NhReleaseBuffer(Replyp);
  1435. NhTrace(
  1436. TRACE_FLAG_DHCP,
  1437. "DhcpProcessRequestMessage: error %d sending reply",
  1438. Error
  1439. );
  1440. NhErrorLog(
  1441. IP_AUTO_DHCP_LOG_REPLY_FAILED,
  1442. Error,
  1443. "%I",
  1444. NhQueryAddressSocket(Bufferp->Socket)
  1445. );
  1446. }
  1447. }
  1448. } // DhcpProcessRequestMessage
  1449. ULONG
  1450. DhcpWriteClientRequestMessage(
  1451. PDHCP_INTERFACE Interfacep,
  1452. PDHCP_BINDING Binding
  1453. )
  1454. /*++
  1455. Routine Description:
  1456. This routine is invoked to check for the existence of a DHCP server
  1457. on the given interface and address. It generates a BOOTP request
  1458. on a socket bound to the DHCP client port.
  1459. Arguments:
  1460. Interfacep - the interface on which the client request is to be sent
  1461. Binding - the binding on which the request is to be sent
  1462. Return Value:
  1463. ULONG - status code.
  1464. Environment:
  1465. Invoked with 'Interfacep' locked and with a reference made to 'Interfacep'
  1466. for the send which occurs here.
  1467. If the routine fails, it is the caller's responsibility to release
  1468. the reference.
  1469. --*/
  1470. {
  1471. PNH_BUFFER Bufferp;
  1472. ULONG Error;
  1473. PDHCP_HEADER Headerp;
  1474. SOCKET Socket;
  1475. PROFILE("DhcpWriteClientRequestMessage");
  1476. //
  1477. // Create a socket using the given address
  1478. //
  1479. Error =
  1480. NhCreateDatagramSocket(
  1481. Binding->Address,
  1482. DHCP_PORT_CLIENT,
  1483. &Binding->ClientSocket
  1484. );
  1485. if (Error) {
  1486. NhTrace(
  1487. TRACE_FLAG_IF,
  1488. "DhcpWriteClientRequestMessage: error %d creating socket for %s",
  1489. Error,
  1490. INET_NTOA(Binding->Address)
  1491. );
  1492. NhWarningLog(
  1493. IP_AUTO_DHCP_LOG_DETECTION_UNAVAILABLE,
  1494. Error,
  1495. "%I",
  1496. Binding->Address
  1497. );
  1498. return Error;
  1499. }
  1500. //
  1501. // Allocate a buffer for the BOOTP request
  1502. //
  1503. Bufferp = NhAcquireBuffer();
  1504. if (!Bufferp) {
  1505. NhDeleteDatagramSocket(Binding->ClientSocket);
  1506. Binding->ClientSocket = INVALID_SOCKET;
  1507. NhTrace(
  1508. TRACE_FLAG_IF,
  1509. "DhcpWriteClientRequestMessage: error allocating buffer for %s",
  1510. INET_NTOA(Binding->Address)
  1511. );
  1512. NhErrorLog(
  1513. IP_AUTO_DHCP_LOG_ALLOCATION_FAILED,
  1514. 0,
  1515. "%d",
  1516. sizeof(NH_BUFFER)
  1517. );
  1518. return ERROR_NOT_ENOUGH_MEMORY;
  1519. }
  1520. //
  1521. // Initialize the BOOTP request
  1522. //
  1523. Headerp = (PDHCP_HEADER)Bufferp->Buffer;
  1524. ZeroMemory(Headerp, sizeof(*Headerp));
  1525. Headerp->Operation = BOOTP_OPERATION_REQUEST;
  1526. Headerp->HardwareAddressType = 1;
  1527. Headerp->HardwareAddressLength = 6;
  1528. Headerp->TransactionId = DHCP_DETECTION_TRANSACTION_ID;
  1529. Headerp->SecondsSinceBoot = 10;
  1530. Headerp->Flags |= BOOTP_FLAG_BROADCAST;
  1531. Headerp->ClientAddress = Binding->Address;
  1532. Headerp->HardwareAddress[1] = 0xab;
  1533. *(PULONG)(Headerp->Footer[0].Cookie) = DHCP_MAGIC_COOKIE;
  1534. *(PUCHAR)(Headerp->Footer + 1) = DHCP_TAG_END;
  1535. //
  1536. // Send the BOOTP request on the socket
  1537. //
  1538. Error =
  1539. NhWriteDatagramSocket(
  1540. &DhcpComponentReference,
  1541. Binding->ClientSocket,
  1542. INADDR_BROADCAST,
  1543. DHCP_PORT_SERVER,
  1544. Bufferp,
  1545. sizeof(DHCP_HEADER) + BOOTP_VENDOR_LENGTH,
  1546. DhcpWriteClientRequestCompletionRoutine,
  1547. (PVOID)Interfacep,
  1548. UlongToPtr(Binding->Address)
  1549. );
  1550. if (Error) {
  1551. NhReleaseBuffer(Bufferp);
  1552. NhDeleteDatagramSocket(Binding->ClientSocket);
  1553. Binding->ClientSocket = INVALID_SOCKET;
  1554. NhTrace(
  1555. TRACE_FLAG_IF,
  1556. "DhcpWriteClientRequestMessage: error %d writing request for %s",
  1557. Error,
  1558. INET_NTOA(Binding->Address)
  1559. );
  1560. NhWarningLog(
  1561. IP_AUTO_DHCP_LOG_DETECTION_UNAVAILABLE,
  1562. Error,
  1563. "%I",
  1564. Binding->Address
  1565. );
  1566. return Error;
  1567. }
  1568. return NO_ERROR;
  1569. } // DhcpWriteClientRequestMessage