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