blob: 3b330982954b367bfc9060c5a30403b0ca44c82a [file] [log] [blame]
Florin Coras04e53442017-07-16 17:12:15 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras04e53442017-07-16 17:12:15 -07003 * 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/** Generate typed init functions for multiple hash table styles... */
17#include <vppinfra/bihash_16_8.h>
18#include <vppinfra/bihash_template.h>
19
20#include <vppinfra/bihash_template.c>
21
22#undef __included_bihash_template_h__
23
24#include <vppinfra/bihash_48_8.h>
25#include <vppinfra/bihash_template.h>
26
27#include <vppinfra/bihash_template.c>
28#include <vnet/session/session_lookup.h>
29#include <vnet/session/session.h>
Florin Corascea194d2017-10-02 00:18:51 -070030#include <vnet/session/application.h>
Florin Coras04e53442017-07-16 17:12:15 -070031
Florin Corascea194d2017-10-02 00:18:51 -070032/**
Florin Corascea194d2017-10-02 00:18:51 -070033 * Network namespace index (i.e., fib index) to session lookup table. We
34 * should have one per network protocol type but for now we only support IP4/6
35 */
36static u32 *fib_index_to_table_index[2];
37
Florin Coras04e53442017-07-16 17:12:15 -070038/* *INDENT-OFF* */
39/* 16 octets */
40typedef CLIB_PACKED (struct {
41 union
42 {
43 struct
44 {
45 ip4_address_t src;
46 ip4_address_t dst;
47 u16 src_port;
48 u16 dst_port;
49 /* align by making this 4 octets even though its a 1-bit field
50 * NOTE: avoid key overlap with other transports that use 5 tuples for
51 * session identification.
52 */
53 u32 proto;
54 };
55 u64 as_u64[2];
56 };
57}) v4_connection_key_t;
58
59typedef CLIB_PACKED (struct {
60 union
61 {
62 struct
63 {
64 /* 48 octets */
65 ip6_address_t src;
66 ip6_address_t dst;
67 u16 src_port;
68 u16 dst_port;
69 u32 proto;
70 u64 unused;
71 };
72 u64 as_u64[6];
73 };
74}) v6_connection_key_t;
75/* *INDENT-ON* */
76
77typedef clib_bihash_kv_16_8_t session_kv4_t;
78typedef clib_bihash_kv_48_8_t session_kv6_t;
79
80always_inline void
81make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
82 u16 lcl_port, u16 rmt_port, u8 proto)
83{
Florin Coras9679c812018-06-21 08:14:34 -070084 kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
85 kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070086 kv->value = ~0ULL;
87}
88
89always_inline void
90make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
91 u8 proto)
92{
Florin Coras9679c812018-06-21 08:14:34 -070093 kv->key[0] = (u64) lcl->as_u32;
94 kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070095 kv->value = ~0ULL;
96}
97
98always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -080099make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
100{
Florin Coras9679c812018-06-21 08:14:34 -0700101 kv->key[0] = (u64) lcl->as_u32;
102 kv->key[1] = (u64) proto << 32;
Florin Corasdbd44562017-11-09 19:30:17 -0800103 kv->value = ~0ULL;
104}
105
106always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800107make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700108{
Florin Coras561af9b2017-12-09 10:19:43 -0800109 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
110 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700111}
112
113always_inline void
114make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
115 u16 lcl_port, u16 rmt_port, u8 proto)
116{
Florin Coras9679c812018-06-21 08:14:34 -0700117 kv->key[0] = lcl->as_u64[0];
118 kv->key[1] = lcl->as_u64[1];
119 kv->key[2] = rmt->as_u64[0];
120 kv->key[3] = rmt->as_u64[1];
121 kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
122 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700123 kv->value = ~0ULL;
124}
125
126always_inline void
127make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
128 u8 proto)
129{
Florin Coras9679c812018-06-21 08:14:34 -0700130 kv->key[0] = lcl->as_u64[0];
131 kv->key[1] = lcl->as_u64[1];
132 kv->key[2] = 0;
133 kv->key[3] = 0;
134 kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
135 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700136 kv->value = ~0ULL;
137}
138
139always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800140make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
141{
Florin Coras9679c812018-06-21 08:14:34 -0700142 kv->key[0] = lcl->as_u64[0];
143 kv->key[1] = lcl->as_u64[1];
144 kv->key[2] = 0;
145 kv->key[3] = 0;
146 kv->key[4] = (u64) proto << 32;
147 kv->key[5] = 0;
Florin Corasdbd44562017-11-09 19:30:17 -0800148 kv->value = ~0ULL;
149}
150
151always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800152make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700153{
Florin Coras561af9b2017-12-09 10:19:43 -0800154 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
155 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700156}
157
Florin Corascea194d2017-10-02 00:18:51 -0700158static session_table_t *
Andreas Schultz30a28c12020-04-23 10:41:50 +0200159session_table_get_or_alloc (u8 fib_proto, u32 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700160{
Florin Corascea194d2017-10-02 00:18:51 -0700161 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700162 u32 table_index;
Andreas Schultz30a28c12020-04-23 10:41:50 +0200163 ASSERT (fib_index != ~0);
164 if (vec_len (fib_index_to_table_index[fib_proto]) > fib_index &&
165 fib_index_to_table_index[fib_proto][fib_index] != ~0)
166 {
167 table_index = fib_index_to_table_index[fib_proto][fib_index];
168 return session_table_get (table_index);
169 }
170 else
Florin Corascea194d2017-10-02 00:18:51 -0700171 {
172 st = session_table_alloc ();
173 table_index = session_table_index (st);
Andreas Schultz30a28c12020-04-23 10:41:50 +0200174 vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index,
175 ~0);
Florin Coras6c36f532017-11-03 18:32:34 -0700176 fib_index_to_table_index[fib_proto][fib_index] = table_index;
177 st->active_fib_proto = fib_proto;
Florin Corasb795bd02017-12-14 11:30:48 -0800178 session_table_init (st, fib_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700179 return st;
180 }
Florin Corascea194d2017-10-02 00:18:51 -0700181}
182
183static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700184session_table_get_or_alloc_for_connection (transport_connection_t * tc)
185{
186 u32 fib_proto;
187 fib_proto = transport_connection_fib_proto (tc);
188 return session_table_get_or_alloc (fib_proto, tc->fib_index);
189}
190
191static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700192session_table_get_for_connection (transport_connection_t * tc)
193{
194 u32 fib_proto = transport_connection_fib_proto (tc);
195 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
196 return 0;
197 return
198 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
199}
200
201static session_table_t *
202session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
203{
204 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
205 return 0;
206 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
207}
208
209u32
210session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
211{
212 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
213 return SESSION_TABLE_INVALID_INDEX;
214 return fib_index_to_table_index[fib_proto][fib_index];
215}
216
217/**
218 * Add transport connection to a session table
219 *
220 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
221 * is added to requested session table.
222 *
223 * @param tc transport connection to be added
224 * @param value value to be stored
225 *
226 * @return non-zero if failure
227 */
228int
229session_lookup_add_connection (transport_connection_t * tc, u64 value)
230{
231 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700232 session_kv4_t kv4;
233 session_kv6_t kv6;
234
Florin Corascea194d2017-10-02 00:18:51 -0700235 st = session_table_get_or_alloc_for_connection (tc);
236 if (!st)
237 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700238 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700239 {
Florin Coras04e53442017-07-16 17:12:15 -0700240 make_v4_ss_kv_from_tc (&kv4, tc);
241 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700242 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
243 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700244 }
245 else
246 {
Florin Coras04e53442017-07-16 17:12:15 -0700247 make_v6_ss_kv_from_tc (&kv6, tc);
248 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700249 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
250 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700251 }
252}
253
Florin Coras04e53442017-07-16 17:12:15 -0700254int
Florin Corascea194d2017-10-02 00:18:51 -0700255session_lookup_add_session_endpoint (u32 table_index,
256 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700257{
Florin Corascea194d2017-10-02 00:18:51 -0700258 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700259 session_kv4_t kv4;
260 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700261
Florin Corascea194d2017-10-02 00:18:51 -0700262 st = session_table_get (table_index);
263 if (!st)
264 return -1;
265 if (sep->is_ip4)
266 {
267 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
268 sep->transport_proto);
269 kv4.value = value;
270 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
271 }
272 else
273 {
274 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
275 sep->transport_proto);
276 kv6.value = value;
277 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
278 }
279}
280
281int
282session_lookup_del_session_endpoint (u32 table_index,
283 session_endpoint_t * sep)
284{
285 session_table_t *st;
286 session_kv4_t kv4;
287 session_kv6_t kv6;
288
289 st = session_table_get (table_index);
290 if (!st)
291 return -1;
292 if (sep->is_ip4)
293 {
294 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
295 sep->transport_proto);
296 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
297 }
298 else
299 {
300 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
301 sep->transport_proto);
302 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
303 }
304}
305
Florin Corasaab06042020-02-26 02:56:14 +0000306int
307session_lookup_del_session_endpoint2 (session_endpoint_t * sep)
308{
309 fib_protocol_t fib_proto;
310 session_table_t *st;
311 session_kv4_t kv4;
312 session_kv6_t kv6;
313
314 fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
315 st = session_table_get_for_fib_index (fib_proto, sep->fib_index);
316 if (!st)
317 return -1;
318 if (sep->is_ip4)
319 {
320 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
321 sep->transport_proto);
322 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
323 }
324 else
325 {
326 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
327 sep->transport_proto);
328 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
329 }
330}
331
Florin Corascea194d2017-10-02 00:18:51 -0700332/**
333 * Delete transport connection from session table
334 *
335 * @param table_index session table index
336 * @param tc transport connection to be removed
337 *
338 * @return non-zero if failure
339 */
340int
341session_lookup_del_connection (transport_connection_t * tc)
342{
343 session_table_t *st;
344 session_kv4_t kv4;
345 session_kv6_t kv6;
346
347 st = session_table_get_for_connection (tc);
348 if (!st)
349 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700350 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700351 {
Florin Coras04e53442017-07-16 17:12:15 -0700352 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700353 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700354 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700355 }
356 else
357 {
Florin Coras04e53442017-07-16 17:12:15 -0700358 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700359 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700360 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700361 }
Florin Coras04e53442017-07-16 17:12:15 -0700362}
363
364int
Florin Coras288eaab2019-02-03 15:26:14 -0800365session_lookup_del_session (session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700366{
367 transport_connection_t *ts;
Florin Coras1ee78302019-02-05 15:51:15 -0800368 ts = transport_get_connection (session_get_transport_proto (s),
369 s->connection_index, s->thread_index);
Florin Corase1e7fb82019-09-25 07:28:34 -0700370 if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200371 return 0;
Florin Corascea194d2017-10-02 00:18:51 -0700372 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700373}
374
Florin Corasdff48db2017-11-19 18:06:58 -0800375static u8
376session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700377{
Florin Corasdff48db2017-11-19 18:06:58 -0800378 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
379 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
380 return 0;
381 return 1;
382}
383
Florin Corasf8f516a2018-02-08 15:10:09 -0800384static u64
385session_lookup_action_to_handle (u32 action_index)
Florin Corasdff48db2017-11-19 18:06:58 -0800386{
387 switch (action_index)
388 {
389 case SESSION_RULES_TABLE_ACTION_DROP:
Florin Corasf8f516a2018-02-08 15:10:09 -0800390 return SESSION_DROP_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800391 case SESSION_RULES_TABLE_ACTION_ALLOW:
392 case SESSION_RULES_TABLE_INVALID_INDEX:
Florin Corasf8f516a2018-02-08 15:10:09 -0800393 return SESSION_INVALID_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800394 default:
Florin Corasf8f516a2018-02-08 15:10:09 -0800395 /* application index */
Florin Corasdff48db2017-11-19 18:06:58 -0800396 return action_index;
397 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700398}
399
Florin Coras288eaab2019-02-03 15:26:14 -0800400static session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700401session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
402 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700403{
404 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800405 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700406 if (!app)
407 return 0;
408
Florin Coras15531972018-08-12 23:50:53 -0700409 return app_worker_first_listener (application_get_default_worker (app),
410 fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700411}
412
Florin Coras288eaab2019-02-03 15:26:14 -0800413static session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800414session_lookup_action_to_session (u32 action_index, u8 fib_proto,
415 u8 transport_proto)
416{
Florin Corasdff48db2017-11-19 18:06:58 -0800417 u32 app_index;
Florin Corasf8f516a2018-02-08 15:10:09 -0800418 app_index = session_lookup_action_to_handle (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800419 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800420 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800421 transport_proto);
422}
423
Florin Corasf8f516a2018-02-08 15:10:09 -0800424/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800425session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800426session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
427 ip4_address_t * lcl, u16 lcl_port,
428 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700429{
Florin Corasc97a7392017-11-05 23:07:07 -0800430 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800431 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800432 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700433 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800434 app_index = session_lookup_action_to_handle (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700435 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800436 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700437 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700438}
439
Florin Corasf8f516a2018-02-08 15:10:09 -0800440/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800441session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800442session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
443 ip6_address_t * lcl, u16 lcl_port,
444 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700445{
Florin Corasc97a7392017-11-05 23:07:07 -0800446 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800447 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800448 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700449 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800450 app_index = session_lookup_action_to_handle (action_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800451 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700452 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700453}
454
Florin Corasa2ff7b82017-11-08 17:55:03 -0800455/**
456 * Lookup listener for session endpoint in table
457 *
458 * @param table_index table where the endpoint should be looked up
459 * @param sep session endpoint to be looked up
460 * @param use_rules flag that indicates if the session rules of the table
461 * should be used
462 * @return invalid handle if nothing is found, the handle of a valid listener
Florin Corasf8f516a2018-02-08 15:10:09 -0800463 * or an action derived handle if a rule is hit
Florin Corasa2ff7b82017-11-08 17:55:03 -0800464 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700465u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800466session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
467 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700468{
Florin Corasc97a7392017-11-05 23:07:07 -0800469 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700470 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700471 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700472 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700473
Florin Corascea194d2017-10-02 00:18:51 -0700474 st = session_table_get (table_index);
475 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700476 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700477 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700478 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800479 session_kv4_t kv4;
480 ip4_address_t lcl4;
481
Florin Coras561af9b2017-12-09 10:19:43 -0800482 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
483 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700484 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
485 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700486 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800487 if (use_rules)
488 {
Dave Barachb7b92992018-10-17 10:38:51 -0400489 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800490 srt = &st->session_rules[sep->transport_proto];
491 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
492 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800493 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800494 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800495 }
Florin Coras68810622017-07-24 17:40:28 -0700496 }
497 else
498 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800499 session_kv6_t kv6;
500 ip6_address_t lcl6;
501
Florin Coras561af9b2017-12-09 10:19:43 -0800502 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
503 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700504 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
505 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700506 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700507
Florin Corasa2ff7b82017-11-08 17:55:03 -0800508 if (use_rules)
509 {
Dave Barachb7b92992018-10-17 10:38:51 -0400510 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800511 srt = &st->session_rules[sep->transport_proto];
512 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
513 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800514 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800515 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800516 }
Florin Coras68810622017-07-24 17:40:28 -0700517 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700518 return SESSION_INVALID_HANDLE;
519}
520
Florin Corasa2ff7b82017-11-08 17:55:03 -0800521/**
522 * Look up endpoint in local session table
523 *
524 * The result, for now, is an application index and it may in the future
525 * be extended to a more complicated "action object". The only action we
526 * emulate now is "drop" and for that we return a special app index.
527 *
528 * Lookup logic is to check in order:
529 * - the rules in the table (connect acls)
530 * - session sub-table for a listener
531 * - session sub-table for a local listener (zeroed addr)
532 *
533 * @param table_index table where the lookup should be done
534 * @param sep session endpoint to be looked up
Florin Corasf8f516a2018-02-08 15:10:09 -0800535 * @return session handle that can be interpreted as an adjacency
Florin Corasa2ff7b82017-11-08 17:55:03 -0800536 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800537u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800538session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700539{
Florin Corasc97a7392017-11-05 23:07:07 -0800540 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700541 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700542 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700543 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700544
Florin Corascea194d2017-10-02 00:18:51 -0700545 st = session_table_get (table_index);
546 if (!st)
547 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800548 ASSERT (st->is_local);
549
Florin Corascea194d2017-10-02 00:18:51 -0700550 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700551 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800552 session_kv4_t kv4;
553 ip4_address_t lcl4;
554
555 /*
556 * Check if endpoint has special rules associated
557 */
Dave Barachb7b92992018-10-17 10:38:51 -0400558 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800559 srt = &st->session_rules[sep->transport_proto];
560 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
561 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800562 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800563 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800564
565 /*
566 * Check if session endpoint is a listener
567 */
Florin Corascea194d2017-10-02 00:18:51 -0700568 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
569 sep->transport_proto);
570 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
571 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800572 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700573
574 /*
575 * Zero out the ip. Logic is that connect to local ips, say
576 * 127.0.0.1:port, can match 0.0.0.0:port
577 */
Florin Coras477e91a2018-02-27 10:05:57 -0800578 if (ip4_is_local_host (&sep->ip.ip4))
579 {
580 kv4.key[0] = 0;
581 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
582 if (rv == 0)
583 return kv4.value;
584 }
585 else
586 {
587 kv4.key[0] = 0;
588 }
Florin Corasdbd44562017-11-09 19:30:17 -0800589
590 /*
591 * Zero out the port and check if we have proxy
592 */
593 kv4.key[1] = 0;
594 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
595 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800596 return kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700597 }
598 else
599 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800600 session_kv6_t kv6;
601 ip6_address_t lcl6;
602
Dave Barachb7b92992018-10-17 10:38:51 -0400603 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800604 srt = &st->session_rules[sep->transport_proto];
605 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
606 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800607 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800608 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800609
Florin Corascea194d2017-10-02 00:18:51 -0700610 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
611 sep->transport_proto);
612 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
613 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800614 return kv6.value;
Florin Corascea194d2017-10-02 00:18:51 -0700615
616 /*
617 * Zero out the ip. Same logic as above.
618 */
Florin Coras477e91a2018-02-27 10:05:57 -0800619
620 if (ip6_is_local_host (&sep->ip.ip6))
621 {
622 kv6.key[0] = kv6.key[1] = 0;
623 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
624 if (rv == 0)
625 return kv6.value;
626 }
627 else
628 {
629 kv6.key[0] = kv6.key[1] = 0;
630 }
Florin Corasdbd44562017-11-09 19:30:17 -0800631
632 /*
633 * Zero out the port. Same logic as above.
634 */
635 kv6.key[4] = kv6.key[5] = 0;
636 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
637 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800638 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700639 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800640 return SESSION_INVALID_HANDLE;
Florin Coras04e53442017-07-16 17:12:15 -0700641}
642
Florin Coras288eaab2019-02-03 15:26:14 -0800643static inline session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700644session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800645 u16 lcl_port, u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700646{
Florin Coras04e53442017-07-16 17:12:15 -0700647 session_kv4_t kv4;
648 int rv;
649
Florin Corasdbd44562017-11-09 19:30:17 -0800650 /*
651 * First, try a fully formed listener
652 */
Florin Coras04e53442017-07-16 17:12:15 -0700653 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700654 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700655 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700656 return listen_session_get ((u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700657
Florin Corasdbd44562017-11-09 19:30:17 -0800658 /*
659 * Zero out the lcl ip and check if any 0/0 port binds have been done
660 */
Florin Coras477e91a2018-02-27 10:05:57 -0800661 if (use_wildcard)
662 {
663 kv4.key[0] = 0;
664 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
665 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700666 return listen_session_get ((u32) kv4.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800667 }
668 else
669 {
670 kv4.key[0] = 0;
671 }
Florin Coras04e53442017-07-16 17:12:15 -0700672
Florin Corasdbd44562017-11-09 19:30:17 -0800673 /*
674 * Zero out port and check if we have a proxy set up for our ip
675 */
676 make_v4_proxy_kv (&kv4, lcl, proto);
677 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
678 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700679 return listen_session_get ((u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800680
Florin Coras04e53442017-07-16 17:12:15 -0700681 return 0;
682}
683
Florin Coras288eaab2019-02-03 15:26:14 -0800684session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700685session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800686 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700687{
Florin Corascea194d2017-10-02 00:18:51 -0700688 session_table_t *st;
689 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
690 if (!st)
691 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800692 return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700693}
694
Florin Coras288eaab2019-02-03 15:26:14 -0800695static session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700696session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800697 u16 lcl_port, u8 proto, u8 ip_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700698{
Florin Coras04e53442017-07-16 17:12:15 -0700699 session_kv6_t kv6;
700 int rv;
701
702 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700703 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700704 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700705 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700706
707 /* Zero out the lcl ip */
Florin Coras477e91a2018-02-27 10:05:57 -0800708 if (ip_wildcard)
709 {
710 kv6.key[0] = kv6.key[1] = 0;
711 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
712 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700713 return listen_session_get ((u32) kv6.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800714 }
715 else
716 {
717 kv6.key[0] = kv6.key[1] = 0;
718 }
Florin Coras04e53442017-07-16 17:12:15 -0700719
Florin Corasdbd44562017-11-09 19:30:17 -0800720 make_v6_proxy_kv (&kv6, lcl, proto);
721 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
722 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700723 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700724 return 0;
725}
726
Florin Coras288eaab2019-02-03 15:26:14 -0800727session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700728session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800729 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700730{
Florin Corascea194d2017-10-02 00:18:51 -0700731 session_table_t *st;
732 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
733 if (!st)
734 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800735 return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700736}
737
Florin Coras477e91a2018-02-27 10:05:57 -0800738/**
739 * Lookup listener, exact or proxy (inaddr_any:0) match
740 */
Florin Coras288eaab2019-02-03 15:26:14 -0800741session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700742session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700743{
Florin Corascea194d2017-10-02 00:18:51 -0700744 session_table_t *st;
745 st = session_table_get (table_index);
746 if (!st)
747 return 0;
748 if (sep->is_ip4)
749 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800750 sep->transport_proto, 0);
Florin Corascea194d2017-10-02 00:18:51 -0700751 else
752 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800753 sep->transport_proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700754 return 0;
755}
756
Florin Coras9f1a5432019-03-10 21:03:20 -0700757/**
758 * Lookup listener wildcard match
759 */
760session_t *
761session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep)
762{
763 session_table_t *st;
764 st = session_table_get (table_index);
765 if (!st)
766 return 0;
767 if (sep->is_ip4)
768 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
769 sep->transport_proto,
770 1 /* use_wildcard */ );
771 else
772 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
773 sep->transport_proto,
774 1 /* use_wildcard */ );
775 return 0;
776}
777
Florin Corascea194d2017-10-02 00:18:51 -0700778int
779session_lookup_add_half_open (transport_connection_t * tc, u64 value)
780{
781 session_table_t *st;
782 session_kv4_t kv4;
783 session_kv6_t kv6;
784
785 st = session_table_get_or_alloc_for_connection (tc);
786 if (!st)
787 return 0;
788 if (tc->is_ip4)
789 {
790 make_v4_ss_kv_from_tc (&kv4, tc);
791 kv4.value = value;
792 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
793 1 /* is_add */ );
794 }
795 else
796 {
797 make_v6_ss_kv_from_tc (&kv6, tc);
798 kv6.value = value;
799 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
800 1 /* is_add */ );
801 }
802}
803
804int
805session_lookup_del_half_open (transport_connection_t * tc)
806{
807 session_table_t *st;
808 session_kv4_t kv4;
809 session_kv6_t kv6;
810
811 st = session_table_get_for_connection (tc);
812 if (!st)
813 return -1;
814 if (tc->is_ip4)
815 {
816 make_v4_ss_kv_from_tc (&kv4, tc);
817 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
818 0 /* is_add */ );
819 }
820 else
821 {
822 make_v6_ss_kv_from_tc (&kv6, tc);
823 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
824 0 /* is_add */ );
825 }
826}
827
Florin Coras04e53442017-07-16 17:12:15 -0700828u64
Florin Corascea194d2017-10-02 00:18:51 -0700829session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700830{
Florin Corascea194d2017-10-02 00:18:51 -0700831 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700832 session_kv4_t kv4;
833 session_kv6_t kv6;
834 int rv;
835
Florin Corascea194d2017-10-02 00:18:51 -0700836 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
837 tc->fib_index);
838 if (!st)
839 return HALF_OPEN_LOOKUP_INVALID_VALUE;
840 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700841 {
Florin Corascea194d2017-10-02 00:18:51 -0700842 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700843 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700844 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700845 if (rv == 0)
846 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700847 }
848 else
849 {
850 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700851 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700852 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700853 if (rv == 0)
854 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700855 }
856 return HALF_OPEN_LOOKUP_INVALID_VALUE;
857}
858
859transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700860session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700861{
Florin Coras04e53442017-07-16 17:12:15 -0700862 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700863 {
Florin Coras1ee78302019-02-05 15:51:15 -0800864 u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
865 return transport_get_half_open (sst, handle & 0xFFFFFFFF);
Florin Corascea194d2017-10-02 00:18:51 -0700866 }
Florin Coras04e53442017-07-16 17:12:15 -0700867 return 0;
868}
869
Florin Corascea194d2017-10-02 00:18:51 -0700870/**
871 * Lookup connection with ip4 and transport layer information
872 *
873 * This is used on the fast path so it needs to be fast. Thereby,
874 * duplication of code and 'hacks' allowed.
875 *
876 * The lookup is incremental and returns whenever something is matched. The
877 * steps are:
878 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700879 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700880 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800881 * - Try to find a fully-formed or local source wildcarded (listener bound to
882 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700883 * - return 0
884 *
885 * @param fib_index index of fib wherein the connection was received
886 * @param lcl local ip4 address
887 * @param rmt remote ip4 address
888 * @param lcl_port local port
889 * @param rmt_port remote port
890 * @param proto transport protocol (e.g., tcp, udp)
891 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800892 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700893 *
894 * @return pointer to transport connection, if one is found, 0 otherwise
895 */
Florin Coras04e53442017-07-16 17:12:15 -0700896transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700897session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
898 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800899 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -0800900 u8 * result)
Florin Coras04e53442017-07-16 17:12:15 -0700901{
Florin Corascea194d2017-10-02 00:18:51 -0700902 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700903 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800904 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800905 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700906 int rv;
907
Florin Corascea194d2017-10-02 00:18:51 -0700908 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
909 if (PREDICT_FALSE (!st))
910 return 0;
911
Florin Corasa2ff7b82017-11-08 17:55:03 -0800912 /*
913 * Lookup session amongst established ones
914 */
Florin Coras04e53442017-07-16 17:12:15 -0700915 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700916 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700917 if (rv == 0)
918 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800919 if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
920 {
921 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
922 return 0;
923 }
Florin Corascea194d2017-10-02 00:18:51 -0700924 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -0800925 return transport_get_connection (proto, s->connection_index,
926 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700927 }
928
Florin Corasa2ff7b82017-11-08 17:55:03 -0800929 /*
930 * Try half-open connections
931 */
Florin Corascea194d2017-10-02 00:18:51 -0700932 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700933 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -0800934 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700935
Florin Corasa2ff7b82017-11-08 17:55:03 -0800936 /*
937 * Check the session rules table
938 */
939 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
940 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800941 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800942 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800943 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
944 {
945 *result = SESSION_LOOKUP_RESULT_FILTERED;
946 return 0;
947 }
Florin Corasdff48db2017-11-19 18:06:58 -0800948 if ((s = session_lookup_action_to_session (action_index,
949 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -0800950 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800951 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800952 }
953
954 /*
955 * If nothing is found, check if any listener is available
956 */
Florin Coras477e91a2018-02-27 10:05:57 -0800957 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800958 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -0800959 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800960
961 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700962}
963
Florin Corascea194d2017-10-02 00:18:51 -0700964/**
965 * Lookup connection with ip4 and transport layer information
966 *
Florin Corasb5e55a22019-01-10 12:42:47 -0800967 * Not optimized. Lookup logic is identical to that of
968 * @ref session_lookup_connection_wt4
Florin Corascea194d2017-10-02 00:18:51 -0700969 *
970 * @param fib_index index of the fib wherein the connection was received
971 * @param lcl local ip4 address
972 * @param rmt remote ip4 address
973 * @param lcl_port local port
974 * @param rmt_port remote port
975 * @param proto transport protocol (e.g., tcp, udp)
976 *
977 * @return pointer to transport connection, if one is found, 0 otherwise
978 */
Florin Coras04e53442017-07-16 17:12:15 -0700979transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700980session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
981 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
982 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700983{
Florin Corascea194d2017-10-02 00:18:51 -0700984 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700985 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800986 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800987 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700988 int rv;
989
Florin Corascea194d2017-10-02 00:18:51 -0700990 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
991 if (PREDICT_FALSE (!st))
992 return 0;
993
Florin Corasa2ff7b82017-11-08 17:55:03 -0800994 /*
995 * Lookup session amongst established ones
996 */
Florin Coras04e53442017-07-16 17:12:15 -0700997 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700998 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700999 if (rv == 0)
1000 {
Florin Corascea194d2017-10-02 00:18:51 -07001001 s = session_get_from_handle (kv4.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001002 return transport_get_connection (proto, s->connection_index,
1003 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001004 }
1005
Florin Corasa2ff7b82017-11-08 17:55:03 -08001006 /*
1007 * Try half-open connections
1008 */
Florin Corascea194d2017-10-02 00:18:51 -07001009 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001010 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001011 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001012
1013 /*
1014 * Check the session rules table
1015 */
1016 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1017 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001018 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001019 {
Florin Corasdff48db2017-11-19 18:06:58 -08001020 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1021 return 0;
1022 if ((s = session_lookup_action_to_session (action_index,
1023 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001024 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001025 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001026 }
1027
1028 /*
1029 * If nothing is found, check if any listener is available
1030 */
Florin Coras477e91a2018-02-27 10:05:57 -08001031 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001032 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001033 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001034
1035 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001036}
1037
Florin Corascea194d2017-10-02 00:18:51 -07001038/**
1039 * Lookup session with ip4 and transport layer information
1040 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001041 * Important note: this may look into another thread's pool table and
1042 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1043 * if needed as soon as possible.
1044 *
1045 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
1046 * this returns a session as opposed to a transport connection and it does not
1047 * try to lookup half-open sessions.
1048 *
1049 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001050 */
Florin Coras288eaab2019-02-03 15:26:14 -08001051session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001052session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1053 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001054{
Florin Corascea194d2017-10-02 00:18:51 -07001055 session_table_t *st;
1056 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001057 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001058 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001059 int rv;
1060
1061 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1062 if (PREDICT_FALSE (!st))
1063 return 0;
1064
Florin Corasa2ff7b82017-11-08 17:55:03 -08001065 /*
1066 * Lookup session amongst established ones
1067 */
Florin Corascea194d2017-10-02 00:18:51 -07001068 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1069 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1070 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001071 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001072
Florin Corasa2ff7b82017-11-08 17:55:03 -08001073 /*
1074 * Check the session rules table
1075 */
1076 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1077 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001078 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001079 {
Florin Corasdff48db2017-11-19 18:06:58 -08001080 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1081 return 0;
1082 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1083 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001084 }
1085
1086 /*
1087 * If nothing is found, check if any listener is available
1088 */
Florin Coras477e91a2018-02-27 10:05:57 -08001089 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001090 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001091
1092 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001093}
1094
1095/**
1096 * Lookup connection with ip6 and transport layer information
1097 *
1098 * This is used on the fast path so it needs to be fast. Thereby,
1099 * duplication of code and 'hacks' allowed.
1100 *
1101 * The lookup is incremental and returns whenever something is matched. The
1102 * steps are:
1103 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001104 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001105 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001106 * - Try to find a fully-formed or local source wildcarded (listener bound to
1107 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001108 * - return 0
1109 *
1110 * @param fib_index index of the fib wherein the connection was received
1111 * @param lcl local ip6 address
1112 * @param rmt remote ip6 address
1113 * @param lcl_port local port
1114 * @param rmt_port remote port
1115 * @param proto transport protocol (e.g., tcp, udp)
1116 * @param thread_index thread index for request
1117 *
1118 * @return pointer to transport connection, if one is found, 0 otherwise
1119 */
1120transport_connection_t *
1121session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1122 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001123 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -08001124 u8 * result)
Florin Corascea194d2017-10-02 00:18:51 -07001125{
1126 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001127 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001128 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001129 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001130 int rv;
1131
Florin Corascea194d2017-10-02 00:18:51 -07001132 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1133 if (PREDICT_FALSE (!st))
1134 return 0;
1135
Florin Coras04e53442017-07-16 17:12:15 -07001136 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001137 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001138 if (rv == 0)
1139 {
Florin Corascea194d2017-10-02 00:18:51 -07001140 ASSERT ((u32) (kv6.value >> 32) == thread_index);
Florin Corasb5e55a22019-01-10 12:42:47 -08001141 if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1142 {
1143 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1144 return 0;
1145 }
Florin Corascea194d2017-10-02 00:18:51 -07001146 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -08001147 return transport_get_connection (proto, s->connection_index,
1148 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001149 }
1150
Florin Corasa2ff7b82017-11-08 17:55:03 -08001151 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001152 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001153 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001154 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001155
Florin Corasa2ff7b82017-11-08 17:55:03 -08001156 /* Check the session rules table */
1157 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1158 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001159 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001160 {
Florin Corasb5e55a22019-01-10 12:42:47 -08001161 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1162 {
1163 *result = SESSION_LOOKUP_RESULT_FILTERED;
1164 return 0;
1165 }
Florin Corasdff48db2017-11-19 18:06:58 -08001166 if ((s = session_lookup_action_to_session (action_index,
1167 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001168 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001169 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001170 }
1171
1172 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001173 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001174 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001175 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001176
1177 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001178}
1179
Florin Corascea194d2017-10-02 00:18:51 -07001180/**
1181 * Lookup connection with ip6 and transport layer information
1182 *
1183 * Not optimized. This is used on the fast path so it needs to be fast.
1184 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1185 * to that of @ref session_lookup_connection_wt4
1186 *
1187 * @param fib_index index of the fib wherein the connection was received
1188 * @param lcl local ip6 address
1189 * @param rmt remote ip6 address
1190 * @param lcl_port local port
1191 * @param rmt_port remote port
1192 * @param proto transport protocol (e.g., tcp, udp)
1193 *
1194 * @return pointer to transport connection, if one is found, 0 otherwise
1195 */
Florin Coras04e53442017-07-16 17:12:15 -07001196transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001197session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1198 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1199 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001200{
Florin Corascea194d2017-10-02 00:18:51 -07001201 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001202 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001203 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001204 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001205 int rv;
1206
Florin Corascea194d2017-10-02 00:18:51 -07001207 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1208 if (PREDICT_FALSE (!st))
1209 return 0;
1210
Florin Coras04e53442017-07-16 17:12:15 -07001211 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001212 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001213 if (rv == 0)
1214 {
Florin Corascea194d2017-10-02 00:18:51 -07001215 s = session_get_from_handle (kv6.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001216 return transport_get_connection (proto, s->connection_index,
1217 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001218 }
1219
Florin Corasa2ff7b82017-11-08 17:55:03 -08001220 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001221 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001222 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001223 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001224
Florin Corasa2ff7b82017-11-08 17:55:03 -08001225 /* Check the session rules table */
1226 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1227 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001228 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001229 {
Florin Corasdff48db2017-11-19 18:06:58 -08001230 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1231 return 0;
1232 if ((s = session_lookup_action_to_session (action_index,
1233 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001234 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001235 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001236 }
1237
1238 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001239 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001240 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001241 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001242
1243 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001244}
1245
Florin Corascea194d2017-10-02 00:18:51 -07001246/**
1247 * Lookup session with ip6 and transport layer information
1248 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001249 * Important note: this may look into another thread's pool table and
1250 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1251 * if needed as soon as possible.
1252 *
1253 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1254 * this returns a session as opposed to a transport connection and it does not
1255 * try to lookup half-open sessions.
1256 *
1257 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001258 */
Florin Coras288eaab2019-02-03 15:26:14 -08001259session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001260session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1261 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001262{
1263 session_table_t *st;
1264 session_kv6_t kv6;
Florin Coras288eaab2019-02-03 15:26:14 -08001265 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001266 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001267 int rv;
1268
1269 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1270 if (PREDICT_FALSE (!st))
1271 return 0;
1272
1273 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1274 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1275 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001276 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001277
Florin Corasa2ff7b82017-11-08 17:55:03 -08001278 /* Check the session rules table */
1279 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1280 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001281 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001282 {
Florin Corasdff48db2017-11-19 18:06:58 -08001283 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1284 return 0;
1285 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1286 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001287 }
1288
Florin Corascea194d2017-10-02 00:18:51 -07001289 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001290 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001291 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001292 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001293}
1294
Florin Coras57660d92020-04-04 22:45:34 +00001295transport_connection_t *
1296session_lookup_connection (u32 fib_index, ip46_address_t * lcl,
1297 ip46_address_t * rmt, u16 lcl_port, u16 rmt_port,
1298 u8 proto, u8 is_ip4)
1299{
1300 if (is_ip4)
1301 return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4,
1302 lcl_port, rmt_port, proto);
1303 else
1304 return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6,
1305 lcl_port, rmt_port, proto);
1306}
1307
Florin Corasc1a42652019-02-08 18:27:29 -08001308int
Florin Coras1c710452017-10-17 00:03:13 -07001309vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1310{
1311 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001312 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001313 session_table_t *st;
1314 u32 fib_index;
1315 u8 fib_proto;
Florin Corasc1a42652019-02-08 18:27:29 -08001316 int rv = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001317
1318 if (!app_ns)
Florin Corasc1a42652019-02-08 18:27:29 -08001319 return VNET_API_ERROR_APP_INVALID_NS;
1320
Florin Coras1c710452017-10-17 00:03:13 -07001321 if (args->scope > 3)
Florin Corasc1a42652019-02-08 18:27:29 -08001322 return VNET_API_ERROR_INVALID_VALUE;
1323
Florin Corasc97a7392017-11-05 23:07:07 -08001324 if (args->transport_proto != TRANSPORT_PROTO_TCP
1325 && args->transport_proto != TRANSPORT_PROTO_UDP)
Florin Corasc1a42652019-02-08 18:27:29 -08001326 return VNET_API_ERROR_INVALID_VALUE;
1327
Florin Coras1c710452017-10-17 00:03:13 -07001328 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1329 {
1330 fib_proto = args->table_args.rmt.fp_proto;
1331 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1332 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001333 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001334 if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1335 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001336 }
1337 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1338 {
Dave Barachb7b92992018-10-17 10:38:51 -04001339 clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
Florin Corasa2ff7b82017-11-08 17:55:03 -08001340 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1341 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001342 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001343 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001344 rv = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001345 }
Florin Corasc1a42652019-02-08 18:27:29 -08001346 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001347}
1348
Florin Coras6c36f532017-11-03 18:32:34 -07001349/**
1350 * Mark (global) tables as pertaining to app ns
1351 */
1352void
1353session_lookup_set_tables_appns (app_namespace_t * app_ns)
1354{
1355 session_table_t *st;
1356 u32 fib_index;
1357 u8 fp;
1358
1359 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1360 {
1361 fib_index = app_namespace_get_fib_index (app_ns, fp);
Florin Coras95cd8642019-12-30 21:53:19 -08001362 st = session_table_get_or_alloc (fp, fib_index);
Florin Coras6c36f532017-11-03 18:32:34 -07001363 if (st)
1364 st->appns_index = app_namespace_index (app_ns);
1365 }
1366}
1367
Florin Corascea194d2017-10-02 00:18:51 -07001368u8 *
1369format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1370{
1371 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001372 u32 is_local = va_arg (*args, u32);
Florin Coras15531972018-08-12 23:50:53 -07001373 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
Florin Coras288eaab2019-02-03 15:26:14 -08001374 session_t *session;
Florin Coras15531972018-08-12 23:50:53 -07001375 app_worker_t *app_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -08001376 const u8 *app_name;
1377 u8 *str = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001378
Florin Corascea194d2017-10-02 00:18:51 -07001379 if (!is_local)
1380 {
1381 session = session_get_from_handle (kvp->value);
Florin Coras15531972018-08-12 23:50:53 -07001382 app_wrk = app_worker_get (session->app_wrk_index);
1383 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001384 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1385 key->proto, format_ip4_address, &key->src,
1386 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1387 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001388 s = format (s, "%-40v%-30v", str, app_name);
1389 }
1390 else
1391 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001392 session = session_get_from_handle (kvp->value);
1393 app_wrk = app_worker_get (session->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -07001394 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001395 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1396 format_ip4_address, &key->src,
1397 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001398 s = format (s, "%-30v%-30v", str, app_name);
1399 }
Florin Corascea194d2017-10-02 00:18:51 -07001400 return s;
1401}
1402
1403typedef struct _ip4_session_table_show_ctx_t
1404{
1405 vlib_main_t *vm;
1406 u8 is_local;
1407} ip4_session_table_show_ctx_t;
1408
1409static int
1410ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1411{
1412 ip4_session_table_show_ctx_t *ctx = arg;
1413 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1414 ctx->is_local);
1415 return 1;
1416}
1417
1418void
1419session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1420 u8 type, u8 is_local)
1421{
1422 ip4_session_table_show_ctx_t ctx = {
1423 .vm = vm,
1424 .is_local = is_local,
1425 };
1426 if (!is_local)
1427 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1428 else
1429 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1430 switch (type)
1431 {
1432 /* main table v4 */
1433 case 0:
1434 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1435 &ctx);
1436 break;
1437 default:
1438 clib_warning ("not supported");
1439 }
1440}
Florin Coras66b11312017-07-31 17:18:03 -07001441
Florin Coras1c710452017-10-17 00:03:13 -07001442static clib_error_t *
1443session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1444 vlib_cli_command_t * cmd)
1445{
Florin Corasc97a7392017-11-05 23:07:07 -08001446 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001447 u32 appns_index, scope = 0;
1448 ip46_address_t lcl_ip, rmt_ip;
1449 u8 is_ip4 = 1, conn_set = 0;
1450 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001451 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001452 app_namespace_t *app_ns;
Florin Corasc1a42652019-02-08 18:27:29 -08001453 int rv;
Florin Coras1c710452017-10-17 00:03:13 -07001454
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001455 session_cli_return_if_not_enabled ();
1456
Dave Barachb7b92992018-10-17 10:38:51 -04001457 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1458 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001459 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1460 {
1461 if (unformat (input, "del"))
1462 is_add = 0;
1463 else if (unformat (input, "add"))
1464 ;
1465 else if (unformat (input, "appns %_%v%_", &ns_id))
1466 ;
1467 else if (unformat (input, "scope global"))
1468 scope = SESSION_RULE_SCOPE_GLOBAL;
1469 else if (unformat (input, "scope local"))
1470 scope = SESSION_RULE_SCOPE_LOCAL;
1471 else if (unformat (input, "scope all"))
1472 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1473 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1474 ;
1475 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1476 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1477 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1478 &rmt_port))
1479 {
1480 is_ip4 = 1;
1481 conn_set = 1;
1482 }
1483 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1484 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1485 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1486 &rmt_port))
1487 {
1488 is_ip4 = 0;
1489 conn_set = 1;
1490 }
1491 else if (unformat (input, "action %d", &action))
1492 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001493 else if (unformat (input, "tag %_%v%_", &tag))
1494 ;
Florin Coras1c710452017-10-17 00:03:13 -07001495 else
1496 return clib_error_return (0, "unknown input `%U'",
1497 format_unformat_error, input);
1498 }
1499
Florin Corasc97a7392017-11-05 23:07:07 -08001500 if (proto == ~0)
1501 {
1502 vlib_cli_output (vm, "proto must be set");
1503 return 0;
1504 }
1505 if (is_add && !conn_set && action == ~0)
1506 {
1507 vlib_cli_output (vm, "connection and action must be set for add");
1508 return 0;
1509 }
1510 if (!is_add && !tag && !conn_set)
1511 {
1512 vlib_cli_output (vm, "connection or tag must be set for delete");
1513 return 0;
1514 }
1515 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1516 {
1517 vlib_cli_output (vm, "tag too long (max u64)");
1518 return 0;
1519 }
Florin Coras1c710452017-10-17 00:03:13 -07001520
1521 if (ns_id)
1522 {
1523 app_ns = app_namespace_get_from_id (ns_id);
1524 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001525 {
1526 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1527 return 0;
1528 }
Florin Coras1c710452017-10-17 00:03:13 -07001529 }
1530 else
1531 {
1532 app_ns = app_namespace_get_default ();
1533 }
1534 appns_index = app_namespace_index (app_ns);
1535
1536 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1537 session_rule_add_del_args_t args = {
zhanglimaoa04ebb82019-03-13 10:36:54 +08001538 .transport_proto = proto,
Florin Coras1c710452017-10-17 00:03:13 -07001539 .table_args.lcl.fp_addr = lcl_ip,
1540 .table_args.lcl.fp_len = lcl_plen,
1541 .table_args.lcl.fp_proto = fib_proto,
1542 .table_args.rmt.fp_addr = rmt_ip,
1543 .table_args.rmt.fp_len = rmt_plen,
1544 .table_args.rmt.fp_proto = fib_proto,
1545 .table_args.lcl_port = lcl_port,
1546 .table_args.rmt_port = rmt_port,
1547 .table_args.action_index = action,
1548 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001549 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001550 .appns_index = appns_index,
1551 .scope = scope,
1552 };
Florin Corasc1a42652019-02-08 18:27:29 -08001553 if ((rv = vnet_session_rule_add_del (&args)))
1554 return clib_error_return (0, "rule add del returned %u", rv);
1555
Florin Corasc97a7392017-11-05 23:07:07 -08001556 vec_free (tag);
Florin Corasc1a42652019-02-08 18:27:29 -08001557 return 0;
Florin Coras1c710452017-10-17 00:03:13 -07001558}
1559
1560/* *INDENT-OFF* */
1561VLIB_CLI_COMMAND (session_rule_command, static) =
1562{
1563 .path = "session rule",
1564 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1565 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1566 .function = session_rule_command_fn,
1567};
1568/* *INDENT-ON* */
1569
Florin Coras7999e832017-10-31 01:51:04 -07001570void
1571session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1572 u8 transport_proto)
1573{
1574 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001575 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001576 session_table_t *st;
1577 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001578 srt = &st->session_rules[transport_proto];
1579 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001580}
1581
1582void
1583session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1584 u8 transport_proto)
1585{
1586 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001587 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001588 session_table_t *st;
1589 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001590 srt = &st->session_rules[transport_proto];
1591 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001592}
1593
Florin Coras1c710452017-10-17 00:03:13 -07001594static clib_error_t *
1595show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1596 vlib_cli_command_t * cmd)
1597{
1598 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1599 u32 fib_index, scope = 0;
1600 ip46_address_t lcl_ip, rmt_ip;
1601 u8 is_ip4 = 1, show_one = 0;
1602 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001603 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001604 session_table_t *st;
1605 u8 *ns_id = 0, fib_proto;
1606
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001607 session_cli_return_if_not_enabled ();
1608
Dave Barachb7b92992018-10-17 10:38:51 -04001609 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1610 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001611 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1612 {
1613 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1614 ;
1615 else if (unformat (input, "appns %_%v%_", &ns_id))
1616 ;
1617 else if (unformat (input, "scope global"))
1618 scope = 1;
1619 else if (unformat (input, "scope local"))
1620 scope = 2;
1621 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1622 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1623 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1624 &rmt_port))
1625 {
1626 is_ip4 = 1;
1627 show_one = 1;
1628 }
1629 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1630 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1631 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1632 &rmt_port))
1633 {
1634 is_ip4 = 0;
1635 show_one = 1;
1636 }
1637 else
1638 return clib_error_return (0, "unknown input `%U'",
1639 format_unformat_error, input);
1640 }
1641
1642 if (transport_proto == ~0)
1643 {
1644 vlib_cli_output (vm, "transport proto must be set");
1645 return 0;
1646 }
1647
1648 if (ns_id)
1649 {
1650 app_ns = app_namespace_get_from_id (ns_id);
1651 if (!app_ns)
1652 {
1653 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1654 return 0;
1655 }
1656 }
1657 else
1658 {
1659 app_ns = app_namespace_get_default ();
1660 }
1661
1662 if (scope == 1 || scope == 0)
1663 {
1664 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1665 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1666 st = session_table_get_for_fib_index (fib_proto, fib_index);
1667 }
1668 else
1669 {
1670 st = app_namespace_get_local_table (app_ns);
1671 }
1672
1673 if (show_one)
1674 {
Florin Corasc97a7392017-11-05 23:07:07 -08001675 srt = &st->session_rules[transport_proto];
1676 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1677 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001678 return 0;
1679 }
1680
Florin Corasc97a7392017-11-05 23:07:07 -08001681 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1682 transport_proto);
1683 srt = &st->session_rules[transport_proto];
1684 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1685 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001686
1687 vec_free (ns_id);
1688 return 0;
1689}
1690
1691/* *INDENT-OFF* */
1692VLIB_CLI_COMMAND (show_session_rules_command, static) =
1693{
1694 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001695 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1696 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001697 .function = show_session_rules_command_fn,
1698};
1699/* *INDENT-ON* */
1700
Florin Coras04e53442017-07-16 17:12:15 -07001701void
1702session_lookup_init (void)
1703{
Florin Corascea194d2017-10-02 00:18:51 -07001704 /*
1705 * Allocate default table and map it to fib_index 0
1706 */
1707 session_table_t *st = session_table_alloc ();
1708 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1709 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001710 st->active_fib_proto = FIB_PROTOCOL_IP4;
1711 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001712 st = session_table_alloc ();
1713 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1714 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001715 st->active_fib_proto = FIB_PROTOCOL_IP6;
1716 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001717}
1718
1719/*
1720 * fd.io coding-style-patch-verification: ON
1721 *
1722 * Local Variables:
1723 * eval: (c-set-style "gnu")
1724 * End:
1725 */