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 inside to outside 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/nat_inlines.h>
29 #include <nat/lib/ipfix_logging.h>
31 #include <nat/nat44-ed/nat44_ed.h>
32 #include <nat/nat44-ed/nat44_ed_inlines.h>
34 static char *nat_in2out_ed_error_strings[] = {
35 #define _(sym,string) string,
36 foreach_nat_in2out_ed_error
45 nat_translation_error_e translation_error;
48 clib_bihash_kv_16_8_t search_key;
50 u8 translation_via_i2of;
53 } nat_in2out_ed_trace_t;
56 format_nat_in2out_ed_trace (u8 * s, va_list * args)
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
64 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
65 "NAT44_IN2OUT_ED_FAST_PATH";
67 s = format (s, "%s: sw_if_index %d, next index %d", tag, t->sw_if_index,
69 if (~0 != t->session_index)
71 s = format (s, ", session %d, translation result '%U' via %s",
72 t->session_index, format_nat_ed_translation_error,
74 t->translation_via_i2of ? "i2of" : "o2if");
75 s = format (s, "\n i2of %U", format_nat_6t_flow, &t->i2of);
76 s = format (s, "\n o2if %U", format_nat_6t_flow, &t->o2if);
80 if (t->lookup_skipped)
82 s = format (s, "\n lookup skipped - cached session index used");
86 s = format (s, "\n search key %U", format_ed_session_kvp,
90 if (IP_PROTOCOL_TCP == t->i2of.match.proto)
92 s = format (s, "\n TCP state: %U", format_nat44_ed_tcp_state,
100 nat_ed_alloc_addr_and_port_with_snat_address (
101 snat_main_t *sm, u8 proto, u32 thread_index, snat_address_t *a,
102 u16 port_per_thread, u32 snat_thread_index, snat_session_t *s,
103 ip4_address_t *outside_addr, u16 *outside_port)
105 const u16 port_thread_offset = (port_per_thread * snat_thread_index) + 1024;
107 s->o2i.match.daddr = a->addr;
108 /* first try port suggested by caller */
109 u16 port = clib_net_to_host_u16 (*outside_port);
110 u16 port_offset = port - port_thread_offset;
111 if (port <= port_thread_offset ||
112 port > port_thread_offset + port_per_thread)
114 /* need to pick a different port, suggested port doesn't fit in
115 * this thread's port range */
116 port_offset = snat_random_port (0, port_per_thread - 1);
117 port = port_thread_offset + port_offset;
119 u16 attempts = ED_PORT_ALLOC_ATTEMPTS;
122 if (IP_PROTOCOL_ICMP == proto)
124 s->o2i.match.sport = clib_host_to_net_u16 (port);
126 s->o2i.match.dport = clib_host_to_net_u16 (port);
127 if (0 == nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
129 *outside_addr = a->addr;
130 *outside_port = clib_host_to_net_u16 (port);
133 port_offset = snat_random_port (0, port_per_thread - 1);
134 port = port_thread_offset + port_offset;
137 while (attempts > 0);
142 nat_ed_alloc_addr_and_port (snat_main_t *sm, u32 rx_fib_index,
143 u32 tx_sw_if_index, u32 nat_proto,
144 u32 thread_index, ip4_address_t s_addr,
145 ip4_address_t d_addr, u32 snat_thread_index,
146 snat_session_t *s, ip4_address_t *outside_addr,
149 if (vec_len (sm->addresses) > 0)
151 u32 s_addr_offset = s_addr.as_u32 % vec_len (sm->addresses);
152 snat_address_t *a, *ja = 0, *ra = 0, *ba = 0;
156 if (tx_sw_if_index != ~0)
158 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
160 a = sm->addresses + i;
161 if (a->fib_index == rx_fib_index)
163 if (a->sw_if_index == tx_sw_if_index)
165 if ((a->addr_len != ~0) &&
167 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
170 return nat_ed_alloc_addr_and_port_with_snat_address (
171 sm, nat_proto, thread_index, a,
172 sm->port_per_thread, snat_thread_index, s,
173 outside_addr, outside_port);
179 else if (a->fib_index == ~0)
184 for (i = 0; i < s_addr_offset; ++i)
186 a = sm->addresses + i;
187 if (a->fib_index == rx_fib_index)
189 if (a->sw_if_index == tx_sw_if_index)
191 if ((a->addr_len != ~0) &&
193 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
196 return nat_ed_alloc_addr_and_port_with_snat_address (
197 sm, nat_proto, thread_index, a,
198 sm->port_per_thread, snat_thread_index, s,
199 outside_addr, outside_port);
205 else if (a->fib_index == ~0)
212 return nat_ed_alloc_addr_and_port_with_snat_address (
213 sm, nat_proto, thread_index, ra, sm->port_per_thread,
214 snat_thread_index, s, outside_addr, outside_port);
219 // first try nat pool addresses to sw interface addreses mappings
220 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
222 a = sm->addresses + i;
223 if (a->fib_index == rx_fib_index)
225 if ((a->addr_len != ~0) &&
227 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
229 return nat_ed_alloc_addr_and_port_with_snat_address (
230 sm, nat_proto, thread_index, a, sm->port_per_thread,
231 snat_thread_index, s, outside_addr, outside_port);
235 else if (a->fib_index == ~0)
240 for (i = 0; i < s_addr_offset; ++i)
242 a = sm->addresses + i;
243 if (a->fib_index == rx_fib_index)
245 if ((a->addr_len != ~0) &&
247 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
249 return nat_ed_alloc_addr_and_port_with_snat_address (
250 sm, nat_proto, thread_index, a, sm->port_per_thread,
251 snat_thread_index, s, outside_addr, outside_port);
255 else if (a->fib_index == ~0)
265 return nat_ed_alloc_addr_and_port_with_snat_address (
266 sm, nat_proto, thread_index, a, sm->port_per_thread,
267 snat_thread_index, s, outside_addr, outside_port);
270 /* Totally out of translations to use... */
271 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
275 static_always_inline int
276 nat44_ed_external_sm_lookup (snat_main_t *sm, ip4_address_t match_addr,
277 u16 match_port, ip_protocol_t match_protocol,
278 ip4_address_t *daddr, u16 *dport)
280 snat_static_mapping_t *m =
281 nat44_ed_sm_o2i_lookup (sm, match_addr, match_port, 0, match_protocol);
284 /* Try address only mapping */
285 m = nat44_ed_sm_o2i_lookup (sm, match_addr, 0, 0, 0);
289 *daddr = m->local_addr;
292 /* Address only mapping doesn't change port */
293 *dport = is_sm_addr_only (m->flags) ? match_port : m->local_port;
298 static_always_inline vrf_table_t *
299 get_vrf_table_by_fib (u32 fib_index)
301 snat_main_t *sm = &snat_main;
304 pool_foreach (t, sm->vrf_tables)
306 if (fib_index == t->table_fib_index)
315 static_always_inline u32
316 get_tx_fib_index (u32 rx_fib_index, ip4_address_t addr)
318 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
320 .fp_proto = FIB_PROTOCOL_IP4,
322 .fp_addr = {.ip4.as_u32 = addr.as_u32,}
326 snat_main_t *sm = &snat_main;
327 vrf_table_t *t = get_vrf_table_by_fib (rx_fib_index);
329 u32 tx_fib_index = rx_fib_index;
333 // managed routes to other fibs
335 pool_foreach (r, t->routes)
337 fei = fib_table_lookup (r->fib_index, &pfx);
338 if ((FIB_NODE_INDEX_INVALID != fei) &&
339 (~0 != fib_entry_get_resolving_interface (fei)))
341 tx_fib_index = r->fib_index;
348 // default to configured fib
349 tx_fib_index = sm->outside_fib_index;
351 // default routes to other fibs
353 vec_foreach (f, sm->outside_fibs)
355 fei = fib_table_lookup (f->fib_index, &pfx);
356 if ((FIB_NODE_INDEX_INVALID != fei) &&
357 (~0 != fib_entry_get_resolving_interface (fei)))
359 tx_fib_index = f->fib_index;
368 static_always_inline int
369 is_destination_resolvable (u32 rx_fib_index, ip4_address_t addr)
371 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
373 .fp_proto = FIB_PROTOCOL_IP4,
375 .fp_addr = {.ip4.as_u32 = addr.as_u32,}
379 snat_main_t *sm = &snat_main;
380 vrf_table_t *t = get_vrf_table_by_fib (rx_fib_index);
385 // managed routes to other fibs
387 pool_foreach (r, t->routes)
389 fei = fib_table_lookup (r->fib_index, &pfx);
390 if ((FIB_NODE_INDEX_INVALID != fei) &&
391 (~0 != (ii = fib_entry_get_resolving_interface (fei))))
399 // default routes to other fibs
401 vec_foreach (f, sm->outside_fibs)
403 fei = fib_table_lookup (f->fib_index, &pfx);
404 if ((FIB_NODE_INDEX_INVALID != fei) &&
405 (~0 != (ii = fib_entry_get_resolving_interface (fei))))
408 pool_foreach (i, sm->interfaces)
410 if ((nat44_ed_is_interface_outside (i)) &&
411 (ii == i->sw_if_index))
424 slow_path_ed (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
425 ip4_address_t l_addr, ip4_address_t r_addr, u16 l_port,
426 u16 r_port, u8 proto, u32 rx_fib_index, u32 tx_sw_if_index,
427 snat_session_t **sessionp, vlib_node_runtime_t *node, u32 next,
428 u32 thread_index, f64 now)
430 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
431 ip4_address_t outside_addr;
434 u8 is_identity_nat = 0;
436 snat_session_t *s = NULL;
437 lb_nat_type_t lb = 0;
438 ip4_address_t daddr = r_addr;
442 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
444 if (!nat_lru_free_one (sm, thread_index, now))
446 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
447 nat_ipfix_logging_max_sessions (thread_index,
448 sm->max_translations_per_thread);
449 nat_elog_notice (sm, "maximum sessions exceeded");
450 return NAT_NEXT_DROP;
454 ip4_address_t sm_addr;
458 // First try to match static mapping by local address and port
459 if (!snat_static_mapping_match (vm, l_addr, l_port, rx_fib_index, proto,
460 &sm_addr, &sm_port, &sm_fib_index, 0, 0, 0,
461 &lb, 0, &is_identity_nat, 0))
463 if (PREDICT_FALSE (is_identity_nat))
471 if (PREDICT_TRUE (proto == IP_PROTOCOL_TCP))
473 if (PREDICT_FALSE (!tcp_flags_is_init (
474 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
476 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
477 return NAT_NEXT_DROP;
481 s = nat_ed_session_alloc (sm, thread_index, now, proto);
484 tx_fib_index = get_tx_fib_index (rx_fib_index, r_addr);
488 s->in2out.addr = l_addr;
489 s->in2out.port = l_port;
491 s->in2out.fib_index = rx_fib_index;
492 s->out2in.fib_index = tx_fib_index;
494 // suggest using local port to allocation function
495 outside_port = l_port;
497 if (PREDICT_FALSE (nat44_ed_external_sm_lookup (sm, r_addr, r_port,
498 proto, &daddr, &dport)))
500 s->flags |= SNAT_SESSION_FLAG_HAIRPINNING;
503 // destination addr/port updated with real values in
504 // nat_ed_alloc_addr_and_port
505 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, daddr, 0,
506 s->out2in.fib_index, proto);
507 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
508 if (IP_PROTOCOL_ICMP == proto)
510 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
514 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
516 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
518 if (nat_ed_alloc_addr_and_port (
519 sm, rx_fib_index, tx_sw_if_index, proto, thread_index, l_addr,
520 r_addr, tsm->snat_thread_index, s, &outside_addr, &outside_port))
522 nat_elog_notice (sm, "addresses exhausted");
523 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
524 nat_ed_session_delete (sm, s, thread_index, 1);
525 return NAT_NEXT_DROP;
527 s->out2in.addr = outside_addr;
528 s->out2in.port = outside_port;
533 s->out2in.addr = outside_addr = sm_addr;
534 s->out2in.port = outside_port = sm_port;
535 s->in2out.addr = l_addr;
536 s->in2out.port = l_port;
538 s->in2out.fib_index = rx_fib_index;
539 s->out2in.fib_index = tx_fib_index;
540 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
543 int is_hairpinning = nat44_ed_external_sm_lookup (sm, r_addr, r_port,
544 proto, &daddr, &dport);
545 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
547 if (IP_PROTOCOL_ICMP == proto)
549 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, sm_port, sm_addr,
550 sm_port, s->out2in.fib_index, proto);
551 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
555 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, sm_addr,
556 sm_port, s->out2in.fib_index, proto);
557 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
559 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
560 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
561 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
563 nat_elog_notice (sm, "out2in key add failed");
569 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
570 s->ext_host_addr = r_addr;
571 s->ext_host_port = r_port;
573 nat_6t_i2o_flow_init (sm, thread_index, s, l_addr, l_port, r_addr, r_port,
574 rx_fib_index, proto);
575 nat_6t_flow_saddr_rewrite_set (&s->i2o, outside_addr.as_u32);
576 nat_6t_flow_daddr_rewrite_set (&s->i2o, daddr.as_u32);
578 if (IP_PROTOCOL_ICMP == proto)
580 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, outside_port);
584 nat_6t_flow_sport_rewrite_set (&s->i2o, outside_port);
585 nat_6t_flow_dport_rewrite_set (&s->i2o, dport);
587 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
589 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
591 nat_elog_notice (sm, "in2out key add failed");
596 nat_ipfix_logging_nat44_ses_create (
597 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->proto,
598 s->in2out.port, s->out2in.port, s->in2out.fib_index);
600 nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
601 s->in2out.port, &s->ext_host_nat_addr,
602 s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
603 &s->ext_host_addr, s->ext_host_port, s->proto, 0);
605 per_vrf_sessions_register_session (s, thread_index);
612 nat_ed_session_delete (sm, s, thread_index, 1);
614 *sessionp = s = NULL;
615 return NAT_NEXT_DROP;
618 static_always_inline int
619 nat44_ed_not_translate (vlib_main_t *vm, vlib_node_runtime_t *node,
620 u32 sw_if_index, vlib_buffer_t *b, ip4_header_t *ip,
621 u32 proto, u32 rx_fib_index)
623 snat_main_t *sm = &snat_main;
625 clib_bihash_kv_16_8_t kv, value;
626 ip4_address_t placeholder_addr;
627 u32 placeholder_fib_index;
628 u16 placeholder_port;
630 init_ed_k (&kv, ip->dst_address.as_u32,
631 vnet_buffer (b)->ip.reass.l4_dst_port, ip->src_address.as_u32,
632 vnet_buffer (b)->ip.reass.l4_src_port, sm->outside_fib_index,
635 // do nat if active session or is static mapping
636 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value) ||
637 !snat_static_mapping_match (
638 vm, ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
639 sm->outside_fib_index, proto, &placeholder_addr, &placeholder_port,
640 &placeholder_fib_index, 1, 0, 0, 0, 0, 0, 0))
645 // do not nat if forwarding enabled
646 if (sm->forwarding_enabled)
651 // do not nat packet aimed at the interface address
653 is_interface_addr (sm, node, sw_if_index, ip->dst_address.as_u32)))
658 // do nat packets with resolvable destination
659 // destination can be resolved either by:
660 // a) vrf routing table entry
661 // b) (non output feature) outside interface fib
662 if (is_destination_resolvable (rx_fib_index, ip->dst_address))
670 static_always_inline int
671 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
672 u32 thread_index, f64 now,
673 vlib_main_t * vm, vlib_buffer_t * b)
675 clib_bihash_kv_16_8_t kv, value;
676 snat_session_t *s = 0;
677 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
679 if (!sm->forwarding_enabled)
682 if (ip->protocol == IP_PROTOCOL_ICMP)
684 ip4_address_t lookup_saddr, lookup_daddr;
685 u16 lookup_sport, lookup_dport;
687 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
688 &lookup_sport, &lookup_daddr,
689 &lookup_dport, &lookup_protocol))
691 init_ed_k (&kv, lookup_saddr.as_u32, lookup_sport, lookup_daddr.as_u32,
692 lookup_dport, 0, lookup_protocol);
694 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
696 init_ed_k (&kv, ip->src_address.as_u32,
697 vnet_buffer (b)->ip.reass.l4_src_port, ip->dst_address.as_u32,
698 vnet_buffer (b)->ip.reass.l4_dst_port, 0, ip->protocol);
702 init_ed_k (&kv, ip->src_address.as_u32, 0, ip->dst_address.as_u32, 0, 0,
706 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
708 ASSERT (thread_index == ed_value_get_thread_index (&value));
710 pool_elt_at_index (tsm->sessions,
711 ed_value_get_session_index (&value));
713 if (na44_ed_is_fwd_bypass_session (s))
715 if (ip->protocol == IP_PROTOCOL_TCP)
717 nat44_set_tcp_session_state_i2o (
718 sm, now, s, vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags,
722 nat44_session_update_counters (s, now,
723 vlib_buffer_length_in_chain (vm, b),
725 /* Per-user LRU list maintenance */
726 nat44_session_update_lru (sm, s, thread_index);
736 static_always_inline int
737 nat44_ed_not_translate_output_feature (snat_main_t *sm, vlib_buffer_t *b,
738 ip4_header_t *ip, u16 src_port,
739 u16 dst_port, u32 thread_index,
740 u32 rx_sw_if_index, u32 tx_sw_if_index,
743 clib_bihash_kv_16_8_t kv, value;
744 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
747 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
748 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
751 init_ed_k (&kv, ip->src_address.as_u32, src_port, ip->dst_address.as_u32,
752 dst_port, tx_fib_index, ip->protocol);
753 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
755 ASSERT (thread_index == ed_value_get_thread_index (&value));
757 pool_elt_at_index (tsm->sessions,
758 ed_value_get_session_index (&value));
763 if (is_multi_worker &&
764 PREDICT_TRUE (!pool_is_free_index (
765 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index)))
768 lookup.fib_index = rx_fib_index;
769 lookup.proto = ip->protocol;
770 lookup.daddr.as_u32 = ip->src_address.as_u32;
771 lookup.dport = src_port;
772 lookup.saddr.as_u32 = ip->dst_address.as_u32;
773 lookup.sport = dst_port;
774 s = pool_elt_at_index (
775 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index);
776 if (PREDICT_TRUE (nat_6t_t_eq (&s->i2o.match, &lookup)))
778 goto skip_dst_nat_lookup;
783 init_ed_k (&kv, ip->dst_address.as_u32, dst_port, ip->src_address.as_u32,
784 src_port, rx_fib_index, ip->protocol);
785 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
787 ASSERT (thread_index == ed_value_get_thread_index (&value));
789 pool_elt_at_index (tsm->sessions,
790 ed_value_get_session_index (&value));
793 if (na44_ed_is_fwd_bypass_session (s))
797 pool_foreach (i, sm->output_feature_interfaces)
799 if ((nat44_ed_is_interface_inside (i)) &&
800 (rx_sw_if_index == i->sw_if_index))
810 icmp_in2out_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
811 icmp46_header_t *icmp, u32 sw_if_index,
812 u32 tx_sw_if_index, u32 rx_fib_index,
813 vlib_node_runtime_t *node, u32 next, f64 now,
814 u32 thread_index, snat_session_t **s_p,
817 vlib_main_t *vm = vlib_get_main ();
820 snat_session_t *s = NULL;
821 u8 lookup_protocol = ip->protocol;
822 u16 lookup_sport, lookup_dport;
823 ip4_address_t lookup_saddr, lookup_daddr;
825 err = nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
826 &lookup_sport, &lookup_daddr,
827 &lookup_dport, &lookup_protocol);
830 b->error = node->errors[err];
831 return NAT_NEXT_DROP;
834 if (tx_sw_if_index != ~0)
836 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
837 sm, b, ip, lookup_sport, lookup_dport, thread_index, sw_if_index,
838 tx_sw_if_index, is_multi_worker)))
845 if (PREDICT_FALSE (nat44_ed_not_translate (
846 vm, node, sw_if_index, b, ip, IP_PROTOCOL_ICMP, rx_fib_index)))
852 if (PREDICT_FALSE (icmp_type_is_error_message (
853 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
855 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
856 return NAT_NEXT_DROP;
860 slow_path_ed (vm, sm, b, ip->src_address, ip->dst_address, lookup_sport,
861 lookup_dport, ip->protocol, rx_fib_index, tx_sw_if_index, &s,
862 node, next, thread_index, vlib_time_now (vm));
864 if (NAT_NEXT_DROP == next)
867 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
869 ip_csum_t sum = ip_incremental_checksum_buffer (
870 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
871 ntohs (ip->length) - ip4_header_bytes (ip), 0);
872 checksum = ~ip_csum_fold (sum);
873 if (PREDICT_FALSE (checksum != 0 && checksum != 0xffff))
875 next = NAT_NEXT_DROP;
881 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
884 nat44_session_update_counters (
885 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
886 /* Per-user LRU list maintenance */
887 nat44_session_update_lru (sm, s, thread_index);
893 static snat_session_t *
894 nat44_ed_in2out_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
895 ip4_header_t *ip, u32 rx_fib_index,
896 u32 thread_index, f64 now,
898 vlib_node_runtime_t *node)
900 clib_bihash_kv_16_8_t s_kv, s_value;
901 snat_static_mapping_t *m = NULL;
902 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
903 snat_session_t *s = NULL;
906 ip4_address_t new_src_addr = { 0 };
907 ip4_address_t new_dst_addr = ip->dst_address;
910 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
912 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
913 nat_ipfix_logging_max_sessions (thread_index,
914 sm->max_translations_per_thread);
915 nat_elog_notice (sm, "maximum sessions exceeded");
919 tx_fib_index = get_tx_fib_index (rx_fib_index, ip->dst_address);
921 // Try to find static mapping first
922 m = nat44_ed_sm_i2o_lookup (sm, ip->src_address, 0, rx_fib_index,
926 new_src_addr = m->external_addr;
930 pool_foreach (s, tsm->sessions)
932 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
934 init_ed_k (&s_kv, s->out2in.addr.as_u32, 0,
935 ip->dst_address.as_u32, 0, tx_fib_index,
937 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
939 new_src_addr = s->out2in.addr;
945 if (!new_src_addr.as_u32)
947 for (i = 0; i < vec_len (sm->addresses); i++)
949 init_ed_k (&s_kv, sm->addresses[i].addr.as_u32, 0,
950 ip->dst_address.as_u32, 0, tx_fib_index,
952 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
954 new_src_addr = sm->addresses[i].addr;
960 if (!new_src_addr.as_u32)
962 // could not allocate address for translation ...
966 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
969 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
970 nat_elog_warn (sm, "create NAT session failed");
974 nat_6t_i2o_flow_init (sm, thread_index, s, ip->src_address, 0,
975 ip->dst_address, 0, rx_fib_index, ip->protocol);
976 nat_6t_flow_saddr_rewrite_set (&s->i2o, new_src_addr.as_u32);
977 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
980 int is_hairpinning = nat44_ed_external_sm_lookup (
981 sm, ip->dst_address, 0, ip->protocol, &new_dst_addr, NULL);
982 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
984 nat_6t_flow_daddr_rewrite_set (&s->i2o, new_dst_addr.as_u32);
985 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
987 nat_6t_o2i_flow_init (sm, thread_index, s, new_dst_addr, 0, new_src_addr, 0,
988 tx_fib_index, ip->protocol);
989 nat_6t_flow_saddr_rewrite_set (&s->o2i, ip->dst_address.as_u32);
990 nat_6t_flow_daddr_rewrite_set (&s->o2i, ip->src_address.as_u32);
991 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
993 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
994 s->out2in.addr.as_u32 = new_src_addr.as_u32;
995 s->out2in.fib_index = tx_fib_index;
996 s->in2out.addr.as_u32 = ip->src_address.as_u32;
997 s->in2out.fib_index = rx_fib_index;
998 s->in2out.port = s->out2in.port = ip->protocol;
1000 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1002 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
1004 nat_elog_notice (sm, "in2out flow hash add failed");
1005 nat_ed_session_delete (sm, s, thread_index, 1);
1009 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
1011 nat_elog_notice (sm, "out2in flow hash add failed");
1012 nat_ed_session_delete (sm, s, thread_index, 1);
1016 per_vrf_sessions_register_session (s, thread_index);
1019 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
1021 /* Per-user LRU list maintenance */
1022 nat44_session_update_lru (sm, s, thread_index);
1028 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t *vm,
1029 vlib_node_runtime_t *node,
1030 vlib_frame_t *frame,
1031 int is_output_feature,
1032 int is_multi_worker)
1034 u32 n_left_from, *from;
1035 snat_main_t *sm = &snat_main;
1036 f64 now = vlib_time_now (vm);
1037 u32 thread_index = vm->thread_index;
1038 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1039 u32 def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH
1040 : NAT_NEXT_IN2OUT_ED_SLOW_PATH;
1042 from = vlib_frame_vector_args (frame);
1043 n_left_from = frame->n_vectors;
1045 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1046 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1047 vlib_get_buffers (vm, from, b, n_left_from);
1049 while (n_left_from > 0)
1052 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1053 u32 tx_sw_if_index0;
1054 u32 cntr_sw_if_index0;
1055 ip_protocol_t proto0;
1057 snat_session_t *s0 = 0;
1058 clib_bihash_kv_16_8_t kv0, value0;
1059 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1060 nat_6t_flow_t *f = 0;
1062 int lookup_skipped = 0;
1067 /* Prefetch next iteration. */
1068 if (PREDICT_TRUE (n_left_from >= 2))
1074 vlib_prefetch_buffer_header (p2, LOAD);
1076 clib_prefetch_load (p2->data);
1079 if (is_output_feature)
1081 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1084 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1087 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) + iph_offset0);
1089 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1090 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1092 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1093 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1095 lookup.fib_index = rx_fib_index0;
1097 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1099 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1100 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1101 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1103 next[0] = NAT_NEXT_ICMP_ERROR;
1107 proto0 = ip0->protocol;
1109 if (is_output_feature)
1112 (nat_not_translate_output_feature_fwd
1113 (sm, ip0, thread_index, now, vm, b0)))
1117 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1119 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1120 ICMP4_echo_request &&
1121 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1123 !icmp_type_is_error_message (
1124 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
1126 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
1127 next[0] = NAT_NEXT_DROP;
1130 int err = nat_get_icmp_session_lookup_values (
1131 b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
1132 &lookup.dport, &lookup.proto);
1135 b0->error = node->errors[err];
1136 next[0] = NAT_NEXT_DROP;
1142 lookup.proto = ip0->protocol;
1143 lookup.saddr.as_u32 = ip0->src_address.as_u32;
1144 lookup.daddr.as_u32 = ip0->dst_address.as_u32;
1145 lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
1146 lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
1149 /* there might be a stashed index in vnet_buffer2 from handoff or
1150 * classify node, see if it can be used */
1151 if (is_multi_worker &&
1152 !pool_is_free_index (tsm->sessions,
1153 vnet_buffer2 (b0)->nat.cached_session_index))
1155 s0 = pool_elt_at_index (tsm->sessions,
1156 vnet_buffer2 (b0)->nat.cached_session_index);
1158 nat_6t_t_eq (&s0->i2o.match, &lookup)
1159 // for some hairpinning cases there are two "i2i" flows instead
1160 // of i2o and o2i as both hosts are on inside
1161 || (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1162 nat_6t_t_eq (&s0->o2i.match, &lookup))))
1164 /* yes, this is the droid we're looking for */
1171 init_ed_k (&kv0, lookup.saddr.as_u32, lookup.sport, lookup.daddr.as_u32,
1172 lookup.dport, lookup.fib_index, lookup.proto);
1175 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1177 // flow does not exist go slow path
1182 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1184 pool_elt_at_index (tsm->sessions,
1185 ed_value_get_session_index (&value0));
1189 ASSERT (thread_index == s0->thread_index);
1191 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
1193 // session is closed, go slow path
1194 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1195 nat_ed_session_delete (sm, s0, thread_index, 1);
1196 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1200 // drop if session expired
1201 u64 sess_timeout_time;
1203 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
1204 if (now >= sess_timeout_time)
1206 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1207 nat_ed_session_delete (sm, s0, thread_index, 1);
1208 // session is closed, go slow path
1213 b0->flags |= VNET_BUFFER_F_IS_NATED;
1215 if (nat_6t_t_eq (&s0->i2o.match, &lookup))
1219 else if (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1220 nat_6t_t_eq (&s0->o2i.match, &lookup))
1226 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
1227 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1228 nat_ed_session_delete (sm, s0, thread_index, 1);
1229 next[0] = NAT_NEXT_DROP;
1230 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1234 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1235 (translation_error = nat_6t_flow_buf_translate_i2o (
1236 vm, sm, b0, ip0, f, proto0, is_output_feature)))
1238 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1239 nat_ed_session_delete (sm, s0, thread_index, 1);
1240 next[0] = NAT_NEXT_DROP;
1241 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1247 case IP_PROTOCOL_TCP:
1248 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.tcp,
1249 thread_index, cntr_sw_if_index0, 1);
1250 nat44_set_tcp_session_state_i2o (
1251 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1254 case IP_PROTOCOL_UDP:
1255 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.udp,
1256 thread_index, cntr_sw_if_index0, 1);
1258 case IP_PROTOCOL_ICMP:
1259 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.icmp,
1260 thread_index, cntr_sw_if_index0, 1);
1263 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.other,
1264 thread_index, cntr_sw_if_index0, 1);
1269 nat44_session_update_counters (s0, now,
1270 vlib_buffer_length_in_chain (vm, b0),
1272 /* Per-user LRU list maintenance */
1273 nat44_session_update_lru (sm, s0, thread_index);
1277 ((node->flags & VLIB_NODE_FLAG_TRACE)
1278 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1280 nat_in2out_ed_trace_t *t =
1281 vlib_add_trace (vm, node, b0, sizeof (*t));
1282 t->sw_if_index = rx_sw_if_index0;
1283 t->next_index = next[0];
1284 t->is_slow_path = 0;
1285 t->translation_error = translation_error;
1286 t->lookup_skipped = lookup_skipped;
1287 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1291 t->session_index = s0 - tsm->sessions;
1292 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1293 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1294 t->translation_via_i2of = (&s0->i2o == f);
1295 t->tcp_state = s0->tcp_state;
1299 t->session_index = ~0;
1303 if (next[0] == NAT_NEXT_DROP)
1305 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.drops,
1306 thread_index, cntr_sw_if_index0, 1);
1313 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1315 return frame->n_vectors;
1319 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t *vm,
1320 vlib_node_runtime_t *node,
1321 vlib_frame_t *frame,
1322 int is_output_feature,
1323 int is_multi_worker)
1325 u32 n_left_from, *from;
1326 snat_main_t *sm = &snat_main;
1327 f64 now = vlib_time_now (vm);
1328 u32 thread_index = vm->thread_index;
1329 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1331 from = vlib_frame_vector_args (frame);
1332 n_left_from = frame->n_vectors;
1334 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1335 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1336 vlib_get_buffers (vm, from, b, n_left_from);
1338 while (n_left_from > 0)
1341 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1342 u32 tx_sw_if_index0;
1343 u32 cntr_sw_if_index0;
1344 ip_protocol_t proto0;
1347 icmp46_header_t *icmp0;
1348 snat_session_t *s0 = 0;
1349 clib_bihash_kv_16_8_t kv0, value0;
1350 int translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1354 if (is_output_feature)
1355 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1357 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1359 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1362 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1363 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1365 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1366 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1369 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1371 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1372 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1373 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1375 next[0] = NAT_NEXT_ICMP_ERROR;
1379 udp0 = ip4_next_header (ip0);
1380 icmp0 = (icmp46_header_t *) udp0;
1381 proto0 = ip0->protocol;
1383 if (PREDICT_FALSE (nat44_ed_is_unk_proto (proto0)))
1385 s0 = nat44_ed_in2out_slowpath_unknown_proto (
1386 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1388 next[0] = NAT_NEXT_DROP;
1390 if (NAT_NEXT_DROP != next[0] && s0 &&
1391 NAT_ED_TRNSL_ERR_SUCCESS !=
1392 (translation_error = nat_6t_flow_buf_translate_i2o (
1393 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1395 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1396 nat_ed_session_delete (sm, s0, thread_index, 1);
1397 next[0] = NAT_NEXT_DROP;
1398 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1402 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.other,
1403 thread_index, cntr_sw_if_index0, 1);
1407 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1409 next[0] = icmp_in2out_ed_slow_path (
1410 sm, b0, ip0, icmp0, rx_sw_if_index0, tx_sw_if_index0,
1411 rx_fib_index0, node, next[0], now, thread_index, &s0,
1413 if (NAT_NEXT_DROP != next[0] && s0 &&
1414 NAT_ED_TRNSL_ERR_SUCCESS !=
1415 (translation_error = nat_6t_flow_buf_translate_i2o (
1416 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1418 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1419 nat_ed_session_delete (sm, s0, thread_index, 1);
1420 next[0] = NAT_NEXT_DROP;
1421 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1425 if (NAT_NEXT_DROP != next[0])
1427 vlib_increment_simple_counter (
1428 &sm->counters.slowpath.in2out.icmp, thread_index,
1429 cntr_sw_if_index0, 1);
1435 &kv0, ip0->src_address.as_u32, vnet_buffer (b0)->ip.reass.l4_src_port,
1436 ip0->dst_address.as_u32, vnet_buffer (b0)->ip.reass.l4_dst_port,
1437 rx_fib_index0, ip0->protocol);
1438 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1440 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1442 pool_elt_at_index (tsm->sessions,
1443 ed_value_get_session_index (&value0));
1448 if (is_output_feature)
1450 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
1451 sm, b0, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1452 vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1453 rx_sw_if_index0, tx_sw_if_index0, is_multi_worker)))
1457 * Send DHCP packets to the ipv4 stack, or we won't
1458 * be able to use dhcp client on the outside interface
1461 proto0 == IP_PROTOCOL_UDP &&
1462 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1463 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server)) &&
1464 ip0->dst_address.as_u32 == 0xffffffff))
1470 nat44_ed_not_translate (vm, node, rx_sw_if_index0, b0, ip0,
1471 proto0, rx_fib_index0)))
1476 slow_path_ed (vm, sm, b0, ip0->src_address, ip0->dst_address,
1477 vnet_buffer (b0)->ip.reass.l4_src_port,
1478 vnet_buffer (b0)->ip.reass.l4_dst_port,
1479 ip0->protocol, rx_fib_index0, tx_sw_if_index0, &s0,
1480 node, next[0], thread_index, now);
1482 if (PREDICT_FALSE (next[0] == NAT_NEXT_DROP))
1485 if (PREDICT_FALSE (!s0))
1490 b0->flags |= VNET_BUFFER_F_IS_NATED;
1492 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1493 (translation_error = nat_6t_flow_buf_translate_i2o (
1494 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1496 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1497 nat_ed_session_delete (sm, s0, thread_index, 1);
1498 next[0] = NAT_NEXT_DROP;
1499 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1503 if (PREDICT_TRUE (proto0 == IP_PROTOCOL_TCP))
1505 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.tcp,
1506 thread_index, cntr_sw_if_index0, 1);
1507 nat44_set_tcp_session_state_i2o (
1508 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1513 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.udp,
1514 thread_index, cntr_sw_if_index0, 1);
1518 nat44_session_update_counters (s0, now,
1519 vlib_buffer_length_in_chain
1520 (vm, b0), thread_index);
1521 /* Per-user LRU list maintenance */
1522 nat44_session_update_lru (sm, s0, thread_index);
1525 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1526 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1528 nat_in2out_ed_trace_t *t =
1529 vlib_add_trace (vm, node, b0, sizeof (*t));
1530 t->sw_if_index = rx_sw_if_index0;
1531 t->next_index = next[0];
1532 t->is_slow_path = 1;
1533 t->translation_error = translation_error;
1534 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1538 t->session_index = s0 - tsm->sessions;
1539 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1540 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1541 t->translation_via_i2of = 1;
1542 t->tcp_state = s0->tcp_state;
1547 t->session_index = ~0;
1551 if (next[0] == NAT_NEXT_DROP)
1553 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.drops,
1554 thread_index, cntr_sw_if_index0, 1);
1562 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1565 return frame->n_vectors;
1568 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1569 vlib_node_runtime_t * node,
1570 vlib_frame_t * frame)
1572 if (snat_main.num_workers > 1)
1574 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 1);
1578 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 0);
1582 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1583 .name = "nat44-ed-in2out",
1584 .vector_size = sizeof (u32),
1585 .sibling_of = "nat-default",
1586 .format_trace = format_nat_in2out_ed_trace,
1587 .type = VLIB_NODE_TYPE_INTERNAL,
1588 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1589 .error_strings = nat_in2out_ed_error_strings,
1590 .runtime_data_bytes = sizeof (snat_runtime_t),
1593 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1594 vlib_node_runtime_t * node,
1595 vlib_frame_t * frame)
1597 if (snat_main.num_workers > 1)
1599 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 1);
1603 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 0);
1607 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1608 .name = "nat44-ed-in2out-output",
1609 .vector_size = sizeof (u32),
1610 .sibling_of = "nat-default",
1611 .format_trace = format_nat_in2out_ed_trace,
1612 .type = VLIB_NODE_TYPE_INTERNAL,
1613 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1614 .error_strings = nat_in2out_ed_error_strings,
1615 .runtime_data_bytes = sizeof (snat_runtime_t),
1618 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1619 vlib_node_runtime_t *
1620 node, vlib_frame_t * frame)
1622 if (snat_main.num_workers > 1)
1624 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 1);
1628 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 0);
1632 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1633 .name = "nat44-ed-in2out-slowpath",
1634 .vector_size = sizeof (u32),
1635 .sibling_of = "nat-default",
1636 .format_trace = format_nat_in2out_ed_trace,
1637 .type = VLIB_NODE_TYPE_INTERNAL,
1638 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1639 .error_strings = nat_in2out_ed_error_strings,
1640 .runtime_data_bytes = sizeof (snat_runtime_t),
1643 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1646 vlib_frame_t * frame)
1648 if (snat_main.num_workers > 1)
1650 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 1);
1654 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 0);
1658 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1659 .name = "nat44-ed-in2out-output-slowpath",
1660 .vector_size = sizeof (u32),
1661 .sibling_of = "nat-default",
1662 .format_trace = format_nat_in2out_ed_trace,
1663 .type = VLIB_NODE_TYPE_INTERNAL,
1664 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1665 .error_strings = nat_in2out_ed_error_strings,
1666 .runtime_data_bytes = sizeof (snat_runtime_t),
1670 format_nat_pre_trace (u8 * s, va_list * args)
1672 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1673 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1674 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1675 return format (s, "in2out next_index %d arc_next_index %d", t->next_index,
1679 VLIB_NODE_FN (nat_pre_in2out_node)
1680 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1682 return nat_pre_node_fn_inline (vm, node, frame,
1683 NAT_NEXT_IN2OUT_ED_FAST_PATH);
1686 VLIB_NODE_FN (nat_pre_in2out_output_node)
1687 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1689 return nat_pre_node_fn_inline (vm, node, frame,
1690 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH);
1693 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1694 .name = "nat-pre-in2out",
1695 .vector_size = sizeof (u32),
1696 .sibling_of = "nat-default",
1697 .format_trace = format_nat_pre_trace,
1698 .type = VLIB_NODE_TYPE_INTERNAL,
1702 VLIB_REGISTER_NODE (nat_pre_in2out_output_node) = {
1703 .name = "nat-pre-in2out-output",
1704 .vector_size = sizeof (u32),
1705 .sibling_of = "nat-default",
1706 .format_trace = format_nat_pre_trace,
1707 .type = VLIB_NODE_TYPE_INTERNAL,
1712 * fd.io coding-style-patch-verification: ON
1715 * eval: (c-set-style "gnu")