acl-plugin: fix a misplaced return (VPP-910)
[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/input_acl.h>
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29
30 /* define message IDs */
31 #include <acl/acl_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <acl/acl_all_api_h.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <acl/acl_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <acl/acl_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.h>
52 #undef vl_api_version
53
54 #include "fa_node.h"
55 #include "hash_lookup.h"
56
57 acl_main_t acl_main;
58
59 #define REPLY_MSG_ID_BASE am->msg_id_base
60 #include <vlibapi/api_helper_macros.h>
61
62 /* List of message types that this plugin understands */
63
64 #define foreach_acl_plugin_api_msg              \
65 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
66 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
67 _(ACL_ADD_REPLACE, acl_add_replace)                             \
68 _(ACL_DEL, acl_del)                             \
69 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
70 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list)       \
71 _(ACL_DUMP, acl_dump)  \
72 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
73 _(MACIP_ACL_ADD, macip_acl_add) \
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
80
81 /* *INDENT-OFF* */
82 VLIB_PLUGIN_REGISTER () = {
83     .version = VPP_BUILD_VER,
84     .description = "Access Control Lists",
85 };
86 /* *INDENT-ON* */
87
88
89 static void *
90 acl_set_heap(acl_main_t *am)
91 {
92   if (0 == am->acl_mheap) {
93     am->acl_mheap = mheap_alloc (0 /* use VM */ , 2 << 29);
94   }
95   void *oldheap = clib_mem_set_heap(am->acl_mheap);
96   return oldheap;
97 }
98
99
100 static void
101 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
102 {
103   acl_main_t *am = &acl_main;
104   vl_api_acl_plugin_get_version_reply_t *rmp;
105   int msg_size = sizeof (*rmp);
106   unix_shared_memory_queue_t *q;
107
108   q = vl_api_client_index_to_input_queue (mp->client_index);
109   if (q == 0)
110     {
111       return;
112     }
113
114   rmp = vl_msg_api_alloc (msg_size);
115   memset (rmp, 0, msg_size);
116   rmp->_vl_msg_id =
117     ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
118   rmp->context = mp->context;
119   rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
120   rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
121
122   vl_msg_api_send_shmem (q, (u8 *) & rmp);
123 }
124
125 static void
126 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
127 {
128   vl_api_acl_plugin_control_ping_reply_t *rmp;
129   acl_main_t *am = &acl_main;
130   int rv = 0;
131
132   /* *INDENT-OFF* */
133   REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
134   ({
135     rmp->vpe_pid = ntohl (getpid ());
136   }));
137   /* *INDENT-ON* */
138 }
139
140 static int
141 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
142               u32 * acl_list_index, u8 * tag)
143 {
144   acl_main_t *am = &acl_main;
145   acl_list_t *a;
146   acl_rule_t *r;
147   acl_rule_t *acl_new_rules = 0;
148   int i;
149
150   if (*acl_list_index != ~0)
151     {
152       /* They supplied some number, let's see if this ACL exists */
153       if (pool_is_free_index (am->acls, *acl_list_index))
154         {
155           /* tried to replace a non-existent ACL, no point doing anything */
156           clib_warning("acl-plugin-error: Trying to replace nonexistent ACL %d (tag %s)", *acl_list_index, tag);
157           return -1;
158         }
159     }
160   if (0 == count) {
161     clib_warning("acl-plugin-warning: supplied no rules for ACL %d (tag %s)", *acl_list_index, tag);
162   }
163
164   void *oldheap = acl_set_heap(am);
165
166   /* Create and populate the rules */
167   if (count > 0)
168     vec_validate(acl_new_rules, count-1);
169
170   for (i = 0; i < count; i++)
171     {
172       r = vec_elt_at_index(acl_new_rules, i);
173       memset(r, 0, sizeof(*r));
174       r->is_permit = rules[i].is_permit;
175       r->is_ipv6 = rules[i].is_ipv6;
176       if (r->is_ipv6)
177         {
178           memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
179           memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
180         }
181       else
182         {
183           memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
184           memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
185         }
186       r->src_prefixlen = rules[i].src_ip_prefix_len;
187       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
188       r->proto = rules[i].proto;
189       r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
190       r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
191       r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
192       r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
193       r->tcp_flags_value = rules[i].tcp_flags_value;
194       r->tcp_flags_mask = rules[i].tcp_flags_mask;
195     }
196
197   if (~0 == *acl_list_index)
198     {
199       /* Get ACL index */
200       pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
201       memset (a, 0, sizeof (*a));
202       /* Will return the newly allocated ACL index */
203       *acl_list_index = a - am->acls;
204     }
205   else
206     {
207       a = am->acls + *acl_list_index;
208       hash_acl_delete(am, *acl_list_index);
209       /* Get rid of the old rules */
210       if (a->rules)
211         vec_free (a->rules);
212     }
213   a->rules = acl_new_rules;
214   a->count = count;
215   memcpy (a->tag, tag, sizeof (a->tag));
216   hash_acl_add(am, *acl_list_index);
217   clib_mem_set_heap (oldheap);
218   return 0;
219 }
220
221 static int
222 acl_del_list (u32 acl_list_index)
223 {
224   acl_main_t *am = &acl_main;
225   acl_list_t *a;
226   int i, ii;
227   if (pool_is_free_index (am->acls, acl_list_index))
228     {
229       return -1;
230     }
231
232   if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
233     if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
234       /* ACL is applied somewhere inbound. Refuse to delete */
235       return -1;
236     }
237   }
238   if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
239     if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
240       /* ACL is applied somewhere outbound. Refuse to delete */
241       return -1;
242     }
243   }
244
245   void *oldheap = acl_set_heap(am);
246   /* delete any references to the ACL */
247   for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
248     {
249       for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
250            /* see body */ )
251         {
252           if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
253             {
254               vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
255             }
256           else
257             {
258               ii++;
259             }
260         }
261     }
262   for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
263     {
264       for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
265            /* see body */ )
266         {
267           if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
268             {
269               vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
270             }
271           else
272             {
273               ii++;
274             }
275         }
276     }
277   /* delete the hash table data */
278
279   hash_acl_delete(am, acl_list_index);
280   /* now we can delete the ACL itself */
281   a = &am->acls[acl_list_index];
282   if (a->rules)
283     vec_free (a->rules);
284
285   pool_put (am->acls, a);
286   clib_mem_set_heap (oldheap);
287   return 0;
288 }
289
290 /* Some aids in ASCII graphing the content */
291 #define XX "\377"
292 #define __ "\000"
293 #define _(x)
294 #define v
295
296 u8 ip4_5tuple_mask[] =
297 _("             dmac               smac            etype ")
298 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
299   _("        v ihl totlen   ")
300   _(0x0000)
301   __ __ __ __
302   _("        ident fl+fo    ")
303   _(0x0004)
304   __ __ __ __
305   _("       ttl pr checksum ")
306   _(0x0008)
307   __ XX __ __
308   _("        src address    ")
309   _(0x000C)
310   XX XX XX XX
311   _("        dst address    ")
312   _(0x0010)
313   XX XX XX XX
314   _("L4 T/U  sport dport    ")
315   _(tcpudp)
316   XX XX XX XX
317   _(padpad)
318   __ __ __ __
319   _(padpad)
320   __ __ __ __
321   _(padeth)
322   __ __;
323
324      u8 ip6_5tuple_mask[] =
325        _("             dmac               smac            etype ")
326   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
327   _("        v  tc + flow ")
328   _(0x0000) __ __ __ __
329   _("        plen  nh hl  ")
330   _(0x0004) __ __ XX __
331   _("        src address  ")
332   _(0x0008) XX XX XX XX
333   _(0x000C) XX XX XX XX
334   _(0x0010) XX XX XX XX
335   _(0x0014) XX XX XX XX
336   _("        dst address  ")
337   _(0x0018) XX XX XX XX
338   _(0x001C) XX XX XX XX
339   _(0x0020) XX XX XX XX
340   _(0x0024) XX XX XX XX
341   _("L4T/U  sport dport   ")
342   _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
343
344 #undef XX
345 #undef __
346 #undef _
347 #undef v
348
349      static int count_skip (u8 * p, u32 size)
350 {
351   u64 *p64 = (u64 *) p;
352   /* Be tolerant to null pointer */
353   if (0 == p)
354     return 0;
355
356   while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
357     {
358       p64++;
359     }
360   return (p64 - (u64 *) p) / 2;
361 }
362
363 static int
364 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
365                             u32 mask_len, u32 next_table_index,
366                             u32 miss_next_index, u32 * table_index,
367                             int is_add)
368 {
369   u32 nbuckets = 1;
370   u32 memory_size = 2 << 13;
371   u32 skip = count_skip (mask, mask_len);
372   u32 match = (mask_len / 16) - skip;
373   u8 *skip_mask_ptr = mask + 16 * skip;
374   u32 current_data_flag = 0;
375   int current_data_offset = 0;
376
377   if (0 == match)
378     match = 1;
379   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
380   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
381                                       memory_size, skip, match,
382                                       next_table_index, miss_next_index,
383                                       table_index, current_data_flag,
384                                       current_data_offset, is_add,
385                                       1 /* delete_chain */);
386   clib_mem_set_heap (oldheap);
387   return ret;
388 }
389
390 static int
391 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
392                             u32 mask_len, u32 next_table_index,
393                             u32 miss_next_index, u32 * table_index,
394                             int is_add)
395 {
396   u32 nbuckets = 32;
397   u32 memory_size = 2 << 20;
398   u32 skip = count_skip (mask, mask_len);
399   u32 match = (mask_len / 16) - skip;
400   u8 *skip_mask_ptr = mask + 16 * skip;
401   u32 current_data_flag = 0;
402   int current_data_offset = 0;
403
404   if (0 == match)
405     match = 1;
406
407   void *oldheap = clib_mem_set_heap (cm->vlib_main->heap_base);
408   int ret = vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
409                                       memory_size, skip, match,
410                                       next_table_index, miss_next_index,
411                                       table_index, current_data_flag,
412                                       current_data_offset, is_add,
413                                       1 /* delete_chain */);
414   clib_mem_set_heap (oldheap);
415   return ret;
416 }
417
418
419 static int
420 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
421 {
422   vnet_classify_main_t *cm = &vnet_classify_main;
423   u32 ip4_table_index = ~0;
424   u32 ip6_table_index = ~0;
425   void *oldheap = acl_set_heap(am);
426
427   vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
428                            sw_if_index, ~0);
429   vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
430                            sw_if_index, ~0);
431
432   /* switch to global heap while calling vnet_* functions */
433   clib_mem_set_heap (cm->vlib_main->heap_base);
434   vnet_l2_input_classify_enable_disable (sw_if_index, 0);
435
436   if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
437     {
438       ip4_table_index =
439         am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
440       am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
441       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
442                                   sizeof (ip4_5tuple_mask) - 1, ~0,
443                                   am->l2_input_classify_next_acl_ip4,
444                                   &ip4_table_index, 0);
445     }
446   if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
447     {
448       ip6_table_index =
449         am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
450       am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
451       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
452                                   sizeof (ip6_5tuple_mask) - 1, ~0,
453                                   am->l2_input_classify_next_acl_ip6,
454                                   &ip6_table_index, 0);
455     }
456   clib_mem_set_heap (oldheap);
457   return 0;
458 }
459
460 static int
461 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
462 {
463   vnet_classify_main_t *cm = &vnet_classify_main;
464   u32 ip4_table_index = ~0;
465   u32 ip6_table_index = ~0;
466   void *oldheap = acl_set_heap(am);
467
468   vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
469                            sw_if_index, ~0);
470   vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
471                            sw_if_index, ~0);
472
473   /* switch to global heap while calling vnet_* functions */
474   clib_mem_set_heap (cm->vlib_main->heap_base);
475
476   vnet_l2_output_classify_enable_disable (sw_if_index, 0);
477
478   if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
479     {
480       ip4_table_index =
481         am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
482       am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
483       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
484                                   sizeof (ip4_5tuple_mask) - 1, ~0,
485                                   am->l2_output_classify_next_acl_ip4,
486                                   &ip4_table_index, 0);
487     }
488   if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
489     {
490       ip6_table_index =
491         am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
492       am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
493       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
494                                   sizeof (ip6_5tuple_mask) - 1, ~0,
495                                   am->l2_output_classify_next_acl_ip6,
496                                   &ip6_table_index, 0);
497     }
498   clib_mem_set_heap (oldheap);
499   return 0;
500 }
501
502 static int
503 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
504 {
505   vnet_classify_main_t *cm = &vnet_classify_main;
506   u32 ip4_table_index = ~0;
507   u32 ip6_table_index = ~0;
508   int rv;
509
510   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
511
512   /* in case there were previous tables attached */
513   acl_unhook_l2_input_classify (am, sw_if_index);
514   rv =
515     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
516                                 sizeof (ip4_5tuple_mask) - 1, ~0,
517                                 am->l2_input_classify_next_acl_ip4,
518                                 &ip4_table_index, 1);
519   if (rv)
520     goto done;
521   rv =
522     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
523                                 sizeof (ip6_5tuple_mask) - 1, ~0,
524                                 am->l2_input_classify_next_acl_ip6,
525                                 &ip6_table_index, 1);
526   if (rv)
527     {
528       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
529                                   sizeof (ip4_5tuple_mask) - 1, ~0,
530                                   am->l2_input_classify_next_acl_ip4,
531                                   &ip4_table_index, 0);
532       goto done;
533     }
534   rv =
535     vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
536                                        ip6_table_index, ~0);
537   clib_warning
538     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
539      sw_if_index, ip4_table_index, ip6_table_index);
540   if (rv)
541     {
542       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
543                                   sizeof (ip6_5tuple_mask) - 1, ~0,
544                                   am->l2_input_classify_next_acl_ip6,
545                                   &ip6_table_index, 0);
546       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
547                                   sizeof (ip4_5tuple_mask) - 1, ~0,
548                                   am->l2_input_classify_next_acl_ip4,
549                                   &ip4_table_index, 0);
550       goto done;
551     }
552
553   am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
554     ip4_table_index;
555   am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
556     ip6_table_index;
557
558   vnet_l2_input_classify_enable_disable (sw_if_index, 1);
559 done:
560   clib_mem_set_heap (prevheap);
561   return rv;
562 }
563
564 static int
565 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
566 {
567   vnet_classify_main_t *cm = &vnet_classify_main;
568   u32 ip4_table_index = ~0;
569   u32 ip6_table_index = ~0;
570   int rv;
571
572   void *prevheap = clib_mem_set_heap (cm->vlib_main->heap_base);
573
574   /* in case there were previous tables attached */
575   acl_unhook_l2_output_classify (am, sw_if_index);
576   rv =
577     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
578                                 sizeof (ip4_5tuple_mask) - 1, ~0,
579                                 am->l2_output_classify_next_acl_ip4,
580                                 &ip4_table_index, 1);
581   if (rv)
582     goto done;
583   rv =
584     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
585                                 sizeof (ip6_5tuple_mask) - 1, ~0,
586                                 am->l2_output_classify_next_acl_ip6,
587                                 &ip6_table_index, 1);
588   if (rv)
589     {
590       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
591                                   sizeof (ip4_5tuple_mask) - 1, ~0,
592                                   am->l2_output_classify_next_acl_ip4,
593                                   &ip4_table_index, 0);
594       goto done;
595     }
596   rv =
597     vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
598                                         ip6_table_index, ~0);
599   clib_warning
600     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
601      sw_if_index, ip4_table_index, ip6_table_index);
602   if (rv)
603     {
604       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
605                                   sizeof (ip6_5tuple_mask) - 1, ~0,
606                                   am->l2_output_classify_next_acl_ip6,
607                                   &ip6_table_index, 0);
608       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
609                                   sizeof (ip4_5tuple_mask) - 1, ~0,
610                                   am->l2_output_classify_next_acl_ip4,
611                                   &ip4_table_index, 0);
612       goto done;
613     }
614
615   am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
616     ip4_table_index;
617   am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
618     ip6_table_index;
619
620   vnet_l2_output_classify_enable_disable (sw_if_index, 1);
621 done:
622   clib_mem_set_heap (prevheap);
623   return rv;
624 }
625
626
627
628 int
629 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
630                                  int enable_disable)
631 {
632   int rv;
633
634   /* Utterly wrong? */
635   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
636                           sw_if_index))
637     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
638
639   acl_fa_enable_disable(sw_if_index, 1, enable_disable);
640
641   if (enable_disable)
642     {
643       rv = acl_hook_l2_input_classify (am, sw_if_index);
644     }
645   else
646     {
647       rv = acl_unhook_l2_input_classify (am, sw_if_index);
648     }
649
650   return rv;
651 }
652
653 int
654 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
655                                   int enable_disable)
656 {
657   int rv;
658
659   /* Utterly wrong? */
660   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
661                           sw_if_index))
662     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
663
664   acl_fa_enable_disable(sw_if_index, 0, enable_disable);
665
666   if (enable_disable)
667     {
668       rv = acl_hook_l2_output_classify (am, sw_if_index);
669     }
670   else
671     {
672       rv = acl_unhook_l2_output_classify (am, sw_if_index);
673     }
674
675   return rv;
676 }
677
678 static int
679 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
680 {
681   return (pool_is_free_index (am->acls, acl_list_index));
682 }
683
684
685 static int
686 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
687 {
688   acl_main_t *am = &acl_main;
689   if (acl_is_not_defined(am, acl_list_index)) {
690     /* ACL is not defined. Can not apply */
691     return -1;
692   }
693   void *oldheap = acl_set_heap(am);
694
695   if (is_input)
696     {
697       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
698
699       u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
700       if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
701         clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
702                      acl_list_index, sw_if_index, index);
703         /* the entry is already there */
704         clib_mem_set_heap (oldheap);
705         return -1;
706       }
707       /* if there was no ACL applied before, enable the ACL processing */
708       if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
709         acl_interface_in_enable_disable (am, sw_if_index, 1);
710       }
711       vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
712                1);
713       vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
714       vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
715                1);
716     }
717   else
718     {
719       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
720
721       u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
722       if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
723         clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
724                      acl_list_index, sw_if_index, index);
725         /* the entry is already there */
726         clib_mem_set_heap (oldheap);
727         return -1;
728       }
729       /* if there was no ACL applied before, enable the ACL processing */
730       if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) == 0) {
731         acl_interface_out_enable_disable (am, sw_if_index, 1);
732       }
733       vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
734                &acl_list_index, 1);
735       vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
736       vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
737                1);
738     }
739   clib_mem_set_heap (oldheap);
740   return 0;
741 }
742
743
744 static int
745 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
746 {
747   acl_main_t *am = &acl_main;
748   int i;
749   int rv = -1;
750   void *oldheap = acl_set_heap(am);
751   if (is_input)
752     {
753       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
754       for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
755            i++)
756         {
757           if (acl_list_index ==
758               am->input_acl_vec_by_sw_if_index[sw_if_index][i])
759             {
760               vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
761               rv = 0;
762               break;
763             }
764         }
765
766       if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
767         u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
768         if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
769           hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
770           vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
771         }
772       }
773
774       /* If there is no more ACLs applied on an interface, disable ACL processing */
775       if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
776         {
777           acl_interface_in_enable_disable (am, sw_if_index, 0);
778         }
779     }
780   else
781     {
782       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
783       for (i = 0;
784            i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
785         {
786           if (acl_list_index ==
787               am->output_acl_vec_by_sw_if_index[sw_if_index][i])
788             {
789               vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
790               rv = 0;
791               break;
792             }
793         }
794
795       if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
796         u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
797         if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
798           hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
799           vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
800         }
801       }
802
803       /* If there is no more ACLs applied on an interface, disable ACL processing */
804       if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
805         {
806           acl_interface_out_enable_disable (am, sw_if_index, 0);
807         }
808     }
809   clib_mem_set_heap (oldheap);
810   return rv;
811 }
812
813 static void
814 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
815 {
816   acl_main_t *am = &acl_main;
817   int i;
818   void *oldheap = acl_set_heap(am);
819   if (is_input)
820     {
821       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
822       if (vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
823         acl_interface_in_enable_disable (am, sw_if_index, 0);
824       }
825
826       for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
827         u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
828         hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
829         if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
830           u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
831           if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
832             vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
833           }
834         }
835       }
836
837       vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
838     }
839   else
840     {
841       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
842       if (vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0) {
843         acl_interface_out_enable_disable (am, sw_if_index, 0);
844       }
845
846       for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
847         u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
848         hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
849         if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
850           u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
851           if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
852             vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
853           }
854         }
855       }
856
857       vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
858     }
859   clib_mem_set_heap (oldheap);
860 }
861
862 static int
863 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
864                                  u32 acl_list_index)
865 {
866   int rv = -1;
867   acl_main_t *am = &acl_main;
868   void *oldheap = acl_set_heap(am);
869   if (is_add)
870     {
871       rv =
872         acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
873       if (rv == 0)
874         {
875           hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
876         }
877     }
878   else
879     {
880       hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
881       rv =
882         acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
883     }
884   clib_mem_set_heap (oldheap);
885   return rv;
886 }
887
888
889 typedef struct
890 {
891   u8 is_ipv6;
892   u8 mac_mask[6];
893   u8 prefix_len;
894   u32 count;
895   u32 table_index;
896   u32 arp_table_index;
897 } macip_match_type_t;
898
899 static u32
900 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
901                        u8 is_ipv6)
902 {
903   u32 i;
904   if (mv)
905     {
906       for (i = 0; i < vec_len (mv); i++)
907         {
908           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
909               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
910             {
911               return i;
912             }
913         }
914     }
915   return ~0;
916 }
917
918
919 /* Get metric used to sort match types.
920    The more specific and the more often seen - the bigger the metric */
921 static int
922 match_type_metric (macip_match_type_t * m)
923 {
924   /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
925   return m->prefix_len + m->is_ipv6 + 10 * m->count;
926 }
927
928 static int
929 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
930 {
931   /* Ascending sort based on the metric values */
932   return match_type_metric (m1) - match_type_metric (m2);
933 }
934
935 /* Get the offset of L3 source within ethernet packet */
936 static int
937 get_l3_src_offset(int is6)
938 {
939   if(is6)
940     return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
941   else
942     return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
943 }
944
945 static int
946 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
947 {
948   macip_match_type_t *mvec = NULL;
949   macip_match_type_t *mt;
950   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
951   int i;
952   u32 match_type_index;
953   u32 last_table;
954   u8 mask[5 * 16];
955   vnet_classify_main_t *cm = &vnet_classify_main;
956
957   /* Count the number of different types of rules */
958   for (i = 0; i < a->count; i++)
959     {
960       if (~0 ==
961           (match_type_index =
962            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
963                                   a->rules[i].src_prefixlen,
964                                   a->rules[i].is_ipv6)))
965         {
966           match_type_index = vec_len (mvec);
967           vec_validate (mvec, match_type_index);
968           memcpy (mvec[match_type_index].mac_mask,
969                   a->rules[match_type_index].src_mac_mask, 6);
970           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
971           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
972           mvec[match_type_index].table_index = ~0;
973         }
974       mvec[match_type_index].count++;
975     }
976   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
977   vec_sort_with_function (mvec, match_type_compare);
978   /* Create the classifier tables */
979   last_table = ~0;
980   /* First add ARP tables */
981   vec_foreach (mt, mvec)
982   {
983     int mask_len;
984     int is6 = mt->is_ipv6;
985
986     mt->arp_table_index = ~0;
987     if (!is6)
988       {
989         memset (mask, 0, sizeof (mask));
990         memcpy (&mask[6], mt->mac_mask, 6);
991         memset (&mask[12], 0xff, 2); /* ethernet protocol */
992         memcpy (&mask[14 + 8], mt->mac_mask, 6);
993
994         for (i = 0; i < (mt->prefix_len / 8); i++)
995           mask[14 + 14 + i] = 0xff;
996         if (mt->prefix_len % 8)
997           mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
998
999         mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1000                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1001         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1002                                (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1003                                1);
1004         last_table = mt->arp_table_index;
1005       }
1006   }
1007   /* Now add IP[46] tables */
1008   vec_foreach (mt, mvec)
1009   {
1010     int mask_len;
1011     int is6 = mt->is_ipv6;
1012     int l3_src_offs = get_l3_src_offset(is6);
1013     memset (mask, 0, sizeof (mask));
1014     memcpy (&mask[6], mt->mac_mask, 6);
1015     for (i = 0; i < (mt->prefix_len / 8); i++)
1016       {
1017         mask[l3_src_offs + i] = 0xff;
1018       }
1019     if (mt->prefix_len % 8)
1020       {
1021         mask[l3_src_offs + (mt->prefix_len / 8)] =
1022           0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1023       }
1024     /*
1025      * Round-up the number of bytes needed to store the prefix,
1026      * and round up the number of vectors too
1027      */
1028     mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1029                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1030     acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1031                                 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1032                                 1);
1033     last_table = mt->table_index;
1034   }
1035   a->ip4_table_index = ~0;
1036   a->ip6_table_index = ~0;
1037   a->l2_table_index = last_table;
1038
1039   /* Populate the classifier tables with rules from the MACIP ACL */
1040   for (i = 0; i < a->count; i++)
1041     {
1042       u32 action = 0;
1043       u32 metadata = 0;
1044       int is6 = a->rules[i].is_ipv6;
1045       int l3_src_offs = get_l3_src_offset(is6);
1046       memset (mask, 0, sizeof (mask));
1047       memcpy (&mask[6], a->rules[i].src_mac, 6);
1048       memset (&mask[12], 0xff, 2); /* ethernet protocol */
1049       if (is6)
1050         {
1051           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1052           mask[12] = 0x86;
1053           mask[13] = 0xdd;
1054         }
1055       else
1056         {
1057           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1058           mask[12] = 0x08;
1059           mask[13] = 0x00;
1060         }
1061       match_type_index =
1062         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1063                                a->rules[i].src_prefixlen,
1064                                a->rules[i].is_ipv6);
1065       ASSERT(match_type_index != ~0);
1066       /* add session to table mvec[match_type_index].table_index; */
1067       vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1068                                      mask, a->rules[i].is_permit ? ~0 : 0, i,
1069                                      0, action, metadata, 1);
1070       /* add ARP table entry too */
1071       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1072         {
1073           memset (mask, 0, sizeof (mask));
1074           memcpy (&mask[6], a->rules[i].src_mac, 6);
1075           mask[12] = 0x08;
1076           mask[13] = 0x06;
1077           memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1078           memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1079           vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1080                                     mask, a->rules[i].is_permit ? ~0 : 0, i,
1081                                     0, action, metadata, 1);
1082         }
1083     }
1084   return 0;
1085 }
1086
1087 static void
1088 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1089 {
1090   vnet_classify_main_t *cm = &vnet_classify_main;
1091   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1092
1093   if (a->ip4_table_index != ~0)
1094     {
1095       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1096       a->ip4_table_index = ~0;
1097     }
1098   if (a->ip6_table_index != ~0)
1099     {
1100       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1101       a->ip6_table_index = ~0;
1102     }
1103   if (a->l2_table_index != ~0)
1104     {
1105       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1106       a->l2_table_index = ~0;
1107     }
1108 }
1109
1110 static int
1111 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1112                     u32 * acl_list_index, u8 * tag)
1113 {
1114   acl_main_t *am = &acl_main;
1115   macip_acl_list_t *a;
1116   macip_acl_rule_t *r;
1117   macip_acl_rule_t *acl_new_rules = 0;
1118   int i;
1119   if (0 == count) {
1120     clib_warning("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)", tag);
1121   }
1122   void *oldheap = acl_set_heap(am);
1123   /* Create and populate the rules */
1124   if (count > 0)
1125     vec_validate(acl_new_rules, count-1);
1126
1127   for (i = 0; i < count; i++)
1128     {
1129       r = &acl_new_rules[i];
1130       r->is_permit = rules[i].is_permit;
1131       r->is_ipv6 = rules[i].is_ipv6;
1132       memcpy (&r->src_mac, rules[i].src_mac, 6);
1133       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1134       if(rules[i].is_ipv6)
1135         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1136       else
1137         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1138       r->src_prefixlen = rules[i].src_ip_prefix_len;
1139     }
1140
1141   /* Get ACL index */
1142   pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1143   memset (a, 0, sizeof (*a));
1144   /* Will return the newly allocated ACL index */
1145   *acl_list_index = a - am->macip_acls;
1146
1147   a->rules = acl_new_rules;
1148   a->count = count;
1149   memcpy (a->tag, tag, sizeof (a->tag));
1150
1151   /* Create and populate the classifer tables */
1152   macip_create_classify_tables (am, *acl_list_index);
1153   clib_mem_set_heap (oldheap);
1154   return 0;
1155 }
1156
1157
1158 /* No check for validity of sw_if_index - the callers were supposed to validate */
1159
1160 static int
1161 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1162 {
1163   int rv;
1164   u32 macip_acl_index;
1165   macip_acl_list_t *a;
1166   void *oldheap = acl_set_heap(am);
1167   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1168   clib_mem_set_heap (oldheap);
1169   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1170   /* No point in deleting MACIP ACL which is not applied */
1171   if (~0 == macip_acl_index)
1172     return -1;
1173   a = &am->macip_acls[macip_acl_index];
1174   /* remove the classifier tables off the interface L2 ACL */
1175   rv =
1176     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1177                               a->ip6_table_index, a->l2_table_index, 0);
1178   /* Unset the MACIP ACL index */
1179   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1180   return rv;
1181 }
1182
1183 /* No check for validity of sw_if_index - the callers were supposed to validate */
1184
1185 static int
1186 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1187                              u32 macip_acl_index)
1188 {
1189   macip_acl_list_t *a;
1190   int rv;
1191   if (pool_is_free_index (am->macip_acls, macip_acl_index))
1192     {
1193       return -1;
1194     }
1195   void *oldheap = acl_set_heap(am);
1196   a = &am->macip_acls[macip_acl_index];
1197   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1198   /* If there already a MACIP ACL applied, unapply it */
1199   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1200     macip_acl_interface_del_acl(am, sw_if_index);
1201   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1202   clib_mem_set_heap (oldheap);
1203
1204   /* Apply the classifier tables for L2 ACLs */
1205   rv =
1206     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1207                               a->ip6_table_index, a->l2_table_index, 1);
1208   return rv;
1209 }
1210
1211 static int
1212 macip_acl_del_list (u32 acl_list_index)
1213 {
1214   acl_main_t *am = &acl_main;
1215   void *oldheap = acl_set_heap(am);
1216   macip_acl_list_t *a;
1217   int i;
1218   if (pool_is_free_index (am->macip_acls, acl_list_index))
1219     {
1220       return -1;
1221     }
1222
1223   /* delete any references to the ACL */
1224   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1225     {
1226       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1227         {
1228           macip_acl_interface_del_acl (am, i);
1229         }
1230     }
1231
1232   /* Now that classifier tables are detached, clean them up */
1233   macip_destroy_classify_tables (am, acl_list_index);
1234
1235   /* now we can delete the ACL itself */
1236   a = &am->macip_acls[acl_list_index];
1237   if (a->rules)
1238     {
1239       vec_free (a->rules);
1240     }
1241   pool_put (am->macip_acls, a);
1242   clib_mem_set_heap (oldheap);
1243   return 0;
1244 }
1245
1246
1247 static int
1248 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1249                                  u32 acl_list_index)
1250 {
1251   acl_main_t *am = &acl_main;
1252   void *oldheap = acl_set_heap(am);
1253   int rv = -1;
1254   if (is_add)
1255     {
1256       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1257     }
1258   else
1259     {
1260       rv = macip_acl_interface_del_acl (am, sw_if_index);
1261     }
1262   clib_mem_set_heap (oldheap);
1263   return rv;
1264 }
1265
1266 /*
1267  * If the client does not allocate enough memory for a variable-length
1268  * message, and then proceed to use it as if the full memory allocated,
1269  * absent the check we happily consume that on the VPP side, and go
1270  * along as if nothing happened. However, the resulting
1271  * effects range from just garbage in the API decode
1272  * (because the decoder snoops too far), to potential memory
1273  * corruptions.
1274  *
1275  * This verifies that the actual length of the message is
1276  * at least expected_len, and complains loudly if it is not.
1277  *
1278  * A failing check here is 100% a software bug on the API user side,
1279  * so we might as well yell.
1280  *
1281  */
1282 static int verify_message_len(void *mp, u32 expected_len, char *where)
1283 {
1284   u32 supplied_len = vl_msg_api_get_msg_length (mp);
1285   if (supplied_len < expected_len) {
1286       clib_warning("%s: Supplied message length %d is less than expected %d",
1287                    where, supplied_len, expected_len);
1288       return 0;
1289   } else {
1290       return 1;
1291   }
1292 }
1293
1294 /* API message handler */
1295 static void
1296 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1297 {
1298   vl_api_acl_add_replace_reply_t *rmp;
1299   acl_main_t *am = &acl_main;
1300   int rv;
1301   u32 acl_list_index = ntohl (mp->acl_index);
1302   u32 acl_count = ntohl (mp->count);
1303   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1304
1305   if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1306       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1307   } else {
1308       rv = VNET_API_ERROR_INVALID_VALUE;
1309   }
1310
1311   /* *INDENT-OFF* */
1312   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1313   ({
1314     rmp->acl_index = htonl(acl_list_index);
1315   }));
1316   /* *INDENT-ON* */
1317 }
1318
1319 static void
1320 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1321 {
1322   acl_main_t *am = &acl_main;
1323   vl_api_acl_del_reply_t *rmp;
1324   int rv;
1325
1326   rv = acl_del_list (ntohl (mp->acl_index));
1327
1328   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1329 }
1330
1331 static void
1332 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1333 {
1334   acl_main_t *am = &acl_main;
1335   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1336   u32 sw_if_index = ntohl (mp->sw_if_index);
1337   vl_api_acl_interface_add_del_reply_t *rmp;
1338   int rv = -1;
1339
1340   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1341     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1342   else
1343     rv =
1344       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1345                                      mp->is_input, ntohl (mp->acl_index));
1346
1347   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1348 }
1349
1350 static void
1351 vl_api_acl_interface_set_acl_list_t_handler
1352   (vl_api_acl_interface_set_acl_list_t * mp)
1353 {
1354   acl_main_t *am = &acl_main;
1355   vl_api_acl_interface_set_acl_list_reply_t *rmp;
1356   int rv = 0;
1357   int i;
1358   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1359   u32 sw_if_index = ntohl (mp->sw_if_index);
1360
1361   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1362     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1363   else
1364     {
1365       acl_interface_reset_inout_acls (sw_if_index, 0);
1366       acl_interface_reset_inout_acls (sw_if_index, 1);
1367
1368       for (i = 0; i < mp->count; i++)
1369         {
1370           if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1371             /* ACL does not exist, so we can not apply it */
1372             rv = -1;
1373           }
1374         }
1375       if (0 == rv) {
1376         for (i = 0; i < mp->count; i++)
1377           {
1378             acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1379                                        ntohl (mp->acls[i]));
1380           }
1381       }
1382     }
1383
1384   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1385 }
1386
1387 static void
1388 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1389 {
1390   api_rule->is_permit = r->is_permit;
1391   api_rule->is_ipv6 = r->is_ipv6;
1392   if(r->is_ipv6)
1393     {
1394       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1395       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1396     }
1397   else
1398     {
1399       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1400       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1401     }
1402   api_rule->src_ip_prefix_len = r->src_prefixlen;
1403   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1404   api_rule->proto = r->proto;
1405   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1406   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1407   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1408   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1409   api_rule->tcp_flags_mask = r->tcp_flags_mask;
1410   api_rule->tcp_flags_value = r->tcp_flags_value;
1411 }
1412
1413 static void
1414 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1415                   acl_list_t * acl, u32 context)
1416 {
1417   vl_api_acl_details_t *mp;
1418   vl_api_acl_rule_t *rules;
1419   int i;
1420   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1421   void *oldheap = acl_set_heap(am);
1422
1423   mp = vl_msg_api_alloc (msg_size);
1424   memset (mp, 0, msg_size);
1425   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1426
1427   /* fill in the message */
1428   mp->context = context;
1429   mp->count = htonl (acl->count);
1430   mp->acl_index = htonl (acl - am->acls);
1431   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1432   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1433   rules = mp->r;
1434   for (i = 0; i < acl->count; i++)
1435     {
1436       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1437     }
1438
1439   clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1440   clib_mem_set_heap (oldheap);
1441   vl_msg_api_send_shmem (q, (u8 *) & mp);
1442 }
1443
1444
1445 static void
1446 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1447 {
1448   acl_main_t *am = &acl_main;
1449   u32 acl_index;
1450   acl_list_t *acl;
1451
1452   int rv = -1;
1453   unix_shared_memory_queue_t *q;
1454
1455   q = vl_api_client_index_to_input_queue (mp->client_index);
1456   if (q == 0)
1457     {
1458       return;
1459     }
1460
1461   if (mp->acl_index == ~0)
1462     {
1463     /* *INDENT-OFF* */
1464     /* Just dump all ACLs */
1465     pool_foreach (acl, am->acls,
1466     ({
1467       send_acl_details(am, q, acl, mp->context);
1468     }));
1469     /* *INDENT-ON* */
1470     }
1471   else
1472     {
1473       acl_index = ntohl (mp->acl_index);
1474       if (!pool_is_free_index (am->acls, acl_index))
1475         {
1476           acl = &am->acls[acl_index];
1477           send_acl_details (am, q, acl, mp->context);
1478         }
1479     }
1480
1481   if (rv == -1)
1482     {
1483       /* FIXME API: should we signal an error here at all ? */
1484       return;
1485     }
1486 }
1487
1488 static void
1489 send_acl_interface_list_details (acl_main_t * am,
1490                                  unix_shared_memory_queue_t * q,
1491                                  u32 sw_if_index, u32 context)
1492 {
1493   vl_api_acl_interface_list_details_t *mp;
1494   int msg_size;
1495   int n_input;
1496   int n_output;
1497   int count;
1498   int i = 0;
1499   void *oldheap = acl_set_heap(am);
1500
1501   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1502   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1503
1504   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1505   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1506   count = n_input + n_output;
1507
1508   msg_size = sizeof (*mp);
1509   msg_size += sizeof (mp->acls[0]) * count;
1510
1511   mp = vl_msg_api_alloc (msg_size);
1512   memset (mp, 0, msg_size);
1513   mp->_vl_msg_id =
1514     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1515
1516   /* fill in the message */
1517   mp->context = context;
1518   mp->sw_if_index = htonl (sw_if_index);
1519   mp->count = count;
1520   mp->n_input = n_input;
1521   for (i = 0; i < n_input; i++)
1522     {
1523       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1524     }
1525   for (i = 0; i < n_output; i++)
1526     {
1527       mp->acls[n_input + i] =
1528         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1529     }
1530   clib_mem_set_heap (oldheap);
1531   vl_msg_api_send_shmem (q, (u8 *) & mp);
1532 }
1533
1534 static void
1535 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1536                                           mp)
1537 {
1538   acl_main_t *am = &acl_main;
1539   vnet_sw_interface_t *swif;
1540   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1541
1542   u32 sw_if_index;
1543   unix_shared_memory_queue_t *q;
1544
1545   q = vl_api_client_index_to_input_queue (mp->client_index);
1546   if (q == 0)
1547     {
1548       return;
1549     }
1550
1551   if (mp->sw_if_index == ~0)
1552     {
1553     /* *INDENT-OFF* */
1554     pool_foreach (swif, im->sw_interfaces,
1555     ({
1556       send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1557     }));
1558     /* *INDENT-ON* */
1559     }
1560   else
1561     {
1562       sw_if_index = ntohl (mp->sw_if_index);
1563       if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1564         send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1565     }
1566 }
1567
1568 /* MACIP ACL API handlers */
1569
1570 static void
1571 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1572 {
1573   vl_api_macip_acl_add_reply_t *rmp;
1574   acl_main_t *am = &acl_main;
1575   int rv;
1576   u32 acl_list_index = ~0;
1577   u32 acl_count = ntohl (mp->count);
1578   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1579
1580   if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1581       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1582   } else {
1583       rv = VNET_API_ERROR_INVALID_VALUE;
1584   }
1585
1586   /* *INDENT-OFF* */
1587   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1588   ({
1589     rmp->acl_index = htonl(acl_list_index);
1590   }));
1591   /* *INDENT-ON* */
1592 }
1593
1594 static void
1595 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1596 {
1597   acl_main_t *am = &acl_main;
1598   vl_api_macip_acl_del_reply_t *rmp;
1599   int rv;
1600
1601   rv = macip_acl_del_list (ntohl (mp->acl_index));
1602
1603   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1604 }
1605
1606 static void
1607 vl_api_macip_acl_interface_add_del_t_handler
1608   (vl_api_macip_acl_interface_add_del_t * mp)
1609 {
1610   acl_main_t *am = &acl_main;
1611   vl_api_macip_acl_interface_add_del_reply_t *rmp;
1612   int rv = -1;
1613   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1614   u32 sw_if_index = ntohl (mp->sw_if_index);
1615
1616   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1617     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1618   else
1619     rv =
1620       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1621                                      ntohl (mp->acl_index));
1622
1623   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1624 }
1625
1626 static void
1627 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1628                         macip_acl_list_t * acl, u32 context)
1629 {
1630   vl_api_macip_acl_details_t *mp;
1631   vl_api_macip_acl_rule_t *rules;
1632   macip_acl_rule_t *r;
1633   int i;
1634   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1635
1636   mp = vl_msg_api_alloc (msg_size);
1637   memset (mp, 0, msg_size);
1638   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1639
1640   /* fill in the message */
1641   mp->context = context;
1642   if (acl)
1643     {
1644       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1645       mp->count = htonl (acl->count);
1646       mp->acl_index = htonl (acl - am->macip_acls);
1647       rules = mp->r;
1648       for (i = 0; i < acl->count; i++)
1649         {
1650           r = &acl->rules[i];
1651           rules[i].is_permit = r->is_permit;
1652           rules[i].is_ipv6 = r->is_ipv6;
1653           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1654           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1655                   sizeof (r->src_mac_mask));
1656           if (r->is_ipv6)
1657             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1658                   sizeof (r->src_ip_addr.ip6));
1659           else
1660             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1661                   sizeof (r->src_ip_addr.ip4));
1662           rules[i].src_ip_prefix_len = r->src_prefixlen;
1663         }
1664     }
1665   else
1666     {
1667       /* No martini, no party - no ACL applied to this interface. */
1668       mp->acl_index = ~0;
1669       mp->count = 0;
1670     }
1671
1672   vl_msg_api_send_shmem (q, (u8 *) & mp);
1673 }
1674
1675
1676 static void
1677 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1678 {
1679   acl_main_t *am = &acl_main;
1680   macip_acl_list_t *acl;
1681
1682   unix_shared_memory_queue_t *q;
1683
1684   q = vl_api_client_index_to_input_queue (mp->client_index);
1685   if (q == 0)
1686     {
1687       return;
1688     }
1689
1690   if (mp->acl_index == ~0)
1691     {
1692       /* Just dump all ACLs for now, with sw_if_index = ~0 */
1693       pool_foreach (acl, am->macip_acls, (
1694                                            {
1695                                            send_macip_acl_details (am, q, acl,
1696                                                                    mp->
1697                                                                    context);}
1698                     ));
1699       /* *INDENT-ON* */
1700     }
1701   else
1702     {
1703       u32 acl_index = ntohl (mp->acl_index);
1704       if (!pool_is_free_index (am->macip_acls, acl_index))
1705         {
1706           acl = &am->macip_acls[acl_index];
1707           send_macip_acl_details (am, q, acl, mp->context);
1708         }
1709     }
1710 }
1711
1712 static void
1713 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1714                                           mp)
1715 {
1716   acl_main_t *am = &acl_main;
1717   vl_api_macip_acl_interface_get_reply_t *rmp;
1718   u32 count = vec_len (am->macip_acl_by_sw_if_index);
1719   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1720   unix_shared_memory_queue_t *q;
1721   int i;
1722
1723   q = vl_api_client_index_to_input_queue (mp->client_index);
1724   if (q == 0)
1725     {
1726       return;
1727     }
1728
1729   rmp = vl_msg_api_alloc (msg_size);
1730   memset (rmp, 0, msg_size);
1731   rmp->_vl_msg_id =
1732     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1733   rmp->context = mp->context;
1734   rmp->count = htonl (count);
1735   for (i = 0; i < count; i++)
1736     {
1737       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1738     }
1739
1740   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1741 }
1742
1743 static void
1744 send_macip_acl_interface_list_details (acl_main_t * am,
1745                                        unix_shared_memory_queue_t * q,
1746                                        u32 sw_if_index,
1747                                        u32 acl_index,
1748                                        u32 context)
1749 {
1750   vl_api_macip_acl_interface_list_details_t *rmp;
1751   /* at this time there is only ever 1 mac ip acl per interface */
1752   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
1753
1754   rmp = vl_msg_api_alloc (msg_size);
1755   memset (rmp, 0, msg_size);
1756   rmp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1757
1758   /* fill in the message */
1759   rmp->context = context;
1760   rmp->count = 1;
1761   rmp->sw_if_index = htonl (sw_if_index);
1762   rmp->acls[0] = htonl (acl_index);
1763
1764   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1765 }
1766
1767 static void
1768 vl_api_macip_acl_interface_list_dump_t_handler (vl_api_macip_acl_interface_list_dump_t *mp)
1769 {
1770   unix_shared_memory_queue_t *q;
1771   acl_main_t *am = &acl_main;
1772   u32 sw_if_index = ntohl (mp->sw_if_index);
1773
1774   q = vl_api_client_index_to_input_queue (mp->client_index);
1775   if (q == 0)
1776     {
1777       return;
1778     }
1779
1780   if (sw_if_index == ~0)
1781     {
1782       vec_foreach_index(sw_if_index, am->macip_acl_by_sw_if_index)
1783         {
1784           if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1785             {
1786               send_macip_acl_interface_list_details(am, q,  sw_if_index,
1787                                                     am->macip_acl_by_sw_if_index[sw_if_index],
1788                                                     mp->context);
1789             }
1790         }
1791     }
1792   else
1793     {
1794       if (vec_len(am->macip_acl_by_sw_if_index) > sw_if_index)
1795         {
1796           send_macip_acl_interface_list_details(am, q, sw_if_index,
1797                                                 am->macip_acl_by_sw_if_index[sw_if_index],
1798                                                 mp->context);
1799         }
1800     }
1801 }
1802
1803 /* Set up the API message handling tables */
1804 static clib_error_t *
1805 acl_plugin_api_hookup (vlib_main_t * vm)
1806 {
1807   acl_main_t *am = &acl_main;
1808 #define _(N,n)                                                  \
1809     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
1810                            #n,                                  \
1811                            vl_api_##n##_t_handler,              \
1812                            vl_noop_handler,                     \
1813                            vl_api_##n##_t_endian,               \
1814                            vl_api_##n##_t_print,                \
1815                            sizeof(vl_api_##n##_t), 1);
1816   foreach_acl_plugin_api_msg;
1817 #undef _
1818
1819   return 0;
1820 }
1821
1822 #define vl_msg_name_crc_list
1823 #include <acl/acl_all_api_h.h>
1824 #undef vl_msg_name_crc_list
1825
1826 static void
1827 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1828 {
1829 #define _(id,n,crc) \
1830   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1831   foreach_vl_msg_name_crc_acl;
1832 #undef _
1833 }
1834
1835 static void
1836 acl_setup_fa_nodes (void)
1837 {
1838   vlib_main_t *vm = vlib_get_main ();
1839   acl_main_t *am = &acl_main;
1840   vlib_node_t *n, *n4, *n6;
1841
1842   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1843   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1844   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1845
1846
1847   am->l2_input_classify_next_acl_ip4 =
1848     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1849   am->l2_input_classify_next_acl_ip6 =
1850     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1851
1852   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1853                                l2input_get_feat_names (),
1854                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1855
1856   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1857                                l2input_get_feat_names (),
1858                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1859
1860
1861   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1862   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1863   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1864
1865   am->l2_output_classify_next_acl_ip4 =
1866     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1867   am->l2_output_classify_next_acl_ip6 =
1868     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1869
1870   feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1871                                l2output_get_feat_names (),
1872                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1873
1874   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1875                                l2output_get_feat_names (),
1876                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1877 }
1878
1879 static void
1880 acl_set_timeout_sec(int timeout_type, u32 value)
1881 {
1882   acl_main_t *am = &acl_main;
1883   clib_time_t *ct = &am->vlib_main->clib_time;
1884
1885   if (timeout_type < ACL_N_TIMEOUTS) {
1886     am->session_timeout_sec[timeout_type] = value;
1887   } else {
1888     clib_warning("Unknown timeout type %d", timeout_type);
1889     return;
1890   }
1891   am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1892 }
1893
1894 static void
1895 acl_set_session_max_entries(u32 value)
1896 {
1897   acl_main_t *am = &acl_main;
1898   am->fa_conn_table_max_entries = value;
1899 }
1900
1901 static int
1902 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1903 {
1904   acl_main_t *am = &acl_main;
1905
1906   if ((eh < 256) && (value < 2))
1907     {
1908       am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1909       return 1;
1910     }
1911   else
1912     return 0;
1913 }
1914
1915
1916 static clib_error_t *
1917 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1918 {
1919   acl_main_t *am = &acl_main;
1920   if (0 == is_add) {
1921     vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1922                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1923     /* also unapply any ACLs in case the users did not do so. */
1924     macip_acl_interface_del_acl(am, sw_if_index);
1925     acl_interface_reset_inout_acls (sw_if_index, 0);
1926     acl_interface_reset_inout_acls (sw_if_index, 1);
1927   }
1928   return 0;
1929 }
1930
1931 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1932
1933 static clib_error_t *
1934 acl_set_aclplugin_fn (vlib_main_t * vm,
1935                               unformat_input_t * input,
1936                               vlib_cli_command_t * cmd)
1937 {
1938   clib_error_t *error = 0;
1939   u32 timeout = 0;
1940   u32 val = 0;
1941   u32 eh_val = 0;
1942   uword memory_size = 0;
1943   acl_main_t *am = &acl_main;
1944
1945   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1946     if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1947       error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1948     }
1949     goto done;
1950   }
1951   if (unformat (input, "use-hash-acl-matching %u", &val))
1952     {
1953       am->use_hash_acl_matching = (val !=0);
1954       goto done;
1955     }
1956   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1957     {
1958       am->l4_match_nonfirst_fragment = (val != 0);
1959       goto done;
1960     }
1961   if (unformat (input, "session")) {
1962     if (unformat (input, "table")) {
1963       /* The commands here are for tuning/testing. No user-serviceable parts inside */
1964       if (unformat (input, "max-entries")) {
1965         if (!unformat(input, "%u", &val)) {
1966           error = clib_error_return(0,
1967                                     "expecting maximum number of entries, got `%U`",
1968                                     format_unformat_error, input);
1969           goto done;
1970         } else {
1971           acl_set_session_max_entries(val);
1972           goto done;
1973         }
1974       }
1975       if (unformat (input, "hash-table-buckets")) {
1976         if (!unformat(input, "%u", &val)) {
1977           error = clib_error_return(0,
1978                                     "expecting maximum number of hash table buckets, got `%U`",
1979                                     format_unformat_error, input);
1980           goto done;
1981         } else {
1982           am->fa_conn_table_hash_num_buckets = val;
1983           goto done;
1984         }
1985       }
1986       if (unformat (input, "hash-table-memory")) {
1987         if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1988           error = clib_error_return(0,
1989                                     "expecting maximum amount of hash table memory, got `%U`",
1990                                     format_unformat_error, input);
1991           goto done;
1992         } else {
1993           am->fa_conn_table_hash_memory_size = memory_size;
1994           goto done;
1995         }
1996       }
1997       goto done;
1998     }
1999     if (unformat (input, "timeout")) {
2000       if (unformat(input, "udp")) {
2001         if(unformat(input, "idle")) {
2002           if (!unformat(input, "%u", &timeout)) {
2003             error = clib_error_return(0,
2004                                       "expecting timeout value in seconds, got `%U`",
2005                                       format_unformat_error, input);
2006             goto done;
2007           } else {
2008             acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
2009             goto done;
2010           }
2011         }
2012       }
2013       if (unformat(input, "tcp")) {
2014         if(unformat(input, "idle")) {
2015           if (!unformat(input, "%u", &timeout)) {
2016             error = clib_error_return(0,
2017                                       "expecting timeout value in seconds, got `%U`",
2018                                       format_unformat_error, input);
2019             goto done;
2020           } else {
2021             acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
2022             goto done;
2023           }
2024         }
2025         if(unformat(input, "transient")) {
2026           if (!unformat(input, "%u", &timeout)) {
2027             error = clib_error_return(0,
2028                                       "expecting timeout value in seconds, got `%U`",
2029                                       format_unformat_error, input);
2030             goto done;
2031           } else {
2032             acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
2033             goto done;
2034           }
2035         }
2036       }
2037       goto done;
2038     }
2039   }
2040 done:
2041   return error;
2042 }
2043
2044 static clib_error_t *
2045 acl_show_aclplugin_fn (vlib_main_t * vm,
2046                               unformat_input_t * input,
2047                               vlib_cli_command_t * cmd)
2048 {
2049   clib_error_t *error = 0;
2050   acl_main_t *am = &acl_main;
2051   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2052   u32 *pj;
2053
2054   vnet_sw_interface_t *swif;
2055
2056   if (unformat (input, "sessions"))
2057     {
2058       u8 * out0 = format(0, "");
2059       u16 wk;
2060       u32 show_bihash_verbose = 0;
2061       u32 show_session_thread_id = ~0;
2062       u32 show_session_session_index = ~0;
2063       unformat (input, "thread %u index %u", &show_session_thread_id, &show_session_session_index);
2064       unformat (input, "verbose %u", &show_bihash_verbose);
2065       {
2066         u64 n_adds = am->fa_session_total_adds;
2067         u64 n_dels = am->fa_session_total_dels;
2068         out0 = format(out0, "Sessions total: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
2069       }
2070       out0 = format(out0, "\n\nPer-thread data:\n");
2071       for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2072         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2073         out0 = format(out0, "Thread #%d:\n", wk);
2074         if (show_session_thread_id == wk && show_session_session_index < pool_len(pw->fa_sessions_pool)) {
2075           out0 = format(out0, "  session index %u:\n", show_session_session_index);
2076           fa_session_t *sess = pw->fa_sessions_pool + show_session_session_index;
2077           u64 *m =  (u64 *)&sess->info;
2078           out0 = format(out0, "    info: %016llx %016llx %016llx %016llx %016llx %016llx\n", m[0], m[1], m[2], m[3], m[4], m[5]);
2079           out0 = format(out0, "    sw_if_index: %u\n", sess->sw_if_index);
2080           out0 = format(out0, "    tcp_flags_seen: %x\n", sess->tcp_flags_seen.as_u16);
2081           out0 = format(out0, "    last active time: %lu\n", sess->last_active_time);
2082           out0 = format(out0, "    thread index: %u\n", sess->thread_index);
2083           out0 = format(out0, "    link enqueue time: %lu\n", sess->link_enqueue_time);
2084           out0 = format(out0, "    link next index: %u\n", sess->link_next_idx);
2085           out0 = format(out0, "    link prev index: %u\n", sess->link_prev_idx);
2086           out0 = format(out0, "    link list id: %u\n", sess->link_list_id);
2087         }
2088         out0 = format(out0, "  connection add/del stats:\n", wk);
2089         pool_foreach (swif, im->sw_interfaces,
2090         ({
2091           u32 sw_if_index =  swif->sw_if_index;
2092           u64 n_adds = sw_if_index < vec_len(pw->fa_session_adds_by_sw_if_index) ? pw->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
2093           u64 n_dels = sw_if_index < vec_len(pw->fa_session_dels_by_sw_if_index) ? pw->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
2094           out0 = format(out0, "    sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
2095         }));
2096
2097         out0 = format(out0, "  connection timeout type lists:\n", wk);
2098         u8 tt = 0;
2099         for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2100           u32 head_session_index = pw->fa_conn_list_head[tt];
2101           out0 = format(out0, "  fa_conn_list_head[%d]: %d\n", tt, head_session_index);
2102           if (~0 != head_session_index) {
2103             fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
2104             out0 = format(out0, "    last active time: %lu\n", sess->last_active_time);
2105             out0 = format(out0, "    link enqueue time: %lu\n", sess->link_enqueue_time);
2106           }
2107         }
2108
2109         out0 = format(out0, "  Next expiry time: %lu\n", pw->next_expiry_time);
2110         out0 = format(out0, "  Requeue until time: %lu\n", pw->requeue_until_time);
2111         out0 = format(out0, "  Current time wait interval: %lu\n", pw->current_time_wait_interval);
2112         out0 = format(out0, "  Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
2113         out0 = format(out0, "  Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
2114         out0 = format(out0, "  Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
2115         out0 = format(out0, "  Swipe until this time: %lu\n", pw->swipe_end_time);
2116         out0 = format(out0, "  sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
2117         out0 = format(out0, "  pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
2118         out0 = format(out0, "  clear in progress: %u\n", pw->clear_in_process);
2119         out0 = format(out0, "  interrupt is pending: %d\n", pw->interrupt_is_pending);
2120         out0 = format(out0, "  interrupt is needed: %d\n", pw->interrupt_is_needed);
2121         out0 = format(out0, "  interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
2122       }
2123       out0 = format(out0, "\n\nConn cleaner thread counters:\n");
2124 #define _(cnt, desc) out0 = format(out0, "             %20lu: %s\n", am->cnt, desc);
2125       foreach_fa_cleaner_counter;
2126 #undef _
2127       vec_terminate_c_string(out0);
2128       vlib_cli_output(vm, "\n\n%s\n\n", out0);
2129       vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
2130               am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
2131               am->fa_cleaner_wait_time_increment * 1000.0, ((f64)am->fa_current_cleaner_timer_wait_interval) * 1000.0/(f64)vm->clib_time.clocks_per_second);
2132
2133       vec_free(out0);
2134       show_fa_sessions_hash(vm, show_bihash_verbose);
2135     }
2136   else if (unformat (input, "interface"))
2137     {
2138       u32 sw_if_index = ~0;
2139       u32 swi;
2140       u8 * out0 = format(0, "");
2141       unformat (input, "sw_if_index %u", &sw_if_index);
2142       for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
2143                    (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
2144         out0 = format(out0, "sw_if_index %d:\n", swi);
2145
2146         if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
2147             (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
2148           out0 = format(out0, "  input acl(s): ");
2149           vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
2150             out0 = format(out0, "%d ", *pj);
2151           }
2152           out0 = format(out0, "\n");
2153         }
2154
2155         if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
2156             (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2157           out0 = format(out0, "  output acl(s): ");
2158           vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2159             out0 = format(out0, "%d ", *pj);
2160           }
2161           out0 = format(out0, "\n");
2162         }
2163
2164       }
2165       vec_terminate_c_string(out0);
2166       vlib_cli_output(vm, "\n%s\n", out0);
2167       vec_free(out0);
2168     }
2169   else if (unformat (input, "acl"))
2170     {
2171       u32 acl_index = ~0;
2172       u32 i;
2173       u8 * out0 = format(0, "");
2174       unformat (input, "index %u", &acl_index);
2175       for(i=0; i<vec_len(am->acls); i++) {
2176         if (acl_is_not_defined(am, i)) {
2177           /* don't attempt to show the ACLs that do not exist */
2178           continue;
2179         }
2180         if ((acl_index != ~0) && (acl_index != i)) {
2181           continue;
2182         }
2183         out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2184         acl_rule_t *r;
2185         int j;
2186         for(j=0; j<am->acls[i].count; j++) {
2187           r = &am->acls[i].rules[j];
2188           out0 = format(out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2189           out0 = format_acl_action(out0, r->is_permit);
2190           out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2191           out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2192           out0 = format(out0, " proto %d", r->proto);
2193           out0 = format(out0, " sport %d", r->src_port_or_type_first);
2194           if (r->src_port_or_type_first != r->src_port_or_type_last) {
2195             out0 = format(out0, "-%d", r->src_port_or_type_last);
2196           }
2197           out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2198           if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2199             out0 = format(out0, "-%d", r->dst_port_or_code_last);
2200           }
2201           if (r->tcp_flags_mask || r->tcp_flags_value) {
2202             out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2203           }
2204           out0 = format(out0, "\n");
2205         }
2206
2207         if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2208           out0 = format(out0, "  applied inbound on sw_if_index: ");
2209           vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2210             out0 = format(out0, "%d ", *pj);
2211           }
2212           out0 = format(out0, "\n");
2213         }
2214         if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2215           out0 = format(out0, "  applied outbound on sw_if_index: ");
2216           vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2217             out0 = format(out0, "%d ", *pj);
2218           }
2219           out0 = format(out0, "\n");
2220         }
2221       }
2222       vec_terminate_c_string(out0);
2223       vlib_cli_output(vm, "\n%s\n", out0);
2224       vec_free(out0);
2225     }
2226   else if (unformat (input, "memory"))
2227     {
2228       vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
2229       if (am->acl_mheap) {
2230         vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
2231       } else {
2232         vlib_cli_output (vm, " Not initialized\n");
2233       }
2234       vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
2235       if (am->hash_lookup_mheap) {
2236         vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
2237       } else {
2238         vlib_cli_output (vm, " Not initialized\n");
2239       }
2240     }
2241   else if (unformat (input, "tables"))
2242     {
2243       ace_mask_type_entry_t *mte;
2244       u32 acl_index = ~0;
2245       u32 sw_if_index = ~0;
2246       int show_acl_hash_info = 0;
2247       int show_applied_info = 0;
2248       int show_mask_type = 0;
2249       int show_bihash = 0;
2250       u32 show_bihash_verbose = 0;
2251
2252       if (unformat (input, "acl")) {
2253         show_acl_hash_info = 1;
2254         /* mask-type is handy to see as well right there */
2255         show_mask_type = 1;
2256         unformat (input, "index %u", &acl_index);
2257       } else if (unformat (input, "applied")) {
2258         show_applied_info = 1;
2259         unformat (input, "sw_if_index %u", &sw_if_index);
2260       } else if (unformat (input, "mask")) {
2261         show_mask_type = 1;
2262       } else if (unformat (input, "hash")) {
2263         show_bihash = 1;
2264         unformat (input, "verbose %u", &show_bihash_verbose);
2265       }
2266
2267       if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2268         /* if no qualifiers specified, show all */
2269         show_mask_type = 1;
2270         show_acl_hash_info = 1;
2271         show_applied_info = 1;
2272         show_bihash = 1;
2273       }
2274
2275       if (show_mask_type) {
2276         vlib_cli_output(vm, "Mask-type entries:");
2277         /* *INDENT-OFF* */
2278         pool_foreach(mte, am->ace_mask_type_pool,
2279         ({
2280           vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
2281                         mte - am->ace_mask_type_pool,
2282                         mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2283                         mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2284         }));
2285         /* *INDENT-ON* */
2286       }
2287
2288       if (show_acl_hash_info) {
2289         u32 i,j;
2290         u8 * out0 = format(0, "");
2291         u64 *m;
2292         out0 = format(out0, "Mask-ready ACL representations\n");
2293         for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2294           if ((acl_index != ~0) && (acl_index != i)) {
2295             continue;
2296           }
2297           hash_acl_info_t *ha = &am->hash_acl_infos[i];
2298           out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2299           out0 = format(out0, "  mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2300           for(j=0; j<vec_len(ha->rules); j++) {
2301             hash_ace_info_t *pa = &ha->rules[j];
2302             m = (u64 *)&pa->match;
2303             out0 = format(out0, "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
2304                                 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2305                                 pa->acl_index, pa->ace_index, pa->action,
2306                                 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2307           }
2308         }
2309         vec_terminate_c_string(out0);
2310         vlib_cli_output(vm, "\n%s\n", out0);
2311         vec_free(out0);
2312       }
2313
2314       if (show_applied_info) {
2315         u32 swi, j;
2316         u8 * out0 = format(0, "");
2317         out0 = format(out0, "Applied lookup entries for interfaces\n");
2318
2319         for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2320                    (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2321                    (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2322                    (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2323           if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2324             continue;
2325           }
2326           out0 = format(out0, "sw_if_index %d:\n", swi);
2327           if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2328             applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2329             out0 = format(out0, "  input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2330           }
2331           if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2332             out0 = format(out0, "  input lookup applied entries:\n");
2333             for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2334               applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2335               out0 = format(out0, "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2336                                        j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2337                                        pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2338             }
2339           }
2340
2341           if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2342             applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2343             out0 = format(out0, "  output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2344           }
2345           if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2346             out0 = format(out0, "  output lookup applied entries:\n");
2347             for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2348               applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2349               out0 = format(out0, "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d\n",
2350                                        j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2351                                        pae->next_applied_entry_index, pae->prev_applied_entry_index, pae->tail_applied_entry_index);
2352             }
2353           }
2354
2355         }
2356         vec_terminate_c_string(out0);
2357         vlib_cli_output(vm, "\n%s\n", out0);
2358         vec_free(out0);
2359       }
2360
2361       if (show_bihash) {
2362         show_hash_acl_hash(vm, am, show_bihash_verbose);
2363       }
2364     }
2365   return error;
2366 }
2367
2368 static clib_error_t *
2369 acl_clear_aclplugin_fn (vlib_main_t * vm,
2370                               unformat_input_t * input,
2371                               vlib_cli_command_t * cmd)
2372 {
2373   clib_error_t *error = 0;
2374   acl_main_t *am = &acl_main;
2375   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2376                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
2377   return error;
2378 }
2379
2380  /* *INDENT-OFF* */
2381 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2382     .path = "set acl-plugin",
2383     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2384     .function = acl_set_aclplugin_fn,
2385 };
2386
2387 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2388     .path = "show acl-plugin",
2389     .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2390     .function = acl_show_aclplugin_fn,
2391 };
2392
2393 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
2394     .path = "clear acl-plugin sessions",
2395     .short_help = "clear acl-plugin sessions",
2396     .function = acl_clear_aclplugin_fn,
2397 };
2398 /* *INDENT-ON* */
2399
2400
2401
2402 static clib_error_t *
2403 acl_init (vlib_main_t * vm)
2404 {
2405   acl_main_t *am = &acl_main;
2406   clib_error_t *error = 0;
2407   memset (am, 0, sizeof (*am));
2408   am->vlib_main = vm;
2409   am->vnet_main = vnet_get_main ();
2410
2411   u8 *name = format (0, "acl_%08x%c", api_version, 0);
2412
2413   /* Ask for a correctly-sized block of API message decode slots */
2414   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2415                                             VL_MSG_FIRST_AVAILABLE);
2416
2417   error = acl_plugin_api_hookup (vm);
2418
2419  /* Add our API messages to the global name_crc hash table */
2420   setup_message_id_table (am, &api_main);
2421
2422   vec_free (name);
2423
2424   acl_setup_fa_nodes();
2425
2426   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2427   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2428   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2429
2430   am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2431   am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2432   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2433   vlib_thread_main_t *tm = vlib_get_thread_main ();
2434   vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2435   {
2436     u16 wk;
2437     u8 tt;
2438     for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2439       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2440       vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2441       vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2442       for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2443         pw->fa_conn_list_head[tt] = ~0;
2444         pw->fa_conn_list_tail[tt] = ~0;
2445       }
2446     }
2447   }
2448
2449   am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2450   am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2451   am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2452
2453   am->fa_cleaner_cnt_delete_by_sw_index = 0;
2454   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2455   am->fa_cleaner_cnt_unknown_event = 0;
2456   am->fa_cleaner_cnt_timer_restarted = 0;
2457   am->fa_cleaner_cnt_wait_with_timeout = 0;
2458
2459
2460 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2461   foreach_acl_eh
2462 #undef _
2463
2464   am->l4_match_nonfirst_fragment = 1;
2465
2466   /* use the new fancy hash-based matching */
2467   am->use_hash_acl_matching = 1;
2468
2469   return error;
2470 }
2471
2472 VLIB_INIT_FUNCTION (acl_init);