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 * @brief Check if packet should be translated
102 * Packets aimed at outside interface and external address with active session
103 * should be translated.
106 * @param rt NAT runtime data
107 * @param sw_if_index0 index of the inside interface
108 * @param ip0 IPv4 header
109 * @param rx_fib_index0 RX FIB index
111 * @returns 0 if packet should be translated otherwise 1
114 snat_not_translate_fast (snat_main_t *sm, vlib_node_runtime_t *node,
115 u32 sw_if_index0, ip4_header_t *ip0,
118 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
119 nat_outside_fib_t *outside_fib;
121 .fp_proto = FIB_PROTOCOL_IP4,
124 .ip4.as_u32 = ip0->dst_address.as_u32,
129 /* Don't NAT packet aimed at the intfc address */
131 is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32)))
134 fei = fib_table_lookup (rx_fib_index0, &pfx);
135 if (FIB_NODE_INDEX_INVALID != fei)
137 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
138 if (sw_if_index == ~0)
140 vec_foreach (outside_fib, sm->outside_fibs)
142 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
143 if (FIB_NODE_INDEX_INVALID != fei)
145 sw_if_index = fib_entry_get_resolving_interface (fei);
146 if (sw_if_index != ~0)
151 if (sw_if_index == ~0)
155 pool_foreach (i, sm->interfaces)
157 /* NAT packet aimed at outside interface */
158 if ((nat44_ed_is_interface_outside (i)) &&
159 (sw_if_index == i->sw_if_index))
168 nat_ed_alloc_addr_and_port_with_snat_address (
169 snat_main_t *sm, u8 proto, u32 thread_index, snat_address_t *a,
170 u16 port_per_thread, u32 snat_thread_index, snat_session_t *s,
171 ip4_address_t *outside_addr, u16 *outside_port)
173 const u16 port_thread_offset = (port_per_thread * snat_thread_index) + 1024;
175 s->o2i.match.daddr = a->addr;
176 /* first try port suggested by caller */
177 u16 port = clib_net_to_host_u16 (*outside_port);
178 u16 port_offset = port - port_thread_offset;
179 if (port <= port_thread_offset ||
180 port > port_thread_offset + port_per_thread)
182 /* need to pick a different port, suggested port doesn't fit in
183 * this thread's port range */
184 port_offset = snat_random_port (0, port_per_thread - 1);
185 port = port_thread_offset + port_offset;
187 u16 attempts = ED_PORT_ALLOC_ATTEMPTS;
190 if (IP_PROTOCOL_ICMP == proto)
192 s->o2i.match.sport = clib_host_to_net_u16 (port);
194 s->o2i.match.dport = clib_host_to_net_u16 (port);
195 if (0 == nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
197 *outside_addr = a->addr;
198 *outside_port = clib_host_to_net_u16 (port);
201 port_offset = snat_random_port (0, port_per_thread - 1);
202 port = port_thread_offset + port_offset;
205 while (attempts > 0);
210 nat_ed_alloc_addr_and_port (snat_main_t *sm, u32 rx_fib_index,
211 u32 tx_sw_if_index, u32 nat_proto,
212 u32 thread_index, ip4_address_t s_addr,
213 ip4_address_t d_addr, u32 snat_thread_index,
214 snat_session_t *s, ip4_address_t *outside_addr,
217 if (vec_len (sm->addresses) > 0)
219 u32 s_addr_offset = s_addr.as_u32 % vec_len (sm->addresses);
220 snat_address_t *a, *ja = 0, *ra = 0, *ba = 0;
224 if (tx_sw_if_index != ~0)
226 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
228 a = sm->addresses + i;
229 if (a->fib_index == rx_fib_index)
231 if (a->sw_if_index == tx_sw_if_index)
233 if ((a->addr_len != ~0) &&
235 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
238 return nat_ed_alloc_addr_and_port_with_snat_address (
239 sm, nat_proto, thread_index, a,
240 sm->port_per_thread, snat_thread_index, s,
241 outside_addr, outside_port);
247 else if (a->fib_index == ~0)
252 for (i = 0; i < s_addr_offset; ++i)
254 a = sm->addresses + i;
255 if (a->fib_index == rx_fib_index)
257 if (a->sw_if_index == tx_sw_if_index)
259 if ((a->addr_len != ~0) &&
261 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
264 return nat_ed_alloc_addr_and_port_with_snat_address (
265 sm, nat_proto, thread_index, a,
266 sm->port_per_thread, snat_thread_index, s,
267 outside_addr, outside_port);
273 else if (a->fib_index == ~0)
280 return nat_ed_alloc_addr_and_port_with_snat_address (
281 sm, nat_proto, thread_index, ra, sm->port_per_thread,
282 snat_thread_index, s, outside_addr, outside_port);
287 // first try nat pool addresses to sw interface addreses mappings
288 for (i = s_addr_offset; i < vec_len (sm->addresses); ++i)
290 a = sm->addresses + i;
291 if (a->fib_index == rx_fib_index)
293 if ((a->addr_len != ~0) &&
295 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
297 return nat_ed_alloc_addr_and_port_with_snat_address (
298 sm, nat_proto, thread_index, a, sm->port_per_thread,
299 snat_thread_index, s, outside_addr, outside_port);
303 else if (a->fib_index == ~0)
308 for (i = 0; i < s_addr_offset; ++i)
310 a = sm->addresses + i;
311 if (a->fib_index == rx_fib_index)
313 if ((a->addr_len != ~0) &&
315 (d_addr.as_u32 & ip4_main.fib_masks[a->addr_len])))
317 return nat_ed_alloc_addr_and_port_with_snat_address (
318 sm, nat_proto, thread_index, a, sm->port_per_thread,
319 snat_thread_index, s, outside_addr, outside_port);
323 else if (a->fib_index == ~0)
333 return nat_ed_alloc_addr_and_port_with_snat_address (
334 sm, nat_proto, thread_index, a, sm->port_per_thread,
335 snat_thread_index, s, outside_addr, outside_port);
338 /* Totally out of translations to use... */
339 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
343 static_always_inline u32
344 nat_outside_fib_index_lookup (snat_main_t * sm, ip4_address_t addr)
346 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
347 nat_outside_fib_t *outside_fib;
349 .fp_proto = FIB_PROTOCOL_IP4,
351 .fp_addr = {.ip4.as_u32 = addr.as_u32,}
354 vec_foreach (outside_fib, sm->outside_fibs)
356 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
357 if (FIB_NODE_INDEX_INVALID != fei)
359 if (fib_entry_get_resolving_interface (fei) != ~0)
361 return outside_fib->fib_index;
368 static_always_inline int
369 nat44_ed_external_sm_lookup (snat_main_t *sm, ip4_address_t match_addr,
370 u16 match_port, ip_protocol_t match_protocol,
371 ip4_address_t *daddr, u16 *dport)
373 snat_static_mapping_t *m =
374 nat44_ed_sm_o2i_lookup (sm, match_addr, match_port, 0, match_protocol);
377 /* Try address only mapping */
378 m = nat44_ed_sm_o2i_lookup (sm, match_addr, 0, 0, 0);
382 *daddr = m->local_addr;
385 /* Address only mapping doesn't change port */
386 *dport = is_sm_addr_only (m->flags) ? match_port : m->local_port;
392 slow_path_ed (vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b,
393 ip4_address_t l_addr, ip4_address_t r_addr, u16 l_port,
394 u16 r_port, u8 proto, u32 rx_fib_index, u32 tx_sw_if_index,
395 snat_session_t **sessionp, vlib_node_runtime_t *node, u32 next,
396 u32 thread_index, f64 now)
398 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
399 ip4_address_t outside_addr;
401 u32 outside_fib_index;
402 u8 is_identity_nat = 0;
404 snat_session_t *s = NULL;
405 lb_nat_type_t lb = 0;
406 ip4_address_t daddr = r_addr;
410 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
412 if (!nat_lru_free_one (sm, thread_index, now))
414 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
415 nat_ipfix_logging_max_sessions (thread_index,
416 sm->max_translations_per_thread);
417 nat_elog_notice (sm, "maximum sessions exceeded");
418 return NAT_NEXT_DROP;
422 outside_fib_index = sm->outside_fib_index;
424 switch (vec_len (sm->outside_fibs))
427 outside_fib_index = sm->outside_fib_index;
430 outside_fib_index = sm->outside_fibs[0].fib_index;
433 outside_fib_index = nat_outside_fib_index_lookup (sm, r_addr);
437 ip4_address_t sm_addr;
440 /* First try to match static mapping by local address and port */
442 if (snat_static_mapping_match (vm, sm, l_addr, l_port, rx_fib_index, proto,
443 &sm_addr, &sm_port, &sm_fib_index, 0, 0, 0,
444 &lb, 0, &is_identity_nat, 0))
450 if (PREDICT_FALSE (is_identity_nat))
458 if (PREDICT_TRUE (proto == IP_PROTOCOL_TCP))
460 if (PREDICT_FALSE (!tcp_flags_is_init (
461 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
463 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
464 return NAT_NEXT_DROP;
468 s = nat_ed_session_alloc (sm, thread_index, now, proto);
473 s->in2out.addr = l_addr;
474 s->in2out.port = l_port;
476 s->in2out.fib_index = rx_fib_index;
477 s->out2in.fib_index = outside_fib_index;
479 // suggest using local port to allocation function
480 outside_port = l_port;
483 int is_hairpinning = nat44_ed_external_sm_lookup (sm, r_addr, r_port,
484 proto, &daddr, &dport);
485 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
487 // destination addr/port updated with real values in
488 // nat_ed_alloc_addr_and_port
489 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, daddr, 0,
490 s->out2in.fib_index, proto);
491 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
492 if (IP_PROTOCOL_ICMP == proto)
494 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
498 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
500 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
502 if (nat_ed_alloc_addr_and_port (
503 sm, rx_fib_index, tx_sw_if_index, proto, thread_index, l_addr,
504 r_addr, tsm->snat_thread_index, s, &outside_addr, &outside_port))
506 nat_elog_notice (sm, "addresses exhausted");
507 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
508 nat_ed_session_delete (sm, s, thread_index, 1);
509 return NAT_NEXT_DROP;
511 s->out2in.addr = outside_addr;
512 s->out2in.port = outside_port;
517 s->out2in.addr = outside_addr = sm_addr;
518 s->out2in.port = outside_port = sm_port;
519 s->in2out.addr = l_addr;
520 s->in2out.port = l_port;
522 s->in2out.fib_index = rx_fib_index;
523 s->out2in.fib_index = outside_fib_index;
524 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
527 int is_hairpinning = nat44_ed_external_sm_lookup (sm, r_addr, r_port,
528 proto, &daddr, &dport);
529 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
531 if (IP_PROTOCOL_ICMP == proto)
533 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, sm_port, sm_addr,
534 sm_port, s->out2in.fib_index, proto);
535 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, l_port);
539 nat_6t_o2i_flow_init (sm, thread_index, s, daddr, dport, sm_addr,
540 sm_port, s->out2in.fib_index, proto);
541 nat_6t_flow_dport_rewrite_set (&s->o2i, l_port);
543 nat_6t_flow_daddr_rewrite_set (&s->o2i, l_addr.as_u32);
544 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
545 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 2))
547 nat_elog_notice (sm, "out2in key add failed");
553 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
554 s->ext_host_addr = r_addr;
555 s->ext_host_port = r_port;
557 nat_6t_i2o_flow_init (sm, thread_index, s, l_addr, l_port, r_addr, r_port,
558 rx_fib_index, proto);
559 nat_6t_flow_saddr_rewrite_set (&s->i2o, outside_addr.as_u32);
560 nat_6t_flow_daddr_rewrite_set (&s->i2o, daddr.as_u32);
562 if (IP_PROTOCOL_ICMP == proto)
564 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, outside_port);
568 nat_6t_flow_sport_rewrite_set (&s->i2o, outside_port);
569 nat_6t_flow_dport_rewrite_set (&s->i2o, dport);
571 nat_6t_flow_txfib_rewrite_set (&s->i2o, outside_fib_index);
573 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
575 nat_elog_notice (sm, "in2out key add failed");
580 nat_ipfix_logging_nat44_ses_create (
581 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->proto,
582 s->in2out.port, s->out2in.port, s->in2out.fib_index);
584 nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
585 s->in2out.port, &s->ext_host_nat_addr,
586 s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
587 &s->ext_host_addr, s->ext_host_port, s->proto, 0);
589 per_vrf_sessions_register_session (s, thread_index);
596 nat_ed_session_delete (sm, s, thread_index, 1);
598 *sessionp = s = NULL;
599 return NAT_NEXT_DROP;
602 static_always_inline int
603 nat44_ed_not_translate (vlib_main_t *vm, snat_main_t *sm,
604 vlib_node_runtime_t *node, u32 sw_if_index,
605 vlib_buffer_t *b, ip4_header_t *ip, u32 proto,
608 clib_bihash_kv_16_8_t kv, value;
610 init_ed_k (&kv, ip->dst_address.as_u32,
611 vnet_buffer (b)->ip.reass.l4_dst_port, ip->src_address.as_u32,
612 vnet_buffer (b)->ip.reass.l4_src_port, sm->outside_fib_index,
615 /* NAT packet aimed at external address if has active sessions */
616 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
618 /* or is static mappings */
619 ip4_address_t placeholder_addr;
620 u16 placeholder_port;
621 u32 placeholder_fib_index;
622 if (!snat_static_mapping_match (
623 vm, sm, ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
624 sm->outside_fib_index, proto, &placeholder_addr, &placeholder_port,
625 &placeholder_fib_index, 1, 0, 0, 0, 0, 0, 0))
631 if (sm->forwarding_enabled)
634 return snat_not_translate_fast (sm, node, sw_if_index, ip, rx_fib_index);
637 static_always_inline int
638 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
639 u32 thread_index, f64 now,
640 vlib_main_t * vm, vlib_buffer_t * b)
642 clib_bihash_kv_16_8_t kv, value;
643 snat_session_t *s = 0;
644 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
646 if (!sm->forwarding_enabled)
649 if (ip->protocol == IP_PROTOCOL_ICMP)
651 ip4_address_t lookup_saddr, lookup_daddr;
652 u16 lookup_sport, lookup_dport;
654 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
655 &lookup_sport, &lookup_daddr,
656 &lookup_dport, &lookup_protocol))
658 init_ed_k (&kv, lookup_saddr.as_u32, lookup_sport, lookup_daddr.as_u32,
659 lookup_dport, 0, lookup_protocol);
661 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
663 init_ed_k (&kv, ip->src_address.as_u32,
664 vnet_buffer (b)->ip.reass.l4_src_port, ip->dst_address.as_u32,
665 vnet_buffer (b)->ip.reass.l4_dst_port, 0, ip->protocol);
669 init_ed_k (&kv, ip->src_address.as_u32, 0, ip->dst_address.as_u32, 0, 0,
673 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
675 ASSERT (thread_index == ed_value_get_thread_index (&value));
677 pool_elt_at_index (tsm->sessions,
678 ed_value_get_session_index (&value));
680 if (na44_ed_is_fwd_bypass_session (s))
682 if (ip->protocol == IP_PROTOCOL_TCP)
684 nat44_set_tcp_session_state_i2o (
685 sm, now, s, vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags,
689 nat44_session_update_counters (s, now,
690 vlib_buffer_length_in_chain (vm, b),
692 /* Per-user LRU list maintenance */
693 nat44_session_update_lru (sm, s, thread_index);
703 static_always_inline int
704 nat44_ed_not_translate_output_feature (snat_main_t *sm, vlib_buffer_t *b,
705 ip4_header_t *ip, u16 src_port,
706 u16 dst_port, u32 thread_index,
707 u32 rx_sw_if_index, u32 tx_sw_if_index,
710 clib_bihash_kv_16_8_t kv, value;
711 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
714 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
715 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
718 init_ed_k (&kv, ip->src_address.as_u32, src_port, ip->dst_address.as_u32,
719 dst_port, tx_fib_index, ip->protocol);
720 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
722 ASSERT (thread_index == ed_value_get_thread_index (&value));
724 pool_elt_at_index (tsm->sessions,
725 ed_value_get_session_index (&value));
730 if (is_multi_worker &&
731 PREDICT_TRUE (!pool_is_free_index (
732 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index)))
735 lookup.fib_index = rx_fib_index;
736 lookup.proto = ip->protocol;
737 lookup.daddr.as_u32 = ip->src_address.as_u32;
738 lookup.dport = src_port;
739 lookup.saddr.as_u32 = ip->dst_address.as_u32;
740 lookup.sport = dst_port;
741 s = pool_elt_at_index (
742 tsm->sessions, vnet_buffer2 (b)->nat.cached_dst_nat_session_index);
743 if (PREDICT_TRUE (nat_6t_t_eq (&s->i2o.match, &lookup)))
745 goto skip_dst_nat_lookup;
750 init_ed_k (&kv, ip->dst_address.as_u32, dst_port, ip->src_address.as_u32,
751 src_port, rx_fib_index, ip->protocol);
752 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
754 ASSERT (thread_index == ed_value_get_thread_index (&value));
756 pool_elt_at_index (tsm->sessions,
757 ed_value_get_session_index (&value));
760 if (na44_ed_is_fwd_bypass_session (s))
764 pool_foreach (i, sm->output_feature_interfaces)
766 if ((nat44_ed_is_interface_inside (i)) &&
767 (rx_sw_if_index == i->sw_if_index))
777 icmp_in2out_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
778 icmp46_header_t *icmp, u32 sw_if_index,
779 u32 tx_sw_if_index, u32 rx_fib_index,
780 vlib_node_runtime_t *node, u32 next, f64 now,
781 u32 thread_index, snat_session_t **s_p,
784 vlib_main_t *vm = vlib_get_main ();
787 snat_session_t *s = NULL;
788 u8 lookup_protocol = ip->protocol;
789 u16 lookup_sport, lookup_dport;
790 ip4_address_t lookup_saddr, lookup_daddr;
792 err = nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
793 &lookup_sport, &lookup_daddr,
794 &lookup_dport, &lookup_protocol);
797 b->error = node->errors[err];
798 return NAT_NEXT_DROP;
801 if (tx_sw_if_index != ~0)
803 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
804 sm, b, ip, lookup_sport, lookup_dport, thread_index, sw_if_index,
805 tx_sw_if_index, is_multi_worker)))
812 if (PREDICT_FALSE (nat44_ed_not_translate (
813 vm, sm, node, sw_if_index, b, ip, IP_PROTOCOL_ICMP, rx_fib_index)))
819 if (PREDICT_FALSE (icmp_type_is_error_message (
820 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
822 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
823 return NAT_NEXT_DROP;
827 slow_path_ed (vm, sm, b, ip->src_address, ip->dst_address, lookup_sport,
828 lookup_dport, ip->protocol, rx_fib_index, tx_sw_if_index, &s,
829 node, next, thread_index, vlib_time_now (vm));
831 if (NAT_NEXT_DROP == next)
834 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
836 ip_csum_t sum = ip_incremental_checksum_buffer (
837 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
838 ntohs (ip->length) - ip4_header_bytes (ip), 0);
839 checksum = ~ip_csum_fold (sum);
840 if (PREDICT_FALSE (checksum != 0 && checksum != 0xffff))
842 next = NAT_NEXT_DROP;
848 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
851 nat44_session_update_counters (
852 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
853 /* Per-user LRU list maintenance */
854 nat44_session_update_lru (sm, s, thread_index);
860 static snat_session_t *
861 nat44_ed_in2out_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
862 ip4_header_t *ip, u32 rx_fib_index,
863 u32 thread_index, f64 now,
865 vlib_node_runtime_t *node)
867 clib_bihash_kv_16_8_t s_kv, s_value;
868 snat_static_mapping_t *m = NULL;
869 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
870 snat_session_t *s = NULL;
871 u32 outside_fib_index = sm->outside_fib_index;
873 ip4_address_t new_src_addr = { 0 };
874 ip4_address_t new_dst_addr = ip->dst_address;
877 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
879 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
880 nat_ipfix_logging_max_sessions (thread_index,
881 sm->max_translations_per_thread);
882 nat_elog_notice (sm, "maximum sessions exceeded");
886 switch (vec_len (sm->outside_fibs))
889 outside_fib_index = sm->outside_fib_index;
892 outside_fib_index = sm->outside_fibs[0].fib_index;
895 outside_fib_index = nat_outside_fib_index_lookup (sm, ip->dst_address);
899 /* Try to find static mapping first */
900 m = nat44_ed_sm_i2o_lookup (sm, ip->src_address, 0, rx_fib_index,
904 new_src_addr = m->external_addr;
908 pool_foreach (s, tsm->sessions)
910 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
912 init_ed_k (&s_kv, s->out2in.addr.as_u32, 0,
913 ip->dst_address.as_u32, 0, outside_fib_index,
915 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
917 new_src_addr = s->out2in.addr;
923 if (!new_src_addr.as_u32)
925 for (i = 0; i < vec_len (sm->addresses); i++)
927 init_ed_k (&s_kv, sm->addresses[i].addr.as_u32, 0,
928 ip->dst_address.as_u32, 0, outside_fib_index,
930 if (clib_bihash_search_16_8 (&sm->flow_hash, &s_kv, &s_value))
932 new_src_addr = sm->addresses[i].addr;
938 if (!new_src_addr.as_u32)
940 // could not allocate address for translation ...
944 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
947 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
948 nat_elog_warn (sm, "create NAT session failed");
952 nat_6t_i2o_flow_init (sm, thread_index, s, ip->src_address, 0,
953 ip->dst_address, 0, rx_fib_index, ip->protocol);
954 nat_6t_flow_saddr_rewrite_set (&s->i2o, new_src_addr.as_u32);
955 nat_6t_flow_txfib_rewrite_set (&s->i2o, outside_fib_index);
958 int is_hairpinning = nat44_ed_external_sm_lookup (
959 sm, ip->dst_address, 0, ip->protocol, &new_dst_addr, NULL);
960 s->flags |= is_hairpinning * SNAT_SESSION_FLAG_HAIRPINNING;
962 nat_6t_flow_daddr_rewrite_set (&s->i2o, new_dst_addr.as_u32);
963 nat_6t_flow_txfib_rewrite_set (&s->i2o, outside_fib_index);
965 nat_6t_o2i_flow_init (sm, thread_index, s, new_dst_addr, 0, new_src_addr, 0,
966 outside_fib_index, ip->protocol);
967 nat_6t_flow_saddr_rewrite_set (&s->o2i, ip->dst_address.as_u32);
968 nat_6t_flow_daddr_rewrite_set (&s->o2i, ip->src_address.as_u32);
969 nat_6t_flow_txfib_rewrite_set (&s->o2i, rx_fib_index);
971 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
972 s->out2in.addr.as_u32 = new_src_addr.as_u32;
973 s->out2in.fib_index = outside_fib_index;
974 s->in2out.addr.as_u32 = ip->src_address.as_u32;
975 s->in2out.fib_index = rx_fib_index;
976 s->in2out.port = s->out2in.port = ip->protocol;
978 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
980 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
982 nat_elog_notice (sm, "in2out flow hash add failed");
983 nat_ed_session_delete (sm, s, thread_index, 1);
987 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
989 nat_elog_notice (sm, "out2in flow hash add failed");
990 nat_ed_session_delete (sm, s, thread_index, 1);
994 per_vrf_sessions_register_session (s, thread_index);
997 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
999 /* Per-user LRU list maintenance */
1000 nat44_session_update_lru (sm, s, thread_index);
1006 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t *vm,
1007 vlib_node_runtime_t *node,
1008 vlib_frame_t *frame,
1009 int is_output_feature,
1010 int is_multi_worker)
1012 u32 n_left_from, *from;
1013 snat_main_t *sm = &snat_main;
1014 f64 now = vlib_time_now (vm);
1015 u32 thread_index = vm->thread_index;
1016 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1017 u32 def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH
1018 : NAT_NEXT_IN2OUT_ED_SLOW_PATH;
1020 from = vlib_frame_vector_args (frame);
1021 n_left_from = frame->n_vectors;
1023 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1024 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1025 vlib_get_buffers (vm, from, b, n_left_from);
1027 while (n_left_from > 0)
1030 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1031 u32 tx_sw_if_index0;
1032 u32 cntr_sw_if_index0;
1033 ip_protocol_t proto0;
1035 snat_session_t *s0 = 0;
1036 clib_bihash_kv_16_8_t kv0, value0;
1037 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1038 nat_6t_flow_t *f = 0;
1040 int lookup_skipped = 0;
1045 /* Prefetch next iteration. */
1046 if (PREDICT_TRUE (n_left_from >= 2))
1052 vlib_prefetch_buffer_header (p2, LOAD);
1054 clib_prefetch_load (p2->data);
1057 if (is_output_feature)
1059 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1062 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1065 (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) + iph_offset0);
1067 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1068 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1070 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1071 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1073 lookup.fib_index = rx_fib_index0;
1075 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1077 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1078 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1079 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1081 next[0] = NAT_NEXT_ICMP_ERROR;
1085 proto0 = ip0->protocol;
1087 if (is_output_feature)
1090 (nat_not_translate_output_feature_fwd
1091 (sm, ip0, thread_index, now, vm, b0)))
1095 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1097 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1098 ICMP4_echo_request &&
1099 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
1101 !icmp_type_is_error_message (
1102 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
1104 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
1105 next[0] = NAT_NEXT_DROP;
1108 int err = nat_get_icmp_session_lookup_values (
1109 b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
1110 &lookup.dport, &lookup.proto);
1113 b0->error = node->errors[err];
1114 next[0] = NAT_NEXT_DROP;
1120 lookup.proto = ip0->protocol;
1121 lookup.saddr.as_u32 = ip0->src_address.as_u32;
1122 lookup.daddr.as_u32 = ip0->dst_address.as_u32;
1123 lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
1124 lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
1127 /* there might be a stashed index in vnet_buffer2 from handoff or
1128 * classify node, see if it can be used */
1129 if (is_multi_worker &&
1130 !pool_is_free_index (tsm->sessions,
1131 vnet_buffer2 (b0)->nat.cached_session_index))
1133 s0 = pool_elt_at_index (tsm->sessions,
1134 vnet_buffer2 (b0)->nat.cached_session_index);
1136 nat_6t_t_eq (&s0->i2o.match, &lookup)
1137 // for some hairpinning cases there are two "i2i" flows instead
1138 // of i2o and o2i as both hosts are on inside
1139 || (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1140 nat_6t_t_eq (&s0->o2i.match, &lookup))))
1142 /* yes, this is the droid we're looking for */
1149 init_ed_k (&kv0, lookup.saddr.as_u32, lookup.sport, lookup.daddr.as_u32,
1150 lookup.dport, lookup.fib_index, lookup.proto);
1153 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1155 // flow does not exist go slow path
1160 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1162 pool_elt_at_index (tsm->sessions,
1163 ed_value_get_session_index (&value0));
1167 ASSERT (thread_index == s0->thread_index);
1169 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
1171 // session is closed, go slow path
1172 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1173 nat_ed_session_delete (sm, s0, thread_index, 1);
1174 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1178 // drop if session expired
1179 u64 sess_timeout_time;
1181 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
1182 if (now >= sess_timeout_time)
1184 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1185 nat_ed_session_delete (sm, s0, thread_index, 1);
1186 // session is closed, go slow path
1191 b0->flags |= VNET_BUFFER_F_IS_NATED;
1193 if (nat_6t_t_eq (&s0->i2o.match, &lookup))
1197 else if (s0->flags & SNAT_SESSION_FLAG_HAIRPINNING &&
1198 nat_6t_t_eq (&s0->o2i.match, &lookup))
1204 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
1205 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1206 nat_ed_session_delete (sm, s0, thread_index, 1);
1207 next[0] = NAT_NEXT_DROP;
1208 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1212 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1213 (translation_error = nat_6t_flow_buf_translate_i2o (
1214 vm, sm, b0, ip0, f, proto0, is_output_feature)))
1216 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1217 nat_ed_session_delete (sm, s0, thread_index, 1);
1218 next[0] = NAT_NEXT_DROP;
1219 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1225 case IP_PROTOCOL_TCP:
1226 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.tcp,
1227 thread_index, cntr_sw_if_index0, 1);
1228 nat44_set_tcp_session_state_i2o (
1229 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1232 case IP_PROTOCOL_UDP:
1233 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.udp,
1234 thread_index, cntr_sw_if_index0, 1);
1236 case IP_PROTOCOL_ICMP:
1237 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.icmp,
1238 thread_index, cntr_sw_if_index0, 1);
1241 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.other,
1242 thread_index, cntr_sw_if_index0, 1);
1247 nat44_session_update_counters (s0, now,
1248 vlib_buffer_length_in_chain (vm, b0),
1250 /* Per-user LRU list maintenance */
1251 nat44_session_update_lru (sm, s0, thread_index);
1255 ((node->flags & VLIB_NODE_FLAG_TRACE)
1256 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1258 nat_in2out_ed_trace_t *t =
1259 vlib_add_trace (vm, node, b0, sizeof (*t));
1260 t->sw_if_index = rx_sw_if_index0;
1261 t->next_index = next[0];
1262 t->is_slow_path = 0;
1263 t->translation_error = translation_error;
1264 t->lookup_skipped = lookup_skipped;
1265 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1269 t->session_index = s0 - tsm->sessions;
1270 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1271 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1272 t->translation_via_i2of = (&s0->i2o == f);
1273 t->tcp_state = s0->tcp_state;
1277 t->session_index = ~0;
1281 if (next[0] == NAT_NEXT_DROP)
1283 vlib_increment_simple_counter (&sm->counters.fastpath.in2out.drops,
1284 thread_index, cntr_sw_if_index0, 1);
1291 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1293 return frame->n_vectors;
1297 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t *vm,
1298 vlib_node_runtime_t *node,
1299 vlib_frame_t *frame,
1300 int is_output_feature,
1301 int is_multi_worker)
1303 u32 n_left_from, *from;
1304 snat_main_t *sm = &snat_main;
1305 f64 now = vlib_time_now (vm);
1306 u32 thread_index = vm->thread_index;
1307 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1309 from = vlib_frame_vector_args (frame);
1310 n_left_from = frame->n_vectors;
1312 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1313 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1314 vlib_get_buffers (vm, from, b, n_left_from);
1316 while (n_left_from > 0)
1319 u32 rx_sw_if_index0, rx_fib_index0, iph_offset0 = 0;
1320 u32 tx_sw_if_index0;
1321 u32 cntr_sw_if_index0;
1322 ip_protocol_t proto0;
1325 icmp46_header_t *icmp0;
1326 snat_session_t *s0 = 0;
1327 clib_bihash_kv_16_8_t kv0, value0;
1328 int translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1332 if (is_output_feature)
1333 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1335 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1337 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1340 rx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1341 tx_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
1343 is_output_feature ? tx_sw_if_index0 : rx_sw_if_index0;
1344 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1347 if (PREDICT_FALSE (!is_output_feature && ip0->ttl == 1))
1349 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1350 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1351 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1353 next[0] = NAT_NEXT_ICMP_ERROR;
1357 udp0 = ip4_next_header (ip0);
1358 icmp0 = (icmp46_header_t *) udp0;
1359 proto0 = ip0->protocol;
1361 if (PREDICT_FALSE (nat44_ed_is_unk_proto (proto0)))
1363 s0 = nat44_ed_in2out_slowpath_unknown_proto (
1364 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1366 next[0] = NAT_NEXT_DROP;
1368 if (NAT_NEXT_DROP != next[0] && s0 &&
1369 NAT_ED_TRNSL_ERR_SUCCESS !=
1370 (translation_error = nat_6t_flow_buf_translate_i2o (
1371 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1373 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1374 nat_ed_session_delete (sm, s0, thread_index, 1);
1375 next[0] = NAT_NEXT_DROP;
1376 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1380 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.other,
1381 thread_index, cntr_sw_if_index0, 1);
1385 if (PREDICT_FALSE (proto0 == IP_PROTOCOL_ICMP))
1387 next[0] = icmp_in2out_ed_slow_path (
1388 sm, b0, ip0, icmp0, rx_sw_if_index0, tx_sw_if_index0,
1389 rx_fib_index0, node, next[0], now, thread_index, &s0,
1391 if (NAT_NEXT_DROP != next[0] && s0 &&
1392 NAT_ED_TRNSL_ERR_SUCCESS !=
1393 (translation_error = nat_6t_flow_buf_translate_i2o (
1394 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1396 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1397 nat_ed_session_delete (sm, s0, thread_index, 1);
1398 next[0] = NAT_NEXT_DROP;
1399 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1403 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.icmp,
1404 thread_index, cntr_sw_if_index0, 1);
1409 &kv0, ip0->src_address.as_u32, vnet_buffer (b0)->ip.reass.l4_src_port,
1410 ip0->dst_address.as_u32, vnet_buffer (b0)->ip.reass.l4_dst_port,
1411 rx_fib_index0, ip0->protocol);
1412 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1414 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1416 pool_elt_at_index (tsm->sessions,
1417 ed_value_get_session_index (&value0));
1422 if (is_output_feature)
1424 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (
1425 sm, b0, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1426 vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1427 rx_sw_if_index0, tx_sw_if_index0, is_multi_worker)))
1431 * Send DHCP packets to the ipv4 stack, or we won't
1432 * be able to use dhcp client on the outside interface
1435 proto0 == IP_PROTOCOL_UDP &&
1436 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1437 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server)) &&
1438 ip0->dst_address.as_u32 == 0xffffffff))
1444 nat44_ed_not_translate (vm, sm, node, rx_sw_if_index0, b0,
1445 ip0, proto0, rx_fib_index0)))
1450 slow_path_ed (vm, sm, b0, ip0->src_address, ip0->dst_address,
1451 vnet_buffer (b0)->ip.reass.l4_src_port,
1452 vnet_buffer (b0)->ip.reass.l4_dst_port,
1453 ip0->protocol, rx_fib_index0, tx_sw_if_index0, &s0,
1454 node, next[0], thread_index, now);
1456 if (PREDICT_FALSE (next[0] == NAT_NEXT_DROP))
1459 if (PREDICT_FALSE (!s0))
1464 b0->flags |= VNET_BUFFER_F_IS_NATED;
1466 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1467 (translation_error = nat_6t_flow_buf_translate_i2o (
1468 vm, sm, b0, ip0, &s0->i2o, proto0, is_output_feature)))
1470 nat44_ed_free_session_data (sm, s0, thread_index, 0);
1471 nat_ed_session_delete (sm, s0, thread_index, 1);
1472 next[0] = NAT_NEXT_DROP;
1473 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TRNSL_FAILED];
1477 if (PREDICT_TRUE (proto0 == IP_PROTOCOL_TCP))
1479 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.tcp,
1480 thread_index, cntr_sw_if_index0, 1);
1481 nat44_set_tcp_session_state_i2o (
1482 sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1487 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.udp,
1488 thread_index, cntr_sw_if_index0, 1);
1492 nat44_session_update_counters (s0, now,
1493 vlib_buffer_length_in_chain
1494 (vm, b0), thread_index);
1495 /* Per-user LRU list maintenance */
1496 nat44_session_update_lru (sm, s0, thread_index);
1499 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1500 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1502 nat_in2out_ed_trace_t *t =
1503 vlib_add_trace (vm, node, b0, sizeof (*t));
1504 t->sw_if_index = rx_sw_if_index0;
1505 t->next_index = next[0];
1506 t->is_slow_path = 1;
1507 t->translation_error = translation_error;
1508 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1512 t->session_index = s0 - tsm->sessions;
1513 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1514 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1515 t->translation_via_i2of = 1;
1516 t->tcp_state = s0->tcp_state;
1521 t->session_index = ~0;
1525 if (next[0] == NAT_NEXT_DROP)
1527 vlib_increment_simple_counter (&sm->counters.slowpath.in2out.drops,
1528 thread_index, cntr_sw_if_index0, 1);
1536 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1539 return frame->n_vectors;
1542 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1543 vlib_node_runtime_t * node,
1544 vlib_frame_t * frame)
1546 if (snat_main.num_workers > 1)
1548 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 1);
1552 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0, 0);
1556 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1557 .name = "nat44-ed-in2out",
1558 .vector_size = sizeof (u32),
1559 .sibling_of = "nat-default",
1560 .format_trace = format_nat_in2out_ed_trace,
1561 .type = VLIB_NODE_TYPE_INTERNAL,
1562 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1563 .error_strings = nat_in2out_ed_error_strings,
1564 .runtime_data_bytes = sizeof (snat_runtime_t),
1567 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1568 vlib_node_runtime_t * node,
1569 vlib_frame_t * frame)
1571 if (snat_main.num_workers > 1)
1573 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 1);
1577 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1, 0);
1581 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1582 .name = "nat44-ed-in2out-output",
1583 .vector_size = sizeof (u32),
1584 .sibling_of = "nat-default",
1585 .format_trace = format_nat_in2out_ed_trace,
1586 .type = VLIB_NODE_TYPE_INTERNAL,
1587 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1588 .error_strings = nat_in2out_ed_error_strings,
1589 .runtime_data_bytes = sizeof (snat_runtime_t),
1592 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1593 vlib_node_runtime_t *
1594 node, vlib_frame_t * frame)
1596 if (snat_main.num_workers > 1)
1598 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 1);
1602 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0, 0);
1606 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1607 .name = "nat44-ed-in2out-slowpath",
1608 .vector_size = sizeof (u32),
1609 .sibling_of = "nat-default",
1610 .format_trace = format_nat_in2out_ed_trace,
1611 .type = VLIB_NODE_TYPE_INTERNAL,
1612 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1613 .error_strings = nat_in2out_ed_error_strings,
1614 .runtime_data_bytes = sizeof (snat_runtime_t),
1617 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1620 vlib_frame_t * frame)
1622 if (snat_main.num_workers > 1)
1624 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 1);
1628 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1, 0);
1632 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1633 .name = "nat44-ed-in2out-output-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),
1644 format_nat_pre_trace (u8 * s, va_list * args)
1646 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1647 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1648 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1649 return format (s, "in2out next_index %d arc_next_index %d", t->next_index,
1653 VLIB_NODE_FN (nat_pre_in2out_node)
1654 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1656 return nat_pre_node_fn_inline (vm, node, frame,
1657 NAT_NEXT_IN2OUT_ED_FAST_PATH);
1660 VLIB_NODE_FN (nat_pre_in2out_output_node)
1661 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1663 return nat_pre_node_fn_inline (vm, node, frame,
1664 NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH);
1667 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1668 .name = "nat-pre-in2out",
1669 .vector_size = sizeof (u32),
1670 .sibling_of = "nat-default",
1671 .format_trace = format_nat_pre_trace,
1672 .type = VLIB_NODE_TYPE_INTERNAL,
1676 VLIB_REGISTER_NODE (nat_pre_in2out_output_node) = {
1677 .name = "nat-pre-in2out-output",
1678 .vector_size = sizeof (u32),
1679 .sibling_of = "nat-default",
1680 .format_trace = format_nat_pre_trace,
1681 .type = VLIB_NODE_TYPE_INTERNAL,
1686 * fd.io coding-style-patch-verification: ON
1689 * eval: (c-set-style "gnu")