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