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>
24 #include <nat/nat_ha.h>
27 ip_proto_to_snat_proto (u8 ip_proto)
31 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
32 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
34 (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
36 (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
42 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
46 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
47 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
48 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
53 static_always_inline u8
54 icmp_is_error_message (icmp46_header_t * icmp)
58 case ICMP4_destination_unreachable:
59 case ICMP4_time_exceeded:
60 case ICMP4_parameter_problem:
61 case ICMP4_source_quench:
63 case ICMP4_alternate_host_address:
70 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
71 u32 sw_if_index0, u32 ip4_addr)
73 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
74 ip4_address_t *first_int_addr;
76 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
79 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
80 0 /* just want the address */ );
81 rt->cached_sw_if_index = sw_if_index0;
83 rt->cached_ip4_address = first_int_addr->as_u32;
85 rt->cached_ip4_address = 0;
88 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
95 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
97 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
105 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
106 vlib_node_runtime_t * node, vlib_error_t * error,
109 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
112 n_left_from = vec_len (bi_vector);
113 next_index = node->cached_next_index;
114 while (n_left_from > 0)
116 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
117 while (n_left_from > 0 && n_left_to_next > 0)
119 u32 bi0 = to_next[0] = from[0];
124 vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
127 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
128 n_left_to_next, bi0, next);
130 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
135 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
137 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
140 u->nstaticsessions++;
147 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
150 clib_bihash_kv_8_8_t kv;
151 snat_user_key_t u_key;
152 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
155 if (u->nstaticsessions == 0 && u->nsessions == 0)
157 u_key.addr.as_u32 = u->addr.as_u32;
158 u_key.fib_index = u->fib_index;
159 kv.key = u_key.as_u64;
160 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
161 pool_put (tsm->users, u);
162 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
163 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
164 pool_elts (tsm->users));
169 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
172 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
174 clib_bihash_kv_8_8_t kv, value;
176 const snat_user_key_t u_key = {
177 .addr = ses->in2out.addr,
178 .fib_index = ses->in2out.fib_index
180 const u8 u_static = snat_is_session_static (ses);
182 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
183 pool_put_index (tsm->list_pool, ses->per_user_index);
184 pool_put (tsm->sessions, ses);
185 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
186 pool_elts (tsm->sessions));
188 kv.key = u_key.as_u64;
189 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
191 u = pool_elt_at_index (tsm->users, value.value);
193 u->nstaticsessions--;
197 nat44_delete_user_with_no_session (sm, u, thread_index);
201 /** \brief Set TCP session state.
202 @return 1 if session was closed, otherwise 0
205 nat44_set_tcp_session_state_i2o (snat_main_t * sm, snat_session_t * ses,
206 tcp_header_t * tcp, u32 thread_index)
208 if ((ses->state == 0) && (tcp->flags & TCP_FLAG_RST))
209 ses->state = NAT44_SES_RST;
210 if ((ses->state == NAT44_SES_RST) && !(tcp->flags & TCP_FLAG_RST))
212 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
213 (ses->state & NAT44_SES_O2I_SYN))
215 if (tcp->flags & TCP_FLAG_SYN)
216 ses->state |= NAT44_SES_I2O_SYN;
217 if (tcp->flags & TCP_FLAG_FIN)
219 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
220 ses->state |= NAT44_SES_I2O_FIN;
222 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
224 if (clib_net_to_host_u32 (tcp->ack_number) > ses->o2i_fin_seq)
225 ses->state |= NAT44_SES_O2I_FIN_ACK;
227 if (nat44_is_ses_closed (ses)
228 && !(ses->flags & SNAT_SESSION_FLAG_OUTPUT_FEATURE))
230 nat_free_session_data (sm, ses, thread_index, 0);
231 nat44_delete_session (sm, ses, thread_index);
238 nat44_set_tcp_session_state_o2i (snat_main_t * sm, snat_session_t * ses,
239 tcp_header_t * tcp, u32 thread_index)
241 if ((ses->state == 0) && (tcp->flags & TCP_FLAG_RST))
242 ses->state = NAT44_SES_RST;
243 if ((ses->state == NAT44_SES_RST) && !(tcp->flags & TCP_FLAG_RST))
245 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
246 (ses->state & NAT44_SES_O2I_SYN))
248 if (tcp->flags & TCP_FLAG_SYN)
249 ses->state |= NAT44_SES_O2I_SYN;
250 if (tcp->flags & TCP_FLAG_FIN)
252 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp->seq_number);
253 ses->state |= NAT44_SES_O2I_FIN;
255 if ((tcp->flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
257 if (clib_net_to_host_u32 (tcp->ack_number) > ses->i2o_fin_seq)
258 ses->state |= NAT44_SES_I2O_FIN_ACK;
260 if (nat44_is_ses_closed (ses))
262 nat_free_session_data (sm, ses, thread_index, 0);
263 nat44_delete_session (sm, ses, thread_index);
270 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
272 switch (s->in2out.protocol)
274 case SNAT_PROTOCOL_ICMP:
275 return sm->icmp_timeout;
276 case SNAT_PROTOCOL_UDP:
277 return sm->udp_timeout;
278 case SNAT_PROTOCOL_TCP:
281 return sm->tcp_transitory_timeout;
283 return sm->tcp_established_timeout;
286 return sm->udp_timeout;
293 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
298 s->total_bytes += bytes;
299 nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
300 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
301 s->total_pkts, s->total_bytes, thread_index,
302 &s->ha_last_refreshed, now);
305 /** \brief Per-user LRU list maintenance */
307 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
310 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
312 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
313 s->per_user_list_head_index, s->per_user_index);
317 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
318 ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
321 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
323 key->l_addr.as_u32 = l_addr->as_u32;
324 key->r_addr.as_u32 = r_addr->as_u32;
325 key->fib_index = fib_index;
327 key->l_port = l_port;
328 key->r_port = r_port;
334 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
335 u32 fib_index, u16 port)
337 snat_session_key_t key;
339 key.addr.as_u32 = addr->as_u32;
341 key.protocol = proto;
342 key.fib_index = fib_index;
344 kv->key = key.as_u64;
348 static_always_inline int
349 get_icmp_i2o_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
351 icmp46_header_t *icmp0;
352 nat_ed_ses_key_t key0;
353 icmp_echo_header_t *echo0, *inner_echo0 = 0;
354 ip4_header_t *inner_ip0 = 0;
356 icmp46_header_t *inner_icmp0;
358 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
359 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
361 if (!icmp_is_error_message (icmp0))
363 key0.proto = IP_PROTOCOL_ICMP;
364 key0.l_addr = ip0->src_address;
365 key0.r_addr = ip0->dst_address;
366 key0.l_port = echo0->identifier;
371 inner_ip0 = (ip4_header_t *) (echo0 + 1);
372 l4_header = ip4_next_header (inner_ip0);
373 key0.proto = inner_ip0->protocol;
374 key0.r_addr = inner_ip0->src_address;
375 key0.l_addr = inner_ip0->dst_address;
376 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
378 case SNAT_PROTOCOL_ICMP:
379 inner_icmp0 = (icmp46_header_t *) l4_header;
380 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
382 key0.l_port = inner_echo0->identifier;
384 case SNAT_PROTOCOL_UDP:
385 case SNAT_PROTOCOL_TCP:
386 key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
387 key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
390 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
398 static_always_inline int
399 get_icmp_o2i_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
401 icmp46_header_t *icmp0;
402 nat_ed_ses_key_t key0;
403 icmp_echo_header_t *echo0, *inner_echo0 = 0;
404 ip4_header_t *inner_ip0;
406 icmp46_header_t *inner_icmp0;
408 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
409 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
411 if (!icmp_is_error_message (icmp0))
413 key0.proto = IP_PROTOCOL_ICMP;
414 key0.l_addr = ip0->dst_address;
415 key0.r_addr = ip0->src_address;
416 key0.l_port = echo0->identifier;
421 inner_ip0 = (ip4_header_t *) (echo0 + 1);
422 l4_header = ip4_next_header (inner_ip0);
423 key0.proto = inner_ip0->protocol;
424 key0.l_addr = inner_ip0->src_address;
425 key0.r_addr = inner_ip0->dst_address;
426 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
428 case SNAT_PROTOCOL_ICMP:
429 inner_icmp0 = (icmp46_header_t *) l4_header;
430 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
431 key0.l_port = inner_echo0->identifier;
434 case SNAT_PROTOCOL_UDP:
435 case SNAT_PROTOCOL_TCP:
436 key0.l_port = ((tcp_udp_header_t *) l4_header)->src_port;
437 key0.r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
448 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
451 u8 opt_len, opts_len, kind;
454 if (!(sm->mss_clamping && tcp_syn (tcp)))
457 opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
458 data = (u8 *) (tcp + 1);
459 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
463 if (kind == TCP_OPTION_EOL)
465 else if (kind == TCP_OPTION_NOOP)
476 if (opt_len < 2 || opt_len > opts_len)
480 if (kind == TCP_OPTION_MSS)
482 mss = *(u16 *) (data + 2);
483 if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
486 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
488 clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
496 * @brief Check if packet should be translated
498 * Packets aimed at outside interface and external address with active session
499 * should be translated.
502 * @param rt NAT runtime data
503 * @param sw_if_index0 index of the inside interface
504 * @param ip0 IPv4 header
505 * @param proto0 NAT protocol
506 * @param rx_fib_index0 RX FIB index
508 * @returns 0 if packet should be translated otherwise 1
511 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
512 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
518 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
519 nat_outside_fib_t *outside_fib;
521 .fp_proto = FIB_PROTOCOL_IP4,
524 .ip4.as_u32 = ip0->dst_address.as_u32,
529 /* Don't NAT packet aimed at the intfc address */
530 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
531 ip0->dst_address.as_u32)))
534 fei = fib_table_lookup (rx_fib_index0, &pfx);
535 if (FIB_NODE_INDEX_INVALID != fei)
537 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
538 if (sw_if_index == ~0)
540 vec_foreach (outside_fib, sm->outside_fibs)
542 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
543 if (FIB_NODE_INDEX_INVALID != fei)
545 sw_if_index = fib_entry_get_resolving_interface (fei);
546 if (sw_if_index != ~0)
551 if (sw_if_index == ~0)
555 pool_foreach (i, sm->interfaces, (
557 /* NAT packet aimed at outside interface */
558 if ((nat_interface_is_outside (i))
560 i->sw_if_index)) return 0;}
567 #endif /* __included_nat_inlines_h__ */
570 * fd.io coding-style-patch-verification: ON
573 * eval: (c-set-style "gnu")