2 * Copyright (c) 2016 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.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/l2/l2_in_out_feat_arc.h>
24 #include <vnet/classify/in_out_acl.h>
25 #include <vpp/app/version.h>
27 #include <vnet/ethernet/ethernet_types_api.h>
28 #include <vnet/ip/format.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/ip/ip_types_api.h>
32 #include <vlibapi/api.h>
33 #include <vlibmemory/api.h>
35 /* define message IDs */
36 #include <acl/acl.api_enum.h>
37 #include <acl/acl.api_types.h>
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include "public_inlines.h"
46 #define REPLY_MSG_ID_BASE am->msg_id_base
47 #include <vlibapi/api_helper_macros.h>
50 * The code for the bihash, used by the session management.
52 #include <vppinfra/bihash_40_8.h>
53 #include <vppinfra/bihash_template.h>
54 #include <vppinfra/bihash_template.c>
57 VLIB_PLUGIN_REGISTER () = {
58 .version = VPP_BUILD_VER,
59 .description = "Access Control Lists (ACL)",
63 /* methods exported from ACL-as-a-service */
64 static acl_plugin_methods_t acl_plugin;
68 format_vec16 (u8 * s, va_list * va)
70 u16 *v = va_arg (*va, u16 *);
71 char *fmt = va_arg (*va, char *);
73 for (i = 0; i < vec_len (v); i++)
77 s = format (s, fmt, v[i]);
83 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
85 acl_main_t *am = &acl_main;
86 vl_api_acl_plugin_get_version_reply_t *rmp;
87 int msg_size = sizeof (*rmp);
88 vl_api_registration_t *reg;
90 reg = vl_api_client_index_to_registration (mp->client_index);
94 rmp = vl_msg_api_alloc (msg_size);
95 clib_memset (rmp, 0, msg_size);
97 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
98 rmp->context = mp->context;
99 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
100 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
102 vl_api_send_msg (reg, (u8 *) rmp);
106 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
109 vl_api_acl_plugin_control_ping_reply_t *rmp;
110 acl_main_t *am = &acl_main;
114 REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
116 rmp->vpe_pid = ntohl (getpid ());
122 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
124 clib_warning ("%v", out0);
125 vec_reset_length (out0);
129 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
131 vlib_cli_output (vm, "%v", out0);
132 vec_reset_length (out0);
135 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
138 format_acl_action (u8 * s, u8 action)
143 s = format (s, "deny");
146 s = format (s, "permit");
149 s = format (s, "permit+reflect");
152 s = format (s, "action %d", action);
158 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
159 acl_main_t * am, int acl_index)
162 acl_rule_t *acl_rules = am->acls[acl_index].rules;
163 u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
164 vec_len (acl_rules), am->acls[acl_index].tag);
167 for (j = 0; j < vec_len (acl_rules); j++)
170 out0 = format (out0, " %9d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
171 out0 = format_acl_action (out0, r->is_permit);
172 out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
173 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
176 format (out0, " dst %U/%d", format_ip46_address, &r->dst,
177 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
178 out0 = format (out0, " proto %d", r->proto);
179 out0 = format (out0, " sport %d", r->src_port_or_type_first);
180 if (r->src_port_or_type_first != r->src_port_or_type_last)
182 out0 = format (out0, "-%d", r->src_port_or_type_last);
184 out0 = format (out0, " dport %d", r->dst_port_or_code_first);
185 if (r->dst_port_or_code_first != r->dst_port_or_code_last)
187 out0 = format (out0, "-%d", r->dst_port_or_code_last);
189 if (r->tcp_flags_mask || r->tcp_flags_value)
192 format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
195 out0 = format (out0, "\n");
201 vl_api_acl_plugin_get_conn_table_max_entries_t_handler
202 (vl_api_acl_plugin_get_conn_table_max_entries_t * mp)
204 acl_main_t *am = &acl_main;
205 vl_api_acl_plugin_get_conn_table_max_entries_reply_t *rmp;
206 int msg_size = sizeof (*rmp);
207 vl_api_registration_t *rp;
209 rp = vl_api_client_index_to_registration (mp->client_index);
213 rmp = vl_msg_api_alloc (msg_size);
214 memset (rmp, 0, msg_size);
216 ntohs (VL_API_ACL_PLUGIN_GET_CONN_TABLE_MAX_ENTRIES_REPLY +
218 rmp->context = mp->context;
219 rmp->conn_table_max_entries =
220 clib_net_to_host_u64 (am->fa_conn_table_max_entries);
222 vl_api_send_msg (rp, (u8 *) rmp);
226 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
228 acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
232 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
234 acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
238 increment_policy_epoch (acl_main_t * am, u32 sw_if_index, int is_input)
241 u32 **ppolicy_epoch_by_swi =
242 is_input ? &am->input_policy_epoch_by_sw_if_index :
243 &am->output_policy_epoch_by_sw_if_index;
244 vec_validate (*ppolicy_epoch_by_swi, sw_if_index);
246 u32 *p_epoch = vec_elt_at_index ((*ppolicy_epoch_by_swi), sw_if_index);
248 ((1 + *p_epoch) & FA_POLICY_EPOCH_MASK) +
249 (is_input * FA_POLICY_EPOCH_IS_INPUT);
253 try_increment_acl_policy_epoch (acl_main_t * am, u32 acl_num, int is_input)
255 u32 ***p_swi_vec_by_acl = is_input ? &am->input_sw_if_index_vec_by_acl
256 : &am->output_sw_if_index_vec_by_acl;
257 if (acl_num < vec_len (*p_swi_vec_by_acl))
260 vec_foreach (p_swi, (*p_swi_vec_by_acl)[acl_num])
262 increment_policy_epoch (am, *p_swi, is_input);
269 policy_notify_acl_change (acl_main_t * am, u32 acl_num)
271 try_increment_acl_policy_epoch (am, acl_num, 0);
272 try_increment_acl_policy_epoch (am, acl_num, 1);
277 validate_and_reset_acl_counters (acl_main_t * am, u32 acl_index)
280 /* counters are set as vectors [acl#] pointing to vectors of [acl rule] */
281 acl_plugin_counter_lock (am);
283 int old_len = vec_len (am->combined_acl_counters);
285 vec_validate (am->combined_acl_counters, acl_index);
287 for (i = old_len; i < vec_len (am->combined_acl_counters); i++)
289 am->combined_acl_counters[i].name = 0;
290 /* filled in once only */
291 am->combined_acl_counters[i].stat_segment_name = (void *)
292 format (0, "/acl/%d/matches%c", i, 0);
293 i32 rule_count = vec_len (am->acls[i].rules);
294 /* Validate one extra so we always have at least one counter for an ACL */
295 vlib_validate_combined_counter (&am->combined_acl_counters[i],
297 vlib_clear_combined_counters (&am->combined_acl_counters[i]);
300 /* (re)validate for the actual ACL that is getting added/updated */
301 i32 rule_count = vec_len (am->acls[acl_index].rules);
302 /* Validate one extra so we always have at least one counter for an ACL */
303 vlib_validate_combined_counter (&am->combined_acl_counters[acl_index],
305 vlib_clear_combined_counters (&am->combined_acl_counters[acl_index]);
306 acl_plugin_counter_unlock (am);
310 acl_api_invalid_prefix (const vl_api_prefix_t * prefix)
312 ip_prefix_t ip_prefix;
313 return ip_prefix_decode2 (prefix, &ip_prefix);
317 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
318 u32 * acl_list_index, u8 * tag)
320 acl_main_t *am = &acl_main;
323 acl_rule_t *acl_new_rules = 0;
327 tag_len = clib_strnlen ((const char *) tag, sizeof (a->tag));
328 if (tag_len == sizeof (a->tag))
329 return VNET_API_ERROR_INVALID_VALUE;
331 if (am->trace_acl > 255)
332 clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
335 /* check if what they request is consistent */
336 for (i = 0; i < count; i++)
338 if (acl_api_invalid_prefix (&rules[i].src_prefix))
339 return VNET_API_ERROR_INVALID_SRC_ADDRESS;
340 if (acl_api_invalid_prefix (&rules[i].dst_prefix))
341 return VNET_API_ERROR_INVALID_DST_ADDRESS;
342 if (ntohs (rules[i].srcport_or_icmptype_first) >
343 ntohs (rules[i].srcport_or_icmptype_last))
344 return VNET_API_ERROR_INVALID_VALUE_2;
345 if (ntohs (rules[i].dstport_or_icmpcode_first) >
346 ntohs (rules[i].dstport_or_icmpcode_last))
347 return VNET_API_ERROR_INVALID_VALUE_2;
350 if (*acl_list_index != ~0)
352 /* They supplied some number, let's see if this ACL exists */
353 if (pool_is_free_index (am->acls, *acl_list_index))
355 /* tried to replace a non-existent ACL, no point doing anything */
357 ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
358 *acl_list_index, tag);
359 return VNET_API_ERROR_NO_SUCH_ENTRY;
365 ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
366 *acl_list_index, tag);
369 /* Create and populate the rules */
371 vec_validate (acl_new_rules, count - 1);
373 for (i = 0; i < count; i++)
375 r = vec_elt_at_index (acl_new_rules, i);
376 clib_memset (r, 0, sizeof (*r));
377 r->is_permit = rules[i].is_permit;
378 r->is_ipv6 = rules[i].src_prefix.address.af;
379 ip_address_decode (&rules[i].src_prefix.address, &r->src);
380 ip_address_decode (&rules[i].dst_prefix.address, &r->dst);
381 r->src_prefixlen = rules[i].src_prefix.len;
382 r->dst_prefixlen = rules[i].dst_prefix.len;
383 r->proto = rules[i].proto;
384 r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
385 r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
386 r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
387 r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
388 r->tcp_flags_value = rules[i].tcp_flags_value;
389 r->tcp_flags_mask = rules[i].tcp_flags_mask;
392 if (~0 == *acl_list_index)
395 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
396 clib_memset (a, 0, sizeof (*a));
397 /* Will return the newly allocated ACL index */
398 *acl_list_index = a - am->acls;
402 a = am->acls + *acl_list_index;
403 /* Get rid of the old rules */
407 a->rules = acl_new_rules;
408 memcpy (a->tag, tag, tag_len + 1);
409 if (am->trace_acl > 255)
410 warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
411 if (am->reclassify_sessions)
413 /* a change in an ACLs if they are applied may mean a new policy epoch */
414 policy_notify_acl_change (am, *acl_list_index);
416 validate_and_reset_acl_counters (am, *acl_list_index);
417 acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
422 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
424 if (acl_index < vec_len (foo_index_vec_by_acl))
426 if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
428 /* ACL is applied somewhere. */
436 acl_del_list (u32 acl_list_index)
438 acl_main_t *am = &acl_main;
440 if (pool_is_free_index (am->acls, acl_list_index))
442 return VNET_API_ERROR_NO_SUCH_ENTRY;
444 if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
445 return VNET_API_ERROR_ACL_IN_USE_INBOUND;
446 if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
447 return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
448 /* lookup contexts cover other cases, not just inbound/outbound, so check that */
449 if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
450 return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
452 /* now we can delete the ACL itself */
453 a = pool_elt_at_index (am->acls, acl_list_index);
456 pool_put (am->acls, a);
457 /* acl_list_index is now free, notify the lookup contexts */
458 acl_plugin_lookup_context_notify_acl_change (acl_list_index);
463 count_skip (u8 * p, u32 size)
465 u64 *p64 = (u64 *) p;
466 /* Be tolerant to null pointer */
470 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
474 return (p64 - (u64 *) p) / 2;
478 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
479 u32 mask_len, u32 next_table_index,
480 u32 miss_next_index, u32 * table_index,
484 u32 memory_size = 2 << 22;
485 u32 skip = count_skip (mask, mask_len);
486 u32 match = (mask_len / 16) - skip;
487 u8 *skip_mask_ptr = mask + 16 * skip;
488 u32 current_data_flag = 0;
489 int current_data_offset = 0;
494 int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
495 memory_size, skip, match,
496 next_table_index, miss_next_index,
497 table_index, current_data_flag,
498 current_data_offset, is_add,
499 1 /* delete_chain */ );
504 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
507 ? am->input_etype_whitelist_by_sw_if_index
508 : am->output_etype_whitelist_by_sw_if_index;
509 u16 *whitelist = (vec_len (v) > sw_if_index) ? vec_elt (v, sw_if_index) : 0;
510 return vec_len (whitelist) > 0;
514 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
516 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
517 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
523 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
529 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
531 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
533 if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
537 acl_fa_enable_disable (sw_if_index, 1, enable_disable);
539 rv = vnet_l2_feature_enable_disable ("l2-input-ip4", "acl-plugin-in-ip4-l2",
540 sw_if_index, enable_disable, 0, 0);
542 clib_error ("Could not enable on input");
543 rv = vnet_l2_feature_enable_disable ("l2-input-ip6", "acl-plugin-in-ip6-l2",
544 sw_if_index, enable_disable, 0, 0);
546 clib_error ("Could not enable on input");
548 if (intf_has_etype_whitelist (am, sw_if_index, 1))
549 vnet_l2_feature_enable_disable ("l2-input-nonip",
550 "acl-plugin-in-nonip-l2", sw_if_index,
551 enable_disable, 0, 0);
552 am->in_acl_on_sw_if_index =
553 clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
559 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
565 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
567 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
569 if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
573 acl_fa_enable_disable (sw_if_index, 0, enable_disable);
576 vnet_l2_feature_enable_disable ("l2-output-ip4", "acl-plugin-out-ip4-l2",
577 sw_if_index, enable_disable, 0, 0);
579 clib_error ("Could not enable on output");
581 vnet_l2_feature_enable_disable ("l2-output-ip6", "acl-plugin-out-ip6-l2",
582 sw_if_index, enable_disable, 0, 0);
584 clib_error ("Could not enable on output");
585 if (intf_has_etype_whitelist (am, sw_if_index, 0))
586 vnet_l2_feature_enable_disable ("l2-output-nonip",
587 "acl-plugin-out-nonip-l2", sw_if_index,
588 enable_disable, 0, 0);
589 am->out_acl_on_sw_if_index =
590 clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
596 acl_stats_intf_counters_enable_disable (acl_main_t * am, int enable_disable)
600 am->interface_acl_counters_enabled = enable_disable;
606 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
607 int is_input, int enable_disable)
610 return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
612 return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
616 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
618 return (pool_is_free_index (am->acls, acl_list_index));
622 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
623 u8 is_input, u32 * vec_acl_list_index,
624 int *may_clear_sessions)
627 uword *seen_acl_bitmap = 0;
628 uword *old_seen_acl_bitmap = 0;
629 uword *change_acl_bitmap = 0;
634 if (am->trace_acl > 255)
636 ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
637 sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
639 vec_foreach (pacln, vec_acl_list_index)
641 if (acl_is_not_defined (am, *pacln))
643 /* ACL is not defined. Can not apply */
644 clib_warning ("ERROR: ACL %d not defined", *pacln);
645 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
648 if (clib_bitmap_get (seen_acl_bitmap, *pacln))
650 /* ACL being applied twice within the list. error. */
651 clib_warning ("ERROR: ACL %d being applied twice", *pacln);
652 rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
655 seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
659 u32 **pinout_lc_index_by_sw_if_index =
660 is_input ? &am->input_lc_index_by_sw_if_index : &am->
661 output_lc_index_by_sw_if_index;
663 u32 ***pinout_acl_vec_by_sw_if_index =
664 is_input ? &am->input_acl_vec_by_sw_if_index : &am->
665 output_acl_vec_by_sw_if_index;
667 u32 ***pinout_sw_if_index_vec_by_acl =
668 is_input ? &am->input_sw_if_index_vec_by_acl : &am->
669 output_sw_if_index_vec_by_acl;
671 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
673 clib_bitmap_validate (old_seen_acl_bitmap, 1);
675 vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
677 old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
680 clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
682 if (am->trace_acl > 255)
683 clib_warning ("bitmaps: old seen %U new seen %U changed %U",
684 format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
685 seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
688 clib_bitmap_foreach (acln, change_acl_bitmap) {
689 if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
690 /* ACL is being removed. */
691 if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
692 int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
693 vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
696 /* ACL is being added. */
697 vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
698 vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
703 vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
704 (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
705 vec_dup (vec_acl_list_index);
707 if (am->reclassify_sessions)
709 /* re-applying ACLs means a new policy epoch */
710 increment_policy_epoch (am, sw_if_index, is_input);
714 /* if no commonalities between the ACL# - then we should definitely clear the sessions */
715 if (may_clear_sessions && *may_clear_sessions
716 && !clib_bitmap_is_zero (change_acl_bitmap))
718 acl_clear_sessions (am, sw_if_index);
719 *may_clear_sessions = 0;
724 * prepare or delete the lookup context if necessary, and if context exists, set ACL list
726 vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
728 if (vec_len (vec_acl_list_index) > 0)
730 u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
734 acl_plugin.get_lookup_context_index (am->interface_acl_user_id,
735 sw_if_index, is_input);
736 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
738 acl_plugin.set_acl_vec_for_context (lc_index, vec_acl_list_index);
742 if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
745 put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)
747 (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
750 /* ensure ACL processing is enabled/disabled as needed */
751 acl_interface_inout_enable_disable (am, sw_if_index, is_input,
752 vec_len (vec_acl_list_index) > 0);
755 clib_bitmap_free (change_acl_bitmap);
756 clib_bitmap_free (seen_acl_bitmap);
757 clib_bitmap_free (old_seen_acl_bitmap);
762 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
763 int *may_clear_sessions)
765 acl_main_t *am = &acl_main;
766 acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
771 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
775 acl_main_t *am = &acl_main;
777 int may_clear_sessions = 1;
779 int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
780 : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
782 u32 ***pinout_acl_vec_by_sw_if_index =
783 is_input ? &am->input_acl_vec_by_sw_if_index : &am->
784 output_acl_vec_by_sw_if_index;
788 vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
789 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
794 rv = error_already_applied;
798 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
799 vec_add1 (acl_vec, acl_list_index);
803 if (sw_if_index >= vec_len (*pinout_acl_vec_by_sw_if_index))
805 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
809 u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
814 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
818 acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
819 vec_del1 (acl_vec, index);
822 rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
823 &may_clear_sessions);
830 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
833 vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
834 vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
836 vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
837 vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
839 am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
840 am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
843 * if there are already inbound/outbound ACLs applied, toggle the
844 * enable/disable - this will recreate the necessary tables.
847 if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
849 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
851 acl_interface_in_enable_disable (am, sw_if_index, 0);
852 acl_interface_in_enable_disable (am, sw_if_index, 1);
855 if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
857 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
859 acl_interface_out_enable_disable (am, sw_if_index, 0);
860 acl_interface_out_enable_disable (am, sw_if_index, 1);
876 u32 dot1q_table_index;
877 u32 dot1ad_table_index;
878 u32 arp_dot1q_table_index;
879 u32 arp_dot1ad_table_index;
882 u32 out_arp_table_index;
883 u32 out_dot1q_table_index;
884 u32 out_dot1ad_table_index;
885 u32 out_arp_dot1q_table_index;
886 u32 out_arp_dot1ad_table_index;
887 } macip_match_type_t;
890 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
896 for (i = 0; i < vec_len (mv); i++)
898 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
899 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
909 /* Get metric used to sort match types.
910 The more specific and the more often seen - the bigger the metric */
912 match_type_metric (macip_match_type_t * m)
914 unsigned int mac_bits_set = 0;
915 unsigned int mac_byte;
917 for (i = 0; i < 6; i++)
919 mac_byte = m->mac_mask[i];
920 for (; mac_byte; mac_byte >>= 1)
921 mac_bits_set += mac_byte & 1;
924 * Attempt to place the more specific and the more used rules on top.
925 * There are obvious caveat corner cases to this, but they do not
926 * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
927 * going with a wildcard IPv4 with a specific MAC).
929 return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
933 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
935 /* Ascending sort based on the metric values */
936 return match_type_metric (m1) - match_type_metric (m2);
939 /* Get the offset of L3 source within ethernet packet */
941 get_l3_src_offset (int is6)
944 return (sizeof (ethernet_header_t) +
945 offsetof (ip6_header_t, src_address));
947 return (sizeof (ethernet_header_t) +
948 offsetof (ip4_header_t, src_address));
952 get_l3_dst_offset (int is6)
955 return (sizeof (ethernet_header_t) +
956 offsetof (ip6_header_t, dst_address));
958 return (sizeof (ethernet_header_t) +
959 offsetof (ip4_header_t, dst_address));
963 * return if the is_permit value also requires to create the egress tables
964 * For backwards compatibility, we keep the is_permit = 1 to only
965 * create the ingress tables, and the new value of 3 will also
966 * create the egress tables based on destination.
969 macip_permit_also_egress (u8 is_permit)
971 return (is_permit == 3);
975 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
977 macip_match_type_t *mvec = NULL;
978 macip_match_type_t *mt;
979 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
981 u32 match_type_index;
985 vnet_classify_main_t *cm = &vnet_classify_main;
987 /* Count the number of different types of rules */
988 for (i = 0; i < a->count; i++)
992 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
993 a->rules[i].src_prefixlen,
994 a->rules[i].is_ipv6)))
996 match_type_index = vec_len (mvec);
997 vec_validate (mvec, match_type_index);
998 memcpy (mvec[match_type_index].mac_mask,
999 a->rules[i].src_mac_mask, 6);
1000 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1001 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1002 mvec[match_type_index].has_egress = 0;
1003 mvec[match_type_index].table_index = ~0;
1004 mvec[match_type_index].arp_table_index = ~0;
1005 mvec[match_type_index].dot1q_table_index = ~0;
1006 mvec[match_type_index].dot1ad_table_index = ~0;
1007 mvec[match_type_index].arp_dot1q_table_index = ~0;
1008 mvec[match_type_index].arp_dot1ad_table_index = ~0;
1009 mvec[match_type_index].out_table_index = ~0;
1010 mvec[match_type_index].out_arp_table_index = ~0;
1011 mvec[match_type_index].out_dot1q_table_index = ~0;
1012 mvec[match_type_index].out_dot1ad_table_index = ~0;
1013 mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1014 mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1016 mvec[match_type_index].count++;
1017 mvec[match_type_index].has_egress |=
1018 macip_permit_also_egress (a->rules[i].is_permit);
1020 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1021 vec_sort_with_function (mvec, match_type_compare);
1022 /* Create the classifier tables */
1024 out_last_table = ~0;
1025 /* First add ARP tables */
1026 vec_foreach (mt, mvec)
1029 int is6 = mt->is_ipv6;
1031 u32 *last_tag_table;
1032 u32 *out_last_tag_table;
1039 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1040 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1041 | Destination Address |
1042 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1046 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1047 | EtherType | Hardware Type |
1048 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1049 | Protocol Type | Hw addr len | Proto addr len|
1050 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1052 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
1053 | Sender Hardware Address |
1054 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1055 | Sender Protocol Address |
1056 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1057 | Target Hardware Address |
1058 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1059 | | TargetProtocolAddress |
1060 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1062 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1064 for (tags = 2; tags >= 0; tags--)
1066 clib_memset (mask, 0, sizeof (mask));
1067 /* source MAC address */
1068 memcpy (&mask[6], mt->mac_mask, 6);
1074 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1076 last_tag_table = &mt->arp_table_index;
1079 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1080 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1082 last_tag_table = &mt->arp_dot1q_table_index;
1085 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1086 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1087 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1089 last_tag_table = &mt->arp_dot1ad_table_index;
1093 /* sender hardware address within ARP */
1094 memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1095 /* sender protocol address within ARP */
1096 for (i = 0; i < (mt->prefix_len / 8); i++)
1097 mask[l3_offset + 14 + i] = 0xff;
1098 if (mt->prefix_len % 8)
1099 mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1100 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1102 mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1104 1)) / sizeof (u32x4)) * sizeof (u32x4);
1105 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1106 (~0 == last_table) ? 0 : ~0,
1108 last_table = *last_tag_table;
1111 /* egress ARP table */
1112 clib_memset (mask, 0, sizeof (mask));
1118 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1120 out_last_tag_table = &mt->out_arp_table_index;
1123 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1124 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1126 out_last_tag_table = &mt->out_arp_dot1q_table_index;
1129 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1130 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1131 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1133 out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1137 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1138 /* mask captures just the destination and the ethertype */
1139 mask_len = ((l3_offset +
1141 1)) / sizeof (u32x4)) * sizeof (u32x4);
1142 acl_classify_add_del_table_small (cm, mask, mask_len,
1145 out_last_table) ? 0 : ~0,
1146 out_last_tag_table, 1);
1147 out_last_table = *out_last_tag_table;
1152 /* Now add IP[46] tables */
1153 vec_foreach (mt, mvec)
1156 int is6 = mt->is_ipv6;
1160 u32 *last_tag_table;
1161 u32 *out_last_tag_table;
1164 * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1166 for (tags = 2; tags >= 0; tags--)
1168 clib_memset (mask, 0, sizeof (mask));
1169 memcpy (&mask[6], mt->mac_mask, 6);
1170 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1175 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1176 last_tag_table = &mt->table_index;
1179 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1180 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1181 last_tag_table = &mt->dot1q_table_index;
1184 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1185 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1186 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1187 last_tag_table = &mt->dot1ad_table_index;
1190 for (i = 0; i < (mt->prefix_len / 8); i++)
1192 mask[l3_src_offs + i] = 0xff;
1194 if (mt->prefix_len % 8)
1196 mask[l3_src_offs + (mt->prefix_len / 8)] =
1197 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1200 * Round-up the number of bytes needed to store the prefix,
1201 * and round up the number of vectors too
1203 mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1204 (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1205 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1206 (~0 == last_table) ? 0 : ~0,
1208 last_table = *last_tag_table;
1212 for (tags = 2; tags >= 0; tags--)
1214 clib_memset (mask, 0, sizeof (mask));
1215 /* MAC destination */
1216 memcpy (&mask[0], mt->mac_mask, 6);
1217 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1222 clib_memset (&mask[12], 0xff, 2); /* ethernet protocol */
1223 out_last_tag_table = &mt->out_table_index;
1226 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1227 clib_memset (&mask[16], 0xff, 2); /* ethernet protocol */
1228 out_last_tag_table = &mt->out_dot1q_table_index;
1231 clib_memset (&mask[12], 0xff, 2); /* VLAN tag1 */
1232 clib_memset (&mask[16], 0xff, 2); /* VLAN tag2 */
1233 clib_memset (&mask[20], 0xff, 2); /* ethernet protocol */
1234 out_last_tag_table = &mt->out_dot1ad_table_index;
1237 for (i = 0; i < (mt->prefix_len / 8); i++)
1239 mask[l3_dst_offs + i] = 0xff;
1241 if (mt->prefix_len % 8)
1243 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1244 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1247 * Round-up the number of bytes needed to store the prefix,
1248 * and round up the number of vectors too
1250 mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1252 1)) / sizeof (u32x4)) * sizeof (u32x4);
1253 acl_classify_add_del_table_small (cm, mask, mask_len,
1255 (~0 == out_last_table) ? 0 : ~0,
1256 out_last_tag_table, 1);
1257 out_last_table = *out_last_tag_table;
1261 a->ip4_table_index = last_table;
1262 a->ip6_table_index = last_table;
1263 a->l2_table_index = last_table;
1265 a->out_ip4_table_index = out_last_table;
1266 a->out_ip6_table_index = out_last_table;
1267 a->out_l2_table_index = out_last_table;
1269 /* Populate the classifier tables with rules from the MACIP ACL */
1270 for (i = 0; i < a->count; i++)
1274 int is6 = a->rules[i].is_ipv6;
1281 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1282 a->rules[i].src_prefixlen,
1283 a->rules[i].is_ipv6);
1284 ASSERT (match_type_index != ~0);
1286 for (tags = 2; tags >= 0; tags--)
1288 clib_memset (mask, 0, sizeof (mask));
1289 l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1290 memcpy (&mask[6], a->rules[i].src_mac, 6);
1295 tag_table = mvec[match_type_index].table_index;
1299 tag_table = mvec[match_type_index].dot1q_table_index;
1305 tag_table = mvec[match_type_index].dot1ad_table_index;
1315 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1317 mask[eth + 1] = 0xdd;
1321 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1323 mask[eth + 1] = 0x00;
1326 /* add session to table mvec[match_type_index].table_index; */
1327 vnet_classify_add_del_session (cm, tag_table,
1328 mask, a->rules[i].is_permit ? ~0 : 0,
1329 i, 0, action, metadata, 1);
1330 clib_memset (&mask[12], 0, sizeof (mask) - 12);
1333 /* add ARP table entry too */
1334 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1336 clib_memset (mask, 0, sizeof (mask));
1337 memcpy (&mask[6], a->rules[i].src_mac, 6);
1339 for (tags = 2; tags >= 0; tags--)
1345 tag_table = mvec[match_type_index].arp_table_index;
1351 tag_table = mvec[match_type_index].arp_dot1q_table_index;
1359 tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1370 memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1371 memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1373 vnet_classify_add_del_session (cm, tag_table, mask,
1374 a->rules[i].is_permit ? ~0 : 0,
1375 i, 0, action, metadata, 1);
1378 if (macip_permit_also_egress (a->rules[i].is_permit))
1380 /* Add the egress entry with destination set */
1381 for (tags = 2; tags >= 0; tags--)
1383 clib_memset (mask, 0, sizeof (mask));
1384 l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1385 /* src mac in the other direction becomes dst */
1386 memcpy (&mask[0], a->rules[i].src_mac, 6);
1391 tag_table = mvec[match_type_index].out_table_index;
1395 tag_table = mvec[match_type_index].out_dot1q_table_index;
1401 tag_table = mvec[match_type_index].out_dot1ad_table_index;
1411 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1414 mask[eth + 1] = 0xdd;
1418 memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1421 mask[eth + 1] = 0x00;
1424 /* add session to table mvec[match_type_index].table_index; */
1425 vnet_classify_add_del_session (cm, tag_table,
1427 a->rules[i].is_permit ? ~0 : 0,
1428 i, 0, action, metadata, 1);
1429 // clib_memset (&mask[12], 0, sizeof (mask) - 12);
1432 /* add ARP table entry too */
1433 if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1435 for (tags = 2; tags >= 0; tags--)
1437 clib_memset (mask, 0, sizeof (mask));
1442 tag_table = mvec[match_type_index].out_arp_table_index;
1448 mvec[match_type_index].out_arp_dot1q_table_index;
1456 mvec[match_type_index].out_arp_dot1ad_table_index;
1466 vnet_classify_add_del_session (cm, tag_table,
1469 is_permit ? ~0 : 0, i, 0,
1470 action, metadata, 1);
1479 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1481 vnet_classify_main_t *cm = &vnet_classify_main;
1482 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1484 if (a->ip4_table_index != ~0)
1486 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1487 &a->ip4_table_index, 0);
1488 a->ip4_table_index = ~0;
1490 if (a->ip6_table_index != ~0)
1492 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1493 &a->ip6_table_index, 0);
1494 a->ip6_table_index = ~0;
1496 if (a->l2_table_index != ~0)
1498 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
1500 a->l2_table_index = ~0;
1502 if (a->out_ip4_table_index != ~0)
1504 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1505 &a->out_ip4_table_index, 0);
1506 a->out_ip4_table_index = ~0;
1508 if (a->out_ip6_table_index != ~0)
1510 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1511 &a->out_ip6_table_index, 0);
1512 a->out_ip6_table_index = ~0;
1514 if (a->out_l2_table_index != ~0)
1516 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
1517 &a->out_l2_table_index, 0);
1518 a->out_l2_table_index = ~0;
1523 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1529 macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1531 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1532 if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1534 rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1535 a->ip6_table_index, a->l2_table_index,
1537 /* return the first unhappy outcome but make try to plough through. */
1540 vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
1541 a->out_ip6_table_index,
1542 a->out_l2_table_index, is_apply);
1543 /* return the first unhappy outcome but make try to plough through. */
1550 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1551 u32 * acl_list_index, u8 * tag)
1553 acl_main_t *am = &acl_main;
1554 macip_acl_list_t *a;
1555 macip_acl_rule_t *r;
1556 macip_acl_rule_t *acl_new_rules = 0;
1561 tag_len = clib_strnlen ((const char *) tag, sizeof (a->tag));
1562 if (tag_len == sizeof (a->tag))
1563 return VNET_API_ERROR_INVALID_VALUE;
1565 if (*acl_list_index != ~0)
1567 /* They supplied some number, let's see if this MACIP ACL exists */
1568 if (pool_is_free_index (am->macip_acls, *acl_list_index))
1570 /* tried to replace a non-existent ACL, no point doing anything */
1572 ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1573 *acl_list_index, tag);
1574 return VNET_API_ERROR_NO_SUCH_ENTRY;
1581 ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1584 /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1585 if (~0 != *acl_list_index)
1586 rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1587 /* Create and populate the rules */
1589 vec_validate (acl_new_rules, count - 1);
1591 for (i = 0; i < count; i++)
1593 r = &acl_new_rules[i];
1594 r->is_permit = rules[i].is_permit;
1595 r->is_ipv6 = rules[i].src_prefix.address.af;
1596 mac_address_decode (rules[i].src_mac, (mac_address_t *) & r->src_mac);
1597 mac_address_decode (rules[i].src_mac_mask,
1598 (mac_address_t *) & r->src_mac_mask);
1599 ip_address_decode (&rules[i].src_prefix.address, &r->src_ip_addr);
1600 r->src_prefixlen = rules[i].src_prefix.len;
1603 if (~0 == *acl_list_index)
1606 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1607 clib_memset (a, 0, sizeof (*a));
1608 /* Will return the newly allocated ACL index */
1609 *acl_list_index = a - am->macip_acls;
1613 a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1616 vec_free (a->rules);
1618 macip_destroy_classify_tables (am, *acl_list_index);
1621 a->rules = acl_new_rules;
1623 memcpy (a->tag, tag, tag_len + 1);
1625 /* Create and populate the classifier tables */
1626 macip_create_classify_tables (am, *acl_list_index);
1627 /* If the ACL was already applied somewhere, reapply the newly created tables */
1629 || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1633 /* No check that sw_if_index denotes a valid interface - the callers
1634 * were supposed to validate.
1636 * That said, if sw_if_index corresponds to an interface that exists at all,
1637 * this function must return errors accordingly if the ACL is not applied.
1641 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1644 u32 macip_acl_index;
1645 macip_acl_list_t *a;
1647 /* The vector is too short - MACIP ACL is not applied */
1648 if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
1649 return VNET_API_ERROR_NO_SUCH_ENTRY;
1651 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1652 /* No point in deleting MACIP ACL which is not applied */
1653 if (~0 == macip_acl_index)
1654 return VNET_API_ERROR_NO_SUCH_ENTRY;
1656 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1657 /* remove the classifier tables off the interface L2 ACL */
1659 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1660 a->ip6_table_index, a->l2_table_index, 0);
1662 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1663 a->out_ip4_table_index, a->out_ip6_table_index,
1664 a->out_l2_table_index, 0);
1665 /* Unset the MACIP ACL index */
1666 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1667 /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
1668 u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
1671 vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
1675 /* No check for validity of sw_if_index - the callers were supposed to validate */
1678 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1679 u32 macip_acl_index)
1681 macip_acl_list_t *a;
1683 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1685 return VNET_API_ERROR_NO_SUCH_ENTRY;
1687 a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1688 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1689 vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
1690 vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
1691 /* If there already a MACIP ACL applied, unapply it */
1692 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1693 macip_acl_interface_del_acl (am, sw_if_index);
1694 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1696 /* Apply the classifier tables for L2 ACLs */
1698 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1699 a->ip6_table_index, a->l2_table_index, 1);
1701 vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1702 a->out_ip4_table_index, a->out_ip6_table_index,
1703 a->out_l2_table_index, 1);
1708 macip_acl_del_list (u32 acl_list_index)
1710 acl_main_t *am = &acl_main;
1711 macip_acl_list_t *a;
1713 if (pool_is_free_index (am->macip_acls, acl_list_index))
1715 return VNET_API_ERROR_NO_SUCH_ENTRY;
1718 /* delete any references to the ACL */
1719 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1721 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1723 macip_acl_interface_del_acl (am, i);
1727 /* Now that classifier tables are detached, clean them up */
1728 macip_destroy_classify_tables (am, acl_list_index);
1730 /* now we can delete the ACL itself */
1731 a = pool_elt_at_index (am->macip_acls, acl_list_index);
1734 vec_free (a->rules);
1736 pool_put (am->macip_acls, a);
1742 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1745 acl_main_t *am = &acl_main;
1749 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1753 rv = macip_acl_interface_del_acl (am, sw_if_index);
1759 * If the client does not allocate enough memory for a variable-length
1760 * message, and then proceed to use it as if the full memory allocated,
1761 * absent the check we happily consume that on the VPP side, and go
1762 * along as if nothing happened. However, the resulting
1763 * effects range from just garbage in the API decode
1764 * (because the decoder snoops too far), to potential memory
1767 * This verifies that the actual length of the message is
1768 * at least expected_len, and complains loudly if it is not.
1770 * A failing check here is 100% a software bug on the API user side,
1771 * so we might as well yell.
1775 verify_message_len (void *mp, u64 expected_len, char *where)
1777 u32 supplied_len = vl_msg_api_get_msg_length (mp);
1778 if (supplied_len < expected_len)
1780 clib_warning ("%s: Supplied message length %d is less than expected %d",
1781 where, supplied_len, expected_len);
1790 /* API message handler */
1792 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1794 vl_api_acl_add_replace_reply_t *rmp;
1795 acl_main_t *am = &acl_main;
1797 u32 acl_list_index = ntohl (mp->acl_index);
1798 u32 acl_count = ntohl (mp->count);
1799 u64 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
1801 if (verify_message_len (mp, expected_len, "acl_add_replace"))
1803 rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1807 rv = VNET_API_ERROR_INVALID_VALUE;
1811 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1813 rmp->acl_index = htonl(acl_list_index);
1819 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1821 acl_main_t *am = &acl_main;
1822 vl_api_acl_del_reply_t *rmp;
1825 rv = acl_del_list (ntohl (mp->acl_index));
1827 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1832 vl_api_acl_stats_intf_counters_enable_t_handler
1833 (vl_api_acl_stats_intf_counters_enable_t * mp)
1835 acl_main_t *am = &acl_main;
1836 vl_api_acl_stats_intf_counters_enable_reply_t *rmp;
1839 rv = acl_stats_intf_counters_enable_disable (am, mp->enable);
1841 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1846 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1848 acl_main_t *am = &acl_main;
1849 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1850 u32 sw_if_index = ntohl (mp->sw_if_index);
1851 vl_api_acl_interface_add_del_reply_t *rmp;
1854 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1855 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1858 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1859 mp->is_input, ntohl (mp->acl_index));
1861 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1865 vl_api_acl_interface_set_acl_list_t_handler
1866 (vl_api_acl_interface_set_acl_list_t * mp)
1868 acl_main_t *am = &acl_main;
1869 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1872 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1873 u32 sw_if_index = ntohl (mp->sw_if_index);
1875 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
1876 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1879 int may_clear_sessions = 1;
1880 for (i = 0; i < mp->count; i++)
1882 if (acl_is_not_defined (am, ntohl (mp->acls[i])))
1884 /* ACL does not exist, so we can not apply it */
1885 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1890 u32 *in_acl_vec = 0;
1891 u32 *out_acl_vec = 0;
1892 for (i = 0; i < mp->count; i++)
1893 if (i < mp->n_input)
1894 vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
1896 vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
1899 acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
1900 &may_clear_sessions);
1902 || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
1904 &may_clear_sessions);
1905 vec_free (in_acl_vec);
1906 vec_free (out_acl_vec);
1910 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1914 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1916 api_rule->is_permit = r->is_permit;
1917 ip_address_encode (&r->src, r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
1918 &api_rule->src_prefix.address);
1919 ip_address_encode (&r->dst, r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
1920 &api_rule->dst_prefix.address);
1921 api_rule->src_prefix.len = r->src_prefixlen;
1922 api_rule->dst_prefix.len = r->dst_prefixlen;
1923 api_rule->proto = r->proto;
1924 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1925 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1926 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1927 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1928 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1929 api_rule->tcp_flags_value = r->tcp_flags_value;
1933 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
1934 acl_list_t * acl, u32 context)
1936 vl_api_acl_details_t *mp;
1937 vl_api_acl_rule_t *rules;
1939 acl_rule_t *acl_rules = acl->rules;
1940 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * vec_len (acl_rules);
1942 mp = vl_msg_api_alloc (msg_size);
1943 clib_memset (mp, 0, msg_size);
1944 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1946 /* fill in the message */
1947 mp->context = context;
1948 mp->count = htonl (vec_len (acl_rules));
1949 mp->acl_index = htonl (acl - am->acls);
1950 snprintf ((char *) mp->tag, sizeof (mp->tag), "%s", acl->tag);
1951 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1953 for (i = 0; i < vec_len (acl_rules); i++)
1955 copy_acl_rule_to_api_rule (&rules[i], &acl_rules[i]);
1958 vl_api_send_msg (reg, (u8 *) mp);
1963 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1965 acl_main_t *am = &acl_main;
1969 vl_api_registration_t *reg;
1971 reg = vl_api_client_index_to_registration (mp->client_index);
1975 if (mp->acl_index == ~0)
1978 /* Just dump all ACLs */
1979 pool_foreach (acl, am->acls)
1981 send_acl_details(am, reg, acl, mp->context);
1987 acl_index = ntohl (mp->acl_index);
1988 if (!pool_is_free_index (am->acls, acl_index))
1990 acl = pool_elt_at_index (am->acls, acl_index);
1991 send_acl_details (am, reg, acl, mp->context);
1997 /* FIXME API: should we signal an error here at all ? */
2003 send_acl_interface_list_details (acl_main_t * am,
2004 vl_api_registration_t * reg,
2005 u32 sw_if_index, u32 context)
2007 vl_api_acl_interface_list_details_t *mp;
2014 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2015 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2017 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2018 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2019 count = n_input + n_output;
2021 msg_size = sizeof (*mp);
2022 msg_size += sizeof (mp->acls[0]) * count;
2024 mp = vl_msg_api_alloc (msg_size);
2025 clib_memset (mp, 0, msg_size);
2027 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2029 /* fill in the message */
2030 mp->context = context;
2031 mp->sw_if_index = htonl (sw_if_index);
2033 mp->n_input = n_input;
2034 for (i = 0; i < n_input; i++)
2036 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2038 for (i = 0; i < n_output; i++)
2040 mp->acls[n_input + i] =
2041 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2043 vl_api_send_msg (reg, (u8 *) mp);
2047 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2050 acl_main_t *am = &acl_main;
2051 vnet_sw_interface_t *swif;
2052 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2055 vl_api_registration_t *reg;
2057 reg = vl_api_client_index_to_registration (mp->client_index);
2061 if (mp->sw_if_index == ~0)
2064 pool_foreach (swif, im->sw_interfaces)
2066 send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2072 sw_if_index = ntohl (mp->sw_if_index);
2073 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2074 send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2078 /* MACIP ACL API handlers */
2081 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2083 vl_api_macip_acl_add_reply_t *rmp;
2084 acl_main_t *am = &acl_main;
2086 u32 acl_list_index = ~0;
2087 u32 acl_count = ntohl (mp->count);
2088 u64 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2090 if (verify_message_len (mp, expected_len, "macip_acl_add"))
2092 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2096 rv = VNET_API_ERROR_INVALID_VALUE;
2100 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2102 rmp->acl_index = htonl(acl_list_index);
2108 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2110 vl_api_macip_acl_add_replace_reply_t *rmp;
2111 acl_main_t *am = &acl_main;
2113 u32 acl_list_index = ntohl (mp->acl_index);
2114 u32 acl_count = ntohl (mp->count);
2115 u64 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2117 if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2119 rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2123 rv = VNET_API_ERROR_INVALID_VALUE;
2127 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2129 rmp->acl_index = htonl(acl_list_index);
2135 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2137 acl_main_t *am = &acl_main;
2138 vl_api_macip_acl_del_reply_t *rmp;
2141 rv = macip_acl_del_list (ntohl (mp->acl_index));
2143 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2147 vl_api_macip_acl_interface_add_del_t_handler
2148 (vl_api_macip_acl_interface_add_del_t * mp)
2150 acl_main_t *am = &acl_main;
2151 vl_api_macip_acl_interface_add_del_reply_t *rmp;
2153 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2154 u32 sw_if_index = ntohl (mp->sw_if_index);
2156 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2157 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2160 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2161 ntohl (mp->acl_index));
2163 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2167 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2168 macip_acl_list_t * acl, u32 context)
2170 vl_api_macip_acl_details_t *mp;
2171 vl_api_macip_acl_rule_t *rules;
2172 macip_acl_rule_t *r;
2174 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2176 mp = vl_msg_api_alloc (msg_size);
2177 clib_memset (mp, 0, msg_size);
2178 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2180 /* fill in the message */
2181 mp->context = context;
2184 snprintf ((char *) mp->tag, sizeof (mp->tag), "%s", acl->tag);
2185 mp->count = htonl (acl->count);
2186 mp->acl_index = htonl (acl - am->macip_acls);
2188 for (i = 0; i < acl->count; i++)
2191 rules[i].is_permit = r->is_permit;
2192 mac_address_encode ((mac_address_t *) & r->src_mac,
2194 mac_address_encode ((mac_address_t *) & r->src_mac_mask,
2195 rules[i].src_mac_mask);
2196 ip_address_encode (&r->src_ip_addr,
2197 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2198 &rules[i].src_prefix.address);
2199 rules[i].src_prefix.len = r->src_prefixlen;
2204 /* No martini, no party - no ACL applied to this interface. */
2209 vl_api_send_msg (reg, (u8 *) mp);
2214 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2216 acl_main_t *am = &acl_main;
2217 macip_acl_list_t *acl;
2219 vl_api_registration_t *reg;
2221 reg = vl_api_client_index_to_registration (mp->client_index);
2225 if (mp->acl_index == ~0)
2227 /* Just dump all ACLs for now, with sw_if_index = ~0 */
2229 pool_foreach (acl, am->macip_acls)
2231 send_macip_acl_details (am, reg, acl, mp->context);
2237 u32 acl_index = ntohl (mp->acl_index);
2238 if (!pool_is_free_index (am->macip_acls, acl_index))
2240 acl = pool_elt_at_index (am->macip_acls, acl_index);
2241 send_macip_acl_details (am, reg, acl, mp->context);
2247 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2250 acl_main_t *am = &acl_main;
2251 vl_api_macip_acl_interface_get_reply_t *rmp;
2252 u32 count = vec_len (am->macip_acl_by_sw_if_index);
2253 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2254 vl_api_registration_t *reg;
2257 reg = vl_api_client_index_to_registration (mp->client_index);
2261 rmp = vl_msg_api_alloc (msg_size);
2262 clib_memset (rmp, 0, msg_size);
2264 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2265 rmp->context = mp->context;
2266 rmp->count = htonl (count);
2267 for (i = 0; i < count; i++)
2269 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2272 vl_api_send_msg (reg, (u8 *) rmp);
2276 send_macip_acl_interface_list_details (acl_main_t * am,
2277 vl_api_registration_t * reg,
2279 u32 acl_index, u32 context)
2281 vl_api_macip_acl_interface_list_details_t *rmp;
2282 /* at this time there is only ever 1 mac ip acl per interface */
2283 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2285 rmp = vl_msg_api_alloc (msg_size);
2286 clib_memset (rmp, 0, msg_size);
2288 ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2290 /* fill in the message */
2291 rmp->context = context;
2293 rmp->sw_if_index = htonl (sw_if_index);
2294 rmp->acls[0] = htonl (acl_index);
2296 vl_api_send_msg (reg, (u8 *) rmp);
2300 vl_api_macip_acl_interface_list_dump_t_handler
2301 (vl_api_macip_acl_interface_list_dump_t * mp)
2303 vl_api_registration_t *reg;
2304 acl_main_t *am = &acl_main;
2305 u32 sw_if_index = ntohl (mp->sw_if_index);
2307 reg = vl_api_client_index_to_registration (mp->client_index);
2311 if (sw_if_index == ~0)
2313 vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2315 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2317 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2319 macip_acl_by_sw_if_index
2327 if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2329 send_macip_acl_interface_list_details (am, reg, sw_if_index,
2330 am->macip_acl_by_sw_if_index
2331 [sw_if_index], mp->context);
2337 vl_api_acl_interface_set_etype_whitelist_t_handler
2338 (vl_api_acl_interface_set_etype_whitelist_t * mp)
2340 acl_main_t *am = &acl_main;
2341 vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2344 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2345 u32 sw_if_index = ntohl (mp->sw_if_index);
2346 u16 *vec_in = 0, *vec_out = 0;
2348 if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2349 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2352 for (i = 0; i < mp->count; i++)
2354 if (i < mp->n_input)
2355 vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2357 vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2359 rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2362 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2366 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2367 vl_api_registration_t * reg,
2368 u32 sw_if_index, u32 context)
2370 vl_api_acl_interface_etype_whitelist_details_t *mp;
2377 u16 *whitelist_in = 0;
2378 u16 *whitelist_out = 0;
2380 if (intf_has_etype_whitelist (am, sw_if_index, 0))
2382 vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2384 if (intf_has_etype_whitelist (am, sw_if_index, 1))
2386 vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2388 if ((0 == whitelist_in) && (0 == whitelist_out))
2389 return; /* nothing to do */
2391 n_input = vec_len (whitelist_in);
2392 n_output = vec_len (whitelist_out);
2393 count = n_input + n_output;
2395 msg_size = sizeof (*mp);
2396 msg_size += sizeof (mp->whitelist[0]) * count;
2398 mp = vl_msg_api_alloc (msg_size);
2399 clib_memset (mp, 0, msg_size);
2401 ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2403 /* fill in the message */
2404 mp->context = context;
2405 mp->sw_if_index = htonl (sw_if_index);
2407 mp->n_input = n_input;
2408 for (i = 0; i < n_input; i++)
2410 mp->whitelist[i] = htons (whitelist_in[i]);
2412 for (i = 0; i < n_output; i++)
2414 mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2416 vl_api_send_msg (reg, (u8 *) mp);
2421 vl_api_acl_interface_etype_whitelist_dump_t_handler
2422 (vl_api_acl_interface_list_dump_t * mp)
2424 acl_main_t *am = &acl_main;
2425 vnet_sw_interface_t *swif;
2426 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2429 vl_api_registration_t *reg;
2431 reg = vl_api_client_index_to_registration (mp->client_index);
2435 if (mp->sw_if_index == ~0)
2438 pool_foreach (swif, im->sw_interfaces)
2440 send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2446 sw_if_index = ntohl (mp->sw_if_index);
2447 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2448 send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2454 vl_api_acl_plugin_use_hash_lookup_set_t_handler (
2455 vl_api_acl_plugin_use_hash_lookup_set_t *mp)
2457 acl_main_t *am = &acl_main;
2458 vl_api_acl_plugin_use_hash_lookup_set_reply_t *rmp;
2459 vl_api_registration_t *reg;
2462 reg = vl_api_client_index_to_registration (mp->client_index);
2466 am->use_hash_acl_matching = mp->enable;
2467 REPLY_MACRO (VL_API_ACL_PLUGIN_USE_HASH_LOOKUP_SET_REPLY);
2471 vl_api_acl_plugin_use_hash_lookup_get_t_handler (
2472 vl_api_acl_plugin_use_hash_lookup_get_t *mp)
2474 acl_main_t *am = &acl_main;
2475 vl_api_acl_plugin_use_hash_lookup_get_reply_t *rmp;
2476 int msg_size = sizeof (*rmp);
2477 vl_api_registration_t *reg;
2479 reg = vl_api_client_index_to_registration (mp->client_index);
2483 rmp = vl_msg_api_alloc (msg_size);
2484 clib_memset (rmp, 0, msg_size);
2486 ntohs (VL_API_ACL_PLUGIN_USE_HASH_LOOKUP_GET_REPLY + am->msg_id_base);
2487 rmp->context = mp->context;
2488 rmp->enable = am->use_hash_acl_matching;
2489 vl_api_send_msg (reg, (u8 *) rmp);
2493 acl_set_timeout_sec (int timeout_type, u32 value)
2495 acl_main_t *am = &acl_main;
2496 clib_time_t *ct = &am->vlib_main->clib_time;
2498 if (timeout_type < ACL_N_TIMEOUTS)
2500 am->session_timeout_sec[timeout_type] = value;
2504 clib_warning ("Unknown timeout type %d", timeout_type);
2507 am->session_timeout[timeout_type] =
2508 (u64) (((f64) value) / ct->seconds_per_clock);
2512 acl_set_session_max_entries (u32 value)
2514 acl_main_t *am = &acl_main;
2515 am->fa_conn_table_max_entries = value;
2519 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2521 acl_main_t *am = &acl_main;
2523 if ((eh < 256) && (value < 2))
2525 am->fa_ipv6_known_eh_bitmap =
2526 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2534 static clib_error_t *
2535 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2537 acl_main_t *am = &acl_main;
2540 int may_clear_sessions = 1;
2541 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2542 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2544 /* also unapply any ACLs in case the users did not do so. */
2545 macip_acl_interface_del_acl (am, sw_if_index);
2546 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
2547 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
2552 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2556 static clib_error_t *
2557 acl_set_aclplugin_fn (vlib_main_t * vm,
2558 unformat_input_t * input, vlib_cli_command_t * cmd)
2560 clib_error_t *error = 0;
2564 uword memory_size = 0;
2565 acl_main_t *am = &acl_main;
2567 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2569 if (!acl_set_skip_ipv6_eh (eh_val, val))
2571 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2575 if (unformat (input, "use-hash-acl-matching %u", &val))
2577 am->use_hash_acl_matching = (val != 0);
2580 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2582 am->l4_match_nonfirst_fragment = (val != 0);
2585 if (unformat (input, "reclassify-sessions %u", &val))
2587 am->reclassify_sessions = (val != 0);
2590 if (unformat (input, "event-trace"))
2592 if (!unformat (input, "%u", &val))
2594 error = clib_error_return (0,
2595 "expecting trace level, got `%U`",
2596 format_unformat_error, input);
2601 am->trace_acl = val;
2605 if (unformat (input, "heap"))
2607 if (unformat (input, "main"))
2609 if (unformat (input, "validate %u", &val))
2610 clib_warning ("ACL local heap is deprecated");
2611 else if (unformat (input, "trace %u", &val))
2612 clib_warning ("ACL local heap is deprecated");
2615 else if (unformat (input, "hash"))
2617 if (unformat (input, "validate %u", &val))
2618 clib_warning ("ACL local heap is deprecated");
2619 else if (unformat (input, "trace %u", &val))
2620 clib_warning ("ACL local heap is deprecated");
2625 if (unformat (input, "session"))
2627 if (unformat (input, "table"))
2629 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2630 if (unformat (input, "max-entries"))
2632 if (!unformat (input, "%u", &val))
2634 error = clib_error_return (0,
2635 "expecting maximum number of entries, got `%U`",
2636 format_unformat_error, input);
2641 acl_set_session_max_entries (val);
2645 if (unformat (input, "hash-table-buckets"))
2647 if (!unformat (input, "%u", &val))
2649 error = clib_error_return (0,
2650 "expecting maximum number of hash table buckets, got `%U`",
2651 format_unformat_error, input);
2656 am->fa_conn_table_hash_num_buckets = val;
2660 if (unformat (input, "hash-table-memory"))
2662 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2664 error = clib_error_return (0,
2665 "expecting maximum amount of hash table memory, got `%U`",
2666 format_unformat_error, input);
2671 am->fa_conn_table_hash_memory_size = memory_size;
2675 if (unformat (input, "event-trace"))
2677 if (!unformat (input, "%u", &val))
2679 error = clib_error_return (0,
2680 "expecting trace level, got `%U`",
2681 format_unformat_error, input);
2686 am->trace_sessions = val;
2692 if (unformat (input, "timeout"))
2694 if (unformat (input, "udp"))
2696 if (unformat (input, "idle"))
2698 if (!unformat (input, "%u", &timeout))
2700 error = clib_error_return (0,
2701 "expecting timeout value in seconds, got `%U`",
2702 format_unformat_error,
2708 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2713 if (unformat (input, "tcp"))
2715 if (unformat (input, "idle"))
2717 if (!unformat (input, "%u", &timeout))
2719 error = clib_error_return (0,
2720 "expecting timeout value in seconds, got `%U`",
2721 format_unformat_error,
2727 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2731 if (unformat (input, "transient"))
2733 if (!unformat (input, "%u", &timeout))
2735 error = clib_error_return (0,
2736 "expecting timeout value in seconds, got `%U`",
2737 format_unformat_error,
2743 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2757 my_format_mac_address (u8 * s, va_list * args)
2759 u8 *a = va_arg (*args, u8 *);
2760 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2761 a[0], a[1], a[2], a[3], a[4], a[5]);
2765 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2767 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2769 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2770 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2771 format_ip46_address, &a->src_ip_addr,
2772 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2774 my_format_mac_address, a->src_mac,
2775 my_format_mac_address, a->src_mac_mask);
2780 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2782 vlib_main_t *vm = am->vlib_main;
2785 /* Don't try to print someone else's memory */
2786 if (macip_acl_index >= vec_len (am->macip_acls))
2789 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2790 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2792 vlib_cli_output (vm,
2793 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2794 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2796 vlib_cli_output (vm,
2797 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2798 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2799 vlib_cli_output (vm,
2800 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
2801 a->out_ip4_table_index, a->out_ip6_table_index,
2802 a->out_l2_table_index);
2803 for (i = 0; i < vec_len (a->rules); i++)
2804 vlib_cli_output (vm, " rule %d: %U\n", i,
2805 my_macip_acl_rule_t_pretty_format,
2806 vec_elt_at_index (a->rules, i));
2810 static clib_error_t *
2811 acl_set_aclplugin_interface_fn (vlib_main_t * vm,
2812 unformat_input_t * input,
2813 vlib_cli_command_t * cmd)
2815 unformat_input_t _line_input, *line_input = &_line_input;
2816 u32 sw_if_index, is_add, is_input, acl_index;
2818 is_add = is_input = 1;
2819 acl_index = sw_if_index = ~0;
2821 if (!unformat_user (input, unformat_line_input, line_input))
2824 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2826 if (unformat (line_input, "%U",
2827 unformat_vnet_sw_interface, vnet_get_main (),
2830 else if (unformat (line_input, "add"))
2832 else if (unformat (line_input, "del"))
2834 else if (unformat (line_input, "acl %d", &acl_index))
2836 else if (unformat (line_input, "input"))
2838 else if (unformat (line_input, "output"))
2844 if (~0 == sw_if_index)
2845 return (clib_error_return (0, "invalid interface"));
2846 if (~0 == acl_index)
2847 return (clib_error_return (0, "invalid acl"));
2849 acl_interface_add_del_inout_acl (sw_if_index, is_add, is_input, acl_index);
2851 unformat_free (line_input);
2855 #define vec_validate_acl_rules(v, idx) \
2857 if (vec_len(v) < idx+1) { \
2858 vec_validate(v, idx); \
2859 v[idx].is_permit = 0x1; \
2860 v[idx].srcport_or_icmptype_last = 0xffff; \
2861 v[idx].dstport_or_icmpcode_last = 0xffff; \
2865 static clib_error_t *
2866 acl_set_aclplugin_acl_fn (vlib_main_t * vm,
2867 unformat_input_t * input, vlib_cli_command_t * cmd)
2869 unformat_input_t _line_input, *line_input = &_line_input;
2870 vl_api_acl_rule_t *rules = 0;
2873 int n_rules_override = -1;
2878 u32 tcpflags, tcpmask;
2879 ip_prefix_t src, dst;
2882 if (!unformat_user (input, unformat_line_input, line_input))
2885 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2887 if (unformat (line_input, "permit+reflect"))
2889 vec_validate_acl_rules (rules, rule_idx);
2890 rules[rule_idx].is_permit = 2;
2892 else if (unformat (line_input, "permit"))
2894 vec_validate_acl_rules (rules, rule_idx);
2895 rules[rule_idx].is_permit = 1;
2897 else if (unformat (line_input, "deny"))
2899 vec_validate_acl_rules (rules, rule_idx);
2900 rules[rule_idx].is_permit = 0;
2902 else if (unformat (line_input, "count %d", &n_rules_override))
2904 /* we will use this later */
2906 else if (unformat (line_input, "action %d", &action))
2908 vec_validate_acl_rules (rules, rule_idx);
2909 rules[rule_idx].is_permit = action;
2911 else if (unformat (line_input, "src %U", unformat_ip_prefix, &src))
2913 vec_validate_acl_rules (rules, rule_idx);
2914 ip_prefix_encode2 (&src, &rules[rule_idx].src_prefix);
2916 else if (unformat (line_input, "dst %U", unformat_ip_prefix, &dst))
2918 vec_validate_acl_rules (rules, rule_idx);
2919 ip_prefix_encode2 (&dst, &rules[rule_idx].dst_prefix);
2921 else if (unformat (line_input, "sport %d-%d", &port1, &port2))
2923 vec_validate_acl_rules (rules, rule_idx);
2924 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
2925 rules[rule_idx].srcport_or_icmptype_last = htons (port2);
2927 else if (unformat (line_input, "sport %d", &port1))
2929 vec_validate_acl_rules (rules, rule_idx);
2930 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
2931 rules[rule_idx].srcport_or_icmptype_last = htons (port1);
2933 else if (unformat (line_input, "dport %d-%d", &port1, &port2))
2935 vec_validate_acl_rules (rules, rule_idx);
2936 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
2937 rules[rule_idx].dstport_or_icmpcode_last = htons (port2);
2939 else if (unformat (line_input, "dport %d", &port1))
2941 vec_validate_acl_rules (rules, rule_idx);
2942 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
2943 rules[rule_idx].dstport_or_icmpcode_last = htons (port1);
2945 else if (unformat (line_input, "tcpflags %d %d", &tcpflags, &tcpmask))
2947 vec_validate_acl_rules (rules, rule_idx);
2948 rules[rule_idx].tcp_flags_value = tcpflags;
2949 rules[rule_idx].tcp_flags_mask = tcpmask;
2952 if (unformat (line_input, "tcpflags %d mask %d", &tcpflags, &tcpmask))
2954 vec_validate_acl_rules (rules, rule_idx);
2955 rules[rule_idx].tcp_flags_value = tcpflags;
2956 rules[rule_idx].tcp_flags_mask = tcpmask;
2958 else if (unformat (line_input, "proto %d", &proto))
2960 vec_validate_acl_rules (rules, rule_idx);
2961 rules[rule_idx].proto = proto;
2963 else if (unformat (line_input, "tag %s", &tag))
2966 else if (unformat (line_input, ","))
2969 vec_validate_acl_rules (rules, rule_idx);
2977 vec_add (tag, "cli", 4);
2979 rv = acl_add_list (vec_len (rules), rules, &acl_index, tag);
2985 return (clib_error_return (0, "failed"));
2987 vlib_cli_output (vm, "ACL index:%d", acl_index);
2992 static clib_error_t *
2993 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2995 input, vlib_cli_command_t * cmd)
2997 clib_error_t *error = 0;
2998 acl_main_t *am = &acl_main;
3002 (void) unformat (input, "index %u", &acl_index);
3004 for (i = 0; i < vec_len (am->macip_acls); i++)
3006 /* Don't attempt to show the ACLs that do not exist */
3007 if (pool_is_free_index (am->macip_acls, i))
3010 if ((acl_index != ~0) && (acl_index != i))
3015 macip_acl_print (am, i);
3016 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3018 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
3020 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3028 static clib_error_t *
3029 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3031 input, vlib_cli_command_t * cmd)
3033 clib_error_t *error = 0;
3034 acl_main_t *am = &acl_main;
3036 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3038 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3039 vec_elt (am->macip_acl_by_sw_if_index, i));
3045 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3048 vlib_main_t *vm = am->vlib_main;
3050 for (i = 0; i < vec_len (am->acls); i++)
3052 if (acl_is_not_defined (am, i))
3054 /* don't attempt to show the ACLs that do not exist */
3057 if ((acl_index != ~0) && (acl_index != i))
3061 acl_print_acl (vm, am, i);
3063 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3065 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3066 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3069 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3071 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3072 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3075 if (i < vec_len (am->lc_index_vec_by_acl))
3077 vlib_cli_output (vm, " used in lookup context index: %U\n",
3078 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3083 static clib_error_t *
3084 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3085 unformat_input_t * input, vlib_cli_command_t * cmd)
3087 clib_error_t *error = 0;
3088 acl_main_t *am = &acl_main;
3091 (void) unformat (input, "index %u", &acl_index);
3093 acl_plugin_show_acl (am, acl_index);
3097 static clib_error_t *
3098 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3099 unformat_input_t * input,
3100 vlib_cli_command_t * cmd)
3102 clib_error_t *error = 0;
3105 (void) unformat (input, "index %u", &lc_index);
3107 acl_plugin_show_lookup_context (lc_index);
3111 static clib_error_t *
3112 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3113 unformat_input_t * input,
3114 vlib_cli_command_t * cmd)
3116 clib_error_t *error = 0;
3119 (void) unformat (input, "index %u", &lc_index);
3121 acl_plugin_show_lookup_user (lc_index);
3127 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3130 vlib_main_t *vm = am->vlib_main;
3133 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3134 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3136 /* if we need a particular interface, skip all the others */
3137 if ((sw_if_index != ~0) && (sw_if_index != swi))
3140 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3141 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3142 vlib_cli_output (vm, " input policy epoch: %x\n",
3143 vec_elt (am->input_policy_epoch_by_sw_if_index,
3145 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3146 vlib_cli_output (vm, " output policy epoch: %x\n",
3147 vec_elt (am->output_policy_epoch_by_sw_if_index,
3151 if (intf_has_etype_whitelist (am, swi, 1))
3153 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3154 am->input_etype_whitelist_by_sw_if_index[swi],
3157 if (intf_has_etype_whitelist (am, swi, 0))
3159 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3160 am->output_etype_whitelist_by_sw_if_index[swi],
3164 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3165 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3167 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3168 am->input_acl_vec_by_sw_if_index[swi], "%d");
3171 vlib_cli_output (vm, "\n");
3172 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3174 acl_print_acl (vm, am, *pj);
3176 vlib_cli_output (vm, "\n");
3180 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3181 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3183 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3184 am->output_acl_vec_by_sw_if_index[swi], "%d");
3187 vlib_cli_output (vm, "\n");
3188 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3190 acl_print_acl (vm, am, *pj);
3192 vlib_cli_output (vm, "\n");
3195 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3197 vlib_cli_output (vm, " input lookup context index: %d",
3198 am->input_lc_index_by_sw_if_index[swi]);
3200 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3202 vlib_cli_output (vm, " output lookup context index: %d",
3203 am->output_lc_index_by_sw_if_index[swi]);
3210 static clib_error_t *
3211 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3212 unformat_input_t * input,
3213 vlib_cli_command_t * cmd)
3215 clib_error_t *error = 0;
3216 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3219 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3220 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3221 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3222 format_acl_plugin_5tuple, five_tuple);
3224 error = clib_error_return (0, "expecting 6 hex integers");
3229 static clib_error_t *
3230 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3232 input, vlib_cli_command_t * cmd)
3234 clib_error_t *error = 0;
3235 acl_main_t *am = &acl_main;
3237 u32 sw_if_index = ~0;
3238 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3239 int show_acl = unformat (input, "acl");
3240 int detail = unformat (input, "detail");
3242 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3246 static clib_error_t *
3247 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3248 unformat_input_t * input,
3249 vlib_cli_command_t * cmd)
3251 clib_error_t *error = 0;
3252 vlib_cli_output (vm, "ACL memory is now part of the main heap");
3257 acl_plugin_show_sessions (acl_main_t * am,
3258 u32 show_session_thread_id,
3259 u32 show_session_session_index)
3261 vlib_main_t *vm = am->vlib_main;
3263 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3264 vnet_sw_interface_t *swif;
3265 u64 now = clib_cpu_time_now ();
3266 u64 clocks_per_second = am->vlib_main->clib_time.clocks_per_second;
3269 u64 n_adds = am->fa_session_total_adds;
3270 u64 n_dels = am->fa_session_total_dels;
3271 u64 n_deact = am->fa_session_total_deactivations;
3272 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3273 n_dels, n_adds - n_dels);
3274 vlib_cli_output (vm, "Sessions active: add %lu - deact %lu = %lu", n_adds,
3275 n_deact, n_adds - n_deact);
3276 vlib_cli_output (vm, "Sessions being purged: deact %lu - del %lu = %lu",
3277 n_deact, n_dels, n_deact - n_dels);
3279 vlib_cli_output (vm, "now: %lu clocks per second: %lu", now,
3281 vlib_cli_output (vm, "\n\nPer-thread data:");
3282 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3284 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3285 vlib_cli_output (vm, "Thread #%d:", wk);
3286 if (show_session_thread_id == wk
3287 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3289 vlib_cli_output (vm, " session index %u:",
3290 show_session_session_index);
3291 fa_session_t *sess =
3292 pw->fa_sessions_pool + show_session_session_index;
3293 u64 *m = (u64 *) & sess->info;
3294 vlib_cli_output (vm,
3295 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3296 m[0], m[1], m[2], m[3], m[4], m[5]);
3297 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3298 vlib_cli_output (vm, " tcp_flags_seen: %x",
3299 sess->tcp_flags_seen.as_u16);
3300 vlib_cli_output (vm, " last active time: %lu",
3301 sess->last_active_time);
3302 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3303 vlib_cli_output (vm, " link enqueue time: %lu",
3304 sess->link_enqueue_time);
3305 vlib_cli_output (vm, " link next index: %u",
3306 sess->link_next_idx);
3307 vlib_cli_output (vm, " link prev index: %u",
3308 sess->link_prev_idx);
3309 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3311 vlib_cli_output (vm, " connection add/del stats:", wk);
3313 pool_foreach (swif, im->sw_interfaces)
3315 u32 sw_if_index = swif->sw_if_index;
3317 (sw_if_index < vec_len (pw->fa_session_adds_by_sw_if_index) ?
3318 pw->fa_session_adds_by_sw_if_index[sw_if_index] :
3321 (sw_if_index < vec_len (pw->fa_session_dels_by_sw_if_index) ?
3322 pw->fa_session_dels_by_sw_if_index[sw_if_index] :
3324 u64 n_epoch_changes =
3325 (sw_if_index < vec_len (pw->fa_session_epoch_change_by_sw_if_index) ?
3326 pw->fa_session_epoch_change_by_sw_if_index[sw_if_index] :
3328 vlib_cli_output (vm,
3329 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3339 vlib_cli_output (vm, " connection timeout type lists:", wk);
3341 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3343 u32 head_session_index = pw->fa_conn_list_head[tt];
3344 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3345 head_session_index);
3346 if (~0 != head_session_index)
3348 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3349 vlib_cli_output (vm, " last active time: %lu",
3350 sess->last_active_time);
3351 vlib_cli_output (vm, " link enqueue time: %lu",
3352 sess->link_enqueue_time);
3356 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3357 vlib_cli_output (vm, " Requeue until time: %lu",
3358 pw->requeue_until_time);
3359 vlib_cli_output (vm, " Current time wait interval: %lu",
3360 pw->current_time_wait_interval);
3361 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3362 pw->cnt_deleted_sessions);
3363 vlib_cli_output (vm, " Delete already deleted: %lu",
3364 pw->cnt_already_deleted_sessions);
3365 vlib_cli_output (vm, " Session timers restarted: %lu",
3366 pw->cnt_session_timer_restarted);
3367 vlib_cli_output (vm, " Swipe until this time: %lu",
3368 pw->swipe_end_time);
3369 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3370 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3371 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3373 pw->pending_clear_sw_if_index_bitmap);
3374 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3375 vlib_cli_output (vm, " interrupt is pending: %d",
3376 pw->interrupt_is_pending);
3377 vlib_cli_output (vm, " interrupt is needed: %d",
3378 pw->interrupt_is_needed);
3379 vlib_cli_output (vm, " interrupt is unwanted: %d",
3380 pw->interrupt_is_unwanted);
3381 vlib_cli_output (vm, " interrupt generation: %d",
3382 pw->interrupt_generation);
3383 vlib_cli_output (vm, " received session change requests: %d",
3384 pw->rcvd_session_change_requests);
3385 vlib_cli_output (vm, " sent session change requests: %d",
3386 pw->sent_session_change_requests);
3388 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3389 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3390 foreach_fa_cleaner_counter;
3392 vlib_cli_output (vm, "Interrupt generation: %d",
3393 am->fa_interrupt_generation);
3394 vlib_cli_output (vm,
3395 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3396 am->fa_min_deleted_sessions_per_interval,
3397 am->fa_max_deleted_sessions_per_interval,
3398 am->fa_cleaner_wait_time_increment * 1000.0,
3399 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3400 1000.0 / (f64) vm->clib_time.clocks_per_second);
3401 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3404 static clib_error_t *
3405 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3406 unformat_input_t * input,
3407 vlib_cli_command_t * cmd)
3409 clib_error_t *error = 0;
3410 acl_main_t *am = &acl_main;
3412 u32 show_bihash_verbose = 0;
3413 u32 show_session_thread_id = ~0;
3414 u32 show_session_session_index = ~0;
3415 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3416 &show_session_session_index);
3417 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3419 acl_plugin_show_sessions (am, show_session_thread_id,
3420 show_session_session_index);
3421 show_fa_sessions_hash (vm, show_bihash_verbose);
3425 static clib_error_t *
3426 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3427 unformat_input_t * input,
3428 vlib_cli_command_t * cmd)
3430 clib_error_t *error = 0;
3434 int show_acl_hash_info = 0;
3435 int show_applied_info = 0;
3436 int show_mask_type = 0;
3437 int show_bihash = 0;
3438 u32 show_bihash_verbose = 0;
3440 if (unformat (input, "acl"))
3442 show_acl_hash_info = 1;
3443 /* mask-type is handy to see as well right there */
3445 unformat (input, "index %u", &acl_index);
3447 else if (unformat (input, "applied"))
3449 show_applied_info = 1;
3450 unformat (input, "lc_index %u", &lc_index);
3452 else if (unformat (input, "mask"))
3456 else if (unformat (input, "hash"))
3459 unformat (input, "verbose %u", &show_bihash_verbose);
3463 (show_mask_type || show_acl_hash_info || show_applied_info
3466 /* if no qualifiers specified, show all */
3468 show_acl_hash_info = 1;
3469 show_applied_info = 1;
3472 vlib_cli_output (vm, "Stats counters enabled for interface ACLs: %d",
3473 acl_main.interface_acl_counters_enabled);
3474 vlib_cli_output (vm, "Use hash-based lookup for ACLs: %d",
3475 acl_main.use_hash_acl_matching);
3477 acl_plugin_show_tables_mask_type ();
3478 if (show_acl_hash_info)
3479 acl_plugin_show_tables_acl_hash_info (acl_index);
3480 if (show_applied_info)
3481 acl_plugin_show_tables_applied_info (lc_index);
3483 acl_plugin_show_tables_bihash (show_bihash_verbose);
3488 static clib_error_t *
3489 acl_clear_aclplugin_fn (vlib_main_t * vm,
3490 unformat_input_t * input, vlib_cli_command_t * cmd)
3492 clib_error_t *error = 0;
3493 acl_main_t *am = &acl_main;
3494 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3495 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3500 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3501 .path = "set acl-plugin",
3502 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3503 .function = acl_set_aclplugin_fn,
3506 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3507 .path = "show acl-plugin acl",
3508 .short_help = "show acl-plugin acl [index N]",
3509 .function = acl_show_aclplugin_acl_fn,
3512 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3513 .path = "show acl-plugin lookup context",
3514 .short_help = "show acl-plugin lookup context [index N]",
3515 .function = acl_show_aclplugin_lookup_context_fn,
3518 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3519 .path = "show acl-plugin lookup user",
3520 .short_help = "show acl-plugin lookup user [index N]",
3521 .function = acl_show_aclplugin_lookup_user_fn,
3524 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3525 .path = "show acl-plugin decode 5tuple",
3526 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3527 .function = acl_show_aclplugin_decode_5tuple_fn,
3530 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3531 .path = "show acl-plugin interface",
3532 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3533 .function = acl_show_aclplugin_interface_fn,
3536 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3537 .path = "show acl-plugin memory",
3538 .short_help = "show acl-plugin memory",
3539 .function = acl_show_aclplugin_memory_fn,
3542 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3543 .path = "show acl-plugin sessions",
3544 .short_help = "show acl-plugin sessions",
3545 .function = acl_show_aclplugin_sessions_fn,
3548 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3549 .path = "show acl-plugin tables",
3550 .short_help = "show acl-plugin tables [ acl [index N] | applied [ lc_index N ] | mask | hash [verbose N] ]",
3551 .function = acl_show_aclplugin_tables_fn,
3554 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3555 .path = "show acl-plugin macip acl",
3556 .short_help = "show acl-plugin macip acl [index N]",
3557 .function = acl_show_aclplugin_macip_acl_fn,
3560 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3561 .path = "show acl-plugin macip interface",
3562 .short_help = "show acl-plugin macip interface",
3563 .function = acl_show_aclplugin_macip_interface_fn,
3566 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3567 .path = "clear acl-plugin sessions",
3568 .short_help = "clear acl-plugin sessions",
3569 .function = acl_clear_aclplugin_fn,
3573 * [un]Apply an ACL to an interface.
3574 * The ACL is applied in a given direction, either input or output.
3575 * The ACL being applied must already exist.
3578 * <b><em> set acl-plugin interface <input|output> acl <index> [del] </b></em>
3581 VLIB_CLI_COMMAND (aclplugin_set_interface_command, static) = {
3582 .path = "set acl-plugin interface",
3583 .short_help = "set acl-plugin interface <interface> <input|output> <acl INDEX> [del] ",
3584 .function = acl_set_aclplugin_interface_fn,
3588 * Create an Access Control List (ACL)
3589 * an ACL is composed of more than one Access control element (ACE). Multiple
3590 * ACEs can be specified with this command using a comma separated list.
3592 * Each ACE describes a tuple of src+dst IP prefix, ip protocol, src+dst port ranges.
3593 * (the ACL plugin also support ICMP types/codes instead of UDP/TCP ports, but
3594 * this CLI does not).
3596 * An ACL can optionally be assigned a 'tag' - which is an identifier understood
3597 * by the client. VPP does not examine it in any way.
3600 * <b><em> set acl-plugin acl <permit|deny> src <PREFIX> dst <PREFIX> proto <TCP|UDP> sport <X-Y> dport <X-Y> [tag FOO] </b></em>
3603 VLIB_CLI_COMMAND (aclplugin_set_acl_command, static) = {
3604 .path = "set acl-plugin acl",
3605 .short_help = "set acl-plugin acl <permit|deny> src <PREFIX> dst <PREFIX> proto X sport X-Y dport X-Y [tag FOO] {use comma separated list for multiple rules}",
3606 .function = acl_set_aclplugin_acl_fn,
3610 static clib_error_t *
3611 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3613 acl_main_t *am = &acl_main;
3614 u32 conn_table_hash_buckets;
3615 uword conn_table_hash_memory_size;
3616 u32 conn_table_max_entries;
3617 uword main_heap_size;
3618 uword hash_heap_size;
3619 u32 hash_lookup_hash_buckets;
3620 uword hash_lookup_hash_memory;
3621 u32 reclassify_sessions;
3622 u32 use_tuple_merge;
3623 u32 tuple_merge_split_threshold;
3625 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3628 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3629 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3632 (input, "connection hash memory %U", unformat_memory_size,
3633 &conn_table_hash_memory_size))
3634 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3635 else if (unformat (input, "connection count max %d",
3636 &conn_table_max_entries))
3637 am->fa_conn_table_max_entries = conn_table_max_entries;
3640 (input, "main heap size %U", unformat_memory_size,
3643 ("WARNING: ACL heap is now part of the main heap. 'main heap size' is ineffective.");
3646 (input, "hash lookup heap size %U", unformat_memory_size,
3649 ("WARNING: ACL heap is now part of the main heap. 'hash lookup heap size' is ineffective.");
3652 (input, "hash lookup hash buckets %d", &hash_lookup_hash_buckets))
3653 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3656 (input, "hash lookup hash memory %U", unformat_memory_size,
3657 &hash_lookup_hash_memory))
3658 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3659 else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
3660 am->use_tuple_merge = use_tuple_merge;
3663 (input, "tuple merge split threshold %d",
3664 &tuple_merge_split_threshold))
3665 am->tuple_merge_split_threshold = tuple_merge_split_threshold;
3667 else if (unformat (input, "reclassify sessions %d",
3668 &reclassify_sessions))
3669 am->reclassify_sessions = reclassify_sessions;
3672 return clib_error_return (0, "unknown input '%U'",
3673 format_unformat_error, input);
3678 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3680 /* Set up the API message handling tables */
3681 #include <vnet/format_fns.h>
3682 #include <acl/acl.api.c>
3684 static clib_error_t *
3685 acl_init (vlib_main_t * vm)
3687 acl_main_t *am = &acl_main;
3688 clib_error_t *error = 0;
3689 clib_memset (am, 0, sizeof (*am));
3691 am->vnet_main = vnet_get_main ();
3692 am->log_default = vlib_log_register_class ("acl_plugin", 0);
3694 /* Ask for a correctly-sized block of API message decode slots */
3695 am->msg_id_base = setup_message_id_table ();
3697 error = acl_plugin_exports_init (&acl_plugin);
3702 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3703 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3705 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3706 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3707 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3708 TCP_SESSION_IDLE_TIMEOUT_SEC;
3709 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3710 UDP_SESSION_IDLE_TIMEOUT_SEC;
3712 am->fa_conn_table_hash_num_buckets =
3713 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3714 am->fa_conn_table_hash_memory_size =
3715 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3716 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3717 am->reclassify_sessions = 0;
3718 vlib_thread_main_t *tm = vlib_get_thread_main ();
3720 am->fa_min_deleted_sessions_per_interval =
3721 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3722 am->fa_max_deleted_sessions_per_interval =
3723 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3724 am->fa_cleaner_wait_time_increment =
3725 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3727 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3730 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3732 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3733 if (tm->n_vlib_mains > 1)
3735 clib_spinlock_init (&pw->pending_session_change_request_lock);
3737 vec_validate (pw->expired,
3739 am->fa_max_deleted_sessions_per_interval);
3740 _vec_len (pw->expired) = 0;
3741 vec_validate_init_empty (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1,
3742 FA_SESSION_BOGUS_INDEX);
3743 vec_validate_init_empty (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1,
3744 FA_SESSION_BOGUS_INDEX);
3745 vec_validate_init_empty (pw->fa_conn_list_head_expiry_time,
3746 ACL_N_TIMEOUTS - 1, ~0ULL);
3750 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3751 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3752 am->fa_cleaner_cnt_unknown_event = 0;
3753 am->fa_cleaner_cnt_timer_restarted = 0;
3754 am->fa_cleaner_cnt_wait_with_timeout = 0;
3757 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3760 am->l4_match_nonfirst_fragment = 1;
3762 /* use the new fancy hash-based matching */
3763 am->use_hash_acl_matching = 1;
3764 /* use tuplemerge by default */
3765 am->use_tuple_merge = 1;
3766 /* Set the default threshold */
3767 am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
3769 am->interface_acl_user_id =
3770 acl_plugin.register_user_module ("interface ACL", "sw_if_index",
3773 am->acl_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
3774 CLIB_CACHE_LINE_BYTES);
3775 am->acl_counter_lock[0] = 0; /* should be no need */
3780 VLIB_INIT_FUNCTION (acl_init);
3784 * fd.io coding-style-patch-verification: ON
3787 * eval: (c-set-style "gnu")