5765e91d43359cf7eb055a28c6a7a7fdac4d4243
[vpp.git] / src / plugins / acl / acl.h
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 #ifndef included_acl_h
16 #define included_acl_h
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/l2/l2_output.h>
21
22
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/bitmap.h>
26 #include <vppinfra/elog.h>
27 #include <vppinfra/bihash_48_8.h>
28 #include <vppinfra/bihash_40_8.h>
29
30 #include "fa_node.h"
31 #include "hash_lookup_types.h"
32 #include "lookup_context.h"
33
34 #define  ACL_PLUGIN_VERSION_MAJOR 1
35 #define  ACL_PLUGIN_VERSION_MINOR 3
36
37 #define UDP_SESSION_IDLE_TIMEOUT_SEC 600
38 #define TCP_SESSION_IDLE_TIMEOUT_SEC (3600*24)
39 #define TCP_SESSION_TRANSIENT_TIMEOUT_SEC 120
40
41 #define ACL_FA_DEFAULT_HEAP_SIZE (2 << 29)
42
43 #define ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE (2 << 25)
44 #define ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS 65536
45 #define ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY (2 << 25)
46
47 extern vlib_node_registration_t acl_in_node;
48 extern vlib_node_registration_t acl_out_node;
49
50 void input_acl_packet_match(u32 sw_if_index, vlib_buffer_t * b0, u32 *nextp, u32 *acl_match_p, u32 *rule_match_p, u32 *trace_bitmap);
51 void output_acl_packet_match(u32 sw_if_index, vlib_buffer_t * b0, u32 *nextp, u32 *acl_match_p, u32 *rule_match_p, u32 *trace_bitmap);
52
53 enum acl_timeout_e {
54   ACL_TIMEOUT_UDP_IDLE = 0,
55   ACL_TIMEOUT_TCP_IDLE,
56   ACL_TIMEOUT_TCP_TRANSIENT,
57   ACL_N_TIMEOUTS
58 };
59
60
61 enum address_e { IP4, IP6 };
62 typedef struct
63 {
64   enum address_e type;
65   union {
66     ip6_address_t ip6;
67     ip4_address_t ip4;
68   } addr;
69 } address_t;
70
71 /*
72  * ACL rules
73  */
74 typedef struct
75 {
76   u8 is_permit;
77   u8 is_ipv6;
78   ip46_address_t src;
79   u8 src_prefixlen;
80   ip46_address_t dst;
81   u8 dst_prefixlen;
82   u8 proto;
83   u16 src_port_or_type_first;
84   u16 src_port_or_type_last;
85   u16 dst_port_or_code_first;
86   u16 dst_port_or_code_last;
87   u8 tcp_flags_value;
88   u8 tcp_flags_mask;
89 } acl_rule_t;
90
91 typedef struct
92 {
93   u8 is_permit;
94   u8 is_ipv6;
95   u8 src_mac[6];
96   u8 src_mac_mask[6];
97   ip46_address_t src_ip_addr;
98   u8 src_prefixlen;
99 } macip_acl_rule_t;
100
101 /*
102  * ACL
103  */
104 typedef struct
105 {
106   u8 tag[64];
107   u32 count;
108   acl_rule_t *rules;
109 } acl_list_t;
110
111 typedef struct
112 {
113   u8 tag[64];
114   u32 count;
115   macip_acl_rule_t *rules;
116   /* References to the classifier tables that will enforce the rules */
117   u32 ip4_table_index;
118   u32 ip6_table_index;
119   u32 l2_table_index;
120   /* outacl classifier tables */
121   u32 out_ip4_table_index;
122   u32 out_ip6_table_index;
123   u32 out_l2_table_index;
124 } macip_acl_list_t;
125
126 /*
127  * An element describing a particular configuration fo the mask,
128  * and how many times it has been used.
129  */
130 typedef struct
131 {
132   fa_5tuple_t mask;
133   u32 refcount;
134 } ace_mask_type_entry_t;
135
136 typedef struct {
137   /* mheap to hold all the ACL module related allocations, other than hash */
138   void *acl_mheap;
139   u32 acl_mheap_size;
140
141   /* API message ID base */
142   u16 msg_id_base;
143
144   /* The pool of users of ACL lookup contexts */
145   acl_lookup_context_user_t *acl_users;
146   /* The pool of ACL lookup contexts */
147   acl_lookup_context_t *acl_lookup_contexts;
148
149   acl_list_t *acls;     /* Pool of ACLs */
150   hash_acl_info_t *hash_acl_infos; /* corresponding hash matching housekeeping info */
151   clib_bihash_48_8_t acl_lookup_hash; /* ACL lookup hash table. */
152   u32 hash_lookup_hash_buckets;
153   u32 hash_lookup_hash_memory;
154
155   /* mheap to hold all the miscellaneous allocations related to hash-based lookups */
156   void *hash_lookup_mheap;
157   u32 hash_lookup_mheap_size;
158   int acl_lookup_hash_initialized;
159 /*
160   applied_hash_ace_entry_t **input_hash_entry_vec_by_sw_if_index;
161   applied_hash_ace_entry_t **output_hash_entry_vec_by_sw_if_index;
162   applied_hash_acl_info_t *input_applied_hash_acl_info_by_sw_if_index;
163   applied_hash_acl_info_t *output_applied_hash_acl_info_by_sw_if_index;
164 */
165   applied_hash_ace_entry_t **hash_entry_vec_by_lc_index;
166   applied_hash_acl_info_t *applied_hash_acl_info_by_lc_index;
167
168   /* Corresponding lookup context indices for in/out lookups per sw_if_index */
169   u32 *input_lc_index_by_sw_if_index;
170   u32 *output_lc_index_by_sw_if_index;
171   /* context user id for interface ACLs */
172   u32 interface_acl_user_id;
173
174   macip_acl_list_t *macip_acls; /* Pool of MAC-IP ACLs */
175
176   /* ACLs associated with interfaces */
177   u32 **input_acl_vec_by_sw_if_index;
178   u32 **output_acl_vec_by_sw_if_index;
179
180   /* interfaces on which given ACLs are applied */
181   u32 **input_sw_if_index_vec_by_acl;
182   u32 **output_sw_if_index_vec_by_acl;
183
184   /* bitmaps 1=sw_if_index has in/out ACL processing enabled */
185   uword *in_acl_on_sw_if_index;
186   uword *out_acl_on_sw_if_index;
187
188   /* lookup contexts where a given ACL is used */
189   u32 **lc_index_vec_by_acl;
190
191   /* Total count of interface+direction pairs enabled */
192   u32 fa_total_enabled_count;
193
194   /* Do we use hash-based ACL matching or linear */
195   int use_hash_acl_matching;
196
197   /* a pool of all mask types present in all ACEs */
198   ace_mask_type_entry_t *ace_mask_type_pool;
199
200   /*
201    * Classify tables used to grab the packets for the ACL check,
202    * and serving as the 5-tuple session tables at the same time
203    */
204   u32 *acl_ip4_input_classify_table_by_sw_if_index;
205   u32 *acl_ip6_input_classify_table_by_sw_if_index;
206   u32 *acl_ip4_output_classify_table_by_sw_if_index;
207   u32 *acl_ip6_output_classify_table_by_sw_if_index;
208
209   u32 *acl_dot1q_input_classify_table_by_sw_if_index;
210   u32 *acl_dot1ad_input_classify_table_by_sw_if_index;
211   u32 *acl_dot1q_output_classify_table_by_sw_if_index;
212   u32 *acl_dot1ad_output_classify_table_by_sw_if_index;
213
214   u32 *acl_etype_input_classify_table_by_sw_if_index;
215   u32 *acl_etype_output_classify_table_by_sw_if_index;
216
217   u16 **input_etype_whitelist_by_sw_if_index;
218   u16 **output_etype_whitelist_by_sw_if_index;
219
220   /* MACIP (input) ACLs associated with the interfaces */
221   u32 *macip_acl_by_sw_if_index;
222
223   /* bitmaps when set the processing is enabled on the interface */
224   uword *fa_in_acl_on_sw_if_index;
225   uword *fa_out_acl_on_sw_if_index;
226   /* bihash holding all of the sessions */
227   int fa_sessions_hash_is_initialized;
228   clib_bihash_40_8_t fa_sessions_hash;
229   /* The process node which orcherstrates the cleanup */
230   u32 fa_cleaner_node_index;
231   /* FA session timeouts, in seconds */
232   u32 session_timeout_sec[ACL_N_TIMEOUTS];
233   /* total session adds/dels */
234   u64 fa_session_total_adds;
235   u64 fa_session_total_dels;
236
237   /* L2 datapath glue */
238
239   /* next indices within L2 classifiers for ip4/ip6 fa L2 nodes */
240   u32 l2_input_classify_next_acl_ip4;
241   u32 l2_input_classify_next_acl_ip6;
242   u32 l2_output_classify_next_acl_ip4;
243   u32 l2_output_classify_next_acl_ip6;
244   /* next node indices for L2 dispatch */
245   u32 fa_acl_in_ip4_l2_node_feat_next_node_index[32];
246   u32 fa_acl_in_ip6_l2_node_feat_next_node_index[32];
247   u32 fa_acl_out_ip4_l2_node_feat_next_node_index[32];
248   u32 fa_acl_out_ip6_l2_node_feat_next_node_index[32];
249
250   /* EH values that we can skip over */
251   uword *fa_ipv6_known_eh_bitmap;
252
253   /* whether to match L4 ACEs with ports on the non-initial fragment */
254   int l4_match_nonfirst_fragment;
255
256   /* conn table per-interface conn table parameters */
257   u32 fa_conn_table_hash_num_buckets;
258   uword fa_conn_table_hash_memory_size;
259   u64 fa_conn_table_max_entries;
260
261   int trace_sessions;
262   int trace_acl;
263
264   /*
265    * If the cleaner has to delete more than this number
266    * of connections, it halves the sleep time.
267    */
268
269 #define ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL 100
270   u64 fa_max_deleted_sessions_per_interval;
271
272   /*
273    * If the cleaner deletes less than these connections,
274    * it increases the wait time by the "increment"
275    */
276
277 #define ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL 1
278   u64 fa_min_deleted_sessions_per_interval;
279
280 #define ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT 0.1
281   f64 fa_cleaner_wait_time_increment;
282
283   u64 fa_current_cleaner_timer_wait_interval;
284
285   int fa_interrupt_generation;
286
287   /* per-worker data related t conn management */
288   acl_fa_per_worker_data_t *per_worker_data;
289
290   /* Configured session timeout */
291   u64 session_timeout[ACL_N_TIMEOUTS];
292
293
294   /* Counters for the cleaner thread */
295
296 #define foreach_fa_cleaner_counter                                         \
297   _(fa_cleaner_cnt_delete_by_sw_index, "delete_by_sw_index events")        \
298   _(fa_cleaner_cnt_delete_by_sw_index_ok, "delete_by_sw_index handled ok") \
299   _(fa_cleaner_cnt_unknown_event, "unknown events received")               \
300   _(fa_cleaner_cnt_timer_restarted, "session idle timers restarted")       \
301   _(fa_cleaner_cnt_wait_with_timeout, "event wait with timeout called")    \
302   _(fa_cleaner_cnt_wait_without_timeout, "event wait w/o timeout called")  \
303   _(fa_cleaner_cnt_event_cycles, "total event cycles")                     \
304 /* end of counters */
305 #define _(id, desc) u32 id;
306   foreach_fa_cleaner_counter
307 #undef _
308
309   /* convenience */
310   vlib_main_t * vlib_main;
311   vnet_main_t * vnet_main;
312 } acl_main_t;
313
314 #define foreach_acl_eh                                          \
315    _(HOPBYHOP , 0  , "IPv6ExtHdrHopByHop")                      \
316    _(ROUTING  , 43 , "IPv6ExtHdrRouting")                       \
317    _(DESTOPT  , 60 , "IPv6ExtHdrDestOpt")                       \
318    _(FRAGMENT , 44 , "IPv6ExtHdrFragment")                      \
319    _(MOBILITY , 135, "Mobility Header")                         \
320    _(HIP      , 139, "Experimental use Host Identity Protocol") \
321    _(SHIM6    , 140, "Shim6 Protocol")                          \
322    _(EXP1     , 253, "Use for experimentation and testing")     \
323    _(EXP2     , 254, "Use for experimentation and testing")
324
325 /*
326
327  "No Next Header" is not a header.
328  Also, Fragment header needs special processing.
329
330    _(NONEXT   , 59 , "NoNextHdr")                               \
331
332
333 ESP is hiding its internal format, so no point in trying to go past it.
334
335    _(ESP      , 50 , "EncapsulatingSecurityPayload")            \
336
337
338 AH has a special treatment of its length, it is in 32-bit words, not 64-bit words like the rest.
339
340    _(AUTH     , 51 , "Authentication Header")                   \
341
342
343 */
344
345
346  typedef enum {
347  #define _(N, v, s) ACL_EH_##N = v,
348          foreach_acl_eh
349  #undef _
350  } acl_eh_t;
351
352
353
354 extern acl_main_t acl_main;
355
356
357 #endif