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 acl_set_timeout_sec (int timeout_type, u32 value)
2456 acl_main_t *am = &acl_main;
2457 clib_time_t *ct = &am->vlib_main->clib_time;
2459 if (timeout_type < ACL_N_TIMEOUTS)
2461 am->session_timeout_sec[timeout_type] = value;
2465 clib_warning ("Unknown timeout type %d", timeout_type);
2468 am->session_timeout[timeout_type] =
2469 (u64) (((f64) value) / ct->seconds_per_clock);
2473 acl_set_session_max_entries (u32 value)
2475 acl_main_t *am = &acl_main;
2476 am->fa_conn_table_max_entries = value;
2480 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2482 acl_main_t *am = &acl_main;
2484 if ((eh < 256) && (value < 2))
2486 am->fa_ipv6_known_eh_bitmap =
2487 clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2495 static clib_error_t *
2496 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2498 acl_main_t *am = &acl_main;
2501 int may_clear_sessions = 1;
2502 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2503 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2505 /* also unapply any ACLs in case the users did not do so. */
2506 macip_acl_interface_del_acl (am, sw_if_index);
2507 acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
2508 acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
2513 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2517 static clib_error_t *
2518 acl_set_aclplugin_fn (vlib_main_t * vm,
2519 unformat_input_t * input, vlib_cli_command_t * cmd)
2521 clib_error_t *error = 0;
2525 uword memory_size = 0;
2526 acl_main_t *am = &acl_main;
2528 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2530 if (!acl_set_skip_ipv6_eh (eh_val, val))
2532 error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2536 if (unformat (input, "use-hash-acl-matching %u", &val))
2538 am->use_hash_acl_matching = (val != 0);
2541 if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2543 am->l4_match_nonfirst_fragment = (val != 0);
2546 if (unformat (input, "reclassify-sessions %u", &val))
2548 am->reclassify_sessions = (val != 0);
2551 if (unformat (input, "event-trace"))
2553 if (!unformat (input, "%u", &val))
2555 error = clib_error_return (0,
2556 "expecting trace level, got `%U`",
2557 format_unformat_error, input);
2562 am->trace_acl = val;
2566 if (unformat (input, "heap"))
2568 if (unformat (input, "main"))
2570 if (unformat (input, "validate %u", &val))
2571 clib_warning ("ACL local heap is deprecated");
2572 else if (unformat (input, "trace %u", &val))
2573 clib_warning ("ACL local heap is deprecated");
2576 else if (unformat (input, "hash"))
2578 if (unformat (input, "validate %u", &val))
2579 clib_warning ("ACL local heap is deprecated");
2580 else if (unformat (input, "trace %u", &val))
2581 clib_warning ("ACL local heap is deprecated");
2586 if (unformat (input, "session"))
2588 if (unformat (input, "table"))
2590 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2591 if (unformat (input, "max-entries"))
2593 if (!unformat (input, "%u", &val))
2595 error = clib_error_return (0,
2596 "expecting maximum number of entries, got `%U`",
2597 format_unformat_error, input);
2602 acl_set_session_max_entries (val);
2606 if (unformat (input, "hash-table-buckets"))
2608 if (!unformat (input, "%u", &val))
2610 error = clib_error_return (0,
2611 "expecting maximum number of hash table buckets, got `%U`",
2612 format_unformat_error, input);
2617 am->fa_conn_table_hash_num_buckets = val;
2621 if (unformat (input, "hash-table-memory"))
2623 if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2625 error = clib_error_return (0,
2626 "expecting maximum amount of hash table memory, got `%U`",
2627 format_unformat_error, input);
2632 am->fa_conn_table_hash_memory_size = memory_size;
2636 if (unformat (input, "event-trace"))
2638 if (!unformat (input, "%u", &val))
2640 error = clib_error_return (0,
2641 "expecting trace level, got `%U`",
2642 format_unformat_error, input);
2647 am->trace_sessions = val;
2653 if (unformat (input, "timeout"))
2655 if (unformat (input, "udp"))
2657 if (unformat (input, "idle"))
2659 if (!unformat (input, "%u", &timeout))
2661 error = clib_error_return (0,
2662 "expecting timeout value in seconds, got `%U`",
2663 format_unformat_error,
2669 acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2674 if (unformat (input, "tcp"))
2676 if (unformat (input, "idle"))
2678 if (!unformat (input, "%u", &timeout))
2680 error = clib_error_return (0,
2681 "expecting timeout value in seconds, got `%U`",
2682 format_unformat_error,
2688 acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2692 if (unformat (input, "transient"))
2694 if (!unformat (input, "%u", &timeout))
2696 error = clib_error_return (0,
2697 "expecting timeout value in seconds, got `%U`",
2698 format_unformat_error,
2704 acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2718 my_format_mac_address (u8 * s, va_list * args)
2720 u8 *a = va_arg (*args, u8 *);
2721 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
2722 a[0], a[1], a[2], a[3], a[4], a[5]);
2726 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
2728 macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
2730 out = format (out, "%s action %d ip %U/%d mac %U mask %U",
2731 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
2732 format_ip46_address, &a->src_ip_addr,
2733 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
2735 my_format_mac_address, a->src_mac,
2736 my_format_mac_address, a->src_mac_mask);
2741 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
2743 vlib_main_t *vm = am->vlib_main;
2746 /* Don't try to print someone else's memory */
2747 if (macip_acl_index >= vec_len (am->macip_acls))
2750 macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
2751 int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
2753 vlib_cli_output (vm,
2754 "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
2755 macip_acl_index, a->count, vec_len (a->rules), a->tag,
2757 vlib_cli_output (vm,
2758 " ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
2759 a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
2760 vlib_cli_output (vm,
2761 " out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
2762 a->out_ip4_table_index, a->out_ip6_table_index,
2763 a->out_l2_table_index);
2764 for (i = 0; i < vec_len (a->rules); i++)
2765 vlib_cli_output (vm, " rule %d: %U\n", i,
2766 my_macip_acl_rule_t_pretty_format,
2767 vec_elt_at_index (a->rules, i));
2771 static clib_error_t *
2772 acl_set_aclplugin_interface_fn (vlib_main_t * vm,
2773 unformat_input_t * input,
2774 vlib_cli_command_t * cmd)
2776 unformat_input_t _line_input, *line_input = &_line_input;
2777 u32 sw_if_index, is_add, is_input, acl_index;
2779 is_add = is_input = 1;
2780 acl_index = sw_if_index = ~0;
2782 if (!unformat_user (input, unformat_line_input, line_input))
2785 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2787 if (unformat (line_input, "%U",
2788 unformat_vnet_sw_interface, vnet_get_main (),
2791 else if (unformat (line_input, "add"))
2793 else if (unformat (line_input, "del"))
2795 else if (unformat (line_input, "acl %d", &acl_index))
2797 else if (unformat (line_input, "input"))
2799 else if (unformat (line_input, "output"))
2805 if (~0 == sw_if_index)
2806 return (clib_error_return (0, "invalid interface"));
2807 if (~0 == acl_index)
2808 return (clib_error_return (0, "invalid acl"));
2810 acl_interface_add_del_inout_acl (sw_if_index, is_add, is_input, acl_index);
2812 unformat_free (line_input);
2816 #define vec_validate_acl_rules(v, idx) \
2818 if (vec_len(v) < idx+1) { \
2819 vec_validate(v, idx); \
2820 v[idx].is_permit = 0x1; \
2821 v[idx].srcport_or_icmptype_last = 0xffff; \
2822 v[idx].dstport_or_icmpcode_last = 0xffff; \
2826 static clib_error_t *
2827 acl_set_aclplugin_acl_fn (vlib_main_t * vm,
2828 unformat_input_t * input, vlib_cli_command_t * cmd)
2830 unformat_input_t _line_input, *line_input = &_line_input;
2831 vl_api_acl_rule_t *rules = 0;
2834 int n_rules_override = -1;
2839 u32 tcpflags, tcpmask;
2840 u32 src_prefix_length = 0, dst_prefix_length = 0;
2841 ip46_address_t src, dst;
2844 if (!unformat_user (input, unformat_line_input, line_input))
2847 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2849 if (unformat (line_input, "permit+reflect"))
2851 vec_validate_acl_rules (rules, rule_idx);
2852 rules[rule_idx].is_permit = 2;
2854 else if (unformat (line_input, "permit"))
2856 vec_validate_acl_rules (rules, rule_idx);
2857 rules[rule_idx].is_permit = 1;
2859 else if (unformat (line_input, "deny"))
2861 vec_validate_acl_rules (rules, rule_idx);
2862 rules[rule_idx].is_permit = 0;
2864 else if (unformat (line_input, "count %d", &n_rules_override))
2866 /* we will use this later */
2868 else if (unformat (line_input, "action %d", &action))
2870 vec_validate_acl_rules (rules, rule_idx);
2871 rules[rule_idx].is_permit = action;
2873 else if (unformat (line_input, "src %U/%d",
2874 unformat_ip46_address, &src, IP46_TYPE_ANY,
2875 &src_prefix_length))
2877 vec_validate_acl_rules (rules, rule_idx);
2878 ip_address_encode (&src, IP46_TYPE_ANY,
2879 &rules[rule_idx].src_prefix.address);
2880 rules[rule_idx].src_prefix.address.af = ADDRESS_IP4;
2881 rules[rule_idx].src_prefix.len = src_prefix_length;
2883 else if (unformat (line_input, "dst %U/%d",
2884 unformat_ip46_address, &dst, IP46_TYPE_ANY,
2885 &dst_prefix_length))
2887 vec_validate_acl_rules (rules, rule_idx);
2888 ip_address_encode (&dst, IP46_TYPE_ANY,
2889 &rules[rule_idx].dst_prefix.address);
2890 rules[rule_idx].dst_prefix.address.af = ADDRESS_IP4;
2891 rules[rule_idx].dst_prefix.len = dst_prefix_length;
2893 else if (unformat (line_input, "sport %d-%d", &port1, &port2))
2895 vec_validate_acl_rules (rules, rule_idx);
2896 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
2897 rules[rule_idx].srcport_or_icmptype_last = htons (port2);
2899 else if (unformat (line_input, "sport %d", &port1))
2901 vec_validate_acl_rules (rules, rule_idx);
2902 rules[rule_idx].srcport_or_icmptype_first = htons (port1);
2903 rules[rule_idx].srcport_or_icmptype_last = htons (port1);
2905 else if (unformat (line_input, "dport %d-%d", &port1, &port2))
2907 vec_validate_acl_rules (rules, rule_idx);
2908 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
2909 rules[rule_idx].dstport_or_icmpcode_last = htons (port2);
2911 else if (unformat (line_input, "dport %d", &port1))
2913 vec_validate_acl_rules (rules, rule_idx);
2914 rules[rule_idx].dstport_or_icmpcode_first = htons (port1);
2915 rules[rule_idx].dstport_or_icmpcode_last = htons (port1);
2917 else if (unformat (line_input, "tcpflags %d %d", &tcpflags, &tcpmask))
2919 vec_validate_acl_rules (rules, rule_idx);
2920 rules[rule_idx].tcp_flags_value = tcpflags;
2921 rules[rule_idx].tcp_flags_mask = tcpmask;
2924 if (unformat (line_input, "tcpflags %d mask %d", &tcpflags, &tcpmask))
2926 vec_validate_acl_rules (rules, rule_idx);
2927 rules[rule_idx].tcp_flags_value = tcpflags;
2928 rules[rule_idx].tcp_flags_mask = tcpmask;
2930 else if (unformat (line_input, "proto %d", &proto))
2932 vec_validate_acl_rules (rules, rule_idx);
2933 rules[rule_idx].proto = proto;
2935 else if (unformat (line_input, "tag %s", &tag))
2938 else if (unformat (line_input, ","))
2941 vec_validate_acl_rules (rules, rule_idx);
2949 vec_add (tag, "cli", 4);
2951 rv = acl_add_list (vec_len (rules), rules, &acl_index, tag);
2957 return (clib_error_return (0, "failed"));
2959 vlib_cli_output (vm, "ACL index:%d", acl_index);
2964 static clib_error_t *
2965 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
2967 input, vlib_cli_command_t * cmd)
2969 clib_error_t *error = 0;
2970 acl_main_t *am = &acl_main;
2974 (void) unformat (input, "index %u", &acl_index);
2976 for (i = 0; i < vec_len (am->macip_acls); i++)
2978 /* Don't attempt to show the ACLs that do not exist */
2979 if (pool_is_free_index (am->macip_acls, i))
2982 if ((acl_index != ~0) && (acl_index != i))
2987 macip_acl_print (am, i);
2988 if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
2990 vlib_cli_output (vm, " applied on sw_if_index(s): %U\n",
2992 vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3000 static clib_error_t *
3001 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3003 input, vlib_cli_command_t * cmd)
3005 clib_error_t *error = 0;
3006 acl_main_t *am = &acl_main;
3008 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3010 vlib_cli_output (vm, " sw_if_index %d: %d\n", i,
3011 vec_elt (am->macip_acl_by_sw_if_index, i));
3017 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3020 vlib_main_t *vm = am->vlib_main;
3022 for (i = 0; i < vec_len (am->acls); i++)
3024 if (acl_is_not_defined (am, i))
3026 /* don't attempt to show the ACLs that do not exist */
3029 if ((acl_index != ~0) && (acl_index != i))
3033 acl_print_acl (vm, am, i);
3035 if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3037 vlib_cli_output (vm, " applied inbound on sw_if_index: %U\n",
3038 format_vec32, am->input_sw_if_index_vec_by_acl[i],
3041 if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3043 vlib_cli_output (vm, " applied outbound on sw_if_index: %U\n",
3044 format_vec32, am->output_sw_if_index_vec_by_acl[i],
3047 if (i < vec_len (am->lc_index_vec_by_acl))
3049 vlib_cli_output (vm, " used in lookup context index: %U\n",
3050 format_vec32, am->lc_index_vec_by_acl[i], "%d");
3055 static clib_error_t *
3056 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3057 unformat_input_t * input, vlib_cli_command_t * cmd)
3059 clib_error_t *error = 0;
3060 acl_main_t *am = &acl_main;
3063 (void) unformat (input, "index %u", &acl_index);
3065 acl_plugin_show_acl (am, acl_index);
3069 static clib_error_t *
3070 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3071 unformat_input_t * input,
3072 vlib_cli_command_t * cmd)
3074 clib_error_t *error = 0;
3077 (void) unformat (input, "index %u", &lc_index);
3079 acl_plugin_show_lookup_context (lc_index);
3083 static clib_error_t *
3084 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3085 unformat_input_t * input,
3086 vlib_cli_command_t * cmd)
3088 clib_error_t *error = 0;
3091 (void) unformat (input, "index %u", &lc_index);
3093 acl_plugin_show_lookup_user (lc_index);
3099 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3102 vlib_main_t *vm = am->vlib_main;
3105 for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3106 (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3108 /* if we need a particular interface, skip all the others */
3109 if ((sw_if_index != ~0) && (sw_if_index != swi))
3112 vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3113 if (swi < vec_len (am->input_policy_epoch_by_sw_if_index))
3114 vlib_cli_output (vm, " input policy epoch: %x\n",
3115 vec_elt (am->input_policy_epoch_by_sw_if_index,
3117 if (swi < vec_len (am->output_policy_epoch_by_sw_if_index))
3118 vlib_cli_output (vm, " output policy epoch: %x\n",
3119 vec_elt (am->output_policy_epoch_by_sw_if_index,
3123 if (intf_has_etype_whitelist (am, swi, 1))
3125 vlib_cli_output (vm, " input etype whitelist: %U", format_vec16,
3126 am->input_etype_whitelist_by_sw_if_index[swi],
3129 if (intf_has_etype_whitelist (am, swi, 0))
3131 vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3132 am->output_etype_whitelist_by_sw_if_index[swi],
3136 if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3137 (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3139 vlib_cli_output (vm, " input acl(s): %U", format_vec32,
3140 am->input_acl_vec_by_sw_if_index[swi], "%d");
3143 vlib_cli_output (vm, "\n");
3144 vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3146 acl_print_acl (vm, am, *pj);
3148 vlib_cli_output (vm, "\n");
3152 if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3153 (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3155 vlib_cli_output (vm, " output acl(s): %U", format_vec32,
3156 am->output_acl_vec_by_sw_if_index[swi], "%d");
3159 vlib_cli_output (vm, "\n");
3160 vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3162 acl_print_acl (vm, am, *pj);
3164 vlib_cli_output (vm, "\n");
3167 if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3169 vlib_cli_output (vm, " input lookup context index: %d",
3170 am->input_lc_index_by_sw_if_index[swi]);
3172 if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3174 vlib_cli_output (vm, " output lookup context index: %d",
3175 am->output_lc_index_by_sw_if_index[swi]);
3182 static clib_error_t *
3183 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3184 unformat_input_t * input,
3185 vlib_cli_command_t * cmd)
3187 clib_error_t *error = 0;
3188 u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3191 (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3192 &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3193 vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3194 format_acl_plugin_5tuple, five_tuple);
3196 error = clib_error_return (0, "expecting 6 hex integers");
3201 static clib_error_t *
3202 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3204 input, vlib_cli_command_t * cmd)
3206 clib_error_t *error = 0;
3207 acl_main_t *am = &acl_main;
3209 u32 sw_if_index = ~0;
3210 (void) unformat (input, "sw_if_index %u", &sw_if_index);
3211 int show_acl = unformat (input, "acl");
3212 int detail = unformat (input, "detail");
3214 acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3218 static clib_error_t *
3219 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3220 unformat_input_t * input,
3221 vlib_cli_command_t * cmd)
3223 clib_error_t *error = 0;
3224 vlib_cli_output (vm, "ACL memory is now part of the main heap");
3229 acl_plugin_show_sessions (acl_main_t * am,
3230 u32 show_session_thread_id,
3231 u32 show_session_session_index)
3233 vlib_main_t *vm = am->vlib_main;
3235 vnet_interface_main_t *im = &am->vnet_main->interface_main;
3236 vnet_sw_interface_t *swif;
3237 u64 now = clib_cpu_time_now ();
3238 u64 clocks_per_second = am->vlib_main->clib_time.clocks_per_second;
3241 u64 n_adds = am->fa_session_total_adds;
3242 u64 n_dels = am->fa_session_total_dels;
3243 u64 n_deact = am->fa_session_total_deactivations;
3244 vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3245 n_dels, n_adds - n_dels);
3246 vlib_cli_output (vm, "Sessions active: add %lu - deact %lu = %lu", n_adds,
3247 n_deact, n_adds - n_deact);
3248 vlib_cli_output (vm, "Sessions being purged: deact %lu - del %lu = %lu",
3249 n_deact, n_dels, n_deact - n_dels);
3251 vlib_cli_output (vm, "now: %lu clocks per second: %lu", now,
3253 vlib_cli_output (vm, "\n\nPer-thread data:");
3254 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3256 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3257 vlib_cli_output (vm, "Thread #%d:", wk);
3258 if (show_session_thread_id == wk
3259 && show_session_session_index < pool_len (pw->fa_sessions_pool))
3261 vlib_cli_output (vm, " session index %u:",
3262 show_session_session_index);
3263 fa_session_t *sess =
3264 pw->fa_sessions_pool + show_session_session_index;
3265 u64 *m = (u64 *) & sess->info;
3266 vlib_cli_output (vm,
3267 " info: %016llx %016llx %016llx %016llx %016llx %016llx",
3268 m[0], m[1], m[2], m[3], m[4], m[5]);
3269 vlib_cli_output (vm, " sw_if_index: %u", sess->sw_if_index);
3270 vlib_cli_output (vm, " tcp_flags_seen: %x",
3271 sess->tcp_flags_seen.as_u16);
3272 vlib_cli_output (vm, " last active time: %lu",
3273 sess->last_active_time);
3274 vlib_cli_output (vm, " thread index: %u", sess->thread_index);
3275 vlib_cli_output (vm, " link enqueue time: %lu",
3276 sess->link_enqueue_time);
3277 vlib_cli_output (vm, " link next index: %u",
3278 sess->link_next_idx);
3279 vlib_cli_output (vm, " link prev index: %u",
3280 sess->link_prev_idx);
3281 vlib_cli_output (vm, " link list id: %u", sess->link_list_id);
3283 vlib_cli_output (vm, " connection add/del stats:", wk);
3285 pool_foreach (swif, im->sw_interfaces)
3287 u32 sw_if_index = swif->sw_if_index;
3289 (sw_if_index < vec_len (pw->fa_session_adds_by_sw_if_index) ?
3290 pw->fa_session_adds_by_sw_if_index[sw_if_index] :
3293 (sw_if_index < vec_len (pw->fa_session_dels_by_sw_if_index) ?
3294 pw->fa_session_dels_by_sw_if_index[sw_if_index] :
3296 u64 n_epoch_changes =
3297 (sw_if_index < vec_len (pw->fa_session_epoch_change_by_sw_if_index) ?
3298 pw->fa_session_epoch_change_by_sw_if_index[sw_if_index] :
3300 vlib_cli_output (vm,
3301 " sw_if_index %d: add %lu - del %lu = %lu; epoch chg: %lu",
3311 vlib_cli_output (vm, " connection timeout type lists:", wk);
3313 for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3315 u32 head_session_index = pw->fa_conn_list_head[tt];
3316 vlib_cli_output (vm, " fa_conn_list_head[%d]: %d", tt,
3317 head_session_index);
3318 if (~0 != head_session_index)
3320 fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3321 vlib_cli_output (vm, " last active time: %lu",
3322 sess->last_active_time);
3323 vlib_cli_output (vm, " link enqueue time: %lu",
3324 sess->link_enqueue_time);
3328 vlib_cli_output (vm, " Next expiry time: %lu", pw->next_expiry_time);
3329 vlib_cli_output (vm, " Requeue until time: %lu",
3330 pw->requeue_until_time);
3331 vlib_cli_output (vm, " Current time wait interval: %lu",
3332 pw->current_time_wait_interval);
3333 vlib_cli_output (vm, " Count of deleted sessions: %lu",
3334 pw->cnt_deleted_sessions);
3335 vlib_cli_output (vm, " Delete already deleted: %lu",
3336 pw->cnt_already_deleted_sessions);
3337 vlib_cli_output (vm, " Session timers restarted: %lu",
3338 pw->cnt_session_timer_restarted);
3339 vlib_cli_output (vm, " Swipe until this time: %lu",
3340 pw->swipe_end_time);
3341 vlib_cli_output (vm, " sw_if_index serviced bitmap: %U",
3342 format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3343 vlib_cli_output (vm, " pending clear intfc bitmap : %U",
3345 pw->pending_clear_sw_if_index_bitmap);
3346 vlib_cli_output (vm, " clear in progress: %u", pw->clear_in_process);
3347 vlib_cli_output (vm, " interrupt is pending: %d",
3348 pw->interrupt_is_pending);
3349 vlib_cli_output (vm, " interrupt is needed: %d",
3350 pw->interrupt_is_needed);
3351 vlib_cli_output (vm, " interrupt is unwanted: %d",
3352 pw->interrupt_is_unwanted);
3353 vlib_cli_output (vm, " interrupt generation: %d",
3354 pw->interrupt_generation);
3355 vlib_cli_output (vm, " received session change requests: %d",
3356 pw->rcvd_session_change_requests);
3357 vlib_cli_output (vm, " sent session change requests: %d",
3358 pw->sent_session_change_requests);
3360 vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3361 #define _(cnt, desc) vlib_cli_output(vm, " %20lu: %s", am->cnt, desc);
3362 foreach_fa_cleaner_counter;
3364 vlib_cli_output (vm, "Interrupt generation: %d",
3365 am->fa_interrupt_generation);
3366 vlib_cli_output (vm,
3367 "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3368 am->fa_min_deleted_sessions_per_interval,
3369 am->fa_max_deleted_sessions_per_interval,
3370 am->fa_cleaner_wait_time_increment * 1000.0,
3371 ((f64) am->fa_current_cleaner_timer_wait_interval) *
3372 1000.0 / (f64) vm->clib_time.clocks_per_second);
3373 vlib_cli_output (vm, "Reclassify sessions: %d", am->reclassify_sessions);
3376 static clib_error_t *
3377 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3378 unformat_input_t * input,
3379 vlib_cli_command_t * cmd)
3381 clib_error_t *error = 0;
3382 acl_main_t *am = &acl_main;
3384 u32 show_bihash_verbose = 0;
3385 u32 show_session_thread_id = ~0;
3386 u32 show_session_session_index = ~0;
3387 (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3388 &show_session_session_index);
3389 (void) unformat (input, "verbose %u", &show_bihash_verbose);
3391 acl_plugin_show_sessions (am, show_session_thread_id,
3392 show_session_session_index);
3393 show_fa_sessions_hash (vm, show_bihash_verbose);
3397 static clib_error_t *
3398 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3399 unformat_input_t * input,
3400 vlib_cli_command_t * cmd)
3402 clib_error_t *error = 0;
3406 int show_acl_hash_info = 0;
3407 int show_applied_info = 0;
3408 int show_mask_type = 0;
3409 int show_bihash = 0;
3410 u32 show_bihash_verbose = 0;
3412 if (unformat (input, "acl"))
3414 show_acl_hash_info = 1;
3415 /* mask-type is handy to see as well right there */
3417 unformat (input, "index %u", &acl_index);
3419 else if (unformat (input, "applied"))
3421 show_applied_info = 1;
3422 unformat (input, "lc_index %u", &lc_index);
3424 else if (unformat (input, "mask"))
3428 else if (unformat (input, "hash"))
3431 unformat (input, "verbose %u", &show_bihash_verbose);
3435 (show_mask_type || show_acl_hash_info || show_applied_info
3438 /* if no qualifiers specified, show all */
3440 show_acl_hash_info = 1;
3441 show_applied_info = 1;
3444 vlib_cli_output (vm, "Stats counters enabled for interface ACLs: %d",
3445 acl_main.interface_acl_counters_enabled);
3447 acl_plugin_show_tables_mask_type ();
3448 if (show_acl_hash_info)
3449 acl_plugin_show_tables_acl_hash_info (acl_index);
3450 if (show_applied_info)
3451 acl_plugin_show_tables_applied_info (lc_index);
3453 acl_plugin_show_tables_bihash (show_bihash_verbose);
3458 static clib_error_t *
3459 acl_clear_aclplugin_fn (vlib_main_t * vm,
3460 unformat_input_t * input, vlib_cli_command_t * cmd)
3462 clib_error_t *error = 0;
3463 acl_main_t *am = &acl_main;
3464 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3465 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3470 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3471 .path = "set acl-plugin",
3472 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3473 .function = acl_set_aclplugin_fn,
3476 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3477 .path = "show acl-plugin acl",
3478 .short_help = "show acl-plugin acl [index N]",
3479 .function = acl_show_aclplugin_acl_fn,
3482 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3483 .path = "show acl-plugin lookup context",
3484 .short_help = "show acl-plugin lookup context [index N]",
3485 .function = acl_show_aclplugin_lookup_context_fn,
3488 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3489 .path = "show acl-plugin lookup user",
3490 .short_help = "show acl-plugin lookup user [index N]",
3491 .function = acl_show_aclplugin_lookup_user_fn,
3494 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3495 .path = "show acl-plugin decode 5tuple",
3496 .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3497 .function = acl_show_aclplugin_decode_5tuple_fn,
3500 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3501 .path = "show acl-plugin interface",
3502 .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3503 .function = acl_show_aclplugin_interface_fn,
3506 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3507 .path = "show acl-plugin memory",
3508 .short_help = "show acl-plugin memory",
3509 .function = acl_show_aclplugin_memory_fn,
3512 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3513 .path = "show acl-plugin sessions",
3514 .short_help = "show acl-plugin sessions",
3515 .function = acl_show_aclplugin_sessions_fn,
3518 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3519 .path = "show acl-plugin tables",
3520 .short_help = "show acl-plugin tables [ acl [index N] | applied [ lc_index N ] | mask | hash [verbose N] ]",
3521 .function = acl_show_aclplugin_tables_fn,
3524 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3525 .path = "show acl-plugin macip acl",
3526 .short_help = "show acl-plugin macip acl [index N]",
3527 .function = acl_show_aclplugin_macip_acl_fn,
3530 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3531 .path = "show acl-plugin macip interface",
3532 .short_help = "show acl-plugin macip interface",
3533 .function = acl_show_aclplugin_macip_interface_fn,
3536 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3537 .path = "clear acl-plugin sessions",
3538 .short_help = "clear acl-plugin sessions",
3539 .function = acl_clear_aclplugin_fn,
3543 * [un]Apply an ACL to an interface.
3544 * The ACL is applied in a given direction, either input or output.
3545 * The ACL being applied must already exist.
3548 * <b><em> set acl-plugin interface <input|output> acl <index> [del] </b></em>
3551 VLIB_CLI_COMMAND (aclplugin_set_interface_command, static) = {
3552 .path = "set acl-plugin interface",
3553 .short_help = "set acl-plugin interface <interface> <input|output> <acl INDEX> [del] ",
3554 .function = acl_set_aclplugin_interface_fn,
3558 * Create an Access Control List (ACL)
3559 * an ACL is composed of more than one Access control element (ACE). Multiple
3560 * ACEs can be specified with this command using a comma separated list.
3562 * Each ACE describes a tuple of src+dst IP prefix, ip protocol, src+dst port ranges.
3563 * (the ACL plugin also support ICMP types/codes instead of UDP/TCP ports, but
3564 * this CLI does not).
3566 * An ACL can optionally be assigned a 'tag' - which is an identifier understood
3567 * by the client. VPP does not examine it in any way.
3570 * <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>
3573 VLIB_CLI_COMMAND (aclplugin_set_acl_command, static) = {
3574 .path = "set acl-plugin acl",
3575 .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}",
3576 .function = acl_set_aclplugin_acl_fn,
3580 static clib_error_t *
3581 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3583 acl_main_t *am = &acl_main;
3584 u32 conn_table_hash_buckets;
3585 uword conn_table_hash_memory_size;
3586 u32 conn_table_max_entries;
3587 uword main_heap_size;
3588 uword hash_heap_size;
3589 u32 hash_lookup_hash_buckets;
3590 uword hash_lookup_hash_memory;
3591 u32 reclassify_sessions;
3592 u32 use_tuple_merge;
3593 u32 tuple_merge_split_threshold;
3595 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3598 (input, "connection hash buckets %d", &conn_table_hash_buckets))
3599 am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3602 (input, "connection hash memory %U", unformat_memory_size,
3603 &conn_table_hash_memory_size))
3604 am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3605 else if (unformat (input, "connection count max %d",
3606 &conn_table_max_entries))
3607 am->fa_conn_table_max_entries = conn_table_max_entries;
3610 (input, "main heap size %U", unformat_memory_size,
3613 ("WARNING: ACL heap is now part of the main heap. 'main heap size' is ineffective.");
3616 (input, "hash lookup heap size %U", unformat_memory_size,
3619 ("WARNING: ACL heap is now part of the main heap. 'hash lookup heap size' is ineffective.");
3622 (input, "hash lookup hash buckets %d", &hash_lookup_hash_buckets))
3623 am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3626 (input, "hash lookup hash memory %U", unformat_memory_size,
3627 &hash_lookup_hash_memory))
3628 am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3629 else if (unformat (input, "use tuple merge %d", &use_tuple_merge))
3630 am->use_tuple_merge = use_tuple_merge;
3633 (input, "tuple merge split threshold %d",
3634 &tuple_merge_split_threshold))
3635 am->tuple_merge_split_threshold = tuple_merge_split_threshold;
3637 else if (unformat (input, "reclassify sessions %d",
3638 &reclassify_sessions))
3639 am->reclassify_sessions = reclassify_sessions;
3642 return clib_error_return (0, "unknown input '%U'",
3643 format_unformat_error, input);
3648 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3650 /* Set up the API message handling tables */
3651 #include <vnet/format_fns.h>
3652 #include <acl/acl.api.c>
3654 static clib_error_t *
3655 acl_init (vlib_main_t * vm)
3657 acl_main_t *am = &acl_main;
3658 clib_error_t *error = 0;
3659 clib_memset (am, 0, sizeof (*am));
3661 am->vnet_main = vnet_get_main ();
3662 am->log_default = vlib_log_register_class ("acl_plugin", 0);
3664 /* Ask for a correctly-sized block of API message decode slots */
3665 am->msg_id_base = setup_message_id_table ();
3667 error = acl_plugin_exports_init (&acl_plugin);
3672 am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3673 am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3675 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3676 TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3677 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3678 TCP_SESSION_IDLE_TIMEOUT_SEC;
3679 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3680 UDP_SESSION_IDLE_TIMEOUT_SEC;
3682 am->fa_conn_table_hash_num_buckets =
3683 ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3684 am->fa_conn_table_hash_memory_size =
3685 ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3686 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3687 am->reclassify_sessions = 0;
3688 vlib_thread_main_t *tm = vlib_get_thread_main ();
3690 am->fa_min_deleted_sessions_per_interval =
3691 ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3692 am->fa_max_deleted_sessions_per_interval =
3693 ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3694 am->fa_cleaner_wait_time_increment =
3695 ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3697 vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3700 for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3702 acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3703 if (tm->n_vlib_mains > 1)
3705 clib_spinlock_init (&pw->pending_session_change_request_lock);
3707 vec_validate (pw->expired,
3709 am->fa_max_deleted_sessions_per_interval);
3710 _vec_len (pw->expired) = 0;
3711 vec_validate_init_empty (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1,
3712 FA_SESSION_BOGUS_INDEX);
3713 vec_validate_init_empty (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1,
3714 FA_SESSION_BOGUS_INDEX);
3715 vec_validate_init_empty (pw->fa_conn_list_head_expiry_time,
3716 ACL_N_TIMEOUTS - 1, ~0ULL);
3720 am->fa_cleaner_cnt_delete_by_sw_index = 0;
3721 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3722 am->fa_cleaner_cnt_unknown_event = 0;
3723 am->fa_cleaner_cnt_timer_restarted = 0;
3724 am->fa_cleaner_cnt_wait_with_timeout = 0;
3727 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3730 am->l4_match_nonfirst_fragment = 1;
3732 /* use the new fancy hash-based matching */
3733 am->use_hash_acl_matching = 1;
3734 /* use tuplemerge by default */
3735 am->use_tuple_merge = 1;
3736 /* Set the default threshold */
3737 am->tuple_merge_split_threshold = TM_SPLIT_THRESHOLD;
3739 am->interface_acl_user_id =
3740 acl_plugin.register_user_module ("interface ACL", "sw_if_index",
3743 am->acl_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
3744 CLIB_CACHE_LINE_BYTES);
3745 am->acl_counter_lock[0] = 0; /* should be no need */
3750 VLIB_INIT_FUNCTION (acl_init);
3754 * fd.io coding-style-patch-verification: ON
3757 * eval: (c-set-style "gnu")