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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/ip/ip.h>
16 #include <vnet/classify/vnet_classify.h>
17 #include <vnet/classify/in_out_acl.h>
18 #include <vnet/l2/l2_output.h>
19 #include <vnet/l2/l2_input.h>
21 in_out_acl_main_t in_out_acl_main;
24 vnet_in_out_acl_ip_feature_enable (vlib_main_t * vnm,
25 in_out_acl_main_t * am,
27 in_out_acl_table_id_t tid,
28 int feature_enable, int is_output)
31 if (tid == IN_OUT_ACL_TABLE_L2)
34 l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_ACL,
37 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ACL,
42 vnet_feature_config_main_t *fcm;
45 if (tid == IN_OUT_ACL_TABLE_IP4)
47 char *arc_name = is_output ? "ip4-output" : "ip4-unicast";
48 vnet_feature_enable_disable (arc_name,
49 is_output ? "ip4-outacl" : "ip4-inacl",
50 sw_if_index, feature_enable, 0, 0);
51 arc = vnet_get_feature_arc_index (arc_name);
55 char *arc_name = is_output ? "ip6-output" : "ip6-unicast";
56 vnet_feature_enable_disable (arc_name,
57 is_output ? "ip6-outacl" : "ip6-inacl",
58 sw_if_index, feature_enable, 0, 0);
59 arc = vnet_get_feature_arc_index (arc_name);
62 fcm = vnet_get_feature_arc_config_main (arc);
63 am->vnet_config_main[is_output][tid] = &fcm->config_main;
70 vnet_set_in_out_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
72 u32 ip6_table_index, u32 l2_table_index,
73 u32 is_add, u32 is_output)
75 in_out_acl_main_t *am = &in_out_acl_main;
76 vnet_classify_main_t *vcm = am->vnet_classify_main;
77 u32 acl[IN_OUT_ACL_N_TABLES] = { ip4_table_index, ip6_table_index,
82 /* Assume that we've validated sw_if_index in the API layer */
84 for (ti = 0; ti < IN_OUT_ACL_N_TABLES; ti++)
89 if (pool_is_free_index (vcm->tables, acl[ti]))
90 return VNET_API_ERROR_NO_SUCH_TABLE;
92 vec_validate_init_empty
93 (am->classify_table_index_by_sw_if_index[is_output][ti], sw_if_index,
96 /* Reject any DEL operation with wrong sw_if_index */
99 am->classify_table_index_by_sw_if_index[is_output][ti]
103 ("Non-existent intf_idx=%d with table_index=%d for delete",
104 sw_if_index, acl[ti]);
105 return VNET_API_ERROR_NO_SUCH_TABLE;
108 /* Return ok on ADD operaton if feature is already enabled */
110 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index]
114 vnet_in_out_acl_ip_feature_enable (vm, am, sw_if_index, ti, is_add,
118 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
121 am->classify_table_index_by_sw_if_index[is_output][ti][sw_if_index] =
129 vnet_set_input_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
131 u32 ip6_table_index, u32 l2_table_index, u32 is_add)
133 return vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
134 ip6_table_index, l2_table_index, is_add,
135 IN_OUT_ACL_INPUT_TABLE_GROUP);
139 vnet_set_output_acl_intfc (vlib_main_t * vm, u32 sw_if_index,
141 u32 ip6_table_index, u32 l2_table_index,
144 return vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
145 ip6_table_index, l2_table_index, is_add,
146 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
149 static clib_error_t *
150 set_in_out_acl_command_fn (vlib_main_t * vm,
151 unformat_input_t * input, vlib_cli_command_t * cmd,
154 vnet_main_t *vnm = vnet_get_main ();
155 u32 sw_if_index = ~0;
156 u32 ip4_table_index = ~0;
157 u32 ip6_table_index = ~0;
158 u32 l2_table_index = ~0;
163 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
165 if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
168 else if (unformat (input, "ip4-table %d", &ip4_table_index))
170 else if (unformat (input, "ip6-table %d", &ip6_table_index))
172 else if (unformat (input, "l2-table %d", &l2_table_index))
174 else if (unformat (input, "del"))
180 if (sw_if_index == ~0)
181 return clib_error_return (0, "Interface must be specified.");
184 return clib_error_return (0, "Table index should be specified.");
187 return clib_error_return (0, "Only one table index per API is allowed.");
189 rv = vnet_set_in_out_acl_intfc (vm, sw_if_index, ip4_table_index,
190 ip6_table_index, l2_table_index, is_add,
198 case VNET_API_ERROR_NO_MATCHING_INTERFACE:
199 return clib_error_return (0, "No such interface");
201 case VNET_API_ERROR_NO_SUCH_ENTRY:
202 return clib_error_return (0, "No such classifier table");
207 static clib_error_t *
208 set_input_acl_command_fn (vlib_main_t * vm,
209 unformat_input_t * input, vlib_cli_command_t * cmd)
211 return set_in_out_acl_command_fn (vm, input, cmd,
212 IN_OUT_ACL_INPUT_TABLE_GROUP);
215 static clib_error_t *
216 set_output_acl_command_fn (vlib_main_t * vm,
217 unformat_input_t * input, vlib_cli_command_t * cmd)
219 return set_in_out_acl_command_fn (vm, input, cmd,
220 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
224 * Configure interface to enable/disble input/output ACL features:
225 * intfc - interface name to be configured as input ACL
226 * Ip4-table <index> [del] - enable/disable IP4 input ACL
227 * Ip6-table <index> [del] - enable/disable IP6 input ACL
228 * l2-table <index> [del] - enable/disable Layer2 input ACL
230 * Note: Only one table index per API call is allowed.
234 VLIB_CLI_COMMAND (set_input_acl_command, static) = {
235 .path = "set interface input acl",
237 "set interface input acl intfc <int> [ip4-table <index>]\n"
238 " [ip6-table <index>] [l2-table <index>] [del]",
239 .function = set_input_acl_command_fn,
241 VLIB_CLI_COMMAND (set_output_acl_command, static) = {
242 .path = "set interface output acl",
244 "set interface output acl intfc <int> [ip4-table <index>]\n"
245 " [ip6-table <index>] [l2-table <index>] [del]",
246 .function = set_output_acl_command_fn,
251 in_out_acl_init (vlib_main_t * vm)
253 in_out_acl_main_t *am = &in_out_acl_main;
256 am->vnet_main = vnet_get_main ();
257 am->vnet_classify_main = &vnet_classify_main;
262 VLIB_INIT_FUNCTION (in_out_acl_init) =
264 .runs_after = VLIB_INITS("ip_in_out_acl_init"),
269 unformat_acl_type (unformat_input_t * input, va_list * args)
271 u32 *acl_type = va_arg (*args, u32 *);
272 u32 tid = IN_OUT_ACL_N_TABLES;
274 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
276 if (unformat (input, "ip4"))
277 tid = IN_OUT_ACL_TABLE_IP4;
278 else if (unformat (input, "ip6"))
279 tid = IN_OUT_ACL_TABLE_IP6;
280 else if (unformat (input, "l2"))
281 tid = IN_OUT_ACL_TABLE_L2;
291 format_vnet_in_out_acl_info (u8 * s, va_list * va)
293 in_out_acl_main_t *am = va_arg (*va, in_out_acl_main_t *);
294 int sw_if_idx = va_arg (*va, int);
295 u32 tid = va_arg (*va, u32);
299 s = format (s, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
304 s = format (s, "%10d%20d\t\t%U", sw_if_idx, tid,
305 format_vnet_sw_if_index_name, am->vnet_main, sw_if_idx);
310 static clib_error_t *
311 show_in_out_acl_command_fn (vlib_main_t * vm,
312 unformat_input_t * input,
313 vlib_cli_command_t * cmd, u32 is_output)
315 in_out_acl_main_t *am = &in_out_acl_main;
316 u32 type = IN_OUT_ACL_N_TABLES;
320 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
322 if (unformat (input, "type %U", unformat_acl_type, &type))
328 if (type == IN_OUT_ACL_N_TABLES)
329 return clib_error_return (0, is_output ? "Invalid output ACL table type."
330 : "Invalid input ACL table type.");
332 vec_tbl = am->classify_table_index_by_sw_if_index[is_output][type];
334 if (vec_len (vec_tbl))
335 vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info, am, ~0 /* hdr */ ,
338 vlib_cli_output (vm, is_output ? "No output ACL tables configured"
339 : "No input ACL tables configured");
341 for (i = 0; i < vec_len (vec_tbl); i++)
343 if (vec_elt (vec_tbl, i) == ~0)
346 vlib_cli_output (vm, "%U", format_vnet_in_out_acl_info,
347 am, i, vec_elt (vec_tbl, i));
353 static clib_error_t *
354 show_inacl_command_fn (vlib_main_t * vm,
355 unformat_input_t * input, vlib_cli_command_t * cmd)
357 return show_in_out_acl_command_fn (vm, input, cmd,
358 IN_OUT_ACL_INPUT_TABLE_GROUP);
361 static clib_error_t *
362 show_outacl_command_fn (vlib_main_t * vm,
363 unformat_input_t * input, vlib_cli_command_t * cmd)
365 return show_in_out_acl_command_fn (vm, input, cmd,
366 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
370 VLIB_CLI_COMMAND (show_inacl_command, static) = {
371 .path = "show inacl",
372 .short_help = "show inacl type [ip4|ip6|l2]",
373 .function = show_inacl_command_fn,
375 VLIB_CLI_COMMAND (show_outacl_command, static) = {
376 .path = "show outacl",
377 .short_help = "show outacl type [ip4|ip6|l2]",
378 .function = show_outacl_command_fn,
383 * fd.io coding-style-patch-verification: ON
386 * eval: (c-set-style "gnu")