aboutsummaryrefslogtreecommitdiffstats
path: root/driver/nsi.c
blob: cf1f3da8f44cf4329853666174746cdf4fb5d330 (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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2015-2026 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

#include "interlocked.h"
#include "containers.h"
#include "device.h"
#include "messages.h"
#include "undocumented.h"
#include "nsi.h"
#include <netiodef.h>
#include <netioapi.h>

static DRIVER_OBJECT *DriverObj;
static HANDLE IpInterfaceNotifier;
static DEVICE_OBJECT *FilterDevice, *NsiDevice;
static EX_RUNDOWN_REF FilterRundown;
static DRIVER_DISPATCH *PriorDispatch[IRP_MJ_MAXIMUM_FUNCTION + 1];
static LIST_ENTRY DeviceList;
static EX_PUSH_LOCK DeviceListLock, FilterLock;

static VOID
IpInterfaceChangeNotification(
    _In_opt_ PVOID CallerContext,
    _In_opt_ PMIB_IPINTERFACE_ROW Row,
    _In_ MIB_NOTIFICATION_TYPE NotificationType)
{
    if ((NotificationType != MibAddInstance && NotificationType != MibParameterNotification) || !Row ||
        (NotificationType == MibParameterNotification && (!Row->NlMtu || Row->NlMtu == ~0U)))
        return;
    MuAcquirePushLockShared(&DeviceListLock);
    WG_DEVICE *IterWg, *Wg = NULL;
    LIST_FOR_EACH_ENTRY (IterWg, &DeviceList, DeviceList)
    {
        if (IterWg->InterfaceLuid.Value == Row->InterfaceLuid.Value)
        {
            Wg = IterWg;
            break;
        }
    }
    if (!Wg)
        goto cleanupDeviceListLock;
    ULONG *Mtu;
    if (Row->Family == AF_INET)
        Mtu = &Wg->Mtu4;
    else if (Row->Family == AF_INET6)
        Mtu = &Wg->Mtu6;
    else
        goto cleanupDeviceListLock;
    if (NotificationType == MibAddInstance && !*Mtu)
    {
        if ((!Row->NlMtu || Row->NlMtu == ~0U) && !NT_SUCCESS(GetIpInterfaceEntry(Row)))
            goto cleanupDeviceListLock;
        *Mtu = min(MTU_MAX, Row->NlMtu);
        if (Row->NlMtu == MTU_MAX || !Row->NlMtu || Row->NlMtu == ~0U)
        {
            Row->SitePrefixLength = 0;
            Row->NlMtu = 1500 - DATA_PACKET_MINIMUM_LENGTH;
            if (NT_SUCCESS(SetIpInterfaceEntry(Row)))
                *Mtu = min(MTU_MAX, Row->NlMtu);
        }
    }
    else if (NotificationType == MibParameterNotification)
        *Mtu = min(MTU_MAX, Row->NlMtu);
cleanupDeviceListLock:
    MuReleasePushLockShared(&DeviceListLock);
}

static_assert(
    FIELD_SIZE(MIB_IPINTERFACE_ROW, NlMtu) == FIELD_SIZE(NSI_IP_INTERFACE_RW, NlMtu) &&
    FIELD_SIZE(MIB_IPINTERFACE_ROW, NlMtu) == FIELD_SIZE(NSI_IP_SUBINTERFACE_RW, NlMtu),
    "NlMtu sizes must match across MIB_IPINTERFACE_ROW, NSI_IP_INTERFACE_RW, and NSI_IP_SUBINTERFACE_RW");

_Success_(return)
static BOOLEAN
TryBuildMTURow(_In_ IRP *Irp, _Out_ MIB_IPINTERFACE_ROW *Row)
{
    IO_STACK_LOCATION *Stack = IoGetCurrentIrpStackLocation(Irp);
    if (Stack->MajorFunction != IRP_MJ_DEVICE_CONTROL ||
        Stack->Parameters.DeviceIoControl.IoControlCode != IOCTL_NSI_SET_ALL_PARAMETERS)
        return FALSE;
    NPI_MODULEID ModuleId = { 0 };
    __try
    {
        NSI_SET_ALL_PARAMETERS Params;
        UCHAR *UserBuffer = Stack->Parameters.DeviceIoControl.Type3InputBuffer;
        ULONG Len = Stack->Parameters.DeviceIoControl.InputBufferLength;
        BOOLEAN ShouldProbe = Irp->RequestorMode != KernelMode;
#ifdef _WIN64
        if (IoIs32bitProcess(Irp))
        {
            NSI_SET_ALL_PARAMETERS_32 Params32;
            if (Len < sizeof(Params32))
                return FALSE;
            if (ShouldProbe)
                ProbeForRead(UserBuffer, sizeof(Params32), 1);
            RtlCopyMemory(&Params32, UserBuffer, sizeof(Params32));
            RtlZeroMemory(&Params, sizeof(Params));
            Params.ModuleId = (PVOID)(ULONG_PTR)Params32.ModuleId;
            Params.ObjectIndex = Params32.ObjectIndex;
            Params.KeyStruct = (PVOID)(ULONG_PTR)Params32.KeyStruct;
            Params.KeyStructLength = Params32.KeyStructLength;
            Params.RwParameterStruct = (PVOID)(ULONG_PTR)Params32.RwParameterStruct;
            Params.RwParameterStructLength = Params32.RwParameterStructLength;
        }
        else
#endif
        {
            if (Len < sizeof(Params))
                return FALSE;
            if (ShouldProbe)
                ProbeForRead(UserBuffer, sizeof(Params), 1);
            RtlCopyMemory(&Params, UserBuffer, sizeof(Params));
        }
        if (Params.KeyStructLength < sizeof(Row->InterfaceLuid))
            return FALSE;
        ULONG MtuOffset;
        if ((ULONG)Params.ObjectIndex == NlInterfaceObject &&
            Params.RwParameterStructLength >= FIELD_OFFSET(NSI_IP_INTERFACE_RW, NlMtu) + FIELD_SIZE(NSI_IP_INTERFACE_RW, NlMtu))
            MtuOffset = FIELD_OFFSET(NSI_IP_INTERFACE_RW, NlMtu);
        else if ((ULONG)Params.ObjectIndex == NlSubInterfaceObject &&
                 Params.RwParameterStructLength >= FIELD_OFFSET(NSI_IP_SUBINTERFACE_RW, NlMtu) + FIELD_SIZE(NSI_IP_SUBINTERFACE_RW, NlMtu))
            MtuOffset = FIELD_OFFSET(NSI_IP_SUBINTERFACE_RW, NlMtu);
        else
            return FALSE;
        if (ShouldProbe)
            ProbeForRead(Params.ModuleId, sizeof(ModuleId), 1);
        RtlCopyMemory(&ModuleId, Params.ModuleId, sizeof(ModuleId));
        if (ShouldProbe)
            ProbeForRead(Params.KeyStruct, sizeof(Row->InterfaceLuid), 1);
        RtlCopyMemory(&Row->InterfaceLuid, Params.KeyStruct, sizeof(Row->InterfaceLuid));
        if (ShouldProbe)
            ProbeForRead((UCHAR *)Params.RwParameterStruct + MtuOffset, sizeof(Row->NlMtu), 1);
        RtlCopyMemory(&Row->NlMtu, (UCHAR *)Params.RwParameterStruct + MtuOffset, sizeof(Row->NlMtu));
    }
    __except (EXCEPTION_EXECUTE_HANDLER) { return FALSE; }
    if (!Row->NlMtu || Row->NlMtu == ~0U)
        return FALSE;
    if (RtlEqualMemory(&NPI_MS_IPV4_MODULEID, &ModuleId, sizeof(ModuleId)))
        Row->Family = AF_INET;
    else if (RtlEqualMemory(&NPI_MS_IPV6_MODULEID, &ModuleId, sizeof(ModuleId)))
        Row->Family = AF_INET6;
    else
        return FALSE;
    return TRUE;
}

static DRIVER_DISPATCH_PAGED FilterDispatch;
_Use_decl_annotations_
static NTSTATUS
FilterDispatch(DEVICE_OBJECT *DeviceObject, IRP *Irp)
{
    FILE_OBJECT *FileObject;

    if (DeviceObject->DeviceExtension) /* NDIS devices always have an extension. */
        return PriorDispatch[IoGetCurrentIrpStackLocation(Irp)->MajorFunction](DeviceObject, Irp);
    if (!ExAcquireRundownProtection(&FilterRundown))
        goto bypassFilter;
    if (DeviceObject != ReadPointerNoFence(&FilterDevice))
    {
        ExReleaseRundownProtection(&FilterRundown);
        goto bypassFilter;
    }

    MIB_IPINTERFACE_ROW Row = { 0 };
    BOOLEAN ValidMTURow = TryBuildMTURow(Irp, &Row);
    IoSkipCurrentIrpStackLocation(Irp);
    NTSTATUS Status = IoCallDriver(NsiDevice, Irp);
    if (ValidMTURow && NT_SUCCESS(Status) && Status != STATUS_PENDING)
        IpInterfaceChangeNotification(NULL, &Row, MibParameterNotification);

    ExReleaseRundownProtection(&FilterRundown);
    return Status;

bypassFilter:
    FileObject = IoGetCurrentIrpStackLocation(Irp)->FileObject;
    if (FileObject && FileObject->DeviceObject)
    {
        IoSkipCurrentIrpStackLocation(Irp);
        return IoCallDriver(FileObject->DeviceObject, Irp);
    }
    Irp->IoStatus.Status = STATUS_DEVICE_REMOVED;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_DEVICE_REMOVED;
}

static NTSTATUS
Attach(VOID)
{
    NTSTATUS Status =
        NotifyIpInterfaceChange(AF_UNSPEC, IpInterfaceChangeNotification, NULL, FALSE, &IpInterfaceNotifier);
    if (!NT_SUCCESS(Status))
        return Status;

    UNICODE_STRING NsiName = RTL_CONSTANT_STRING(L"\\Device\\Nsi");
    PFILE_OBJECT NsiFileObject;
    Status = IoGetDeviceObjectPointer(&NsiName, FILE_READ_ATTRIBUTES, &NsiFileObject, &NsiDevice);
    if (!NT_SUCCESS(Status))
        goto cleanupIpInterfaceNotifier;

    Status = IoCreateDevice(DriverObj, 0, NULL, NsiDevice->DeviceType, 0, FALSE, &FilterDevice);
    if (!NT_SUCCESS(Status))
        goto cleanupFileObject;
    FilterDevice->Flags |= NsiDevice->Flags & (DO_BUFFERED_IO | DO_DIRECT_IO | DO_POWER_PAGABLE);

    ExReInitializeRundownProtection(&FilterRundown);

    PDEVICE_OBJECT Attached = IoAttachDeviceToDeviceStack(FilterDevice, NsiDevice);
    if (!Attached)
    {
        Status = STATUS_DEVICE_REMOVED;
        goto cleanupRundown;
    }
    NsiDevice = Attached;
    FilterDevice->Flags &= ~DO_DEVICE_INITIALIZING;
    ObReferenceObject(NsiDevice);
    ObDereferenceObject(NsiFileObject);
    return STATUS_SUCCESS;

cleanupRundown:
    ExWaitForRundownProtectionRelease(&FilterRundown);
    {
        DEVICE_OBJECT *FilterDeviceToDelete = FilterDevice;
        WritePointerNoFence(&FilterDevice, NULL);
        IoDeleteDevice(FilterDeviceToDelete);
    }
cleanupFileObject:
    ObDereferenceObject(NsiFileObject);
cleanupIpInterfaceNotifier:
    CancelMibChangeNotify2(IpInterfaceNotifier);
    return Status;
}

static VOID
Detach(VOID)
{
    CancelMibChangeNotify2(IpInterfaceNotifier);
    IoDetachDevice(NsiDevice);
    ExWaitForRundownProtectionRelease(&FilterRundown);
    DEVICE_OBJECT *FilterDeviceToDelete = FilterDevice;
    WritePointerNoFence(&FilterDevice, NULL);
    IoDeleteDevice(FilterDeviceToDelete);
    ObDereferenceObject(NsiDevice);
}

#ifdef ALLOC_PRAGMA
#    pragma alloc_text(INIT, NsiDriverEntry)
#endif
_Use_decl_annotations_
VOID
NsiDriverEntry(DRIVER_OBJECT *DriverObject)
{
    DriverObj = DriverObject;
    InitializeListHead(&DeviceList);
    MuInitializePushLock(&DeviceListLock);
    MuInitializePushLock(&FilterLock);
    ExInitializeRundownProtection(&FilterRundown);
    ExWaitForRundownProtectionRelease(&FilterRundown);
    for (ULONG i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; ++i)
    {
        PriorDispatch[i] = DriverObject->MajorFunction[i];
        DriverObject->MajorFunction[i] = FilterDispatch;
    }
}

_Use_decl_annotations_
NTSTATUS
NsiActivate(WG_DEVICE *Wg)
{
    NTSTATUS Status;
    MuAcquirePushLockExclusive(&FilterLock);
    if (IsListEmpty(&DeviceList))
    {
        Status = Attach();
        if (!NT_SUCCESS(Status))
        {
            MuReleasePushLockExclusive(&FilterLock);
            return Status;
        }
    }
    MuAcquirePushLockExclusive(&DeviceListLock);
    InsertTailList(&DeviceList, &Wg->DeviceList);
    MuReleasePushLockExclusive(&DeviceListLock);
    MuReleasePushLockExclusive(&FilterLock);
    return STATUS_SUCCESS;
}

_Use_decl_annotations_
VOID
NsiDeactivate(WG_DEVICE *Wg)
{
    MuAcquirePushLockExclusive(&FilterLock);
    MuAcquirePushLockExclusive(&DeviceListLock);
    RemoveEntryList(&Wg->DeviceList);
    BOOLEAN NowEmpty = IsListEmpty(&DeviceList);
    MuReleasePushLockExclusive(&DeviceListLock);
    if (NowEmpty)
        Detach();
    MuReleasePushLockExclusive(&FilterLock);
}