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.

2266 lines
54 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. routing\ip\wanarp\ioctl.c
  5. Abstract:
  6. IOCTL handlers for wanarp
  7. Revision History:
  8. AmritanR
  9. --*/
  10. #define __FILE_SIG__ IOCTL_SIG
  11. #include "inc.h"
  12. NTSTATUS
  13. WanProcessNotification(
  14. PIRP pIrp,
  15. ULONG ulInLength,
  16. ULONG ulOutLength
  17. )
  18. /*++
  19. Routine Description:
  20. The handler for IOCTL_WANARP_NOTIFICATION. We see if we have some info
  21. we wish to return to the caller and if we do, we return it. Otherwise,
  22. we pend the IRP and use it later when we need to report an event to
  23. the user mode
  24. Locks:
  25. Acquires the IoCancelSpinLock
  26. Arguments:
  27. Return Value:
  28. STATUS_PENDING
  29. STATUS_SUCCESS
  30. STATUS_BUFFER_TOO_SMALL
  31. --*/
  32. {
  33. KIRQL kiIrql;
  34. PLIST_ENTRY pleNode;
  35. PVOID pvIoBuffer;
  36. PPENDING_NOTIFICATION pNotification;
  37. TraceEnter(CONN, "ProcessNotification");
  38. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  39. pIrp->IoStatus.Information = 0;
  40. if((ulInLength < sizeof(WANARP_NOTIFICATION)) or
  41. (ulOutLength < sizeof(WANARP_NOTIFICATION)))
  42. {
  43. return STATUS_BUFFER_TOO_SMALL;
  44. }
  45. //
  46. // use cancel spin lock to prevent irp being cancelled during this call.
  47. //
  48. IoAcquireCancelSpinLock(&kiIrql);
  49. //
  50. // If we have a pending notification then complete it - else
  51. // queue the notification IRP
  52. //
  53. if(!IsListEmpty(&g_lePendingNotificationList))
  54. {
  55. //
  56. // We have some old info
  57. //
  58. Trace(GLOBAL, TRACE,
  59. ("ProcNotification: Pending notification being completed\n"));
  60. //
  61. // Remove it off the pending list
  62. //
  63. pleNode = RemoveHeadList(&g_lePendingNotificationList);
  64. //
  65. // Get a pointer to the structure
  66. //
  67. pNotification = CONTAINING_RECORD(pleNode,
  68. PENDING_NOTIFICATION,
  69. leNotificationLink);
  70. //
  71. // Copy out the event to the user mode buffer
  72. //
  73. RtlCopyMemory(pIrp->AssociatedIrp.SystemBuffer,
  74. &pNotification->wnMsg,
  75. sizeof(WANARP_NOTIFICATION));
  76. //
  77. // Mark the IRP as non pending (and hence non cancelable)
  78. //
  79. IoSetCancelRoutine(pIrp,
  80. NULL);
  81. //
  82. // Fill the irp info
  83. //
  84. pIrp->IoStatus.Information = sizeof(WANARP_NOTIFICATION);
  85. IoReleaseCancelSpinLock(kiIrql);
  86. //
  87. // Free the allocated notification
  88. //
  89. FreeNotification(pNotification);
  90. return STATUS_SUCCESS;
  91. }
  92. Trace(GLOBAL, TRACE,
  93. ("ProcNotification: Notification being queued\n"));
  94. //
  95. // Queue this IRP to use for later
  96. //
  97. //
  98. // Mark the irp as pending
  99. //
  100. IoMarkIrpPending(pIrp);
  101. //
  102. // Queue up the irp at the end
  103. //
  104. InsertTailList(&g_lePendingIrpList,
  105. &(pIrp->Tail.Overlay.ListEntry));
  106. //
  107. // Set the cancel routine
  108. //
  109. IoSetCancelRoutine(pIrp,
  110. WanCancelNotificationIrp);
  111. IoReleaseCancelSpinLock(kiIrql);
  112. return STATUS_PENDING;
  113. }
  114. NTSTATUS
  115. WanAddUserModeInterface(
  116. PIRP pIrp,
  117. ULONG ulInLength,
  118. ULONG ulOutLength
  119. )
  120. /*++
  121. Routine Description:
  122. The handler for IOCTL_WANARP_ADD_INTERFACE.
  123. We walk our list of interface and make sure we dont have an interface
  124. with the same user mode index as the one we are being asked to create.
  125. If this is a new interface, we create a UMODE_INTERFACE structure
  126. and string it to the list. If it is the server interface then we also
  127. keep a special pointer to it
  128. Locks:
  129. Acquires the g_rwlIfLock as WRITER
  130. Arguments:
  131. Return Value:
  132. STATUS_SUCCESS
  133. STATUS_BUFFER_TOO_SMALL
  134. STATUS_OBJECT_NAME_EXISTS
  135. STATUS_OBJECT_NAME_NOT_FOUND
  136. STATUS_INSUFFICIENT_RESOURCES
  137. --*/
  138. {
  139. PVOID pvIoBuffer;
  140. PUMODE_INTERFACE pInterface;
  141. KIRQL kiIrql;
  142. NTSTATUS nStatus;
  143. PWANARP_ADD_INTERFACE_INFO pAddIfInfo;
  144. TraceEnter(ADPT, "AddUserModeInterface");
  145. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  146. pIrp->IoStatus.Information = 0;
  147. if(ulInLength < sizeof(WANARP_ADD_INTERFACE_INFO))
  148. {
  149. return STATUS_BUFFER_TOO_SMALL;
  150. }
  151. pAddIfInfo = (PWANARP_ADD_INTERFACE_INFO)pvIoBuffer;
  152. if(pAddIfInfo->bCallinInterface)
  153. {
  154. if(ulOutLength < sizeof(WANARP_ADD_INTERFACE_INFO))
  155. {
  156. return STATUS_BUFFER_TOO_SMALL;
  157. }
  158. RtlZeroMemory(pAddIfInfo->rgwcDeviceName,
  159. (WANARP_MAX_DEVICE_NAME_LEN + 2) * sizeof(WCHAR));
  160. EnterReader(&g_rwlAdapterLock,
  161. &kiIrql);
  162. if(g_pServerAdapter is NULL)
  163. {
  164. ExitReader(&g_rwlAdapterLock,
  165. kiIrql);
  166. Trace(ADPT, ERROR,
  167. ("AddUserModeInterface: No Server adapter\n"));
  168. return STATUS_OBJECT_NAME_NOT_FOUND;
  169. }
  170. //
  171. // Lock out the server adapter
  172. //
  173. RtAcquireSpinLockAtDpcLevel(&(g_pServerAdapter->rlLock));
  174. RtAssert(g_pServerInterface);
  175. RtAcquireSpinLockAtDpcLevel(&(g_pServerInterface->rlLock));
  176. ExitReaderFromDpcLevel(&g_rwlAdapterLock);
  177. if(pAddIfInfo->dwUserIfIndex isnot WANARP_INVALID_IFINDEX)
  178. {
  179. //
  180. // In this case all we need to do is set the interface index
  181. // for the server adapter. It is OK for this index to be INVALID
  182. // in that case, the user is merely asking for the server
  183. // adapter name
  184. //
  185. g_pServerInterface->dwIfIndex = pAddIfInfo->dwUserIfIndex;
  186. }
  187. //
  188. // We also need to return the name to the user
  189. //
  190. RtAssert(g_pServerAdapter->usDeviceNameW.Length <= WANARP_MAX_DEVICE_NAME_LEN * sizeof(WCHAR));
  191. RtlCopyMemory(pAddIfInfo->rgwcDeviceName,
  192. &(g_pServerAdapter->usDeviceNameW.Buffer[wcslen(TCPIP_IF_PREFIX) + 1]),
  193. g_pServerAdapter->usDeviceNameW.Length - ((wcslen(TCPIP_IF_PREFIX) + 1) * sizeof(WCHAR)));
  194. //
  195. // Also copy out the index
  196. //
  197. pAddIfInfo->dwAdapterIndex = g_pServerInterface->dwRsvdAdapterIndex;
  198. RtReleaseSpinLockFromDpcLevel(&(g_pServerInterface->rlLock));
  199. RtReleaseSpinLock(&(g_pServerAdapter->rlLock),
  200. kiIrql);
  201. //
  202. // We need to copy out info in this case
  203. //
  204. pIrp->IoStatus.Information = sizeof(WANARP_ADD_INTERFACE_INFO);
  205. return STATUS_SUCCESS;
  206. }
  207. EnterWriter(&g_rwlIfLock,
  208. &kiIrql);
  209. pInterface = WanpFindInterfaceGivenIndex(pAddIfInfo->dwUserIfIndex);
  210. if(pInterface isnot NULL)
  211. {
  212. //
  213. // Found an interface with the matching index. Not good
  214. //
  215. DereferenceInterface(pInterface);
  216. RtReleaseSpinLockFromDpcLevel(&(pInterface->rlLock));
  217. ExitWriter(&g_rwlIfLock,
  218. kiIrql);
  219. Trace(ADPT, ERROR,
  220. ("AddUserModeInterface: %d already exists\n",
  221. pAddIfInfo->dwUserIfIndex));
  222. return STATUS_OBJECT_NAME_EXISTS;
  223. }
  224. ExitWriter(&g_rwlIfLock,
  225. kiIrql);
  226. pInterface = RtAllocate(NonPagedPool,
  227. sizeof(UMODE_INTERFACE),
  228. WAN_INTERFACE_TAG);
  229. if(pInterface is NULL)
  230. {
  231. return STATUS_INSUFFICIENT_RESOURCES;
  232. }
  233. RtlZeroMemory(pInterface,
  234. sizeof(UMODE_INTERFACE));
  235. //
  236. // Reserve an Index with IP
  237. // This sets the value to invalid if it cant find an index
  238. //
  239. nStatus = WanpGetNewIndex(&(pInterface->dwRsvdAdapterIndex));
  240. if(nStatus isnot STATUS_SUCCESS)
  241. {
  242. RtFree(pInterface);
  243. return STATUS_INSUFFICIENT_RESOURCES;
  244. }
  245. Trace(ADPT, TRACE,
  246. ("AddUserModeInterface: for 0x%x - will use 0x%x\n",
  247. pAddIfInfo->dwUserIfIndex,
  248. pInterface->dwRsvdAdapterIndex));
  249. RtInitializeSpinLock(&(pInterface->rlLock));
  250. //
  251. // Initialize the interface
  252. //
  253. pInterface->dwIfIndex = pAddIfInfo->dwUserIfIndex;
  254. pInterface->dwAdminState = IF_ADMIN_STATUS_UP;
  255. pInterface->dwOperState = IF_OPER_STATUS_DISCONNECTED;
  256. pInterface->dwLastChange = GetTimeTicks();
  257. pAddIfInfo->dwAdapterIndex = pInterface->dwRsvdAdapterIndex;
  258. //
  259. // Now set the refcount to 1 to account for the fact that the interface
  260. // will be put on the g_leIfList.
  261. //
  262. InitInterfaceRefCount(pInterface);
  263. pInterface->duUsage = DU_ROUTER;
  264. EnterWriter(&g_rwlIfLock,
  265. &kiIrql);
  266. InsertHeadList(&g_leIfList,
  267. &(pInterface->leIfLink));
  268. ExitWriter(&g_rwlIfLock,
  269. kiIrql);
  270. pIrp->IoStatus.Information = sizeof(WANARP_ADD_INTERFACE_INFO);
  271. return STATUS_SUCCESS;
  272. }
  273. NTSTATUS
  274. WanDeleteUserModeInterface(
  275. PIRP pIrp,
  276. ULONG ulInLength,
  277. ULONG ulOutLength
  278. )
  279. /*++
  280. Routine Description:
  281. Handler for IOCTL_WANARP_DELETE_INTERFACE.
  282. We lookup our list to see if we have the interface. If we do, then
  283. we remove the interface from the g_leIfList and dereference it.
  284. If the interface was not mapped, then this should be the last
  285. reference on the interface, otherwise when the refcount goes to 0, it
  286. will get deleted.
  287. Locks:
  288. Acquires the g_rwlIfLock as WRITER and then calls FindInterface
  289. which locks the interface in question
  290. Arguments:
  291. Return Value:
  292. STATUS_BUFFER_TOO_SMALL
  293. STATUS_OBJECT_NAME_NOT_FOUND
  294. STATUS_SUCCESS
  295. --*/
  296. {
  297. KIRQL kiIrql;
  298. PUMODE_INTERFACE pInterface;
  299. PVOID pvIoBuffer;
  300. PWANARP_DELETE_INTERFACE_INFO pDeleteInfo;
  301. TraceEnter(ADPT, "DeleteUserModeInterface");
  302. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  303. pIrp->IoStatus.Information = 0;
  304. if(ulInLength < sizeof(WANARP_DELETE_INTERFACE_INFO))
  305. {
  306. return STATUS_BUFFER_TOO_SMALL;
  307. }
  308. pDeleteInfo = (PWANARP_DELETE_INTERFACE_INFO)pvIoBuffer;
  309. //
  310. // Cant service binds or unbinds here
  311. //
  312. WanpAcquireResource(&g_wrBindMutex);
  313. EnterWriter(&g_rwlIfLock,
  314. &kiIrql);
  315. //
  316. // Find the interface for the index
  317. //
  318. pInterface = WanpFindInterfaceGivenIndex(pDeleteInfo->dwUserIfIndex);
  319. //
  320. // If the interface is not found, bug out
  321. //
  322. Trace(ADPT, TRACE,
  323. ("DeleteUserModeInterface: Deleting i/f 0x%x\n",
  324. pDeleteInfo->dwUserIfIndex));
  325. if(pInterface is NULL)
  326. {
  327. ExitWriter(&g_rwlIfLock,
  328. kiIrql);
  329. WanpReleaseResource(&g_wrBindMutex);
  330. Trace(ADPT, ERROR,
  331. ("DeleteUserModeInterface: Couldnt find i/f 0x%x\n",
  332. pDeleteInfo->dwUserIfIndex));
  333. return STATUS_OBJECT_NAME_NOT_FOUND;
  334. }
  335. RemoveEntryList(&(pInterface->leIfLink));
  336. ExitWriterFromDpcLevel(&g_rwlIfLock);
  337. //
  338. // If found, the interface is locked
  339. // So dereference it and remove it from the list. The interface may
  340. // not get deleted here because it is already mapped and has a connection
  341. // active on it.
  342. //
  343. if(pInterface->dwOperState >= IF_OPER_STATUS_CONNECTING)
  344. {
  345. Trace(ADPT, ERROR,
  346. ("DeleteUserModeInterface: I/f %d is in state %d\n",
  347. pInterface->dwIfIndex,
  348. pInterface->dwOperState));
  349. }
  350. RtReleaseSpinLock(&(pInterface->rlLock),
  351. kiIrql);
  352. //
  353. // Dereference the interface twice. Once because we put a ref on it
  354. // when we called FindInterface... and once because we removed it
  355. // from the if list
  356. //
  357. DereferenceInterface(pInterface);
  358. DereferenceInterface(pInterface);
  359. WanpReleaseResource(&g_wrBindMutex);
  360. return STATUS_SUCCESS;
  361. }
  362. VOID
  363. WanpCleanOutInterfaces(
  364. VOID
  365. )
  366. /*++
  367. Routine Description:
  368. Called to delete all the interface from the system.
  369. We remove the interface from the g_leIfList and dereference it.
  370. For the server interface we simply mark it as disconnected
  371. Locks:
  372. Acquires the g_rwlIfLock as WRITER
  373. Arguments:
  374. Return Value:
  375. None
  376. --*/
  377. {
  378. KIRQL kiIrql;
  379. PUMODE_INTERFACE pInterface;
  380. TraceEnter(ADPT, "CleanOutInterfaces");
  381. //
  382. // Cant service binds or unbinds here
  383. //
  384. WanpAcquireResource(&g_wrBindMutex);
  385. EnterWriter(&g_rwlAdapterLock,
  386. &kiIrql);
  387. EnterWriterAtDpcLevel(&g_rwlIfLock);
  388. while(!IsListEmpty(&g_leIfList))
  389. {
  390. PLIST_ENTRY pleNode;
  391. pleNode = RemoveHeadList(&g_leIfList);
  392. pInterface = CONTAINING_RECORD(pleNode,
  393. UMODE_INTERFACE,
  394. leIfLink);
  395. if(pInterface->dwOperState >= IF_OPER_STATUS_CONNECTING)
  396. {
  397. Trace(ADPT, ERROR,
  398. ("CleanOutInterfaces: I/f %d is in state %d\n",
  399. pInterface->dwIfIndex,
  400. pInterface->dwOperState));
  401. }
  402. DereferenceInterface(pInterface);
  403. }
  404. //
  405. // What should we do with the server interface?
  406. //
  407. ExitWriterFromDpcLevel(&g_rwlIfLock);
  408. ExitWriter(&g_rwlAdapterLock,
  409. kiIrql);
  410. WanpReleaseResource(&g_wrBindMutex);
  411. return;
  412. }
  413. VOID
  414. WanpDeleteInterface(
  415. PUMODE_INTERFACE pInterface
  416. )
  417. /*++
  418. Routine Description
  419. Called by DereferenceInterface() when the refcount on an interface
  420. falls to 0
  421. Locks
  422. The interface is neither locked nor refcounted. Since there are no
  423. stored pointers to the interface, this structure can not be accessed
  424. by anyone but this function
  425. Arguments
  426. pInterface The interface to delete
  427. Return Value
  428. None
  429. --*/
  430. {
  431. PADAPTER pAdapter;
  432. if(pInterface is g_pServerInterface)
  433. {
  434. //
  435. // If this is the server interface, make sure that all
  436. // connection entries are gone
  437. //
  438. }
  439. //
  440. // There should be no interface mapped to it because otherwise we wouldnt
  441. // be down to a refcount of 0
  442. //
  443. RtAssert(pInterface->dwRsvdAdapterIndex isnot INVALID_IF_INDEX);
  444. RtAssert(pInterface->dwRsvdAdapterIndex isnot 0);
  445. WanpFreeIndex(pInterface->dwRsvdAdapterIndex);
  446. RtAssert(pInterface->pAdapter is NULL);
  447. RtFree(pInterface);
  448. }
  449. NTSTATUS
  450. WanProcessConnectionFailure(
  451. PIRP pIrp,
  452. ULONG ulInLength,
  453. ULONG ulOutLength
  454. )
  455. /*++
  456. Routine Description:
  457. Handler for IOCTL_WANARP_CONNECT_FAILED
  458. We find the interface that failed to connect. If we do find one, we
  459. mark its state as disconnected and then see if it was mapped to an
  460. adapter (as it should be). If we find an adapter, we do funky stuff
  461. with lock orders, clean out the packets queued to the adapter and
  462. unmap the adapter.
  463. Locks:
  464. One of the more complex functions.
  465. We take the g_rwlIfLock to get to the interface and lock it.
  466. Then we get a pointer to the adapter, and ref it. We release the
  467. interface lock and lock the adapter.
  468. Arguments:
  469. Return Value:
  470. STATUS_BUFFER_TOO_SMALL
  471. STATUS_OBJECT_NAME_NOT_FOUND
  472. STATUS_INVALID_PARAMETER
  473. STATUS_INVALID_DEVICE_STATE
  474. STATUS_SUCCESS
  475. --*/
  476. {
  477. PNDIS_PACKET packet;
  478. PADAPTER pAdapter;
  479. PUMODE_INTERFACE pInterface;
  480. DWORD dwIfIndex;
  481. PVOID pvIoBuffer;
  482. KIRQL kiIrql;
  483. TraceEnter(CONN, "ProcessConnectionFailure");
  484. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  485. pIrp->IoStatus.Information = 0;
  486. if(ulInLength < sizeof(WANARP_CONNECT_FAILED_INFO))
  487. {
  488. return STATUS_BUFFER_TOO_SMALL;
  489. }
  490. dwIfIndex = ((PWANARP_CONNECT_FAILED_INFO)pvIoBuffer)->dwUserIfIndex;
  491. pIrp->IoStatus.Information = 0;
  492. //
  493. // Find the interface
  494. //
  495. EnterReader(&g_rwlIfLock,
  496. &kiIrql);
  497. pInterface = WanpFindInterfaceGivenIndex(dwIfIndex);
  498. if(pInterface is NULL)
  499. {
  500. ExitReader(&g_rwlIfLock,
  501. kiIrql);
  502. Trace(CONN, ERROR,
  503. ("ProcessConnectionFailure: Couldnt find i/f for index %d\n",
  504. dwIfIndex));
  505. return STATUS_OBJECT_NAME_NOT_FOUND;
  506. }
  507. if(pInterface is g_pServerInterface)
  508. {
  509. //
  510. // Cant get a disconnect on this
  511. //
  512. RtReleaseSpinLockFromDpcLevel(&(pInterface->rlLock));
  513. DereferenceInterface(pInterface);
  514. ExitReader(&g_rwlIfLock,
  515. kiIrql);
  516. Trace(CONN, ERROR,
  517. ("ProcessConnectionFailure: disconnect on server i/f (%d)n",
  518. dwIfIndex));
  519. return STATUS_INVALID_PARAMETER;
  520. }
  521. ExitReaderFromDpcLevel(&g_rwlIfLock);
  522. RtAssert(pInterface->dwIfIndex is dwIfIndex);
  523. //
  524. // So now the interface is locked.
  525. //
  526. Trace(CONN, TRACE,
  527. ("ProcessConnectionFailure for %d %p\n", pInterface->dwIfIndex, pInterface));
  528. if(pInterface->dwOperState isnot IF_OPER_STATUS_CONNECTING)
  529. {
  530. Trace(CONN, ERROR,
  531. ("ProcessConnectionFailure: %p is in state %d\n",
  532. pInterface,
  533. pInterface->dwOperState));
  534. RtReleaseSpinLock(&(pInterface->rlLock),
  535. kiIrql);
  536. DereferenceInterface(pInterface);
  537. return STATUS_INVALID_DEVICE_STATE;
  538. }
  539. pAdapter = pInterface->pAdapter;
  540. if(pAdapter is NULL)
  541. {
  542. //
  543. // This is the case where we couldnt find an adapter added to IP
  544. // to make a DOD connection
  545. // We dont need to do too much here, just release the interface
  546. // lock and remove ref that was put by FindInterface()
  547. //
  548. RtAssert(pInterface->dwOperState is IF_OPER_STATUS_CONNECTING);
  549. pInterface->dwOperState = IF_OPER_STATUS_DISCONNECTED;
  550. pInterface->dwLastChange= GetTimeTicks();
  551. RtAssert(pInterface->ulPacketsPending is 0);
  552. RtReleaseSpinLock(&(pInterface->rlLock),
  553. kiIrql);
  554. DereferenceInterface(pInterface);
  555. return STATUS_SUCCESS;
  556. }
  557. //
  558. // We should never get a connection failure if have gotten a LinkUp
  559. //
  560. RtAssert(pAdapter->pConnEntry is NULL);
  561. //
  562. // If we do have an adapter then it can not go away because the
  563. // interface has a refcount on it (i.e when we set the pAdapter field
  564. // in the interface, we refcounted the adapter because we had a stored
  565. // pointer to it)
  566. //
  567. RtAssert(pInterface->dwOperState is IF_OPER_STATUS_CONNECTING);
  568. //
  569. // If the interface is still mapped, unmap it and drain any packets we
  570. // may have queued
  571. //
  572. pInterface->ulPacketsPending = 0;
  573. pInterface->dwOperState = IF_OPER_STATUS_DISCONNECTED;
  574. pInterface->dwLastChange= GetTimeTicks();
  575. //
  576. // The adapter can not go away because the interface has a refcount on
  577. // it (i.e when we set the pAdapter field in the interface, we
  578. // refcounted the adapter because we had a stored pointer to it)
  579. //
  580. pAdapter = pInterface->pAdapter;
  581. RtAssert(pAdapter);
  582. //
  583. // Clear out the adapter field, BUT DONT DEREF the adapter
  584. //
  585. pInterface->pAdapter = NULL;
  586. //
  587. // So we are done with the interface. We now go and clean out the
  588. // adapter. To do that we need to acquire the adapter lock. However we
  589. // can not do that since we have the interface lock. So we first
  590. // reference the adapter (so that it will be around). Then we
  591. // let go of the interface lock. (The interface can not go away since
  592. // we put a refcount on the it when we called FindInterface). After
  593. // which we can acquire the adapter lock
  594. //
  595. ReferenceAdapter(pAdapter);
  596. RtReleaseSpinLockFromDpcLevel(&(pInterface->rlLock));
  597. //
  598. // The adapter has to be around, because of the refcount
  599. //
  600. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  601. //
  602. // Make sure that the adapter still thinks that it is mapped to the
  603. // interface in question
  604. //
  605. if(pAdapter->pInterface is pInterface)
  606. {
  607. RtAssert(pAdapter->byState is AS_MAPPED);
  608. //
  609. // Drain all the packets
  610. //
  611. Trace(CONN, TRACE,
  612. ("ProcsConnFailure: Draining and freeing any queued packets\n"));
  613. while(!IsListEmpty(&(pAdapter->lePendingPktList)))
  614. {
  615. PLIST_ENTRY pleNode;
  616. PNDIS_PACKET pnpPacket;
  617. pleNode = RemoveHeadList(&(pAdapter->lePendingPktList));
  618. //
  619. // get to the packet structure in which LIST_ENTRY is embedded
  620. //
  621. pnpPacket = CONTAINING_RECORD(pleNode,
  622. NDIS_PACKET,
  623. MacReserved);
  624. WanpFreePacketAndBuffers(pnpPacket);
  625. }
  626. if(!IsListEmpty(&(pAdapter->lePendingHdrList)))
  627. {
  628. LIST_ENTRY leTempList;
  629. leTempList = pAdapter->lePendingHdrList;
  630. pAdapter->lePendingHdrList.Flink->Blink = &leTempList;
  631. pAdapter->lePendingHdrList.Blink->Flink = &leTempList;
  632. InitializeListHead(&(pAdapter->lePendingHdrList));
  633. FreeBufferListToPool(&g_bpHeaderBufferPool,
  634. &leTempList);
  635. }
  636. pAdapter->pInterface = NULL;
  637. //
  638. // Deref the interface because we are clearing out a stored pointer
  639. //
  640. DereferenceInterface(pInterface);
  641. //
  642. // Deref the adapter now (due to the fact that we cleared out
  643. // the pAdapter field in pInterface
  644. //
  645. DereferenceAdapter(pAdapter);
  646. }
  647. //
  648. // Done with the adapter
  649. //
  650. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  651. WanpUnmapAdapter(pAdapter);
  652. KeLowerIrql(kiIrql);
  653. //
  654. // Remove ref that was put by FindInterface()
  655. //
  656. DereferenceInterface(pInterface);
  657. //
  658. // Remove the ref that was put when we let go of the interface lock
  659. //
  660. DereferenceAdapter(pAdapter);
  661. return STATUS_SUCCESS;
  662. }
  663. NTSTATUS
  664. WanGetIfStats(
  665. PIRP pIrp,
  666. ULONG ulInLength,
  667. ULONG ulOutLength
  668. )
  669. /*++
  670. Routine Description:
  671. Locks:
  672. Arguments:
  673. Return Value:
  674. NO_ERROR
  675. --*/
  676. {
  677. PVOID pvIoBuffer;
  678. PWANARP_GET_IF_STATS_INFO pInfo;
  679. KIRQL kiIrql;
  680. PUMODE_INTERFACE pInterface;
  681. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  682. pIrp->IoStatus.Information = 0;
  683. if((ulOutLength < (FIELD_OFFSET(WANARP_GET_IF_STATS_INFO, ifeInfo)
  684. + IFE_FIXED_SIZE))or
  685. (ulInLength < (FIELD_OFFSET(WANARP_GET_IF_STATS_INFO, ifeInfo)
  686. + IFE_FIXED_SIZE)))
  687. {
  688. return STATUS_BUFFER_TOO_SMALL;
  689. }
  690. EnterReader(&g_rwlIfLock,
  691. &kiIrql);
  692. pInfo = (PWANARP_GET_IF_STATS_INFO)pvIoBuffer;
  693. pInterface = WanpFindInterfaceGivenIndex(pInfo->dwUserIfIndex);
  694. if(pInterface is NULL)
  695. {
  696. ExitReader(&g_rwlIfLock,
  697. kiIrql);
  698. return STATUS_OBJECT_NAME_NOT_FOUND;
  699. }
  700. ExitReaderFromDpcLevel(&g_rwlIfLock);
  701. //
  702. // We dont take the adapter lock, because the adapter can not
  703. // go away while the interface is mapped to it.
  704. // Sure the qlen can be inconsistent, but hey
  705. //
  706. if((pInterface->pAdapter) and
  707. (pInterface->pAdapter->pConnEntry))
  708. {
  709. pInfo->ifeInfo.if_index = pInterface->pAdapter->dwAdapterIndex;
  710. pInfo->ifeInfo.if_outqlen = pInterface->pAdapter->ulQueueLen;
  711. pInfo->ifeInfo.if_mtu = pInterface->pAdapter->pConnEntry->ulMtu;
  712. pInfo->ifeInfo.if_speed = pInterface->pAdapter->pConnEntry->ulSpeed;
  713. }
  714. else
  715. {
  716. pInfo->ifeInfo.if_index = (uint)-1;
  717. pInfo->ifeInfo.if_outqlen = 0;
  718. pInfo->ifeInfo.if_mtu = WANARP_DEFAULT_MTU;
  719. pInfo->ifeInfo.if_speed = WANARP_DEFAULT_SPEED;
  720. }
  721. pInfo->ifeInfo.if_adminstatus = pInterface->dwAdminState;
  722. pInfo->ifeInfo.if_operstatus = pInterface->dwOperState;
  723. pInfo->ifeInfo.if_lastchange = pInterface->dwLastChange;
  724. pInfo->ifeInfo.if_inoctets = pInterface->ulInOctets;
  725. pInfo->ifeInfo.if_inucastpkts = pInterface->ulInUniPkts;
  726. pInfo->ifeInfo.if_innucastpkts = pInterface->ulInNonUniPkts;
  727. pInfo->ifeInfo.if_indiscards = pInterface->ulInDiscards;
  728. pInfo->ifeInfo.if_inerrors = pInterface->ulInErrors;
  729. pInfo->ifeInfo.if_inunknownprotos = pInterface->ulInUnknownProto;
  730. pInfo->ifeInfo.if_outoctets = pInterface->ulOutOctets;
  731. pInfo->ifeInfo.if_outucastpkts = pInterface->ulOutUniPkts;
  732. pInfo->ifeInfo.if_outnucastpkts = pInterface->ulOutNonUniPkts;
  733. pInfo->ifeInfo.if_outdiscards = pInterface->ulOutDiscards;
  734. pInfo->ifeInfo.if_outerrors = pInterface->ulOutErrors;
  735. RtReleaseSpinLock(&(pInterface->rlLock),
  736. kiIrql);
  737. DereferenceInterface(pInterface);
  738. pInfo->ifeInfo.if_type = IF_TYPE_PPP;
  739. pInfo->ifeInfo.if_physaddrlen = ARP_802_ADDR_LENGTH;
  740. pInfo->ifeInfo.if_descrlen = 0;
  741. RtlZeroMemory(pInfo->ifeInfo.if_physaddr,
  742. MAX_PHYSADDR_SIZE);
  743. pIrp->IoStatus.Information =
  744. FIELD_OFFSET(WANARP_GET_IF_STATS_INFO, ifeInfo) + IFE_FIXED_SIZE;
  745. return STATUS_SUCCESS;
  746. }
  747. NTSTATUS
  748. WanDeleteAdapters(
  749. PIRP pIrp,
  750. ULONG ulInLength,
  751. ULONG ulOutLength
  752. )
  753. /*++
  754. Routine Description:
  755. Handler for IOCTL_WANARP_DELETE_ADAPTERS.
  756. The caller indicates to us the number of adapters that she wants
  757. removed. If we have that many free adapters, we remove them and
  758. return the names of the devices, removed.
  759. Locks:
  760. Acquires the g_rwlAdaptersLock as WRITER
  761. Arguments:
  762. Return Value:
  763. STATUS_BUFFER_TOO_SMALL
  764. STATUS_SUCCESS
  765. --*/
  766. {
  767. KIRQL kiIrql;
  768. PADAPTER pAdapter;
  769. PVOID pvIoBuffer;
  770. ULONG i;
  771. PLIST_ENTRY pleNode;
  772. LIST_ENTRY leTempHead;
  773. PVOID pvNameBuffer;
  774. NTSTATUS nStatus;
  775. KEVENT keChangeEvent;
  776. PWANARP_DELETE_ADAPTERS_INFO pDeleteInfo;
  777. TraceEnter(ADPT, "DeleteAdapters");
  778. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  779. pIrp->IoStatus.Information = 0;
  780. if(ulInLength < sizeof(WANARP_DELETE_ADAPTERS_INFO))
  781. {
  782. return STATUS_BUFFER_TOO_SMALL;
  783. }
  784. pDeleteInfo = (PWANARP_DELETE_ADAPTERS_INFO)pvIoBuffer;
  785. //
  786. // Dont service binds or unbinds here
  787. //
  788. WanpAcquireResource(&g_wrBindMutex);
  789. EnterWriter(&g_rwlAdapterLock,
  790. &kiIrql);
  791. if(pDeleteInfo->ulNumAdapters > g_ulNumFreeAdapters + g_ulNumAddedAdapters)
  792. {
  793. //
  794. // Asking to delete more adapters than are present
  795. //
  796. pIrp->IoStatus.Information = g_ulNumFreeAdapters + g_ulNumAddedAdapters;
  797. ExitWriter(&g_rwlAdapterLock,
  798. kiIrql);
  799. WanpReleaseResource(&g_wrBindMutex);
  800. return STATUS_INSUFFICIENT_RESOURCES;
  801. }
  802. //
  803. // So there are enough unmapped adapters. See if we have enough space to
  804. // return the names of the adapters
  805. //
  806. if(ulOutLength <
  807. FIELD_OFFSET(WANARP_DELETE_ADAPTERS_INFO, rgAdapterGuid[0]) +
  808. (pDeleteInfo->ulNumAdapters * sizeof(GUID)))
  809. {
  810. //
  811. // Not enough space to hold the names
  812. //
  813. ExitWriter(&g_rwlAdapterLock,
  814. kiIrql);
  815. WanpReleaseResource(&g_wrBindMutex);
  816. pIrp->IoStatus.Information =
  817. FIELD_OFFSET(WANARP_DELETE_ADAPTERS_INFO, rgAdapterGuid[0]) +
  818. (pDeleteInfo->ulNumAdapters * sizeof(GUID));
  819. return STATUS_BUFFER_TOO_SMALL;
  820. }
  821. pIrp->IoStatus.Information =
  822. FIELD_OFFSET(WANARP_DELETE_ADAPTERS_INFO, rgAdapterGuid[0]) +
  823. (pDeleteInfo->ulNumAdapters * sizeof(GUID));
  824. //
  825. // Everything's good. First see if we can remove the ones we want
  826. // removed from the free list
  827. //
  828. i = 0;
  829. while((i < pDeleteInfo->ulNumAdapters) and
  830. (!IsListEmpty(&g_leFreeAdapterList)))
  831. {
  832. pleNode = RemoveHeadList(&g_leFreeAdapterList);
  833. g_ulNumFreeAdapters--;
  834. pAdapter = CONTAINING_RECORD(pleNode,
  835. ADAPTER,
  836. leAdapterLink);
  837. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  838. RtAssert(pAdapter->byState is AS_FREE);
  839. RtAssert(pAdapter->pInterface is NULL);
  840. //
  841. // Copy out the name
  842. // TCPIP_IF_PREFIX is \Device and we need to remove \Device\
  843. //
  844. ConvertStringToGuid(
  845. &(pAdapter->usDeviceNameW.Buffer[wcslen(TCPIP_IF_PREFIX) + 1]),
  846. pAdapter->usDeviceNameW.Length - ((wcslen(TCPIP_IF_PREFIX) + 1) * sizeof(WCHAR)),
  847. &(pDeleteInfo->rgAdapterGuid[i])
  848. );
  849. i++;
  850. //
  851. // Deref it for removing it from the list. This should delete
  852. // it
  853. //
  854. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  855. DereferenceAdapter(pAdapter);
  856. }
  857. if(i is pDeleteInfo->ulNumAdapters)
  858. {
  859. //
  860. // We are done
  861. //
  862. ExitWriter(&g_rwlAdapterLock,
  863. kiIrql);
  864. WanpReleaseResource(&g_wrBindMutex);
  865. return STATUS_SUCCESS;
  866. }
  867. //
  868. // Need to get some added adapters deleted, too
  869. //
  870. InitializeListHead(&leTempHead);
  871. while((i < pDeleteInfo->ulNumAdapters) and
  872. (!IsListEmpty(&g_leAddedAdapterList)))
  873. {
  874. pleNode = RemoveHeadList(&g_leAddedAdapterList);
  875. g_ulNumAddedAdapters--;
  876. pAdapter = CONTAINING_RECORD(pleNode,
  877. ADAPTER,
  878. leAdapterLink);
  879. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  880. pAdapter->byState = AS_REMOVING;
  881. InsertHeadList(&leTempHead,
  882. &(pAdapter->leAdapterLink));
  883. //
  884. // Copy out the name
  885. //
  886. ConvertStringToGuid(
  887. &(pAdapter->usDeviceNameW.Buffer[wcslen(TCPIP_IF_PREFIX) + 1]),
  888. pAdapter->usDeviceNameW.Length - ((wcslen(TCPIP_IF_PREFIX) + 1) * sizeof(WCHAR)),
  889. &(pDeleteInfo->rgAdapterGuid[i])
  890. );
  891. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  892. i++;
  893. }
  894. //
  895. // We better have enough adapters
  896. //
  897. RtAssert(i is pDeleteInfo->ulNumAdapters);
  898. //
  899. // Now we can let go of the lock
  900. //
  901. ExitWriter(&g_rwlAdapterLock,
  902. kiIrql);
  903. KeInitializeEvent(&keChangeEvent,
  904. SynchronizationEvent,
  905. FALSE);
  906. //
  907. // Loop through and delete the adapters
  908. //
  909. while(!IsListEmpty(&leTempHead))
  910. {
  911. pleNode = RemoveHeadList(&leTempHead);
  912. pAdapter = CONTAINING_RECORD(pleNode,
  913. ADAPTER,
  914. leAdapterLink);
  915. EnterWriter(&g_rwlAdapterLock,
  916. &kiIrql);
  917. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  918. //
  919. // Insert it into the change list
  920. //
  921. InsertHeadList(&g_leChangeAdapterList,
  922. &(pAdapter->leAdapterLink));
  923. //
  924. // Set the event to block on
  925. //
  926. RtAssert(pAdapter->pkeChangeEvent is NULL);
  927. pAdapter->pkeChangeEvent = &keChangeEvent;
  928. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  929. ExitWriter(&g_rwlAdapterLock,
  930. kiIrql);
  931. g_pfnIpDeleteInterface(pAdapter->pvIpContext,
  932. FALSE);
  933. //
  934. // Wait till the CloseAdapter completes
  935. //
  936. nStatus = KeWaitForSingleObject(&keChangeEvent,
  937. Executive,
  938. KernelMode,
  939. FALSE,
  940. NULL);
  941. //
  942. // Remove from the change list
  943. //
  944. EnterWriter(&g_rwlAdapterLock,
  945. &kiIrql);
  946. #if DBG
  947. RtAssert(IsEntryOnList(&g_leChangeAdapterList,
  948. &(pAdapter->leAdapterLink)));
  949. #endif
  950. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  951. RemoveEntryList(&(pAdapter->leAdapterLink));
  952. pAdapter->byState = 0xFF;
  953. pAdapter->pkeChangeEvent = NULL;
  954. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  955. ExitWriter(&g_rwlAdapterLock,
  956. kiIrql);
  957. //
  958. // Dereference the adapter for removing from the list
  959. // (CloseAdapter will deref it for removing from IP)
  960. //
  961. DereferenceAdapter(pAdapter);
  962. }
  963. WanpReleaseResource(&g_wrBindMutex);
  964. return STATUS_SUCCESS;
  965. }
  966. NTSTATUS
  967. WanMapServerAdapter(
  968. PIRP pIrp,
  969. ULONG ulInLength,
  970. ULONG ulOutLength
  971. )
  972. /*++
  973. Routine Description:
  974. Called by RAS to add the server adapter and map it to an interface.
  975. It has to be done before the first client dials in.
  976. Locks:
  977. Acquires the g_wrBindMutex. Also acquires the adapter list lock and the
  978. adapter lock
  979. Arguments:
  980. Return Value:
  981. STATUS_SUCCESS
  982. STATUS_BUFFER_TOO_SMALL
  983. STATUS_NO_SUCH_DEVICE
  984. STATUS_DIRECTORY_NOT_EMPTY
  985. --*/
  986. {
  987. NTSTATUS nStatus;
  988. PVOID pvIoBuffer;
  989. KIRQL kiIrql;
  990. WCHAR rgwcGuid[GUID_STR_LEN + 1];
  991. PLIST_ENTRY pleNode;
  992. PADAPTER pTempAdapter;
  993. KEVENT keTempEvent;
  994. UNICODE_STRING usTempName;
  995. PUMODE_INTERFACE pInterface;
  996. PWANARP_MAP_SERVER_ADAPTER_INFO pInfo;
  997. TraceEnter(ADPT, "MapServerAdapter");
  998. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  999. pInfo = (PWANARP_MAP_SERVER_ADAPTER_INFO)pvIoBuffer;
  1000. pIrp->IoStatus.Information = 0;
  1001. if((ulOutLength < sizeof(WANARP_MAP_SERVER_ADAPTER_INFO)) or
  1002. (ulInLength < sizeof(WANARP_MAP_SERVER_ADAPTER_INFO)))
  1003. {
  1004. return STATUS_BUFFER_TOO_SMALL;
  1005. }
  1006. WanpAcquireResource(&g_wrBindMutex);
  1007. EnterWriter(&g_rwlAdapterLock,
  1008. &kiIrql);
  1009. if(g_pServerAdapter is NULL)
  1010. {
  1011. Trace(ADPT, ERROR,
  1012. ("MapServerAdapter: No server adapter\n"));
  1013. ExitWriter(&g_rwlAdapterLock,
  1014. kiIrql);
  1015. WanpReleaseResource(&g_wrBindMutex);
  1016. return STATUS_NO_SUCH_DEVICE;
  1017. }
  1018. RtAssert(g_pServerInterface);
  1019. //
  1020. // Lock the adapter and change the state to let people know we are
  1021. // trying to add or remove the adapter, hence they should wait on the
  1022. // global event
  1023. //
  1024. RtAcquireSpinLockAtDpcLevel(&(g_pServerAdapter->rlLock));
  1025. if(pInfo->fMap is 0)
  1026. {
  1027. //
  1028. // Trying to unmap
  1029. //
  1030. pIrp->IoStatus.Information = 0;
  1031. if(g_pServerAdapter->byState is AS_FREE)
  1032. {
  1033. //
  1034. // Nothing to do
  1035. //
  1036. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1037. ExitWriter(&g_rwlAdapterLock,
  1038. kiIrql);
  1039. WanpReleaseResource(&g_wrBindMutex);
  1040. return STATUS_SUCCESS;
  1041. }
  1042. //
  1043. // Since add is serialized, the only other state is AS_MAPPED
  1044. //
  1045. RtAssert(g_pServerAdapter->byState is AS_MAPPED);
  1046. //
  1047. // Make sure there are no new connections
  1048. //
  1049. if(!WanpIsConnectionTableEmpty())
  1050. {
  1051. Trace(ADPT, ERROR,
  1052. ("MapServerAdapter: Connection Table not empty\n"));
  1053. // RtAssert(FALSE);
  1054. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1055. ExitWriter(&g_rwlAdapterLock,
  1056. kiIrql);
  1057. WanpReleaseResource(&g_wrBindMutex);
  1058. return STATUS_DIRECTORY_NOT_EMPTY;
  1059. }
  1060. //
  1061. // Remove the adapter from ip, remove the cross ref
  1062. //
  1063. //
  1064. // Since we are changing the state, no one else should be also
  1065. // changing the state
  1066. //
  1067. RtAssert(g_pServerAdapter->pkeChangeEvent is NULL);
  1068. KeInitializeEvent(&keTempEvent,
  1069. SynchronizationEvent,
  1070. FALSE);
  1071. g_pServerAdapter->pkeChangeEvent = &keTempEvent;
  1072. ReferenceAdapter(g_pServerAdapter);
  1073. g_pServerAdapter->byState = AS_REMOVING;
  1074. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1075. ExitWriter(&g_rwlAdapterLock,
  1076. kiIrql);
  1077. //
  1078. // Delete from IP, but dont clear the index
  1079. //
  1080. g_pfnIpDeleteInterface(g_pServerAdapter->pvIpContext,
  1081. FALSE);
  1082. nStatus = KeWaitForSingleObject(&keTempEvent,
  1083. Executive,
  1084. KernelMode,
  1085. FALSE,
  1086. NULL);
  1087. RtAssert(nStatus is STATUS_SUCCESS);
  1088. EnterWriter(&g_rwlAdapterLock,
  1089. &kiIrql);
  1090. RtAcquireSpinLockAtDpcLevel(&(g_pServerAdapter->rlLock));
  1091. #if DBG
  1092. Trace(ADPT, INFO,
  1093. ("MapServerAdapter: Removed %s (server adapter) from Ip\n",
  1094. g_pServerAdapter->asDeviceNameA.Buffer));
  1095. #endif
  1096. g_pServerAdapter->pkeChangeEvent = NULL;
  1097. g_pServerAdapter->byState = AS_FREE;
  1098. g_pServerInterface->pAdapter = NULL;
  1099. g_pServerAdapter->pInterface = NULL;
  1100. //
  1101. // deref because of the cross ref
  1102. //
  1103. DereferenceAdapter(g_pServerAdapter);
  1104. DereferenceInterface(g_pServerInterface);
  1105. g_pServerAdapter->dwAdapterIndex = 0;
  1106. //
  1107. // If anyone is waiting on a state change, notify them
  1108. //
  1109. for(pleNode = g_pServerAdapter->leEventList.Flink;
  1110. pleNode isnot &(g_pServerAdapter->leEventList);
  1111. pleNode = pleNode->Flink)
  1112. {
  1113. PWAN_EVENT_NODE pTempEvent;
  1114. pTempEvent = CONTAINING_RECORD(pleNode,
  1115. WAN_EVENT_NODE,
  1116. leEventLink);
  1117. KeSetEvent(&(pTempEvent->keEvent),
  1118. 0,
  1119. FALSE);
  1120. }
  1121. pTempAdapter = g_pServerAdapter;
  1122. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1123. ExitWriter(&g_rwlAdapterLock,
  1124. kiIrql);
  1125. WanpReleaseResource(&g_wrBindMutex);
  1126. DereferenceAdapter(pTempAdapter);
  1127. return STATUS_SUCCESS;
  1128. }
  1129. if(g_pServerAdapter->byState isnot AS_FREE)
  1130. {
  1131. //
  1132. // Valid states are AS_FREE, AS_ADDING, AS_MAPPED.
  1133. // It can not be in the process of being added since the resource
  1134. // is acquired
  1135. //
  1136. RtAssert(g_pServerAdapter->byState is AS_MAPPED);
  1137. RtAssert(g_pServerAdapter->pInterface is g_pServerInterface);
  1138. RtAssert(g_pServerInterface->pAdapter is g_pServerAdapter);
  1139. pInfo->dwAdapterIndex = g_pServerAdapter->dwAdapterIndex;
  1140. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1141. ExitWriter(&g_rwlAdapterLock,
  1142. kiIrql);
  1143. WanpReleaseResource(&g_wrBindMutex);
  1144. pIrp->IoStatus.Information = sizeof(WANARP_MAP_SERVER_ADAPTER_INFO);
  1145. return STATUS_SUCCESS;
  1146. }
  1147. RtAssert(g_pServerInterface->pAdapter is NULL);
  1148. RtAssert(g_pServerAdapter->pInterface is NULL);
  1149. ReferenceAdapter(g_pServerAdapter);
  1150. g_pServerAdapter->byState = AS_ADDING;
  1151. //
  1152. // Since we are changing the state, no one else should be also
  1153. // changing the state
  1154. //
  1155. RtAssert(g_pServerAdapter->pkeChangeEvent is NULL)
  1156. KeInitializeEvent(&keTempEvent,
  1157. SynchronizationEvent,
  1158. FALSE);
  1159. g_pServerAdapter->pkeChangeEvent = &keTempEvent;
  1160. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1161. ExitWriter(&g_rwlAdapterLock,
  1162. kiIrql);
  1163. usTempName.MaximumLength = (GUID_STR_LEN + 1) * sizeof(WCHAR);
  1164. usTempName.Length = GUID_STR_LEN * sizeof(WCHAR);
  1165. usTempName.Buffer = rgwcGuid;
  1166. ConvertGuidToString(&(g_pServerInterface->Guid),
  1167. rgwcGuid);
  1168. nStatus = WanpAddAdapterToIp(g_pServerAdapter,
  1169. TRUE,
  1170. g_pServerInterface->dwRsvdAdapterIndex,
  1171. &usTempName,
  1172. IF_TYPE_PPP,
  1173. IF_ACCESS_POINTTOMULTIPOINT,
  1174. IF_CONNECTION_PASSIVE);
  1175. if(nStatus isnot STATUS_SUCCESS)
  1176. {
  1177. Trace(ADPT, ERROR,
  1178. ("MapServerAdapter: %x adding %x to IP\n",
  1179. nStatus, g_pServerAdapter));
  1180. EnterWriter(&g_rwlAdapterLock,
  1181. &kiIrql);
  1182. RtAcquireSpinLockAtDpcLevel(&(g_pServerAdapter->rlLock));
  1183. g_pServerAdapter->byState = AS_FREE;
  1184. g_pServerAdapter->pkeChangeEvent = NULL;
  1185. //
  1186. // If anyone is waiting on a state change, notify them
  1187. //
  1188. for(pleNode = g_pServerAdapter->leEventList.Flink;
  1189. pleNode isnot &(g_pServerAdapter->leEventList);
  1190. pleNode = pleNode->Flink)
  1191. {
  1192. PWAN_EVENT_NODE pTempEvent;
  1193. pTempEvent = CONTAINING_RECORD(pleNode,
  1194. WAN_EVENT_NODE,
  1195. leEventLink);
  1196. KeSetEvent(&(pTempEvent->keEvent),
  1197. 0,
  1198. FALSE);
  1199. }
  1200. pTempAdapter = g_pServerAdapter;
  1201. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1202. ExitWriter(&g_rwlAdapterLock,
  1203. kiIrql);
  1204. #if DBG
  1205. Trace(ADPT, ERROR,
  1206. ("MapServerAdapter: Couldnt add %s to Ip as server adapter\n",
  1207. pTempAdapter->asDeviceNameA.Buffer));
  1208. #endif
  1209. DereferenceAdapter(pTempAdapter);
  1210. WanpReleaseResource(&g_wrBindMutex);
  1211. return nStatus;
  1212. }
  1213. //
  1214. // Wait till the OpenAdapter is called
  1215. //
  1216. nStatus = KeWaitForSingleObject(&keTempEvent,
  1217. Executive,
  1218. KernelMode,
  1219. FALSE,
  1220. NULL);
  1221. RtAssert(nStatus is STATUS_SUCCESS);
  1222. Trace(ADPT, TRACE,
  1223. ("MapServerAdapter: IPAddInterface succeeded for adapter %w\n",
  1224. g_pServerAdapter->usDeviceNameW.Buffer));
  1225. EnterWriter(&g_rwlAdapterLock,
  1226. &kiIrql);
  1227. RtAcquireSpinLockAtDpcLevel(&(g_pServerAdapter->rlLock));
  1228. //
  1229. // Cross ref the structures
  1230. //
  1231. g_pServerAdapter->pInterface = g_pServerInterface;
  1232. g_pServerInterface->pAdapter = g_pServerAdapter;
  1233. g_pServerAdapter->byState = AS_MAPPED;
  1234. g_pServerInterface->dwOperState = IF_OPER_STATUS_CONNECTED;
  1235. g_pServerInterface->dwLastChange= GetTimeTicks();
  1236. //
  1237. // bump the refcount because of the cross ref
  1238. //
  1239. ReferenceAdapter(g_pServerAdapter);
  1240. ReferenceInterface(g_pServerInterface);
  1241. pInfo->dwAdapterIndex = g_pServerAdapter->dwAdapterIndex;
  1242. g_pServerAdapter->pkeChangeEvent = NULL;
  1243. //
  1244. // If anyone is waiting on a state change, notify them
  1245. //
  1246. for(pleNode = g_pServerAdapter->leEventList.Flink;
  1247. pleNode isnot &(g_pServerAdapter->leEventList);
  1248. pleNode = pleNode->Flink)
  1249. {
  1250. PWAN_EVENT_NODE pTempEvent;
  1251. pTempEvent = CONTAINING_RECORD(pleNode,
  1252. WAN_EVENT_NODE,
  1253. leEventLink);
  1254. KeSetEvent(&(pTempEvent->keEvent),
  1255. 0,
  1256. FALSE);
  1257. }
  1258. pTempAdapter = g_pServerAdapter;
  1259. RtReleaseSpinLockFromDpcLevel(&(g_pServerAdapter->rlLock));
  1260. ExitWriter(&g_rwlAdapterLock,
  1261. kiIrql);
  1262. DereferenceAdapter(pTempAdapter);
  1263. WanpReleaseResource(&g_wrBindMutex);
  1264. pIrp->IoStatus.Information = sizeof(WANARP_MAP_SERVER_ADAPTER_INFO);
  1265. return STATUS_SUCCESS;
  1266. }
  1267. NTSTATUS
  1268. WanStartStopQueuing(
  1269. PIRP pIrp,
  1270. ULONG ulInLength,
  1271. ULONG ulOutLength
  1272. )
  1273. /*++
  1274. Routine Description:
  1275. The handler for IOCTL_WANARP_QUEUE.
  1276. It is used to start or stop queuing notifications to the router manager.
  1277. On start, we return the dial out interfaces that we currently have.
  1278. Locks:
  1279. Acquires the IoCancelSpinLock
  1280. Arguments:
  1281. Return Value:
  1282. STATUS_PENDING
  1283. STATUS_SUCCESS
  1284. STATUS_BUFFER_TOO_SMALL
  1285. --*/
  1286. {
  1287. KIRQL kiIrql;
  1288. PLIST_ENTRY pleNode;
  1289. PVOID pvIoBuffer;
  1290. ULONG i, ulMaxInterfaces, ulSizeReq;
  1291. PWANARP_QUEUE_INFO pQueueInfo;
  1292. PPENDING_NOTIFICATION pNotification;
  1293. TraceEnter(GLOBAL, "StartStopQueuing");
  1294. pvIoBuffer = pIrp->AssociatedIrp.SystemBuffer;
  1295. pIrp->IoStatus.Information = 0;
  1296. if(ulInLength < FIELD_OFFSET(WANARP_QUEUE_INFO, rgIfInfo))
  1297. {
  1298. return STATUS_BUFFER_TOO_SMALL;
  1299. }
  1300. //
  1301. // use cancel spin lock to prevent irp being cancelled during this call.
  1302. //
  1303. IoAcquireCancelSpinLock(&kiIrql);
  1304. //
  1305. // If the user is stopping queueing, delete all the pending notifications
  1306. //
  1307. pQueueInfo = (PWANARP_QUEUE_INFO)pvIoBuffer;
  1308. if(!pQueueInfo->fQueue)
  1309. {
  1310. g_bQueueNotifications = FALSE;
  1311. while(!IsListEmpty(&g_lePendingNotificationList))
  1312. {
  1313. //
  1314. // We have some old info
  1315. // Remove it off the pending list
  1316. //
  1317. pleNode = RemoveHeadList(&g_lePendingNotificationList);
  1318. //
  1319. // Get a pointer to the structure
  1320. //
  1321. pNotification = CONTAINING_RECORD(pleNode,
  1322. PENDING_NOTIFICATION,
  1323. leNotificationLink);
  1324. //
  1325. // Free the allocated notification
  1326. //
  1327. FreeNotification(pNotification);
  1328. }
  1329. //
  1330. // Done
  1331. //
  1332. IoReleaseCancelSpinLock(kiIrql);
  1333. WanpClearPendingIrps();
  1334. return STATUS_SUCCESS;
  1335. }
  1336. //
  1337. // The user wants to start queueing
  1338. // See if she has given us enough space to copy out
  1339. // the current dial outs
  1340. //
  1341. if(ulOutLength < FIELD_OFFSET(WANARP_QUEUE_INFO, rgIfInfo))
  1342. {
  1343. IoReleaseCancelSpinLock(kiIrql);
  1344. return STATUS_BUFFER_TOO_SMALL;
  1345. }
  1346. EnterReaderAtDpcLevel(&g_rwlAdapterLock);
  1347. ulSizeReq = FIELD_OFFSET(WANARP_QUEUE_INFO, rgIfInfo) +
  1348. (g_ulNumDialOutInterfaces * sizeof(WANARP_IF_INFO));
  1349. pQueueInfo->ulNumCallout = g_ulNumDialOutInterfaces;
  1350. if(ulOutLength < ulSizeReq)
  1351. {
  1352. pIrp->IoStatus.Information = FIELD_OFFSET(WANARP_QUEUE_INFO, rgIfInfo);
  1353. ExitReaderFromDpcLevel(&g_rwlAdapterLock);
  1354. IoReleaseCancelSpinLock(kiIrql);
  1355. return STATUS_MORE_ENTRIES;
  1356. }
  1357. ulMaxInterfaces =
  1358. (ulOutLength - FIELD_OFFSET(WANARP_QUEUE_INFO, rgIfInfo)) / sizeof(WANARP_IF_INFO);
  1359. //
  1360. // Have enough space
  1361. // Walk the list of mapped adapters looking for CALLOUTs
  1362. //
  1363. for(i = 0, pleNode = g_leMappedAdapterList.Flink;
  1364. pleNode isnot &g_leMappedAdapterList;
  1365. pleNode = pleNode->Flink)
  1366. {
  1367. PUMODE_INTERFACE pIf;
  1368. PADAPTER pAdapter;
  1369. PCONN_ENTRY pConnEntry;
  1370. pAdapter = CONTAINING_RECORD(pleNode,
  1371. ADAPTER,
  1372. leAdapterLink);
  1373. //
  1374. // Lock the adapter and check its connection entry
  1375. //
  1376. RtAcquireSpinLockAtDpcLevel(&(pAdapter->rlLock));
  1377. pConnEntry = pAdapter->pConnEntry;
  1378. if(pConnEntry and
  1379. (pConnEntry->duUsage is DU_CALLOUT))
  1380. {
  1381. RtAssert(i < ulMaxInterfaces);
  1382. pIf = pAdapter->pInterface;
  1383. RtAssert(pIf);
  1384. RtAssert(pIf->dwRsvdAdapterIndex);
  1385. pQueueInfo->rgIfInfo[i].InterfaceGuid = pIf->Guid;
  1386. pQueueInfo->rgIfInfo[i].dwAdapterIndex = pIf->dwRsvdAdapterIndex;
  1387. pQueueInfo->rgIfInfo[i].dwLocalAddr = pConnEntry->dwLocalAddr;
  1388. pQueueInfo->rgIfInfo[i].dwLocalMask = pConnEntry->dwLocalMask;
  1389. pQueueInfo->rgIfInfo[i].dwRemoteAddr = pConnEntry->dwRemoteAddr;
  1390. i++;
  1391. }
  1392. RtReleaseSpinLockFromDpcLevel(&(pAdapter->rlLock));
  1393. }
  1394. g_bQueueNotifications = TRUE;
  1395. ExitReaderFromDpcLevel(&g_rwlAdapterLock);
  1396. IoReleaseCancelSpinLock(kiIrql);
  1397. pIrp->IoStatus.Information = ulSizeReq;
  1398. return STATUS_SUCCESS;
  1399. }
  1400. VOID
  1401. WanCancelNotificationIrp(
  1402. PDEVICE_OBJECT pDeviceObject,
  1403. PIRP pIrp
  1404. )
  1405. /*++
  1406. Routine Description:
  1407. Called to cancel a queued irp
  1408. Locks:
  1409. Arguments:
  1410. Return Value:
  1411. --*/
  1412. {
  1413. Trace(GLOBAL, TRACE,
  1414. ("CancelNotificationIrp\n"));
  1415. //
  1416. // Mark this Irp as cancelled
  1417. //
  1418. pIrp->IoStatus.Status = STATUS_CANCELLED;
  1419. pIrp->IoStatus.Information = 0;
  1420. //
  1421. // Take off our own list
  1422. //
  1423. RemoveEntryList(&pIrp->Tail.Overlay.ListEntry);
  1424. //
  1425. // Release cancel spin lock which the IO system acquired
  1426. //
  1427. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  1428. IoCompleteRequest(pIrp,
  1429. IO_NETWORK_INCREMENT);
  1430. }
  1431. VOID
  1432. WanpCompleteIrp(
  1433. PPENDING_NOTIFICATION pEvent
  1434. )
  1435. /*++
  1436. Routine Description:
  1437. Completes a notification irp.
  1438. Locks:
  1439. Arguments:
  1440. Return Value:
  1441. --*/
  1442. {
  1443. KIRQL kiIrql;
  1444. Trace(GLOBAL, TRACE,
  1445. ("Completing Notification Irp\n"));
  1446. //
  1447. // grab cancel spin lock
  1448. //
  1449. IoAcquireCancelSpinLock(&kiIrql);
  1450. if(!g_bQueueNotifications)
  1451. {
  1452. IoReleaseCancelSpinLock(kiIrql);
  1453. FreeNotification(pEvent);
  1454. return;
  1455. }
  1456. if(!IsListEmpty(&g_lePendingIrpList))
  1457. {
  1458. PLIST_ENTRY pleNode;
  1459. PIRP pIrp;
  1460. //
  1461. // We have a pending IRP. Use it to return info to router manager
  1462. //
  1463. pleNode = RemoveHeadList(&g_lePendingIrpList) ;
  1464. pIrp = CONTAINING_RECORD(pleNode,
  1465. IRP,
  1466. Tail.Overlay.ListEntry);
  1467. RtlCopyMemory(pIrp->AssociatedIrp.SystemBuffer,
  1468. &(pEvent->wnMsg),
  1469. sizeof(WANARP_NOTIFICATION));
  1470. Trace(GLOBAL, TRACE,
  1471. ("Returning Irp with event code of %d\n",
  1472. ((PWANARP_NOTIFICATION)pIrp->AssociatedIrp.SystemBuffer)->ddeEvent));
  1473. IoSetCancelRoutine(pIrp,
  1474. NULL);
  1475. pIrp->IoStatus.Status = STATUS_SUCCESS;
  1476. pIrp->IoStatus.Information = sizeof(WANARP_NOTIFICATION);
  1477. //
  1478. // release lock
  1479. //
  1480. IoReleaseCancelSpinLock(kiIrql);
  1481. IoCompleteRequest(pIrp,
  1482. IO_NETWORK_INCREMENT);
  1483. //
  1484. // Free the allocated notification
  1485. //
  1486. FreeNotification(pEvent);
  1487. }
  1488. else
  1489. {
  1490. Trace(GLOBAL, TRACE,
  1491. ("Found no pending Irp so queuing the notification\n"));
  1492. InsertTailList(&g_lePendingNotificationList,
  1493. &(pEvent->leNotificationLink));
  1494. //
  1495. // release lock
  1496. //
  1497. IoReleaseCancelSpinLock(kiIrql);
  1498. }
  1499. }
  1500. NTSTATUS
  1501. WanpGetNewIndex(
  1502. OUT PULONG pulIndex
  1503. )
  1504. /*++
  1505. Routine Description:
  1506. Gets a new interface index from IP
  1507. Locks:
  1508. None
  1509. Arguments:
  1510. pulIndex OUT interface index
  1511. Return Value:
  1512. --*/
  1513. {
  1514. ULONG ulMax;
  1515. *pulIndex = INVALID_IF_INDEX;
  1516. return g_pfnIpReserveIndex(1, pulIndex, &ulMax);
  1517. }
  1518. VOID
  1519. WanpFreeIndex(
  1520. IN ULONG ulIndex
  1521. )
  1522. /*++
  1523. Routine Description:
  1524. Frees an index back to IP
  1525. Locks:
  1526. None
  1527. Arguments:
  1528. ulIndex
  1529. Return Value:
  1530. --*/
  1531. {
  1532. ULONG ulMax;
  1533. g_pfnIpDereserveIndex(1,
  1534. ulIndex);
  1535. }