Fix sw-if-index display error.
[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
186 always_inline void
187 acl_fill_5tuple_l3_data (acl_main_t * am, vlib_buffer_t * b0, int is_ip6,
188                  int l3_offset, fa_5tuple_t * p5tuple_pkt)
189 {
190   if (is_ip6)
191     {
192       clib_memcpy (&p5tuple_pkt->ip6_addr,
193                    get_ptr_to_offset (b0,
194                                       offsetof (ip6_header_t,
195                                                 src_address) + l3_offset),
196                    sizeof (p5tuple_pkt->ip6_addr));
197     }
198   else
199     {
200       memset(p5tuple_pkt->l3_zero_pad, 0, sizeof(p5tuple_pkt->l3_zero_pad));
201       clib_memcpy (&p5tuple_pkt->ip4_addr,
202                    get_ptr_to_offset (b0,
203                                       offsetof (ip4_header_t,
204                                                 src_address) + l3_offset),
205                    sizeof (p5tuple_pkt->ip4_addr));
206     }
207 }
208
209 always_inline void
210 acl_fill_5tuple_l4_and_pkt_data (acl_main_t * am, u32 sw_if_index0, vlib_buffer_t * b0, int is_ip6, int is_input,
211                  int l3_offset, fa_session_l4_key_t *p5tuple_l4, fa_packet_info_t *p5tuple_pkt)
212 {
213   /* IP4 and IP6 protocol numbers of ICMP */
214   static u8 icmp_protos_v4v6[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
215
216   int l4_offset;
217   u16 ports[2];
218   u8 proto;
219
220   fa_session_l4_key_t tmp_l4 = { .lsb_of_sw_if_index = sw_if_index0 & 0xffff };
221   fa_packet_info_t tmp_pkt = { .is_ip6 = is_ip6, .mask_type_index_lsb = ~0 };
222
223   if (is_ip6)
224     {
225       proto =
226         *(u8 *) get_ptr_to_offset (b0,
227                                    offsetof (ip6_header_t,
228                                              protocol) + l3_offset);
229       l4_offset = l3_offset + sizeof (ip6_header_t);
230 #ifdef FA_NODE_VERBOSE_DEBUG
231       clib_warning ("ACL_FA_NODE_DBG: proto: %d, l4_offset: %d", proto,
232                     l4_offset);
233 #endif
234       /* IP6 EH handling is here, increment l4_offset if needs to, update the proto */
235       int need_skip_eh = clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
236       if (PREDICT_FALSE (need_skip_eh))
237         {
238           while (need_skip_eh && offset_within_packet (b0, l4_offset))
239             {
240               /* Fragment header needs special handling */
241               if (PREDICT_FALSE(ACL_EH_FRAGMENT == proto))
242                 {
243                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
244                   u16 frag_offset;
245                   clib_memcpy (&frag_offset, get_ptr_to_offset (b0, 2 + l4_offset), sizeof(frag_offset));
246                   frag_offset = clib_net_to_host_u16(frag_offset) >> 3;
247                   if (frag_offset)
248                     {
249                       tmp_pkt.is_nonfirst_fragment = 1;
250                       /* invalidate L4 offset so we don't try to find L4 info */
251                       l4_offset += b0->current_length;
252                     }
253                   else
254                     {
255                       /* First fragment: skip the frag header and move on. */
256                       l4_offset += 8;
257                     }
258                 }
259               else
260                 {
261                   u8 nwords = *(u8 *) get_ptr_to_offset (b0, 1 + l4_offset);
262                   proto = *(u8 *) get_ptr_to_offset (b0, l4_offset);
263                   l4_offset += 8 * (1 + (u16) nwords);
264                 }
265 #ifdef FA_NODE_VERBOSE_DEBUG
266               clib_warning ("ACL_FA_NODE_DBG: new proto: %d, new offset: %d",
267                             proto, l4_offset);
268 #endif
269               need_skip_eh =
270                 clib_bitmap_get (am->fa_ipv6_known_eh_bitmap, proto);
271             }
272         }
273     }
274   else
275     {
276       proto =
277         *(u8 *) get_ptr_to_offset (b0,
278                                    offsetof (ip4_header_t,
279                                              protocol) + l3_offset);
280       l4_offset = l3_offset + sizeof (ip4_header_t);
281       u16 flags_and_fragment_offset;
282       clib_memcpy (&flags_and_fragment_offset,
283                    get_ptr_to_offset (b0,
284                                       offsetof (ip4_header_t,
285                                                 flags_and_fragment_offset)) + l3_offset,
286                                                 sizeof(flags_and_fragment_offset));
287       flags_and_fragment_offset = clib_net_to_host_u16 (flags_and_fragment_offset);
288
289       /* non-initial fragments have non-zero offset */
290       if ((PREDICT_FALSE(0xfff & flags_and_fragment_offset)))
291         {
292           tmp_pkt.is_nonfirst_fragment = 1;
293           /* invalidate L4 offset so we don't try to find L4 info */
294           l4_offset += b0->current_length;
295         }
296
297     }
298   tmp_l4.proto = proto;
299   tmp_l4.is_input = is_input;
300
301   if (PREDICT_TRUE (offset_within_packet (b0, l4_offset)))
302     {
303       tmp_pkt.l4_valid = 1;
304       if (icmp_protos_v4v6[is_ip6] == proto)
305         {
306           /* type */
307           tmp_l4.port[0] =
308             *(u8 *) get_ptr_to_offset (b0,
309                                        l4_offset + offsetof (icmp46_header_t,
310                                                              type));
311           /* code */
312           tmp_l4.port[1] =
313             *(u8 *) get_ptr_to_offset (b0,
314                                        l4_offset + offsetof (icmp46_header_t,
315                                                              code));
316           tmp_l4.is_slowpath = 1;
317         }
318       else if ((IP_PROTOCOL_TCP == proto) || (IP_PROTOCOL_UDP == proto))
319         {
320           clib_memcpy (&ports,
321                        get_ptr_to_offset (b0,
322                                           l4_offset + offsetof (tcp_header_t,
323                                                                 src_port)),
324                        sizeof (ports));
325           tmp_l4.port[0] = clib_net_to_host_u16 (ports[0]);
326           tmp_l4.port[1] = clib_net_to_host_u16 (ports[1]);
327
328           tmp_pkt.tcp_flags =
329             *(u8 *) get_ptr_to_offset (b0,
330                                        l4_offset + offsetof (tcp_header_t,
331                                                              flags));
332           tmp_pkt.tcp_flags_valid = (proto == IP_PROTOCOL_TCP);
333           tmp_l4.is_slowpath = 0;
334         }
335       else
336         {
337           tmp_l4.is_slowpath = 1;
338         }
339     }
340
341   p5tuple_pkt->as_u64 = tmp_pkt.as_u64;
342   p5tuple_l4->as_u64 = tmp_l4.as_u64;
343 }
344
345 always_inline void
346 acl_fill_5tuple (acl_main_t * am, u32 sw_if_index0, vlib_buffer_t * b0, int is_ip6,
347                  int is_input, int is_l2_path, fa_5tuple_t * p5tuple_pkt)
348 {
349   int l3_offset;
350
351   if (is_l2_path)
352     {
353       l3_offset = ethernet_buffer_header_size(b0);
354     }
355   else
356     {
357       if (is_input)
358         l3_offset = 0;
359       else
360         l3_offset = vnet_buffer(b0)->ip.save_rewrite_length;
361     }
362
363   /* key[0..3] contains src/dst address and is cleared/set below */
364   /* Remainder of the key and per-packet non-key data */
365   acl_fill_5tuple_l3_data(am, b0, is_ip6, l3_offset, p5tuple_pkt);
366   acl_fill_5tuple_l4_and_pkt_data(am, sw_if_index0, b0, is_ip6, is_input, l3_offset, &p5tuple_pkt->l4, &p5tuple_pkt->pkt);
367 }
368
369 always_inline void
370 acl_plugin_fill_5tuple_inline (u32 lc_index, vlib_buffer_t * b0, int is_ip6,
371                  int is_input, int is_l2_path, fa_5tuple_opaque_t * p5tuple_pkt)
372 {
373   acl_main_t *am = p_acl_main;
374   acl_fill_5tuple(am, 0, b0, is_ip6, is_input, is_l2_path, (fa_5tuple_t *)p5tuple_pkt);
375 }
376
377
378
379 always_inline int
380 fa_acl_match_ip4_addr (ip4_address_t * addr1, ip4_address_t * addr2,
381                    int prefixlen)
382 {
383   if (prefixlen == 0)
384     {
385       /* match any always succeeds */
386       return 1;
387     }
388       uint32_t a1 = clib_net_to_host_u32 (addr1->as_u32);
389       uint32_t a2 = clib_net_to_host_u32 (addr2->as_u32);
390       uint32_t mask0 = 0xffffffff - ((1 << (32 - prefixlen)) - 1);
391       return (a1 & mask0) == a2;
392 }
393
394 always_inline int
395 fa_acl_match_ip6_addr (ip6_address_t * addr1, ip6_address_t * addr2,
396                    int prefixlen)
397 {
398   if (prefixlen == 0)
399     {
400       /* match any always succeeds */
401       return 1;
402     }
403       if (memcmp (addr1, addr2, prefixlen / 8))
404         {
405           /* If the starting full bytes do not match, no point in bittwidling the thumbs further */
406           return 0;
407         }
408       if (prefixlen % 8)
409         {
410           u8 b1 = *((u8 *) addr1 + 1 + prefixlen / 8);
411           u8 b2 = *((u8 *) addr2 + 1 + prefixlen / 8);
412           u8 mask0 = (0xff - ((1 << (8 - (prefixlen % 8))) - 1));
413           return (b1 & mask0) == b2;
414         }
415       else
416         {
417           /* The prefix fits into integer number of bytes, so nothing left to do */
418           return 1;
419         }
420 }
421
422 always_inline int
423 fa_acl_match_port (u16 port, u16 port_first, u16 port_last, int is_ip6)
424 {
425   return ((port >= port_first) && (port <= port_last));
426 }
427
428 always_inline int
429 single_acl_match_5tuple (acl_main_t * am, u32 acl_index, fa_5tuple_t * pkt_5tuple,
430                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
431                   u32 * r_rule_match_p, u32 * trace_bitmap)
432 {
433   int i;
434   acl_list_t *a;
435   acl_rule_t *r;
436
437   if (pool_is_free_index (am->acls, acl_index))
438     {
439       if (r_acl_match_p)
440         *r_acl_match_p = acl_index;
441       if (r_rule_match_p)
442         *r_rule_match_p = -1;
443       /* the ACL does not exist but is used for policy. Block traffic. */
444       return 0;
445     }
446   a = am->acls + acl_index;
447   for (i = 0; i < a->count; i++)
448     {
449       r = a->rules + i;
450       if (is_ip6 != r->is_ipv6)
451         {
452           continue;
453         }
454       if (is_ip6) {
455         if (!fa_acl_match_ip6_addr
456           (&pkt_5tuple->ip6_addr[1], &r->dst.ip6, r->dst_prefixlen))
457         continue;
458         if (!fa_acl_match_ip6_addr
459           (&pkt_5tuple->ip6_addr[0], &r->src.ip6, r->src_prefixlen))
460         continue;
461       } else {
462         if (!fa_acl_match_ip4_addr
463           (&pkt_5tuple->ip4_addr[1], &r->dst.ip4, r->dst_prefixlen))
464         continue;
465         if (!fa_acl_match_ip4_addr
466           (&pkt_5tuple->ip4_addr[0], &r->src.ip4, r->src_prefixlen))
467         continue;
468       }
469
470       if (r->proto)
471         {
472           if (pkt_5tuple->l4.proto != r->proto)
473             continue;
474
475           if (PREDICT_FALSE (pkt_5tuple->pkt.is_nonfirst_fragment &&
476                      am->l4_match_nonfirst_fragment))
477           {
478             /* non-initial fragment with frag match configured - match this rule */
479             *trace_bitmap |= 0x80000000;
480             *r_action = r->is_permit;
481             if (r_acl_match_p)
482               *r_acl_match_p = acl_index;
483             if (r_rule_match_p)
484               *r_rule_match_p = i;
485             return 1;
486           }
487
488           /* A sanity check just to ensure we are about to match the ports extracted from the packet */
489           if (PREDICT_FALSE (!pkt_5tuple->pkt.l4_valid))
490             continue;
491
492 #ifdef FA_NODE_VERBOSE_DEBUG
493           clib_warning
494             ("ACL_FA_NODE_DBG acl %d rule %d pkt proto %d match rule %d",
495              acl_index, i, pkt_5tuple->l4.proto, r->proto);
496 #endif
497
498           if (!fa_acl_match_port
499               (pkt_5tuple->l4.port[0], r->src_port_or_type_first,
500                r->src_port_or_type_last, is_ip6))
501             continue;
502
503 #ifdef FA_NODE_VERBOSE_DEBUG
504           clib_warning
505             ("ACL_FA_NODE_DBG acl %d rule %d pkt sport %d match rule [%d..%d]",
506              acl_index, i, pkt_5tuple->l4.port[0], r->src_port_or_type_first,
507              r->src_port_or_type_last);
508 #endif
509
510           if (!fa_acl_match_port
511               (pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
512                r->dst_port_or_code_last, is_ip6))
513             continue;
514
515 #ifdef FA_NODE_VERBOSE_DEBUG
516           clib_warning
517             ("ACL_FA_NODE_DBG acl %d rule %d pkt dport %d match rule [%d..%d]",
518              acl_index, i, pkt_5tuple->l4.port[1], r->dst_port_or_code_first,
519              r->dst_port_or_code_last);
520 #endif
521           if (pkt_5tuple->pkt.tcp_flags_valid
522               && ((pkt_5tuple->pkt.tcp_flags & r->tcp_flags_mask) !=
523                   r->tcp_flags_value))
524             continue;
525         }
526       /* everything matches! */
527 #ifdef FA_NODE_VERBOSE_DEBUG
528       clib_warning ("ACL_FA_NODE_DBG acl %d rule %d FULL-MATCH, action %d",
529                     acl_index, i, r->is_permit);
530 #endif
531       *r_action = r->is_permit;
532       if (r_acl_match_p)
533         *r_acl_match_p = acl_index;
534       if (r_rule_match_p)
535         *r_rule_match_p = i;
536       return 1;
537     }
538   return 0;
539 }
540
541 always_inline int
542 acl_plugin_single_acl_match_5tuple (u32 acl_index, fa_5tuple_t * pkt_5tuple,
543                   int is_ip6, u8 * r_action, u32 * r_acl_match_p,
544                   u32 * r_rule_match_p, u32 * trace_bitmap)
545 {
546   acl_main_t * am = p_acl_main;
547   return single_acl_match_5tuple(am, acl_index, pkt_5tuple, is_ip6, r_action,
548                                  r_acl_match_p, r_rule_match_p, trace_bitmap);
549 }
550
551 always_inline int
552 linear_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple, 
553                        int is_ip6, u8 *r_action, u32 *acl_pos_p, u32 * acl_match_p,
554                        u32 * rule_match_p, u32 * trace_bitmap)
555 {
556   acl_main_t *am = p_acl_main;
557   int i;
558   u32 *acl_vector;
559   u8 action = 0;
560   acl_lookup_context_t *acontext = pool_elt_at_index(am->acl_lookup_contexts, lc_index);
561
562   acl_vector = acontext->acl_indices;
563
564   for (i = 0; i < vec_len (acl_vector); i++)
565     {
566 #ifdef FA_NODE_VERBOSE_DEBUG
567       clib_warning ("ACL_FA_NODE_DBG: Trying to match ACL: %d",
568                     acl_vector[i]);
569 #endif
570       if (single_acl_match_5tuple
571           (am, acl_vector[i], pkt_5tuple, is_ip6, &action,
572            acl_match_p, rule_match_p, trace_bitmap))
573         {
574           *r_action = action;
575           *acl_pos_p = i;
576           return 1;
577         }
578     }
579   if (vec_len (acl_vector) > 0)
580     {
581       return 0;
582     }
583 #ifdef FA_NODE_VERBOSE_DEBUG
584   clib_warning ("ACL_FA_NODE_DBG: No ACL on lc_index %d", lc_index);
585 #endif
586   /* If there are no ACLs defined we should not be here. */
587   return 0;
588 }
589
590
591
592 /*
593  * This returns true if there is indeed a match on the portranges.
594  * With all these levels of indirections, this is not going to be very fast,
595  * so, best use the individual ports or wildcard ports for performance.
596  */
597 always_inline int
598 match_portranges(acl_main_t *am, fa_5tuple_t *match, u32 index)
599 {
600
601   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
602   applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), index);
603
604   acl_rule_t *r = &(am->acls[pae->acl_index].rules[pae->ace_index]);
605
606 #ifdef FA_NODE_VERBOSE_DEBUG
607   clib_warning("PORTMATCH: %d <= %d <= %d && %d <= %d <= %d ?",
608                 r->src_port_or_type_first, match->l4.port[0], r->src_port_or_type_last,
609                 r->dst_port_or_code_first, match->l4.port[1], r->dst_port_or_code_last);
610 #endif
611
612   return ( ((r->src_port_or_type_first <= match->l4.port[0]) && r->src_port_or_type_last >= match->l4.port[0]) &&
613            ((r->dst_port_or_code_first <= match->l4.port[1]) && r->dst_port_or_code_last >= match->l4.port[1]) );
614 }
615
616 always_inline u32
617 multi_acl_match_get_applied_ace_index(acl_main_t *am, fa_5tuple_t *match)
618 {
619   clib_bihash_kv_48_8_t kv;
620   clib_bihash_kv_48_8_t result;
621   fa_5tuple_t *kv_key = (fa_5tuple_t *)kv.key;
622   hash_acl_lookup_value_t *result_val = (hash_acl_lookup_value_t *)&result.value;
623   u64 *pmatch = (u64 *)match;
624   u64 *pmask;
625   u64 *pkey;
626   int mask_type_index;
627   u32 curr_match_index = ~0;
628
629   u32 lc_index = match->pkt.lc_index;
630   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, match->pkt.lc_index);
631   applied_hash_acl_info_t **applied_hash_acls = &am->applied_hash_acl_info_by_lc_index;
632
633   DBG("TRYING TO MATCH: %016llx %016llx %016llx %016llx %016llx %016llx",
634                pmatch[0], pmatch[1], pmatch[2], pmatch[3], pmatch[4], pmatch[5]);
635
636   for(mask_type_index=0; mask_type_index < pool_len(am->ace_mask_type_pool); mask_type_index++) {
637     if (!clib_bitmap_get(vec_elt_at_index((*applied_hash_acls), lc_index)->mask_type_index_bitmap, mask_type_index)) {
638       /* This bit is not set. Avoid trying to match */
639       continue;
640     }
641     ace_mask_type_entry_t *mte = vec_elt_at_index(am->ace_mask_type_pool, mask_type_index);
642     pmatch = (u64 *)match;
643     pmask = (u64 *)&mte->mask;
644     pkey = (u64 *)kv.key;
645     /*
646     * unrolling the below loop results in a noticeable performance increase.
647     int i;
648     for(i=0; i<6; i++) {
649       kv.key[i] = pmatch[i] & pmask[i];
650     }
651     */
652
653     *pkey++ = *pmatch++ & *pmask++;
654     *pkey++ = *pmatch++ & *pmask++;
655     *pkey++ = *pmatch++ & *pmask++;
656     *pkey++ = *pmatch++ & *pmask++;
657     *pkey++ = *pmatch++ & *pmask++;
658     *pkey++ = *pmatch++ & *pmask++;
659
660     kv_key->pkt.mask_type_index_lsb = mask_type_index;
661     DBG("        KEY %3d: %016llx %016llx %016llx %016llx %016llx %016llx", mask_type_index,
662                 kv.key[0], kv.key[1], kv.key[2], kv.key[3], kv.key[4], kv.key[5]);
663     int res = clib_bihash_search_48_8 (&am->acl_lookup_hash, &kv, &result);
664     if (res == 0) {
665       DBG("ACL-MATCH! result_val: %016llx", result_val->as_u64);
666       if (result_val->applied_entry_index < curr_match_index) {
667         if (PREDICT_FALSE(result_val->need_portrange_check)) {
668           /*
669            * This is going to be slow, since we can have multiple superset
670            * entries for narrow-ish portranges, e.g.:
671            * 0..42 100..400, 230..60000,
672            * so we need to walk linearly and check if they match.
673            */
674
675           u32 curr_index = result_val->applied_entry_index;
676           while ((curr_index != ~0) && !match_portranges(am, match, curr_index)) {
677             /* while no match and there are more entries, walk... */
678             applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces),curr_index);
679             DBG("entry %d did not portmatch, advancing to %d", curr_index, pae->next_applied_entry_index);
680             curr_index = pae->next_applied_entry_index;
681           }
682           if (curr_index < curr_match_index) {
683             DBG("The index %d is the new candidate in portrange matches.", curr_index);
684             curr_match_index = curr_index;
685           } else {
686             DBG("Curr portmatch index %d is too big vs. current matched one %d", curr_index, curr_match_index);
687           }
688         } else {
689           /* The usual path is here. Found an entry in front of the current candiate - so it's a new one */
690           DBG("This match is the new candidate");
691           curr_match_index = result_val->applied_entry_index;
692           if (!result_val->shadowed) {
693           /* new result is known to not be shadowed, so no point to look up further */
694             break;
695           }
696         }
697       }
698     }
699   }
700   DBG("MATCH-RESULT: %d", curr_match_index);
701   return curr_match_index;
702 }
703
704 always_inline int
705 hash_multi_acl_match_5tuple (u32 lc_index, fa_5tuple_t * pkt_5tuple,
706                        int is_ip6, u8 *action, u32 *acl_pos_p, u32 * acl_match_p,
707                        u32 * rule_match_p, u32 * trace_bitmap)
708 {
709   acl_main_t *am = p_acl_main;
710   applied_hash_ace_entry_t **applied_hash_aces = vec_elt_at_index(am->hash_entry_vec_by_lc_index, lc_index);
711   u32 match_index = multi_acl_match_get_applied_ace_index(am, pkt_5tuple);
712   if (match_index < vec_len((*applied_hash_aces))) {
713     applied_hash_ace_entry_t *pae = vec_elt_at_index((*applied_hash_aces), match_index);
714     pae->hitcount++;
715     *acl_pos_p = pae->acl_position;
716     *acl_match_p = pae->acl_index;
717     *rule_match_p = pae->ace_index;
718     *action = pae->action;
719     return 1;
720   }
721   return 0;
722 }
723
724
725
726 always_inline int
727 acl_plugin_match_5tuple_inline (u32 lc_index,
728                                            fa_5tuple_opaque_t * pkt_5tuple,
729                                            int is_ip6, u8 * r_action,
730                                            u32 * r_acl_pos_p,
731                                            u32 * r_acl_match_p,
732                                            u32 * r_rule_match_p,
733                                            u32 * trace_bitmap)
734 {
735   acl_main_t *am = p_acl_main;
736   fa_5tuple_t * pkt_5tuple_internal = (fa_5tuple_t *)pkt_5tuple;
737   pkt_5tuple_internal->pkt.lc_index = lc_index;
738   if (am->use_hash_acl_matching) {
739     return hash_multi_acl_match_5tuple(lc_index, pkt_5tuple_internal, is_ip6, r_action,
740                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
741   } else {
742     return linear_multi_acl_match_5tuple(lc_index, pkt_5tuple_internal, is_ip6, r_action,
743                                  r_acl_pos_p, r_acl_match_p, r_rule_match_p, trace_bitmap);
744   }
745 }
746
747
748
749 #endif