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>
21 #include <acl/l2sess.h>
23 #include <vnet/l2/l2_classify.h>
24 #include <vnet/classify/input_acl.h>
25 #include <vpp/app/version.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29 #include <vlibsocket/api.h>
31 /* define message IDs */
32 #include <acl/acl_msg_enum.h>
34 /* define message structures */
36 #include <acl/acl_all_api_h.h>
39 /* define generated endian-swappers */
41 #include <acl/acl_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <acl/acl_all_api_h.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <acl/acl_all_api_h.h>
61 #define REPLY_MSG_ID_BASE am->msg_id_base
62 #include <vlibapi/api_helper_macros.h>
64 /* List of message types that this plugin understands */
66 #define foreach_acl_plugin_api_msg \
67 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
68 _(ACL_ADD_REPLACE, acl_add_replace) \
70 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
71 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
72 _(ACL_DUMP, acl_dump) \
73 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
74 _(MACIP_ACL_ADD, macip_acl_add) \
75 _(MACIP_ACL_DEL, macip_acl_del) \
76 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
77 _(MACIP_ACL_DUMP, macip_acl_dump) \
78 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
81 VLIB_PLUGIN_REGISTER () = {
82 .version = VPP_BUILD_VER,
83 .description = "Access Control Lists",
88 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
90 acl_main_t *am = &acl_main;
91 vl_api_acl_plugin_get_version_reply_t *rmp;
92 int msg_size = sizeof (*rmp);
93 unix_shared_memory_queue_t *q;
95 q = vl_api_client_index_to_input_queue (mp->client_index);
101 rmp = vl_msg_api_alloc (msg_size);
102 memset (rmp, 0, msg_size);
104 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
105 rmp->context = mp->context;
106 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
107 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
109 vl_msg_api_send_shmem (q, (u8 *) & rmp);
114 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
115 u32 * acl_list_index, u8 * tag)
117 acl_main_t *am = &acl_main;
120 acl_rule_t *acl_new_rules;
123 if (*acl_list_index != ~0)
125 /* They supplied some number, let's see if this ACL exists */
126 if (pool_is_free_index (am->acls, *acl_list_index))
128 /* tried to replace a non-existent ACL, no point doing anything */
133 /* Create and populate the rules */
134 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
135 CLIB_CACHE_LINE_BYTES);
138 /* Could not allocate rules. New or existing ACL - bail out regardless */
142 for (i = 0; i < count; i++)
144 r = &acl_new_rules[i];
145 r->is_permit = rules[i].is_permit;
146 r->is_ipv6 = rules[i].is_ipv6;
149 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
150 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
154 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
155 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
157 r->src_prefixlen = rules[i].src_ip_prefix_len;
158 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
159 r->proto = rules[i].proto;
160 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
161 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
162 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
163 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
164 r->tcp_flags_value = rules[i].tcp_flags_value;
165 r->tcp_flags_mask = rules[i].tcp_flags_mask;
168 if (~0 == *acl_list_index)
171 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
172 memset (a, 0, sizeof (*a));
173 /* Will return the newly allocated ACL index */
174 *acl_list_index = a - am->acls;
178 a = am->acls + *acl_list_index;
179 /* Get rid of the old rules */
180 clib_mem_free (a->rules);
182 a->rules = acl_new_rules;
184 memcpy (a->tag, tag, sizeof (a->tag));
190 acl_del_list (u32 acl_list_index)
192 acl_main_t *am = &acl_main;
195 if (pool_is_free_index (am->acls, acl_list_index))
200 /* delete any references to the ACL */
201 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
203 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
206 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
208 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
216 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
218 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
221 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
223 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
232 /* now we can delete the ACL itself */
233 a = &am->acls[acl_list_index];
236 clib_mem_free (a->rules);
238 pool_put (am->acls, a);
242 /* Some aids in ASCII graphing the content */
248 u8 ip4_5tuple_mask[] =
249 _(" dmac smac etype ")
250 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
257 _(" ttl pr checksum ")
266 _("L4 T/U sport dport ")
276 u8 ip6_5tuple_mask[] =
277 _(" dmac smac etype ")
278 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
280 _(0x0000) __ __ __ __
282 _(0x0004) __ __ XX __
284 _(0x0008) XX XX XX XX
285 _(0x000C) XX XX XX XX
286 _(0x0010) XX XX XX XX
287 _(0x0014) XX XX XX XX
289 _(0x0018) XX XX XX XX
290 _(0x001C) XX XX XX XX
291 _(0x0020) XX XX XX XX
292 _(0x0024) XX XX XX XX
293 _("L4T/U sport dport ")
294 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
301 static int count_skip (u8 * p, u32 size)
303 u64 *p64 = (u64 *) p;
304 /* Be tolerant to null pointer */
308 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
312 return (p64 - (u64 *) p) / 2;
316 acl_classify_add_del_table_big (vnet_classify_main_t * cm, u8 * mask,
317 u32 mask_len, u32 next_table_index,
318 u32 miss_next_index, u32 * table_index,
321 u32 nbuckets = 65536;
322 u32 memory_size = 2 << 30;
323 u32 skip = count_skip (mask, mask_len);
324 u32 match = (mask_len / 16) - skip;
325 u8 *skip_mask_ptr = mask + 16 * skip;
326 u32 current_data_flag = 0;
327 int current_data_offset = 0;
332 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
333 memory_size, skip, match,
334 next_table_index, miss_next_index,
335 table_index, current_data_flag,
336 current_data_offset, is_add,
337 1 /* delete_chain */);
341 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
342 u32 mask_len, u32 next_table_index,
343 u32 miss_next_index, u32 * table_index,
347 u32 memory_size = 2 << 20;
348 u32 skip = count_skip (mask, mask_len);
349 u32 match = (mask_len / 16) - skip;
350 u8 *skip_mask_ptr = mask + 16 * skip;
351 u32 current_data_flag = 0;
352 int current_data_offset = 0;
357 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
358 memory_size, skip, match,
359 next_table_index, miss_next_index,
360 table_index, current_data_flag,
361 current_data_offset, is_add,
362 1 /* delete_chain */);
367 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
369 vnet_classify_main_t *cm = &vnet_classify_main;
370 u32 ip4_table_index = ~0;
371 u32 ip6_table_index = ~0;
373 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
375 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
378 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
380 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
383 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
384 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
385 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
386 sizeof (ip4_5tuple_mask) - 1, ~0,
387 am->l2_input_classify_next_acl_ip4,
388 &ip4_table_index, 0);
390 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
393 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
394 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
395 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
396 sizeof (ip6_5tuple_mask) - 1, ~0,
397 am->l2_input_classify_next_acl_ip6,
398 &ip6_table_index, 0);
405 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
407 vnet_classify_main_t *cm = &vnet_classify_main;
408 u32 ip4_table_index = ~0;
409 u32 ip6_table_index = ~0;
411 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
413 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
416 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
418 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
421 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
422 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
423 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
424 sizeof (ip4_5tuple_mask) - 1, ~0,
425 am->l2_output_classify_next_acl_ip4,
426 &ip4_table_index, 0);
428 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
431 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
432 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
433 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
434 sizeof (ip6_5tuple_mask) - 1, ~0,
435 am->l2_output_classify_next_acl_ip6,
436 &ip6_table_index, 0);
443 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
445 vnet_classify_main_t *cm = &vnet_classify_main;
446 u32 ip4_table_index = ~0;
447 u32 ip6_table_index = ~0;
450 /* in case there were previous tables attached */
451 acl_unhook_l2_input_classify (am, sw_if_index);
453 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
454 sizeof (ip4_5tuple_mask) - 1, ~0,
455 am->l2_input_classify_next_acl_ip4,
456 &ip4_table_index, 1);
460 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
461 sizeof (ip6_5tuple_mask) - 1, ~0,
462 am->l2_input_classify_next_acl_ip6,
463 &ip6_table_index, 1);
466 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
467 sizeof (ip4_5tuple_mask) - 1, ~0,
468 am->l2_input_classify_next_acl_ip4,
469 &ip4_table_index, 0);
473 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
474 ip6_table_index, ~0);
476 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
477 sw_if_index, ip4_table_index, ip6_table_index);
480 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
481 sizeof (ip6_5tuple_mask) - 1, ~0,
482 am->l2_input_classify_next_acl_ip6,
483 &ip6_table_index, 0);
484 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
485 sizeof (ip4_5tuple_mask) - 1, ~0,
486 am->l2_input_classify_next_acl_ip4,
487 &ip4_table_index, 0);
491 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
493 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
496 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
501 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
503 vnet_classify_main_t *cm = &vnet_classify_main;
504 u32 ip4_table_index = ~0;
505 u32 ip6_table_index = ~0;
508 /* in case there were previous tables attached */
509 acl_unhook_l2_output_classify (am, sw_if_index);
511 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
512 sizeof (ip4_5tuple_mask) - 1, ~0,
513 am->l2_output_classify_next_acl_ip4,
514 &ip4_table_index, 1);
518 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
519 sizeof (ip6_5tuple_mask) - 1, ~0,
520 am->l2_output_classify_next_acl_ip6,
521 &ip6_table_index, 1);
524 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
525 sizeof (ip4_5tuple_mask) - 1, ~0,
526 am->l2_output_classify_next_acl_ip4,
527 &ip4_table_index, 0);
531 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
532 ip6_table_index, ~0);
534 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
535 sw_if_index, ip4_table_index, ip6_table_index);
538 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
539 sizeof (ip6_5tuple_mask) - 1, ~0,
540 am->l2_output_classify_next_acl_ip6,
541 &ip6_table_index, 0);
542 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
543 sizeof (ip4_5tuple_mask) - 1, ~0,
544 am->l2_output_classify_next_acl_ip4,
545 &ip4_table_index, 0);
549 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
551 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
554 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
561 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
567 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
569 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
571 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
575 rv = acl_hook_l2_input_classify (am, sw_if_index);
579 rv = acl_unhook_l2_input_classify (am, sw_if_index);
586 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
592 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
594 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
596 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
600 rv = acl_hook_l2_output_classify (am, sw_if_index);
604 rv = acl_unhook_l2_output_classify (am, sw_if_index);
612 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
614 acl_main_t *am = &acl_main;
617 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
618 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
620 acl_interface_in_enable_disable (am, sw_if_index, 1);
624 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
625 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
627 acl_interface_out_enable_disable (am, sw_if_index, 1);
633 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
635 acl_main_t *am = &acl_main;
640 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
641 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
644 if (acl_list_index ==
645 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
647 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
652 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
654 acl_interface_in_enable_disable (am, sw_if_index, 0);
659 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
661 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
663 if (acl_list_index ==
664 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
666 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
671 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
673 acl_interface_out_enable_disable (am, sw_if_index, 0);
680 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
682 acl_main_t *am = &acl_main;
685 acl_interface_in_enable_disable (am, sw_if_index, 0);
686 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
687 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
691 acl_interface_out_enable_disable (am, sw_if_index, 0);
692 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
693 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
698 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
705 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
710 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
717 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
719 u8 *p = vlib_buffer_get_current (b0) + offset;
724 acl_get_l4_proto (vlib_buffer_t * b0, int node_is_ip6)
736 proto = *((u8 *) vlib_buffer_get_current (b0) + proto_offset);
741 acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2, int prefixlen,
746 /* match any always succeeds */
751 if (memcmp (addr1, addr2, prefixlen / 8))
753 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
758 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
759 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
760 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
761 return (b1 & mask0) == b2;
765 /* The prefix fits into integer number of bytes, so nothing left to do */
771 uint32_t a1 = ntohl (addr1->ip4.as_u32);
772 uint32_t a2 = ntohl (addr2->ip4.as_u32);
773 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
774 return (a1 & mask0) == a2;
779 acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
781 return ((port >= port_first) && (port <= port_last));
785 acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
786 u8 * r_action, int *r_is_ip6, u32 * r_acl_match_p,
787 u32 * r_rule_match_p, u32 * trace_bitmap)
789 ethernet_header_t *h0;
792 ip46_address_t src, dst;
803 h0 = vlib_buffer_get_current (b0);
804 type0 = clib_net_to_host_u16 (h0->type);
805 is_ip4 = (type0 == ETHERNET_TYPE_IP4);
806 is_ip6 = (type0 == ETHERNET_TYPE_IP6);
808 if (!(is_ip4 || is_ip6))
812 /* The bunch of hardcoded offsets here is intentional to get rid of them
813 ASAP, when getting to a faster matching code */
816 clib_memcpy (&src.ip4, get_ptr_to_offset (b0, 26), 4);
817 clib_memcpy (&dst.ip4, get_ptr_to_offset (b0, 30), 4);
818 proto = acl_get_l4_proto (b0, 0);
821 *trace_bitmap |= 0x00000001;
823 src_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 34)));
825 dst_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 35)));
828 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 34)));
829 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 36)));
830 /* UDP gets ability to check on an oddball data byte as a bonus */
831 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
834 else /* is_ipv6 implicitly */
836 clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
837 clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
838 proto = acl_get_l4_proto (b0, 1);
841 *trace_bitmap |= 0x00000002;
843 src_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 54));
845 dst_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 55));
850 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 54)));
851 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 56)));
852 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
855 if (pool_is_free_index (am->acls, acl_index))
858 *r_acl_match_p = acl_index;
860 *r_rule_match_p = -1;
861 /* the ACL does not exist but is used for policy. Block traffic. */
864 a = am->acls + acl_index;
865 for (i = 0; i < a->count; i++)
868 if (is_ip6 != r->is_ipv6)
872 if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
874 if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
878 if (proto != r->proto)
881 (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
885 (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
888 /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
889 if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
892 /* everything matches! */
893 *r_action = r->is_permit;
896 *r_acl_match_p = acl_index;
905 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
906 u32 * acl_match_p, u32 * rule_match_p,
909 acl_main_t *am = &acl_main;
913 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
914 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
918 (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
919 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
923 *nextp = am->acl_in_ip6_match_next[action];
927 *nextp = am->acl_in_ip4_match_next[action];
932 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
934 /* If there are ACLs and none matched, deny by default */
941 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
942 u32 * acl_match_p, u32 * rule_match_p,
945 acl_main_t *am = &acl_main;
949 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
950 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
954 (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
955 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
959 *nextp = am->acl_out_ip6_match_next[action];
963 *nextp = am->acl_out_ip4_match_next[action];
968 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
970 /* If there are ACLs and none matched, deny by default */
983 } macip_match_type_t;
986 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
992 for (i = 0; i < vec_len (mv); i++)
994 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
995 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1005 /* Get metric used to sort match types.
1006 The more specific and the more often seen - the bigger the metric */
1008 match_type_metric (macip_match_type_t * m)
1010 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1011 return m->prefix_len + m->is_ipv6 + 10 * m->count;
1015 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1017 /* Ascending sort based on the metric values */
1018 return match_type_metric (m1) - match_type_metric (m2);
1021 /* Get the offset of L3 source within ethernet packet */
1023 get_l3_src_offset(int is6)
1026 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
1028 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
1032 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1034 macip_match_type_t *mvec = NULL;
1035 macip_match_type_t *mt;
1036 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1038 u32 match_type_index;
1041 vnet_classify_main_t *cm = &vnet_classify_main;
1043 /* Count the number of different types of rules */
1044 for (i = 0; i < a->count; i++)
1048 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1049 a->rules[i].src_prefixlen,
1050 a->rules[i].is_ipv6)))
1052 match_type_index = vec_len (mvec);
1053 vec_validate (mvec, match_type_index);
1054 memcpy (mvec[match_type_index].mac_mask,
1055 a->rules[match_type_index].src_mac_mask, 6);
1056 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1057 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1058 mvec[match_type_index].table_index = ~0;
1060 mvec[match_type_index].count++;
1062 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1063 vec_sort_with_function (mvec, match_type_compare);
1064 /* Create the classifier tables */
1066 /* First add ARP tables */
1067 vec_foreach (mt, mvec)
1070 int is6 = mt->is_ipv6;
1072 mt->arp_table_index = ~0;
1075 memset (mask, 0, sizeof (mask));
1076 memcpy (&mask[6], mt->mac_mask, 6);
1077 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1078 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1080 for (i = 0; i < (mt->prefix_len / 8); i++)
1081 mask[14 + 14 + i] = 0xff;
1082 if (mt->prefix_len % 8)
1083 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1085 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1086 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1087 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1088 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1090 last_table = mt->arp_table_index;
1093 /* Now add IP[46] tables */
1094 vec_foreach (mt, mvec)
1097 int is6 = mt->is_ipv6;
1098 int l3_src_offs = get_l3_src_offset(is6);
1099 memset (mask, 0, sizeof (mask));
1100 memcpy (&mask[6], mt->mac_mask, 6);
1101 for (i = 0; i < (mt->prefix_len / 8); i++)
1103 mask[l3_src_offs + i] = 0xff;
1105 if (mt->prefix_len % 8)
1107 mask[l3_src_offs + (mt->prefix_len / 8)] =
1108 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1111 * Round-up the number of bytes needed to store the prefix,
1112 * and round up the number of vectors too
1114 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1115 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1116 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1117 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1119 last_table = mt->table_index;
1121 a->ip4_table_index = ~0;
1122 a->ip6_table_index = ~0;
1123 a->l2_table_index = last_table;
1125 /* Populate the classifier tables with rules from the MACIP ACL */
1126 for (i = 0; i < a->count; i++)
1130 int is6 = a->rules[i].is_ipv6;
1131 int l3_src_offs = get_l3_src_offset(is6);
1132 memset (mask, 0, sizeof (mask));
1133 memcpy (&mask[6], a->rules[i].src_mac, 6);
1134 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1137 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1143 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1148 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1149 a->rules[i].src_prefixlen,
1150 a->rules[i].is_ipv6);
1151 /* add session to table mvec[match_type_index].table_index; */
1152 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1153 mask, a->rules[i].is_permit ? ~0 : 0, i,
1154 0, action, metadata, 1);
1155 /* add ARP table entry too */
1156 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1158 memset (mask, 0, sizeof (mask));
1159 memcpy (&mask[6], a->rules[i].src_mac, 6);
1162 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1163 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1164 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1165 mask, a->rules[i].is_permit ? ~0 : 0, i,
1166 0, action, metadata, 1);
1173 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1175 vnet_classify_main_t *cm = &vnet_classify_main;
1176 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1178 if (a->ip4_table_index != ~0)
1180 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1181 a->ip4_table_index = ~0;
1183 if (a->ip6_table_index != ~0)
1185 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1186 a->ip6_table_index = ~0;
1188 if (a->l2_table_index != ~0)
1190 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1191 a->l2_table_index = ~0;
1196 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1197 u32 * acl_list_index, u8 * tag)
1199 acl_main_t *am = &acl_main;
1200 macip_acl_list_t *a;
1201 macip_acl_rule_t *r;
1202 macip_acl_rule_t *acl_new_rules;
1205 /* Create and populate the rules */
1206 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1207 CLIB_CACHE_LINE_BYTES);
1210 /* Could not allocate rules. New or existing ACL - bail out regardless */
1214 for (i = 0; i < count; i++)
1216 r = &acl_new_rules[i];
1217 r->is_permit = rules[i].is_permit;
1218 r->is_ipv6 = rules[i].is_ipv6;
1219 memcpy (&r->src_mac, rules[i].src_mac, 6);
1220 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1221 if(rules[i].is_ipv6)
1222 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1224 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1225 r->src_prefixlen = rules[i].src_ip_prefix_len;
1229 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1230 memset (a, 0, sizeof (*a));
1231 /* Will return the newly allocated ACL index */
1232 *acl_list_index = a - am->macip_acls;
1234 a->rules = acl_new_rules;
1236 memcpy (a->tag, tag, sizeof (a->tag));
1238 /* Create and populate the classifer tables */
1239 macip_create_classify_tables (am, *acl_list_index);
1245 /* No check for validity of sw_if_index - the callers were supposed to validate */
1248 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1251 u32 macip_acl_index;
1252 macip_acl_list_t *a;
1253 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1254 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1255 /* No point in deleting MACIP ACL which is not applied */
1256 if (~0 == macip_acl_index)
1258 a = &am->macip_acls[macip_acl_index];
1259 /* remove the classifier tables off the interface L2 ACL */
1261 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1262 a->ip6_table_index, a->l2_table_index, 0);
1263 /* Unset the MACIP ACL index */
1264 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1268 /* No check for validity of sw_if_index - the callers were supposed to validate */
1271 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1272 u32 macip_acl_index)
1274 macip_acl_list_t *a;
1276 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1280 a = &am->macip_acls[macip_acl_index];
1281 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1282 /* If there already a MACIP ACL applied, unapply it */
1283 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1284 macip_acl_interface_del_acl(am, sw_if_index);
1285 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1286 /* Apply the classifier tables for L2 ACLs */
1288 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1289 a->ip6_table_index, a->l2_table_index, 1);
1294 macip_acl_del_list (u32 acl_list_index)
1296 acl_main_t *am = &acl_main;
1297 macip_acl_list_t *a;
1299 if (pool_is_free_index (am->macip_acls, acl_list_index))
1304 /* delete any references to the ACL */
1305 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1307 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1309 macip_acl_interface_del_acl (am, i);
1313 /* Now that classifier tables are detached, clean them up */
1314 macip_destroy_classify_tables (am, acl_list_index);
1316 /* now we can delete the ACL itself */
1317 a = &am->macip_acls[acl_list_index];
1320 clib_mem_free (a->rules);
1322 pool_put (am->macip_acls, a);
1328 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1331 acl_main_t *am = &acl_main;
1335 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1339 rv = macip_acl_interface_del_acl (am, sw_if_index);
1344 /* API message handler */
1346 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1348 vl_api_acl_add_replace_reply_t *rmp;
1349 acl_main_t *am = &acl_main;
1351 u32 acl_list_index = ntohl (mp->acl_index);
1353 rv = acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1356 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1358 rmp->acl_index = htonl(acl_list_index);
1364 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1366 acl_main_t *am = &acl_main;
1367 vl_api_acl_del_reply_t *rmp;
1370 rv = acl_del_list (ntohl (mp->acl_index));
1372 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1376 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1378 acl_main_t *am = &acl_main;
1379 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1380 u32 sw_if_index = ntohl (mp->sw_if_index);
1381 vl_api_acl_interface_add_del_reply_t *rmp;
1384 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1385 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1388 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1389 mp->is_input, ntohl (mp->acl_index));
1391 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1395 vl_api_acl_interface_set_acl_list_t_handler
1396 (vl_api_acl_interface_set_acl_list_t * mp)
1398 acl_main_t *am = &acl_main;
1399 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1402 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1403 u32 sw_if_index = ntohl (mp->sw_if_index);
1405 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1406 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1409 acl_interface_reset_inout_acls (sw_if_index, 0);
1410 acl_interface_reset_inout_acls (sw_if_index, 1);
1412 for (i = 0; i < mp->count; i++)
1414 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1415 ntohl (mp->acls[i]));
1419 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1423 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1425 api_rule->is_permit = r->is_permit;
1426 api_rule->is_ipv6 = r->is_ipv6;
1429 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1430 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1434 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1435 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1437 api_rule->src_ip_prefix_len = r->src_prefixlen;
1438 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1439 api_rule->proto = r->proto;
1440 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1441 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1442 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1443 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1444 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1445 api_rule->tcp_flags_value = r->tcp_flags_value;
1449 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1450 acl_list_t * acl, u32 context)
1452 vl_api_acl_details_t *mp;
1453 vl_api_acl_rule_t *rules;
1455 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1457 mp = vl_msg_api_alloc (msg_size);
1458 memset (mp, 0, msg_size);
1459 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1461 /* fill in the message */
1462 mp->context = context;
1463 mp->count = htonl (acl->count);
1464 mp->acl_index = htonl (acl - am->acls);
1465 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1466 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1468 for (i = 0; i < acl->count; i++)
1470 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1473 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1474 vl_msg_api_send_shmem (q, (u8 *) & mp);
1479 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1481 acl_main_t *am = &acl_main;
1486 unix_shared_memory_queue_t *q;
1488 q = vl_api_client_index_to_input_queue (mp->client_index);
1494 if (mp->acl_index == ~0)
1497 /* Just dump all ACLs */
1498 pool_foreach (acl, am->acls,
1500 send_acl_details(am, q, acl, mp->context);
1506 acl_index = ntohl (mp->acl_index);
1507 if (!pool_is_free_index (am->acls, acl_index))
1509 acl = &am->acls[acl_index];
1510 send_acl_details (am, q, acl, mp->context);
1516 /* FIXME API: should we signal an error here at all ? */
1522 send_acl_interface_list_details (acl_main_t * am,
1523 unix_shared_memory_queue_t * q,
1524 u32 sw_if_index, u32 context)
1526 vl_api_acl_interface_list_details_t *mp;
1533 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1534 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1536 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1537 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1538 count = n_input + n_output;
1540 msg_size = sizeof (*mp);
1541 msg_size += sizeof (mp->acls[0]) * count;
1543 mp = vl_msg_api_alloc (msg_size);
1544 memset (mp, 0, msg_size);
1546 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1548 /* fill in the message */
1549 mp->context = context;
1550 mp->sw_if_index = htonl (sw_if_index);
1552 mp->n_input = n_input;
1553 for (i = 0; i < n_input; i++)
1555 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1557 for (i = 0; i < n_output; i++)
1559 mp->acls[n_input + i] =
1560 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1563 vl_msg_api_send_shmem (q, (u8 *) & mp);
1567 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1570 acl_main_t *am = &acl_main;
1571 vnet_sw_interface_t *swif;
1572 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1575 unix_shared_memory_queue_t *q;
1577 q = vl_api_client_index_to_input_queue (mp->client_index);
1583 if (mp->sw_if_index == ~0)
1586 pool_foreach (swif, im->sw_interfaces,
1588 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1594 sw_if_index = ntohl (mp->sw_if_index);
1595 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1596 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1600 /* MACIP ACL API handlers */
1603 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1605 vl_api_macip_acl_add_reply_t *rmp;
1606 acl_main_t *am = &acl_main;
1608 u32 acl_list_index = ~0;
1611 macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1614 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1616 rmp->acl_index = htonl(acl_list_index);
1622 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1624 acl_main_t *am = &acl_main;
1625 vl_api_macip_acl_del_reply_t *rmp;
1628 rv = macip_acl_del_list (ntohl (mp->acl_index));
1630 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1634 vl_api_macip_acl_interface_add_del_t_handler
1635 (vl_api_macip_acl_interface_add_del_t * mp)
1637 acl_main_t *am = &acl_main;
1638 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1640 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1641 u32 sw_if_index = ntohl (mp->sw_if_index);
1643 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1644 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1647 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1648 ntohl (mp->acl_index));
1650 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1654 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1655 macip_acl_list_t * acl, u32 context)
1657 vl_api_macip_acl_details_t *mp;
1658 vl_api_macip_acl_rule_t *rules;
1659 macip_acl_rule_t *r;
1661 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1663 mp = vl_msg_api_alloc (msg_size);
1664 memset (mp, 0, msg_size);
1665 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1667 /* fill in the message */
1668 mp->context = context;
1671 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1672 mp->count = htonl (acl->count);
1673 mp->acl_index = htonl (acl - am->macip_acls);
1675 for (i = 0; i < acl->count; i++)
1678 rules[i].is_permit = r->is_permit;
1679 rules[i].is_ipv6 = r->is_ipv6;
1680 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1681 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1682 sizeof (r->src_mac_mask));
1684 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1685 sizeof (r->src_ip_addr.ip6));
1687 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1688 sizeof (r->src_ip_addr.ip4));
1689 rules[i].src_ip_prefix_len = r->src_prefixlen;
1694 /* No martini, no party - no ACL applied to this interface. */
1699 vl_msg_api_send_shmem (q, (u8 *) & mp);
1704 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1706 acl_main_t *am = &acl_main;
1707 macip_acl_list_t *acl;
1709 unix_shared_memory_queue_t *q;
1711 q = vl_api_client_index_to_input_queue (mp->client_index);
1717 if (mp->acl_index == ~0)
1719 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1720 pool_foreach (acl, am->macip_acls, (
1722 send_macip_acl_details (am, q, acl,
1730 u32 acl_index = ntohl (mp->acl_index);
1731 if (!pool_is_free_index (am->macip_acls, acl_index))
1733 acl = &am->macip_acls[acl_index];
1734 send_macip_acl_details (am, q, acl, mp->context);
1740 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1743 acl_main_t *am = &acl_main;
1744 vl_api_macip_acl_interface_get_reply_t *rmp;
1745 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1746 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1747 unix_shared_memory_queue_t *q;
1750 q = vl_api_client_index_to_input_queue (mp->client_index);
1756 rmp = vl_msg_api_alloc (msg_size);
1757 memset (rmp, 0, msg_size);
1759 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1760 rmp->context = mp->context;
1761 rmp->count = htonl (count);
1762 for (i = 0; i < count; i++)
1764 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1767 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1772 /* Set up the API message handling tables */
1773 static clib_error_t *
1774 acl_plugin_api_hookup (vlib_main_t * vm)
1776 acl_main_t *am = &acl_main;
1778 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1780 vl_api_##n##_t_handler, \
1782 vl_api_##n##_t_endian, \
1783 vl_api_##n##_t_print, \
1784 sizeof(vl_api_##n##_t), 1);
1785 foreach_acl_plugin_api_msg;
1791 #define vl_msg_name_crc_list
1792 #include <acl/acl_all_api_h.h>
1793 #undef vl_msg_name_crc_list
1796 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1798 #define _(id,n,crc) \
1799 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1800 foreach_vl_msg_name_crc_acl;
1805 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1806 u32 next_out_ip4, u32 next_out_ip6)
1808 acl_main_t *am = &acl_main;
1809 if (am->n_match_actions == 255)
1813 u32 act = am->n_match_actions;
1814 am->n_match_actions++;
1815 am->acl_in_ip4_match_next[act] = next_in_ip4;
1816 am->acl_in_ip6_match_next[act] = next_in_ip6;
1817 am->acl_out_ip4_match_next[act] = next_out_ip4;
1818 am->acl_out_ip6_match_next[act] = next_out_ip6;
1823 acl_setup_nodes (void)
1825 vlib_main_t *vm = vlib_get_main ();
1826 acl_main_t *am = &acl_main;
1829 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1830 am->l2_input_classify_next_acl_old =
1831 vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1832 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1833 am->l2_output_classify_next_acl_old =
1834 vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1836 feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1837 l2input_get_feat_names (),
1838 am->acl_in_node_feat_next_node_index);
1840 feat_bitmap_init_next_nodes (vm, acl_out_node.index, L2OUTPUT_N_FEAT,
1841 l2output_get_feat_names (),
1842 am->acl_out_node_feat_next_node_index);
1844 memset (&am->acl_in_ip4_match_next[0], 0,
1845 sizeof (am->acl_in_ip4_match_next));
1846 memset (&am->acl_in_ip6_match_next[0], 0,
1847 sizeof (am->acl_in_ip6_match_next));
1848 memset (&am->acl_out_ip4_match_next[0], 0,
1849 sizeof (am->acl_out_ip4_match_next));
1850 memset (&am->acl_out_ip6_match_next[0], 0,
1851 sizeof (am->acl_out_ip6_match_next));
1852 am->n_match_actions = 0;
1854 am->l2_input_classify_next_acl_ip4 = am->l2_input_classify_next_acl_old;
1855 am->l2_input_classify_next_acl_ip6 = am->l2_input_classify_next_acl_old;
1856 am->l2_output_classify_next_acl_ip4 = am->l2_output_classify_next_acl_old;
1857 am->l2_output_classify_next_acl_ip6 = am->l2_output_classify_next_acl_old;
1859 register_match_action_nexts (0, 0, 0, 0); /* drop */
1860 register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1861 register_match_action_nexts (ACL_IN_L2S_INPUT_IP4_ADD, ACL_IN_L2S_INPUT_IP6_ADD, ACL_OUT_L2S_OUTPUT_IP4_ADD, ACL_OUT_L2S_OUTPUT_IP6_ADD); /* permit + create session */
1865 acl_setup_fa_nodes (void)
1867 vlib_main_t *vm = vlib_get_main ();
1868 acl_main_t *am = &acl_main;
1869 vlib_node_t *n, *n4, *n6;
1871 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1872 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1873 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1876 am->fa_l2_input_classify_next_acl_ip4 =
1877 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1878 am->fa_l2_input_classify_next_acl_ip6 =
1879 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1881 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1882 l2input_get_feat_names (),
1883 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1885 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1886 l2input_get_feat_names (),
1887 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1890 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1891 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1892 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1894 am->fa_l2_output_classify_next_acl_ip4 =
1895 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1896 am->fa_l2_output_classify_next_acl_ip6 =
1897 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1899 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1900 l2output_get_feat_names (),
1901 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1903 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1904 l2output_get_feat_names (),
1905 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1907 am->l2_input_classify_next_acl_ip4 = am->fa_l2_input_classify_next_acl_ip4;
1908 am->l2_input_classify_next_acl_ip6 = am->fa_l2_input_classify_next_acl_ip6;
1909 am->l2_output_classify_next_acl_ip4 = am->fa_l2_output_classify_next_acl_ip4;
1910 am->l2_output_classify_next_acl_ip6 = am->fa_l2_output_classify_next_acl_ip6;
1915 acl_set_timeout_sec(int timeout_type, u32 value)
1917 acl_main_t *am = &acl_main;
1918 l2sess_main_t *sm = &l2sess_main;
1919 clib_time_t *ct = &am->vlib_main->clib_time;
1921 if (timeout_type < ACL_N_TIMEOUTS) {
1922 am->session_timeout_sec[timeout_type] = value;
1924 clib_warning("Unknown timeout type %d", timeout_type);
1928 switch(timeout_type) {
1929 case ACL_TIMEOUT_UDP_IDLE:
1930 sm->udp_session_idle_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1932 case ACL_TIMEOUT_TCP_IDLE:
1933 sm->tcp_session_idle_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1935 case ACL_TIMEOUT_TCP_TRANSIENT:
1936 sm->tcp_session_transient_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1939 clib_warning("Unknown timeout type %d", timeout_type);
1944 acl_set_session_max_entries(u32 value)
1946 acl_main_t *am = &acl_main;
1947 am->fa_conn_table_max_entries = value;
1951 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1953 acl_main_t *am = &acl_main;
1954 if ((eh < 256) && (value < 2))
1956 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1964 static clib_error_t *
1965 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1967 acl_main_t *am = &acl_main;
1969 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1970 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1975 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1977 static clib_error_t *
1978 acl_set_aclplugin_fn (vlib_main_t * vm,
1979 unformat_input_t * input,
1980 vlib_cli_command_t * cmd)
1982 clib_error_t *error = 0;
1986 uword memory_size = 0;
1987 acl_main_t *am = &acl_main;
1989 /* The new datapath is the default. This command exists out of precaution and for comparing the two */
1990 if (unformat (input, "l2-datapath")) {
1991 if (unformat(input, "old")) {
1992 am->l2_input_classify_next_acl_ip4 = am->l2_input_classify_next_acl_old;
1993 am->l2_input_classify_next_acl_ip6 = am->l2_input_classify_next_acl_old;
1994 am->l2_output_classify_next_acl_ip4 = am->l2_output_classify_next_acl_old;
1995 am->l2_output_classify_next_acl_ip6 = am->l2_output_classify_next_acl_old;
1998 if (unformat(input, "new")) {
1999 am->l2_input_classify_next_acl_ip4 = am->fa_l2_input_classify_next_acl_ip4;
2000 am->l2_input_classify_next_acl_ip6 = am->fa_l2_input_classify_next_acl_ip6;
2001 am->l2_output_classify_next_acl_ip4 = am->fa_l2_output_classify_next_acl_ip4;
2002 am->l2_output_classify_next_acl_ip6 = am->fa_l2_output_classify_next_acl_ip6;
2007 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
2008 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
2009 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
2013 if (unformat (input, "session")) {
2014 if (unformat (input, "clear")) {
2015 acl_main_t *am = &acl_main;
2016 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2017 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2020 if (unformat (input, "table")) {
2021 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2022 if (unformat (input, "max-entries")) {
2023 if (!unformat(input, "%u", &val)) {
2024 error = clib_error_return(0,
2025 "expecting maximum number of entries, got `%U`",
2026 format_unformat_error, input);
2029 acl_set_session_max_entries(val);
2033 if (unformat (input, "hash-table-buckets")) {
2034 if (!unformat(input, "%u", &val)) {
2035 error = clib_error_return(0,
2036 "expecting maximum number of hash table buckets, got `%U`",
2037 format_unformat_error, input);
2040 am->fa_conn_table_hash_num_buckets = val;
2044 if (unformat (input, "hash-table-memory")) {
2045 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
2046 error = clib_error_return(0,
2047 "expecting maximum amount of hash table memory, got `%U`",
2048 format_unformat_error, input);
2051 am->fa_conn_table_hash_memory_size = memory_size;
2057 if (unformat (input, "timeout")) {
2058 if (unformat(input, "udp")) {
2059 if(unformat(input, "idle")) {
2060 if (!unformat(input, "%u", &timeout)) {
2061 error = clib_error_return(0,
2062 "expecting timeout value in seconds, got `%U`",
2063 format_unformat_error, input);
2066 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
2071 if (unformat(input, "tcp")) {
2072 if(unformat(input, "idle")) {
2073 if (!unformat(input, "%u", &timeout)) {
2074 error = clib_error_return(0,
2075 "expecting timeout value in seconds, got `%U`",
2076 format_unformat_error, input);
2079 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
2083 if(unformat(input, "transient")) {
2084 if (!unformat(input, "%u", &timeout)) {
2085 error = clib_error_return(0,
2086 "expecting timeout value in seconds, got `%U`",
2087 format_unformat_error, input);
2090 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
2102 static clib_error_t *
2103 acl_show_aclplugin_fn (vlib_main_t * vm,
2104 unformat_input_t * input,
2105 vlib_cli_command_t * cmd)
2107 clib_error_t *error = 0;
2108 acl_main_t *am = &acl_main;
2109 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2111 vnet_sw_interface_t *swif;
2113 if (unformat (input, "sessions"))
2116 pool_foreach (swif, im->sw_interfaces,
2118 u32 sw_if_index = swif->sw_if_index;
2119 u64 n_adds = sw_if_index < vec_len(am->fa_session_adds_by_sw_if_index) ? am->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
2120 u64 n_dels = sw_if_index < vec_len(am->fa_session_dels_by_sw_if_index) ? am->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
2121 out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2123 out0 = format(out0, "\n\nConn cleaner thread counters:\n");
2124 #define _(cnt, desc) out0 = format(out0, " %20lu: %s\n", am->cnt, desc);
2125 foreach_fa_cleaner_counter;
2127 vlib_cli_output(vm, "\n\n%s\n\n", out0);
2128 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2129 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2130 am->fa_cleaner_wait_time_increment * 1000.0, ((f64)am->fa_current_cleaner_timer_wait_interval) * 1000.0/(f64)vm->clib_time.clocks_per_second);
2139 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2140 .path = "set acl-plugin",
2141 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2142 .function = acl_set_aclplugin_fn,
2145 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2146 .path = "show acl-plugin",
2147 .short_help = "show acl-plugin sessions",
2148 .function = acl_show_aclplugin_fn,
2154 static clib_error_t *
2155 acl_init (vlib_main_t * vm)
2157 acl_main_t *am = &acl_main;
2158 clib_error_t *error = 0;
2159 memset (am, 0, sizeof (*am));
2161 am->vnet_main = vnet_get_main ();
2163 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2165 /* Ask for a correctly-sized block of API message decode slots */
2166 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2167 VL_MSG_FIRST_AVAILABLE);
2169 error = acl_plugin_api_hookup (vm);
2172 /* Add our API messages to the global name_crc hash table */
2173 setup_message_id_table (am, &api_main);
2177 acl_setup_fa_nodes();
2178 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2179 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2180 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2182 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2183 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2184 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2188 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2189 am->fa_conn_list_head[tt] = ~0;
2190 am->fa_conn_list_tail[tt] = ~0;
2194 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2195 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2196 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2198 am->fa_cleaner_cnt_delete_by_sw_index = 0;
2199 am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2200 am->fa_cleaner_cnt_unknown_event = 0;
2201 am->fa_cleaner_cnt_deleted_sessions = 0;
2202 am->fa_cleaner_cnt_timer_restarted = 0;
2203 am->fa_cleaner_cnt_wait_with_timeout = 0;
2206 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2213 VLIB_INIT_FUNCTION (acl_init);