Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | * @brief The NAT inline functions |
| 17 | */ |
| 18 | |
| 19 | #ifndef __included_nat_inlines_h__ |
| 20 | #define __included_nat_inlines_h__ |
| 21 | |
| 22 | #include <nat/nat.h> |
| 23 | |
| 24 | always_inline u32 |
| 25 | ip_proto_to_snat_proto (u8 ip_proto) |
| 26 | { |
| 27 | u32 snat_proto = ~0; |
| 28 | |
| 29 | snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto; |
| 30 | snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto; |
| 31 | snat_proto = |
| 32 | (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto; |
| 33 | snat_proto = |
| 34 | (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto; |
| 35 | |
| 36 | return snat_proto; |
| 37 | } |
| 38 | |
| 39 | always_inline u8 |
| 40 | snat_proto_to_ip_proto (snat_protocol_t snat_proto) |
| 41 | { |
| 42 | u8 ip_proto = ~0; |
| 43 | |
| 44 | ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto; |
| 45 | ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto; |
| 46 | ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto; |
| 47 | |
| 48 | return ip_proto; |
| 49 | } |
| 50 | |
| 51 | static_always_inline u8 |
| 52 | icmp_is_error_message (icmp46_header_t * icmp) |
| 53 | { |
| 54 | switch (icmp->type) |
| 55 | { |
| 56 | case ICMP4_destination_unreachable: |
| 57 | case ICMP4_time_exceeded: |
| 58 | case ICMP4_parameter_problem: |
| 59 | case ICMP4_source_quench: |
| 60 | case ICMP4_redirect: |
| 61 | case ICMP4_alternate_host_address: |
| 62 | return 1; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | always_inline u8 |
| 68 | is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node, |
| 69 | u32 sw_if_index0, u32 ip4_addr) |
| 70 | { |
| 71 | snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data; |
| 72 | ip4_address_t *first_int_addr; |
| 73 | |
| 74 | if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0)) |
| 75 | { |
| 76 | first_int_addr = |
| 77 | ip4_interface_first_address (sm->ip4_main, sw_if_index0, |
| 78 | 0 /* just want the address */ ); |
| 79 | rt->cached_sw_if_index = sw_if_index0; |
| 80 | if (first_int_addr) |
| 81 | rt->cached_ip4_address = first_int_addr->as_u32; |
| 82 | else |
| 83 | rt->cached_ip4_address = 0; |
| 84 | } |
| 85 | |
| 86 | if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address)) |
| 87 | return 1; |
| 88 | else |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | always_inline u8 |
| 93 | maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index) |
| 94 | { |
| 95 | if (pool_elts (sm->per_thread_data[thread_index].sessions) >= |
| 96 | sm->max_translations) |
| 97 | return 1; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | always_inline void |
| 103 | nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector, |
| 104 | vlib_node_runtime_t * node, vlib_error_t * error, |
| 105 | u32 next) |
| 106 | { |
| 107 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
| 108 | |
| 109 | from = bi_vector; |
| 110 | n_left_from = vec_len (bi_vector); |
| 111 | next_index = node->cached_next_index; |
| 112 | while (n_left_from > 0) |
| 113 | { |
| 114 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 115 | while (n_left_from > 0 && n_left_to_next > 0) |
| 116 | { |
| 117 | u32 bi0 = to_next[0] = from[0]; |
| 118 | from += 1; |
| 119 | n_left_from -= 1; |
| 120 | to_next += 1; |
| 121 | n_left_to_next -= 1; |
| 122 | vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0); |
Juraj Sloboda | fe0aa76 | 2018-07-23 12:22:54 +0200 | [diff] [blame^] | 123 | if (error) |
| 124 | p0->error = *error; |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 125 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 126 | n_left_to_next, bi0, next); |
| 127 | } |
| 128 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | always_inline void |
| 133 | user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static) |
| 134 | { |
| 135 | if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user) |
| 136 | { |
| 137 | if (is_static) |
| 138 | u->nstaticsessions++; |
| 139 | else |
| 140 | u->nsessions++; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | always_inline void |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 145 | nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u, |
| 146 | u32 thread_index) |
| 147 | { |
| 148 | clib_bihash_kv_8_8_t kv; |
| 149 | snat_user_key_t u_key; |
| 150 | snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data, |
| 151 | thread_index); |
| 152 | |
| 153 | if (u->nstaticsessions == 0 && u->nsessions == 0) |
| 154 | { |
| 155 | u_key.addr.as_u32 = u->addr.as_u32; |
| 156 | u_key.fib_index = u->fib_index; |
| 157 | kv.key = u_key.as_u64; |
| 158 | pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index); |
| 159 | pool_put (tsm->users, u); |
| 160 | clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | always_inline void |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 165 | nat44_delete_session (snat_main_t * sm, snat_session_t * ses, |
| 166 | u32 thread_index) |
| 167 | { |
| 168 | snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data, |
| 169 | thread_index); |
| 170 | clib_bihash_kv_8_8_t kv, value; |
| 171 | snat_user_key_t u_key; |
| 172 | snat_user_t *u; |
| 173 | |
| 174 | nat_log_debug ("session deleted %U", format_snat_session, tsm, ses); |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 175 | |
| 176 | clib_dlist_remove (tsm->list_pool, ses->per_user_index); |
| 177 | pool_put_index (tsm->list_pool, ses->per_user_index); |
| 178 | pool_put (tsm->sessions, ses); |
| 179 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 180 | u_key.addr = ses->in2out.addr; |
| 181 | u_key.fib_index = ses->in2out.fib_index; |
| 182 | kv.key = u_key.as_u64; |
| 183 | if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 184 | { |
| 185 | u = pool_elt_at_index (tsm->users, value.value); |
| 186 | if (snat_is_session_static (ses)) |
| 187 | u->nstaticsessions--; |
| 188 | else |
| 189 | u->nsessions--; |
Matus Fabian | 68ba880 | 2018-08-08 05:52:47 -0700 | [diff] [blame] | 190 | |
| 191 | nat44_delete_user_with_no_session (sm, u, thread_index); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 192 | } |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** \brief Set TCP session state. |
| 196 | @return 1 if session was closed, otherwise 0 |
| 197 | */ |
| 198 | always_inline int |
| 199 | nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses, |
| 200 | tcp_header_t * tcp, u32 thread_index) |
| 201 | { |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 202 | if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) && |
| 203 | (ses->state & NAT44_SES_O2I_SYN)) |
| 204 | ses->state = 0; |
| 205 | if (tcp->flags & TCP_FLAG_SYN) |
| 206 | ses->state |= NAT44_SES_I2O_SYN; |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 207 | if (tcp->flags & TCP_FLAG_FIN) |
| 208 | { |
| 209 | ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number); |
| 210 | ses->state |= NAT44_SES_I2O_FIN; |
| 211 | } |
| 212 | if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN)) |
| 213 | { |
| 214 | if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq) |
| 215 | ses->state |= NAT44_SES_O2I_FIN_ACK; |
| 216 | } |
| 217 | if (nat44_is_ses_closed (ses)) |
| 218 | { |
| 219 | nat_log_debug ("TCP close connection %U", format_snat_session, |
| 220 | &sm->per_thread_data[thread_index], ses); |
| 221 | nat_free_session_data (sm, ses, thread_index); |
| 222 | nat44_delete_session (sm, ses, thread_index); |
| 223 | return 1; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | always_inline int |
| 229 | nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses, |
| 230 | tcp_header_t * tcp, u32 thread_index) |
| 231 | { |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 232 | if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) && |
| 233 | (ses->state & NAT44_SES_O2I_SYN)) |
| 234 | ses->state = 0; |
| 235 | if (tcp->flags & TCP_FLAG_SYN) |
| 236 | ses->state |= NAT44_SES_O2I_SYN; |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 237 | if (tcp->flags & TCP_FLAG_FIN) |
| 238 | { |
| 239 | ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number); |
| 240 | ses->state |= NAT44_SES_O2I_FIN; |
| 241 | } |
| 242 | if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN)) |
| 243 | { |
| 244 | if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq) |
| 245 | ses->state |= NAT44_SES_I2O_FIN_ACK; |
| 246 | } |
| 247 | if (nat44_is_ses_closed (ses)) |
| 248 | { |
| 249 | nat_log_debug ("TCP close connection %U", format_snat_session, |
| 250 | &sm->per_thread_data[thread_index], ses); |
| 251 | nat_free_session_data (sm, ses, thread_index); |
| 252 | nat44_delete_session (sm, ses, thread_index); |
| 253 | return 1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 258 | always_inline u32 |
| 259 | nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s) |
| 260 | { |
| 261 | switch (s->in2out.protocol) |
| 262 | { |
| 263 | case SNAT_PROTOCOL_ICMP: |
| 264 | return sm->icmp_timeout; |
| 265 | case SNAT_PROTOCOL_UDP: |
| 266 | return sm->udp_timeout; |
| 267 | case SNAT_PROTOCOL_TCP: |
| 268 | { |
| 269 | if (s->state) |
| 270 | return sm->tcp_transitory_timeout; |
| 271 | else |
| 272 | return sm->tcp_established_timeout; |
| 273 | } |
| 274 | default: |
| 275 | return sm->udp_timeout; |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 281 | always_inline void |
| 282 | nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes) |
| 283 | { |
| 284 | s->last_heard = now; |
| 285 | s->total_pkts++; |
| 286 | s->total_bytes += bytes; |
| 287 | } |
| 288 | |
| 289 | /** \brief Per-user LRU list maintenance */ |
| 290 | always_inline void |
| 291 | nat44_session_update_lru (snat_main_t * sm, snat_session_t * s, |
| 292 | u32 thread_index) |
| 293 | { |
| 294 | clib_dlist_remove (sm->per_thread_data[thread_index].list_pool, |
| 295 | s->per_user_index); |
| 296 | clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool, |
| 297 | s->per_user_list_head_index, s->per_user_index); |
| 298 | } |
| 299 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 300 | always_inline void |
| 301 | make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr, |
| 302 | ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port, |
| 303 | u16 r_port) |
| 304 | { |
| 305 | nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key; |
| 306 | |
| 307 | key->l_addr.as_u32 = l_addr->as_u32; |
| 308 | key->r_addr.as_u32 = r_addr->as_u32; |
| 309 | key->fib_index = fib_index; |
| 310 | key->proto = proto; |
| 311 | key->l_port = l_port; |
| 312 | key->r_port = r_port; |
| 313 | |
| 314 | kv->value = ~0ULL; |
| 315 | } |
| 316 | |
| 317 | always_inline void |
| 318 | make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto, |
| 319 | u32 fib_index, u16 port) |
| 320 | { |
| 321 | snat_session_key_t key; |
| 322 | |
| 323 | key.addr.as_u32 = addr->as_u32; |
| 324 | key.port = port; |
| 325 | key.protocol = proto; |
| 326 | key.fib_index = fib_index; |
| 327 | |
| 328 | kv->key = key.as_u64; |
| 329 | kv->value = ~0ULL; |
| 330 | } |
| 331 | |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 332 | always_inline void |
| 333 | mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum) |
| 334 | { |
| 335 | u8 *data; |
| 336 | u8 opt_len, opts_len, kind; |
| 337 | u16 mss; |
| 338 | |
| 339 | if (!(sm->mss_clamping && tcp_syn (tcp))) |
| 340 | return; |
| 341 | |
| 342 | opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t); |
| 343 | data = (u8 *) (tcp + 1); |
| 344 | for (; opts_len > 0; opts_len -= opt_len, data += opt_len) |
| 345 | { |
| 346 | kind = data[0]; |
| 347 | |
| 348 | if (kind == TCP_OPTION_EOL) |
| 349 | break; |
| 350 | else if (kind == TCP_OPTION_NOOP) |
| 351 | { |
| 352 | opt_len = 1; |
| 353 | continue; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | if (opts_len < 2) |
| 358 | return; |
| 359 | opt_len = data[1]; |
| 360 | |
| 361 | if (opt_len < 2 || opt_len > opts_len) |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if (kind == TCP_OPTION_MSS) |
| 366 | { |
| 367 | mss = *(u16 *) (data + 2); |
| 368 | if (clib_net_to_host_u16 (mss) > sm->mss_clamping) |
| 369 | { |
| 370 | *sum = |
| 371 | ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t, |
| 372 | length); |
| 373 | clib_memcpy (data + 2, &sm->mss_value_net, 2); |
| 374 | } |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 380 | #endif /* __included_nat_inlines_h__ */ |
| 381 | |
| 382 | /* |
| 383 | * fd.io coding-style-patch-verification: ON |
| 384 | * |
| 385 | * Local Variables: |
| 386 | * eval: (c-set-style "gnu") |
| 387 | * End: |
| 388 | */ |