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