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
|
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2021 Cisco Systems, Inc.
*/
#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <snort/snort.h>
static u8 *
format_snort_instance (u8 *s, va_list *args)
{
snort_instance_t *i = va_arg (*args, snort_instance_t *);
s = format (s, "%s [idx:%d sz:%d fd:%d]", i->name, i->index, i->shm_size,
i->shm_fd);
return s;
}
static clib_error_t *
snort_create_instance_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
unformat_input_t _line_input, *line_input = &_line_input;
clib_error_t *err = 0;
u8 *name = 0;
u32 queue_size = 1024;
u8 drop_on_diconnect = 1;
int rv = 0;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))
return 0;
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "queue-size %u", &queue_size))
;
else if (unformat (line_input, "on-disconnect drop"))
drop_on_diconnect = 1;
else if (unformat (line_input, "on-disconnect pass"))
drop_on_diconnect = 0;
else if (unformat (line_input, "name %s", &name))
;
else
{
err = clib_error_return (0, "unknown input `%U'",
format_unformat_error, input);
goto done;
}
}
if (!is_pow2 (queue_size))
{
err = clib_error_return (0, "Queue size must be a power of two");
goto done;
}
if (!name)
{
err = clib_error_return (0, "please specify instance name");
goto done;
}
rv = snort_instance_create (vm, (char *) name, min_log2 (queue_size),
drop_on_diconnect);
switch (rv)
{
case 0:
break;
case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
err = clib_error_return (0, "instance '%s' already exists", name);
break;
case VNET_API_ERROR_SYSCALL_ERROR_1:
err = clib_error_return (0, "memory fd failure: %U", format_clib_error,
clib_mem_get_last_error ());
break;
case VNET_API_ERROR_SYSCALL_ERROR_2:
err = clib_error_return (0, "ftruncate failure");
break;
case VNET_API_ERROR_SYSCALL_ERROR_3:
err = clib_error_return (0, "mmap failure");
break;
default:
err = clib_error_return (0, "snort_instance_create returned %d", rv);
break;
}
done:
vec_free (name);
unformat_free (line_input);
return err;
}
VLIB_CLI_COMMAND (snort_create_instance_command, static) = {
.path = "snort create-instance",
.short_help = "snort create-instaince name <name> [queue-size <size>] "
"[on-disconnect drop|pass]",
.function = snort_create_instance_command_fn,
};
static clib_error_t *
snort_disconnect_instance_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
unformat_input_t _line_input, *line_input = &_line_input;
clib_error_t *err = 0;
u8 *name = 0;
snort_instance_t *si;
int rv = 0;
if (!unformat_user (input, unformat_line_input, line_input))
return clib_error_return (0, "please specify instance name");
if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
unformat (line_input, "%s", &name);
if (!name)
{
err = clib_error_return (0, "please specify instance name");
goto done;
}
si = snort_get_instance_by_name ((char *) name);
if (!si)
rv = VNET_API_ERROR_NO_SUCH_ENTRY;
else
rv = snort_instance_disconnect (vm, si->index);
switch (rv)
{
case 0:
break;
case VNET_API_ERROR_NO_SUCH_ENTRY:
err = clib_error_return (0, "unknown instance '%s'", name);
break;
case VNET_API_ERROR_FEATURE_DISABLED:
err = clib_error_return (0, "instance '%s' is not connected", name);
break;
case VNET_API_ERROR_INVALID_VALUE:
err = clib_error_return (0, "failed to disconnect a broken client");
break;
default:
err = clib_error_return (0, "snort_instance_disconnect returned %d", rv);
break;
}
done:
vec_free (name);
unformat_free (line_input);
return err;
}
VLIB_CLI_COMMAND (snort_disconnect_instance_command, static) = {
.path = "snort disconnect instance",
.short_help = "snort disconnect instance <name>",
.function = snort_disconnect_instance_command_fn,
};
static clib_error_t *
snort_delete_instance_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
unformat_input_t _line_input, *line_input = &_line_input;
clib_error_t *err = 0;
u8 *name = 0;
int rv = 0;
if (!unformat_user (input, unformat_line_input, line_input))
return clib_error_return (0, "please specify instance name");
if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
unformat (line_input, "%s", &name);
if (!name)
{
err = clib_error_return (0, "please specify instance name");
goto done;
}
snort_instance_t *si = snort_get_instance_by_name ((char *) name);
if (!si)
err = clib_error_return (0, "unknown instance '%s' requested", name);
else
rv = snort_instance_delete (vm, si->index);
switch (rv)
{
case 0:
break;
case VNET_API_ERROR_NO_SUCH_ENTRY:
err = clib_error_return (0, "instance '%s' deletion failure", name);
break;
case VNET_API_ERROR_INSTANCE_IN_USE:
err = clib_error_return (0, "instance '%s' has connected client", name);
break;
default:
err = clib_error_return (0, "snort_instance_delete returned %d", rv);
break;
}
done:
vec_free (name);
unformat_free (line_input);
return err;
}
VLIB_CLI_COMMAND (snort_delete_instance_command, static) = {
.path = "snort delete instance",
.short_help = "snort delete instance <name>",
.function = snort_delete_instance_command_fn,
};
static clib_error_t *
snort_attach_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
unformat_input_t _line_input, *line_input = &_line_input;
vnet_main_t *vnm = vnet_get_main ();
clib_error_t *err = 0;
u8 *name = 0;
u32 sw_if_index = ~0;
snort_attach_dir_t dir = SNORT_INOUT;
int rv = 0;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))
return 0;
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
vnm, &sw_if_index))
;
else if (unformat (line_input, "instance %s", &name))
;
else if (unformat (line_input, "input"))
dir = SNORT_INPUT;
else if (unformat (line_input, "output"))
dir = SNORT_OUTPUT;
else if (unformat (line_input, "inout"))
dir = SNORT_INOUT;
else
{
err = clib_error_return (0, "unknown input `%U'",
format_unformat_error, input);
goto done;
}
}
if (sw_if_index == ~0)
{
err = clib_error_return (0, "please specify interface");
goto done;
}
if (!name)
{
err = clib_error_return (0, "please specify instance name");
goto done;
}
rv = snort_interface_enable_disable (vm, (char *) name, sw_if_index, 1, dir);
switch (rv)
{
case 0:
break;
case VNET_API_ERROR_FEATURE_ALREADY_ENABLED:
/* already attached to same instance */
break;
case VNET_API_ERROR_INSTANCE_IN_USE:
err = clib_error_return (0,
"interface %U already assigned to "
"an instance",
format_vnet_sw_if_index_name, vnm, sw_if_index);
break;
case VNET_API_ERROR_NO_SUCH_ENTRY:
err = clib_error_return (0, "unknown instance '%s'", name);
break;
default:
err = clib_error_return (0, "snort_interface_enable_disable returned %d",
rv);
break;
}
done:
vec_free (name);
unformat_free (line_input);
return err;
}
VLIB_CLI_COMMAND (snort_attach_command, static) = {
.path = "snort attach",
.short_help = "snort attach instance <name> interface <if-name> "
"[input|ouput|inout]",
.function = snort_attach_command_fn,
};
static clib_error_t *
snort_detach_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
unformat_input_t _line_input, *line_input = &_line_input;
vnet_main_t *vnm = vnet_get_main ();
clib_error_t *err = 0;
u32 sw_if_index = ~0;
int rv = 0;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))
return 0;
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
vnm, &sw_if_index))
;
else
{
err = clib_error_return (0, "unknown input `%U'",
format_unformat_error, input);
goto done;
}
}
if (sw_if_index == ~0)
{
err = clib_error_return (0, "please specify interface");
goto done;
}
rv = snort_interface_enable_disable (vm, 0, sw_if_index, 0, SNORT_INOUT);
switch (rv)
{
case 0:
break;
case VNET_API_ERROR_INVALID_INTERFACE:
err = clib_error_return (0,
"interface %U is not assigned to snort "
"instance!",
format_vnet_sw_if_index_name, vnm, sw_if_index);
break;
default:
err = clib_error_return (0, "snort_interface_enable_disable returned %d",
rv);
break;
}
done:
unformat_free (line_input);
return err;
}
VLIB_CLI_COMMAND (snort_detach_command, static) = {
.path = "snort detach",
.short_help = "snort detach interface <if-name>",
.function = snort_detach_command_fn,
};
static clib_error_t *
snort_show_instances_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_main_t *sm = &snort_main;
snort_instance_t *si;
pool_foreach (si, sm->instances)
vlib_cli_output (vm, "%U", format_snort_instance, si);
return 0;
}
VLIB_CLI_COMMAND (snort_show_instances_command, static) = {
.path = "show snort instances",
.short_help = "show snort instances",
.function = snort_show_instances_command_fn,
};
static clib_error_t *
snort_show_interfaces_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_main_t *sm = &snort_main;
vnet_main_t *vnm = vnet_get_main ();
snort_instance_t *si;
u32 *index;
vlib_cli_output (vm, "interface\t\tsnort instance");
vec_foreach (index, sm->instance_by_sw_if_index)
{
if (index[0] != ~0)
{
si = vec_elt_at_index (sm->instances, index[0]);
vlib_cli_output (vm, "%U:\t%s", format_vnet_sw_if_index_name, vnm,
index - sm->instance_by_sw_if_index, si->name);
}
}
return 0;
}
VLIB_CLI_COMMAND (snort_show_interfaces_command, static) = {
.path = "show snort interfaces",
.short_help = "show snort interfaces",
.function = snort_show_interfaces_command_fn,
};
static clib_error_t *
snort_show_clients_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_main_t *sm = &snort_main;
u32 n_clients = pool_elts (sm->clients);
snort_client_t *c;
snort_instance_t *si;
vlib_cli_output (vm, "number of clients: %d", n_clients);
if (n_clients)
vlib_cli_output (vm, "client snort instance");
pool_foreach (c, sm->clients)
{
si = vec_elt_at_index (sm->instances, c->instance_index);
vlib_cli_output (vm, "%6d %s", c - sm->clients, si->name);
}
return 0;
}
VLIB_CLI_COMMAND (snort_show_clients_command, static) = {
.path = "show snort clients",
.short_help = "show snort clients",
.function = snort_show_clients_command_fn,
};
static clib_error_t *
snort_mode_polling_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_set_node_mode (vm, VLIB_NODE_STATE_POLLING);
return 0;
}
static clib_error_t *
snort_mode_interrupt_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_set_node_mode (vm, VLIB_NODE_STATE_INTERRUPT);
return 0;
}
VLIB_CLI_COMMAND (snort_mode_polling_command, static) = {
.path = "snort mode polling",
.short_help = "snort mode polling|interrupt",
.function = snort_mode_polling_command_fn,
};
VLIB_CLI_COMMAND (snort_mode_interrupt_command, static) = {
.path = "snort mode interrupt",
.short_help = "snort mode polling|interrupt",
.function = snort_mode_interrupt_command_fn,
};
static clib_error_t *
snort_show_mode_command_fn (vlib_main_t *vm, unformat_input_t *input,
vlib_cli_command_t *cmd)
{
snort_main_t *sm = &snort_main;
char *mode =
sm->input_mode == VLIB_NODE_STATE_POLLING ? "polling" : "interrupt";
vlib_cli_output (vm, "input mode: %s", mode);
return 0;
}
VLIB_CLI_COMMAND (snort_show_mode_command, static) = {
.path = "show snort mode",
.short_help = "show snort mode",
.function = snort_show_mode_command_fn,
};
|