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.

678 lines
18 KiB

  1. /* Copyright (c) 1993, Microsoft Corporation, all rights reserved
  2. **
  3. ** rasppp.c
  4. ** Remote Access PPP APIs
  5. **
  6. ** 11/15/93 Steve Cobb
  7. */
  8. #include <nt.h>
  9. #include <ntrtl.h>
  10. #include <nturtl.h>
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <wchar.h>
  15. #include <ctype.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <lmcons.h>
  20. #include <rasauth.h>
  21. #include <rasman.h>
  22. #include <rasppp.h>
  23. #include <ddmif.h>
  24. #include <rtutils.h>
  25. #include <mprlog.h>
  26. #include <raserror.h>
  27. #include <pppcp.h>
  28. #include <ppp.h>
  29. #include <util.h>
  30. #include <init.h>
  31. #include <worker.h>
  32. #include <bap.h>
  33. #include <raseapif.h>
  34. //**
  35. //
  36. // Call: StartPPP
  37. //
  38. // Returns: NO_ERROR - Success.
  39. // Non zero return code - Failure
  40. //
  41. // Description:
  42. //
  43. DWORD APIENTRY
  44. StartPPP(
  45. DWORD NumPorts
  46. )
  47. {
  48. DWORD dwRetCode;
  49. //
  50. // Read registry info, load CP DLLS, initialize globals etc.
  51. //
  52. PcbTable.NumPcbBuckets = ((DWORD)NumPorts > MAX_NUMBER_OF_PCB_BUCKETS)
  53. ? MAX_NUMBER_OF_PCB_BUCKETS
  54. : (DWORD)NumPorts;
  55. dwRetCode = InitializePPP();
  56. if ( dwRetCode != NO_ERROR )
  57. {
  58. PPPCleanUp();
  59. }
  60. else
  61. {
  62. PppLog( 2, "PPP Initialized successfully." );
  63. }
  64. return( dwRetCode );
  65. }
  66. //**
  67. //
  68. // Call: StopPPP
  69. //
  70. // Returns: NO_ERROR - Success.
  71. // Non zero return code - Failure
  72. //
  73. DWORD APIENTRY
  74. StopPPP(
  75. HANDLE hEventStopPPP
  76. )
  77. {
  78. PCB_WORK_ITEM * pWorkItem;
  79. PppLog( 2, "StopPPP called" );
  80. //
  81. // Insert shutdown event
  82. //
  83. pWorkItem = (PCB_WORK_ITEM*)LOCAL_ALLOC(LPTR,sizeof(PCB_WORK_ITEM));
  84. if ( pWorkItem == (PCB_WORK_ITEM *)NULL )
  85. {
  86. return( GetLastError() );
  87. }
  88. pWorkItem->Process = ProcessStopPPP;
  89. pWorkItem->hEvent = hEventStopPPP;
  90. InsertWorkItemInQ( pWorkItem );
  91. return( NO_ERROR );
  92. }
  93. //**
  94. //
  95. // Call: SendPPPMessageToEngine
  96. //
  97. // Returns: NO_ERROR - Success
  98. // non-zero - FAILURE
  99. //
  100. // Description: Will create a PCB_WORK_ITEM from a PPPE_MESSAGE structure
  101. // received from client or rassrv and Send it to the engine.
  102. //
  103. DWORD APIENTRY
  104. SendPPPMessageToEngine(
  105. IN PPPE_MESSAGE* pMessage
  106. )
  107. {
  108. PCB_WORK_ITEM * pWorkItem = (PCB_WORK_ITEM *)LOCAL_ALLOC(
  109. LPTR,
  110. sizeof(PCB_WORK_ITEM));
  111. if ( pWorkItem == (PCB_WORK_ITEM *)NULL )
  112. {
  113. LogPPPEvent( ROUTERLOG_NOT_ENOUGH_MEMORY, 0 );
  114. return( GetLastError() );
  115. }
  116. //
  117. // Set up PCB_WORK_ITEM structure from the PPPE_MESSAGE
  118. //
  119. pWorkItem->hPort = pMessage->hPort;
  120. switch( pMessage->dwMsgId )
  121. {
  122. case PPPEMSG_Start:
  123. pWorkItem->Process = ProcessLineUp;
  124. pWorkItem->fServer = FALSE;
  125. pWorkItem->PppMsg.Start = pMessage->ExtraInfo.Start;
  126. //Create a seed for encoding the password
  127. pMessage->ExtraInfo.Start.chSeed = pWorkItem->PppMsg.Start.chSeed = GEN_RAND_ENCODE_SEED;
  128. PppLog( 2, "PPPEMSG_Start recvd, d=%s, hPort=%d,callback=%d,"
  129. "mask=%x,IfType=%d",
  130. pMessage->ExtraInfo.Start.szDomain,
  131. pWorkItem->hPort,
  132. pMessage->ExtraInfo.Start.fThisIsACallback,
  133. pMessage->ExtraInfo.Start.ConfigInfo.dwConfigMask,
  134. pMessage->ExtraInfo.Start.PppInterfaceInfo.IfType );
  135. EncodePw( pWorkItem->PppMsg.Start.chSeed, pWorkItem->PppMsg.Start.szPassword );
  136. EncodePw( pMessage->ExtraInfo.Start.chSeed, pMessage->ExtraInfo.Start.szPassword );
  137. break;
  138. case PPPEMSG_Stop:
  139. PppLog( 2, "PPPEMSG_Stop recvd\r\n" );
  140. pWorkItem->Process = ProcessClose;
  141. pWorkItem->PppMsg.PppStop = pMessage->ExtraInfo.Stop;
  142. break;
  143. case PPPEMSG_Callback:
  144. PppLog( 2, "PPPEMSG_Callback recvd, hPort=%d\r\n",
  145. pWorkItem->hPort);
  146. pWorkItem->Process = ProcessGetCallbackNumberFromUser;
  147. pWorkItem->PppMsg.Callback = pMessage->ExtraInfo.Callback;
  148. break;
  149. case PPPEMSG_ChangePw:
  150. PppLog( 2, "PPPEMSG_ChangePw recvd, hPort=%d\r\n",
  151. pWorkItem->hPort);
  152. pWorkItem->Process = ProcessChangePassword;
  153. pWorkItem->PppMsg.ChangePw = pMessage->ExtraInfo.ChangePw;
  154. pWorkItem->PppMsg.ChangePw.chSeed = pMessage->ExtraInfo.ChangePw.chSeed = GEN_RAND_ENCODE_SEED;
  155. EncodePw( pWorkItem->PppMsg.ChangePw.chSeed, pWorkItem->PppMsg.ChangePw.szNewPassword );
  156. EncodePw( pMessage->ExtraInfo.ChangePw.chSeed, pMessage->ExtraInfo.ChangePw.szNewPassword );
  157. EncodePw( pWorkItem->PppMsg.ChangePw.chSeed, pWorkItem->PppMsg.ChangePw.szOldPassword );
  158. EncodePw( pMessage->ExtraInfo.ChangePw.chSeed, pMessage->ExtraInfo.ChangePw.szOldPassword );
  159. break;
  160. case PPPEMSG_Retry:
  161. PppLog( 2, "PPPEMSG_Retry recvd hPort=%d,u=%s",
  162. pWorkItem->hPort,
  163. pMessage->ExtraInfo.Start.szUserName );
  164. pWorkItem->Process = ProcessRetryPassword;
  165. pWorkItem->PppMsg.Retry = pMessage->ExtraInfo.Retry;
  166. pWorkItem->PppMsg.Retry.chSeed = pMessage->ExtraInfo.Retry.chSeed = GEN_RAND_ENCODE_SEED;
  167. EncodePw( pWorkItem->PppMsg.Retry.chSeed, pWorkItem->PppMsg.Retry.szPassword );
  168. EncodePw( pMessage->ExtraInfo.Retry.chSeed, pMessage->ExtraInfo.Retry.szPassword );
  169. break;
  170. case PPPEMSG_Receive:
  171. pWorkItem->Process = ProcessReceive;
  172. pWorkItem->PacketLen = pMessage->ExtraInfo.Receive.dwNumBytes;
  173. PppLog( 2, "Packet received (%d bytes) for hPort %d",
  174. pWorkItem->PacketLen, pWorkItem->hPort );
  175. pWorkItem->pPacketBuf = (PPP_PACKET *)LOCAL_ALLOC(LPTR,
  176. pWorkItem->PacketLen);
  177. if ( pWorkItem->pPacketBuf == (PPP_PACKET*)NULL )
  178. {
  179. LogPPPEvent( ROUTERLOG_NOT_ENOUGH_MEMORY, 0 );
  180. LOCAL_FREE( pWorkItem );
  181. return( GetLastError() );
  182. }
  183. CopyMemory( pWorkItem->pPacketBuf,
  184. pMessage->ExtraInfo.Receive.pbBuffer,
  185. pWorkItem->PacketLen );
  186. break;
  187. case PPPEMSG_LineDown:
  188. PppLog( 2, "PPPEMSG_LineDown recvd, hPort=%d\r\n",
  189. pWorkItem->hPort);
  190. pWorkItem->Process = ProcessLineDown;
  191. break;
  192. case PPPEMSG_ListenResult:
  193. pWorkItem->Process = ProcessRasPortListenEvent;
  194. break;
  195. case PPPEMSG_BapEvent:
  196. BapTrace( "Threshold event on HCONN 0x%x. Type: %s, Threshold: %s",
  197. pMessage->hConnection,
  198. pMessage->ExtraInfo.BapEvent.fTransmit ? "transmit" : "receive",
  199. pMessage->ExtraInfo.BapEvent.fAdd ? "upper" : "lower");
  200. pWorkItem->Process = ProcessThresholdEvent;
  201. pWorkItem->hConnection = pMessage->hConnection;
  202. pWorkItem->PppMsg.BapEvent = pMessage->ExtraInfo.BapEvent;
  203. break;
  204. case PPPEMSG_DdmStart:
  205. pWorkItem->Process = ProcessLineUp;
  206. pWorkItem->fServer = TRUE;
  207. pWorkItem->PppMsg.DdmStart = pMessage->ExtraInfo.DdmStart;
  208. break;
  209. case PPPEMSG_DdmCallbackDone:
  210. PppLog( 2, "PPPEMSG_DdmCallbackDone recvd\r\n" );
  211. pWorkItem->Process = ProcessCallbackDone;
  212. pWorkItem->fServer = TRUE;
  213. pWorkItem->PppMsg.CallbackDone = pMessage->ExtraInfo.CallbackDone;
  214. break;
  215. case PPPEMSG_DdmInterfaceInfo:
  216. pWorkItem->Process = ProcessInterfaceInfo;
  217. pWorkItem->fServer = TRUE;
  218. pWorkItem->hConnection = pMessage->hConnection;
  219. pWorkItem->PppMsg.InterfaceInfo = pMessage->ExtraInfo.InterfaceInfo;
  220. PppLog(2,"PPPEMSG_DdmInterfaceInfo recvd,IPXif=%x,IPif=%x,Type=%x\r\n",
  221. pWorkItem->PppMsg.InterfaceInfo.hIPXInterface,
  222. pWorkItem->PppMsg.InterfaceInfo.hIPInterface,
  223. pWorkItem->PppMsg.InterfaceInfo.IfType );
  224. break;
  225. case PPPEMSG_DdmBapCallbackResult:
  226. pWorkItem->Process = ProcessCallResult;
  227. pWorkItem->hConnection = pMessage->hConnection;
  228. pWorkItem->PppMsg.BapCallResult.dwResult =
  229. pMessage->ExtraInfo.BapCallbackResult.dwCallbackResultCode;
  230. pWorkItem->PppMsg.BapCallResult.hRasConn = (HRASCONN) -1;
  231. break;
  232. case PPPEMSG_DhcpInform:
  233. pWorkItem->Process = ProcessDhcpInform;
  234. pWorkItem->hConnection = pMessage->hConnection;
  235. pWorkItem->PppMsg.DhcpInform = pMessage->ExtraInfo.DhcpInform;
  236. break;
  237. case PPPEMSG_EapUIData:
  238. pWorkItem->Process = ProcessEapUIData;
  239. pWorkItem->PppMsg.EapUIData = pMessage->ExtraInfo.EapUIData;
  240. break;
  241. case PPPEMSG_ProtocolEvent:
  242. pWorkItem->Process = ProcessProtocolEvent;
  243. pWorkItem->PppMsg.ProtocolEvent = pMessage->ExtraInfo.ProtocolEvent;
  244. break;
  245. case PPPEMSG_DdmChangeNotification:
  246. pWorkItem->Process = ProcessChangeNotification;
  247. break;
  248. case PPPEMSG_IpAddressLeaseExpired:
  249. pWorkItem->Process = ProcessIpAddressLeaseExpired;
  250. pWorkItem->PppMsg.IpAddressLeaseExpired =
  251. pMessage->ExtraInfo.IpAddressLeaseExpired;
  252. break;
  253. case PPPEMSG_PostLineDown:
  254. pWorkItem->Process = ProcessPostLineDown;
  255. pWorkItem->PppMsg.PostLineDown =
  256. pMessage->ExtraInfo.PostLineDown;
  257. break;
  258. default:
  259. PppLog( 2,"Unknown IPC message %d received\r\n", pMessage->dwMsgId );
  260. LOCAL_FREE( pWorkItem );
  261. pWorkItem = (PCB_WORK_ITEM*)NULL;
  262. }
  263. if ( pWorkItem != NULL )
  264. {
  265. InsertWorkItemInQ( pWorkItem );
  266. }
  267. return( NO_ERROR );
  268. }
  269. //**
  270. //
  271. // Call: PppDdmInit
  272. //
  273. // Returns: NO_ERROR - Success
  274. // Non-zero returns - Failure
  275. //
  276. // Description: Will initialize the entry point into DDM that it will call to
  277. // send DDM a PPP_MESSAGE
  278. //
  279. DWORD
  280. PppDdmInit(
  281. IN VOID (*SendPPPMessageToDdm)( PPP_MESSAGE * PppMsg ),
  282. IN DWORD dwServerFlags,
  283. IN DWORD dwLoggingLevel,
  284. IN DWORD dwNASIpAddress,
  285. IN BOOL fRadiusAuthentication,
  286. IN LPVOID lpfnRasAuthProviderAuthenticateUser,
  287. IN LPVOID lpfnRasAuthProviderFreeAttributes,
  288. IN LPVOID lpfnRasAcctProviderStartAccounting,
  289. IN LPVOID lpfnRasAcctProviderInterimAccounting,
  290. IN LPVOID lpfnRasAcctProviderStopAccounting,
  291. IN LPVOID lpfnRasAcctProviderFreeAttributes,
  292. IN LPVOID lpfnGetNextAccountingSessionId
  293. )
  294. {
  295. DWORD dwRetCode;
  296. PppConfigInfo.SendPPPMessageToDdm = SendPPPMessageToDdm;
  297. PppConfigInfo.ServerConfigInfo.dwConfigMask |= dwServerFlags;
  298. PppConfigInfo.dwNASIpAddress = dwNASIpAddress;
  299. PppConfigInfo.dwLoggingLevel = dwLoggingLevel;
  300. if ( dwNASIpAddress == 0 )
  301. {
  302. DWORD dwComputerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
  303. //
  304. // Failed to get the LOCAL IP address, used computer name instead.
  305. //
  306. PppConfigInfo.dwNASIpAddress = 0;
  307. if ( !GetComputerNameA( PppConfigInfo.szNASIdentifier,
  308. &dwComputerNameLen ) )
  309. {
  310. return( GetLastError() );
  311. }
  312. }
  313. PppConfigInfo.fRadiusAuthenticationUsed = fRadiusAuthentication;
  314. PppConfigInfo.RasAuthProviderFreeAttributes =
  315. (DWORD(*)( RAS_AUTH_ATTRIBUTE *))
  316. lpfnRasAuthProviderFreeAttributes;
  317. PppConfigInfo.RasAuthProviderAuthenticateUser =
  318. (DWORD(*)( RAS_AUTH_ATTRIBUTE * ,
  319. PRAS_AUTH_ATTRIBUTE* ,
  320. DWORD *))
  321. lpfnRasAuthProviderAuthenticateUser;
  322. PppConfigInfo.RasAcctProviderStartAccounting =
  323. (DWORD(*)( RAS_AUTH_ATTRIBUTE *,
  324. PRAS_AUTH_ATTRIBUTE*))
  325. lpfnRasAcctProviderStartAccounting;
  326. PppConfigInfo.RasAcctProviderInterimAccounting =
  327. (DWORD(*)( RAS_AUTH_ATTRIBUTE * ,
  328. PRAS_AUTH_ATTRIBUTE*))
  329. lpfnRasAcctProviderInterimAccounting;
  330. PppConfigInfo.RasAcctProviderStopAccounting =
  331. (DWORD(*)( RAS_AUTH_ATTRIBUTE * ,
  332. PRAS_AUTH_ATTRIBUTE*))
  333. lpfnRasAcctProviderStopAccounting;
  334. PppConfigInfo.RasAcctProviderFreeAttributes =
  335. (DWORD(*)( RAS_AUTH_ATTRIBUTE *))
  336. lpfnRasAcctProviderFreeAttributes;
  337. PppConfigInfo.GetNextAccountingSessionId =
  338. (DWORD(*)(VOID))
  339. lpfnGetNextAccountingSessionId;
  340. return( NO_ERROR );
  341. }
  342. //**
  343. //
  344. // Call: PppDdmDeInit
  345. //
  346. // Returns: NO_ERROR - Success
  347. // Non-zero returns - Failure
  348. //
  349. // Description:
  350. //
  351. VOID
  352. PppDdmDeInit(
  353. VOID
  354. )
  355. {
  356. return;
  357. }
  358. //**
  359. //
  360. // Call: PppDdmCallbackDone
  361. //
  362. // Returns: NO_ERROR - Success
  363. // Non-zero returns - Failure
  364. //
  365. // Description: Will create a PPPEMSG_DdmCallbackDone and send it to the
  366. // worker thread of the PPP engine.
  367. //
  368. DWORD
  369. PppDdmCallbackDone(
  370. IN HPORT hPort,
  371. IN WCHAR* pwszCallbackNumber
  372. )
  373. {
  374. PPPE_MESSAGE PppMessage;
  375. PppMessage.hPort = hPort;
  376. PppMessage.dwMsgId = PPPEMSG_DdmCallbackDone;
  377. wcstombs( PppMessage.ExtraInfo.CallbackDone.szCallbackNumber,
  378. pwszCallbackNumber,
  379. sizeof( PppMessage.ExtraInfo.CallbackDone.szCallbackNumber ) );
  380. return( SendPPPMessageToEngine( &PppMessage ) );
  381. }
  382. //**
  383. //
  384. // Call: PppDdmStart
  385. //
  386. // Returns: NO_ERROR - Success
  387. // Non-zero returns - Failure
  388. //
  389. // Description: Will create a PPPEMSG_DdmStart and send it to the
  390. // worker thread of the PPP engine.
  391. //
  392. DWORD
  393. PppDdmStart(
  394. IN HPORT hPort,
  395. IN WCHAR * wszPortName,
  396. IN CHAR* pchFirstFrame,
  397. IN DWORD cbFirstFrame,
  398. IN DWORD dwAuthRetries
  399. )
  400. {
  401. PPPE_MESSAGE PppMessage;
  402. PppMessage.hPort = hPort;
  403. PppMessage.dwMsgId = PPPEMSG_DdmStart;
  404. PppMessage.ExtraInfo.DdmStart.dwAuthRetries = dwAuthRetries;
  405. wcstombs( PppMessage.ExtraInfo.DdmStart.szPortName,
  406. wszPortName,
  407. sizeof( PppMessage.ExtraInfo.DdmStart.szPortName ) );
  408. CopyMemory( &(PppMessage.ExtraInfo.DdmStart.achFirstFrame),
  409. pchFirstFrame,
  410. sizeof( PppMessage.ExtraInfo.DdmStart.achFirstFrame ) );
  411. PppMessage.ExtraInfo.DdmStart.cbFirstFrame = cbFirstFrame;
  412. return( SendPPPMessageToEngine( &PppMessage ) );
  413. }
  414. //**
  415. //
  416. // Call: PppStop
  417. //
  418. // Returns: NO_ERROR - Success
  419. // Non-zero returns - Failure
  420. //
  421. // Description: Will create a PPPEMSG_Stop and send it to the
  422. // worker thread of the PPP engine.
  423. //
  424. DWORD
  425. PppStop(
  426. IN HPORT hPort
  427. )
  428. {
  429. PPPE_MESSAGE PppMessage;
  430. PppLog( 2, "PppStop\r\n" );
  431. PppMessage.hPort = hPort;
  432. PppMessage.dwMsgId = PPPEMSG_Stop;
  433. PppMessage.ExtraInfo.Stop.dwStopReason = NO_ERROR;
  434. return( SendPPPMessageToEngine( &PppMessage ) );
  435. }
  436. //**
  437. //
  438. // Call: PppDdmStop
  439. //
  440. // Returns: NO_ERROR - Success
  441. // Non-zero returns - Failure
  442. //
  443. // Description: Will create a PPPEMSG_Stop and send it to the
  444. // worker thread of the PPP engine.
  445. //
  446. DWORD
  447. PppDdmStop(
  448. IN HPORT hPort,
  449. IN DWORD dwStopReason
  450. )
  451. {
  452. PPPE_MESSAGE PppMessage;
  453. PppLog( 2, "PppDdmStop\r\n" );
  454. PppMessage.hPort = hPort;
  455. PppMessage.dwMsgId = PPPEMSG_Stop;
  456. PppMessage.ExtraInfo.Stop.dwStopReason = dwStopReason;
  457. return( SendPPPMessageToEngine( &PppMessage ) );
  458. }
  459. //**
  460. //
  461. // Call: PppDdmSendInterfaceInfo
  462. //
  463. // Returns: NO_ERROR - Success
  464. // Non-zero returns - Failure
  465. //
  466. // Description: Will create a PPPEMSG_DdmInterfaceInfo and send it to the
  467. // worker thread of the PPP engine.
  468. //
  469. DWORD
  470. PppDdmSendInterfaceInfo(
  471. IN HCONN hConnection,
  472. IN PPP_INTERFACE_INFO * pInterfaceInfo
  473. )
  474. {
  475. PPPE_MESSAGE PppMessage;
  476. PppMessage.hConnection = hConnection;
  477. PppMessage.dwMsgId = PPPEMSG_DdmInterfaceInfo;
  478. PppMessage.ExtraInfo.InterfaceInfo = *pInterfaceInfo;
  479. return( SendPPPMessageToEngine( &PppMessage ) );
  480. }
  481. //**
  482. //
  483. // Call: PppDdmBapCallbackResult
  484. //
  485. // Returns: NO_ERROR - Success
  486. // Non-zero returns - Failure
  487. //
  488. // Description: Will create a PPPEMSG_DdmBapCallbackResult and send it to the
  489. // worker thread of the PPP engine.
  490. //
  491. DWORD
  492. PppDdmBapCallbackResult(
  493. IN HCONN hConnection,
  494. IN DWORD dwBapCallbackResultCode
  495. )
  496. {
  497. PPPE_MESSAGE PppMessage;
  498. PppMessage.hConnection = hConnection;
  499. PppMessage.dwMsgId = PPPEMSG_DdmBapCallbackResult;
  500. PppMessage.ExtraInfo.BapCallbackResult.dwCallbackResultCode
  501. = dwBapCallbackResultCode;
  502. return( SendPPPMessageToEngine( &PppMessage ) );
  503. }
  504. //**
  505. //
  506. // Call: PppDdmChangeNotification
  507. //
  508. // Returns: NO_ERROR - Success
  509. // Non-zero returns - Failure
  510. //
  511. // Description:
  512. //
  513. DWORD
  514. PppDdmChangeNotification(
  515. IN DWORD dwServerFlags,
  516. IN DWORD dwLoggingLevel
  517. )
  518. {
  519. PPPE_MESSAGE PppMessage;
  520. PppLog( 2, "PppDdmChangeNotification. New flags: 0x%x", dwServerFlags );
  521. PppMessage.dwMsgId = PPPEMSG_DdmChangeNotification;
  522. PppConfigInfo.ServerConfigInfo.dwConfigMask = dwServerFlags;
  523. PppConfigInfo.dwLoggingLevel = dwLoggingLevel;
  524. return( SendPPPMessageToEngine( &PppMessage ) );
  525. }