acl-plugin: fix coverity error 171135
[vpp.git] / src / plugins / acl / acl.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stddef.h>
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <acl/acl.h>
21
22 #include <vnet/l2/l2_classify.h>
23 #include <vnet/classify/input_acl.h>
24 #include <vpp/app/version.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29
30 /* define message IDs */
31 #include <acl/acl_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <acl/acl_all_api_h.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <acl/acl_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <acl/acl_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <acl/acl_all_api_h.h>
52 #undef vl_api_version
53
54 #include "fa_node.h"
55 #include "hash_lookup.h"
56
57 acl_main_t acl_main;
58
59 #define REPLY_MSG_ID_BASE am->msg_id_base
60 #include <vlibapi/api_helper_macros.h>
61
62 /* List of message types that this plugin understands */
63
64 #define foreach_acl_plugin_api_msg              \
65 _(ACL_PLUGIN_GET_VERSION, acl_plugin_get_version) \
66 _(ACL_PLUGIN_CONTROL_PING, acl_plugin_control_ping) \
67 _(ACL_ADD_REPLACE, acl_add_replace)                             \
68 _(ACL_DEL, acl_del)                             \
69 _(ACL_INTERFACE_ADD_DEL, acl_interface_add_del) \
70 _(ACL_INTERFACE_SET_ACL_LIST, acl_interface_set_acl_list)       \
71 _(ACL_DUMP, acl_dump)  \
72 _(ACL_INTERFACE_LIST_DUMP, acl_interface_list_dump) \
73 _(MACIP_ACL_ADD, macip_acl_add) \
74 _(MACIP_ACL_DEL, macip_acl_del) \
75 _(MACIP_ACL_INTERFACE_ADD_DEL, macip_acl_interface_add_del) \
76 _(MACIP_ACL_DUMP, macip_acl_dump) \
77 _(MACIP_ACL_INTERFACE_GET, macip_acl_interface_get)
78
79 /* *INDENT-OFF* */
80 VLIB_PLUGIN_REGISTER () = {
81     .version = VPP_BUILD_VER,
82     .description = "Access Control Lists",
83 };
84 /* *INDENT-ON* */
85
86 static void
87 vl_api_acl_plugin_get_version_t_handler (vl_api_acl_plugin_get_version_t * mp)
88 {
89   acl_main_t *am = &acl_main;
90   vl_api_acl_plugin_get_version_reply_t *rmp;
91   int msg_size = sizeof (*rmp);
92   unix_shared_memory_queue_t *q;
93
94   q = vl_api_client_index_to_input_queue (mp->client_index);
95   if (q == 0)
96     {
97       return;
98     }
99
100   rmp = vl_msg_api_alloc (msg_size);
101   memset (rmp, 0, msg_size);
102   rmp->_vl_msg_id =
103     ntohs (VL_API_ACL_PLUGIN_GET_VERSION_REPLY + am->msg_id_base);
104   rmp->context = mp->context;
105   rmp->major = htonl (ACL_PLUGIN_VERSION_MAJOR);
106   rmp->minor = htonl (ACL_PLUGIN_VERSION_MINOR);
107
108   vl_msg_api_send_shmem (q, (u8 *) & rmp);
109 }
110
111 static void
112 vl_api_acl_plugin_control_ping_t_handler (vl_api_acl_plugin_control_ping_t * mp)
113 {
114   vl_api_acl_plugin_control_ping_reply_t *rmp;
115   acl_main_t *am = &acl_main;
116   int rv = 0;
117
118   /* *INDENT-OFF* */
119   REPLY_MACRO2 (VL_API_ACL_PLUGIN_CONTROL_PING_REPLY,
120   ({
121     rmp->vpe_pid = ntohl (getpid ());
122   }));
123   /* *INDENT-ON* */
124 }
125
126 static int
127 acl_add_list (u32 count, vl_api_acl_rule_t rules[],
128               u32 * acl_list_index, u8 * tag)
129 {
130   acl_main_t *am = &acl_main;
131   acl_list_t *a;
132   acl_rule_t *r;
133   acl_rule_t *acl_new_rules;
134   int i;
135
136   if (*acl_list_index != ~0)
137     {
138       /* They supplied some number, let's see if this ACL exists */
139       if (pool_is_free_index (am->acls, *acl_list_index))
140         {
141           /* tried to replace a non-existent ACL, no point doing anything */
142           return -1;
143         }
144     }
145
146   /* Create and populate the rules */
147   acl_new_rules = clib_mem_alloc_aligned (sizeof (acl_rule_t) * count,
148                                           CLIB_CACHE_LINE_BYTES);
149   if (!acl_new_rules)
150     {
151       /* Could not allocate rules. New or existing ACL - bail out regardless */
152       return -1;
153     }
154
155   for (i = 0; i < count; i++)
156     {
157       r = &acl_new_rules[i];
158       memset(r, 0, sizeof(*r));
159       r->is_permit = rules[i].is_permit;
160       r->is_ipv6 = rules[i].is_ipv6;
161       if (r->is_ipv6)
162         {
163           memcpy (&r->src, rules[i].src_ip_addr, sizeof (r->src));
164           memcpy (&r->dst, rules[i].dst_ip_addr, sizeof (r->dst));
165         }
166       else
167         {
168           memcpy (&r->src.ip4, rules[i].src_ip_addr, sizeof (r->src.ip4));
169           memcpy (&r->dst.ip4, rules[i].dst_ip_addr, sizeof (r->dst.ip4));
170         }
171       r->src_prefixlen = rules[i].src_ip_prefix_len;
172       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
173       r->proto = rules[i].proto;
174       r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
175       r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
176       r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
177       r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
178       r->tcp_flags_value = rules[i].tcp_flags_value;
179       r->tcp_flags_mask = rules[i].tcp_flags_mask;
180     }
181
182   if (~0 == *acl_list_index)
183     {
184       /* Get ACL index */
185       pool_get_aligned (am->acls, a, CLIB_CACHE_LINE_BYTES);
186       memset (a, 0, sizeof (*a));
187       /* Will return the newly allocated ACL index */
188       *acl_list_index = a - am->acls;
189     }
190   else
191     {
192       a = am->acls + *acl_list_index;
193       hash_acl_delete(am, *acl_list_index);
194       /* Get rid of the old rules */
195       clib_mem_free (a->rules);
196     }
197   a->rules = acl_new_rules;
198   a->count = count;
199   memcpy (a->tag, tag, sizeof (a->tag));
200   hash_acl_add(am, *acl_list_index);
201
202   return 0;
203 }
204
205 static int
206 acl_del_list (u32 acl_list_index)
207 {
208   acl_main_t *am = &acl_main;
209   acl_list_t *a;
210   int i, ii;
211   if (pool_is_free_index (am->acls, acl_list_index))
212     {
213       return -1;
214     }
215
216   if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
217     if (vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
218       /* ACL is applied somewhere inbound. Refuse to delete */
219       return -1;
220     }
221   }
222   if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
223     if (vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index]) > 0) {
224       /* ACL is applied somewhere outbound. Refuse to delete */
225       return -1;
226     }
227   }
228
229   /* delete any references to the ACL */
230   for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index); i++)
231     {
232       for (ii = 0; ii < vec_len (am->output_acl_vec_by_sw_if_index[i]);
233            /* see body */ )
234         {
235           if (acl_list_index == am->output_acl_vec_by_sw_if_index[i][ii])
236             {
237               vec_del1 (am->output_acl_vec_by_sw_if_index[i], ii);
238             }
239           else
240             {
241               ii++;
242             }
243         }
244     }
245   for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index); i++)
246     {
247       for (ii = 0; ii < vec_len (am->input_acl_vec_by_sw_if_index[i]);
248            /* see body */ )
249         {
250           if (acl_list_index == am->input_acl_vec_by_sw_if_index[i][ii])
251             {
252               vec_del1 (am->input_acl_vec_by_sw_if_index[i], ii);
253             }
254           else
255             {
256               ii++;
257             }
258         }
259     }
260   /* delete the hash table data */
261
262   hash_acl_delete(am, acl_list_index);
263   /* now we can delete the ACL itself */
264   a = &am->acls[acl_list_index];
265   if (a->rules)
266     {
267       clib_mem_free (a->rules);
268     }
269   pool_put (am->acls, a);
270   return 0;
271 }
272
273 /* Some aids in ASCII graphing the content */
274 #define XX "\377"
275 #define __ "\000"
276 #define _(x)
277 #define v
278
279 u8 ip4_5tuple_mask[] =
280 _("             dmac               smac            etype ")
281 _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
282   _("        v ihl totlen   ")
283   _(0x0000)
284   __ __ __ __
285   _("        ident fl+fo    ")
286   _(0x0004)
287   __ __ __ __
288   _("       ttl pr checksum ")
289   _(0x0008)
290   __ XX __ __
291   _("        src address    ")
292   _(0x000C)
293   XX XX XX XX
294   _("        dst address    ")
295   _(0x0010)
296   XX XX XX XX
297   _("L4 T/U  sport dport    ")
298   _(tcpudp)
299   XX XX XX XX
300   _(padpad)
301   __ __ __ __
302   _(padpad)
303   __ __ __ __
304   _(padeth)
305   __ __;
306
307      u8 ip6_5tuple_mask[] =
308        _("             dmac               smac            etype ")
309   _(ether) __ __ __ __ __ __ v __ __ __ __ __ __ v __ __ v
310   _("        v  tc + flow ")
311   _(0x0000) __ __ __ __
312   _("        plen  nh hl  ")
313   _(0x0004) __ __ XX __
314   _("        src address  ")
315   _(0x0008) XX XX XX XX
316   _(0x000C) XX XX XX XX
317   _(0x0010) XX XX XX XX
318   _(0x0014) XX XX XX XX
319   _("        dst address  ")
320   _(0x0018) XX XX XX XX
321   _(0x001C) XX XX XX XX
322   _(0x0020) XX XX XX XX
323   _(0x0024) XX XX XX XX
324   _("L4T/U  sport dport   ")
325   _(tcpudp) XX XX XX XX _(padpad) __ __ __ __ _(padeth) __ __;
326
327 #undef XX
328 #undef __
329 #undef _
330 #undef v
331
332      static int count_skip (u8 * p, u32 size)
333 {
334   u64 *p64 = (u64 *) p;
335   /* Be tolerant to null pointer */
336   if (0 == p)
337     return 0;
338
339   while ((0ULL == *p64) && ((u8 *) p64 - p) < size)
340     {
341       p64++;
342     }
343   return (p64 - (u64 *) p) / 2;
344 }
345
346 static int
347 acl_classify_add_del_table_tiny (vnet_classify_main_t * cm, u8 * mask,
348                             u32 mask_len, u32 next_table_index,
349                             u32 miss_next_index, u32 * table_index,
350                             int is_add)
351 {
352   u32 nbuckets = 1;
353   u32 memory_size = 2 << 13;
354   u32 skip = count_skip (mask, mask_len);
355   u32 match = (mask_len / 16) - skip;
356   u8 *skip_mask_ptr = mask + 16 * skip;
357   u32 current_data_flag = 0;
358   int current_data_offset = 0;
359
360   if (0 == match)
361     match = 1;
362
363   return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
364                                       memory_size, skip, match,
365                                       next_table_index, miss_next_index,
366                                       table_index, current_data_flag,
367                                       current_data_offset, is_add,
368                                       1 /* delete_chain */);
369 }
370
371 static int
372 acl_classify_add_del_table_small (vnet_classify_main_t * cm, u8 * mask,
373                             u32 mask_len, u32 next_table_index,
374                             u32 miss_next_index, u32 * table_index,
375                             int is_add)
376 {
377   u32 nbuckets = 32;
378   u32 memory_size = 2 << 20;
379   u32 skip = count_skip (mask, mask_len);
380   u32 match = (mask_len / 16) - skip;
381   u8 *skip_mask_ptr = mask + 16 * skip;
382   u32 current_data_flag = 0;
383   int current_data_offset = 0;
384
385   if (0 == match)
386     match = 1;
387
388   return vnet_classify_add_del_table (cm, skip_mask_ptr, nbuckets,
389                                       memory_size, skip, match,
390                                       next_table_index, miss_next_index,
391                                       table_index, current_data_flag,
392                                       current_data_offset, is_add,
393                                       1 /* delete_chain */);
394 }
395
396
397 static int
398 acl_unhook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
399 {
400   vnet_classify_main_t *cm = &vnet_classify_main;
401   u32 ip4_table_index = ~0;
402   u32 ip6_table_index = ~0;
403
404   vec_validate_init_empty (am->acl_ip4_input_classify_table_by_sw_if_index,
405                            sw_if_index, ~0);
406   vec_validate_init_empty (am->acl_ip6_input_classify_table_by_sw_if_index,
407                            sw_if_index, ~0);
408
409   vnet_l2_input_classify_enable_disable (sw_if_index, 0);
410
411   if (am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
412     {
413       ip4_table_index =
414         am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index];
415       am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
416       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
417                                   sizeof (ip4_5tuple_mask) - 1, ~0,
418                                   am->l2_input_classify_next_acl_ip4,
419                                   &ip4_table_index, 0);
420     }
421   if (am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] != ~0)
422     {
423       ip6_table_index =
424         am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index];
425       am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] = ~0;
426       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
427                                   sizeof (ip6_5tuple_mask) - 1, ~0,
428                                   am->l2_input_classify_next_acl_ip6,
429                                   &ip6_table_index, 0);
430     }
431
432   return 0;
433 }
434
435 static int
436 acl_unhook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
437 {
438   vnet_classify_main_t *cm = &vnet_classify_main;
439   u32 ip4_table_index = ~0;
440   u32 ip6_table_index = ~0;
441
442   vec_validate_init_empty (am->acl_ip4_output_classify_table_by_sw_if_index,
443                            sw_if_index, ~0);
444   vec_validate_init_empty (am->acl_ip6_output_classify_table_by_sw_if_index,
445                            sw_if_index, ~0);
446
447   vnet_l2_output_classify_enable_disable (sw_if_index, 0);
448
449   if (am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
450     {
451       ip4_table_index =
452         am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index];
453       am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
454       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
455                                   sizeof (ip4_5tuple_mask) - 1, ~0,
456                                   am->l2_output_classify_next_acl_ip4,
457                                   &ip4_table_index, 0);
458     }
459   if (am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] != ~0)
460     {
461       ip6_table_index =
462         am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index];
463       am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] = ~0;
464       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
465                                   sizeof (ip6_5tuple_mask) - 1, ~0,
466                                   am->l2_output_classify_next_acl_ip6,
467                                   &ip6_table_index, 0);
468     }
469
470   return 0;
471 }
472
473 static int
474 acl_hook_l2_input_classify (acl_main_t * am, u32 sw_if_index)
475 {
476   vnet_classify_main_t *cm = &vnet_classify_main;
477   u32 ip4_table_index = ~0;
478   u32 ip6_table_index = ~0;
479   int rv;
480
481   /* in case there were previous tables attached */
482   acl_unhook_l2_input_classify (am, sw_if_index);
483   rv =
484     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
485                                 sizeof (ip4_5tuple_mask) - 1, ~0,
486                                 am->l2_input_classify_next_acl_ip4,
487                                 &ip4_table_index, 1);
488   if (rv)
489     return rv;
490   rv =
491     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
492                                 sizeof (ip6_5tuple_mask) - 1, ~0,
493                                 am->l2_input_classify_next_acl_ip6,
494                                 &ip6_table_index, 1);
495   if (rv)
496     {
497       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
498                                   sizeof (ip4_5tuple_mask) - 1, ~0,
499                                   am->l2_input_classify_next_acl_ip4,
500                                   &ip4_table_index, 0);
501       return rv;
502     }
503   rv =
504     vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
505                                        ip6_table_index, ~0);
506   clib_warning
507     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
508      sw_if_index, ip4_table_index, ip6_table_index);
509   if (rv)
510     {
511       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
512                                   sizeof (ip6_5tuple_mask) - 1, ~0,
513                                   am->l2_input_classify_next_acl_ip6,
514                                   &ip6_table_index, 0);
515       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
516                                   sizeof (ip4_5tuple_mask) - 1, ~0,
517                                   am->l2_input_classify_next_acl_ip4,
518                                   &ip4_table_index, 0);
519       return rv;
520     }
521
522   am->acl_ip4_input_classify_table_by_sw_if_index[sw_if_index] =
523     ip4_table_index;
524   am->acl_ip6_input_classify_table_by_sw_if_index[sw_if_index] =
525     ip6_table_index;
526
527   vnet_l2_input_classify_enable_disable (sw_if_index, 1);
528   return rv;
529 }
530
531 static int
532 acl_hook_l2_output_classify (acl_main_t * am, u32 sw_if_index)
533 {
534   vnet_classify_main_t *cm = &vnet_classify_main;
535   u32 ip4_table_index = ~0;
536   u32 ip6_table_index = ~0;
537   int rv;
538
539   /* in case there were previous tables attached */
540   acl_unhook_l2_output_classify (am, sw_if_index);
541   rv =
542     acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
543                                 sizeof (ip4_5tuple_mask) - 1, ~0,
544                                 am->l2_output_classify_next_acl_ip4,
545                                 &ip4_table_index, 1);
546   if (rv)
547     return rv;
548   rv =
549     acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
550                                 sizeof (ip6_5tuple_mask) - 1, ~0,
551                                 am->l2_output_classify_next_acl_ip6,
552                                 &ip6_table_index, 1);
553   if (rv)
554     {
555       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
556                                   sizeof (ip4_5tuple_mask) - 1, ~0,
557                                   am->l2_output_classify_next_acl_ip4,
558                                   &ip4_table_index, 0);
559       return rv;
560     }
561   rv =
562     vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
563                                         ip6_table_index, ~0);
564   clib_warning
565     ("ACL enabling on interface sw_if_index %d, setting tables to the following: ip4: %d ip6: %d\n",
566      sw_if_index, ip4_table_index, ip6_table_index);
567   if (rv)
568     {
569       acl_classify_add_del_table_tiny (cm, ip6_5tuple_mask,
570                                   sizeof (ip6_5tuple_mask) - 1, ~0,
571                                   am->l2_output_classify_next_acl_ip6,
572                                   &ip6_table_index, 0);
573       acl_classify_add_del_table_tiny (cm, ip4_5tuple_mask,
574                                   sizeof (ip4_5tuple_mask) - 1, ~0,
575                                   am->l2_output_classify_next_acl_ip4,
576                                   &ip4_table_index, 0);
577       return rv;
578     }
579
580   am->acl_ip4_output_classify_table_by_sw_if_index[sw_if_index] =
581     ip4_table_index;
582   am->acl_ip6_output_classify_table_by_sw_if_index[sw_if_index] =
583     ip6_table_index;
584
585   vnet_l2_output_classify_enable_disable (sw_if_index, 1);
586   return rv;
587 }
588
589
590
591 int
592 acl_interface_in_enable_disable (acl_main_t * am, u32 sw_if_index,
593                                  int enable_disable)
594 {
595   int rv;
596
597   /* Utterly wrong? */
598   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
599                           sw_if_index))
600     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
601
602   acl_fa_enable_disable(sw_if_index, 1, enable_disable);
603
604   if (enable_disable)
605     {
606       rv = acl_hook_l2_input_classify (am, sw_if_index);
607     }
608   else
609     {
610       rv = acl_unhook_l2_input_classify (am, sw_if_index);
611     }
612
613   return rv;
614 }
615
616 int
617 acl_interface_out_enable_disable (acl_main_t * am, u32 sw_if_index,
618                                   int enable_disable)
619 {
620   int rv;
621
622   /* Utterly wrong? */
623   if (pool_is_free_index (am->vnet_main->interface_main.sw_interfaces,
624                           sw_if_index))
625     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
626
627   acl_fa_enable_disable(sw_if_index, 0, enable_disable);
628
629   if (enable_disable)
630     {
631       rv = acl_hook_l2_output_classify (am, sw_if_index);
632     }
633   else
634     {
635       rv = acl_unhook_l2_output_classify (am, sw_if_index);
636     }
637
638   return rv;
639 }
640
641 static int
642 acl_is_not_defined(acl_main_t *am, u32 acl_list_index)
643 {
644   return (pool_is_free_index (am->acls, acl_list_index));
645 }
646
647
648 static int
649 acl_interface_add_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
650 {
651   acl_main_t *am = &acl_main;
652   if (acl_is_not_defined(am, acl_list_index)) {
653     /* ACL is not defined. Can not apply */
654     return -1;
655   }
656
657   if (is_input)
658     {
659       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
660
661       u32 index = vec_search(am->input_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
662       if (index < vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])) {
663         clib_warning("ACL %d is already applied inbound on sw_if_index %d (index %d)",
664                      acl_list_index, sw_if_index, index);
665         /* the entry is already there */
666         return -1;
667       }
668       vec_add (am->input_acl_vec_by_sw_if_index[sw_if_index], &acl_list_index,
669                1);
670       vec_validate (am->input_sw_if_index_vec_by_acl, acl_list_index);
671       vec_add (am->input_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
672                1);
673       acl_interface_in_enable_disable (am, sw_if_index, 1);
674     }
675   else
676     {
677       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
678
679       u32 index = vec_search(am->output_acl_vec_by_sw_if_index[sw_if_index], acl_list_index);
680       if (index < vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])) {
681         clib_warning("ACL %d is already applied outbound on sw_if_index %d (index %d)",
682                      acl_list_index, sw_if_index, index);
683         /* the entry is already there */
684         return -1;
685       }
686       vec_add (am->output_acl_vec_by_sw_if_index[sw_if_index],
687                &acl_list_index, 1);
688       vec_validate (am->output_sw_if_index_vec_by_acl, acl_list_index);
689       vec_add (am->output_sw_if_index_vec_by_acl[acl_list_index], &sw_if_index,
690                1);
691       acl_interface_out_enable_disable (am, sw_if_index, 1);
692     }
693   return 0;
694 }
695
696
697 static int
698 acl_interface_del_inout_acl (u32 sw_if_index, u8 is_input, u32 acl_list_index)
699 {
700   acl_main_t *am = &acl_main;
701   int i;
702   int rv = -1;
703   if (is_input)
704     {
705       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
706       for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
707            i++)
708         {
709           if (acl_list_index ==
710               am->input_acl_vec_by_sw_if_index[sw_if_index][i])
711             {
712               vec_del1 (am->input_acl_vec_by_sw_if_index[sw_if_index], i);
713               rv = 0;
714               break;
715             }
716         }
717
718       if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
719         u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
720         if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
721           hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
722           vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
723         }
724       }
725
726       if (0 == vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]))
727         {
728           acl_interface_in_enable_disable (am, sw_if_index, 0);
729         }
730     }
731   else
732     {
733       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
734       for (i = 0;
735            i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]); i++)
736         {
737           if (acl_list_index ==
738               am->output_acl_vec_by_sw_if_index[sw_if_index][i])
739             {
740               vec_del1 (am->output_acl_vec_by_sw_if_index[sw_if_index], i);
741               rv = 0;
742               break;
743             }
744         }
745
746       if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
747         u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
748         if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
749           hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
750           vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
751         }
752       }
753
754       if (0 == vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]))
755         {
756           acl_interface_out_enable_disable (am, sw_if_index, 0);
757         }
758     }
759   return rv;
760 }
761
762 static void
763 acl_interface_reset_inout_acls (u32 sw_if_index, u8 is_input)
764 {
765   acl_main_t *am = &acl_main;
766   int i;
767   if (is_input)
768     {
769       acl_interface_in_enable_disable (am, sw_if_index, 0);
770       vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
771
772       for(i = vec_len(am->input_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
773         u32 acl_list_index = am->input_acl_vec_by_sw_if_index[sw_if_index][i];
774         hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
775         if (acl_list_index < vec_len(am->input_sw_if_index_vec_by_acl)) {
776           u32 index = vec_search(am->input_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
777           if (index < vec_len(am->input_sw_if_index_vec_by_acl[acl_list_index])) {
778             vec_del1 (am->input_sw_if_index_vec_by_acl[acl_list_index], index);
779           }
780         }
781       }
782
783       vec_reset_length (am->input_acl_vec_by_sw_if_index[sw_if_index]);
784     }
785   else
786     {
787       acl_interface_out_enable_disable (am, sw_if_index, 0);
788       vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
789
790       for(i = vec_len(am->output_acl_vec_by_sw_if_index[sw_if_index])-1; i>=0; i--) {
791         u32 acl_list_index = am->output_acl_vec_by_sw_if_index[sw_if_index][i];
792         hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
793         if (acl_list_index < vec_len(am->output_sw_if_index_vec_by_acl)) {
794           u32 index = vec_search(am->output_sw_if_index_vec_by_acl[acl_list_index], sw_if_index);
795           if (index < vec_len(am->output_sw_if_index_vec_by_acl[acl_list_index])) {
796             vec_del1 (am->output_sw_if_index_vec_by_acl[acl_list_index], index);
797           }
798         }
799       }
800
801       vec_reset_length (am->output_acl_vec_by_sw_if_index[sw_if_index]);
802     }
803 }
804
805 static int
806 acl_interface_add_del_inout_acl (u32 sw_if_index, u8 is_add, u8 is_input,
807                                  u32 acl_list_index)
808 {
809   int rv = -1;
810   acl_main_t *am = &acl_main;
811   if (is_add)
812     {
813       rv =
814         acl_interface_add_inout_acl (sw_if_index, is_input, acl_list_index);
815       if (rv == 0)
816         {
817           hash_acl_apply(am, sw_if_index, is_input, acl_list_index);
818         }
819     }
820   else
821     {
822       hash_acl_unapply(am, sw_if_index, is_input, acl_list_index);
823       rv =
824         acl_interface_del_inout_acl (sw_if_index, is_input, acl_list_index);
825     }
826   return rv;
827 }
828
829
830 typedef struct
831 {
832   u8 is_ipv6;
833   u8 mac_mask[6];
834   u8 prefix_len;
835   u32 count;
836   u32 table_index;
837   u32 arp_table_index;
838 } macip_match_type_t;
839
840 static u32
841 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
842                        u8 is_ipv6)
843 {
844   u32 i;
845   if (mv)
846     {
847       for (i = 0; i < vec_len (mv); i++)
848         {
849           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
850               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
851             {
852               return i;
853             }
854         }
855     }
856   return ~0;
857 }
858
859
860 /* Get metric used to sort match types.
861    The more specific and the more often seen - the bigger the metric */
862 static int
863 match_type_metric (macip_match_type_t * m)
864 {
865   /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
866   return m->prefix_len + m->is_ipv6 + 10 * m->count;
867 }
868
869 static int
870 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
871 {
872   /* Ascending sort based on the metric values */
873   return match_type_metric (m1) - match_type_metric (m2);
874 }
875
876 /* Get the offset of L3 source within ethernet packet */
877 static int
878 get_l3_src_offset(int is6)
879 {
880   if(is6)
881     return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
882   else
883     return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
884 }
885
886 static int
887 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
888 {
889   macip_match_type_t *mvec = NULL;
890   macip_match_type_t *mt;
891   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
892   int i;
893   u32 match_type_index;
894   u32 last_table;
895   u8 mask[5 * 16];
896   vnet_classify_main_t *cm = &vnet_classify_main;
897
898   /* Count the number of different types of rules */
899   for (i = 0; i < a->count; i++)
900     {
901       if (~0 ==
902           (match_type_index =
903            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
904                                   a->rules[i].src_prefixlen,
905                                   a->rules[i].is_ipv6)))
906         {
907           match_type_index = vec_len (mvec);
908           vec_validate (mvec, match_type_index);
909           memcpy (mvec[match_type_index].mac_mask,
910                   a->rules[match_type_index].src_mac_mask, 6);
911           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
912           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
913           mvec[match_type_index].table_index = ~0;
914         }
915       mvec[match_type_index].count++;
916     }
917   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
918   vec_sort_with_function (mvec, match_type_compare);
919   /* Create the classifier tables */
920   last_table = ~0;
921   /* First add ARP tables */
922   vec_foreach (mt, mvec)
923   {
924     int mask_len;
925     int is6 = mt->is_ipv6;
926
927     mt->arp_table_index = ~0;
928     if (!is6)
929       {
930         memset (mask, 0, sizeof (mask));
931         memcpy (&mask[6], mt->mac_mask, 6);
932         memset (&mask[12], 0xff, 2); /* ethernet protocol */
933         memcpy (&mask[14 + 8], mt->mac_mask, 6);
934
935         for (i = 0; i < (mt->prefix_len / 8); i++)
936           mask[14 + 14 + i] = 0xff;
937         if (mt->prefix_len % 8)
938           mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
939
940         mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
941                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
942         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
943                                (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
944                                1);
945         last_table = mt->arp_table_index;
946       }
947   }
948   /* Now add IP[46] tables */
949   vec_foreach (mt, mvec)
950   {
951     int mask_len;
952     int is6 = mt->is_ipv6;
953     int l3_src_offs = get_l3_src_offset(is6);
954     memset (mask, 0, sizeof (mask));
955     memcpy (&mask[6], mt->mac_mask, 6);
956     for (i = 0; i < (mt->prefix_len / 8); i++)
957       {
958         mask[l3_src_offs + i] = 0xff;
959       }
960     if (mt->prefix_len % 8)
961       {
962         mask[l3_src_offs + (mt->prefix_len / 8)] =
963           0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
964       }
965     /*
966      * Round-up the number of bytes needed to store the prefix,
967      * and round up the number of vectors too
968      */
969     mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
970                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
971     acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
972                                 (~0 == last_table) ? 0 : ~0, &mt->table_index,
973                                 1);
974     last_table = mt->table_index;
975   }
976   a->ip4_table_index = ~0;
977   a->ip6_table_index = ~0;
978   a->l2_table_index = last_table;
979
980   /* Populate the classifier tables with rules from the MACIP ACL */
981   for (i = 0; i < a->count; i++)
982     {
983       u32 action = 0;
984       u32 metadata = 0;
985       int is6 = a->rules[i].is_ipv6;
986       int l3_src_offs = get_l3_src_offset(is6);
987       memset (mask, 0, sizeof (mask));
988       memcpy (&mask[6], a->rules[i].src_mac, 6);
989       memset (&mask[12], 0xff, 2); /* ethernet protocol */
990       if (is6)
991         {
992           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
993           mask[12] = 0x86;
994           mask[13] = 0xdd;
995         }
996       else
997         {
998           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
999           mask[12] = 0x08;
1000           mask[13] = 0x00;
1001         }
1002       match_type_index =
1003         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1004                                a->rules[i].src_prefixlen,
1005                                a->rules[i].is_ipv6);
1006       /* add session to table mvec[match_type_index].table_index; */
1007       vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1008                                      mask, a->rules[i].is_permit ? ~0 : 0, i,
1009                                      0, action, metadata, 1);
1010       /* add ARP table entry too */
1011       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1012         {
1013           memset (mask, 0, sizeof (mask));
1014           memcpy (&mask[6], a->rules[i].src_mac, 6);
1015           mask[12] = 0x08;
1016           mask[13] = 0x06;
1017           memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1018           memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1019           vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1020                                     mask, a->rules[i].is_permit ? ~0 : 0, i,
1021                                     0, action, metadata, 1);
1022         }
1023     }
1024   return 0;
1025 }
1026
1027 static void
1028 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1029 {
1030   vnet_classify_main_t *cm = &vnet_classify_main;
1031   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1032
1033   if (a->ip4_table_index != ~0)
1034     {
1035       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1036       a->ip4_table_index = ~0;
1037     }
1038   if (a->ip6_table_index != ~0)
1039     {
1040       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1041       a->ip6_table_index = ~0;
1042     }
1043   if (a->l2_table_index != ~0)
1044     {
1045       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1046       a->l2_table_index = ~0;
1047     }
1048 }
1049
1050 static int
1051 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1052                     u32 * acl_list_index, u8 * tag)
1053 {
1054   acl_main_t *am = &acl_main;
1055   macip_acl_list_t *a;
1056   macip_acl_rule_t *r;
1057   macip_acl_rule_t *acl_new_rules;
1058   int i;
1059
1060   /* Create and populate the rules */
1061   acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1062                                           CLIB_CACHE_LINE_BYTES);
1063   if (!acl_new_rules)
1064     {
1065       /* Could not allocate rules. New or existing ACL - bail out regardless */
1066       return -1;
1067     }
1068
1069   for (i = 0; i < count; i++)
1070     {
1071       r = &acl_new_rules[i];
1072       r->is_permit = rules[i].is_permit;
1073       r->is_ipv6 = rules[i].is_ipv6;
1074       memcpy (&r->src_mac, rules[i].src_mac, 6);
1075       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1076       if(rules[i].is_ipv6)
1077         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1078       else
1079         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1080       r->src_prefixlen = rules[i].src_ip_prefix_len;
1081     }
1082
1083   /* Get ACL index */
1084   pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1085   memset (a, 0, sizeof (*a));
1086   /* Will return the newly allocated ACL index */
1087   *acl_list_index = a - am->macip_acls;
1088
1089   a->rules = acl_new_rules;
1090   a->count = count;
1091   memcpy (a->tag, tag, sizeof (a->tag));
1092
1093   /* Create and populate the classifer tables */
1094   macip_create_classify_tables (am, *acl_list_index);
1095
1096   return 0;
1097 }
1098
1099
1100 /* No check for validity of sw_if_index - the callers were supposed to validate */
1101
1102 static int
1103 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1104 {
1105   int rv;
1106   u32 macip_acl_index;
1107   macip_acl_list_t *a;
1108   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1109   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1110   /* No point in deleting MACIP ACL which is not applied */
1111   if (~0 == macip_acl_index)
1112     return -1;
1113   a = &am->macip_acls[macip_acl_index];
1114   /* remove the classifier tables off the interface L2 ACL */
1115   rv =
1116     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1117                               a->ip6_table_index, a->l2_table_index, 0);
1118   /* Unset the MACIP ACL index */
1119   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1120   return rv;
1121 }
1122
1123 /* No check for validity of sw_if_index - the callers were supposed to validate */
1124
1125 static int
1126 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1127                              u32 macip_acl_index)
1128 {
1129   macip_acl_list_t *a;
1130   int rv;
1131   if (pool_is_free_index (am->macip_acls, macip_acl_index))
1132     {
1133       return -1;
1134     }
1135   a = &am->macip_acls[macip_acl_index];
1136   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1137   /* If there already a MACIP ACL applied, unapply it */
1138   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1139     macip_acl_interface_del_acl(am, sw_if_index);
1140   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1141   /* Apply the classifier tables for L2 ACLs */
1142   rv =
1143     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1144                               a->ip6_table_index, a->l2_table_index, 1);
1145   return rv;
1146 }
1147
1148 static int
1149 macip_acl_del_list (u32 acl_list_index)
1150 {
1151   acl_main_t *am = &acl_main;
1152   macip_acl_list_t *a;
1153   int i;
1154   if (pool_is_free_index (am->macip_acls, acl_list_index))
1155     {
1156       return -1;
1157     }
1158
1159   /* delete any references to the ACL */
1160   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1161     {
1162       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1163         {
1164           macip_acl_interface_del_acl (am, i);
1165         }
1166     }
1167
1168   /* Now that classifier tables are detached, clean them up */
1169   macip_destroy_classify_tables (am, acl_list_index);
1170
1171   /* now we can delete the ACL itself */
1172   a = &am->macip_acls[acl_list_index];
1173   if (a->rules)
1174     {
1175       clib_mem_free (a->rules);
1176     }
1177   pool_put (am->macip_acls, a);
1178   return 0;
1179 }
1180
1181
1182 static int
1183 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1184                                  u32 acl_list_index)
1185 {
1186   acl_main_t *am = &acl_main;
1187   int rv = -1;
1188   if (is_add)
1189     {
1190       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1191     }
1192   else
1193     {
1194       rv = macip_acl_interface_del_acl (am, sw_if_index);
1195     }
1196   return rv;
1197 }
1198
1199 /*
1200  * If the client does not allocate enough memory for a variable-length
1201  * message, and then proceed to use it as if the full memory allocated,
1202  * absent the check we happily consume that on the VPP side, and go
1203  * along as if nothing happened. However, the resulting
1204  * effects range from just garbage in the API decode
1205  * (because the decoder snoops too far), to potential memory
1206  * corruptions.
1207  *
1208  * This verifies that the actual length of the message is
1209  * at least expected_len, and complains loudly if it is not.
1210  *
1211  * A failing check here is 100% a software bug on the API user side,
1212  * so we might as well yell.
1213  *
1214  */
1215 static int verify_message_len(void *mp, u32 expected_len, char *where)
1216 {
1217   u32 supplied_len = vl_msg_api_get_msg_length (mp);
1218   if (supplied_len < expected_len) {
1219       clib_warning("%s: Supplied message length %d is less than expected %d",
1220                    where, supplied_len, expected_len);
1221       return 0;
1222   } else {
1223       return 1;
1224   }
1225 }
1226
1227 /* API message handler */
1228 static void
1229 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1230 {
1231   vl_api_acl_add_replace_reply_t *rmp;
1232   acl_main_t *am = &acl_main;
1233   int rv;
1234   u32 acl_list_index = ntohl (mp->acl_index);
1235   u32 acl_count = ntohl (mp->count);
1236   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1237
1238   if (verify_message_len(mp, expected_len, "acl_add_replace")) {
1239       rv = acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1240   } else {
1241       rv = VNET_API_ERROR_INVALID_VALUE;
1242   }
1243
1244   /* *INDENT-OFF* */
1245   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1246   ({
1247     rmp->acl_index = htonl(acl_list_index);
1248   }));
1249   /* *INDENT-ON* */
1250 }
1251
1252 static void
1253 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1254 {
1255   acl_main_t *am = &acl_main;
1256   vl_api_acl_del_reply_t *rmp;
1257   int rv;
1258
1259   rv = acl_del_list (ntohl (mp->acl_index));
1260
1261   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1262 }
1263
1264 static void
1265 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1266 {
1267   acl_main_t *am = &acl_main;
1268   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1269   u32 sw_if_index = ntohl (mp->sw_if_index);
1270   vl_api_acl_interface_add_del_reply_t *rmp;
1271   int rv = -1;
1272
1273   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1274     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1275   else
1276     rv =
1277       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1278                                      mp->is_input, ntohl (mp->acl_index));
1279
1280   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1281 }
1282
1283 static void
1284 vl_api_acl_interface_set_acl_list_t_handler
1285   (vl_api_acl_interface_set_acl_list_t * mp)
1286 {
1287   acl_main_t *am = &acl_main;
1288   vl_api_acl_interface_set_acl_list_reply_t *rmp;
1289   int rv = 0;
1290   int i;
1291   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1292   u32 sw_if_index = ntohl (mp->sw_if_index);
1293
1294   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1295     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1296   else
1297     {
1298       acl_interface_reset_inout_acls (sw_if_index, 0);
1299       acl_interface_reset_inout_acls (sw_if_index, 1);
1300
1301       for (i = 0; i < mp->count; i++)
1302         {
1303           if(acl_is_not_defined(am, ntohl (mp->acls[i]))) {
1304             /* ACL does not exist, so we can not apply it */
1305             rv = -1;
1306           }
1307         }
1308       if (0 == rv) {
1309         for (i = 0; i < mp->count; i++)
1310           {
1311             acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1312                                        ntohl (mp->acls[i]));
1313           }
1314       }
1315     }
1316
1317   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1318 }
1319
1320 static void
1321 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1322 {
1323   api_rule->is_permit = r->is_permit;
1324   api_rule->is_ipv6 = r->is_ipv6;
1325   if(r->is_ipv6)
1326     {
1327       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1328       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1329     }
1330   else
1331     {
1332       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1333       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1334     }
1335   api_rule->src_ip_prefix_len = r->src_prefixlen;
1336   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1337   api_rule->proto = r->proto;
1338   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1339   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1340   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1341   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1342   api_rule->tcp_flags_mask = r->tcp_flags_mask;
1343   api_rule->tcp_flags_value = r->tcp_flags_value;
1344 }
1345
1346 static void
1347 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1348                   acl_list_t * acl, u32 context)
1349 {
1350   vl_api_acl_details_t *mp;
1351   vl_api_acl_rule_t *rules;
1352   int i;
1353   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1354
1355   mp = vl_msg_api_alloc (msg_size);
1356   memset (mp, 0, msg_size);
1357   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1358
1359   /* fill in the message */
1360   mp->context = context;
1361   mp->count = htonl (acl->count);
1362   mp->acl_index = htonl (acl - am->acls);
1363   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1364   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1365   rules = mp->r;
1366   for (i = 0; i < acl->count; i++)
1367     {
1368       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1369     }
1370
1371   clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1372   vl_msg_api_send_shmem (q, (u8 *) & mp);
1373 }
1374
1375
1376 static void
1377 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1378 {
1379   acl_main_t *am = &acl_main;
1380   u32 acl_index;
1381   acl_list_t *acl;
1382
1383   int rv = -1;
1384   unix_shared_memory_queue_t *q;
1385
1386   q = vl_api_client_index_to_input_queue (mp->client_index);
1387   if (q == 0)
1388     {
1389       return;
1390     }
1391
1392   if (mp->acl_index == ~0)
1393     {
1394     /* *INDENT-OFF* */
1395     /* Just dump all ACLs */
1396     pool_foreach (acl, am->acls,
1397     ({
1398       send_acl_details(am, q, acl, mp->context);
1399     }));
1400     /* *INDENT-ON* */
1401     }
1402   else
1403     {
1404       acl_index = ntohl (mp->acl_index);
1405       if (!pool_is_free_index (am->acls, acl_index))
1406         {
1407           acl = &am->acls[acl_index];
1408           send_acl_details (am, q, acl, mp->context);
1409         }
1410     }
1411
1412   if (rv == -1)
1413     {
1414       /* FIXME API: should we signal an error here at all ? */
1415       return;
1416     }
1417 }
1418
1419 static void
1420 send_acl_interface_list_details (acl_main_t * am,
1421                                  unix_shared_memory_queue_t * q,
1422                                  u32 sw_if_index, u32 context)
1423 {
1424   vl_api_acl_interface_list_details_t *mp;
1425   int msg_size;
1426   int n_input;
1427   int n_output;
1428   int count;
1429   int i = 0;
1430
1431   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1432   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1433
1434   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1435   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1436   count = n_input + n_output;
1437
1438   msg_size = sizeof (*mp);
1439   msg_size += sizeof (mp->acls[0]) * count;
1440
1441   mp = vl_msg_api_alloc (msg_size);
1442   memset (mp, 0, msg_size);
1443   mp->_vl_msg_id =
1444     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1445
1446   /* fill in the message */
1447   mp->context = context;
1448   mp->sw_if_index = htonl (sw_if_index);
1449   mp->count = count;
1450   mp->n_input = n_input;
1451   for (i = 0; i < n_input; i++)
1452     {
1453       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1454     }
1455   for (i = 0; i < n_output; i++)
1456     {
1457       mp->acls[n_input + i] =
1458         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1459     }
1460
1461   vl_msg_api_send_shmem (q, (u8 *) & mp);
1462 }
1463
1464 static void
1465 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1466                                           mp)
1467 {
1468   acl_main_t *am = &acl_main;
1469   vnet_sw_interface_t *swif;
1470   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1471
1472   u32 sw_if_index;
1473   unix_shared_memory_queue_t *q;
1474
1475   q = vl_api_client_index_to_input_queue (mp->client_index);
1476   if (q == 0)
1477     {
1478       return;
1479     }
1480
1481   if (mp->sw_if_index == ~0)
1482     {
1483     /* *INDENT-OFF* */
1484     pool_foreach (swif, im->sw_interfaces,
1485     ({
1486       send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1487     }));
1488     /* *INDENT-ON* */
1489     }
1490   else
1491     {
1492       sw_if_index = ntohl (mp->sw_if_index);
1493       if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1494         send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1495     }
1496 }
1497
1498 /* MACIP ACL API handlers */
1499
1500 static void
1501 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1502 {
1503   vl_api_macip_acl_add_reply_t *rmp;
1504   acl_main_t *am = &acl_main;
1505   int rv;
1506   u32 acl_list_index = ~0;
1507   u32 acl_count = ntohl (mp->count);
1508   u32 expected_len = sizeof(*mp) + acl_count*sizeof(mp->r[0]);
1509
1510   if (verify_message_len(mp, expected_len, "macip_acl_add")) {
1511       rv = macip_acl_add_list (acl_count, mp->r, &acl_list_index, mp->tag);
1512   } else {
1513       rv = VNET_API_ERROR_INVALID_VALUE;
1514   }
1515
1516   /* *INDENT-OFF* */
1517   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1518   ({
1519     rmp->acl_index = htonl(acl_list_index);
1520   }));
1521   /* *INDENT-ON* */
1522 }
1523
1524 static void
1525 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1526 {
1527   acl_main_t *am = &acl_main;
1528   vl_api_macip_acl_del_reply_t *rmp;
1529   int rv;
1530
1531   rv = macip_acl_del_list (ntohl (mp->acl_index));
1532
1533   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1534 }
1535
1536 static void
1537 vl_api_macip_acl_interface_add_del_t_handler
1538   (vl_api_macip_acl_interface_add_del_t * mp)
1539 {
1540   acl_main_t *am = &acl_main;
1541   vl_api_macip_acl_interface_add_del_reply_t *rmp;
1542   int rv = -1;
1543   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1544   u32 sw_if_index = ntohl (mp->sw_if_index);
1545
1546   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1547     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1548   else
1549     rv =
1550       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1551                                      ntohl (mp->acl_index));
1552
1553   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1554 }
1555
1556 static void
1557 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1558                         macip_acl_list_t * acl, u32 context)
1559 {
1560   vl_api_macip_acl_details_t *mp;
1561   vl_api_macip_acl_rule_t *rules;
1562   macip_acl_rule_t *r;
1563   int i;
1564   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1565
1566   mp = vl_msg_api_alloc (msg_size);
1567   memset (mp, 0, msg_size);
1568   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1569
1570   /* fill in the message */
1571   mp->context = context;
1572   if (acl)
1573     {
1574       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1575       mp->count = htonl (acl->count);
1576       mp->acl_index = htonl (acl - am->macip_acls);
1577       rules = mp->r;
1578       for (i = 0; i < acl->count; i++)
1579         {
1580           r = &acl->rules[i];
1581           rules[i].is_permit = r->is_permit;
1582           rules[i].is_ipv6 = r->is_ipv6;
1583           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1584           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1585                   sizeof (r->src_mac_mask));
1586           if (r->is_ipv6)
1587             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1588                   sizeof (r->src_ip_addr.ip6));
1589           else
1590             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1591                   sizeof (r->src_ip_addr.ip4));
1592           rules[i].src_ip_prefix_len = r->src_prefixlen;
1593         }
1594     }
1595   else
1596     {
1597       /* No martini, no party - no ACL applied to this interface. */
1598       mp->acl_index = ~0;
1599       mp->count = 0;
1600     }
1601
1602   vl_msg_api_send_shmem (q, (u8 *) & mp);
1603 }
1604
1605
1606 static void
1607 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1608 {
1609   acl_main_t *am = &acl_main;
1610   macip_acl_list_t *acl;
1611
1612   unix_shared_memory_queue_t *q;
1613
1614   q = vl_api_client_index_to_input_queue (mp->client_index);
1615   if (q == 0)
1616     {
1617       return;
1618     }
1619
1620   if (mp->acl_index == ~0)
1621     {
1622       /* Just dump all ACLs for now, with sw_if_index = ~0 */
1623       pool_foreach (acl, am->macip_acls, (
1624                                            {
1625                                            send_macip_acl_details (am, q, acl,
1626                                                                    mp->
1627                                                                    context);}
1628                     ));
1629       /* *INDENT-ON* */
1630     }
1631   else
1632     {
1633       u32 acl_index = ntohl (mp->acl_index);
1634       if (!pool_is_free_index (am->macip_acls, acl_index))
1635         {
1636           acl = &am->macip_acls[acl_index];
1637           send_macip_acl_details (am, q, acl, mp->context);
1638         }
1639     }
1640 }
1641
1642 static void
1643 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1644                                           mp)
1645 {
1646   acl_main_t *am = &acl_main;
1647   vl_api_macip_acl_interface_get_reply_t *rmp;
1648   u32 count = vec_len (am->macip_acl_by_sw_if_index);
1649   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1650   unix_shared_memory_queue_t *q;
1651   int i;
1652
1653   q = vl_api_client_index_to_input_queue (mp->client_index);
1654   if (q == 0)
1655     {
1656       return;
1657     }
1658
1659   rmp = vl_msg_api_alloc (msg_size);
1660   memset (rmp, 0, msg_size);
1661   rmp->_vl_msg_id =
1662     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1663   rmp->context = mp->context;
1664   rmp->count = htonl (count);
1665   for (i = 0; i < count; i++)
1666     {
1667       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1668     }
1669
1670   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1671 }
1672
1673 /* Set up the API message handling tables */
1674 static clib_error_t *
1675 acl_plugin_api_hookup (vlib_main_t * vm)
1676 {
1677   acl_main_t *am = &acl_main;
1678 #define _(N,n)                                                  \
1679     vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
1680                            #n,                                  \
1681                            vl_api_##n##_t_handler,              \
1682                            vl_noop_handler,                     \
1683                            vl_api_##n##_t_endian,               \
1684                            vl_api_##n##_t_print,                \
1685                            sizeof(vl_api_##n##_t), 1);
1686   foreach_acl_plugin_api_msg;
1687 #undef _
1688
1689   return 0;
1690 }
1691
1692 #define vl_msg_name_crc_list
1693 #include <acl/acl_all_api_h.h>
1694 #undef vl_msg_name_crc_list
1695
1696 static void
1697 setup_message_id_table (acl_main_t * am, api_main_t * apim)
1698 {
1699 #define _(id,n,crc) \
1700   vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
1701   foreach_vl_msg_name_crc_acl;
1702 #undef _
1703 }
1704
1705 static void
1706 acl_setup_fa_nodes (void)
1707 {
1708   vlib_main_t *vm = vlib_get_main ();
1709   acl_main_t *am = &acl_main;
1710   vlib_node_t *n, *n4, *n6;
1711
1712   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1713   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip4-l2");
1714   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-in-ip6-l2");
1715
1716
1717   am->l2_input_classify_next_acl_ip4 =
1718     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1719   am->l2_input_classify_next_acl_ip6 =
1720     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1721
1722   feat_bitmap_init_next_nodes (vm, n4->index, L2INPUT_N_FEAT,
1723                                l2input_get_feat_names (),
1724                                am->fa_acl_in_ip4_l2_node_feat_next_node_index);
1725
1726   feat_bitmap_init_next_nodes (vm, n6->index, L2INPUT_N_FEAT,
1727                                l2input_get_feat_names (),
1728                                am->fa_acl_in_ip6_l2_node_feat_next_node_index);
1729
1730
1731   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1732   n4 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip4-l2");
1733   n6 = vlib_get_node_by_name (vm, (u8 *) "acl-plugin-out-ip6-l2");
1734
1735   am->l2_output_classify_next_acl_ip4 =
1736     vlib_node_add_next_with_slot (vm, n->index, n4->index, ~0);
1737   am->l2_output_classify_next_acl_ip6 =
1738     vlib_node_add_next_with_slot (vm, n->index, n6->index, ~0);
1739
1740   feat_bitmap_init_next_nodes (vm, n4->index, L2OUTPUT_N_FEAT,
1741                                l2output_get_feat_names (),
1742                                am->fa_acl_out_ip4_l2_node_feat_next_node_index);
1743
1744   feat_bitmap_init_next_nodes (vm, n6->index, L2OUTPUT_N_FEAT,
1745                                l2output_get_feat_names (),
1746                                am->fa_acl_out_ip6_l2_node_feat_next_node_index);
1747 }
1748
1749 static void
1750 acl_set_timeout_sec(int timeout_type, u32 value)
1751 {
1752   acl_main_t *am = &acl_main;
1753   clib_time_t *ct = &am->vlib_main->clib_time;
1754
1755   if (timeout_type < ACL_N_TIMEOUTS) {
1756     am->session_timeout_sec[timeout_type] = value;
1757   } else {
1758     clib_warning("Unknown timeout type %d", timeout_type);
1759     return;
1760   }
1761   am->session_timeout[timeout_type] = (u64)(((f64)value)/ct->seconds_per_clock);
1762 }
1763
1764 static void
1765 acl_set_session_max_entries(u32 value)
1766 {
1767   acl_main_t *am = &acl_main;
1768   am->fa_conn_table_max_entries = value;
1769 }
1770
1771 static int
1772 acl_set_skip_ipv6_eh(u32 eh, u32 value)
1773 {
1774   acl_main_t *am = &acl_main;
1775   if ((eh < 256) && (value < 2))
1776     {
1777       am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, eh, value);
1778       return 1;
1779     }
1780   else
1781     return 0;
1782 }
1783
1784
1785 static clib_error_t *
1786 acl_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
1787 {
1788   acl_main_t *am = &acl_main;
1789   if (0 == is_add) {
1790     vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1791                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, sw_if_index);
1792     /* also unapply any ACLs in case the users did not do so. */
1793     macip_acl_interface_del_acl(am, sw_if_index);
1794     acl_interface_reset_inout_acls (sw_if_index, 0);
1795     acl_interface_reset_inout_acls (sw_if_index, 1);
1796   }
1797   return 0;
1798 }
1799
1800 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (acl_sw_interface_add_del);
1801
1802 static clib_error_t *
1803 acl_set_aclplugin_fn (vlib_main_t * vm,
1804                               unformat_input_t * input,
1805                               vlib_cli_command_t * cmd)
1806 {
1807   clib_error_t *error = 0;
1808   u32 timeout = 0;
1809   u32 val = 0;
1810   u32 eh_val = 0;
1811   uword memory_size = 0;
1812   acl_main_t *am = &acl_main;
1813
1814   if (unformat (input, "skip-ipv6-extension-header %u %u", &eh_val, &val)) {
1815     if(!acl_set_skip_ipv6_eh(eh_val, val)) {
1816       error = clib_error_return(0, "expecting eh=0..255, value=0..1");
1817     }
1818     goto done;
1819   }
1820   if (unformat (input, "use-hash-acl-matching %u", &val))
1821     {
1822       am->use_hash_acl_matching = (val !=0);
1823       goto done;
1824     }
1825   if (unformat (input, "l4-match-nonfirst-fragment %u", &val))
1826     {
1827       am->l4_match_nonfirst_fragment = (val != 0);
1828       goto done;
1829     }
1830   if (unformat (input, "session")) {
1831     if (unformat (input, "clear")) {
1832       acl_main_t *am = &acl_main;
1833       vlib_process_signal_event (am->vlib_main, am->fa_cleaner_node_index,
1834                                ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX, ~0);
1835           goto done;
1836     }
1837     if (unformat (input, "table")) {
1838       /* The commands here are for tuning/testing. No user-serviceable parts inside */
1839       if (unformat (input, "max-entries")) {
1840         if (!unformat(input, "%u", &val)) {
1841           error = clib_error_return(0,
1842                                     "expecting maximum number of entries, got `%U`",
1843                                     format_unformat_error, input);
1844           goto done;
1845         } else {
1846           acl_set_session_max_entries(val);
1847           goto done;
1848         }
1849       }
1850       if (unformat (input, "hash-table-buckets")) {
1851         if (!unformat(input, "%u", &val)) {
1852           error = clib_error_return(0,
1853                                     "expecting maximum number of hash table buckets, got `%U`",
1854                                     format_unformat_error, input);
1855           goto done;
1856         } else {
1857           am->fa_conn_table_hash_num_buckets = val;
1858           goto done;
1859         }
1860       }
1861       if (unformat (input, "hash-table-memory")) {
1862         if (!unformat(input, "%U", unformat_memory_size, &memory_size)) {
1863           error = clib_error_return(0,
1864                                     "expecting maximum amount of hash table memory, got `%U`",
1865                                     format_unformat_error, input);
1866           goto done;
1867         } else {
1868           am->fa_conn_table_hash_memory_size = memory_size;
1869           goto done;
1870         }
1871       }
1872       goto done;
1873     }
1874     if (unformat (input, "timeout")) {
1875       if (unformat(input, "udp")) {
1876         if(unformat(input, "idle")) {
1877           if (!unformat(input, "%u", &timeout)) {
1878             error = clib_error_return(0,
1879                                       "expecting timeout value in seconds, got `%U`",
1880                                       format_unformat_error, input);
1881             goto done;
1882           } else {
1883             acl_set_timeout_sec(ACL_TIMEOUT_UDP_IDLE, timeout);
1884             goto done;
1885           }
1886         }
1887       }
1888       if (unformat(input, "tcp")) {
1889         if(unformat(input, "idle")) {
1890           if (!unformat(input, "%u", &timeout)) {
1891             error = clib_error_return(0,
1892                                       "expecting timeout value in seconds, got `%U`",
1893                                       format_unformat_error, input);
1894             goto done;
1895           } else {
1896             acl_set_timeout_sec(ACL_TIMEOUT_TCP_IDLE, timeout);
1897             goto done;
1898           }
1899         }
1900         if(unformat(input, "transient")) {
1901           if (!unformat(input, "%u", &timeout)) {
1902             error = clib_error_return(0,
1903                                       "expecting timeout value in seconds, got `%U`",
1904                                       format_unformat_error, input);
1905             goto done;
1906           } else {
1907             acl_set_timeout_sec(ACL_TIMEOUT_TCP_TRANSIENT, timeout);
1908             goto done;
1909           }
1910         }
1911       }
1912       goto done;
1913     }
1914   }
1915 done:
1916   return error;
1917 }
1918
1919 static clib_error_t *
1920 acl_show_aclplugin_fn (vlib_main_t * vm,
1921                               unformat_input_t * input,
1922                               vlib_cli_command_t * cmd)
1923 {
1924   clib_error_t *error = 0;
1925   acl_main_t *am = &acl_main;
1926   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1927   u32 *pj;
1928
1929   vnet_sw_interface_t *swif;
1930
1931   if (unformat (input, "sessions"))
1932     {
1933       u8 * out0 = format(0, "");
1934       u16 wk;
1935       u32 show_bihash_verbose = 0;
1936       unformat (input, "verbose %u", &show_bihash_verbose);
1937       pool_foreach (swif, im->sw_interfaces,
1938       ({
1939         u32 sw_if_index =  swif->sw_if_index;
1940         u64 n_adds = sw_if_index < vec_len(am->fa_session_adds_by_sw_if_index) ? am->fa_session_adds_by_sw_if_index[sw_if_index] : 0;
1941         u64 n_dels = sw_if_index < vec_len(am->fa_session_dels_by_sw_if_index) ? am->fa_session_dels_by_sw_if_index[sw_if_index] : 0;
1942         out0 = format(out0, "sw_if_index %d: add %lu - del %lu = %lu\n", sw_if_index, n_adds, n_dels, n_adds - n_dels);
1943       }));
1944       {
1945         u64 n_adds = am->fa_session_total_adds;
1946         u64 n_dels = am->fa_session_total_dels;
1947         out0 = format(out0, "TOTAL: add %lu - del %lu = %lu\n", n_adds, n_dels, n_adds - n_dels);
1948       }
1949       out0 = format(out0, "\n\nPer-worker data:\n");
1950       for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
1951         acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
1952         out0 = format(out0, "Worker #%d:\n", wk);
1953         out0 = format(out0, "  Next expiry time: %lu\n", pw->next_expiry_time);
1954         out0 = format(out0, "  Requeue until time: %lu\n", pw->requeue_until_time);
1955         out0 = format(out0, "  Current time wait interval: %lu\n", pw->current_time_wait_interval);
1956         out0 = format(out0, "  Count of deleted sessions: %lu\n", pw->cnt_deleted_sessions);
1957         out0 = format(out0, "  Delete already deleted: %lu\n", pw->cnt_already_deleted_sessions);
1958         out0 = format(out0, "  Session timers restarted: %lu\n", pw->cnt_session_timer_restarted);
1959         out0 = format(out0, "  Swipe until this time: %lu\n", pw->swipe_end_time);
1960         out0 = format(out0, "  sw_if_index serviced bitmap: %U\n", format_bitmap_hex, pw->serviced_sw_if_index_bitmap);
1961         out0 = format(out0, "  pending clear intfc bitmap : %U\n", format_bitmap_hex, pw->pending_clear_sw_if_index_bitmap);
1962         out0 = format(out0, "  clear in progress: %u\n", pw->clear_in_process);
1963         out0 = format(out0, "  interrupt is pending: %d\n", pw->interrupt_is_pending);
1964         out0 = format(out0, "  interrupt is needed: %d\n", pw->interrupt_is_needed);
1965         out0 = format(out0, "  interrupt is unwanted: %d\n", pw->interrupt_is_unwanted);
1966       }
1967       out0 = format(out0, "\n\nConn cleaner thread counters:\n");
1968 #define _(cnt, desc) out0 = format(out0, "             %20lu: %s\n", am->cnt, desc);
1969       foreach_fa_cleaner_counter;
1970 #undef _
1971       vlib_cli_output(vm, "\n\n%s\n\n", out0);
1972       vlib_cli_output(vm, "Sessions per interval: min %lu max %lu increment: %f ms current: %f ms",
1973               am->fa_min_deleted_sessions_per_interval, am->fa_max_deleted_sessions_per_interval,
1974               am->fa_cleaner_wait_time_increment * 1000.0, ((f64)am->fa_current_cleaner_timer_wait_interval) * 1000.0/(f64)vm->clib_time.clocks_per_second);
1975
1976       vec_free(out0);
1977       show_fa_sessions_hash(vm, show_bihash_verbose);
1978     }
1979   else if (unformat (input, "interface"))
1980     {
1981       u32 sw_if_index = ~0;
1982       u32 swi;
1983       u8 * out0 = format(0, "");
1984       unformat (input, "sw_if_index %u", &sw_if_index);
1985       for(swi = 0; (swi < vec_len(am->input_acl_vec_by_sw_if_index)) ||
1986                    (swi < vec_len(am->output_acl_vec_by_sw_if_index)); swi++) {
1987         out0 = format(out0, "sw_if_index %d:\n", swi);
1988
1989         if ((swi < vec_len(am->input_acl_vec_by_sw_if_index)) &&
1990             (vec_len(am->input_acl_vec_by_sw_if_index[swi]) > 0)) {
1991           out0 = format(out0, "  input acl(s): ");
1992           vec_foreach(pj, am->input_acl_vec_by_sw_if_index[swi]) {
1993             out0 = format(out0, "%d ", *pj);
1994           }
1995           out0 = format(out0, "\n");
1996         }
1997
1998         if ((swi < vec_len(am->output_acl_vec_by_sw_if_index)) &&
1999             (vec_len(am->output_acl_vec_by_sw_if_index[swi]) > 0)) {
2000           out0 = format(out0, "  output acl(s): ");
2001           vec_foreach(pj, am->output_acl_vec_by_sw_if_index[swi]) {
2002             out0 = format(out0, "%d ", *pj);
2003           }
2004           out0 = format(out0, "\n");
2005         }
2006
2007       }
2008       vlib_cli_output(vm, "\n%s\n", out0);
2009       vec_free(out0);
2010     }
2011   else if (unformat (input, "acl"))
2012     {
2013       u32 acl_index = ~0;
2014       u32 i;
2015       u8 * out0 = format(0, "");
2016       unformat (input, "index %u", &acl_index);
2017       for(i=0; i<vec_len(am->acls); i++) {
2018         if (acl_is_not_defined(am, i)) {
2019           /* don't attempt to show the ACLs that do not exist */
2020           continue;
2021         }
2022         if ((acl_index != ~0) && (acl_index != i)) {
2023           continue;
2024         }
2025         out0 = format(out0, "acl-index %u count %u tag {%s}\n", i, am->acls[i].count, am->acls[i].tag);
2026         acl_rule_t *r;
2027         int j;
2028         for(j=0; j<am->acls[i].count; j++) {
2029           r = &am->acls[i].rules[j];
2030           out0 = format(out0, "  %4d: %s ", j, r->is_ipv6 ? "ipv6" : "ipv4");
2031           out0 = format_acl_action(out0, r->is_permit);
2032           out0 = format(out0, " src %U/%d", format_ip46_address, &r->src, IP46_TYPE_ANY, r->src_prefixlen);
2033           out0 = format(out0, " dst %U/%d", format_ip46_address, &r->dst, IP46_TYPE_ANY, r->dst_prefixlen);
2034           out0 = format(out0, " proto %d", r->proto);
2035           out0 = format(out0, " sport %d", r->src_port_or_type_first);
2036           if (r->src_port_or_type_first != r->src_port_or_type_last) {
2037             out0 = format(out0, "-%d", r->src_port_or_type_last);
2038           }
2039           out0 = format(out0, " dport %d", r->dst_port_or_code_first);
2040           if (r->dst_port_or_code_first != r->dst_port_or_code_last) {
2041             out0 = format(out0, "-%d", r->dst_port_or_code_last);
2042           }
2043           if (r->tcp_flags_mask || r->tcp_flags_value) {
2044             out0 = format(out0, " tcpflags %d mask %d", r->tcp_flags_value, r->tcp_flags_mask);
2045           }
2046           out0 = format(out0, "\n");
2047         }
2048
2049         if (i<vec_len(am->input_sw_if_index_vec_by_acl)) {
2050           out0 = format(out0, "  applied inbound on sw_if_index: ");
2051           vec_foreach(pj, am->input_sw_if_index_vec_by_acl[i]) {
2052             out0 = format(out0, "%d ", *pj);
2053           }
2054           out0 = format(out0, "\n");
2055         }
2056         if (i<vec_len(am->output_sw_if_index_vec_by_acl)) {
2057           out0 = format(out0, "  applied outbound on sw_if_index: ");
2058           vec_foreach(pj, am->output_sw_if_index_vec_by_acl[i]) {
2059             out0 = format(out0, "%d ", *pj);
2060           }
2061           out0 = format(out0, "\n");
2062         }
2063       }
2064       vlib_cli_output(vm, "\n%s\n", out0);
2065       vec_free(out0);
2066     }
2067   else if (unformat (input, "tables"))
2068     {
2069       ace_mask_type_entry_t *mte;
2070       u32 acl_index = ~0;
2071       u32 sw_if_index = ~0;
2072       int show_acl_hash_info = 0;
2073       int show_applied_info = 0;
2074       int show_mask_type = 0;
2075       int show_bihash = 0;
2076       u32 show_bihash_verbose = 0;
2077
2078       if (unformat (input, "acl")) {
2079         show_acl_hash_info = 1;
2080         /* mask-type is handy to see as well right there */
2081         show_mask_type = 1;
2082         unformat (input, "index %u", &acl_index);
2083       } else if (unformat (input, "applied")) {
2084         show_applied_info = 1;
2085         unformat (input, "sw_if_index %u", &sw_if_index);
2086       } else if (unformat (input, "mask")) {
2087         show_mask_type = 1;
2088       } else if (unformat (input, "hash")) {
2089         show_bihash = 1;
2090         unformat (input, "verbose %u", &show_bihash_verbose);
2091       }
2092
2093       if ( ! (show_mask_type || show_acl_hash_info || show_applied_info || show_bihash) ) {
2094         /* if no qualifiers specified, show all */
2095         show_mask_type = 1;
2096         show_acl_hash_info = 1;
2097         show_applied_info = 1;
2098         show_bihash = 1;
2099       }
2100
2101       if (show_mask_type) {
2102         vlib_cli_output(vm, "Mask-type entries:");
2103         /* *INDENT-OFF* */
2104         pool_foreach(mte, am->ace_mask_type_pool,
2105         ({
2106           vlib_cli_output(vm, "     %3d: %016llx %016llx %016llx %016llx %016llx %016llx  refcount %d",
2107                         mte - am->ace_mask_type_pool,
2108                         mte->mask.kv.key[0], mte->mask.kv.key[1], mte->mask.kv.key[2],
2109                         mte->mask.kv.key[3], mte->mask.kv.key[4], mte->mask.kv.value, mte->refcount);
2110         }));
2111         /* *INDENT-ON* */
2112       }
2113
2114       if (show_acl_hash_info) {
2115         u32 i,j;
2116         u8 * out0 = format(0, "");
2117         u64 *m;
2118         out0 = format(out0, "Mask-ready ACL representations\n");
2119         for (i=0; i< vec_len(am->hash_acl_infos); i++) {
2120           if ((acl_index != ~0) && (acl_index != i)) {
2121             continue;
2122           }
2123           hash_acl_info_t *ha = &am->hash_acl_infos[i];
2124           out0 = format(out0, "acl-index %u bitmask-ready layout\n", i);
2125           out0 = format(out0, "  mask type index bitmap: %U\n", format_bitmap_hex, ha->mask_type_index_bitmap);
2126           for(j=0; j<vec_len(ha->rules); j++) {
2127             hash_ace_info_t *pa = &ha->rules[j];
2128             m = (u64 *)&pa->match;
2129             out0 = format(out0, "    %4d: %016llx %016llx %016llx %016llx %016llx %016llx mask index %d acl %d rule %d action %d src/dst portrange not ^2: %d,%d\n",
2130                                 j, m[0], m[1], m[2], m[3], m[4], m[5], pa->mask_type_index,
2131                                 pa->acl_index, pa->ace_index, pa->action,
2132                                 pa->src_portrange_not_powerof2, pa->dst_portrange_not_powerof2);
2133           }
2134         }
2135         vlib_cli_output(vm, "\n%s\n", out0);
2136         vec_free(out0);
2137       }
2138
2139       if (show_applied_info) {
2140         u32 swi, j;
2141         u8 * out0 = format(0, "");
2142         out0 = format(out0, "Applied lookup entries for interfaces\n");
2143
2144         for(swi = 0; (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) ||
2145                    (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) ||
2146                    (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) ||
2147                    (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)); swi++) {
2148           if ((sw_if_index != ~0) && (sw_if_index != swi)) {
2149             continue;
2150           }
2151           out0 = format(out0, "sw_if_index %d:\n", swi);
2152           if (swi < vec_len(am->input_applied_hash_acl_info_by_sw_if_index)) {
2153             applied_hash_acl_info_t *pal = &am->input_applied_hash_acl_info_by_sw_if_index[swi];
2154             out0 = format(out0, "  input lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2155           }
2156           if (swi < vec_len(am->input_hash_entry_vec_by_sw_if_index)) {
2157             out0 = format(out0, "  input lookup applied entries:\n");
2158             for(j=0; j<vec_len(am->input_hash_entry_vec_by_sw_if_index[swi]); j++) {
2159               applied_hash_ace_entry_t *pae = &am->input_hash_entry_vec_by_sw_if_index[swi][j];
2160               out0 = format(out0, "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d\n",
2161                                        j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2162                                        pae->next_applied_entry_index, pae->prev_applied_entry_index);
2163             }
2164           }
2165
2166           if (swi < vec_len(am->output_applied_hash_acl_info_by_sw_if_index)) {
2167             applied_hash_acl_info_t *pal = &am->output_applied_hash_acl_info_by_sw_if_index[swi];
2168             out0 = format(out0, "  output lookup mask_type_index_bitmap: %U\n", format_bitmap_hex, pal->mask_type_index_bitmap);
2169           }
2170           if (swi < vec_len(am->output_hash_entry_vec_by_sw_if_index)) {
2171             out0 = format(out0, "  output lookup applied entries:\n");
2172             for(j=0; j<vec_len(am->output_hash_entry_vec_by_sw_if_index[swi]); j++) {
2173               applied_hash_ace_entry_t *pae = &am->output_hash_entry_vec_by_sw_if_index[swi][j];
2174               out0 = format(out0, "    %4d: acl %d rule %d action %d bitmask-ready rule %d next %d prev %d\n",
2175                                        j, pae->acl_index, pae->ace_index, pae->action, pae->hash_ace_info_index,
2176                                        pae->next_applied_entry_index, pae->prev_applied_entry_index);
2177             }
2178           }
2179
2180         }
2181         vlib_cli_output(vm, "\n%s\n", out0);
2182         vec_free(out0);
2183       }
2184
2185       if (show_bihash) {
2186         show_hash_acl_hash(vm, am, show_bihash_verbose);
2187       }
2188     }
2189   return error;
2190 }
2191
2192
2193  /* *INDENT-OFF* */
2194 VLIB_CLI_COMMAND (aclplugin_set_command, static) = {
2195     .path = "set acl-plugin",
2196     .short_help = "set acl-plugin session timeout {{udp idle}|tcp {idle|transient}} <seconds>",
2197     .function = acl_set_aclplugin_fn,
2198 };
2199
2200 VLIB_CLI_COMMAND (aclplugin_show_command, static) = {
2201     .path = "show acl-plugin",
2202     .short_help = "show acl-plugin {sessions|acl|interface|tables}",
2203     .function = acl_show_aclplugin_fn,
2204 };
2205 /* *INDENT-ON* */
2206
2207
2208
2209 static clib_error_t *
2210 acl_init (vlib_main_t * vm)
2211 {
2212   acl_main_t *am = &acl_main;
2213   clib_error_t *error = 0;
2214   memset (am, 0, sizeof (*am));
2215   am->vlib_main = vm;
2216   am->vnet_main = vnet_get_main ();
2217
2218   u8 *name = format (0, "acl_%08x%c", api_version, 0);
2219
2220   /* Ask for a correctly-sized block of API message decode slots */
2221   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
2222                                             VL_MSG_FIRST_AVAILABLE);
2223
2224   error = acl_plugin_api_hookup (vm);
2225
2226  /* Add our API messages to the global name_crc hash table */
2227   setup_message_id_table (am, &api_main);
2228
2229   vec_free (name);
2230
2231   acl_setup_fa_nodes();
2232   am->session_timeout_sec[ACL_TIMEOUT_TCP_TRANSIENT] = TCP_SESSION_TRANSIENT_TIMEOUT_SEC;
2233   am->session_timeout_sec[ACL_TIMEOUT_TCP_IDLE] = TCP_SESSION_IDLE_TIMEOUT_SEC;
2234   am->session_timeout_sec[ACL_TIMEOUT_UDP_IDLE] = UDP_SESSION_IDLE_TIMEOUT_SEC;
2235
2236   am->fa_conn_table_hash_num_buckets = ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS;
2237   am->fa_conn_table_hash_memory_size = ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE;
2238   am->fa_conn_table_max_entries = ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES;
2239   vlib_thread_main_t *tm = vlib_get_thread_main ();
2240   vec_validate(am->per_worker_data, tm->n_vlib_mains-1);
2241   {
2242     u16 wk;
2243     u8 tt;
2244     for (wk = 0; wk < vec_len (am->per_worker_data); wk++) {
2245       acl_fa_per_worker_data_t *pw = &am->per_worker_data[wk];
2246       vec_validate(pw->fa_conn_list_head, ACL_N_TIMEOUTS-1);
2247       vec_validate(pw->fa_conn_list_tail, ACL_N_TIMEOUTS-1);
2248       for(tt = 0; tt < ACL_N_TIMEOUTS; tt++) {
2249         pw->fa_conn_list_head[tt] = ~0;
2250         pw->fa_conn_list_tail[tt] = ~0;
2251       }
2252     }
2253   }
2254
2255   am->fa_min_deleted_sessions_per_interval = ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL;
2256   am->fa_max_deleted_sessions_per_interval = ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL;
2257   am->fa_cleaner_wait_time_increment = ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT;
2258
2259   am->fa_cleaner_cnt_delete_by_sw_index = 0;
2260   am->fa_cleaner_cnt_delete_by_sw_index_ok = 0;
2261   am->fa_cleaner_cnt_unknown_event = 0;
2262   am->fa_cleaner_cnt_timer_restarted = 0;
2263   am->fa_cleaner_cnt_wait_with_timeout = 0;
2264
2265
2266 #define _(N, v, s) am->fa_ipv6_known_eh_bitmap = clib_bitmap_set(am->fa_ipv6_known_eh_bitmap, v, 1);
2267   foreach_acl_eh
2268 #undef _
2269
2270   am->l4_match_nonfirst_fragment = 1;
2271
2272   /* use the new fancy hash-based matching */
2273   // NOT IMMEDIATELY
2274   am->use_hash_acl_matching = 1;
2275
2276   return error;
2277 }
2278
2279 VLIB_INIT_FUNCTION (acl_init);