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