Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0
2 : * Copyright(c) 2021 Cisco Systems, Inc.
3 : */
4 :
5 : #include <vlib/vlib.h>
6 : #include <vnet/vnet.h>
7 : #include <snort/snort.h>
8 :
9 : static u8 *
10 0 : format_snort_instance (u8 *s, va_list *args)
11 : {
12 0 : snort_instance_t *i = va_arg (*args, snort_instance_t *);
13 0 : s = format (s, "%s [idx:%d sz:%d fd:%d]", i->name, i->index, i->shm_size,
14 : i->shm_fd);
15 :
16 0 : return s;
17 : }
18 :
19 : static clib_error_t *
20 0 : snort_create_instance_command_fn (vlib_main_t *vm, unformat_input_t *input,
21 : vlib_cli_command_t *cmd)
22 : {
23 0 : unformat_input_t _line_input, *line_input = &_line_input;
24 0 : clib_error_t *err = 0;
25 0 : u8 *name = 0;
26 0 : u32 queue_size = 1024;
27 0 : u8 drop_on_diconnect = 1;
28 :
29 : /* Get a line of input. */
30 0 : if (!unformat_user (input, unformat_line_input, line_input))
31 0 : return 0;
32 :
33 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
34 : {
35 0 : if (unformat (line_input, "queue-size %u", &queue_size))
36 : ;
37 0 : else if (unformat (line_input, "on-disconnect drop"))
38 0 : drop_on_diconnect = 1;
39 0 : else if (unformat (line_input, "on-disconnect pass"))
40 0 : drop_on_diconnect = 0;
41 0 : else if (unformat (line_input, "name %s", &name))
42 : ;
43 : else
44 : {
45 0 : err = clib_error_return (0, "unknown input `%U'",
46 : format_unformat_error, input);
47 0 : goto done;
48 : }
49 : }
50 :
51 0 : if (!is_pow2 (queue_size))
52 : {
53 0 : err = clib_error_return (0, "Queue size must be a power of two");
54 0 : goto done;
55 : }
56 :
57 0 : if (!name)
58 : {
59 0 : err = clib_error_return (0, "please specify instance name");
60 0 : goto done;
61 : }
62 :
63 0 : err = snort_instance_create (vm, (char *) name, min_log2 (queue_size),
64 : drop_on_diconnect);
65 :
66 0 : done:
67 0 : vec_free (name);
68 0 : unformat_free (line_input);
69 0 : return err;
70 : }
71 :
72 39759 : VLIB_CLI_COMMAND (snort_create_instance_command, static) = {
73 : .path = "snort create-instance",
74 : .short_help = "snort create-instaince name <name> [queue-size <size>] "
75 : "[on-disconnect drop|pass]",
76 : .function = snort_create_instance_command_fn,
77 : };
78 :
79 : static clib_error_t *
80 0 : snort_attach_command_fn (vlib_main_t *vm, unformat_input_t *input,
81 : vlib_cli_command_t *cmd)
82 : {
83 0 : unformat_input_t _line_input, *line_input = &_line_input;
84 0 : vnet_main_t *vnm = vnet_get_main ();
85 0 : clib_error_t *err = 0;
86 0 : u8 *name = 0;
87 0 : u32 sw_if_index = ~0;
88 0 : snort_attach_dir_t dir = SNORT_INOUT;
89 :
90 : /* Get a line of input. */
91 0 : if (!unformat_user (input, unformat_line_input, line_input))
92 0 : return 0;
93 :
94 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
95 : {
96 0 : if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
97 : vnm, &sw_if_index))
98 : ;
99 0 : else if (unformat (line_input, "instance %s", &name))
100 : ;
101 0 : else if (unformat (line_input, "input"))
102 0 : dir = SNORT_INPUT;
103 0 : else if (unformat (line_input, "output"))
104 0 : dir = SNORT_OUTPUT;
105 0 : else if (unformat (line_input, "inout"))
106 0 : dir = SNORT_INOUT;
107 : else
108 : {
109 0 : err = clib_error_return (0, "unknown input `%U'",
110 : format_unformat_error, input);
111 0 : goto done;
112 : }
113 : }
114 :
115 0 : if (sw_if_index == ~0)
116 : {
117 0 : err = clib_error_return (0, "please specify interface");
118 0 : goto done;
119 : }
120 :
121 0 : if (!name)
122 : {
123 0 : err = clib_error_return (0, "please specify instance name");
124 0 : goto done;
125 : }
126 :
127 : err =
128 0 : snort_interface_enable_disable (vm, (char *) name, sw_if_index, 1, dir);
129 :
130 0 : done:
131 0 : vec_free (name);
132 0 : unformat_free (line_input);
133 0 : return err;
134 : }
135 :
136 39759 : VLIB_CLI_COMMAND (snort_attach_command, static) = {
137 : .path = "snort attach",
138 : .short_help = "snort attach instance <name> interface <if-name> "
139 : "[input|ouput|inout]",
140 : .function = snort_attach_command_fn,
141 : };
142 :
143 : static clib_error_t *
144 0 : snort_detach_command_fn (vlib_main_t *vm, unformat_input_t *input,
145 : vlib_cli_command_t *cmd)
146 : {
147 0 : unformat_input_t _line_input, *line_input = &_line_input;
148 0 : vnet_main_t *vnm = vnet_get_main ();
149 0 : clib_error_t *err = 0;
150 0 : u32 sw_if_index = ~0;
151 :
152 : /* Get a line of input. */
153 0 : if (!unformat_user (input, unformat_line_input, line_input))
154 0 : return 0;
155 :
156 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
157 : {
158 0 : if (unformat (line_input, "interface %U", unformat_vnet_sw_interface,
159 : vnm, &sw_if_index))
160 : ;
161 : else
162 : {
163 0 : err = clib_error_return (0, "unknown input `%U'",
164 : format_unformat_error, input);
165 0 : goto done;
166 : }
167 : }
168 :
169 0 : if (sw_if_index == ~0)
170 : {
171 0 : err = clib_error_return (0, "please specify interface");
172 0 : goto done;
173 : }
174 :
175 0 : err = snort_interface_enable_disable (vm, 0, sw_if_index, 0, SNORT_INOUT);
176 :
177 0 : done:
178 0 : unformat_free (line_input);
179 0 : return err;
180 : }
181 :
182 39759 : VLIB_CLI_COMMAND (snort_detach_command, static) = {
183 : .path = "snort detach",
184 : .short_help = "snort detach interface <if-name>",
185 : .function = snort_detach_command_fn,
186 : };
187 :
188 : static clib_error_t *
189 0 : snort_show_instances_command_fn (vlib_main_t *vm, unformat_input_t *input,
190 : vlib_cli_command_t *cmd)
191 : {
192 0 : snort_main_t *sm = &snort_main;
193 : snort_instance_t *si;
194 :
195 0 : pool_foreach (si, sm->instances)
196 0 : vlib_cli_output (vm, "%U", format_snort_instance, si);
197 :
198 0 : return 0;
199 : }
200 :
201 39759 : VLIB_CLI_COMMAND (snort_show_instances_command, static) = {
202 : .path = "show snort instances",
203 : .short_help = "show snort instances",
204 : .function = snort_show_instances_command_fn,
205 : };
206 :
207 : static clib_error_t *
208 0 : snort_show_interfaces_command_fn (vlib_main_t *vm, unformat_input_t *input,
209 : vlib_cli_command_t *cmd)
210 : {
211 0 : snort_main_t *sm = &snort_main;
212 0 : vnet_main_t *vnm = vnet_get_main ();
213 : snort_instance_t *si;
214 : u32 *index;
215 :
216 0 : vlib_cli_output (vm, "interface\tsnort instance");
217 0 : vec_foreach (index, sm->instance_by_sw_if_index)
218 : {
219 0 : if (index[0] != ~0)
220 : {
221 0 : si = vec_elt_at_index (sm->instances, index[0]);
222 0 : vlib_cli_output (vm, "%U:\t%s", format_vnet_sw_if_index_name, vnm,
223 0 : index - sm->instance_by_sw_if_index, si->name);
224 : }
225 : }
226 0 : return 0;
227 : }
228 :
229 39759 : VLIB_CLI_COMMAND (snort_show_interfaces_command, static) = {
230 : .path = "show snort interfaces",
231 : .short_help = "show snort interfaces",
232 : .function = snort_show_interfaces_command_fn,
233 : };
234 :
235 : static clib_error_t *
236 0 : snort_show_clients_command_fn (vlib_main_t *vm, unformat_input_t *input,
237 : vlib_cli_command_t *cmd)
238 : {
239 0 : snort_main_t *sm = &snort_main;
240 0 : vlib_cli_output (vm, "number of clients: %d", pool_elts (sm->clients));
241 0 : return 0;
242 : }
243 :
244 39759 : VLIB_CLI_COMMAND (snort_show_clients_command, static) = {
245 : .path = "show snort clients",
246 : .short_help = "show snort clients",
247 : .function = snort_show_clients_command_fn,
248 : };
249 :
250 : static clib_error_t *
251 0 : snort_mode_polling_command_fn (vlib_main_t *vm, unformat_input_t *input,
252 : vlib_cli_command_t *cmd)
253 : {
254 0 : return snort_set_node_mode (vm, VLIB_NODE_STATE_POLLING);
255 : }
256 :
257 : static clib_error_t *
258 0 : snort_mode_interrupt_command_fn (vlib_main_t *vm, unformat_input_t *input,
259 : vlib_cli_command_t *cmd)
260 : {
261 0 : return snort_set_node_mode (vm, VLIB_NODE_STATE_INTERRUPT);
262 : }
263 :
264 39759 : VLIB_CLI_COMMAND (snort_mode_polling_command, static) = {
265 : .path = "snort mode polling",
266 : .short_help = "snort mode polling|interrupt",
267 : .function = snort_mode_polling_command_fn,
268 : };
269 :
270 39759 : VLIB_CLI_COMMAND (snort_mode_interrupt_command, static) = {
271 : .path = "snort mode interrupt",
272 : .short_help = "snort mode polling|interrupt",
273 : .function = snort_mode_interrupt_command_fn,
274 : };
275 :
276 : static clib_error_t *
277 0 : snort_show_mode_command_fn (vlib_main_t *vm, unformat_input_t *input,
278 : vlib_cli_command_t *cmd)
279 : {
280 0 : snort_main_t *sm = &snort_main;
281 0 : char *mode =
282 0 : sm->input_mode == VLIB_NODE_STATE_POLLING ? "polling" : "interrupt";
283 0 : vlib_cli_output (vm, "input mode: %s", mode);
284 0 : return 0;
285 : }
286 :
287 39759 : VLIB_CLI_COMMAND (snort_show_mode_command, static) = {
288 : .path = "show snort mode",
289 : .short_help = "show snort mode",
290 : .function = snort_show_mode_command_fn,
291 : };
|