blob: af7651393328378cd189861adc6b05005e2a903a [file] [log] [blame]
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +01001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <vnet/ip/ip.h>
16#include <vnet/classify/vnet_classify.h>
17#include <vnet/classify/in_out_acl.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070018#include <vnet/l2/l2_output.h>
19#include <vnet/l2/l2_input.h>
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010020
21in_out_acl_main_t in_out_acl_main;
22
23static int
Benoît Ganneabb2a422021-09-30 13:41:00 +020024vnet_in_out_acl_feature_enable (in_out_acl_main_t *am, u32 sw_if_index,
25 in_out_acl_table_id_t tid, int feature_enable,
26 int is_output)
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010027{
Benoît Ganneabb2a422021-09-30 13:41:00 +020028 const char *arc_name, *feature_name;
29 vnet_feature_config_main_t *fcm;
30 u8 arc;
31 int rv;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010032
Benoît Ganneabb2a422021-09-30 13:41:00 +020033 switch (tid)
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010034 {
Benoît Ganneabb2a422021-09-30 13:41:00 +020035 case IN_OUT_ACL_N_TABLES:
36 return VNET_API_ERROR_NO_SUCH_TABLE;
37 case IN_OUT_ACL_TABLE_L2:
Andrew Yourtchenko08118f02018-02-08 21:45:08 +010038 if (is_output)
39 l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_ACL,
40 feature_enable);
41 else
42 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ACL,
43 feature_enable);
Benoît Ganneabb2a422021-09-30 13:41:00 +020044 return 0;
45 case IN_OUT_ACL_TABLE_IP4:
46 arc_name = is_output ? "ip4-output" : "ip4-unicast";
47 feature_name = is_output ? "ip4-outacl" : "ip4-inacl";
48 break;
49 case IN_OUT_ACL_TABLE_IP6:
50 arc_name = is_output ? "ip6-output" : "ip6-unicast";
51 feature_name = is_output ? "ip6-outacl" : "ip6-inacl";
52 break;
53 case IN_OUT_ACL_TABLE_IP4_PUNT:
54 if (sw_if_index != 0)
55 return VNET_API_ERROR_INVALID_INTERFACE;
56 arc_name = "ip4-punt";
57 feature_name = "ip4-punt-acl";
58 break;
59 case IN_OUT_ACL_TABLE_IP6_PUNT:
60 if (sw_if_index != 0)
61 return VNET_API_ERROR_INVALID_INTERFACE;
62 arc_name = "ip6-punt";
63 feature_name = "ip6-punt-acl";
64 break;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010065 }
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010066
Benoît Ganneabb2a422021-09-30 13:41:00 +020067 rv = vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
68 feature_enable, 0, 0);
69 if (rv)
70 return rv;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010071
Benoît Ganneabb2a422021-09-30 13:41:00 +020072 arc = vnet_get_feature_arc_index (arc_name);
73 fcm = vnet_get_feature_arc_config_main (arc);
74 am->vnet_config_main[is_output][tid] = &fcm->config_main;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010075
76 return 0;
77}
78
79int
Benoît Ganneabb2a422021-09-30 13:41:00 +020080vnet_set_in_out_acl_intfc (vlib_main_t *vm, u32 sw_if_index,
81 u32 ip4_table_index, u32 ip6_table_index,
82 u32 l2_table_index, u32 ip4_punt_table_index,
83 u32 ip6_punt_table_index, u32 is_add, u32 is_output)
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010084{
85 in_out_acl_main_t *am = &in_out_acl_main;
86 vnet_classify_main_t *vcm = am->vnet_classify_main;
Benoît Ganneabb2a422021-09-30 13:41:00 +020087 u32 acl[IN_OUT_ACL_N_TABLES] = {
88 ip4_table_index, ip6_table_index, l2_table_index,
89 ip4_punt_table_index, ip6_punt_table_index,
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010090 };
91 u32 ti;
Benoît Ganneabb2a422021-09-30 13:41:00 +020092 int rv;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +010093
94 /* Assume that we've validated sw_if_index in the API layer */
95
96 for (ti = 0; ti < IN_OUT_ACL_N_TABLES; ti++)
97 {
98 if (acl[ti] == ~0)
99 continue;
100
101 if (pool_is_free_index (vcm->tables, acl[ti]))
102 return VNET_API_ERROR_NO_SUCH_TABLE;
103
104 vec_validate_init_empty
105 (am->classify_table_index_by_sw_if_index[is_output][ti], sw_if_index,
106 ~0);
107
108 /* Reject any DEL operation with wrong sw_if_index */
109 if (!is_add &&
110 (acl[ti] !=
111 am->classify_table_index_by_sw_if_index[is_output][ti]
112 [sw_if_index]))
113 {
114 clib_warning
115 ("Non-existent intf_idx=%d with table_index=%d for delete",
116 sw_if_index, acl[ti]);
117 return VNET_API_ERROR_NO_SUCH_TABLE;
118 }
119
120 /* Return ok on ADD operaton if feature is already enabled */
121 if (is_add &&
122 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index]
123 != ~0)
124 return 0;
125
Benoît Ganneabb2a422021-09-30 13:41:00 +0200126 rv = vnet_in_out_acl_feature_enable (am, sw_if_index, ti, is_add,
127 is_output);
128 if (rv)
129 return rv;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100130
131 if (is_add)
132 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
133 acl[ti];
134 else
135 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
136 ~0;
137 }
138
139 return 0;
140}
141
142int
143vnet_set_input_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
144 u32 ip4_table_index,
145 u32 ip6_table_index, u32 l2_table_index, u32 is_add)
146{
Benoît Ganneabb2a422021-09-30 13:41:00 +0200147 return vnet_set_in_out_acl_intfc (
148 vm, sw_if_index, ip4_table_index, ip6_table_index, l2_table_index,
149 ~0 /* ip4_punt_table_index */, ~0 /* ip6_punt_table_index */, is_add,
150 IN_OUT_ACL_INPUT_TABLE_GROUP);
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100151}
152
153int
154vnet_set_output_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
155 u32 ip4_table_index,
156 u32 ip6_table_index, u32 l2_table_index,
157 u32 is_add)
158{
Benoît Ganneabb2a422021-09-30 13:41:00 +0200159 return vnet_set_in_out_acl_intfc (
160 vm, sw_if_index, ip4_table_index, ip6_table_index, l2_table_index,
161 ~0 /* ip4_punt_table_index */, ~0 /* ip6_punt_table_index */, is_add,
162 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100163}
164
165static clib_error_t *
166set_in_out_acl_command_fn (vlib_main_t * vm,
167 unformat_input_t * input, vlib_cli_command_t * cmd,
168 u32 is_output)
169{
170 vnet_main_t *vnm = vnet_get_main ();
171 u32 sw_if_index = ~0;
172 u32 ip4_table_index = ~0;
173 u32 ip6_table_index = ~0;
Benoît Ganneabb2a422021-09-30 13:41:00 +0200174 u32 ip4_punt_table_index = ~0;
175 u32 ip6_punt_table_index = ~0;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100176 u32 l2_table_index = ~0;
177 u32 is_add = 1;
178 u32 idx_cnt = 0;
179 int rv;
180
181 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
182 {
183 if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
184 vnm, &sw_if_index))
185 ;
186 else if (unformat (input, "ip4-table %d", &ip4_table_index))
187 idx_cnt++;
188 else if (unformat (input, "ip6-table %d", &ip6_table_index))
189 idx_cnt++;
Benoît Ganneabb2a422021-09-30 13:41:00 +0200190 else if (unformat (input, "ip4-punt-table %d", &ip4_punt_table_index))
191 idx_cnt++;
192 else if (unformat (input, "ip6-punt-table %d", &ip6_punt_table_index))
193 idx_cnt++;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100194 else if (unformat (input, "l2-table %d", &l2_table_index))
195 idx_cnt++;
196 else if (unformat (input, "del"))
197 is_add = 0;
198 else
199 break;
200 }
201
202 if (sw_if_index == ~0)
203 return clib_error_return (0, "Interface must be specified.");
204
205 if (!idx_cnt)
206 return clib_error_return (0, "Table index should be specified.");
207
208 if (idx_cnt > 1)
209 return clib_error_return (0, "Only one table index per API is allowed.");
210
Benoît Ganneabb2a422021-09-30 13:41:00 +0200211 rv = vnet_set_in_out_acl_intfc (
212 vm, sw_if_index, ip4_table_index, ip6_table_index, l2_table_index,
213 ip4_punt_table_index, ip6_punt_table_index, is_add, is_output);
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100214
215 switch (rv)
216 {
217 case 0:
218 break;
219
220 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
221 return clib_error_return (0, "No such interface");
222
223 case VNET_API_ERROR_NO_SUCH_ENTRY:
224 return clib_error_return (0, "No such classifier table");
Benoît Ganneabb2a422021-09-30 13:41:00 +0200225
226 default:
227 return clib_error_return (0, "Error: %d", rv);
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100228 }
229 return 0;
230}
231
232static clib_error_t *
233set_input_acl_command_fn (vlib_main_t * vm,
234 unformat_input_t * input, vlib_cli_command_t * cmd)
235{
236 return set_in_out_acl_command_fn (vm, input, cmd,
237 IN_OUT_ACL_INPUT_TABLE_GROUP);
238}
239
240static clib_error_t *
241set_output_acl_command_fn (vlib_main_t * vm,
242 unformat_input_t * input, vlib_cli_command_t * cmd)
243{
244 return set_in_out_acl_command_fn (vm, input, cmd,
245 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
246}
247
248/*
249 * Configure interface to enable/disble input/output ACL features:
250 * intfc - interface name to be configured as input ACL
251 * Ip4-table <index> [del] - enable/disable IP4 input ACL
252 * Ip6-table <index> [del] - enable/disable IP6 input ACL
253 * l2-table <index> [del] - enable/disable Layer2 input ACL
254 *
255 * Note: Only one table index per API call is allowed.
256 *
257 */
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100258VLIB_CLI_COMMAND (set_input_acl_command, static) = {
Benoît Ganneabb2a422021-09-30 13:41:00 +0200259 .path = "set interface input acl",
260 .short_help =
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100261 "set interface input acl intfc <int> [ip4-table <index>]\n"
Benoît Ganneabb2a422021-09-30 13:41:00 +0200262 " [ip6-table <index>] [l2-table <index>] [ip4-punt-table <index>]\n"
263 " [ip6-punt-table <index> [del]",
264 .function = set_input_acl_command_fn,
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100265};
266VLIB_CLI_COMMAND (set_output_acl_command, static) = {
267 .path = "set interface output acl",
268 .short_help =
269 "set interface output acl intfc <int> [ip4-table <index>]\n"
270 " [ip6-table <index>] [l2-table <index>] [del]",
271 .function = set_output_acl_command_fn,
272};
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100273
274clib_error_t *
275in_out_acl_init (vlib_main_t * vm)
276{
277 in_out_acl_main_t *am = &in_out_acl_main;
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100278
279 am->vlib_main = vm;
280 am->vnet_main = vnet_get_main ();
281 am->vnet_classify_main = &vnet_classify_main;
282
283 return 0;
284}
Dave Barachf8d50682019-05-14 18:01:44 -0400285VLIB_INIT_FUNCTION (in_out_acl_init) =
286{
287 .runs_after = VLIB_INITS("ip_in_out_acl_init"),
288};
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100289
290uword
291unformat_acl_type (unformat_input_t * input, va_list * args)
292{
293 u32 *acl_type = va_arg (*args, u32 *);
294 u32 tid = IN_OUT_ACL_N_TABLES;
295
296 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
297 {
298 if (unformat (input, "ip4"))
299 tid = IN_OUT_ACL_TABLE_IP4;
300 else if (unformat (input, "ip6"))
301 tid = IN_OUT_ACL_TABLE_IP6;
302 else if (unformat (input, "l2"))
303 tid = IN_OUT_ACL_TABLE_L2;
304 else
305 break;
306 }
307
308 *acl_type = tid;
309 return 1;
310}
311
312u8 *
313format_vnet_in_out_acl_info (u8 * s, va_list * va)
314{
315 in_out_acl_main_t *am = va_arg (*va, in_out_acl_main_t *);
316 int sw_if_idx = va_arg (*va, int);
317 u32 tid = va_arg (*va, u32);
318
319 if (tid == ~0)
320 {
321 s = format (s, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
322 "Interface name");
323 return s;
324 }
325
326 s = format (s, "%10d%20d\t\t%U", sw_if_idx, tid,
327 format_vnet_sw_if_index_name, am->vnet_main, sw_if_idx);
328
329 return s;
330}
331
332static clib_error_t *
333show_in_out_acl_command_fn (vlib_main_t * vm,
334 unformat_input_t * input,
335 vlib_cli_command_t * cmd, u32 is_output)
336{
337 in_out_acl_main_t *am = &in_out_acl_main;
338 u32 type = IN_OUT_ACL_N_TABLES;
339 int i;
340 u32 *vec_tbl;
341
342 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
343 {
344 if (unformat (input, "type %U", unformat_acl_type, &type))
345 ;
346 else
347 break;
348 }
349
350 if (type == IN_OUT_ACL_N_TABLES)
351 return clib_error_return (0, is_output ? "Invalid output ACL table type."
352 : "Invalid input ACL table type.");
353
354 vec_tbl = am->classify_table_index_by_sw_if_index[is_output][type];
355
356 if (vec_len (vec_tbl))
357 vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info, am, ~0 /* hdr */ ,
358 ~0);
359 else
360 vlib_cli_output (vm, is_output ? "No output ACL tables configured"
361 : "No input ACL tables configured");
362
363 for (i = 0; i < vec_len (vec_tbl); i++)
364 {
365 if (vec_elt (vec_tbl, i) == ~0)
366 continue;
367
368 vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info,
369 am, i, vec_elt (vec_tbl, i));
370 }
371
372 return 0;
373}
374
375static clib_error_t *
376show_inacl_command_fn (vlib_main_t * vm,
377 unformat_input_t * input, vlib_cli_command_t * cmd)
378{
379 return show_in_out_acl_command_fn (vm, input, cmd,
380 IN_OUT_ACL_INPUT_TABLE_GROUP);
381}
382
383static clib_error_t *
384show_outacl_command_fn (vlib_main_t * vm,
385 unformat_input_t * input, vlib_cli_command_t * cmd)
386{
387 return show_in_out_acl_command_fn (vm, input, cmd,
388 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
389}
390
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100391VLIB_CLI_COMMAND (show_inacl_command, static) = {
392 .path = "show inacl",
393 .short_help = "show inacl type [ip4|ip6|l2]",
394 .function = show_inacl_command_fn,
395};
396VLIB_CLI_COMMAND (show_outacl_command, static) = {
397 .path = "show outacl",
398 .short_help = "show outacl type [ip4|ip6|l2]",
399 .function = show_outacl_command_fn,
400};
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100401
402/*
403 * fd.io coding-style-patch-verification: ON
404 *
405 * Local Variables:
406 * eval: (c-set-style "gnu")
407 * End:
408 */