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/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.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 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75 next0, thread_index, p_s0, 0);
76 snat_session_t *s0 = *p_s0;
77 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
80 nat44_session_update_counters (s0, now,
81 vlib_buffer_length_in_chain
82 (sm->vlib_main, b0), thread_index);
83 /* Per-user LRU list maintenance */
84 nat44_session_update_lru (sm, s0, thread_index);
89 #ifndef CLIB_MARCH_VARIANT
91 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
93 snat_main_t *sm = &snat_main;
94 nat44_is_idle_session_ctx_t *ctx = arg;
96 u64 sess_timeout_time;
97 nat_ed_ses_key_t ed_key;
98 clib_bihash_kv_16_8_t ed_kv;
101 snat_session_key_t key;
102 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
105 s = pool_elt_at_index (tsm->sessions, kv->value);
106 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
107 if (ctx->now >= sess_timeout_time)
109 ed_key.l_addr = s->in2out.addr;
110 ed_key.r_addr = s->ext_host_addr;
111 ed_key.fib_index = s->in2out.fib_index;
112 if (snat_is_unk_proto_session (s))
114 ed_key.proto = s->in2out.port;
120 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
121 ed_key.l_port = s->in2out.port;
122 ed_key.r_port = s->ext_host_port;
124 if (is_twice_nat_session (s))
126 ed_key.r_addr = s->ext_host_nat_addr;
127 ed_key.r_port = s->ext_host_nat_port;
129 ed_kv.key[0] = ed_key.as_u64[0];
130 ed_kv.key[1] = ed_key.as_u64[1];
131 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
132 nat_elog_warn ("in2out_ed key del failed");
134 ed_bihash_kv_t bihash_key;
135 clib_memset (&bihash_key, 0, sizeof (bihash_key));
136 bihash_key.k.dst_address = s->ext_host_addr.as_u32;
137 bihash_key.k.dst_port = s->ext_host_port;
138 bihash_key.k.src_address = s->out2in.addr.as_u32;
139 bihash_key.k.src_port = s->out2in.port;
140 bihash_key.k.protocol = s->out2in.protocol;
141 clib_bihash_add_del_16_8 (&sm->ed_ext_ports, &bihash_key.kv,
144 if (snat_is_unk_proto_session (s))
147 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
148 s->in2out.addr.as_u32,
149 s->out2in.addr.as_u32,
153 s->in2out.fib_index);
155 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
156 &s->in2out.addr, s->in2out.port,
157 &s->ext_host_nat_addr, s->ext_host_nat_port,
158 &s->out2in.addr, s->out2in.port,
159 &s->ext_host_addr, s->ext_host_port,
160 s->in2out.protocol, is_twice_nat_session (s));
162 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
163 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
166 if (is_twice_nat_session (s))
168 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
170 key.protocol = s->in2out.protocol;
171 key.port = s->ext_host_nat_port;
172 a = sm->twice_nat_addresses + i;
173 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
175 snat_free_outside_address_and_port (sm->twice_nat_addresses,
183 if (snat_is_session_static (s))
186 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
189 nat44_delete_session (sm, s, ctx->thread_index);
197 static snat_session_t *
198 create_session_for_static_mapping_ed (snat_main_t * sm,
200 snat_session_key_t l_key,
201 snat_session_key_t e_key,
202 vlib_node_runtime_t * node,
204 twice_nat_type_t twice_nat,
205 lb_nat_type_t lb_nat, f64 now)
211 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
212 clib_bihash_kv_16_8_t kv;
213 snat_session_key_t eh_key;
214 nat44_is_idle_session_ctx_t ctx;
216 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
218 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
219 nat_elog_notice ("maximum sessions exceeded");
223 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
226 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
227 nat_elog_warn ("create NAT user failed");
231 s = nat_ed_session_alloc (sm, u, thread_index, now);
234 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
235 nat44_delete_user_with_no_session (sm, u, thread_index);
236 nat_elog_warn ("create NAT session failed");
240 ip = vlib_buffer_get_current (b);
241 udp = ip4_next_header (ip);
243 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
244 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
245 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
247 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
248 if (lb_nat == AFFINITY_LB_NAT)
249 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
250 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
253 s->in2out.protocol = s->out2in.protocol;
254 user_session_increment (sm, u, 1);
256 /* Add to lookup tables */
257 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
258 e_key.fib_index, e_key.port, s->ext_host_port);
259 kv.value = s - tsm->sessions;
261 ctx.thread_index = thread_index;
262 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
263 nat44_o2i_ed_is_idle_session_cb,
265 nat_elog_notice ("out2in-ed key add failed");
267 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
268 ip->src_address.as_u32 == l_key.addr.as_u32))
270 eh_key.protocol = e_key.protocol;
271 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
272 thread_index, &eh_key,
274 tsm->snat_thread_index))
276 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
277 nat44_delete_session (sm, s, thread_index);
278 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
279 nat_elog_notice ("out2in-ed key del failed");
282 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
283 s->ext_host_nat_port = eh_key.port;
284 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
285 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
286 l_key.fib_index, l_key.port, s->ext_host_nat_port);
290 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
291 l_key.fib_index, l_key.port, s->ext_host_port);
293 kv.value = s - tsm->sessions;
294 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
295 nat44_i2o_ed_is_idle_session_cb,
297 nat_elog_notice ("in2out-ed key add failed");
299 snat_ipfix_logging_nat44_ses_create (thread_index,
300 s->in2out.addr.as_u32,
301 s->out2in.addr.as_u32,
304 s->out2in.port, s->in2out.fib_index);
306 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
307 &s->in2out.addr, s->in2out.port,
308 &s->ext_host_nat_addr, s->ext_host_nat_port,
309 &s->out2in.addr, s->out2in.port,
310 &s->ext_host_addr, s->ext_host_port,
311 s->in2out.protocol, is_twice_nat_session (s));
313 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
314 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
315 &s->ext_host_nat_addr, s->ext_host_nat_port,
316 s->in2out.protocol, s->in2out.fib_index, s->flags,
323 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
324 u16 dst_port, u32 thread_index, u32 rx_fib_index)
326 clib_bihash_kv_16_8_t kv, value;
327 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
329 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
330 rx_fib_index, src_port, dst_port);
331 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
338 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
339 u32 rx_fib_index, u32 thread_index)
341 nat_ed_ses_key_t key;
342 clib_bihash_kv_16_8_t kv, value;
345 snat_session_t *s = 0;
346 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
347 f64 now = vlib_time_now (sm->vlib_main);
349 if (ip->protocol == IP_PROTOCOL_ICMP)
351 if (get_icmp_o2i_ed_key (b, ip, &key))
354 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
356 udp = ip4_next_header (ip);
357 key.r_addr = ip->src_address;
358 key.l_addr = ip->dst_address;
359 key.proto = ip->protocol;
360 key.l_port = udp->dst_port;
361 key.r_port = udp->src_port;
365 key.r_addr = ip->src_address;
366 key.l_addr = ip->dst_address;
367 key.proto = ip->protocol;
368 key.l_port = key.r_port = 0;
371 kv.key[0] = key.as_u64[0];
372 kv.key[1] = key.as_u64[1];
374 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
376 s = pool_elt_at_index (tsm->sessions, value.value);
382 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
385 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
389 nat_elog_warn ("create NAT user failed");
393 s = nat_ed_session_alloc (sm, u, thread_index, now);
396 nat44_delete_user_with_no_session (sm, u, thread_index);
397 nat_elog_warn ("create NAT session failed");
401 proto = ip_proto_to_snat_proto (key.proto);
403 s->ext_host_addr = key.r_addr;
404 s->ext_host_port = key.r_port;
405 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
406 s->out2in.addr = key.l_addr;
407 s->out2in.port = key.l_port;
408 s->out2in.protocol = proto;
411 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
412 s->out2in.port = ip->protocol;
414 s->out2in.fib_index = 0;
415 s->in2out = s->out2in;
416 user_session_increment (sm, u, 0);
418 kv.value = s - tsm->sessions;
419 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
420 nat_elog_notice ("in2out_ed key add failed");
423 if (ip->protocol == IP_PROTOCOL_TCP)
425 tcp_header_t *tcp = ip4_next_header (ip);
426 if (nat44_set_tcp_session_state_o2i
427 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
433 nat44_session_update_counters (s, now, 0, thread_index);
434 /* Per-user LRU list maintenance */
435 nat44_session_update_lru (sm, s, thread_index);
439 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
440 ip4_header_t * ip, u32 rx_fib_index)
442 ip4_header_t ip_wkr = {
443 .src_address = ip->dst_address,
445 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
447 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
450 #ifndef CLIB_MARCH_VARIANT
452 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
453 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
454 u8 * p_proto, snat_session_key_t * p_value,
455 u8 * p_dont_translate, void *d, void *e)
457 u32 next = ~0, sw_if_index, rx_fib_index;
458 nat_ed_ses_key_t key;
459 clib_bihash_kv_16_8_t kv, value;
460 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
461 snat_session_t *s = 0;
462 u8 dont_translate = 0, is_addr_only, identity_nat;
463 snat_session_key_t e_key, l_key;
465 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
466 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
468 if (get_icmp_o2i_ed_key (b, ip, &key))
470 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
471 next = NAT_NEXT_DROP;
474 key.fib_index = rx_fib_index;
475 kv.key[0] = key.as_u64[0];
476 kv.key[1] = key.as_u64[1];
478 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
480 /* Try to match static mapping */
481 e_key.addr = ip->dst_address;
482 e_key.port = key.l_port;
483 e_key.protocol = ip_proto_to_snat_proto (key.proto);
484 e_key.fib_index = rx_fib_index;
485 if (snat_static_mapping_match
486 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
488 if (!sm->forwarding_enabled)
490 /* Don't NAT packet aimed at the intfc address */
491 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
492 ip->dst_address.as_u32)))
497 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
498 next = NAT_NEXT_DROP;
504 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
505 thread_index, rx_fib_index))
507 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
510 if (sm->num_workers > 1)
511 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
513 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
519 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
521 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
522 ICMP4_echo_request || !is_addr_only)))
524 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
525 next = NAT_NEXT_DROP;
529 if (PREDICT_FALSE (identity_nat))
535 /* Create session initiated by host from external network */
536 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
543 next = NAT_NEXT_DROP;
550 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
552 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
554 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
555 reass.icmp_type_or_tcp_flags)))
557 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
558 next = NAT_NEXT_DROP;
562 s = pool_elt_at_index (tsm->sessions, value.value);
565 *p_proto = ip_proto_to_snat_proto (key.proto);
568 *p_value = s->in2out;
569 *p_dont_translate = dont_translate;
571 *(snat_session_t **) d = s;
576 static snat_session_t *
577 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
583 vlib_main_t * vm, vlib_node_runtime_t * node)
585 clib_bihash_kv_8_8_t kv, value;
586 clib_bihash_kv_16_8_t s_kv, s_value;
587 snat_static_mapping_t *m;
588 u32 old_addr, new_addr;
591 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
594 old_addr = ip->dst_address.as_u32;
596 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
599 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
601 s = pool_elt_at_index (tsm->sessions, s_value.value);
602 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
606 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
608 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
609 nat_elog_notice ("maximum sessions exceeded");
613 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
614 if (clib_bihash_search_8_8
615 (&sm->static_mapping_by_external, &kv, &value))
617 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
621 m = pool_elt_at_index (sm->static_mappings, value.value);
623 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
625 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
629 b->error = node->errors[NAT_OUT2IN_ED_ERROR_CANNOT_CREATE_USER];
630 nat_elog_warn ("create NAT user failed");
634 /* Create a new session */
635 s = nat_ed_session_alloc (sm, u, thread_index, now);
638 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
639 nat44_delete_user_with_no_session (sm, u, thread_index);
640 nat_elog_warn ("create NAT session failed");
644 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
645 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
646 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
647 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
648 s->out2in.addr.as_u32 = old_addr;
649 s->out2in.fib_index = rx_fib_index;
650 s->in2out.addr.as_u32 = new_addr;
651 s->in2out.fib_index = m->fib_index;
652 s->in2out.port = s->out2in.port = ip->protocol;
653 user_session_increment (sm, u, 1);
655 /* Add to lookup tables */
656 s_kv.value = s - tsm->sessions;
657 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
658 nat_elog_notice ("out2in key add failed");
660 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
662 s_kv.value = s - tsm->sessions;
663 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
664 nat_elog_notice ("in2out key add failed");
667 /* Update IP checksum */
669 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
670 ip->checksum = ip_csum_fold (sum);
672 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
675 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
677 /* Per-user LRU list maintenance */
678 nat44_session_update_lru (sm, s, thread_index);
684 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
685 vlib_node_runtime_t * node,
686 vlib_frame_t * frame)
688 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
689 nat_next_t next_index;
690 snat_main_t *sm = &snat_main;
691 f64 now = vlib_time_now (vm);
692 u32 thread_index = vm->thread_index;
693 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
694 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
697 stats_node_index = sm->ed_out2in_node_index;
699 from = vlib_frame_vector_args (frame);
700 n_left_from = frame->n_vectors;
701 next_index = node->cached_next_index;
703 while (n_left_from > 0)
707 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
709 while (n_left_from > 0 && n_left_to_next > 0)
713 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
715 u16 old_port0, new_port0;
719 snat_session_t *s0 = 0;
720 clib_bihash_kv_16_8_t kv0, value0;
723 /* speculatively enqueue b0 to the current next frame */
731 b0 = vlib_get_buffer (vm, bi0);
732 next0 = nat_buffer_opaque (b0)->arc_next;
734 vnet_buffer (b0)->snat.flags = 0;
735 ip0 = vlib_buffer_get_current (b0);
737 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
739 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
742 if (PREDICT_FALSE (ip0->ttl == 1))
744 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
745 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
746 ICMP4_time_exceeded_ttl_exceeded_in_transit,
748 next0 = NAT_NEXT_ICMP_ERROR;
752 udp0 = ip4_next_header (ip0);
753 tcp0 = (tcp_header_t *) udp0;
754 proto0 = ip_proto_to_snat_proto (ip0->protocol);
756 if (PREDICT_FALSE (proto0 == ~0))
758 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
762 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
764 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
768 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
769 ip0->protocol, rx_fib_index0,
770 vnet_buffer (b0)->ip.reass.l4_dst_port,
771 vnet_buffer (b0)->ip.reass.l4_src_port);
773 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
775 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
778 s0 = pool_elt_at_index (tsm->sessions, value0.value);
780 if (s0->tcp_close_timestamp)
782 if (now >= s0->tcp_close_timestamp)
784 // session is closed, go slow path
785 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
789 // session in transitory timeout, drop
790 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
791 next0 = NAT_NEXT_DROP;
796 // drop if session expired
797 u64 sess_timeout_time;
798 sess_timeout_time = s0->last_heard +
799 (f64) nat44_session_get_timeout (sm, s0);
800 if (now >= sess_timeout_time)
802 // session is closed, go slow path
803 nat_free_session_data (sm, s0, thread_index, 0);
804 nat44_delete_session (sm, s0, thread_index);
805 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
810 old_addr0 = ip0->dst_address.as_u32;
811 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
812 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
814 sum0 = ip0->checksum;
815 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
817 if (PREDICT_FALSE (is_twice_nat_session (s0)))
818 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
819 s0->ext_host_nat_addr.as_u32, ip4_header_t,
821 ip0->checksum = ip_csum_fold (sum0);
823 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
825 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
827 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
829 new_port0 = udp0->dst_port = s0->in2out.port;
830 sum0 = tcp0->checksum;
832 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
835 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
837 if (is_twice_nat_session (s0))
839 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
840 s0->ext_host_nat_addr.as_u32,
841 ip4_header_t, dst_address);
843 ip_csum_update (sum0,
844 vnet_buffer (b0)->ip.
846 s0->ext_host_nat_port, ip4_header_t,
848 tcp0->src_port = s0->ext_host_nat_port;
849 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
851 tcp0->checksum = ip_csum_fold (sum0);
854 if (nat44_set_tcp_session_state_o2i
856 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
857 vnet_buffer (b0)->ip.reass.tcp_ack_number,
858 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
861 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
864 new_port0 = udp0->dst_port = s0->in2out.port;
865 sum0 = udp0->checksum;
866 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
868 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
870 if (PREDICT_FALSE (is_twice_nat_session (s0)))
872 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
873 s0->ext_host_nat_addr.as_u32,
874 ip4_header_t, dst_address);
876 ip_csum_update (sum0,
877 vnet_buffer (b0)->ip.reass.l4_src_port,
878 s0->ext_host_nat_port, ip4_header_t,
880 udp0->src_port = s0->ext_host_nat_port;
881 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
883 udp0->checksum = ip_csum_fold (sum0);
888 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
890 new_port0 = udp0->dst_port = s0->in2out.port;
891 if (PREDICT_FALSE (is_twice_nat_session (s0)))
893 udp0->src_port = s0->ext_host_nat_port;
894 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
901 nat44_session_update_counters (s0, now,
902 vlib_buffer_length_in_chain (vm, b0),
904 /* Per-user LRU list maintenance */
905 nat44_session_update_lru (sm, s0, thread_index);
908 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
909 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
911 nat44_ed_out2in_trace_t *t =
912 vlib_add_trace (vm, node, b0, sizeof (*t));
913 t->sw_if_index = sw_if_index0;
914 t->next_index = next0;
918 t->session_index = s0 - tsm->sessions;
920 t->session_index = ~0;
923 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
924 /* verify speculative enqueue, maybe switch current next frame */
925 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
926 to_next, n_left_to_next,
930 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
933 vlib_node_increment_counter (vm, stats_node_index,
934 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
936 vlib_node_increment_counter (vm, stats_node_index,
937 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
938 vlib_node_increment_counter (vm, stats_node_index,
939 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
940 vlib_node_increment_counter (vm, stats_node_index,
941 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
943 vlib_node_increment_counter (vm, stats_node_index,
944 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
946 vlib_node_increment_counter (vm, stats_node_index,
947 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
948 return frame->n_vectors;
952 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
953 vlib_node_runtime_t * node,
954 vlib_frame_t * frame)
956 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
957 nat_next_t next_index;
958 snat_main_t *sm = &snat_main;
959 f64 now = vlib_time_now (vm);
960 u32 thread_index = vm->thread_index;
961 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
962 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
965 stats_node_index = sm->ed_out2in_slowpath_node_index;
967 from = vlib_frame_vector_args (frame);
968 n_left_from = frame->n_vectors;
969 next_index = node->cached_next_index;
971 while (n_left_from > 0)
975 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
977 while (n_left_from > 0 && n_left_to_next > 0)
981 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
983 u16 old_port0, new_port0;
987 icmp46_header_t *icmp0;
988 snat_session_t *s0 = 0;
989 clib_bihash_kv_16_8_t kv0, value0;
991 snat_session_key_t e_key0, l_key0;
992 lb_nat_type_t lb_nat0;
993 twice_nat_type_t twice_nat0;
996 /* speculatively enqueue b0 to the current next frame */
1002 n_left_to_next -= 1;
1004 b0 = vlib_get_buffer (vm, bi0);
1005 next0 = nat_buffer_opaque (b0)->arc_next;
1007 vnet_buffer (b0)->snat.flags = 0;
1008 ip0 = vlib_buffer_get_current (b0);
1010 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1012 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1015 if (PREDICT_FALSE (ip0->ttl == 1))
1017 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1018 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1019 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1021 next0 = NAT_NEXT_ICMP_ERROR;
1025 udp0 = ip4_next_header (ip0);
1026 tcp0 = (tcp_header_t *) udp0;
1027 icmp0 = (icmp46_header_t *) udp0;
1028 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1030 if (PREDICT_FALSE (proto0 == ~0))
1033 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1034 thread_index, now, vm, node);
1035 if (!sm->forwarding_enabled)
1038 next0 = NAT_NEXT_DROP;
1044 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1046 next0 = icmp_out2in_ed_slow_path
1047 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1048 next0, now, thread_index, &s0);
1053 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1054 ip0->protocol, rx_fib_index0,
1055 vnet_buffer (b0)->ip.reass.l4_dst_port,
1056 vnet_buffer (b0)->ip.reass.l4_src_port);
1059 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1061 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1063 if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1065 nat_free_session_data (sm, s0, thread_index, 0);
1066 nat44_delete_session (sm, s0, thread_index);
1073 /* Try to match static mapping by external address and port,
1074 destination address and port in packet */
1075 e_key0.addr = ip0->dst_address;
1076 e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1077 e_key0.protocol = proto0;
1078 e_key0.fib_index = rx_fib_index0;
1080 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1081 &twice_nat0, &lb_nat0,
1086 * Send DHCP packets to the ipv4 stack, or we won't
1087 * be able to use dhcp client on the outside interface
1089 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1090 && (vnet_buffer (b0)->ip.
1091 reass.l4_dst_port ==
1092 clib_host_to_net_u16
1093 (UDP_DST_PORT_dhcp_to_client))))
1098 if (!sm->forwarding_enabled)
1101 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1102 next0 = NAT_NEXT_DROP;
1106 if (next_src_nat (sm, ip0, ip0->protocol,
1107 vnet_buffer (b0)->ip.
1109 vnet_buffer (b0)->ip.
1110 reass.l4_dst_port, thread_index,
1113 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1116 if (sm->num_workers > 1)
1117 create_bypass_for_fwd_worker (sm, b0, ip0,
1120 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1126 if (PREDICT_FALSE (identity_nat0))
1129 if ((proto0 == SNAT_PROTOCOL_TCP)
1130 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1131 reass.icmp_type_or_tcp_flags))
1133 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1134 next0 = NAT_NEXT_DROP;
1138 /* Create session initiated by host from external network */
1139 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1146 next0 = NAT_NEXT_DROP;
1151 old_addr0 = ip0->dst_address.as_u32;
1152 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1153 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1155 sum0 = ip0->checksum;
1156 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1158 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1159 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1160 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1162 ip0->checksum = ip_csum_fold (sum0);
1164 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1166 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1168 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1170 new_port0 = udp0->dst_port = s0->in2out.port;
1171 sum0 = tcp0->checksum;
1173 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1176 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1178 if (is_twice_nat_session (s0))
1180 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1181 s0->ext_host_nat_addr.as_u32,
1182 ip4_header_t, dst_address);
1184 ip_csum_update (sum0,
1185 vnet_buffer (b0)->ip.
1187 s0->ext_host_nat_port, ip4_header_t,
1189 tcp0->src_port = s0->ext_host_nat_port;
1190 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1192 tcp0->checksum = ip_csum_fold (sum0);
1195 if (nat44_set_tcp_session_state_o2i
1197 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1198 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1199 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1202 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1205 new_port0 = udp0->dst_port = s0->in2out.port;
1206 sum0 = udp0->checksum;
1207 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1209 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1211 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1213 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1214 s0->ext_host_nat_addr.as_u32,
1215 ip4_header_t, dst_address);
1217 ip_csum_update (sum0,
1218 vnet_buffer (b0)->ip.reass.l4_src_port,
1219 s0->ext_host_nat_port, ip4_header_t,
1221 udp0->src_port = s0->ext_host_nat_port;
1222 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1224 udp0->checksum = ip_csum_fold (sum0);
1229 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1231 new_port0 = udp0->dst_port = s0->in2out.port;
1232 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1234 udp0->src_port = s0->ext_host_nat_port;
1235 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1242 nat44_session_update_counters (s0, now,
1243 vlib_buffer_length_in_chain (vm, b0),
1245 /* Per-user LRU list maintenance */
1246 nat44_session_update_lru (sm, s0, thread_index);
1249 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1250 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1252 nat44_ed_out2in_trace_t *t =
1253 vlib_add_trace (vm, node, b0, sizeof (*t));
1254 t->sw_if_index = sw_if_index0;
1255 t->next_index = next0;
1256 t->is_slow_path = 1;
1259 t->session_index = s0 - tsm->sessions;
1261 t->session_index = ~0;
1264 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1265 /* verify speculative enqueue, maybe switch current next frame */
1266 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1267 to_next, n_left_to_next,
1271 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1274 vlib_node_increment_counter (vm, stats_node_index,
1275 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1277 vlib_node_increment_counter (vm, stats_node_index,
1278 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1279 vlib_node_increment_counter (vm, stats_node_index,
1280 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1281 vlib_node_increment_counter (vm, stats_node_index,
1282 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1284 vlib_node_increment_counter (vm, stats_node_index,
1285 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1287 vlib_node_increment_counter (vm, stats_node_index,
1288 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1289 return frame->n_vectors;
1292 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1293 vlib_node_runtime_t * node,
1294 vlib_frame_t * frame)
1296 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1300 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1301 .name = "nat44-ed-out2in",
1302 .vector_size = sizeof (u32),
1303 .sibling_of = "nat-default",
1304 .format_trace = format_nat44_ed_out2in_trace,
1305 .type = VLIB_NODE_TYPE_INTERNAL,
1306 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1307 .error_strings = nat_out2in_ed_error_strings,
1308 .runtime_data_bytes = sizeof (snat_runtime_t),
1312 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1313 vlib_node_runtime_t * node,
1314 vlib_frame_t * frame)
1316 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1320 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1321 .name = "nat44-ed-out2in-slowpath",
1322 .vector_size = sizeof (u32),
1323 .sibling_of = "nat-default",
1324 .format_trace = format_nat44_ed_out2in_trace,
1325 .type = VLIB_NODE_TYPE_INTERNAL,
1326 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1327 .error_strings = nat_out2in_ed_error_strings,
1328 .runtime_data_bytes = sizeof (snat_runtime_t),
1333 format_nat_pre_trace (u8 * s, va_list * args)
1335 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1336 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1337 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1338 return format (s, "out2in next_index %d", t->next_index);
1341 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1342 vlib_node_runtime_t * node,
1343 vlib_frame_t * frame)
1345 return nat_pre_node_fn_inline (vm, node, frame,
1346 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1350 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1351 .name = "nat-pre-out2in",
1352 .vector_size = sizeof (u32),
1353 .sibling_of = "nat-default",
1354 .format_trace = format_nat_pre_trace,
1355 .type = VLIB_NODE_TYPE_INTERNAL,
1361 * fd.io coding-style-patch-verification: ON
1364 * eval: (c-set-style "gnu")