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