summaryrefslogtreecommitdiffstats
path: root/private/ntos/afd/create.c
blob: 7b2cf5a39028516086f65a817a6b314211469069 (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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    dispatch.c

Abstract:

    This module contains code for opening a handle to AFD.

Author:

    David Treadwell (davidtr)    21-Feb-1992

Revision History:

--*/

#include "afdp.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text( PAGE, AfdCreate )
#endif

extern PSECURITY_DESCRIPTOR AfdRawSecurityDescriptor;



NTSTATUS
AfdCreate (
    IN PIRP Irp,
    IN PIO_STACK_LOCATION IrpSp
    )

/*++

Routine Description:

    This is the routine that handles Create IRPs in AFD.  If creates an
    AFD_ENDPOINT structure and fills it in with the information
    specified in the open packet.

Arguments:

    Irp - Pointer to I/O request packet.

    IrpSp - pointer to the IO stack location to use for this request.

Return Value:

    NTSTATUS -- Indicates whether the request was successfully queued.

--*/

{
    PAFD_OPEN_PACKET openPacket;
    PAFD_ENDPOINT endpoint;
    PFILE_FULL_EA_INFORMATION eaBuffer;
    UNICODE_STRING transportDeviceName;
    NTSTATUS status;

    PAGED_CODE( );

    DEBUG endpoint = NULL;

    //
    // Find the open packet from the EA buffer in the system buffer of
    // the associated IRP.  Fail the request if there was no EA
    // buffer specified.
    //

    eaBuffer = Irp->AssociatedIrp.SystemBuffer;

    if ( eaBuffer == NULL ) {

        //
        // Allocate an AFD "helper" endpoint.
        //

        status = AfdAllocateEndpoint(
                     &endpoint,
                     NULL,
                     0
                     );

        if( !NT_SUCCESS(status) ) {
            return status;
        }

    } else {

        openPacket = (PAFD_OPEN_PACKET)(eaBuffer->EaName +
                                            eaBuffer->EaNameLength + 1);

        //
        // Validate parameters in the open packet.
        //

        if ( openPacket->EndpointType < MIN_AFD_ENDPOINT_TYPE ||
                 openPacket->EndpointType > MAX_AFD_ENDPOINT_TYPE ) {
            return STATUS_INVALID_PARAMETER;
        }

        //
        // Make sure that the transport address fits within the specified
        // EA buffer.
        //

        if ( eaBuffer->EaValueLength <
                 sizeof(AFD_OPEN_PACKET) + openPacket->TransportDeviceNameLength ) {
            return STATUS_ACCESS_VIOLATION;
        }

        //
        // Set up a string that describes the transport device name.
        //

        transportDeviceName.Buffer = openPacket->TransportDeviceName;
        transportDeviceName.Length = (USHORT)openPacket->TransportDeviceNameLength;
        transportDeviceName.MaximumLength =
            transportDeviceName.Length + sizeof(WCHAR);

        //
        // If this is an open of a  raw endpoint, perform an access check.
        //
        if ( ( openPacket->EndpointType == AfdEndpointTypeRaw ) &&
             !AfdDisableRawSecurity
           )
        {
            BOOLEAN               accessGranted;
            PACCESS_STATE         accessState;
            PIO_SECURITY_CONTEXT  securityContext;
            NTSTATUS              status;
            PPRIVILEGE_SET        privileges = NULL;
            ACCESS_MASK           grantedAccess;


            securityContext = IrpSp->Parameters.Create.SecurityContext;
            accessState = securityContext->AccessState;

            SeLockSubjectContext(&accessState->SubjectSecurityContext);

            accessGranted = SeAccessCheck(
                                AfdRawSecurityDescriptor,
                                &accessState->SubjectSecurityContext,
                                TRUE,
                                IrpSp->Parameters.Create.SecurityContext->DesiredAccess,
                                0,
                                &privileges,
                                IoGetFileObjectGenericMapping(),
                                UserMode,
                                &grantedAccess,
                                &status
                                );


            if (privileges) {
                (VOID) SeAppendPrivileges(
                           accessState,
                           privileges
                           );
                SeFreePrivileges(privileges);
            }

            if (accessGranted) {
                accessState->PreviouslyGrantedAccess |= grantedAccess;
                accessState->RemainingDesiredAccess &= ~( grantedAccess | MAXIMUM_ALLOWED );
            }

            SeUnlockSubjectContext(&accessState->SubjectSecurityContext);

            if (!accessGranted) {
                return STATUS_ACCESS_DENIED;
            }
        }

        //
        // Allocate an AFD endpoint.
        //

        status = AfdAllocateEndpoint(
                     &endpoint,
                     &transportDeviceName,
                     openPacket->GroupID
                     );

        if( !NT_SUCCESS(status) ) {
            return status;
        }
    }

    ASSERT( endpoint != NULL );

    //
    // Set up a pointer to the endpoint in the file object so that we
    // can find the endpoint in future calls.
    //

    IrpSp->FileObject->FsContext = endpoint;

    IF_DEBUG(OPEN_CLOSE) {
        KdPrint(( "AfdCreate: opened file object = %lx, endpoint = %lx\n",
                      IrpSp->FileObject, endpoint ));

    }

    //
    // Remember the type of endpoint that this is.  If this is a datagram
    // endpoint, change the block type to reflect this.
    //

    if ( eaBuffer != NULL ) {
        if (openPacket->EndpointType == AfdEndpointTypeRaw) {
            //
            // There is no other distinction between a raw endpoint and
            // a datagram endpoint, so we mark them all as datagram.
            //
            endpoint->EndpointType = AfdEndpointTypeDatagram;
        }
        else {
            endpoint->EndpointType = openPacket->EndpointType;
        }
    }

    if ( IS_DGRAM_ENDPOINT(endpoint) ) {

        if ( eaBuffer == NULL ) {

           DEREFERENCE_ENDPOINT( endpoint );
           AfdCloseEndpoint( endpoint );

           return STATUS_INVALID_PARAMETER;
        }

        endpoint->Type = AfdBlockTypeDatagram;

        //
        // Initialize lists which exist only in datagram endpoints.
        //

        InitializeListHead( &endpoint->ReceiveDatagramIrpListHead );
        InitializeListHead( &endpoint->PeekDatagramIrpListHead );
        InitializeListHead( &endpoint->ReceiveDatagramBufferListHead );

        //
        // Charge quota for the endpoint to account for the data
        // bufferring we'll do on behalf of the process.
        //

        try {

            PsChargePoolQuota(
                endpoint->OwningProcess,
                NonPagedPool,
                AfdReceiveWindowSize + AfdSendWindowSize
                );
            AfdRecordQuotaHistory(
                endpoint->OwningProcess,
                (LONG)(AfdReceiveWindowSize + AfdSendWindowSize),
                "Create dgram",
                endpoint
                );
            AfdRecordPoolQuotaCharged(
                AfdReceiveWindowSize + AfdSendWindowSize
                );

        } except ( EXCEPTION_EXECUTE_HANDLER ) {

#if DBG
           DbgPrint( "AfdCreate: PsChargePoolQuota failed.\n" );
#endif

           DEREFERENCE_ENDPOINT( endpoint );
           AfdCloseEndpoint( endpoint );

           return STATUS_QUOTA_EXCEEDED;
        }

        endpoint->Common.Datagram.MaxBufferredReceiveBytes = AfdReceiveWindowSize;
        endpoint->Common.Datagram.MaxBufferredReceiveCount =
            (CSHORT)(AfdReceiveWindowSize / AfdBufferMultiplier);
        endpoint->Common.Datagram.MaxBufferredSendBytes = AfdSendWindowSize;
        endpoint->Common.Datagram.MaxBufferredSendCount =
            (CSHORT)(AfdSendWindowSize / AfdBufferMultiplier);
    }

    //
    // The open worked.  Dereference the endpoint and return success.
    //

    DEREFERENCE_ENDPOINT( endpoint );

    return STATUS_SUCCESS;

} // AfdCreate