2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/udp/udp_local.h>
26 #include <vppinfra/error.h>
28 #include <nat/lib/nat_syslog.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_out2in_ed_error_strings[] = {
35 #define _(sym,string) string,
36 foreach_nat_out2in_ed_error
45 nat_translation_error_e translation_error;
48 clib_bihash_kv_16_8_t search_key;
50 u8 translation_via_i2of;
52 } nat44_ed_out2in_trace_t;
55 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
57 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
63 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
64 "NAT44_OUT2IN_ED_FAST_PATH";
66 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
67 t->sw_if_index, t->next_index, t->session_index);
68 if (~0 != t->session_index)
70 s = format (s, ", translation result '%U' via %s",
71 format_nat_ed_translation_error, t->translation_error,
72 t->translation_via_i2of ? "i2of" : "o2if");
73 s = format (s, "\n i2of %U", format_nat_6t_flow, &t->i2of);
74 s = format (s, "\n o2if %U", format_nat_6t_flow, &t->o2if);
78 if (t->lookup_skipped)
80 s = format (s, "\n lookup skipped - cached session index used");
84 s = format (s, "\n search key %U", format_ed_session_kvp,
93 next_src_nat (snat_main_t *sm, ip4_header_t *ip, u16 src_port, u16 dst_port,
94 u32 thread_index, u32 rx_fib_index)
96 clib_bihash_kv_16_8_t kv, value;
98 init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
99 rx_fib_index, ip->protocol);
100 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
106 static void create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b,
107 snat_session_t *s, ip4_header_t *ip,
108 u32 rx_fib_index, u32 thread_index);
110 static snat_session_t *create_session_for_static_mapping_ed (
111 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
112 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
113 nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
114 u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
115 snat_static_mapping_t *mapping);
118 icmp_out2in_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
119 icmp46_header_t *icmp, u32 sw_if_index,
120 u32 rx_fib_index, vlib_node_runtime_t *node,
121 u32 next, f64 now, u32 thread_index,
122 snat_session_t **s_p)
124 vlib_main_t *vm = vlib_get_main ();
129 snat_session_t *s = 0;
130 u8 is_addr_only, identity_nat;
131 ip4_address_t sm_addr;
134 snat_static_mapping_t *m;
136 ip4_address_t lookup_saddr, lookup_daddr;
137 u16 lookup_sport, lookup_dport;
139 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
140 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
142 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr, &lookup_sport,
143 &lookup_daddr, &lookup_dport,
146 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
147 next = NAT_NEXT_DROP;
151 if (snat_static_mapping_match (
152 sm, ip->dst_address, lookup_sport, rx_fib_index,
153 ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
154 &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat, &m))
156 // static mapping not matched
157 if (!sm->forwarding_enabled)
159 /* Don't NAT packet aimed at the intfc address */
160 if (!is_interface_addr (sm, node, sw_if_index,
161 ip->dst_address.as_u32))
163 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
164 next = NAT_NEXT_DROP;
169 if (next_src_nat (sm, ip, lookup_sport, lookup_dport, thread_index,
172 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
176 create_bypass_for_fwd (sm, b, s, ip, rx_fib_index, thread_index);
182 if (PREDICT_FALSE (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
184 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
185 ICMP4_echo_request ||
188 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
189 next = NAT_NEXT_DROP;
193 if (PREDICT_FALSE (identity_nat))
198 /* Create session initiated by host from external network */
199 s = create_session_for_static_mapping_ed (
200 sm, b, sm_addr, sm_port, sm_fib_index, ip->dst_address, lookup_sport,
201 rx_fib_index, ip_proto_to_nat_proto (lookup_protocol), node, rx_fib_index,
202 thread_index, 0, 0, vlib_time_now (vm), m);
204 next = NAT_NEXT_DROP;
206 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
208 sum = ip_incremental_checksum_buffer (
209 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
210 ntohs (ip->length) - ip4_header_bytes (ip), 0);
211 checksum = ~ip_csum_fold (sum);
212 if (checksum != 0 && checksum != 0xffff)
214 next = NAT_NEXT_DROP;
219 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
222 nat44_session_update_counters (
223 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
224 /* Per-user LRU list maintenance */
225 nat44_session_update_lru (sm, s, thread_index);
228 if (NAT_NEXT_DROP == next && s)
230 nat_ed_session_delete (sm, s, thread_index, 1);
237 // allocate exact address based on preference
238 static_always_inline int
239 nat_alloc_addr_and_port_exact (snat_address_t * a,
241 nat_protocol_t proto,
242 ip4_address_t * addr,
244 u16 port_per_thread, u32 snat_thread_index)
246 snat_main_t *sm = &snat_main;
251 #define _(N, j, n, s) \
252 case NAT_PROTOCOL_##N: \
253 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
257 portnum = (port_per_thread * \
258 snat_thread_index) + \
259 snat_random_port(0, port_per_thread - 1) + 1024; \
260 if (a->busy_##n##_port_refcounts[portnum]) \
262 --a->busy_##n##_port_refcounts[portnum]; \
263 a->busy_##n##_ports_per_thread[thread_index]++; \
264 a->busy_##n##_ports++; \
266 *port = clib_host_to_net_u16(portnum); \
273 default : nat_elog_info (sm, "unknown protocol");
277 /* Totally out of translations to use... */
278 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
282 static_always_inline int
283 nat44_ed_alloc_outside_addr_and_port (snat_address_t *addresses, u32 fib_index,
284 u32 thread_index, nat_protocol_t proto,
285 ip4_address_t *addr, u16 *port,
287 u32 snat_thread_index)
289 snat_main_t *sm = &snat_main;
290 snat_address_t *a, *ga = 0;
294 for (i = 0; i < vec_len (addresses); i++)
299 #define _(N, j, n, s) \
300 case NAT_PROTOCOL_##N: \
301 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
303 if (a->fib_index == fib_index) \
307 portnum = (port_per_thread * snat_thread_index) + \
308 snat_random_port (0, port_per_thread - 1) + 1024; \
309 if (a->busy_##n##_port_refcounts[portnum]) \
311 --a->busy_##n##_port_refcounts[portnum]; \
312 a->busy_##n##_ports_per_thread[thread_index]++; \
313 a->busy_##n##_ports++; \
315 *port = clib_host_to_net_u16 (portnum); \
319 else if (a->fib_index == ~0) \
327 default : nat_elog_info (sm, "unknown protocol");
337 #define _(N, j, n, s) \
338 case NAT_PROTOCOL_##N: \
341 portnum = (port_per_thread * snat_thread_index) + \
342 snat_random_port (0, port_per_thread - 1) + 1024; \
343 if (a->busy_##n##_port_refcounts[portnum]) \
345 ++a->busy_##n##_port_refcounts[portnum]; \
346 a->busy_##n##_ports_per_thread[thread_index]++; \
347 a->busy_##n##_ports++; \
349 *port = clib_host_to_net_u16 (portnum); \
355 default : nat_elog_info (sm, "unknown protocol");
360 /* Totally out of translations to use... */
361 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
365 static snat_session_t *
366 create_session_for_static_mapping_ed (
367 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
368 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
369 nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
370 u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
371 snat_static_mapping_t *mapping)
376 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
379 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
381 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
382 nat_elog_notice (sm, "maximum sessions exceeded");
386 s = nat_ed_session_alloc (sm, thread_index, now, nat_proto);
389 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
390 nat_elog_warn (sm, "create NAT session failed");
394 ip = vlib_buffer_get_current (b);
395 udp = ip4_next_header (ip);
397 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
398 s->ext_host_port = nat_proto == NAT_PROTOCOL_ICMP ? 0 : udp->src_port;
399 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
401 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
402 if (lb_nat == AFFINITY_LB_NAT)
403 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
404 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
405 s->out2in.addr = o2i_addr;
406 s->out2in.port = o2i_port;
407 s->out2in.fib_index = o2i_fib_index;
408 s->in2out.addr = i2o_addr;
409 s->in2out.port = i2o_port;
410 s->in2out.fib_index = i2o_fib_index;
411 s->nat_proto = nat_proto;
413 if (NAT_PROTOCOL_ICMP == nat_proto)
415 nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr, o2i_port,
416 o2i_addr, o2i_port, o2i_fib_index, ip->protocol);
417 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, i2o_port);
421 nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr,
422 s->ext_host_port, o2i_addr, o2i_port,
423 o2i_fib_index, ip->protocol);
424 nat_6t_flow_dport_rewrite_set (&s->o2i, i2o_port);
426 nat_6t_flow_daddr_rewrite_set (&s->o2i, i2o_addr.as_u32);
427 nat_6t_flow_txfib_rewrite_set (&s->o2i, i2o_fib_index);
429 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
431 b->error = node->errors[NAT_OUT2IN_ED_ERROR_HASH_ADD_FAILED];
432 nat_ed_session_delete (sm, s, thread_index, 1);
433 nat_elog_warn (sm, "out2in flow hash add failed");
437 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
438 ip->src_address.as_u32 == i2o_addr.as_u32))
441 snat_address_t *filter = 0;
443 // if exact address is specified use this address
444 if (is_exact_address (mapping))
447 vec_foreach (ap, sm->twice_nat_addresses)
449 if (mapping->pool_addr.as_u32 == ap->addr.as_u32)
459 rc = nat_alloc_addr_and_port_exact (filter,
462 &s->ext_host_nat_addr,
463 &s->ext_host_nat_port,
465 tsm->snat_thread_index);
466 s->flags |= SNAT_SESSION_FLAG_EXACT_ADDRESS;
470 rc = nat44_ed_alloc_outside_addr_and_port (
471 sm->twice_nat_addresses, 0, thread_index, nat_proto,
472 &s->ext_host_nat_addr, &s->ext_host_nat_port, sm->port_per_thread,
473 tsm->snat_thread_index);
478 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
479 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
481 nat_elog_warn (sm, "out2in flow hash del failed");
483 snat_free_outside_address_and_port (
484 sm->twice_nat_addresses, thread_index, &s->ext_host_nat_addr,
485 s->ext_host_nat_port, s->nat_proto);
486 nat_ed_session_delete (sm, s, thread_index, 1);
490 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
492 nat_6t_flow_saddr_rewrite_set (&s->o2i, s->ext_host_nat_addr.as_u32);
493 if (NAT_PROTOCOL_ICMP == nat_proto)
495 nat_6t_flow_icmp_id_rewrite_set (&s->o2i, s->ext_host_nat_port);
499 nat_6t_flow_sport_rewrite_set (&s->o2i, s->ext_host_nat_port);
502 nat_6t_l3_l4_csum_calc (&s->o2i);
504 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
505 s->ext_host_nat_addr, s->ext_host_nat_port,
506 i2o_fib_index, ip->protocol);
507 nat_6t_flow_daddr_rewrite_set (&s->i2o, s->ext_host_addr.as_u32);
508 if (NAT_PROTOCOL_ICMP == nat_proto)
510 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, s->ext_host_port);
514 nat_6t_flow_dport_rewrite_set (&s->i2o, s->ext_host_port);
519 if (NAT_PROTOCOL_ICMP == nat_proto)
521 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
522 s->ext_host_addr, i2o_port, i2o_fib_index,
527 nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
528 s->ext_host_addr, s->ext_host_port,
529 i2o_fib_index, ip->protocol);
533 nat_6t_flow_saddr_rewrite_set (&s->i2o, o2i_addr.as_u32);
534 if (NAT_PROTOCOL_ICMP == nat_proto)
536 nat_6t_flow_icmp_id_rewrite_set (&s->i2o, o2i_port);
540 nat_6t_flow_sport_rewrite_set (&s->i2o, o2i_port);
543 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
545 nat_elog_notice (sm, "in2out flow hash add failed");
546 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
548 nat_elog_warn (sm, "out2in flow hash del failed");
550 nat_ed_session_delete (sm, s, thread_index, 1);
554 nat_ipfix_logging_nat44_ses_create (thread_index,
555 s->in2out.addr.as_u32,
556 s->out2in.addr.as_u32,
559 s->out2in.port, s->in2out.fib_index);
561 nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
562 s->in2out.port, &s->ext_host_nat_addr,
563 s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
564 &s->ext_host_addr, s->ext_host_port, s->nat_proto,
565 is_twice_nat_session (s));
567 per_vrf_sessions_register_session (s, thread_index);
573 create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b, snat_session_t *s,
574 ip4_header_t *ip, u32 rx_fib_index, u32 thread_index)
576 clib_bihash_kv_16_8_t kv, value;
578 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
579 vlib_main_t *vm = vlib_get_main ();
580 f64 now = vlib_time_now (vm);
581 u16 lookup_sport, lookup_dport;
583 ip4_address_t lookup_saddr, lookup_daddr;
585 if (ip->protocol == IP_PROTOCOL_ICMP)
587 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
588 &lookup_sport, &lookup_daddr,
589 &lookup_dport, &lookup_protocol))
594 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
596 udp = ip4_next_header (ip);
597 lookup_sport = udp->dst_port;
598 lookup_dport = udp->src_port;
605 lookup_saddr.as_u32 = ip->dst_address.as_u32;
606 lookup_daddr.as_u32 = ip->src_address.as_u32;
607 lookup_protocol = ip->protocol;
610 init_ed_k (&kv, lookup_saddr, lookup_sport, lookup_daddr, lookup_dport,
611 rx_fib_index, lookup_protocol);
613 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
615 ASSERT (thread_index == ed_value_get_thread_index (&value));
617 pool_elt_at_index (tsm->sessions,
618 ed_value_get_session_index (&value));
620 else if (ip->protocol == IP_PROTOCOL_ICMP &&
621 icmp_type_is_error_message
622 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
631 (nat44_ed_maximum_sessions_exceeded
632 (sm, rx_fib_index, thread_index)))
635 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
638 nat_elog_warn (sm, "create NAT session failed");
642 proto = ip_proto_to_nat_proto (ip->protocol);
644 s->ext_host_addr = ip->src_address;
645 s->ext_host_port = lookup_dport;
646 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
647 s->out2in.addr = ip->dst_address;
648 s->out2in.port = lookup_sport;
649 s->nat_proto = proto;
650 if (proto == NAT_PROTOCOL_OTHER)
652 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
653 s->out2in.port = ip->protocol;
655 s->out2in.fib_index = rx_fib_index;
656 s->in2out.addr = s->out2in.addr;
657 s->in2out.port = s->out2in.port;
658 s->in2out.fib_index = s->out2in.fib_index;
660 nat_6t_i2o_flow_init (sm, thread_index, s, ip->dst_address, lookup_sport,
661 ip->src_address, lookup_dport, rx_fib_index,
663 nat_6t_flow_txfib_rewrite_set (&s->i2o, rx_fib_index);
664 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
666 nat_elog_notice (sm, "in2out flow add failed");
667 nat_ed_session_delete (sm, s, thread_index, 1);
671 per_vrf_sessions_register_session (s, thread_index);
674 if (ip->protocol == IP_PROTOCOL_TCP)
676 tcp_header_t *tcp = ip4_next_header (ip);
677 nat44_set_tcp_session_state_o2i (sm, now, s, tcp->flags,
678 tcp->ack_number, tcp->seq_number,
683 nat44_session_update_counters (s, now, 0, thread_index);
684 /* Per-user LRU list maintenance */
685 nat44_session_update_lru (sm, s, thread_index);
688 static snat_session_t *
689 nat44_ed_out2in_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
690 ip4_header_t *ip, u32 rx_fib_index,
691 u32 thread_index, f64 now,
693 vlib_node_runtime_t *node)
695 clib_bihash_kv_8_8_t kv, value;
696 snat_static_mapping_t *m;
700 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
702 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
703 nat_elog_notice (sm, "maximum sessions exceeded");
707 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
708 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
710 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
714 m = pool_elt_at_index (sm->static_mappings, value.value);
716 /* Create a new session */
717 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
720 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
721 nat_elog_warn (sm, "create NAT session failed");
725 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
726 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
727 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
728 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
729 s->out2in.addr.as_u32 = ip->dst_address.as_u32;
730 s->out2in.fib_index = rx_fib_index;
731 s->in2out.addr.as_u32 = m->local_addr.as_u32;
732 s->in2out.fib_index = m->fib_index;
733 s->in2out.port = s->out2in.port = ip->protocol;
735 nat_6t_o2i_flow_init (sm, thread_index, s, ip->dst_address, 0,
736 ip->src_address, 0, m->fib_index, ip->protocol);
737 nat_6t_flow_saddr_rewrite_set (&s->i2o, ip->dst_address.as_u32);
738 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
740 nat_elog_notice (sm, "in2out key add failed");
741 nat_ed_session_delete (sm, s, thread_index, 1);
745 nat_6t_o2i_flow_init (sm, thread_index, s, ip->src_address, 0,
746 ip->dst_address, 0, rx_fib_index, ip->protocol);
747 nat_6t_flow_daddr_rewrite_set (&s->o2i, m->local_addr.as_u32);
748 nat_6t_flow_txfib_rewrite_set (&s->o2i, m->fib_index);
749 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
751 nat_elog_notice (sm, "out2in flow hash add failed");
752 nat_ed_session_delete (sm, s, thread_index, 1);
756 per_vrf_sessions_register_session (s, thread_index);
759 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
761 /* Per-user LRU list maintenance */
762 nat44_session_update_lru (sm, s, thread_index);
768 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
769 vlib_node_runtime_t * node,
770 vlib_frame_t * frame,
773 u32 n_left_from, *from;
774 snat_main_t *sm = &snat_main;
775 f64 now = vlib_time_now (vm);
776 u32 thread_index = vm->thread_index;
777 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
779 from = vlib_frame_vector_args (frame);
780 n_left_from = frame->n_vectors;
782 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
783 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
784 vlib_get_buffers (vm, from, b, n_left_from);
786 while (n_left_from > 0)
789 u32 sw_if_index0, rx_fib_index0;
790 nat_protocol_t proto0;
792 snat_session_t *s0 = 0;
793 clib_bihash_kv_16_8_t kv0, value0;
794 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
795 nat_6t_flow_t *f = 0;
796 ip4_address_t lookup_saddr, lookup_daddr;
797 u16 lookup_sport, lookup_dport;
799 int lookup_skipped = 0;
804 lookup_sport = vnet_buffer (b0)->ip.reass.l4_src_port;
805 lookup_dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
807 /* Prefetch next iteration. */
808 if (PREDICT_TRUE (n_left_from >= 2))
814 vlib_prefetch_buffer_header (p2, LOAD);
816 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
819 next[0] = vnet_buffer2 (b0)->nat.arc_next;
821 vnet_buffer (b0)->snat.flags = 0;
822 ip0 = vlib_buffer_get_current (b0);
824 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
826 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
828 if (PREDICT_FALSE (ip0->ttl == 1))
830 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
831 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
832 ICMP4_time_exceeded_ttl_exceeded_in_transit,
834 next[0] = NAT_NEXT_ICMP_ERROR;
838 proto0 = ip_proto_to_nat_proto (ip0->protocol);
840 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
842 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
843 ICMP4_echo_request &&
844 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
846 !icmp_type_is_error_message (
847 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
849 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
850 next[0] = NAT_NEXT_DROP;
853 int err = nat_get_icmp_session_lookup_values (
854 b0, ip0, &lookup_saddr, &lookup_sport, &lookup_daddr,
855 &lookup_dport, &lookup_protocol);
858 b0->error = node->errors[err];
859 next[0] = NAT_NEXT_DROP;
865 lookup_saddr.as_u32 = ip0->src_address.as_u32;
866 lookup_daddr.as_u32 = ip0->dst_address.as_u32;
867 lookup_protocol = ip0->protocol;
870 /* there might be a stashed index in vnet_buffer2 from handoff or
871 * classify node, see if it can be used */
872 if (!pool_is_free_index (tsm->sessions,
873 vnet_buffer2 (b0)->nat.cached_session_index))
875 s0 = pool_elt_at_index (tsm->sessions,
876 vnet_buffer2 (b0)->nat.cached_session_index);
878 nat_6t_flow_match (&s0->o2i, b0, lookup_saddr, lookup_sport,
879 lookup_daddr, lookup_dport, lookup_protocol,
881 (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
882 nat_6t_flow_match (&s0->i2o, b0, lookup_saddr, lookup_sport,
883 lookup_daddr, lookup_dport,
884 lookup_protocol, rx_fib_index0))))
886 /* yes, this is the droid we're looking for */
893 init_ed_k (&kv0, lookup_saddr, lookup_sport, lookup_daddr, lookup_dport,
894 rx_fib_index0, lookup_protocol);
897 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
899 // flow does not exist go slow path
900 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
903 ASSERT (thread_index == ed_value_get_thread_index (&value0));
905 pool_elt_at_index (tsm->sessions,
906 ed_value_get_session_index (&value0));
909 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
911 // session is closed, go slow path
912 nat_free_session_data (sm, s0, thread_index, 0);
913 nat_ed_session_delete (sm, s0, thread_index, 1);
914 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
918 if (s0->tcp_closed_timestamp)
920 if (now >= s0->tcp_closed_timestamp)
922 // session is closed, go slow path, freed in slow path
923 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
927 // session in transitory timeout, drop
928 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
929 next[0] = NAT_NEXT_DROP;
934 // drop if session expired
935 u64 sess_timeout_time;
937 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
938 if (now >= sess_timeout_time)
940 // session is closed, go slow path
941 nat_free_session_data (sm, s0, thread_index, 0);
942 nat_ed_session_delete (sm, s0, thread_index, 1);
943 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
947 if (nat_6t_flow_match (&s0->o2i, b0, lookup_saddr, lookup_sport,
948 lookup_daddr, lookup_dport, lookup_protocol,
953 else if (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
954 nat_6t_flow_match (&s0->i2o, b0, lookup_saddr, lookup_sport,
955 lookup_daddr, lookup_dport, lookup_protocol,
963 * Send DHCP packets to the ipv4 stack, or we won't
964 * be able to use dhcp client on the outside interface
967 proto0 == NAT_PROTOCOL_UDP &&
968 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
969 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
974 if (!sm->forwarding_enabled)
976 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
977 next[0] = NAT_NEXT_DROP;
982 if (nat_6t_flow_match (&s0->i2o, b0, lookup_saddr, lookup_sport,
983 lookup_daddr, lookup_dport,
984 lookup_protocol, rx_fib_index0))
990 // FIXME TODO bypass ???
991 // create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
993 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
994 nat_free_session_data (sm, s0, thread_index, 0);
995 nat_ed_session_delete (sm, s0, thread_index, 1);
996 next[0] = NAT_NEXT_DROP;
1002 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1003 (translation_error = nat_6t_flow_buf_translate (
1004 sm, b0, ip0, f, proto0, 0 /* is_output_feature */)))
1006 next[0] = NAT_NEXT_DROP;
1012 case NAT_PROTOCOL_TCP:
1013 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.tcp,
1014 thread_index, sw_if_index0, 1);
1015 nat44_set_tcp_session_state_o2i (sm, now, s0,
1016 vnet_buffer (b0)->ip.
1017 reass.icmp_type_or_tcp_flags,
1018 vnet_buffer (b0)->ip.
1019 reass.tcp_ack_number,
1020 vnet_buffer (b0)->ip.
1021 reass.tcp_seq_number,
1024 case NAT_PROTOCOL_UDP:
1025 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.udp,
1026 thread_index, sw_if_index0, 1);
1028 case NAT_PROTOCOL_ICMP:
1029 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.icmp,
1030 thread_index, sw_if_index0, 1);
1032 case NAT_PROTOCOL_OTHER:
1033 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.other,
1034 thread_index, sw_if_index0, 1);
1039 nat44_session_update_counters (s0, now,
1040 vlib_buffer_length_in_chain (vm, b0),
1042 /* Per-user LRU list maintenance */
1043 nat44_session_update_lru (sm, s0, thread_index);
1046 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1047 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1049 nat44_ed_out2in_trace_t *t =
1050 vlib_add_trace (vm, node, b0, sizeof (*t));
1051 t->sw_if_index = sw_if_index0;
1052 t->next_index = next[0];
1053 t->is_slow_path = 0;
1054 t->translation_error = translation_error;
1055 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1056 t->lookup_skipped = lookup_skipped;
1060 t->session_index = s0 - tsm->sessions;
1061 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1062 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1063 t->translation_via_i2of = (&s0->i2o == f);
1067 t->session_index = ~0;
1071 if (next[0] == NAT_NEXT_DROP)
1073 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1074 thread_index, sw_if_index0, 1);
1081 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1083 return frame->n_vectors;
1087 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
1088 vlib_node_runtime_t * node,
1089 vlib_frame_t * frame)
1091 u32 n_left_from, *from;
1092 snat_main_t *sm = &snat_main;
1093 f64 now = vlib_time_now (vm);
1094 u32 thread_index = vm->thread_index;
1095 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1096 snat_static_mapping_t *m;
1098 from = vlib_frame_vector_args (frame);
1099 n_left_from = frame->n_vectors;
1101 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1102 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1103 vlib_get_buffers (vm, from, b, n_left_from);
1105 while (n_left_from > 0)
1108 u32 sw_if_index0, rx_fib_index0;
1109 nat_protocol_t proto0;
1112 icmp46_header_t *icmp0;
1113 snat_session_t *s0 = 0;
1114 clib_bihash_kv_16_8_t kv0, value0;
1115 lb_nat_type_t lb_nat0;
1116 twice_nat_type_t twice_nat0;
1118 ip4_address_t sm_addr;
1121 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1124 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1126 vnet_buffer (b0)->snat.flags = 0;
1127 ip0 = vlib_buffer_get_current (b0);
1129 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1131 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1133 if (PREDICT_FALSE (ip0->ttl == 1))
1135 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1136 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1137 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1139 next[0] = NAT_NEXT_ICMP_ERROR;
1143 udp0 = ip4_next_header (ip0);
1144 icmp0 = (icmp46_header_t *) udp0;
1145 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1147 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1149 s0 = nat44_ed_out2in_slowpath_unknown_proto (
1150 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1151 if (!sm->forwarding_enabled)
1154 next[0] = NAT_NEXT_DROP;
1156 if (NAT_NEXT_DROP != next[0] &&
1157 NAT_ED_TRNSL_ERR_SUCCESS !=
1158 (translation_error = nat_6t_flow_buf_translate (
1159 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1164 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1165 thread_index, sw_if_index0, 1);
1169 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1171 next[0] = icmp_out2in_ed_slow_path
1172 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1173 next[0], now, thread_index, &s0);
1175 if (NAT_NEXT_DROP != next[0] && s0 &&
1176 NAT_ED_TRNSL_ERR_SUCCESS !=
1177 (translation_error = nat_6t_flow_buf_translate (
1178 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1183 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1184 thread_index, sw_if_index0, 1);
1188 init_ed_k (&kv0, ip0->src_address,
1189 vnet_buffer (b0)->ip.reass.l4_src_port, ip0->dst_address,
1190 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1194 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1196 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1198 pool_elt_at_index (tsm->sessions,
1199 ed_value_get_session_index (&value0));
1201 if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1203 nat_free_session_data (sm, s0, thread_index, 0);
1204 nat_ed_session_delete (sm, s0, thread_index, 1);
1211 /* Try to match static mapping by external address and port,
1212 destination address and port in packet */
1214 if (snat_static_mapping_match
1215 (sm, ip0->dst_address,
1216 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1217 proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1218 &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0, &m))
1221 * Send DHCP packets to the ipv4 stack, or we won't
1222 * be able to use dhcp client on the outside interface
1224 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1225 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1226 clib_host_to_net_u16
1227 (UDP_DST_PORT_dhcp_to_client))))
1232 if (!sm->forwarding_enabled)
1235 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1236 next[0] = NAT_NEXT_DROP;
1241 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1242 vnet_buffer (b0)->ip.reass.l4_dst_port,
1243 thread_index, rx_fib_index0))
1245 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1249 create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
1256 if (PREDICT_FALSE (identity_nat0))
1259 if ((proto0 == NAT_PROTOCOL_TCP)
1260 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1261 reass.icmp_type_or_tcp_flags))
1263 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1264 next[0] = NAT_NEXT_DROP;
1268 /* Create session initiated by host from external network */
1269 s0 = create_session_for_static_mapping_ed (sm, b0,
1274 ip.reass.l4_dst_port,
1275 rx_fib_index0, proto0,
1276 node, rx_fib_index0,
1277 thread_index, twice_nat0,
1281 next[0] = NAT_NEXT_DROP;
1286 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1287 (translation_error = nat_6t_flow_buf_translate (
1288 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1290 next[0] = NAT_NEXT_DROP;
1294 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1296 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1297 thread_index, sw_if_index0, 1);
1298 nat44_set_tcp_session_state_o2i (sm, now, s0,
1299 vnet_buffer (b0)->ip.
1300 reass.icmp_type_or_tcp_flags,
1301 vnet_buffer (b0)->ip.
1302 reass.tcp_ack_number,
1303 vnet_buffer (b0)->ip.
1304 reass.tcp_seq_number,
1309 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1310 thread_index, sw_if_index0, 1);
1314 nat44_session_update_counters (s0, now,
1315 vlib_buffer_length_in_chain (vm, b0),
1317 /* Per-user LRU list maintenance */
1318 nat44_session_update_lru (sm, s0, thread_index);
1321 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1322 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1324 nat44_ed_out2in_trace_t *t =
1325 vlib_add_trace (vm, node, b0, sizeof (*t));
1326 t->sw_if_index = sw_if_index0;
1327 t->next_index = next[0];
1328 t->is_slow_path = 1;
1329 t->translation_error = translation_error;
1330 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1334 t->session_index = s0 - tsm->sessions;
1335 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1336 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1340 t->session_index = ~0;
1344 if (next[0] == NAT_NEXT_DROP)
1346 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1347 thread_index, sw_if_index0, 1);
1355 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1358 return frame->n_vectors;
1361 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1362 vlib_node_runtime_t * node,
1363 vlib_frame_t * frame)
1365 if (snat_main.num_workers > 1)
1367 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1371 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1376 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1377 .name = "nat44-ed-out2in",
1378 .vector_size = sizeof (u32),
1379 .sibling_of = "nat-default",
1380 .format_trace = format_nat44_ed_out2in_trace,
1381 .type = VLIB_NODE_TYPE_INTERNAL,
1382 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1383 .error_strings = nat_out2in_ed_error_strings,
1384 .runtime_data_bytes = sizeof (snat_runtime_t),
1388 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1389 vlib_node_runtime_t * node,
1390 vlib_frame_t * frame)
1392 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1396 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1397 .name = "nat44-ed-out2in-slowpath",
1398 .vector_size = sizeof (u32),
1399 .sibling_of = "nat-default",
1400 .format_trace = format_nat44_ed_out2in_trace,
1401 .type = VLIB_NODE_TYPE_INTERNAL,
1402 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1403 .error_strings = nat_out2in_ed_error_strings,
1404 .runtime_data_bytes = sizeof (snat_runtime_t),
1409 format_nat_pre_trace (u8 * s, va_list * args)
1411 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1412 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1413 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1414 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1418 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1419 vlib_node_runtime_t * node,
1420 vlib_frame_t * frame)
1422 return nat_pre_node_fn_inline (vm, node, frame,
1423 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1427 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1428 .name = "nat-pre-out2in",
1429 .vector_size = sizeof (u32),
1430 .sibling_of = "nat-default",
1431 .format_trace = format_nat_pre_trace,
1432 .type = VLIB_NODE_TYPE_INTERNAL,
1438 * fd.io coding-style-patch-verification: ON
1441 * eval: (c-set-style "gnu")