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