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