summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/nt/tdicnct.c
blob: 3ff5610e4211cf4efbea0105a3b3be02ab811e2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
//
//  NBTCONNCT.C
//
//  This file contains code relating to opening connections with the transport
//  provider.  The Code is NT specific.

#include "nbtprocs.h"

//*******************  Pageable Routine Declarations ****************
#ifdef ALLOC_PRAGMA
#pragma CTEMakePageable(PAGE, NbtTdiOpenConnection)
#pragma CTEMakePageable(PAGE, NbtTdiAssociateConnection)
#pragma CTEMakePageable(PAGE, TdiOpenandAssocConnection)
#pragma CTEMakePageable(PAGE, NbtTdiCloseConnection)
#pragma CTEMakePageable(PAGE, CreateDeviceString)
#pragma CTEMakePageable(PAGE, NbtTdiCloseAddress)
#endif
//*******************  Pageable Routine Declarations ****************

//----------------------------------------------------------------------------
NTSTATUS
NbtTdiOpenConnection (
    IN tLOWERCONNECTION     *pLowerConn,
    IN  tDEVICECONTEXT      *pDeviceContext
    )
/*++

Routine Description:

    This routine opens a connection with the transport provider.

Arguments:

    pLowerConn - Pointer to where the handle to the Transport for this virtual
        connection should be stored.

    pNbtConfig - the name of the adapter to connect to is in this structure

Return Value:

    Status of the operation.

--*/
{
    IO_STATUS_BLOCK             IoStatusBlock;
    NTSTATUS                    Status;
    OBJECT_ATTRIBUTES           ObjectAttributes;
    PWSTR                       pName=L"Tcp";
    PFILE_FULL_EA_INFORMATION   EaBuffer;
    UNICODE_STRING              DeviceName;
    PMDL                        pMdl;
    PVOID                       pBuffer;
    BOOLEAN                     Attached = FALSE;

    CTEPagedCode();
    // zero out the connection data structure
    CTEZeroMemory(pLowerConn,sizeof(tLOWERCONNECTION));
    pLowerConn->State = NBT_IDLE;
    pLowerConn->pDeviceContext = pDeviceContext;
    pLowerConn->RefCount = 1;
    pLowerConn->LockNumber = LOWERCON_LOCK;
    pLowerConn->Verify = NBT_VERIFY_LOWERCONN;

    Status = CreateDeviceString(pName,&DeviceName);
    if (!NT_SUCCESS(Status))
    {
        return(Status);
    }

    //
    // Allocate an MDL for the Indication buffer since we may need to buffer
    // up to 128 bytes
    //
    pBuffer = NbtAllocMem(NBT_INDICATE_BUFFER_SIZE,NBT_TAG('l'));

    if (!pBuffer)
    {
        CTEMemFree(DeviceName.Buffer);
        return(STATUS_INSUFFICIENT_RESOURCES);
    }

    pMdl = IoAllocateMdl(pBuffer,NBT_INDICATE_BUFFER_SIZE,FALSE,FALSE,NULL);

    if (pMdl)
    {

        MmBuildMdlForNonPagedPool(pMdl);

        pLowerConn->pIndicateMdl = pMdl;


        InitializeObjectAttributes (
            &ObjectAttributes,
            &DeviceName,
            0,
            NULL,
            NULL);

        IF_DBG(NBT_DEBUG_TDICNCT)
            KdPrint(("tcp device to open = %ws\n",DeviceName.Buffer));

        // Allocate memory for the address info to be passed to the transport
        EaBuffer = (PFILE_FULL_EA_INFORMATION)NbtAllocMem (
                        sizeof(FILE_FULL_EA_INFORMATION) - 1 +
                        TDI_CONNECTION_CONTEXT_LENGTH + 1 +
                        sizeof(CONNECTION_CONTEXT),NBT_TAG('m'));

        if (EaBuffer)
        {

            EaBuffer->NextEntryOffset = 0;
            EaBuffer->Flags = 0;
            EaBuffer->EaNameLength = TDI_CONNECTION_CONTEXT_LENGTH;
            EaBuffer->EaValueLength = sizeof (CONNECTION_CONTEXT);

            // TdiConnectionContext is a macro that = "ConnectionContext" - so move
            // this text to EaName
            RtlMoveMemory( EaBuffer->EaName, TdiConnectionContext, EaBuffer->EaNameLength + 1 );

            // put the context value into the EaBuffer too - i.e. the value that the
            // transport returns with each indication on this connection
            RtlMoveMemory (
                (PVOID)&EaBuffer->EaName[EaBuffer->EaNameLength + 1],
                (CONST PVOID)&pLowerConn,
                sizeof (CONNECTION_CONTEXT));

            {

                Status = ZwCreateFile (
                             &pLowerConn->FileHandle,
                             GENERIC_READ | GENERIC_WRITE,
                             &ObjectAttributes,     // object attributes.
                             &IoStatusBlock,        // returned status information.
                             NULL,                  // block size (unused).
                             FILE_ATTRIBUTE_NORMAL, // file attributes.
                             0,
                             FILE_CREATE,
                             0,                     // create options.
                             (PVOID)EaBuffer,       // EA buffer.
                             sizeof(FILE_FULL_EA_INFORMATION) - 1 +
                                TDI_CONNECTION_CONTEXT_LENGTH + 1 +
                                sizeof(CONNECTION_CONTEXT)
                                );
            }

            IF_DBG(NBT_DEBUG_TDICNCT)
                KdPrint( ("OpenConnection CreateFile Status:%X, IoStatus:%X\n", Status, IoStatusBlock.Status));

            CTEMemFree((PVOID)EaBuffer);

            if ( NT_SUCCESS( Status ))
            {

                // if the ZwCreate passed set the status to the IoStatus
                //
                Status = IoStatusBlock.Status;

                if (NT_SUCCESS(Status))
                {
                    // get a reference to the file object and save it since we can't
                    // dereference a file handle at DPC level so we do it now and keep
                    // the ptr around for later.
                    Status = ObReferenceObjectByHandle(
                                pLowerConn->FileHandle,
                                0L,
                                NULL,
                                KernelMode,
                                (PVOID *)&pLowerConn->pFileObject,
                                NULL);

                    if (NT_SUCCESS(Status))
                    {
                        CTEMemFree(DeviceName.Buffer);
                        return(Status);
                    }

                    ZwClose(pLowerConn->FileHandle);

                }

            }

        }
        else
        {
            Status = STATUS_INSUFFICIENT_RESOURCES;
        }


        IoFreeMdl(pMdl);
    }
    else
    {
        Status = STATUS_INSUFFICIENT_RESOURCES;
    }

    CTEMemFree(pBuffer);
    CTEMemFree(DeviceName.Buffer);

    return Status;

} /* NbtTdiOpenConnection */

