-
Notifications
You must be signed in to change notification settings - Fork 26
/
options.c
341 lines (306 loc) · 10.2 KB
/
options.c
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
/*
* Copyright (c) 2020 SUSE LLC
*
* Licensed under LGPL-2.1 (see LICENSE)
*/
#include "options.h"
#include "debug.h"
#include "util.h"
#include "wnbd_ioctl.h"
#pragma warning(push)
#pragma warning(disable:4204)
extern HANDLE GlobalDrvRegHandle;
#define WNBD_DEF_OPT(OptName, TypeSuffix, DefaultVal) \
{.Name = OptName, \
.Type = WnbdOpt ## TypeSuffix, \
.Default = { .Type = WnbdOpt ## TypeSuffix, \
.Data.As ## TypeSuffix = DefaultVal}, \
.Value = { .Type = WnbdOpt ## TypeSuffix, \
.Data.As ## TypeSuffix = DefaultVal}, \
}
// Make sure to update WNBD_OPT_KEY whenever adding or removing options.
// It provides simple and fast access to the options without having to
// introduce other structures. Also, it would be nice to keep the
// options sorted.
WNBD_OPTION WnbdDriverOptions[] = {
WNBD_DEF_OPT(L"LogLevel", Int64, WNBD_LVL_WARN),
WNBD_DEF_OPT(L"NewMappingsAllowed", Bool, TRUE),
WNBD_DEF_OPT(L"EtwLoggingEnabled", Bool, TRUE),
WNBD_DEF_OPT(L"WppLoggingEnabled", Bool, FALSE),
WNBD_DEF_OPT(L"DbgPrintEnabled", Bool, TRUE),
WNBD_DEF_OPT(L"MaxIOReqPerAdapter", Int64, WNBD_DEFAULT_MAX_IO_REQ_PER_ADAPTER),
WNBD_DEF_OPT(L"MaxIOReqPerLun", Int64, WNBD_DEFAULT_MAX_IO_REQ_PER_LUN),
WNBD_DEF_OPT(L"RemoveStaleConnections", Bool, TRUE),
WNBD_DEF_OPT(L"StaleReqTimeoutMs", Int64, WNBD_DEFAULT_STALE_REQ_TIMEOUT_MS),
WNBD_DEF_OPT(L"StaleConnTimeoutMs", Int64, WNBD_DEFAULT_STALE_CONN_TIMEOUT_MS),
};
DWORD WnbdOptionsCount = sizeof(WnbdDriverOptions) / sizeof(WNBD_OPTION);
PWNBD_OPTION WnbdFindOpt(PWCHAR Name)
{
for (DWORD i = 0; i < WnbdOptionsCount; i++) {
if (!_wcsicmp(Name, WnbdDriverOptions[i].Name)) {
return &WnbdDriverOptions[i];
}
}
return NULL;
}
_Use_decl_annotations_
NTSTATUS WnbdGetPersistentOpt(
PWCHAR Name,
PWNBD_OPTION_VALUE Value)
{
WNBD_LOG_DEBUG("Retrieving persistent opt: %ls.", Name);
PWNBD_OPTION Option = WnbdFindOpt(Name);
if (!Option) {
WNBD_LOG_WARN("Could not find option: %ls.", Name);
return STATUS_OBJECT_NAME_NOT_FOUND;
}
RtlZeroMemory(&Value->Data, sizeof(Value->Data));
BYTE Buff[
sizeof(KEY_VALUE_PARTIAL_INFORMATION) +
(WNBD_MAX_NAME_LENGTH * sizeof(WCHAR))] = { 0 };
PKEY_VALUE_PARTIAL_INFORMATION ValueInformation = (
PKEY_VALUE_PARTIAL_INFORMATION) Buff;
UNICODE_STRING KeyName = { 0 };
RtlInitUnicodeString(&KeyName, Name);
ULONG RequiredBufferSize = 0;
NTSTATUS Status = ZwQueryValueKey(
GlobalDrvRegHandle, &KeyName, KeyValuePartialInformation,
ValueInformation,
sizeof(Buff), &RequiredBufferSize);
if (Status) {
WNBD_LOG_WARN(
"Couldn't retrieve registry key: %ls. Status: %d",
Name, Status);
return Status;
}
if (WnbdOptRegType(Option->Type) != ValueInformation->Type) {
WNBD_LOG_WARN(
"Registry value type mismatch: %ls. "
"Expecting: %d, retrieved: %d",
Name, Option->Type, ValueInformation->Type);
return STATUS_OBJECT_TYPE_MISMATCH;
}
switch(Option->Type) {
case WnbdOptBool:
// We're storing bool and int64 values as QWORD
if (ValueInformation->DataLength != sizeof(UINT64)) {
WNBD_LOG_WARN(
"Registry value size mismatch: %ls. "
"Expecting: %d, actual: %d",
Name, sizeof(UINT64), ValueInformation->DataLength);
return STATUS_OBJECT_TYPE_MISMATCH;
}
Value->Data.AsBool = !!*(PUINT64)ValueInformation->Data;
break;
case WnbdOptInt64:
if (ValueInformation->DataLength != sizeof(UINT64)) {
WNBD_LOG_WARN(
"Registry value size mismatch: %ls. "
"Expecting: %d, actual: %d",
Name, sizeof(UINT64), ValueInformation->DataLength);
return STATUS_OBJECT_TYPE_MISMATCH;
}
Value->Data.AsInt64 = *(PUINT64)ValueInformation->Data;
break;
case WnbdOptWstr:
if (ValueInformation->DataLength > sizeof(Value->Data.AsWstr)) {
WNBD_LOG_WARN(
"Registry value size overflow: %ls. "
"Maximum allowed: %d, actual: %d",
Name,
sizeof(Value->Data.AsWstr), ValueInformation->DataLength);
return STATUS_BUFFER_OVERFLOW;
}
RtlCopyMemory(Value->Data.AsWstr, ValueInformation->Data,
ValueInformation->DataLength);
break;
default:
WNBD_LOG_WARN(
"Unsupported option type: %d. "
"Key: %ls.", Option->Type, Name);
return STATUS_OBJECT_TYPE_MISMATCH;
}
WNBD_LOG_DEBUG("Retrieved persistent option: %ls.", Name);
Value->Type = Option->Type;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS WnbdGetDrvOpt(
PWCHAR Name,
PWNBD_OPTION_VALUE Value,
BOOLEAN Persistent)
{
if (Persistent) {
return WnbdGetPersistentOpt(Name, Value);
}
PWNBD_OPTION Option = WnbdFindOpt(Name);
if (!Option) {
WNBD_LOG_WARN("Could not find option: %ls.", Name);
return STATUS_OBJECT_NAME_NOT_FOUND;
}
*Value = Option->Value;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS WnbdProcessOptionValue(
PWNBD_OPTION Option,
PWNBD_OPTION_VALUE Value)
{
if (Value->Type == WnbdOptWstr) {
// Ensure that the string is NULL terminated.
Value->Data.AsWstr[WNBD_MAX_NAME_LENGTH - 1] = L'\0';
}
NTSTATUS Status = STATUS_SUCCESS;
if (Value->Type != Option->Type) {
// We'll try to convert strings, rejecting other
// type mismatches.
if (Value->Type != WnbdOptWstr) {
return STATUS_OBJECT_TYPE_MISMATCH;
}
WNBD_OPTION_VALUE ConvertedValue = { .Type = Option->Type };
UNICODE_STRING StringBuff = {
0,
sizeof(Option->Value),
(PWCHAR) &Value->Data.AsWstr};
switch (Option->Type) {
case WnbdOptBool:
Status = WstrToBool(Value->Data.AsWstr, &ConvertedValue.Data.AsBool);
break;
case WnbdOptInt64:
Status = RtlUnicodeStringToInt64(
&StringBuff, 0,
&ConvertedValue.Data.AsInt64, NULL);
break;
default:
WNBD_LOG_WARN("Unsupported option type: %d.", Option->Type);
Status = STATUS_OBJECT_TYPE_MISMATCH;
break;
}
if (!Status) {
*Value = ConvertedValue;
}
}
return Status;
}
_Use_decl_annotations_
NTSTATUS WnbdSetDrvOpt(
PWCHAR Name,
PWNBD_OPTION_VALUE Value,
BOOLEAN Persistent)
{
PWNBD_OPTION Option = WnbdFindOpt(Name);
if (!Option) {
WNBD_LOG_WARN("Could not find option: %ls.", Name);
return STATUS_OBJECT_NAME_NOT_FOUND;
}
NTSTATUS Status = WnbdProcessOptionValue(Option, Value);
if (Status) {
return Status;
}
// We'll set the persistent value first. If that fails,
// we won't set the runtime value.
if (Persistent) {
UNICODE_STRING KeyName = { 0 };
RtlInitUnicodeString(&KeyName, Name);
Status = ZwSetValueKey(
GlobalDrvRegHandle,
&KeyName,
0,
WnbdOptRegType(Option->Type),
(PVOID)&Value->Data,
WnbdOptRegSize(Option->Type));
if (Status) {
WNBD_LOG_ERROR("Couln't set registry key. Status: %d", Status);
return Status;
}
}
Option->Value = *Value;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS WnbdResetDrvOpt(
PWCHAR Name,
BOOLEAN Persistent)
{
PWNBD_OPTION Option = WnbdFindOpt(Name);
if (!Option) {
WNBD_LOG_WARN("Could not find opton: %ls.", Name);
return STATUS_OBJECT_NAME_NOT_FOUND;
}
if (Persistent) {
UNICODE_STRING KeyName = { 0 };
RtlInitUnicodeString(&KeyName, Name);
NTSTATUS Status = ZwDeleteValueKey(
GlobalDrvRegHandle,
&KeyName);
if (Status) {
WNBD_LOG_ERROR("Couln't remove registry key. Status: %d", Status);
return Status;
}
}
Option->Value = Option->Default;
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS WnbdReloadPersistentOptions()
{
DWORD Status = STATUS_SUCCESS;
for (DWORD i = 0; i < WnbdOptionsCount; i++) {
PWNBD_OPTION Option = &WnbdDriverOptions[i];
WNBD_OPTION_VALUE PersistentValue = { 0 };
Status = WnbdGetPersistentOpt(Option->Name, &PersistentValue);
if (Status) {
if (Status != STATUS_OBJECT_NAME_NOT_FOUND) {
WNBD_LOG_WARN("Could not load option %ls. Error: 0x%x",
Option->Name, Status);
}
continue;
}
Status = WnbdProcessOptionValue(Option, &PersistentValue);
if (Status) {
WNBD_LOG_WARN("Could not process option %ls. Error: 0x%x",
Option->Name, Status);
continue;
}
Option->Value = PersistentValue;
}
return Status;
}
_Use_decl_annotations_
NTSTATUS
WnbdListDrvOpt(
PWNBD_OPTION_LIST OptionList,
PULONG BufferSize,
BOOLEAN Persistent)
{
ASSERT(OptionList);
DWORD RequiredBuffSize =
sizeof(WNBD_OPTION) * WnbdOptionsCount + sizeof(WNBD_OPTION_LIST);
if (*BufferSize < RequiredBuffSize) {
*BufferSize = RequiredBuffSize;
return STATUS_BUFFER_TOO_SMALL;
}
OptionList->Count = 0;
for (DWORD i = 0; i < WnbdOptionsCount; i++) {
// When persistent options are requested, we'll only retrieve
// the ones that are currently set.
if (Persistent) {
WNBD_OPTION_VALUE PersistentValue = { 0 };
DWORD Status = WnbdGetPersistentOpt(
WnbdDriverOptions[i].Name,
&PersistentValue);
if (!Status) {
OptionList->Options[OptionList->Count] = WnbdDriverOptions[i];
OptionList->Options[OptionList->Count].Value = PersistentValue;
OptionList->Count += 1;
}
}
else {
OptionList->Options[i] = WnbdDriverOptions[i];
OptionList->Count += 1;
}
}
return STATUS_SUCCESS;
}
#pragma warning(pop)