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 * A handy macro to set up a message reply.
62 * Assumes that the following variables are available:
63 * mp - pointer to request message
64 * rmp - pointer to reply message type
68 #define REPLY_MACRO(t) \
70 unix_shared_memory_queue_t * q = \
71 vl_api_client_index_to_input_queue (mp->client_index); \
75 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
76 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
77 rmp->context = mp->context; \
78 rmp->retval = ntohl(rv); \
80 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
83 #define REPLY_MACRO2(t, body) \
85 unix_shared_memory_queue_t * q; \
86 rv = vl_msg_api_pd_handler (mp, rv); \
87 q = vl_api_client_index_to_input_queue (mp->client_index); \
91 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
92 rmp->_vl_msg_id = ntohs((t)+am->msg_id_base); \
93 rmp->context = mp->context; \
94 rmp->retval = ntohl(rv); \
95 do {body;} while (0); \
96 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
99 #define REPLY_MACRO3(t, n, body) \
101 unix_shared_memory_queue_t * q; \
102 rv = vl_msg_api_pd_handler (mp, rv); \
103 q = vl_api_client_index_to_input_queue (mp->client_index); \
107 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
108 rmp->_vl_msg_id = ntohs((t)+am->msg_id_base); \
109 rmp->context = mp->context; \
110 rmp->retval = ntohl(rv); \
111 do {body;} while (0); \
112 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
116 /* List of message types that this plugin understands */
118 #define foreach_acl_plugin_api_msg \
119 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
120 _(ACL_ADD_REPLACE, acl_add_replace) \
121 _(ACL_DEL, acl_del) \
122 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
123 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list) \
124 _(ACL_DUMP, acl_dump) \
125 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
126 _(MACIP_ACL_ADD, macip_acl_add) \
127 _(MACIP_ACL_DEL, macip_acl_del) \
128 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
129 _(MACIP_ACL_DUMP, macip_acl_dump) \
130 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
133 VLIB_PLUGIN_REGISTER () = {
134 .version = VPP_BUILD_VER,
139 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
141 acl_main_t *am = &acl_main;
142 vl_api_acl_plugin_get_version_reply_t *rmp;
143 int msg_size = sizeof (*rmp);
144 unix_shared_memory_queue_t *q;
146 q = vl_api_client_index_to_input_queue (mp->client_index);
152 rmp = vl_msg_api_alloc (msg_size);
153 memset (rmp, 0, msg_size);
155 ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
156 rmp->context = mp->context;
157 rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
158 rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
160 vl_msg_api_send_shmem (q, (u8 *) & rmp);
165 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
166 u32 * acl_list_index, u8 * tag)
168 acl_main_t *am = &acl_main;
171 acl_rule_t *acl_new_rules;
174 if (*acl_list_index != ~0)
176 /* They supplied some number, let's see if this ACL exists */
177 if (pool_is_free_index (am->acls, *acl_list_index))
179 /* tried to replace a non-existent ACL, no point doing anything */
184 /* Create and populate the rules */
185 acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
186 CLIB_CACHE_LINE_BYTES);
189 /* Could not allocate rules. New or existing ACL - bail out regardless */
193 for (i = 0; i < count; i++)
195 r = &acl_new_rules[i];
196 r->is_permit = rules[i].is_permit;
197 r->is_ipv6 = rules[i].is_ipv6;
200 memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
201 memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
205 memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
206 memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
208 r->src_prefixlen = rules[i].src_ip_prefix_len;
209 r->dst_prefixlen = rules[i].dst_ip_prefix_len;
210 r->proto = rules[i].proto;
211 r->src_port_or_type_first = rules[i].srcport_or_icmptype_first;
212 r->src_port_or_type_last = rules[i].srcport_or_icmptype_last;
213 r->dst_port_or_code_first = rules[i].dstport_or_icmpcode_first;
214 r->dst_port_or_code_last = rules[i].dstport_or_icmpcode_last;
215 r->tcp_flags_value = rules[i].tcp_flags_value;
216 r->tcp_flags_mask = rules[i].tcp_flags_mask;
219 if (~0 == *acl_list_index)
222 pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
223 memset (a, 0, sizeof (*a));
224 /* Will return the newly allocated ACL index */
225 *acl_list_index = a - am->acls;
229 a = am->acls + *acl_list_index;
230 /* Get rid of the old rules */
231 clib_mem_free (a->rules);
233 a->rules = acl_new_rules;
235 memcpy (a->tag, tag, sizeof (a->tag));
241 acl_del_list (u32 acl_list_index)
243 acl_main_t *am = &acl_main;
246 if (pool_is_free_index (am->acls, acl_list_index))
251 /* delete any references to the ACL */
252 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
254 for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
257 if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
259 vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
267 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
269 for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
272 if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
274 vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
283 /* now we can delete the ACL itself */
284 a = &am->acls[acl_list_index];
287 clib_mem_free (a->rules);
289 pool_put (am->acls, a);
293 /* Some aids in ASCII graphing the content */
299 u8 ip4_5tuple_mask[] =
300 _(" dmac smac etype ")
301 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
308 _(" ttl pr checksum ")
317 _("L4 T/U sport dport ")
327 u8 ip6_5tuple_mask[] =
328 _(" dmac smac etype ")
329 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
331 _(0x0000) __ __ __ __
333 _(0x0004) __ __ XX __
335 _(0x0008) XX XX XX XX
336 _(0x000C) XX XX XX XX
337 _(0x0010) XX XX XX XX
338 _(0x0014) XX XX XX XX
340 _(0x0018) XX XX XX XX
341 _(0x001C) XX XX XX XX
342 _(0x0020) XX XX XX XX
343 _(0x0024) XX XX XX XX
344 _("L4T/U sport dport ")
345 _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
352 static int count_skip (u8 * p, u32 size)
354 u64 *p64 = (u64 *) p;
355 /* Be tolerant to null pointer */
359 while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
363 return (p64 - (u64 *) p) / 2;
367 acl_classify_add_del_table_big (vnet_classify_main_t * cm, u8 * mask,
368 u32 mask_len, u32 next_table_index,
369 u32 miss_next_index, u32 * table_index,
372 u32 nbuckets = 65536;
373 u32 memory_size = 2 << 30;
374 u32 skip = count_skip (mask, mask_len);
375 u32 match = (mask_len / 16) - skip;
376 u8 *skip_mask_ptr = mask + 16 * skip;
377 u32 current_data_flag = 0;
378 int current_data_offset = 0;
383 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
384 memory_size, skip, match,
385 next_table_index, miss_next_index,
386 table_index, current_data_flag,
387 current_data_offset, is_add,
388 1 /* delete_chain */);
392 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
393 u32 mask_len, u32 next_table_index,
394 u32 miss_next_index, u32 * table_index,
398 u32 memory_size = 2 << 20;
399 u32 skip = count_skip (mask, mask_len);
400 u32 match = (mask_len / 16) - skip;
401 u8 *skip_mask_ptr = mask + 16 * skip;
402 u32 current_data_flag = 0;
403 int current_data_offset = 0;
408 return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
409 memory_size, skip, match,
410 next_table_index, miss_next_index,
411 table_index, current_data_flag,
412 current_data_offset, is_add,
413 1 /* delete_chain */);
418 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
420 vnet_classify_main_t *cm = &vnet_classify_main;
421 u32 ip4_table_index = ~0;
422 u32 ip6_table_index = ~0;
424 vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
426 vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
429 vnet_l2_input_classify_enable_disable (sw_if_index, 0);
431 if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
434 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
435 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
436 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
437 sizeof (ip4_5tuple_mask) - 1, ~0,
438 am->l2_input_classify_next_acl,
439 &ip4_table_index, 0);
441 if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
444 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
445 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
446 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
447 sizeof (ip6_5tuple_mask) - 1, ~0,
448 am->l2_input_classify_next_acl,
449 &ip6_table_index, 0);
456 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
458 vnet_classify_main_t *cm = &vnet_classify_main;
459 u32 ip4_table_index = ~0;
460 u32 ip6_table_index = ~0;
462 vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
464 vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
467 vnet_l2_output_classify_enable_disable (sw_if_index, 0);
469 if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
472 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
473 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
474 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
475 sizeof (ip4_5tuple_mask) - 1, ~0,
476 am->l2_output_classify_next_acl,
477 &ip4_table_index, 0);
479 if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
482 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
483 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
484 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
485 sizeof (ip6_5tuple_mask) - 1, ~0,
486 am->l2_output_classify_next_acl,
487 &ip6_table_index, 0);
494 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
496 vnet_classify_main_t *cm = &vnet_classify_main;
497 u32 ip4_table_index = ~0;
498 u32 ip6_table_index = ~0;
501 /* in case there were previous tables attached */
502 acl_unhook_l2_input_classify (am, sw_if_index);
504 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
505 sizeof (ip4_5tuple_mask) - 1, ~0,
506 am->l2_input_classify_next_acl,
507 &ip4_table_index, 1);
511 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
512 sizeof (ip6_5tuple_mask) - 1, ~0,
513 am->l2_input_classify_next_acl,
514 &ip6_table_index, 1);
517 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
518 sizeof (ip4_5tuple_mask) - 1, ~0,
519 am->l2_input_classify_next_acl,
520 &ip4_table_index, 0);
524 vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
525 ip6_table_index, ~0);
527 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
528 sw_if_index, ip4_table_index, ip6_table_index);
531 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
532 sizeof (ip6_5tuple_mask) - 1, ~0,
533 am->l2_input_classify_next_acl,
534 &ip6_table_index, 0);
535 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
536 sizeof (ip4_5tuple_mask) - 1, ~0,
537 am->l2_input_classify_next_acl,
538 &ip4_table_index, 0);
542 am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
544 am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
547 vnet_l2_input_classify_enable_disable (sw_if_index, 1);
552 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
554 vnet_classify_main_t *cm = &vnet_classify_main;
555 u32 ip4_table_index = ~0;
556 u32 ip6_table_index = ~0;
559 /* in case there were previous tables attached */
560 acl_unhook_l2_output_classify (am, sw_if_index);
562 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
563 sizeof (ip4_5tuple_mask) - 1, ~0,
564 am->l2_output_classify_next_acl,
565 &ip4_table_index, 1);
569 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
570 sizeof (ip6_5tuple_mask) - 1, ~0,
571 am->l2_output_classify_next_acl,
572 &ip6_table_index, 1);
575 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
576 sizeof (ip4_5tuple_mask) - 1, ~0,
577 am->l2_output_classify_next_acl,
578 &ip4_table_index, 0);
582 vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
583 ip6_table_index, ~0);
585 ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
586 sw_if_index, ip4_table_index, ip6_table_index);
589 acl_classify_add_del_table_big (cm, ip6_5tuple_mask,
590 sizeof (ip6_5tuple_mask) - 1, ~0,
591 am->l2_output_classify_next_acl,
592 &ip6_table_index, 0);
593 acl_classify_add_del_table_big (cm, ip4_5tuple_mask,
594 sizeof (ip4_5tuple_mask) - 1, ~0,
595 am->l2_output_classify_next_acl,
596 &ip4_table_index, 0);
600 am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
602 am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
605 vnet_l2_output_classify_enable_disable (sw_if_index, 1);
611 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
617 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
619 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
623 rv = acl_hook_l2_input_classify (am, sw_if_index);
627 rv = acl_unhook_l2_input_classify (am, sw_if_index);
634 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
640 if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
642 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
646 rv = acl_hook_l2_output_classify (am, sw_if_index);
650 rv = acl_unhook_l2_output_classify (am, sw_if_index);
658 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
660 acl_main_t *am = &acl_main;
663 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
664 vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
666 acl_interface_in_enable_disable (am, sw_if_index, 1);
670 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
671 vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
673 acl_interface_out_enable_disable (am, sw_if_index, 1);
679 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
681 acl_main_t *am = &acl_main;
686 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
687 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
690 if (acl_list_index ==
691 am->input_acl_vec_by_sw_if_index[sw_if_index][i])
693 vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
698 if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
700 acl_interface_in_enable_disable (am, sw_if_index, 0);
705 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
707 i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
709 if (acl_list_index ==
710 am->output_acl_vec_by_sw_if_index[sw_if_index][i])
712 vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
717 if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
719 acl_interface_out_enable_disable (am, sw_if_index, 0);
726 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
728 acl_main_t *am = &acl_main;
731 acl_interface_in_enable_disable (am, sw_if_index, 0);
732 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
733 vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
737 acl_interface_out_enable_disable (am, sw_if_index, 0);
738 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
739 vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
744 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
751 acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
756 acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
763 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
765 u8 *p = vlib_buffer_get_current (b0) + offset;
770 acl_get_l4_proto (vlib_buffer_t * b0, int node_is_ip6)
782 proto = *((u8 *) vlib_buffer_get_current (b0) + proto_offset);
787 acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2, int prefixlen,
792 /* match any always succeeds */
797 if (memcmp (addr1, addr2, prefixlen / 8))
799 /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
804 u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
805 u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
806 u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
807 return (b1 & mask0) == b2;
811 /* The prefix fits into integer number of bytes, so nothing left to do */
817 uint32_t a1 = ntohl (addr1->ip4.as_u32);
818 uint32_t a2 = ntohl (addr2->ip4.as_u32);
819 uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
820 return (a1 & mask0) == a2;
825 acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
827 return ((port >= port_first) && (port <= port_last));
831 acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
832 u8 * r_action, int *r_is_ip6, u32 * r_acl_match_p,
833 u32 * r_rule_match_p, u32 * trace_bitmap)
835 ethernet_header_t *h0;
838 ip46_address_t src, dst;
849 h0 = vlib_buffer_get_current (b0);
850 type0 = clib_net_to_host_u16 (h0->type);
851 is_ip4 = (type0 == ETHERNET_TYPE_IP4);
852 is_ip6 = (type0 == ETHERNET_TYPE_IP6);
854 if (!(is_ip4 || is_ip6))
858 /* The bunch of hardcoded offsets here is intentional to get rid of them
859 ASAP, when getting to a faster matching code */
862 clib_memcpy (&src.ip4, get_ptr_to_offset (b0, 26), 4);
863 clib_memcpy (&dst.ip4, get_ptr_to_offset (b0, 30), 4);
864 proto = acl_get_l4_proto (b0, 0);
867 *trace_bitmap |= 0x00000001;
869 src_port = *(u8 *) get_ptr_to_offset (b0, 34);
871 dst_port = *(u8 *) get_ptr_to_offset (b0, 35);
876 src_port = (*(u16 *) get_ptr_to_offset (b0, 34));
877 dst_port = (*(u16 *) get_ptr_to_offset (b0, 36));
878 /* UDP gets ability to check on an oddball data byte as a bonus */
879 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
882 else /* is_ipv6 implicitly */
884 clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
885 clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
886 proto = acl_get_l4_proto (b0, 1);
889 *trace_bitmap |= 0x00000002;
891 src_port = *(u8 *) get_ptr_to_offset (b0, 54);
893 dst_port = *(u8 *) get_ptr_to_offset (b0, 55);
898 src_port = (*(u16 *) get_ptr_to_offset (b0, 54));
899 dst_port = (*(u16 *) get_ptr_to_offset (b0, 56));
900 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
903 if (pool_is_free_index (am->acls, acl_index))
906 *r_acl_match_p = acl_index;
908 *r_rule_match_p = -1;
909 /* the ACL does not exist but is used for policy. Block traffic. */
912 a = am->acls + acl_index;
913 for (i = 0; i < a->count; i++)
916 if (is_ip6 != r->is_ipv6)
920 if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
922 if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
926 if (proto != r->proto)
929 (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
933 (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
936 /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
937 if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
940 /* everything matches! */
941 *r_action = r->is_permit;
944 *r_acl_match_p = acl_index;
953 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
954 u32 * acl_match_p, u32 * rule_match_p,
957 acl_main_t *am = &acl_main;
961 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
962 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
966 (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
967 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
971 *nextp = am->acl_in_ip6_match_next[action];
975 *nextp = am->acl_in_ip4_match_next[action];
980 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
982 /* If there are ACLs and none matched, deny by default */
989 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
990 u32 * acl_match_p, u32 * rule_match_p,
993 acl_main_t *am = &acl_main;
997 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
998 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1001 if (acl_packet_match
1002 (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
1003 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
1007 *nextp = am->acl_out_ip6_match_next[action];
1011 *nextp = am->acl_out_ip4_match_next[action];
1016 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1018 /* If there are ACLs and none matched, deny by default */
1030 u32 arp_table_index;
1031 } macip_match_type_t;
1034 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1040 for (i = 0; i < vec_len (mv); i++)
1042 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1043 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1053 /* Get metric used to sort match types.
1054 The more specific and the more often seen - the bigger the metric */
1056 match_type_metric (macip_match_type_t * m)
1058 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1059 return m->prefix_len + m->is_ipv6 + 10 * m->count;
1063 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1065 /* Ascending sort based on the metric values */
1066 return match_type_metric (m1) - match_type_metric (m2);
1069 /* Get the offset of L3 source within ethernet packet */
1071 get_l3_src_offset(int is6)
1074 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
1076 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
1080 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1082 macip_match_type_t *mvec = NULL;
1083 macip_match_type_t *mt;
1084 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1086 u32 match_type_index;
1089 vnet_classify_main_t *cm = &vnet_classify_main;
1091 /* Count the number of different types of rules */
1092 for (i = 0; i < a->count; i++)
1096 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1097 a->rules[i].src_prefixlen,
1098 a->rules[i].is_ipv6)))
1100 match_type_index = vec_len (mvec);
1101 vec_validate (mvec, match_type_index);
1102 memcpy (mvec[match_type_index].mac_mask,
1103 a->rules[match_type_index].src_mac_mask, 6);
1104 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1105 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1106 mvec[match_type_index].table_index = ~0;
1108 mvec[match_type_index].count++;
1110 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1111 vec_sort_with_function (mvec, match_type_compare);
1112 /* Create the classifier tables */
1114 /* First add ARP tables */
1115 vec_foreach (mt, mvec)
1118 int is6 = mt->is_ipv6;
1120 mt->arp_table_index = ~0;
1123 memset (mask, 0, sizeof (mask));
1124 memcpy (&mask[6], mt->mac_mask, 6);
1125 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1126 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1128 for (i = 0; i < (mt->prefix_len / 8); i++)
1129 mask[14 + 14 + i] = 0xff;
1130 if (mt->prefix_len % 8)
1131 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1133 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1134 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1135 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1136 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1138 last_table = mt->arp_table_index;
1141 /* Now add IP[46] tables */
1142 vec_foreach (mt, mvec)
1145 int is6 = mt->is_ipv6;
1146 int l3_src_offs = get_l3_src_offset(is6);
1147 memset (mask, 0, sizeof (mask));
1148 memcpy (&mask[6], mt->mac_mask, 6);
1149 for (i = 0; i < (mt->prefix_len / 8); i++)
1151 mask[l3_src_offs + i] = 0xff;
1153 if (mt->prefix_len % 8)
1155 mask[l3_src_offs + (mt->prefix_len / 8)] =
1156 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1159 * Round-up the number of bytes needed to store the prefix,
1160 * and round up the number of vectors too
1162 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1163 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1164 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1165 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1167 last_table = mt->table_index;
1169 a->ip4_table_index = ~0;
1170 a->ip6_table_index = ~0;
1171 a->l2_table_index = last_table;
1173 /* Populate the classifier tables with rules from the MACIP ACL */
1174 for (i = 0; i < a->count; i++)
1178 int is6 = a->rules[i].is_ipv6;
1179 int l3_src_offs = get_l3_src_offset(is6);
1180 memset (mask, 0, sizeof (mask));
1181 memcpy (&mask[6], a->rules[i].src_mac, 6);
1182 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1185 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1191 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1196 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1197 a->rules[i].src_prefixlen,
1198 a->rules[i].is_ipv6);
1199 /* add session to table mvec[match_type_index].table_index; */
1200 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1201 mask, a->rules[i].is_permit ? ~0 : 0, i,
1202 0, action, metadata, 1);
1203 /* add ARP table entry too */
1204 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1206 memset (mask, 0, sizeof (mask));
1207 memcpy (&mask[6], a->rules[i].src_mac, 6);
1210 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1211 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1212 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1213 mask, a->rules[i].is_permit ? ~0 : 0, i,
1214 0, action, metadata, 1);
1221 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1223 vnet_classify_main_t *cm = &vnet_classify_main;
1224 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1226 if (a->ip4_table_index != ~0)
1228 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1229 a->ip4_table_index = ~0;
1231 if (a->ip6_table_index != ~0)
1233 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1234 a->ip6_table_index = ~0;
1236 if (a->l2_table_index != ~0)
1238 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1239 a->l2_table_index = ~0;
1244 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1245 u32 * acl_list_index, u8 * tag)
1247 acl_main_t *am = &acl_main;
1248 macip_acl_list_t *a;
1249 macip_acl_rule_t *r;
1250 macip_acl_rule_t *acl_new_rules;
1253 /* Create and populate the rules */
1254 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1255 CLIB_CACHE_LINE_BYTES);
1258 /* Could not allocate rules. New or existing ACL - bail out regardless */
1262 for (i = 0; i < count; i++)
1264 r = &acl_new_rules[i];
1265 r->is_permit = rules[i].is_permit;
1266 r->is_ipv6 = rules[i].is_ipv6;
1267 memcpy (&r->src_mac, rules[i].src_mac, 6);
1268 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1269 if(rules[i].is_ipv6)
1270 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1272 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1273 r->src_prefixlen = rules[i].src_ip_prefix_len;
1277 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1278 memset (a, 0, sizeof (*a));
1279 /* Will return the newly allocated ACL index */
1280 *acl_list_index = a - am->macip_acls;
1282 a->rules = acl_new_rules;
1284 memcpy (a->tag, tag, sizeof (a->tag));
1286 /* Create and populate the classifer tables */
1287 macip_create_classify_tables (am, *acl_list_index);
1293 /* No check for validity of sw_if_index - the callers were supposed to validate */
1296 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1299 u32 macip_acl_index;
1300 macip_acl_list_t *a;
1301 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1302 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1303 /* No point in deleting MACIP ACL which is not applied */
1304 if (~0 == macip_acl_index)
1306 a = &am->macip_acls[macip_acl_index];
1307 /* remove the classifier tables off the interface L2 ACL */
1309 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1310 a->ip6_table_index, a->l2_table_index, 0);
1311 /* Unset the MACIP ACL index */
1312 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1316 /* No check for validity of sw_if_index - the callers were supposed to validate */
1319 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1320 u32 macip_acl_index)
1322 macip_acl_list_t *a;
1324 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1328 a = &am->macip_acls[macip_acl_index];
1329 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1330 /* If there already a MACIP ACL applied, unapply it */
1331 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1332 macip_acl_interface_del_acl(am, sw_if_index);
1333 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1334 /* Apply the classifier tables for L2 ACLs */
1336 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1337 a->ip6_table_index, a->l2_table_index, 1);
1342 macip_acl_del_list (u32 acl_list_index)
1344 acl_main_t *am = &acl_main;
1345 macip_acl_list_t *a;
1347 if (pool_is_free_index (am->macip_acls, acl_list_index))
1352 /* delete any references to the ACL */
1353 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1355 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1357 macip_acl_interface_del_acl (am, i);
1361 /* Now that classifier tables are detached, clean them up */
1362 macip_destroy_classify_tables (am, acl_list_index);
1364 /* now we can delete the ACL itself */
1365 a = &am->macip_acls[acl_list_index];
1368 clib_mem_free (a->rules);
1370 pool_put (am->macip_acls, a);
1376 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1379 acl_main_t *am = &acl_main;
1383 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1387 rv = macip_acl_interface_del_acl (am, sw_if_index);
1392 /* API message handler */
1394 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1396 vl_api_acl_add_replace_reply_t *rmp;
1397 acl_main_t *am = &acl_main;
1399 u32 acl_list_index = ntohl (mp->acl_index);
1401 rv = acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1404 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1406 rmp->acl_index = htonl(acl_list_index);
1412 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1414 acl_main_t *sm = &acl_main;
1415 vl_api_acl_del_reply_t *rmp;
1418 rv = acl_del_list (ntohl (mp->acl_index));
1420 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1424 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1426 acl_main_t *sm = &acl_main;
1427 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1428 u32 sw_if_index = ntohl (mp->sw_if_index);
1429 vl_api_acl_interface_add_del_reply_t *rmp;
1432 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1433 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1436 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1437 mp->is_input, ntohl (mp->acl_index));
1439 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1443 vl_api_acl_interface_set_acl_list_t_handler
1444 (vl_api_acl_interface_set_acl_list_t * mp)
1446 acl_main_t *sm = &acl_main;
1447 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1450 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1451 u32 sw_if_index = ntohl (mp->sw_if_index);
1453 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1454 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1457 acl_interface_reset_inout_acls (sw_if_index, 0);
1458 acl_interface_reset_inout_acls (sw_if_index, 1);
1460 for (i = 0; i < mp->count; i++)
1462 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1463 ntohl (mp->acls[i]));
1467 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1471 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1473 api_rule->is_permit = r->is_permit;
1474 api_rule->is_ipv6 = r->is_ipv6;
1477 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1478 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1482 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1483 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1485 api_rule->src_ip_prefix_len = r->src_prefixlen;
1486 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1487 api_rule->proto = r->proto;
1488 api_rule->srcport_or_icmptype_first = r->src_port_or_type_first;
1489 api_rule->srcport_or_icmptype_last = r->src_port_or_type_last;
1490 api_rule->dstport_or_icmpcode_first = r->dst_port_or_code_first;
1491 api_rule->dstport_or_icmpcode_last = r->dst_port_or_code_last;
1492 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1493 api_rule->tcp_flags_value = r->tcp_flags_value;
1497 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1498 acl_list_t * acl, u32 context)
1500 vl_api_acl_details_t *mp;
1501 vl_api_acl_rule_t *rules;
1503 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1505 mp = vl_msg_api_alloc (msg_size);
1506 memset (mp, 0, msg_size);
1507 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1509 /* fill in the message */
1510 mp->context = context;
1511 mp->count = htonl (acl->count);
1512 mp->acl_index = htonl (acl - am->acls);
1513 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1514 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1516 for (i = 0; i < acl->count; i++)
1518 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1521 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1522 vl_msg_api_send_shmem (q, (u8 *) & mp);
1527 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1529 acl_main_t *am = &acl_main;
1534 unix_shared_memory_queue_t *q;
1536 q = vl_api_client_index_to_input_queue (mp->client_index);
1542 if (mp->acl_index == ~0)
1545 /* Just dump all ACLs */
1546 pool_foreach (acl, am->acls,
1548 send_acl_details(am, q, acl, mp->context);
1554 acl_index = ntohl (mp->acl_index);
1555 if (!pool_is_free_index (am->acls, acl_index))
1557 acl = &am->acls[acl_index];
1558 send_acl_details (am, q, acl, mp->context);
1564 /* FIXME API: should we signal an error here at all ? */
1570 send_acl_interface_list_details (acl_main_t * am,
1571 unix_shared_memory_queue_t * q,
1572 u32 sw_if_index, u32 context)
1574 vl_api_acl_interface_list_details_t *mp;
1581 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1582 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1584 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1585 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1586 count = n_input + n_output;
1588 msg_size = sizeof (*mp);
1589 msg_size += sizeof (mp->acls[0]) * count;
1591 mp = vl_msg_api_alloc (msg_size);
1592 memset (mp, 0, msg_size);
1594 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1596 /* fill in the message */
1597 mp->context = context;
1598 mp->sw_if_index = htonl (sw_if_index);
1600 mp->n_input = n_input;
1601 for (i = 0; i < n_input; i++)
1603 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1605 for (i = 0; i < n_output; i++)
1607 mp->acls[n_input + i] =
1608 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1611 vl_msg_api_send_shmem (q, (u8 *) & mp);
1615 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1618 acl_main_t *am = &acl_main;
1619 vnet_sw_interface_t *swif;
1620 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1623 unix_shared_memory_queue_t *q;
1625 q = vl_api_client_index_to_input_queue (mp->client_index);
1631 if (mp->sw_if_index == ~0)
1634 pool_foreach (swif, im->sw_interfaces,
1636 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1642 sw_if_index = ntohl (mp->sw_if_index);
1643 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1644 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1648 /* MACIP ACL API handlers */
1651 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1653 vl_api_macip_acl_add_reply_t *rmp;
1654 acl_main_t *am = &acl_main;
1656 u32 acl_list_index = ~0;
1659 macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1662 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1664 rmp->acl_index = htonl(acl_list_index);
1670 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1672 acl_main_t *sm = &acl_main;
1673 vl_api_macip_acl_del_reply_t *rmp;
1676 rv = macip_acl_del_list (ntohl (mp->acl_index));
1678 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1682 vl_api_macip_acl_interface_add_del_t_handler
1683 (vl_api_macip_acl_interface_add_del_t * mp)
1685 acl_main_t *sm = &acl_main;
1686 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1688 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1689 u32 sw_if_index = ntohl (mp->sw_if_index);
1691 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1692 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1695 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1696 ntohl (mp->acl_index));
1698 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1702 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1703 macip_acl_list_t * acl, u32 context)
1705 vl_api_macip_acl_details_t *mp;
1706 vl_api_macip_acl_rule_t *rules;
1707 macip_acl_rule_t *r;
1709 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1711 mp = vl_msg_api_alloc (msg_size);
1712 memset (mp, 0, msg_size);
1713 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1715 /* fill in the message */
1716 mp->context = context;
1719 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1720 mp->count = htonl (acl->count);
1721 mp->acl_index = htonl (acl - am->macip_acls);
1723 for (i = 0; i < acl->count; i++)
1726 rules[i].is_permit = r->is_permit;
1727 rules[i].is_ipv6 = r->is_ipv6;
1728 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1729 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1730 sizeof (r->src_mac_mask));
1732 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1733 sizeof (r->src_ip_addr.ip6));
1735 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1736 sizeof (r->src_ip_addr.ip4));
1737 rules[i].src_ip_prefix_len = r->src_prefixlen;
1742 /* No martini, no party - no ACL applied to this interface. */
1747 vl_msg_api_send_shmem (q, (u8 *) & mp);
1752 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1754 acl_main_t *am = &acl_main;
1755 macip_acl_list_t *acl;
1757 unix_shared_memory_queue_t *q;
1759 q = vl_api_client_index_to_input_queue (mp->client_index);
1765 if (mp->acl_index == ~0)
1767 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1768 pool_foreach (acl, am->macip_acls, (
1770 send_macip_acl_details (am, q, acl,
1778 u32 acl_index = ntohl (mp->acl_index);
1779 if (!pool_is_free_index (am->macip_acls, acl_index))
1781 acl = &am->macip_acls[acl_index];
1782 send_macip_acl_details (am, q, acl, mp->context);
1788 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1791 acl_main_t *am = &acl_main;
1792 vl_api_macip_acl_interface_get_reply_t *rmp;
1793 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1794 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1795 unix_shared_memory_queue_t *q;
1798 q = vl_api_client_index_to_input_queue (mp->client_index);
1804 rmp = vl_msg_api_alloc (msg_size);
1805 memset (rmp, 0, msg_size);
1807 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1808 rmp->context = mp->context;
1809 rmp->count = htonl (count);
1810 for (i = 0; i < count; i++)
1812 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1815 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1820 /* Set up the API message handling tables */
1821 static clib_error_t *
1822 acl_plugin_api_hookup (vlib_main_t * vm)
1824 acl_main_t *sm = &acl_main;
1826 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1828 vl_api_##n##_t_handler, \
1830 vl_api_##n##_t_endian, \
1831 vl_api_##n##_t_print, \
1832 sizeof(vl_api_##n##_t), 1);
1833 foreach_acl_plugin_api_msg;
1839 #define vl_msg_name_crc_list
1840 #include <acl/acl_all_api_h.h>
1841 #undef vl_msg_name_crc_list
1844 setup_message_id_table (acl_main_t * sm, api_main_t * am)
1846 #define _(id,n,crc) \
1847 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1848 foreach_vl_msg_name_crc_acl;
1853 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1854 u32 next_out_ip4, u32 next_out_ip6)
1856 acl_main_t *am = &acl_main;
1857 u32 act = am->n_match_actions;
1858 if (am->n_match_actions == 255)
1862 am->n_match_actions++;
1863 am->acl_in_ip4_match_next[act] = next_in_ip4;
1864 am->acl_in_ip6_match_next[act] = next_in_ip6;
1865 am->acl_out_ip4_match_next[act] = next_out_ip4;
1866 am->acl_out_ip6_match_next[act] = next_out_ip6;
1871 acl_setup_nodes (void)
1873 vlib_main_t *vm = vlib_get_main ();
1874 acl_main_t *am = &acl_main;
1877 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1878 am->l2_input_classify_next_acl =
1879 vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1880 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1881 am->l2_output_classify_next_acl =
1882 vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1884 feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1885 l2input_get_feat_names (),
1886 am->acl_in_node_feat_next_node_index);
1888 feat_bitmap_init_next_nodes (vm, acl_out_node.index, L2OUTPUT_N_FEAT,
1889 l2output_get_feat_names (),
1890 am->acl_out_node_feat_next_node_index);
1892 memset (&am->acl_in_ip4_match_next[0], 0,
1893 sizeof (am->acl_in_ip4_match_next));
1894 memset (&am->acl_in_ip6_match_next[0], 0,
1895 sizeof (am->acl_in_ip6_match_next));
1896 memset (&am->acl_out_ip4_match_next[0], 0,
1897 sizeof (am->acl_out_ip4_match_next));
1898 memset (&am->acl_out_ip6_match_next[0], 0,
1899 sizeof (am->acl_out_ip6_match_next));
1900 am->n_match_actions = 0;
1902 register_match_action_nexts (0, 0, 0, 0); /* drop */
1903 register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1904 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 */
1909 static clib_error_t *
1910 acl_init (vlib_main_t * vm)
1912 acl_main_t *am = &acl_main;
1913 clib_error_t *error = 0;
1914 memset (am, 0, sizeof (*am));
1916 am->vnet_main = vnet_get_main ();
1918 u8 *name = format (0, "acl_%08x%c", api_version, 0);
1920 /* Ask for a correctly-sized block of API message decode slots */
1921 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1922 VL_MSG_FIRST_AVAILABLE);
1924 error = acl_plugin_api_hookup (vm);
1927 /* Add our API messages to the global name_crc hash table */
1928 setup_message_id_table (am, &api_main);
1935 VLIB_INIT_FUNCTION (acl_init);