//----------------------------------------------------------------------------
NTSTATUS
NbtTdiAssociateConnection(
    IN  PFILE_OBJECT        pFileObject,
    IN  HANDLE              Handle
    )
/*++

Routine Description:

    This routine associates an open connection with the address object.

Arguments:


    pFileObject - the connection file object
    Handle      - the address object to associate the connection with

Return Value:

    Status of the operation.

--*/
{
    NTSTATUS        status;
    PIRP            pIrp;
    KEVENT          Event;
    BOOLEAN         Attached = FALSE;

    CTEPagedCode();

    KeInitializeEvent(
        &Event,
        SynchronizationEvent,
        FALSE);

    pIrp = NTAllocateNbtIrp(IoGetRelatedDeviceObject(pFileObject));

    if (!pIrp)
    {
        KdPrint(("NBT:Failed to build internal device Irp\n"));
        return(STATUS_UNSUCCESSFUL);
    }

    TdiBuildAssociateAddress (
                pIrp,
                pFileObject->DeviceObject,
                pFileObject,
                CompletionRoutine,
                &Event,
                Handle);

    status = SubmitTdiRequest(pFileObject,pIrp);

    IoFreeIrp(pIrp);

    return status;


}
//----------------------------------------------------------------------------
NTSTATUS
CreateDeviceString(
    IN  PWSTR               AppendingString,
    IN OUT PUNICODE_STRING  pucDeviceName
    )
/*++

Routine Description:

    This routine creates a string name for the transport device such as
    "\Device\Streams\Tcp"

Arguments:


Return Value:

    Status of the operation.

--*/
{
    NTSTATUS            status;
    ULONG               Len;
    PVOID               pBuffer;

    CTEPagedCode();
    // copy device name into the unicode string - either Udp or Tcp
    //
    Len = (wcslen(NbtConfig.pTcpBindName) + wcslen(AppendingString) + 1) * sizeof(WCHAR);

    pBuffer = NbtAllocMem(Len,NBT_TAG('n'));
    if (!pBuffer)
    {
        return(STATUS_INSUFFICIENT_RESOURCES);
    }

    pucDeviceName->MaximumLength = (USHORT)Len;
    pucDeviceName->Length = 0;
    pucDeviceName->Buffer = pBuffer;

    // this puts \Device\Streams into the string
    //
    status = RtlAppendUnicodeToString(pucDeviceName,NbtConfig.pTcpBindName);
    if (NT_SUCCESS(status))
    {
        status = RtlAppendUnicodeToString (pucDeviceName,AppendingString);
    }
    else
        CTEMemFree(pBuffer);

    return(status);

}

//----------------------------------------------------------------------------
NTSTATUS
TdiOpenandAssocConnection(
    IN  tCONNECTELE         *pConnEle,
    IN  tDEVICECONTEXT      *pDeviceContext,
    IN  ULONG               PortNumber
    )
