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.
16 #include <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
19 #include <acl/l2sess.h>
21 #include <vnet/l2/l2_classify.h>
22 #include <vnet/classify/input_acl.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vlibsocket/api.h>
28 /* define message IDs */
29 #include <acl/acl_msg_enum.h>
31 /* define message structures */
33 #include <acl/acl_all_api_h.h>
36 /* define generated endian-swappers */
38 #include <acl/acl_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #include <acl/acl_all_api_h.h>
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <acl/acl_all_api_h.h>
58 * A handy macro to set up a message reply.
59 * Assumes that the following variables are available:
60 * mp - pointer to request message
61 * rmp - pointer to reply message type
65 #define REPLY_MACRO(t) \
67 unix_shared_memory_queue_t * q = \
68 vl_api_client_index_to_input_queue (mp->client_index); \
72 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
73 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
74 rmp->context = mp->context; \
75 rmp->retval = ntohl(rv); \
77 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
80 #define REPLY_MACRO2(t, body) \
82 unix_shared_memory_queue_t * q; \
83 rv = vl_msg_api_pd_handler (mp, rv); \
84 q = vl_api_client_index_to_input_queue (mp->client_index); \
88 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
89 rmp->_vl_msg_id = ntohs((t)+am->msg_id_base); \
90 rmp->context = mp->context; \
91 rmp->retval = ntohl(rv); \
92 do {body;} while (0); \
93 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
96 #define REPLY_MACRO3(t, n, body) \
98 unix_shared_memory_queue_t * q; \
99 rv = vl_msg_api_pd_handler (mp, rv); \
100 q = vl_api_client_index_to_input_queue (mp->client_index); \
104 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
105 rmp->_vl_msg_id = ntohs((t)+am->msg_id_base); \
106 rmp->context = mp->context; \
107 rmp->retval = ntohl(rv); \
108 do {body;} while (0); \
109 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
112 #define VALIDATE_SW_IF_INDEX(mp) \
113 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
114 vnet_main_t *__vnm = vnet_get_main(); \
115 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
117 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
118 goto bad_sw_if_index; \
122 #define BAD_SW_IF_INDEX_LABEL \
130 /* List of message types that this plugin understands */
132 #define foreach_acl_plugin_api_msg \
133 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
134 _(ACL_ADD_REPLACE, acl_add_replace) \
135 _(ACL_DEL, acl_del) \
136 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
137 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
138 _(ACL_DUMP, acl_dump) \
139 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
140 _(MACIP_ACL_ADD, macip_acl_add) \
141 _(MACIP_ACL_DEL, macip_acl_del) \
142 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
143 _(MACIP_ACL_DUMP, macip_acl_dump) \
144 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
147 * This routine exists to convince the vlib plugin framework that
148 * we haven't accidentally copied a random .dll into the plugin directory.
150 * Also collects global variable pointers passed from the vpp engine
154 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
157 acl_main_t *am = &acl_main;
158 clib_error_t *error = 0;
161 am->vnet_main = h->vnet_main;
162 am->ethernet_main = h->ethernet_main;
164 l2sess_vlib_plugin_register(vm, h, from_early_init);
171 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
173 acl_main_t *am = &acl_main;
174 vl_api_acl_plugin_get_version_reply_t *rmp;
175 int msg_size = sizeof (*rmp);
176 unix_shared_memory_queue_t *q;
178 q = vl_api_client_index_to_input_queue (mp->client_index);
184 rmp = vl_msg_api_alloc (msg_size);
185 memset (rmp, 0, msg_size);
187 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
188 rmp->context = mp->context;
189 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
190 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
192 vl_msg_api_send_shmem (q, (u8 *) & rmp);
197 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
198 u32 * acl_list_index, u8 * tag)
200 acl_main_t *am = &acl_main;
203 acl_rule_t *acl_new_rules;
206 if (*acl_list_index != ~0)
208 /* They supplied some number, let's see if this ACL exists */
209 if (pool_is_free_index (am->acls, *acl_list_index))
211 /* tried to replace a non-existent ACL, no point doing anything */
216 /* Create and populate the rules */
217 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
218 CLIB_CACHE_LINE_BYTES);
221 /* Could not allocate rules. New or existing ACL - bail out regardless */
225 for (i = 0; i < count; i++)
227 r = &acl_new_rules[i];
228 r->is_permit = rules[i].is_permit;
229 r->is_ipv6 = rules[i].is_ipv6;
232 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
233 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
237 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
238 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
240 r->src_prefixlen = rules[i].src_ip_prefix_len;
241 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
242 r->proto = rules[i].proto;
243 r->src_port_or_type_first = rules[i].srcport_or_icmptype_first;
244 r->src_port_or_type_last = rules[i].srcport_or_icmptype_last;
245 r->dst_port_or_code_first = rules[i].dstport_or_icmpcode_first;
246 r->dst_port_or_code_last = rules[i].dstport_or_icmpcode_last;
247 r->tcp_flags_value = rules[i].tcp_flags_value;
248 r->tcp_flags_mask = rules[i].tcp_flags_mask;
251 if (~0 == *acl_list_index)
254 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
255 memset (a, 0, sizeof (*a));
256 /* Will return the newly allocated ACL index */
257 *acl_list_index = a - am->acls;
261 a = am->acls + *acl_list_index;
262 /* Get rid of the old rules */
263 clib_mem_free (a->rules);
265 a->rules = acl_new_rules;
267 memcpy (a->tag, tag, sizeof (a->tag));
273 acl_del_list (u32 acl_list_index)
275 acl_main_t *am = &acl_main;
278 if (pool_is_free_index (am->acls, acl_list_index))
283 /* delete any references to the ACL */
284 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
286 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
289 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
291 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
299 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
301 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
304 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
306 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
315 /* now we can delete the ACL itself */
316 a = &am->acls[acl_list_index];
319 clib_mem_free (a->rules);
321 pool_put (am->acls, a);
325 /* Some aids in ASCII graphing the content */
331 u8 ip4_5tuple_mask[] =
332 _(" dmac smac etype ")
333 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
340 _(" ttl pr checksum ")
349 _("L4 T/U sport dport ")
359 u8 ip6_5tuple_mask[] =
360 _(" dmac smac etype ")
361 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
363 _(0x0000) __ __ __ __
365 _(0x0004) __ __ XX __
367 _(0x0008) XX XX XX XX
368 _(0x000C) XX XX XX XX
369 _(0x0010) XX XX XX XX
370 _(0x0014) XX XX XX XX
372 _(0x0018) XX XX XX XX
373 _(0x001C) XX XX XX XX
374 _(0x0020) XX XX XX XX
375 _(0x0024) XX XX XX XX
376 _("L4T/U sport dport ")
377 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
384 static int count_skip (u8 * p, u32 size)
386 u64 *p64 = (u64 *) p;
387 /* Be tolerant to null pointer */
391 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
395 return (p64 - (u64 *) p) / 2;
399 acl_classify_add_del_table_big (vnet_classify_main_t * cm, u8 * mask,
400 u32 mask_len, u32 next_table_index,
401 u32 miss_next_index, u32 * table_index,
404 u32 nbuckets = 65536;
405 u32 memory_size = 2 << 30;
406 u32 skip = count_skip (mask, mask_len);
407 u32 match = (mask_len / 16) - skip;
408 u8 *skip_mask_ptr = mask + 16 * skip;
409 u32 current_data_flag = 0;
410 int current_data_offset = 0;
415 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
416 memory_size, skip, match,
417 next_table_index, miss_next_index,
418 table_index, current_data_flag,
419 current_data_offset, is_add);
423 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
424 u32 mask_len, u32 next_table_index,
425 u32 miss_next_index, u32 * table_index,
429 u32 memory_size = 2 << 20;
430 u32 skip = count_skip (mask, mask_len);
431 u32 match = (mask_len / 16) - skip;
432 u8 *skip_mask_ptr = mask + 16 * skip;
433 u32 current_data_flag = 0;
434 int current_data_offset = 0;
439 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
440 memory_size, skip, match,
441 next_table_index, miss_next_index,
442 table_index, current_data_flag,
443 current_data_offset, is_add);
448 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
450 vnet_classify_main_t *cm = &vnet_classify_main;
451 u32 ip4_table_index = ~0;
452 u32 ip6_table_index = ~0;
454 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
456 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
459 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
461 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
464 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
465 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
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,
469 &ip4_table_index, 0);
471 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
474 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
475 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
476 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
477 sizeof (ip6_5tuple_mask) - 1, ~0,
478 am->l2_input_classify_next_acl,
479 &ip6_table_index, 0);
486 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
488 vnet_classify_main_t *cm = &vnet_classify_main;
489 u32 ip4_table_index = ~0;
490 u32 ip6_table_index = ~0;
492 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
494 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
497 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
499 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
502 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
503 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
504 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
505 sizeof (ip4_5tuple_mask) - 1, ~0,
506 am->l2_output_classify_next_acl,
507 &ip4_table_index, 0);
509 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
512 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
513 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
514 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
515 sizeof (ip6_5tuple_mask) - 1, ~0,
516 am->l2_output_classify_next_acl,
517 &ip6_table_index, 0);
524 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
526 vnet_classify_main_t *cm = &vnet_classify_main;
527 u32 ip4_table_index = ~0;
528 u32 ip6_table_index = ~0;
531 /* in case there were previous tables attached */
532 acl_unhook_l2_input_classify (am, sw_if_index);
534 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
535 sizeof (ip4_5tuple_mask) - 1, ~0,
536 am->l2_input_classify_next_acl,
537 &ip4_table_index, 1);
541 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
542 sizeof (ip6_5tuple_mask) - 1, ~0,
543 am->l2_input_classify_next_acl,
544 &ip6_table_index, 1);
547 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
548 sizeof (ip4_5tuple_mask) - 1, ~0,
549 am->l2_input_classify_next_acl,
550 &ip4_table_index, 0);
554 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
555 ip6_table_index, ~0);
557 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
558 sw_if_index, ip4_table_index, ip6_table_index);
561 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
562 sizeof (ip6_5tuple_mask) - 1, ~0,
563 am->l2_input_classify_next_acl,
564 &ip6_table_index, 0);
565 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
566 sizeof (ip4_5tuple_mask) - 1, ~0,
567 am->l2_input_classify_next_acl,
568 &ip4_table_index, 0);
572 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
574 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
577 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
582 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
584 vnet_classify_main_t *cm = &vnet_classify_main;
585 u32 ip4_table_index = ~0;
586 u32 ip6_table_index = ~0;
589 /* in case there were previous tables attached */
590 acl_unhook_l2_output_classify (am, sw_if_index);
592 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
593 sizeof (ip4_5tuple_mask) - 1, ~0,
594 am->l2_output_classify_next_acl,
595 &ip4_table_index, 1);
599 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
600 sizeof (ip6_5tuple_mask) - 1, ~0,
601 am->l2_output_classify_next_acl,
602 &ip6_table_index, 1);
605 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
606 sizeof (ip4_5tuple_mask) - 1, ~0,
607 am->l2_output_classify_next_acl,
608 &ip4_table_index, 0);
612 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
613 ip6_table_index, ~0);
615 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
616 sw_if_index, ip4_table_index, ip6_table_index);
619 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
620 sizeof (ip6_5tuple_mask) - 1, ~0,
621 am->l2_output_classify_next_acl,
622 &ip6_table_index, 0);
623 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
624 sizeof (ip4_5tuple_mask) - 1, ~0,
625 am->l2_output_classify_next_acl,
626 &ip4_table_index, 0);
630 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
632 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
635 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
641 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
647 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
649 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
653 rv = acl_hook_l2_input_classify (am, sw_if_index);
657 rv = acl_unhook_l2_input_classify (am, sw_if_index);
664 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
670 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
672 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
676 rv = acl_hook_l2_output_classify (am, sw_if_index);
680 rv = acl_unhook_l2_output_classify (am, sw_if_index);
688 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
690 acl_main_t *am = &acl_main;
693 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
694 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
696 acl_interface_in_enable_disable (am, sw_if_index, 1);
700 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
701 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
703 acl_interface_out_enable_disable (am, sw_if_index, 1);
709 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
711 acl_main_t *am = &acl_main;
716 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
717 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
720 if (acl_list_index ==
721 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
723 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
728 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
730 acl_interface_in_enable_disable (am, sw_if_index, 0);
735 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
737 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
739 if (acl_list_index ==
740 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
742 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
747 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
749 acl_interface_out_enable_disable (am, sw_if_index, 0);
756 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
758 acl_main_t *am = &acl_main;
761 acl_interface_in_enable_disable (am, sw_if_index, 0);
762 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
763 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
767 acl_interface_out_enable_disable (am, sw_if_index, 0);
768 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
769 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
774 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
781 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
786 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
793 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
795 u8 *p = vlib_buffer_get_current (b0) + offset;
800 acl_get_l4_proto (vlib_buffer_t * b0, int node_is_ip6)
812 proto = *((u8 *) vlib_buffer_get_current (b0) + proto_offset);
817 acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2, int prefixlen,
822 /* match any always succeeds */
827 if (memcmp (addr1, addr2, prefixlen / 8))
829 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
834 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
835 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
836 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
837 return (b1 & mask0) == b2;
841 /* The prefix fits into integer number of bytes, so nothing left to do */
847 uint32_t a1 = ntohl (addr1->ip4.as_u32);
848 uint32_t a2 = ntohl (addr2->ip4.as_u32);
849 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
850 return (a1 & mask0) == a2;
855 acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
857 return ((port >= port_first) && (port <= port_last));
861 acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
862 u8 * r_action, int *r_is_ip6, u32 * r_acl_match_p,
863 u32 * r_rule_match_p, u32 * trace_bitmap)
865 ethernet_header_t *h0;
868 ip46_address_t src, dst;
879 h0 = vlib_buffer_get_current (b0);
880 type0 = clib_net_to_host_u16 (h0->type);
881 is_ip4 = (type0 == ETHERNET_TYPE_IP4);
882 is_ip6 = (type0 == ETHERNET_TYPE_IP6);
884 if (!(is_ip4 || is_ip6))
888 /* The bunch of hardcoded offsets here is intentional to get rid of them
889 ASAP, when getting to a faster matching code */
892 clib_memcpy (&src.ip4, get_ptr_to_offset (b0, 26), 4);
893 clib_memcpy (&dst.ip4, get_ptr_to_offset (b0, 30), 4);
894 proto = acl_get_l4_proto (b0, 0);
897 *trace_bitmap |= 0x00000001;
899 src_port = *(u8 *) get_ptr_to_offset (b0, 34);
901 dst_port = *(u8 *) get_ptr_to_offset (b0, 35);
906 src_port = (*(u16 *) get_ptr_to_offset (b0, 34));
907 dst_port = (*(u16 *) get_ptr_to_offset (b0, 36));
908 /* UDP gets ability to check on an oddball data byte as a bonus */
909 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
912 else /* is_ipv6 implicitly */
914 clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
915 clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
916 proto = acl_get_l4_proto (b0, 1);
919 *trace_bitmap |= 0x00000002;
921 src_port = *(u8 *) get_ptr_to_offset (b0, 54);
923 dst_port = *(u8 *) get_ptr_to_offset (b0, 55);
928 src_port = (*(u16 *) get_ptr_to_offset (b0, 54));
929 dst_port = (*(u16 *) get_ptr_to_offset (b0, 56));
930 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
933 if (pool_is_free_index (am->acls, acl_index))
936 *r_acl_match_p = acl_index;
938 *r_rule_match_p = -1;
939 /* the ACL does not exist but is used for policy. Block traffic. */
942 a = am->acls + acl_index;
943 for (i = 0; i < a->count; i++)
946 if (is_ip6 != r->is_ipv6)
950 if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
952 if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
956 if (proto != r->proto)
959 (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
963 (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
966 /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
967 if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
970 /* everything matches! */
971 *r_action = r->is_permit;
974 *r_acl_match_p = acl_index;
983 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
984 u32 * acl_match_p, u32 * rule_match_p,
987 acl_main_t *am = &acl_main;
991 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
992 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
996 (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
997 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
1001 *nextp = am->acl_in_ip6_match_next[action];
1005 *nextp = am->acl_in_ip4_match_next[action];
1010 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1012 /* If there are ACLs and none matched, deny by default */
1019 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
1020 u32 * acl_match_p, u32 * rule_match_p,
1023 acl_main_t *am = &acl_main;
1027 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1028 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1031 if (acl_packet_match
1032 (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
1033 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
1037 *nextp = am->acl_out_ip6_match_next[action];
1041 *nextp = am->acl_out_ip4_match_next[action];
1046 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1048 /* If there are ACLs and none matched, deny by default */
1060 } macip_match_type_t;
1063 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1069 for (i = 0; i < vec_len (mv); i++)
1071 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1072 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1082 /* Get metric used to sort match types.
1083 The more specific and the more often seen - the bigger the metric */
1085 match_type_metric (macip_match_type_t * m)
1087 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1088 return m->prefix_len + m->is_ipv6 + 10 * m->count;
1092 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1094 /* Ascending sort based on the metric values */
1095 return match_type_metric (m1) - match_type_metric (m2);
1099 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1101 macip_match_type_t *mvec = NULL;
1102 macip_match_type_t *mt;
1103 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1105 u32 match_type_index;
1108 vnet_classify_main_t *cm = &vnet_classify_main;
1110 /* Count the number of different types of rules */
1111 for (i = 0; i < a->count; i++)
1115 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1116 a->rules[i].src_prefixlen,
1117 a->rules[i].is_ipv6)))
1119 match_type_index = vec_len (mvec);
1120 vec_validate (mvec, match_type_index);
1121 memcpy (mvec[match_type_index].mac_mask,
1122 a->rules[match_type_index].src_mac_mask, 6);
1123 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1124 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1125 mvec[match_type_index].table_index = ~0;
1127 mvec[match_type_index].count++;
1129 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1130 vec_sort_with_function (mvec, match_type_compare);
1131 /* Create the classifier tables */
1133 vec_foreach (mt, mvec)
1136 int is6 = a->rules[i].is_ipv6;
1137 int l3_src_offs = is6 ? 22 : 26; /* See the ascii art packet format above to verify these */
1138 memset (mask, 0, sizeof (mask));
1139 memcpy (&mask[6], mt->mac_mask, 6);
1140 for (i = 0; i < (mt->prefix_len / 8); i++)
1142 mask[l3_src_offs + i] = 0xff;
1144 if (mt->prefix_len % 8)
1146 mask[l3_src_offs + (mt->prefix_len / 8)] =
1147 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1149 mask_len = ((l3_src_offs + (mt->prefix_len / 8)) / 16 + 1) * 16;
1150 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1151 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1153 last_table = mt->table_index;
1155 a->ip4_table_index = ~0;
1156 a->ip6_table_index = ~0;
1157 a->l2_table_index = last_table;
1159 /* Populate the classifier tables with rules from the MACIP ACL */
1160 for (i = 0; i < a->count; i++)
1164 int is6 = a->rules[i].is_ipv6;
1165 int l3_src_offs = is6 ? 22 : 26; /* See the ascii art packet format above to verify these */
1166 memset (mask, 0, sizeof (mask));
1167 memcpy (&mask[6], a->rules[i].src_mac, 6);
1170 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1174 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1177 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1178 a->rules[i].src_prefixlen,
1179 a->rules[i].is_ipv6);
1180 /* add session to table mvec[match_type_index].table_index; */
1181 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1182 mask, a->rules[i].is_permit ? ~0 : 0, i,
1183 0, action, metadata, 1);
1189 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1191 vnet_classify_main_t *cm = &vnet_classify_main;
1192 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1194 if (a->ip4_table_index != ~0)
1196 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1197 a->ip4_table_index = ~0;
1199 if (a->ip6_table_index != ~0)
1201 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1202 a->ip6_table_index = ~0;
1204 if (a->l2_table_index != ~0)
1206 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1207 a->l2_table_index = ~0;
1212 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1213 u32 * acl_list_index, u8 * tag)
1215 acl_main_t *am = &acl_main;
1216 macip_acl_list_t *a;
1217 macip_acl_rule_t *r;
1218 macip_acl_rule_t *acl_new_rules;
1221 /* Create and populate the rules */
1222 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1223 CLIB_CACHE_LINE_BYTES);
1226 /* Could not allocate rules. New or existing ACL - bail out regardless */
1230 for (i = 0; i < count; i++)
1232 r = &acl_new_rules[i];
1233 r->is_permit = rules[i].is_permit;
1234 r->is_ipv6 = rules[i].is_ipv6;
1235 memcpy (&r->src_mac, rules[i].src_mac, 6);
1236 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1238 memcpy (&r->src_ip_addr, rules[i].src_ip_addr, sizeof (r->src_ip_addr));
1239 r->src_prefixlen = rules[i].src_ip_prefix_len;
1243 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1244 memset (a, 0, sizeof (*a));
1245 /* Will return the newly allocated ACL index */
1246 *acl_list_index = a - am->macip_acls;
1248 a->rules = acl_new_rules;
1250 memcpy (a->tag, tag, sizeof (a->tag));
1252 /* Create and populate the classifer tables */
1253 macip_create_classify_tables (am, *acl_list_index);
1259 /* No check for validity of sw_if_index - the callers were supposed to validate */
1262 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1263 u32 macip_acl_index)
1265 macip_acl_list_t *a;
1267 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1271 a = &am->macip_acls[macip_acl_index];
1272 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1273 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1274 /* Apply the classifier tables for L2 ACLs */
1276 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1277 a->ip6_table_index, a->l2_table_index, 1);
1282 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1285 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1286 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1287 /* remove the classifier tables off the interface L2 ACL */
1288 rv = vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, ~0, ~0, ~0, 0);
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 *sm = &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 *sm = &acl_main;
1378 vl_api_acl_interface_add_del_reply_t *rmp;
1380 VALIDATE_SW_IF_INDEX (mp);
1383 acl_interface_add_del_inout_acl (ntohl (mp->sw_if_index), mp->is_add,
1384 mp->is_input, ntohl (mp->acl_index));
1386 BAD_SW_IF_INDEX_LABEL;
1388 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1392 vl_api_acl_interface_set_acl_list_t_handler
1393 (vl_api_acl_interface_set_acl_list_t * mp)
1395 acl_main_t *sm = &acl_main;
1396 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1399 VALIDATE_SW_IF_INDEX (mp);
1400 u32 sw_if_index = ntohl (mp->sw_if_index);
1402 acl_interface_reset_inout_acls (sw_if_index, 0);
1403 acl_interface_reset_inout_acls (sw_if_index, 1);
1405 for (i = 0; i < mp->count; i++)
1407 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1408 ntohl (mp->acls[i]));
1411 BAD_SW_IF_INDEX_LABEL;
1413 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1417 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1419 api_rule->is_permit = r->is_permit;
1420 api_rule->is_ipv6 = r->is_ipv6;
1423 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1424 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1428 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1429 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1431 api_rule->src_ip_prefix_len = r->src_prefixlen;
1432 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1433 api_rule->proto = r->proto;
1434 api_rule->srcport_or_icmptype_first = r->src_port_or_type_first;
1435 api_rule->srcport_or_icmptype_last = r->src_port_or_type_last;
1436 api_rule->dstport_or_icmpcode_first = r->dst_port_or_code_first;
1437 api_rule->dstport_or_icmpcode_last = r->dst_port_or_code_last;
1438 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1439 api_rule->tcp_flags_value = r->tcp_flags_value;
1443 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1444 acl_list_t * acl, u32 context)
1446 vl_api_acl_details_t *mp;
1447 vl_api_acl_rule_t *rules;
1449 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1451 mp = vl_msg_api_alloc (msg_size);
1452 memset (mp, 0, msg_size);
1453 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1455 /* fill in the message */
1456 mp->context = context;
1457 mp->count = htonl (acl->count);
1458 mp->acl_index = htonl (acl - am->acls);
1459 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1460 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1462 for (i = 0; i < acl->count; i++)
1464 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1467 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1468 vl_msg_api_send_shmem (q, (u8 *) & mp);
1473 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1475 acl_main_t *am = &acl_main;
1480 unix_shared_memory_queue_t *q;
1482 q = vl_api_client_index_to_input_queue (mp->client_index);
1488 if (mp->acl_index == ~0)
1491 /* Just dump all ACLs */
1492 pool_foreach (acl, am->acls,
1494 send_acl_details(am, q, acl, mp->context);
1500 acl_index = ntohl (mp->acl_index);
1501 if (!pool_is_free_index (am->acls, acl_index))
1503 acl = &am->acls[acl_index];
1504 send_acl_details (am, q, acl, mp->context);
1510 /* FIXME API: should we signal an error here at all ? */
1516 send_acl_interface_list_details (acl_main_t * am,
1517 unix_shared_memory_queue_t * q,
1518 u32 sw_if_index, u32 context)
1520 vl_api_acl_interface_list_details_t *mp;
1527 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1528 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1530 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1531 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1532 count = n_input + n_output;
1534 msg_size = sizeof (*mp);
1535 msg_size += sizeof (mp->acls[0]) * count;
1537 mp = vl_msg_api_alloc (msg_size);
1538 memset (mp, 0, msg_size);
1540 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1542 /* fill in the message */
1543 mp->context = context;
1544 mp->sw_if_index = htonl (sw_if_index);
1546 mp->n_input = n_input;
1547 for (i = 0; i < n_input; i++)
1549 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1551 for (i = 0; i < n_output; i++)
1553 mp->acls[n_input + i] =
1554 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1557 vl_msg_api_send_shmem (q, (u8 *) & mp);
1561 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1564 acl_main_t *am = &acl_main;
1565 vnet_sw_interface_t *swif;
1566 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1570 unix_shared_memory_queue_t *q;
1572 q = vl_api_client_index_to_input_queue (mp->client_index);
1578 if (mp->sw_if_index == ~0)
1581 pool_foreach (swif, im->sw_interfaces,
1583 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1589 VALIDATE_SW_IF_INDEX (mp);
1590 sw_if_index = ntohl (mp->sw_if_index);
1591 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1595 BAD_SW_IF_INDEX_LABEL;
1598 /* FIXME API: should we signal an error here at all ? */
1603 /* MACIP ACL API handlers */
1606 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1608 vl_api_macip_acl_add_reply_t *rmp;
1609 acl_main_t *am = &acl_main;
1611 u32 acl_list_index = ~0;
1614 macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1617 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1619 rmp->acl_index = htonl(acl_list_index);
1625 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1627 acl_main_t *sm = &acl_main;
1628 vl_api_macip_acl_del_reply_t *rmp;
1631 rv = macip_acl_del_list (ntohl (mp->acl_index));
1633 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1637 vl_api_macip_acl_interface_add_del_t_handler
1638 (vl_api_macip_acl_interface_add_del_t * mp)
1640 acl_main_t *sm = &acl_main;
1641 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1643 VALIDATE_SW_IF_INDEX (mp);
1646 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1647 ntohl (mp->acl_index));
1649 BAD_SW_IF_INDEX_LABEL;
1651 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1655 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1656 macip_acl_list_t * acl, u32 context)
1658 vl_api_macip_acl_details_t *mp;
1659 vl_api_macip_acl_rule_t *rules;
1660 macip_acl_rule_t *r;
1662 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1664 mp = vl_msg_api_alloc (msg_size);
1665 memset (mp, 0, msg_size);
1666 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1668 /* fill in the message */
1669 mp->context = context;
1672 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1673 mp->count = htonl (acl->count);
1674 mp->acl_index = htonl (acl - am->macip_acls);
1676 for (i = 0; i < acl->count; i++)
1679 rules[i].is_permit = r->is_permit;
1680 rules[i].is_ipv6 = r->is_ipv6;
1681 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1682 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1683 sizeof (r->src_mac_mask));
1685 memcpy (rules[i].src_ip_addr, &r->src_ip_addr,
1686 sizeof (r->src_ip_addr));
1687 rules[i].src_ip_prefix_len = r->src_prefixlen;
1692 /* No martini, no party - no ACL applied to this interface. */
1697 vl_msg_api_send_shmem (q, (u8 *) & mp);
1702 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1704 acl_main_t *am = &acl_main;
1705 macip_acl_list_t *acl;
1707 unix_shared_memory_queue_t *q;
1709 q = vl_api_client_index_to_input_queue (mp->client_index);
1715 if (mp->acl_index == ~0)
1717 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1718 pool_foreach (acl, am->macip_acls, (
1720 send_macip_acl_details (am, q, acl,
1728 u32 acl_index = ntohl (mp->acl_index);
1729 if (!pool_is_free_index (am->macip_acls, acl_index))
1731 acl = &am->macip_acls[acl_index];
1732 send_macip_acl_details (am, q, acl, mp->context);
1738 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1741 acl_main_t *am = &acl_main;
1742 vl_api_macip_acl_interface_get_reply_t *rmp;
1743 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1744 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1745 unix_shared_memory_queue_t *q;
1748 q = vl_api_client_index_to_input_queue (mp->client_index);
1754 rmp = vl_msg_api_alloc (msg_size);
1755 memset (rmp, 0, msg_size);
1757 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1758 rmp->context = mp->context;
1759 rmp->count = htonl (count);
1760 for (i = 0; i < count; i++)
1762 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1765 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1770 /* Set up the API message handling tables */
1771 static clib_error_t *
1772 acl_plugin_api_hookup (vlib_main_t * vm)
1774 acl_main_t *sm = &acl_main;
1776 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1778 vl_api_##n##_t_handler, \
1780 vl_api_##n##_t_endian, \
1781 vl_api_##n##_t_print, \
1782 sizeof(vl_api_##n##_t), 1);
1783 foreach_acl_plugin_api_msg;
1789 #define vl_msg_name_crc_list
1790 #include <acl/acl_all_api_h.h>
1791 #undef vl_msg_name_crc_list
1794 setup_message_id_table (acl_main_t * sm, api_main_t * am)
1796 #define _(id,n,crc) \
1797 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1798 foreach_vl_msg_name_crc_acl;
1803 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1804 u32 next_out_ip4, u32 next_out_ip6)
1806 acl_main_t *am = &acl_main;
1807 u32 act = am->n_match_actions;
1808 if (am->n_match_actions == 255)
1812 am->n_match_actions++;
1813 am->acl_in_ip4_match_next[act] = next_in_ip4;
1814 am->acl_in_ip6_match_next[act] = next_in_ip6;
1815 am->acl_out_ip4_match_next[act] = next_out_ip4;
1816 am->acl_out_ip6_match_next[act] = next_out_ip6;
1821 acl_setup_nodes (void)
1823 vlib_main_t *vm = vlib_get_main ();
1824 acl_main_t *am = &acl_main;
1827 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1828 am->l2_input_classify_next_acl =
1829 vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1830 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1831 am->l2_output_classify_next_acl =
1832 vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1834 feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1835 l2input_get_feat_names (),
1836 am->acl_in_node_input_next_node_index);
1838 memset (&am->acl_in_ip4_match_next[0], 0,
1839 sizeof (am->acl_in_ip4_match_next));
1840 memset (&am->acl_in_ip6_match_next[0], 0,
1841 sizeof (am->acl_in_ip6_match_next));
1842 memset (&am->acl_out_ip4_match_next[0], 0,
1843 sizeof (am->acl_out_ip4_match_next));
1844 memset (&am->acl_out_ip6_match_next[0], 0,
1845 sizeof (am->acl_out_ip6_match_next));
1846 am->n_match_actions = 0;
1848 register_match_action_nexts (0, 0, 0, 0); /* drop */
1849 register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1850 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 */
1855 static clib_error_t *
1856 acl_init (vlib_main_t * vm)
1858 acl_main_t *am = &acl_main;
1859 clib_error_t *error = 0;
1860 memset (am, 0, sizeof (*am));
1862 am->vnet_main = vnet_get_main ();
1864 u8 *name = format (0, "acl_%08x%c", api_version, 0);
1866 /* Ask for a correctly-sized block of API message decode slots */
1867 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1868 VL_MSG_FIRST_AVAILABLE);
1870 error = acl_plugin_api_hookup (vm);
1873 /* Add our API messages to the global name_crc hash table */
1874 setup_message_id_table (am, &api_main);
1881 VLIB_INIT_FUNCTION (acl_init);