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_user_with_no_session (snat_main_t * sm, snat_user_t * u,
147 clib_bihash_kv_8_8_t kv;
148 snat_user_key_t u_key;
149 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
152 if (u->nstaticsessions == 0 && u->nsessions == 0)
154 u_key.addr.as_u32 = u->addr.as_u32;
155 u_key.fib_index = u->fib_index;
156 kv.key = u_key.as_u64;
157 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
158 pool_put (tsm->users, u);
159 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
164 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
167 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
169 clib_bihash_kv_8_8_t kv, value;
170 snat_user_key_t u_key;
173 nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
175 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
176 pool_put_index (tsm->list_pool, ses->per_user_index);
177 pool_put (tsm->sessions, ses);
179 u_key.addr = ses->in2out.addr;
180 u_key.fib_index = ses->in2out.fib_index;
181 kv.key = u_key.as_u64;
182 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
184 u = pool_elt_at_index (tsm->users, value.value);
185 if (snat_is_session_static (ses))
186 u->nstaticsessions--;
190 nat44_delete_user_with_no_session (sm, u, thread_index);
194 /** \brief Set TCP session state.
195 @return 1 if session was closed, otherwise 0
198 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
199 tcp_header_t * tcp, u32 thread_index)
201 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
202 (ses->state & NAT44_SES_O2I_SYN))
204 if (tcp->flags & TCP_FLAG_SYN)
205 ses->state |= NAT44_SES_I2O_SYN;
206 if (tcp->flags & TCP_FLAG_FIN)
208 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
209 ses->state |= NAT44_SES_I2O_FIN;
211 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
213 if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
214 ses->state |= NAT44_SES_O2I_FIN_ACK;
216 if (nat44_is_ses_closed (ses))
218 nat_log_debug ("TCP close connection %U", format_snat_session,
219 &sm->per_thread_data[thread_index], ses);
220 nat_free_session_data (sm, ses, thread_index);
221 nat44_delete_session (sm, ses, thread_index);
228 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
229 tcp_header_t * tcp, u32 thread_index)
231 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
232 (ses->state & NAT44_SES_O2I_SYN))
234 if (tcp->flags & TCP_FLAG_SYN)
235 ses->state |= NAT44_SES_O2I_SYN;
236 if (tcp->flags & TCP_FLAG_FIN)
238 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
239 ses->state |= NAT44_SES_O2I_FIN;
241 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
243 if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
244 ses->state |= NAT44_SES_I2O_FIN_ACK;
246 if (nat44_is_ses_closed (ses))
248 nat_log_debug ("TCP close connection %U", format_snat_session,
249 &sm->per_thread_data[thread_index], ses);
250 nat_free_session_data (sm, ses, thread_index);
251 nat44_delete_session (sm, ses, thread_index);
258 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
260 switch (s->in2out.protocol)
262 case SNAT_PROTOCOL_ICMP:
263 return sm->icmp_timeout;
264 case SNAT_PROTOCOL_UDP:
265 return sm->udp_timeout;
266 case SNAT_PROTOCOL_TCP:
269 return sm->tcp_transitory_timeout;
271 return sm->tcp_established_timeout;
274 return sm->udp_timeout;
281 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
285 s->total_bytes += bytes;
288 /** \brief Per-user LRU list maintenance */
290 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
293 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
295 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
296 s->per_user_list_head_index, s->per_user_index);
300 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
301 ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
304 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
306 key->l_addr.as_u32 = l_addr->as_u32;
307 key->r_addr.as_u32 = r_addr->as_u32;
308 key->fib_index = fib_index;
310 key->l_port = l_port;
311 key->r_port = r_port;
317 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
318 u32 fib_index, u16 port)
320 snat_session_key_t key;
322 key.addr.as_u32 = addr->as_u32;
324 key.protocol = proto;
325 key.fib_index = fib_index;
327 kv->key = key.as_u64;
331 #endif /* __included_nat_inlines_h__ */
334 * fd.io coding-style-patch-verification: ON
337 * eval: (c-set-style "gnu")