/*++

Routine Description:

    This routine opens and associates an open connection.

    This routine is called with the Spin Lock held on the pConnele.  It is
    released in this routine.

Arguments:


Return Value:

    Status of the operation.

--*/
{
    NTSTATUS            status;
    NTSTATUS            Locstatus;
    PDEVICE_OBJECT      pDeviceObject;
    tLOWERCONNECTION    *pLowerConn;
    BOOLEAN             Attached=FALSE;

    CTEPagedCode();

    CTEAttachFsp(&Attached);

    // allocate memory for the lower connection block.
    //
    pConnEle->pLowerConnId = (PVOID)NbtAllocMem(sizeof(tLOWERCONNECTION),NBT_TAG('o'));

    if (!pConnEle->pLowerConnId)
    {
        return(STATUS_INSUFFICIENT_RESOURCES);
    }

    //
    // fill in the lower connection element to point to the upper one and
    // vice versa
    //
    pLowerConn = pConnEle->pLowerConnId;

    status = NbtTdiOpenConnection(pLowerConn,pDeviceContext);
    if (!NT_SUCCESS(status))
    {
        CTEDetachFsp(Attached);
        CTEMemFree((PVOID)pConnEle->pLowerConnId);
        pConnEle->pLowerConnId = NULL;
        return(status);
    }

    pLowerConn->pUpperConnection = pConnEle;
    pLowerConn->State = NBT_IDLE;

    //
    // until the correct state proc is set (i.e.Outbound), reject any data
    // (in other words, don't let this field stay NULL!)
    //
    SetStateProc( pLowerConn, RejectAnyData ) ;


    if (NT_SUCCESS(status))
    {

        // Open an address object (aka port)
        //
        status = NbtTdiOpenAddress(
                    &pLowerConn->AddrFileHandle,
                    &pDeviceObject,          // dummy argument, not used here
                    &pLowerConn->pAddrFileObject,
                    pDeviceContext,
                    (USHORT)PortNumber,  // port
                    pDeviceContext->IpAddress,
                    TCP_FLAG);

        if (NT_SUCCESS(status))
        {
            // now associate the two
            status = NbtTdiAssociateConnection(
                                    pLowerConn->pFileObject,
                                    pLowerConn->AddrFileHandle);


            if (NT_SUCCESS(status))
            {
                CTEDetachFsp(Attached);
                //
                // put the lower connection on the Q of active lower connections for
                // this device
                //
                ExInterlockedInsertTailList(&pDeviceContext->LowerConnection,
                                            &pLowerConn->Linkage,
                                            &pDeviceContext->SpinLock);

                return(status);
            }

            ObDereferenceObject(pLowerConn->pAddrFileObject);
            Locstatus = ZwClose(pLowerConn->AddrFileHandle);

        }
        KdPrint(("Nbt:Open Xport Address Failed, status %X\n",status));

        ObDereferenceObject(pLowerConn->pFileObject);
        Locstatus = ZwClose(pLowerConn->FileHandle);

    }

    CTEDetachFsp(Attached);

    // Error Path... delete memory
    //
    pConnEle->pLowerConnId = NULL;
    CTEMemFree((PVOID)pLowerConn);

    return(status);

}

//----------------------------------------------------------------------------

NTSTATUS
NbtTdiCloseConnection(
    IN tLOWERCONNECTION * pLowerConn
    )
/*++

Routine Description:

    This routine closes a TDI connection

Arguments:


Return Value:

    Status of the operation.

--*/
{
    NTSTATUS    status;
    BOOLEAN     Attached= FALSE;

    CTEPagedCode();
    ASSERT( pLowerConn != NULL ) ;

    CTEAttachFsp(&Attached);

    if (pLowerConn->FileHandle) {
        status = ZwClose(pLowerConn->FileHandle);
        pLowerConn->FileHandle = NULL;
    }

#if DBG
    if (!NT_SUCCESS(status))
    KdPrint(("Nbt:Failed to close Connection FileHandle pLower %X, status %X\n",pLowerConn,status));
#endif

    CTEDetachFsp(Attached);

    return(status);
}

//----------------------------------------------------------------------------
NTSTATUS
NbtTdiCloseAddress(
    IN tLOWERCONNECTION * pLowerConn
    )
/*++

Routine Description:

    This routine closes a TDI address

Arguments:


Return Value:

    Status of the operation.

--*/
{
    NTSTATUS    status;
    BOOLEAN     Attached= FALSE;

    CTEPagedCode();

    ASSERT( pLowerConn != NULL ) ;

    CTEAttachFsp(&Attached);

    status = ZwClose(pLowerConn->AddrFileHandle);
#if DBG
    if (!NT_SUCCESS(status))
    KdPrint(("Nbt:Failed to close Address FileHandle pLower %X,status %X\n",pLowerConn,status));
#endif

    CTEDetachFsp(Attached);

    return(status);

}