1fc49818e9559ad245887b6f61a8dbae9513c2af
[vpp.git] / src / plugins / acl / session_inlines.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
16
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
23 };
24
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
31 };
32
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
37 };
38
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
43 };
44
45 /* IP4 and IP6 protocol numbers of ICMP */
46 static u8 icmp_protos[] = { IP_PROTOCOL_ICMP, IP_PROTOCOL_ICMP6 };
47
48
49
50 always_inline int
51 acl_fa_ifc_has_sessions (acl_main_t * am, int sw_if_index0)
52 {
53   return am->fa_sessions_hash_is_initialized;
54 }
55
56 always_inline int
57 acl_fa_ifc_has_in_acl (acl_main_t * am, int sw_if_index0)
58 {
59   int it_has = clib_bitmap_get (am->fa_in_acl_on_sw_if_index, sw_if_index0);
60   return it_has;
61 }
62
63 always_inline int
64 acl_fa_ifc_has_out_acl (acl_main_t * am, int sw_if_index0)
65 {
66   int it_has = clib_bitmap_get (am->fa_out_acl_on_sw_if_index, sw_if_index0);
67   return it_has;
68 }
69
70 always_inline int
71 fa_session_get_timeout_type (acl_main_t * am, fa_session_t * sess)
72 {
73   /* seen both SYNs and ACKs but not FINs means we are in establshed state */
74   u16 masked_flags =
75     sess->tcp_flags_seen.as_u16 & ((TCP_FLAGS_RSTFINACKSYN << 8) +
76                                    TCP_FLAGS_RSTFINACKSYN);
77   switch (sess->info.l4.proto)
78     {
79     case IPPROTO_TCP:
80       if (((TCP_FLAGS_ACKSYN << 8) + TCP_FLAGS_ACKSYN) == masked_flags)
81         {
82           return ACL_TIMEOUT_TCP_IDLE;
83         }
84       else
85         {
86           return ACL_TIMEOUT_TCP_TRANSIENT;
87         }
88       break;
89     case IPPROTO_UDP:
90       return ACL_TIMEOUT_UDP_IDLE;
91       break;
92     default:
93       return ACL_TIMEOUT_UDP_IDLE;
94     }
95 }
96
97 /*
98  * Get the idle timeout of a session.
99  */
100
101 always_inline u64
102 fa_session_get_timeout (acl_main_t * am, fa_session_t * sess)
103 {
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];
107   return timeout;
108 }
109
110
111
112 always_inline fa_session_t *
113 get_session_ptr (acl_main_t * am, u16 thread_index, u32 session_index)
114 {
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,
117                                            session_index) ? 0 :
118     pool_elt_at_index (pw->fa_sessions_pool,
119                        session_index);
120   return sess;
121 }
122
123 always_inline int
124 is_valid_session_ptr (acl_main_t * am, u16 thread_index, fa_session_t * sess)
125 {
126   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
127   return ((sess != 0)
128           && ((sess - pw->fa_sessions_pool) <
129               pool_len (pw->fa_sessions_pool)));
130 }
131
132 always_inline void
133 acl_fa_conn_list_add_session (acl_main_t * am, fa_full_session_id_t sess_id,
134                               u64 now)
135 {
136   fa_session_t *sess =
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])
150     {
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);
156     }
157   pw->fa_conn_list_tail[list_id] = sess_id.session_index;
158
159 #ifdef FA_NODE_VERBOSE_DEBUG
160   clib_warning
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);
163 #endif
164   pw->serviced_sw_if_index_bitmap =
165     clib_bitmap_set (pw->serviced_sw_if_index_bitmap, sess->sw_if_index, 1);
166
167   if (~0 == pw->fa_conn_list_head[list_id])
168     {
169       pw->fa_conn_list_head[list_id] = sess_id.session_index;
170     }
171 }
172
173 static int
174 acl_fa_conn_list_delete_session (acl_main_t * am,
175                                  fa_full_session_id_t sess_id)
176 {
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)
180     {
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");
184 #endif
185       return 0;
186     }
187   fa_session_t *sess =
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)
192     {
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;
198     }
199   if (~0 != sess->link_next_idx)
200     {
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;
206     }
207   if (pw->fa_conn_list_head[sess->link_list_id] == sess_id.session_index)
208     {
209       pw->fa_conn_list_head[sess->link_list_id] = sess->link_next_idx;
210     }
211   if (pw->fa_conn_list_tail[sess->link_list_id] == sess_id.session_index)
212     {
213       pw->fa_conn_list_tail[sess->link_list_id] = sess->link_prev_idx;
214     }
215   return 1;
216 }
217
218 always_inline int
219 acl_fa_restart_timer_for_session (acl_main_t * am, u64 now,
220                                   fa_full_session_id_t sess_id)
221 {
222   if (acl_fa_conn_list_delete_session (am, sess_id))
223     {
224       acl_fa_conn_list_add_session (am, sess_id, now);
225       return 1;
226     }
227   else
228     {
229       /*
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.
235        */
236       return 0;
237     }
238 }
239
240
241 always_inline u8
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)
244 {
245   sess->last_active_time = now;
246   if (pkt_5tuple->pkt.tcp_flags_valid)
247     {
248       sess->tcp_flags_seen.as_u8[is_input] |= pkt_5tuple->pkt.tcp_flags;
249     }
250   return 3;
251 }
252
253 always_inline u64
254 reverse_l4_u64_fastpath (u64 l4, int is_ip6)
255 {
256   fa_session_l4_key_t l4i = {.as_u64 = l4 };
257   fa_session_l4_key_t l4o;
258
259   l4o.port[1] = l4i.port[0];
260   l4o.port[0] = l4i.port[1];
261
262   l4o.non_port_l4_data = l4i.non_port_l4_data;
263   l4o.is_input = 1 - l4i.is_input;
264   return l4o.as_u64;
265 }
266
267 always_inline u64
268 reverse_l4_u64_slowpath (u64 l4, int is_ip6)
269 {
270   fa_session_l4_key_t l4i = {.as_u64 = l4 };
271   fa_session_l4_key_t l4o;
272
273   if (l4i.proto == icmp_protos[is_ip6])
274     {
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)
280       };
281       static const u8 icmp_valid_new_size[] = { sizeof (icmp4_valid_new),
282         sizeof (icmp6_valid_new)
283       };
284       int type = is_ip6 ? l4i.port[0] - 128 : l4i.port[0];
285
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];
289
290
291       /*
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.
295        */
296
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])
300         {
301           /*
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.
306            */
307           l4o.is_input = 1 - l4i.is_input;
308           l4o.port[0] = icmp_invmap[is_ip6][type] - 1;
309         }
310
311       return l4o.as_u64;
312     }
313   else
314     return reverse_l4_u64_fastpath (l4, is_ip6);
315 }
316
317 always_inline u64
318 reverse_l4_u64 (u64 l4, int is_ip6)
319 {
320   fa_session_l4_key_t l4i = {.as_u64 = l4 };
321
322   if (PREDICT_FALSE (l4i.is_slowpath))
323     {
324       return reverse_l4_u64_slowpath (l4, is_ip6);
325     }
326   else
327     {
328       return reverse_l4_u64_fastpath (l4, is_ip6);
329     }
330 }
331
332 always_inline void
333 reverse_session_add_del (acl_main_t * am, const int is_ip6,
334                          clib_bihash_kv_40_8_t * pkv, int is_add)
335 {
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);
346 }
347
348 always_inline void
349 acl_fa_delete_session (acl_main_t * am, u32 sw_if_index,
350                        fa_full_session_id_t sess_id)
351 {
352   void *oldheap = clib_mem_set_heap (am->acl_mheap);
353   fa_session_t *sess =
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);
357
358   reverse_session_add_del (am, sess->info.pkt.is_ip6, &sess->info.kv, 0);
359
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);
368 }
369
370 always_inline int
371 acl_fa_can_add_session (acl_main_t * am, int is_input, u32 sw_if_index)
372 {
373   u64 curr_sess_count;
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);
376 }
377
378
379 always_inline void
380 acl_fa_try_recycle_session (acl_main_t * am, int is_input, u16 thread_index,
381                             u32 sw_if_index)
382 {
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)
389     {
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);
393     }
394 }
395
396
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)
401 {
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];
408
409   f_sess_id.thread_index = thread_index;
410   fa_session_t *sess;
411
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;
415
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;
422
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;
431
432   ASSERT (am->fa_sessions_hash_is_initialized == 1);
433   clib_bihash_add_del_40_8 (&am->fa_sessions_hash, &kv, 1);
434
435   reverse_session_add_del (am, is_ip6, &kv, 1);
436
437   acl_fa_conn_list_add_session (am, f_sess_id, now);
438
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);
443   return sess;
444 }
445
446 always_inline int
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)
449 {
450   return (clib_bihash_search_inline_2_40_8
451           (&am->fa_sessions_hash, &p5tuple->kv, pvalue_sess) == 0);
452 }
453
454 /*
455  * fd.io coding-style-patch-verification: ON
456  *
457  * Local Variables:
458  * eval: (c-set-style "gnu")
459  * End:
460  */