acl-plugin: add whitelisted ethertype mode (VPP-1163)
[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 }
1856
1857 static int
1858 macip_maybe_apply_unapply_classifier_tables (acl_main_t * am, u32 acl_index,
1859                                              int is_apply)
1860 {
1861   int rv = 0;
1862   int rv0 = 0;
1863   int i;
1864   macip_acl_list_t *a = pool_elt_at_index (am->macip_acls, acl_index);
1865
1866   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1867     if (vec_elt (am->macip_acl_by_sw_if_index, i) == acl_index)
1868       {
1869         rv0 = vnet_set_input_acl_intfc (am->vlib_main, i, a->ip4_table_index,
1870                                         a->ip6_table_index, a->l2_table_index,
1871                                         is_apply);
1872         /* return the first unhappy outcome but make try to plough through. */
1873         rv = rv || rv0;
1874         rv0 =
1875           vnet_set_output_acl_intfc (am->vlib_main, i, a->out_ip4_table_index,
1876                                      a->out_ip6_table_index,
1877                                      a->out_l2_table_index, is_apply);
1878         /* return the first unhappy outcome but make try to plough through. */
1879         rv = rv || rv0;
1880       }
1881   return rv;
1882 }
1883
1884 static int
1885 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1886                     u32 * acl_list_index, u8 * tag)
1887 {
1888   acl_main_t *am = &acl_main;
1889   macip_acl_list_t *a;
1890   macip_acl_rule_t *r;
1891   macip_acl_rule_t *acl_new_rules = 0;
1892   int i;
1893   int rv = 0;
1894
1895   if (*acl_list_index != ~0)
1896     {
1897       /* They supplied some number, let's see if this MACIP ACL exists */
1898       if (pool_is_free_index (am->macip_acls, *acl_list_index))
1899         {
1900           /* tried to replace a non-existent ACL, no point doing anything */
1901           clib_warning
1902             ("acl-plugin-error: Trying to replace nonexistent MACIP ACL %d (tag %s)",
1903              *acl_list_index, tag);
1904           return VNET_API_ERROR_NO_SUCH_ENTRY;
1905         }
1906     }
1907
1908   if (0 == count)
1909     {
1910       clib_warning
1911         ("acl-plugin-warning: Trying to create empty MACIP ACL (tag %s)",
1912          tag);
1913     }
1914   /* if replacing the ACL, unapply the classifier tables first - they will be gone.. */
1915   if (~0 != *acl_list_index)
1916     rv = macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 0);
1917   void *oldheap = acl_set_heap (am);
1918   /* Create and populate the rules */
1919   if (count > 0)
1920     vec_validate (acl_new_rules, count - 1);
1921
1922   for (i = 0; i < count; i++)
1923     {
1924       r = &acl_new_rules[i];
1925       r->is_permit = rules[i].is_permit;
1926       r->is_ipv6 = rules[i].is_ipv6;
1927       memcpy (&r->src_mac, rules[i].src_mac, 6);
1928       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1929       if (rules[i].is_ipv6)
1930         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1931       else
1932         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1933       r->src_prefixlen = rules[i].src_ip_prefix_len;
1934     }
1935
1936   if (~0 == *acl_list_index)
1937     {
1938       /* Get ACL index */
1939       pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1940       memset (a, 0, sizeof (*a));
1941       /* Will return the newly allocated ACL index */
1942       *acl_list_index = a - am->macip_acls;
1943     }
1944   else
1945     {
1946       a = pool_elt_at_index (am->macip_acls, *acl_list_index);
1947       if (a->rules)
1948         {
1949           vec_free (a->rules);
1950         }
1951       macip_destroy_classify_tables (am, *acl_list_index);
1952     }
1953
1954   a->rules = acl_new_rules;
1955   a->count = count;
1956   memcpy (a->tag, tag, sizeof (a->tag));
1957
1958   /* Create and populate the classifer tables */
1959   macip_create_classify_tables (am, *acl_list_index);
1960   clib_mem_set_heap (oldheap);
1961   /* If the ACL was already applied somewhere, reapply the newly created tables */
1962   rv = rv
1963     || macip_maybe_apply_unapply_classifier_tables (am, *acl_list_index, 1);
1964   return rv;
1965 }
1966
1967
1968 /* No check for validity of sw_if_index - the callers were supposed to validate */
1969
1970 static int
1971 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1972 {
1973   int rv;
1974   u32 macip_acl_index;
1975   macip_acl_list_t *a;
1976   void *oldheap = acl_set_heap (am);
1977   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1978   clib_mem_set_heap (oldheap);
1979   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1980   /* No point in deleting MACIP ACL which is not applied */
1981   if (~0 == macip_acl_index)
1982     return VNET_API_ERROR_NO_SUCH_ENTRY;
1983   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
1984   /* remove the classifier tables off the interface L2 ACL */
1985   rv =
1986     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1987                               a->ip6_table_index, a->l2_table_index, 0);
1988   rv |=
1989     vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
1990                                a->out_ip4_table_index, a->out_ip6_table_index,
1991                                a->out_l2_table_index, 0);
1992   /* Unset the MACIP ACL index */
1993   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1994   return rv;
1995 }
1996
1997 /* No check for validity of sw_if_index - the callers were supposed to validate */
1998
1999 static int
2000 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
2001                              u32 macip_acl_index)
2002 {
2003   macip_acl_list_t *a;
2004   int rv;
2005   if (pool_is_free_index (am->macip_acls, macip_acl_index))
2006     {
2007       return VNET_API_ERROR_NO_SUCH_ENTRY;
2008     }
2009   void *oldheap = acl_set_heap (am);
2010   a = pool_elt_at_index (am->macip_acls, macip_acl_index);
2011   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
2012   clib_mem_set_heap (oldheap);
2013   /* If there already a MACIP ACL applied, unapply it */
2014   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2015     macip_acl_interface_del_acl (am, sw_if_index);
2016   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
2017
2018   /* Apply the classifier tables for L2 ACLs */
2019   rv =
2020     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
2021                               a->ip6_table_index, a->l2_table_index, 1);
2022   rv |=
2023     vnet_set_output_acl_intfc (am->vlib_main, sw_if_index,
2024                                a->out_ip4_table_index, a->out_ip6_table_index,
2025                                a->out_l2_table_index, 1);
2026   return rv;
2027 }
2028
2029 static int
2030 macip_acl_del_list (u32 acl_list_index)
2031 {
2032   acl_main_t *am = &acl_main;
2033   macip_acl_list_t *a;
2034   int i;
2035   if (pool_is_free_index (am->macip_acls, acl_list_index))
2036     {
2037       return VNET_API_ERROR_NO_SUCH_ENTRY;
2038     }
2039
2040   /* delete any references to the ACL */
2041   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
2042     {
2043       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
2044         {
2045           macip_acl_interface_del_acl (am, i);
2046         }
2047     }
2048
2049   void *oldheap = acl_set_heap (am);
2050   /* Now that classifier tables are detached, clean them up */
2051   macip_destroy_classify_tables (am, acl_list_index);
2052
2053   /* now we can delete the ACL itself */
2054   a = pool_elt_at_index (am->macip_acls, acl_list_index);
2055   if (a->rules)
2056     {
2057       vec_free (a->rules);
2058     }
2059   pool_put (am->macip_acls, a);
2060   clib_mem_set_heap (oldheap);
2061   return 0;
2062 }
2063
2064
2065 static int
2066 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
2067                                  u32 acl_list_index)
2068 {
2069   acl_main_t *am = &acl_main;
2070   int rv = -1;
2071   if (is_add)
2072     {
2073       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
2074     }
2075   else
2076     {
2077       rv = macip_acl_interface_del_acl (am, sw_if_index);
2078     }
2079   return rv;
2080 }
2081
2082 /*
2083  * If the client does not allocate enough memory for a variable-length
2084  * message, and then proceed to use it as if the full memory allocated,
2085  * absent the check we happily consume that on the VPP side, and go
2086  * along as if nothing happened. However, the resulting
2087  * effects range from just garbage in the API decode
2088  * (because the decoder snoops too far), to potential memory
2089  * corruptions.
2090  *
2091  * This verifies that the actual length of the message is
2092  * at least expected_len, and complains loudly if it is not.
2093  *
2094  * A failing check here is 100% a software bug on the API user side,
2095  * so we might as well yell.
2096  *
2097  */
2098 static int
2099 verify_message_len (void *mp, u32 expected_len, char *where)
2100 {
2101   u32 supplied_len = vl_msg_api_get_msg_length (mp);
2102   if (supplied_len < expected_len)
2103     {
2104       clib_warning ("%s: Supplied message length %d is less than expected %d",
2105                     where, supplied_len, expected_len);
2106       return 0;
2107     }
2108   else
2109     {
2110       return 1;
2111     }
2112 }
2113
2114 /* API message handler */
2115 static void
2116 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
2117 {
2118   vl_api_acl_add_replace_reply_t *rmp;
2119   acl_main_t *am = &acl_main;
2120   int rv;
2121   u32 acl_list_index = ntohl (mp->acl_index);
2122   u32 acl_count = ntohl (mp->count);
2123   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2124
2125   if (verify_message_len (mp, expected_len, "acl_add_replace"))
2126     {
2127       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2128     }
2129   else
2130     {
2131       rv = VNET_API_ERROR_INVALID_VALUE;
2132     }
2133
2134   /* *INDENT-OFF* */
2135   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
2136   ({
2137     rmp->acl_index = htonl(acl_list_index);
2138   }));
2139   /* *INDENT-ON* */
2140 }
2141
2142 static void
2143 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
2144 {
2145   acl_main_t *am = &acl_main;
2146   vl_api_acl_del_reply_t *rmp;
2147   int rv;
2148
2149   rv = acl_del_list (ntohl (mp->acl_index));
2150
2151   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
2152 }
2153
2154 static void
2155 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
2156 {
2157   acl_main_t *am = &acl_main;
2158   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2159   u32 sw_if_index = ntohl (mp->sw_if_index);
2160   vl_api_acl_interface_add_del_reply_t *rmp;
2161   int rv = -1;
2162
2163   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2164     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2165   else
2166     rv =
2167       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
2168                                        mp->is_input, ntohl (mp->acl_index));
2169
2170   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
2171 }
2172
2173 static void
2174   vl_api_acl_interface_set_acl_list_t_handler
2175   (vl_api_acl_interface_set_acl_list_t * mp)
2176 {
2177   acl_main_t *am = &acl_main;
2178   vl_api_acl_interface_set_acl_list_reply_t *rmp;
2179   int rv = 0;
2180   int i;
2181   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2182   u32 sw_if_index = ntohl (mp->sw_if_index);
2183
2184   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2185     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2186   else
2187     {
2188       acl_interface_reset_inout_acls (sw_if_index, 0);
2189       acl_interface_reset_inout_acls (sw_if_index, 1);
2190
2191       for (i = 0; i < mp->count; i++)
2192         {
2193           if (acl_is_not_defined (am, ntohl (mp->acls[i])))
2194             {
2195               /* ACL does not exist, so we can not apply it */
2196               rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2197             }
2198         }
2199       if (0 == rv)
2200         {
2201           for (i = 0; i < mp->count; i++)
2202             {
2203               acl_interface_add_del_inout_acl (sw_if_index, 1,
2204                                                (i < mp->n_input),
2205                                                ntohl (mp->acls[i]));
2206             }
2207         }
2208     }
2209
2210   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
2211 }
2212
2213 static void
2214 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
2215 {
2216   api_rule->is_permit = r->is_permit;
2217   api_rule->is_ipv6 = r->is_ipv6;
2218   if (r->is_ipv6)
2219     {
2220       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
2221       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
2222     }
2223   else
2224     {
2225       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
2226       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
2227     }
2228   api_rule->src_ip_prefix_len = r->src_prefixlen;
2229   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
2230   api_rule->proto = r->proto;
2231   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
2232   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
2233   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
2234   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
2235   api_rule->tcp_flags_mask = r->tcp_flags_mask;
2236   api_rule->tcp_flags_value = r->tcp_flags_value;
2237 }
2238
2239 static void
2240 send_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2241                   acl_list_t * acl, u32 context)
2242 {
2243   vl_api_acl_details_t *mp;
2244   vl_api_acl_rule_t *rules;
2245   int i;
2246   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
2247   void *oldheap = acl_set_heap (am);
2248
2249   mp = vl_msg_api_alloc (msg_size);
2250   memset (mp, 0, msg_size);
2251   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
2252
2253   /* fill in the message */
2254   mp->context = context;
2255   mp->count = htonl (acl->count);
2256   mp->acl_index = htonl (acl - am->acls);
2257   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2258   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
2259   rules = mp->r;
2260   for (i = 0; i < acl->count; i++)
2261     {
2262       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
2263     }
2264
2265   clib_mem_set_heap (oldheap);
2266   vl_api_send_msg (reg, (u8 *) mp);
2267 }
2268
2269
2270 static void
2271 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
2272 {
2273   acl_main_t *am = &acl_main;
2274   u32 acl_index;
2275   acl_list_t *acl;
2276   int rv = -1;
2277   vl_api_registration_t *reg;
2278
2279   reg = vl_api_client_index_to_registration (mp->client_index);
2280   if (!reg)
2281     return;
2282
2283   if (mp->acl_index == ~0)
2284     {
2285     /* *INDENT-OFF* */
2286     /* Just dump all ACLs */
2287     pool_foreach (acl, am->acls,
2288     ({
2289       send_acl_details(am, reg, acl, mp->context);
2290     }));
2291     /* *INDENT-ON* */
2292     }
2293   else
2294     {
2295       acl_index = ntohl (mp->acl_index);
2296       if (!pool_is_free_index (am->acls, acl_index))
2297         {
2298           acl = pool_elt_at_index (am->acls, acl_index);
2299           send_acl_details (am, reg, acl, mp->context);
2300         }
2301     }
2302
2303   if (rv == -1)
2304     {
2305       /* FIXME API: should we signal an error here at all ? */
2306       return;
2307     }
2308 }
2309
2310 static void
2311 send_acl_interface_list_details (acl_main_t * am,
2312                                  vl_api_registration_t * reg,
2313                                  u32 sw_if_index, u32 context)
2314 {
2315   vl_api_acl_interface_list_details_t *mp;
2316   int msg_size;
2317   int n_input;
2318   int n_output;
2319   int count;
2320   int i = 0;
2321   void *oldheap = acl_set_heap (am);
2322
2323   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
2324   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
2325
2326   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
2327   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
2328   count = n_input + n_output;
2329
2330   msg_size = sizeof (*mp);
2331   msg_size += sizeof (mp->acls[0]) * count;
2332
2333   mp = vl_msg_api_alloc (msg_size);
2334   memset (mp, 0, msg_size);
2335   mp->_vl_msg_id =
2336     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2337
2338   /* fill in the message */
2339   mp->context = context;
2340   mp->sw_if_index = htonl (sw_if_index);
2341   mp->count = count;
2342   mp->n_input = n_input;
2343   for (i = 0; i < n_input; i++)
2344     {
2345       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
2346     }
2347   for (i = 0; i < n_output; i++)
2348     {
2349       mp->acls[n_input + i] =
2350         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
2351     }
2352   clib_mem_set_heap (oldheap);
2353   vl_api_send_msg (reg, (u8 *) mp);
2354 }
2355
2356 static void
2357 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
2358                                           mp)
2359 {
2360   acl_main_t *am = &acl_main;
2361   vnet_sw_interface_t *swif;
2362   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2363
2364   u32 sw_if_index;
2365   vl_api_registration_t *reg;
2366
2367   reg = vl_api_client_index_to_registration (mp->client_index);
2368   if (!reg)
2369     return;
2370
2371   if (mp->sw_if_index == ~0)
2372     {
2373     /* *INDENT-OFF* */
2374     pool_foreach (swif, im->sw_interfaces,
2375     ({
2376       send_acl_interface_list_details(am, reg, swif->sw_if_index, mp->context);
2377     }));
2378     /* *INDENT-ON* */
2379     }
2380   else
2381     {
2382       sw_if_index = ntohl (mp->sw_if_index);
2383       if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
2384         send_acl_interface_list_details (am, reg, sw_if_index, mp->context);
2385     }
2386 }
2387
2388 /* MACIP ACL API handlers */
2389
2390 static void
2391 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
2392 {
2393   vl_api_macip_acl_add_reply_t *rmp;
2394   acl_main_t *am = &acl_main;
2395   int rv;
2396   u32 acl_list_index = ~0;
2397   u32 acl_count = ntohl (mp->count);
2398   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2399
2400   if (verify_message_len (mp, expected_len, "macip_acl_add"))
2401     {
2402       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2403     }
2404   else
2405     {
2406       rv = VNET_API_ERROR_INVALID_VALUE;
2407     }
2408
2409   /* *INDENT-OFF* */
2410   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
2411   ({
2412     rmp->acl_index = htonl(acl_list_index);
2413   }));
2414   /* *INDENT-ON* */
2415 }
2416
2417 static void
2418 vl_api_macip_acl_add_replace_t_handler (vl_api_macip_acl_add_replace_t * mp)
2419 {
2420   vl_api_macip_acl_add_replace_reply_t *rmp;
2421   acl_main_t *am = &acl_main;
2422   int rv;
2423   u32 acl_list_index = ntohl (mp->acl_index);
2424   u32 acl_count = ntohl (mp->count);
2425   u32 expected_len = sizeof (*mp) + acl_count * sizeof (mp->r[0]);
2426
2427   if (verify_message_len (mp, expected_len, "macip_acl_add_replace"))
2428     {
2429       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
2430     }
2431   else
2432     {
2433       rv = VNET_API_ERROR_INVALID_VALUE;
2434     }
2435
2436   /* *INDENT-OFF* */
2437   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLACE_REPLY,
2438   ({
2439     rmp->acl_index = htonl(acl_list_index);
2440   }));
2441   /* *INDENT-ON* */
2442 }
2443
2444 static void
2445 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
2446 {
2447   acl_main_t *am = &acl_main;
2448   vl_api_macip_acl_del_reply_t *rmp;
2449   int rv;
2450
2451   rv = macip_acl_del_list (ntohl (mp->acl_index));
2452
2453   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
2454 }
2455
2456 static void
2457   vl_api_macip_acl_interface_add_del_t_handler
2458   (vl_api_macip_acl_interface_add_del_t * mp)
2459 {
2460   acl_main_t *am = &acl_main;
2461   vl_api_macip_acl_interface_add_del_reply_t *rmp;
2462   int rv = -1;
2463   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2464   u32 sw_if_index = ntohl (mp->sw_if_index);
2465
2466   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2467     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2468   else
2469     rv =
2470       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
2471                                        ntohl (mp->acl_index));
2472
2473   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
2474 }
2475
2476 static void
2477 send_macip_acl_details (acl_main_t * am, vl_api_registration_t * reg,
2478                         macip_acl_list_t * acl, u32 context)
2479 {
2480   vl_api_macip_acl_details_t *mp;
2481   vl_api_macip_acl_rule_t *rules;
2482   macip_acl_rule_t *r;
2483   int i;
2484   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
2485
2486   mp = vl_msg_api_alloc (msg_size);
2487   memset (mp, 0, msg_size);
2488   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
2489
2490   /* fill in the message */
2491   mp->context = context;
2492   if (acl)
2493     {
2494       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
2495       mp->count = htonl (acl->count);
2496       mp->acl_index = htonl (acl - am->macip_acls);
2497       rules = mp->r;
2498       for (i = 0; i < acl->count; i++)
2499         {
2500           r = &acl->rules[i];
2501           rules[i].is_permit = r->is_permit;
2502           rules[i].is_ipv6 = r->is_ipv6;
2503           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
2504           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
2505                   sizeof (r->src_mac_mask));
2506           if (r->is_ipv6)
2507             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
2508                     sizeof (r->src_ip_addr.ip6));
2509           else
2510             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
2511                     sizeof (r->src_ip_addr.ip4));
2512           rules[i].src_ip_prefix_len = r->src_prefixlen;
2513         }
2514     }
2515   else
2516     {
2517       /* No martini, no party - no ACL applied to this interface. */
2518       mp->acl_index = ~0;
2519       mp->count = 0;
2520     }
2521
2522   vl_api_send_msg (reg, (u8 *) mp);
2523 }
2524
2525
2526 static void
2527 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
2528 {
2529   acl_main_t *am = &acl_main;
2530   macip_acl_list_t *acl;
2531
2532   vl_api_registration_t *reg;
2533
2534   reg = vl_api_client_index_to_registration (mp->client_index);
2535   if (!reg)
2536     return;
2537
2538   if (mp->acl_index == ~0)
2539     {
2540       /* Just dump all ACLs for now, with sw_if_index = ~0 */
2541       pool_foreach (acl, am->macip_acls, (
2542                                            {
2543                                            send_macip_acl_details (am, reg,
2544                                                                    acl,
2545                                                                    mp->context);
2546                                            }
2547                     ));
2548       /* *INDENT-ON* */
2549     }
2550   else
2551     {
2552       u32 acl_index = ntohl (mp->acl_index);
2553       if (!pool_is_free_index (am->macip_acls, acl_index))
2554         {
2555           acl = pool_elt_at_index (am->macip_acls, acl_index);
2556           send_macip_acl_details (am, reg, acl, mp->context);
2557         }
2558     }
2559 }
2560
2561 static void
2562 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
2563                                           mp)
2564 {
2565   acl_main_t *am = &acl_main;
2566   vl_api_macip_acl_interface_get_reply_t *rmp;
2567   u32 count = vec_len (am->macip_acl_by_sw_if_index);
2568   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
2569   vl_api_registration_t *reg;
2570   int i;
2571
2572   reg = vl_api_client_index_to_registration (mp->client_index);
2573   if (!reg)
2574     return;
2575
2576   rmp = vl_msg_api_alloc (msg_size);
2577   memset (rmp, 0, msg_size);
2578   rmp->_vl_msg_id =
2579     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
2580   rmp->context = mp->context;
2581   rmp->count = htonl (count);
2582   for (i = 0; i < count; i++)
2583     {
2584       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
2585     }
2586
2587   vl_api_send_msg (reg, (u8 *) rmp);
2588 }
2589
2590 static void
2591 send_macip_acl_interface_list_details (acl_main_t * am,
2592                                        vl_api_registration_t * reg,
2593                                        u32 sw_if_index,
2594                                        u32 acl_index, u32 context)
2595 {
2596   vl_api_macip_acl_interface_list_details_t *rmp;
2597   /* at this time there is only ever 1 mac ip acl per interface */
2598   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]);
2599
2600   rmp = vl_msg_api_alloc (msg_size);
2601   memset (rmp, 0, msg_size);
2602   rmp->_vl_msg_id =
2603     ntohs (VL_API_MACIP_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
2604
2605   /* fill in the message */
2606   rmp->context = context;
2607   rmp->count = 1;
2608   rmp->sw_if_index = htonl (sw_if_index);
2609   rmp->acls[0] = htonl (acl_index);
2610
2611   vl_api_send_msg (reg, (u8 *) rmp);
2612 }
2613
2614 static void
2615   vl_api_macip_acl_interface_list_dump_t_handler
2616   (vl_api_macip_acl_interface_list_dump_t * mp)
2617 {
2618   vl_api_registration_t *reg;
2619   acl_main_t *am = &acl_main;
2620   u32 sw_if_index = ntohl (mp->sw_if_index);
2621
2622   reg = vl_api_client_index_to_registration (mp->client_index);
2623   if (!reg)
2624     return;
2625
2626   if (sw_if_index == ~0)
2627     {
2628       vec_foreach_index (sw_if_index, am->macip_acl_by_sw_if_index)
2629       {
2630         if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
2631           {
2632             send_macip_acl_interface_list_details (am, reg, sw_if_index,
2633                                                    am->macip_acl_by_sw_if_index
2634                                                    [sw_if_index],
2635                                                    mp->context);
2636           }
2637       }
2638     }
2639   else
2640     {
2641       if (vec_len (am->macip_acl_by_sw_if_index) > sw_if_index)
2642         {
2643           send_macip_acl_interface_list_details (am, reg, sw_if_index,
2644                                                  am->macip_acl_by_sw_if_index
2645                                                  [sw_if_index], mp->context);
2646         }
2647     }
2648 }
2649
2650 static void
2651   vl_api_acl_interface_set_etype_whitelist_t_handler
2652   (vl_api_acl_interface_set_etype_whitelist_t * mp)
2653 {
2654   acl_main_t *am = &acl_main;
2655   vl_api_acl_interface_set_etype_whitelist_reply_t *rmp;
2656   int rv = 0;
2657   int i;
2658   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2659   u32 sw_if_index = ntohl (mp->sw_if_index);
2660   u16 *vec_in = 0, *vec_out = 0;
2661   void *oldheap = acl_set_heap (am);
2662
2663   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2664     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2665   else
2666     {
2667       for (i = 0; i < mp->count; i++)
2668         {
2669           if (i < mp->n_input)
2670             vec_add1 (vec_in, ntohs (mp->whitelist[i]));
2671           else
2672             vec_add1 (vec_out, ntohs (mp->whitelist[i]));
2673         }
2674       rv = acl_set_etype_whitelists (am, sw_if_index, vec_in, vec_out);
2675     }
2676
2677   clib_mem_set_heap (oldheap);
2678   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ETYPE_WHITELIST_REPLY);
2679 }
2680
2681
2682 /* Set up the API message handling tables */
2683 static clib_error_t *
2684 acl_plugin_api_hookup (vlib_main_t * vm)
2685 {
2686   acl_main_t *am = &acl_main;
2687 #define _(N,n)                                                  \
2688     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
2689                            #n,                                  \
2690                            vl_api_##n##_t_handler,              \
2691                            vl_noop_handler,                     \
2692                            vl_api_##n##_t_endian,               \
2693                            vl_api_##n##_t_print,                \
2694                            sizeof(vl_api_##n##_t), 1);
2695   foreach_acl_plugin_api_msg;
2696 #undef _
2697
2698   return 0;
2699 }
2700
2701 #define vl_msg_name_crc_list
2702 #include <acl/acl_all_api_h.h>
2703 #undef vl_msg_name_crc_list
2704
2705 static void
2706 setup_message_id_table (acl_main_t * am, api_main_t * apim)
2707 {
2708 #define _(id,n,crc) \
2709   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
2710   foreach_vl_msg_name_crc_acl;
2711 #undef _
2712 }
2713
2714 static void
2715 acl_setup_fa_nodes (void)
2716 {
2717   vlib_main_t *vm = vlib_get_main ();
2718   acl_main_t *am = &acl_main;
2719   vlib_node_t *n, *n4, *n6;
2720
2721   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
2722   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
2723   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
2724
2725
2726   am->l2_input_classify_next_acl_ip4 =
2727     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2728   am->l2_input_classify_next_acl_ip6 =
2729     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
2730
2731   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
2732                                l2input_get_feat_names (),
2733                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
2734
2735   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
2736                                l2input_get_feat_names (),
2737                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
2738
2739
2740   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
2741   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
2742   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
2743
2744   am->l2_output_classify_next_acl_ip4 =
2745     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
2746   am->l2_output_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, L2OUTPUT_N_FEAT,
2750                                l2output_get_feat_names (),
2751                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
2752
2753   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
2754                                l2output_get_feat_names (),
2755                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
2756 }
2757
2758 static void
2759 acl_set_timeout_sec (int timeout_type, u32 value)
2760 {
2761   acl_main_t *am = &acl_main;
2762   clib_time_t *ct = &am->vlib_main->clib_time;
2763
2764   if (timeout_type < ACL_N_TIMEOUTS)
2765     {
2766       am->session_timeout_sec[timeout_type] = value;
2767     }
2768   else
2769     {
2770       clib_warning ("Unknown timeout type %d", timeout_type);
2771       return;
2772     }
2773   am->session_timeout[timeout_type] =
2774     (u64) (((f64) value) / ct->seconds_per_clock);
2775 }
2776
2777 static void
2778 acl_set_session_max_entries (u32 value)
2779 {
2780   acl_main_t *am = &acl_main;
2781   am->fa_conn_table_max_entries = value;
2782 }
2783
2784 static int
2785 acl_set_skip_ipv6_eh (u32 eh, u32 value)
2786 {
2787   acl_main_t *am = &acl_main;
2788
2789   if ((eh < 256) && (value < 2))
2790     {
2791       am->fa_ipv6_known_eh_bitmap =
2792         clib_bitmap_set (am->fa_ipv6_known_eh_bitmap, eh, value);
2793       return 1;
2794     }
2795   else
2796     return 0;
2797 }
2798
2799
2800 static clib_error_t *
2801 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
2802 {
2803   acl_main_t *am = &acl_main;
2804   if (0 == am->acl_mheap)
2805     {
2806       /* ACL heap is not initialized, so definitely nothing to do. */
2807       return 0;
2808     }
2809   if (0 == is_add)
2810     {
2811       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
2812                                  ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
2813                                  sw_if_index);
2814       /* also unapply any ACLs in case the users did not do so. */
2815       macip_acl_interface_del_acl (am, sw_if_index);
2816       acl_interface_reset_inout_acls (sw_if_index, 0);
2817       acl_interface_reset_inout_acls (sw_if_index, 1);
2818     }
2819   return 0;
2820 }
2821
2822 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
2823
2824
2825
2826 static clib_error_t *
2827 acl_set_aclplugin_fn (vlib_main_t * vm,
2828                       unformat_input_t * input, vlib_cli_command_t * cmd)
2829 {
2830   clib_error_t *error = 0;
2831   u32 timeout = 0;
2832   u32 val = 0;
2833   u32 eh_val = 0;
2834   uword memory_size = 0;
2835   acl_main_t *am = &acl_main;
2836
2837   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val))
2838     {
2839       if (!acl_set_skip_ipv6_eh (eh_val, val))
2840         {
2841           error = clib_error_return (0, "expecting eh=0..255, value=0..1");
2842         }
2843       goto done;
2844     }
2845   if (unformat (input, "use-hash-acl-matching %u", &val))
2846     {
2847       am->use_hash_acl_matching = (val != 0);
2848       goto done;
2849     }
2850   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
2851     {
2852       am->l4_match_nonfirst_fragment = (val != 0);
2853       goto done;
2854     }
2855   if (unformat (input, "heap"))
2856     {
2857       if (unformat (input, "main"))
2858         {
2859           if (unformat (input, "validate %u", &val))
2860             acl_plugin_acl_set_validate_heap (am, val);
2861           else if (unformat (input, "trace %u", &val))
2862             acl_plugin_acl_set_trace_heap (am, val);
2863           goto done;
2864         }
2865       else if (unformat (input, "hash"))
2866         {
2867           if (unformat (input, "validate %u", &val))
2868             acl_plugin_hash_acl_set_validate_heap (am, val);
2869           else if (unformat (input, "trace %u", &val))
2870             acl_plugin_hash_acl_set_trace_heap (am, val);
2871           goto done;
2872         }
2873       goto done;
2874     }
2875   if (unformat (input, "session"))
2876     {
2877       if (unformat (input, "table"))
2878         {
2879           /* The commands here are for tuning/testing. No user-serviceable parts inside */
2880           if (unformat (input, "max-entries"))
2881             {
2882               if (!unformat (input, "%u", &val))
2883                 {
2884                   error = clib_error_return (0,
2885                                              "expecting maximum number of entries, got `%U`",
2886                                              format_unformat_error, input);
2887                   goto done;
2888                 }
2889               else
2890                 {
2891                   acl_set_session_max_entries (val);
2892                   goto done;
2893                 }
2894             }
2895           if (unformat (input, "hash-table-buckets"))
2896             {
2897               if (!unformat (input, "%u", &val))
2898                 {
2899                   error = clib_error_return (0,
2900                                              "expecting maximum number of hash table buckets, got `%U`",
2901                                              format_unformat_error, input);
2902                   goto done;
2903                 }
2904               else
2905                 {
2906                   am->fa_conn_table_hash_num_buckets = val;
2907                   goto done;
2908                 }
2909             }
2910           if (unformat (input, "hash-table-memory"))
2911             {
2912               if (!unformat (input, "%U", unformat_memory_size, &memory_size))
2913                 {
2914                   error = clib_error_return (0,
2915                                              "expecting maximum amount of hash table memory, got `%U`",
2916                                              format_unformat_error, input);
2917                   goto done;
2918                 }
2919               else
2920                 {
2921                   am->fa_conn_table_hash_memory_size = memory_size;
2922                   goto done;
2923                 }
2924             }
2925           if (unformat (input, "event-trace"))
2926             {
2927               if (!unformat (input, "%u", &val))
2928                 {
2929                   error = clib_error_return (0,
2930                                              "expecting trace level, got `%U`",
2931                                              format_unformat_error, input);
2932                   goto done;
2933                 }
2934               else
2935                 {
2936                   am->trace_sessions = val;
2937                   goto done;
2938                 }
2939             }
2940           goto done;
2941         }
2942       if (unformat (input, "timeout"))
2943         {
2944           if (unformat (input, "udp"))
2945             {
2946               if (unformat (input, "idle"))
2947                 {
2948                   if (!unformat (input, "%u", &timeout))
2949                     {
2950                       error = clib_error_return (0,
2951                                                  "expecting timeout value in seconds, got `%U`",
2952                                                  format_unformat_error,
2953                                                  input);
2954                       goto done;
2955                     }
2956                   else
2957                     {
2958                       acl_set_timeout_sec (ACL_TIMEOUT_UDP_IDLE, timeout);
2959                       goto done;
2960                     }
2961                 }
2962             }
2963           if (unformat (input, "tcp"))
2964             {
2965               if (unformat (input, "idle"))
2966                 {
2967                   if (!unformat (input, "%u", &timeout))
2968                     {
2969                       error = clib_error_return (0,
2970                                                  "expecting timeout value in seconds, got `%U`",
2971                                                  format_unformat_error,
2972                                                  input);
2973                       goto done;
2974                     }
2975                   else
2976                     {
2977                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_IDLE, timeout);
2978                       goto done;
2979                     }
2980                 }
2981               if (unformat (input, "transient"))
2982                 {
2983                   if (!unformat (input, "%u", &timeout))
2984                     {
2985                       error = clib_error_return (0,
2986                                                  "expecting timeout value in seconds, got `%U`",
2987                                                  format_unformat_error,
2988                                                  input);
2989                       goto done;
2990                     }
2991                   else
2992                     {
2993                       acl_set_timeout_sec (ACL_TIMEOUT_TCP_TRANSIENT,
2994                                            timeout);
2995                       goto done;
2996                     }
2997                 }
2998             }
2999           goto done;
3000         }
3001     }
3002 done:
3003   return error;
3004 }
3005
3006 static u8 *
3007 my_format_mac_address (u8 * s, va_list * args)
3008 {
3009   u8 *a = va_arg (*args, u8 *);
3010   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
3011                  a[0], a[1], a[2], a[3], a[4], a[5]);
3012 }
3013
3014 static inline u8 *
3015 my_macip_acl_rule_t_pretty_format (u8 * out, va_list * args)
3016 {
3017   macip_acl_rule_t *a = va_arg (*args, macip_acl_rule_t *);
3018
3019   out = format (out, "%s action %d ip %U/%d mac %U mask %U",
3020                 a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
3021                 format_ip46_address, &a->src_ip_addr,
3022                 a->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3023                 a->src_prefixlen,
3024                 my_format_mac_address, a->src_mac,
3025                 my_format_mac_address, a->src_mac_mask);
3026   return (out);
3027 }
3028
3029 static void
3030 macip_acl_print (acl_main_t * am, u32 macip_acl_index)
3031 {
3032   vlib_main_t *vm = am->vlib_main;
3033   int i;
3034
3035   /* Don't try to print someone else's memory */
3036   if (macip_acl_index > vec_len (am->macip_acls))
3037     return;
3038
3039   macip_acl_list_t *a = vec_elt_at_index (am->macip_acls, macip_acl_index);
3040   int free_pool_slot = pool_is_free_index (am->macip_acls, macip_acl_index);
3041
3042   vlib_cli_output (vm,
3043                    "MACIP acl_index: %d, count: %d (true len %d) tag {%s} is free pool slot: %d\n",
3044                    macip_acl_index, a->count, vec_len (a->rules), a->tag,
3045                    free_pool_slot);
3046   vlib_cli_output (vm,
3047                    "  ip4_table_index %d, ip6_table_index %d, l2_table_index %d\n",
3048                    a->ip4_table_index, a->ip6_table_index, a->l2_table_index);
3049   vlib_cli_output (vm,
3050                    "  out_ip4_table_index %d, out_ip6_table_index %d, out_l2_table_index %d\n",
3051                    a->out_ip4_table_index, a->out_ip6_table_index,
3052                    a->out_l2_table_index);
3053   for (i = 0; i < vec_len (a->rules); i++)
3054     vlib_cli_output (vm, "    rule %d: %U\n", i,
3055                      my_macip_acl_rule_t_pretty_format,
3056                      vec_elt_at_index (a->rules, i));
3057
3058 }
3059
3060 static clib_error_t *
3061 acl_show_aclplugin_macip_acl_fn (vlib_main_t * vm,
3062                                  unformat_input_t *
3063                                  input, vlib_cli_command_t * cmd)
3064 {
3065   clib_error_t *error = 0;
3066   acl_main_t *am = &acl_main;
3067   int i;
3068   for (i = 0; i < vec_len (am->macip_acls); i++)
3069     macip_acl_print (am, i);
3070   return error;
3071 }
3072
3073 static clib_error_t *
3074 acl_show_aclplugin_macip_interface_fn (vlib_main_t * vm,
3075                                        unformat_input_t *
3076                                        input, vlib_cli_command_t * cmd)
3077 {
3078   clib_error_t *error = 0;
3079   acl_main_t *am = &acl_main;
3080   int i;
3081   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
3082     {
3083       vlib_cli_output (vm, "  sw_if_index %d: %d\n", i,
3084                        vec_elt (am->macip_acl_by_sw_if_index, i));
3085     }
3086   return error;
3087 }
3088
3089 #define PRINT_AND_RESET(vm, out0) do { vlib_cli_output(vm, "%v", out0); vec_reset_length(out0); } while(0)
3090 static void
3091 acl_print_acl (vlib_main_t * vm, acl_main_t * am, int acl_index)
3092 {
3093   acl_rule_t *r;
3094   u8 *out0 = format (0, "acl-index %u count %u tag {%s}\n", acl_index,
3095                      am->acls[acl_index].count, am->acls[acl_index].tag);
3096   int j;
3097   PRINT_AND_RESET (vm, out0);
3098   for (j = 0; j < am->acls[acl_index].count; j++)
3099     {
3100       r = &am->acls[acl_index].rules[j];
3101       out0 = format (out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
3102       out0 = format_acl_action (out0, r->is_permit);
3103       out0 = format (out0, " src %U/%d", format_ip46_address, &r->src,
3104                      r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4,
3105                      r->src_prefixlen);
3106       out0 =
3107         format (out0, " dst %U/%d", format_ip46_address, &r->dst,
3108                 r->is_ipv6 ? IP46_TYPE_IP6 : IP46_TYPE_IP4, r->dst_prefixlen);
3109       out0 = format (out0, " proto %d", r->proto);
3110       out0 = format (out0, " sport %d", r->src_port_or_type_first);
3111       if (r->src_port_or_type_first != r->src_port_or_type_last)
3112         {
3113           out0 = format (out0, "-%d", r->src_port_or_type_last);
3114         }
3115       out0 = format (out0, " dport %d", r->dst_port_or_code_first);
3116       if (r->dst_port_or_code_first != r->dst_port_or_code_last)
3117         {
3118           out0 = format (out0, "-%d", r->dst_port_or_code_last);
3119         }
3120       if (r->tcp_flags_mask || r->tcp_flags_value)
3121         {
3122           out0 =
3123             format (out0, " tcpflags %d mask %d", r->tcp_flags_value,
3124                     r->tcp_flags_mask);
3125         }
3126       out0 = format (out0, "\n");
3127       PRINT_AND_RESET (vm, out0);
3128     }
3129 }
3130
3131 #undef PRINT_AND_RESET
3132
3133 static void
3134 acl_plugin_show_acl (acl_main_t * am, u32 acl_index)
3135 {
3136   u32 i;
3137   vlib_main_t *vm = am->vlib_main;
3138
3139   for (i = 0; i < vec_len (am->acls); i++)
3140     {
3141       if (acl_is_not_defined (am, i))
3142         {
3143           /* don't attempt to show the ACLs that do not exist */
3144           continue;
3145         }
3146       if ((acl_index != ~0) && (acl_index != i))
3147         {
3148           continue;
3149         }
3150       acl_print_acl (vm, am, i);
3151
3152       if (i < vec_len (am->input_sw_if_index_vec_by_acl))
3153         {
3154           vlib_cli_output (vm, "  applied inbound on sw_if_index: %U\n",
3155                            format_vec32, am->input_sw_if_index_vec_by_acl[i],
3156                            "%d");
3157         }
3158       if (i < vec_len (am->output_sw_if_index_vec_by_acl))
3159         {
3160           vlib_cli_output (vm, "  applied outbound on sw_if_index: %U\n",
3161                            format_vec32, am->output_sw_if_index_vec_by_acl[i],
3162                            "%d");
3163         }
3164     }
3165 }
3166
3167 static clib_error_t *
3168 acl_show_aclplugin_acl_fn (vlib_main_t * vm,
3169                            unformat_input_t * input, vlib_cli_command_t * cmd)
3170 {
3171   clib_error_t *error = 0;
3172   acl_main_t *am = &acl_main;
3173
3174   u32 acl_index = ~0;
3175   (void) unformat (input, "index %u", &acl_index);
3176
3177   acl_plugin_show_acl (am, acl_index);
3178   return error;
3179 }
3180
3181 static void
3182 acl_plugin_show_interface (acl_main_t * am, u32 sw_if_index, int show_acl)
3183 {
3184   vlib_main_t *vm = am->vlib_main;
3185   u32 swi;
3186   u32 *pj;
3187   for (swi = 0; (swi < vec_len (am->input_acl_vec_by_sw_if_index)) ||
3188        (swi < vec_len (am->output_acl_vec_by_sw_if_index)); swi++)
3189     {
3190       /* if we need a particular interface, skip all the others */
3191       if ((sw_if_index != ~0) && (sw_if_index != swi))
3192         continue;
3193
3194       vlib_cli_output (vm, "sw_if_index %d:\n", swi);
3195
3196       if (intf_has_etype_whitelist (am, swi, 1))
3197         {
3198           vlib_cli_output (vm, "  input etype whitelist: %U", format_vec16,
3199                            am->input_etype_whitelist_by_sw_if_index[swi],
3200                            "%04x");
3201         }
3202       if (intf_has_etype_whitelist (am, swi, 0))
3203         {
3204           vlib_cli_output (vm, " output etype whitelist: %U", format_vec16,
3205                            am->output_etype_whitelist_by_sw_if_index[swi],
3206                            "%04x");
3207         }
3208
3209       if ((swi < vec_len (am->input_acl_vec_by_sw_if_index)) &&
3210           (vec_len (am->input_acl_vec_by_sw_if_index[swi]) > 0))
3211         {
3212           vlib_cli_output (vm, "  input acl(s): %U", format_vec32,
3213                            am->input_acl_vec_by_sw_if_index[swi], "%d");
3214           if (show_acl)
3215             {
3216               vlib_cli_output (vm, "\n");
3217               vec_foreach (pj, am->input_acl_vec_by_sw_if_index[swi])
3218               {
3219                 acl_print_acl (vm, am, *pj);
3220               }
3221               vlib_cli_output (vm, "\n");
3222             }
3223         }
3224
3225       if ((swi < vec_len (am->output_acl_vec_by_sw_if_index)) &&
3226           (vec_len (am->output_acl_vec_by_sw_if_index[swi]) > 0))
3227         {
3228           vlib_cli_output (vm, "  output acl(s): %U", format_vec32,
3229                            am->output_acl_vec_by_sw_if_index[swi], "%d");
3230           if (show_acl)
3231             {
3232               vlib_cli_output (vm, "\n");
3233               vec_foreach (pj, am->output_acl_vec_by_sw_if_index[swi])
3234               {
3235                 acl_print_acl (vm, am, *pj);
3236               }
3237               vlib_cli_output (vm, "\n");
3238             }
3239         }
3240     }
3241
3242 }
3243
3244
3245 static clib_error_t *
3246 acl_show_aclplugin_decode_5tuple_fn (vlib_main_t * vm,
3247                                      unformat_input_t * input,
3248                                      vlib_cli_command_t * cmd)
3249 {
3250   clib_error_t *error = 0;
3251   u64 five_tuple[6] = { 0, 0, 0, 0, 0, 0 };
3252
3253   if (unformat
3254       (input, "%llx %llx %llx %llx %llx %llx", &five_tuple[0], &five_tuple[1],
3255        &five_tuple[2], &five_tuple[3], &five_tuple[4], &five_tuple[5]))
3256     vlib_cli_output (vm, "5-tuple structure decode: %U\n\n",
3257                      format_acl_plugin_5tuple, five_tuple);
3258   else
3259     error = clib_error_return (0, "expecting 6 hex integers");
3260   return error;
3261 }
3262
3263
3264 static clib_error_t *
3265 acl_show_aclplugin_interface_fn (vlib_main_t * vm,
3266                                  unformat_input_t *
3267                                  input, vlib_cli_command_t * cmd)
3268 {
3269   clib_error_t *error = 0;
3270   acl_main_t *am = &acl_main;
3271
3272   u32 sw_if_index = ~0;
3273   (void) unformat (input, "sw_if_index %u", &sw_if_index);
3274   int show_acl = unformat (input, "acl");
3275
3276   acl_plugin_show_interface (am, sw_if_index, show_acl);
3277   return error;
3278 }
3279
3280 static clib_error_t *
3281 acl_show_aclplugin_memory_fn (vlib_main_t * vm,
3282                               unformat_input_t * input,
3283                               vlib_cli_command_t * cmd)
3284 {
3285   clib_error_t *error = 0;
3286   acl_main_t *am = &acl_main;
3287
3288   vlib_cli_output (vm, "ACL plugin main heap statistics:\n");
3289   if (am->acl_mheap)
3290     {
3291       vlib_cli_output (vm, " %U\n", format_mheap, am->acl_mheap, 1);
3292     }
3293   else
3294     {
3295       vlib_cli_output (vm, " Not initialized\n");
3296     }
3297   vlib_cli_output (vm, "ACL hash lookup support heap statistics:\n");
3298   if (am->hash_lookup_mheap)
3299     {
3300       vlib_cli_output (vm, " %U\n", format_mheap, am->hash_lookup_mheap, 1);
3301     }
3302   else
3303     {
3304       vlib_cli_output (vm, " Not initialized\n");
3305     }
3306   return error;
3307 }
3308
3309 static void
3310 acl_plugin_show_sessions (acl_main_t * am,
3311                           u32 show_session_thread_id,
3312                           u32 show_session_session_index)
3313 {
3314   vlib_main_t *vm = am->vlib_main;
3315   u16 wk;
3316   vnet_interface_main_t *im = &am->vnet_main->interface_main;
3317   vnet_sw_interface_t *swif;
3318
3319   {
3320     u64 n_adds = am->fa_session_total_adds;
3321     u64 n_dels = am->fa_session_total_dels;
3322     vlib_cli_output (vm, "Sessions total: add %lu - del %lu = %lu", n_adds,
3323                      n_dels, n_adds - n_dels);
3324   }
3325   vlib_cli_output (vm, "\n\nPer-thread data:");
3326   for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3327     {
3328       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3329       vlib_cli_output (vm, "Thread #%d:", wk);
3330       if (show_session_thread_id == wk
3331           && show_session_session_index < pool_len (pw->fa_sessions_pool))
3332         {
3333           vlib_cli_output (vm, "  session index %u:",
3334                            show_session_session_index);
3335           fa_session_t *sess =
3336             pw->fa_sessions_pool + show_session_session_index;
3337           u64 *m = (u64 *) & sess->info;
3338           vlib_cli_output (vm,
3339                            "    info: %016llx %016llx %016llx %016llx %016llx %016llx",
3340                            m[0], m[1], m[2], m[3], m[4], m[5]);
3341           vlib_cli_output (vm, "    sw_if_index: %u", sess->sw_if_index);
3342           vlib_cli_output (vm, "    tcp_flags_seen: %x",
3343                            sess->tcp_flags_seen.as_u16);
3344           vlib_cli_output (vm, "    last active time: %lu",
3345                            sess->last_active_time);
3346           vlib_cli_output (vm, "    thread index: %u", sess->thread_index);
3347           vlib_cli_output (vm, "    link enqueue time: %lu",
3348                            sess->link_enqueue_time);
3349           vlib_cli_output (vm, "    link next index: %u",
3350                            sess->link_next_idx);
3351           vlib_cli_output (vm, "    link prev index: %u",
3352                            sess->link_prev_idx);
3353           vlib_cli_output (vm, "    link list id: %u", sess->link_list_id);
3354         }
3355       vlib_cli_output (vm, "  connection add/del stats:", wk);
3356       pool_foreach (swif, im->sw_interfaces, (
3357                                                {
3358                                                u32 sw_if_index =
3359                                                swif->sw_if_index;
3360                                                u64 n_adds =
3361                                                sw_if_index <
3362                                                vec_len
3363                                                (pw->fa_session_adds_by_sw_if_index)
3364                                                ?
3365                                                pw->fa_session_adds_by_sw_if_index
3366                                                [sw_if_index] : 0;
3367                                                u64 n_dels =
3368                                                sw_if_index <
3369                                                vec_len
3370                                                (pw->fa_session_dels_by_sw_if_index)
3371                                                ?
3372                                                pw->fa_session_dels_by_sw_if_index
3373                                                [sw_if_index] : 0;
3374                                                vlib_cli_output (vm,
3375                                                                 "    sw_if_index %d: add %lu - del %lu = %lu",
3376                                                                 sw_if_index,
3377                                                                 n_adds,
3378                                                                 n_dels,
3379                                                                 n_adds -
3380                                                                 n_dels);
3381                                                }
3382                     ));
3383
3384       vlib_cli_output (vm, "  connection timeout type lists:", wk);
3385       u8 tt = 0;
3386       for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3387         {
3388           u32 head_session_index = pw->fa_conn_list_head[tt];
3389           vlib_cli_output (vm, "  fa_conn_list_head[%d]: %d", tt,
3390                            head_session_index);
3391           if (~0 != head_session_index)
3392             {
3393               fa_session_t *sess = pw->fa_sessions_pool + head_session_index;
3394               vlib_cli_output (vm, "    last active time: %lu",
3395                                sess->last_active_time);
3396               vlib_cli_output (vm, "    link enqueue time: %lu",
3397                                sess->link_enqueue_time);
3398             }
3399         }
3400
3401       vlib_cli_output (vm, "  Next expiry time: %lu", pw->next_expiry_time);
3402       vlib_cli_output (vm, "  Requeue until time: %lu",
3403                        pw->requeue_until_time);
3404       vlib_cli_output (vm, "  Current time wait interval: %lu",
3405                        pw->current_time_wait_interval);
3406       vlib_cli_output (vm, "  Count of deleted sessions: %lu",
3407                        pw->cnt_deleted_sessions);
3408       vlib_cli_output (vm, "  Delete already deleted: %lu",
3409                        pw->cnt_already_deleted_sessions);
3410       vlib_cli_output (vm, "  Session timers restarted: %lu",
3411                        pw->cnt_session_timer_restarted);
3412       vlib_cli_output (vm, "  Swipe until this time: %lu",
3413                        pw->swipe_end_time);
3414       vlib_cli_output (vm, "  sw_if_index serviced bitmap: %U",
3415                        format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
3416       vlib_cli_output (vm, "  pending clear intfc bitmap : %U",
3417                        format_bitmap_hex,
3418                        pw->pending_clear_sw_if_index_bitmap);
3419       vlib_cli_output (vm, "  clear in progress: %u", pw->clear_in_process);
3420       vlib_cli_output (vm, "  interrupt is pending: %d",
3421                        pw->interrupt_is_pending);
3422       vlib_cli_output (vm, "  interrupt is needed: %d",
3423                        pw->interrupt_is_needed);
3424       vlib_cli_output (vm, "  interrupt is unwanted: %d",
3425                        pw->interrupt_is_unwanted);
3426       vlib_cli_output (vm, "  interrupt generation: %d",
3427                        pw->interrupt_generation);
3428     }
3429   vlib_cli_output (vm, "\n\nConn cleaner thread counters:");
3430 #define _(cnt, desc) vlib_cli_output(vm, "             %20lu: %s", am->cnt, desc);
3431   foreach_fa_cleaner_counter;
3432 #undef _
3433   vlib_cli_output (vm, "Interrupt generation: %d",
3434                    am->fa_interrupt_generation);
3435   vlib_cli_output (vm,
3436                    "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
3437                    am->fa_min_deleted_sessions_per_interval,
3438                    am->fa_max_deleted_sessions_per_interval,
3439                    am->fa_cleaner_wait_time_increment * 1000.0,
3440                    ((f64) am->fa_current_cleaner_timer_wait_interval) *
3441                    1000.0 / (f64) vm->clib_time.clocks_per_second);
3442 }
3443
3444 static clib_error_t *
3445 acl_show_aclplugin_sessions_fn (vlib_main_t * vm,
3446                                 unformat_input_t * input,
3447                                 vlib_cli_command_t * cmd)
3448 {
3449   clib_error_t *error = 0;
3450   acl_main_t *am = &acl_main;
3451
3452   u32 show_bihash_verbose = 0;
3453   u32 show_session_thread_id = ~0;
3454   u32 show_session_session_index = ~0;
3455   (void) unformat (input, "thread %u index %u", &show_session_thread_id,
3456                    &show_session_session_index);
3457   (void) unformat (input, "verbose %u", &show_bihash_verbose);
3458
3459   acl_plugin_show_sessions (am, show_session_thread_id,
3460                             show_session_session_index);
3461   show_fa_sessions_hash (vm, show_bihash_verbose);
3462   return error;
3463 }
3464
3465 static void
3466 acl_plugin_show_tables_mask_type (acl_main_t * am)
3467 {
3468   vlib_main_t *vm = am->vlib_main;
3469   ace_mask_type_entry_t *mte;
3470
3471   vlib_cli_output (vm, "Mask-type entries:");
3472     /* *INDENT-OFF* */
3473     pool_foreach(mte, am->ace_mask_type_pool,
3474     ({
3475       vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
3476                     mte - am->ace_mask_type_pool,
3477                     mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
3478                     mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
3479     }));
3480     /* *INDENT-ON* */
3481 }
3482
3483 static void
3484 acl_plugin_show_tables_acl_hash_info (acl_main_t * am, u32 acl_index)
3485 {
3486   vlib_main_t *vm = am->vlib_main;
3487   u32 i, j;
3488   u64 *m;
3489   vlib_cli_output (vm, "Mask-ready ACL representations\n");
3490   for (i = 0; i < vec_len (am->hash_acl_infos); i++)
3491     {
3492       if ((acl_index != ~0) && (acl_index != i))
3493         {
3494           continue;
3495         }
3496       hash_acl_info_t *ha = &am->hash_acl_infos[i];
3497       vlib_cli_output (vm, "acl-index %u bitmask-ready layout\n", i);
3498       vlib_cli_output (vm, "  applied  inbound on sw_if_index list: %U\n",
3499                        format_vec32, ha->inbound_sw_if_index_list, "%d");
3500       vlib_cli_output (vm, "  applied outbound on sw_if_index list: %U\n",
3501                        format_vec32, ha->outbound_sw_if_index_list, "%d");
3502       vlib_cli_output (vm, "  mask type index bitmap: %U\n",
3503                        format_bitmap_hex, ha->mask_type_index_bitmap);
3504       for (j = 0; j < vec_len (ha->rules); j++)
3505         {
3506           hash_ace_info_t *pa = &ha->rules[j];
3507           m = (u64 *) & pa->match;
3508           vlib_cli_output (vm,
3509                            "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
3510                            j, m[0], m[1], m[2], m[3], m[4], m[5],
3511                            pa->mask_type_index, pa->acl_index, pa->ace_index,
3512                            pa->action, pa->src_portrange_not_powerof2,
3513                            pa->dst_portrange_not_powerof2);
3514         }
3515     }
3516 }
3517
3518 static void
3519 acl_plugin_print_pae (vlib_main_t * vm, int j, applied_hash_ace_entry_t * pae)
3520 {
3521   vlib_cli_output (vm,
3522                    "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d tail %d hitcount %lld",
3523                    j, pae->acl_index, pae->ace_index, pae->action,
3524                    pae->hash_ace_info_index, pae->next_applied_entry_index,
3525                    pae->prev_applied_entry_index,
3526                    pae->tail_applied_entry_index, pae->hitcount);
3527 }
3528
3529 static void
3530 acl_plugin_show_tables_applied_info (acl_main_t * am, u32 sw_if_index)
3531 {
3532   vlib_main_t *vm = am->vlib_main;
3533   u32 swi, j;
3534   vlib_cli_output (vm, "Applied lookup entries for interfaces");
3535
3536   for (swi = 0;
3537        (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3538        || (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3539        || (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3540        || (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index)); swi++)
3541     {
3542       if ((sw_if_index != ~0) && (sw_if_index != swi))
3543         {
3544           continue;
3545         }
3546       vlib_cli_output (vm, "sw_if_index %d:", swi);
3547       if (swi < vec_len (am->input_applied_hash_acl_info_by_sw_if_index))
3548         {
3549           applied_hash_acl_info_t *pal =
3550             &am->input_applied_hash_acl_info_by_sw_if_index[swi];
3551           vlib_cli_output (vm, "  input lookup mask_type_index_bitmap: %U",
3552                            format_bitmap_hex, pal->mask_type_index_bitmap);
3553           vlib_cli_output (vm, "  input applied acls: %U", format_vec32,
3554                            pal->applied_acls, "%d");
3555         }
3556       if (swi < vec_len (am->input_hash_entry_vec_by_sw_if_index))
3557         {
3558           vlib_cli_output (vm, "  input lookup applied entries:");
3559           for (j = 0;
3560                j < vec_len (am->input_hash_entry_vec_by_sw_if_index[swi]);
3561                j++)
3562             {
3563               acl_plugin_print_pae (vm, j,
3564                                     &am->input_hash_entry_vec_by_sw_if_index
3565                                     [swi][j]);
3566             }
3567         }
3568
3569       if (swi < vec_len (am->output_applied_hash_acl_info_by_sw_if_index))
3570         {
3571           applied_hash_acl_info_t *pal =
3572             &am->output_applied_hash_acl_info_by_sw_if_index[swi];
3573           vlib_cli_output (vm, "  output lookup mask_type_index_bitmap: %U",
3574                            format_bitmap_hex, pal->mask_type_index_bitmap);
3575           vlib_cli_output (vm, "  output applied acls: %U", format_vec32,
3576                            pal->applied_acls, "%d");
3577         }
3578       if (swi < vec_len (am->output_hash_entry_vec_by_sw_if_index))
3579         {
3580           vlib_cli_output (vm, "  output lookup applied entries:");
3581           for (j = 0;
3582                j < vec_len (am->output_hash_entry_vec_by_sw_if_index[swi]);
3583                j++)
3584             {
3585               acl_plugin_print_pae (vm, j,
3586                                     &am->output_hash_entry_vec_by_sw_if_index
3587                                     [swi][j]);
3588             }
3589         }
3590     }
3591 }
3592
3593 static void
3594 acl_plugin_show_tables_bihash (acl_main_t * am, u32 show_bihash_verbose)
3595 {
3596   vlib_main_t *vm = am->vlib_main;
3597   show_hash_acl_hash (vm, am, show_bihash_verbose);
3598 }
3599
3600 static clib_error_t *
3601 acl_show_aclplugin_tables_fn (vlib_main_t * vm,
3602                               unformat_input_t * input,
3603                               vlib_cli_command_t * cmd)
3604 {
3605   clib_error_t *error = 0;
3606   acl_main_t *am = &acl_main;
3607
3608   u32 acl_index = ~0;
3609   u32 sw_if_index = ~0;
3610   int show_acl_hash_info = 0;
3611   int show_applied_info = 0;
3612   int show_mask_type = 0;
3613   int show_bihash = 0;
3614   u32 show_bihash_verbose = 0;
3615
3616   if (unformat (input, "acl"))
3617     {
3618       show_acl_hash_info = 1;
3619       /* mask-type is handy to see as well right there */
3620       show_mask_type = 1;
3621       unformat (input, "index %u", &acl_index);
3622     }
3623   else if (unformat (input, "applied"))
3624     {
3625       show_applied_info = 1;
3626       unformat (input, "sw_if_index %u", &sw_if_index);
3627     }
3628   else if (unformat (input, "mask"))
3629     {
3630       show_mask_type = 1;
3631     }
3632   else if (unformat (input, "hash"))
3633     {
3634       show_bihash = 1;
3635       unformat (input, "verbose %u", &show_bihash_verbose);
3636     }
3637
3638   if (!
3639       (show_mask_type || show_acl_hash_info || show_applied_info
3640        || show_bihash))
3641     {
3642       /* if no qualifiers specified, show all */
3643       show_mask_type = 1;
3644       show_acl_hash_info = 1;
3645       show_applied_info = 1;
3646       show_bihash = 1;
3647     }
3648   if (show_mask_type)
3649     acl_plugin_show_tables_mask_type (am);
3650   if (show_acl_hash_info)
3651     acl_plugin_show_tables_acl_hash_info (am, acl_index);
3652   if (show_applied_info)
3653     acl_plugin_show_tables_applied_info (am, sw_if_index);
3654   if (show_bihash)
3655     acl_plugin_show_tables_bihash (am, show_bihash_verbose);
3656
3657   return error;
3658 }
3659
3660 static clib_error_t *
3661 acl_clear_aclplugin_fn (vlib_main_t * vm,
3662                         unformat_input_t * input, vlib_cli_command_t * cmd)
3663 {
3664   clib_error_t *error = 0;
3665   acl_main_t *am = &acl_main;
3666   vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
3667                              ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
3668   return error;
3669 }
3670
3671  /* *INDENT-OFF* */
3672 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
3673     .path = "set acl-plugin",
3674     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
3675     .function = acl_set_aclplugin_fn,
3676 };
3677
3678 VLIB_CLI_COMMAND (aclplugin_show_acl_command, static) = {
3679     .path = "show acl-plugin acl",
3680     .short_help = "show acl-plugin acl [index N]",
3681     .function = acl_show_aclplugin_acl_fn,
3682 };
3683
3684 VLIB_CLI_COMMAND (aclplugin_show_decode_5tuple_command, static) = {
3685     .path = "show acl-plugin decode 5tuple",
3686     .short_help = "show acl-plugin decode 5tuple XXXX XXXX XXXX XXXX XXXX XXXX",
3687     .function = acl_show_aclplugin_decode_5tuple_fn,
3688 };
3689
3690 VLIB_CLI_COMMAND (aclplugin_show_interface_command, static) = {
3691     .path = "show acl-plugin interface",
3692     .short_help = "show acl-plugin interface [sw_if_index N] [acl]",
3693     .function = acl_show_aclplugin_interface_fn,
3694 };
3695
3696 VLIB_CLI_COMMAND (aclplugin_show_memory_command, static) = {
3697     .path = "show acl-plugin memory",
3698     .short_help = "show acl-plugin memory",
3699     .function = acl_show_aclplugin_memory_fn,
3700 };
3701
3702 VLIB_CLI_COMMAND (aclplugin_show_sessions_command, static) = {
3703     .path = "show acl-plugin sessions",
3704     .short_help = "show acl-plugin sessions",
3705     .function = acl_show_aclplugin_sessions_fn,
3706 };
3707
3708 VLIB_CLI_COMMAND (aclplugin_show_tables_command, static) = {
3709     .path = "show acl-plugin tables",
3710     .short_help = "show acl-plugin tables [ acl [index N] | applied [ sw_if_index N ] | mask | hash [verbose N] ]",
3711     .function = acl_show_aclplugin_tables_fn,
3712 };
3713
3714 VLIB_CLI_COMMAND (aclplugin_show_macip_acl_command, static) = {
3715     .path = "show acl-plugin macip acl",
3716     .short_help = "show acl-plugin macip acl",
3717     .function = acl_show_aclplugin_macip_acl_fn,
3718 };
3719
3720 VLIB_CLI_COMMAND (aclplugin_show_macip_interface_command, static) = {
3721     .path = "show acl-plugin macip interface",
3722     .short_help = "show acl-plugin macip interface",
3723     .function = acl_show_aclplugin_macip_interface_fn,
3724 };
3725
3726 VLIB_CLI_COMMAND (aclplugin_clear_command, static) = {
3727     .path = "clear acl-plugin sessions",
3728     .short_help = "clear acl-plugin sessions",
3729     .function = acl_clear_aclplugin_fn,
3730 };
3731 /* *INDENT-ON* */
3732
3733 static clib_error_t *
3734 acl_plugin_config (vlib_main_t * vm, unformat_input_t * input)
3735 {
3736   acl_main_t *am = &acl_main;
3737   u32 conn_table_hash_buckets;
3738   u32 conn_table_hash_memory_size;
3739   u32 conn_table_max_entries;
3740   u32 main_heap_size;
3741   u32 hash_heap_size;
3742   u32 hash_lookup_hash_buckets;
3743   u32 hash_lookup_hash_memory;
3744
3745   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3746     {
3747       if (unformat
3748           (input, "connection hash buckets %d", &conn_table_hash_buckets))
3749         am->fa_conn_table_hash_num_buckets = conn_table_hash_buckets;
3750       else if (unformat (input, "connection hash memory %d",
3751                          &conn_table_hash_memory_size))
3752         am->fa_conn_table_hash_memory_size = conn_table_hash_memory_size;
3753       else if (unformat (input, "connection count max %d",
3754                          &conn_table_max_entries))
3755         am->fa_conn_table_max_entries = conn_table_max_entries;
3756       else if (unformat (input, "main heap size %d", &main_heap_size))
3757         am->acl_mheap_size = main_heap_size;
3758       else if (unformat (input, "hash lookup heap size %d", &hash_heap_size))
3759         am->hash_lookup_mheap_size = hash_heap_size;
3760       else if (unformat (input, "hash lookup hash buckets %d",
3761                          &hash_lookup_hash_buckets))
3762         am->hash_lookup_hash_buckets = hash_lookup_hash_buckets;
3763       else if (unformat (input, "hash lookup hash memory %d",
3764                          &hash_lookup_hash_memory))
3765         am->hash_lookup_hash_memory = hash_lookup_hash_memory;
3766       else
3767         return clib_error_return (0, "unknown input '%U'",
3768                                   format_unformat_error, input);
3769     }
3770   return 0;
3771 }
3772
3773 VLIB_CONFIG_FUNCTION (acl_plugin_config, "acl-plugin");
3774
3775 static clib_error_t *
3776 acl_init (vlib_main_t * vm)
3777 {
3778   acl_main_t *am = &acl_main;
3779   clib_error_t *error = 0;
3780   memset (am, 0, sizeof (*am));
3781   am->vlib_main = vm;
3782   am->vnet_main = vnet_get_main ();
3783
3784   u8 *name = format (0, "acl_%08x%c", api_version, 0);
3785
3786   /* Ask for a correctly-sized block of API message decode slots */
3787   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
3788                                             VL_MSG_FIRST_AVAILABLE);
3789
3790   error = acl_plugin_api_hookup (vm);
3791
3792   /* Add our API messages to the global name_crc hash table */
3793   setup_message_id_table (am, &api_main);
3794
3795   vec_free (name);
3796
3797   acl_setup_fa_nodes ();
3798
3799   am->acl_mheap_size = ACL_FA_DEFAULT_HEAP_SIZE;
3800   am->hash_lookup_mheap_size = ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE;
3801
3802   am->hash_lookup_hash_buckets = ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS;
3803   am->hash_lookup_hash_memory = ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY;
3804
3805   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] =
3806     TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
3807   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] =
3808     TCP_SESSION_IDLE_TIMEOUT_SEC;
3809   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] =
3810     UDP_SESSION_IDLE_TIMEOUT_SEC;
3811
3812   am->fa_conn_table_hash_num_buckets =
3813     ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
3814   am->fa_conn_table_hash_memory_size =
3815     ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
3816   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
3817   vlib_thread_main_t *tm = vlib_get_thread_main ();
3818   vec_validate (am->per_worker_data, tm->n_vlib_mains - 1);
3819   {
3820     u16 wk;
3821     u8 tt;
3822     for (wk = 0; wk < vec_len (am->per_worker_data); wk++)
3823       {
3824         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
3825         vec_validate (pw->fa_conn_list_head, ACL_N_TIMEOUTS - 1);
3826         vec_validate (pw->fa_conn_list_tail, ACL_N_TIMEOUTS - 1);
3827         for (tt = 0; tt < ACL_N_TIMEOUTS; tt++)
3828           {
3829             pw->fa_conn_list_head[tt] = ~0;
3830             pw->fa_conn_list_tail[tt] = ~0;
3831           }
3832       }
3833   }
3834
3835   am->fa_min_deleted_sessions_per_interval =
3836     ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
3837   am->fa_max_deleted_sessions_per_interval =
3838     ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
3839   am->fa_cleaner_wait_time_increment =
3840     ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
3841
3842   am->fa_cleaner_cnt_delete_by_sw_index = 0;
3843   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
3844   am->fa_cleaner_cnt_unknown_event = 0;
3845   am->fa_cleaner_cnt_timer_restarted = 0;
3846   am->fa_cleaner_cnt_wait_with_timeout = 0;
3847
3848
3849 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
3850   foreach_acl_eh
3851 #undef _
3852     am->l4_match_nonfirst_fragment = 1;
3853
3854   /* use the new fancy hash-based matching */
3855   am->use_hash_acl_matching = 1;
3856
3857   return error;
3858 }
3859
3860 VLIB_INIT_FUNCTION (acl_init);
3861
3862
3863 /*
3864  * fd.io coding-style-patch-verification: ON
3865  *
3866  * Local Variables:
3867  * eval: (c-set-style "gnu")
3868  * End:
3869  */