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.

1439 lines
34 KiB

  1. /*
  2. Copyright (c) 1998, Microsoft Corporation, all rights reserved
  3. Description:
  4. History:
  5. */
  6. #include "rassrvr_.h"
  7. // This flag indicates if netbios options are to be preserved for
  8. // the ras server adapter or not.
  9. BOOL g_fDisableNetbiosOverTcpip = FALSE;
  10. extern BOOL HelperInitialized;
  11. BOOL WINAPI ShutdownHandlerRoutine ( DWORD dwCtrlType )
  12. {
  13. if ( CTRL_SHUTDOWN_EVENT == dwCtrlType )
  14. {
  15. TraceHlp("ShutdownHandlerRoutine. Got Shutdown Event. Releasing DhcpAddresses");
  16. //uninitialize Dhcp addresses here.
  17. RasDhcpUninitialize();
  18. return TRUE;
  19. }
  20. return FALSE;
  21. }
  22. /*
  23. Returns:
  24. Notes:
  25. */
  26. DWORD
  27. RasSrvrInitialize(
  28. IN MPRADMINGETIPADDRESSFORUSER* pfnMprGetAddress,
  29. IN MPRADMINRELEASEIPADDRESS* pfnMprReleaseAddress
  30. )
  31. {
  32. DNS_STATUS DnsStatus;
  33. HINSTANCE hInstance;
  34. DWORD dwErr = NO_ERROR;
  35. TraceHlp("RasSrvrInitialize");
  36. g_fDisableNetbiosOverTcpip = FALSE;
  37. //
  38. // Read the key that tells whether to disable netbios over ip
  39. //
  40. {
  41. HKEY hkeyRASIp;
  42. dwErr = RegOpenKeyExA(
  43. HKEY_LOCAL_MACHINE, REGKEY_RAS_IP_PARAM_A, 0,
  44. KEY_READ, &hkeyRASIp);
  45. if(NO_ERROR == dwErr)
  46. {
  47. DWORD dwSize = sizeof(DWORD) , dwData = 0, dwType;
  48. dwErr = RegQueryValueExA(
  49. hkeyRASIp,
  50. "DisableNetbiosOverTcpip",
  51. NULL, &dwType, (BYTE *) &dwData,
  52. &dwSize);
  53. if( (NO_ERROR == dwErr)
  54. && (dwType == REG_DWORD))
  55. {
  56. g_fDisableNetbiosOverTcpip = !!(dwData);
  57. }
  58. RegCloseKey(hkeyRASIp);
  59. dwErr = NO_ERROR;
  60. }
  61. }
  62. TraceHlp("DisableNetbt = %d", g_fDisableNetbiosOverTcpip);
  63. // Keep a ref count in HelperUninitialize, and call HelperUninitialize
  64. // once for each time you call HelperInitialize.
  65. dwErr = HelperInitialize(&hInstance);
  66. if (NO_ERROR != dwErr)
  67. {
  68. // goto LDone; Don't do this. The CriticalSections are not available.
  69. return(dwErr);
  70. }
  71. EnterCriticalSection(&RasSrvrCriticalSection);
  72. if (RasSrvrRunning)
  73. {
  74. goto LDone;
  75. }
  76. //Set the control handler for the process here
  77. if ( !SetProcessShutdownParameters( 510 , SHUTDOWN_NORETRY ) )
  78. {
  79. TraceHlp("SetProcessShutdownParameters failed and returned 0x%x. This is not a fatal error so continuing on with server start", GetLastError());
  80. }
  81. else
  82. {
  83. if ( !SetConsoleCtrlHandler( ShutdownHandlerRoutine, TRUE ) )
  84. {
  85. TraceHlp("SetConsoleCtrlHandler failed and returned 0x%x. This is not a fatal error so continuing on with server start", GetLastError());
  86. }
  87. }
  88. dwErr = rasSrvrInitAdapterName();
  89. if (NO_ERROR != dwErr)
  90. {
  91. goto LDone;
  92. }
  93. dwErr = MprAdminMIBServerConnect(NULL, &RasSrvrHMIBServer);
  94. if (NO_ERROR != dwErr)
  95. {
  96. TraceHlp("MprAdminMIBServerConnect failed and returned 0x%x", dwErr);
  97. goto LDone;
  98. }
  99. pfnMprAdminGetIpAddressForUser = pfnMprGetAddress;
  100. pfnMprAdminReleaseIpAddress = pfnMprReleaseAddress;
  101. DnsStatus = DnsDhcpSrvRegisterInit(
  102. NULL, // no creds
  103. 0 // no special queue length limit
  104. );
  105. if (DNSDHCP_SUCCESS != DnsStatus)
  106. {
  107. dwErr = DnsStatus;
  108. TraceHlp("DnsDhcpSrvRegisterInit failed and returned 0x%x", dwErr);
  109. goto LDone;
  110. }
  111. HelperChangeNotification();
  112. dwErr = RasSrvrStart();
  113. if (NO_ERROR != dwErr)
  114. {
  115. TraceHlp("RasSrvrStart failed and returned 0x%x", dwErr);
  116. goto LDone;
  117. }
  118. RasSrvrRunning = TRUE;
  119. LDone:
  120. //
  121. // Moved this up above RasSrvrUninitialize based on the
  122. // comment in that function. We should not call RasSrvrStop
  123. // when holding this critical section.
  124. //
  125. LeaveCriticalSection(&RasSrvrCriticalSection);
  126. if (NO_ERROR != dwErr)
  127. {
  128. RasSrvrUninitialize();
  129. }
  130. return(dwErr);
  131. }
  132. /*
  133. Returns:
  134. VOID
  135. Notes:
  136. */
  137. VOID
  138. RasSrvrUninitialize(
  139. VOID
  140. )
  141. {
  142. AINODE* pAiNode;
  143. DNS_STATUS DnsStatus;
  144. DWORD dwErr;
  145. TraceHlp("RasSrvrUninitialize");
  146. RasSrvrRunning = FALSE;
  147. /*
  148. Don't call RasSrvrStop when you have RasSrvrCriticalSection. It will call
  149. RasDhcpUninitialize, which will call RasDhcpTimerUninitialize, which will
  150. wait till all the timer work itemss are done. The timer work item could be
  151. rasDhcpAllocateAddress or rasDhcpRenewLease, both of which can call
  152. RasSrvrDhcpCallback, which tries to acquire RasSrvrCriticalSection.
  153. */
  154. SetConsoleCtrlHandler(ShutdownHandlerRoutine, FALSE);
  155. RasSrvrStop(FALSE /* fParametersChanged */);
  156. EnterCriticalSection(&RasSrvrCriticalSection);
  157. if (NULL != RasSrvrHMIBServer)
  158. {
  159. MprAdminMIBServerDisconnect(RasSrvrHMIBServer);
  160. RasSrvrHMIBServer = NULL;
  161. }
  162. pfnMprAdminGetIpAddressForUser = NULL;
  163. pfnMprAdminReleaseIpAddress = NULL;
  164. DnsStatus = DnsDhcpSrvRegisterTerm();
  165. if (DNSDHCP_SUCCESS != DnsStatus)
  166. {
  167. TraceHlp("DnsDhcpSrvRegisterTerm failed and returned 0x%x",
  168. DnsStatus);
  169. }
  170. if (NULL != PDisableDhcpInformServer)
  171. {
  172. PDisableDhcpInformServer();
  173. }
  174. g_fDisableNetbiosOverTcpip = FALSE;
  175. LeaveCriticalSection(&RasSrvrCriticalSection);
  176. }
  177. /*
  178. Returns:
  179. Notes:
  180. */
  181. DWORD
  182. RasSrvrStart(
  183. VOID
  184. )
  185. {
  186. DWORD dwErr;
  187. TraceHlp("RasSrvrStart");
  188. EnterCriticalSection(&RasSrvrCriticalSection);
  189. if (HelperRegVal.fUseDhcpAddressing)
  190. {
  191. dwErr = RasDhcpInitialize();
  192. }
  193. else
  194. {
  195. dwErr = RasStatInitialize();
  196. }
  197. LeaveCriticalSection(&RasSrvrCriticalSection);
  198. return(dwErr);
  199. }
  200. /*
  201. Returns:
  202. VOID
  203. Notes:
  204. */
  205. VOID
  206. RasSrvrStop(
  207. IN BOOL fParametersChanged
  208. )
  209. {
  210. AINODE* pAiNode;
  211. CHAR szIpAddress[MAXIPSTRLEN + 1];
  212. CHAR* sz;
  213. DWORD dwNumBytes;
  214. DWORD dwErr;
  215. WANARP_MAP_SERVER_ADAPTER_INFO info;
  216. TraceHlp("RasSrvrStop");
  217. RasDhcpUninitialize();
  218. EnterCriticalSection(&RasSrvrCriticalSection);
  219. if ( fParametersChanged
  220. && (0 != RasSrvrNboServerIpAddress))
  221. {
  222. AbcdSzFromIpAddress(RasSrvrNboServerIpAddress, szIpAddress);
  223. sz = szIpAddress;
  224. LogEvent(EVENTLOG_WARNING_TYPE, ROUTERLOG_SRV_ADDR_CHANGED, 1,
  225. (CHAR**)&sz);
  226. }
  227. if (!fParametersChanged)
  228. {
  229. while (NULL != RasSrvrAcquiredIpAddresses)
  230. {
  231. RasSrvrReleaseAddress(
  232. RasSrvrAcquiredIpAddresses->nboIpAddr,
  233. RasSrvrAcquiredIpAddresses->wszUserName,
  234. RasSrvrAcquiredIpAddresses->wszPortName,
  235. TRUE);
  236. // Assert: the list decreases by one node in each iteration.
  237. }
  238. }
  239. RasStatUninitialize();
  240. RasStatSetRoutes(RasSrvrNboServerIpAddress, FALSE);
  241. RasTcpSetProxyArp(RasSrvrNboServerIpAddress, FALSE);
  242. rasSrvrSetIpAddressInRegistry(0, 0);
  243. dwErr = PDhcpNotifyConfigChange(NULL, g_rgwcAdapterName, TRUE,
  244. 0, 0, 0, IgnoreFlag);
  245. if (NO_ERROR != dwErr)
  246. {
  247. TraceHlp("DhcpNotifyConfigChange failed and returned %d", dwErr);
  248. }
  249. if (RasSrvrAdapterMapped)
  250. {
  251. // Ask wanarp to unmap the adapter
  252. info.fMap = 0;
  253. if(!DeviceIoControl(HelperWanArpHandle,
  254. IOCTL_WANARP_MAP_SERVER_ADAPTER,
  255. &info,
  256. sizeof(WANARP_MAP_SERVER_ADAPTER_INFO),
  257. &info,
  258. sizeof(WANARP_MAP_SERVER_ADAPTER_INFO),
  259. &dwNumBytes,
  260. NULL))
  261. {
  262. dwErr = GetLastError();
  263. TraceHlp("Error %d unmapping server adapter", dwErr);
  264. }
  265. TraceHlp("RasSrvrAdapterUnMapped");
  266. RasSrvrAdapterMapped = FALSE;
  267. }
  268. RasSrvrNboServerIpAddress = 0;
  269. RasSrvrNboServerSubnetMask = 0;
  270. LeaveCriticalSection(&RasSrvrCriticalSection);
  271. /*
  272. Don't call RasDhcpUninitialize when you have RasSrvrCriticalSection. It
  273. will call RasDhcpTimerUninitialize, which will wait till all the timer work
  274. itemss are done. The timer work item could be rasDhcpAllocateAddress or
  275. rasDhcpRenewLease, both of which can call RasSrvrDhcpCallback, which tries
  276. to acquire RasSrvrCriticalSection.
  277. */
  278. //RasDhcpUninitialize();
  279. }
  280. /*
  281. Returns:
  282. Description:
  283. */
  284. DWORD
  285. RasSrvrAcquireAddress(
  286. IN HPORT hPort,
  287. IN IPADDR nboIpAddressRequested,
  288. OUT IPADDR* pnboIpAddressAllocated,
  289. IN WCHAR* wszUserName,
  290. IN WCHAR* wszPortName
  291. )
  292. {
  293. IPADDR nboIpAddr;
  294. IPADDR nboIpMask;
  295. IPADDR nboIpAddrObtained = 0;
  296. IPADDR nboIpAddrFromDll = 0;
  297. BOOL fNotifyDll = FALSE;
  298. BOOL fEasyNet = FALSE;
  299. WCHAR* wszUserNameTemp = NULL;
  300. WCHAR* wszPortNameTemp = NULL;
  301. AINODE* pAiNode = NULL;
  302. DWORD dwErr = NO_ERROR;
  303. TraceHlp("RasSrvrAcquireAddress(hPort: 0x%x, IP address: 0x%x, "
  304. "UserName: %ws, PortName: %ws)",
  305. hPort, nboIpAddressRequested, wszUserName, wszPortName);
  306. EnterCriticalSection(&RasSrvrCriticalSection);
  307. dwErr = rasSrvrGetAddressForServerAdapter();
  308. if (NO_ERROR != dwErr)
  309. {
  310. goto LDone;
  311. }
  312. if (nboIpAddressRequested == RasSrvrNboServerIpAddress)
  313. {
  314. // The server's address is being requested. Forget the request.
  315. nboIpAddressRequested = 0;
  316. }
  317. nboIpAddr = nboIpAddressRequested;
  318. dwErr = rasSrvrAcquireAddressEx(hPort, &nboIpAddr, &nboIpMask, &fEasyNet);
  319. if ( (NO_ERROR != dwErr)
  320. && (0 != nboIpAddr))
  321. {
  322. // We couldn't get the address we wanted. Let us get any other
  323. // address.
  324. nboIpAddr = 0;
  325. dwErr = rasSrvrAcquireAddressEx(hPort, &nboIpAddr, &nboIpMask,
  326. &fEasyNet);
  327. }
  328. if (NO_ERROR == dwErr)
  329. {
  330. RTASSERT(0 != nboIpAddr);
  331. nboIpAddrObtained = nboIpAddr;
  332. }
  333. else
  334. {
  335. RTASSERT(0 == nboIpAddr);
  336. goto LDone;
  337. }
  338. if (NULL != pfnMprAdminGetIpAddressForUser)
  339. {
  340. nboIpAddrFromDll = nboIpAddrObtained;
  341. dwErr = pfnMprAdminGetIpAddressForUser(wszUserName, wszPortName,
  342. &nboIpAddrFromDll, &fNotifyDll);
  343. if (NO_ERROR != dwErr)
  344. {
  345. TraceHlp("MprAdminGetIpAddressForUser(%ws, %ws, 0x%x) failed "
  346. "and returned %d",
  347. wszUserName, wszPortName, nboIpAddrFromDll, dwErr);
  348. goto LDone;
  349. }
  350. if ( (0 == nboIpAddrFromDll)
  351. || (RasSrvrNboServerIpAddress == nboIpAddrFromDll))
  352. {
  353. // We can't give the server's address.
  354. TraceHlp("3rd party DLL wants to hand out bad address 0x%x",
  355. nboIpAddrFromDll);
  356. dwErr = ERROR_NOT_FOUND;
  357. goto LDone;
  358. }
  359. if (nboIpAddrObtained != nboIpAddrFromDll)
  360. {
  361. TraceHlp("3rd party DLL wants to hand out address 0x%x",
  362. nboIpAddrFromDll);
  363. // We have to make sure that nboIpAddrFromDll is available.
  364. // The DLL changed what we had got from Dhcp or Static. Release the
  365. // old address.
  366. if (HelperRegVal.fUseDhcpAddressing)
  367. {
  368. RasDhcpReleaseAddress(nboIpAddrObtained);
  369. }
  370. else
  371. {
  372. RasStatReleaseAddress(nboIpAddrObtained);
  373. }
  374. nboIpAddrObtained = 0;
  375. fEasyNet = FALSE;
  376. nboIpAddr = nboIpAddrFromDll;
  377. dwErr = rasSrvrAcquireAddressEx(hPort, &nboIpAddr, &nboIpMask,
  378. &fEasyNet);
  379. if (NO_ERROR != dwErr)
  380. {
  381. goto LDone;
  382. }
  383. nboIpAddrObtained = nboIpAddr;
  384. }
  385. }
  386. wszUserNameTemp = _wcsdup(wszUserName);
  387. if (NULL == wszUserNameTemp)
  388. {
  389. dwErr = ERROR_OUTOFMEMORY;
  390. TraceHlp("_strdup failed and returned %d", dwErr);
  391. goto LDone;
  392. }
  393. wszPortNameTemp = _wcsdup(wszPortName);
  394. if (NULL == wszPortNameTemp)
  395. {
  396. dwErr = ERROR_OUTOFMEMORY;
  397. TraceHlp("_strdup failed and returned %d", dwErr);
  398. goto LDone;
  399. }
  400. pAiNode = LocalAlloc(LPTR, sizeof(AINODE));
  401. if (NULL == pAiNode)
  402. {
  403. dwErr = GetLastError();
  404. TraceHlp("LocalAlloc failed and returned %d", dwErr);
  405. goto LDone;
  406. }
  407. pAiNode->hPort = hPort;
  408. pAiNode->fFlags = fNotifyDll ? AINODE_FLAG_NOTIFY_DLL : 0;
  409. pAiNode->fFlags |= fEasyNet ? AINODE_FLAG_EASYNET : 0;
  410. pAiNode->wszUserName = wszUserNameTemp;
  411. pAiNode->wszPortName = wszPortNameTemp;
  412. pAiNode->pNext = RasSrvrAcquiredIpAddresses;
  413. RasSrvrAcquiredIpAddresses = pAiNode;
  414. pAiNode->nboIpAddr = *pnboIpAddressAllocated = nboIpAddrObtained;
  415. LDone:
  416. if (NO_ERROR != dwErr)
  417. {
  418. if (fNotifyDll)
  419. {
  420. pfnMprAdminReleaseIpAddress(wszUserName, wszPortName,
  421. &nboIpAddrFromDll);
  422. }
  423. if (0 != nboIpAddrObtained)
  424. {
  425. if (HelperRegVal.fUseDhcpAddressing)
  426. {
  427. RasDhcpReleaseAddress(nboIpAddrObtained);
  428. }
  429. else
  430. {
  431. RasStatReleaseAddress(nboIpAddrObtained);
  432. }
  433. }
  434. free(wszUserNameTemp);
  435. free(wszPortNameTemp);
  436. }
  437. LeaveCriticalSection(&RasSrvrCriticalSection);
  438. return(dwErr);
  439. }
  440. /*
  441. Returns:
  442. Description:
  443. */
  444. VOID
  445. RasSrvrReleaseAddress(
  446. IN IPADDR nboIpAddress,
  447. IN WCHAR* wszUserName,
  448. IN WCHAR* wszPortName,
  449. IN BOOL fDeregister
  450. )
  451. {
  452. DNS_STATUS DnsStatus;
  453. REGISTER_HOST_ENTRY HostAddr;
  454. AINODE* pAiNode = NULL;
  455. DWORD dwErr;
  456. TraceHlp("RasSrvrReleaseAddress(IP address: 0x%x, "
  457. "UserName: %ws, PortName: %ws)",
  458. nboIpAddress, wszUserName, wszPortName);
  459. EnterCriticalSection(&RasSrvrCriticalSection);
  460. if (fDeregister)
  461. {
  462. HostAddr.dwOptions = REGISTER_HOST_PTR;
  463. HostAddr.Addr.ipAddr = nboIpAddress;
  464. DnsStatus = DnsDhcpSrvRegisterHostName(
  465. HostAddr,
  466. NULL,
  467. 600,
  468. DYNDNS_DELETE_ENTRY | DYNDNS_REG_FORWARD,
  469. NULL,
  470. NULL,
  471. NULL,
  472. 0 );
  473. if (DNSDHCP_SUCCESS != DnsStatus)
  474. {
  475. TraceHlp("DnsDhcpSrvRegisterHostName_A(0x%x) failed: 0x%x",
  476. nboIpAddress, DnsStatus);
  477. }
  478. }
  479. pAiNode = rasSrvrFindAiNode(nboIpAddress, TRUE /* fRemoveFromList */);
  480. if (NULL == pAiNode)
  481. {
  482. TraceHlp("Couldn't find address 0x%x in Acquired Ip Addresses list",
  483. nboIpAddress);
  484. goto LDone;
  485. }
  486. if (HelperRegVal.fUseDhcpAddressing)
  487. {
  488. RasDhcpReleaseAddress(nboIpAddress);
  489. }
  490. else
  491. {
  492. RasStatReleaseAddress(nboIpAddress);
  493. }
  494. if (pAiNode->fFlags & AINODE_FLAG_NOTIFY_DLL)
  495. {
  496. pfnMprAdminReleaseIpAddress(wszUserName, wszPortName, &nboIpAddress);
  497. }
  498. if (pAiNode->fFlags & AINODE_FLAG_ACTIVATED)
  499. {
  500. if (!(pAiNode->fFlags & AINODE_FLAG_EASYNET))
  501. {
  502. RasTcpSetProxyArp(nboIpAddress, FALSE);
  503. }
  504. dwErr = rasSrvrGetAddressForServerAdapter();
  505. if (NO_ERROR != dwErr)
  506. {
  507. TraceHlp("Couldn't get address for server adapter");
  508. goto LDone;
  509. }
  510. RasTcpSetRoute(nboIpAddress,
  511. nboIpAddress,
  512. HOST_MASK,
  513. RasSrvrNboServerIpAddress,
  514. FALSE,
  515. 1,
  516. TRUE);
  517. }
  518. LDone:
  519. LeaveCriticalSection(&RasSrvrCriticalSection);
  520. rasSrvrFreeAiNode(pAiNode);
  521. }
  522. /*
  523. Returns:
  524. Description:
  525. Look up the DNS server, WINS server, and "this server" addresses.
  526. */
  527. DWORD
  528. RasSrvrQueryServerAddresses(
  529. IN OUT IPINFO* pIpInfo
  530. )
  531. {
  532. DWORD dwNumBytes;
  533. IPADDR nboWins1 = 0;
  534. IPADDR nboWins2 = 0;
  535. IPADDR nboDns1 = 0;
  536. IPADDR nboDns2 = 0;
  537. DWORD dwErr = NO_ERROR;
  538. TraceHlp("RasSrvrQueryServerAddresses");
  539. EnterCriticalSection(&RasSrvrCriticalSection);
  540. dwErr = rasSrvrGetAddressForServerAdapter();
  541. if (NO_ERROR != dwErr)
  542. {
  543. goto LDone;
  544. }
  545. // Ignore errors; its OK not to be able to give DNS or WINS server
  546. // addresses
  547. GetPreferredAdapterInfo(NULL, &nboDns1, &nboDns2, &nboWins1,
  548. &nboWins2, NULL);
  549. if (TRUE == HelperRegVal.fSuppressWINSNameServers)
  550. {
  551. nboWins1 = 0;
  552. nboWins2 = 0;
  553. }
  554. else if (0 != HelperRegVal.nboWINSNameServer1)
  555. {
  556. nboWins1 = HelperRegVal.nboWINSNameServer1;
  557. nboWins2 = HelperRegVal.nboWINSNameServer2;
  558. }
  559. if (TRUE == HelperRegVal.fSuppressDNSNameServers)
  560. {
  561. nboDns1 = 0;
  562. nboDns2 = 0;
  563. }
  564. else if (0 != HelperRegVal.nboDNSNameServer1)
  565. {
  566. nboDns1 = HelperRegVal.nboDNSNameServer1;
  567. nboDns2 = HelperRegVal.nboDNSNameServer2;
  568. }
  569. pIpInfo->nboDNSAddress = nboDns1;
  570. pIpInfo->nboDNSAddressBackup = nboDns2;
  571. pIpInfo->nboWINSAddress = nboWins1;
  572. pIpInfo->nboWINSAddressBackup = nboWins2;
  573. pIpInfo->nboServerIpAddress = RasSrvrNboServerIpAddress;
  574. pIpInfo->nboServerSubnetMask = RasSrvrNboServerSubnetMask;
  575. LDone:
  576. LeaveCriticalSection(&RasSrvrCriticalSection);
  577. return(dwErr);
  578. }
  579. /*
  580. Returns:
  581. Description:
  582. Does two things - RasTcpSetRoute and RasTcpSetProxyArp.
  583. */
  584. DWORD
  585. RasSrvrActivateIp(
  586. IN IPADDR nboIpAddress,
  587. IN DWORD dwUsage
  588. )
  589. {
  590. AINODE* pAiNode;
  591. DWORD dwErr = NO_ERROR;
  592. TraceHlp("RasSrvrActivateIp(IpAddr = 0x%x, dwUsage = %d)",
  593. nboIpAddress, dwUsage);
  594. EnterCriticalSection(&RasSrvrCriticalSection);
  595. pAiNode = rasSrvrFindAiNode(nboIpAddress, FALSE /* fRemoveFromList */);
  596. if (NULL == pAiNode)
  597. {
  598. TraceHlp("Couldn't find address 0x%x in Acquired Ip Addresses list",
  599. nboIpAddress);
  600. dwErr = ERROR_IP_CONFIGURATION;
  601. goto LDone;
  602. }
  603. pAiNode->fFlags |= AINODE_FLAG_ACTIVATED;
  604. RasTcpSetProxyArp(nboIpAddress, TRUE);
  605. if (dwUsage != DU_ROUTER)
  606. {
  607. // Add a route to the route table. Router connections get
  608. // added by router manager
  609. dwErr = rasSrvrGetAddressForServerAdapter();
  610. if (NO_ERROR != dwErr)
  611. {
  612. // Don't return an error, because we have done RasTcpSetProxyArp.
  613. dwErr = NO_ERROR;
  614. TraceHlp("Couldn't get address for server adapter");
  615. goto LDone;
  616. }
  617. RasTcpSetRoute(nboIpAddress,
  618. nboIpAddress,
  619. HOST_MASK,
  620. RasSrvrNboServerIpAddress,
  621. TRUE,
  622. 1,
  623. TRUE);
  624. }
  625. LDone:
  626. LeaveCriticalSection(&RasSrvrCriticalSection);
  627. return(dwErr);
  628. }
  629. /*
  630. Returns:
  631. VOID
  632. Description:
  633. Called by dhcp address code when the lease for a given address expires.
  634. nboIpAddr = 0 indicates the server's IP address.
  635. */
  636. VOID
  637. RasSrvrDhcpCallback(
  638. IN IPADDR nboIpAddr
  639. )
  640. {
  641. AINODE* pAiNode = NULL;
  642. CHAR szIpAddress[MAXIPSTRLEN + 1];
  643. CHAR* sz;
  644. DWORD dwErr = NO_ERROR;
  645. TraceHlp("RasSrvrDhcpCallback(0x%x)", nboIpAddr);
  646. EnterCriticalSection(&RasSrvrCriticalSection);
  647. if ( (0 == nboIpAddr)
  648. && (0 == RasSrvrNboServerIpAddress))
  649. {
  650. // The server hasn't got an IP address yet. Its lease hasn't really
  651. // expired. We are just simulating it.
  652. goto LDone;
  653. }
  654. if ( (0 == nboIpAddr)
  655. || (nboIpAddr == RasSrvrNboServerIpAddress))
  656. {
  657. TraceHlp("******** SERVER ADDRESS (0x%x) LEASE EXPIRED ********",
  658. RasSrvrNboServerIpAddress);
  659. AbcdSzFromIpAddress(RasSrvrNboServerIpAddress, szIpAddress);
  660. sz = szIpAddress;
  661. // Log that the server adapter address lease was lost
  662. LogEvent(EVENTLOG_WARNING_TYPE, ROUTERLOG_SRV_ADDR_CHANGED, 1,
  663. (CHAR**)&sz);
  664. // Unroute all the connected clients
  665. while (NULL != RasSrvrAcquiredIpAddresses)
  666. {
  667. RasSrvrReleaseAddress(
  668. RasSrvrAcquiredIpAddresses->nboIpAddr,
  669. RasSrvrAcquiredIpAddresses->wszUserName,
  670. RasSrvrAcquiredIpAddresses->wszPortName,
  671. TRUE);
  672. // Assert: the list decreases by one node in each iteration.
  673. }
  674. RasTcpSetProxyArp(RasSrvrNboServerIpAddress, FALSE);
  675. rasSrvrSetIpAddressInRegistry(0, 0);
  676. dwErr = PDhcpNotifyConfigChange(NULL, g_rgwcAdapterName, TRUE,
  677. 0, 0, 0, IgnoreFlag);
  678. if (NO_ERROR != dwErr)
  679. {
  680. TraceHlp("DhcpNotifyConfigChange failed and returned %d", dwErr);
  681. }
  682. RasSrvrNboServerIpAddress = 0;
  683. RasSrvrNboServerSubnetMask = 0;
  684. }
  685. else
  686. {
  687. pAiNode = rasSrvrFindAiNode(nboIpAddr, TRUE /* fRemoveFromList */);
  688. if (NULL != pAiNode)
  689. {
  690. TraceHlp("******** CLIENT ADDRESS (0x%x) LEASE EXPIRED ********",
  691. nboIpAddr);
  692. AbcdSzFromIpAddress(nboIpAddr, szIpAddress);
  693. sz = szIpAddress;
  694. // Log that the client's address lease could not be renewed
  695. LogEvent(EVENTLOG_WARNING_TYPE, ROUTERLOG_CLIENT_ADDR_LEASE_LOST, 1,
  696. (CHAR**)&sz);
  697. RasSrvrReleaseAddress(nboIpAddr, pAiNode->wszUserName,
  698. pAiNode->wszPortName, TRUE);
  699. }
  700. }
  701. LDone:
  702. LeaveCriticalSection(&RasSrvrCriticalSection);
  703. rasSrvrFreeAiNode(pAiNode);
  704. }
  705. /*
  706. Returns:
  707. VOID
  708. Description:
  709. */
  710. VOID
  711. RasSrvrEnableRouter(
  712. BOOL fEnable
  713. )
  714. {
  715. DWORD dwErr;
  716. DEFINE_MIB_BUFFER(pSetInfo, MIB_IPSTATS, pSetStats);
  717. TraceHlp("RasSrvrEnableRouter(%d)", fEnable);
  718. EnterCriticalSection(&RasSrvrCriticalSection);
  719. pSetInfo->dwId = IP_STATS;
  720. pSetStats->dwForwarding = fEnable? MIB_IP_FORWARDING: MIB_IP_NOT_FORWARDING;
  721. pSetStats->dwDefaultTTL = MIB_USE_CURRENT_TTL;
  722. dwErr = MprAdminMIBEntrySet(
  723. RasSrvrHMIBServer,
  724. PID_IP,
  725. IPRTRMGR_PID,
  726. (VOID*)pSetInfo,
  727. MIB_INFO_SIZE(MIB_IPSTATS));
  728. if (NO_ERROR != dwErr)
  729. {
  730. TraceHlp("MprAdminMIBEntrySet failed with error %x", dwErr);
  731. }
  732. LeaveCriticalSection(&RasSrvrCriticalSection);
  733. }
  734. /*
  735. Returns:
  736. VOID
  737. Description:
  738. */
  739. VOID
  740. RasSrvrAdapterUnmapped(
  741. VOID
  742. )
  743. {
  744. if (HelperInitialized)
  745. {
  746. EnterCriticalSection(&RasSrvrCriticalSection);
  747. RasSrvrAdapterMapped = FALSE;
  748. TraceHlp("RasSrvrAdapterUnMapped");
  749. LeaveCriticalSection(&RasSrvrCriticalSection);
  750. }
  751. }
  752. /*
  753. Returns:
  754. VOID
  755. Description:
  756. */
  757. DWORD
  758. rasSrvrInitAdapterName(
  759. VOID
  760. )
  761. {
  762. DWORD dwNumBytes;
  763. WANARP_ADD_INTERFACE_INFO info;
  764. DWORD dwErr = NO_ERROR;
  765. info.dwUserIfIndex = WANARP_INVALID_IFINDEX;
  766. info.bCallinInterface = TRUE;
  767. if (!DeviceIoControl(HelperWanArpHandle,
  768. IOCTL_WANARP_ADD_INTERFACE,
  769. &info,
  770. sizeof(WANARP_ADD_INTERFACE_INFO),
  771. &info,
  772. sizeof(WANARP_ADD_INTERFACE_INFO),
  773. &dwNumBytes,
  774. NULL))
  775. {
  776. dwErr = GetLastError();
  777. TraceHlp("rasSrvrInitAdapterName: Error %d getting server name",
  778. dwErr);
  779. goto LDone;
  780. }
  781. wcsncpy(g_rgwcAdapterName, info.rgwcDeviceName, WANARP_MAX_DEVICE_NAME_LEN);
  782. // The RAS server adapter must not be registered with DNS. (These API's are
  783. // called in IpcpProjectionNotification also.)
  784. DnsDisableAdapterDomainNameRegistration(g_rgwcAdapterName);
  785. DnsDisableDynamicRegistration(g_rgwcAdapterName);
  786. LDone:
  787. return(dwErr);
  788. }
  789. /*
  790. Returns:
  791. Description:
  792. */
  793. AINODE*
  794. rasSrvrFindAiNode(
  795. IN IPADDR nboIpAddr,
  796. IN BOOL fRemoveFromList
  797. )
  798. {
  799. AINODE* pNode;
  800. AINODE* pNodePrev;
  801. EnterCriticalSection(&RasSrvrCriticalSection);
  802. for (pNode = RasSrvrAcquiredIpAddresses, pNodePrev = pNode;
  803. NULL != pNode;
  804. pNodePrev = pNode, pNode = pNode->pNext)
  805. {
  806. if (pNode->nboIpAddr == nboIpAddr)
  807. {
  808. break;
  809. }
  810. }
  811. if (!fRemoveFromList)
  812. {
  813. goto LDone;
  814. }
  815. if (NULL == pNode)
  816. {
  817. goto LDone;
  818. }
  819. if (pNode == pNodePrev)
  820. {
  821. RTASSERT(pNode == RasSrvrAcquiredIpAddresses);
  822. RasSrvrAcquiredIpAddresses = pNode->pNext;
  823. goto LDone;
  824. }
  825. pNodePrev->pNext = pNode->pNext;
  826. LDone:
  827. LeaveCriticalSection(&RasSrvrCriticalSection);
  828. return(pNode);
  829. }
  830. /*
  831. Returns:
  832. Description:
  833. */
  834. VOID
  835. rasSrvrFreeAiNode(
  836. IN AINODE* pNode
  837. )
  838. {
  839. if (NULL != pNode)
  840. {
  841. free(pNode->wszUserName);
  842. free(pNode->wszPortName);
  843. LocalFree(pNode);
  844. }
  845. }
  846. /*
  847. Returns:
  848. Description:
  849. */
  850. DWORD
  851. rasSrvrSetIpAddressInRegistry(
  852. IN IPADDR nboIpAddr,
  853. IN IPADDR nboIpMask
  854. )
  855. {
  856. TCPIP_INFO* pTcpipInfo = NULL;
  857. DWORD dwErr = NO_ERROR;
  858. dwErr = LoadTcpipInfo(&pTcpipInfo, g_rgwcAdapterName,
  859. TRUE /* fAdapterOnly */);
  860. if (NO_ERROR != dwErr)
  861. {
  862. TraceHlp("LoadTcpipInfo(%ws) failed and returned %d",
  863. g_rgwcAdapterName, dwErr);
  864. goto LDone;
  865. }
  866. AbcdWszFromIpAddress(nboIpAddr, pTcpipInfo->wszIPAddress);
  867. AbcdWszFromIpAddress(nboIpMask, pTcpipInfo->wszSubnetMask);
  868. if(g_fDisableNetbiosOverTcpip)
  869. {
  870. TraceHlp("rasSrvrSetIpAddressInRegistry: Netbios disabled");
  871. pTcpipInfo->fDisableNetBIOSoverTcpip = TRUE;
  872. }
  873. pTcpipInfo->fChanged = TRUE;
  874. dwErr = SaveTcpipInfo(pTcpipInfo);
  875. if (dwErr != NO_ERROR)
  876. {
  877. TraceHlp("SaveTcpipInfo(%ws) failed and returned %d",
  878. g_rgwcAdapterName, dwErr);
  879. goto LDone;
  880. }
  881. LDone:
  882. FreeTcpipInfo(&pTcpipInfo);
  883. return(dwErr);
  884. }
  885. /*
  886. Returns:
  887. Notes:
  888. */
  889. DWORD
  890. rasSrvrAcquireAddressEx(
  891. IN HPORT hPort,
  892. IN OUT IPADDR* pnboIpAddr,
  893. IN OUT IPADDR* pnboIpMask,
  894. OUT BOOL* pfEasyNet
  895. )
  896. {
  897. BOOL fExitWhile;
  898. BOOL fAnyAddress;
  899. AINODE* pAiNode;
  900. DWORD dwErr = NO_ERROR;
  901. EnterCriticalSection(&RasSrvrCriticalSection);
  902. if (NULL != pfEasyNet)
  903. {
  904. *pfEasyNet = FALSE;
  905. }
  906. fAnyAddress = (0 == *pnboIpAddr);
  907. fExitWhile = FALSE;
  908. while (!fExitWhile)
  909. {
  910. dwErr = NO_ERROR;
  911. if (fAnyAddress)
  912. {
  913. if (HelperRegVal.fUseDhcpAddressing)
  914. {
  915. dwErr = RasDhcpAcquireAddress(hPort, pnboIpAddr, pnboIpMask,
  916. pfEasyNet);
  917. }
  918. else
  919. {
  920. dwErr = RasStatAcquireAddress(hPort, pnboIpAddr, pnboIpMask);
  921. }
  922. }
  923. if (NO_ERROR != dwErr)
  924. {
  925. goto LDone;
  926. }
  927. for (pAiNode = RasSrvrAcquiredIpAddresses; NULL != pAiNode;
  928. pAiNode = pAiNode->pNext)
  929. {
  930. if (pAiNode->nboIpAddr == *pnboIpAddr)
  931. {
  932. // This address is in use
  933. if (fAnyAddress)
  934. {
  935. // Ask for another address
  936. goto LWhileEnd;
  937. }
  938. else
  939. {
  940. TraceHlp("Address 0x%x is already in use", *pnboIpAddr);
  941. dwErr = ERROR_PPP_REQUIRED_ADDRESS_REJECTED;
  942. goto LDone;
  943. }
  944. }
  945. }
  946. dwErr = NO_ERROR;
  947. fExitWhile = TRUE;
  948. LWhileEnd:
  949. ;
  950. }
  951. LDone:
  952. LeaveCriticalSection(&RasSrvrCriticalSection);
  953. if ( fAnyAddress
  954. && (NO_ERROR != dwErr))
  955. {
  956. LogEvent(EVENTLOG_WARNING_TYPE, ROUTERLOG_NO_IP_ADDRESS, 0, NULL);
  957. }
  958. return(dwErr);
  959. }
  960. /*
  961. Returns:
  962. Description:
  963. */
  964. DWORD
  965. rasSrvrGetAddressForServerAdapter(
  966. VOID
  967. )
  968. {
  969. IPADDR nboIpAddr = 0;
  970. IPADDR nboIpMask;
  971. CHAR szIpAddress[MAXIPSTRLEN + 1];
  972. CHAR* sz;
  973. BOOL fAddrAcquired = FALSE;
  974. BOOL fAdapterMapped = FALSE;
  975. DWORD dwNumBytes;
  976. DWORD dwErrTemp;
  977. DWORD dwErr = NO_ERROR;
  978. WANARP_MAP_SERVER_ADAPTER_INFO info;
  979. TraceHlp("rasSrvrGetAddressForServerAdapter");
  980. EnterCriticalSection(&RasSrvrCriticalSection);
  981. if (!RasSrvrAdapterMapped)
  982. {
  983. // First time - ask wanarp to map the adapter
  984. info.fMap = 1;
  985. info.dwAdapterIndex = (DWORD)-1;
  986. if(!DeviceIoControl(HelperWanArpHandle,
  987. IOCTL_WANARP_MAP_SERVER_ADAPTER,
  988. &info,
  989. sizeof(WANARP_MAP_SERVER_ADAPTER_INFO),
  990. &info,
  991. sizeof(WANARP_MAP_SERVER_ADAPTER_INFO),
  992. &dwNumBytes,
  993. NULL))
  994. {
  995. dwErr = GetLastError();
  996. TraceHlp("Error %d mapping server adapter", dwErr);
  997. goto LDone;
  998. }
  999. TraceHlp("RasSrvrAdapterMapped");
  1000. RasSrvrAdapterMapped = TRUE;
  1001. fAdapterMapped = TRUE;
  1002. }
  1003. if (0 != RasSrvrNboServerIpAddress)
  1004. {
  1005. if (!fAdapterMapped)
  1006. {
  1007. goto LDone;
  1008. }
  1009. }
  1010. else
  1011. {
  1012. dwErr = rasSrvrAcquireAddressEx((HPORT) ULongToPtr(((ULONG) SERVER_HPORT)),
  1013. &nboIpAddr, &nboIpMask, NULL);
  1014. if (NO_ERROR != dwErr)
  1015. {
  1016. goto LDone;
  1017. }
  1018. fAddrAcquired = TRUE;
  1019. RasSrvrNboServerIpAddress = nboIpAddr;
  1020. RasSrvrNboServerSubnetMask = HOST_MASK;
  1021. }
  1022. dwErr = rasSrvrSetIpAddressInRegistry(
  1023. RasSrvrNboServerIpAddress, RasSrvrNboServerSubnetMask);
  1024. if (NO_ERROR != dwErr)
  1025. {
  1026. goto LDone;
  1027. }
  1028. dwErr = PDhcpNotifyConfigChange(NULL, g_rgwcAdapterName, TRUE, 0,
  1029. RasSrvrNboServerIpAddress,
  1030. RasSrvrNboServerSubnetMask,
  1031. IgnoreFlag);
  1032. if (NO_ERROR != dwErr)
  1033. {
  1034. TraceHlp("DhcpNotifyConfigChange(%ws) failed and returned %d",
  1035. g_rgwcAdapterName, dwErr);
  1036. goto LDone;
  1037. }
  1038. /*
  1039. It looks like the default subnet route no longer gets added.
  1040. // Now delete the default subnet route added as a result of setting the
  1041. // adapter's IP address and subnet mask
  1042. RasTcpSetRoute(RasSrvrNboServerIpAddress & RasSrvrNboServerSubnetMask,
  1043. RasSrvrNboServerIpAddress,
  1044. RasSrvrNboServerSubnetMask,
  1045. RasSrvrNboServerIpAddress,
  1046. FALSE,
  1047. 1,
  1048. TRUE);
  1049. */
  1050. RasTcpSetProxyArp(RasSrvrNboServerIpAddress, TRUE);
  1051. if (!HelperRegVal.fUseDhcpAddressing)
  1052. {
  1053. RasStatSetRoutes(RasSrvrNboServerIpAddress, TRUE);
  1054. }
  1055. AbcdSzFromIpAddress(RasSrvrNboServerIpAddress, szIpAddress);
  1056. sz = szIpAddress;
  1057. LogEvent(EVENTLOG_INFORMATION_TYPE, ROUTERLOG_SRV_ADDR_ACQUIRED, 1,
  1058. (CHAR**)&sz);
  1059. TraceHlp("Acquired IP address 0x%x(%s) and subnet mask 0x%x for the server",
  1060. RasSrvrNboServerIpAddress, szIpAddress, RasSrvrNboServerSubnetMask);
  1061. LDone:
  1062. if (NO_ERROR != dwErr)
  1063. {
  1064. // Some cleanup is required here. We must release the
  1065. // address if RasSrvrNboServerIpAddress != 0 and get rid of the
  1066. // variable fAddrAcquired.
  1067. if (fAddrAcquired)
  1068. {
  1069. if (HelperRegVal.fUseDhcpAddressing)
  1070. {
  1071. RasDhcpReleaseAddress(nboIpAddr);
  1072. }
  1073. else
  1074. {
  1075. RasStatReleaseAddress(nboIpAddr);
  1076. }
  1077. RasTcpSetProxyArp(RasSrvrNboServerIpAddress, FALSE);
  1078. }
  1079. RasSrvrNboServerIpAddress = RasSrvrNboServerSubnetMask = 0;
  1080. rasSrvrSetIpAddressInRegistry(0, 0);
  1081. dwErrTemp = PDhcpNotifyConfigChange(NULL, g_rgwcAdapterName, TRUE,
  1082. 0, 0, 0, IgnoreFlag);
  1083. if (NO_ERROR != dwErrTemp)
  1084. {
  1085. TraceHlp("DhcpNotifyConfigChange failed and returned %d",
  1086. dwErrTemp);
  1087. }
  1088. }
  1089. LeaveCriticalSection(&RasSrvrCriticalSection);
  1090. return(dwErr);
  1091. }