Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 30 | #include <vnet/session/application.h> |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 31 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 32 | /** |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 33 | * 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 | */ |
| 36 | static u32 *fib_index_to_table_index[2]; |
| 37 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 38 | /* *INDENT-OFF* */ |
| 39 | /* 16 octets */ |
| 40 | typedef 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 | |
| 59 | typedef 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 | |
| 77 | typedef clib_bihash_kv_16_8_t session_kv4_t; |
| 78 | typedef clib_bihash_kv_48_8_t session_kv6_t; |
| 79 | |
| 80 | always_inline void |
| 81 | make_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 Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 84 | 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 Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 86 | kv->value = ~0ULL; |
| 87 | } |
| 88 | |
| 89 | always_inline void |
| 90 | make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port, |
| 91 | u8 proto) |
| 92 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 93 | kv->key[0] = (u64) lcl->as_u32; |
| 94 | kv->key[1] = (u64) proto << 32 | (u64) lcl_port; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 95 | kv->value = ~0ULL; |
| 96 | } |
| 97 | |
| 98 | always_inline void |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 99 | make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto) |
| 100 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 101 | kv->key[0] = (u64) lcl->as_u32; |
| 102 | kv->key[1] = (u64) proto << 32; |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 103 | kv->value = ~0ULL; |
| 104 | } |
| 105 | |
| 106 | always_inline void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 107 | make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 108 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 109 | make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port, |
| 110 | tc->rmt_port, tc->proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | always_inline void |
| 114 | make_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 Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 117 | 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 Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 123 | kv->value = ~0ULL; |
| 124 | } |
| 125 | |
| 126 | always_inline void |
| 127 | make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port, |
| 128 | u8 proto) |
| 129 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 130 | 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 Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 136 | kv->value = ~0ULL; |
| 137 | } |
| 138 | |
| 139 | always_inline void |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 140 | make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto) |
| 141 | { |
Florin Coras | 9679c81 | 2018-06-21 08:14:34 -0700 | [diff] [blame] | 142 | 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 Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 148 | kv->value = ~0ULL; |
| 149 | } |
| 150 | |
| 151 | always_inline void |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 152 | make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 153 | { |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 154 | make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port, |
| 155 | tc->rmt_port, tc->proto); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 158 | static session_table_t * |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 159 | session_table_get_or_alloc (u8 fib_proto, u8 fib_index) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 160 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 161 | session_table_t *st; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 162 | u32 table_index; |
| 163 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 164 | { |
| 165 | st = session_table_alloc (); |
| 166 | table_index = session_table_index (st); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 167 | vec_validate (fib_index_to_table_index[fib_proto], fib_index); |
| 168 | fib_index_to_table_index[fib_proto][fib_index] = table_index; |
| 169 | st->active_fib_proto = fib_proto; |
Florin Coras | b795bd0 | 2017-12-14 11:30:48 -0800 | [diff] [blame] | 170 | session_table_init (st, fib_proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 171 | return st; |
| 172 | } |
| 173 | else |
| 174 | { |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 175 | table_index = fib_index_to_table_index[fib_proto][fib_index]; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 176 | return session_table_get (table_index); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static session_table_t * |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 181 | session_table_get_or_alloc_for_connection (transport_connection_t * tc) |
| 182 | { |
| 183 | u32 fib_proto; |
| 184 | fib_proto = transport_connection_fib_proto (tc); |
| 185 | return session_table_get_or_alloc (fib_proto, tc->fib_index); |
| 186 | } |
| 187 | |
| 188 | static session_table_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 189 | session_table_get_for_connection (transport_connection_t * tc) |
| 190 | { |
| 191 | u32 fib_proto = transport_connection_fib_proto (tc); |
| 192 | if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index) |
| 193 | return 0; |
| 194 | return |
| 195 | session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]); |
| 196 | } |
| 197 | |
| 198 | static session_table_t * |
| 199 | session_table_get_for_fib_index (u32 fib_proto, u32 fib_index) |
| 200 | { |
| 201 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 202 | return 0; |
| 203 | return session_table_get (fib_index_to_table_index[fib_proto][fib_index]); |
| 204 | } |
| 205 | |
| 206 | u32 |
| 207 | session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index) |
| 208 | { |
| 209 | if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index) |
| 210 | return SESSION_TABLE_INVALID_INDEX; |
| 211 | return fib_index_to_table_index[fib_proto][fib_index]; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Add transport connection to a session table |
| 216 | * |
| 217 | * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type) |
| 218 | * is added to requested session table. |
| 219 | * |
| 220 | * @param tc transport connection to be added |
| 221 | * @param value value to be stored |
| 222 | * |
| 223 | * @return non-zero if failure |
| 224 | */ |
| 225 | int |
| 226 | session_lookup_add_connection (transport_connection_t * tc, u64 value) |
| 227 | { |
| 228 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 229 | session_kv4_t kv4; |
| 230 | session_kv6_t kv6; |
| 231 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 232 | st = session_table_get_or_alloc_for_connection (tc); |
| 233 | if (!st) |
| 234 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 235 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 236 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 237 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 238 | kv4.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 239 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
| 240 | 1 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 241 | } |
| 242 | else |
| 243 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 244 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 245 | kv6.value = value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 246 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
| 247 | 1 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 251 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 252 | session_lookup_add_session_endpoint (u32 table_index, |
| 253 | session_endpoint_t * sep, u64 value) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 254 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 255 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 256 | session_kv4_t kv4; |
| 257 | session_kv6_t kv6; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 258 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 259 | st = session_table_get (table_index); |
| 260 | if (!st) |
| 261 | return -1; |
| 262 | if (sep->is_ip4) |
| 263 | { |
| 264 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 265 | sep->transport_proto); |
| 266 | kv4.value = value; |
| 267 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 272 | sep->transport_proto); |
| 273 | kv6.value = value; |
| 274 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | int |
| 279 | session_lookup_del_session_endpoint (u32 table_index, |
| 280 | session_endpoint_t * sep) |
| 281 | { |
| 282 | session_table_t *st; |
| 283 | session_kv4_t kv4; |
| 284 | session_kv6_t kv6; |
| 285 | |
| 286 | st = session_table_get (table_index); |
| 287 | if (!st) |
| 288 | return -1; |
| 289 | if (sep->is_ip4) |
| 290 | { |
| 291 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 292 | sep->transport_proto); |
| 293 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 298 | sep->transport_proto); |
| 299 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0); |
| 300 | } |
| 301 | } |
| 302 | |
Florin Coras | aab0604 | 2020-02-26 02:56:14 +0000 | [diff] [blame] | 303 | int |
| 304 | session_lookup_del_session_endpoint2 (session_endpoint_t * sep) |
| 305 | { |
| 306 | fib_protocol_t fib_proto; |
| 307 | session_table_t *st; |
| 308 | session_kv4_t kv4; |
| 309 | session_kv6_t kv6; |
| 310 | |
| 311 | fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 312 | st = session_table_get_for_fib_index (fib_proto, sep->fib_index); |
| 313 | if (!st) |
| 314 | return -1; |
| 315 | if (sep->is_ip4) |
| 316 | { |
| 317 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 318 | sep->transport_proto); |
| 319 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 324 | sep->transport_proto); |
| 325 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0); |
| 326 | } |
| 327 | } |
| 328 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Delete transport connection from session table |
| 331 | * |
| 332 | * @param table_index session table index |
| 333 | * @param tc transport connection to be removed |
| 334 | * |
| 335 | * @return non-zero if failure |
| 336 | */ |
| 337 | int |
| 338 | session_lookup_del_connection (transport_connection_t * tc) |
| 339 | { |
| 340 | session_table_t *st; |
| 341 | session_kv4_t kv4; |
| 342 | session_kv6_t kv6; |
| 343 | |
| 344 | st = session_table_get_for_connection (tc); |
| 345 | if (!st) |
| 346 | return -1; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 347 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 348 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 349 | make_v4_ss_kv_from_tc (&kv4, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 350 | return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 351 | 0 /* is_add */ ); |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 355 | make_v6_ss_kv_from_tc (&kv6, tc); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 356 | return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 357 | 0 /* is_add */ ); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 358 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 362 | session_lookup_del_session (session_t * s) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 363 | { |
| 364 | transport_connection_t *ts; |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 365 | ts = transport_get_connection (session_get_transport_proto (s), |
| 366 | s->connection_index, s->thread_index); |
Florin Coras | e1e7fb8 | 2019-09-25 07:28:34 -0700 | [diff] [blame] | 367 | if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) |
Nathan Skrzypczak | 2eed1a1 | 2019-07-04 14:26:21 +0200 | [diff] [blame] | 368 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 369 | return session_lookup_del_connection (ts); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 372 | static u8 |
| 373 | session_lookup_action_index_is_valid (u32 action_index) |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 374 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 375 | if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW |
| 376 | || action_index == SESSION_RULES_TABLE_INVALID_INDEX) |
| 377 | return 0; |
| 378 | return 1; |
| 379 | } |
| 380 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 381 | static u64 |
| 382 | session_lookup_action_to_handle (u32 action_index) |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 383 | { |
| 384 | switch (action_index) |
| 385 | { |
| 386 | case SESSION_RULES_TABLE_ACTION_DROP: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 387 | return SESSION_DROP_HANDLE; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 388 | case SESSION_RULES_TABLE_ACTION_ALLOW: |
| 389 | case SESSION_RULES_TABLE_INVALID_INDEX: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 390 | return SESSION_INVALID_HANDLE; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 391 | default: |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 392 | /* application index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 393 | return action_index; |
| 394 | } |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 397 | static session_t * |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 398 | session_lookup_app_listen_session (u32 app_index, u8 fib_proto, |
| 399 | u8 transport_proto) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 400 | { |
| 401 | application_t *app; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 402 | app = application_get_if_valid (app_index); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 403 | if (!app) |
| 404 | return 0; |
| 405 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 406 | return app_worker_first_listener (application_get_default_worker (app), |
| 407 | fib_proto, transport_proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 410 | static session_t * |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 411 | session_lookup_action_to_session (u32 action_index, u8 fib_proto, |
| 412 | u8 transport_proto) |
| 413 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 414 | u32 app_index; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 415 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 416 | /* Nothing sophisticated for now, action index is app index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 417 | return session_lookup_app_listen_session (app_index, fib_proto, |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 418 | transport_proto); |
| 419 | } |
| 420 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 421 | /** UNUSED */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 422 | session_t * |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 423 | session_lookup_rules_table_session4 (session_table_t * st, u8 proto, |
| 424 | ip4_address_t * lcl, u16 lcl_port, |
| 425 | ip4_address_t * rmt, u16 rmt_port) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 426 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 427 | session_rules_table_t *srt = &st->session_rules[proto]; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 428 | u32 action_index, app_index; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 429 | action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 430 | rmt_port); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 431 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 432 | /* Nothing sophisticated for now, action index is app index */ |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 433 | return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4, |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 434 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 437 | /** UNUSED */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 438 | session_t * |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 439 | session_lookup_rules_table_session6 (session_table_t * st, u8 proto, |
| 440 | ip6_address_t * lcl, u16 lcl_port, |
| 441 | ip6_address_t * rmt, u16 rmt_port) |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 442 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 443 | session_rules_table_t *srt = &st->session_rules[proto]; |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 444 | u32 action_index, app_index; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 445 | action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 446 | rmt_port); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 447 | app_index = session_lookup_action_to_handle (action_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 448 | return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6, |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 449 | proto); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 452 | /** |
| 453 | * Lookup listener for session endpoint in table |
| 454 | * |
| 455 | * @param table_index table where the endpoint should be looked up |
| 456 | * @param sep session endpoint to be looked up |
| 457 | * @param use_rules flag that indicates if the session rules of the table |
| 458 | * should be used |
| 459 | * @return invalid handle if nothing is found, the handle of a valid listener |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 460 | * or an action derived handle if a rule is hit |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 461 | */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 462 | u64 |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 463 | session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep, |
| 464 | u8 use_rules) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 465 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 466 | session_rules_table_t *srt; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 467 | session_table_t *st; |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 468 | u32 ai; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 469 | int rv; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 470 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 471 | st = session_table_get (table_index); |
| 472 | if (!st) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 473 | return SESSION_INVALID_HANDLE; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 474 | if (sep->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 475 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 476 | session_kv4_t kv4; |
| 477 | ip4_address_t lcl4; |
| 478 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 479 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 480 | sep->transport_proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 481 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 482 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 483 | return kv4.value; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 484 | if (use_rules) |
| 485 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 486 | clib_memset (&lcl4, 0, sizeof (lcl4)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 487 | srt = &st->session_rules[sep->transport_proto]; |
| 488 | ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0, |
| 489 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 490 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 491 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 492 | } |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 493 | } |
| 494 | else |
| 495 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 496 | session_kv6_t kv6; |
| 497 | ip6_address_t lcl6; |
| 498 | |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 499 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 500 | sep->transport_proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 501 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 502 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 503 | return kv6.value; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 504 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 505 | if (use_rules) |
| 506 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 507 | clib_memset (&lcl6, 0, sizeof (lcl6)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 508 | srt = &st->session_rules[sep->transport_proto]; |
| 509 | ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0, |
| 510 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 511 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 512 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 513 | } |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 514 | } |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 515 | return SESSION_INVALID_HANDLE; |
| 516 | } |
| 517 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 518 | /** |
| 519 | * Look up endpoint in local session table |
| 520 | * |
| 521 | * The result, for now, is an application index and it may in the future |
| 522 | * be extended to a more complicated "action object". The only action we |
| 523 | * emulate now is "drop" and for that we return a special app index. |
| 524 | * |
| 525 | * Lookup logic is to check in order: |
| 526 | * - the rules in the table (connect acls) |
| 527 | * - session sub-table for a listener |
| 528 | * - session sub-table for a local listener (zeroed addr) |
| 529 | * |
| 530 | * @param table_index table where the lookup should be done |
| 531 | * @param sep session endpoint to be looked up |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 532 | * @return session handle that can be interpreted as an adjacency |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 533 | */ |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 534 | u64 |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 535 | session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 536 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 537 | session_rules_table_t *srt; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 538 | session_table_t *st; |
Florin Coras | f0c1c96 | 2017-11-02 21:31:46 -0700 | [diff] [blame] | 539 | u32 ai; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 540 | int rv; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 541 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 542 | st = session_table_get (table_index); |
| 543 | if (!st) |
| 544 | return SESSION_INVALID_INDEX; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 545 | ASSERT (st->is_local); |
| 546 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 547 | if (sep->is_ip4) |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 548 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 549 | session_kv4_t kv4; |
| 550 | ip4_address_t lcl4; |
| 551 | |
| 552 | /* |
| 553 | * Check if endpoint has special rules associated |
| 554 | */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 555 | clib_memset (&lcl4, 0, sizeof (lcl4)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 556 | srt = &st->session_rules[sep->transport_proto]; |
| 557 | ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0, |
| 558 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 559 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 560 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * Check if session endpoint is a listener |
| 564 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 565 | make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, |
| 566 | sep->transport_proto); |
| 567 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 568 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 569 | return kv4.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * Zero out the ip. Logic is that connect to local ips, say |
| 573 | * 127.0.0.1:port, can match 0.0.0.0:port |
| 574 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 575 | if (ip4_is_local_host (&sep->ip.ip4)) |
| 576 | { |
| 577 | kv4.key[0] = 0; |
| 578 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 579 | if (rv == 0) |
| 580 | return kv4.value; |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | kv4.key[0] = 0; |
| 585 | } |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * Zero out the port and check if we have proxy |
| 589 | */ |
| 590 | kv4.key[1] = 0; |
| 591 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 592 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 593 | return kv4.value; |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 594 | } |
| 595 | else |
| 596 | { |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 597 | session_kv6_t kv6; |
| 598 | ip6_address_t lcl6; |
| 599 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 600 | clib_memset (&lcl6, 0, sizeof (lcl6)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 601 | srt = &st->session_rules[sep->transport_proto]; |
| 602 | ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0, |
| 603 | sep->port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 604 | if (session_lookup_action_index_is_valid (ai)) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 605 | return session_lookup_action_to_handle (ai); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 606 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 607 | make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, |
| 608 | sep->transport_proto); |
| 609 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 610 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 611 | return kv6.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Zero out the ip. Same logic as above. |
| 615 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 616 | |
| 617 | if (ip6_is_local_host (&sep->ip.ip6)) |
| 618 | { |
| 619 | kv6.key[0] = kv6.key[1] = 0; |
| 620 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 621 | if (rv == 0) |
| 622 | return kv6.value; |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | kv6.key[0] = kv6.key[1] = 0; |
| 627 | } |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * Zero out the port. Same logic as above. |
| 631 | */ |
| 632 | kv6.key[4] = kv6.key[5] = 0; |
| 633 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 634 | if (rv == 0) |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 635 | return kv6.value; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 636 | } |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 637 | return SESSION_INVALID_HANDLE; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 640 | static inline session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 641 | session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 642 | u16 lcl_port, u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 643 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 644 | session_kv4_t kv4; |
| 645 | int rv; |
| 646 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 647 | /* |
| 648 | * First, try a fully formed listener |
| 649 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 650 | make_v4_listener_kv (&kv4, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 651 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 652 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 653 | return listen_session_get ((u32) kv4.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 654 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 655 | /* |
| 656 | * Zero out the lcl ip and check if any 0/0 port binds have been done |
| 657 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 658 | if (use_wildcard) |
| 659 | { |
| 660 | kv4.key[0] = 0; |
| 661 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 662 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 663 | return listen_session_get ((u32) kv4.value); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 664 | } |
| 665 | else |
| 666 | { |
| 667 | kv4.key[0] = 0; |
| 668 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 669 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 670 | /* |
| 671 | * Zero out port and check if we have a proxy set up for our ip |
| 672 | */ |
| 673 | make_v4_proxy_kv (&kv4, lcl, proto); |
| 674 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 675 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 676 | return listen_session_get ((u32) kv4.value); |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 677 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 681 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 682 | session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port, |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 683 | u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 684 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 685 | session_table_t *st; |
| 686 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 687 | if (!st) |
| 688 | return 0; |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 689 | return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 692 | static session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 693 | session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 694 | u16 lcl_port, u8 proto, u8 ip_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 695 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 696 | session_kv6_t kv6; |
| 697 | int rv; |
| 698 | |
| 699 | make_v6_listener_kv (&kv6, lcl, lcl_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 700 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 701 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 702 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 703 | |
| 704 | /* Zero out the lcl ip */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 705 | if (ip_wildcard) |
| 706 | { |
| 707 | kv6.key[0] = kv6.key[1] = 0; |
| 708 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 709 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 710 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 711 | } |
| 712 | else |
| 713 | { |
| 714 | kv6.key[0] = kv6.key[1] = 0; |
| 715 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 716 | |
Florin Coras | dbd4456 | 2017-11-09 19:30:17 -0800 | [diff] [blame] | 717 | make_v6_proxy_kv (&kv6, lcl, proto); |
| 718 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 719 | if (rv == 0) |
Florin Coras | 5c9083d | 2018-04-13 06:39:07 -0700 | [diff] [blame] | 720 | return listen_session_get ((u32) kv6.value); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 724 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 725 | session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port, |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 726 | u8 proto, u8 use_wildcard) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 727 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 728 | session_table_t *st; |
| 729 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 730 | if (!st) |
| 731 | return 0; |
Yu Ping | b092b77 | 2019-12-27 04:04:33 +0800 | [diff] [blame] | 732 | return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 735 | /** |
| 736 | * Lookup listener, exact or proxy (inaddr_any:0) match |
| 737 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 738 | session_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 739 | session_lookup_listener (u32 table_index, session_endpoint_t * sep) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 740 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 741 | session_table_t *st; |
| 742 | st = session_table_get (table_index); |
| 743 | if (!st) |
| 744 | return 0; |
| 745 | if (sep->is_ip4) |
| 746 | return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 747 | sep->transport_proto, 0); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 748 | else |
| 749 | return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port, |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 750 | sep->transport_proto, 0); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 754 | /** |
| 755 | * Lookup listener wildcard match |
| 756 | */ |
| 757 | session_t * |
| 758 | session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep) |
| 759 | { |
| 760 | session_table_t *st; |
| 761 | st = session_table_get (table_index); |
| 762 | if (!st) |
| 763 | return 0; |
| 764 | if (sep->is_ip4) |
| 765 | return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port, |
| 766 | sep->transport_proto, |
| 767 | 1 /* use_wildcard */ ); |
| 768 | else |
| 769 | return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port, |
| 770 | sep->transport_proto, |
| 771 | 1 /* use_wildcard */ ); |
| 772 | return 0; |
| 773 | } |
| 774 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 775 | int |
| 776 | session_lookup_add_half_open (transport_connection_t * tc, u64 value) |
| 777 | { |
| 778 | session_table_t *st; |
| 779 | session_kv4_t kv4; |
| 780 | session_kv6_t kv6; |
| 781 | |
| 782 | st = session_table_get_or_alloc_for_connection (tc); |
| 783 | if (!st) |
| 784 | return 0; |
| 785 | if (tc->is_ip4) |
| 786 | { |
| 787 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 788 | kv4.value = value; |
| 789 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 790 | 1 /* is_add */ ); |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 795 | kv6.value = value; |
| 796 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 797 | 1 /* is_add */ ); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | int |
| 802 | session_lookup_del_half_open (transport_connection_t * tc) |
| 803 | { |
| 804 | session_table_t *st; |
| 805 | session_kv4_t kv4; |
| 806 | session_kv6_t kv6; |
| 807 | |
| 808 | st = session_table_get_for_connection (tc); |
| 809 | if (!st) |
| 810 | return -1; |
| 811 | if (tc->is_ip4) |
| 812 | { |
| 813 | make_v4_ss_kv_from_tc (&kv4, tc); |
| 814 | return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4, |
| 815 | 0 /* is_add */ ); |
| 816 | } |
| 817 | else |
| 818 | { |
| 819 | make_v6_ss_kv_from_tc (&kv6, tc); |
| 820 | return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6, |
| 821 | 0 /* is_add */ ); |
| 822 | } |
| 823 | } |
| 824 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 825 | u64 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 826 | session_lookup_half_open_handle (transport_connection_t * tc) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 827 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 828 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 829 | session_kv4_t kv4; |
| 830 | session_kv6_t kv6; |
| 831 | int rv; |
| 832 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 833 | st = session_table_get_for_fib_index (transport_connection_fib_proto (tc), |
| 834 | tc->fib_index); |
| 835 | if (!st) |
| 836 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 837 | if (tc->is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 838 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 839 | make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 840 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 841 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 842 | if (rv == 0) |
| 843 | return kv4.value; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 844 | } |
| 845 | else |
| 846 | { |
| 847 | make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 848 | tc->rmt_port, tc->proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 849 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 850 | if (rv == 0) |
| 851 | return kv6.value; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 852 | } |
| 853 | return HALF_OPEN_LOOKUP_INVALID_VALUE; |
| 854 | } |
| 855 | |
| 856 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 857 | session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 858 | { |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 859 | if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 860 | { |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 861 | u32 sst = session_type_from_proto_and_ip (proto, is_ip4); |
| 862 | return transport_get_half_open (sst, handle & 0xFFFFFFFF); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 863 | } |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 867 | /** |
| 868 | * Lookup connection with ip4 and transport layer information |
| 869 | * |
| 870 | * This is used on the fast path so it needs to be fast. Thereby, |
| 871 | * duplication of code and 'hacks' allowed. |
| 872 | * |
| 873 | * The lookup is incremental and returns whenever something is matched. The |
| 874 | * steps are: |
| 875 | * - Try to find an established session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 876 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 877 | * - Try session rules table |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 878 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 879 | * all interfaces) listener session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 880 | * - return 0 |
| 881 | * |
| 882 | * @param fib_index index of fib wherein the connection was received |
| 883 | * @param lcl local ip4 address |
| 884 | * @param rmt remote ip4 address |
| 885 | * @param lcl_port local port |
| 886 | * @param rmt_port remote port |
| 887 | * @param proto transport protocol (e.g., tcp, udp) |
| 888 | * @param thread_index thread index for request |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 889 | * @param is_filtered return flag that indicates if connection was filtered. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 890 | * |
| 891 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 892 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 893 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 894 | session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl, |
| 895 | ip4_address_t * rmt, u16 lcl_port, |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 896 | u16 rmt_port, u8 proto, u32 thread_index, |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 897 | u8 * result) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 898 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 899 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 900 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 901 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 902 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 903 | int rv; |
| 904 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 905 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 906 | if (PREDICT_FALSE (!st)) |
| 907 | return 0; |
| 908 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 909 | /* |
| 910 | * Lookup session amongst established ones |
| 911 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 912 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 913 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 914 | if (rv == 0) |
| 915 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 916 | if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index)) |
| 917 | { |
| 918 | *result = SESSION_LOOKUP_RESULT_WRONG_THREAD; |
| 919 | return 0; |
| 920 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 921 | s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 922 | return transport_get_connection (proto, s->connection_index, |
| 923 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 926 | /* |
| 927 | * Try half-open connections |
| 928 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 929 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 930 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 931 | return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 932 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 933 | /* |
| 934 | * Check the session rules table |
| 935 | */ |
| 936 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 937 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 938 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 939 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 940 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 941 | { |
| 942 | *result = SESSION_LOOKUP_RESULT_FILTERED; |
| 943 | return 0; |
| 944 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 945 | if ((s = session_lookup_action_to_session (action_index, |
| 946 | FIB_PROTOCOL_IP4, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 947 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 948 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /* |
| 952 | * If nothing is found, check if any listener is available |
| 953 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 954 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 955 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 956 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 957 | |
| 958 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 961 | /** |
| 962 | * Lookup connection with ip4 and transport layer information |
| 963 | * |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 964 | * Not optimized. Lookup logic is identical to that of |
| 965 | * @ref session_lookup_connection_wt4 |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 966 | * |
| 967 | * @param fib_index index of the fib wherein the connection was received |
| 968 | * @param lcl local ip4 address |
| 969 | * @param rmt remote ip4 address |
| 970 | * @param lcl_port local port |
| 971 | * @param rmt_port remote port |
| 972 | * @param proto transport protocol (e.g., tcp, udp) |
| 973 | * |
| 974 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 975 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 976 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 977 | session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl, |
| 978 | ip4_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 979 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 980 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 981 | session_table_t *st; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 982 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 983 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 984 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 985 | int rv; |
| 986 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 987 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 988 | if (PREDICT_FALSE (!st)) |
| 989 | return 0; |
| 990 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 991 | /* |
| 992 | * Lookup session amongst established ones |
| 993 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 994 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 995 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 996 | if (rv == 0) |
| 997 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 998 | s = session_get_from_handle (kv4.value); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 999 | return transport_get_connection (proto, s->connection_index, |
| 1000 | s->thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1003 | /* |
| 1004 | * Try half-open connections |
| 1005 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1006 | rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1007 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1008 | return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1009 | |
| 1010 | /* |
| 1011 | * Check the session rules table |
| 1012 | */ |
| 1013 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 1014 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1015 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1016 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1017 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1018 | return 0; |
| 1019 | if ((s = session_lookup_action_to_session (action_index, |
| 1020 | FIB_PROTOCOL_IP4, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1021 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1022 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * If nothing is found, check if any listener is available |
| 1027 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1028 | s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1029 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1030 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1031 | |
| 1032 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1035 | /** |
| 1036 | * Lookup session with ip4 and transport layer information |
| 1037 | * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1038 | * Important note: this may look into another thread's pool table and |
| 1039 | * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as |
| 1040 | * if needed as soon as possible. |
| 1041 | * |
| 1042 | * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but |
| 1043 | * this returns a session as opposed to a transport connection and it does not |
| 1044 | * try to lookup half-open sessions. |
| 1045 | * |
| 1046 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1047 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1048 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1049 | session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt, |
| 1050 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1051 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1052 | session_table_t *st; |
| 1053 | session_kv4_t kv4; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1054 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1055 | u32 action_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1056 | int rv; |
| 1057 | |
| 1058 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index); |
| 1059 | if (PREDICT_FALSE (!st)) |
| 1060 | return 0; |
| 1061 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1062 | /* |
| 1063 | * Lookup session amongst established ones |
| 1064 | */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1065 | make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto); |
| 1066 | rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4); |
| 1067 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1068 | return session_get_from_handle_safe (kv4.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1069 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1070 | /* |
| 1071 | * Check the session rules table |
| 1072 | */ |
| 1073 | action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl, |
| 1074 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1075 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1076 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1077 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1078 | return 0; |
| 1079 | return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4, |
| 1080 | proto); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * If nothing is found, check if any listener is available |
| 1085 | */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1086 | if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1087 | return s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1088 | |
| 1089 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * Lookup connection with ip6 and transport layer information |
| 1094 | * |
| 1095 | * This is used on the fast path so it needs to be fast. Thereby, |
| 1096 | * duplication of code and 'hacks' allowed. |
| 1097 | * |
| 1098 | * The lookup is incremental and returns whenever something is matched. The |
| 1099 | * steps are: |
| 1100 | * - Try to find an established session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1101 | * - Try to find a half-open connection |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1102 | * - Try session rules table |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1103 | * - Try to find a fully-formed or local source wildcarded (listener bound to |
| 1104 | * all interfaces) listener session |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1105 | * - return 0 |
| 1106 | * |
| 1107 | * @param fib_index index of the fib wherein the connection was received |
| 1108 | * @param lcl local ip6 address |
| 1109 | * @param rmt remote ip6 address |
| 1110 | * @param lcl_port local port |
| 1111 | * @param rmt_port remote port |
| 1112 | * @param proto transport protocol (e.g., tcp, udp) |
| 1113 | * @param thread_index thread index for request |
| 1114 | * |
| 1115 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 1116 | */ |
| 1117 | transport_connection_t * |
| 1118 | session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl, |
| 1119 | ip6_address_t * rmt, u16 lcl_port, |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1120 | u16 rmt_port, u8 proto, u32 thread_index, |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1121 | u8 * result) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1122 | { |
| 1123 | session_table_t *st; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1124 | session_t *s; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1125 | session_kv6_t kv6; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1126 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1127 | int rv; |
| 1128 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1129 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1130 | if (PREDICT_FALSE (!st)) |
| 1131 | return 0; |
| 1132 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1133 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1134 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1135 | if (rv == 0) |
| 1136 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1137 | ASSERT ((u32) (kv6.value >> 32) == thread_index); |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1138 | if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index)) |
| 1139 | { |
| 1140 | *result = SESSION_LOOKUP_RESULT_WRONG_THREAD; |
| 1141 | return 0; |
| 1142 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1143 | s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1144 | return transport_get_connection (proto, s->connection_index, |
| 1145 | thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1148 | /* Try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1149 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1150 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1151 | return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1152 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1153 | /* Check the session rules table */ |
| 1154 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1155 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1156 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1157 | { |
Florin Coras | b5e55a2 | 2019-01-10 12:42:47 -0800 | [diff] [blame] | 1158 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1159 | { |
| 1160 | *result = SESSION_LOOKUP_RESULT_FILTERED; |
| 1161 | return 0; |
| 1162 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1163 | if ((s = session_lookup_action_to_session (action_index, |
| 1164 | FIB_PROTOCOL_IP6, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1165 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1166 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1170 | s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1171 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1172 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1173 | |
| 1174 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1177 | /** |
| 1178 | * Lookup connection with ip6 and transport layer information |
| 1179 | * |
| 1180 | * Not optimized. This is used on the fast path so it needs to be fast. |
| 1181 | * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical |
| 1182 | * to that of @ref session_lookup_connection_wt4 |
| 1183 | * |
| 1184 | * @param fib_index index of the fib wherein the connection was received |
| 1185 | * @param lcl local ip6 address |
| 1186 | * @param rmt remote ip6 address |
| 1187 | * @param lcl_port local port |
| 1188 | * @param rmt_port remote port |
| 1189 | * @param proto transport protocol (e.g., tcp, udp) |
| 1190 | * |
| 1191 | * @return pointer to transport connection, if one is found, 0 otherwise |
| 1192 | */ |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1193 | transport_connection_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1194 | session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl, |
| 1195 | ip6_address_t * rmt, u16 lcl_port, u16 rmt_port, |
| 1196 | u8 proto) |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1197 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1198 | session_table_t *st; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1199 | session_t *s; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1200 | session_kv6_t kv6; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1201 | u32 action_index; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1202 | int rv; |
| 1203 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1204 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1205 | if (PREDICT_FALSE (!st)) |
| 1206 | return 0; |
| 1207 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1208 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1209 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1210 | if (rv == 0) |
| 1211 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1212 | s = session_get_from_handle (kv6.value); |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1213 | return transport_get_connection (proto, s->connection_index, |
| 1214 | s->thread_index); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1217 | /* Try half-open connections */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1218 | rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1219 | if (rv == 0) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1220 | return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1221 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1222 | /* Check the session rules table */ |
| 1223 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1224 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1225 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1226 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1227 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1228 | return 0; |
| 1229 | if ((s = session_lookup_action_to_session (action_index, |
| 1230 | FIB_PROTOCOL_IP6, proto))) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1231 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1232 | return 0; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1236 | s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1237 | if (s) |
Florin Coras | 1ee7830 | 2019-02-05 15:51:15 -0800 | [diff] [blame] | 1238 | return transport_get_listener (proto, s->connection_index); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1239 | |
| 1240 | return 0; |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1243 | /** |
| 1244 | * Lookup session with ip6 and transport layer information |
| 1245 | * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1246 | * Important note: this may look into another thread's pool table and |
| 1247 | * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as |
| 1248 | * if needed as soon as possible. |
| 1249 | * |
| 1250 | * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but |
| 1251 | * this returns a session as opposed to a transport connection and it does not |
| 1252 | * try to lookup half-open sessions. |
| 1253 | * |
| 1254 | * Typically used by dgram connections |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1255 | */ |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1256 | session_t * |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1257 | session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt, |
| 1258 | u16 lcl_port, u16 rmt_port, u8 proto) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1259 | { |
| 1260 | session_table_t *st; |
| 1261 | session_kv6_t kv6; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1262 | session_t *s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1263 | u32 action_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1264 | int rv; |
| 1265 | |
| 1266 | st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index); |
| 1267 | if (PREDICT_FALSE (!st)) |
| 1268 | return 0; |
| 1269 | |
| 1270 | make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto); |
| 1271 | rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6); |
| 1272 | if (rv == 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1273 | return session_get_from_handle_safe (kv6.value); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1274 | |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1275 | /* Check the session rules table */ |
| 1276 | action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl, |
| 1277 | rmt, lcl_port, rmt_port); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1278 | if (session_lookup_action_index_is_valid (action_index)) |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1279 | { |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1280 | if (action_index == SESSION_RULES_TABLE_ACTION_DROP) |
| 1281 | return 0; |
| 1282 | return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6, |
| 1283 | proto); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1286 | /* If nothing is found, check if any listener is available */ |
Florin Coras | 477e91a | 2018-02-27 10:05:57 -0800 | [diff] [blame] | 1287 | if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1))) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1288 | return s; |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1289 | return 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1292 | int |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1293 | vnet_session_rule_add_del (session_rule_add_del_args_t * args) |
| 1294 | { |
| 1295 | app_namespace_t *app_ns = app_namespace_get (args->appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1296 | session_rules_table_t *srt; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1297 | session_table_t *st; |
| 1298 | u32 fib_index; |
| 1299 | u8 fib_proto; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1300 | int rv = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1301 | |
| 1302 | if (!app_ns) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1303 | return VNET_API_ERROR_APP_INVALID_NS; |
| 1304 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1305 | if (args->scope > 3) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1306 | return VNET_API_ERROR_INVALID_VALUE; |
| 1307 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1308 | if (args->transport_proto != TRANSPORT_PROTO_TCP |
| 1309 | && args->transport_proto != TRANSPORT_PROTO_UDP) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1310 | return VNET_API_ERROR_INVALID_VALUE; |
| 1311 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1312 | if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0) |
| 1313 | { |
| 1314 | fib_proto = args->table_args.rmt.fp_proto; |
| 1315 | fib_index = app_namespace_get_fib_index (app_ns, fib_proto); |
| 1316 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1317 | srt = &st->session_rules[args->transport_proto]; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1318 | if ((rv = session_rules_table_add_del (srt, &args->table_args))) |
| 1319 | return rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1320 | } |
| 1321 | if (args->scope & SESSION_RULE_SCOPE_LOCAL) |
| 1322 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1323 | clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl)); |
Florin Coras | a2ff7b8 | 2017-11-08 17:55:03 -0800 | [diff] [blame] | 1324 | args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto; |
| 1325 | args->table_args.lcl_port = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1326 | st = app_namespace_get_local_table (app_ns); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1327 | srt = &st->session_rules[args->transport_proto]; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1328 | rv = session_rules_table_add_del (srt, &args->table_args); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1329 | } |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1330 | return rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1333 | /** |
| 1334 | * Mark (global) tables as pertaining to app ns |
| 1335 | */ |
| 1336 | void |
| 1337 | session_lookup_set_tables_appns (app_namespace_t * app_ns) |
| 1338 | { |
| 1339 | session_table_t *st; |
| 1340 | u32 fib_index; |
| 1341 | u8 fp; |
| 1342 | |
| 1343 | for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++) |
| 1344 | { |
| 1345 | fib_index = app_namespace_get_fib_index (app_ns, fp); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 1346 | st = session_table_get_or_alloc (fp, fib_index); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1347 | if (st) |
| 1348 | st->appns_index = app_namespace_index (app_ns); |
| 1349 | } |
| 1350 | } |
| 1351 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1352 | u8 * |
| 1353 | format_ip4_session_lookup_kvp (u8 * s, va_list * args) |
| 1354 | { |
| 1355 | clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1356 | u32 is_local = va_arg (*args, u32); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1357 | v4_connection_key_t *key = (v4_connection_key_t *) kvp->key; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 1358 | session_t *session; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1359 | app_worker_t *app_wrk; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1360 | const u8 *app_name; |
| 1361 | u8 *str = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1362 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1363 | if (!is_local) |
| 1364 | { |
| 1365 | session = session_get_from_handle (kvp->value); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1366 | app_wrk = app_worker_get (session->app_wrk_index); |
| 1367 | app_name = application_name_from_index (app_wrk->app_index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1368 | str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short, |
| 1369 | key->proto, format_ip4_address, &key->src, |
| 1370 | clib_net_to_host_u16 (key->src_port), format_ip4_address, |
| 1371 | &key->dst, clib_net_to_host_u16 (key->dst_port)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1372 | s = format (s, "%-40v%-30v", str, app_name); |
| 1373 | } |
| 1374 | else |
| 1375 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1376 | session = session_get_from_handle (kvp->value); |
| 1377 | app_wrk = app_worker_get (session->app_wrk_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1378 | app_name = application_name_from_index (app_wrk->app_index); |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 1379 | str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto, |
| 1380 | format_ip4_address, &key->src, |
| 1381 | clib_net_to_host_u16 (key->src_port)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1382 | s = format (s, "%-30v%-30v", str, app_name); |
| 1383 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1384 | return s; |
| 1385 | } |
| 1386 | |
| 1387 | typedef struct _ip4_session_table_show_ctx_t |
| 1388 | { |
| 1389 | vlib_main_t *vm; |
| 1390 | u8 is_local; |
| 1391 | } ip4_session_table_show_ctx_t; |
| 1392 | |
| 1393 | static int |
| 1394 | ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg) |
| 1395 | { |
| 1396 | ip4_session_table_show_ctx_t *ctx = arg; |
| 1397 | vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp, |
| 1398 | ctx->is_local); |
| 1399 | return 1; |
| 1400 | } |
| 1401 | |
| 1402 | void |
| 1403 | session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table, |
| 1404 | u8 type, u8 is_local) |
| 1405 | { |
| 1406 | ip4_session_table_show_ctx_t ctx = { |
| 1407 | .vm = vm, |
| 1408 | .is_local = is_local, |
| 1409 | }; |
| 1410 | if (!is_local) |
| 1411 | vlib_cli_output (vm, "%-40s%-30s", "Session", "Application"); |
| 1412 | else |
| 1413 | vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application"); |
| 1414 | switch (type) |
| 1415 | { |
| 1416 | /* main table v4 */ |
| 1417 | case 0: |
| 1418 | ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show, |
| 1419 | &ctx); |
| 1420 | break; |
| 1421 | default: |
| 1422 | clib_warning ("not supported"); |
| 1423 | } |
| 1424 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 1425 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1426 | static clib_error_t * |
| 1427 | session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1428 | vlib_cli_command_t * cmd) |
| 1429 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1430 | u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1431 | u32 appns_index, scope = 0; |
| 1432 | ip46_address_t lcl_ip, rmt_ip; |
| 1433 | u8 is_ip4 = 1, conn_set = 0; |
| 1434 | u8 fib_proto, is_add = 1, *ns_id = 0; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1435 | u8 *tag = 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1436 | app_namespace_t *app_ns; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1437 | int rv; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1438 | |
Guanghua Zhang | fcd5e12 | 2019-08-24 10:52:19 +0800 | [diff] [blame] | 1439 | session_cli_return_if_not_enabled (); |
| 1440 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1441 | clib_memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1442 | clib_memset (&rmt_ip, 0, sizeof (rmt_ip)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1443 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1444 | { |
| 1445 | if (unformat (input, "del")) |
| 1446 | is_add = 0; |
| 1447 | else if (unformat (input, "add")) |
| 1448 | ; |
| 1449 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1450 | ; |
| 1451 | else if (unformat (input, "scope global")) |
| 1452 | scope = SESSION_RULE_SCOPE_GLOBAL; |
| 1453 | else if (unformat (input, "scope local")) |
| 1454 | scope = SESSION_RULE_SCOPE_LOCAL; |
| 1455 | else if (unformat (input, "scope all")) |
| 1456 | scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL; |
| 1457 | else if (unformat (input, "proto %U", unformat_transport_proto, &proto)) |
| 1458 | ; |
| 1459 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1460 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1461 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1462 | &rmt_port)) |
| 1463 | { |
| 1464 | is_ip4 = 1; |
| 1465 | conn_set = 1; |
| 1466 | } |
| 1467 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1468 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1469 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1470 | &rmt_port)) |
| 1471 | { |
| 1472 | is_ip4 = 0; |
| 1473 | conn_set = 1; |
| 1474 | } |
| 1475 | else if (unformat (input, "action %d", &action)) |
| 1476 | ; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1477 | else if (unformat (input, "tag %_%v%_", &tag)) |
| 1478 | ; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1479 | else |
| 1480 | return clib_error_return (0, "unknown input `%U'", |
| 1481 | format_unformat_error, input); |
| 1482 | } |
| 1483 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1484 | if (proto == ~0) |
| 1485 | { |
| 1486 | vlib_cli_output (vm, "proto must be set"); |
| 1487 | return 0; |
| 1488 | } |
| 1489 | if (is_add && !conn_set && action == ~0) |
| 1490 | { |
| 1491 | vlib_cli_output (vm, "connection and action must be set for add"); |
| 1492 | return 0; |
| 1493 | } |
| 1494 | if (!is_add && !tag && !conn_set) |
| 1495 | { |
| 1496 | vlib_cli_output (vm, "connection or tag must be set for delete"); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN) |
| 1500 | { |
| 1501 | vlib_cli_output (vm, "tag too long (max u64)"); |
| 1502 | return 0; |
| 1503 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1504 | |
| 1505 | if (ns_id) |
| 1506 | { |
| 1507 | app_ns = app_namespace_get_from_id (ns_id); |
| 1508 | if (!app_ns) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1509 | { |
| 1510 | vlib_cli_output (vm, "namespace %v does not exist", ns_id); |
| 1511 | return 0; |
| 1512 | } |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1513 | } |
| 1514 | else |
| 1515 | { |
| 1516 | app_ns = app_namespace_get_default (); |
| 1517 | } |
| 1518 | appns_index = app_namespace_index (app_ns); |
| 1519 | |
| 1520 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1521 | session_rule_add_del_args_t args = { |
zhanglimao | a04ebb8 | 2019-03-13 10:36:54 +0800 | [diff] [blame] | 1522 | .transport_proto = proto, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1523 | .table_args.lcl.fp_addr = lcl_ip, |
| 1524 | .table_args.lcl.fp_len = lcl_plen, |
| 1525 | .table_args.lcl.fp_proto = fib_proto, |
| 1526 | .table_args.rmt.fp_addr = rmt_ip, |
| 1527 | .table_args.rmt.fp_len = rmt_plen, |
| 1528 | .table_args.rmt.fp_proto = fib_proto, |
| 1529 | .table_args.lcl_port = lcl_port, |
| 1530 | .table_args.rmt_port = rmt_port, |
| 1531 | .table_args.action_index = action, |
| 1532 | .table_args.is_add = is_add, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1533 | .table_args.tag = tag, |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1534 | .appns_index = appns_index, |
| 1535 | .scope = scope, |
| 1536 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1537 | if ((rv = vnet_session_rule_add_del (&args))) |
| 1538 | return clib_error_return (0, "rule add del returned %u", rv); |
| 1539 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1540 | vec_free (tag); |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 1541 | return 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* *INDENT-OFF* */ |
| 1545 | VLIB_CLI_COMMAND (session_rule_command, static) = |
| 1546 | { |
| 1547 | .path = "session rule", |
| 1548 | .short_help = "session rule [add|del] appns <ns_id> proto <proto> " |
| 1549 | "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>", |
| 1550 | .function = session_rule_command_fn, |
| 1551 | }; |
| 1552 | /* *INDENT-ON* */ |
| 1553 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1554 | void |
| 1555 | session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto, |
| 1556 | u8 transport_proto) |
| 1557 | { |
| 1558 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1559 | session_rules_table_t *srt; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1560 | session_table_t *st; |
| 1561 | st = session_table_get_for_fib_index (fib_index, fib_proto); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1562 | srt = &st->session_rules[transport_proto]; |
| 1563 | session_rules_table_cli_dump (vm, srt, fib_proto); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | void |
| 1567 | session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto, |
| 1568 | u8 transport_proto) |
| 1569 | { |
| 1570 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1571 | session_rules_table_t *srt; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1572 | session_table_t *st; |
| 1573 | st = session_table_get (table_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1574 | srt = &st->session_rules[transport_proto]; |
| 1575 | session_rules_table_cli_dump (vm, srt, fib_proto); |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1578 | static clib_error_t * |
| 1579 | show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1580 | vlib_cli_command_t * cmd) |
| 1581 | { |
| 1582 | u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen; |
| 1583 | u32 fib_index, scope = 0; |
| 1584 | ip46_address_t lcl_ip, rmt_ip; |
| 1585 | u8 is_ip4 = 1, show_one = 0; |
| 1586 | app_namespace_t *app_ns; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1587 | session_rules_table_t *srt; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1588 | session_table_t *st; |
| 1589 | u8 *ns_id = 0, fib_proto; |
| 1590 | |
Guanghua Zhang | fcd5e12 | 2019-08-24 10:52:19 +0800 | [diff] [blame] | 1591 | session_cli_return_if_not_enabled (); |
| 1592 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1593 | clib_memset (&lcl_ip, 0, sizeof (lcl_ip)); |
| 1594 | clib_memset (&rmt_ip, 0, sizeof (rmt_ip)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1595 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1596 | { |
| 1597 | if (unformat (input, "%U", unformat_transport_proto, &transport_proto)) |
| 1598 | ; |
| 1599 | else if (unformat (input, "appns %_%v%_", &ns_id)) |
| 1600 | ; |
| 1601 | else if (unformat (input, "scope global")) |
| 1602 | scope = 1; |
| 1603 | else if (unformat (input, "scope local")) |
| 1604 | scope = 2; |
| 1605 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address, |
| 1606 | &lcl_ip.ip4, &lcl_plen, &lcl_port, |
| 1607 | unformat_ip4_address, &rmt_ip.ip4, &rmt_plen, |
| 1608 | &rmt_port)) |
| 1609 | { |
| 1610 | is_ip4 = 1; |
| 1611 | show_one = 1; |
| 1612 | } |
| 1613 | else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address, |
| 1614 | &lcl_ip.ip6, &lcl_plen, &lcl_port, |
| 1615 | unformat_ip6_address, &rmt_ip.ip6, &rmt_plen, |
| 1616 | &rmt_port)) |
| 1617 | { |
| 1618 | is_ip4 = 0; |
| 1619 | show_one = 1; |
| 1620 | } |
| 1621 | else |
| 1622 | return clib_error_return (0, "unknown input `%U'", |
| 1623 | format_unformat_error, input); |
| 1624 | } |
| 1625 | |
| 1626 | if (transport_proto == ~0) |
| 1627 | { |
| 1628 | vlib_cli_output (vm, "transport proto must be set"); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | if (ns_id) |
| 1633 | { |
| 1634 | app_ns = app_namespace_get_from_id (ns_id); |
| 1635 | if (!app_ns) |
| 1636 | { |
| 1637 | vlib_cli_output (vm, "appns %v doesn't exist", ns_id); |
| 1638 | return 0; |
| 1639 | } |
| 1640 | } |
| 1641 | else |
| 1642 | { |
| 1643 | app_ns = app_namespace_get_default (); |
| 1644 | } |
| 1645 | |
| 1646 | if (scope == 1 || scope == 0) |
| 1647 | { |
| 1648 | fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; |
| 1649 | fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 1650 | st = session_table_get_for_fib_index (fib_proto, fib_index); |
| 1651 | } |
| 1652 | else |
| 1653 | { |
| 1654 | st = app_namespace_get_local_table (app_ns); |
| 1655 | } |
| 1656 | |
| 1657 | if (show_one) |
| 1658 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1659 | srt = &st->session_rules[transport_proto]; |
| 1660 | session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip, |
| 1661 | rmt_port, is_ip4); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
| 1664 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1665 | vlib_cli_output (vm, "%U rules table", format_transport_proto, |
| 1666 | transport_proto); |
| 1667 | srt = &st->session_rules[transport_proto]; |
| 1668 | session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4); |
| 1669 | session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1670 | |
| 1671 | vec_free (ns_id); |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | /* *INDENT-OFF* */ |
| 1676 | VLIB_CLI_COMMAND (show_session_rules_command, static) = |
| 1677 | { |
| 1678 | .path = "show session rules", |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 1679 | .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> " |
| 1680 | "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]", |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 1681 | .function = show_session_rules_command_fn, |
| 1682 | }; |
| 1683 | /* *INDENT-ON* */ |
| 1684 | |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1685 | void |
| 1686 | session_lookup_init (void) |
| 1687 | { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Allocate default table and map it to fib_index 0 |
| 1690 | */ |
| 1691 | session_table_t *st = session_table_alloc (); |
| 1692 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0); |
| 1693 | fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1694 | st->active_fib_proto = FIB_PROTOCOL_IP4; |
| 1695 | session_table_init (st, FIB_PROTOCOL_IP4); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1696 | st = session_table_alloc (); |
| 1697 | vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0); |
| 1698 | fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1699 | st->active_fib_proto = FIB_PROTOCOL_IP6; |
| 1700 | session_table_init (st, FIB_PROTOCOL_IP6); |
Florin Coras | 04e5344 | 2017-07-16 17:12:15 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | * fd.io coding-style-patch-verification: ON |
| 1705 | * |
| 1706 | * Local Variables: |
| 1707 | * eval: (c-set-style "gnu") |
| 1708 | * End: |
| 1709 | */ |