acl-plugin: defer the ACL plugin user module registration with ACL lookup until it...
[vpp.git] / src / plugins / acl / acl.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <stddef.h>
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <acl/acl.h>
21
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/in_out_acl.h>
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 /* define message IDs */
30 #include <acl/acl_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <acl/acl_all_api_h.h>
35 #undef vl_typedefs
36
37 /* define generated endian-swappers */
38 #define vl_endianfun
39 #include <acl/acl_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <acl/acl_all_api_h.h>
46 #undef vl_printfun
47
48 /* Get the API version number */
49 #define vl_api_version(n,v) static u32 api_version=(v);
50 #include <acl/acl_all_api_h.h>
51 #undef vl_api_version
52
53 #include "fa_node.h"
54 #include "public_inlines.h"
55
56 acl_main_t acl_main;
57
58 #define REPLY_MSG_ID_BASE am->msg_id_base
59 #include <vlibapi/api_helper_macros.h>
60
61 /* List of message types that this plugin understands */
62
63 #define foreach_acl_plugin_api_msg              \
64 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
65 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
66 _(ACL_ADD_REPLACE, acl_add_replace)                             \
67 _(ACL_DEL, acl_del)                             \
68 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
69 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list)       \
70 _(ACL_DUMP, acl_dump)  \
71 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
72 _(MACIP_ACL_ADD, macip_acl_add) \
73 _(MACIP_ACL_ADD_REPLACE, macip_acl_add_replace) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get) \
78 _(MACIP_ACL_INTERFACE_LIST_DUMP, macip_acl_interface_list_dump) \
79 _(ACL_INTERFACE_SET_ETYPE_WHITELIST, acl_interface_set_etype_whitelist) \
80 _(ACL_INTERFACE_ETYPE_WHITELIST_DUMP, acl_interface_etype_whitelist_dump)
81
82
83 /* *INDENT-OFF* */
84 VLIB_PLUGIN_REGISTER () = {
85     .version = VPP_BUILD_VER,
86     .description = "Access Control Lists",
87 };
88 /* *INDENT-ON* */
89
90 /* Format vec16. */
91 u8 *
92 format_vec16 (u8 * s, va_list * va)
93 {
94   u16 *v = va_arg (*va, u16 *);
95   char *fmt = va_arg (*va, char *);
96   uword i;
97   for (i = 0; i < vec_len (v); i++)
98     {
99       if (i > 0)
100         s = format (s, ", ");
101       s = format (s, fmt, v[i]);
102     }
103   return s;
104 }
105
106
107
108 u8
109 acl_plugin_acl_exists (u32 acl_index)
110 {
111   acl_main_t *am = &acl_main;
112
113   if (pool_is_free_index (am->acls, acl_index))
114     return 0;
115
116   return 1;
117 }
118
119 static void *
120 acl_set_heap (acl_main_t * am)
121 {
122   if (0 == am->acl_mheap)
123     {
124       am->acl_mheap = mheap_alloc (0 /* use VM */ , am->acl_mheap_size);
125       mheap_t *h = mheap_header (am->acl_mheap);
126       h->flags |= MHEAP_FLAG_THREAD_SAFE;
127     }
128   void *oldheap = clib_mem_set_heap (am->acl_mheap);
129   return oldheap;
130 }
131
132 void *
133 acl_plugin_set_heap ()
134 {
135   acl_main_t *am = &acl_main;
136   return acl_set_heap (am);
137 }
138
139 void
140 acl_plugin_acl_set_validate_heap (acl_main_t * am, int on)
141 {
142   clib_mem_set_heap (acl_set_heap (am));
143   mheap_t *h = mheap_header (am->acl_mheap);
144   if (on)
145     {
146       h->flags |= MHEAP_FLAG_VALIDATE;
147       h->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
148       mheap_validate (h);
149     }
150   else
151     {
152       h->flags &= ~MHEAP_FLAG_VALIDATE;
153       h->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
154     }
155 }
156
157 void
158 acl_plugin_acl_set_trace_heap (acl_main_t * am, int on)
159 {
160   clib_mem_set_heap (acl_set_heap (am));
161   mheap_t *h = mheap_header (am->acl_mheap);
162   if (on)
163     {
164       h->flags |= MHEAP_FLAG_TRACE;
165     }
166   else
167     {
168       h->flags &= ~MHEAP_FLAG_TRACE;
169     }
170 }
171
172 static void
173 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
174 {
175   acl_main_t *am = &acl_main;
176   vl_api_acl_plugin_get_version_reply_t *rmp;
177   int msg_size = sizeof (*rmp);
178   vl_api_registration_t *reg;
179
180   reg = vl_api_client_index_to_registration (mp->client_index);
181   if (!reg)
182     return;
183
184   rmp = vl_msg_api_alloc (msg_size);
185   memset (rmp, 0, msg_size);
186   rmp->_vl_msg_id =
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);
191
192   vl_api_send_msg (reg, (u8 *) rmp);
193 }
194
195 static void
196 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t *
197                                           mp)
198 {
199   vl_api_acl_plugin_control_ping_reply_t *rmp;
200   acl_main_t *am = &acl_main;
201   int rv = 0;
202
203   /* *INDENT-OFF* */
204   REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
205   ({
206     rmp->vpe_pid = ntohl (getpid ());
207   }));
208   /* *INDENT-ON* */
209 }
210
211 static void
212 print_clib_warning_and_reset (vlib_main_t * vm, u8 * out0)
213 {
214   clib_warning ("%v", out0);
215   vec_reset_length (out0);
216 }
217
218 static void
219 print_cli_and_reset (vlib_main_t * vm, u8 * out0)
220 {
221   vlib_cli_output (vm, "%v", out0);
222   vec_reset_length (out0);
223 }
224
225 typedef void (*acl_vector_print_func_t) (vlib_main_t * vm, u8 * out0);
226
227 static void
228 acl_print_acl_x (acl_vector_print_func_t vpr, vlib_main_t * vm,
229                  acl_main_t * am, int acl_index)
230 {
231   acl_rule_t *r;
232   u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
233                      am->acls[acl_index].count, am->acls[acl_index].tag);
234   int j;
235   vpr (vm, out0);
236   for (j = 0; j < am->acls[acl_index].count; j++)
237     {
238       r = &am->acls[acl_index].rules[j];
239       out0 = format (out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
240       out0 = format_acl_action (out0, r->is_permit);
241       out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
242                      r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
243                      r->src_prefixlen);
244       out0 =
245         format (out0, " dst %U/%d", format_ip46_address, &r->dst,
246                 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
247       out0 = format (out0, " proto %d", r->proto);
248       out0 = format (out0, " sport %d", r->src_port_or_type_first);
249       if (r->src_port_or_type_first != r->src_port_or_type_last)
250         {
251           out0 = format (out0, "-%d", r->src_port_or_type_last);
252         }
253       out0 = format (out0, " dport %d", r->dst_port_or_code_first);
254       if (r->dst_port_or_code_first != r->dst_port_or_code_last)
255         {
256           out0 = format (out0, "-%d", r->dst_port_or_code_last);
257         }
258       if (r->tcp_flags_mask || r->tcp_flags_value)
259         {
260           out0 =
261             format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
262                     r->tcp_flags_mask);
263         }
264       out0 = format (out0, "\n");
265       vpr (vm, out0);
266     }
267 }
268
269 static void
270 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
271 {
272   acl_print_acl_x (print_cli_and_reset, vm, am, acl_index);
273 }
274
275 static void
276 warning_acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
277 {
278   acl_print_acl_x (print_clib_warning_and_reset, vm, am, acl_index);
279 }
280
281
282 static int
283 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
284               u32 * acl_list_index, u8 * tag)
285 {
286   acl_main_t *am = &acl_main;
287   acl_list_t *a;
288   acl_rule_t *r;
289   acl_rule_t *acl_new_rules = 0;
290   int i;
291
292   if (am->trace_acl > 255)
293     clib_warning ("API dbg: acl_add_list index %d tag %s", *acl_list_index,
294                   tag);
295
296   if (*acl_list_index != ~0)
297     {
298       /* They supplied some number, let's see if this ACL exists */
299       if (pool_is_free_index (am->acls, *acl_list_index))
300         {
301           /* tried to replace a non-existent ACL, no point doing anything */
302           clib_warning
303             ("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)",
304              *acl_list_index, tag);
305           return VNET_API_ERROR_NO_SUCH_ENTRY;
306         }
307     }
308   if (0 == count)
309     {
310       clib_warning
311         ("acl-plugin-warning: supplied no rules for ACL %d (tag %s)",
312          *acl_list_index, tag);
313     }
314
315   void *oldheap = acl_set_heap (am);
316
317   /* Create and populate the rules */
318   if (count > 0)
319     vec_validate (acl_new_rules, count - 1);
320
321   for (i = 0; i < count; i++)
322     {
323       r = vec_elt_at_index (acl_new_rules, i);
324       memset (r, 0, sizeof (*r));
325       r->is_permit = rules[i].is_permit;
326       r->is_ipv6 = rules[i].is_ipv6;
327       if (r->is_ipv6)
328         {
329           memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
330           memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
331         }
332       else
333         {
334           memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
335           memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
336         }
337       r->src_prefixlen = rules[i].src_ip_prefix_len;
338       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
339       r->proto = rules[i].proto;
340       r->src_port_or_type_first = ntohs (rules[i].srcport_or_icmptype_first);
341       r->src_port_or_type_last = ntohs (rules[i].srcport_or_icmptype_last);
342       r->dst_port_or_code_first = ntohs (rules[i].dstport_or_icmpcode_first);
343       r->dst_port_or_code_last = ntohs (rules[i].dstport_or_icmpcode_last);
344       r->tcp_flags_value = rules[i].tcp_flags_value;
345       r->tcp_flags_mask = rules[i].tcp_flags_mask;
346     }
347
348   if (~0 == *acl_list_index)
349     {
350       /* Get ACL index */
351       pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
352       memset (a, 0, sizeof (*a));
353       /* Will return the newly allocated ACL index */
354       *acl_list_index = a - am->acls;
355     }
356   else
357     {
358       a = am->acls + *acl_list_index;
359       /* Get rid of the old rules */
360       if (a->rules)
361         vec_free (a->rules);
362     }
363   a->rules = acl_new_rules;
364   a->count = count;
365   memcpy (a->tag, tag, sizeof (a->tag));
366   if (am->trace_acl > 255)
367     warning_acl_print_acl (am->vlib_main, am, *acl_list_index);
368   /* notify the lookup contexts about the ACL changes */
369   acl_plugin_lookup_context_notify_acl_change (*acl_list_index);
370   clib_mem_set_heap (oldheap);
371   return 0;
372 }
373
374 static int
375 acl_is_used_by (u32 acl_index, u32 ** foo_index_vec_by_acl)
376 {
377   if (acl_index < vec_len (foo_index_vec_by_acl))
378     {
379       if (vec_len (vec_elt (foo_index_vec_by_acl, acl_index)) > 0)
380         {
381           /* ACL is applied somewhere. */
382           return 1;
383         }
384     }
385   return 0;
386 }
387
388 static int
389 acl_del_list (u32 acl_list_index)
390 {
391   acl_main_t *am = &acl_main;
392   acl_list_t *a;
393   if (pool_is_free_index (am->acls, acl_list_index))
394     {
395       return VNET_API_ERROR_NO_SUCH_ENTRY;
396     }
397   if (acl_is_used_by (acl_list_index, am->input_sw_if_index_vec_by_acl))
398     return VNET_API_ERROR_ACL_IN_USE_INBOUND;
399   if (acl_is_used_by (acl_list_index, am->output_sw_if_index_vec_by_acl))
400     return VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
401   /* lookup contexts cover other cases, not just inbound/oubound, so check that */
402   if (acl_is_used_by (acl_list_index, am->lc_index_vec_by_acl))
403     return VNET_API_ERROR_ACL_IN_USE_BY_LOOKUP_CONTEXT;
404
405   void *oldheap = acl_set_heap (am);
406
407   /* now we can delete the ACL itself */
408   a = pool_elt_at_index (am->acls, acl_list_index);
409   if (a->rules)
410     vec_free (a->rules);
411   pool_put (am->acls, a);
412   /* acl_list_index is now free, notify the lookup contexts */
413   acl_plugin_lookup_context_notify_acl_change (acl_list_index);
414   clib_mem_set_heap (oldheap);
415   return 0;
416 }
417
418 /* Some aids in ASCII graphing the content */
419 #define XX "\377"
420 #define __ "\000"
421 #define _(x)
422 #define v
423 /* *INDENT-OFF* */
424
425 u8 ip4_5tuple_mask[] =
426   _("             dmac               smac            etype ")
427   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
428   _("        v ihl totlen   ")
429   _(0x0000)
430   __ __ __ __
431   _("        ident fl+fo    ")
432   _(0x0004)
433   __ __ __ __
434   _("       ttl pr checksum ")
435   _(0x0008)
436   __ XX __ __
437   _("        src address    ")
438   _(0x000C)
439   XX XX XX XX
440   _("        dst address    ")
441   _(0x0010)
442   XX XX XX XX
443   _("L4 T/U  sport dport    ")
444   _(tcpudp)
445   XX XX XX XX
446   _(padpad)
447   __ __ __ __
448   _(padpad)
449   __ __ __ __
450   _(padeth)
451   __ __;
452
453  u8 ip6_5tuple_mask[] =
454   _("             dmac               smac            etype ")
455   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
456   _("        v  tc + flow ")
457   _(0x0000) __ __ __ __
458   _("        plen  nh hl  ")
459   _(0x0004) __ __ XX __
460   _("        src address  ")
461   _(0x0008) XX XX XX XX
462   _(0x000C) XX XX XX XX
463   _(0x0010) XX XX XX XX
464   _(0x0014) XX XX XX XX
465   _("        dst address  ")
466   _(0x0018) XX XX XX XX
467   _(0x001C) XX XX XX XX
468   _(0x0020) XX XX XX XX
469   _(0x0024) XX XX XX XX
470   _("L4T/U  sport dport   ")
471   _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
472
473  u8 dot1q_5tuple_mask[] =
474    _("             dmac               smac          dot1q         etype ")
475    _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ v XX XX v
476    _(padpad) __ __ __ __
477    _(padpad) __ __ __ __
478    _(padpad) __ __ __ __
479    _(padeth) __ __;
480
481  u8 dot1ad_5tuple_mask[] =
482    _("             dmac               smac          dot1ad      dot1q         etype ")
483    _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __ XX XX __ __ v XX XX v
484    _(padpad) __ __ __ __
485    _(padpad) __ __ __ __
486    _(padeth) __ __;
487
488  u8 ethertype_mask[] =
489    _("             dmac               smac          etype ")
490    _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v XX XX __ __;
491
492 /* *INDENT-ON* */
493 #undef XX
494 #undef __
495 #undef _
496 #undef v
497
498 static int
499 count_skip (u8 * p, u32 size)
500 {
501   u64 *p64 = (u64 *) p;
502   /* Be tolerant to null pointer */
503   if (0 == p)
504     return 0;
505
506   while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
507     {
508       p64++;
509     }
510   return (p64 - (u64 *) p) / 2;
511 }
512
513 static int
514 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
515                                  u32 mask_len, u32 next_table_index,
516                                  u32 miss_next_index, u32 * table_index,
517                                  int is_add)
518 {
519   u32 nbuckets = 1;
520   u32 memory_size = 2 << 13;
521   u32 skip = count_skip (mask, mask_len);
522   u32 match = (mask_len / 16) - skip;
523   u8 *skip_mask_ptr = mask + 16 * skip;
524   u32 current_data_flag = 0;
525   int current_data_offset = 0;
526
527   if (0 == match)
528     match = 1;
529   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
530   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
531                                          memory_size, skip, match,
532                                          next_table_index, miss_next_index,
533                                          table_index, current_data_flag,
534                                          current_data_offset, is_add,
535                                          1 /* delete_chain */ );
536   clib_mem_set_heap (oldheap);
537   return ret;
538 }
539
540 static int
541 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
542                                   u32 mask_len, u32 next_table_index,
543                                   u32 miss_next_index, u32 * table_index,
544                                   int is_add)
545 {
546   u32 nbuckets = 32;
547   u32 memory_size = 2 << 22;
548   u32 skip = count_skip (mask, mask_len);
549   u32 match = (mask_len / 16) - skip;
550   u8 *skip_mask_ptr = mask + 16 * skip;
551   u32 current_data_flag = 0;
552   int current_data_offset = 0;
553
554   if (0 == match)
555     match = 1;
556
557   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
558   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
559                                          memory_size, skip, match,
560                                          next_table_index, miss_next_index,
561                                          table_index, current_data_flag,
562                                          current_data_offset, is_add,
563                                          1 /* delete_chain */ );
564   clib_mem_set_heap (oldheap);
565   return ret;
566 }
567
568 static int
569 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
570 {
571   vnet_classify_main_t *cm = &vnet_classify_main;
572   u32 ip4_table_index = ~0;
573   u32 ip6_table_index = ~0;
574   u32 dot1q_table_index = ~0;
575   u32 dot1ad_table_index = ~0;
576   u32 etype_table_index = ~0;
577   void *oldheap = acl_set_heap (am);
578
579   vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
580                            sw_if_index, ~0);
581   vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
582                            sw_if_index, ~0);
583   vec_validate_init_empty (am->acl_dot1q_input_classify_table_by_sw_if_index,
584                            sw_if_index, ~0);
585   vec_validate_init_empty (am->acl_dot1ad_input_classify_table_by_sw_if_index,
586                            sw_if_index, ~0);
587   vec_validate_init_empty (am->acl_etype_input_classify_table_by_sw_if_index,
588                            sw_if_index, ~0);
589
590   /* switch to global heap while calling vnet_* functions */
591   clib_mem_set_heap (cm->vlib_main->heap_base);
592   vnet_l2_input_classify_enable_disable (sw_if_index, 0);
593
594   if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
595     {
596       ip4_table_index =
597         am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
598       am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
599       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
600                                        sizeof (ip4_5tuple_mask) - 1, ~0,
601                                        am->l2_input_classify_next_acl_ip4,
602                                        &ip4_table_index, 0);
603     }
604   if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
605     {
606       ip6_table_index =
607         am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
608       am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
609       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
610                                        sizeof (ip6_5tuple_mask) - 1, ~0,
611                                        am->l2_input_classify_next_acl_ip6,
612                                        &ip6_table_index, 0);
613     }
614   if (am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
615     {
616       dot1q_table_index =
617         am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index];
618       am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
619       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
620                                        sizeof (ip6_5tuple_mask) - 1, ~0,
621                                        ~0, &dot1q_table_index, 0);
622     }
623   if (am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
624     {
625       dot1ad_table_index =
626         am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index];
627       am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
628       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
629                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
630                                        ~0, &dot1ad_table_index, 0);
631     }
632   if (am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
633     {
634       etype_table_index =
635         am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index];
636       am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
637       acl_classify_add_del_table_tiny (cm, ethertype_mask,
638                                        sizeof (ethertype_mask) - 1, ~0,
639                                        ~0, &etype_table_index, 0);
640     }
641   clib_mem_set_heap (oldheap);
642   return 0;
643 }
644
645 static int
646 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
647 {
648   vnet_classify_main_t *cm = &vnet_classify_main;
649   u32 ip4_table_index = ~0;
650   u32 ip6_table_index = ~0;
651   u32 dot1q_table_index = ~0;
652   u32 dot1ad_table_index = ~0;
653   u32 etype_table_index = ~0;
654   void *oldheap = acl_set_heap (am);
655
656   vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
657                            sw_if_index, ~0);
658   vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
659                            sw_if_index, ~0);
660   vec_validate_init_empty (am->acl_dot1q_output_classify_table_by_sw_if_index,
661                            sw_if_index, ~0);
662   vec_validate_init_empty
663     (am->acl_dot1ad_output_classify_table_by_sw_if_index, sw_if_index, ~0);
664   vec_validate_init_empty (am->acl_etype_output_classify_table_by_sw_if_index,
665                            sw_if_index, ~0);
666
667   /* switch to global heap while calling vnet_* functions */
668   clib_mem_set_heap (cm->vlib_main->heap_base);
669
670   vnet_l2_output_classify_enable_disable (sw_if_index, 0);
671
672   if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
673     {
674       ip4_table_index =
675         am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
676       am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
677       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
678                                        sizeof (ip4_5tuple_mask) - 1, ~0,
679                                        am->l2_output_classify_next_acl_ip4,
680                                        &ip4_table_index, 0);
681     }
682   if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
683     {
684       ip6_table_index =
685         am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
686       am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
687       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
688                                        sizeof (ip6_5tuple_mask) - 1, ~0,
689                                        am->l2_output_classify_next_acl_ip6,
690                                        &ip6_table_index, 0);
691     }
692   if (am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
693     {
694       dot1q_table_index =
695         am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index];
696       am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
697       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
698                                        sizeof (ip6_5tuple_mask) - 1, ~0,
699                                        ~0, &dot1q_table_index, 0);
700     }
701   if (am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
702     {
703       dot1ad_table_index =
704         am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index];
705       am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
706       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
707                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
708                                        ~0, &dot1ad_table_index, 0);
709     }
710   if (am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
711     {
712       etype_table_index =
713         am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index];
714       am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
715       acl_classify_add_del_table_tiny (cm, ethertype_mask,
716                                        sizeof (ethertype_mask) - 1, ~0,
717                                        ~0, &etype_table_index, 0);
718     }
719   clib_mem_set_heap (oldheap);
720   return 0;
721 }
722
723 static void
724 acl_add_vlan_session (acl_main_t * am, u32 table_index, u8 is_output,
725                       u8 is_dot1ad, u8 is_ip6)
726 {
727   vnet_classify_main_t *cm = &vnet_classify_main;
728   u8 *match;
729   u32 next_acl;
730   u8 idx;
731   u8 session_idx;
732
733   if (is_ip6)
734     {
735       next_acl =
736         (is_output) ? am->
737         l2_output_classify_next_acl_ip6 : am->l2_input_classify_next_acl_ip6;
738     }
739   else
740     {
741       next_acl =
742         (is_output) ? am->
743         l2_output_classify_next_acl_ip4 : am->l2_input_classify_next_acl_ip4;
744     }
745   match = (is_dot1ad) ? dot1ad_5tuple_mask : dot1q_5tuple_mask;
746   idx = (is_dot1ad) ? 20 : 16;
747   if (is_dot1ad)
748     {
749       /* 802.1ad ethertype */
750       match[12] = 0x88;
751       match[13] = 0xa8;
752       /* 802.1q ethertype */
753       match[16] = 0x81;
754       match[17] = 0x00;
755     }
756   else
757     {
758       /* 802.1q ethertype */
759       match[12] = 0x81;
760       match[13] = 0x00;
761     }
762
763   /* add sessions to vlan tables per ethernet_type */
764   if (is_ip6)
765     {
766       match[idx] = 0x86;
767       match[idx + 1] = 0xdd;
768       session_idx = 1;
769     }
770   else
771     {
772       match[idx] = 0x08;
773       match[idx + 1] = 0x00;
774       session_idx = 0;
775     }
776   vnet_classify_add_del_session (cm, table_index, match, next_acl,
777                                  session_idx, 0, 0, 0, 1);
778   /* reset the mask back to being a mask */
779   match[idx] = 0xff;
780   match[idx + 1] = 0xff;
781   match[12] = 0xff;
782   match[13] = 0xff;
783   if (is_dot1ad)
784     {
785       match[16] = 0xff;
786       match[17] = 0xff;
787     }
788 }
789
790 static int
791 intf_has_etype_whitelist (acl_main_t * am, u32 sw_if_index, int is_input)
792 {
793   u16 **v = is_input
794     ? am->input_etype_whitelist_by_sw_if_index
795     : am->output_etype_whitelist_by_sw_if_index;
796   return ((vec_len (v) > sw_if_index) && vec_elt (v, sw_if_index));
797 }
798
799 static int
800 etype_whitelist_add_sessions (acl_main_t * am, u32 sw_if_index, int is_input,
801                               u32 etype_table_index)
802 {
803   vnet_classify_main_t *cm = &vnet_classify_main;
804   u16 **v = is_input
805     ? am->input_etype_whitelist_by_sw_if_index
806     : am->output_etype_whitelist_by_sw_if_index;
807   u8 *match = ethertype_mask;
808
809   int i;
810   int rv = 0;
811   u16 *whitelist = vec_elt (v, sw_if_index);
812   u32 next = ~0;                /* permit */
813   for (i = 0; i < vec_len (whitelist); i++)
814     {
815       /* big-endian */
816       match[12] = (whitelist[i] >> 8) & 0xff;
817       match[13] = whitelist[i] & 0xff;
818       rv = rv
819         || vnet_classify_add_del_session (cm, etype_table_index, match, next,
820                                           whitelist[i], 0, 0, 0, 1);
821     }
822
823   /* restore the mask */
824   match[12] = 0xff;
825   match[13] = 0xff;
826   return rv;
827 }
828
829 static int
830 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
831 {
832   vnet_classify_main_t *cm = &vnet_classify_main;
833   u32 ip4_table_index = ~0;
834   u32 ip6_table_index = ~0;
835   u32 dot1q_table_index = ~0;
836   u32 dot1ad_table_index = ~0;
837   u32 etype_table_index = ~0;
838   int rv;
839
840   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
841
842   /* in case there were previous tables attached */
843   acl_unhook_l2_input_classify (am, sw_if_index);
844   rv =
845     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
846                                      sizeof (ip4_5tuple_mask) - 1, ~0,
847                                      am->l2_input_classify_next_acl_ip4,
848                                      &ip4_table_index, 1);
849   if (rv)
850     goto done;
851
852   rv =
853     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
854                                      sizeof (ip6_5tuple_mask) - 1, ~0,
855                                      am->l2_input_classify_next_acl_ip6,
856                                      &ip6_table_index, 1);
857   if (rv)
858     {
859       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
860                                        sizeof (ip4_5tuple_mask) - 1, ~0,
861                                        am->l2_input_classify_next_acl_ip4,
862                                        &ip4_table_index, 0);
863       goto done;
864     }
865
866   if (intf_has_etype_whitelist (am, sw_if_index, 1))
867     {
868       acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0,  /* drop if no match */
869                                        &etype_table_index, 1);
870       etype_whitelist_add_sessions (am, sw_if_index, 1, etype_table_index);
871     }
872
873   rv =
874     acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
875                                      sizeof (dot1ad_5tuple_mask) - 1,
876                                      etype_table_index, ~0,
877                                      &dot1ad_table_index, 1);
878   rv =
879     acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
880                                      sizeof (dot1q_5tuple_mask) - 1,
881                                      dot1ad_table_index, ~0,
882                                      &dot1q_table_index, 1);
883   if (rv)
884     {
885       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
886                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
887                                        ~0, &dot1ad_table_index, 0);
888       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
889                                        sizeof (ip6_5tuple_mask) - 1, ~0,
890                                        am->l2_input_classify_next_acl_ip6,
891                                        &ip6_table_index, 0);
892       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
893                                        sizeof (ip4_5tuple_mask) - 1, ~0,
894                                        am->l2_input_classify_next_acl_ip4,
895                                        &ip4_table_index, 0);
896       goto done;
897     }
898
899   rv =
900     vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
901                                        ip6_table_index, dot1q_table_index);
902
903   if (rv)
904     {
905       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
906                                        sizeof (ip4_5tuple_mask) - 1, ~0,
907                                        am->l2_input_classify_next_acl_ip4,
908                                        &ip4_table_index, 0);
909       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
910                                        sizeof (ip6_5tuple_mask) - 1, ~0,
911                                        am->l2_input_classify_next_acl_ip6,
912                                        &ip6_table_index, 0);
913       acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
914                                        sizeof (dot1q_5tuple_mask) - 1, ~0,
915                                        ~0, &dot1q_table_index, 0);
916       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
917                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
918                                        ~0, &dot1ad_table_index, 0);
919       goto done;
920     }
921
922   /* add sessions to vlan tables per ethernet_type */
923   acl_add_vlan_session (am, dot1q_table_index, 0, 0, 0);
924   acl_add_vlan_session (am, dot1q_table_index, 0, 0, 1);
925   acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 0);
926   acl_add_vlan_session (am, dot1ad_table_index, 0, 1, 1);
927
928   am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
929     ip4_table_index;
930   am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
931     ip6_table_index;
932   am->acl_dot1q_input_classify_table_by_sw_if_index[sw_if_index] =
933     dot1q_table_index;
934   am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
935     dot1ad_table_index;
936   am->acl_dot1ad_input_classify_table_by_sw_if_index[sw_if_index] =
937     dot1ad_table_index;
938   am->acl_etype_input_classify_table_by_sw_if_index[sw_if_index] =
939     etype_table_index;
940
941   vnet_l2_input_classify_enable_disable (sw_if_index, 1);
942 done:
943   clib_mem_set_heap (prevheap);
944   return rv;
945 }
946
947 static int
948 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
949 {
950   vnet_classify_main_t *cm = &vnet_classify_main;
951   u32 ip4_table_index = ~0;
952   u32 ip6_table_index = ~0;
953   u32 dot1q_table_index = ~0;
954   u32 dot1ad_table_index = ~0;
955   u32 etype_table_index = ~0;
956   int rv;
957
958   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
959
960   /* in case there were previous tables attached */
961   acl_unhook_l2_output_classify (am, sw_if_index);
962   rv =
963     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
964                                      sizeof (ip4_5tuple_mask) - 1, ~0,
965                                      am->l2_output_classify_next_acl_ip4,
966                                      &ip4_table_index, 1);
967   if (rv)
968     goto done;
969   rv =
970     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
971                                      sizeof (ip6_5tuple_mask) - 1, ~0,
972                                      am->l2_output_classify_next_acl_ip6,
973                                      &ip6_table_index, 1);
974   if (rv)
975     {
976       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
977                                        sizeof (ip4_5tuple_mask) - 1, ~0,
978                                        am->l2_output_classify_next_acl_ip4,
979                                        &ip4_table_index, 0);
980       goto done;
981     }
982
983   if (intf_has_etype_whitelist (am, sw_if_index, 0))
984     {
985       acl_classify_add_del_table_tiny (cm, ethertype_mask, sizeof (ethertype_mask) - 1, ~0, 0,  /* drop if no match */
986                                        &etype_table_index, 1);
987       etype_whitelist_add_sessions (am, sw_if_index, 0, etype_table_index);
988     }
989
990
991   rv =
992     acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
993                                      sizeof (dot1ad_5tuple_mask) - 1,
994                                      etype_table_index, ~0,
995                                      &dot1ad_table_index, 1);
996   rv =
997     acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
998                                      sizeof (dot1q_5tuple_mask) - 1,
999                                      dot1ad_table_index, ~0,
1000                                      &dot1q_table_index, 1);
1001   if (rv)
1002     {
1003       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1004                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
1005                                        ~0, &dot1ad_table_index, 0);
1006       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1007                                        sizeof (ip6_5tuple_mask) - 1, ~0,
1008                                        am->l2_output_classify_next_acl_ip6,
1009                                        &ip6_table_index, 0);
1010       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1011                                        sizeof (ip4_5tuple_mask) - 1, ~0,
1012                                        am->l2_output_classify_next_acl_ip4,
1013                                        &ip4_table_index, 0);
1014       goto done;
1015     }
1016
1017   rv =
1018     vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1019                                         ip6_table_index, dot1q_table_index);
1020 /*
1021   clib_warning
1022     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
1023      sw_if_index, ip4_table_index, ip6_table_index);
1024 */
1025   if (rv)
1026     {
1027       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
1028                                        sizeof (ip6_5tuple_mask) - 1, ~0,
1029                                        am->l2_output_classify_next_acl_ip6,
1030                                        &ip6_table_index, 0);
1031       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
1032                                        sizeof (ip4_5tuple_mask) - 1, ~0,
1033                                        am->l2_output_classify_next_acl_ip4,
1034                                        &ip4_table_index, 0);
1035       acl_classify_add_del_table_tiny (cm, dot1q_5tuple_mask,
1036                                        sizeof (dot1q_5tuple_mask) - 1, ~0,
1037                                        ~0, &dot1q_table_index, 0);
1038       acl_classify_add_del_table_tiny (cm, dot1ad_5tuple_mask,
1039                                        sizeof (dot1ad_5tuple_mask) - 1, ~0,
1040                                        ~0, &dot1ad_table_index, 0);
1041       goto done;
1042     }
1043
1044   /* add sessions to vlan tables per ethernet_type */
1045   acl_add_vlan_session (am, dot1q_table_index, 1, 0, 0);
1046   acl_add_vlan_session (am, dot1q_table_index, 1, 0, 1);
1047   acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 0);
1048   acl_add_vlan_session (am, dot1ad_table_index, 1, 1, 1);
1049
1050   am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
1051     ip4_table_index;
1052   am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
1053     ip6_table_index;
1054   am->acl_dot1q_output_classify_table_by_sw_if_index[sw_if_index] =
1055     dot1q_table_index;
1056   am->acl_dot1ad_output_classify_table_by_sw_if_index[sw_if_index] =
1057     dot1ad_table_index;
1058   am->acl_etype_output_classify_table_by_sw_if_index[sw_if_index] =
1059     etype_table_index;
1060
1061   vnet_l2_output_classify_enable_disable (sw_if_index, 1);
1062 done:
1063   clib_mem_set_heap (prevheap);
1064   return rv;
1065 }
1066
1067 static void
1068 acl_clear_sessions (acl_main_t * am, u32 sw_if_index)
1069 {
1070   void *oldheap = clib_mem_set_heap (am->vlib_main->heap_base);
1071   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1072                              ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
1073                              sw_if_index);
1074   clib_mem_set_heap (oldheap);
1075 }
1076
1077
1078 static int
1079 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
1080                                  int enable_disable)
1081 {
1082   int rv = 0;
1083
1084   /* Utterly wrong? */
1085   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1086                           sw_if_index))
1087     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1088
1089   if (clib_bitmap_get (am->in_acl_on_sw_if_index, sw_if_index) ==
1090       enable_disable)
1091     return 0;
1092
1093   acl_fa_enable_disable (sw_if_index, 1, enable_disable);
1094
1095   if (enable_disable)
1096     {
1097       rv = acl_hook_l2_input_classify (am, sw_if_index);
1098     }
1099   else
1100     {
1101       rv = acl_unhook_l2_input_classify (am, sw_if_index);
1102     }
1103
1104   am->in_acl_on_sw_if_index =
1105     clib_bitmap_set (am->in_acl_on_sw_if_index, sw_if_index, enable_disable);
1106
1107   return rv;
1108 }
1109
1110 static int
1111 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
1112                                   int enable_disable)
1113 {
1114   int rv;
1115
1116   /* Utterly wrong? */
1117   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
1118                           sw_if_index))
1119     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1120
1121   if (clib_bitmap_get (am->out_acl_on_sw_if_index, sw_if_index) ==
1122       enable_disable)
1123     return 0;
1124
1125   acl_fa_enable_disable (sw_if_index, 0, enable_disable);
1126
1127   if (enable_disable)
1128     {
1129       rv = acl_hook_l2_output_classify (am, sw_if_index);
1130     }
1131   else
1132     {
1133       rv = acl_unhook_l2_output_classify (am, sw_if_index);
1134     }
1135
1136   am->out_acl_on_sw_if_index =
1137     clib_bitmap_set (am->out_acl_on_sw_if_index, sw_if_index, enable_disable);
1138
1139   return rv;
1140 }
1141
1142 static int
1143 acl_interface_inout_enable_disable (acl_main_t * am, u32 sw_if_index,
1144                                     int is_input, int enable_disable)
1145 {
1146   if (is_input)
1147     return acl_interface_in_enable_disable (am, sw_if_index, enable_disable);
1148   else
1149     return acl_interface_out_enable_disable (am, sw_if_index, enable_disable);
1150 }
1151
1152 static int
1153 acl_is_not_defined (acl_main_t * am, u32 acl_list_index)
1154 {
1155   return (pool_is_free_index (am->acls, acl_list_index));
1156 }
1157
1158 static int
1159 acl_interface_set_inout_acl_list (acl_main_t * am, u32 sw_if_index,
1160                                   u8 is_input, u32 * vec_acl_list_index,
1161                                   int *may_clear_sessions)
1162 {
1163   u32 *pacln;
1164   uword *seen_acl_bitmap = 0;
1165   uword *old_seen_acl_bitmap = 0;
1166   uword *change_acl_bitmap = 0;
1167   int acln;
1168   int rv = 0;
1169
1170
1171   if (am->trace_acl > 255)
1172     clib_warning
1173       ("API dbg: acl_interface_set_inout_acl_list: sw_if_index %d is_input %d acl_vec: [%U]",
1174        sw_if_index, is_input, format_vec32, vec_acl_list_index, "%d");
1175
1176   vec_foreach (pacln, vec_acl_list_index)
1177   {
1178     if (acl_is_not_defined (am, *pacln))
1179       {
1180         /* ACL is not defined. Can not apply */
1181         clib_warning ("ERROR: ACL %d not defined", *pacln);
1182         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1183         goto done;
1184       }
1185     if (clib_bitmap_get (seen_acl_bitmap, *pacln))
1186       {
1187         /* ACL being applied twice within the list. error. */
1188         clib_warning ("ERROR: ACL %d being applied twice", *pacln);
1189         rv = VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
1190         goto done;
1191       }
1192     seen_acl_bitmap = clib_bitmap_set (seen_acl_bitmap, *pacln, 1);
1193   }
1194
1195
1196   u32 **pinout_lc_index_by_sw_if_index =
1197     is_input ? &am->
1198     input_lc_index_by_sw_if_index : &am->output_lc_index_by_sw_if_index;
1199
1200   u32 ***pinout_acl_vec_by_sw_if_index =
1201     is_input ? &am->
1202     input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1203
1204   u32 ***pinout_sw_if_index_vec_by_acl =
1205     is_input ? &am->
1206     input_sw_if_index_vec_by_acl : &am->output_sw_if_index_vec_by_acl;
1207
1208   vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1209
1210   clib_bitmap_validate (old_seen_acl_bitmap, 1);
1211
1212   vec_foreach (pacln, (*pinout_acl_vec_by_sw_if_index)[sw_if_index])
1213   {
1214     old_seen_acl_bitmap = clib_bitmap_set (old_seen_acl_bitmap, *pacln, 1);
1215   }
1216   change_acl_bitmap =
1217     clib_bitmap_dup_xor (old_seen_acl_bitmap, seen_acl_bitmap);
1218
1219   if (am->trace_acl > 255)
1220     clib_warning ("bitmaps: old seen %U new seen %U changed %U",
1221                   format_bitmap_hex, old_seen_acl_bitmap, format_bitmap_hex,
1222                   seen_acl_bitmap, format_bitmap_hex, change_acl_bitmap);
1223
1224 /* *INDENT-OFF* */
1225   clib_bitmap_foreach(acln, change_acl_bitmap, ({
1226     if (clib_bitmap_get(old_seen_acl_bitmap, acln)) {
1227       /* ACL is being removed. */
1228       if (acln < vec_len((*pinout_sw_if_index_vec_by_acl))) {
1229         int index = vec_search((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1230         vec_del1((*pinout_sw_if_index_vec_by_acl)[acln], index);
1231       }
1232     } else {
1233       /* ACL is being added. */
1234       vec_validate((*pinout_sw_if_index_vec_by_acl), acln);
1235       vec_add1((*pinout_sw_if_index_vec_by_acl)[acln], sw_if_index);
1236     }
1237   }));
1238 /* *INDENT-ON* */
1239
1240   vec_free ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1241   (*pinout_acl_vec_by_sw_if_index)[sw_if_index] =
1242     vec_dup (vec_acl_list_index);
1243
1244   /* if no commonalities between the ACL# - then we should definitely clear the sessions */
1245   if (may_clear_sessions && *may_clear_sessions
1246       && !clib_bitmap_is_zero (change_acl_bitmap))
1247     {
1248       acl_clear_sessions (am, sw_if_index);
1249       *may_clear_sessions = 0;
1250     }
1251
1252   /*
1253    * prepare or delete the lookup context if necessary, and if context exists, set ACL list
1254    */
1255   vec_validate_init_empty ((*pinout_lc_index_by_sw_if_index), sw_if_index,
1256                            ~0);
1257   if (vec_len (vec_acl_list_index) > 0)
1258     {
1259       u32 lc_index = (*pinout_lc_index_by_sw_if_index)[sw_if_index];
1260       if (~0 == lc_index)
1261         {
1262           if (~0 == am->interface_acl_user_id)
1263             am->interface_acl_user_id =
1264               acl_plugin_register_user_module ("interface ACL", "sw_if_index",
1265                                                "is_input");
1266           lc_index =
1267             acl_plugin_get_lookup_context_index (am->interface_acl_user_id,
1268                                                  sw_if_index, is_input);
1269           ASSERT (lc_index >= 0);
1270           (*pinout_lc_index_by_sw_if_index)[sw_if_index] = lc_index;
1271         }
1272       acl_plugin_set_acl_vec_for_context (lc_index, vec_acl_list_index);
1273     }
1274   else
1275     {
1276       if (~0 != (*pinout_lc_index_by_sw_if_index)[sw_if_index])
1277         {
1278           acl_plugin_put_lookup_context_index ((*pinout_lc_index_by_sw_if_index)[sw_if_index]);
1279           (*pinout_lc_index_by_sw_if_index)[sw_if_index] = ~0;
1280         }
1281     }
1282
1283   /* ensure ACL processing is enabled/disabled as needed */
1284   acl_interface_inout_enable_disable (am, sw_if_index, is_input,
1285                                       vec_len (vec_acl_list_index) > 0);
1286
1287 done:
1288   clib_bitmap_free (change_acl_bitmap);
1289   clib_bitmap_free (seen_acl_bitmap);
1290   clib_bitmap_free (old_seen_acl_bitmap);
1291   return rv;
1292 }
1293
1294 static void
1295 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input,
1296                                 int *may_clear_sessions)
1297 {
1298   acl_main_t *am = &acl_main;
1299   void *oldheap = acl_set_heap (am);
1300   acl_interface_set_inout_acl_list (am, sw_if_index, is_input, 0,
1301                                     may_clear_sessions);
1302   clib_mem_set_heap (oldheap);
1303 }
1304
1305 static int
1306 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
1307                                  u32 acl_list_index)
1308 {
1309
1310   acl_main_t *am = &acl_main;
1311   u32 *acl_vec = 0;
1312   int may_clear_sessions = 1;
1313
1314   int error_already_applied = is_input ? VNET_API_ERROR_ACL_IN_USE_INBOUND
1315     : VNET_API_ERROR_ACL_IN_USE_OUTBOUND;
1316
1317   u32 ***pinout_acl_vec_by_sw_if_index =
1318     is_input ? &am->
1319     input_acl_vec_by_sw_if_index : &am->output_acl_vec_by_sw_if_index;
1320   int rv = 0;
1321   void *oldheap = acl_set_heap (am);
1322
1323   if (is_add)
1324     {
1325       vec_validate ((*pinout_acl_vec_by_sw_if_index), sw_if_index);
1326       u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1327                               acl_list_index);
1328
1329       if (~0 != index)
1330         {
1331           rv = error_already_applied;
1332           goto done;
1333         }
1334
1335       acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1336       vec_add1 (acl_vec, acl_list_index);
1337     }
1338   else
1339     {
1340       if (sw_if_index > vec_len (*pinout_acl_vec_by_sw_if_index))
1341         {
1342           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1343           goto done;
1344         }
1345
1346       u32 index = vec_search ((*pinout_acl_vec_by_sw_if_index)[sw_if_index],
1347                               acl_list_index);
1348
1349       if (~0 == index)
1350         {
1351           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1352           goto done;
1353         }
1354
1355       acl_vec = vec_dup ((*pinout_acl_vec_by_sw_if_index)[sw_if_index]);
1356       vec_del1 (acl_vec, index);
1357     }
1358
1359   rv = acl_interface_set_inout_acl_list (am, sw_if_index, is_input, acl_vec,
1360                                          &may_clear_sessions);
1361 done:
1362   vec_free (acl_vec);
1363   clib_mem_set_heap (oldheap);
1364   return rv;
1365 }
1366
1367 static int
1368 acl_set_etype_whitelists (acl_main_t * am, u32 sw_if_index, u16 * vec_in,
1369                           u16 * vec_out)
1370 {
1371   vec_validate (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
1372   vec_validate (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
1373
1374   vec_free (am->input_etype_whitelist_by_sw_if_index[sw_if_index]);
1375   vec_free (am->output_etype_whitelist_by_sw_if_index[sw_if_index]);
1376
1377   am->input_etype_whitelist_by_sw_if_index[sw_if_index] = vec_in;
1378   am->output_etype_whitelist_by_sw_if_index[sw_if_index] = vec_out;
1379
1380   /*
1381    * if there are already inbound/outbound ACLs applied, toggle the
1382    * enable/disable - this will recreate the necessary tables.
1383    */
1384
1385   if (vec_len (am->input_acl_vec_by_sw_if_index) > sw_if_index)
1386     {
1387       if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1388         {
1389           acl_interface_in_enable_disable (am, sw_if_index, 0);
1390           acl_interface_in_enable_disable (am, sw_if_index, 1);
1391         }
1392     }
1393   if (vec_len (am->output_acl_vec_by_sw_if_index) > sw_if_index)
1394     {
1395       if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1396         {
1397           acl_interface_out_enable_disable (am, sw_if_index, 0);
1398           acl_interface_out_enable_disable (am, sw_if_index, 1);
1399         }
1400     }
1401   return 0;
1402 }
1403
1404
1405 typedef struct
1406 {
1407   u8 is_ipv6;
1408   u8 has_egress;
1409   u8 mac_mask[6];
1410   u8 prefix_len;
1411   u32 count;
1412   u32 table_index;
1413   u32 arp_table_index;
1414   u32 dot1q_table_index;
1415   u32 dot1ad_table_index;
1416   u32 arp_dot1q_table_index;
1417   u32 arp_dot1ad_table_index;
1418   /* egress tables */
1419   u32 out_table_index;
1420   u32 out_arp_table_index;
1421   u32 out_dot1q_table_index;
1422   u32 out_dot1ad_table_index;
1423   u32 out_arp_dot1q_table_index;
1424   u32 out_arp_dot1ad_table_index;
1425 } macip_match_type_t;
1426
1427 static u32
1428 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1429                        u8 is_ipv6)
1430 {
1431   u32 i;
1432   if (mv)
1433     {
1434       for (i = 0; i < vec_len (mv); i++)
1435         {
1436           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1437               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1438             {
1439               return i;
1440             }
1441         }
1442     }
1443   return ~0;
1444 }
1445
1446
1447 /* Get metric used to sort match types.
1448    The more specific and the more often seen - the bigger the metric */
1449 static int
1450 match_type_metric (macip_match_type_t * m)
1451 {
1452   unsigned int mac_bits_set = 0;
1453   unsigned int mac_byte;
1454   int i;
1455   for (i = 0; i < 6; i++)
1456     {
1457       mac_byte = m->mac_mask[i];
1458       for (; mac_byte; mac_byte >>= 1)
1459         mac_bits_set += mac_byte & 1;
1460     }
1461   /*
1462    * Attempt to place the more specific and the more used rules on top.
1463    * There are obvious caveat corner cases to this, but they do not
1464    * seem to be sensible in real world (e.g. specific IPv4 with wildcard MAC
1465    * going with a wildcard IPv4 with a specific MAC).
1466    */
1467   return m->prefix_len + mac_bits_set + m->is_ipv6 + 10 * m->count;
1468 }
1469
1470 static int
1471 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1472 {
1473   /* Ascending sort based on the metric values */
1474   return match_type_metric (m1) - match_type_metric (m2);
1475 }
1476
1477 /* Get the offset of L3 source within ethernet packet */
1478 static int
1479 get_l3_src_offset (int is6)
1480 {
1481   if (is6)
1482     return (sizeof (ethernet_header_t) +
1483             offsetof (ip6_header_t, src_address));
1484   else
1485     return (sizeof (ethernet_header_t) +
1486             offsetof (ip4_header_t, src_address));
1487 }
1488
1489 static int
1490 get_l3_dst_offset (int is6)
1491 {
1492   if (is6)
1493     return (sizeof (ethernet_header_t) +
1494             offsetof (ip6_header_t, dst_address));
1495   else
1496     return (sizeof (ethernet_header_t) +
1497             offsetof (ip4_header_t, dst_address));
1498 }
1499
1500 /*
1501  * return if the is_permit value also requires to create the egress tables
1502  * For backwards compatibility, we keep the is_permit = 1 to only
1503  * create the ingress tables, and the new value of 3 will also
1504  * create the egress tables based on destination.
1505  */
1506 static int
1507 macip_permit_also_egress (u8 is_permit)
1508 {
1509   return (is_permit == 3);
1510 }
1511
1512 static int
1513 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1514 {
1515   macip_match_type_t *mvec = NULL;
1516   macip_match_type_t *mt;
1517   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1518   int i;
1519   u32 match_type_index;
1520   u32 last_table;
1521   u32 out_last_table;
1522   u8 mask[5 * 16];
1523   vnet_classify_main_t *cm = &vnet_classify_main;
1524
1525   /* Count the number of different types of rules */
1526   for (i = 0; i < a->count; i++)
1527     {
1528       if (~0 ==
1529           (match_type_index =
1530            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1531                                   a->rules[i].src_prefixlen,
1532                                   a->rules[i].is_ipv6)))
1533         {
1534           match_type_index = vec_len (mvec);
1535           vec_validate (mvec, match_type_index);
1536           memcpy (mvec[match_type_index].mac_mask,
1537                   a->rules[i].src_mac_mask, 6);
1538           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1539           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1540           mvec[match_type_index].has_egress = 0;
1541           mvec[match_type_index].table_index = ~0;
1542           mvec[match_type_index].arp_table_index = ~0;
1543           mvec[match_type_index].dot1q_table_index = ~0;
1544           mvec[match_type_index].dot1ad_table_index = ~0;
1545           mvec[match_type_index].arp_dot1q_table_index = ~0;
1546           mvec[match_type_index].arp_dot1ad_table_index = ~0;
1547           mvec[match_type_index].out_table_index = ~0;
1548           mvec[match_type_index].out_arp_table_index = ~0;
1549           mvec[match_type_index].out_dot1q_table_index = ~0;
1550           mvec[match_type_index].out_dot1ad_table_index = ~0;
1551           mvec[match_type_index].out_arp_dot1q_table_index = ~0;
1552           mvec[match_type_index].out_arp_dot1ad_table_index = ~0;
1553         }
1554       mvec[match_type_index].count++;
1555       mvec[match_type_index].has_egress |=
1556         macip_permit_also_egress (a->rules[i].is_permit);
1557     }
1558   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1559   vec_sort_with_function (mvec, match_type_compare);
1560   /* Create the classifier tables */
1561   last_table = ~0;
1562   out_last_table = ~0;
1563   /* First add ARP tables */
1564   vec_foreach (mt, mvec)
1565   {
1566     int mask_len;
1567     int is6 = mt->is_ipv6;
1568     int tags;
1569     u32 *last_tag_table;
1570     u32 *out_last_tag_table;
1571     u32 l3_offset;
1572
1573     if (!is6)
1574       {
1575         /*
1576            0                   1                   2                   3
1577            0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1578            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1579            |                      Destination Address                      |
1580            +                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1581            |                               |                               |
1582            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
1583            |                         Source Address                        |
1584            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1585            |           EtherType           |         Hardware Type         |
1586            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1587            |         Protocol Type         |  Hw addr len  | Proto addr len|
1588            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1589            |             Opcode            |                               |
1590            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
1591            |                    Sender Hardware Address                    |
1592            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1593            |                    Sender Protocol Address                    |
1594            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1595            |                    Target Hardware Address                    |
1596            +                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1597            |                               |     TargetProtocolAddress     |
1598            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1599            |                               |
1600            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1601          */
1602         for (tags = 2; tags >= 0; tags--)
1603           {
1604             memset (mask, 0, sizeof (mask));
1605             /* source MAC address */
1606             memcpy (&mask[6], mt->mac_mask, 6);
1607
1608             switch (tags)
1609               {
1610               case 0:
1611               default:
1612                 memset (&mask[12], 0xff, 2);    /* ethernet protocol */
1613                 l3_offset = 14;
1614                 last_tag_table = &mt->arp_table_index;
1615                 break;
1616               case 1:
1617                 memset (&mask[12], 0xff, 2);    /* VLAN tag1 */
1618                 memset (&mask[16], 0xff, 2);    /* ethernet protocol */
1619                 l3_offset = 18;
1620                 last_tag_table = &mt->arp_dot1q_table_index;
1621                 break;
1622               case 2:
1623                 memset (&mask[12], 0xff, 2);    /* VLAN tag1 */
1624                 memset (&mask[16], 0xff, 2);    /* VLAN tag2 */
1625                 memset (&mask[20], 0xff, 2);    /* ethernet protocol */
1626                 l3_offset = 22;
1627                 last_tag_table = &mt->arp_dot1ad_table_index;
1628                 break;
1629               }
1630
1631             /* sender hardware address within ARP */
1632             memcpy (&mask[l3_offset + 8], mt->mac_mask, 6);
1633             /* sender protocol address within ARP */
1634             for (i = 0; i < (mt->prefix_len / 8); i++)
1635               mask[l3_offset + 14 + i] = 0xff;
1636             if (mt->prefix_len % 8)
1637               mask[l3_offset + 14 + (mt->prefix_len / 8)] =
1638                 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1639
1640             mask_len = ((l3_offset + 14 + ((mt->prefix_len + 7) / 8) +
1641                          (sizeof (u32x4) -
1642                           1)) / sizeof (u32x4)) * sizeof (u32x4);
1643             acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1644                                               (~0 == last_table) ? 0 : ~0,
1645                                               last_tag_table, 1);
1646             last_table = *last_tag_table;
1647             if (mt->has_egress)
1648               {
1649                 /* egress ARP table */
1650                 memset (mask, 0, sizeof (mask));
1651
1652                 switch (tags)
1653                   {
1654                   case 0:
1655                   default:
1656                     memset (&mask[12], 0xff, 2);        /* ethernet protocol */
1657                     l3_offset = 14;
1658                     out_last_tag_table = &mt->out_arp_table_index;
1659                     break;
1660                   case 1:
1661                     memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1662                     memset (&mask[16], 0xff, 2);        /* ethernet protocol */
1663                     l3_offset = 18;
1664                     out_last_tag_table = &mt->out_arp_dot1q_table_index;
1665                     break;
1666                   case 2:
1667                     memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1668                     memset (&mask[16], 0xff, 2);        /* VLAN tag2 */
1669                     memset (&mask[20], 0xff, 2);        /* ethernet protocol */
1670                     l3_offset = 22;
1671                     out_last_tag_table = &mt->out_arp_dot1ad_table_index;
1672                     break;
1673                   }
1674
1675                 /* AYXX: FIXME here - can we tighten the ARP-related table more ? */
1676                 /* mask captures just the destination and the ethertype */
1677                 mask_len = ((l3_offset +
1678                              (sizeof (u32x4) -
1679                               1)) / sizeof (u32x4)) * sizeof (u32x4);
1680                 acl_classify_add_del_table_small (cm, mask, mask_len,
1681                                                   out_last_table,
1682                                                   (~0 ==
1683                                                    out_last_table) ? 0 : ~0,
1684                                                   out_last_tag_table, 1);
1685                 out_last_table = *out_last_tag_table;
1686               }
1687           }
1688       }
1689   }
1690   /* Now add IP[46] tables */
1691   vec_foreach (mt, mvec)
1692   {
1693     int mask_len;
1694     int is6 = mt->is_ipv6;
1695     int l3_src_offs;
1696     int l3_dst_offs;
1697     int tags;
1698     u32 *last_tag_table;
1699     u32 *out_last_tag_table;
1700
1701     /*
1702      * create chained tables for VLAN (no-tags, dot1q and dot1ad) packets
1703      */
1704     for (tags = 2; tags >= 0; tags--)
1705       {
1706         memset (mask, 0, sizeof (mask));
1707         memcpy (&mask[6], mt->mac_mask, 6);
1708         l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1709         switch (tags)
1710           {
1711           case 0:
1712           default:
1713             memset (&mask[12], 0xff, 2);        /* ethernet protocol */
1714             last_tag_table = &mt->table_index;
1715             break;
1716           case 1:
1717             memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1718             memset (&mask[16], 0xff, 2);        /* ethernet protocol */
1719             last_tag_table = &mt->dot1q_table_index;
1720             break;
1721           case 2:
1722             memset (&mask[12], 0xff, 2);        /* VLAN tag1 */
1723             memset (&mask[16], 0xff, 2);        /* VLAN tag2 */
1724             memset (&mask[20], 0xff, 2);        /* ethernet protocol */
1725             last_tag_table = &mt->dot1ad_table_index;
1726             break;
1727           }
1728         for (i = 0; i < (mt->prefix_len / 8); i++)
1729           {
1730             mask[l3_src_offs + i] = 0xff;
1731           }
1732         if (mt->prefix_len % 8)
1733           {
1734             mask[l3_src_offs + (mt->prefix_len / 8)] =
1735               0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1736           }
1737         /*
1738          * Round-up the number of bytes needed to store the prefix,
1739          * and round up the number of vectors too
1740          */
1741         mask_len = ((l3_src_offs + ((mt->prefix_len + 7) / 8) +
1742                      (sizeof (u32x4) - 1)) / sizeof (u32x4)) * sizeof (u32x4);
1743         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1744                                           (~0 == last_table) ? 0 : ~0,
1745                                           last_tag_table, 1);
1746         last_table = *last_tag_table;
1747       }
1748     if (mt->has_egress)
1749       {
1750         for (tags = 2; tags >= 0; tags--)
1751           {
1752             memset (mask, 0, sizeof (mask));
1753             /* MAC destination */
1754             memcpy (&mask[0], mt->mac_mask, 6);
1755             l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1756             switch (tags)
1757               {
1758               case 0:
1759               default:
1760                 memset (&mask[12], 0xff, 2);    /* ethernet protocol */
1761                 out_last_tag_table = &mt->out_table_index;
1762                 break;
1763               case 1:
1764                 memset (&mask[12], 0xff, 2);    /* VLAN tag1 */
1765                 memset (&mask[16], 0xff, 2);    /* ethernet protocol */
1766                 out_last_tag_table = &mt->out_dot1q_table_index;
1767                 break;
1768               case 2:
1769                 memset (&mask[12], 0xff, 2);    /* VLAN tag1 */
1770                 memset (&mask[16], 0xff, 2);    /* VLAN tag2 */
1771                 memset (&mask[20], 0xff, 2);    /* ethernet protocol */
1772                 out_last_tag_table = &mt->out_dot1ad_table_index;
1773                 break;
1774               }
1775             for (i = 0; i < (mt->prefix_len / 8); i++)
1776               {
1777                 mask[l3_dst_offs + i] = 0xff;
1778               }
1779             if (mt->prefix_len % 8)
1780               {
1781                 mask[l3_dst_offs + (mt->prefix_len / 8)] =
1782                   0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1783               }
1784             /*
1785              * Round-up the number of bytes needed to store the prefix,
1786              * and round up the number of vectors too
1787              */
1788             mask_len = ((l3_dst_offs + ((mt->prefix_len + 7) / 8) +
1789                          (sizeof (u32x4) -
1790                           1)) / sizeof (u32x4)) * sizeof (u32x4);
1791             acl_classify_add_del_table_small (cm, mask, mask_len,
1792                                               out_last_table,
1793                                               (~0 == out_last_table) ? 0 : ~0,
1794                                               out_last_tag_table, 1);
1795             out_last_table = *out_last_tag_table;
1796           }
1797       }
1798   }
1799   a->ip4_table_index = last_table;
1800   a->ip6_table_index = last_table;
1801   a->l2_table_index = last_table;
1802
1803   a->out_ip4_table_index = out_last_table;
1804   a->out_ip6_table_index = out_last_table;
1805   a->out_l2_table_index = out_last_table;
1806
1807   /* Populate the classifier tables with rules from the MACIP ACL */
1808   for (i = 0; i < a->count; i++)
1809     {
1810       u32 action = 0;
1811       u32 metadata = 0;
1812       int is6 = a->rules[i].is_ipv6;
1813       int l3_src_offs;
1814       int l3_dst_offs;
1815       u32 tag_table;
1816       int tags, eth;
1817
1818       match_type_index =
1819         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1820                                a->rules[i].src_prefixlen,
1821                                a->rules[i].is_ipv6);
1822       ASSERT (match_type_index != ~0);
1823
1824       for (tags = 2; tags >= 0; tags--)
1825         {
1826           memset (mask, 0, sizeof (mask));
1827           l3_src_offs = tags * 4 + get_l3_src_offset (is6);
1828           memcpy (&mask[6], a->rules[i].src_mac, 6);
1829           switch (tags)
1830             {
1831             case 0:
1832             default:
1833               tag_table = mvec[match_type_index].table_index;
1834               eth = 12;
1835               break;
1836             case 1:
1837               tag_table = mvec[match_type_index].dot1q_table_index;
1838               mask[12] = 0x81;
1839               mask[13] = 0x00;
1840               eth = 16;
1841               break;
1842             case 2:
1843               tag_table = mvec[match_type_index].dot1ad_table_index;
1844               mask[12] = 0x88;
1845               mask[13] = 0xa8;
1846               mask[16] = 0x81;
1847               mask[17] = 0x00;
1848               eth = 20;
1849               break;
1850             }
1851           if (is6)
1852             {
1853               memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1854               mask[eth] = 0x86;
1855               mask[eth + 1] = 0xdd;
1856             }
1857           else
1858             {
1859               memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1860               mask[eth] = 0x08;
1861               mask[eth + 1] = 0x00;
1862             }
1863
1864           /* add session to table mvec[match_type_index].table_index; */
1865           vnet_classify_add_del_session (cm, tag_table,
1866                                          mask, a->rules[i].is_permit ? ~0 : 0,
1867                                          i, 0, action, metadata, 1);
1868           memset (&mask[12], 0, sizeof (mask) - 12);
1869         }
1870
1871       /* add ARP table entry too */
1872       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1873         {
1874           memset (mask, 0, sizeof (mask));
1875           memcpy (&mask[6], a->rules[i].src_mac, 6);
1876
1877           for (tags = 2; tags >= 0; tags--)
1878             {
1879               switch (tags)
1880                 {
1881                 case 0:
1882                 default:
1883                   tag_table = mvec[match_type_index].arp_table_index;
1884                   mask[12] = 0x08;
1885                   mask[13] = 0x06;
1886                   l3_src_offs = 14;
1887                   break;
1888                 case 1:
1889                   tag_table = mvec[match_type_index].arp_dot1q_table_index;
1890                   mask[12] = 0x81;
1891                   mask[13] = 0x00;
1892                   mask[16] = 0x08;
1893                   mask[17] = 0x06;
1894                   l3_src_offs = 18;
1895                   break;
1896                 case 2:
1897                   tag_table = mvec[match_type_index].arp_dot1ad_table_index;
1898                   mask[12] = 0x88;
1899                   mask[13] = 0xa8;
1900                   mask[16] = 0x81;
1901                   mask[17] = 0x00;
1902                   mask[20] = 0x08;
1903                   mask[21] = 0x06;
1904                   l3_src_offs = 22;
1905                   break;
1906                 }
1907
1908               memcpy (&mask[l3_src_offs + 8], a->rules[i].src_mac, 6);
1909               memcpy (&mask[l3_src_offs + 14], &a->rules[i].src_ip_addr.ip4,
1910                       4);
1911               vnet_classify_add_del_session (cm, tag_table, mask,
1912                                              a->rules[i].is_permit ? ~0 : 0,
1913                                              i, 0, action, metadata, 1);
1914             }
1915         }
1916       if (macip_permit_also_egress (a->rules[i].is_permit))
1917         {
1918           /* Add the egress entry with destination set */
1919           for (tags = 2; tags >= 0; tags--)
1920             {
1921               memset (mask, 0, sizeof (mask));
1922               l3_dst_offs = tags * 4 + get_l3_dst_offset (is6);
1923               /* src mac in the other direction becomes dst */
1924               memcpy (&mask[0], a->rules[i].src_mac, 6);
1925               switch (tags)
1926                 {
1927                 case 0:
1928                 default:
1929                   tag_table = mvec[match_type_index].out_table_index;
1930                   eth = 12;
1931                   break;
1932                 case 1:
1933                   tag_table = mvec[match_type_index].out_dot1q_table_index;
1934                   mask[12] = 0x81;
1935                   mask[13] = 0x00;
1936                   eth = 16;
1937                   break;
1938                 case 2:
1939                   tag_table = mvec[match_type_index].out_dot1ad_table_index;
1940                   mask[12] = 0x88;
1941                   mask[13] = 0xa8;
1942                   mask[16] = 0x81;
1943                   mask[17] = 0x00;
1944                   eth = 20;
1945                   break;
1946                 }
1947               if (is6)
1948                 {
1949                   memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip6,
1950                           16);
1951                   mask[eth] = 0x86;
1952                   mask[eth + 1] = 0xdd;
1953                 }
1954               else
1955                 {
1956                   memcpy (&mask[l3_dst_offs], &a->rules[i].src_ip_addr.ip4,
1957                           4);
1958                   mask[eth] = 0x08;
1959                   mask[eth + 1] = 0x00;
1960                 }
1961
1962               /* add session to table mvec[match_type_index].table_index; */
1963               vnet_classify_add_del_session (cm, tag_table,
1964                                              mask,
1965                                              a->rules[i].is_permit ? ~0 : 0,
1966                                              i, 0, action, metadata, 1);
1967               // memset (&mask[12], 0, sizeof (mask) - 12);
1968             }
1969
1970           /* add ARP table entry too */
1971           if (!is6 && (mvec[match_type_index].out_arp_table_index != ~0))
1972             {
1973               for (tags = 2; tags >= 0; tags--)
1974                 {
1975                   memset (mask, 0, sizeof (mask));
1976                   switch (tags)
1977                     {
1978                     case 0:
1979                     default:
1980                       tag_table = mvec[match_type_index].out_arp_table_index;
1981                       mask[12] = 0x08;
1982                       mask[13] = 0x06;
1983                       break;
1984                     case 1:
1985                       tag_table =
1986                         mvec[match_type_index].out_arp_dot1q_table_index;
1987                       mask[12] = 0x81;
1988                       mask[13] = 0x00;
1989                       mask[16] = 0x08;
1990                       mask[17] = 0x06;
1991                       break;
1992                     case 2:
1993                       tag_table =
1994                         mvec[match_type_index].out_arp_dot1ad_table_index;
1995                       mask[12] = 0x88;
1996                       mask[13] = 0xa8;
1997                       mask[16] = 0x81;
1998                       mask[17] = 0x00;
1999                       mask[20] = 0x08;
2000                       mask[21] = 0x06;
2001                       break;
2002                     }
2003
2004                   vnet_classify_add_del_session (cm, tag_table,
2005                                                  mask,
2006                                                  a->
2007                                                  rules[i].is_permit ? ~0 : 0,
2008                                                  i, 0, action, metadata, 1);
2009                 }
2010             }
2011         }
2012     }
2013   return 0;
2014 }
2015
2016 static void
2017 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
2018 {
2019   vnet_classify_main_t *cm = &vnet_classify_main;
2020   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2021
2022   if (a->ip4_table_index != ~0)
2023     {
2024       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2025                                         &a->ip4_table_index, 0);
2026       a->ip4_table_index = ~0;
2027     }
2028   if (a->ip6_table_index != ~0)
2029     {
2030       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2031                                         &a->ip6_table_index, 0);
2032       a->ip6_table_index = ~0;
2033     }
2034   if (a->l2_table_index != ~0)
2035     {
2036       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index,
2037                                         0);
2038       a->l2_table_index = ~0;
2039     }
2040   if (a->out_ip4_table_index != ~0)
2041     {
2042       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2043                                         &a->out_ip4_table_index, 0);
2044       a->out_ip4_table_index = ~0;
2045     }
2046   if (a->out_ip6_table_index != ~0)
2047     {
2048       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2049                                         &a->out_ip6_table_index, 0);
2050       a->out_ip6_table_index = ~0;
2051     }
2052   if (a->out_l2_table_index != ~0)
2053     {
2054       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0,
2055                                         &a->out_l2_table_index, 0);
2056       a->out_l2_table_index = ~0;
2057     }
2058 }
2059
2060 static int
2061 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
2062                                              int is_apply)
2063 {
2064   int rv = 0;
2065   int rv0 = 0;
2066   int i;
2067   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
2068
2069   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2070     if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
2071       {
2072         rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
2073                                         a->ip6_table_index, a->l2_table_index,
2074                                         is_apply);
2075         /* return the first unhappy outcome but make try to plough through. */
2076         rv = rv || rv0;
2077         rv0 =
2078           vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
2079                                      a->out_ip6_table_index,
2080                                      a->out_l2_table_index, is_apply);
2081         /* return the first unhappy outcome but make try to plough through. */
2082         rv = rv || rv0;
2083       }
2084   return rv;
2085 }
2086
2087 static int
2088 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
2089                     u32 * acl_list_index, u8 * tag)
2090 {
2091   acl_main_t *am = &acl_main;
2092   macip_acl_list_t *a;
2093   macip_acl_rule_t *r;
2094   macip_acl_rule_t *acl_new_rules = 0;
2095   int i;
2096   int rv = 0;
2097
2098   if (*acl_list_index != ~0)
2099     {
2100       /* They supplied some number, let's see if this MACIP ACL exists */
2101       if (pool_is_free_index (am->macip_acls, *acl_list_index))
2102         {
2103           /* tried to replace a non-existent ACL, no point doing anything */
2104           clib_warning
2105             ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
2106              *acl_list_index, tag);
2107           return VNET_API_ERROR_NO_SUCH_ENTRY;
2108         }
2109     }
2110
2111   if (0 == count)
2112     {
2113       clib_warning
2114         ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
2115          tag);
2116     }
2117   /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
2118   if (~0 != *acl_list_index)
2119     rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
2120   void *oldheap = acl_set_heap (am);
2121   /* Create and populate the rules */
2122   if (count > 0)
2123     vec_validate (acl_new_rules, count - 1);
2124
2125   for (i = 0; i < count; i++)
2126     {
2127       r = &acl_new_rules[i];
2128       r->is_permit = rules[i].is_permit;
2129       r->is_ipv6 = rules[i].is_ipv6;
2130       memcpy (&r->src_mac, rules[i].src_mac, 6);
2131       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
2132       if (rules[i].is_ipv6)
2133         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
2134       else
2135         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
2136       r->src_prefixlen = rules[i].src_ip_prefix_len;
2137     }
2138
2139   if (~0 == *acl_list_index)
2140     {
2141       /* Get ACL index */
2142       pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
2143       memset (a, 0, sizeof (*a));
2144       /* Will return the newly allocated ACL index */
2145       *acl_list_index = a - am->macip_acls;
2146     }
2147   else
2148     {
2149       a = pool_elt_at_index (am->macip_acls, *acl_list_index);
2150       if (a->rules)
2151         {
2152           vec_free (a->rules);
2153         }
2154       macip_destroy_classify_tables (am, *acl_list_index);
2155     }
2156
2157   a->rules = acl_new_rules;
2158   a->count = count;
2159   memcpy (a->tag, tag, sizeof (a->tag));
2160
2161   /* Create and populate the classifer tables */
2162   macip_create_classify_tables (am, *acl_list_index);
2163   clib_mem_set_heap (oldheap);
2164   /* If the ACL was already applied somewhere, reapply the newly created tables */
2165   rv = rv
2166     || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
2167   return rv;
2168 }
2169
2170 /* No check that sw_if_index denotes a valid interface - the callers
2171  * were supposed to validate.
2172  *
2173  * That said, if sw_if_index corresponds to an interface that exists at all,
2174  * this function must return errors accordingly if the ACL is not applied.
2175  */
2176
2177 static int
2178 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
2179 {
2180   int rv;
2181   u32 macip_acl_index;
2182   macip_acl_list_t *a;
2183
2184   /* The vector is too short - MACIP ACL is not applied */
2185   if (sw_if_index >= vec_len (am->macip_acl_by_sw_if_index))
2186     return VNET_API_ERROR_NO_SUCH_ENTRY;
2187
2188   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
2189   /* No point in deleting MACIP ACL which is not applied */
2190   if (~0 == macip_acl_index)
2191     return VNET_API_ERROR_NO_SUCH_ENTRY;
2192
2193   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2194   /* remove the classifier tables off the interface L2 ACL */
2195   rv =
2196     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2197                               a->ip6_table_index, a->l2_table_index, 0);
2198   rv |=
2199     vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2200                                a->out_ip4_table_index, a->out_ip6_table_index,
2201                                a->out_l2_table_index, 0);
2202   /* Unset the MACIP ACL index */
2203   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
2204   /* macip_acl_interface_add_acl did a vec_add1() to this previously, so [sw_if_index] should be valid */
2205   u32 index = vec_search (am->sw_if_index_vec_by_macip_acl[macip_acl_index],
2206                           sw_if_index);
2207   if (index != ~0)
2208     vec_del1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], index);
2209   return rv;
2210 }
2211
2212 /* No check for validity of sw_if_index - the callers were supposed to validate */
2213
2214 static int
2215 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2216                              u32 macip_acl_index)
2217 {
2218   macip_acl_list_t *a;
2219   int rv;
2220   if (pool_is_free_index (am->macip_acls, macip_acl_index))
2221     {
2222       return VNET_API_ERROR_NO_SUCH_ENTRY;
2223     }
2224   void *oldheap = acl_set_heap (am);
2225   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2226   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2227   vec_validate (am->sw_if_index_vec_by_macip_acl, macip_acl_index);
2228   vec_add1 (am->sw_if_index_vec_by_macip_acl[macip_acl_index], sw_if_index);
2229   clib_mem_set_heap (oldheap);
2230   /* If there already a MACIP ACL applied, unapply it */
2231   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2232     macip_acl_interface_del_acl (am, sw_if_index);
2233   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2234
2235   /* Apply the classifier tables for L2 ACLs */
2236   rv =
2237     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2238                               a->ip6_table_index, a->l2_table_index, 1);
2239   rv |=
2240     vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2241                                a->out_ip4_table_index, a->out_ip6_table_index,
2242                                a->out_l2_table_index, 1);
2243   return rv;
2244 }
2245
2246 static int
2247 macip_acl_del_list (u32 acl_list_index)
2248 {
2249   acl_main_t *am = &acl_main;
2250   macip_acl_list_t *a;
2251   int i;
2252   if (pool_is_free_index (am->macip_acls, acl_list_index))
2253     {
2254       return VNET_API_ERROR_NO_SUCH_ENTRY;
2255     }
2256
2257   /* delete any references to the ACL */
2258   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2259     {
2260       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2261         {
2262           macip_acl_interface_del_acl (am, i);
2263         }
2264     }
2265
2266   void *oldheap = acl_set_heap (am);
2267   /* Now that classifier tables are detached, clean them up */
2268   macip_destroy_classify_tables (am, acl_list_index);
2269
2270   /* now we can delete the ACL itself */
2271   a = pool_elt_at_index (am->macip_acls, acl_list_index);
2272   if (a->rules)
2273     {
2274       vec_free (a->rules);
2275     }
2276   pool_put (am->macip_acls, a);
2277   clib_mem_set_heap (oldheap);
2278   return 0;
2279 }
2280
2281
2282 static int
2283 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2284                                  u32 acl_list_index)
2285 {
2286   acl_main_t *am = &acl_main;
2287   int rv = -1;
2288   if (is_add)
2289     {
2290       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2291     }
2292   else
2293     {
2294       rv = macip_acl_interface_del_acl (am, sw_if_index);
2295     }
2296   return rv;
2297 }
2298
2299 /*
2300  * If the client does not allocate enough memory for a variable-length
2301  * message, and then proceed to use it as if the full memory allocated,
2302  * absent the check we happily consume that on the VPP side, and go
2303  * along as if nothing happened. However, the resulting
2304  * effects range from just garbage in the API decode
2305  * (because the decoder snoops too far), to potential memory
2306  * corruptions.
2307  *
2308  * This verifies that the actual length of the message is
2309  * at least expected_len, and complains loudly if it is not.
2310  *
2311  * A failing check here is 100% a software bug on the API user side,
2312  * so we might as well yell.
2313  *
2314  */
2315 static int
2316 verify_message_len (void *mp, u32 expected_len, char *where)
2317 {
2318   u32 supplied_len = vl_msg_api_get_msg_length (mp);
2319   if (supplied_len < expected_len)
2320     {
2321       clib_warning ("%s: Supplied message length %d is less than expected %d",
2322                     where, supplied_len, expected_len);
2323       return 0;
2324     }
2325   else
2326     {
2327       return 1;
2328     }
2329 }
2330
2331 /* API message handler */
2332 static void
2333 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2334 {
2335   vl_api_acl_add_replace_reply_t *rmp;
2336   acl_main_t *am = &acl_main;
2337   int rv;
2338   u32 acl_list_index = ntohl (mp->acl_index);
2339   u32 acl_count = ntohl (mp->count);
2340   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2341
2342   if (verify_message_len (mp, expected_len, "acl_add_replace"))
2343     {
2344       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2345     }
2346   else
2347     {
2348       rv = VNET_API_ERROR_INVALID_VALUE;
2349     }
2350
2351   /* *INDENT-OFF* */
2352   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2353   ({
2354     rmp->acl_index = htonl(acl_list_index);
2355   }));
2356   /* *INDENT-ON* */
2357 }
2358
2359 static void
2360 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2361 {
2362   acl_main_t *am = &acl_main;
2363   vl_api_acl_del_reply_t *rmp;
2364   int rv;
2365
2366   rv = acl_del_list (ntohl (mp->acl_index));
2367
2368   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2369 }
2370
2371 static void
2372 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2373 {
2374   acl_main_t *am = &acl_main;
2375   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2376   u32 sw_if_index = ntohl (mp->sw_if_index);
2377   vl_api_acl_interface_add_del_reply_t *rmp;
2378   int rv = -1;
2379
2380   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2381     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2382   else
2383     rv =
2384       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2385                                        mp->is_input, ntohl (mp->acl_index));
2386
2387   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2388 }
2389
2390 static void
2391   vl_api_acl_interface_set_acl_list_t_handler
2392   (vl_api_acl_interface_set_acl_list_t * mp)
2393 {
2394   acl_main_t *am = &acl_main;
2395   vl_api_acl_interface_set_acl_list_reply_t *rmp;
2396   int rv = 0;
2397   int i;
2398   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2399   u32 sw_if_index = ntohl (mp->sw_if_index);
2400
2401   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2402     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2403   else
2404     {
2405       int may_clear_sessions = 1;
2406       for (i = 0; i < mp->count; i++)
2407         {
2408           if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2409             {
2410               /* ACL does not exist, so we can not apply it */
2411               rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2412             }
2413         }
2414       if (0 == rv)
2415         {
2416           void *oldheap = acl_set_heap (am);
2417
2418           u32 *in_acl_vec = 0;
2419           u32 *out_acl_vec = 0;
2420           for (i = 0; i < mp->count; i++)
2421             if (i < mp->n_input)
2422               vec_add1 (in_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2423             else
2424               vec_add1 (out_acl_vec, clib_net_to_host_u32 (mp->acls[i]));
2425
2426           rv =
2427             acl_interface_set_inout_acl_list (am, sw_if_index, 0, out_acl_vec,
2428                                               &may_clear_sessions);
2429           rv = rv
2430             || acl_interface_set_inout_acl_list (am, sw_if_index, 1,
2431                                                  in_acl_vec,
2432                                                  &may_clear_sessions);
2433           vec_free (in_acl_vec);
2434           vec_free (out_acl_vec);
2435           clib_mem_set_heap (oldheap);
2436         }
2437     }
2438
2439   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2440 }
2441
2442 static void
2443 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2444 {
2445   api_rule->is_permit = r->is_permit;
2446   api_rule->is_ipv6 = r->is_ipv6;
2447   if (r->is_ipv6)
2448     {
2449       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2450       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2451     }
2452   else
2453     {
2454       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2455       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2456     }
2457   api_rule->src_ip_prefix_len = r->src_prefixlen;
2458   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2459   api_rule->proto = r->proto;
2460   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2461   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2462   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2463   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2464   api_rule->tcp_flags_mask = r->tcp_flags_mask;
2465   api_rule->tcp_flags_value = r->tcp_flags_value;
2466 }
2467
2468 static void
2469 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2470                   acl_list_t * acl, u32 context)
2471 {
2472   vl_api_acl_details_t *mp;
2473   vl_api_acl_rule_t *rules;
2474   int i;
2475   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2476   void *oldheap = acl_set_heap (am);
2477
2478   mp = vl_msg_api_alloc (msg_size);
2479   memset (mp, 0, msg_size);
2480   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2481
2482   /* fill in the message */
2483   mp->context = context;
2484   mp->count = htonl (acl->count);
2485   mp->acl_index = htonl (acl - am->acls);
2486   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2487   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2488   rules = mp->r;
2489   for (i = 0; i < acl->count; i++)
2490     {
2491       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2492     }
2493
2494   clib_mem_set_heap (oldheap);
2495   vl_api_send_msg (reg, (u8 *) mp);
2496 }
2497
2498
2499 static void
2500 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2501 {
2502   acl_main_t *am = &acl_main;
2503   u32 acl_index;
2504   acl_list_t *acl;
2505   int rv = -1;
2506   vl_api_registration_t *reg;
2507
2508   reg = vl_api_client_index_to_registration (mp->client_index);
2509   if (!reg)
2510     return;
2511
2512   if (mp->acl_index == ~0)
2513     {
2514     /* *INDENT-OFF* */
2515     /* Just dump all ACLs */
2516     pool_foreach (acl, am->acls,
2517     ({
2518       send_acl_details(am, reg, acl, mp->context);
2519     }));
2520     /* *INDENT-ON* */
2521     }
2522   else
2523     {
2524       acl_index = ntohl (mp->acl_index);
2525       if (!pool_is_free_index (am->acls, acl_index))
2526         {
2527           acl = pool_elt_at_index (am->acls, acl_index);
2528           send_acl_details (am, reg, acl, mp->context);
2529         }
2530     }
2531
2532   if (rv == -1)
2533     {
2534       /* FIXME API: should we signal an error here at all ? */
2535       return;
2536     }
2537 }
2538
2539 static void
2540 send_acl_interface_list_details (acl_main_t * am,
2541                                  vl_api_registration_t * reg,
2542                                  u32 sw_if_index, u32 context)
2543 {
2544   vl_api_acl_interface_list_details_t *mp;
2545   int msg_size;
2546   int n_input;
2547   int n_output;
2548   int count;
2549   int i = 0;
2550   void *oldheap = acl_set_heap (am);
2551
2552   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2553   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2554
2555   clib_mem_set_heap (oldheap);
2556
2557   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2558   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2559   count = n_input + n_output;
2560
2561   msg_size = sizeof (*mp);
2562   msg_size += sizeof (mp->acls[0]) * count;
2563
2564   mp = vl_msg_api_alloc (msg_size);
2565   memset (mp, 0, msg_size);
2566   mp->_vl_msg_id =
2567     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2568
2569   /* fill in the message */
2570   mp->context = context;
2571   mp->sw_if_index = htonl (sw_if_index);
2572   mp->count = count;
2573   mp->n_input = n_input;
2574   for (i = 0; i < n_input; i++)
2575     {
2576       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2577     }
2578   for (i = 0; i < n_output; i++)
2579     {
2580       mp->acls[n_input + i] =
2581         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2582     }
2583   vl_api_send_msg (reg, (u8 *) mp);
2584 }
2585
2586 static void
2587 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2588                                           mp)
2589 {
2590   acl_main_t *am = &acl_main;
2591   vnet_sw_interface_t *swif;
2592   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2593
2594   u32 sw_if_index;
2595   vl_api_registration_t *reg;
2596
2597   reg = vl_api_client_index_to_registration (mp->client_index);
2598   if (!reg)
2599     return;
2600
2601   if (mp->sw_if_index == ~0)
2602     {
2603     /* *INDENT-OFF* */
2604     pool_foreach (swif, im->sw_interfaces,
2605     ({
2606       send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2607     }));
2608     /* *INDENT-ON* */
2609     }
2610   else
2611     {
2612       sw_if_index = ntohl (mp->sw_if_index);
2613       if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2614         send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2615     }
2616 }
2617
2618 /* MACIP ACL API handlers */
2619
2620 static void
2621 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2622 {
2623   vl_api_macip_acl_add_reply_t *rmp;
2624   acl_main_t *am = &acl_main;
2625   int rv;
2626   u32 acl_list_index = ~0;
2627   u32 acl_count = ntohl (mp->count);
2628   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2629
2630   if (verify_message_len (mp, expected_len, "macip_acl_add"))
2631     {
2632       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2633     }
2634   else
2635     {
2636       rv = VNET_API_ERROR_INVALID_VALUE;
2637     }
2638
2639   /* *INDENT-OFF* */
2640   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2641   ({
2642     rmp->acl_index = htonl(acl_list_index);
2643   }));
2644   /* *INDENT-ON* */
2645 }
2646
2647 static void
2648 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2649 {
2650   vl_api_macip_acl_add_replace_reply_t *rmp;
2651   acl_main_t *am = &acl_main;
2652   int rv;
2653   u32 acl_list_index = ntohl (mp->acl_index);
2654   u32 acl_count = ntohl (mp->count);
2655   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2656
2657   if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2658     {
2659       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2660     }
2661   else
2662     {
2663       rv = VNET_API_ERROR_INVALID_VALUE;
2664     }
2665
2666   /* *INDENT-OFF* */
2667   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2668   ({
2669     rmp->acl_index = htonl(acl_list_index);
2670   }));
2671   /* *INDENT-ON* */
2672 }
2673
2674 static void
2675 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2676 {
2677   acl_main_t *am = &acl_main;
2678   vl_api_macip_acl_del_reply_t *rmp;
2679   int rv;
2680
2681   rv = macip_acl_del_list (ntohl (mp->acl_index));
2682
2683   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2684 }
2685
2686 static void
2687   vl_api_macip_acl_interface_add_del_t_handler
2688   (vl_api_macip_acl_interface_add_del_t * mp)
2689 {
2690   acl_main_t *am = &acl_main;
2691   vl_api_macip_acl_interface_add_del_reply_t *rmp;
2692   int rv = -1;
2693   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2694   u32 sw_if_index = ntohl (mp->sw_if_index);
2695
2696   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2697     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2698   else
2699     rv =
2700       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2701                                        ntohl (mp->acl_index));
2702
2703   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2704 }
2705
2706 static void
2707 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2708                         macip_acl_list_t * acl, u32 context)
2709 {
2710   vl_api_macip_acl_details_t *mp;
2711   vl_api_macip_acl_rule_t *rules;
2712   macip_acl_rule_t *r;
2713   int i;
2714   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2715
2716   mp = vl_msg_api_alloc (msg_size);
2717   memset (mp, 0, msg_size);
2718   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2719
2720   /* fill in the message */
2721   mp->context = context;
2722   if (acl)
2723     {
2724       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2725       mp->count = htonl (acl->count);
2726       mp->acl_index = htonl (acl - am->macip_acls);
2727       rules = mp->r;
2728       for (i = 0; i < acl->count; i++)
2729         {
2730           r = &acl->rules[i];
2731           rules[i].is_permit = r->is_permit;
2732           rules[i].is_ipv6 = r->is_ipv6;
2733           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2734           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2735                   sizeof (r->src_mac_mask));
2736           if (r->is_ipv6)
2737             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2738                     sizeof (r->src_ip_addr.ip6));
2739           else
2740             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2741                     sizeof (r->src_ip_addr.ip4));
2742           rules[i].src_ip_prefix_len = r->src_prefixlen;
2743         }
2744     }
2745   else
2746     {
2747       /* No martini, no party - no ACL applied to this interface. */
2748       mp->acl_index = ~0;
2749       mp->count = 0;
2750     }
2751
2752   vl_api_send_msg (reg, (u8 *) mp);
2753 }
2754
2755
2756 static void
2757 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2758 {
2759   acl_main_t *am = &acl_main;
2760   macip_acl_list_t *acl;
2761
2762   vl_api_registration_t *reg;
2763
2764   reg = vl_api_client_index_to_registration (mp->client_index);
2765   if (!reg)
2766     return;
2767
2768   if (mp->acl_index == ~0)
2769     {
2770       /* Just dump all ACLs for now, with sw_if_index = ~0 */
2771       pool_foreach (acl, am->macip_acls, (
2772                                            {
2773                                            send_macip_acl_details (am, reg,
2774                                                                    acl,
2775                                                                    mp->context);
2776                                            }
2777                     ));
2778       /* *INDENT-ON* */
2779     }
2780   else
2781     {
2782       u32 acl_index = ntohl (mp->acl_index);
2783       if (!pool_is_free_index (am->macip_acls, acl_index))
2784         {
2785           acl = pool_elt_at_index (am->macip_acls, acl_index);
2786           send_macip_acl_details (am, reg, acl, mp->context);
2787         }
2788     }
2789 }
2790
2791 static void
2792 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2793                                           mp)
2794 {
2795   acl_main_t *am = &acl_main;
2796   vl_api_macip_acl_interface_get_reply_t *rmp;
2797   u32 count = vec_len (am->macip_acl_by_sw_if_index);
2798   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2799   vl_api_registration_t *reg;
2800   int i;
2801
2802   reg = vl_api_client_index_to_registration (mp->client_index);
2803   if (!reg)
2804     return;
2805
2806   rmp = vl_msg_api_alloc (msg_size);
2807   memset (rmp, 0, msg_size);
2808   rmp->_vl_msg_id =
2809     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2810   rmp->context = mp->context;
2811   rmp->count = htonl (count);
2812   for (i = 0; i < count; i++)
2813     {
2814       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2815     }
2816
2817   vl_api_send_msg (reg, (u8 *) rmp);
2818 }
2819
2820 static void
2821 send_macip_acl_interface_list_details (acl_main_t * am,
2822                                        vl_api_registration_t * reg,
2823                                        u32 sw_if_index,
2824                                        u32 acl_index, u32 context)
2825 {
2826   vl_api_macip_acl_interface_list_details_t *rmp;
2827   /* at this time there is only ever 1 mac ip acl per interface */
2828   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2829
2830   rmp = vl_msg_api_alloc (msg_size);
2831   memset (rmp, 0, msg_size);
2832   rmp->_vl_msg_id =
2833     ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2834
2835   /* fill in the message */
2836   rmp->context = context;
2837   rmp->count = 1;
2838   rmp->sw_if_index = htonl (sw_if_index);
2839   rmp->acls[0] = htonl (acl_index);
2840
2841   vl_api_send_msg (reg, (u8 *) rmp);
2842 }
2843
2844 static void
2845   vl_api_macip_acl_interface_list_dump_t_handler
2846   (vl_api_macip_acl_interface_list_dump_t * mp)
2847 {
2848   vl_api_registration_t *reg;
2849   acl_main_t *am = &acl_main;
2850   u32 sw_if_index = ntohl (mp->sw_if_index);
2851
2852   reg = vl_api_client_index_to_registration (mp->client_index);
2853   if (!reg)
2854     return;
2855
2856   if (sw_if_index == ~0)
2857     {
2858       vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2859       {
2860         if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2861           {
2862             send_macip_acl_interface_list_details (am, reg, sw_if_index,
2863                                                    am->macip_acl_by_sw_if_index
2864                                                    [sw_if_index],
2865                                                    mp->context);
2866           }
2867       }
2868     }
2869   else
2870     {
2871       if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2872         {
2873           send_macip_acl_interface_list_details (am, reg, sw_if_index,
2874                                                  am->macip_acl_by_sw_if_index
2875                                                  [sw_if_index], mp->context);
2876         }
2877     }
2878 }
2879
2880 static void
2881   vl_api_acl_interface_set_etype_whitelist_t_handler
2882   (vl_api_acl_interface_set_etype_whitelist_t * mp)
2883 {
2884   acl_main_t *am = &acl_main;
2885   vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2886   int rv = 0;
2887   int i;
2888   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2889   u32 sw_if_index = ntohl (mp->sw_if_index);
2890   u16 *vec_in = 0, *vec_out = 0;
2891   void *oldheap = acl_set_heap (am);
2892
2893   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2894     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2895   else
2896     {
2897       for (i = 0; i < mp->count; i++)
2898         {
2899           if (i < mp->n_input)
2900             vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2901           else
2902             vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2903         }
2904       rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2905     }
2906
2907   clib_mem_set_heap (oldheap);
2908   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2909 }
2910
2911 static void
2912 send_acl_interface_etype_whitelist_details (acl_main_t * am,
2913                                             vl_api_registration_t * reg,
2914                                             u32 sw_if_index, u32 context)
2915 {
2916   vl_api_acl_interface_etype_whitelist_details_t *mp;
2917   int msg_size;
2918   int n_input = 0;
2919   int n_output = 0;
2920   int count = 0;
2921   int i = 0;
2922
2923   u16 *whitelist_in = 0;
2924   u16 *whitelist_out = 0;
2925
2926   if (intf_has_etype_whitelist (am, sw_if_index, 0))
2927     whitelist_out =
2928       vec_elt (am->output_etype_whitelist_by_sw_if_index, sw_if_index);
2929
2930   if (intf_has_etype_whitelist (am, sw_if_index, 1))
2931     whitelist_in =
2932       vec_elt (am->input_etype_whitelist_by_sw_if_index, sw_if_index);
2933
2934   if ((0 == whitelist_in) && (0 == whitelist_out))
2935     return;                     /* nothing to do */
2936
2937   void *oldheap = acl_set_heap (am);
2938
2939   n_input = vec_len (whitelist_in);
2940   n_output = vec_len (whitelist_out);
2941   count = n_input + n_output;
2942
2943   msg_size = sizeof (*mp);
2944   msg_size += sizeof (mp->whitelist[0]) * count;
2945
2946   mp = vl_msg_api_alloc (msg_size);
2947   memset (mp, 0, msg_size);
2948   mp->_vl_msg_id =
2949     ntohs (VL_API_ACL_INTERFACE_ETYPE_WHITELIST_DETAILS + am->msg_id_base);
2950
2951   /* fill in the message */
2952   mp->context = context;
2953   mp->sw_if_index = htonl (sw_if_index);
2954   mp->count = count;
2955   mp->n_input = n_input;
2956   for (i = 0; i < n_input; i++)
2957     {
2958       mp->whitelist[i] = htons (whitelist_in[i]);
2959     }
2960   for (i = 0; i < n_output; i++)
2961     {
2962       mp->whitelist[n_input + i] = htons (whitelist_out[i]);
2963     }
2964   clib_mem_set_heap (oldheap);
2965   vl_api_send_msg (reg, (u8 *) mp);
2966 }
2967
2968
2969 static void
2970   vl_api_acl_interface_etype_whitelist_dump_t_handler
2971   (vl_api_acl_interface_list_dump_t * mp)
2972 {
2973   acl_main_t *am = &acl_main;
2974   vnet_sw_interface_t *swif;
2975   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2976
2977   u32 sw_if_index;
2978   vl_api_registration_t *reg;
2979
2980   reg = vl_api_client_index_to_registration (mp->client_index);
2981   if (!reg)
2982     return;
2983
2984   if (mp->sw_if_index == ~0)
2985     {
2986     /* *INDENT-OFF* */
2987     pool_foreach (swif, im->sw_interfaces,
2988     ({
2989       send_acl_interface_etype_whitelist_details(am, reg, swif->sw_if_index, mp->context);
2990     }));
2991     /* *INDENT-ON* */
2992     }
2993   else
2994     {
2995       sw_if_index = ntohl (mp->sw_if_index);
2996       if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2997         send_acl_interface_etype_whitelist_details (am, reg, sw_if_index,
2998                                                     mp->context);
2999     }
3000 }
3001
3002
3003
3004 /* Set up the API message handling tables */
3005 static clib_error_t *
3006 acl_plugin_api_hookup (vlib_main_t * vm)
3007 {
3008   acl_main_t *am = &acl_main;
3009 #define _(N,n)                                                  \
3010     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
3011                            #n,                                  \
3012                            vl_api_##n##_t_handler,              \
3013                            vl_noop_handler,                     \
3014                            vl_api_##n##_t_endian,               \
3015                            vl_api_##n##_t_print,                \
3016                            sizeof(vl_api_##n##_t), 1);
3017   foreach_acl_plugin_api_msg;
3018 #undef _
3019
3020   return 0;
3021 }
3022
3023 #define vl_msg_name_crc_list
3024 #include <acl/acl_all_api_h.h>
3025 #undef vl_msg_name_crc_list
3026
3027 static void
3028 setup_message_id_table (acl_main_t * am, api_main_t * apim)
3029 {
3030 #define _(id,n,crc) \
3031   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
3032   foreach_vl_msg_name_crc_acl;
3033 #undef _
3034 }
3035
3036 static void
3037 acl_setup_fa_nodes (void)
3038 {
3039   vlib_main_t *vm = vlib_get_main ();
3040   acl_main_t *am = &acl_main;
3041   vlib_node_t *n, *n4, *n6;
3042
3043   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
3044   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
3045   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
3046
3047
3048   am->l2_input_classify_next_acl_ip4 =
3049     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3050   am->l2_input_classify_next_acl_ip6 =
3051     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3052
3053   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
3054                                l2input_get_feat_names (),
3055                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
3056
3057   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
3058                                l2input_get_feat_names (),
3059                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
3060
3061
3062   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
3063   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
3064   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
3065
3066   am->l2_output_classify_next_acl_ip4 =
3067     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
3068   am->l2_output_classify_next_acl_ip6 =
3069     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
3070
3071   feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
3072                                l2output_get_feat_names (),
3073                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
3074
3075   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
3076                                l2output_get_feat_names (),
3077                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
3078 }
3079
3080 static void
3081 acl_set_timeout_sec (int timeout_type, u32 value)
3082 {
3083   acl_main_t *am = &acl_main;
3084   clib_time_t *ct = &am->vlib_main->clib_time;
3085
3086   if (timeout_type < ACL_N_TIMEOUTS)
3087     {
3088       am->session_timeout_sec[timeout_type] = value;
3089     }
3090   else
3091     {
3092       clib_warning ("Unknown timeout type %d", timeout_type);
3093       return;
3094     }
3095   am->session_timeout[timeout_type] =
3096     (u64) (((f64) value) / ct->seconds_per_clock);
3097 }
3098
3099 static void
3100 acl_set_session_max_entries (u32 value)
3101 {
3102   acl_main_t *am = &acl_main;
3103   am->fa_conn_table_max_entries = value;
3104 }
3105
3106 static int
3107 acl_set_skip_ipv6_eh (u32 eh, u32 value)
3108 {
3109   acl_main_t *am = &acl_main;
3110
3111   if ((eh < 256) && (value < 2))
3112     {
3113       am->fa_ipv6_known_eh_bitmap =
3114         clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
3115       return 1;
3116     }
3117   else
3118     return 0;
3119 }
3120
3121
3122 static clib_error_t *
3123 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
3124 {
3125   acl_main_t *am = &acl_main;
3126   if (0 == am->acl_mheap)
3127     {
3128       /* ACL heap is not initialized, so definitely nothing to do. */
3129       return 0;
3130     }
3131   if (0 == is_add)
3132     {
3133       int may_clear_sessions = 1;
3134       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3135                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
3136                                  sw_if_index);
3137       /* also unapply any ACLs in case the users did not do so. */
3138       macip_acl_interface_del_acl (am, sw_if_index);
3139       acl_interface_reset_inout_acls (sw_if_index, 0, &may_clear_sessions);
3140       acl_interface_reset_inout_acls (sw_if_index, 1, &may_clear_sessions);
3141     }
3142   return 0;
3143 }
3144
3145 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
3146
3147
3148
3149 static clib_error_t *
3150 acl_set_aclplugin_fn (vlib_main_t * vm,
3151                       unformat_input_t * input, vlib_cli_command_t * cmd)
3152 {
3153   clib_error_t *error = 0;
3154   u32 timeout = 0;
3155   u32 val = 0;
3156   u32 eh_val = 0;
3157   uword memory_size = 0;
3158   acl_main_t *am = &acl_main;
3159
3160   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
3161     {
3162       if (!acl_set_skip_ipv6_eh (eh_val, val))
3163         {
3164           error = clib_error_return (0, "expecting eh=0..255, value=0..1");
3165         }
3166       goto done;
3167     }
3168   if (unformat (input, "use-hash-acl-matching %u", &val))
3169     {
3170       am->use_hash_acl_matching = (val != 0);
3171       goto done;
3172     }
3173   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
3174     {
3175       am->l4_match_nonfirst_fragment = (val != 0);
3176       goto done;
3177     }
3178   if (unformat (input, "event-trace"))
3179     {
3180       if (!unformat (input, "%u", &val))
3181         {
3182           error = clib_error_return (0,
3183                                      "expecting trace level, got `%U`",
3184                                      format_unformat_error, input);
3185           goto done;
3186         }
3187       else
3188         {
3189           am->trace_acl = val;
3190           goto done;
3191         }
3192     }
3193   if (unformat (input, "heap"))
3194     {
3195       if (unformat (input, "main"))
3196         {
3197           if (unformat (input, "validate %u", &val))
3198             acl_plugin_acl_set_validate_heap (am, val);
3199           else if (unformat (input, "trace %u", &val))
3200             acl_plugin_acl_set_trace_heap (am, val);
3201           goto done;
3202         }
3203       else if (unformat (input, "hash"))
3204         {
3205           if (unformat (input, "validate %u", &val))
3206             acl_plugin_hash_acl_set_validate_heap (val);
3207           else if (unformat (input, "trace %u", &val))
3208             acl_plugin_hash_acl_set_trace_heap (val);
3209           goto done;
3210         }
3211       goto done;
3212     }
3213   if (unformat (input, "session"))
3214     {
3215       if (unformat (input, "table"))
3216         {
3217           /* The commands here are for tuning/testing. No user-serviceable parts inside */
3218           if (unformat (input, "max-entries"))
3219             {
3220               if (!unformat (input, "%u", &val))
3221                 {
3222                   error = clib_error_return (0,
3223                                              "expecting maximum number of entries, got `%U`",
3224                                              format_unformat_error, input);
3225                   goto done;
3226                 }
3227               else
3228                 {
3229                   acl_set_session_max_entries (val);
3230                   goto done;
3231                 }
3232             }
3233           if (unformat (input, "hash-table-buckets"))
3234             {
3235               if (!unformat (input, "%u", &val))
3236                 {
3237                   error = clib_error_return (0,
3238                                              "expecting maximum number of hash table buckets, got `%U`",
3239                                              format_unformat_error, input);
3240                   goto done;
3241                 }
3242               else
3243                 {
3244                   am->fa_conn_table_hash_num_buckets = val;
3245                   goto done;
3246                 }
3247             }
3248           if (unformat (input, "hash-table-memory"))
3249             {
3250               if (!unformat (input, "%U", unformat_memory_size, &memory_size))
3251                 {
3252                   error = clib_error_return (0,
3253                                              "expecting maximum amount of hash table memory, got `%U`",
3254                                              format_unformat_error, input);
3255                   goto done;
3256                 }
3257               else
3258                 {
3259                   am->fa_conn_table_hash_memory_size = memory_size;
3260                   goto done;
3261                 }
3262             }
3263           if (unformat (input, "event-trace"))
3264             {
3265               if (!unformat (input, "%u", &val))
3266                 {
3267                   error = clib_error_return (0,
3268                                              "expecting trace level, got `%U`",
3269                                              format_unformat_error, input);
3270                   goto done;
3271                 }
3272               else
3273                 {
3274                   am->trace_sessions = val;
3275                   goto done;
3276                 }
3277             }
3278           goto done;
3279         }
3280       if (unformat (input, "timeout"))
3281         {
3282           if (unformat (input, "udp"))
3283             {
3284               if (unformat (input, "idle"))
3285                 {
3286                   if (!unformat (input, "%u", &timeout))
3287                     {
3288                       error = clib_error_return (0,
3289                                                  "expecting timeout value in seconds, got `%U`",
3290                                                  format_unformat_error,
3291                                                  input);
3292                       goto done;
3293                     }
3294                   else
3295                     {
3296                       acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
3297                       goto done;
3298                     }
3299                 }
3300             }
3301           if (unformat (input, "tcp"))
3302             {
3303               if (unformat (input, "idle"))
3304                 {
3305                   if (!unformat (input, "%u", &timeout))
3306                     {
3307                       error = clib_error_return (0,
3308                                                  "expecting timeout value in seconds, got `%U`",
3309                                                  format_unformat_error,
3310                                                  input);
3311                       goto done;
3312                     }
3313                   else
3314                     {
3315                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
3316                       goto done;
3317                     }
3318                 }
3319               if (unformat (input, "transient"))
3320                 {
3321                   if (!unformat (input, "%u", &timeout))
3322                     {
3323                       error = clib_error_return (0,
3324                                                  "expecting timeout value in seconds, got `%U`",
3325                                                  format_unformat_error,
3326                                                  input);
3327                       goto done;
3328                     }
3329                   else
3330                     {
3331                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
3332                                            timeout);
3333                       goto done;
3334                     }
3335                 }
3336             }
3337           goto done;
3338         }
3339     }
3340 done:
3341   return error;
3342 }
3343
3344 static u8 *
3345 my_format_mac_address (u8 * s, va_list * args)
3346 {
3347   u8 *a = va_arg (*args, u8 *);
3348   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3349                  a[0], a[1], a[2], a[3], a[4], a[5]);
3350 }
3351
3352 static inline u8 *
3353 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3354 {
3355   macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3356
3357   out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3358                 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3359                 format_ip46_address, &a->src_ip_addr,
3360                 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3361                 a->src_prefixlen,
3362                 my_format_mac_address, a->src_mac,
3363                 my_format_mac_address, a->src_mac_mask);
3364   return (out);
3365 }
3366
3367 static void
3368 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3369 {
3370   vlib_main_t *vm = am->vlib_main;
3371   int i;
3372
3373   /* Don't try to print someone else's memory */
3374   if (macip_acl_index > vec_len (am->macip_acls))
3375     return;
3376
3377   macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3378   int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3379
3380   vlib_cli_output (vm,
3381                    "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3382                    macip_acl_index, a->count, vec_len (a->rules), a->tag,
3383                    free_pool_slot);
3384   vlib_cli_output (vm,
3385                    "  ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3386                    a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3387   vlib_cli_output (vm,
3388                    "  out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3389                    a->out_ip4_table_index, a->out_ip6_table_index,
3390                    a->out_l2_table_index);
3391   for (i = 0; i < vec_len (a->rules); i++)
3392     vlib_cli_output (vm, "    rule %d: %U\n", i,
3393                      my_macip_acl_rule_t_pretty_format,
3394                      vec_elt_at_index (a->rules, i));
3395
3396 }
3397
3398 static clib_error_t *
3399 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3400                                  unformat_input_t *
3401                                  input, vlib_cli_command_t * cmd)
3402 {
3403   clib_error_t *error = 0;
3404   acl_main_t *am = &acl_main;
3405   int i;
3406   u32 acl_index = ~0;
3407
3408   (void) unformat (input, "index %u", &acl_index);
3409
3410   for (i = 0; i < vec_len (am->macip_acls); i++)
3411     {
3412       /* Don't attempt to show the ACLs that do not exist */
3413       if (pool_is_free_index (am->macip_acls, i))
3414         continue;
3415
3416       if ((acl_index != ~0) && (acl_index != i))
3417         {
3418           continue;
3419         }
3420
3421       macip_acl_print (am, i);
3422       if (i < vec_len (am->sw_if_index_vec_by_macip_acl))
3423         {
3424           vlib_cli_output (vm, "  applied on sw_if_index(s): %U\n",
3425                            format_vec32,
3426                            vec_elt (am->sw_if_index_vec_by_macip_acl, i),
3427                            "%d");
3428         }
3429     }
3430
3431   return error;
3432 }
3433
3434 static clib_error_t *
3435 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3436                                        unformat_input_t *
3437                                        input, vlib_cli_command_t * cmd)
3438 {
3439   clib_error_t *error = 0;
3440   acl_main_t *am = &acl_main;
3441   int i;
3442   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3443     {
3444       vlib_cli_output (vm, "  sw_if_index %d: %d\n", i,
3445                        vec_elt (am->macip_acl_by_sw_if_index, i));
3446     }
3447   return error;
3448 }
3449
3450 static void
3451 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3452 {
3453   u32 i;
3454   vlib_main_t *vm = am->vlib_main;
3455
3456   for (i = 0; i < vec_len (am->acls); i++)
3457     {
3458       if (acl_is_not_defined (am, i))
3459         {
3460           /* don't attempt to show the ACLs that do not exist */
3461           continue;
3462         }
3463       if ((acl_index != ~0) && (acl_index != i))
3464         {
3465           continue;
3466         }
3467       acl_print_acl (vm, am, i);
3468
3469       if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3470         {
3471           vlib_cli_output (vm, "  applied inbound on sw_if_index: %U\n",
3472                            format_vec32, am->input_sw_if_index_vec_by_acl[i],
3473                            "%d");
3474         }
3475       if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3476         {
3477           vlib_cli_output (vm, "  applied outbound on sw_if_index: %U\n",
3478                            format_vec32, am->output_sw_if_index_vec_by_acl[i],
3479                            "%d");
3480         }
3481       if (i < vec_len (am->lc_index_vec_by_acl))
3482         {
3483           vlib_cli_output (vm, "  used in lookup context index: %U\n",
3484                            format_vec32, am->lc_index_vec_by_acl[i], "%d");
3485         }
3486     }
3487 }
3488
3489 static clib_error_t *
3490 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3491                            unformat_input_t * input, vlib_cli_command_t * cmd)
3492 {
3493   clib_error_t *error = 0;
3494   acl_main_t *am = &acl_main;
3495
3496   u32 acl_index = ~0;
3497   (void) unformat (input, "index %u", &acl_index);
3498
3499   acl_plugin_show_acl (am, acl_index);
3500   return error;
3501 }
3502
3503 static clib_error_t *
3504 acl_show_aclplugin_lookup_context_fn (vlib_main_t * vm,
3505                                       unformat_input_t * input,
3506                                       vlib_cli_command_t * cmd)
3507 {
3508   clib_error_t *error = 0;
3509
3510   u32 lc_index = ~0;
3511   (void) unformat (input, "index %u", &lc_index);
3512
3513   acl_plugin_show_lookup_context (lc_index);
3514   return error;
3515 }
3516
3517 static clib_error_t *
3518 acl_show_aclplugin_lookup_user_fn (vlib_main_t * vm,
3519                                    unformat_input_t * input,
3520                                    vlib_cli_command_t * cmd)
3521 {
3522   clib_error_t *error = 0;
3523
3524   u32 lc_index = ~0;
3525   (void) unformat (input, "index %u", &lc_index);
3526
3527   acl_plugin_show_lookup_user (lc_index);
3528   return error;
3529 }
3530
3531
3532 static void
3533 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl,
3534                            int detail)
3535 {
3536   vlib_main_t *vm = am->vlib_main;
3537   u32 swi;
3538   u32 *pj;
3539   for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3540        (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3541     {
3542       /* if we need a particular interface, skip all the others */
3543       if ((sw_if_index != ~0) && (sw_if_index != swi))
3544         continue;
3545
3546       vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3547
3548       if (intf_has_etype_whitelist (am, swi, 1))
3549         {
3550           vlib_cli_output (vm, "  input etype whitelist: %U", format_vec16,
3551                            am->input_etype_whitelist_by_sw_if_index[swi],
3552                            "%04x");
3553         }
3554       if (intf_has_etype_whitelist (am, swi, 0))
3555         {
3556           vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3557                            am->output_etype_whitelist_by_sw_if_index[swi],
3558                            "%04x");
3559         }
3560
3561       if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3562           (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3563         {
3564           vlib_cli_output (vm, "  input acl(s): %U", format_vec32,
3565                            am->input_acl_vec_by_sw_if_index[swi], "%d");
3566           if (show_acl)
3567             {
3568               vlib_cli_output (vm, "\n");
3569               vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3570               {
3571                 acl_print_acl (vm, am, *pj);
3572               }
3573               vlib_cli_output (vm, "\n");
3574             }
3575         }
3576
3577       if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3578           (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3579         {
3580           vlib_cli_output (vm, "  output acl(s): %U", format_vec32,
3581                            am->output_acl_vec_by_sw_if_index[swi], "%d");
3582           if (show_acl)
3583             {
3584               vlib_cli_output (vm, "\n");
3585               vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3586               {
3587                 acl_print_acl (vm, am, *pj);
3588               }
3589               vlib_cli_output (vm, "\n");
3590             }
3591         }
3592       if (detail && (swi < vec_len (am->input_lc_index_by_sw_if_index)))
3593         {
3594           vlib_cli_output (vm, "   input lookup context index: %d",
3595                            am->input_lc_index_by_sw_if_index[swi]);
3596         }
3597       if (detail && (swi < vec_len (am->output_lc_index_by_sw_if_index)))
3598         {
3599           vlib_cli_output (vm, "  output lookup context index: %d",
3600                            am->output_lc_index_by_sw_if_index[swi]);
3601         }
3602     }
3603
3604 }
3605
3606
3607 static clib_error_t *
3608 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3609                                      unformat_input_t * input,
3610                                      vlib_cli_command_t * cmd)
3611 {
3612   clib_error_t *error = 0;
3613   u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3614
3615   if (unformat
3616       (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3617        &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3618     vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3619                      format_acl_plugin_5tuple, five_tuple);
3620   else
3621     error = clib_error_return (0, "expecting 6 hex integers");
3622   return error;
3623 }
3624
3625
3626 static clib_error_t *
3627 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3628                                  unformat_input_t *
3629                                  input, vlib_cli_command_t * cmd)
3630 {
3631   clib_error_t *error = 0;
3632   acl_main_t *am = &acl_main;
3633
3634   u32 sw_if_index = ~0;
3635   (void) unformat (input, "sw_if_index %u", &sw_if_index);
3636   int show_acl = unformat (input, "acl");
3637   int detail = unformat (input, "detail");
3638
3639   acl_plugin_show_interface (am, sw_if_index, show_acl, detail);
3640   return error;
3641 }
3642
3643 static clib_error_t *
3644 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3645                               unformat_input_t * input,
3646                               vlib_cli_command_t * cmd)
3647 {
3648   clib_error_t *error = 0;
3649   acl_main_t *am = &acl_main;
3650
3651   vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3652   if (am->acl_mheap)
3653     {
3654       vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3655     }
3656   else
3657     {
3658       vlib_cli_output (vm, " Not initialized\n");
3659     }
3660   vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3661   if (am->hash_lookup_mheap)
3662     {
3663       vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3664     }
3665   else
3666     {
3667       vlib_cli_output (vm, " Not initialized\n");
3668     }
3669   return error;
3670 }
3671
3672 static void
3673 acl_plugin_show_sessions (acl_main_t * am,
3674                           u32 show_session_thread_id,
3675                           u32 show_session_session_index)
3676 {
3677   vlib_main_t *vm = am->vlib_main;
3678   u16 wk;
3679   vnet_interface_main_t *im = &am->vnet_main->interface_main;
3680   vnet_sw_interface_t *swif;
3681
3682   {
3683     u64 n_adds = am->fa_session_total_adds;
3684     u64 n_dels = am->fa_session_total_dels;
3685     vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3686                      n_dels, n_adds - n_dels);
3687   }
3688   vlib_cli_output (vm, "\n\nPer-thread data:");
3689   for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3690     {
3691       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3692       vlib_cli_output (vm, "Thread #%d:", wk);
3693       if (show_session_thread_id == wk
3694           && show_session_session_index < pool_len (pw->fa_sessions_pool))
3695         {
3696           vlib_cli_output (vm, "  session index %u:",
3697                            show_session_session_index);
3698           fa_session_t *sess =
3699             pw->fa_sessions_pool + show_session_session_index;
3700           u64 *m = (u64 *) & sess->info;
3701           vlib_cli_output (vm,
3702                            "    info: %016llx %016llx %016llx %016llx %016llx %016llx",
3703                            m[0], m[1], m[2], m[3], m[4], m[5]);
3704           vlib_cli_output (vm, "    sw_if_index: %u", sess->sw_if_index);
3705           vlib_cli_output (vm, "    tcp_flags_seen: %x",
3706                            sess->tcp_flags_seen.as_u16);
3707           vlib_cli_output (vm, "    last active time: %lu",
3708                            sess->last_active_time);
3709           vlib_cli_output (vm, "    thread index: %u", sess->thread_index);
3710           vlib_cli_output (vm, "    link enqueue time: %lu",
3711                            sess->link_enqueue_time);
3712           vlib_cli_output (vm, "    link next index: %u",
3713                            sess->link_next_idx);
3714           vlib_cli_output (vm, "    link prev index: %u",
3715                            sess->link_prev_idx);
3716           vlib_cli_output (vm, "    link list id: %u", sess->link_list_id);
3717         }
3718       vlib_cli_output (vm, "  connection add/del stats:", wk);
3719       pool_foreach (swif, im->sw_interfaces, (
3720                                                {
3721                                                u32 sw_if_index =
3722                                                swif->sw_if_index;
3723                                                u64 n_adds =
3724                                                sw_if_index <
3725                                                vec_len
3726                                                (pw->fa_session_adds_by_sw_if_index)
3727                                                ?
3728                                                pw->fa_session_adds_by_sw_if_index
3729                                                [sw_if_index] : 0;
3730                                                u64 n_dels =
3731                                                sw_if_index <
3732                                                vec_len
3733                                                (pw->fa_session_dels_by_sw_if_index)
3734                                                ?
3735                                                pw->fa_session_dels_by_sw_if_index
3736                                                [sw_if_index] : 0;
3737                                                vlib_cli_output (vm,
3738                                                                 "    sw_if_index %d: add %lu - del %lu = %lu",
3739                                                                 sw_if_index,
3740                                                                 n_adds,
3741                                                                 n_dels,
3742                                                                 n_adds -
3743                                                                 n_dels);
3744                                                }
3745                     ));
3746
3747       vlib_cli_output (vm, "  connection timeout type lists:", wk);
3748       u8 tt = 0;
3749       for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3750         {
3751           u32 head_session_index = pw->fa_conn_list_head[tt];
3752           vlib_cli_output (vm, "  fa_conn_list_head[%d]: %d", tt,
3753                            head_session_index);
3754           if (~0 != head_session_index)
3755             {
3756               fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3757               vlib_cli_output (vm, "    last active time: %lu",
3758                                sess->last_active_time);
3759               vlib_cli_output (vm, "    link enqueue time: %lu",
3760                                sess->link_enqueue_time);
3761             }
3762         }
3763
3764       vlib_cli_output (vm, "  Next expiry time: %lu", pw->next_expiry_time);
3765       vlib_cli_output (vm, "  Requeue until time: %lu",
3766                        pw->requeue_until_time);
3767       vlib_cli_output (vm, "  Current time wait interval: %lu",
3768                        pw->current_time_wait_interval);
3769       vlib_cli_output (vm, "  Count of deleted sessions: %lu",
3770                        pw->cnt_deleted_sessions);
3771       vlib_cli_output (vm, "  Delete already deleted: %lu",
3772                        pw->cnt_already_deleted_sessions);
3773       vlib_cli_output (vm, "  Session timers restarted: %lu",
3774                        pw->cnt_session_timer_restarted);
3775       vlib_cli_output (vm, "  Swipe until this time: %lu",
3776                        pw->swipe_end_time);
3777       vlib_cli_output (vm, "  sw_if_index serviced bitmap: %U",
3778                        format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3779       vlib_cli_output (vm, "  pending clear intfc bitmap : %U",
3780                        format_bitmap_hex,
3781                        pw->pending_clear_sw_if_index_bitmap);
3782       vlib_cli_output (vm, "  clear in progress: %u", pw->clear_in_process);
3783       vlib_cli_output (vm, "  interrupt is pending: %d",
3784                        pw->interrupt_is_pending);
3785       vlib_cli_output (vm, "  interrupt is needed: %d",
3786                        pw->interrupt_is_needed);
3787       vlib_cli_output (vm, "  interrupt is unwanted: %d",
3788                        pw->interrupt_is_unwanted);
3789       vlib_cli_output (vm, "  interrupt generation: %d",
3790                        pw->interrupt_generation);
3791     }
3792   vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3793 #define _(cnt, desc) vlib_cli_output(vm, "             %20lu: %s", am->cnt, desc);
3794   foreach_fa_cleaner_counter;
3795 #undef _
3796   vlib_cli_output (vm, "Interrupt generation: %d",
3797                    am->fa_interrupt_generation);
3798   vlib_cli_output (vm,
3799                    "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3800                    am->fa_min_deleted_sessions_per_interval,
3801                    am->fa_max_deleted_sessions_per_interval,
3802                    am->fa_cleaner_wait_time_increment * 1000.0,
3803                    ((f64) am->fa_current_cleaner_timer_wait_interval) *
3804                    1000.0 / (f64) vm->clib_time.clocks_per_second);
3805 }
3806
3807 static clib_error_t *
3808 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3809                                 unformat_input_t * input,
3810                                 vlib_cli_command_t * cmd)
3811 {
3812   clib_error_t *error = 0;
3813   acl_main_t *am = &acl_main;
3814
3815   u32 show_bihash_verbose = 0;
3816   u32 show_session_thread_id = ~0;
3817   u32 show_session_session_index = ~0;
3818   (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3819                    &show_session_session_index);
3820   (void) unformat (input, "verbose %u", &show_bihash_verbose);
3821
3822   acl_plugin_show_sessions (am, show_session_thread_id,
3823                             show_session_session_index);
3824   show_fa_sessions_hash (vm, show_bihash_verbose);
3825   return error;
3826 }
3827
3828 static clib_error_t *
3829 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3830                               unformat_input_t * input,
3831                               vlib_cli_command_t * cmd)
3832 {
3833   clib_error_t *error = 0;
3834
3835   u32 acl_index = ~0;
3836   u32 sw_if_index = ~0;
3837   int show_acl_hash_info = 0;
3838   int show_applied_info = 0;
3839   int show_mask_type = 0;
3840   int show_bihash = 0;
3841   u32 show_bihash_verbose = 0;
3842
3843   if (unformat (input, "acl"))
3844     {
3845       show_acl_hash_info = 1;
3846       /* mask-type is handy to see as well right there */
3847       show_mask_type = 1;
3848       unformat (input, "index %u", &acl_index);
3849     }
3850   else if (unformat (input, "applied"))
3851     {
3852       show_applied_info = 1;
3853       unformat (input, "sw_if_index %u", &sw_if_index);
3854     }
3855   else if (unformat (input, "mask"))
3856     {
3857       show_mask_type = 1;
3858     }
3859   else if (unformat (input, "hash"))
3860     {
3861       show_bihash = 1;
3862       unformat (input, "verbose %u", &show_bihash_verbose);
3863     }
3864
3865   if (!
3866       (show_mask_type || show_acl_hash_info || show_applied_info
3867        || show_bihash))
3868     {
3869       /* if no qualifiers specified, show all */
3870       show_mask_type = 1;
3871       show_acl_hash_info = 1;
3872       show_applied_info = 1;
3873       show_bihash = 1;
3874     }
3875   if (show_mask_type)
3876     acl_plugin_show_tables_mask_type ();
3877   if (show_acl_hash_info)
3878     acl_plugin_show_tables_acl_hash_info (acl_index);
3879   if (show_applied_info)
3880     acl_plugin_show_tables_applied_info (sw_if_index);
3881   if (show_bihash)
3882     acl_plugin_show_tables_bihash (show_bihash_verbose);
3883
3884   return error;
3885 }
3886
3887 static clib_error_t *
3888 acl_clear_aclplugin_fn (vlib_main_t * vm,
3889                         unformat_input_t * input, vlib_cli_command_t * cmd)
3890 {
3891   clib_error_t *error = 0;
3892   acl_main_t *am = &acl_main;
3893   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3894                              ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3895   return error;
3896 }
3897
3898  /* *INDENT-OFF* */
3899 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3900     .path = "set acl-plugin",
3901     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3902     .function = acl_set_aclplugin_fn,
3903 };
3904
3905 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3906     .path = "show acl-plugin acl",
3907     .short_help = "show acl-plugin acl [index N]",
3908     .function = acl_show_aclplugin_acl_fn,
3909 };
3910
3911 VLIB_CLI_COMMAND (aclplugin_show_lookup_context_command, static) = {
3912     .path = "show acl-plugin lookup context",
3913     .short_help = "show acl-plugin lookup context [index N]",
3914     .function = acl_show_aclplugin_lookup_context_fn,
3915 };
3916
3917 VLIB_CLI_COMMAND (aclplugin_show_lookup_user_command, static) = {
3918     .path = "show acl-plugin lookup user",
3919     .short_help = "show acl-plugin lookup user [index N]",
3920     .function = acl_show_aclplugin_lookup_user_fn,
3921 };
3922
3923 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3924     .path = "show acl-plugin decode 5tuple",
3925     .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3926     .function = acl_show_aclplugin_decode_5tuple_fn,
3927 };
3928
3929 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3930     .path = "show acl-plugin interface",
3931     .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3932     .function = acl_show_aclplugin_interface_fn,
3933 };
3934
3935 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3936     .path = "show acl-plugin memory",
3937     .short_help = "show acl-plugin memory",
3938     .function = acl_show_aclplugin_memory_fn,
3939 };
3940
3941 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3942     .path = "show acl-plugin sessions",
3943     .short_help = "show acl-plugin sessions",
3944     .function = acl_show_aclplugin_sessions_fn,
3945 };
3946
3947 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3948     .path = "show acl-plugin tables",
3949     .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3950     .function = acl_show_aclplugin_tables_fn,
3951 };
3952
3953 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3954     .path = "show acl-plugin macip acl",
3955     .short_help = "show acl-plugin macip acl [index N]",
3956     .function = acl_show_aclplugin_macip_acl_fn,
3957 };
3958
3959 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3960     .path = "show acl-plugin macip interface",
3961     .short_help = "show acl-plugin macip interface",
3962     .function = acl_show_aclplugin_macip_interface_fn,
3963 };
3964
3965 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3966     .path = "clear acl-plugin sessions",
3967     .short_help = "clear acl-plugin sessions",
3968     .function = acl_clear_aclplugin_fn,
3969 };
3970 /* *INDENT-ON* */
3971
3972 static clib_error_t *
3973 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3974 {
3975   acl_main_t *am = &acl_main;
3976   u32 conn_table_hash_buckets;
3977   u32 conn_table_hash_memory_size;
3978   u32 conn_table_max_entries;
3979   u32 main_heap_size;
3980   u32 hash_heap_size;
3981   u32 hash_lookup_hash_buckets;
3982   u32 hash_lookup_hash_memory;
3983
3984   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3985     {
3986       if (unformat
3987           (input, "connection hash buckets %d", &conn_table_hash_buckets))
3988         am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3989       else if (unformat (input, "connection hash memory %d",
3990                          &conn_table_hash_memory_size))
3991         am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3992       else if (unformat (input, "connection count max %d",
3993                          &conn_table_max_entries))
3994         am->fa_conn_table_max_entries = conn_table_max_entries;
3995       else if (unformat (input, "main heap size %d", &main_heap_size))
3996         am->acl_mheap_size = main_heap_size;
3997       else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3998         am->hash_lookup_mheap_size = hash_heap_size;
3999       else if (unformat (input, "hash lookup hash buckets %d",
4000                          &hash_lookup_hash_buckets))
4001         am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
4002       else if (unformat (input, "hash lookup hash memory %d",
4003                          &hash_lookup_hash_memory))
4004         am->hash_lookup_hash_memory = hash_lookup_hash_memory;
4005       else
4006         return clib_error_return (0, "unknown input '%U'",
4007                                   format_unformat_error, input);
4008     }
4009   return 0;
4010 }
4011
4012 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
4013
4014 static clib_error_t *
4015 acl_init (vlib_main_t * vm)
4016 {
4017   acl_main_t *am = &acl_main;
4018   clib_error_t *error = 0;
4019   memset (am, 0, sizeof (*am));
4020   am->vlib_main = vm;
4021   am->vnet_main = vnet_get_main ();
4022
4023   u8 *name = format (0, "acl_%08x%c", api_version, 0);
4024
4025   /* Ask for a correctly-sized block of API message decode slots */
4026   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
4027                                             VL_MSG_FIRST_AVAILABLE);
4028
4029   error = acl_plugin_api_hookup (vm);
4030
4031   /* Add our API messages to the global name_crc hash table */
4032   setup_message_id_table (am, &api_main);
4033
4034   vec_free (name);
4035
4036   acl_setup_fa_nodes ();
4037
4038   am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
4039   am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
4040
4041   am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
4042   am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
4043
4044   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
4045     TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
4046   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
4047     TCP_SESSION_IDLE_TIMEOUT_SEC;
4048   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
4049     UDP_SESSION_IDLE_TIMEOUT_SEC;
4050
4051   am->fa_conn_table_hash_num_buckets =
4052     ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
4053   am->fa_conn_table_hash_memory_size =
4054     ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
4055   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
4056   vlib_thread_main_t *tm = vlib_get_thread_main ();
4057   vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
4058   {
4059     u16 wk;
4060     u8 tt;
4061     for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
4062       {
4063         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
4064         vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
4065         vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
4066         for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
4067           {
4068             pw->fa_conn_list_head[tt] = ~0;
4069             pw->fa_conn_list_tail[tt] = ~0;
4070           }
4071       }
4072   }
4073
4074   am->fa_min_deleted_sessions_per_interval =
4075     ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
4076   am->fa_max_deleted_sessions_per_interval =
4077     ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
4078   am->fa_cleaner_wait_time_increment =
4079     ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
4080
4081   am->fa_cleaner_cnt_delete_by_sw_index = 0;
4082   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
4083   am->fa_cleaner_cnt_unknown_event = 0;
4084   am->fa_cleaner_cnt_timer_restarted = 0;
4085   am->fa_cleaner_cnt_wait_with_timeout = 0;
4086
4087
4088 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
4089   foreach_acl_eh
4090 #undef _
4091     am->l4_match_nonfirst_fragment = 1;
4092
4093   /* use the new fancy hash-based matching */
4094   am->use_hash_acl_matching = 1;
4095
4096   am->interface_acl_user_id = ~0;       /* defer till the first use */
4097
4098   return error;
4099 }
4100
4101 VLIB_INIT_FUNCTION (acl_init);
4102
4103
4104 /*
4105  * fd.io coding-style-patch-verification: ON
4106  *
4107  * Local Variables:
4108  * eval: (c-set-style "gnu")
4109  * End:
4110  */