blob: 68886a09456a2c71994c30461e0eeac4f98b7fa7 [file] [log] [blame]
Florin Coras04e53442017-07-16 17:12:15 -07001/*
2 * Copyright (c) 2017 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/** 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/**
33 * External vector of per transport virtual functions table
34 */
Florin Coras04e53442017-07-16 17:12:15 -070035extern transport_proto_vft_t *tp_vfts;
36
Florin Corascea194d2017-10-02 00:18:51 -070037/**
38 * Network namespace index (i.e., fib index) to session lookup table. We
39 * should have one per network protocol type but for now we only support IP4/6
40 */
41static u32 *fib_index_to_table_index[2];
42
Florin Coras04e53442017-07-16 17:12:15 -070043/* *INDENT-OFF* */
44/* 16 octets */
45typedef CLIB_PACKED (struct {
46 union
47 {
48 struct
49 {
50 ip4_address_t src;
51 ip4_address_t dst;
52 u16 src_port;
53 u16 dst_port;
54 /* align by making this 4 octets even though its a 1-bit field
55 * NOTE: avoid key overlap with other transports that use 5 tuples for
56 * session identification.
57 */
58 u32 proto;
59 };
60 u64 as_u64[2];
61 };
62}) v4_connection_key_t;
63
64typedef CLIB_PACKED (struct {
65 union
66 {
67 struct
68 {
69 /* 48 octets */
70 ip6_address_t src;
71 ip6_address_t dst;
72 u16 src_port;
73 u16 dst_port;
74 u32 proto;
75 u64 unused;
76 };
77 u64 as_u64[6];
78 };
79}) v6_connection_key_t;
80/* *INDENT-ON* */
81
82typedef clib_bihash_kv_16_8_t session_kv4_t;
83typedef clib_bihash_kv_48_8_t session_kv6_t;
84
85always_inline void
86make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
87 u16 lcl_port, u16 rmt_port, u8 proto)
88{
89 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
90
91 key->src.as_u32 = lcl->as_u32;
92 key->dst.as_u32 = rmt->as_u32;
93 key->src_port = lcl_port;
94 key->dst_port = rmt_port;
95 key->proto = proto;
96
97 kv->value = ~0ULL;
98}
99
100always_inline void
101make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
102 u8 proto)
103{
104 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
105
106 key->src.as_u32 = lcl->as_u32;
107 key->dst.as_u32 = 0;
108 key->src_port = lcl_port;
109 key->dst_port = 0;
110 key->proto = proto;
111
112 kv->value = ~0ULL;
113}
114
115always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800116make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
117{
118 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
119
120 key->src.as_u32 = lcl->as_u32;
121 key->dst.as_u32 = 0;
122 key->src_port = 0;
123 key->dst_port = 0;
124 key->proto = proto;
125
126 kv->value = ~0ULL;
127}
128
129always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800130make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700131{
Florin Coras561af9b2017-12-09 10:19:43 -0800132 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
133 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700134}
135
136always_inline void
137make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
138 u16 lcl_port, u16 rmt_port, u8 proto)
139{
140 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
141
142 key->src.as_u64[0] = lcl->as_u64[0];
143 key->src.as_u64[1] = lcl->as_u64[1];
144 key->dst.as_u64[0] = rmt->as_u64[0];
145 key->dst.as_u64[1] = rmt->as_u64[1];
146 key->src_port = lcl_port;
147 key->dst_port = rmt_port;
148 key->proto = proto;
149 key->unused = 0;
150
151 kv->value = ~0ULL;
152}
153
154always_inline void
155make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
156 u8 proto)
157{
158 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
159
160 key->src.as_u64[0] = lcl->as_u64[0];
161 key->src.as_u64[1] = lcl->as_u64[1];
162 key->dst.as_u64[0] = 0;
163 key->dst.as_u64[1] = 0;
164 key->src_port = lcl_port;
165 key->dst_port = 0;
166 key->proto = proto;
167 key->unused = 0;
168
169 kv->value = ~0ULL;
170}
171
172always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800173make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
174{
175 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
176
177 key->src.as_u64[0] = lcl->as_u64[0];
178 key->src.as_u64[1] = lcl->as_u64[1];
179 key->dst.as_u64[0] = 0;
180 key->dst.as_u64[1] = 0;
181 key->src_port = 0;
182 key->dst_port = 0;
183 key->proto = proto;
184 key->unused = 0;
185
186 kv->value = ~0ULL;
187}
188
189always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800190make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700191{
Florin Coras561af9b2017-12-09 10:19:43 -0800192 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
193 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700194}
195
Florin Corascea194d2017-10-02 00:18:51 -0700196static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700197session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700198{
Florin Corascea194d2017-10-02 00:18:51 -0700199 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700200 u32 table_index;
201 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
Florin Corascea194d2017-10-02 00:18:51 -0700202 {
203 st = session_table_alloc ();
204 table_index = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -0700205 vec_validate (fib_index_to_table_index[fib_proto], fib_index);
206 fib_index_to_table_index[fib_proto][fib_index] = table_index;
207 st->active_fib_proto = fib_proto;
Florin Corascea194d2017-10-02 00:18:51 -0700208 return st;
209 }
210 else
211 {
Florin Coras6c36f532017-11-03 18:32:34 -0700212 table_index = fib_index_to_table_index[fib_proto][fib_index];
Florin Corascea194d2017-10-02 00:18:51 -0700213 return session_table_get (table_index);
214 }
215}
216
217static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700218session_table_get_or_alloc_for_connection (transport_connection_t * tc)
219{
220 u32 fib_proto;
221 fib_proto = transport_connection_fib_proto (tc);
222 return session_table_get_or_alloc (fib_proto, tc->fib_index);
223}
224
225static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700226session_table_get_for_connection (transport_connection_t * tc)
227{
228 u32 fib_proto = transport_connection_fib_proto (tc);
229 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
230 return 0;
231 return
232 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
233}
234
235static session_table_t *
236session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
237{
238 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
239 return 0;
240 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
241}
242
243u32
244session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
245{
246 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
247 return SESSION_TABLE_INVALID_INDEX;
248 return fib_index_to_table_index[fib_proto][fib_index];
249}
250
251/**
252 * Add transport connection to a session table
253 *
254 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
255 * is added to requested session table.
256 *
257 * @param tc transport connection to be added
258 * @param value value to be stored
259 *
260 * @return non-zero if failure
261 */
262int
263session_lookup_add_connection (transport_connection_t * tc, u64 value)
264{
265 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700266 session_kv4_t kv4;
267 session_kv6_t kv6;
268
Florin Corascea194d2017-10-02 00:18:51 -0700269 st = session_table_get_or_alloc_for_connection (tc);
270 if (!st)
271 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700272 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700273 {
Florin Coras04e53442017-07-16 17:12:15 -0700274 make_v4_ss_kv_from_tc (&kv4, tc);
275 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700276 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
277 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700278 }
279 else
280 {
Florin Coras04e53442017-07-16 17:12:15 -0700281 make_v6_ss_kv_from_tc (&kv6, tc);
282 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700283 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
284 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700285 }
286}
287
Florin Coras04e53442017-07-16 17:12:15 -0700288int
Florin Corascea194d2017-10-02 00:18:51 -0700289session_lookup_add_session_endpoint (u32 table_index,
290 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700291{
Florin Corascea194d2017-10-02 00:18:51 -0700292 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700293 session_kv4_t kv4;
294 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700295
Florin Corascea194d2017-10-02 00:18:51 -0700296 st = session_table_get (table_index);
297 if (!st)
298 return -1;
299 if (sep->is_ip4)
300 {
301 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
302 sep->transport_proto);
303 kv4.value = value;
304 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
305 }
306 else
307 {
308 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
309 sep->transport_proto);
310 kv6.value = value;
311 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
312 }
313}
314
315int
316session_lookup_del_session_endpoint (u32 table_index,
317 session_endpoint_t * sep)
318{
319 session_table_t *st;
320 session_kv4_t kv4;
321 session_kv6_t kv6;
322
323 st = session_table_get (table_index);
324 if (!st)
325 return -1;
326 if (sep->is_ip4)
327 {
328 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
329 sep->transport_proto);
330 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
331 }
332 else
333 {
334 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
335 sep->transport_proto);
336 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
337 }
338}
339
340/**
341 * Delete transport connection from session table
342 *
343 * @param table_index session table index
344 * @param tc transport connection to be removed
345 *
346 * @return non-zero if failure
347 */
348int
349session_lookup_del_connection (transport_connection_t * tc)
350{
351 session_table_t *st;
352 session_kv4_t kv4;
353 session_kv6_t kv6;
354
355 st = session_table_get_for_connection (tc);
356 if (!st)
357 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700358 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700359 {
Florin Coras04e53442017-07-16 17:12:15 -0700360 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700361 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700362 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700363 }
364 else
365 {
Florin Coras04e53442017-07-16 17:12:15 -0700366 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700367 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700368 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700369 }
Florin Coras04e53442017-07-16 17:12:15 -0700370}
371
372int
Florin Corascea194d2017-10-02 00:18:51 -0700373session_lookup_del_session (stream_session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700374{
Florin Coras561af9b2017-12-09 10:19:43 -0800375 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras04e53442017-07-16 17:12:15 -0700376 transport_connection_t *ts;
Florin Coras561af9b2017-12-09 10:19:43 -0800377 ts = tp_vfts[tp].get_connection (s->connection_index, s->thread_index);
Florin Corascea194d2017-10-02 00:18:51 -0700378 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700379}
380
Florin Corasdff48db2017-11-19 18:06:58 -0800381static u8
382session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700383{
Florin Corasdff48db2017-11-19 18:06:58 -0800384 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
385 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
386 return 0;
387 return 1;
388}
389
390static u32
391session_lookup_action_to_app_index (u32 action_index)
392{
393 switch (action_index)
394 {
395 case SESSION_RULES_TABLE_ACTION_DROP:
396 return APP_DROP_INDEX;
397 case SESSION_RULES_TABLE_ACTION_ALLOW:
398 case SESSION_RULES_TABLE_INVALID_INDEX:
399 return APP_INVALID_INDEX;
400 default:
401 return action_index;
402 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700403}
404
Florin Coras1c710452017-10-17 00:03:13 -0700405static stream_session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700406session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
407 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700408{
409 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800410 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700411 if (!app)
412 return 0;
413
Florin Coras7999e832017-10-31 01:51:04 -0700414 return application_first_listener (app, fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700415}
416
Florin Corasa2ff7b82017-11-08 17:55:03 -0800417static stream_session_t *
418session_lookup_action_to_session (u32 action_index, u8 fib_proto,
419 u8 transport_proto)
420{
Florin Corasdff48db2017-11-19 18:06:58 -0800421 u32 app_index;
422 app_index = session_lookup_action_to_app_index (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800423 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800424 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800425 transport_proto);
426}
427
Florin Coras1c710452017-10-17 00:03:13 -0700428stream_session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800429session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
430 ip4_address_t * lcl, u16 lcl_port,
431 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700432{
Florin Corasc97a7392017-11-05 23:07:07 -0800433 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800434 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800435 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700436 rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800437 app_index = session_lookup_action_to_app_index (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700438 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800439 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700440 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700441}
442
443stream_session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800444session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
445 ip6_address_t * lcl, u16 lcl_port,
446 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700447{
Florin Corasc97a7392017-11-05 23:07:07 -0800448 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800449 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800450 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700451 rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800452 app_index = session_lookup_action_to_app_index (action_index);
453 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700454 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700455}
456
Florin Corasa2ff7b82017-11-08 17:55:03 -0800457/**
458 * Lookup listener for session endpoint in table
459 *
460 * @param table_index table where the endpoint should be looked up
461 * @param sep session endpoint to be looked up
462 * @param use_rules flag that indicates if the session rules of the table
463 * should be used
464 * @return invalid handle if nothing is found, the handle of a valid listener
465 * or an action_index if a rule is hit
466 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700467u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800468session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
469 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700470{
Florin Corasc97a7392017-11-05 23:07:07 -0800471 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700472 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700473 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700474 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700475
Florin Corascea194d2017-10-02 00:18:51 -0700476 st = session_table_get (table_index);
477 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700478 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700479 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700480 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800481 session_kv4_t kv4;
482 ip4_address_t lcl4;
483
Florin Coras561af9b2017-12-09 10:19:43 -0800484 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
485 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700486 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
487 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700488 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800489 if (use_rules)
490 {
491 memset (&lcl4, 0, sizeof (lcl4));
492 srt = &st->session_rules[sep->transport_proto];
493 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
494 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800495 if (session_lookup_action_index_is_valid (ai))
496 return session_lookup_action_to_app_index (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800497 }
Florin Coras68810622017-07-24 17:40:28 -0700498 }
499 else
500 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800501 session_kv6_t kv6;
502 ip6_address_t lcl6;
503
Florin Coras561af9b2017-12-09 10:19:43 -0800504 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
505 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700506 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
507 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700508 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700509
Florin Corasa2ff7b82017-11-08 17:55:03 -0800510 if (use_rules)
511 {
512 memset (&lcl6, 0, sizeof (lcl6));
513 srt = &st->session_rules[sep->transport_proto];
514 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
515 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800516 if (session_lookup_action_index_is_valid (ai))
517 return session_lookup_action_to_app_index (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800518 }
Florin Coras68810622017-07-24 17:40:28 -0700519 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700520 return SESSION_INVALID_HANDLE;
521}
522
Florin Corasa2ff7b82017-11-08 17:55:03 -0800523/**
524 * Look up endpoint in local session table
525 *
526 * The result, for now, is an application index and it may in the future
527 * be extended to a more complicated "action object". The only action we
528 * emulate now is "drop" and for that we return a special app index.
529 *
530 * Lookup logic is to check in order:
531 * - the rules in the table (connect acls)
532 * - session sub-table for a listener
533 * - session sub-table for a local listener (zeroed addr)
534 *
535 * @param table_index table where the lookup should be done
536 * @param sep session endpoint to be looked up
537 * @return index that can be interpreted as an app index or drop action.
538 */
Florin Corascea194d2017-10-02 00:18:51 -0700539u32
Florin Corasa2ff7b82017-11-08 17:55:03 -0800540session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700541{
Florin Corasc97a7392017-11-05 23:07:07 -0800542 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700543 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700544 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700545 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700546
Florin Corascea194d2017-10-02 00:18:51 -0700547 st = session_table_get (table_index);
548 if (!st)
549 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800550 ASSERT (st->is_local);
551
Florin Corascea194d2017-10-02 00:18:51 -0700552 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700553 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800554 session_kv4_t kv4;
555 ip4_address_t lcl4;
556
557 /*
558 * Check if endpoint has special rules associated
559 */
560 memset (&lcl4, 0, sizeof (lcl4));
561 srt = &st->session_rules[sep->transport_proto];
562 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
563 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800564 if (session_lookup_action_index_is_valid (ai))
565 return session_lookup_action_to_app_index (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800566
567 /*
568 * Check if session endpoint is a listener
569 */
Florin Corascea194d2017-10-02 00:18:51 -0700570 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
571 sep->transport_proto);
572 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
573 if (rv == 0)
574 return (u32) kv4.value;
575
576 /*
577 * Zero out the ip. Logic is that connect to local ips, say
578 * 127.0.0.1:port, can match 0.0.0.0:port
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 (u32) kv4.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800584
585 /*
586 * Zero out the port and check if we have proxy
587 */
588 kv4.key[1] = 0;
589 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
590 if (rv == 0)
591 return (u32) kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700592 }
593 else
594 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800595 session_kv6_t kv6;
596 ip6_address_t lcl6;
597
598 memset (&lcl6, 0, sizeof (lcl6));
599 srt = &st->session_rules[sep->transport_proto];
600 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
601 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800602 if (session_lookup_action_index_is_valid (ai))
603 return session_lookup_action_to_app_index (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800604
Florin Corascea194d2017-10-02 00:18:51 -0700605 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
606 sep->transport_proto);
607 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
608 if (rv == 0)
609 return (u32) kv6.value;
610
611 /*
612 * Zero out the ip. Same logic as above.
613 */
614 kv6.key[0] = kv6.key[1] = 0;
615 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
616 if (rv == 0)
617 return (u32) kv6.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800618
619 /*
620 * Zero out the port. Same logic as above.
621 */
622 kv6.key[4] = kv6.key[5] = 0;
623 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
624 if (rv == 0)
625 return (u32) kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700626 }
Florin Corasa2ff7b82017-11-08 17:55:03 -0800627 return APP_INVALID_INDEX;
Florin Coras04e53442017-07-16 17:12:15 -0700628}
629
Florin Corascea194d2017-10-02 00:18:51 -0700630static stream_session_t *
631session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
632 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700633{
Florin Coras04e53442017-07-16 17:12:15 -0700634 session_kv4_t kv4;
635 int rv;
Florin Coras561af9b2017-12-09 10:19:43 -0800636 session_type_t session_type;
Florin Coras04e53442017-07-16 17:12:15 -0700637
Florin Corasdbd44562017-11-09 19:30:17 -0800638 /*
639 * First, try a fully formed listener
640 */
Florin Coras561af9b2017-12-09 10:19:43 -0800641 session_type = session_type_from_proto_and_ip (proto, 1);
Florin Coras04e53442017-07-16 17:12:15 -0700642 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700643 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700644 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800645 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700646
Florin Corasdbd44562017-11-09 19:30:17 -0800647 /*
648 * Zero out the lcl ip and check if any 0/0 port binds have been done
649 */
Florin Coras04e53442017-07-16 17:12:15 -0700650 kv4.key[0] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700651 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700652 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800653 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700654
Florin Corasdbd44562017-11-09 19:30:17 -0800655 /*
656 * Zero out port and check if we have a proxy set up for our ip
657 */
658 make_v4_proxy_kv (&kv4, lcl, proto);
659 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
660 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800661 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800662
Florin Coras04e53442017-07-16 17:12:15 -0700663 return 0;
664}
665
Florin Coras04e53442017-07-16 17:12:15 -0700666stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700667session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
668 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700669{
Florin Corascea194d2017-10-02 00:18:51 -0700670 session_table_t *st;
671 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
672 if (!st)
673 return 0;
674 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700675}
676
Florin Corascea194d2017-10-02 00:18:51 -0700677static stream_session_t *
678session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
679 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700680{
Florin Coras04e53442017-07-16 17:12:15 -0700681 session_kv6_t kv6;
682 int rv;
Florin Coras561af9b2017-12-09 10:19:43 -0800683 session_type_t session_type;
Florin Coras04e53442017-07-16 17:12:15 -0700684
Florin Coras561af9b2017-12-09 10:19:43 -0800685 session_type = session_type_from_proto_and_ip (proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700686 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700687 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700688 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800689 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700690
691 /* Zero out the lcl ip */
692 kv6.key[0] = kv6.key[1] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700693 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700694 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800695 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700696
Florin Corasdbd44562017-11-09 19:30:17 -0800697 make_v6_proxy_kv (&kv6, lcl, proto);
698 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
699 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800700 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700701 return 0;
702}
703
Florin Coras04e53442017-07-16 17:12:15 -0700704stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700705session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
706 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700707{
Florin Corascea194d2017-10-02 00:18:51 -0700708 session_table_t *st;
709 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
710 if (!st)
711 return 0;
712 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700713}
714
715stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700716session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700717{
Florin Corascea194d2017-10-02 00:18:51 -0700718 session_table_t *st;
719 st = session_table_get (table_index);
720 if (!st)
721 return 0;
722 if (sep->is_ip4)
723 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
724 sep->transport_proto);
725 else
726 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
727 sep->transport_proto);
Florin Coras04e53442017-07-16 17:12:15 -0700728 return 0;
729}
730
Florin Corascea194d2017-10-02 00:18:51 -0700731int
732session_lookup_add_half_open (transport_connection_t * tc, u64 value)
733{
734 session_table_t *st;
735 session_kv4_t kv4;
736 session_kv6_t kv6;
737
738 st = session_table_get_or_alloc_for_connection (tc);
739 if (!st)
740 return 0;
741 if (tc->is_ip4)
742 {
743 make_v4_ss_kv_from_tc (&kv4, tc);
744 kv4.value = value;
745 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
746 1 /* is_add */ );
747 }
748 else
749 {
750 make_v6_ss_kv_from_tc (&kv6, tc);
751 kv6.value = value;
752 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
753 1 /* is_add */ );
754 }
755}
756
757int
758session_lookup_del_half_open (transport_connection_t * tc)
759{
760 session_table_t *st;
761 session_kv4_t kv4;
762 session_kv6_t kv6;
763
764 st = session_table_get_for_connection (tc);
765 if (!st)
766 return -1;
767 if (tc->is_ip4)
768 {
769 make_v4_ss_kv_from_tc (&kv4, tc);
770 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
771 0 /* is_add */ );
772 }
773 else
774 {
775 make_v6_ss_kv_from_tc (&kv6, tc);
776 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
777 0 /* is_add */ );
778 }
779}
780
Florin Coras04e53442017-07-16 17:12:15 -0700781u64
Florin Corascea194d2017-10-02 00:18:51 -0700782session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700783{
Florin Corascea194d2017-10-02 00:18:51 -0700784 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700785 session_kv4_t kv4;
786 session_kv6_t kv6;
787 int rv;
788
Florin Corascea194d2017-10-02 00:18:51 -0700789 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
790 tc->fib_index);
791 if (!st)
792 return HALF_OPEN_LOOKUP_INVALID_VALUE;
793 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700794 {
Florin Corascea194d2017-10-02 00:18:51 -0700795 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700796 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700797 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700798 if (rv == 0)
799 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700800 }
801 else
802 {
803 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700804 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700805 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700806 if (rv == 0)
807 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700808 }
809 return HALF_OPEN_LOOKUP_INVALID_VALUE;
810}
811
812transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700813session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700814{
Florin Corascea194d2017-10-02 00:18:51 -0700815 u32 sst;
816
Florin Coras04e53442017-07-16 17:12:15 -0700817 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700818 {
819 sst = session_type_from_proto_and_ip (proto, is_ip4);
820 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
821 }
Florin Coras04e53442017-07-16 17:12:15 -0700822 return 0;
823}
824
Florin Corascea194d2017-10-02 00:18:51 -0700825/**
826 * Lookup connection with ip4 and transport layer information
827 *
828 * This is used on the fast path so it needs to be fast. Thereby,
829 * duplication of code and 'hacks' allowed.
830 *
831 * The lookup is incremental and returns whenever something is matched. The
832 * steps are:
833 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700834 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700835 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800836 * - Try to find a fully-formed or local source wildcarded (listener bound to
837 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700838 * - return 0
839 *
840 * @param fib_index index of fib wherein the connection was received
841 * @param lcl local ip4 address
842 * @param rmt remote ip4 address
843 * @param lcl_port local port
844 * @param rmt_port remote port
845 * @param proto transport protocol (e.g., tcp, udp)
846 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800847 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700848 *
849 * @return pointer to transport connection, if one is found, 0 otherwise
850 */
Florin Coras04e53442017-07-16 17:12:15 -0700851transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700852session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
853 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800854 u16 rmt_port, u8 proto, u32 thread_index,
855 u8 * is_filtered)
Florin Coras04e53442017-07-16 17:12:15 -0700856{
Florin Corascea194d2017-10-02 00:18:51 -0700857 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700858 session_kv4_t kv4;
859 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800860 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700861 int rv;
862
Florin Corascea194d2017-10-02 00:18:51 -0700863 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
864 if (PREDICT_FALSE (!st))
865 return 0;
866
Florin Corasa2ff7b82017-11-08 17:55:03 -0800867 /*
868 * Lookup session amongst established ones
869 */
Florin Coras04e53442017-07-16 17:12:15 -0700870 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700871 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700872 if (rv == 0)
873 {
Florin Corascea194d2017-10-02 00:18:51 -0700874 ASSERT ((u32) (kv4.value >> 32) == thread_index);
875 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras561af9b2017-12-09 10:19:43 -0800876 return tp_vfts[proto].get_connection (s->connection_index,
877 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700878 }
879
Florin Corasa2ff7b82017-11-08 17:55:03 -0800880 /*
881 * Try half-open connections
882 */
Florin Corascea194d2017-10-02 00:18:51 -0700883 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700884 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800885 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700886
Florin Corasa2ff7b82017-11-08 17:55:03 -0800887 /*
888 * Check the session rules table
889 */
890 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
891 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800892 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800893 {
Florin Corasdff48db2017-11-19 18:06:58 -0800894 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
895 return 0;
896 if ((s = session_lookup_action_to_session (action_index,
897 FIB_PROTOCOL_IP4, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -0800898 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800899 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800900 }
901
902 /*
903 * If nothing is found, check if any listener is available
904 */
905 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
906 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -0800907 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800908
909 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700910}
911
Florin Corascea194d2017-10-02 00:18:51 -0700912/**
913 * Lookup connection with ip4 and transport layer information
914 *
915 * Not optimized. This is used on the fast path so it needs to be fast.
916 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
917 * to that of @ref session_lookup_connection_wt4
918 *
919 * @param fib_index index of the fib wherein the connection was received
920 * @param lcl local ip4 address
921 * @param rmt remote ip4 address
922 * @param lcl_port local port
923 * @param rmt_port remote port
924 * @param proto transport protocol (e.g., tcp, udp)
925 *
926 * @return pointer to transport connection, if one is found, 0 otherwise
927 */
Florin Coras04e53442017-07-16 17:12:15 -0700928transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700929session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
930 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
931 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700932{
Florin Corascea194d2017-10-02 00:18:51 -0700933 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700934 session_kv4_t kv4;
935 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800936 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700937 int rv;
938
Florin Corascea194d2017-10-02 00:18:51 -0700939 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
940 if (PREDICT_FALSE (!st))
941 return 0;
942
Florin Corasa2ff7b82017-11-08 17:55:03 -0800943 /*
944 * Lookup session amongst established ones
945 */
Florin Coras04e53442017-07-16 17:12:15 -0700946 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700947 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700948 if (rv == 0)
949 {
Florin Corascea194d2017-10-02 00:18:51 -0700950 s = session_get_from_handle (kv4.value);
Florin Coras561af9b2017-12-09 10:19:43 -0800951 return tp_vfts[proto].get_connection (s->connection_index,
952 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700953 }
954
Florin Corasa2ff7b82017-11-08 17:55:03 -0800955 /*
956 * Try half-open connections
957 */
Florin Corascea194d2017-10-02 00:18:51 -0700958 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700959 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800960 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800961
962 /*
963 * Check the session rules table
964 */
965 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
966 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800967 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800968 {
Florin Corasdff48db2017-11-19 18:06:58 -0800969 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
970 return 0;
971 if ((s = session_lookup_action_to_session (action_index,
972 FIB_PROTOCOL_IP4, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -0800973 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800974 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800975 }
976
977 /*
978 * If nothing is found, check if any listener is available
979 */
980 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
981 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -0800982 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800983
984 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700985}
986
Florin Corascea194d2017-10-02 00:18:51 -0700987/**
988 * Lookup session with ip4 and transport layer information
989 *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700990 * Important note: this may look into another thread's pool table and
991 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
992 * if needed as soon as possible.
993 *
994 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
995 * this returns a session as opposed to a transport connection and it does not
996 * try to lookup half-open sessions.
997 *
998 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -0700999 */
1000stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001001session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1002 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001003{
Florin Corascea194d2017-10-02 00:18:51 -07001004 session_table_t *st;
1005 session_kv4_t kv4;
1006 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001007 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001008 int rv;
1009
1010 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1011 if (PREDICT_FALSE (!st))
1012 return 0;
1013
Florin Corasa2ff7b82017-11-08 17:55:03 -08001014 /*
1015 * Lookup session amongst established ones
1016 */
Florin Corascea194d2017-10-02 00:18:51 -07001017 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1018 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1019 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001020 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001021
Florin Corasa2ff7b82017-11-08 17:55:03 -08001022 /*
1023 * Check the session rules table
1024 */
1025 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1026 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001027 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001028 {
Florin Corasdff48db2017-11-19 18:06:58 -08001029 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1030 return 0;
1031 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1032 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001033 }
1034
1035 /*
1036 * If nothing is found, check if any listener is available
1037 */
Florin Corascea194d2017-10-02 00:18:51 -07001038 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
1039 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001040
1041 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001042}
1043
1044/**
1045 * Lookup connection with ip6 and transport layer information
1046 *
1047 * This is used on the fast path so it needs to be fast. Thereby,
1048 * duplication of code and 'hacks' allowed.
1049 *
1050 * The lookup is incremental and returns whenever something is matched. The
1051 * steps are:
1052 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001053 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001054 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001055 * - Try to find a fully-formed or local source wildcarded (listener bound to
1056 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001057 * - return 0
1058 *
1059 * @param fib_index index of the fib wherein the connection was received
1060 * @param lcl local ip6 address
1061 * @param rmt remote ip6 address
1062 * @param lcl_port local port
1063 * @param rmt_port remote port
1064 * @param proto transport protocol (e.g., tcp, udp)
1065 * @param thread_index thread index for request
1066 *
1067 * @return pointer to transport connection, if one is found, 0 otherwise
1068 */
1069transport_connection_t *
1070session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1071 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001072 u16 rmt_port, u8 proto, u32 thread_index,
1073 u8 * is_filtered)
Florin Corascea194d2017-10-02 00:18:51 -07001074{
1075 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001076 stream_session_t *s;
1077 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001078 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001079 int rv;
1080
Florin Corascea194d2017-10-02 00:18:51 -07001081 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1082 if (PREDICT_FALSE (!st))
1083 return 0;
1084
Florin Coras04e53442017-07-16 17:12:15 -07001085 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001086 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001087 if (rv == 0)
1088 {
Florin Corascea194d2017-10-02 00:18:51 -07001089 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1090 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001091 return tp_vfts[proto].get_connection (s->connection_index,
1092 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001093 }
1094
Florin Corasa2ff7b82017-11-08 17:55:03 -08001095 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001096 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001097 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -08001098 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001099
Florin Corasa2ff7b82017-11-08 17:55:03 -08001100 /* Check the session rules table */
1101 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1102 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001103 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001104 {
Florin Corasdff48db2017-11-19 18:06:58 -08001105 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
1106 return 0;
1107 if ((s = session_lookup_action_to_session (action_index,
1108 FIB_PROTOCOL_IP6, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -08001109 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001110 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001111 }
1112
1113 /* If nothing is found, check if any listener is available */
1114 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1115 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -08001116 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001117
1118 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001119}
1120
Florin Corascea194d2017-10-02 00:18:51 -07001121/**
1122 * Lookup connection with ip6 and transport layer information
1123 *
1124 * Not optimized. This is used on the fast path so it needs to be fast.
1125 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1126 * to that of @ref session_lookup_connection_wt4
1127 *
1128 * @param fib_index index of the fib wherein the connection was received
1129 * @param lcl local ip6 address
1130 * @param rmt remote ip6 address
1131 * @param lcl_port local port
1132 * @param rmt_port remote port
1133 * @param proto transport protocol (e.g., tcp, udp)
1134 *
1135 * @return pointer to transport connection, if one is found, 0 otherwise
1136 */
Florin Coras04e53442017-07-16 17:12:15 -07001137transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001138session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1139 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1140 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001141{
Florin Corascea194d2017-10-02 00:18:51 -07001142 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001143 stream_session_t *s;
1144 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001145 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001146 int rv;
1147
Florin Corascea194d2017-10-02 00:18:51 -07001148 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1149 if (PREDICT_FALSE (!st))
1150 return 0;
1151
Florin Coras04e53442017-07-16 17:12:15 -07001152 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001153 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001154 if (rv == 0)
1155 {
Florin Corascea194d2017-10-02 00:18:51 -07001156 s = session_get_from_handle (kv6.value);
Florin Coras561af9b2017-12-09 10:19:43 -08001157 return tp_vfts[proto].get_connection (s->connection_index,
1158 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001159 }
1160
Florin Corasa2ff7b82017-11-08 17:55:03 -08001161 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001162 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001163 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -08001164 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001165
Florin Corasa2ff7b82017-11-08 17:55:03 -08001166 /* Check the session rules table */
1167 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1168 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001169 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001170 {
Florin Corasdff48db2017-11-19 18:06:58 -08001171 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1172 return 0;
1173 if ((s = session_lookup_action_to_session (action_index,
1174 FIB_PROTOCOL_IP6, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -08001175 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001176 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001177 }
1178
1179 /* If nothing is found, check if any listener is available */
1180 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1181 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -08001182 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001183
1184 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001185}
1186
Florin Corascea194d2017-10-02 00:18:51 -07001187/**
1188 * Lookup session with ip6 and transport layer information
1189 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001190 * Important note: this may look into another thread's pool table and
1191 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1192 * if needed as soon as possible.
1193 *
1194 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1195 * this returns a session as opposed to a transport connection and it does not
1196 * try to lookup half-open sessions.
1197 *
1198 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001199 */
1200stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001201session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1202 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001203{
1204 session_table_t *st;
1205 session_kv6_t kv6;
1206 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001207 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001208 int rv;
1209
1210 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1211 if (PREDICT_FALSE (!st))
1212 return 0;
1213
1214 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1215 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1216 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001217 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001218
Florin Corasa2ff7b82017-11-08 17:55:03 -08001219 /* Check the session rules table */
1220 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1221 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001222 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001223 {
Florin Corasdff48db2017-11-19 18:06:58 -08001224 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1225 return 0;
1226 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1227 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001228 }
1229
Florin Corascea194d2017-10-02 00:18:51 -07001230 /* If nothing is found, check if any listener is available */
1231 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1232 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001233 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001234}
1235
1236u64
1237session_lookup_local_listener_make_handle (session_endpoint_t * sep)
1238{
1239 return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
1240 | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
1241 | (u32) sep->is_ip4);
1242}
1243
1244u8
1245session_lookup_local_is_handle (u64 handle)
1246{
1247 if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
1248 return 1;
1249 return 0;
1250}
1251
1252int
1253session_lookup_local_listener_parse_handle (u64 handle,
1254 session_endpoint_t * sep)
1255{
1256 u32 local_table_handle;
1257 if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
1258 return -1;
1259 local_table_handle = handle & 0xFFFFFFFFULL;
1260 sep->is_ip4 = local_table_handle & 0xff;
1261 local_table_handle >>= 8;
1262 sep->transport_proto = local_table_handle & 0xff;
1263 sep->port = local_table_handle >> 8;
1264 return 0;
1265}
1266
Florin Coras1c710452017-10-17 00:03:13 -07001267clib_error_t *
1268vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1269{
1270 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001271 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001272 session_table_t *st;
1273 u32 fib_index;
1274 u8 fib_proto;
1275 clib_error_t *error;
1276
1277 if (!app_ns)
1278 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1279 "invalid app ns");
1280 if (args->scope > 3)
1281 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1282 "invalid scope");
Florin Corasc97a7392017-11-05 23:07:07 -08001283 if (args->transport_proto != TRANSPORT_PROTO_TCP
1284 && args->transport_proto != TRANSPORT_PROTO_UDP)
1285 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1286 "invalid transport proto");
Florin Coras1c710452017-10-17 00:03:13 -07001287 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1288 {
1289 fib_proto = args->table_args.rmt.fp_proto;
1290 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1291 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001292 srt = &st->session_rules[args->transport_proto];
1293 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1294 {
1295 clib_error_report (error);
1296 return error;
1297 }
Florin Coras1c710452017-10-17 00:03:13 -07001298 }
1299 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1300 {
Florin Corasa2ff7b82017-11-08 17:55:03 -08001301 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1302 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1303 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001304 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001305 srt = &st->session_rules[args->transport_proto];
1306 error = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001307 }
1308 return error;
1309}
1310
Florin Coras6c36f532017-11-03 18:32:34 -07001311/**
1312 * Mark (global) tables as pertaining to app ns
1313 */
1314void
1315session_lookup_set_tables_appns (app_namespace_t * app_ns)
1316{
1317 session_table_t *st;
1318 u32 fib_index;
1319 u8 fp;
1320
1321 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1322 {
1323 fib_index = app_namespace_get_fib_index (app_ns, fp);
1324 st = session_table_get_for_fib_index (fp, fib_index);
1325 if (st)
1326 st->appns_index = app_namespace_index (app_ns);
1327 }
1328}
1329
Florin Corascea194d2017-10-02 00:18:51 -07001330u8 *
1331format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1332{
1333 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1334 u32 is_local = va_arg (*args, u32);
1335 u8 *app_name, *str = 0;
1336 stream_session_t *session;
1337 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1338
Florin Corascea194d2017-10-02 00:18:51 -07001339 if (!is_local)
1340 {
1341 session = session_get_from_handle (kvp->value);
1342 app_name = application_name_from_index (session->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001343 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1344 key->proto, format_ip4_address, &key->src,
1345 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1346 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001347 s = format (s, "%-40v%-30v", str, app_name);
1348 }
1349 else
1350 {
1351 app_name = application_name_from_index (kvp->value);
Florin Coras561af9b2017-12-09 10:19:43 -08001352 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1353 format_ip4_address, &key->src,
1354 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001355 s = format (s, "%-30v%-30v", str, app_name);
1356 }
1357 vec_free (app_name);
1358 return s;
1359}
1360
1361typedef struct _ip4_session_table_show_ctx_t
1362{
1363 vlib_main_t *vm;
1364 u8 is_local;
1365} ip4_session_table_show_ctx_t;
1366
1367static int
1368ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1369{
1370 ip4_session_table_show_ctx_t *ctx = arg;
1371 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1372 ctx->is_local);
1373 return 1;
1374}
1375
1376void
1377session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1378 u8 type, u8 is_local)
1379{
1380 ip4_session_table_show_ctx_t ctx = {
1381 .vm = vm,
1382 .is_local = is_local,
1383 };
1384 if (!is_local)
1385 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1386 else
1387 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1388 switch (type)
1389 {
1390 /* main table v4 */
1391 case 0:
1392 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1393 &ctx);
1394 break;
1395 default:
1396 clib_warning ("not supported");
1397 }
1398}
Florin Coras66b11312017-07-31 17:18:03 -07001399
Florin Coras1c710452017-10-17 00:03:13 -07001400static clib_error_t *
1401session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1402 vlib_cli_command_t * cmd)
1403{
Florin Corasc97a7392017-11-05 23:07:07 -08001404 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001405 u32 appns_index, scope = 0;
1406 ip46_address_t lcl_ip, rmt_ip;
1407 u8 is_ip4 = 1, conn_set = 0;
1408 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001409 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001410 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001411 clib_error_t *error;
Florin Coras1c710452017-10-17 00:03:13 -07001412
1413 memset (&lcl_ip, 0, sizeof (lcl_ip));
1414 memset (&rmt_ip, 0, sizeof (rmt_ip));
1415 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1416 {
1417 if (unformat (input, "del"))
1418 is_add = 0;
1419 else if (unformat (input, "add"))
1420 ;
1421 else if (unformat (input, "appns %_%v%_", &ns_id))
1422 ;
1423 else if (unformat (input, "scope global"))
1424 scope = SESSION_RULE_SCOPE_GLOBAL;
1425 else if (unformat (input, "scope local"))
1426 scope = SESSION_RULE_SCOPE_LOCAL;
1427 else if (unformat (input, "scope all"))
1428 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1429 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1430 ;
1431 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1432 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1433 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1434 &rmt_port))
1435 {
1436 is_ip4 = 1;
1437 conn_set = 1;
1438 }
1439 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1440 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1441 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1442 &rmt_port))
1443 {
1444 is_ip4 = 0;
1445 conn_set = 1;
1446 }
1447 else if (unformat (input, "action %d", &action))
1448 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001449 else if (unformat (input, "tag %_%v%_", &tag))
1450 ;
Florin Coras1c710452017-10-17 00:03:13 -07001451 else
1452 return clib_error_return (0, "unknown input `%U'",
1453 format_unformat_error, input);
1454 }
1455
Florin Corasc97a7392017-11-05 23:07:07 -08001456 if (proto == ~0)
1457 {
1458 vlib_cli_output (vm, "proto must be set");
1459 return 0;
1460 }
1461 if (is_add && !conn_set && action == ~0)
1462 {
1463 vlib_cli_output (vm, "connection and action must be set for add");
1464 return 0;
1465 }
1466 if (!is_add && !tag && !conn_set)
1467 {
1468 vlib_cli_output (vm, "connection or tag must be set for delete");
1469 return 0;
1470 }
1471 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1472 {
1473 vlib_cli_output (vm, "tag too long (max u64)");
1474 return 0;
1475 }
Florin Coras1c710452017-10-17 00:03:13 -07001476
1477 if (ns_id)
1478 {
1479 app_ns = app_namespace_get_from_id (ns_id);
1480 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001481 {
1482 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1483 return 0;
1484 }
Florin Coras1c710452017-10-17 00:03:13 -07001485 }
1486 else
1487 {
1488 app_ns = app_namespace_get_default ();
1489 }
1490 appns_index = app_namespace_index (app_ns);
1491
1492 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1493 session_rule_add_del_args_t args = {
1494 .table_args.lcl.fp_addr = lcl_ip,
1495 .table_args.lcl.fp_len = lcl_plen,
1496 .table_args.lcl.fp_proto = fib_proto,
1497 .table_args.rmt.fp_addr = rmt_ip,
1498 .table_args.rmt.fp_len = rmt_plen,
1499 .table_args.rmt.fp_proto = fib_proto,
1500 .table_args.lcl_port = lcl_port,
1501 .table_args.rmt_port = rmt_port,
1502 .table_args.action_index = action,
1503 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001504 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001505 .appns_index = appns_index,
1506 .scope = scope,
1507 };
Florin Corasc97a7392017-11-05 23:07:07 -08001508 error = vnet_session_rule_add_del (&args);
1509 vec_free (tag);
1510 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001511}
1512
1513/* *INDENT-OFF* */
1514VLIB_CLI_COMMAND (session_rule_command, static) =
1515{
1516 .path = "session rule",
1517 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1518 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1519 .function = session_rule_command_fn,
1520};
1521/* *INDENT-ON* */
1522
Florin Coras7999e832017-10-31 01:51:04 -07001523void
1524session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1525 u8 transport_proto)
1526{
1527 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001528 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001529 session_table_t *st;
1530 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001531 srt = &st->session_rules[transport_proto];
1532 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001533}
1534
1535void
1536session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1537 u8 transport_proto)
1538{
1539 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001540 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001541 session_table_t *st;
1542 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001543 srt = &st->session_rules[transport_proto];
1544 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001545}
1546
Florin Coras1c710452017-10-17 00:03:13 -07001547static clib_error_t *
1548show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1549 vlib_cli_command_t * cmd)
1550{
1551 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1552 u32 fib_index, scope = 0;
1553 ip46_address_t lcl_ip, rmt_ip;
1554 u8 is_ip4 = 1, show_one = 0;
1555 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001556 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001557 session_table_t *st;
1558 u8 *ns_id = 0, fib_proto;
1559
1560 memset (&lcl_ip, 0, sizeof (lcl_ip));
1561 memset (&rmt_ip, 0, sizeof (rmt_ip));
1562 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1563 {
1564 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1565 ;
1566 else if (unformat (input, "appns %_%v%_", &ns_id))
1567 ;
1568 else if (unformat (input, "scope global"))
1569 scope = 1;
1570 else if (unformat (input, "scope local"))
1571 scope = 2;
1572 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1573 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1574 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1575 &rmt_port))
1576 {
1577 is_ip4 = 1;
1578 show_one = 1;
1579 }
1580 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1581 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1582 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1583 &rmt_port))
1584 {
1585 is_ip4 = 0;
1586 show_one = 1;
1587 }
1588 else
1589 return clib_error_return (0, "unknown input `%U'",
1590 format_unformat_error, input);
1591 }
1592
1593 if (transport_proto == ~0)
1594 {
1595 vlib_cli_output (vm, "transport proto must be set");
1596 return 0;
1597 }
1598
1599 if (ns_id)
1600 {
1601 app_ns = app_namespace_get_from_id (ns_id);
1602 if (!app_ns)
1603 {
1604 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1605 return 0;
1606 }
1607 }
1608 else
1609 {
1610 app_ns = app_namespace_get_default ();
1611 }
1612
1613 if (scope == 1 || scope == 0)
1614 {
1615 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1616 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1617 st = session_table_get_for_fib_index (fib_proto, fib_index);
1618 }
1619 else
1620 {
1621 st = app_namespace_get_local_table (app_ns);
1622 }
1623
1624 if (show_one)
1625 {
Florin Corasc97a7392017-11-05 23:07:07 -08001626 srt = &st->session_rules[transport_proto];
1627 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1628 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001629 return 0;
1630 }
1631
Florin Corasc97a7392017-11-05 23:07:07 -08001632 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1633 transport_proto);
1634 srt = &st->session_rules[transport_proto];
1635 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1636 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001637
1638 vec_free (ns_id);
1639 return 0;
1640}
1641
1642/* *INDENT-OFF* */
1643VLIB_CLI_COMMAND (show_session_rules_command, static) =
1644{
1645 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001646 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1647 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001648 .function = show_session_rules_command_fn,
1649};
1650/* *INDENT-ON* */
1651
Florin Coras04e53442017-07-16 17:12:15 -07001652void
1653session_lookup_init (void)
1654{
Florin Corascea194d2017-10-02 00:18:51 -07001655 /*
1656 * Allocate default table and map it to fib_index 0
1657 */
1658 session_table_t *st = session_table_alloc ();
1659 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1660 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001661 st->active_fib_proto = FIB_PROTOCOL_IP4;
1662 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001663 st = session_table_alloc ();
1664 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1665 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001666 st->active_fib_proto = FIB_PROTOCOL_IP6;
1667 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001668}
1669
1670/*
1671 * fd.io coding-style-patch-verification: ON
1672 *
1673 * Local Variables:
1674 * eval: (c-set-style "gnu")
1675 * End:
1676 */