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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* ICMPv4 invert type for stateful ACL */
18 static const u8 icmp4_invmap[] = {
19 [ICMP4_echo_reply] = ICMP4_echo_request + 1,
20 [ICMP4_timestamp_reply] = ICMP4_timestamp_request + 1,
21 [ICMP4_information_reply] = ICMP4_information_request + 1,
22 [ICMP4_address_mask_reply] = ICMP4_address_mask_request + 1
25 /* Supported ICMPv4 messages for session creation */
26 static const u8 icmp4_valid_new[] = {
27 [ICMP4_echo_request] = 1,
28 [ICMP4_timestamp_request] = 1,
29 [ICMP4_information_request] = 1,
30 [ICMP4_address_mask_request] = 1
33 /* ICMPv6 invert type for stateful ACL */
34 static const u8 icmp6_invmap[] = {
35 [ICMP6_echo_reply - 128] = ICMP6_echo_request + 1,
36 [ICMP6_node_information_response - 128] = ICMP6_node_information_request + 1
39 /* Supported ICMPv6 messages for session creation */
40 static const u8 icmp6_valid_new[] = {
41 [ICMP6_echo_request - 128] = 1,
42 [ICMP6_node_information_request - 128] = 1
45 /* IP4 and IP6 protocol numbers of ICMP */
46 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
51 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
53 return am->fa_sessions_hash_is_initialized;
57 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
59 int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
64 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
66 int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
71 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
73 /* seen both SYNs and ACKs but not FINs means we are in establshed state */
75 sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
76 TCP_FLAGS_RSTFINACKSYN);
77 switch (sess->info.l4.proto)
80 if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
82 return ACL_TIMEOUT_TCP_IDLE;
86 return ACL_TIMEOUT_TCP_TRANSIENT;
90 return ACL_TIMEOUT_UDP_IDLE;
93 return ACL_TIMEOUT_UDP_IDLE;
98 * Get the idle timeout of a session.
102 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
104 u64 timeout = am->vlib_main->clib_time.clocks_per_second;
105 int timeout_type = fa_session_get_timeout_type (am, sess);
106 timeout *= am->session_timeout_sec[timeout_type];
112 always_inline fa_session_t *
113 get_session_ptr (acl_main_t * am, u16 thread_index, u32 session_index)
115 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
116 fa_session_t *sess = pool_is_free_index (pw->fa_sessions_pool,
118 pool_elt_at_index (pw->fa_sessions_pool,
124 is_valid_session_ptr (acl_main_t * am, u16 thread_index, fa_session_t * sess)
126 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
128 && ((sess - pw->fa_sessions_pool) <
129 pool_len (pw->fa_sessions_pool)));
133 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id,
137 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
138 u8 list_id = fa_session_get_timeout_type (am, sess);
139 uword thread_index = os_get_thread_index ();
140 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
141 /* the retrieved session thread index must be necessarily the same as the one in the key */
142 ASSERT (sess->thread_index == sess_id.thread_index);
143 /* the retrieved session thread index must be the same as current thread */
144 ASSERT (sess->thread_index == thread_index);
145 sess->link_enqueue_time = now;
146 sess->link_list_id = list_id;
147 sess->link_next_idx = ~0;
148 sess->link_prev_idx = pw->fa_conn_list_tail[list_id];
149 if (~0 != pw->fa_conn_list_tail[list_id])
151 fa_session_t *prev_sess =
152 get_session_ptr (am, thread_index, pw->fa_conn_list_tail[list_id]);
153 prev_sess->link_next_idx = sess_id.session_index;
154 /* We should never try to link with a session on another thread */
155 ASSERT (prev_sess->thread_index == sess->thread_index);
157 pw->fa_conn_list_tail[list_id] = sess_id.session_index;
159 #ifdef FA_NODE_VERBOSE_DEBUG
161 ("FA-SESSION-DEBUG: add session id %d on thread %d sw_if_index %d",
162 sess_id.session_index, thread_index, sess->sw_if_index);
164 pw->serviced_sw_if_index_bitmap =
165 clib_bitmap_set (pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
167 if (~0 == pw->fa_conn_list_head[list_id])
169 pw->fa_conn_list_head[list_id] = sess_id.session_index;
174 acl_fa_conn_list_delete_session (acl_main_t * am,
175 fa_full_session_id_t sess_id)
177 uword thread_index = os_get_thread_index ();
178 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
179 if (thread_index != sess_id.thread_index)
181 /* If another thread attempts to delete the session, fail it. */
182 #ifdef FA_NODE_VERBOSE_DEBUG
183 clib_warning ("thread id in key %d != curr thread index, not deleting");
188 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
189 /* we should never try to delete the session with another thread index */
190 ASSERT (sess->thread_index == thread_index);
191 if (~0 != sess->link_prev_idx)
193 fa_session_t *prev_sess =
194 get_session_ptr (am, thread_index, sess->link_prev_idx);
195 /* the previous session must be in the same list as this one */
196 ASSERT (prev_sess->link_list_id == sess->link_list_id);
197 prev_sess->link_next_idx = sess->link_next_idx;
199 if (~0 != sess->link_next_idx)
201 fa_session_t *next_sess =
202 get_session_ptr (am, thread_index, sess->link_next_idx);
203 /* The next session must be in the same list as the one we are deleting */
204 ASSERT (next_sess->link_list_id == sess->link_list_id);
205 next_sess->link_prev_idx = sess->link_prev_idx;
207 if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index)
209 pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
211 if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index)
213 pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
219 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now,
220 fa_full_session_id_t sess_id)
222 if (acl_fa_conn_list_delete_session (am, sess_id))
224 acl_fa_conn_list_add_session (am, sess_id, now);
230 * Our thread does not own this connection, so we can not delete
231 * The session. To avoid the complicated signaling, we simply
232 * pick the list waiting time to be the shortest of the timeouts.
233 * This way we do not have to do anything special, and let
234 * the regular requeue check take care of everything.
242 acl_fa_track_session (acl_main_t * am, int is_input, u32 sw_if_index, u64 now,
243 fa_session_t * sess, fa_5tuple_t * pkt_5tuple)
245 sess->last_active_time = now;
246 if (pkt_5tuple->pkt.tcp_flags_valid)
248 sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
254 reverse_l4_u64_fastpath (u64 l4, int is_ip6)
256 fa_session_l4_key_t l4i = {.as_u64 = l4 };
257 fa_session_l4_key_t l4o;
259 l4o.port[1] = l4i.port[0];
260 l4o.port[0] = l4i.port[1];
262 l4o.non_port_l4_data = l4i.non_port_l4_data;
263 l4o.is_input = 1 - l4i.is_input;
268 reverse_l4_u64_slowpath (u64 l4, int is_ip6)
270 fa_session_l4_key_t l4i = {.as_u64 = l4 };
271 fa_session_l4_key_t l4o;
273 if (l4i.proto == icmp_protos[is_ip6])
275 static const u8 *icmp_invmap[] = { icmp4_invmap, icmp6_invmap };
276 static const u8 *icmp_valid_new[] =
277 { icmp4_valid_new, icmp6_valid_new };
278 static const u8 icmp_invmap_size[] = { sizeof (icmp4_invmap),
279 sizeof (icmp6_invmap)
281 static const u8 icmp_valid_new_size[] = { sizeof (icmp4_valid_new),
282 sizeof (icmp6_valid_new)
284 int type = is_ip6 ? l4i.port[0] - 128 : l4i.port[0];
286 l4o.non_port_l4_data = l4i.non_port_l4_data;
287 l4o.port[0] = l4i.port[0];
288 l4o.port[1] = l4i.port[1];
292 * ONLY ICMP messages defined in icmp4_valid_new/icmp6_valid_new table
293 * are allowed to create stateful ACL.
294 * The other messages will be forwarded without creating a reverse session.
297 if (type >= 0 && (type <= icmp_valid_new_size[is_ip6])
298 && (icmp_valid_new[is_ip6][type])
299 && (type <= icmp_invmap_size[is_ip6]) && icmp_invmap[is_ip6][type])
302 * we set the inverse direction and correct the port,
303 * if it is okay to add the reverse session.
304 * If not, then the same session will be added twice
305 * to bihash, which is the same as adding just one session.
307 l4o.is_input = 1 - l4i.is_input;
308 l4o.port[0] = icmp_invmap[is_ip6][type] - 1;
314 return reverse_l4_u64_fastpath (l4, is_ip6);
318 reverse_l4_u64 (u64 l4, int is_ip6)
320 fa_session_l4_key_t l4i = {.as_u64 = l4 };
322 if (PREDICT_FALSE (l4i.is_slowpath))
324 return reverse_l4_u64_slowpath (l4, is_ip6);
328 return reverse_l4_u64_fastpath (l4, is_ip6);
333 reverse_session_add_del (acl_main_t * am, const int is_ip6,
334 clib_bihash_kv_40_8_t * pkv, int is_add)
336 clib_bihash_kv_40_8_t kv2;
337 /* the first 4xu64 is two addresses, so just swap them */
338 kv2.key[0] = pkv->key[2];
339 kv2.key[1] = pkv->key[3];
340 kv2.key[2] = pkv->key[0];
341 kv2.key[3] = pkv->key[1];
342 /* the last u64 needs special treatment (ports, etc.) */
343 kv2.key[4] = reverse_l4_u64 (pkv->key[4], is_ip6);
344 kv2.value = pkv->value;
345 clib_bihash_add_del_40_8 (&am->fa_sessions_hash, &kv2, is_add);
349 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index,
350 fa_full_session_id_t sess_id)
352 void *oldheap = clib_mem_set_heap (am->acl_mheap);
354 get_session_ptr (am, sess_id.thread_index, sess_id.session_index);
355 ASSERT (sess->thread_index == os_get_thread_index ());
356 clib_bihash_add_del_40_8 (&am->fa_sessions_hash, &sess->info.kv, 0);
358 reverse_session_add_del (am, sess->info.pkt.is_ip6, &sess->info.kv, 0);
360 acl_fa_per_worker_data_t *pw = &am->per_worker_data[sess_id.thread_index];
361 pool_put_index (pw->fa_sessions_pool, sess_id.session_index);
362 /* Deleting from timer structures not needed,
363 as the caller must have dealt with the timers. */
364 vec_validate (pw->fa_session_dels_by_sw_if_index, sw_if_index);
365 clib_mem_set_heap (oldheap);
366 pw->fa_session_dels_by_sw_if_index[sw_if_index]++;
367 clib_smp_atomic_add (&am->fa_session_total_dels, 1);
371 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
374 curr_sess_count = am->fa_session_total_adds - am->fa_session_total_dels;
375 return (curr_sess_count < am->fa_conn_table_max_entries);
380 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index,
383 /* try to recycle a TCP transient session */
384 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
385 u8 timeout_type = ACL_TIMEOUT_TCP_TRANSIENT;
386 fa_full_session_id_t sess_id;
387 sess_id.session_index = pw->fa_conn_list_head[timeout_type];
388 if (~0 != sess_id.session_index)
390 sess_id.thread_index = thread_index;
391 acl_fa_conn_list_delete_session (am, sess_id);
392 acl_fa_delete_session (am, sw_if_index, sess_id);
397 always_inline fa_session_t *
398 acl_fa_add_session (acl_main_t * am, int is_input, int is_ip6,
399 u32 sw_if_index, u64 now, fa_5tuple_t * p5tuple,
400 u16 current_policy_epoch)
402 clib_bihash_kv_40_8_t *pkv = &p5tuple->kv;
403 clib_bihash_kv_40_8_t kv;
404 fa_full_session_id_t f_sess_id;
405 uword thread_index = os_get_thread_index ();
406 void *oldheap = clib_mem_set_heap (am->acl_mheap);
407 acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
409 f_sess_id.thread_index = thread_index;
412 pool_get_aligned (pw->fa_sessions_pool, sess, CLIB_CACHE_LINE_BYTES);
413 f_sess_id.session_index = sess - pw->fa_sessions_pool;
414 f_sess_id.intf_policy_epoch = current_policy_epoch;
416 kv.key[0] = pkv->key[0];
417 kv.key[1] = pkv->key[1];
418 kv.key[2] = pkv->key[2];
419 kv.key[3] = pkv->key[3];
420 kv.key[4] = pkv->key[4];
421 kv.value = f_sess_id.as_u64;
423 memcpy (sess, pkv, sizeof (pkv->key));
424 sess->last_active_time = now;
425 sess->sw_if_index = sw_if_index;
426 sess->tcp_flags_seen.as_u16 = 0;
427 sess->thread_index = thread_index;
428 sess->link_list_id = ~0;
429 sess->link_prev_idx = ~0;
430 sess->link_next_idx = ~0;
432 ASSERT (am->fa_sessions_hash_is_initialized == 1);
433 clib_bihash_add_del_40_8 (&am->fa_sessions_hash, &kv, 1);
435 reverse_session_add_del (am, is_ip6, &kv, 1);
437 acl_fa_conn_list_add_session (am, f_sess_id, now);
439 vec_validate (pw->fa_session_adds_by_sw_if_index, sw_if_index);
440 clib_mem_set_heap (oldheap);
441 pw->fa_session_adds_by_sw_if_index[sw_if_index]++;
442 clib_smp_atomic_add (&am->fa_session_total_adds, 1);
447 acl_fa_find_session (acl_main_t * am, u32 sw_if_index0, fa_5tuple_t * p5tuple,
448 clib_bihash_kv_40_8_t * pvalue_sess)
450 return (clib_bihash_search_40_8
451 (&am->fa_sessions_hash, &p5tuple->kv, pvalue_sess) == 0);
455 * fd.io coding-style-patch-verification: ON
458 * eval: (c-set-style "gnu")