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.h>
26 #include <vppinfra/error.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_inlines.h>
30 #include <nat/nat44/inlines.h>
31 #include <nat/nat_syslog.h>
32 #include <nat/nat_ha.h>
33 #include <nat/nat44/ed_inlines.h>
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
37 foreach_nat_out2in_ed_error
47 } nat44_ed_out2in_trace_t;
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
58 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59 "NAT44_OUT2IN_ED_FAST_PATH";
61 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62 t->sw_if_index, t->next_index, t->session_index);
68 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
69 ip4_header_t * ip0, icmp46_header_t * icmp0,
70 u32 sw_if_index0, u32 rx_fib_index0,
71 vlib_node_runtime_t * node, u32 next0, f64 now,
72 u32 thread_index, snat_session_t ** p_s0)
74 vlib_main_t *vm = vlib_get_main ();
76 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
77 next0, thread_index, p_s0, 0);
78 snat_session_t *s0 = *p_s0;
79 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
82 nat44_session_update_counters (s0, now,
83 vlib_buffer_length_in_chain
84 (vm, b0), thread_index);
85 /* Per-user LRU list maintenance */
86 nat44_session_update_lru (sm, s0, thread_index);
91 #ifndef CLIB_MARCH_VARIANT
93 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
95 snat_main_t *sm = &snat_main;
96 nat44_is_idle_session_ctx_t *ctx = arg;
98 u64 sess_timeout_time;
101 ip4_address_t *l_addr, *r_addr;
103 clib_bihash_kv_16_8_t ed_kv;
106 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
109 s = pool_elt_at_index (tsm->sessions, kv->value);
110 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
111 if (ctx->now >= sess_timeout_time)
113 l_addr = &s->in2out.addr;
114 r_addr = &s->ext_host_addr;
115 fib_index = s->in2out.fib_index;
116 if (snat_is_unk_proto_session (s))
118 proto = s->in2out.port;
124 proto = nat_proto_to_ip_proto (s->nat_proto);
125 l_port = s->in2out.port;
126 r_port = s->ext_host_port;
128 if (is_twice_nat_session (s))
130 r_addr = &s->ext_host_nat_addr;
131 r_port = s->ext_host_nat_port;
133 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
134 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
135 nat_elog_warn ("in2out_ed key del failed");
137 if (snat_is_unk_proto_session (s))
140 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
141 s->in2out.addr.as_u32,
142 s->out2in.addr.as_u32,
146 s->in2out.fib_index);
148 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
149 &s->in2out.addr, s->in2out.port,
150 &s->ext_host_nat_addr, s->ext_host_nat_port,
151 &s->out2in.addr, s->out2in.port,
152 &s->ext_host_addr, s->ext_host_port,
153 s->nat_proto, is_twice_nat_session (s));
155 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
156 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
159 if (is_twice_nat_session (s))
161 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
163 // FIXME TODO this is obviously wrong code ... needs fix!
164 // key.protocol = s->nat_proto;
165 // key.port = s->ext_host_nat_port;
166 // a = sm->twice_nat_addresses + i;
167 // if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
169 // snat_free_outside_address_and_port (sm->twice_nat_addresses,
170 // ctx->thread_index,
177 if (snat_is_session_static (s))
180 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
181 &s->out2in.addr, s->out2in.port,
184 nat_ed_session_delete (sm, s, ctx->thread_index, 1);
192 // allocate exact address based on preference
193 static_always_inline int
194 nat_alloc_addr_and_port_exact (snat_address_t * a,
196 nat_protocol_t proto,
197 ip4_address_t * addr,
199 u16 port_per_thread, u32 snat_thread_index)
205 #define _(N, j, n, s) \
206 case NAT_PROTOCOL_##N: \
207 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
211 portnum = (port_per_thread * \
212 snat_thread_index) + \
213 snat_random_port(0, port_per_thread - 1) + 1024; \
214 if (a->busy_##n##_port_refcounts[portnum]) \
216 --a->busy_##n##_port_refcounts[portnum]; \
217 a->busy_##n##_ports_per_thread[thread_index]++; \
218 a->busy_##n##_ports++; \
220 *port = clib_host_to_net_u16(portnum); \
228 nat_elog_info ("unknown protocol");
232 /* Totally out of translations to use... */
233 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
238 static snat_session_t *
239 create_session_for_static_mapping_ed (snat_main_t * sm,
241 ip4_address_t i2o_addr,
244 ip4_address_t o2i_addr,
247 nat_protocol_t nat_proto,
248 vlib_node_runtime_t * node,
251 twice_nat_type_t twice_nat,
252 lb_nat_type_t lb_nat, f64 now,
253 snat_static_mapping_t * mapping)
258 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
259 clib_bihash_kv_16_8_t kv;
260 nat44_is_idle_session_ctx_t ctx;
263 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
265 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
266 nat_elog_notice ("maximum sessions exceeded");
270 s = nat_ed_session_alloc (sm, thread_index, now, nat_proto);
273 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
274 nat_elog_warn ("create NAT session failed");
278 ip = vlib_buffer_get_current (b);
279 udp = ip4_next_header (ip);
281 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
282 s->ext_host_port = nat_proto == NAT_PROTOCOL_ICMP ? 0 : udp->src_port;
283 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
285 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
286 if (lb_nat == AFFINITY_LB_NAT)
287 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
288 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
289 s->out2in.addr = o2i_addr;
290 s->out2in.port = o2i_port;
291 s->out2in.fib_index = o2i_fib_index;
292 s->in2out.addr = i2o_addr;
293 s->in2out.port = i2o_port;
294 s->in2out.fib_index = i2o_fib_index;
295 s->nat_proto = nat_proto;
297 /* Add to lookup tables */
298 init_ed_kv (&kv, o2i_addr, o2i_port, s->ext_host_addr, s->ext_host_port,
299 o2i_fib_index, ip->protocol, thread_index, s - tsm->sessions);
301 ctx.thread_index = thread_index;
302 if (clib_bihash_add_or_overwrite_stale_16_8 (&sm->out2in_ed, &kv,
303 nat44_o2i_ed_is_idle_session_cb,
305 nat_elog_notice ("out2in-ed key add failed");
307 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
308 ip->src_address.as_u32 == i2o_addr.as_u32))
311 snat_address_t *filter = 0;
313 // if exact address is specified use this address
314 if (is_exact_address (mapping))
317 vec_foreach (ap, sm->twice_nat_addresses)
319 if (mapping->pool_addr.as_u32 == ap->addr.as_u32)
329 rc = nat_alloc_addr_and_port_exact (filter,
332 &s->ext_host_nat_addr,
333 &s->ext_host_nat_port,
335 tsm->snat_thread_index);
336 s->flags |= SNAT_SESSION_FLAG_EXACT_ADDRESS;
341 snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
342 thread_index, nat_proto,
343 &s->ext_host_nat_addr,
344 &s->ext_host_nat_port,
346 tsm->snat_thread_index);
351 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
352 nat_ed_session_delete (sm, s, thread_index, 1);
353 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 0))
354 nat_elog_notice ("out2in-ed key del failed");
358 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
359 init_ed_kv (&kv, i2o_addr, i2o_port, s->ext_host_nat_addr,
360 s->ext_host_nat_port, i2o_fib_index, ip->protocol,
361 thread_index, s - tsm->sessions);
365 init_ed_kv (&kv, i2o_addr, i2o_port, s->ext_host_addr,
366 s->ext_host_port, i2o_fib_index, ip->protocol,
367 thread_index, s - tsm->sessions);
369 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
370 nat44_i2o_ed_is_idle_session_cb,
372 nat_elog_notice ("in2out-ed key add failed");
374 snat_ipfix_logging_nat44_ses_create (thread_index,
375 s->in2out.addr.as_u32,
376 s->out2in.addr.as_u32,
379 s->out2in.port, s->in2out.fib_index);
381 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
382 &s->in2out.addr, s->in2out.port,
383 &s->ext_host_nat_addr, s->ext_host_nat_port,
384 &s->out2in.addr, s->out2in.port,
385 &s->ext_host_addr, s->ext_host_port,
386 s->nat_proto, is_twice_nat_session (s));
388 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
389 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
390 &s->ext_host_nat_addr, s->ext_host_nat_port,
391 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
393 per_vrf_sessions_register_session (s, thread_index);
399 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
400 u16 dst_port, u32 thread_index, u32 rx_fib_index)
402 clib_bihash_kv_16_8_t kv, value;
403 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
405 init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
406 rx_fib_index, ip->protocol);
407 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
414 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
415 u32 rx_fib_index, u32 thread_index)
417 clib_bihash_kv_16_8_t kv, value;
419 snat_session_t *s = 0;
420 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
421 vlib_main_t *vm = vlib_get_main ();
422 f64 now = vlib_time_now (vm);
425 if (ip->protocol == IP_PROTOCOL_ICMP)
427 if (get_icmp_o2i_ed_key
428 (b, ip, rx_fib_index, ~0, ~0, 0, &l_port, &r_port, &kv))
433 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
435 udp = ip4_next_header (ip);
436 l_port = udp->dst_port;
437 r_port = udp->src_port;
444 init_ed_k (&kv, ip->dst_address, l_port, ip->src_address, r_port,
445 rx_fib_index, ip->protocol);
448 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
450 ASSERT (thread_index == ed_value_get_thread_index (&value));
452 pool_elt_at_index (tsm->sessions,
453 ed_value_get_session_index (&value));
460 (nat44_ed_maximum_sessions_exceeded
461 (sm, rx_fib_index, thread_index)))
464 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
467 nat_elog_warn ("create NAT session failed");
471 proto = ip_proto_to_nat_proto (ip->protocol);
473 s->ext_host_addr = ip->src_address;
474 s->ext_host_port = r_port;
475 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
476 s->out2in.addr = ip->dst_address;
477 s->out2in.port = l_port;
478 s->nat_proto = proto;
479 if (proto == NAT_PROTOCOL_OTHER)
481 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
482 s->out2in.port = ip->protocol;
484 s->out2in.fib_index = 0;
485 s->in2out.addr = s->out2in.addr;
486 s->in2out.port = s->out2in.port;
487 s->in2out.fib_index = s->out2in.fib_index;
489 kv.value = s - tsm->sessions;
490 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
491 nat_elog_notice ("in2out_ed key add failed");
493 per_vrf_sessions_register_session (s, thread_index);
496 if (ip->protocol == IP_PROTOCOL_TCP)
498 tcp_header_t *tcp = ip4_next_header (ip);
499 if (nat44_set_tcp_session_state_o2i
500 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
506 nat44_session_update_counters (s, now, 0, thread_index);
507 /* Per-user LRU list maintenance */
508 nat44_session_update_lru (sm, s, thread_index);
512 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
513 ip4_header_t * ip, u32 rx_fib_index)
515 ip4_header_t ip_wkr = {
516 .src_address = ip->dst_address,
518 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
520 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
523 #ifndef CLIB_MARCH_VARIANT
525 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
526 u32 thread_index, vlib_buffer_t * b,
527 ip4_header_t * ip, ip4_address_t * addr,
528 u16 * port, u32 * fib_index, nat_protocol_t * proto,
529 void *d, void *e, u8 * dont_translate)
531 u32 next = ~0, sw_if_index, rx_fib_index;
532 clib_bihash_kv_16_8_t kv, value;
533 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
534 snat_session_t *s = 0;
535 u8 is_addr_only, identity_nat;
537 vlib_main_t *vm = vlib_get_main ();
538 ip4_address_t sm_addr;
542 snat_static_mapping_t *m;
544 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
545 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
547 if (get_icmp_o2i_ed_key
548 (b, ip, rx_fib_index, ~0, ~0, proto, &l_port, &r_port, &kv))
550 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
551 next = NAT_NEXT_DROP;
555 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
557 if (snat_static_mapping_match
558 (sm, ip->dst_address, l_port, rx_fib_index,
559 ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
560 &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat, &m))
562 // static mapping not matched
563 if (!sm->forwarding_enabled)
565 /* Don't NAT packet aimed at the intfc address */
566 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
567 ip->dst_address.as_u32)))
573 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
574 next = NAT_NEXT_DROP;
580 if (next_src_nat (sm, ip, l_port, r_port,
581 thread_index, rx_fib_index))
583 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
587 if (sm->num_workers > 1)
588 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
590 create_bypass_for_fwd (sm, b, ip, rx_fib_index,
598 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
600 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
601 ICMP4_echo_request || !is_addr_only)))
603 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
604 next = NAT_NEXT_DROP;
608 if (PREDICT_FALSE (identity_nat))
614 /* Create session initiated by host from external network */
616 create_session_for_static_mapping_ed (sm, b, sm_addr, sm_port,
617 sm_fib_index, ip->dst_address,
618 l_port, rx_fib_index, *proto,
621 vlib_time_now (vm), m);
623 next = NAT_NEXT_DROP;
628 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
630 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
632 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
633 reass.icmp_type_or_tcp_flags)))
635 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
636 next = NAT_NEXT_DROP;
640 ASSERT (thread_index == ed_value_get_thread_index (&value));
642 pool_elt_at_index (tsm->sessions,
643 ed_value_get_session_index (&value));
648 *addr = s->in2out.addr;
649 *port = s->in2out.port;
650 *fib_index = s->in2out.fib_index;
653 *(snat_session_t **) d = s;
658 static snat_session_t *
659 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
665 vlib_main_t * vm, vlib_node_runtime_t * node)
667 clib_bihash_kv_8_8_t kv, value;
668 clib_bihash_kv_16_8_t s_kv, s_value;
669 snat_static_mapping_t *m;
670 u32 old_addr, new_addr;
673 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
675 old_addr = ip->dst_address.as_u32;
677 init_ed_k (&s_kv, ip->dst_address, 0, ip->src_address, 0, rx_fib_index,
680 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
682 ASSERT (thread_index == ed_value_get_thread_index (&s_value));
684 pool_elt_at_index (tsm->sessions,
685 ed_value_get_session_index (&s_value));
686 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
691 (nat44_ed_maximum_sessions_exceeded
692 (sm, rx_fib_index, thread_index)))
694 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
695 nat_elog_notice ("maximum sessions exceeded");
699 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
700 if (clib_bihash_search_8_8
701 (&sm->static_mapping_by_external, &kv, &value))
703 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
707 m = pool_elt_at_index (sm->static_mappings, value.value);
709 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
711 /* Create a new session */
712 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
715 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
716 nat_elog_warn ("create NAT session failed");
720 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
721 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
722 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
723 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
724 s->out2in.addr.as_u32 = old_addr;
725 s->out2in.fib_index = rx_fib_index;
726 s->in2out.addr.as_u32 = new_addr;
727 s->in2out.fib_index = m->fib_index;
728 s->in2out.port = s->out2in.port = ip->protocol;
730 /* Add to lookup tables */
731 s_kv.value = s - tsm->sessions;
732 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
733 nat_elog_notice ("out2in key add failed");
735 init_ed_kv (&s_kv, ip->dst_address, 0, ip->src_address, 0, m->fib_index,
736 ip->protocol, thread_index, s - tsm->sessions);
737 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
738 nat_elog_notice ("in2out key add failed");
740 per_vrf_sessions_register_session (s, thread_index);
743 /* Update IP checksum */
745 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
746 ip->checksum = ip_csum_fold (sum);
748 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
751 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
753 /* Per-user LRU list maintenance */
754 nat44_session_update_lru (sm, s, thread_index);
760 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
761 vlib_node_runtime_t * node,
762 vlib_frame_t * frame,
765 u32 n_left_from, *from;
766 snat_main_t *sm = &snat_main;
767 f64 now = vlib_time_now (vm);
768 u32 thread_index = vm->thread_index;
769 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
771 from = vlib_frame_vector_args (frame);
772 n_left_from = frame->n_vectors;
774 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
775 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
776 vlib_get_buffers (vm, from, b, n_left_from);
778 while (n_left_from > 0)
781 u32 sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
782 u16 old_port0, new_port0;
786 snat_session_t *s0 = 0;
787 clib_bihash_kv_16_8_t kv0, value0;
793 /* Prefetch next iteration. */
794 if (PREDICT_TRUE (n_left_from >= 2))
800 vlib_prefetch_buffer_header (p2, LOAD);
802 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
805 next[0] = vnet_buffer2 (b0)->nat.arc_next;
807 vnet_buffer (b0)->snat.flags = 0;
808 ip0 = vlib_buffer_get_current (b0);
810 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
812 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
814 if (PREDICT_FALSE (ip0->ttl == 1))
816 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
817 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
818 ICMP4_time_exceeded_ttl_exceeded_in_transit,
820 next[0] = NAT_NEXT_ICMP_ERROR;
824 udp0 = ip4_next_header (ip0);
825 tcp0 = (tcp_header_t *) udp0;
826 proto0 = ip_proto_to_nat_proto (ip0->protocol);
828 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
830 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
834 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
836 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
840 init_ed_k (&kv0, ip0->dst_address,
841 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
842 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
845 /* there is a stashed index in vnet_buffer2 from handoff node,
846 * see if we can use it */
849 PREDICT_TRUE (!pool_is_free_index
851 vnet_buffer2 (b0)->nat.ed_out2in_nat_session_index)))
853 s0 = pool_elt_at_index (tsm->sessions,
855 nat.ed_out2in_nat_session_index);
857 (s0->out2in.addr.as_u32 == ip0->dst_address.as_u32
858 && s0->out2in.port == vnet_buffer (b0)->ip.reass.l4_dst_port
859 && s0->nat_proto == ip_proto_to_nat_proto (ip0->protocol)
860 && s0->out2in.fib_index == rx_fib_index0
861 && s0->ext_host_addr.as_u32 == ip0->src_address.as_u32
862 && s0->ext_host_port ==
863 vnet_buffer (b0)->ip.reass.l4_src_port))
865 /* yes, this is the droid we're looking for */
870 // lookup for session
871 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
873 // session does not exist go slow path
874 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
877 ASSERT (thread_index == ed_value_get_thread_index (&value0));
879 pool_elt_at_index (tsm->sessions,
880 ed_value_get_session_index (&value0));
884 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
886 // session is closed, go slow path
887 nat_free_session_data (sm, s0, thread_index, 0);
888 nat_ed_session_delete (sm, s0, thread_index, 1);
889 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
893 if (s0->tcp_closed_timestamp)
895 if (now >= s0->tcp_closed_timestamp)
897 // session is closed, go slow path, freed in slow path
898 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
902 // session in transitory timeout, drop
903 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
904 next[0] = NAT_NEXT_DROP;
909 // drop if session expired
910 u64 sess_timeout_time;
912 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
913 if (now >= sess_timeout_time)
915 // session is closed, go slow path
916 nat_free_session_data (sm, s0, thread_index, 0);
917 nat_ed_session_delete (sm, s0, thread_index, 1);
918 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
922 old_addr0 = ip0->dst_address.as_u32;
923 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
924 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
926 sum0 = ip0->checksum;
927 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
929 if (PREDICT_FALSE (is_twice_nat_session (s0)))
930 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
931 s0->ext_host_nat_addr.as_u32, ip4_header_t,
933 ip0->checksum = ip_csum_fold (sum0);
935 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
937 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
939 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
941 new_port0 = udp0->dst_port = s0->in2out.port;
942 sum0 = tcp0->checksum;
944 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
947 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
949 if (is_twice_nat_session (s0))
951 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
952 s0->ext_host_nat_addr.as_u32,
953 ip4_header_t, dst_address);
955 ip_csum_update (sum0,
956 vnet_buffer (b0)->ip.reass.l4_src_port,
957 s0->ext_host_nat_port, ip4_header_t,
959 tcp0->src_port = s0->ext_host_nat_port;
960 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
962 tcp0->checksum = ip_csum_fold (sum0);
964 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.tcp,
965 thread_index, sw_if_index0, 1);
966 if (nat44_set_tcp_session_state_o2i
967 (sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
968 vnet_buffer (b0)->ip.reass.tcp_ack_number,
969 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
972 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
975 new_port0 = udp0->dst_port = s0->in2out.port;
976 sum0 = udp0->checksum;
978 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
981 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length);
982 if (PREDICT_FALSE (is_twice_nat_session (s0)))
985 ip_csum_update (sum0, ip0->src_address.as_u32,
986 s0->ext_host_nat_addr.as_u32, ip4_header_t,
989 ip_csum_update (sum0, vnet_buffer (b0)->ip.reass.l4_src_port,
990 s0->ext_host_nat_port, ip4_header_t, length);
991 udp0->src_port = s0->ext_host_nat_port;
992 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
994 udp0->checksum = ip_csum_fold (sum0);
995 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.udp,
996 thread_index, sw_if_index0, 1);
1000 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1002 new_port0 = udp0->dst_port = s0->in2out.port;
1003 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1005 udp0->src_port = s0->ext_host_nat_port;
1006 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1009 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.udp,
1010 thread_index, sw_if_index0, 1);
1014 nat44_session_update_counters (s0, now,
1015 vlib_buffer_length_in_chain (vm, b0),
1017 /* Per-user LRU list maintenance */
1018 nat44_session_update_lru (sm, s0, thread_index);
1021 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1022 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1024 nat44_ed_out2in_trace_t *t =
1025 vlib_add_trace (vm, node, b0, sizeof (*t));
1026 t->sw_if_index = sw_if_index0;
1027 t->next_index = next[0];
1028 t->is_slow_path = 0;
1031 t->session_index = s0 - tsm->sessions;
1033 t->session_index = ~0;
1036 if (next[0] == NAT_NEXT_DROP)
1038 vlib_increment_simple_counter (&sm->counters.fastpath.
1039 out2in_ed.drops, thread_index,
1047 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1049 return frame->n_vectors;
1053 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
1054 vlib_node_runtime_t * node,
1055 vlib_frame_t * frame)
1057 u32 n_left_from, *from;
1058 snat_main_t *sm = &snat_main;
1059 f64 now = vlib_time_now (vm);
1060 u32 thread_index = vm->thread_index;
1061 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1062 snat_static_mapping_t *m;
1064 from = vlib_frame_vector_args (frame);
1065 n_left_from = frame->n_vectors;
1067 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1068 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1069 vlib_get_buffers (vm, from, b, n_left_from);
1071 while (n_left_from > 0)
1074 u32 sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
1075 u16 old_port0, new_port0;
1079 icmp46_header_t *icmp0;
1080 snat_session_t *s0 = 0;
1081 clib_bihash_kv_16_8_t kv0, value0;
1083 lb_nat_type_t lb_nat0;
1084 twice_nat_type_t twice_nat0;
1086 ip4_address_t sm_addr;
1091 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1093 vnet_buffer (b0)->snat.flags = 0;
1094 ip0 = vlib_buffer_get_current (b0);
1096 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1098 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1100 if (PREDICT_FALSE (ip0->ttl == 1))
1102 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1103 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1104 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1106 next[0] = NAT_NEXT_ICMP_ERROR;
1110 udp0 = ip4_next_header (ip0);
1111 tcp0 = (tcp_header_t *) udp0;
1112 icmp0 = (icmp46_header_t *) udp0;
1113 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1115 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1118 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1119 thread_index, now, vm, node);
1120 if (!sm->forwarding_enabled)
1123 next[0] = NAT_NEXT_DROP;
1125 vlib_increment_simple_counter (&sm->counters.slowpath.
1126 out2in_ed.other, thread_index,
1131 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1133 next[0] = icmp_out2in_ed_slow_path
1134 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1135 next[0], now, thread_index, &s0);
1136 vlib_increment_simple_counter (&sm->counters.slowpath.
1137 out2in_ed.icmp, thread_index,
1142 init_ed_k (&kv0, ip0->dst_address,
1143 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
1144 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1148 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
1150 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1152 pool_elt_at_index (tsm->sessions,
1153 ed_value_get_session_index (&value0));
1155 if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1157 nat_free_session_data (sm, s0, thread_index, 0);
1158 nat_ed_session_delete (sm, s0, thread_index, 1);
1165 /* Try to match static mapping by external address and port,
1166 destination address and port in packet */
1168 if (snat_static_mapping_match
1169 (sm, ip0->dst_address,
1170 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1171 proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1172 &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0, &m))
1175 * Send DHCP packets to the ipv4 stack, or we won't
1176 * be able to use dhcp client on the outside interface
1178 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1179 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1180 clib_host_to_net_u16
1181 (UDP_DST_PORT_dhcp_to_client))))
1186 if (!sm->forwarding_enabled)
1189 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1190 next[0] = NAT_NEXT_DROP;
1195 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1196 vnet_buffer (b0)->ip.reass.l4_dst_port,
1197 thread_index, rx_fib_index0))
1199 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1203 if (sm->num_workers > 1)
1204 create_bypass_for_fwd_worker (sm, b0, ip0,
1207 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1214 if (PREDICT_FALSE (identity_nat0))
1217 if ((proto0 == NAT_PROTOCOL_TCP)
1218 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1219 reass.icmp_type_or_tcp_flags))
1221 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1222 next[0] = NAT_NEXT_DROP;
1226 /* Create session initiated by host from external network */
1227 s0 = create_session_for_static_mapping_ed (sm, b0,
1232 ip.reass.l4_dst_port,
1233 rx_fib_index0, proto0,
1234 node, rx_fib_index0,
1235 thread_index, twice_nat0,
1239 next[0] = NAT_NEXT_DROP;
1244 old_addr0 = ip0->dst_address.as_u32;
1245 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1246 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1248 sum0 = ip0->checksum;
1249 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1251 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1252 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1253 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1255 ip0->checksum = ip_csum_fold (sum0);
1257 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1259 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1261 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1263 new_port0 = udp0->dst_port = s0->in2out.port;
1264 sum0 = tcp0->checksum;
1266 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1269 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1271 if (is_twice_nat_session (s0))
1273 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1274 s0->ext_host_nat_addr.as_u32,
1275 ip4_header_t, dst_address);
1277 ip_csum_update (sum0,
1278 vnet_buffer (b0)->ip.reass.l4_src_port,
1279 s0->ext_host_nat_port, ip4_header_t,
1281 tcp0->src_port = s0->ext_host_nat_port;
1282 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1284 tcp0->checksum = ip_csum_fold (sum0);
1286 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.tcp,
1287 thread_index, sw_if_index0, 1);
1288 if (nat44_set_tcp_session_state_o2i
1289 (sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1290 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1291 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1294 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1297 new_port0 = udp0->dst_port = s0->in2out.port;
1298 sum0 = udp0->checksum;
1299 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1301 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1303 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1305 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1306 s0->ext_host_nat_addr.as_u32,
1307 ip4_header_t, dst_address);
1309 ip_csum_update (sum0,
1310 vnet_buffer (b0)->ip.reass.l4_src_port,
1311 s0->ext_host_nat_port, ip4_header_t, length);
1312 udp0->src_port = s0->ext_host_nat_port;
1313 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1315 udp0->checksum = ip_csum_fold (sum0);
1316 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.udp,
1317 thread_index, sw_if_index0, 1);
1321 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1323 new_port0 = udp0->dst_port = s0->in2out.port;
1324 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1326 udp0->src_port = s0->ext_host_nat_port;
1327 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1330 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.udp,
1331 thread_index, sw_if_index0, 1);
1335 nat44_session_update_counters (s0, now,
1336 vlib_buffer_length_in_chain (vm, b0),
1338 /* Per-user LRU list maintenance */
1339 nat44_session_update_lru (sm, s0, thread_index);
1342 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1343 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1345 nat44_ed_out2in_trace_t *t =
1346 vlib_add_trace (vm, node, b0, sizeof (*t));
1347 t->sw_if_index = sw_if_index0;
1348 t->next_index = next[0];
1349 t->is_slow_path = 1;
1352 t->session_index = s0 - tsm->sessions;
1354 t->session_index = ~0;
1357 if (next[0] == NAT_NEXT_DROP)
1359 vlib_increment_simple_counter (&sm->counters.slowpath.
1360 out2in_ed.drops, thread_index,
1369 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1372 return frame->n_vectors;
1375 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1376 vlib_node_runtime_t * node,
1377 vlib_frame_t * frame)
1379 if (snat_main.num_workers > 1)
1381 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1385 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1390 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1391 .name = "nat44-ed-out2in",
1392 .vector_size = sizeof (u32),
1393 .sibling_of = "nat-default",
1394 .format_trace = format_nat44_ed_out2in_trace,
1395 .type = VLIB_NODE_TYPE_INTERNAL,
1396 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1397 .error_strings = nat_out2in_ed_error_strings,
1398 .runtime_data_bytes = sizeof (snat_runtime_t),
1402 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1403 vlib_node_runtime_t * node,
1404 vlib_frame_t * frame)
1406 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1410 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1411 .name = "nat44-ed-out2in-slowpath",
1412 .vector_size = sizeof (u32),
1413 .sibling_of = "nat-default",
1414 .format_trace = format_nat44_ed_out2in_trace,
1415 .type = VLIB_NODE_TYPE_INTERNAL,
1416 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1417 .error_strings = nat_out2in_ed_error_strings,
1418 .runtime_data_bytes = sizeof (snat_runtime_t),
1423 format_nat_pre_trace (u8 * s, va_list * args)
1425 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1426 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1427 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1428 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1432 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1433 vlib_node_runtime_t * node,
1434 vlib_frame_t * frame)
1436 return nat_pre_node_fn_inline (vm, node, frame,
1437 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1441 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1442 .name = "nat-pre-out2in",
1443 .vector_size = sizeof (u32),
1444 .sibling_of = "nat-default",
1445 .format_trace = format_nat_pre_trace,
1446 .type = VLIB_NODE_TYPE_INTERNAL,
1452 * fd.io coding-style-patch-verification: ON
1455 * eval: (c-set-style "gnu")