ACL plugin rejects ICMP messages (VPP-624)
[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 = ntohs ( rules[i].srcport_or_icmptype_first );
212       r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
213       r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
214       r->dst_port_or_code_last = ntohs ( 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 = 0;
843   u16 dst_port = 0;
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 = ((u16) (*(u8 *) get_ptr_to_offset (b0, 34)));
870           /* code */
871           dst_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 35)));
872         } else {
873           /* assume TCP/UDP */
874           src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 34)));
875           dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 36)));
876           /* UDP gets ability to check on an oddball data byte as a bonus */
877           tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
878         }
879     }
880   else /* is_ipv6 implicitly */
881     {
882       clib_memcpy (&src, get_ptr_to_offset (b0, 22), 16);
883       clib_memcpy (&dst, get_ptr_to_offset (b0, 38), 16);
884       proto = acl_get_l4_proto (b0, 1);
885       if (58 == proto)
886         {
887           *trace_bitmap |= 0x00000002;
888           /* type */
889           src_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 54));
890           /* code */
891           dst_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 55));
892         }
893       else
894         {
895           /* assume TCP/UDP */
896           src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 54)));
897           dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 56)));
898           tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
899         }
900     }
901   if (pool_is_free_index (am->acls, acl_index))
902     {
903       if (r_acl_match_p)
904         *r_acl_match_p = acl_index;
905       if (r_rule_match_p)
906         *r_rule_match_p = -1;
907       /* the ACL does not exist but is used for policy. Block traffic. */
908       return 0;
909     }
910   a = am->acls + acl_index;
911   for (i = 0; i < a->count; i++)
912     {
913       r = a->rules + i;
914       if (is_ip6 != r->is_ipv6)
915         {
916           continue;
917         }
918       if (!acl_match_addr (&dst, &r->dst, r->dst_prefixlen, is_ip6))
919         continue;
920       if (!acl_match_addr (&src, &r->src, r->src_prefixlen, is_ip6))
921         continue;
922       if (r->proto)
923         {
924           if (proto != r->proto)
925             continue;
926           if (!acl_match_port
927               (src_port, r->src_port_or_type_first, r->src_port_or_type_last,
928                is_ip6))
929             continue;
930           if (!acl_match_port
931               (dst_port, r->dst_port_or_code_first, r->dst_port_or_code_last,
932                is_ip6))
933             continue;
934           /* No need for check of proto == TCP, since in other rules both fields should be zero, so this match will succeed */
935           if ((tcp_flags & r->tcp_flags_mask) != r->tcp_flags_value)
936             continue;
937         }
938       /* everything matches! */
939       *r_action = r->is_permit;
940       *r_is_ip6 = is_ip6;
941       if (r_acl_match_p)
942         *r_acl_match_p = acl_index;
943       if (r_rule_match_p)
944         *r_rule_match_p = i;
945       return 1;
946     }
947   return 0;
948 }
949
950 void
951 input_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
952                         u32 * acl_match_p, u32 * rule_match_p,
953                         u32 * trace_bitmap)
954 {
955   acl_main_t *am = &acl_main;
956   uint8_t action = 0;
957   int is_ip6 = 0;
958   int i;
959   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
960   for (i = 0; i < vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
961        i++)
962     {
963       if (acl_packet_match
964           (am, am->input_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
965            &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
966         {
967           if (is_ip6)
968             {
969               *nextp = am->acl_in_ip6_match_next[action];
970             }
971           else
972             {
973               *nextp = am->acl_in_ip4_match_next[action];
974             }
975           return;
976         }
977     }
978   if (vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]) > 0)
979     {
980       /* If there are ACLs and none matched, deny by default */
981       *nextp = 0;
982     }
983
984 }
985
986 void
987 output_acl_packet_match (u32 sw_if_index, vlib_buffer_t * b0, u32 * nextp,
988                          u32 * acl_match_p, u32 * rule_match_p,
989                          u32 * trace_bitmap)
990 {
991   acl_main_t *am = &acl_main;
992   uint8_t action = 0;
993   int is_ip6 = 0;
994   int i;
995   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
996   for (i = 0; i < vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
997        i++)
998     {
999       if (acl_packet_match
1000           (am, am->output_acl_vec_by_sw_if_index[sw_if_index][i], b0, &action,
1001            &is_ip6, acl_match_p, rule_match_p, trace_bitmap))
1002         {
1003           if (is_ip6)
1004             {
1005               *nextp = am->acl_out_ip6_match_next[action];
1006             }
1007           else
1008             {
1009               *nextp = am->acl_out_ip4_match_next[action];
1010             }
1011           return;
1012         }
1013     }
1014   if (vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]) > 0)
1015     {
1016       /* If there are ACLs and none matched, deny by default */
1017       *nextp = 0;
1018     }
1019 }
1020
1021 typedef struct
1022 {
1023   u8 is_ipv6;
1024   u8 mac_mask[6];
1025   u8 prefix_len;
1026   u32 count;
1027   u32 table_index;
1028   u32 arp_table_index;
1029 } macip_match_type_t;
1030
1031 static u32
1032 macip_find_match_type (macip_match_type_t * mv, u8 * mac_mask, u8 prefix_len,
1033                        u8 is_ipv6)
1034 {
1035   u32 i;
1036   if (mv)
1037     {
1038       for (i = 0; i < vec_len (mv); i++)
1039         {
1040           if ((mv[i].prefix_len == prefix_len) && (mv[i].is_ipv6 == is_ipv6)
1041               && (0 == memcmp (mv[i].mac_mask, mac_mask, 6)))
1042             {
1043               return i;
1044             }
1045         }
1046     }
1047   return ~0;
1048 }
1049
1050
1051 /* Get metric used to sort match types.
1052    The more specific and the more often seen - the bigger the metric */
1053 static int
1054 match_type_metric (macip_match_type_t * m)
1055 {
1056   /* FIXME: count the ones in the MAC mask as well, check how well this heuristic works in real life */
1057   return m->prefix_len + m->is_ipv6 + 10 * m->count;
1058 }
1059
1060 static int
1061 match_type_compare (macip_match_type_t * m1, macip_match_type_t * m2)
1062 {
1063   /* Ascending sort based on the metric values */
1064   return match_type_metric (m1) - match_type_metric (m2);
1065 }
1066
1067 /* Get the offset of L3 source within ethernet packet */
1068 static int
1069 get_l3_src_offset(int is6)
1070 {
1071   if(is6)
1072     return (sizeof(ethernet_header_t) + offsetof(ip6_header_t, src_address));
1073   else
1074     return (sizeof(ethernet_header_t) + offsetof(ip4_header_t, src_address));
1075 }
1076
1077 static int
1078 macip_create_classify_tables (acl_main_t * am, u32 macip_acl_index)
1079 {
1080   macip_match_type_t *mvec = NULL;
1081   macip_match_type_t *mt;
1082   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1083   int i;
1084   u32 match_type_index;
1085   u32 last_table;
1086   u8 mask[5 * 16];
1087   vnet_classify_main_t *cm = &vnet_classify_main;
1088
1089   /* Count the number of different types of rules */
1090   for (i = 0; i < a->count; i++)
1091     {
1092       if (~0 ==
1093           (match_type_index =
1094            macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1095                                   a->rules[i].src_prefixlen,
1096                                   a->rules[i].is_ipv6)))
1097         {
1098           match_type_index = vec_len (mvec);
1099           vec_validate (mvec, match_type_index);
1100           memcpy (mvec[match_type_index].mac_mask,
1101                   a->rules[match_type_index].src_mac_mask, 6);
1102           mvec[match_type_index].prefix_len = a->rules[i].src_prefixlen;
1103           mvec[match_type_index].is_ipv6 = a->rules[i].is_ipv6;
1104           mvec[match_type_index].table_index = ~0;
1105         }
1106       mvec[match_type_index].count++;
1107     }
1108   /* Put the most frequently used tables last in the list so we can create classifier tables in reverse order */
1109   vec_sort_with_function (mvec, match_type_compare);
1110   /* Create the classifier tables */
1111   last_table = ~0;
1112   /* First add ARP tables */
1113   vec_foreach (mt, mvec)
1114   {
1115     int mask_len;
1116     int is6 = mt->is_ipv6;
1117
1118     mt->arp_table_index = ~0;
1119     if (!is6)
1120       {
1121         memset (mask, 0, sizeof (mask));
1122         memcpy (&mask[6], mt->mac_mask, 6);
1123         memset (&mask[12], 0xff, 2); /* ethernet protocol */
1124         memcpy (&mask[14 + 8], mt->mac_mask, 6);
1125
1126         for (i = 0; i < (mt->prefix_len / 8); i++)
1127           mask[14 + 14 + i] = 0xff;
1128         if (mt->prefix_len % 8)
1129           mask[14 + 14 + (mt->prefix_len / 8)] = 0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1130
1131         mask_len = ((14 + 14 + ((mt->prefix_len+7) / 8) +
1132                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1133         acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1134                                (~0 == last_table) ? 0 : ~0, &mt->arp_table_index,
1135                                1);
1136         last_table = mt->arp_table_index;
1137       }
1138   }
1139   /* Now add IP[46] tables */
1140   vec_foreach (mt, mvec)
1141   {
1142     int mask_len;
1143     int is6 = mt->is_ipv6;
1144     int l3_src_offs = get_l3_src_offset(is6);
1145     memset (mask, 0, sizeof (mask));
1146     memcpy (&mask[6], mt->mac_mask, 6);
1147     for (i = 0; i < (mt->prefix_len / 8); i++)
1148       {
1149         mask[l3_src_offs + i] = 0xff;
1150       }
1151     if (mt->prefix_len % 8)
1152       {
1153         mask[l3_src_offs + (mt->prefix_len / 8)] =
1154           0xff - ((1 << (8 - mt->prefix_len % 8)) - 1);
1155       }
1156     /*
1157      * Round-up the number of bytes needed to store the prefix,
1158      * and round up the number of vectors too
1159      */
1160     mask_len = ((l3_src_offs + ((mt->prefix_len+7) / 8) +
1161                 (sizeof (u32x4)-1))/sizeof(u32x4)) * sizeof (u32x4);
1162     acl_classify_add_del_table_small (cm, mask, mask_len, last_table,
1163                                 (~0 == last_table) ? 0 : ~0, &mt->table_index,
1164                                 1);
1165     last_table = mt->table_index;
1166   }
1167   a->ip4_table_index = ~0;
1168   a->ip6_table_index = ~0;
1169   a->l2_table_index = last_table;
1170
1171   /* Populate the classifier tables with rules from the MACIP ACL */
1172   for (i = 0; i < a->count; i++)
1173     {
1174       u32 action = 0;
1175       u32 metadata = 0;
1176       int is6 = a->rules[i].is_ipv6;
1177       int l3_src_offs = get_l3_src_offset(is6);
1178       memset (mask, 0, sizeof (mask));
1179       memcpy (&mask[6], a->rules[i].src_mac, 6);
1180       memset (&mask[12], 0xff, 2); /* ethernet protocol */
1181       if (is6)
1182         {
1183           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip6, 16);
1184           mask[12] = 0x86;
1185           mask[13] = 0xdd;
1186         }
1187       else
1188         {
1189           memcpy (&mask[l3_src_offs], &a->rules[i].src_ip_addr.ip4, 4);
1190           mask[12] = 0x08;
1191           mask[13] = 0x00;
1192         }
1193       match_type_index =
1194         macip_find_match_type (mvec, a->rules[i].src_mac_mask,
1195                                a->rules[i].src_prefixlen,
1196                                a->rules[i].is_ipv6);
1197       /* add session to table mvec[match_type_index].table_index; */
1198       vnet_classify_add_del_session (cm, mvec[match_type_index].table_index,
1199                                      mask, a->rules[i].is_permit ? ~0 : 0, i,
1200                                      0, action, metadata, 1);
1201       /* add ARP table entry too */
1202       if (!is6 && (mvec[match_type_index].arp_table_index != ~0))
1203         {
1204           memset (mask, 0, sizeof (mask));
1205           memcpy (&mask[6], a->rules[i].src_mac, 6);
1206           mask[12] = 0x08;
1207           mask[13] = 0x06;
1208           memcpy (&mask[14 + 8], a->rules[i].src_mac, 6);
1209           memcpy (&mask[14 + 14], &a->rules[i].src_ip_addr.ip4, 4);
1210           vnet_classify_add_del_session (cm, mvec[match_type_index].arp_table_index,
1211                                     mask, a->rules[i].is_permit ? ~0 : 0, i,
1212                                     0, action, metadata, 1);
1213         }
1214     }
1215   return 0;
1216 }
1217
1218 static void
1219 macip_destroy_classify_tables (acl_main_t * am, u32 macip_acl_index)
1220 {
1221   vnet_classify_main_t *cm = &vnet_classify_main;
1222   macip_acl_list_t *a = &am->macip_acls[macip_acl_index];
1223
1224   if (a->ip4_table_index != ~0)
1225     {
1226       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip4_table_index, 0);
1227       a->ip4_table_index = ~0;
1228     }
1229   if (a->ip6_table_index != ~0)
1230     {
1231       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->ip6_table_index, 0);
1232       a->ip6_table_index = ~0;
1233     }
1234   if (a->l2_table_index != ~0)
1235     {
1236       acl_classify_add_del_table_small (cm, 0, ~0, ~0, ~0, &a->l2_table_index, 0);
1237       a->l2_table_index = ~0;
1238     }
1239 }
1240
1241 static int
1242 macip_acl_add_list (u32 count, vl_api_macip_acl_rule_t rules[],
1243                     u32 * acl_list_index, u8 * tag)
1244 {
1245   acl_main_t *am = &acl_main;
1246   macip_acl_list_t *a;
1247   macip_acl_rule_t *r;
1248   macip_acl_rule_t *acl_new_rules;
1249   int i;
1250
1251   /* Create and populate the rules */
1252   acl_new_rules = clib_mem_alloc_aligned (sizeof (macip_acl_rule_t) * count,
1253                                           CLIB_CACHE_LINE_BYTES);
1254   if (!acl_new_rules)
1255     {
1256       /* Could not allocate rules. New or existing ACL - bail out regardless */
1257       return -1;
1258     }
1259
1260   for (i = 0; i < count; i++)
1261     {
1262       r = &acl_new_rules[i];
1263       r->is_permit = rules[i].is_permit;
1264       r->is_ipv6 = rules[i].is_ipv6;
1265       memcpy (&r->src_mac, rules[i].src_mac, 6);
1266       memcpy (&r->src_mac_mask, rules[i].src_mac_mask, 6);
1267       if(rules[i].is_ipv6)
1268         memcpy (&r->src_ip_addr.ip6, rules[i].src_ip_addr, 16);
1269       else
1270         memcpy (&r->src_ip_addr.ip4, rules[i].src_ip_addr, 4);
1271       r->src_prefixlen = rules[i].src_ip_prefix_len;
1272     }
1273
1274   /* Get ACL index */
1275   pool_get_aligned (am->macip_acls, a, CLIB_CACHE_LINE_BYTES);
1276   memset (a, 0, sizeof (*a));
1277   /* Will return the newly allocated ACL index */
1278   *acl_list_index = a - am->macip_acls;
1279
1280   a->rules = acl_new_rules;
1281   a->count = count;
1282   memcpy (a->tag, tag, sizeof (a->tag));
1283
1284   /* Create and populate the classifer tables */
1285   macip_create_classify_tables (am, *acl_list_index);
1286
1287   return 0;
1288 }
1289
1290
1291 /* No check for validity of sw_if_index - the callers were supposed to validate */
1292
1293 static int
1294 macip_acl_interface_del_acl (acl_main_t * am, u32 sw_if_index)
1295 {
1296   int rv;
1297   u32 macip_acl_index;
1298   macip_acl_list_t *a;
1299   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1300   macip_acl_index = am->macip_acl_by_sw_if_index[sw_if_index];
1301   /* No point in deleting MACIP ACL which is not applied */
1302   if (~0 == macip_acl_index)
1303     return -1;
1304   a = &am->macip_acls[macip_acl_index];
1305   /* remove the classifier tables off the interface L2 ACL */
1306   rv =
1307     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1308                               a->ip6_table_index, a->l2_table_index, 0);
1309   /* Unset the MACIP ACL index */
1310   am->macip_acl_by_sw_if_index[sw_if_index] = ~0;
1311   return rv;
1312 }
1313
1314 /* No check for validity of sw_if_index - the callers were supposed to validate */
1315
1316 static int
1317 macip_acl_interface_add_acl (acl_main_t * am, u32 sw_if_index,
1318                              u32 macip_acl_index)
1319 {
1320   macip_acl_list_t *a;
1321   int rv;
1322   if (pool_is_free_index (am->macip_acls, macip_acl_index))
1323     {
1324       return -1;
1325     }
1326   a = &am->macip_acls[macip_acl_index];
1327   vec_validate_init_empty (am->macip_acl_by_sw_if_index, sw_if_index, ~0);
1328   /* If there already a MACIP ACL applied, unapply it */
1329   if (~0 != am->macip_acl_by_sw_if_index[sw_if_index])
1330     macip_acl_interface_del_acl(am, sw_if_index);
1331   am->macip_acl_by_sw_if_index[sw_if_index] = macip_acl_index;
1332   /* Apply the classifier tables for L2 ACLs */
1333   rv =
1334     vnet_set_input_acl_intfc (am->vlib_main, sw_if_index, a->ip4_table_index,
1335                               a->ip6_table_index, a->l2_table_index, 1);
1336   return rv;
1337 }
1338
1339 static int
1340 macip_acl_del_list (u32 acl_list_index)
1341 {
1342   acl_main_t *am = &acl_main;
1343   macip_acl_list_t *a;
1344   int i;
1345   if (pool_is_free_index (am->macip_acls, acl_list_index))
1346     {
1347       return -1;
1348     }
1349
1350   /* delete any references to the ACL */
1351   for (i = 0; i < vec_len (am->macip_acl_by_sw_if_index); i++)
1352     {
1353       if (am->macip_acl_by_sw_if_index[i] == acl_list_index)
1354         {
1355           macip_acl_interface_del_acl (am, i);
1356         }
1357     }
1358
1359   /* Now that classifier tables are detached, clean them up */
1360   macip_destroy_classify_tables (am, acl_list_index);
1361
1362   /* now we can delete the ACL itself */
1363   a = &am->macip_acls[acl_list_index];
1364   if (a->rules)
1365     {
1366       clib_mem_free (a->rules);
1367     }
1368   pool_put (am->macip_acls, a);
1369   return 0;
1370 }
1371
1372
1373 static int
1374 macip_acl_interface_add_del_acl (u32 sw_if_index, u8 is_add,
1375                                  u32 acl_list_index)
1376 {
1377   acl_main_t *am = &acl_main;
1378   int rv = -1;
1379   if (is_add)
1380     {
1381       rv = macip_acl_interface_add_acl (am, sw_if_index, acl_list_index);
1382     }
1383   else
1384     {
1385       rv = macip_acl_interface_del_acl (am, sw_if_index);
1386     }
1387   return rv;
1388 }
1389
1390 /* API message handler */
1391 static void
1392 vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
1393 {
1394   vl_api_acl_add_replace_reply_t *rmp;
1395   acl_main_t *am = &acl_main;
1396   int rv;
1397   u32 acl_list_index = ntohl (mp->acl_index);
1398
1399   rv = acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1400
1401   /* *INDENT-OFF* */
1402   REPLY_MACRO2(VL_API_ACL_ADD_REPLACE_REPLY,
1403   ({
1404     rmp->acl_index = htonl(acl_list_index);
1405   }));
1406   /* *INDENT-ON* */
1407 }
1408
1409 static void
1410 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
1411 {
1412   acl_main_t *sm = &acl_main;
1413   vl_api_acl_del_reply_t *rmp;
1414   int rv;
1415
1416   rv = acl_del_list (ntohl (mp->acl_index));
1417
1418   REPLY_MACRO (VL_API_ACL_DEL_REPLY);
1419 }
1420
1421 static void
1422 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
1423 {
1424   acl_main_t *sm = &acl_main;
1425   vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1426   u32 sw_if_index = ntohl (mp->sw_if_index);
1427   vl_api_acl_interface_add_del_reply_t *rmp;
1428   int rv = -1;
1429
1430   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1431     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1432   else
1433     rv =
1434       acl_interface_add_del_inout_acl (sw_if_index, mp->is_add,
1435                                      mp->is_input, ntohl (mp->acl_index));
1436
1437   REPLY_MACRO (VL_API_ACL_INTERFACE_ADD_DEL_REPLY);
1438 }
1439
1440 static void
1441 vl_api_acl_interface_set_acl_list_t_handler
1442   (vl_api_acl_interface_set_acl_list_t * mp)
1443 {
1444   acl_main_t *sm = &acl_main;
1445   vl_api_acl_interface_set_acl_list_reply_t *rmp;
1446   int rv = 0;
1447   int i;
1448   vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1449   u32 sw_if_index = ntohl (mp->sw_if_index);
1450
1451   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1452     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1453   else
1454     {
1455       acl_interface_reset_inout_acls (sw_if_index, 0);
1456       acl_interface_reset_inout_acls (sw_if_index, 1);
1457
1458       for (i = 0; i < mp->count; i++)
1459         {
1460           acl_interface_add_del_inout_acl (sw_if_index, 1, (i < mp->n_input),
1461                                        ntohl (mp->acls[i]));
1462         }
1463     }
1464
1465   REPLY_MACRO (VL_API_ACL_INTERFACE_SET_ACL_LIST_REPLY);
1466 }
1467
1468 static void
1469 copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
1470 {
1471   api_rule->is_permit = r->is_permit;
1472   api_rule->is_ipv6 = r->is_ipv6;
1473   if(r->is_ipv6)
1474     {
1475       memcpy (api_rule->src_ip_addr, &r->src, sizeof (r->src));
1476       memcpy (api_rule->dst_ip_addr, &r->dst, sizeof (r->dst));
1477     }
1478   else
1479     {
1480       memcpy (api_rule->src_ip_addr, &r->src.ip4, sizeof (r->src.ip4));
1481       memcpy (api_rule->dst_ip_addr, &r->dst.ip4, sizeof (r->dst.ip4));
1482     }
1483   api_rule->src_ip_prefix_len = r->src_prefixlen;
1484   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
1485   api_rule->proto = r->proto;
1486   api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
1487   api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
1488   api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
1489   api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
1490   api_rule->tcp_flags_mask = r->tcp_flags_mask;
1491   api_rule->tcp_flags_value = r->tcp_flags_value;
1492 }
1493
1494 static void
1495 send_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1496                   acl_list_t * acl, u32 context)
1497 {
1498   vl_api_acl_details_t *mp;
1499   vl_api_acl_rule_t *rules;
1500   int i;
1501   int msg_size = sizeof (*mp) + sizeof (mp->r[0]) * acl->count;
1502
1503   mp = vl_msg_api_alloc (msg_size);
1504   memset (mp, 0, msg_size);
1505   mp->_vl_msg_id = ntohs (VL_API_ACL_DETAILS + am->msg_id_base);
1506
1507   /* fill in the message */
1508   mp->context = context;
1509   mp->count = htonl (acl->count);
1510   mp->acl_index = htonl (acl - am->acls);
1511   memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1512   // clib_memcpy (mp->r, acl->rules, acl->count * sizeof(acl->rules[0]));
1513   rules = mp->r;
1514   for (i = 0; i < acl->count; i++)
1515     {
1516       copy_acl_rule_to_api_rule (&rules[i], &acl->rules[i]);
1517     }
1518
1519   clib_warning("Sending acl details for ACL index %d", ntohl(mp->acl_index));
1520   vl_msg_api_send_shmem (q, (u8 *) & mp);
1521 }
1522
1523
1524 static void
1525 vl_api_acl_dump_t_handler (vl_api_acl_dump_t * mp)
1526 {
1527   acl_main_t *am = &acl_main;
1528   u32 acl_index;
1529   acl_list_t *acl;
1530
1531   int rv = -1;
1532   unix_shared_memory_queue_t *q;
1533
1534   q = vl_api_client_index_to_input_queue (mp->client_index);
1535   if (q == 0)
1536     {
1537       return;
1538     }
1539
1540   if (mp->acl_index == ~0)
1541     {
1542     /* *INDENT-OFF* */
1543     /* Just dump all ACLs */
1544     pool_foreach (acl, am->acls,
1545     ({
1546       send_acl_details(am, q, acl, mp->context);
1547     }));
1548     /* *INDENT-ON* */
1549     }
1550   else
1551     {
1552       acl_index = ntohl (mp->acl_index);
1553       if (!pool_is_free_index (am->acls, acl_index))
1554         {
1555           acl = &am->acls[acl_index];
1556           send_acl_details (am, q, acl, mp->context);
1557         }
1558     }
1559
1560   if (rv == -1)
1561     {
1562       /* FIXME API: should we signal an error here at all ? */
1563       return;
1564     }
1565 }
1566
1567 static void
1568 send_acl_interface_list_details (acl_main_t * am,
1569                                  unix_shared_memory_queue_t * q,
1570                                  u32 sw_if_index, u32 context)
1571 {
1572   vl_api_acl_interface_list_details_t *mp;
1573   int msg_size;
1574   int n_input;
1575   int n_output;
1576   int count;
1577   int i = 0;
1578
1579   vec_validate (am->input_acl_vec_by_sw_if_index, sw_if_index);
1580   vec_validate (am->output_acl_vec_by_sw_if_index, sw_if_index);
1581
1582   n_input = vec_len (am->input_acl_vec_by_sw_if_index[sw_if_index]);
1583   n_output = vec_len (am->output_acl_vec_by_sw_if_index[sw_if_index]);
1584   count = n_input + n_output;
1585
1586   msg_size = sizeof (*mp);
1587   msg_size += sizeof (mp->acls[0]) * count;
1588
1589   mp = vl_msg_api_alloc (msg_size);
1590   memset (mp, 0, msg_size);
1591   mp->_vl_msg_id =
1592     ntohs (VL_API_ACL_INTERFACE_LIST_DETAILS + am->msg_id_base);
1593
1594   /* fill in the message */
1595   mp->context = context;
1596   mp->sw_if_index = htonl (sw_if_index);
1597   mp->count = count;
1598   mp->n_input = n_input;
1599   for (i = 0; i < n_input; i++)
1600     {
1601       mp->acls[i] = htonl (am->input_acl_vec_by_sw_if_index[sw_if_index][i]);
1602     }
1603   for (i = 0; i < n_output; i++)
1604     {
1605       mp->acls[n_input + i] =
1606         htonl (am->output_acl_vec_by_sw_if_index[sw_if_index][i]);
1607     }
1608
1609   vl_msg_api_send_shmem (q, (u8 *) & mp);
1610 }
1611
1612 static void
1613 vl_api_acl_interface_list_dump_t_handler (vl_api_acl_interface_list_dump_t *
1614                                           mp)
1615 {
1616   acl_main_t *am = &acl_main;
1617   vnet_sw_interface_t *swif;
1618   vnet_interface_main_t *im = &am->vnet_main->interface_main;
1619
1620   u32 sw_if_index;
1621   unix_shared_memory_queue_t *q;
1622
1623   q = vl_api_client_index_to_input_queue (mp->client_index);
1624   if (q == 0)
1625     {
1626       return;
1627     }
1628
1629   if (mp->sw_if_index == ~0)
1630     {
1631     /* *INDENT-OFF* */
1632     pool_foreach (swif, im->sw_interfaces,
1633     ({
1634       send_acl_interface_list_details(am, q, swif->sw_if_index, mp->context);
1635     }));
1636     /* *INDENT-ON* */
1637     }
1638   else
1639     {
1640       sw_if_index = ntohl (mp->sw_if_index);
1641       if (!pool_is_free_index(im->sw_interfaces, sw_if_index))
1642         send_acl_interface_list_details (am, q, sw_if_index, mp->context);
1643     }
1644 }
1645
1646 /* MACIP ACL API handlers */
1647
1648 static void
1649 vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
1650 {
1651   vl_api_macip_acl_add_reply_t *rmp;
1652   acl_main_t *am = &acl_main;
1653   int rv;
1654   u32 acl_list_index = ~0;
1655
1656   rv =
1657     macip_acl_add_list (ntohl (mp->count), mp->r, &acl_list_index, mp->tag);
1658
1659   /* *INDENT-OFF* */
1660   REPLY_MACRO2(VL_API_MACIP_ACL_ADD_REPLY,
1661   ({
1662     rmp->acl_index = htonl(acl_list_index);
1663   }));
1664   /* *INDENT-ON* */
1665 }
1666
1667 static void
1668 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
1669 {
1670   acl_main_t *sm = &acl_main;
1671   vl_api_macip_acl_del_reply_t *rmp;
1672   int rv;
1673
1674   rv = macip_acl_del_list (ntohl (mp->acl_index));
1675
1676   REPLY_MACRO (VL_API_MACIP_ACL_DEL_REPLY);
1677 }
1678
1679 static void
1680 vl_api_macip_acl_interface_add_del_t_handler
1681   (vl_api_macip_acl_interface_add_del_t * mp)
1682 {
1683   acl_main_t *sm = &acl_main;
1684   vl_api_macip_acl_interface_add_del_reply_t *rmp;
1685   int rv = -1;
1686   vnet_interface_main_t *im = &sm->vnet_main->interface_main;
1687   u32 sw_if_index = ntohl (mp->sw_if_index);
1688
1689   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
1690     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1691   else
1692     rv =
1693       macip_acl_interface_add_del_acl (ntohl (mp->sw_if_index), mp->is_add,
1694                                      ntohl (mp->acl_index));
1695
1696   REPLY_MACRO (VL_API_MACIP_ACL_INTERFACE_ADD_DEL_REPLY);
1697 }
1698
1699 static void
1700 send_macip_acl_details (acl_main_t * am, unix_shared_memory_queue_t * q,
1701                         macip_acl_list_t * acl, u32 context)
1702 {
1703   vl_api_macip_acl_details_t *mp;
1704   vl_api_macip_acl_rule_t *rules;
1705   macip_acl_rule_t *r;
1706   int i;
1707   int msg_size = sizeof (*mp) + (acl ? sizeof (mp->r[0]) * acl->count : 0);
1708
1709   mp = vl_msg_api_alloc (msg_size);
1710   memset (mp, 0, msg_size);
1711   mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_DETAILS + am->msg_id_base);
1712
1713   /* fill in the message */
1714   mp->context = context;
1715   if (acl)
1716     {
1717       memcpy (mp->tag, acl->tag, sizeof (mp->tag));
1718       mp->count = htonl (acl->count);
1719       mp->acl_index = htonl (acl - am->macip_acls);
1720       rules = mp->r;
1721       for (i = 0; i < acl->count; i++)
1722         {
1723           r = &acl->rules[i];
1724           rules[i].is_permit = r->is_permit;
1725           rules[i].is_ipv6 = r->is_ipv6;
1726           memcpy (rules[i].src_mac, &r->src_mac, sizeof (r->src_mac));
1727           memcpy (rules[i].src_mac_mask, &r->src_mac_mask,
1728                   sizeof (r->src_mac_mask));
1729           if (r->is_ipv6)
1730             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip6,
1731                   sizeof (r->src_ip_addr.ip6));
1732           else
1733             memcpy (rules[i].src_ip_addr, &r->src_ip_addr.ip4,
1734                   sizeof (r->src_ip_addr.ip4));
1735           rules[i].src_ip_prefix_len = r->src_prefixlen;
1736         }
1737     }
1738   else
1739     {
1740       /* No martini, no party - no ACL applied to this interface. */
1741       mp->acl_index = ~0;
1742       mp->count = 0;
1743     }
1744
1745   vl_msg_api_send_shmem (q, (u8 *) & mp);
1746 }
1747
1748
1749 static void
1750 vl_api_macip_acl_dump_t_handler (vl_api_macip_acl_dump_t * mp)
1751 {
1752   acl_main_t *am = &acl_main;
1753   macip_acl_list_t *acl;
1754
1755   unix_shared_memory_queue_t *q;
1756
1757   q = vl_api_client_index_to_input_queue (mp->client_index);
1758   if (q == 0)
1759     {
1760       return;
1761     }
1762
1763   if (mp->acl_index == ~0)
1764     {
1765       /* Just dump all ACLs for now, with sw_if_index = ~0 */
1766       pool_foreach (acl, am->macip_acls, (
1767                                            {
1768                                            send_macip_acl_details (am, q, acl,
1769                                                                    mp->
1770                                                                    context);}
1771                     ));
1772       /* *INDENT-ON* */
1773     }
1774   else
1775     {
1776       u32 acl_index = ntohl (mp->acl_index);
1777       if (!pool_is_free_index (am->macip_acls, acl_index))
1778         {
1779           acl = &am->macip_acls[acl_index];
1780           send_macip_acl_details (am, q, acl, mp->context);
1781         }
1782     }
1783 }
1784
1785 static void
1786 vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
1787                                           mp)
1788 {
1789   acl_main_t *am = &acl_main;
1790   vl_api_macip_acl_interface_get_reply_t *rmp;
1791   u32 count = vec_len (am->macip_acl_by_sw_if_index);
1792   int msg_size = sizeof (*rmp) + sizeof (rmp->acls[0]) * count;
1793   unix_shared_memory_queue_t *q;
1794   int i;
1795
1796   q = vl_api_client_index_to_input_queue (mp->client_index);
1797   if (q == 0)
1798     {
1799       return;
1800     }
1801
1802   rmp = vl_msg_api_alloc (msg_size);
1803   memset (rmp, 0, msg_size);
1804   rmp->_vl_msg_id =
1805     ntohs (VL_API_MACIP_ACL_INTERFACE_GET_REPLY + am->msg_id_base);
1806   rmp->context = mp->context;
1807   rmp->count = htonl (count);
1808   for (i = 0; i < count; i++)
1809     {
1810       rmp->acls[i] = htonl (am->macip_acl_by_sw_if_index[i]);
1811     }
1812
1813   vl_msg_api_send_shmem (q, (u8 *) & rmp);
1814 }
1815
1816
1817
1818 /* Set up the API message handling tables */
1819 static clib_error_t *
1820 acl_plugin_api_hookup (vlib_main_t * vm)
1821 {
1822   acl_main_t *sm = &acl_main;
1823 #define _(N,n)                                                  \
1824     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
1825                            #n,                                  \
1826                            vl_api_##n##_t_handler,              \
1827                            vl_noop_handler,                     \
1828                            vl_api_##n##_t_endian,               \
1829                            vl_api_##n##_t_print,                \
1830                            sizeof(vl_api_##n##_t), 1);
1831   foreach_acl_plugin_api_msg;
1832 #undef _
1833
1834   return 0;
1835 }
1836
1837 #define vl_msg_name_crc_list
1838 #include <acl/acl_all_api_h.h>
1839 #undef vl_msg_name_crc_list
1840
1841 static void
1842 setup_message_id_table (acl_main_t * sm, api_main_t * am)
1843 {
1844 #define _(id,n,crc) \
1845   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1846   foreach_vl_msg_name_crc_acl;
1847 #undef _
1848 }
1849
1850 u32
1851 register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
1852                              u32 next_out_ip4, u32 next_out_ip6)
1853 {
1854   acl_main_t *am = &acl_main;
1855   u32 act = am->n_match_actions;
1856   if (am->n_match_actions == 255)
1857     {
1858       return ~0;
1859     }
1860   am->n_match_actions++;
1861   am->acl_in_ip4_match_next[act] = next_in_ip4;
1862   am->acl_in_ip6_match_next[act] = next_in_ip6;
1863   am->acl_out_ip4_match_next[act] = next_out_ip4;
1864   am->acl_out_ip6_match_next[act] = next_out_ip6;
1865   return act;
1866 }
1867
1868 void
1869 acl_setup_nodes (void)
1870 {
1871   vlib_main_t *vm = vlib_get_main ();
1872   acl_main_t *am = &acl_main;
1873   vlib_node_t *n;
1874
1875   n = vlib_get_node_by_name (vm, (u8 *) "l2-input-classify");
1876   am->l2_input_classify_next_acl =
1877     vlib_node_add_next_with_slot (vm, n->index, acl_in_node.index, ~0);
1878   n = vlib_get_node_by_name (vm, (u8 *) "l2-output-classify");
1879   am->l2_output_classify_next_acl =
1880     vlib_node_add_next_with_slot (vm, n->index, acl_out_node.index, ~0);
1881
1882   feat_bitmap_init_next_nodes (vm, acl_in_node.index, L2INPUT_N_FEAT,
1883                                l2input_get_feat_names (),
1884                                am->acl_in_node_feat_next_node_index);
1885
1886   feat_bitmap_init_next_nodes (vm, acl_out_node.index, L2OUTPUT_N_FEAT,
1887                                l2output_get_feat_names (),
1888                                am->acl_out_node_feat_next_node_index);
1889
1890   memset (&am->acl_in_ip4_match_next[0], 0,
1891           sizeof (am->acl_in_ip4_match_next));
1892   memset (&am->acl_in_ip6_match_next[0], 0,
1893           sizeof (am->acl_in_ip6_match_next));
1894   memset (&am->acl_out_ip4_match_next[0], 0,
1895           sizeof (am->acl_out_ip4_match_next));
1896   memset (&am->acl_out_ip6_match_next[0], 0,
1897           sizeof (am->acl_out_ip6_match_next));
1898   am->n_match_actions = 0;
1899
1900   register_match_action_nexts (0, 0, 0, 0);     /* drop */
1901   register_match_action_nexts (~0, ~0, ~0, ~0); /* permit */
1902   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 */
1903 }
1904
1905
1906
1907 static clib_error_t *
1908 acl_init (vlib_main_t * vm)
1909 {
1910   acl_main_t *am = &acl_main;
1911   clib_error_t *error = 0;
1912   memset (am, 0, sizeof (*am));
1913   am->vlib_main = vm;
1914   am->vnet_main = vnet_get_main ();
1915
1916   u8 *name = format (0, "acl_%08x%c", api_version, 0);
1917
1918   /* Ask for a correctly-sized block of API message decode slots */
1919   am->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
1920                                             VL_MSG_FIRST_AVAILABLE);
1921
1922   error = acl_plugin_api_hookup (vm);
1923   acl_setup_nodes ();
1924
1925  /* Add our API messages to the global name_crc hash table */
1926   setup_message_id_table (am, &api_main);
1927
1928   vec_free (name);
1929
1930   return error;
1931 }
1932
1933 VLIB_INIT_FUNCTION (acl_init);