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 = ntohs ( rules[i].srcport_or_icmptype_first );
212 r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
213 r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
214 r->dst_port_or_code_last = ntohs ( 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 = ((u16) (*(u8 *) get_ptr_to_offset (b0, 34)));
871 dst_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 35)));
874 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 34)));
875 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 36)));
876 /* UDP gets ability to check on an oddball data byte as a bonus */
877 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
880 else /* is_ipv6 implicitly */
882 clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
883 clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
884 proto = acl_get_l4_proto (b0, 1);
887 *trace_bitmap |= 0x00000002;
889 src_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 54));
891 dst_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 55));
896 src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 54)));
897 dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 56)));
898 tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
901 if (pool_is_free_index (am->acls, acl_index))
904 *r_acl_match_p = acl_index;
906 *r_rule_match_p = -1;
907 /* the ACL does not exist but is used for policy. Block traffic. */
910 a = am->acls + acl_index;
911 for (i = 0; i < a->count; i++)
914 if (is_ip6 != r->is_ipv6)
918 if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
920 if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
924 if (proto != r->proto)
927 (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
931 (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
934 /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
935 if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
938 /* everything matches! */
939 *r_action = r->is_permit;
942 *r_acl_match_p = acl_index;
951 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
952 u32 * acl_match_p, u32 * rule_match_p,
955 acl_main_t *am = &acl_main;
959 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
960 for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
964 (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
965 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
969 *nextp = am->acl_in_ip6_match_next[action];
973 *nextp = am->acl_in_ip4_match_next[action];
978 if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
980 /* If there are ACLs and none matched, deny by default */
987 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
988 u32 * acl_match_p, u32 * rule_match_p,
991 acl_main_t *am = &acl_main;
995 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
996 for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1000 (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
1001 &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
1005 *nextp = am->acl_out_ip6_match_next[action];
1009 *nextp = am->acl_out_ip4_match_next[action];
1014 if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1016 /* If there are ACLs and none matched, deny by default */
1028 u32 arp_table_index;
1029 } macip_match_type_t;
1032 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1038 for (i = 0; i < vec_len (mv); i++)
1040 if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1041 && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1051 /* Get metric used to sort match types.
1052 The more specific and the more often seen - the bigger the metric */
1054 match_type_metric (macip_match_type_t * m)
1056 /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1057 return m->prefix_len + m->is_ipv6 + 10 * m->count;
1061 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1063 /* Ascending sort based on the metric values */
1064 return match_type_metric (m1) - match_type_metric (m2);
1067 /* Get the offset of L3 source within ethernet packet */
1069 get_l3_src_offset(int is6)
1072 return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
1074 return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
1078 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1080 macip_match_type_t *mvec = NULL;
1081 macip_match_type_t *mt;
1082 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1084 u32 match_type_index;
1087 vnet_classify_main_t *cm = &vnet_classify_main;
1089 /* Count the number of different types of rules */
1090 for (i = 0; i < a->count; i++)
1094 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1095 a->rules[i].src_prefixlen,
1096 a->rules[i].is_ipv6)))
1098 match_type_index = vec_len (mvec);
1099 vec_validate (mvec, match_type_index);
1100 memcpy (mvec[match_type_index].mac_mask,
1101 a->rules[match_type_index].src_mac_mask, 6);
1102 mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1103 mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1104 mvec[match_type_index].table_index = ~0;
1106 mvec[match_type_index].count++;
1108 /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1109 vec_sort_with_function (mvec, match_type_compare);
1110 /* Create the classifier tables */
1112 /* First add ARP tables */
1113 vec_foreach (mt, mvec)
1116 int is6 = mt->is_ipv6;
1118 mt->arp_table_index = ~0;
1121 memset (mask, 0, sizeof (mask));
1122 memcpy (&mask[6], mt->mac_mask, 6);
1123 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1124 memcpy (&mask[14 + 8], mt->mac_mask, 6);
1126 for (i = 0; i < (mt->prefix_len / 8); i++)
1127 mask[14 + 14 + i] = 0xff;
1128 if (mt->prefix_len % 8)
1129 mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1131 mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1132 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1133 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1134 (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1136 last_table = mt->arp_table_index;
1139 /* Now add IP[46] tables */
1140 vec_foreach (mt, mvec)
1143 int is6 = mt->is_ipv6;
1144 int l3_src_offs = get_l3_src_offset(is6);
1145 memset (mask, 0, sizeof (mask));
1146 memcpy (&mask[6], mt->mac_mask, 6);
1147 for (i = 0; i < (mt->prefix_len / 8); i++)
1149 mask[l3_src_offs + i] = 0xff;
1151 if (mt->prefix_len % 8)
1153 mask[l3_src_offs + (mt->prefix_len / 8)] =
1154 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1157 * Round-up the number of bytes needed to store the prefix,
1158 * and round up the number of vectors too
1160 mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1161 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1162 acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1163 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1165 last_table = mt->table_index;
1167 a->ip4_table_index = ~0;
1168 a->ip6_table_index = ~0;
1169 a->l2_table_index = last_table;
1171 /* Populate the classifier tables with rules from the MACIP ACL */
1172 for (i = 0; i < a->count; i++)
1176 int is6 = a->rules[i].is_ipv6;
1177 int l3_src_offs = get_l3_src_offset(is6);
1178 memset (mask, 0, sizeof (mask));
1179 memcpy (&mask[6], a->rules[i].src_mac, 6);
1180 memset (&mask[12], 0xff, 2); /* ethernet protocol */
1183 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1189 memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1194 macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1195 a->rules[i].src_prefixlen,
1196 a->rules[i].is_ipv6);
1197 /* add session to table mvec[match_type_index].table_index; */
1198 vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1199 mask, a->rules[i].is_permit ? ~0 : 0, i,
1200 0, action, metadata, 1);
1201 /* add ARP table entry too */
1202 if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1204 memset (mask, 0, sizeof (mask));
1205 memcpy (&mask[6], a->rules[i].src_mac, 6);
1208 memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1209 memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1210 vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1211 mask, a->rules[i].is_permit ? ~0 : 0, i,
1212 0, action, metadata, 1);
1219 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1221 vnet_classify_main_t *cm = &vnet_classify_main;
1222 macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1224 if (a->ip4_table_index != ~0)
1226 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1227 a->ip4_table_index = ~0;
1229 if (a->ip6_table_index != ~0)
1231 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1232 a->ip6_table_index = ~0;
1234 if (a->l2_table_index != ~0)
1236 acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1237 a->l2_table_index = ~0;
1242 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1243 u32 * acl_list_index, u8 * tag)
1245 acl_main_t *am = &acl_main;
1246 macip_acl_list_t *a;
1247 macip_acl_rule_t *r;
1248 macip_acl_rule_t *acl_new_rules;
1251 /* Create and populate the rules */
1252 acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1253 CLIB_CACHE_LINE_BYTES);
1256 /* Could not allocate rules. New or existing ACL - bail out regardless */
1260 for (i = 0; i < count; i++)
1262 r = &acl_new_rules[i];
1263 r->is_permit = rules[i].is_permit;
1264 r->is_ipv6 = rules[i].is_ipv6;
1265 memcpy (&r->src_mac, rules[i].src_mac, 6);
1266 memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1267 if(rules[i].is_ipv6)
1268 memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1270 memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1271 r->src_prefixlen = rules[i].src_ip_prefix_len;
1275 pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1276 memset (a, 0, sizeof (*a));
1277 /* Will return the newly allocated ACL index */
1278 *acl_list_index = a - am->macip_acls;
1280 a->rules = acl_new_rules;
1282 memcpy (a->tag, tag, sizeof (a->tag));
1284 /* Create and populate the classifer tables */
1285 macip_create_classify_tables (am, *acl_list_index);
1291 /* No check for validity of sw_if_index - the callers were supposed to validate */
1294 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1297 u32 macip_acl_index;
1298 macip_acl_list_t *a;
1299 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1300 macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1301 /* No point in deleting MACIP ACL which is not applied */
1302 if (~0 == macip_acl_index)
1304 a = &am->macip_acls[macip_acl_index];
1305 /* remove the classifier tables off the interface L2 ACL */
1307 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1308 a->ip6_table_index, a->l2_table_index, 0);
1309 /* Unset the MACIP ACL index */
1310 am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1314 /* No check for validity of sw_if_index - the callers were supposed to validate */
1317 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1318 u32 macip_acl_index)
1320 macip_acl_list_t *a;
1322 if (pool_is_free_index (am->macip_acls, macip_acl_index))
1326 a = &am->macip_acls[macip_acl_index];
1327 vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1328 /* If there already a MACIP ACL applied, unapply it */
1329 if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1330 macip_acl_interface_del_acl(am, sw_if_index);
1331 am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1332 /* Apply the classifier tables for L2 ACLs */
1334 vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1335 a->ip6_table_index, a->l2_table_index, 1);
1340 macip_acl_del_list (u32 acl_list_index)
1342 acl_main_t *am = &acl_main;
1343 macip_acl_list_t *a;
1345 if (pool_is_free_index (am->macip_acls, acl_list_index))
1350 /* delete any references to the ACL */
1351 for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1353 if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1355 macip_acl_interface_del_acl (am, i);
1359 /* Now that classifier tables are detached, clean them up */
1360 macip_destroy_classify_tables (am, acl_list_index);
1362 /* now we can delete the ACL itself */
1363 a = &am->macip_acls[acl_list_index];
1366 clib_mem_free (a->rules);
1368 pool_put (am->macip_acls, a);
1374 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1377 acl_main_t *am = &acl_main;
1381 rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1385 rv = macip_acl_interface_del_acl (am, sw_if_index);
1390 /* API message handler */
1392 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1394 vl_api_acl_add_replace_reply_t *rmp;
1395 acl_main_t *am = &acl_main;
1397 u32 acl_list_index = ntohl (mp->acl_index);
1399 rv = acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1402 REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1404 rmp->acl_index = htonl(acl_list_index);
1410 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1412 acl_main_t *sm = &acl_main;
1413 vl_api_acl_del_reply_t *rmp;
1416 rv = acl_del_list (ntohl (mp->acl_index));
1418 REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1422 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1424 acl_main_t *sm = &acl_main;
1425 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1426 u32 sw_if_index = ntohl (mp->sw_if_index);
1427 vl_api_acl_interface_add_del_reply_t *rmp;
1430 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1431 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1434 acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1435 mp->is_input, ntohl (mp->acl_index));
1437 REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1441 vl_api_acl_interface_set_acl_list_t_handler
1442 (vl_api_acl_interface_set_acl_list_t * mp)
1444 acl_main_t *sm = &acl_main;
1445 vl_api_acl_interface_set_acl_list_reply_t *rmp;
1448 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1449 u32 sw_if_index = ntohl (mp->sw_if_index);
1451 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1452 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1455 acl_interface_reset_inout_acls (sw_if_index, 0);
1456 acl_interface_reset_inout_acls (sw_if_index, 1);
1458 for (i = 0; i < mp->count; i++)
1460 acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1461 ntohl (mp->acls[i]));
1465 REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1469 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1471 api_rule->is_permit = r->is_permit;
1472 api_rule->is_ipv6 = r->is_ipv6;
1475 memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1476 memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1480 memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1481 memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1483 api_rule->src_ip_prefix_len = r->src_prefixlen;
1484 api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1485 api_rule->proto = r->proto;
1486 api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1487 api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1488 api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1489 api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1490 api_rule->tcp_flags_mask = r->tcp_flags_mask;
1491 api_rule->tcp_flags_value = r->tcp_flags_value;
1495 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1496 acl_list_t * acl, u32 context)
1498 vl_api_acl_details_t *mp;
1499 vl_api_acl_rule_t *rules;
1501 int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1503 mp = vl_msg_api_alloc (msg_size);
1504 memset (mp, 0, msg_size);
1505 mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1507 /* fill in the message */
1508 mp->context = context;
1509 mp->count = htonl (acl->count);
1510 mp->acl_index = htonl (acl - am->acls);
1511 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1512 // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1514 for (i = 0; i < acl->count; i++)
1516 copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1519 clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1520 vl_msg_api_send_shmem (q, (u8 *) & mp);
1525 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1527 acl_main_t *am = &acl_main;
1532 unix_shared_memory_queue_t *q;
1534 q = vl_api_client_index_to_input_queue (mp->client_index);
1540 if (mp->acl_index == ~0)
1543 /* Just dump all ACLs */
1544 pool_foreach (acl, am->acls,
1546 send_acl_details(am, q, acl, mp->context);
1552 acl_index = ntohl (mp->acl_index);
1553 if (!pool_is_free_index (am->acls, acl_index))
1555 acl = &am->acls[acl_index];
1556 send_acl_details (am, q, acl, mp->context);
1562 /* FIXME API: should we signal an error here at all ? */
1568 send_acl_interface_list_details (acl_main_t * am,
1569 unix_shared_memory_queue_t * q,
1570 u32 sw_if_index, u32 context)
1572 vl_api_acl_interface_list_details_t *mp;
1579 vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1580 vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1582 n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1583 n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1584 count = n_input + n_output;
1586 msg_size = sizeof (*mp);
1587 msg_size += sizeof (mp->acls[0]) * count;
1589 mp = vl_msg_api_alloc (msg_size);
1590 memset (mp, 0, msg_size);
1592 ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1594 /* fill in the message */
1595 mp->context = context;
1596 mp->sw_if_index = htonl (sw_if_index);
1598 mp->n_input = n_input;
1599 for (i = 0; i < n_input; i++)
1601 mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1603 for (i = 0; i < n_output; i++)
1605 mp->acls[n_input + i] =
1606 htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1609 vl_msg_api_send_shmem (q, (u8 *) & mp);
1613 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1616 acl_main_t *am = &acl_main;
1617 vnet_sw_interface_t *swif;
1618 vnet_interface_main_t *im = &am->vnet_main->interface_main;
1621 unix_shared_memory_queue_t *q;
1623 q = vl_api_client_index_to_input_queue (mp->client_index);
1629 if (mp->sw_if_index == ~0)
1632 pool_foreach (swif, im->sw_interfaces,
1634 send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1640 sw_if_index = ntohl (mp->sw_if_index);
1641 if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1642 send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1646 /* MACIP ACL API handlers */
1649 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1651 vl_api_macip_acl_add_reply_t *rmp;
1652 acl_main_t *am = &acl_main;
1654 u32 acl_list_index = ~0;
1657 macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1660 REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1662 rmp->acl_index = htonl(acl_list_index);
1668 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1670 acl_main_t *sm = &acl_main;
1671 vl_api_macip_acl_del_reply_t *rmp;
1674 rv = macip_acl_del_list (ntohl (mp->acl_index));
1676 REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1680 vl_api_macip_acl_interface_add_del_t_handler
1681 (vl_api_macip_acl_interface_add_del_t * mp)
1683 acl_main_t *sm = &acl_main;
1684 vl_api_macip_acl_interface_add_del_reply_t *rmp;
1686 vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1687 u32 sw_if_index = ntohl (mp->sw_if_index);
1689 if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1690 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1693 macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1694 ntohl (mp->acl_index));
1696 REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1700 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1701 macip_acl_list_t * acl, u32 context)
1703 vl_api_macip_acl_details_t *mp;
1704 vl_api_macip_acl_rule_t *rules;
1705 macip_acl_rule_t *r;
1707 int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1709 mp = vl_msg_api_alloc (msg_size);
1710 memset (mp, 0, msg_size);
1711 mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1713 /* fill in the message */
1714 mp->context = context;
1717 memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1718 mp->count = htonl (acl->count);
1719 mp->acl_index = htonl (acl - am->macip_acls);
1721 for (i = 0; i < acl->count; i++)
1724 rules[i].is_permit = r->is_permit;
1725 rules[i].is_ipv6 = r->is_ipv6;
1726 memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1727 memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1728 sizeof (r->src_mac_mask));
1730 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1731 sizeof (r->src_ip_addr.ip6));
1733 memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1734 sizeof (r->src_ip_addr.ip4));
1735 rules[i].src_ip_prefix_len = r->src_prefixlen;
1740 /* No martini, no party - no ACL applied to this interface. */
1745 vl_msg_api_send_shmem (q, (u8 *) & mp);
1750 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1752 acl_main_t *am = &acl_main;
1753 macip_acl_list_t *acl;
1755 unix_shared_memory_queue_t *q;
1757 q = vl_api_client_index_to_input_queue (mp->client_index);
1763 if (mp->acl_index == ~0)
1765 /* Just dump all ACLs for now, with sw_if_index = ~0 */
1766 pool_foreach (acl, am->macip_acls, (
1768 send_macip_acl_details (am, q, acl,
1776 u32 acl_index = ntohl (mp->acl_index);
1777 if (!pool_is_free_index (am->macip_acls, acl_index))
1779 acl = &am->macip_acls[acl_index];
1780 send_macip_acl_details (am, q, acl, mp->context);
1786 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1789 acl_main_t *am = &acl_main;
1790 vl_api_macip_acl_interface_get_reply_t *rmp;
1791 u32 count = vec_len (am->macip_acl_by_sw_if_index);
1792 int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1793 unix_shared_memory_queue_t *q;
1796 q = vl_api_client_index_to_input_queue (mp->client_index);
1802 rmp = vl_msg_api_alloc (msg_size);
1803 memset (rmp, 0, msg_size);
1805 ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1806 rmp->context = mp->context;
1807 rmp->count = htonl (count);
1808 for (i = 0; i < count; i++)
1810 rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1813 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1818 /* Set up the API message handling tables */
1819 static clib_error_t *
1820 acl_plugin_api_hookup (vlib_main_t * vm)
1822 acl_main_t *sm = &acl_main;
1824 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1826 vl_api_##n##_t_handler, \
1828 vl_api_##n##_t_endian, \
1829 vl_api_##n##_t_print, \
1830 sizeof(vl_api_##n##_t), 1);
1831 foreach_acl_plugin_api_msg;
1837 #define vl_msg_name_crc_list
1838 #include <acl/acl_all_api_h.h>
1839 #undef vl_msg_name_crc_list
1842 setup_message_id_table (acl_main_t * sm, api_main_t * am)
1844 #define _(id,n,crc) \
1845 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1846 foreach_vl_msg_name_crc_acl;
1851 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1852 u32 next_out_ip4, u32 next_out_ip6)
1854 acl_main_t *am = &acl_main;
1855 u32 act = am->n_match_actions;
1856 if (am->n_match_actions == 255)
1860 am->n_match_actions++;
1861 am->acl_in_ip4_match_next[act] = next_in_ip4;
1862 am->acl_in_ip6_match_next[act] = next_in_ip6;
1863 am->acl_out_ip4_match_next[act] = next_out_ip4;
1864 am->acl_out_ip6_match_next[act] = next_out_ip6;
1869 acl_setup_nodes (void)
1871 vlib_main_t *vm = vlib_get_main ();
1872 acl_main_t *am = &acl_main;
1875 n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1876 am->l2_input_classify_next_acl =
1877 vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1878 n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1879 am->l2_output_classify_next_acl =
1880 vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1882 feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1883 l2input_get_feat_names (),
1884 am->acl_in_node_feat_next_node_index);
1886 feat_bitmap_init_next_nodes (vm, acl_out_node.index, L2OUTPUT_N_FEAT,
1887 l2output_get_feat_names (),
1888 am->acl_out_node_feat_next_node_index);
1890 memset (&am->acl_in_ip4_match_next[0], 0,
1891 sizeof (am->acl_in_ip4_match_next));
1892 memset (&am->acl_in_ip6_match_next[0], 0,
1893 sizeof (am->acl_in_ip6_match_next));
1894 memset (&am->acl_out_ip4_match_next[0], 0,
1895 sizeof (am->acl_out_ip4_match_next));
1896 memset (&am->acl_out_ip6_match_next[0], 0,
1897 sizeof (am->acl_out_ip6_match_next));
1898 am->n_match_actions = 0;
1900 register_match_action_nexts (0, 0, 0, 0); /* drop */
1901 register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1902 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 */
1907 static clib_error_t *
1908 acl_init (vlib_main_t * vm)
1910 acl_main_t *am = &acl_main;
1911 clib_error_t *error = 0;
1912 memset (am, 0, sizeof (*am));
1914 am->vnet_main = vnet_get_main ();
1916 u8 *name = format (0, "acl_%08x%c", api_version, 0);
1918 /* Ask for a correctly-sized block of API message decode slots */
1919 am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1920 VL_MSG_FIRST_AVAILABLE);
1922 error = acl_plugin_api_hookup (vm);
1925 /* Add our API messages to the global name_crc hash table */
1926 setup_message_id_table (am, &api_main);
1933 VLIB_INIT_FUNCTION (acl_init);