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.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/udp/udp_local.h>
26 #include <vppinfra/error.h>
28 #include <nat/lib/ipfix_logging.h>
30 #include <nat/nat44-ed/nat44_ed.h>
31 #include <nat/nat44-ed/nat44_ed_inlines.h>
33 static char *nat_out2in_ed_error_strings[] = {
34 #define _(sym,string) string,
35 foreach_nat_out2in_ed_error
41 NAT_ED_SP_REASON_NO_REASON,
42 NAT_ED_SP_REASON_LOOKUP_FAILED,
43 NAT_ED_SP_REASON_VRF_EXPIRED,
44 NAT_ED_SP_SESS_EXPIRED,
45 } nat_slow_path_reason_e;
52 nat_translation_error_e translation_error;
55 clib_bihash_kv_16_8_t search_key;
57 u8 translation_via_i2of;
60 nat_slow_path_reason_e slow_path_reason;
61 } nat44_ed_out2in_trace_t;
64 format_slow_path_reason (u8 *s, va_list *args)
66 nat_slow_path_reason_e reason = va_arg (*args, nat_slow_path_reason_e);
69 case NAT_ED_SP_REASON_NO_REASON:
70 return format (s, "no reason for slow path");
71 case NAT_ED_SP_REASON_LOOKUP_FAILED:
72 return format (s, "slow path because lookup failed");
73 case NAT_ED_SP_REASON_VRF_EXPIRED:
74 return format (s, "slow path because vrf expired");
75 case NAT_ED_SP_SESS_EXPIRED:
76 return format (s, "slow path because session expired");
78 return format (s, "invalid reason value");
82 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
84 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
85 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
90 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
91 "NAT44_OUT2IN_ED_FAST_PATH";
93 s = format (s, "%s: sw_if_index %d, next index %d", tag, t->sw_if_index,
95 if (~0 != t->session_index)
97 s = format (s, ", session %d, translation result '%U' via %s",
98 t->session_index, format_nat_ed_translation_error,
100 t->translation_via_i2of ? "i2of" : "o2if");
101 s = format (s, "\n i2of %U", format_nat_6t_flow, &t->i2of);
102 s = format (s, "\n o2if %U", format_nat_6t_flow, &t->o2if);
104 if (!t->is_slow_path)
106 if (t->lookup_skipped)
108 s = format (s, "\n lookup skipped - cached session index used");
112 s = format (s, "\n search key %U", format_ed_session_kvp,
115 s = format (s, "\n %U", format_slow_path_reason, t->slow_path_reason);
117 if (IP_PROTOCOL_TCP == t->i2of.match.proto)
119 s = format (s, "\n TCP state: %U", format_nat44_ed_tcp_state,
127 next_src_nat (snat_main_t *sm, ip4_header_t *ip, u16 src_port, u16 dst_port,
130 clib_bihash_kv_16_8_t kv, value;
132 init_ed_k (&kv, ip->src_address.as_u32, src_port, ip->dst_address.as_u32,
133 dst_port, rx_fib_index, ip->protocol);
134 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
140 static void create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b,
141 snat_session_t *s, ip4_header_t *ip,
142 u32 rx_fib_index, u32 thread_index);
144 static snat_session_t *create_session_for_static_mapping_ed (
145 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
146 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
147 ip_protocol_t proto, vlib_node_runtime_t *node, u32 thread_index,
148 twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
149 snat_static_mapping_t *mapping);
152 icmp_out2in_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
153 icmp46_header_t *icmp, u32 sw_if_index,
154 u32 rx_fib_index, vlib_node_runtime_t *node,
155 u32 next, f64 now, u32 thread_index,
156 snat_session_t **s_p)
158 vlib_main_t *vm = vlib_get_main ();
163 snat_session_t *s = 0;
164 u8 is_addr_only, identity_nat;
165 ip4_address_t sm_addr;
168 snat_static_mapping_t *m;
170 ip4_address_t lookup_saddr, lookup_daddr;
171 u16 lookup_sport, lookup_dport;
173 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
174 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
176 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr, &lookup_sport,
177 &lookup_daddr, &lookup_dport,
180 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
181 next = NAT_NEXT_DROP;
185 if (snat_static_mapping_match (vm, ip->dst_address, lookup_sport,
186 rx_fib_index, ip->protocol, &sm_addr,
187 &sm_port, &sm_fib_index, 1, &is_addr_only, 0,
188 0, 0, &identity_nat, &m))
190 // static mapping not matched
191 if (!sm->forwarding_enabled)
193 /* Don't NAT packet aimed at the intfc address */
194 if (!is_interface_addr (sm, node, sw_if_index,
195 ip->dst_address.as_u32))
197 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
198 next = NAT_NEXT_DROP;
203 if (next_src_nat (sm, ip, lookup_sport, lookup_dport, rx_fib_index))
205 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
209 create_bypass_for_fwd (sm, b, s, ip, rx_fib_index, thread_index);
215 if (PREDICT_FALSE (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
217 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
218 ICMP4_echo_request ||
221 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
222 next = NAT_NEXT_DROP;
226 if (PREDICT_FALSE (identity_nat))
231 /* Create session initiated by host from external network */
232 s = create_session_for_static_mapping_ed (
233 sm, b, sm_addr, sm_port, sm_fib_index, ip->dst_address, lookup_sport,
234 rx_fib_index, lookup_protocol, node, thread_index, 0, 0,
235 vlib_time_now (vm), m);
237 next = NAT_NEXT_DROP;
239 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
241 sum = ip_incremental_checksum_buffer (
242 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
243 ntohs (ip->length) - ip4_header_bytes (ip), 0);
244 checksum = ~ip_csum_fold (sum);
245 if (checksum != 0 && checksum != 0xffff)
247 next = NAT_NEXT_DROP;
252 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
255 nat44_session_update_counters (
256 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
257 /* Per-user LRU list maintenance */
258 nat44_session_update_lru (sm, s, thread_index);
261 if (NAT_NEXT_DROP == next && s)
263 nat_ed_session_delete (sm, s, thread_index, 1);
270 static_always_inline int
271 nat44_ed_alloc_i2o_port (snat_main_t *sm, snat_address_t *a, snat_session_t *s,
272 ip4_address_t i2o_addr, u16 i2o_port,
273 u32 i2o_fib_index, ip_protocol_t proto,
274 u32 thread_index, u32 snat_thread_index,
275 ip4_address_t *outside_addr, u16 *outside_port)
279 for (int i = 0; i < ED_PORT_ALLOC_ATTEMPTS; ++i)
281 portnum = (sm->port_per_thread * snat_thread_index) +
282 snat_random_port (0, sm->port_per_thread - 1) +
284 portnum = clib_host_to_net_u16 (portnum);
285 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port, a->addr,
286 portnum, i2o_fib_index, proto);
287 if (!nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s,
290 *outside_addr = a->addr;
291 *outside_port = portnum;
296 /* Totally out of translations to use... */
297 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
301 static_always_inline int
302 nat44_ed_alloc_i2o_addr_and_port (snat_main_t *sm, snat_address_t *addresses,
303 snat_session_t *s, ip4_address_t i2o_addr,
304 u16 i2o_port, u32 i2o_fib_index,
305 ip_protocol_t proto, u32 thread_index,
306 u32 snat_thread_index,
307 ip4_address_t *outside_addr,
310 snat_address_t *a, *ga = 0;
313 if (vec_len (addresses) > 0)
315 int s_addr_offset = i2o_addr.as_u32 % vec_len (addresses);
317 for (i = s_addr_offset; i < vec_len (addresses); ++i)
320 if (a->fib_index == i2o_fib_index)
322 return nat44_ed_alloc_i2o_port (
323 sm, a, s, i2o_addr, i2o_port, i2o_fib_index, proto,
324 thread_index, snat_thread_index, outside_addr, outside_port);
326 else if (a->fib_index == ~0)
332 for (i = 0; i < s_addr_offset; ++i)
335 if (a->fib_index == i2o_fib_index)
337 return nat44_ed_alloc_i2o_port (
338 sm, a, s, i2o_addr, i2o_port, i2o_fib_index, proto,
339 thread_index, snat_thread_index, outside_addr, outside_port);
341 else if (a->fib_index == ~0)
349 return nat44_ed_alloc_i2o_port (
350 sm, a, s, i2o_addr, i2o_port, i2o_fib_index, proto, thread_index,
351 snat_thread_index, outside_addr, outside_port);
355 /* Totally out of translations to use... */
356 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
360 static snat_session_t *
361 create_session_for_static_mapping_ed (
362 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
363 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
364 ip_protocol_t proto, vlib_node_runtime_t *node, u32 thread_index,
365 twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
366 snat_static_mapping_t *mapping)
370 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
373 nat44_ed_maximum_sessions_exceeded (sm, o2i_fib_index, thread_index)))
375 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
376 nat_elog_notice (sm, "maximum sessions exceeded");
380 s = nat_ed_session_alloc (sm, thread_index, now, proto);
383 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
384 nat_elog_warn (sm, "create NAT session failed");
388 ip = vlib_buffer_get_current (b);
390 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
392 proto == IP_PROTOCOL_ICMP ? 0 : vnet_buffer (b)->ip.reass.l4_src_port;
393 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
395 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
396 if (lb_nat == AFFINITY_LB_NAT)
397 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
398 s->out2in.addr = o2i_addr;
399 s->out2in.port = o2i_port;
400 s->out2in.fib_index = o2i_fib_index;
401 s->in2out.addr = i2o_addr;
402 s->in2out.port = i2o_port;
403 s->in2out.fib_index = i2o_fib_index;
406 if (IP_PROTOCOL_ICMP == proto)
408 nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr, o2i_port,
409 o2i_addr, o2i_port, o2i_fib_index, ip->protocol);
410 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, i2o_port);
414 nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr,
415 s->ext_host_port, o2i_addr, o2i_port,
416 o2i_fib_index, ip->protocol);
417 nat_6t_flow_dport_rewrite_set (&s->o2i, i2o_port);
419 nat_6t_flow_daddr_rewrite_set (&s->o2i, i2o_addr.as_u32);
420 nat_6t_flow_txfib_rewrite_set (&s->o2i, i2o_fib_index);
422 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
424 b->error = node->errors[NAT_OUT2IN_ED_ERROR_HASH_ADD_FAILED];
425 nat_ed_session_delete (sm, s, thread_index, 1);
426 nat_elog_warn (sm, "out2in flow hash add failed");
430 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
431 ip->src_address.as_u32 == i2o_addr.as_u32))
434 snat_address_t *filter = 0;
436 // if exact address is specified use this address
437 if (is_sm_exact_address (mapping->flags))
440 vec_foreach (ap, sm->twice_nat_addresses)
442 if (mapping->pool_addr.as_u32 == ap->addr.as_u32)
452 rc = nat44_ed_alloc_i2o_port (
453 sm, filter, s, i2o_addr, i2o_port, i2o_fib_index, proto,
454 thread_index, tsm->snat_thread_index, &s->ext_host_nat_addr,
455 &s->ext_host_nat_port);
456 s->flags |= SNAT_SESSION_FLAG_EXACT_ADDRESS;
460 rc = nat44_ed_alloc_i2o_addr_and_port (
461 sm, sm->twice_nat_addresses, s, i2o_addr, i2o_port, i2o_fib_index,
462 proto, thread_index, tsm->snat_thread_index, &s->ext_host_nat_addr,
463 &s->ext_host_nat_port);
468 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
469 nat_ed_session_delete (sm, s, thread_index, 1);
473 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
475 nat_6t_flow_saddr_rewrite_set (&s->o2i, s->ext_host_nat_addr.as_u32);
476 if (IP_PROTOCOL_ICMP == proto)
478 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, s->ext_host_nat_port);
482 nat_6t_flow_sport_rewrite_set (&s->o2i, s->ext_host_nat_port);
485 nat_6t_l3_l4_csum_calc (&s->o2i);
487 nat_6t_flow_daddr_rewrite_set (&s->i2o, s->ext_host_addr.as_u32);
488 if (IP_PROTOCOL_ICMP == proto)
490 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, s->ext_host_port);
494 nat_6t_flow_dport_rewrite_set (&s->i2o, s->ext_host_port);
497 nat_6t_flow_saddr_rewrite_set (&s->i2o, o2i_addr.as_u32);
498 if (IP_PROTOCOL_ICMP == proto)
500 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, o2i_port);
504 nat_6t_flow_sport_rewrite_set (&s->i2o, o2i_port);
506 nat_6t_l3_l4_csum_calc (&s->i2o);
510 if (IP_PROTOCOL_ICMP == proto)
512 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
513 s->ext_host_addr, i2o_port, i2o_fib_index,
518 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
519 s->ext_host_addr, s->ext_host_port,
520 i2o_fib_index, ip->protocol);
523 nat_6t_flow_saddr_rewrite_set (&s->i2o, o2i_addr.as_u32);
524 if (IP_PROTOCOL_ICMP == proto)
526 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, o2i_port);
530 nat_6t_flow_sport_rewrite_set (&s->i2o, o2i_port);
533 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
535 nat_elog_notice (sm, "in2out flow hash add failed");
536 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
538 nat_elog_warn (sm, "out2in flow hash del failed");
540 nat_ed_session_delete (sm, s, thread_index, 1);
544 nat_ipfix_logging_nat44_ses_create (
545 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->proto,
546 s->in2out.port, s->out2in.port, s->in2out.fib_index);
548 nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
549 s->in2out.port, &s->ext_host_nat_addr,
550 s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
551 &s->ext_host_addr, s->ext_host_port, s->proto,
552 nat44_ed_is_twice_nat_session (s));
554 per_vrf_sessions_register_session (s, thread_index);
560 create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b, snat_session_t *s,
561 ip4_header_t *ip, u32 rx_fib_index, u32 thread_index)
563 clib_bihash_kv_16_8_t kv, value;
564 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
565 vlib_main_t *vm = vlib_get_main ();
566 f64 now = vlib_time_now (vm);
567 u16 lookup_sport, lookup_dport;
569 ip4_address_t lookup_saddr, lookup_daddr;
571 if (ip->protocol == IP_PROTOCOL_ICMP)
573 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_daddr,
574 &lookup_sport, &lookup_saddr,
575 &lookup_dport, &lookup_protocol))
580 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
582 lookup_sport = vnet_buffer (b)->ip.reass.l4_dst_port;
583 lookup_dport = vnet_buffer (b)->ip.reass.l4_src_port;
590 lookup_saddr.as_u32 = ip->dst_address.as_u32;
591 lookup_daddr.as_u32 = ip->src_address.as_u32;
592 lookup_protocol = ip->protocol;
595 init_ed_k (&kv, lookup_saddr.as_u32, lookup_sport, lookup_daddr.as_u32,
596 lookup_dport, rx_fib_index, lookup_protocol);
598 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
600 ASSERT (thread_index == ed_value_get_thread_index (&value));
602 pool_elt_at_index (tsm->sessions,
603 ed_value_get_session_index (&value));
605 else if (ip->protocol == IP_PROTOCOL_ICMP &&
606 icmp_type_is_error_message
607 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
614 (nat44_ed_maximum_sessions_exceeded
615 (sm, rx_fib_index, thread_index)))
618 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
621 nat_elog_warn (sm, "create NAT session failed");
625 s->ext_host_addr = ip->src_address;
626 s->ext_host_port = lookup_dport;
627 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
628 s->out2in.addr = ip->dst_address;
629 s->out2in.port = lookup_sport;
630 s->proto = ip->protocol;
631 s->out2in.fib_index = rx_fib_index;
632 s->in2out.addr = s->out2in.addr;
633 s->in2out.port = s->out2in.port;
634 s->in2out.fib_index = s->out2in.fib_index;
636 nat_6t_i2o_flow_init (sm, thread_index, s, ip->dst_address, lookup_sport,
637 ip->src_address, lookup_dport, rx_fib_index,
639 nat_6t_flow_txfib_rewrite_set (&s->i2o, rx_fib_index);
640 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
642 nat_elog_notice (sm, "in2out flow add failed");
643 nat_ed_session_delete (sm, s, thread_index, 1);
647 per_vrf_sessions_register_session (s, thread_index);
650 if (ip->protocol == IP_PROTOCOL_TCP)
652 nat44_set_tcp_session_state_o2i (
653 sm, now, s, vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags,
658 nat44_session_update_counters (s, now, 0, thread_index);
659 /* Per-user LRU list maintenance */
660 nat44_session_update_lru (sm, s, thread_index);
663 static snat_session_t *
664 nat44_ed_out2in_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
665 ip4_header_t *ip, u32 rx_fib_index,
666 u32 thread_index, f64 now,
668 vlib_node_runtime_t *node)
670 snat_static_mapping_t *m;
674 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
676 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
677 nat_elog_notice (sm, "maximum sessions exceeded");
681 m = nat44_ed_sm_o2i_lookup (sm, ip->dst_address, 0, 0, ip->protocol);
684 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
688 /* Create a new session */
689 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
692 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
693 nat_elog_warn (sm, "create NAT session failed");
697 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
698 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
699 s->out2in.addr.as_u32 = ip->dst_address.as_u32;
700 s->out2in.fib_index = rx_fib_index;
701 s->in2out.addr.as_u32 = m->local_addr.as_u32;
702 s->in2out.fib_index = m->fib_index;
703 s->in2out.port = s->out2in.port = ip->protocol;
705 nat_6t_o2i_flow_init (sm, thread_index, s, ip->dst_address, 0,
706 ip->src_address, 0, m->fib_index, ip->protocol);
707 nat_6t_flow_saddr_rewrite_set (&s->i2o, ip->dst_address.as_u32);
708 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
710 nat_elog_notice (sm, "in2out key add failed");
711 nat_ed_session_delete (sm, s, thread_index, 1);
715 nat_6t_o2i_flow_init (sm, thread_index, s, ip->src_address, 0,
716 ip->dst_address, 0, rx_fib_index, ip->protocol);
717 nat_6t_flow_daddr_rewrite_set (&s->o2i, m->local_addr.as_u32);
718 nat_6t_flow_txfib_rewrite_set (&s->o2i, m->fib_index);
719 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
721 nat_elog_notice (sm, "out2in flow hash add failed");
722 nat_ed_session_delete (sm, s, thread_index, 1);
726 per_vrf_sessions_register_session (s, thread_index);
729 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
731 /* Per-user LRU list maintenance */
732 nat44_session_update_lru (sm, s, thread_index);
738 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
739 vlib_node_runtime_t * node,
740 vlib_frame_t * frame,
743 u32 n_left_from, *from;
744 snat_main_t *sm = &snat_main;
745 f64 now = vlib_time_now (vm);
746 u32 thread_index = vm->thread_index;
747 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
749 from = vlib_frame_vector_args (frame);
750 n_left_from = frame->n_vectors;
752 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
753 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
754 vlib_get_buffers (vm, from, b, n_left_from);
756 while (n_left_from > 0)
759 u32 sw_if_index0, rx_fib_index0;
760 ip_protocol_t proto0;
762 snat_session_t *s0 = 0;
763 clib_bihash_kv_16_8_t kv0 = {}, value0;
764 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
765 nat_slow_path_reason_e slow_path_reason = NAT_ED_SP_REASON_NO_REASON;
766 nat_6t_flow_t *f = 0;
768 int lookup_skipped = 0;
773 /* Prefetch next iteration. */
774 if (PREDICT_TRUE (n_left_from >= 2))
780 vlib_prefetch_buffer_header (p2, LOAD);
782 clib_prefetch_load (p2->data);
785 next[0] = vnet_buffer2 (b0)->nat.arc_next;
787 lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
788 lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
790 vnet_buffer (b0)->snat.flags = 0;
791 ip0 = vlib_buffer_get_current (b0);
793 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
795 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
797 lookup.fib_index = rx_fib_index0;
799 if (PREDICT_FALSE (ip0->ttl == 1))
801 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
802 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
803 ICMP4_time_exceeded_ttl_exceeded_in_transit,
805 next[0] = NAT_NEXT_ICMP_ERROR;
809 proto0 = ip0->protocol;
811 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
813 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
814 ICMP4_echo_request &&
815 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
817 !icmp_type_is_error_message (
818 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
820 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
821 next[0] = NAT_NEXT_DROP;
824 int err = nat_get_icmp_session_lookup_values (
825 b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
826 &lookup.dport, &lookup.proto);
829 b0->error = node->errors[err];
830 next[0] = NAT_NEXT_DROP;
836 lookup.saddr.as_u32 = ip0->src_address.as_u32;
837 lookup.daddr.as_u32 = ip0->dst_address.as_u32;
838 lookup.proto = ip0->protocol;
841 /* there might be a stashed index in vnet_buffer2 from handoff or
842 * classify node, see if it can be used */
843 if (is_multi_worker &&
844 !pool_is_free_index (tsm->sessions,
845 vnet_buffer2 (b0)->nat.cached_session_index))
847 s0 = pool_elt_at_index (tsm->sessions,
848 vnet_buffer2 (b0)->nat.cached_session_index);
849 if (PREDICT_TRUE (nat_6t_t_eq (&s0->o2i.match, &lookup)) ||
850 (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
851 nat_6t_t_eq (&s0->i2o.match, &lookup)))
853 /* yes, this is the droid we're looking for */
860 init_ed_k (&kv0, lookup.saddr.as_u32, lookup.sport, lookup.daddr.as_u32,
861 lookup.dport, lookup.fib_index, lookup.proto);
864 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
866 // flow does not exist go slow path
867 slow_path_reason = NAT_ED_SP_REASON_LOOKUP_FAILED;
868 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
871 ASSERT (thread_index == ed_value_get_thread_index (&value0));
873 pool_elt_at_index (tsm->sessions,
874 ed_value_get_session_index (&value0));
877 ASSERT (thread_index == s0->thread_index);
879 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
881 // session is closed, go slow path
882 nat44_ed_free_session_data (sm, s0, thread_index, 0);
883 nat_ed_session_delete (sm, s0, thread_index, 1);
884 slow_path_reason = NAT_ED_SP_REASON_VRF_EXPIRED;
885 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
889 // drop if session expired
890 u64 sess_timeout_time;
892 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
893 if (now >= sess_timeout_time)
895 // session is closed, go slow path
896 nat44_ed_free_session_data (sm, s0, thread_index, 0);
897 nat_ed_session_delete (sm, s0, thread_index, 1);
898 slow_path_reason = NAT_ED_SP_SESS_EXPIRED;
899 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
903 if (nat_6t_t_eq (&s0->o2i.match, &lookup))
907 else if (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
908 nat_6t_t_eq (&s0->i2o.match, &lookup))
915 * Send DHCP packets to the ipv4 stack, or we won't
916 * be able to use dhcp client on the outside interface
919 proto0 == IP_PROTOCOL_UDP &&
920 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
921 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
926 if (!sm->forwarding_enabled)
928 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
929 next[0] = NAT_NEXT_DROP;
934 if (nat_6t_t_eq (&s0->i2o.match, &lookup))
940 // FIXME TODO bypass ???
941 // create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
943 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
944 nat44_ed_free_session_data (sm, s0, thread_index, 0);
945 nat_ed_session_delete (sm, s0, thread_index, 1);
946 next[0] = NAT_NEXT_DROP;
947 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TRNSL_FAILED];
953 if (NAT_ED_TRNSL_ERR_SUCCESS !=
954 (translation_error = nat_6t_flow_buf_translate_o2i (
955 vm, sm, b0, ip0, f, proto0, 0 /* is_output_feature */)))
957 next[0] = NAT_NEXT_DROP;
958 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TRNSL_FAILED];
964 case IP_PROTOCOL_TCP:
965 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.tcp,
966 thread_index, sw_if_index0, 1);
967 nat44_set_tcp_session_state_o2i (sm, now, s0,
968 vnet_buffer (b0)->ip.
969 reass.icmp_type_or_tcp_flags,
972 case IP_PROTOCOL_UDP:
973 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.udp,
974 thread_index, sw_if_index0, 1);
976 case IP_PROTOCOL_ICMP:
977 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.icmp,
978 thread_index, sw_if_index0, 1);
981 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.other,
982 thread_index, sw_if_index0, 1);
987 nat44_session_update_counters (s0, now,
988 vlib_buffer_length_in_chain (vm, b0),
990 /* Per-user LRU list maintenance */
991 nat44_session_update_lru (sm, s0, thread_index);
994 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
995 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
997 nat44_ed_out2in_trace_t *t =
998 vlib_add_trace (vm, node, b0, sizeof (*t));
999 t->sw_if_index = sw_if_index0;
1000 t->next_index = next[0];
1001 t->is_slow_path = 0;
1002 t->translation_error = translation_error;
1003 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1004 t->lookup_skipped = lookup_skipped;
1005 t->slow_path_reason = slow_path_reason;
1009 t->session_index = s0 - tsm->sessions;
1010 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1011 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1012 t->translation_via_i2of = (&s0->i2o == f);
1013 t->tcp_state = s0->tcp_state;
1017 t->session_index = ~0;
1021 if (next[0] == NAT_NEXT_DROP)
1023 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1024 thread_index, sw_if_index0, 1);
1031 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1033 return frame->n_vectors;
1037 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
1038 vlib_node_runtime_t * node,
1039 vlib_frame_t * frame)
1041 u32 n_left_from, *from;
1042 snat_main_t *sm = &snat_main;
1043 f64 now = vlib_time_now (vm);
1044 u32 thread_index = vm->thread_index;
1045 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1046 snat_static_mapping_t *m;
1048 from = vlib_frame_vector_args (frame);
1049 n_left_from = frame->n_vectors;
1051 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1052 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1053 vlib_get_buffers (vm, from, b, n_left_from);
1055 while (n_left_from > 0)
1058 u32 sw_if_index0, rx_fib_index0;
1059 ip_protocol_t proto0;
1062 icmp46_header_t *icmp0;
1063 snat_session_t *s0 = 0;
1064 clib_bihash_kv_16_8_t kv0 = {}, value0;
1065 lb_nat_type_t lb_nat0;
1066 twice_nat_type_t twice_nat0;
1068 ip4_address_t sm_addr;
1071 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1074 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1076 vnet_buffer (b0)->snat.flags = 0;
1077 ip0 = vlib_buffer_get_current (b0);
1079 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1081 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1083 if (PREDICT_FALSE (ip0->ttl == 1))
1085 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1086 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1087 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1089 next[0] = NAT_NEXT_ICMP_ERROR;
1093 udp0 = ip4_next_header (ip0);
1094 icmp0 = (icmp46_header_t *) udp0;
1095 proto0 = ip0->protocol;
1097 if (PREDICT_FALSE (nat44_ed_is_unk_proto (proto0)))
1099 s0 = nat44_ed_out2in_slowpath_unknown_proto (
1100 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1101 if (!sm->forwarding_enabled)
1104 next[0] = NAT_NEXT_DROP;
1106 if (NAT_NEXT_DROP != next[0] && s0 &&
1107 NAT_ED_TRNSL_ERR_SUCCESS !=
1108 (translation_error = nat_6t_flow_buf_translate_o2i (
1109 vm, sm, b0, ip0, &s0->o2i, proto0,
1110 0 /* is_output_feature */)))
1112 next[0] = NAT_NEXT_DROP;
1113 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TRNSL_FAILED];
1117 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1118 thread_index, sw_if_index0, 1);
1122 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1124 next[0] = icmp_out2in_ed_slow_path
1125 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1126 next[0], now, thread_index, &s0);
1128 if (NAT_NEXT_DROP != next[0] && s0 &&
1129 NAT_ED_TRNSL_ERR_SUCCESS !=
1130 (translation_error = nat_6t_flow_buf_translate_o2i (
1131 vm, sm, b0, ip0, &s0->o2i, proto0,
1132 0 /* is_output_feature */)))
1134 next[0] = NAT_NEXT_DROP;
1135 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TRNSL_FAILED];
1139 if (NAT_NEXT_DROP != next[0])
1141 vlib_increment_simple_counter (
1142 &sm->counters.slowpath.out2in.icmp, thread_index, sw_if_index0,
1149 &kv0, ip0->src_address.as_u32, vnet_buffer (b0)->ip.reass.l4_src_port,
1150 ip0->dst_address.as_u32, vnet_buffer (b0)->ip.reass.l4_dst_port,
1151 rx_fib_index0, ip0->protocol);
1154 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1156 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1158 pool_elt_at_index (tsm->sessions,
1159 ed_value_get_session_index (&value0));
1164 /* Try to match static mapping by external address and port,
1165 destination address and port in packet */
1167 if (snat_static_mapping_match (
1168 vm, ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
1169 rx_fib_index0, proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1170 &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0, &m))
1173 * Send DHCP packets to the ipv4 stack, or we won't
1174 * be able to use dhcp client on the outside interface
1177 proto0 == IP_PROTOCOL_UDP &&
1178 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1179 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1184 if (!sm->forwarding_enabled)
1187 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1188 next[0] = NAT_NEXT_DROP;
1193 sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1194 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0))
1196 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1200 create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
1207 if (PREDICT_FALSE (identity_nat0))
1210 if ((proto0 == IP_PROTOCOL_TCP) &&
1211 !tcp_flags_is_init (
1212 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
1214 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1215 next[0] = NAT_NEXT_DROP;
1219 /* Create session initiated by host from external network */
1220 s0 = create_session_for_static_mapping_ed (
1221 sm, b0, sm_addr, sm_port, sm_fib_index, ip0->dst_address,
1222 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0, proto0,
1223 node, thread_index, twice_nat0, lb_nat0, now, m);
1226 next[0] = NAT_NEXT_DROP;
1231 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1232 (translation_error = nat_6t_flow_buf_translate_o2i (
1233 vm, sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1235 next[0] = NAT_NEXT_DROP;
1239 if (PREDICT_TRUE (proto0 == IP_PROTOCOL_TCP))
1241 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1242 thread_index, sw_if_index0, 1);
1243 nat44_set_tcp_session_state_o2i (sm, now, s0,
1244 vnet_buffer (b0)->ip.
1245 reass.icmp_type_or_tcp_flags,
1250 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1251 thread_index, sw_if_index0, 1);
1255 nat44_session_update_counters (s0, now,
1256 vlib_buffer_length_in_chain (vm, b0),
1258 /* Per-user LRU list maintenance */
1259 nat44_session_update_lru (sm, s0, thread_index);
1262 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1263 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1265 nat44_ed_out2in_trace_t *t =
1266 vlib_add_trace (vm, node, b0, sizeof (*t));
1267 t->sw_if_index = sw_if_index0;
1268 t->next_index = next[0];
1269 t->is_slow_path = 1;
1270 t->translation_error = translation_error;
1271 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1275 t->session_index = s0 - tsm->sessions;
1276 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1277 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1278 t->tcp_state = s0->tcp_state;
1282 t->session_index = ~0;
1286 if (next[0] == NAT_NEXT_DROP)
1288 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1289 thread_index, sw_if_index0, 1);
1297 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1300 return frame->n_vectors;
1303 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1304 vlib_node_runtime_t * node,
1305 vlib_frame_t * frame)
1307 if (snat_main.num_workers > 1)
1309 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1313 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1317 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1318 .name = "nat44-ed-out2in",
1319 .vector_size = sizeof (u32),
1320 .sibling_of = "nat-default",
1321 .format_trace = format_nat44_ed_out2in_trace,
1322 .type = VLIB_NODE_TYPE_INTERNAL,
1323 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1324 .error_strings = nat_out2in_ed_error_strings,
1325 .runtime_data_bytes = sizeof (snat_runtime_t),
1328 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1329 vlib_node_runtime_t * node,
1330 vlib_frame_t * frame)
1332 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1335 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1336 .name = "nat44-ed-out2in-slowpath",
1337 .vector_size = sizeof (u32),
1338 .sibling_of = "nat-default",
1339 .format_trace = format_nat44_ed_out2in_trace,
1340 .type = VLIB_NODE_TYPE_INTERNAL,
1341 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1342 .error_strings = nat_out2in_ed_error_strings,
1343 .runtime_data_bytes = sizeof (snat_runtime_t),
1347 format_nat_pre_trace (u8 * s, va_list * args)
1349 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1350 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1351 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1352 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1356 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1357 vlib_node_runtime_t * node,
1358 vlib_frame_t * frame)
1360 return nat_pre_node_fn_inline (vm, node, frame,
1361 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1364 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1365 .name = "nat-pre-out2in",
1366 .vector_size = sizeof (u32),
1367 .sibling_of = "nat-default",
1368 .format_trace = format_nat_pre_trace,
1369 .type = VLIB_NODE_TYPE_INTERNAL,
1374 * fd.io coding-style-patch-verification: ON
1377 * eval: (c-set-style "gnu")