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