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 =
106 (port_per_thread * snat_thread_index) + ED_USER_PORT_OFFSET;
108 /* Backup original match in case of failure */
109 const nat_6t_t match = s->o2i.match;
111 s->o2i.match.daddr = a->addr;
112 /* first try port suggested by caller */
113 u16 port = clib_net_to_host_u16 (*outside_port);
114 u16 port_offset = port - port_thread_offset;
115 if (port < port_thread_offset ||
116 port >= port_thread_offset + port_per_thread)
118 /* need to pick a different port, suggested port doesn't fit in
119 * this thread's port range */
120 port_offset = snat_random_port (0, port_per_thread - 1);
121 port = port_thread_offset + port_offset;
123 u16 attempts = ED_PORT_ALLOC_ATTEMPTS;
126 if (IP_PROTOCOL_ICMP == proto)
128 s->o2i.match.sport = clib_host_to_net_u16 (port);
130 s->o2i.match.dport = clib_host_to_net_u16 (port);
131 if (0 == nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
133 *outside_addr = a->addr;
134 *outside_port = clib_host_to_net_u16 (port);
137 port_offset = snat_random_port (0, port_per_thread - 1);
138 port = port_thread_offset + port_offset;
141 while (attempts > 0);
144 s->o2i.match = match;
149 nat_ed_alloc_addr_and_port (snat_main_t *sm, u32 rx_fib_index,
150 u32 tx_sw_if_index, u32 nat_proto,
151 u32 thread_index, ip4_address_t s_addr,
152 ip4_address_t d_addr, u32 snat_thread_index,
153 snat_session_t *s, ip4_address_t *outside_addr,
156 if (vec_len (sm->addresses) > 0)
158 u32 s_addr_offset = s_addr.as_u32 % vec_len (sm->addresses);
159 snat_address_t *a, *ja = 0, *ra = 0, *ba = 0;
163 if (tx_sw_if_index != ~0)
165 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
167 a = sm->addresses + i;
168 if (a->fib_index == rx_fib_index)
170 if (a->sw_if_index == tx_sw_if_index)
172 if ((a->addr_len != ~0) &&
174 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
177 return nat_ed_alloc_addr_and_port_with_snat_address (
178 sm, nat_proto, thread_index, a,
179 sm->port_per_thread, snat_thread_index, s,
180 outside_addr, outside_port);
186 else if (a->fib_index == ~0)
191 for (i = 0; i < s_addr_offset; ++i)
193 a = sm->addresses + i;
194 if (a->fib_index == rx_fib_index)
196 if (a->sw_if_index == tx_sw_if_index)
198 if ((a->addr_len != ~0) &&
200 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
203 return nat_ed_alloc_addr_and_port_with_snat_address (
204 sm, nat_proto, thread_index, a,
205 sm->port_per_thread, snat_thread_index, s,
206 outside_addr, outside_port);
212 else if (a->fib_index == ~0)
219 return nat_ed_alloc_addr_and_port_with_snat_address (
220 sm, nat_proto, thread_index, ra, sm->port_per_thread,
221 snat_thread_index, s, outside_addr, outside_port);
226 // first try nat pool addresses to sw interface addreses mappings
227 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
229 a = sm->addresses + i;
230 if (a->fib_index == rx_fib_index)
232 if ((a->addr_len != ~0) &&
234 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
236 return nat_ed_alloc_addr_and_port_with_snat_address (
237 sm, nat_proto, thread_index, a, sm->port_per_thread,
238 snat_thread_index, s, outside_addr, outside_port);
242 else if (a->fib_index == ~0)
247 for (i = 0; i < s_addr_offset; ++i)
249 a = sm->addresses + i;
250 if (a->fib_index == rx_fib_index)
252 if ((a->addr_len != ~0) &&
254 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
256 return nat_ed_alloc_addr_and_port_with_snat_address (
257 sm, nat_proto, thread_index, a, sm->port_per_thread,
258 snat_thread_index, s, outside_addr, outside_port);
262 else if (a->fib_index == ~0)
272 return nat_ed_alloc_addr_and_port_with_snat_address (
273 sm, nat_proto, thread_index, a, sm->port_per_thread,
274 snat_thread_index, s, outside_addr, outside_port);
277 /* Totally out of translations to use... */
278 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
282 static_always_inline int
283 nat44_ed_external_sm_lookup (snat_main_t *sm, ip4_address_t match_addr,
284 u16 match_port, ip_protocol_t match_protocol,
285 ip4_address_t *daddr, u16 *dport)
287 snat_static_mapping_t *m =
288 nat44_ed_sm_o2i_lookup (sm, match_addr, match_port, 0, match_protocol);
291 /* Try address only mapping */
292 m = nat44_ed_sm_o2i_lookup (sm, match_addr, 0, 0, 0);
296 *daddr = m->local_addr;
299 /* Address only mapping doesn't change port */
300 *dport = is_sm_addr_only (m->flags) ? match_port : m->local_port;
305 static_always_inline vrf_table_t *
306 get_vrf_table_by_fib (u32 fib_index)
308 snat_main_t *sm = &snat_main;
311 pool_foreach (t, sm->vrf_tables)
313 if (fib_index == t->table_fib_index)
322 static_always_inline u32
323 get_tx_fib_index (u32 rx_fib_index, ip4_address_t addr)
325 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
327 .fp_proto = FIB_PROTOCOL_IP4,
329 .fp_addr = {.ip4.as_u32 = addr.as_u32,}
333 snat_main_t *sm = &snat_main;
334 vrf_table_t *t = get_vrf_table_by_fib (rx_fib_index);
336 u32 tx_fib_index = rx_fib_index;
340 // managed routes to other fibs
342 pool_foreach (r, t->routes)
344 fei = fib_table_lookup (r->fib_index, &pfx);
345 if ((FIB_NODE_INDEX_INVALID != fei) &&
346 (~0 != fib_entry_get_resolving_interface (fei)))
348 tx_fib_index = r->fib_index;
355 // default to configured fib
356 tx_fib_index = sm->outside_fib_index;
358 // default routes to other fibs
360 vec_foreach (f, sm->outside_fibs)
362 fei = fib_table_lookup (f->fib_index, &pfx);
363 if ((FIB_NODE_INDEX_INVALID != fei) &&
364 (~0 != fib_entry_get_resolving_interface (fei)))
366 tx_fib_index = f->fib_index;
375 static_always_inline int
376 is_destination_resolvable (u32 rx_fib_index, ip4_address_t addr)
378 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
380 .fp_proto = FIB_PROTOCOL_IP4,
382 .fp_addr = {.ip4.as_u32 = addr.as_u32,}
386 snat_main_t *sm = &snat_main;
387 vrf_table_t *t = get_vrf_table_by_fib (rx_fib_index);
392 // managed routes to other fibs
394 pool_foreach (r, t->routes)
396 fei = fib_table_lookup (r->fib_index, &pfx);
397 if ((FIB_NODE_INDEX_INVALID != fei) &&
398 (~0 != (ii = fib_entry_get_resolving_interface (fei))))
406 // default routes to other fibs
408 vec_foreach (f, sm->outside_fibs)
410 fei = fib_table_lookup (f->fib_index, &pfx);
411 if ((FIB_NODE_INDEX_INVALID != fei) &&
412 (~0 != (ii = fib_entry_get_resolving_interface (fei))))
415 pool_foreach (i, sm->interfaces)
417 if ((nat44_ed_is_interface_outside (i)) &&
418 (ii == i->sw_if_index))
431 slow_path_ed (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
432 ip4_address_t l_addr, ip4_address_t r_addr, u16 l_port,
433 u16 r_port, u8 proto, u32 rx_fib_index, u32 tx_sw_if_index,
434 snat_session_t **sessionp, vlib_node_runtime_t *node, u32 next,
435 u32 thread_index, f64 now)
437 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
438 ip4_address_t outside_addr;
441 u8 is_identity_nat = 0;
443 snat_session_t *s = NULL;
444 lb_nat_type_t lb = 0;
445 ip4_address_t daddr = r_addr;
449 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
451 if (!nat_lru_free_one (sm, thread_index, now))
453 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
454 nat_ipfix_logging_max_sessions (thread_index,
455 sm->max_translations_per_thread);
456 nat_elog_notice (sm, "maximum sessions exceeded");
457 return NAT_NEXT_DROP;
461 ip4_address_t sm_addr;
465 // First try to match static mapping by local address and port
466 if (!snat_static_mapping_match (vm, l_addr, l_port, rx_fib_index, proto,
467 &sm_addr, &sm_port, &sm_fib_index, 0, 0, 0,
468 &lb, 0, &is_identity_nat, 0))
470 if (PREDICT_FALSE (is_identity_nat))
478 if (PREDICT_TRUE (proto == IP_PROTOCOL_TCP))
480 if (PREDICT_FALSE (!tcp_flags_is_init (
481 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
483 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
484 return NAT_NEXT_DROP;
488 s = nat_ed_session_alloc (sm, thread_index, now, proto);
491 tx_fib_index = get_tx_fib_index (rx_fib_index, r_addr);
495 s->in2out.addr = l_addr;
496 s->in2out.port = l_port;
498 s->in2out.fib_index = rx_fib_index;
499 s->out2in.fib_index = tx_fib_index;
501 // suggest using local port to allocation function
502 outside_port = l_port;
504 if (PREDICT_FALSE (nat44_ed_external_sm_lookup (sm, r_addr, r_port,
505 proto, &daddr, &dport)))
507 s->flags |= SNAT_SESSION_FLAG_HAIRPINNING;
510 // destination addr/port updated with real values in
511 // nat_ed_alloc_addr_and_port
512 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, daddr, 0,
513 s->out2in.fib_index, proto);
514 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
515 if (IP_PROTOCOL_ICMP == proto)
517 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
521 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
523 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
525 if (nat_ed_alloc_addr_and_port (
526 sm, rx_fib_index, tx_sw_if_index, proto, thread_index, l_addr,
527 r_addr, tsm->snat_thread_index, s, &outside_addr, &outside_port))
529 nat_elog_notice (sm, "addresses exhausted");
530 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
531 nat_ed_session_delete (sm, s, thread_index, 1);
532 return NAT_NEXT_DROP;
534 s->out2in.addr = outside_addr;
535 s->out2in.port = outside_port;
540 s->out2in.addr = outside_addr = sm_addr;
541 s->out2in.port = outside_port = sm_port;
542 s->in2out.addr = l_addr;
543 s->in2out.port = l_port;
545 s->in2out.fib_index = rx_fib_index;
546 s->out2in.fib_index = tx_fib_index;
547 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
550 int is_hairpinning = nat44_ed_external_sm_lookup (sm, r_addr, r_port,
551 proto, &daddr, &dport);
552 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
554 if (IP_PROTOCOL_ICMP == proto)
556 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, sm_port, sm_addr,
557 sm_port, s->out2in.fib_index, proto);
558 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
562 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, sm_addr,
563 sm_port, s->out2in.fib_index, proto);
564 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
566 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
567 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
568 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
570 nat_elog_notice (sm, "out2in key add failed");
576 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
577 s->ext_host_addr = r_addr;
578 s->ext_host_port = r_port;
580 nat_6t_i2o_flow_init (sm, thread_index, s, l_addr, l_port, r_addr, r_port,
581 rx_fib_index, proto);
582 nat_6t_flow_saddr_rewrite_set (&s->i2o, outside_addr.as_u32);
583 nat_6t_flow_daddr_rewrite_set (&s->i2o, daddr.as_u32);
585 if (IP_PROTOCOL_ICMP == proto)
587 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, outside_port);
591 nat_6t_flow_sport_rewrite_set (&s->i2o, outside_port);
592 nat_6t_flow_dport_rewrite_set (&s->i2o, dport);
594 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
596 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
598 nat_elog_notice (sm, "in2out key add failed");
603 nat_ipfix_logging_nat44_ses_create (
604 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->proto,
605 s->in2out.port, s->out2in.port, s->in2out.fib_index);
607 nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
608 s->in2out.port, &s->ext_host_nat_addr,
609 s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
610 &s->ext_host_addr, s->ext_host_port, s->proto, 0);
612 per_vrf_sessions_register_session (s, thread_index);
619 nat_ed_session_delete (sm, s, thread_index, 1);
621 *sessionp = s = NULL;
622 return NAT_NEXT_DROP;
625 static_always_inline int
626 nat44_ed_not_translate (vlib_main_t *vm, vlib_node_runtime_t *node,
627 u32 sw_if_index, vlib_buffer_t *b, ip4_header_t *ip,
628 u32 proto, u32 rx_fib_index)
630 snat_main_t *sm = &snat_main;
632 clib_bihash_kv_16_8_t kv, value;
633 ip4_address_t placeholder_addr;
634 u32 placeholder_fib_index;
635 u16 placeholder_port;
637 init_ed_k (&kv, ip->dst_address.as_u32,
638 vnet_buffer (b)->ip.reass.l4_dst_port, ip->src_address.as_u32,
639 vnet_buffer (b)->ip.reass.l4_src_port, sm->outside_fib_index,
642 // do nat if active session or is static mapping
643 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value) ||
644 !snat_static_mapping_match (
645 vm, ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
646 sm->outside_fib_index, proto, &placeholder_addr, &placeholder_port,
647 &placeholder_fib_index, 1, 0, 0, 0, 0, 0, 0))
652 // do not nat if forwarding enabled
653 if (sm->forwarding_enabled)
658 // do not nat packet aimed at the interface address
660 is_interface_addr (sm, node, sw_if_index, ip->dst_address.as_u32)))
665 // do nat packets with resolvable destination
666 // destination can be resolved either by:
667 // a) vrf routing table entry
668 // b) (non output feature) outside interface fib
669 if (is_destination_resolvable (rx_fib_index, ip->dst_address))
677 static_always_inline int
678 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
679 u32 thread_index, f64 now,
680 vlib_main_t * vm, vlib_buffer_t * b)
682 clib_bihash_kv_16_8_t kv, value;
683 snat_session_t *s = 0;
684 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
686 if (!sm->forwarding_enabled)
689 if (ip->protocol == IP_PROTOCOL_ICMP)
691 ip4_address_t lookup_saddr, lookup_daddr;
692 u16 lookup_sport, lookup_dport;
694 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
695 &lookup_sport, &lookup_daddr,
696 &lookup_dport, &lookup_protocol))
698 init_ed_k (&kv, lookup_saddr.as_u32, lookup_sport, lookup_daddr.as_u32,
699 lookup_dport, 0, lookup_protocol);
701 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
703 init_ed_k (&kv, ip->src_address.as_u32,
704 vnet_buffer (b)->ip.reass.l4_src_port, ip->dst_address.as_u32,
705 vnet_buffer (b)->ip.reass.l4_dst_port, 0, ip->protocol);
709 init_ed_k (&kv, ip->src_address.as_u32, 0, ip->dst_address.as_u32, 0, 0,
713 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
715 ASSERT (thread_index == ed_value_get_thread_index (&value));
717 pool_elt_at_index (tsm->sessions,
718 ed_value_get_session_index (&value));
720 if (na44_ed_is_fwd_bypass_session (s))
722 if (ip->protocol == IP_PROTOCOL_TCP)
724 nat44_set_tcp_session_state_i2o (
725 sm, now, s, vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags,
729 nat44_session_update_counters (s, now,
730 vlib_buffer_length_in_chain (vm, b),
732 /* Per-user LRU list maintenance */
733 nat44_session_update_lru (sm, s, thread_index);
743 static_always_inline int
744 nat44_ed_not_translate_output_feature (snat_main_t *sm, vlib_buffer_t *b,
745 ip4_header_t *ip, u16 src_port,
746 u16 dst_port, u32 thread_index,
747 u32 rx_sw_if_index, u32 tx_sw_if_index,
750 clib_bihash_kv_16_8_t kv, value;
751 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
754 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
755 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
758 init_ed_k (&kv, ip->src_address.as_u32, src_port, ip->dst_address.as_u32,
759 dst_port, tx_fib_index, ip->protocol);
760 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
762 ASSERT (thread_index == ed_value_get_thread_index (&value));
764 pool_elt_at_index (tsm->sessions,
765 ed_value_get_session_index (&value));
770 if (is_multi_worker &&
771 PREDICT_TRUE (!pool_is_free_index (
772 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index)))
775 lookup.fib_index = rx_fib_index;
776 lookup.proto = ip->protocol;
777 lookup.daddr.as_u32 = ip->src_address.as_u32;
778 lookup.dport = src_port;
779 lookup.saddr.as_u32 = ip->dst_address.as_u32;
780 lookup.sport = dst_port;
781 s = pool_elt_at_index (
782 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index);
783 if (PREDICT_TRUE (nat_6t_t_eq (&s->i2o.match, &lookup)))
785 goto skip_dst_nat_lookup;
790 init_ed_k (&kv, ip->dst_address.as_u32, dst_port, ip->src_address.as_u32,
791 src_port, rx_fib_index, ip->protocol);
792 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
794 ASSERT (thread_index == ed_value_get_thread_index (&value));
796 pool_elt_at_index (tsm->sessions,
797 ed_value_get_session_index (&value));
800 if (na44_ed_is_fwd_bypass_session (s))
804 pool_foreach (i, sm->output_feature_interfaces)
806 if ((nat44_ed_is_interface_inside (i)) &&
807 (rx_sw_if_index == i->sw_if_index))
817 icmp_in2out_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
818 icmp46_header_t *icmp, u32 sw_if_index,
819 u32 tx_sw_if_index, u32 rx_fib_index,
820 vlib_node_runtime_t *node, u32 next, f64 now,
821 u32 thread_index, snat_session_t **s_p,
824 vlib_main_t *vm = vlib_get_main ();
827 snat_session_t *s = NULL;
828 u8 lookup_protocol = ip->protocol;
829 u16 lookup_sport, lookup_dport;
830 ip4_address_t lookup_saddr, lookup_daddr;
832 err = nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
833 &lookup_sport, &lookup_daddr,
834 &lookup_dport, &lookup_protocol);
837 b->error = node->errors[err];
838 return NAT_NEXT_DROP;
841 if (tx_sw_if_index != ~0)
843 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
844 sm, b, ip, lookup_sport, lookup_dport, thread_index, sw_if_index,
845 tx_sw_if_index, is_multi_worker)))
852 if (PREDICT_FALSE (nat44_ed_not_translate (
853 vm, node, sw_if_index, b, ip, IP_PROTOCOL_ICMP, rx_fib_index)))
859 if (PREDICT_FALSE (icmp_type_is_error_message (
860 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
862 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
863 return NAT_NEXT_DROP;
867 slow_path_ed (vm, sm, b, ip->src_address, ip->dst_address, lookup_sport,
868 lookup_dport, ip->protocol, rx_fib_index, tx_sw_if_index, &s,
869 node, next, thread_index, vlib_time_now (vm));
871 if (NAT_NEXT_DROP == next)
874 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
876 ip_csum_t sum = ip_incremental_checksum_buffer (
877 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
878 ntohs (ip->length) - ip4_header_bytes (ip), 0);
879 checksum = ~ip_csum_fold (sum);
880 if (PREDICT_FALSE (checksum != 0 && checksum != 0xffff))
882 next = NAT_NEXT_DROP;
888 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
891 nat44_session_update_counters (
892 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
893 /* Per-user LRU list maintenance */
894 nat44_session_update_lru (sm, s, thread_index);
900 static snat_session_t *
901 nat44_ed_in2out_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
902 ip4_header_t *ip, u32 rx_fib_index,
903 u32 thread_index, f64 now,
905 vlib_node_runtime_t *node)
907 clib_bihash_kv_16_8_t s_kv, s_value;
908 snat_static_mapping_t *m = NULL;
909 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
910 snat_session_t *s = NULL;
913 ip4_address_t new_src_addr = { 0 };
914 ip4_address_t new_dst_addr = ip->dst_address;
917 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
919 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
920 nat_ipfix_logging_max_sessions (thread_index,
921 sm->max_translations_per_thread);
922 nat_elog_notice (sm, "maximum sessions exceeded");
926 tx_fib_index = get_tx_fib_index (rx_fib_index, ip->dst_address);
928 // Try to find static mapping first
929 m = nat44_ed_sm_i2o_lookup (sm, ip->src_address, 0, rx_fib_index,
933 new_src_addr = m->external_addr;
937 pool_foreach (s, tsm->sessions)
939 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
941 init_ed_k (&s_kv, s->out2in.addr.as_u32, 0,
942 ip->dst_address.as_u32, 0, tx_fib_index,
944 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
946 new_src_addr = s->out2in.addr;
952 if (!new_src_addr.as_u32)
954 for (i = 0; i < vec_len (sm->addresses); i++)
956 init_ed_k (&s_kv, sm->addresses[i].addr.as_u32, 0,
957 ip->dst_address.as_u32, 0, tx_fib_index,
959 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
961 new_src_addr = sm->addresses[i].addr;
967 if (!new_src_addr.as_u32)
969 // could not allocate address for translation ...
973 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
976 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
977 nat_elog_warn (sm, "create NAT session failed");
981 nat_6t_i2o_flow_init (sm, thread_index, s, ip->src_address, 0,
982 ip->dst_address, 0, rx_fib_index, ip->protocol);
983 nat_6t_flow_saddr_rewrite_set (&s->i2o, new_src_addr.as_u32);
984 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
987 int is_hairpinning = nat44_ed_external_sm_lookup (
988 sm, ip->dst_address, 0, ip->protocol, &new_dst_addr, NULL);
989 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
991 nat_6t_flow_daddr_rewrite_set (&s->i2o, new_dst_addr.as_u32);
992 nat_6t_flow_txfib_rewrite_set (&s->i2o, tx_fib_index);
994 nat_6t_o2i_flow_init (sm, thread_index, s, new_dst_addr, 0, new_src_addr, 0,
995 tx_fib_index, ip->protocol);
996 nat_6t_flow_saddr_rewrite_set (&s->o2i, ip->dst_address.as_u32);
997 nat_6t_flow_daddr_rewrite_set (&s->o2i, ip->src_address.as_u32);
998 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
1000 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1001 s->out2in.addr.as_u32 = new_src_addr.as_u32;
1002 s->out2in.fib_index = tx_fib_index;
1003 s->in2out.addr.as_u32 = ip->src_address.as_u32;
1004 s->in2out.fib_index = rx_fib_index;
1005 s->in2out.port = s->out2in.port = ip->protocol;
1007 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1009 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
1011 nat_elog_notice (sm, "in2out flow hash add failed");
1012 nat_ed_session_delete (sm, s, thread_index, 1);
1016 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
1018 nat_elog_notice (sm, "out2in flow hash add failed");
1019 nat_ed_session_delete (sm, s, thread_index, 1);
1023 per_vrf_sessions_register_session (s, thread_index);
1026 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
1028 /* Per-user LRU list maintenance */
1029 nat44_session_update_lru (sm, s, thread_index);
1035 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t *vm,
1036 vlib_node_runtime_t *node,
1037 vlib_frame_t *frame,
1038 int is_output_feature,
1039 int is_multi_worker)
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 u32 def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH
1047 : NAT_NEXT_IN2OUT_ED_SLOW_PATH;
1049 from = vlib_frame_vector_args (frame);
1050 n_left_from = frame->n_vectors;
1052 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1053 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1054 vlib_get_buffers (vm, from, b, n_left_from);
1056 while (n_left_from > 0)
1059 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1060 u32 tx_sw_if_index0;
1061 u32 cntr_sw_if_index0;
1062 ip_protocol_t proto0;
1064 snat_session_t *s0 = 0;
1065 clib_bihash_kv_16_8_t kv0 = { 0 }, value0;
1066 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1067 nat_6t_flow_t *f = 0;
1069 int lookup_skipped = 0;
1074 /* Prefetch next iteration. */
1075 if (PREDICT_TRUE (n_left_from >= 2))
1081 vlib_prefetch_buffer_header (p2, LOAD);
1083 clib_prefetch_load (p2->data);
1086 if (is_output_feature)
1088 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1091 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1094 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) + iph_offset0);
1096 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1097 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1099 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1100 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1102 lookup.fib_index = rx_fib_index0;
1104 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1106 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1107 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1108 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1110 next[0] = NAT_NEXT_ICMP_ERROR;
1114 proto0 = ip0->protocol;
1116 if (is_output_feature)
1119 (nat_not_translate_output_feature_fwd
1120 (sm, ip0, thread_index, now, vm, b0)))
1124 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1126 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1127 ICMP4_echo_request &&
1128 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1130 !icmp_type_is_error_message (
1131 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
1133 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
1134 next[0] = NAT_NEXT_DROP;
1137 int err = nat_get_icmp_session_lookup_values (
1138 b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
1139 &lookup.dport, &lookup.proto);
1142 b0->error = node->errors[err];
1143 next[0] = NAT_NEXT_DROP;
1149 lookup.proto = ip0->protocol;
1150 lookup.saddr.as_u32 = ip0->src_address.as_u32;
1151 lookup.daddr.as_u32 = ip0->dst_address.as_u32;
1152 lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
1153 lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
1156 /* there might be a stashed index in vnet_buffer2 from handoff or
1157 * classify node, see if it can be used */
1158 if (is_multi_worker &&
1159 !pool_is_free_index (tsm->sessions,
1160 vnet_buffer2 (b0)->nat.cached_session_index))
1162 s0 = pool_elt_at_index (tsm->sessions,
1163 vnet_buffer2 (b0)->nat.cached_session_index);
1165 nat_6t_t_eq (&s0->i2o.match, &lookup)
1166 // for some hairpinning cases there are two "i2i" flows instead
1167 // of i2o and o2i as both hosts are on inside
1168 || (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1169 nat_6t_t_eq (&s0->o2i.match, &lookup))))
1171 /* yes, this is the droid we're looking for */
1178 init_ed_k (&kv0, lookup.saddr.as_u32, lookup.sport, lookup.daddr.as_u32,
1179 lookup.dport, lookup.fib_index, lookup.proto);
1182 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1184 // flow does not exist go slow path
1189 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1191 pool_elt_at_index (tsm->sessions,
1192 ed_value_get_session_index (&value0));
1196 ASSERT (thread_index == s0->thread_index);
1198 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
1200 // session is closed, go slow path
1201 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1202 nat_ed_session_delete (sm, s0, thread_index, 1);
1208 // drop if session expired
1209 u64 sess_timeout_time;
1211 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
1212 if (now >= sess_timeout_time)
1214 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1215 nat_ed_session_delete (sm, s0, thread_index, 1);
1217 // session is closed, go slow path
1222 b0->flags |= VNET_BUFFER_F_IS_NATED;
1224 if (nat_6t_t_eq (&s0->i2o.match, &lookup))
1228 else if (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1229 nat_6t_t_eq (&s0->o2i.match, &lookup))
1235 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
1236 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1237 nat_ed_session_delete (sm, s0, thread_index, 1);
1239 next[0] = NAT_NEXT_DROP;
1240 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1244 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1245 (translation_error = nat_6t_flow_buf_translate_i2o (
1246 vm, sm, b0, ip0, f, proto0, is_output_feature)))
1248 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1249 nat_ed_session_delete (sm, s0, thread_index, 1);
1251 next[0] = NAT_NEXT_DROP;
1252 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1258 case IP_PROTOCOL_TCP:
1259 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.tcp,
1260 thread_index, cntr_sw_if_index0, 1);
1261 nat44_set_tcp_session_state_i2o (
1262 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1265 case IP_PROTOCOL_UDP:
1266 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.udp,
1267 thread_index, cntr_sw_if_index0, 1);
1269 case IP_PROTOCOL_ICMP:
1270 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.icmp,
1271 thread_index, cntr_sw_if_index0, 1);
1274 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.other,
1275 thread_index, cntr_sw_if_index0, 1);
1280 nat44_session_update_counters (s0, now,
1281 vlib_buffer_length_in_chain (vm, b0),
1283 /* Per-user LRU list maintenance */
1284 nat44_session_update_lru (sm, s0, thread_index);
1288 ((node->flags & VLIB_NODE_FLAG_TRACE)
1289 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1291 nat_in2out_ed_trace_t *t =
1292 vlib_add_trace (vm, node, b0, sizeof (*t));
1293 t->sw_if_index = rx_sw_if_index0;
1294 t->next_index = next[0];
1295 t->is_slow_path = 0;
1296 t->translation_error = translation_error;
1297 t->lookup_skipped = lookup_skipped;
1298 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1302 t->session_index = s0 - tsm->sessions;
1303 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1304 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1305 t->translation_via_i2of = (&s0->i2o == f);
1306 t->tcp_state = s0->tcp_state;
1310 t->session_index = ~0;
1314 if (next[0] == NAT_NEXT_DROP)
1316 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.drops,
1317 thread_index, cntr_sw_if_index0, 1);
1324 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1326 return frame->n_vectors;
1330 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t *vm,
1331 vlib_node_runtime_t *node,
1332 vlib_frame_t *frame,
1333 int is_output_feature,
1334 int is_multi_worker)
1336 u32 n_left_from, *from;
1337 snat_main_t *sm = &snat_main;
1338 f64 now = vlib_time_now (vm);
1339 u32 thread_index = vm->thread_index;
1340 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1342 from = vlib_frame_vector_args (frame);
1343 n_left_from = frame->n_vectors;
1345 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1346 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1347 vlib_get_buffers (vm, from, b, n_left_from);
1349 while (n_left_from > 0)
1352 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1353 u32 tx_sw_if_index0;
1354 u32 cntr_sw_if_index0;
1355 ip_protocol_t proto0;
1358 icmp46_header_t *icmp0;
1359 snat_session_t *s0 = 0;
1360 clib_bihash_kv_16_8_t kv0 = { 0 }, value0;
1361 int translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1365 if (is_output_feature)
1366 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1368 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1370 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1373 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1374 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1376 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1377 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1380 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1382 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1383 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1384 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1386 next[0] = NAT_NEXT_ICMP_ERROR;
1390 udp0 = ip4_next_header (ip0);
1391 icmp0 = (icmp46_header_t *) udp0;
1392 proto0 = ip0->protocol;
1394 if (PREDICT_FALSE (nat44_ed_is_unk_proto (proto0)))
1396 s0 = nat44_ed_in2out_slowpath_unknown_proto (
1397 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1399 next[0] = NAT_NEXT_DROP;
1401 if (NAT_NEXT_DROP != next[0] && s0 &&
1402 NAT_ED_TRNSL_ERR_SUCCESS !=
1403 (translation_error = nat_6t_flow_buf_translate_i2o (
1404 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1406 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1407 nat_ed_session_delete (sm, s0, thread_index, 1);
1409 next[0] = NAT_NEXT_DROP;
1410 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1414 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.other,
1415 thread_index, cntr_sw_if_index0, 1);
1419 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1421 next[0] = icmp_in2out_ed_slow_path (
1422 sm, b0, ip0, icmp0, rx_sw_if_index0, tx_sw_if_index0,
1423 rx_fib_index0, node, next[0], now, thread_index, &s0,
1425 if (NAT_NEXT_DROP != next[0] && s0 &&
1426 NAT_ED_TRNSL_ERR_SUCCESS !=
1427 (translation_error = nat_6t_flow_buf_translate_i2o (
1428 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1430 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1431 nat_ed_session_delete (sm, s0, thread_index, 1);
1433 next[0] = NAT_NEXT_DROP;
1434 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1438 if (NAT_NEXT_DROP != next[0])
1440 vlib_increment_simple_counter (
1441 &sm->counters.slowpath.in2out.icmp, thread_index,
1442 cntr_sw_if_index0, 1);
1448 &kv0, ip0->src_address.as_u32, vnet_buffer (b0)->ip.reass.l4_src_port,
1449 ip0->dst_address.as_u32, vnet_buffer (b0)->ip.reass.l4_dst_port,
1450 rx_fib_index0, ip0->protocol);
1451 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1453 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1455 pool_elt_at_index (tsm->sessions,
1456 ed_value_get_session_index (&value0));
1461 if (is_output_feature)
1463 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
1464 sm, b0, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1465 vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1466 rx_sw_if_index0, tx_sw_if_index0, is_multi_worker)))
1470 * Send DHCP packets to the ipv4 stack, or we won't
1471 * be able to use dhcp client on the outside interface
1474 proto0 == IP_PROTOCOL_UDP &&
1475 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1476 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server)) &&
1477 ip0->dst_address.as_u32 == 0xffffffff))
1483 nat44_ed_not_translate (vm, node, rx_sw_if_index0, b0, ip0,
1484 proto0, rx_fib_index0)))
1489 slow_path_ed (vm, sm, b0, ip0->src_address, ip0->dst_address,
1490 vnet_buffer (b0)->ip.reass.l4_src_port,
1491 vnet_buffer (b0)->ip.reass.l4_dst_port,
1492 ip0->protocol, rx_fib_index0, tx_sw_if_index0, &s0,
1493 node, next[0], thread_index, now);
1495 if (PREDICT_FALSE (next[0] == NAT_NEXT_DROP))
1498 if (PREDICT_FALSE (!s0))
1503 b0->flags |= VNET_BUFFER_F_IS_NATED;
1505 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1506 (translation_error = nat_6t_flow_buf_translate_i2o (
1507 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1509 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1510 nat_ed_session_delete (sm, s0, thread_index, 1);
1512 next[0] = NAT_NEXT_DROP;
1513 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1517 if (PREDICT_TRUE (proto0 == IP_PROTOCOL_TCP))
1519 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.tcp,
1520 thread_index, cntr_sw_if_index0, 1);
1521 nat44_set_tcp_session_state_i2o (
1522 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1527 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.udp,
1528 thread_index, cntr_sw_if_index0, 1);
1532 nat44_session_update_counters (s0, now,
1533 vlib_buffer_length_in_chain
1534 (vm, b0), thread_index);
1535 /* Per-user LRU list maintenance */
1536 nat44_session_update_lru (sm, s0, thread_index);
1539 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1540 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1542 nat_in2out_ed_trace_t *t =
1543 vlib_add_trace (vm, node, b0, sizeof (*t));
1544 t->sw_if_index = rx_sw_if_index0;
1545 t->next_index = next[0];
1546 t->is_slow_path = 1;
1547 t->translation_error = translation_error;
1548 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1552 t->session_index = s0 - tsm->sessions;
1553 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1554 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1555 t->translation_via_i2of = 1;
1556 t->tcp_state = s0->tcp_state;
1561 t->session_index = ~0;
1565 if (next[0] == NAT_NEXT_DROP)
1567 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.drops,
1568 thread_index, cntr_sw_if_index0, 1);
1576 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1579 return frame->n_vectors;
1582 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1583 vlib_node_runtime_t * node,
1584 vlib_frame_t * frame)
1586 if (snat_main.num_workers > 1)
1588 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 1);
1592 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 0);
1596 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1597 .name = "nat44-ed-in2out",
1598 .vector_size = sizeof (u32),
1599 .sibling_of = "nat-default",
1600 .format_trace = format_nat_in2out_ed_trace,
1601 .type = VLIB_NODE_TYPE_INTERNAL,
1602 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1603 .error_strings = nat_in2out_ed_error_strings,
1604 .runtime_data_bytes = sizeof (snat_runtime_t),
1607 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1608 vlib_node_runtime_t * node,
1609 vlib_frame_t * frame)
1611 if (snat_main.num_workers > 1)
1613 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 1);
1617 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 0);
1621 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1622 .name = "nat44-ed-in2out-output",
1623 .vector_size = sizeof (u32),
1624 .sibling_of = "nat-default",
1625 .format_trace = format_nat_in2out_ed_trace,
1626 .type = VLIB_NODE_TYPE_INTERNAL,
1627 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1628 .error_strings = nat_in2out_ed_error_strings,
1629 .runtime_data_bytes = sizeof (snat_runtime_t),
1632 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1633 vlib_node_runtime_t *
1634 node, vlib_frame_t * frame)
1636 if (snat_main.num_workers > 1)
1638 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 1);
1642 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 0);
1646 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1647 .name = "nat44-ed-in2out-slowpath",
1648 .vector_size = sizeof (u32),
1649 .sibling_of = "nat-default",
1650 .format_trace = format_nat_in2out_ed_trace,
1651 .type = VLIB_NODE_TYPE_INTERNAL,
1652 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1653 .error_strings = nat_in2out_ed_error_strings,
1654 .runtime_data_bytes = sizeof (snat_runtime_t),
1657 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1660 vlib_frame_t * frame)
1662 if (snat_main.num_workers > 1)
1664 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 1);
1668 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 0);
1672 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1673 .name = "nat44-ed-in2out-output-slowpath",
1674 .vector_size = sizeof (u32),
1675 .sibling_of = "nat-default",
1676 .format_trace = format_nat_in2out_ed_trace,
1677 .type = VLIB_NODE_TYPE_INTERNAL,
1678 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1679 .error_strings = nat_in2out_ed_error_strings,
1680 .runtime_data_bytes = sizeof (snat_runtime_t),
1684 format_nat_pre_trace (u8 * s, va_list * args)
1686 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1687 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1688 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1689 return format (s, "in2out next_index %d arc_next_index %d", t->next_index,
1693 VLIB_NODE_FN (nat_pre_in2out_node)
1694 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1696 return nat_pre_node_fn_inline (vm, node, frame,
1697 NAT_NEXT_IN2OUT_ED_FAST_PATH);
1700 VLIB_NODE_FN (nat_pre_in2out_output_node)
1701 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1703 return nat_pre_node_fn_inline (vm, node, frame,
1704 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH);
1707 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1708 .name = "nat-pre-in2out",
1709 .vector_size = sizeof (u32),
1710 .sibling_of = "nat-default",
1711 .format_trace = format_nat_pre_trace,
1712 .type = VLIB_NODE_TYPE_INTERNAL,
1716 VLIB_REGISTER_NODE (nat_pre_in2out_output_node) = {
1717 .name = "nat-pre-in2out-output",
1718 .vector_size = sizeof (u32),
1719 .sibling_of = "nat-default",
1720 .format_trace = format_nat_pre_trace,
1721 .type = VLIB_NODE_TYPE_INTERNAL,
1726 * fd.io coding-style-patch-verification: ON
1729 * eval: (c-set-style "gnu")