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