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", tag, t->sw_if_index,
68 if (~0 != t->session_index)
70 s = format (s, ", session %d, translation result '%U' via %s",
71 t->session_index, format_nat_ed_translation_error,
73 t->translation_via_i2of ? "i2of" : "o2if");
74 s = format (s, "\n i2of %U", format_nat_6t_flow, &t->i2of);
75 s = format (s, "\n o2if %U", format_nat_6t_flow, &t->o2if);
79 if (t->lookup_skipped)
81 s = format (s, "\n lookup skipped - cached session index used");
85 s = format (s, "\n search key %U", format_ed_session_kvp,
94 next_src_nat (snat_main_t *sm, ip4_header_t *ip, u16 src_port, u16 dst_port,
95 u32 thread_index, u32 rx_fib_index)
97 clib_bihash_kv_16_8_t kv, value;
99 init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
100 rx_fib_index, ip->protocol);
101 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
107 static void create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b,
108 snat_session_t *s, ip4_header_t *ip,
109 u32 rx_fib_index, u32 thread_index);
111 static snat_session_t *create_session_for_static_mapping_ed (
112 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
113 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
114 nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
115 u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
116 snat_static_mapping_t *mapping);
119 icmp_out2in_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
120 icmp46_header_t *icmp, u32 sw_if_index,
121 u32 rx_fib_index, vlib_node_runtime_t *node,
122 u32 next, f64 now, u32 thread_index,
123 snat_session_t **s_p)
125 vlib_main_t *vm = vlib_get_main ();
130 snat_session_t *s = 0;
131 u8 is_addr_only, identity_nat;
132 ip4_address_t sm_addr;
135 snat_static_mapping_t *m;
137 ip4_address_t lookup_saddr, lookup_daddr;
138 u16 lookup_sport, lookup_dport;
140 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
141 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
143 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr, &lookup_sport,
144 &lookup_daddr, &lookup_dport,
147 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
148 next = NAT_NEXT_DROP;
152 if (snat_static_mapping_match (
153 vm, sm, ip->dst_address, lookup_sport, rx_fib_index,
154 ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
155 &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat, &m))
157 // static mapping not matched
158 if (!sm->forwarding_enabled)
160 /* Don't NAT packet aimed at the intfc address */
161 if (!is_interface_addr (sm, node, sw_if_index,
162 ip->dst_address.as_u32))
164 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
165 next = NAT_NEXT_DROP;
170 if (next_src_nat (sm, ip, lookup_sport, lookup_dport, thread_index,
173 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
177 create_bypass_for_fwd (sm, b, s, ip, rx_fib_index, thread_index);
183 if (PREDICT_FALSE (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
185 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
186 ICMP4_echo_request ||
189 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
190 next = NAT_NEXT_DROP;
194 if (PREDICT_FALSE (identity_nat))
199 /* Create session initiated by host from external network */
200 s = create_session_for_static_mapping_ed (
201 sm, b, sm_addr, sm_port, sm_fib_index, ip->dst_address, lookup_sport,
202 rx_fib_index, ip_proto_to_nat_proto (lookup_protocol), node, rx_fib_index,
203 thread_index, 0, 0, vlib_time_now (vm), m);
205 next = NAT_NEXT_DROP;
207 if (PREDICT_TRUE (!ip4_is_fragment (ip)))
209 sum = ip_incremental_checksum_buffer (
210 vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
211 ntohs (ip->length) - ip4_header_bytes (ip), 0);
212 checksum = ~ip_csum_fold (sum);
213 if (checksum != 0 && checksum != 0xffff)
215 next = NAT_NEXT_DROP;
220 if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
223 nat44_session_update_counters (
224 s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
225 /* Per-user LRU list maintenance */
226 nat44_session_update_lru (sm, s, thread_index);
229 if (NAT_NEXT_DROP == next && s)
231 nat_ed_session_delete (sm, s, thread_index, 1);
238 // allocate exact address based on preference
239 static_always_inline int
240 nat_alloc_addr_and_port_exact (snat_address_t * a,
242 nat_protocol_t proto,
243 ip4_address_t * addr,
245 u16 port_per_thread, u32 snat_thread_index)
247 snat_main_t *sm = &snat_main;
252 #define _(N, j, n, s) \
253 case NAT_PROTOCOL_##N: \
254 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
258 portnum = (port_per_thread * \
259 snat_thread_index) + \
260 snat_random_port(0, port_per_thread - 1) + 1024; \
261 if (a->busy_##n##_port_refcounts[portnum]) \
263 --a->busy_##n##_port_refcounts[portnum]; \
264 a->busy_##n##_ports_per_thread[thread_index]++; \
265 a->busy_##n##_ports++; \
267 *port = clib_host_to_net_u16(portnum); \
274 default : nat_elog_info (sm, "unknown protocol");
278 /* Totally out of translations to use... */
279 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
283 static_always_inline int
284 nat44_ed_alloc_outside_addr_and_port (snat_address_t *addresses, u32 fib_index,
285 u32 thread_index, nat_protocol_t proto,
286 ip4_address_t *addr, u16 *port,
288 u32 snat_thread_index)
290 snat_main_t *sm = &snat_main;
291 snat_address_t *a, *ga = 0;
295 for (i = 0; i < vec_len (addresses); i++)
300 #define _(N, j, n, s) \
301 case NAT_PROTOCOL_##N: \
302 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
304 if (a->fib_index == fib_index) \
308 portnum = (port_per_thread * snat_thread_index) + \
309 snat_random_port (0, port_per_thread - 1) + 1024; \
310 if (a->busy_##n##_port_refcounts[portnum]) \
312 --a->busy_##n##_port_refcounts[portnum]; \
313 a->busy_##n##_ports_per_thread[thread_index]++; \
314 a->busy_##n##_ports++; \
316 *port = clib_host_to_net_u16 (portnum); \
320 else if (a->fib_index == ~0) \
328 default : nat_elog_info (sm, "unknown protocol");
338 #define _(N, j, n, s) \
339 case NAT_PROTOCOL_##N: \
342 portnum = (port_per_thread * snat_thread_index) + \
343 snat_random_port (0, port_per_thread - 1) + 1024; \
344 if (a->busy_##n##_port_refcounts[portnum]) \
346 ++a->busy_##n##_port_refcounts[portnum]; \
347 a->busy_##n##_ports_per_thread[thread_index]++; \
348 a->busy_##n##_ports++; \
350 *port = clib_host_to_net_u16 (portnum); \
356 default : nat_elog_info (sm, "unknown protocol");
361 /* Totally out of translations to use... */
362 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
366 static snat_session_t *
367 create_session_for_static_mapping_ed (
368 snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
369 u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
370 nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
371 u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
372 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);
396 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
398 nat_proto == NAT_PROTOCOL_ICMP ? 0 : vnet_buffer (b)->ip.reass.l4_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;
577 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
578 vlib_main_t *vm = vlib_get_main ();
579 f64 now = vlib_time_now (vm);
580 u16 lookup_sport, lookup_dport;
582 ip4_address_t lookup_saddr, lookup_daddr;
584 if (ip->protocol == IP_PROTOCOL_ICMP)
586 if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
587 &lookup_sport, &lookup_daddr,
588 &lookup_dport, &lookup_protocol))
593 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
595 lookup_sport = vnet_buffer (b)->ip.reass.l4_dst_port;
596 lookup_dport = vnet_buffer (b)->ip.reass.l4_src_port;
603 lookup_saddr.as_u32 = ip->dst_address.as_u32;
604 lookup_daddr.as_u32 = ip->src_address.as_u32;
605 lookup_protocol = ip->protocol;
608 init_ed_k (&kv, lookup_saddr, lookup_sport, lookup_daddr, lookup_dport,
609 rx_fib_index, lookup_protocol);
611 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
613 ASSERT (thread_index == ed_value_get_thread_index (&value));
615 pool_elt_at_index (tsm->sessions,
616 ed_value_get_session_index (&value));
618 else if (ip->protocol == IP_PROTOCOL_ICMP &&
619 icmp_type_is_error_message
620 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
629 (nat44_ed_maximum_sessions_exceeded
630 (sm, rx_fib_index, thread_index)))
633 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
636 nat_elog_warn (sm, "create NAT session failed");
640 proto = ip_proto_to_nat_proto (ip->protocol);
642 s->ext_host_addr = ip->src_address;
643 s->ext_host_port = lookup_dport;
644 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
645 s->out2in.addr = ip->dst_address;
646 s->out2in.port = lookup_sport;
647 s->nat_proto = proto;
648 if (proto == NAT_PROTOCOL_OTHER)
650 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
651 s->out2in.port = ip->protocol;
653 s->out2in.fib_index = rx_fib_index;
654 s->in2out.addr = s->out2in.addr;
655 s->in2out.port = s->out2in.port;
656 s->in2out.fib_index = s->out2in.fib_index;
658 nat_6t_i2o_flow_init (sm, thread_index, s, ip->dst_address, lookup_sport,
659 ip->src_address, lookup_dport, rx_fib_index,
661 nat_6t_flow_txfib_rewrite_set (&s->i2o, rx_fib_index);
662 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
664 nat_elog_notice (sm, "in2out flow add failed");
665 nat_ed_session_delete (sm, s, thread_index, 1);
669 per_vrf_sessions_register_session (s, thread_index);
672 if (ip->protocol == IP_PROTOCOL_TCP)
674 tcp_header_t *tcp = ip4_next_header (ip);
675 nat44_set_tcp_session_state_o2i (sm, now, s, tcp->flags,
676 tcp->ack_number, tcp->seq_number,
681 nat44_session_update_counters (s, now, 0, thread_index);
682 /* Per-user LRU list maintenance */
683 nat44_session_update_lru (sm, s, thread_index);
686 static snat_session_t *
687 nat44_ed_out2in_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
688 ip4_header_t *ip, u32 rx_fib_index,
689 u32 thread_index, f64 now,
691 vlib_node_runtime_t *node)
693 clib_bihash_kv_8_8_t kv, value;
694 snat_static_mapping_t *m;
698 nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
700 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
701 nat_elog_notice (sm, "maximum sessions exceeded");
705 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
706 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
708 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
712 m = pool_elt_at_index (sm->static_mappings, value.value);
714 /* Create a new session */
715 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
718 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
719 nat_elog_warn (sm, "create NAT session failed");
723 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
724 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
725 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
726 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
727 s->out2in.addr.as_u32 = ip->dst_address.as_u32;
728 s->out2in.fib_index = rx_fib_index;
729 s->in2out.addr.as_u32 = m->local_addr.as_u32;
730 s->in2out.fib_index = m->fib_index;
731 s->in2out.port = s->out2in.port = ip->protocol;
733 nat_6t_o2i_flow_init (sm, thread_index, s, ip->dst_address, 0,
734 ip->src_address, 0, m->fib_index, ip->protocol);
735 nat_6t_flow_saddr_rewrite_set (&s->i2o, ip->dst_address.as_u32);
736 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
738 nat_elog_notice (sm, "in2out key add failed");
739 nat_ed_session_delete (sm, s, thread_index, 1);
743 nat_6t_o2i_flow_init (sm, thread_index, s, ip->src_address, 0,
744 ip->dst_address, 0, rx_fib_index, ip->protocol);
745 nat_6t_flow_daddr_rewrite_set (&s->o2i, m->local_addr.as_u32);
746 nat_6t_flow_txfib_rewrite_set (&s->o2i, m->fib_index);
747 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
749 nat_elog_notice (sm, "out2in flow hash add failed");
750 nat_ed_session_delete (sm, s, thread_index, 1);
754 per_vrf_sessions_register_session (s, thread_index);
757 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
759 /* Per-user LRU list maintenance */
760 nat44_session_update_lru (sm, s, thread_index);
766 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
767 vlib_node_runtime_t * node,
768 vlib_frame_t * frame,
771 u32 n_left_from, *from;
772 snat_main_t *sm = &snat_main;
773 f64 now = vlib_time_now (vm);
774 u32 thread_index = vm->thread_index;
775 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
777 from = vlib_frame_vector_args (frame);
778 n_left_from = frame->n_vectors;
780 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
781 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
782 vlib_get_buffers (vm, from, b, n_left_from);
784 while (n_left_from > 0)
787 u32 sw_if_index0, rx_fib_index0;
788 nat_protocol_t proto0;
790 snat_session_t *s0 = 0;
791 clib_bihash_kv_16_8_t kv0, value0;
792 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
793 nat_6t_flow_t *f = 0;
795 int lookup_skipped = 0;
800 /* Prefetch next iteration. */
801 if (PREDICT_TRUE (n_left_from >= 2))
807 vlib_prefetch_buffer_header (p2, LOAD);
809 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
812 next[0] = vnet_buffer2 (b0)->nat.arc_next;
814 lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
815 lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
817 vnet_buffer (b0)->snat.flags = 0;
818 ip0 = vlib_buffer_get_current (b0);
820 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
822 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
824 lookup.fib_index = rx_fib_index0;
826 if (PREDICT_FALSE (ip0->ttl == 1))
828 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
829 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
830 ICMP4_time_exceeded_ttl_exceeded_in_transit,
832 next[0] = NAT_NEXT_ICMP_ERROR;
836 proto0 = ip_proto_to_nat_proto (ip0->protocol);
838 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
840 if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
841 ICMP4_echo_request &&
842 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
844 !icmp_type_is_error_message (
845 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
847 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
848 next[0] = NAT_NEXT_DROP;
851 int err = nat_get_icmp_session_lookup_values (
852 b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
853 &lookup.dport, &lookup.proto);
856 b0->error = node->errors[err];
857 next[0] = NAT_NEXT_DROP;
863 lookup.saddr.as_u32 = ip0->src_address.as_u32;
864 lookup.daddr.as_u32 = ip0->dst_address.as_u32;
865 lookup.proto = ip0->protocol;
868 /* there might be a stashed index in vnet_buffer2 from handoff or
869 * classify node, see if it can be used */
870 if (is_multi_worker &&
871 !pool_is_free_index (tsm->sessions,
872 vnet_buffer2 (b0)->nat.cached_session_index))
874 s0 = pool_elt_at_index (tsm->sessions,
875 vnet_buffer2 (b0)->nat.cached_session_index);
876 if (PREDICT_TRUE (nat_6t_t_eq (&s0->o2i.match, &lookup)) ||
877 (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
878 nat_6t_t_eq (&s0->i2o.match, &lookup)))
880 /* yes, this is the droid we're looking for */
887 init_ed_k (&kv0, lookup.saddr, lookup.sport, lookup.daddr, lookup.dport,
888 lookup.fib_index, lookup.proto);
891 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
893 // flow does not exist go slow path
894 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
897 ASSERT (thread_index == ed_value_get_thread_index (&value0));
899 pool_elt_at_index (tsm->sessions,
900 ed_value_get_session_index (&value0));
903 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
905 // session is closed, go slow path
906 nat_free_session_data (sm, s0, thread_index, 0);
907 nat_ed_session_delete (sm, s0, thread_index, 1);
908 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
912 if (s0->tcp_closed_timestamp)
914 if (now >= s0->tcp_closed_timestamp)
916 // session is closed, go slow path, freed in slow path
917 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
921 // session in transitory timeout, drop
922 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
923 next[0] = NAT_NEXT_DROP;
928 // drop if session expired
929 u64 sess_timeout_time;
931 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
932 if (now >= sess_timeout_time)
934 // session is closed, go slow path
935 nat_free_session_data (sm, s0, thread_index, 0);
936 nat_ed_session_delete (sm, s0, thread_index, 1);
937 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
941 if (nat_6t_t_eq (&s0->o2i.match, &lookup))
945 else if (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
946 nat_6t_t_eq (&s0->i2o.match, &lookup))
953 * Send DHCP packets to the ipv4 stack, or we won't
954 * be able to use dhcp client on the outside interface
957 proto0 == NAT_PROTOCOL_UDP &&
958 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
959 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
964 if (!sm->forwarding_enabled)
966 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
967 next[0] = NAT_NEXT_DROP;
972 if (nat_6t_t_eq (&s0->i2o.match, &lookup))
978 // FIXME TODO bypass ???
979 // create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
981 translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
982 nat_free_session_data (sm, s0, thread_index, 0);
983 nat_ed_session_delete (sm, s0, thread_index, 1);
984 next[0] = NAT_NEXT_DROP;
990 if (NAT_ED_TRNSL_ERR_SUCCESS !=
991 (translation_error = nat_6t_flow_buf_translate (
992 sm, b0, ip0, f, proto0, 0 /* is_output_feature */)))
994 next[0] = NAT_NEXT_DROP;
1000 case NAT_PROTOCOL_TCP:
1001 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.tcp,
1002 thread_index, sw_if_index0, 1);
1003 nat44_set_tcp_session_state_o2i (sm, now, s0,
1004 vnet_buffer (b0)->ip.
1005 reass.icmp_type_or_tcp_flags,
1006 vnet_buffer (b0)->ip.
1007 reass.tcp_ack_number,
1008 vnet_buffer (b0)->ip.
1009 reass.tcp_seq_number,
1012 case NAT_PROTOCOL_UDP:
1013 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.udp,
1014 thread_index, sw_if_index0, 1);
1016 case NAT_PROTOCOL_ICMP:
1017 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.icmp,
1018 thread_index, sw_if_index0, 1);
1020 case NAT_PROTOCOL_OTHER:
1021 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.other,
1022 thread_index, sw_if_index0, 1);
1027 nat44_session_update_counters (s0, now,
1028 vlib_buffer_length_in_chain (vm, b0),
1030 /* Per-user LRU list maintenance */
1031 nat44_session_update_lru (sm, s0, thread_index);
1034 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1035 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1037 nat44_ed_out2in_trace_t *t =
1038 vlib_add_trace (vm, node, b0, sizeof (*t));
1039 t->sw_if_index = sw_if_index0;
1040 t->next_index = next[0];
1041 t->is_slow_path = 0;
1042 t->translation_error = translation_error;
1043 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1044 t->lookup_skipped = lookup_skipped;
1048 t->session_index = s0 - tsm->sessions;
1049 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1050 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1051 t->translation_via_i2of = (&s0->i2o == f);
1055 t->session_index = ~0;
1059 if (next[0] == NAT_NEXT_DROP)
1061 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1062 thread_index, sw_if_index0, 1);
1069 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1071 return frame->n_vectors;
1075 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
1076 vlib_node_runtime_t * node,
1077 vlib_frame_t * frame)
1079 u32 n_left_from, *from;
1080 snat_main_t *sm = &snat_main;
1081 f64 now = vlib_time_now (vm);
1082 u32 thread_index = vm->thread_index;
1083 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1084 snat_static_mapping_t *m;
1086 from = vlib_frame_vector_args (frame);
1087 n_left_from = frame->n_vectors;
1089 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1090 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1091 vlib_get_buffers (vm, from, b, n_left_from);
1093 while (n_left_from > 0)
1096 u32 sw_if_index0, rx_fib_index0;
1097 nat_protocol_t proto0;
1100 icmp46_header_t *icmp0;
1101 snat_session_t *s0 = 0;
1102 clib_bihash_kv_16_8_t kv0, value0;
1103 lb_nat_type_t lb_nat0;
1104 twice_nat_type_t twice_nat0;
1106 ip4_address_t sm_addr;
1109 nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1112 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1114 vnet_buffer (b0)->snat.flags = 0;
1115 ip0 = vlib_buffer_get_current (b0);
1117 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1119 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1121 if (PREDICT_FALSE (ip0->ttl == 1))
1123 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1124 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1125 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1127 next[0] = NAT_NEXT_ICMP_ERROR;
1131 udp0 = ip4_next_header (ip0);
1132 icmp0 = (icmp46_header_t *) udp0;
1133 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1135 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1137 s0 = nat44_ed_out2in_slowpath_unknown_proto (
1138 sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1139 if (!sm->forwarding_enabled)
1142 next[0] = NAT_NEXT_DROP;
1144 if (NAT_NEXT_DROP != next[0] &&
1145 NAT_ED_TRNSL_ERR_SUCCESS !=
1146 (translation_error = nat_6t_flow_buf_translate (
1147 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1152 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1153 thread_index, sw_if_index0, 1);
1157 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1159 next[0] = icmp_out2in_ed_slow_path
1160 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1161 next[0], now, thread_index, &s0);
1163 if (NAT_NEXT_DROP != next[0] && s0 &&
1164 NAT_ED_TRNSL_ERR_SUCCESS !=
1165 (translation_error = nat_6t_flow_buf_translate (
1166 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1171 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1172 thread_index, sw_if_index0, 1);
1176 init_ed_k (&kv0, ip0->src_address,
1177 vnet_buffer (b0)->ip.reass.l4_src_port, ip0->dst_address,
1178 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1182 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1184 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1186 pool_elt_at_index (tsm->sessions,
1187 ed_value_get_session_index (&value0));
1189 if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1191 nat_free_session_data (sm, s0, thread_index, 0);
1192 nat_ed_session_delete (sm, s0, thread_index, 1);
1199 /* Try to match static mapping by external address and port,
1200 destination address and port in packet */
1202 if (snat_static_mapping_match (
1203 vm, sm, ip0->dst_address,
1204 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0, proto0,
1205 &sm_addr, &sm_port, &sm_fib_index, 1, 0, &twice_nat0, &lb_nat0,
1206 &ip0->src_address, &identity_nat0, &m))
1209 * Send DHCP packets to the ipv4 stack, or we won't
1210 * be able to use dhcp client on the outside interface
1212 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1213 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1214 clib_host_to_net_u16
1215 (UDP_DST_PORT_dhcp_to_client))))
1220 if (!sm->forwarding_enabled)
1223 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1224 next[0] = NAT_NEXT_DROP;
1229 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1230 vnet_buffer (b0)->ip.reass.l4_dst_port,
1231 thread_index, rx_fib_index0))
1233 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1237 create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
1244 if (PREDICT_FALSE (identity_nat0))
1247 if ((proto0 == NAT_PROTOCOL_TCP)
1248 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1249 reass.icmp_type_or_tcp_flags))
1251 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1252 next[0] = NAT_NEXT_DROP;
1256 /* Create session initiated by host from external network */
1257 s0 = create_session_for_static_mapping_ed (sm, b0,
1262 ip.reass.l4_dst_port,
1263 rx_fib_index0, proto0,
1264 node, rx_fib_index0,
1265 thread_index, twice_nat0,
1269 next[0] = NAT_NEXT_DROP;
1274 if (NAT_ED_TRNSL_ERR_SUCCESS !=
1275 (translation_error = nat_6t_flow_buf_translate (
1276 sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1278 next[0] = NAT_NEXT_DROP;
1282 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1284 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1285 thread_index, sw_if_index0, 1);
1286 nat44_set_tcp_session_state_o2i (sm, now, s0,
1287 vnet_buffer (b0)->ip.
1288 reass.icmp_type_or_tcp_flags,
1289 vnet_buffer (b0)->ip.
1290 reass.tcp_ack_number,
1291 vnet_buffer (b0)->ip.
1292 reass.tcp_seq_number,
1297 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1298 thread_index, sw_if_index0, 1);
1302 nat44_session_update_counters (s0, now,
1303 vlib_buffer_length_in_chain (vm, b0),
1305 /* Per-user LRU list maintenance */
1306 nat44_session_update_lru (sm, s0, thread_index);
1309 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1310 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1312 nat44_ed_out2in_trace_t *t =
1313 vlib_add_trace (vm, node, b0, sizeof (*t));
1314 t->sw_if_index = sw_if_index0;
1315 t->next_index = next[0];
1316 t->is_slow_path = 1;
1317 t->translation_error = translation_error;
1318 clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1322 t->session_index = s0 - tsm->sessions;
1323 clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1324 clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1328 t->session_index = ~0;
1332 if (next[0] == NAT_NEXT_DROP)
1334 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1335 thread_index, sw_if_index0, 1);
1343 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1346 return frame->n_vectors;
1349 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1350 vlib_node_runtime_t * node,
1351 vlib_frame_t * frame)
1353 if (snat_main.num_workers > 1)
1355 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1359 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1364 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1365 .name = "nat44-ed-out2in",
1366 .vector_size = sizeof (u32),
1367 .sibling_of = "nat-default",
1368 .format_trace = format_nat44_ed_out2in_trace,
1369 .type = VLIB_NODE_TYPE_INTERNAL,
1370 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1371 .error_strings = nat_out2in_ed_error_strings,
1372 .runtime_data_bytes = sizeof (snat_runtime_t),
1376 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1377 vlib_node_runtime_t * node,
1378 vlib_frame_t * frame)
1380 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1384 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1385 .name = "nat44-ed-out2in-slowpath",
1386 .vector_size = sizeof (u32),
1387 .sibling_of = "nat-default",
1388 .format_trace = format_nat44_ed_out2in_trace,
1389 .type = VLIB_NODE_TYPE_INTERNAL,
1390 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1391 .error_strings = nat_out2in_ed_error_strings,
1392 .runtime_data_bytes = sizeof (snat_runtime_t),
1397 format_nat_pre_trace (u8 * s, va_list * args)
1399 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1400 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1401 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1402 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1406 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1407 vlib_node_runtime_t * node,
1408 vlib_frame_t * frame)
1410 return nat_pre_node_fn_inline (vm, node, frame,
1411 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1415 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1416 .name = "nat-pre-out2in",
1417 .vector_size = sizeof (u32),
1418 .sibling_of = "nat-default",
1419 .format_trace = format_nat_pre_trace,
1420 .type = VLIB_NODE_TYPE_INTERNAL,
1426 * fd.io coding-style-patch-verification: ON
1429 * eval: (c-set-style "gnu")