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,
87 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
89 acl_main_t *am = &acl_main;
90 vl_api_acl_plugin_get_version_reply_t *rmp;
91 int msg_size = sizeof (*rmp);
92 unix_shared_memory_queue_t *q;
94 q = vl_api_client_index_to_input_queue (mp->client_index);
100 rmp = vl_msg_api_alloc (msg_size);
101 memset (rmp, 0, msg_size);
103 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
104 rmp->context = mp->context;
105 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
106 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
108 vl_msg_api_send_shmem (q, (u8 *) & rmp);
113 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
114 u32 * acl_list_index, u8 * tag)
116 acl_main_t *am = &acl_main;
119 acl_rule_t *acl_new_rules;
122 if (*acl_list_index != ~0)
124 /* They supplied some number, let's see if this ACL exists */
125 if (pool_is_free_index (am->acls, *acl_list_index))
127 /* tried to replace a non-existent ACL, no point doing anything */
132 /* Create and populate the rules */
133 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
134 CLIB_CACHE_LINE_BYTES);
137 /* Could not allocate rules. New or existing ACL - bail out regardless */
141 for (i = 0; i < count; i++)
143 r = &acl_new_rules[i];
144 r->is_permit = rules[i].is_permit;
145 r->is_ipv6 = rules[i].is_ipv6;
148 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
149 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
153 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
154 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
156 r->src_prefixlen = rules[i].src_ip_prefix_len;
157 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
158 r->proto = rules[i].proto;
159 r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
160 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
161 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
162 r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
163 r->tcp_flags_value = rules[i].tcp_flags_value;
164 r->tcp_flags_mask = rules[i].tcp_flags_mask;
167 if (~0 == *acl_list_index)
170 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
171 memset (a, 0, sizeof (*a));
172 /* Will return the newly allocated ACL index */
173 *acl_list_index = a - am->acls;
177 a = am->acls + *acl_list_index;
178 /* Get rid of the old rules */
179 clib_mem_free (a->rules);
181 a->rules = acl_new_rules;
183 memcpy (a->tag, tag, sizeof (a->tag));
189 acl_del_list (u32 acl_list_index)
191 acl_main_t *am = &acl_main;
194 if (pool_is_free_index (am->acls, acl_list_index))
199 /* delete any references to the ACL */
200 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
202 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
205 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
207 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
215 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
217 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
220 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
222 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
231 /* now we can delete the ACL itself */
232 a = &am->acls[acl_list_index];
235 clib_mem_free (a->rules);
237 pool_put (am->acls, a);
241 /* Some aids in ASCII graphing the content */
247 u8 ip4_5tuple_mask[] =
248 _(" dmac smac etype ")
249 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
256 _(" ttl pr checksum ")
265 _("L4 T/U sport dport ")
275 u8 ip6_5tuple_mask[] =
276 _(" dmac smac etype ")
277 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
279 _(0x0000) __ __ __ __
281 _(0x0004) __ __ XX __
283 _(0x0008) XX XX XX XX
284 _(0x000C) XX XX XX XX
285 _(0x0010) XX XX XX XX
286 _(0x0014) XX XX XX XX
288 _(0x0018) XX XX XX XX
289 _(0x001C) XX XX XX XX
290 _(0x0020) XX XX XX XX
291 _(0x0024) XX XX XX XX
292 _("L4T/U sport dport ")
293 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
300 static int count_skip (u8 * p, u32 size)
302 u64 *p64 = (u64 *) p;
303 /* Be tolerant to null pointer */
307 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
311 return (p64 - (u64 *) p) / 2;
315 acl_classify_add_del_table_big (vnet_classify_main_t * cm, u8 * mask,
316 u32 mask_len, u32 next_table_index,
317 u32 miss_next_index, u32 * table_index,
320 u32 nbuckets = 65536;
321 u32 memory_size = 2 << 30;
322 u32 skip = count_skip (mask, mask_len);
323 u32 match = (mask_len / 16) - skip;
324 u8 *skip_mask_ptr = mask + 16 * skip;
325 u32 current_data_flag = 0;
326 int current_data_offset = 0;
331 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
332 memory_size, skip, match,
333 next_table_index, miss_next_index,
334 table_index, current_data_flag,
335 current_data_offset, is_add,
336 1 /* delete_chain */);
340 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
341 u32 mask_len, u32 next_table_index,
342 u32 miss_next_index, u32 * table_index,
346 u32 memory_size = 2 << 20;
347 u32 skip = count_skip (mask, mask_len);
348 u32 match = (mask_len / 16) - skip;
349 u8 *skip_mask_ptr = mask + 16 * skip;
350 u32 current_data_flag = 0;
351 int current_data_offset = 0;
356 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
357 memory_size, skip, match,
358 next_table_index, miss_next_index,
359 table_index, current_data_flag,
360 current_data_offset, is_add,
361 1 /* delete_chain */);
366 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
368 vnet_classify_main_t *cm = &vnet_classify_main;
369 u32 ip4_table_index = ~0;
370 u32 ip6_table_index = ~0;
372 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
374 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
377 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
379 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
382 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
383 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
384 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
385 sizeof (ip4_5tuple_mask) - 1, ~0,
386 am->l2_input_classify_next_acl_ip4,
387 &ip4_table_index, 0);
389 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
392 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
393 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
394 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
395 sizeof (ip6_5tuple_mask) - 1, ~0,
396 am->l2_input_classify_next_acl_ip6,
397 &ip6_table_index, 0);
404 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
406 vnet_classify_main_t *cm = &vnet_classify_main;
407 u32 ip4_table_index = ~0;
408 u32 ip6_table_index = ~0;
410 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
412 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
415 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
417 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
420 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
421 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
422 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
423 sizeof (ip4_5tuple_mask) - 1, ~0,
424 am->l2_output_classify_next_acl_ip4,
425 &ip4_table_index, 0);
427 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
430 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
431 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
432 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
433 sizeof (ip6_5tuple_mask) - 1, ~0,
434 am->l2_output_classify_next_acl_ip6,
435 &ip6_table_index, 0);
442 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
444 vnet_classify_main_t *cm = &vnet_classify_main;
445 u32 ip4_table_index = ~0;
446 u32 ip6_table_index = ~0;
449 /* in case there were previous tables attached */
450 acl_unhook_l2_input_classify (am, sw_if_index);
452 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
453 sizeof (ip4_5tuple_mask) - 1, ~0,
454 am->l2_input_classify_next_acl_ip4,
455 &ip4_table_index, 1);
459 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
460 sizeof (ip6_5tuple_mask) - 1, ~0,
461 am->l2_input_classify_next_acl_ip6,
462 &ip6_table_index, 1);
465 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
466 sizeof (ip4_5tuple_mask) - 1, ~0,
467 am->l2_input_classify_next_acl_ip4,
468 &ip4_table_index, 0);
472 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
473 ip6_table_index, ~0);
475 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
476 sw_if_index, ip4_table_index, ip6_table_index);
479 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
480 sizeof (ip6_5tuple_mask) - 1, ~0,
481 am->l2_input_classify_next_acl_ip6,
482 &ip6_table_index, 0);
483 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
484 sizeof (ip4_5tuple_mask) - 1, ~0,
485 am->l2_input_classify_next_acl_ip4,
486 &ip4_table_index, 0);
490 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
492 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
495 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
500 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
502 vnet_classify_main_t *cm = &vnet_classify_main;
503 u32 ip4_table_index = ~0;
504 u32 ip6_table_index = ~0;
507 /* in case there were previous tables attached */
508 acl_unhook_l2_output_classify (am, sw_if_index);
510 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
511 sizeof (ip4_5tuple_mask) - 1, ~0,
512 am->l2_output_classify_next_acl_ip4,
513 &ip4_table_index, 1);
517 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
518 sizeof (ip6_5tuple_mask) - 1, ~0,
519 am->l2_output_classify_next_acl_ip6,
520 &ip6_table_index, 1);
523 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
524 sizeof (ip4_5tuple_mask) - 1, ~0,
525 am->l2_output_classify_next_acl_ip4,
526 &ip4_table_index, 0);
530 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
531 ip6_table_index, ~0);
533 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
534 sw_if_index, ip4_table_index, ip6_table_index);
537 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
538 sizeof (ip6_5tuple_mask) - 1, ~0,
539 am->l2_output_classify_next_acl_ip6,
540 &ip6_table_index, 0);
541 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
542 sizeof (ip4_5tuple_mask) - 1, ~0,
543 am->l2_output_classify_next_acl_ip4,
544 &ip4_table_index, 0);
548 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
550 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
553 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
560 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
566 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
568 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
570 acl_fa_enable_disable(sw_if_index, 1, enable_disable);
574 rv = acl_hook_l2_input_classify (am, sw_if_index);
578 rv = acl_unhook_l2_input_classify (am, sw_if_index);
585 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
591 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
593 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
595 acl_fa_enable_disable(sw_if_index, 0, enable_disable);
599 rv = acl_hook_l2_output_classify (am, sw_if_index);
603 rv = acl_unhook_l2_output_classify (am, sw_if_index);
611 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
613 acl_main_t *am = &acl_main;
616 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
617 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
619 acl_interface_in_enable_disable (am, sw_if_index, 1);
623 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
624 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
626 acl_interface_out_enable_disable (am, sw_if_index, 1);
632 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
634 acl_main_t *am = &acl_main;
639 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
640 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
643 if (acl_list_index ==
644 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
646 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
651 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
653 acl_interface_in_enable_disable (am, sw_if_index, 0);
658 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
660 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
662 if (acl_list_index ==
663 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
665 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
670 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
672 acl_interface_out_enable_disable (am, sw_if_index, 0);
679 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
681 acl_main_t *am = &acl_main;
684 acl_interface_in_enable_disable (am, sw_if_index, 0);
685 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
686 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
690 acl_interface_out_enable_disable (am, sw_if_index, 0);
691 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
692 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
697 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
704 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
709 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
716 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
718 u8 *p = vlib_buffer_get_current (b0) + offset;
723 acl_get_l4_proto (vlib_buffer_t * b0, int node_is_ip6)
735 proto = *((u8 *) vlib_buffer_get_current (b0) + proto_offset);
740 acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2, int prefixlen,
745 /* match any always succeeds */
750 if (memcmp (addr1, addr2, prefixlen / 8))
752 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
757 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
758 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
759 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
760 return (b1 & mask0) == b2;
764 /* The prefix fits into integer number of bytes, so nothing left to do */
770 uint32_t a1 = ntohl (addr1->ip4.as_u32);
771 uint32_t a2 = ntohl (addr2->ip4.as_u32);
772 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
773 return (a1 & mask0) == a2;
778 acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
780 return ((port >= port_first) && (port <= port_last));
784 acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
785 u8 * r_action, int *r_is_ip6, u32 * r_acl_match_p,
786 u32 * r_rule_match_p, u32 * trace_bitmap)
788 ethernet_header_t *h0;
791 ip46_address_t src, dst;
802 h0 = vlib_buffer_get_current (b0);
803 type0 = clib_net_to_host_u16 (h0->type);
804 is_ip4 = (type0 == ETHERNET_TYPE_IP4);
805 is_ip6 = (type0 == ETHERNET_TYPE_IP6);
807 if (!(is_ip4 || is_ip6))
811 /* The bunch of hardcoded offsets here is intentional to get rid of them
812 ASAP, when getting to a faster matching code */
815 clib_memcpy (&src.ip4, get_ptr_to_offset (b0, 26), 4);
816 clib_memcpy (&dst.ip4, get_ptr_to_offset (b0, 30), 4);
817 proto = acl_get_l4_proto (b0, 0);
820 *trace_bitmap |= 0x00000001;
822 src_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 34)));
824 dst_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 35)));
827 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 34)));
828 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 36)));
829 /* UDP gets ability to check on an oddball data byte as a bonus */
830 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
833 else /* is_ipv6 implicitly */
835 clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
836 clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
837 proto = acl_get_l4_proto (b0, 1);
840 *trace_bitmap |= 0x00000002;
842 src_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 54));
844 dst_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 55));
849 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 54)));
850 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 56)));
851 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
854 if (pool_is_free_index (am->acls, acl_index))
857 *r_acl_match_p = acl_index;
859 *r_rule_match_p = -1;
860 /* the ACL does not exist but is used for policy. Block traffic. */
863 a = am->acls + acl_index;
864 for (i = 0; i < a->count; i++)
867 if (is_ip6 != r->is_ipv6)
871 if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
873 if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
877 if (proto != r->proto)
880 (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
884 (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
887 /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
888 if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
891 /* everything matches! */
892 *r_action = r->is_permit;
895 *r_acl_match_p = acl_index;
904 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
905 u32 * acl_match_p, u32 * rule_match_p,
908 acl_main_t *am = &acl_main;
912 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
913 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
917 (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
918 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
922 *nextp = am->acl_in_ip6_match_next[action];
926 *nextp = am->acl_in_ip4_match_next[action];
931 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
933 /* If there are ACLs and none matched, deny by default */
940 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
941 u32 * acl_match_p, u32 * rule_match_p,
944 acl_main_t *am = &acl_main;
948 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
949 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
953 (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
954 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
958 *nextp = am->acl_out_ip6_match_next[action];
962 *nextp = am->acl_out_ip4_match_next[action];
967 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
969 /* If there are ACLs and none matched, deny by default */
982 } macip_match_type_t;
985 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
991 for (i = 0; i < vec_len (mv); i++)
993 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
994 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1004 /* Get metric used to sort match types.
1005 The more specific and the more often seen - the bigger the metric */
1007 match_type_metric (macip_match_type_t * m)
1009 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1010 return m->prefix_len + m->is_ipv6 + 10 * m->count;
1014 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1016 /* Ascending sort based on the metric values */
1017 return match_type_metric (m1) - match_type_metric (m2);
1020 /* Get the offset of L3 source within ethernet packet */
1022 get_l3_src_offset(int is6)
1025 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
1027 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
1031 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1033 macip_match_type_t *mvec = NULL;
1034 macip_match_type_t *mt;
1035 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1037 u32 match_type_index;
1040 vnet_classify_main_t *cm = &vnet_classify_main;
1042 /* Count the number of different types of rules */
1043 for (i = 0; i < a->count; i++)
1047 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1048 a->rules[i].src_prefixlen,
1049 a->rules[i].is_ipv6)))
1051 match_type_index = vec_len (mvec);
1052 vec_validate (mvec, match_type_index);
1053 memcpy (mvec[match_type_index].mac_mask,
1054 a->rules[match_type_index].src_mac_mask, 6);
1055 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1056 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1057 mvec[match_type_index].table_index = ~0;
1059 mvec[match_type_index].count++;
1061 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1062 vec_sort_with_function (mvec, match_type_compare);
1063 /* Create the classifier tables */
1065 /* First add ARP tables */
1066 vec_foreach (mt, mvec)
1069 int is6 = mt->is_ipv6;
1071 mt->arp_table_index = ~0;
1074 memset (mask, 0, sizeof (mask));
1075 memcpy (&mask[6], mt->mac_mask, 6);
1076 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1077 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1079 for (i = 0; i < (mt->prefix_len / 8); i++)
1080 mask[14 + 14 + i] = 0xff;
1081 if (mt->prefix_len % 8)
1082 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1084 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1085 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1086 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1087 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1089 last_table = mt->arp_table_index;
1092 /* Now add IP[46] tables */
1093 vec_foreach (mt, mvec)
1096 int is6 = mt->is_ipv6;
1097 int l3_src_offs = get_l3_src_offset(is6);
1098 memset (mask, 0, sizeof (mask));
1099 memcpy (&mask[6], mt->mac_mask, 6);
1100 for (i = 0; i < (mt->prefix_len / 8); i++)
1102 mask[l3_src_offs + i] = 0xff;
1104 if (mt->prefix_len % 8)
1106 mask[l3_src_offs + (mt->prefix_len / 8)] =
1107 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1110 * Round-up the number of bytes needed to store the prefix,
1111 * and round up the number of vectors too
1113 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1114 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1115 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1116 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1118 last_table = mt->table_index;
1120 a->ip4_table_index = ~0;
1121 a->ip6_table_index = ~0;
1122 a->l2_table_index = last_table;
1124 /* Populate the classifier tables with rules from the MACIP ACL */
1125 for (i = 0; i < a->count; i++)
1129 int is6 = a->rules[i].is_ipv6;
1130 int l3_src_offs = get_l3_src_offset(is6);
1131 memset (mask, 0, sizeof (mask));
1132 memcpy (&mask[6], a->rules[i].src_mac, 6);
1133 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1136 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1142 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1147 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1148 a->rules[i].src_prefixlen,
1149 a->rules[i].is_ipv6);
1150 /* add session to table mvec[match_type_index].table_index; */
1151 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1152 mask, a->rules[i].is_permit ? ~0 : 0, i,
1153 0, action, metadata, 1);
1154 /* add ARP table entry too */
1155 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1157 memset (mask, 0, sizeof (mask));
1158 memcpy (&mask[6], a->rules[i].src_mac, 6);
1161 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1162 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1163 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1164 mask, a->rules[i].is_permit ? ~0 : 0, i,
1165 0, action, metadata, 1);
1172 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1174 vnet_classify_main_t *cm = &vnet_classify_main;
1175 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1177 if (a->ip4_table_index != ~0)
1179 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1180 a->ip4_table_index = ~0;
1182 if (a->ip6_table_index != ~0)
1184 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1185 a->ip6_table_index = ~0;
1187 if (a->l2_table_index != ~0)
1189 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1190 a->l2_table_index = ~0;
1195 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1196 u32 * acl_list_index, u8 * tag)
1198 acl_main_t *am = &acl_main;
1199 macip_acl_list_t *a;
1200 macip_acl_rule_t *r;
1201 macip_acl_rule_t *acl_new_rules;
1204 /* Create and populate the rules */
1205 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1206 CLIB_CACHE_LINE_BYTES);
1209 /* Could not allocate rules. New or existing ACL - bail out regardless */
1213 for (i = 0; i < count; i++)
1215 r = &acl_new_rules[i];
1216 r->is_permit = rules[i].is_permit;
1217 r->is_ipv6 = rules[i].is_ipv6;
1218 memcpy (&r->src_mac, rules[i].src_mac, 6);
1219 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1220 if(rules[i].is_ipv6)
1221 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1223 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1224 r->src_prefixlen = rules[i].src_ip_prefix_len;
1228 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1229 memset (a, 0, sizeof (*a));
1230 /* Will return the newly allocated ACL index */
1231 *acl_list_index = a - am->macip_acls;
1233 a->rules = acl_new_rules;
1235 memcpy (a->tag, tag, sizeof (a->tag));
1237 /* Create and populate the classifer tables */
1238 macip_create_classify_tables (am, *acl_list_index);
1244 /* No check for validity of sw_if_index - the callers were supposed to validate */
1247 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1250 u32 macip_acl_index;
1251 macip_acl_list_t *a;
1252 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1253 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1254 /* No point in deleting MACIP ACL which is not applied */
1255 if (~0 == macip_acl_index)
1257 a = &am->macip_acls[macip_acl_index];
1258 /* remove the classifier tables off the interface L2 ACL */
1260 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1261 a->ip6_table_index, a->l2_table_index, 0);
1262 /* Unset the MACIP ACL index */
1263 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1267 /* No check for validity of sw_if_index - the callers were supposed to validate */
1270 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1271 u32 macip_acl_index)
1273 macip_acl_list_t *a;
1275 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1279 a = &am->macip_acls[macip_acl_index];
1280 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1281 /* If there already a MACIP ACL applied, unapply it */
1282 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1283 macip_acl_interface_del_acl(am, sw_if_index);
1284 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1285 /* Apply the classifier tables for L2 ACLs */
1287 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1288 a->ip6_table_index, a->l2_table_index, 1);
1293 macip_acl_del_list (u32 acl_list_index)
1295 acl_main_t *am = &acl_main;
1296 macip_acl_list_t *a;
1298 if (pool_is_free_index (am->macip_acls, acl_list_index))
1303 /* delete any references to the ACL */
1304 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1306 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1308 macip_acl_interface_del_acl (am, i);
1312 /* Now that classifier tables are detached, clean them up */
1313 macip_destroy_classify_tables (am, acl_list_index);
1315 /* now we can delete the ACL itself */
1316 a = &am->macip_acls[acl_list_index];
1319 clib_mem_free (a->rules);
1321 pool_put (am->macip_acls, a);
1327 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1330 acl_main_t *am = &acl_main;
1334 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1338 rv = macip_acl_interface_del_acl (am, sw_if_index);
1343 /* API message handler */
1345 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1347 vl_api_acl_add_replace_reply_t *rmp;
1348 acl_main_t *am = &acl_main;
1350 u32 acl_list_index = ntohl (mp->acl_index);
1352 rv = acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1355 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1357 rmp->acl_index = htonl(acl_list_index);
1363 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1365 acl_main_t *am = &acl_main;
1366 vl_api_acl_del_reply_t *rmp;
1369 rv = acl_del_list (ntohl (mp->acl_index));
1371 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1375 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1377 acl_main_t *am = &acl_main;
1378 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1379 u32 sw_if_index = ntohl (mp->sw_if_index);
1380 vl_api_acl_interface_add_del_reply_t *rmp;
1383 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1384 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1387 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1388 mp->is_input, ntohl (mp->acl_index));
1390 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1394 vl_api_acl_interface_set_acl_list_t_handler
1395 (vl_api_acl_interface_set_acl_list_t * mp)
1397 acl_main_t *am = &acl_main;
1398 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1401 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1402 u32 sw_if_index = ntohl (mp->sw_if_index);
1404 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1405 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1408 acl_interface_reset_inout_acls (sw_if_index, 0);
1409 acl_interface_reset_inout_acls (sw_if_index, 1);
1411 for (i = 0; i < mp->count; i++)
1413 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1414 ntohl (mp->acls[i]));
1418 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1422 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1424 api_rule->is_permit = r->is_permit;
1425 api_rule->is_ipv6 = r->is_ipv6;
1428 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1429 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1433 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1434 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1436 api_rule->src_ip_prefix_len = r->src_prefixlen;
1437 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1438 api_rule->proto = r->proto;
1439 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1440 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1441 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1442 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1443 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1444 api_rule->tcp_flags_value = r->tcp_flags_value;
1448 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1449 acl_list_t * acl, u32 context)
1451 vl_api_acl_details_t *mp;
1452 vl_api_acl_rule_t *rules;
1454 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1456 mp = vl_msg_api_alloc (msg_size);
1457 memset (mp, 0, msg_size);
1458 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1460 /* fill in the message */
1461 mp->context = context;
1462 mp->count = htonl (acl->count);
1463 mp->acl_index = htonl (acl - am->acls);
1464 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1465 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1467 for (i = 0; i < acl->count; i++)
1469 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1472 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1473 vl_msg_api_send_shmem (q, (u8 *) & mp);
1478 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1480 acl_main_t *am = &acl_main;
1485 unix_shared_memory_queue_t *q;
1487 q = vl_api_client_index_to_input_queue (mp->client_index);
1493 if (mp->acl_index == ~0)
1496 /* Just dump all ACLs */
1497 pool_foreach (acl, am->acls,
1499 send_acl_details(am, q, acl, mp->context);
1505 acl_index = ntohl (mp->acl_index);
1506 if (!pool_is_free_index (am->acls, acl_index))
1508 acl = &am->acls[acl_index];
1509 send_acl_details (am, q, acl, mp->context);
1515 /* FIXME API: should we signal an error here at all ? */
1521 send_acl_interface_list_details (acl_main_t * am,
1522 unix_shared_memory_queue_t * q,
1523 u32 sw_if_index, u32 context)
1525 vl_api_acl_interface_list_details_t *mp;
1532 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1533 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1535 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1536 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1537 count = n_input + n_output;
1539 msg_size = sizeof (*mp);
1540 msg_size += sizeof (mp->acls[0]) * count;
1542 mp = vl_msg_api_alloc (msg_size);
1543 memset (mp, 0, msg_size);
1545 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1547 /* fill in the message */
1548 mp->context = context;
1549 mp->sw_if_index = htonl (sw_if_index);
1551 mp->n_input = n_input;
1552 for (i = 0; i < n_input; i++)
1554 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1556 for (i = 0; i < n_output; i++)
1558 mp->acls[n_input + i] =
1559 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1562 vl_msg_api_send_shmem (q, (u8 *) & mp);
1566 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1569 acl_main_t *am = &acl_main;
1570 vnet_sw_interface_t *swif;
1571 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1574 unix_shared_memory_queue_t *q;
1576 q = vl_api_client_index_to_input_queue (mp->client_index);
1582 if (mp->sw_if_index == ~0)
1585 pool_foreach (swif, im->sw_interfaces,
1587 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1593 sw_if_index = ntohl (mp->sw_if_index);
1594 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1595 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1599 /* MACIP ACL API handlers */
1602 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1604 vl_api_macip_acl_add_reply_t *rmp;
1605 acl_main_t *am = &acl_main;
1607 u32 acl_list_index = ~0;
1610 macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1613 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1615 rmp->acl_index = htonl(acl_list_index);
1621 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1623 acl_main_t *am = &acl_main;
1624 vl_api_macip_acl_del_reply_t *rmp;
1627 rv = macip_acl_del_list (ntohl (mp->acl_index));
1629 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1633 vl_api_macip_acl_interface_add_del_t_handler
1634 (vl_api_macip_acl_interface_add_del_t * mp)
1636 acl_main_t *am = &acl_main;
1637 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1639 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1640 u32 sw_if_index = ntohl (mp->sw_if_index);
1642 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1643 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1646 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1647 ntohl (mp->acl_index));
1649 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1653 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1654 macip_acl_list_t * acl, u32 context)
1656 vl_api_macip_acl_details_t *mp;
1657 vl_api_macip_acl_rule_t *rules;
1658 macip_acl_rule_t *r;
1660 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1662 mp = vl_msg_api_alloc (msg_size);
1663 memset (mp, 0, msg_size);
1664 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1666 /* fill in the message */
1667 mp->context = context;
1670 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1671 mp->count = htonl (acl->count);
1672 mp->acl_index = htonl (acl - am->macip_acls);
1674 for (i = 0; i < acl->count; i++)
1677 rules[i].is_permit = r->is_permit;
1678 rules[i].is_ipv6 = r->is_ipv6;
1679 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1680 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1681 sizeof (r->src_mac_mask));
1683 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1684 sizeof (r->src_ip_addr.ip6));
1686 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1687 sizeof (r->src_ip_addr.ip4));
1688 rules[i].src_ip_prefix_len = r->src_prefixlen;
1693 /* No martini, no party - no ACL applied to this interface. */
1698 vl_msg_api_send_shmem (q, (u8 *) & mp);
1703 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1705 acl_main_t *am = &acl_main;
1706 macip_acl_list_t *acl;
1708 unix_shared_memory_queue_t *q;
1710 q = vl_api_client_index_to_input_queue (mp->client_index);
1716 if (mp->acl_index == ~0)
1718 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1719 pool_foreach (acl, am->macip_acls, (
1721 send_macip_acl_details (am, q, acl,
1729 u32 acl_index = ntohl (mp->acl_index);
1730 if (!pool_is_free_index (am->macip_acls, acl_index))
1732 acl = &am->macip_acls[acl_index];
1733 send_macip_acl_details (am, q, acl, mp->context);
1739 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1742 acl_main_t *am = &acl_main;
1743 vl_api_macip_acl_interface_get_reply_t *rmp;
1744 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1745 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1746 unix_shared_memory_queue_t *q;
1749 q = vl_api_client_index_to_input_queue (mp->client_index);
1755 rmp = vl_msg_api_alloc (msg_size);
1756 memset (rmp, 0, msg_size);
1758 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1759 rmp->context = mp->context;
1760 rmp->count = htonl (count);
1761 for (i = 0; i < count; i++)
1763 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1766 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1771 /* Set up the API message handling tables */
1772 static clib_error_t *
1773 acl_plugin_api_hookup (vlib_main_t * vm)
1775 acl_main_t *am = &acl_main;
1777 vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base), \
1779 vl_api_##n##_t_handler, \
1781 vl_api_##n##_t_endian, \
1782 vl_api_##n##_t_print, \
1783 sizeof(vl_api_##n##_t), 1);
1784 foreach_acl_plugin_api_msg;
1790 #define vl_msg_name_crc_list
1791 #include <acl/acl_all_api_h.h>
1792 #undef vl_msg_name_crc_list
1795 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1797 #define _(id,n,crc) \
1798 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1799 foreach_vl_msg_name_crc_acl;
1804 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1805 u32 next_out_ip4, u32 next_out_ip6)
1807 acl_main_t *am = &acl_main;
1808 if (am->n_match_actions == 255)
1812 u32 act = am->n_match_actions;
1813 am->n_match_actions++;
1814 am->acl_in_ip4_match_next[act] = next_in_ip4;
1815 am->acl_in_ip6_match_next[act] = next_in_ip6;
1816 am->acl_out_ip4_match_next[act] = next_out_ip4;
1817 am->acl_out_ip6_match_next[act] = next_out_ip6;
1822 acl_setup_nodes (void)
1824 vlib_main_t *vm = vlib_get_main ();
1825 acl_main_t *am = &acl_main;
1828 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1829 am->l2_input_classify_next_acl_old =
1830 vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1831 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1832 am->l2_output_classify_next_acl_old =
1833 vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1835 feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1836 l2input_get_feat_names (),
1837 am->acl_in_node_feat_next_node_index);
1839 feat_bitmap_init_next_nodes (vm, acl_out_node.index, L2OUTPUT_N_FEAT,
1840 l2output_get_feat_names (),
1841 am->acl_out_node_feat_next_node_index);
1843 memset (&am->acl_in_ip4_match_next[0], 0,
1844 sizeof (am->acl_in_ip4_match_next));
1845 memset (&am->acl_in_ip6_match_next[0], 0,
1846 sizeof (am->acl_in_ip6_match_next));
1847 memset (&am->acl_out_ip4_match_next[0], 0,
1848 sizeof (am->acl_out_ip4_match_next));
1849 memset (&am->acl_out_ip6_match_next[0], 0,
1850 sizeof (am->acl_out_ip6_match_next));
1851 am->n_match_actions = 0;
1853 am->l2_input_classify_next_acl_ip4 = am->l2_input_classify_next_acl_old;
1854 am->l2_input_classify_next_acl_ip6 = am->l2_input_classify_next_acl_old;
1855 am->l2_output_classify_next_acl_ip4 = am->l2_output_classify_next_acl_old;
1856 am->l2_output_classify_next_acl_ip6 = am->l2_output_classify_next_acl_old;
1858 register_match_action_nexts (0, 0, 0, 0); /* drop */
1859 register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1860 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 */
1864 acl_setup_fa_nodes (void)
1866 vlib_main_t *vm = vlib_get_main ();
1867 acl_main_t *am = &acl_main;
1868 vlib_node_t *n, *n4, *n6;
1870 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1871 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1872 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1875 am->fa_l2_input_classify_next_acl_ip4 =
1876 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1877 am->fa_l2_input_classify_next_acl_ip6 =
1878 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1880 feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1881 l2input_get_feat_names (),
1882 am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1884 feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1885 l2input_get_feat_names (),
1886 am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1889 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1890 n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1891 n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1893 am->fa_l2_output_classify_next_acl_ip4 =
1894 vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1895 am->fa_l2_output_classify_next_acl_ip6 =
1896 vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1898 feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1899 l2output_get_feat_names (),
1900 am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1902 feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1903 l2output_get_feat_names (),
1904 am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1906 am->l2_input_classify_next_acl_ip4 = am->fa_l2_input_classify_next_acl_ip4;
1907 am->l2_input_classify_next_acl_ip6 = am->fa_l2_input_classify_next_acl_ip6;
1908 am->l2_output_classify_next_acl_ip4 = am->fa_l2_output_classify_next_acl_ip4;
1909 am->l2_output_classify_next_acl_ip6 = am->fa_l2_output_classify_next_acl_ip6;
1914 acl_set_timeout_sec(int timeout_type, u32 value)
1916 acl_main_t *am = &acl_main;
1917 l2sess_main_t *sm = &l2sess_main;
1918 clib_time_t *ct = &am->vlib_main->clib_time;
1920 if (timeout_type < ACL_N_TIMEOUTS) {
1921 am->session_timeout_sec[timeout_type] = value;
1923 clib_warning("Unknown timeout type %d", timeout_type);
1927 switch(timeout_type) {
1928 case ACL_TIMEOUT_UDP_IDLE:
1929 sm->udp_session_idle_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1931 case ACL_TIMEOUT_TCP_IDLE:
1932 sm->tcp_session_idle_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1934 case ACL_TIMEOUT_TCP_TRANSIENT:
1935 sm->tcp_session_transient_timeout = (u64)(((f64)value)/ct->seconds_per_clock);
1938 clib_warning("Unknown timeout type %d", timeout_type);
1943 acl_set_session_max_entries(u32 value)
1945 acl_main_t *am = &acl_main;
1946 am->fa_conn_table_max_entries = value;
1950 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1952 acl_main_t *am = &acl_main;
1953 if ((eh < 256) && (value < 2))
1955 am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1963 static clib_error_t *
1964 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1966 acl_main_t *am = &acl_main;
1968 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1969 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1974 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1976 static clib_error_t *
1977 acl_set_aclplugin_fn (vlib_main_t * vm,
1978 unformat_input_t * input,
1979 vlib_cli_command_t * cmd)
1981 clib_error_t *error = 0;
1985 uword memory_size = 0;
1986 acl_main_t *am = &acl_main;
1988 /* The new datapath is the default. This command exists out of precaution and for comparing the two */
1989 if (unformat (input, "l2-datapath")) {
1990 if (unformat(input, "old")) {
1991 am->l2_input_classify_next_acl_ip4 = am->l2_input_classify_next_acl_old;
1992 am->l2_input_classify_next_acl_ip6 = am->l2_input_classify_next_acl_old;
1993 am->l2_output_classify_next_acl_ip4 = am->l2_output_classify_next_acl_old;
1994 am->l2_output_classify_next_acl_ip6 = am->l2_output_classify_next_acl_old;
1997 if (unformat(input, "new")) {
1998 am->l2_input_classify_next_acl_ip4 = am->fa_l2_input_classify_next_acl_ip4;
1999 am->l2_input_classify_next_acl_ip6 = am->fa_l2_input_classify_next_acl_ip6;
2000 am->l2_output_classify_next_acl_ip4 = am->fa_l2_output_classify_next_acl_ip4;
2001 am->l2_output_classify_next_acl_ip6 = am->fa_l2_output_classify_next_acl_ip6;
2006 if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
2007 if(!acl_set_skip_ipv6_eh(eh_val, val)) {
2008 error = clib_error_return(0, "expecting eh=0..255, value=0..1");
2012 if (unformat (input, "session")) {
2013 if (unformat (input, "clear")) {
2014 acl_main_t *am = &acl_main;
2015 vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2016 ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2019 if (unformat (input, "table")) {
2020 /* The commands here are for tuning/testing. No user-serviceable parts inside */
2021 if (unformat (input, "max-entries")) {
2022 if (!unformat(input, "%u", &val)) {
2023 error = clib_error_return(0,
2024 "expecting maximum number of entries, got `%U`",
2025 format_unformat_error, input);
2028 acl_set_session_max_entries(val);
2032 if (unformat (input, "hash-table-buckets")) {
2033 if (!unformat(input, "%u", &val)) {
2034 error = clib_error_return(0,
2035 "expecting maximum number of hash table buckets, got `%U`",
2036 format_unformat_error, input);
2039 am->fa_conn_table_hash_num_buckets = val;
2043 if (unformat (input, "hash-table-memory")) {
2044 if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
2045 error = clib_error_return(0,
2046 "expecting maximum amount of hash table memory, got `%U`",
2047 format_unformat_error, input);
2050 am->fa_conn_table_hash_memory_size = memory_size;
2056 if (unformat (input, "timeout")) {
2057 if (unformat(input, "udp")) {
2058 if(unformat(input, "idle")) {
2059 if (!unformat(input, "%u", &timeout)) {
2060 error = clib_error_return(0,
2061 "expecting timeout value in seconds, got `%U`",
2062 format_unformat_error, input);
2065 acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
2070 if (unformat(input, "tcp")) {
2071 if(unformat(input, "idle")) {
2072 if (!unformat(input, "%u", &timeout)) {
2073 error = clib_error_return(0,
2074 "expecting timeout value in seconds, got `%U`",
2075 format_unformat_error, input);
2078 acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
2082 if(unformat(input, "transient")) {
2083 if (!unformat(input, "%u", &timeout)) {
2084 error = clib_error_return(0,
2085 "expecting timeout value in seconds, got `%U`",
2086 format_unformat_error, input);
2089 acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
2101 static clib_error_t *
2102 acl_show_aclplugin_fn (vlib_main_t * vm,
2103 unformat_input_t * input,
2104 vlib_cli_command_t * cmd)
2106 clib_error_t *error = 0;
2107 acl_main_t *am = &acl_main;
2108 vnet_interface_main_t *im = &am->vnet_main->interface_main;
2110 vnet_sw_interface_t *swif;
2112 if (unformat (input, "sessions"))
2115 pool_foreach (swif, im->sw_interfaces,
2117 u32 sw_if_index = swif->sw_if_index;
2118 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;
2119 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;
2120 out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2122 vlib_cli_output(vm, "\n\n%s\n\n", out0);
2123 vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2124 am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2125 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);
2133 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2134 .path = "set acl-plugin",
2135 .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2136 .function = acl_set_aclplugin_fn,
2139 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2140 .path = "show acl-plugin",
2141 .short_help = "show acl-plugin sessions",
2142 .function = acl_show_aclplugin_fn,
2148 static clib_error_t *
2149 acl_init (vlib_main_t * vm)
2151 acl_main_t *am = &acl_main;
2152 clib_error_t *error = 0;
2153 memset (am, 0, sizeof (*am));
2155 am->vnet_main = vnet_get_main ();
2157 u8 *name = format (0, "acl_%08x%c", api_version, 0);
2159 /* Ask for a correctly-sized block of API message decode slots */
2160 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2161 VL_MSG_FIRST_AVAILABLE);
2163 error = acl_plugin_api_hookup (vm);
2166 /* Add our API messages to the global name_crc hash table */
2167 setup_message_id_table (am, &api_main);
2171 acl_setup_fa_nodes();
2172 am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2173 am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2174 am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2176 am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2177 am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2178 am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2182 for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2183 am->fa_conn_list_head[tt] = ~0;
2184 am->fa_conn_list_tail[tt] = ~0;
2188 am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2189 am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2190 am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2192 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2199 VLIB_INIT_FUNCTION (acl_init);