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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @brief The NAT inline functions
19 #ifndef __included_nat_inlines_h__
20 #define __included_nat_inlines_h__
22 #include <vnet/fib/ip4_fib.h>
26 ip_proto_to_snat_proto (u8 ip_proto)
30 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
31 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
33 (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
35 (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
41 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
45 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
46 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
47 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
52 static_always_inline u8
53 icmp_is_error_message (icmp46_header_t * icmp)
57 case ICMP4_destination_unreachable:
58 case ICMP4_time_exceeded:
59 case ICMP4_parameter_problem:
60 case ICMP4_source_quench:
62 case ICMP4_alternate_host_address:
69 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
70 u32 sw_if_index0, u32 ip4_addr)
72 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
73 ip4_address_t *first_int_addr;
75 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
78 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
79 0 /* just want the address */ );
80 rt->cached_sw_if_index = sw_if_index0;
82 rt->cached_ip4_address = first_int_addr->as_u32;
84 rt->cached_ip4_address = 0;
87 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
94 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
96 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
104 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
105 vlib_node_runtime_t * node, vlib_error_t * error,
108 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
111 n_left_from = vec_len (bi_vector);
112 next_index = node->cached_next_index;
113 while (n_left_from > 0)
115 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
116 while (n_left_from > 0 && n_left_to_next > 0)
118 u32 bi0 = to_next[0] = from[0];
123 vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
126 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
127 n_left_to_next, bi0, next);
129 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
134 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
136 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
139 u->nstaticsessions++;
146 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
149 clib_bihash_kv_8_8_t kv;
150 snat_user_key_t u_key;
151 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
154 if (u->nstaticsessions == 0 && u->nsessions == 0)
156 u_key.addr.as_u32 = u->addr.as_u32;
157 u_key.fib_index = u->fib_index;
158 kv.key = u_key.as_u64;
159 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
160 pool_put (tsm->users, u);
161 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
162 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
163 pool_elts (tsm->users));
168 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
171 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
173 clib_bihash_kv_8_8_t kv, value;
174 snat_user_key_t u_key;
177 nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
179 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
180 pool_put_index (tsm->list_pool, ses->per_user_index);
181 pool_put (tsm->sessions, ses);
182 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
183 pool_elts (tsm->sessions));
185 u_key.addr = ses->in2out.addr;
186 u_key.fib_index = ses->in2out.fib_index;
187 kv.key = u_key.as_u64;
188 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
190 u = pool_elt_at_index (tsm->users, value.value);
191 if (snat_is_session_static (ses))
192 u->nstaticsessions--;
196 nat44_delete_user_with_no_session (sm, u, thread_index);
200 /** \brief Set TCP session state.
201 @return 1 if session was closed, otherwise 0
204 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
205 tcp_header_t * tcp, u32 thread_index)
207 if ((ses->state == 0) && (tcp->flags & TCP_FLAG_RST))
208 ses->state = NAT44_SES_RST;
209 if ((ses->state == NAT44_SES_RST) && !(tcp->flags & TCP_FLAG_RST))
211 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
212 (ses->state & NAT44_SES_O2I_SYN))
214 if (tcp->flags & TCP_FLAG_SYN)
215 ses->state |= NAT44_SES_I2O_SYN;
216 if (tcp->flags & TCP_FLAG_FIN)
218 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
219 ses->state |= NAT44_SES_I2O_FIN;
221 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
223 if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
224 ses->state |= NAT44_SES_O2I_FIN_ACK;
226 if (nat44_is_ses_closed (ses)
227 && !(ses->flags & SNAT_SESSION_FLAG_OUTPUT_FEATURE))
229 nat_log_debug ("TCP close connection %U", format_snat_session,
230 &sm->per_thread_data[thread_index], ses);
231 nat_free_session_data (sm, ses, thread_index);
232 nat44_delete_session (sm, ses, thread_index);
239 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
240 tcp_header_t * tcp, u32 thread_index)
242 if ((ses->state == 0) && (tcp->flags & TCP_FLAG_RST))
243 ses->state = NAT44_SES_RST;
244 if ((ses->state == NAT44_SES_RST) && !(tcp->flags & TCP_FLAG_RST))
246 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
247 (ses->state & NAT44_SES_O2I_SYN))
249 if (tcp->flags & TCP_FLAG_SYN)
250 ses->state |= NAT44_SES_O2I_SYN;
251 if (tcp->flags & TCP_FLAG_FIN)
253 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
254 ses->state |= NAT44_SES_O2I_FIN;
256 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
258 if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
259 ses->state |= NAT44_SES_I2O_FIN_ACK;
261 if (nat44_is_ses_closed (ses))
263 nat_log_debug ("TCP close connection %U", format_snat_session,
264 &sm->per_thread_data[thread_index], ses);
265 nat_free_session_data (sm, ses, thread_index);
266 nat44_delete_session (sm, ses, thread_index);
273 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
275 switch (s->in2out.protocol)
277 case SNAT_PROTOCOL_ICMP:
278 return sm->icmp_timeout;
279 case SNAT_PROTOCOL_UDP:
280 return sm->udp_timeout;
281 case SNAT_PROTOCOL_TCP:
284 return sm->tcp_transitory_timeout;
286 return sm->tcp_established_timeout;
289 return sm->udp_timeout;
296 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
300 s->total_bytes += bytes;
303 /** \brief Per-user LRU list maintenance */
305 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
308 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
310 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
311 s->per_user_list_head_index, s->per_user_index);
315 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
316 ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
319 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
321 key->l_addr.as_u32 = l_addr->as_u32;
322 key->r_addr.as_u32 = r_addr->as_u32;
323 key->fib_index = fib_index;
325 key->l_port = l_port;
326 key->r_port = r_port;
332 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
333 u32 fib_index, u16 port)
335 snat_session_key_t key;
337 key.addr.as_u32 = addr->as_u32;
339 key.protocol = proto;
340 key.fib_index = fib_index;
342 kv->key = key.as_u64;
347 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
350 u8 opt_len, opts_len, kind;
353 if (!(sm->mss_clamping && tcp_syn (tcp)))
356 opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
357 data = (u8 *) (tcp + 1);
358 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
362 if (kind == TCP_OPTION_EOL)
364 else if (kind == TCP_OPTION_NOOP)
375 if (opt_len < 2 || opt_len > opts_len)
379 if (kind == TCP_OPTION_MSS)
381 mss = *(u16 *) (data + 2);
382 if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
385 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
387 clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
395 * @brief Check if packet should be translated
397 * Packets aimed at outside interface and external address with active session
398 * should be translated.
401 * @param rt NAT runtime data
402 * @param sw_if_index0 index of the inside interface
403 * @param ip0 IPv4 header
404 * @param proto0 NAT protocol
405 * @param rx_fib_index0 RX FIB index
407 * @returns 0 if packet should be translated otherwise 1
410 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
411 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
417 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
418 nat_outside_fib_t *outside_fib;
420 .fp_proto = FIB_PROTOCOL_IP4,
423 .ip4.as_u32 = ip0->dst_address.as_u32,
428 /* Don't NAT packet aimed at the intfc address */
429 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
430 ip0->dst_address.as_u32)))
433 fei = fib_table_lookup (rx_fib_index0, &pfx);
434 if (FIB_NODE_INDEX_INVALID != fei)
436 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
437 if (sw_if_index == ~0)
439 vec_foreach (outside_fib, sm->outside_fibs)
441 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
442 if (FIB_NODE_INDEX_INVALID != fei)
444 sw_if_index = fib_entry_get_resolving_interface (fei);
445 if (sw_if_index != ~0)
450 if (sw_if_index == ~0)
454 pool_foreach (i, sm->interfaces, (
456 /* NAT packet aimed at outside interface */
457 if ((nat_interface_is_outside (i))
459 i->sw_if_index)) return 0;}
466 #endif /* __included_nat_inlines_h__ */
469 * fd.io coding-style-patch-verification: ON
472 * eval: (c-set-style "gnu")