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