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