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