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);
125 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
126 n_left_to_next, bi0, next);
128 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
133 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
135 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
138 u->nstaticsessions++;
145 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
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,
153 if (u->nstaticsessions == 0 && u->nsessions == 0)
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);
165 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
168 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
170 clib_bihash_kv_8_8_t kv, value;
171 snat_user_key_t u_key;
174 nat_log_debug ("session deleted %U", format_snat_session, tsm, ses);
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);
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))
185 u = pool_elt_at_index (tsm->users, value.value);
186 if (snat_is_session_static (ses))
187 u->nstaticsessions--;
191 nat44_delete_user_with_no_session (sm, u, thread_index);
195 /** \brief Set TCP session state.
196 @return 1 if session was closed, otherwise 0
199 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
200 tcp_header_t * tcp, u32 thread_index)
202 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
203 (ses->state & NAT44_SES_O2I_SYN))
205 if (tcp->flags & TCP_FLAG_SYN)
206 ses->state |= NAT44_SES_I2O_SYN;
207 if (tcp->flags & TCP_FLAG_FIN)
209 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
210 ses->state |= NAT44_SES_I2O_FIN;
212 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
214 if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
215 ses->state |= NAT44_SES_O2I_FIN_ACK;
217 if (nat44_is_ses_closed (ses))
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);
229 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
230 tcp_header_t * tcp, u32 thread_index)
232 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
233 (ses->state & NAT44_SES_O2I_SYN))
235 if (tcp->flags & TCP_FLAG_SYN)
236 ses->state |= NAT44_SES_O2I_SYN;
237 if (tcp->flags & TCP_FLAG_FIN)
239 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
240 ses->state |= NAT44_SES_O2I_FIN;
242 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
244 if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
245 ses->state |= NAT44_SES_I2O_FIN_ACK;
247 if (nat44_is_ses_closed (ses))
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);
259 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
261 switch (s->in2out.protocol)
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:
270 return sm->tcp_transitory_timeout;
272 return sm->tcp_established_timeout;
275 return sm->udp_timeout;
282 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes)
286 s->total_bytes += bytes;
289 /** \brief Per-user LRU list maintenance */
291 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
294 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
296 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
297 s->per_user_list_head_index, s->per_user_index);
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,
305 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
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;
311 key->l_port = l_port;
312 key->r_port = r_port;
318 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
319 u32 fib_index, u16 port)
321 snat_session_key_t key;
323 key.addr.as_u32 = addr->as_u32;
325 key.protocol = proto;
326 key.fib_index = fib_index;
328 kv->key = key.as_u64;
333 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
336 u8 opt_len, opts_len, kind;
339 if (!(sm->mss_clamping && tcp_syn (tcp)))
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)
348 if (kind == TCP_OPTION_EOL)
350 else if (kind == TCP_OPTION_NOOP)
361 if (opt_len < 2 || opt_len > opts_len)
365 if (kind == TCP_OPTION_MSS)
367 mss = *(u16 *) (data + 2);
368 if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
371 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
373 clib_memcpy (data + 2, &sm->mss_value_net, 2);
380 #endif /* __included_nat_inlines_h__ */
383 * fd.io coding-style-patch-verification: ON
386 * eval: (c-set-style "gnu")