acl-plugin: VPP-1230: fix the "undefined symbol" error for acl_main when using the...
[vpp.git] / src / plugins / acl / public_inlines.h
1 /*
2  * Copyright (c) 2018 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 #ifndef included_acl_inlines_h
17 #define included_acl_inlines_h
18
19 #include <stdint.h>
20
21 #include <plugins/acl/acl.h>
22 #include <plugins/acl/fa_node.h>
23 #include <plugins/acl/hash_lookup_private.h>
24
25
26 /* check if a given ACL exists */
27
28 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
29
30 /*
31  * Define a pointer to the acl_main which will be filled during the initialization.
32  */
33 acl_main_t *p_acl_main = 0;
34
35 /*
36  * If the file is included more than once, the symbol collision will make the problem obvious.
37  * If the include is done only once, it is just a lonely null var
38  * sitting around.
39  */
40 void *ERROR_ACL_PLUGIN_EXPORTS_FILE_MUST_BE_INCLUDED_ONLY_IN_ONE_PLACE = 0;
41
42 u8 (*acl_plugin_acl_exists) (u32 acl_index);
43 #else
44 u8 acl_plugin_acl_exists (u32 acl_index);
45 #endif
46
47
48 /*
49  * If you are using ACL plugin, get this unique ID first,
50  * so you can identify yourself when creating the lookup contexts.
51  */
52
53 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
54 u32 (*acl_plugin_register_user_module) (char *caller_module_string, char *val1_label, char *val2_label);
55 #else
56 u32 acl_plugin_register_user_module (char *caller_module_string, char *val1_label, char *val2_label);
57 #endif
58
59 /*
60  * Allocate a new lookup context index.
61  * Supply the id assigned to your module during registration,
62  * and two values of your choice identifying instances
63  * of use within your module. They are useful for debugging.
64  */
65 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
66 int (*acl_plugin_get_lookup_context_index) (u32 acl_user_id, u32 val1, u32 val2);
67 #else
68 int acl_plugin_get_lookup_context_index (u32 acl_user_id, u32 val1, u32 val2);
69 #endif
70
71 /*
72  * Release the lookup context index and destroy
73  * any asssociated data structures.
74  */
75 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
76 void (*acl_plugin_put_lookup_context_index) (u32 lc_index);
77 #else
78 void acl_plugin_put_lookup_context_index (u32 lc_index);
79 #endif
80
81 /*
82  * Prepare the sequential vector of ACL#s to lookup within a given context.
83  * Any existing list will be overwritten. acl_list is a vector.
84  */
85 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
86 int (*acl_plugin_set_acl_vec_for_context) (u32 lc_index, u32 *acl_list);
87 #else
88 int acl_plugin_set_acl_vec_for_context (u32 lc_index, u32 *acl_list);
89 #endif
90
91 /* Fill the 5-tuple from the packet */
92
93 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
94 void (*acl_plugin_fill_5tuple) (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
95                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
96 #else
97 void acl_plugin_fill_5tuple (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
98                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt);
99 #endif
100
101 #ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
102 static inline
103 void acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6, int is_input,
104                                 int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt) {
105   /* FIXME: normally the inlined version of filling in the 5-tuple. But for now just call the non-inlined version */
106   acl_plugin_fill_5tuple(lc_index, b0, is_ip6, is_input, is_l2_path, p5tuple_pkt);
107 }
108 #endif
109
110
111 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
112 int (*acl_plugin_match_5tuple) (u32 lc_index,
113                                            fa_5tuple_opaque_t * pkt_5tuple,
114                                            int is_ip6, u8 * r_action,
115                                            u32 * r_acl_pos_p,
116                                            u32 * r_acl_match_p,
117                                            u32 * r_rule_match_p,
118                                            u32 * trace_bitmap);
119 #else
120 int acl_plugin_match_5tuple (u32 lc_index,
121                                            fa_5tuple_opaque_t * pkt_5tuple,
122                                            int is_ip6, u8 * r_action,
123                                            u32 * r_acl_pos_p,
124                                            u32 * r_acl_match_p,
125                                            u32 * r_rule_match_p,
126                                            u32 * trace_bitmap);
127 #endif
128
129 #ifdef ACL_PLUGIN_DEFINED_BELOW_IN_FILE
130 static inline int
131 acl_plugin_match_5tuple_inline (u32 lc_index,
132                                            fa_5tuple_opaque_t * pkt_5tuple,
133                                            int is_ip6, u8 * r_action,
134                                            u32 * r_acl_pos_p,
135                                            u32 * r_acl_match_p,
136                                            u32 * r_rule_match_p,
137                                            u32 * trace_bitmap) {
138   return acl_plugin_match_5tuple(lc_index, pkt_5tuple, is_ip6, r_action, r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
139 }
140 #endif
141
142 #ifdef ACL_PLUGIN_EXTERNAL_EXPORTS
143
144 #define LOAD_SYMBOL_FROM_PLUGIN_TO(p, s, st)                              \
145 ({                                                                        \
146     st = vlib_get_plugin_symbol(p, #s);                                    \
147     if (!st)                                                               \
148         return clib_error_return(0,                                       \
149                 "Plugin %s and/or symbol %s not found.", p, #s);          \
150 })
151
152 #define LOAD_SYMBOL(s) LOAD_SYMBOL_FROM_PLUGIN_TO("acl_plugin.so", s, s)
153
154 static inline clib_error_t * acl_plugin_exports_init (void)
155 {
156     LOAD_SYMBOL_FROM_PLUGIN_TO("acl_plugin.so", acl_main, p_acl_main);
157     LOAD_SYMBOL(acl_plugin_acl_exists);
158     LOAD_SYMBOL(acl_plugin_register_user_module);
159     LOAD_SYMBOL(acl_plugin_get_lookup_context_index);
160     LOAD_SYMBOL(acl_plugin_put_lookup_context_index);
161     LOAD_SYMBOL(acl_plugin_set_acl_vec_for_context);
162     LOAD_SYMBOL(acl_plugin_fill_5tuple);
163     LOAD_SYMBOL(acl_plugin_match_5tuple);
164     return 0;
165 }
166
167 #endif
168
169
170
171 always_inline void *
172 get_ptr_to_offset (vlib_buffer_t * b0, int offset)
173 {
174   u8 *p = vlib_buffer_get_current (b0) + offset;
175   return p;
176 }
177
178 always_inline int
179 offset_within_packet (vlib_buffer_t * b0, int offset)
180 {
181   /* For the purposes of this code, "within" means we have at least 8 bytes after it */
182   return (offset <= (b0->current_length - 8));
183 }
184
185 always_inline void
186 acl_fill_5tuple (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
187                  int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
188 {
189   /* IP4 and IP6 protocol numbers of ICMP */
190   static u8 icmp_protos_v4v6[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
191
192   int l3_offset;
193   int l4_offset;
194   u16 ports[2];
195   u16 proto;
196
197   if (is_l2_path)
198     {
199       l3_offset = ethernet_buffer_header_size(b0);
200     }
201   else
202     {
203       if (is_input)
204         l3_offset = 0;
205       else
206         l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
207     }
208
209   /* key[0..3] contains src/dst address and is cleared/set below */
210   /* Remainder of the key and per-packet non-key data */
211   p5tuple_pkt->kv.key[4] = 0;
212   p5tuple_pkt->kv.value = 0;
213
214   if (is_ip6)
215     {
216       clib_memcpy (&p5tuple_pkt->addr,
217                    get_ptr_to_offset (b0,
218                                       offsetof (ip6_header_t,
219                                                 src_address) + l3_offset),
220                    sizeof (p5tuple_pkt->addr));
221       proto =
222         *(u8 *) get_ptr_to_offset (b0,
223                                    offsetof (ip6_header_t,
224                                              protocol) + l3_offset);
225       l4_offset = l3_offset + sizeof (ip6_header_t);
226 #ifdef FA_NODE_VERBOSE_DEBUG
227       clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
228                     l4_offset);
229 #endif
230       /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
231       int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
232       if (PREDICT_FALSE (need_skip_eh))
233         {
234           while (need_skip_eh && offset_within_packet (b0, l4_offset))
235             {
236               /* Fragment header needs special handling */
237               if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
238                 {
239                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
240                   u16 frag_offset;
241                   clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
242                   frag_offset = clib_net_to_host_u16(frag_offset) >> 3;
243                   if (frag_offset)
244                     {
245                       p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
246                       /* invalidate L4 offset so we don't try to find L4 info */
247                       l4_offset += b0->current_length;
248                     }
249                   else
250                     {
251                       /* First fragment: skip the frag header and move on. */
252                       l4_offset += 8;
253                     }
254                 }
255               else
256                 {
257                   u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
258                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
259                   l4_offset += 8 * (1 + (u16) nwords);
260                 }
261 #ifdef FA_NODE_VERBOSE_DEBUG
262               clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
263                             proto, l4_offset);
264 #endif
265               need_skip_eh =
266                 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
267             }
268         }
269     }
270   else
271     {
272       p5tuple_pkt->kv.key[0] = 0;
273       p5tuple_pkt->kv.key[1] = 0;
274       p5tuple_pkt->kv.key[2] = 0;
275       p5tuple_pkt->kv.key[3] = 0;
276       clib_memcpy (&p5tuple_pkt->addr[0].ip4,
277                    get_ptr_to_offset (b0,
278                                       offsetof (ip4_header_t,
279                                                 src_address) + l3_offset),
280                    sizeof (p5tuple_pkt->addr[0].ip4));
281       clib_memcpy (&p5tuple_pkt->addr[1].ip4,
282                    get_ptr_to_offset (b0,
283                                       offsetof (ip4_header_t,
284                                                 dst_address) + l3_offset),
285                    sizeof (p5tuple_pkt->addr[1].ip4));
286       proto =
287         *(u8 *) get_ptr_to_offset (b0,
288                                    offsetof (ip4_header_t,
289                                              protocol) + l3_offset);
290       l4_offset = l3_offset + sizeof (ip4_header_t);
291       u16 flags_and_fragment_offset;
292       clib_memcpy (&flags_and_fragment_offset,
293                    get_ptr_to_offset (b0,
294                                       offsetof (ip4_header_t,
295                                                 flags_and_fragment_offset)) + l3_offset,
296                                                 sizeof(flags_and_fragment_offset));
297       flags_and_fragment_offset = clib_net_to_host_u16 (flags_and_fragment_offset);
298
299       /* non-initial fragments have non-zero offset */
300       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
301         {
302           p5tuple_pkt->pkt.is_nonfirst_fragment = 1;
303           /* invalidate L4 offset so we don't try to find L4 info */
304           l4_offset += b0->current_length;
305         }
306
307     }
308   p5tuple_pkt->l4.proto = proto;
309   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
310     {
311       p5tuple_pkt->pkt.l4_valid = 1;
312       if (icmp_protos_v4v6[is_ip6] == proto)
313         {
314           /* type */
315           p5tuple_pkt->l4.port[0] =
316             *(u8 *) get_ptr_to_offset (b0,
317                                        l4_offset + offsetof (icmp46_header_t,
318                                                              type));
319           /* code */
320           p5tuple_pkt->l4.port[1] =
321             *(u8 *) get_ptr_to_offset (b0,
322                                        l4_offset + offsetof (icmp46_header_t,
323                                                              code));
324         }
325       else if ((IP_PROTOCOL_TCP == proto) || (IP_PROTOCOL_UDP == proto))
326         {
327           clib_memcpy (&ports,
328                        get_ptr_to_offset (b0,
329                                           l4_offset + offsetof (tcp_header_t,
330                                                                 src_port)),
331                        sizeof (ports));
332           p5tuple_pkt->l4.port[0] = clib_net_to_host_u16 (ports[0]);
333           p5tuple_pkt->l4.port[1] = clib_net_to_host_u16 (ports[1]);
334
335           p5tuple_pkt->pkt.tcp_flags =
336             *(u8 *) get_ptr_to_offset (b0,
337                                        l4_offset + offsetof (tcp_header_t,
338                                                              flags));
339           p5tuple_pkt->pkt.tcp_flags_valid = (proto == IP_PROTOCOL_TCP);
340         }
341       /*
342        * FIXME: rather than the above conditional, here could
343        * be a nice generic mechanism to extract two L4 values:
344        *
345        * have a per-protocol array of 4 elements like this:
346        *   u8 offset; to take the byte from, off L4 header
347        *   u8 mask; to mask it with, before storing
348        *
349        * this way we can describe UDP, TCP and ICMP[46] semantics,
350        * and add a sort of FPM-type behavior for other protocols.
351        *
352        * Of course, is it faster ? and is it needed ?
353        *
354        */
355     }
356 }
357
358 always_inline void
359 acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6,
360                  int is_input, int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt)
361 {
362   acl_main_t *am = p_acl_main;
363   acl_fill_5tuple(am, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_t *)p5tuple_pkt);
364 }
365
366
367
368 always_inline int
369 fa_acl_match_addr (ip46_address_t * addr1, ip46_address_t * addr2,
370                    int prefixlen, int is_ip6)
371 {
372   if (prefixlen == 0)
373     {
374       /* match any always succeeds */
375       return 1;
376     }
377   if (is_ip6)
378     {
379       if (memcmp (addr1, addr2, prefixlen / 8))
380         {
381           /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
382           return 0;
383         }
384       if (prefixlen % 8)
385         {
386           u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
387           u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
388           u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
389           return (b1 & mask0) == b2;
390         }
391       else
392         {
393           /* The prefix fits into integer number of bytes, so nothing left to do */
394           return 1;
395         }
396     }
397   else
398     {
399       uint32_t a1 = clib_net_to_host_u32 (addr1->ip4.as_u32);
400       uint32_t a2 = clib_net_to_host_u32 (addr2->ip4.as_u32);
401       uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
402       return (a1 & mask0) == a2;
403     }
404 }
405
406 always_inline int
407 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
408 {
409   return ((port >= port_first) && (port <= port_last));
410 }
411
412 always_inline int
413 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
414                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
415                   u32 * r_rule_match_p, u32 * trace_bitmap)
416 {
417   int i;
418   acl_list_t *a;
419   acl_rule_t *r;
420
421   if (pool_is_free_index (am->acls, acl_index))
422     {
423       if (r_acl_match_p)
424         *r_acl_match_p = acl_index;
425       if (r_rule_match_p)
426         *r_rule_match_p = -1;
427       /* the ACL does not exist but is used for policy. Block traffic. */
428       return 0;
429     }
430   a = am->acls + acl_index;
431   for (i = 0; i < a->count; i++)
432     {
433       r = a->rules + i;
434 #ifdef FA_NODE_VERBOSE_DEBUG
435       clib_warning("ACL_FA_NODE_DBG acl %d rule %d tag %s", acl_index, i, a->tag);
436 #endif
437       if (is_ip6 != r->is_ipv6)
438         {
439           continue;
440         }
441       if (!fa_acl_match_addr
442           (&pkt_5tuple->addr[1], &r->dst, r->dst_prefixlen, is_ip6))
443         continue;
444
445 #ifdef FA_NODE_VERBOSE_DEBUG
446       clib_warning
447         ("ACL_FA_NODE_DBG acl %d rule %d pkt dst addr %U match rule addr %U/%d",
448          acl_index, i, format_ip46_address, &pkt_5tuple->addr[1],
449          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
450          &r->dst, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
451          r->dst_prefixlen);
452 #endif
453
454       if (!fa_acl_match_addr
455           (&pkt_5tuple->addr[0], &r->src, r->src_prefixlen, is_ip6))
456         continue;
457
458 #ifdef FA_NODE_VERBOSE_DEBUG
459       clib_warning
460         ("ACL_FA_NODE_DBG acl %d rule %d pkt src addr %U match rule addr %U/%d",
461          acl_index, i, format_ip46_address, &pkt_5tuple->addr[0],
462          r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4, format_ip46_address,
463          &r->src, r->is_ipv6 ? IP46_TYPE_IP6: IP46_TYPE_IP4,
464          r->src_prefixlen);
465       clib_warning
466         ("ACL_FA_NODE_DBG acl %d rule %d trying to match pkt proto %d with rule %d",
467          acl_index, i, pkt_5tuple->l4.proto, r->proto);
468 #endif
469       if (r->proto)
470         {
471           if (pkt_5tuple->l4.proto != r->proto)
472             continue;
473
474           if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
475                      am->l4_match_nonfirst_fragment))
476           {
477             /* non-initial fragment with frag match configured - match this rule */
478             *trace_bitmap |= 0x80000000;
479             *r_action = r->is_permit;
480             if (r_acl_match_p)
481               *r_acl_match_p = acl_index;
482             if (r_rule_match_p)
483               *r_rule_match_p = i;
484             return 1;
485           }
486
487           /* A sanity check just to ensure we are about to match the ports extracted from the packet */
488           if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
489             continue;
490
491 #ifdef FA_NODE_VERBOSE_DEBUG
492           clib_warning
493             ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
494              acl_index, i, pkt_5tuple->l4.proto, r->proto);
495 #endif
496
497           if (!fa_acl_match_port
498               (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
499                r->src_port_or_type_last, is_ip6))
500             continue;
501
502 #ifdef FA_NODE_VERBOSE_DEBUG
503           clib_warning
504             ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
505              acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
506              r->src_port_or_type_last);
507 #endif
508
509           if (!fa_acl_match_port
510               (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
511                r->dst_port_or_code_last, is_ip6))
512             continue;
513
514 #ifdef FA_NODE_VERBOSE_DEBUG
515           clib_warning
516             ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
517              acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
518              r->dst_port_or_code_last);
519 #endif
520           if (pkt_5tuple->pkt.tcp_flags_valid
521               && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
522                   r->tcp_flags_value))
523             continue;
524         }
525       /* everything matches! */
526 #ifdef FA_NODE_VERBOSE_DEBUG
527       clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
528                     acl_index, i, r->is_permit);
529 #endif
530       *r_action = r->is_permit;
531       if (r_acl_match_p)
532         *r_acl_match_p = acl_index;
533       if (r_rule_match_p)
534         *r_rule_match_p = i;
535       return 1;
536     }
537   return 0;
538 }
539
540 always_inline int
541 acl_plugin_single_acl_match_5tuple (u32 acl_index, fa_5tuple_t * pkt_5tuple,
542                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
543                   u32 * r_rule_match_p, u32 * trace_bitmap)
544 {
545   acl_main_t * am = p_acl_main;
546   return single_acl_match_5tuple(am, acl_index, pkt_5tuple, is_ip6, r_action,
547                                  r_acl_match_p, r_rule_match_p, trace_bitmap);
548 }
549
550 always_inline int
551 linear_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple, 
552                        int is_ip6, u8 *r_action, u32 *acl_pos_p, u32 * acl_match_p,
553                        u32 * rule_match_p, u32 * trace_bitmap)
554 {
555   acl_main_t *am = p_acl_main;
556   int i;
557   u32 *acl_vector;
558   u8 action = 0;
559   acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
560
561   acl_vector = acontext->acl_indices;
562
563   for (i = 0; i < vec_len (acl_vector); i++)
564     {
565 #ifdef FA_NODE_VERBOSE_DEBUG
566       clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
567                     acl_vector[i]);
568 #endif
569       if (single_acl_match_5tuple
570           (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
571            acl_match_p, rule_match_p, trace_bitmap))
572         {
573           *r_action = action;
574           *acl_pos_p = i;
575           return 1;
576         }
577     }
578   if (vec_len (acl_vector) > 0)
579     {
580       return 0;
581     }
582 #ifdef FA_NODE_VERBOSE_DEBUG
583   clib_warning ("ACL_FA_NODE_DBG: No ACL on lc_index %d", lc_index);
584 #endif
585   /* If there are no ACLs defined we should not be here. */
586   return 0;
587 }
588
589
590
591 /*
592  * This returns true if there is indeed a match on the portranges.
593  * With all these levels of indirections, this is not going to be very fast,
594  * so, best use the individual ports or wildcard ports for performance.
595  */
596 always_inline int
597 match_portranges(acl_main_t *am, fa_5tuple_t *match, u32 index)
598 {
599
600   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
601   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), index);
602
603   acl_rule_t *r = &(am->acls[pae->acl_index].rules[pae->ace_index]);
604
605 #ifdef FA_NODE_VERBOSE_DEBUG
606   clib_warning("PORTMATCH: %d <= %d <= %d && %d <= %d <= %d ?",
607                 r->src_port_or_type_first, match->l4.port[0], r->src_port_or_type_last,
608                 r->dst_port_or_code_first, match->l4.port[1], r->dst_port_or_code_last);
609 #endif
610
611   return ( ((r->src_port_or_type_first <= match->l4.port[0]) && r->src_port_or_type_last >= match->l4.port[0]) &&
612            ((r->dst_port_or_code_first <= match->l4.port[1]) && r->dst_port_or_code_last >= match->l4.port[1]) );
613 }
614
615 always_inline u32
616 multi_acl_match_get_applied_ace_index(acl_main_t *am, fa_5tuple_t *match)
617 {
618   clib_bihash_kv_48_8_t kv;
619   clib_bihash_kv_48_8_t result;
620   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv.key;
621   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
622   u64 *pmatch = (u64 *)match;
623   u64 *pmask;
624   u64 *pkey;
625   int mask_type_index;
626   u32 curr_match_index = ~0;
627
628   u32 lc_index = match->pkt.lc_index;
629   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
630   applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
631
632   DBG("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
633                pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
634
635   for(mask_type_index=0; mask_type_index < pool_len(am->ace_mask_type_pool); mask_type_index++) {
636     if (!clib_bitmap_get(vec_elt_at_index((*applied_hash_acls), lc_index)->mask_type_index_bitmap, mask_type_index)) {
637       /* This bit is not set. Avoid trying to match */
638       continue;
639     }
640     ace_mask_type_entry_t *mte = vec_elt_at_index(am->ace_mask_type_pool, mask_type_index);
641     pmatch = (u64 *)match;
642     pmask = (u64 *)&mte->mask;
643     pkey = (u64 *)kv.key;
644     /*
645     * unrolling the below loop results in a noticeable performance increase.
646     int i;
647     for(i=0; i<6; i++) {
648       kv.key[i] = pmatch[i] & pmask[i];
649     }
650     */
651
652     *pkey++ = *pmatch++ & *pmask++;
653     *pkey++ = *pmatch++ & *pmask++;
654     *pkey++ = *pmatch++ & *pmask++;
655     *pkey++ = *pmatch++ & *pmask++;
656     *pkey++ = *pmatch++ & *pmask++;
657     *pkey++ = *pmatch++ & *pmask++;
658
659     kv_key->pkt.mask_type_index_lsb = mask_type_index;
660     DBG("        KEY %3d: %016llx %016llx %016llx %016llx %016llx %016llx", mask_type_index,
661                 kv.key[0], kv.key[1], kv.key[2], kv.key[3], kv.key[4], kv.key[5]);
662     int res = clib_bihash_search_48_8 (&am->acl_lookup_hash, &kv, &result);
663     if (res == 0) {
664       DBG("ACL-MATCH! result_val: %016llx", result_val->as_u64);
665       if (result_val->applied_entry_index < curr_match_index) {
666         if (PREDICT_FALSE(result_val->need_portrange_check)) {
667           /*
668            * This is going to be slow, since we can have multiple superset
669            * entries for narrow-ish portranges, e.g.:
670            * 0..42 100..400, 230..60000,
671            * so we need to walk linearly and check if they match.
672            */
673
674           u32 curr_index = result_val->applied_entry_index;
675           while ((curr_index != ~0) && !match_portranges(am, match, curr_index)) {
676             /* while no match and there are more entries, walk... */
677             applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces),curr_index);
678             DBG("entry %d did not portmatch, advancing to %d", curr_index, pae->next_applied_entry_index);
679             curr_index = pae->next_applied_entry_index;
680           }
681           if (curr_index < curr_match_index) {
682             DBG("The index %d is the new candidate in portrange matches.", curr_index);
683             curr_match_index = curr_index;
684           } else {
685             DBG("Curr portmatch index %d is too big vs. current matched one %d", curr_index, curr_match_index);
686           }
687         } else {
688           /* The usual path is here. Found an entry in front of the current candiate - so it's a new one */
689           DBG("This match is the new candidate");
690           curr_match_index = result_val->applied_entry_index;
691           if (!result_val->shadowed) {
692           /* new result is known to not be shadowed, so no point to look up further */
693             break;
694           }
695         }
696       }
697     }
698   }
699   DBG("MATCH-RESULT: %d", curr_match_index);
700   return curr_match_index;
701 }
702
703 always_inline int
704 hash_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple,
705                        int is_ip6, u8 *action, u32 *acl_pos_p, u32 * acl_match_p,
706                        u32 * rule_match_p, u32 * trace_bitmap)
707 {
708   acl_main_t *am = p_acl_main;
709   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
710   u32 match_index = multi_acl_match_get_applied_ace_index(am, pkt_5tuple);
711   if (match_index < vec_len((*applied_hash_aces))) {
712     applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), match_index);
713     pae->hitcount++;
714     *acl_pos_p = pae->acl_position;
715     *acl_match_p = pae->acl_index;
716     *rule_match_p = pae->ace_index;
717     *action = pae->action;
718     return 1;
719   }
720   return 0;
721 }
722
723
724
725 always_inline int
726 acl_plugin_match_5tuple_inline (u32 lc_index,
727                                            fa_5tuple_opaque_t * pkt_5tuple,
728                                            int is_ip6, u8 * r_action,
729                                            u32 * r_acl_pos_p,
730                                            u32 * r_acl_match_p,
731                                            u32 * r_rule_match_p,
732                                            u32 * trace_bitmap)
733 {
734   acl_main_t *am = p_acl_main;
735   if (am->use_hash_acl_matching) {
736     return hash_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
737                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
738   } else {
739     return linear_multi_acl_match_5tuple(lc_index, (fa_5tuple_t *)pkt_5tuple, is_ip6, r_action,
740                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
741   }
742 }
743
744
745
746 #endif