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>
33 #define foreach_nat_out2in_ed_error \
34 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
35 _(OUT2IN_PACKETS, "Good out2in packets processed") \
36 _(OUT_OF_PORTS, "Out of ports") \
37 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
38 _(NO_TRANSLATION, "No translation") \
39 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
40 _(DROP_FRAGMENT, "Drop fragment") \
41 _(MAX_REASS, "Maximum reassemblies exceeded") \
42 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")\
43 _(NON_SYN, "non-SYN packet try to create session")
47 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
48 foreach_nat_out2in_ed_error
50 NAT_OUT2IN_ED_N_ERROR,
51 } nat_out2in_ed_error_t;
53 static char *nat_out2in_ed_error_strings[] = {
54 #define _(sym,string) string,
55 foreach_nat_out2in_ed_error
61 NAT44_ED_OUT2IN_NEXT_DROP,
62 NAT44_ED_OUT2IN_NEXT_LOOKUP,
63 NAT44_ED_OUT2IN_NEXT_ICMP_ERROR,
64 NAT44_ED_OUT2IN_NEXT_IN2OUT,
65 NAT44_ED_OUT2IN_NEXT_SLOW_PATH,
66 NAT44_ED_OUT2IN_NEXT_REASS,
67 NAT44_ED_OUT2IN_N_NEXT,
68 } nat44_ed_out2in_next_t;
76 } nat44_ed_out2in_trace_t;
78 vlib_node_registration_t nat44_ed_out2in_node;
79 vlib_node_registration_t nat44_ed_out2in_slowpath_node;
80 vlib_node_registration_t nat44_ed_out2in_reass_node;
83 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
85 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
86 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
87 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
91 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
92 "NAT44_OUT2IN_ED_FAST_PATH";
94 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
95 t->sw_if_index, t->next_index, t->session_index);
101 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
102 ip4_header_t * ip0, icmp46_header_t * icmp0,
103 u32 sw_if_index0, u32 rx_fib_index0,
104 vlib_node_runtime_t * node, u32 next0, f64 now,
105 u32 thread_index, snat_session_t ** p_s0)
107 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
108 next0, thread_index, p_s0, 0);
109 snat_session_t *s0 = *p_s0;
110 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP && s0))
113 nat44_session_update_counters (s0, now,
114 vlib_buffer_length_in_chain
115 (sm->vlib_main, b0));
116 /* Per-user LRU list maintenance */
117 nat44_session_update_lru (sm, s0, thread_index);
123 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
125 snat_main_t *sm = &snat_main;
126 nat44_is_idle_session_ctx_t *ctx = arg;
128 u64 sess_timeout_time;
129 nat_ed_ses_key_t ed_key;
130 clib_bihash_kv_16_8_t ed_kv;
133 snat_session_key_t key;
134 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
137 s = pool_elt_at_index (tsm->sessions, kv->value);
138 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
139 if (ctx->now >= sess_timeout_time)
141 ed_key.l_addr = s->in2out.addr;
142 ed_key.r_addr = s->ext_host_addr;
143 ed_key.fib_index = s->in2out.fib_index;
144 if (snat_is_unk_proto_session (s))
146 ed_key.proto = s->in2out.port;
152 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
153 ed_key.l_port = s->in2out.port;
154 ed_key.r_port = s->ext_host_port;
156 if (is_twice_nat_session (s))
158 ed_key.r_addr = s->ext_host_nat_addr;
159 ed_key.r_port = s->ext_host_nat_port;
161 ed_kv.key[0] = ed_key.as_u64[0];
162 ed_kv.key[1] = ed_key.as_u64[1];
163 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
164 nat_log_warn ("in2out_ed key del failed");
166 if (snat_is_unk_proto_session (s))
169 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
170 s->out2in.addr.as_u32,
174 s->in2out.fib_index);
176 if (is_twice_nat_session (s))
178 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
180 key.protocol = s->in2out.protocol;
181 key.port = s->ext_host_nat_port;
182 a = sm->twice_nat_addresses + i;
183 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
185 snat_free_outside_address_and_port (sm->twice_nat_addresses,
193 if (snat_is_session_static (s))
196 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
199 nat44_delete_session (sm, s, ctx->thread_index);
206 static snat_session_t *
207 create_session_for_static_mapping_ed (snat_main_t * sm,
209 snat_session_key_t l_key,
210 snat_session_key_t e_key,
211 vlib_node_runtime_t * node,
213 twice_nat_type_t twice_nat,
214 lb_nat_type_t lb_nat, f64 now)
220 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
221 clib_bihash_kv_16_8_t kv;
222 snat_session_key_t eh_key;
223 nat44_is_idle_session_ctx_t ctx;
225 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
227 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
228 nat_log_notice ("maximum sessions exceeded");
232 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
235 nat_log_warn ("create NAT user failed");
239 s = nat_ed_session_alloc (sm, u, thread_index, now);
242 nat44_delete_user_with_no_session (sm, u, thread_index);
243 nat_log_warn ("create NAT session failed");
247 ip = vlib_buffer_get_current (b);
248 udp = ip4_next_header (ip);
250 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
251 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
252 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
254 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
255 if (lb_nat == AFFINITY_LB_NAT)
256 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
257 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
260 s->in2out.protocol = s->out2in.protocol;
261 user_session_increment (sm, u, 1);
263 /* Add to lookup tables */
264 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
265 e_key.fib_index, e_key.port, s->ext_host_port);
266 kv.value = s - tsm->sessions;
268 ctx.thread_index = thread_index;
269 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
270 nat44_o2i_ed_is_idle_session_cb,
272 nat_log_notice ("out2in-ed key add failed");
274 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
275 ip->src_address.as_u32 == l_key.addr.as_u32))
277 eh_key.protocol = e_key.protocol;
278 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
279 thread_index, &eh_key,
281 tsm->snat_thread_index))
283 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
284 nat44_delete_session (sm, s, thread_index);
285 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
286 nat_log_notice ("out2in-ed key del failed");
289 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
290 s->ext_host_nat_port = eh_key.port;
291 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
292 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
293 l_key.fib_index, l_key.port, s->ext_host_nat_port);
297 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
298 l_key.fib_index, l_key.port, s->ext_host_port);
300 kv.value = s - tsm->sessions;
301 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
302 nat44_i2o_ed_is_idle_session_cb,
304 nat_log_notice ("in2out-ed key add failed");
309 static_always_inline int
310 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
312 icmp46_header_t *icmp0;
313 nat_ed_ses_key_t key0;
314 icmp_echo_header_t *echo0, *inner_echo0 = 0;
315 ip4_header_t *inner_ip0;
317 icmp46_header_t *inner_icmp0;
319 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
320 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
322 if (!icmp_is_error_message (icmp0))
324 key0.proto = IP_PROTOCOL_ICMP;
325 key0.l_addr = ip0->dst_address;
326 key0.r_addr = ip0->src_address;
327 key0.l_port = echo0->identifier;
332 inner_ip0 = (ip4_header_t *) (echo0 + 1);
333 l4_header = ip4_next_header (inner_ip0);
334 key0.proto = inner_ip0->protocol;
335 key0.l_addr = inner_ip0->src_address;
336 key0.r_addr = inner_ip0->dst_address;
337 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
339 case SNAT_PROTOCOL_ICMP:
340 inner_icmp0 = (icmp46_header_t *) l4_header;
341 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
342 key0.l_port = inner_echo0->identifier;
345 case SNAT_PROTOCOL_UDP:
346 case SNAT_PROTOCOL_TCP:
347 key0.l_port = ((tcp_udp_header_t *) l4_header)->src_port;
348 key0.r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
359 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
360 u16 dst_port, u32 thread_index, u32 rx_fib_index)
362 clib_bihash_kv_16_8_t kv, value;
363 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
365 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
366 rx_fib_index, src_port, dst_port);
367 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
374 create_bypass_for_fwd (snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
377 nat_ed_ses_key_t key;
378 clib_bihash_kv_16_8_t kv, value;
381 snat_session_t *s = 0;
382 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
383 f64 now = vlib_time_now (sm->vlib_main);
385 if (ip->protocol == IP_PROTOCOL_ICMP)
387 if (icmp_get_ed_key (ip, &key))
390 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
392 udp = ip4_next_header (ip);
393 key.r_addr = ip->src_address;
394 key.l_addr = ip->dst_address;
395 key.proto = ip->protocol;
396 key.l_port = udp->dst_port;
397 key.r_port = udp->src_port;
401 key.r_addr = ip->src_address;
402 key.l_addr = ip->dst_address;
403 key.proto = ip->protocol;
404 key.l_port = key.r_port = 0;
407 kv.key[0] = key.as_u64[0];
408 kv.key[1] = key.as_u64[1];
410 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
412 s = pool_elt_at_index (tsm->sessions, value.value);
416 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
419 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
423 nat_log_warn ("create NAT user failed");
427 s = nat_ed_session_alloc (sm, u, thread_index, now);
430 nat44_delete_user_with_no_session (sm, u, thread_index);
431 nat_log_warn ("create NAT session failed");
435 s->ext_host_addr = key.r_addr;
436 s->ext_host_port = key.r_port;
437 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
438 s->out2in.addr = key.l_addr;
439 s->out2in.port = key.l_port;
440 s->out2in.protocol = ip_proto_to_snat_proto (key.proto);
441 s->out2in.fib_index = 0;
442 s->in2out = s->out2in;
443 user_session_increment (sm, u, 0);
445 kv.value = s - tsm->sessions;
446 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
447 nat_log_notice ("in2out_ed key add failed");
450 if (ip->protocol == IP_PROTOCOL_TCP)
452 tcp_header_t *tcp = ip4_next_header (ip);
453 if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
458 nat44_session_update_counters (s, now, 0);
459 /* Per-user LRU list maintenance */
460 nat44_session_update_lru (sm, s, thread_index);
464 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
465 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
466 u8 * p_proto, snat_session_key_t * p_value,
467 u8 * p_dont_translate, void *d, void *e)
469 u32 next = ~0, sw_if_index, rx_fib_index;
470 icmp46_header_t *icmp;
471 nat_ed_ses_key_t key;
472 clib_bihash_kv_16_8_t kv, value;
473 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
474 snat_session_t *s = 0;
475 u8 dont_translate = 0, is_addr_only, identity_nat;
476 snat_session_key_t e_key, l_key;
478 icmp = (icmp46_header_t *) ip4_next_header (ip);
479 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
480 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
482 if (icmp_get_ed_key (ip, &key))
484 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
485 next = NAT44_ED_OUT2IN_NEXT_DROP;
488 key.fib_index = rx_fib_index;
489 kv.key[0] = key.as_u64[0];
490 kv.key[1] = key.as_u64[1];
492 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
494 /* Try to match static mapping */
495 e_key.addr = ip->dst_address;
496 e_key.port = key.l_port;
497 e_key.protocol = ip_proto_to_snat_proto (key.proto);
498 e_key.fib_index = rx_fib_index;
499 if (snat_static_mapping_match
500 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
502 if (!sm->forwarding_enabled)
504 /* Don't NAT packet aimed at the intfc address */
505 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
506 ip->dst_address.as_u32)))
511 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
512 next = NAT44_ED_OUT2IN_NEXT_DROP;
518 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
519 thread_index, rx_fib_index))
521 next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
524 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
529 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
530 (icmp->type != ICMP4_echo_request || !is_addr_only)))
532 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
533 next = NAT44_ED_OUT2IN_NEXT_DROP;
537 if (PREDICT_FALSE (identity_nat))
543 /* Create session initiated by host from external network */
544 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
551 next = NAT44_ED_OUT2IN_NEXT_DROP;
557 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
558 icmp->type != ICMP4_echo_request &&
559 !icmp_is_error_message (icmp)))
561 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
562 next = NAT44_ED_OUT2IN_NEXT_DROP;
566 s = pool_elt_at_index (tsm->sessions, value.value);
569 *p_proto = ip_proto_to_snat_proto (key.proto);
572 *p_value = s->in2out;
573 *p_dont_translate = dont_translate;
575 *(snat_session_t **) d = s;
579 static snat_session_t *
580 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
586 vlib_main_t * vm, vlib_node_runtime_t * node)
588 clib_bihash_kv_8_8_t kv, value;
589 clib_bihash_kv_16_8_t s_kv, s_value;
590 snat_static_mapping_t *m;
591 u32 old_addr, new_addr;
594 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
597 old_addr = ip->dst_address.as_u32;
599 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
602 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
604 s = pool_elt_at_index (tsm->sessions, s_value.value);
605 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
609 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
611 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
612 nat_log_notice ("maximum sessions exceeded");
616 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
617 if (clib_bihash_search_8_8
618 (&sm->static_mapping_by_external, &kv, &value))
620 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
624 m = pool_elt_at_index (sm->static_mappings, value.value);
626 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
628 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
632 nat_log_warn ("create NAT user failed");
636 /* Create a new session */
637 s = nat_ed_session_alloc (sm, u, thread_index, now);
640 nat44_delete_user_with_no_session (sm, u, thread_index);
641 nat_log_warn ("create NAT session failed");
645 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
646 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
647 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
648 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
649 s->out2in.addr.as_u32 = old_addr;
650 s->out2in.fib_index = rx_fib_index;
651 s->in2out.addr.as_u32 = new_addr;
652 s->in2out.fib_index = m->fib_index;
653 s->in2out.port = s->out2in.port = ip->protocol;
654 user_session_increment (sm, u, 1);
656 /* Add to lookup tables */
657 s_kv.value = s - tsm->sessions;
658 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
659 nat_log_notice ("out2in key add failed");
661 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
663 s_kv.value = s - tsm->sessions;
664 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
665 nat_log_notice ("in2out key add failed");
668 /* Update IP checksum */
670 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
671 ip->checksum = ip_csum_fold (sum);
673 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
676 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_node_fn_inline (vlib_main_t * vm,
685 vlib_node_runtime_t * node,
686 vlib_frame_t * frame, int is_slow_path)
688 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
689 nat44_ed_out2in_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];
695 stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
696 nat44_ed_out2in_node.index;
698 from = vlib_frame_vector_args (frame);
699 n_left_from = frame->n_vectors;
700 next_index = node->cached_next_index;
702 while (n_left_from > 0)
706 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
708 while (n_left_from >= 4 && n_left_to_next >= 2)
711 vlib_buffer_t *b0, *b1;
712 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
714 u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
716 u16 old_port0, new_port0, old_port1, new_port1;
717 ip4_header_t *ip0, *ip1;
718 udp_header_t *udp0, *udp1;
719 tcp_header_t *tcp0, *tcp1;
720 icmp46_header_t *icmp0, *icmp1;
721 snat_session_t *s0 = 0, *s1 = 0;
722 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
723 ip_csum_t sum0, sum1;
724 snat_session_key_t e_key0, l_key0, e_key1, l_key1;
725 lb_nat_type_t lb_nat0, lb_nat1;
726 twice_nat_type_t twice_nat0, twice_nat1;
727 u8 identity_nat0, identity_nat1;
729 /* Prefetch next iteration. */
731 vlib_buffer_t *p2, *p3;
733 p2 = vlib_get_buffer (vm, from[2]);
734 p3 = vlib_get_buffer (vm, from[3]);
736 vlib_prefetch_buffer_header (p2, LOAD);
737 vlib_prefetch_buffer_header (p3, LOAD);
739 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
740 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
743 /* speculatively enqueue b0 and b1 to the current next frame */
744 to_next[0] = bi0 = from[0];
745 to_next[1] = bi1 = from[1];
751 b0 = vlib_get_buffer (vm, bi0);
752 b1 = vlib_get_buffer (vm, bi1);
754 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
755 vnet_buffer (b0)->snat.flags = 0;
756 ip0 = vlib_buffer_get_current (b0);
758 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
760 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
763 if (PREDICT_FALSE (ip0->ttl == 1))
765 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
766 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
767 ICMP4_time_exceeded_ttl_exceeded_in_transit,
769 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
773 udp0 = ip4_next_header (ip0);
774 tcp0 = (tcp_header_t *) udp0;
775 icmp0 = (icmp46_header_t *) udp0;
776 proto0 = ip_proto_to_snat_proto (ip0->protocol);
780 if (PREDICT_FALSE (proto0 == ~0))
783 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
784 thread_index, now, vm,
786 if (!sm->forwarding_enabled)
789 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
794 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
796 next0 = icmp_out2in_ed_slow_path
797 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
798 next0, now, thread_index, &s0);
804 if (PREDICT_FALSE (proto0 == ~0))
806 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
810 if (ip4_is_fragment (ip0))
812 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
816 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
818 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
823 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
824 ip0->protocol, rx_fib_index0, udp0->dst_port,
827 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
831 /* Try to match static mapping by external address and port,
832 destination address and port in packet */
833 e_key0.addr = ip0->dst_address;
834 e_key0.port = udp0->dst_port;
835 e_key0.protocol = proto0;
836 e_key0.fib_index = rx_fib_index0;
837 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
838 &twice_nat0, &lb_nat0,
843 * Send DHCP packets to the ipv4 stack, or we won't
844 * be able to use dhcp client on the outside interface
846 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
847 && (udp0->dst_port ==
849 (UDP_DST_PORT_dhcp_to_client))))
851 vnet_feature_next (&next0, b0);
855 if (!sm->forwarding_enabled)
858 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
859 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
863 if (next_src_nat (sm, ip0, ip0->protocol,
864 udp0->src_port, udp0->dst_port,
865 thread_index, rx_fib_index0))
867 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
870 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
876 if (PREDICT_FALSE (identity_nat0))
879 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
881 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
882 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
886 /* Create session initiated by host from external network */
887 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
895 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
901 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
907 s0 = pool_elt_at_index (tsm->sessions, value0.value);
910 old_addr0 = ip0->dst_address.as_u32;
911 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
912 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
914 sum0 = ip0->checksum;
915 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
917 if (PREDICT_FALSE (is_twice_nat_session (s0)))
918 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
919 s0->ext_host_nat_addr.as_u32, ip4_header_t,
921 ip0->checksum = ip_csum_fold (sum0);
923 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
925 old_port0 = tcp0->dst_port;
926 new_port0 = tcp0->dst_port = s0->in2out.port;
928 sum0 = tcp0->checksum;
929 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
931 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
933 if (is_twice_nat_session (s0))
935 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
936 s0->ext_host_nat_addr.as_u32,
937 ip4_header_t, dst_address);
938 sum0 = ip_csum_update (sum0, tcp0->src_port,
939 s0->ext_host_nat_port, ip4_header_t,
941 tcp0->src_port = s0->ext_host_nat_port;
942 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
944 tcp0->checksum = ip_csum_fold (sum0);
945 if (nat44_set_tcp_session_state_o2i
946 (sm, s0, tcp0, thread_index))
951 udp0->dst_port = s0->in2out.port;
952 if (is_twice_nat_session (s0))
954 udp0->src_port = s0->ext_host_nat_port;
955 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
961 nat44_session_update_counters (s0, now,
962 vlib_buffer_length_in_chain (vm,
964 /* Per-user LRU list maintenance */
965 nat44_session_update_lru (sm, s0, thread_index);
968 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
969 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
971 nat44_ed_out2in_trace_t *t =
972 vlib_add_trace (vm, node, b0, sizeof (*t));
973 t->is_slow_path = is_slow_path;
974 t->sw_if_index = sw_if_index0;
975 t->next_index = next0;
976 t->session_index = ~0;
978 t->session_index = s0 - tsm->sessions;
981 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
983 next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
984 vnet_buffer (b1)->snat.flags = 0;
985 ip1 = vlib_buffer_get_current (b1);
987 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
989 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
992 if (PREDICT_FALSE (ip1->ttl == 1))
994 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
995 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
996 ICMP4_time_exceeded_ttl_exceeded_in_transit,
998 next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1002 udp1 = ip4_next_header (ip1);
1003 tcp1 = (tcp_header_t *) udp1;
1004 icmp1 = (icmp46_header_t *) udp1;
1005 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1009 if (PREDICT_FALSE (proto1 == ~0))
1012 nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1013 thread_index, now, vm,
1015 if (!sm->forwarding_enabled)
1018 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1023 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1025 next1 = icmp_out2in_ed_slow_path
1026 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1027 next1, now, thread_index, &s1);
1033 if (PREDICT_FALSE (proto1 == ~0))
1035 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1039 if (ip4_is_fragment (ip1))
1041 next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1045 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1047 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1052 make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1053 ip1->protocol, rx_fib_index1, udp1->dst_port,
1056 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1060 /* Try to match static mapping by external address and port,
1061 destination address and port in packet */
1062 e_key1.addr = ip1->dst_address;
1063 e_key1.port = udp1->dst_port;
1064 e_key1.protocol = proto1;
1065 e_key1.fib_index = rx_fib_index1;
1066 if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1067 &twice_nat1, &lb_nat1,
1072 * Send DHCP packets to the ipv4 stack, or we won't
1073 * be able to use dhcp client on the outside interface
1075 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1076 && (udp1->dst_port ==
1077 clib_host_to_net_u16
1078 (UDP_DST_PORT_dhcp_to_client))))
1080 vnet_feature_next (&next1, b1);
1084 if (!sm->forwarding_enabled)
1087 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1088 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1092 if (next_src_nat (sm, ip1, ip1->protocol,
1093 udp1->src_port, udp1->dst_port,
1094 thread_index, rx_fib_index1))
1096 next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1099 create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1105 if (PREDICT_FALSE (identity_nat1))
1108 if ((proto1 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp1))
1110 b1->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1111 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1115 /* Create session initiated by host from external network */
1116 s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1124 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1130 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1136 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1139 old_addr1 = ip1->dst_address.as_u32;
1140 new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1141 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1143 sum1 = ip1->checksum;
1144 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1146 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1147 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1148 s1->ext_host_nat_addr.as_u32, ip4_header_t,
1150 ip1->checksum = ip_csum_fold (sum1);
1152 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1154 old_port1 = tcp1->dst_port;
1155 new_port1 = tcp1->dst_port = s1->in2out.port;
1157 sum1 = tcp1->checksum;
1158 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1160 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1162 if (is_twice_nat_session (s1))
1164 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1165 s1->ext_host_nat_addr.as_u32,
1166 ip4_header_t, dst_address);
1167 sum1 = ip_csum_update (sum1, tcp1->src_port,
1168 s1->ext_host_nat_port, ip4_header_t,
1170 tcp1->src_port = s1->ext_host_nat_port;
1171 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1173 tcp1->checksum = ip_csum_fold (sum1);
1174 if (nat44_set_tcp_session_state_o2i
1175 (sm, s1, tcp1, thread_index))
1180 udp1->dst_port = s1->in2out.port;
1181 if (is_twice_nat_session (s1))
1183 udp1->src_port = s1->ext_host_nat_port;
1184 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1190 nat44_session_update_counters (s1, now,
1191 vlib_buffer_length_in_chain (vm,
1193 /* Per-user LRU list maintenance */
1194 nat44_session_update_lru (sm, s1, thread_index);
1197 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1198 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1200 nat44_ed_out2in_trace_t *t =
1201 vlib_add_trace (vm, node, b1, sizeof (*t));
1202 t->is_slow_path = is_slow_path;
1203 t->sw_if_index = sw_if_index1;
1204 t->next_index = next1;
1205 t->session_index = ~0;
1207 t->session_index = s1 - tsm->sessions;
1210 pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
1212 /* verify speculative enqueues, maybe switch current next frame */
1213 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1214 to_next, n_left_to_next,
1215 bi0, bi1, next0, next1);
1218 while (n_left_from > 0 && n_left_to_next > 0)
1222 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1224 u16 old_port0, new_port0;
1228 icmp46_header_t *icmp0;
1229 snat_session_t *s0 = 0;
1230 clib_bihash_kv_16_8_t kv0, value0;
1232 snat_session_key_t e_key0, l_key0;
1233 lb_nat_type_t lb_nat0;
1234 twice_nat_type_t twice_nat0;
1237 /* speculatively enqueue b0 to the current next frame */
1243 n_left_to_next -= 1;
1245 b0 = vlib_get_buffer (vm, bi0);
1246 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1247 vnet_buffer (b0)->snat.flags = 0;
1248 ip0 = vlib_buffer_get_current (b0);
1250 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1252 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1255 if (PREDICT_FALSE (ip0->ttl == 1))
1257 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1258 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1259 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1261 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1265 udp0 = ip4_next_header (ip0);
1266 tcp0 = (tcp_header_t *) udp0;
1267 icmp0 = (icmp46_header_t *) udp0;
1268 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1272 if (PREDICT_FALSE (proto0 == ~0))
1275 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1276 thread_index, now, vm,
1278 if (!sm->forwarding_enabled)
1281 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1286 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1288 next0 = icmp_out2in_ed_slow_path
1289 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1290 next0, now, thread_index, &s0);
1296 if (PREDICT_FALSE (proto0 == ~0))
1298 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1302 if (ip4_is_fragment (ip0))
1304 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1308 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1310 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1315 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1316 ip0->protocol, rx_fib_index0, udp0->dst_port,
1319 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1323 /* Try to match static mapping by external address and port,
1324 destination address and port in packet */
1325 e_key0.addr = ip0->dst_address;
1326 e_key0.port = udp0->dst_port;
1327 e_key0.protocol = proto0;
1328 e_key0.fib_index = rx_fib_index0;
1329 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1330 &twice_nat0, &lb_nat0,
1335 * Send DHCP packets to the ipv4 stack, or we won't
1336 * be able to use dhcp client on the outside interface
1338 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1339 && (udp0->dst_port ==
1340 clib_host_to_net_u16
1341 (UDP_DST_PORT_dhcp_to_client))))
1343 vnet_feature_next (&next0, b0);
1347 if (!sm->forwarding_enabled)
1350 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1351 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1355 if (next_src_nat (sm, ip0, ip0->protocol,
1356 udp0->src_port, udp0->dst_port,
1357 thread_index, rx_fib_index0))
1359 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1362 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1368 if (PREDICT_FALSE (identity_nat0))
1371 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1373 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1374 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1378 /* Create session initiated by host from external network */
1379 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1387 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1393 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1399 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1402 old_addr0 = ip0->dst_address.as_u32;
1403 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1404 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1406 sum0 = ip0->checksum;
1407 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1409 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1410 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1411 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1413 ip0->checksum = ip_csum_fold (sum0);
1415 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1417 old_port0 = tcp0->dst_port;
1418 new_port0 = tcp0->dst_port = s0->in2out.port;
1420 sum0 = tcp0->checksum;
1421 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1423 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1425 if (is_twice_nat_session (s0))
1427 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1428 s0->ext_host_nat_addr.as_u32,
1429 ip4_header_t, dst_address);
1430 sum0 = ip_csum_update (sum0, tcp0->src_port,
1431 s0->ext_host_nat_port, ip4_header_t,
1433 tcp0->src_port = s0->ext_host_nat_port;
1434 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1436 tcp0->checksum = ip_csum_fold (sum0);
1437 if (nat44_set_tcp_session_state_o2i
1438 (sm, s0, tcp0, thread_index))
1443 udp0->dst_port = s0->in2out.port;
1444 if (is_twice_nat_session (s0))
1446 udp0->src_port = s0->ext_host_nat_port;
1447 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1453 nat44_session_update_counters (s0, now,
1454 vlib_buffer_length_in_chain (vm,
1456 /* Per-user LRU list maintenance */
1457 nat44_session_update_lru (sm, s0, thread_index);
1460 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1461 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1463 nat44_ed_out2in_trace_t *t =
1464 vlib_add_trace (vm, node, b0, sizeof (*t));
1465 t->is_slow_path = is_slow_path;
1466 t->sw_if_index = sw_if_index0;
1467 t->next_index = next0;
1468 t->session_index = ~0;
1470 t->session_index = s0 - tsm->sessions;
1473 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1474 /* verify speculative enqueue, maybe switch current next frame */
1475 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1476 to_next, n_left_to_next,
1480 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1483 vlib_node_increment_counter (vm, stats_node_index,
1484 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1486 return frame->n_vectors;
1490 nat44_ed_out2in_fast_path_fn (vlib_main_t * vm,
1491 vlib_node_runtime_t * node,
1492 vlib_frame_t * frame)
1494 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1498 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1499 .function = nat44_ed_out2in_fast_path_fn,
1500 .name = "nat44-ed-out2in",
1501 .vector_size = sizeof (u32),
1502 .format_trace = format_nat44_ed_out2in_trace,
1503 .type = VLIB_NODE_TYPE_INTERNAL,
1504 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1505 .error_strings = nat_out2in_ed_error_strings,
1506 .runtime_data_bytes = sizeof (snat_runtime_t),
1507 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1509 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1510 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1511 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1512 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1513 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1514 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1519 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_node,
1520 nat44_ed_out2in_fast_path_fn);
1523 nat44_ed_out2in_slow_path_fn (vlib_main_t * vm,
1524 vlib_node_runtime_t * node,
1525 vlib_frame_t * frame)
1527 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1531 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1532 .function = nat44_ed_out2in_slow_path_fn,
1533 .name = "nat44-ed-out2in-slowpath",
1534 .vector_size = sizeof (u32),
1535 .format_trace = format_nat44_ed_out2in_trace,
1536 .type = VLIB_NODE_TYPE_INTERNAL,
1537 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1538 .error_strings = nat_out2in_ed_error_strings,
1539 .runtime_data_bytes = sizeof (snat_runtime_t),
1540 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1542 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1543 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1544 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1545 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1546 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1547 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1552 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_slowpath_node,
1553 nat44_ed_out2in_slow_path_fn);
1556 nat44_ed_out2in_reass_node_fn (vlib_main_t * vm,
1557 vlib_node_runtime_t * node,
1558 vlib_frame_t * frame)
1560 u32 n_left_from, *from, *to_next;
1561 nat44_ed_out2in_next_t next_index;
1562 u32 pkts_processed = 0;
1563 snat_main_t *sm = &snat_main;
1564 f64 now = vlib_time_now (vm);
1565 u32 thread_index = vm->thread_index;
1566 snat_main_per_thread_data_t *per_thread_data =
1567 &sm->per_thread_data[thread_index];
1568 u32 *fragments_to_drop = 0;
1569 u32 *fragments_to_loopback = 0;
1571 from = vlib_frame_vector_args (frame);
1572 n_left_from = frame->n_vectors;
1573 next_index = node->cached_next_index;
1575 while (n_left_from > 0)
1579 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1581 while (n_left_from > 0 && n_left_to_next > 0)
1583 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1588 nat_reass_ip4_t *reass0;
1591 icmp46_header_t *icmp0;
1592 clib_bihash_kv_16_8_t kv0, value0;
1593 snat_session_t *s0 = 0;
1594 u16 old_port0, new_port0;
1596 snat_session_key_t e_key0, l_key0;
1598 twice_nat_type_t twice_nat0;
1601 /* speculatively enqueue b0 to the current next frame */
1607 n_left_to_next -= 1;
1609 b0 = vlib_get_buffer (vm, bi0);
1610 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1612 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1614 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1617 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1619 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1620 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1624 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1625 udp0 = ip4_next_header (ip0);
1626 tcp0 = (tcp_header_t *) udp0;
1627 icmp0 = (icmp46_header_t *) udp0;
1628 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1630 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1634 1, &fragments_to_drop);
1636 if (PREDICT_FALSE (!reass0))
1638 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1639 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1640 nat_log_notice ("maximum reassemblies exceeded");
1644 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1646 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1648 next0 = icmp_out2in_ed_slow_path
1649 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1650 next0, now, thread_index, &s0);
1652 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1655 reass0->sess_index = s0 - per_thread_data->sessions;
1657 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1658 reass0->thread_index = thread_index;
1659 nat_ip4_reass_get_frags (reass0,
1660 &fragments_to_loopback);
1666 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1667 ip0->protocol, rx_fib_index0, udp0->dst_port,
1670 if (clib_bihash_search_16_8
1671 (&per_thread_data->out2in_ed, &kv0, &value0))
1673 /* Try to match static mapping by external address and port,
1674 destination address and port in packet */
1675 e_key0.addr = ip0->dst_address;
1676 e_key0.port = udp0->dst_port;
1677 e_key0.protocol = proto0;
1678 e_key0.fib_index = rx_fib_index0;
1679 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1680 &twice_nat0, &lb0, 0,
1684 * Send DHCP packets to the ipv4 stack, or we won't
1685 * be able to use dhcp client on the outside interface
1687 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1690 clib_host_to_net_u16
1691 (UDP_DST_PORT_dhcp_to_client))))
1693 vnet_feature_next (&next0, b0);
1697 if (!sm->forwarding_enabled)
1700 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1701 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1705 if (next_src_nat (sm, ip0, ip0->protocol,
1706 udp0->src_port, udp0->dst_port,
1707 thread_index, rx_fib_index0))
1709 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1712 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1714 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1715 nat_ip4_reass_get_frags (reass0,
1716 &fragments_to_loopback);
1721 if (PREDICT_FALSE (identity_nat0))
1723 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1727 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1729 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1730 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1734 /* Create session initiated by host from external network */
1735 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1743 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1744 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1747 reass0->sess_index = s0 - per_thread_data->sessions;
1748 reass0->thread_index = thread_index;
1752 s0 = pool_elt_at_index (per_thread_data->sessions,
1754 reass0->sess_index = value0.value;
1756 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1760 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1762 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1764 if (nat_ip4_reass_add_fragment
1765 (reass0, bi0, &fragments_to_drop))
1767 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1769 ("maximum fragments per reassembly exceeded");
1770 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1776 s0 = pool_elt_at_index (per_thread_data->sessions,
1777 reass0->sess_index);
1780 old_addr0 = ip0->dst_address.as_u32;
1781 ip0->dst_address = s0->in2out.addr;
1782 new_addr0 = ip0->dst_address.as_u32;
1783 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1785 sum0 = ip0->checksum;
1786 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1788 dst_address /* changed member */ );
1789 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1790 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1791 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1793 ip0->checksum = ip_csum_fold (sum0);
1795 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1797 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1799 old_port0 = tcp0->dst_port;
1800 tcp0->dst_port = s0->in2out.port;
1801 new_port0 = tcp0->dst_port;
1803 sum0 = tcp0->checksum;
1804 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1806 dst_address /* changed member */ );
1808 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1809 ip4_header_t /* cheat */ ,
1810 length /* changed member */ );
1811 if (is_twice_nat_session (s0))
1813 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1814 s0->ext_host_nat_addr.as_u32,
1815 ip4_header_t, dst_address);
1816 sum0 = ip_csum_update (sum0, tcp0->src_port,
1817 s0->ext_host_nat_port,
1818 ip4_header_t, length);
1819 tcp0->src_port = s0->ext_host_nat_port;
1820 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1822 tcp0->checksum = ip_csum_fold (sum0);
1826 old_port0 = udp0->dst_port;
1827 udp0->dst_port = s0->in2out.port;
1828 if (is_twice_nat_session (s0))
1830 udp0->src_port = s0->ext_host_nat_port;
1831 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1838 nat44_session_update_counters (s0, now,
1839 vlib_buffer_length_in_chain (vm,
1841 /* Per-user LRU list maintenance */
1842 nat44_session_update_lru (sm, s0, thread_index);
1845 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1846 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1848 nat44_reass_trace_t *t =
1849 vlib_add_trace (vm, node, b0, sizeof (*t));
1850 t->cached = cached0;
1851 t->sw_if_index = sw_if_index0;
1852 t->next_index = next0;
1862 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1864 /* verify speculative enqueue, maybe switch current next frame */
1865 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1866 to_next, n_left_to_next,
1870 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1872 from = vlib_frame_vector_args (frame);
1873 u32 len = vec_len (fragments_to_loopback);
1874 if (len <= VLIB_FRAME_SIZE)
1876 clib_memcpy_fast (from, fragments_to_loopback,
1877 sizeof (u32) * len);
1879 vec_reset_length (fragments_to_loopback);
1883 clib_memcpy_fast (from, fragments_to_loopback +
1884 (len - VLIB_FRAME_SIZE),
1885 sizeof (u32) * VLIB_FRAME_SIZE);
1886 n_left_from = VLIB_FRAME_SIZE;
1887 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1892 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1895 vlib_node_increment_counter (vm, nat44_ed_out2in_reass_node.index,
1896 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1899 nat_send_all_to_node (vm, fragments_to_drop, node,
1900 &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1901 NAT44_ED_OUT2IN_NEXT_DROP);
1903 vec_free (fragments_to_drop);
1904 vec_free (fragments_to_loopback);
1905 return frame->n_vectors;
1909 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1910 .function = nat44_ed_out2in_reass_node_fn,
1911 .name = "nat44-ed-out2in-reass",
1912 .vector_size = sizeof (u32),
1913 .format_trace = format_nat44_reass_trace,
1914 .type = VLIB_NODE_TYPE_INTERNAL,
1915 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1916 .error_strings = nat_out2in_ed_error_strings,
1917 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1919 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1920 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1921 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1922 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1923 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1924 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1929 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_reass_node,
1930 nat44_ed_out2in_reass_node_fn);
1933 * fd.io coding-style-patch-verification: ON
1936 * eval: (c-set-style "gnu")