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_reass.h>
31 #include <nat/nat_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 if (snat_is_unk_proto_session (s))
137 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
138 s->in2out.addr.as_u32,
139 s->out2in.addr.as_u32,
143 s->in2out.fib_index);
145 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
146 &s->in2out.addr, s->in2out.port,
147 &s->ext_host_nat_addr, s->ext_host_nat_port,
148 &s->out2in.addr, s->out2in.port,
149 &s->ext_host_addr, s->ext_host_port,
150 s->in2out.protocol, is_twice_nat_session (s));
152 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
153 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
156 if (is_twice_nat_session (s))
158 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
160 key.protocol = s->in2out.protocol;
161 key.port = s->ext_host_nat_port;
162 a = sm->twice_nat_addresses + i;
163 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
165 snat_free_outside_address_and_port (sm->twice_nat_addresses,
173 if (snat_is_session_static (s))
176 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
179 nat44_delete_session (sm, s, ctx->thread_index);
187 static snat_session_t *
188 create_session_for_static_mapping_ed (snat_main_t * sm,
190 snat_session_key_t l_key,
191 snat_session_key_t e_key,
192 vlib_node_runtime_t * node,
194 twice_nat_type_t twice_nat,
195 lb_nat_type_t lb_nat, f64 now)
201 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
202 clib_bihash_kv_16_8_t kv;
203 snat_session_key_t eh_key;
204 nat44_is_idle_session_ctx_t ctx;
206 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
208 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
209 nat_elog_notice ("maximum sessions exceeded");
213 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
216 nat_elog_warn ("create NAT user failed");
220 s = nat_ed_session_alloc (sm, u, thread_index, now);
223 nat44_delete_user_with_no_session (sm, u, thread_index);
224 nat_elog_warn ("create NAT session failed");
228 ip = vlib_buffer_get_current (b);
229 udp = ip4_next_header (ip);
231 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
232 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
233 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
235 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
236 if (lb_nat == AFFINITY_LB_NAT)
237 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
238 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
241 s->in2out.protocol = s->out2in.protocol;
242 user_session_increment (sm, u, 1);
244 /* Add to lookup tables */
245 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
246 e_key.fib_index, e_key.port, s->ext_host_port);
247 kv.value = s - tsm->sessions;
249 ctx.thread_index = thread_index;
250 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
251 nat44_o2i_ed_is_idle_session_cb,
253 nat_elog_notice ("out2in-ed key add failed");
255 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
256 ip->src_address.as_u32 == l_key.addr.as_u32))
258 eh_key.protocol = e_key.protocol;
259 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
260 thread_index, &eh_key,
262 tsm->snat_thread_index))
264 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
265 nat44_delete_session (sm, s, thread_index);
266 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
267 nat_elog_notice ("out2in-ed key del failed");
270 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
271 s->ext_host_nat_port = eh_key.port;
272 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
273 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
274 l_key.fib_index, l_key.port, s->ext_host_nat_port);
278 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
279 l_key.fib_index, l_key.port, s->ext_host_port);
281 kv.value = s - tsm->sessions;
282 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
283 nat44_i2o_ed_is_idle_session_cb,
285 nat_elog_notice ("in2out-ed key add failed");
287 snat_ipfix_logging_nat44_ses_create (thread_index,
288 s->in2out.addr.as_u32,
289 s->out2in.addr.as_u32,
292 s->out2in.port, s->in2out.fib_index);
294 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
295 &s->in2out.addr, s->in2out.port,
296 &s->ext_host_nat_addr, s->ext_host_nat_port,
297 &s->out2in.addr, s->out2in.port,
298 &s->ext_host_addr, s->ext_host_port,
299 s->in2out.protocol, is_twice_nat_session (s));
301 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
302 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
303 &s->ext_host_nat_addr, s->ext_host_nat_port,
304 s->in2out.protocol, s->in2out.fib_index, s->flags,
311 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
312 u16 dst_port, u32 thread_index, u32 rx_fib_index)
314 clib_bihash_kv_16_8_t kv, value;
315 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
317 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
318 rx_fib_index, src_port, dst_port);
319 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
326 create_bypass_for_fwd (snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
329 nat_ed_ses_key_t key;
330 clib_bihash_kv_16_8_t kv, value;
333 snat_session_t *s = 0;
334 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
335 f64 now = vlib_time_now (sm->vlib_main);
337 if (ip->protocol == IP_PROTOCOL_ICMP)
339 if (get_icmp_o2i_ed_key (ip, &key))
342 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
344 udp = ip4_next_header (ip);
345 key.r_addr = ip->src_address;
346 key.l_addr = ip->dst_address;
347 key.proto = ip->protocol;
348 key.l_port = udp->dst_port;
349 key.r_port = udp->src_port;
353 key.r_addr = ip->src_address;
354 key.l_addr = ip->dst_address;
355 key.proto = ip->protocol;
356 key.l_port = key.r_port = 0;
359 kv.key[0] = key.as_u64[0];
360 kv.key[1] = key.as_u64[1];
362 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
364 s = pool_elt_at_index (tsm->sessions, value.value);
370 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
373 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
377 nat_elog_warn ("create NAT user failed");
381 s = nat_ed_session_alloc (sm, u, thread_index, now);
384 nat44_delete_user_with_no_session (sm, u, thread_index);
385 nat_elog_warn ("create NAT session failed");
389 proto = ip_proto_to_snat_proto (key.proto);
391 s->ext_host_addr = key.r_addr;
392 s->ext_host_port = key.r_port;
393 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
394 s->out2in.addr = key.l_addr;
395 s->out2in.port = key.l_port;
396 s->out2in.protocol = proto;
399 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
400 s->out2in.port = ip->protocol;
402 s->out2in.fib_index = 0;
403 s->in2out = s->out2in;
404 user_session_increment (sm, u, 0);
406 kv.value = s - tsm->sessions;
407 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
408 nat_elog_notice ("in2out_ed key add failed");
411 if (ip->protocol == IP_PROTOCOL_TCP)
413 tcp_header_t *tcp = ip4_next_header (ip);
414 if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
419 nat44_session_update_counters (s, now, 0, thread_index);
420 /* Per-user LRU list maintenance */
421 nat44_session_update_lru (sm, s, thread_index);
425 create_bypass_for_fwd_worker (snat_main_t * sm, ip4_header_t * ip,
428 ip4_header_t ip_wkr = {
429 .src_address = ip->dst_address,
431 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
433 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
436 #ifndef CLIB_MARCH_VARIANT
438 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
439 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
440 u8 * p_proto, snat_session_key_t * p_value,
441 u8 * p_dont_translate, void *d, void *e)
443 u32 next = ~0, sw_if_index, rx_fib_index;
444 icmp46_header_t *icmp;
445 nat_ed_ses_key_t key;
446 clib_bihash_kv_16_8_t kv, value;
447 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
448 snat_session_t *s = 0;
449 u8 dont_translate = 0, is_addr_only, identity_nat;
450 snat_session_key_t e_key, l_key;
452 icmp = (icmp46_header_t *) ip4_next_header (ip);
453 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
454 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
456 if (get_icmp_o2i_ed_key (ip, &key))
458 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
459 next = NAT_NEXT_DROP;
462 key.fib_index = rx_fib_index;
463 kv.key[0] = key.as_u64[0];
464 kv.key[1] = key.as_u64[1];
466 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
468 /* Try to match static mapping */
469 e_key.addr = ip->dst_address;
470 e_key.port = key.l_port;
471 e_key.protocol = ip_proto_to_snat_proto (key.proto);
472 e_key.fib_index = rx_fib_index;
473 if (snat_static_mapping_match
474 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
476 if (!sm->forwarding_enabled)
478 /* Don't NAT packet aimed at the intfc address */
479 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
480 ip->dst_address.as_u32)))
485 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
486 next = NAT_NEXT_DROP;
492 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
493 thread_index, rx_fib_index))
495 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
498 if (sm->num_workers > 1)
499 create_bypass_for_fwd_worker (sm, ip, rx_fib_index);
501 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
506 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
507 (icmp->type != ICMP4_echo_request || !is_addr_only)))
509 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
510 next = NAT_NEXT_DROP;
514 if (PREDICT_FALSE (identity_nat))
520 /* Create session initiated by host from external network */
521 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
528 next = NAT_NEXT_DROP;
534 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
535 icmp->type != ICMP4_echo_request &&
536 !icmp_is_error_message (icmp)))
538 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
539 next = NAT_NEXT_DROP;
543 s = pool_elt_at_index (tsm->sessions, value.value);
546 *p_proto = ip_proto_to_snat_proto (key.proto);
549 *p_value = s->in2out;
550 *p_dont_translate = dont_translate;
552 *(snat_session_t **) d = s;
557 static snat_session_t *
558 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
564 vlib_main_t * vm, vlib_node_runtime_t * node)
566 clib_bihash_kv_8_8_t kv, value;
567 clib_bihash_kv_16_8_t s_kv, s_value;
568 snat_static_mapping_t *m;
569 u32 old_addr, new_addr;
572 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
575 old_addr = ip->dst_address.as_u32;
577 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
580 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
582 s = pool_elt_at_index (tsm->sessions, s_value.value);
583 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
587 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
589 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
590 nat_elog_notice ("maximum sessions exceeded");
594 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
595 if (clib_bihash_search_8_8
596 (&sm->static_mapping_by_external, &kv, &value))
598 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
602 m = pool_elt_at_index (sm->static_mappings, value.value);
604 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
606 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
610 nat_elog_warn ("create NAT user failed");
614 /* Create a new session */
615 s = nat_ed_session_alloc (sm, u, thread_index, now);
618 nat44_delete_user_with_no_session (sm, u, thread_index);
619 nat_elog_warn ("create NAT session failed");
623 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
624 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
625 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
626 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
627 s->out2in.addr.as_u32 = old_addr;
628 s->out2in.fib_index = rx_fib_index;
629 s->in2out.addr.as_u32 = new_addr;
630 s->in2out.fib_index = m->fib_index;
631 s->in2out.port = s->out2in.port = ip->protocol;
632 user_session_increment (sm, u, 1);
634 /* Add to lookup tables */
635 s_kv.value = s - tsm->sessions;
636 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
637 nat_elog_notice ("out2in key add failed");
639 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
641 s_kv.value = s - tsm->sessions;
642 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
643 nat_elog_notice ("in2out key add failed");
646 /* Update IP checksum */
648 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
649 ip->checksum = ip_csum_fold (sum);
651 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
654 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
656 /* Per-user LRU list maintenance */
657 nat44_session_update_lru (sm, s, thread_index);
663 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
664 vlib_node_runtime_t * node,
665 vlib_frame_t * frame, int is_slow_path)
667 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
668 nat_next_t next_index;
669 snat_main_t *sm = &snat_main;
670 f64 now = vlib_time_now (vm);
671 u32 thread_index = vm->thread_index;
672 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
673 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
676 stats_node_index = is_slow_path ? sm->ed_out2in_slowpath_node_index :
677 sm->ed_out2in_node_index;
679 from = vlib_frame_vector_args (frame);
680 n_left_from = frame->n_vectors;
681 next_index = node->cached_next_index;
683 while (n_left_from > 0)
687 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
689 while (n_left_from >= 4 && n_left_to_next >= 2)
692 vlib_buffer_t *b0, *b1;
693 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
695 u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
697 u16 old_port0, new_port0, old_port1, new_port1;
698 ip4_header_t *ip0, *ip1;
699 udp_header_t *udp0, *udp1;
700 tcp_header_t *tcp0, *tcp1;
701 icmp46_header_t *icmp0, *icmp1;
702 snat_session_t *s0 = 0, *s1 = 0;
703 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
704 ip_csum_t sum0, sum1;
705 snat_session_key_t e_key0, l_key0, e_key1, l_key1;
706 lb_nat_type_t lb_nat0, lb_nat1;
707 twice_nat_type_t twice_nat0, twice_nat1;
708 u8 identity_nat0, identity_nat1;
710 /* Prefetch next iteration. */
712 vlib_buffer_t *p2, *p3;
714 p2 = vlib_get_buffer (vm, from[2]);
715 p3 = vlib_get_buffer (vm, from[3]);
717 vlib_prefetch_buffer_header (p2, LOAD);
718 vlib_prefetch_buffer_header (p3, LOAD);
720 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
721 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
724 /* speculatively enqueue b0 and b1 to the current next frame */
725 to_next[0] = bi0 = from[0];
726 to_next[1] = bi1 = from[1];
732 b0 = vlib_get_buffer (vm, bi0);
733 b1 = vlib_get_buffer (vm, bi1);
735 next0 = nat_buffer_opaque (b0)->arc_next;
736 next1 = nat_buffer_opaque (b1)->arc_next;
738 vnet_buffer (b0)->snat.flags = 0;
739 ip0 = vlib_buffer_get_current (b0);
741 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
743 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
746 if (PREDICT_FALSE (ip0->ttl == 1))
748 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
749 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
750 ICMP4_time_exceeded_ttl_exceeded_in_transit,
752 next0 = NAT_NEXT_ICMP_ERROR;
756 udp0 = ip4_next_header (ip0);
757 tcp0 = (tcp_header_t *) udp0;
758 icmp0 = (icmp46_header_t *) udp0;
759 proto0 = ip_proto_to_snat_proto (ip0->protocol);
763 if (PREDICT_FALSE (proto0 == ~0))
766 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
767 thread_index, now, vm,
770 if (!sm->forwarding_enabled)
773 next0 = NAT_NEXT_DROP;
778 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
780 next0 = icmp_out2in_ed_slow_path
781 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
782 next0, now, thread_index, &s0);
789 if (PREDICT_FALSE (proto0 == ~0))
791 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
795 if (ip4_is_fragment (ip0))
797 next0 = NAT_NEXT_OUT2IN_ED_REASS;
802 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
804 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
809 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
810 ip0->protocol, rx_fib_index0, udp0->dst_port,
813 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
817 /* Try to match static mapping by external address and port,
818 destination address and port in packet */
819 e_key0.addr = ip0->dst_address;
820 e_key0.port = udp0->dst_port;
821 e_key0.protocol = proto0;
822 e_key0.fib_index = rx_fib_index0;
823 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
824 &twice_nat0, &lb_nat0,
829 * Send DHCP packets to the ipv4 stack, or we won't
830 * be able to use dhcp client on the outside interface
832 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
833 && (udp0->dst_port ==
835 (UDP_DST_PORT_dhcp_to_client))))
840 if (!sm->forwarding_enabled)
843 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
844 next0 = NAT_NEXT_DROP;
848 if (next_src_nat (sm, ip0, ip0->protocol,
849 udp0->src_port, udp0->dst_port,
850 thread_index, rx_fib_index0))
852 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
855 if (sm->num_workers > 1)
856 create_bypass_for_fwd_worker (sm, ip0,
859 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
865 if (PREDICT_FALSE (identity_nat0))
868 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
870 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
871 next0 = NAT_NEXT_DROP;
875 /* Create session initiated by host from external network */
876 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
884 next0 = NAT_NEXT_DROP;
890 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
896 s0 = pool_elt_at_index (tsm->sessions, value0.value);
899 old_addr0 = ip0->dst_address.as_u32;
900 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
901 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
903 sum0 = ip0->checksum;
904 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
906 if (PREDICT_FALSE (is_twice_nat_session (s0)))
907 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
908 s0->ext_host_nat_addr.as_u32, ip4_header_t,
910 ip0->checksum = ip_csum_fold (sum0);
912 old_port0 = udp0->dst_port;
913 new_port0 = udp0->dst_port = s0->in2out.port;
915 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
917 sum0 = tcp0->checksum;
918 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
920 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
922 if (is_twice_nat_session (s0))
924 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
925 s0->ext_host_nat_addr.as_u32,
926 ip4_header_t, dst_address);
927 sum0 = ip_csum_update (sum0, tcp0->src_port,
928 s0->ext_host_nat_port, ip4_header_t,
930 tcp0->src_port = s0->ext_host_nat_port;
931 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
933 tcp0->checksum = ip_csum_fold (sum0);
935 if (nat44_set_tcp_session_state_o2i
936 (sm, s0, tcp0, thread_index))
939 else if (udp0->checksum)
941 sum0 = udp0->checksum;
942 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
944 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
946 if (PREDICT_FALSE (is_twice_nat_session (s0)))
948 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
949 s0->ext_host_nat_addr.as_u32,
950 ip4_header_t, dst_address);
951 sum0 = ip_csum_update (sum0, udp0->src_port,
952 s0->ext_host_nat_port, ip4_header_t,
954 udp0->src_port = s0->ext_host_nat_port;
955 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
957 udp0->checksum = ip_csum_fold (sum0);
962 if (PREDICT_FALSE (is_twice_nat_session (s0)))
964 udp0->src_port = s0->ext_host_nat_port;
965 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
971 nat44_session_update_counters (s0, now,
972 vlib_buffer_length_in_chain (vm, b0),
974 /* Per-user LRU list maintenance */
975 nat44_session_update_lru (sm, s0, thread_index);
978 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
979 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
981 nat44_ed_out2in_trace_t *t =
982 vlib_add_trace (vm, node, b0, sizeof (*t));
983 t->is_slow_path = is_slow_path;
984 t->sw_if_index = sw_if_index0;
985 t->next_index = next0;
986 t->session_index = ~0;
988 t->session_index = s0 - tsm->sessions;
991 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
993 vnet_buffer (b1)->snat.flags = 0;
994 ip1 = vlib_buffer_get_current (b1);
996 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
998 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1001 if (PREDICT_FALSE (ip1->ttl == 1))
1003 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1004 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1005 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1007 next1 = NAT_NEXT_ICMP_ERROR;
1011 udp1 = ip4_next_header (ip1);
1012 tcp1 = (tcp_header_t *) udp1;
1013 icmp1 = (icmp46_header_t *) udp1;
1014 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1018 if (PREDICT_FALSE (proto1 == ~0))
1021 nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1022 thread_index, now, vm,
1025 if (!sm->forwarding_enabled)
1028 next1 = NAT_NEXT_DROP;
1033 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1035 next1 = icmp_out2in_ed_slow_path
1036 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1037 next1, now, thread_index, &s1);
1044 if (PREDICT_FALSE (proto1 == ~0))
1046 next1 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1050 if (ip4_is_fragment (ip1))
1052 next1 = NAT_NEXT_OUT2IN_ED_REASS;
1057 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1059 next1 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1064 make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1065 ip1->protocol, rx_fib_index1, udp1->dst_port,
1068 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1072 /* Try to match static mapping by external address and port,
1073 destination address and port in packet */
1074 e_key1.addr = ip1->dst_address;
1075 e_key1.port = udp1->dst_port;
1076 e_key1.protocol = proto1;
1077 e_key1.fib_index = rx_fib_index1;
1078 if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1079 &twice_nat1, &lb_nat1,
1084 * Send DHCP packets to the ipv4 stack, or we won't
1085 * be able to use dhcp client on the outside interface
1087 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1088 && (udp1->dst_port ==
1089 clib_host_to_net_u16
1090 (UDP_DST_PORT_dhcp_to_client))))
1095 if (!sm->forwarding_enabled)
1098 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1099 next1 = NAT_NEXT_DROP;
1103 if (next_src_nat (sm, ip1, ip1->protocol,
1104 udp1->src_port, udp1->dst_port,
1105 thread_index, rx_fib_index1))
1107 next1 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1110 if (sm->num_workers > 1)
1111 create_bypass_for_fwd_worker (sm, ip1,
1114 create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1120 if (PREDICT_FALSE (identity_nat1))
1123 if ((proto1 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp1))
1125 b1->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1126 next1 = NAT_NEXT_DROP;
1130 /* Create session initiated by host from external network */
1131 s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1139 next1 = NAT_NEXT_DROP;
1145 next1 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1151 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1154 old_addr1 = ip1->dst_address.as_u32;
1155 new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1156 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1158 sum1 = ip1->checksum;
1159 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1161 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1162 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1163 s1->ext_host_nat_addr.as_u32, ip4_header_t,
1165 ip1->checksum = ip_csum_fold (sum1);
1167 old_port1 = udp1->dst_port;
1168 new_port1 = udp1->dst_port = s1->in2out.port;
1170 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1172 sum1 = tcp1->checksum;
1173 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1175 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1177 if (is_twice_nat_session (s1))
1179 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1180 s1->ext_host_nat_addr.as_u32,
1181 ip4_header_t, dst_address);
1182 sum1 = ip_csum_update (sum1, tcp1->src_port,
1183 s1->ext_host_nat_port, ip4_header_t,
1185 tcp1->src_port = s1->ext_host_nat_port;
1186 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1188 tcp1->checksum = ip_csum_fold (sum1);
1190 if (nat44_set_tcp_session_state_o2i
1191 (sm, s1, tcp1, thread_index))
1194 else if (udp1->checksum)
1196 sum1 = udp1->checksum;
1197 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1199 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1201 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1203 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1204 s1->ext_host_nat_addr.as_u32,
1205 ip4_header_t, dst_address);
1206 sum1 = ip_csum_update (sum1, udp1->src_port,
1207 s1->ext_host_nat_port, ip4_header_t,
1209 udp1->src_port = s1->ext_host_nat_port;
1210 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1212 udp1->checksum = ip_csum_fold (sum1);
1217 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1219 udp1->src_port = s1->ext_host_nat_port;
1220 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1226 nat44_session_update_counters (s1, now,
1227 vlib_buffer_length_in_chain (vm, b1),
1229 /* Per-user LRU list maintenance */
1230 nat44_session_update_lru (sm, s1, thread_index);
1233 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1234 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1236 nat44_ed_out2in_trace_t *t =
1237 vlib_add_trace (vm, node, b1, sizeof (*t));
1238 t->is_slow_path = is_slow_path;
1239 t->sw_if_index = sw_if_index1;
1240 t->next_index = next1;
1241 t->session_index = ~0;
1243 t->session_index = s1 - tsm->sessions;
1246 pkts_processed += next1 == nat_buffer_opaque (b1)->arc_next;
1248 /* verify speculative enqueues, maybe switch current next frame */
1249 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1250 to_next, n_left_to_next,
1251 bi0, bi1, next0, next1);
1254 while (n_left_from > 0 && n_left_to_next > 0)
1258 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1260 u16 old_port0, new_port0;
1264 icmp46_header_t *icmp0;
1265 snat_session_t *s0 = 0;
1266 clib_bihash_kv_16_8_t kv0, value0;
1268 snat_session_key_t e_key0, l_key0;
1269 lb_nat_type_t lb_nat0;
1270 twice_nat_type_t twice_nat0;
1273 /* speculatively enqueue b0 to the current next frame */
1279 n_left_to_next -= 1;
1281 b0 = vlib_get_buffer (vm, bi0);
1282 next0 = nat_buffer_opaque (b0)->arc_next;
1284 vnet_buffer (b0)->snat.flags = 0;
1285 ip0 = vlib_buffer_get_current (b0);
1287 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1289 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1292 if (PREDICT_FALSE (ip0->ttl == 1))
1294 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1295 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1296 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1298 next0 = NAT_NEXT_ICMP_ERROR;
1302 udp0 = ip4_next_header (ip0);
1303 tcp0 = (tcp_header_t *) udp0;
1304 icmp0 = (icmp46_header_t *) udp0;
1305 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1309 if (PREDICT_FALSE (proto0 == ~0))
1312 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1313 thread_index, now, vm,
1316 if (!sm->forwarding_enabled)
1319 next0 = NAT_NEXT_DROP;
1324 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1326 next0 = icmp_out2in_ed_slow_path
1327 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1328 next0, now, thread_index, &s0);
1335 if (PREDICT_FALSE (proto0 == ~0))
1337 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1341 if (ip4_is_fragment (ip0))
1343 next0 = NAT_NEXT_OUT2IN_ED_REASS;
1348 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1350 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1355 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1356 ip0->protocol, rx_fib_index0, udp0->dst_port,
1359 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1363 /* Try to match static mapping by external address and port,
1364 destination address and port in packet */
1365 e_key0.addr = ip0->dst_address;
1366 e_key0.port = udp0->dst_port;
1367 e_key0.protocol = proto0;
1368 e_key0.fib_index = rx_fib_index0;
1369 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1370 &twice_nat0, &lb_nat0,
1375 * Send DHCP packets to the ipv4 stack, or we won't
1376 * be able to use dhcp client on the outside interface
1378 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1379 && (udp0->dst_port ==
1380 clib_host_to_net_u16
1381 (UDP_DST_PORT_dhcp_to_client))))
1386 if (!sm->forwarding_enabled)
1389 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1390 next0 = NAT_NEXT_DROP;
1394 if (next_src_nat (sm, ip0, ip0->protocol,
1395 udp0->src_port, udp0->dst_port,
1396 thread_index, rx_fib_index0))
1398 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1401 if (sm->num_workers > 1)
1402 create_bypass_for_fwd_worker (sm, ip0,
1405 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1411 if (PREDICT_FALSE (identity_nat0))
1414 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1416 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1417 next0 = NAT_NEXT_DROP;
1421 /* Create session initiated by host from external network */
1422 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1430 next0 = NAT_NEXT_DROP;
1436 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1442 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1445 old_addr0 = ip0->dst_address.as_u32;
1446 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1447 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1449 sum0 = ip0->checksum;
1450 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1452 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1453 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1454 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1456 ip0->checksum = ip_csum_fold (sum0);
1458 old_port0 = udp0->dst_port;
1459 new_port0 = udp0->dst_port = s0->in2out.port;
1461 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1463 sum0 = tcp0->checksum;
1464 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1466 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1468 if (is_twice_nat_session (s0))
1470 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1471 s0->ext_host_nat_addr.as_u32,
1472 ip4_header_t, dst_address);
1473 sum0 = ip_csum_update (sum0, tcp0->src_port,
1474 s0->ext_host_nat_port, ip4_header_t,
1476 tcp0->src_port = s0->ext_host_nat_port;
1477 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1479 tcp0->checksum = ip_csum_fold (sum0);
1481 if (nat44_set_tcp_session_state_o2i
1482 (sm, s0, tcp0, thread_index))
1485 else if (udp0->checksum)
1487 sum0 = udp0->checksum;
1488 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1490 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1492 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1494 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1495 s0->ext_host_nat_addr.as_u32,
1496 ip4_header_t, dst_address);
1497 sum0 = ip_csum_update (sum0, udp0->src_port,
1498 s0->ext_host_nat_port, ip4_header_t,
1500 udp0->src_port = s0->ext_host_nat_port;
1501 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1503 udp0->checksum = ip_csum_fold (sum0);
1508 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1510 udp0->src_port = s0->ext_host_nat_port;
1511 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1517 nat44_session_update_counters (s0, now,
1518 vlib_buffer_length_in_chain (vm, b0),
1520 /* Per-user LRU list maintenance */
1521 nat44_session_update_lru (sm, s0, thread_index);
1524 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1525 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1527 nat44_ed_out2in_trace_t *t =
1528 vlib_add_trace (vm, node, b0, sizeof (*t));
1529 t->is_slow_path = is_slow_path;
1530 t->sw_if_index = sw_if_index0;
1531 t->next_index = next0;
1532 t->session_index = ~0;
1534 t->session_index = s0 - tsm->sessions;
1537 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1538 /* verify speculative enqueue, maybe switch current next frame */
1539 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1540 to_next, n_left_to_next,
1544 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1547 vlib_node_increment_counter (vm, stats_node_index,
1548 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1550 vlib_node_increment_counter (vm, stats_node_index,
1551 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1552 vlib_node_increment_counter (vm, stats_node_index,
1553 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1554 vlib_node_increment_counter (vm, stats_node_index,
1555 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1557 vlib_node_increment_counter (vm, stats_node_index,
1558 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1560 vlib_node_increment_counter (vm, stats_node_index,
1561 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1562 return frame->n_vectors;
1566 nat44_ed_out2in_reass_node_fn_inline (vlib_main_t * vm,
1567 vlib_node_runtime_t * node,
1568 vlib_frame_t * frame)
1570 u32 n_left_from, *from, *to_next;
1571 nat_next_t next_index;
1572 u32 pkts_processed = 0;
1573 snat_main_t *sm = &snat_main;
1574 f64 now = vlib_time_now (vm);
1575 u32 thread_index = vm->thread_index;
1576 snat_main_per_thread_data_t *per_thread_data =
1577 &sm->per_thread_data[thread_index];
1578 u32 *fragments_to_drop = 0;
1579 u32 *fragments_to_loopback = 0;
1581 from = vlib_frame_vector_args (frame);
1582 n_left_from = frame->n_vectors;
1583 next_index = node->cached_next_index;
1585 while (n_left_from > 0)
1589 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1591 while (n_left_from > 0 && n_left_to_next > 0)
1593 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1598 nat_reass_ip4_t *reass0;
1601 icmp46_header_t *icmp0;
1602 clib_bihash_kv_16_8_t kv0, value0;
1603 snat_session_t *s0 = 0;
1604 u16 old_port0, new_port0;
1606 snat_session_key_t e_key0, l_key0;
1608 twice_nat_type_t twice_nat0;
1611 /* speculatively enqueue b0 to the current next frame */
1617 n_left_to_next -= 1;
1619 b0 = vlib_get_buffer (vm, bi0);
1620 next0 = nat_buffer_opaque (b0)->arc_next;
1622 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1624 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1627 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1629 next0 = NAT_NEXT_DROP;
1630 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1634 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1635 udp0 = ip4_next_header (ip0);
1636 tcp0 = (tcp_header_t *) udp0;
1637 icmp0 = (icmp46_header_t *) udp0;
1638 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1640 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1644 1, &fragments_to_drop);
1646 if (PREDICT_FALSE (!reass0))
1648 next0 = NAT_NEXT_DROP;
1649 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1650 nat_elog_notice ("maximum reassemblies exceeded");
1654 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1656 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1658 next0 = icmp_out2in_ed_slow_path
1659 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1660 next0, now, thread_index, &s0);
1662 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP))
1665 reass0->sess_index = s0 - per_thread_data->sessions;
1667 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1668 reass0->thread_index = thread_index;
1669 nat_ip4_reass_get_frags (reass0,
1670 &fragments_to_loopback);
1676 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1677 ip0->protocol, rx_fib_index0, udp0->dst_port,
1680 if (clib_bihash_search_16_8
1681 (&per_thread_data->out2in_ed, &kv0, &value0))
1683 /* Try to match static mapping by external address and port,
1684 destination address and port in packet */
1685 e_key0.addr = ip0->dst_address;
1686 e_key0.port = udp0->dst_port;
1687 e_key0.protocol = proto0;
1688 e_key0.fib_index = rx_fib_index0;
1689 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1690 &twice_nat0, &lb0, 0,
1694 * Send DHCP packets to the ipv4 stack, or we won't
1695 * be able to use dhcp client on the outside interface
1697 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1700 clib_host_to_net_u16
1701 (UDP_DST_PORT_dhcp_to_client))))
1706 if (!sm->forwarding_enabled)
1709 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1710 next0 = NAT_NEXT_DROP;
1714 if (next_src_nat (sm, ip0, ip0->protocol,
1715 udp0->src_port, udp0->dst_port,
1716 thread_index, rx_fib_index0))
1718 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1721 if (sm->num_workers > 1)
1722 create_bypass_for_fwd_worker (sm, ip0,
1725 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1727 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1728 nat_ip4_reass_get_frags (reass0,
1729 &fragments_to_loopback);
1734 if (PREDICT_FALSE (identity_nat0))
1736 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1740 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1742 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1743 next0 = NAT_NEXT_DROP;
1747 /* Create session initiated by host from external network */
1748 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1756 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1757 next0 = NAT_NEXT_DROP;
1760 reass0->sess_index = s0 - per_thread_data->sessions;
1761 reass0->thread_index = thread_index;
1765 s0 = pool_elt_at_index (per_thread_data->sessions,
1767 reass0->sess_index = value0.value;
1769 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1773 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1775 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1777 if (nat_ip4_reass_add_fragment
1778 (thread_index, reass0, bi0, &fragments_to_drop))
1780 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1782 ("maximum fragments per reassembly exceeded");
1783 next0 = NAT_NEXT_DROP;
1789 s0 = pool_elt_at_index (per_thread_data->sessions,
1790 reass0->sess_index);
1793 old_addr0 = ip0->dst_address.as_u32;
1794 ip0->dst_address = s0->in2out.addr;
1795 new_addr0 = ip0->dst_address.as_u32;
1796 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1798 sum0 = ip0->checksum;
1799 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1801 dst_address /* changed member */ );
1802 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1803 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1804 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1806 ip0->checksum = ip_csum_fold (sum0);
1808 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1810 old_port0 = udp0->dst_port;
1811 new_port0 = udp0->dst_port = s0->in2out.port;
1813 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1815 sum0 = tcp0->checksum;
1816 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1818 dst_address /* changed member */ );
1820 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1821 ip4_header_t /* cheat */ ,
1822 length /* changed member */ );
1823 if (is_twice_nat_session (s0))
1825 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1826 s0->ext_host_nat_addr.as_u32,
1827 ip4_header_t, dst_address);
1828 sum0 = ip_csum_update (sum0, tcp0->src_port,
1829 s0->ext_host_nat_port,
1830 ip4_header_t, length);
1831 tcp0->src_port = s0->ext_host_nat_port;
1832 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1834 tcp0->checksum = ip_csum_fold (sum0);
1836 else if (udp0->checksum)
1838 sum0 = udp0->checksum;
1840 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1843 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1845 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1847 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1848 s0->ext_host_nat_addr.as_u32,
1849 ip4_header_t, dst_address);
1850 sum0 = ip_csum_update (sum0, udp0->src_port,
1851 s0->ext_host_nat_port,
1852 ip4_header_t, length);
1853 udp0->src_port = s0->ext_host_nat_port;
1854 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1856 udp0->checksum = ip_csum_fold (sum0);
1860 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1862 udp0->src_port = s0->ext_host_nat_port;
1863 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1869 nat44_session_update_counters (s0, now,
1870 vlib_buffer_length_in_chain (vm, b0),
1872 /* Per-user LRU list maintenance */
1873 nat44_session_update_lru (sm, s0, thread_index);
1876 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1877 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1879 nat44_reass_trace_t *t =
1880 vlib_add_trace (vm, node, b0, sizeof (*t));
1881 t->cached = cached0;
1882 t->sw_if_index = sw_if_index0;
1883 t->next_index = next0;
1893 pkts_processed += next0 != NAT_NEXT_DROP;
1895 /* verify speculative enqueue, maybe switch current next frame */
1896 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1897 to_next, n_left_to_next,
1901 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1903 from = vlib_frame_vector_args (frame);
1904 u32 len = vec_len (fragments_to_loopback);
1905 if (len <= VLIB_FRAME_SIZE)
1907 clib_memcpy_fast (from, fragments_to_loopback,
1908 sizeof (u32) * len);
1910 vec_reset_length (fragments_to_loopback);
1914 clib_memcpy_fast (from, fragments_to_loopback +
1915 (len - VLIB_FRAME_SIZE),
1916 sizeof (u32) * VLIB_FRAME_SIZE);
1917 n_left_from = VLIB_FRAME_SIZE;
1918 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1923 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1926 vlib_node_increment_counter (vm, sm->ed_out2in_reass_node_index,
1927 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1930 nat_send_all_to_node (vm, fragments_to_drop, node,
1931 &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1934 vec_free (fragments_to_drop);
1935 vec_free (fragments_to_loopback);
1936 return frame->n_vectors;
1939 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1940 vlib_node_runtime_t * node,
1941 vlib_frame_t * frame)
1943 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1947 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1948 .name = "nat44-ed-out2in",
1949 .vector_size = sizeof (u32),
1950 .sibling_of = "nat-default",
1951 .format_trace = format_nat44_ed_out2in_trace,
1952 .type = VLIB_NODE_TYPE_INTERNAL,
1953 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1954 .error_strings = nat_out2in_ed_error_strings,
1955 .runtime_data_bytes = sizeof (snat_runtime_t),
1959 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1960 vlib_node_runtime_t * node,
1961 vlib_frame_t * frame)
1963 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1967 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1968 .name = "nat44-ed-out2in-slowpath",
1969 .vector_size = sizeof (u32),
1970 .sibling_of = "nat-default",
1971 .format_trace = format_nat44_ed_out2in_trace,
1972 .type = VLIB_NODE_TYPE_INTERNAL,
1973 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1974 .error_strings = nat_out2in_ed_error_strings,
1975 .runtime_data_bytes = sizeof (snat_runtime_t),
1979 VLIB_NODE_FN (nat44_ed_out2in_reass_node) (vlib_main_t * vm,
1980 vlib_node_runtime_t * node,
1981 vlib_frame_t * frame)
1983 return nat44_ed_out2in_reass_node_fn_inline (vm, node, frame);
1987 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1988 .name = "nat44-ed-out2in-reass",
1989 .vector_size = sizeof (u32),
1990 .sibling_of = "nat-default",
1991 .format_trace = format_nat44_reass_trace,
1992 .type = VLIB_NODE_TYPE_INTERNAL,
1993 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1994 .error_strings = nat_out2in_ed_error_strings,
1999 format_nat_pre_trace (u8 * s, va_list * args)
2001 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2002 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2003 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
2004 return format (s, "out2in next_index %d", t->next_index);
2007 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
2008 vlib_node_runtime_t * node,
2009 vlib_frame_t * frame)
2011 return nat_pre_node_fn_inline (vm, node, frame,
2012 NAT_NEXT_OUT2IN_ED_FAST_PATH);
2016 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
2017 .name = "nat-pre-out2in",
2018 .vector_size = sizeof (u32),
2019 .sibling_of = "nat-default",
2020 .format_trace = format_nat_pre_trace,
2021 .type = VLIB_NODE_TYPE_INTERNAL,
2027 * fd.io coding-style-patch-verification: ON
2030 * eval: (c-set-style "gnu")