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__
25 ip_proto_to_snat_proto (u8 ip_proto)
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;
32 (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
34 (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
40 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
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;
51 static_always_inline u8
52 icmp_is_error_message (icmp46_header_t * icmp)
56 case ICMP4_destination_unreachable:
57 case ICMP4_time_exceeded:
58 case ICMP4_parameter_problem:
59 case ICMP4_source_quench:
61 case ICMP4_alternate_host_address:
68 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
69 u32 sw_if_index0, u32 ip4_addr)
71 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
72 ip4_address_t *first_int_addr;
74 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
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;
81 rt->cached_ip4_address = first_int_addr->as_u32;
83 rt->cached_ip4_address = 0;
86 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
93 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
95 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
103 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
104 vlib_node_runtime_t * node, vlib_error_t * error,
107 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
110 n_left_from = vec_len (bi_vector);
111 next_index = node->cached_next_index;
112 while (n_left_from > 0)
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)
117 u32 bi0 = to_next[0] = from[0];
122 vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
124 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
125 n_left_to_next, bi0, next);
127 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
132 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
134 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
137 u->nstaticsessions++;
144 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
147 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
149 clib_bihash_kv_8_8_t kv, value;
150 snat_user_key_t u_key;
153 nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
154 u_key.addr = ses->in2out.addr;
155 u_key.fib_index = ses->in2out.fib_index;
156 kv.key = u_key.as_u64;
157 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
159 u = pool_elt_at_index (tsm->users, value.value);
160 if (snat_is_session_static (ses))
161 u->nstaticsessions--;
165 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
166 pool_put_index (tsm->list_pool, ses->per_user_index);
167 pool_put (tsm->sessions, ses);
170 /** \brief Set TCP session state.
171 @return 1 if session was closed, otherwise 0
174 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
175 tcp_header_t * tcp, u32 thread_index)
177 if (tcp->flags & TCP_FLAG_FIN)
179 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
180 ses->state |= NAT44_SES_I2O_FIN;
182 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
184 if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
185 ses->state |= NAT44_SES_O2I_FIN_ACK;
187 if (nat44_is_ses_closed (ses))
189 nat_log_debug ("TCP close connection %U", format_snat_session,
190 &sm->per_thread_data[thread_index], ses);
191 nat_free_session_data (sm, ses, thread_index);
192 nat44_delete_session (sm, ses, thread_index);
199 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
200 tcp_header_t * tcp, u32 thread_index)
202 if (tcp->flags & TCP_FLAG_FIN)
204 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
205 ses->state |= NAT44_SES_O2I_FIN;
207 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
209 if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
210 ses->state |= NAT44_SES_I2O_FIN_ACK;
212 if (nat44_is_ses_closed (ses))
214 nat_log_debug ("TCP close connection %U", format_snat_session,
215 &sm->per_thread_data[thread_index], ses);
216 nat_free_session_data (sm, ses, thread_index);
217 nat44_delete_session (sm, ses, thread_index);
224 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
228 s->total_bytes += bytes;
231 /** \brief Per-user LRU list maintenance */
233 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
236 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
238 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
239 s->per_user_list_head_index, s->per_user_index);
242 #endif /* __included_nat_inlines_h__ */
245 * fd.io coding-style-patch-verification: ON
248 * eval: (c-set-style "gnu")