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")
46 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
47 foreach_nat_out2in_ed_error
49 NAT_OUT2IN_ED_N_ERROR,
50 } nat_out2in_ed_error_t;
52 static char *nat_out2in_ed_error_strings[] = {
53 #define _(sym,string) string,
54 foreach_nat_out2in_ed_error
60 NAT44_ED_OUT2IN_NEXT_DROP,
61 NAT44_ED_OUT2IN_NEXT_LOOKUP,
62 NAT44_ED_OUT2IN_NEXT_ICMP_ERROR,
63 NAT44_ED_OUT2IN_NEXT_IN2OUT,
64 NAT44_ED_OUT2IN_NEXT_SLOW_PATH,
65 NAT44_ED_OUT2IN_NEXT_REASS,
66 NAT44_ED_OUT2IN_N_NEXT,
67 } nat44_ed_out2in_next_t;
75 } nat44_ed_out2in_trace_t;
77 vlib_node_registration_t nat44_ed_out2in_node;
78 vlib_node_registration_t nat44_ed_out2in_slowpath_node;
79 vlib_node_registration_t nat44_ed_out2in_reass_node;
82 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
84 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
85 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
90 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
91 "NAT44_OUT2IN_ED_FAST_PATH";
93 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
94 t->sw_if_index, t->next_index, t->session_index);
100 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
101 ip4_header_t * ip0, icmp46_header_t * icmp0,
102 u32 sw_if_index0, u32 rx_fib_index0,
103 vlib_node_runtime_t * node, u32 next0, f64 now,
104 u32 thread_index, snat_session_t ** p_s0)
106 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
107 next0, thread_index, p_s0, 0);
108 snat_session_t *s0 = *p_s0;
109 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP && s0))
112 nat44_session_update_counters (s0, now,
113 vlib_buffer_length_in_chain
114 (sm->vlib_main, b0));
115 /* Per-user LRU list maintenance */
116 nat44_session_update_lru (sm, s0, thread_index);
122 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
124 snat_main_t *sm = &snat_main;
125 nat44_is_idle_session_ctx_t *ctx = arg;
127 u64 sess_timeout_time;
128 nat_ed_ses_key_t ed_key;
129 clib_bihash_kv_16_8_t ed_kv;
132 snat_session_key_t key;
133 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
136 s = pool_elt_at_index (tsm->sessions, kv->value);
137 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
138 if (ctx->now >= sess_timeout_time)
140 ed_key.l_addr = s->in2out.addr;
141 ed_key.r_addr = s->ext_host_addr;
142 ed_key.fib_index = s->in2out.fib_index;
143 if (snat_is_unk_proto_session (s))
145 ed_key.proto = s->in2out.port;
151 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
152 ed_key.l_port = s->in2out.port;
153 ed_key.r_port = s->ext_host_port;
155 if (is_twice_nat_session (s))
157 ed_key.r_addr = s->ext_host_nat_addr;
158 ed_key.r_port = s->ext_host_nat_port;
160 ed_kv.key[0] = ed_key.as_u64[0];
161 ed_kv.key[1] = ed_key.as_u64[1];
162 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
163 nat_log_warn ("in2out_ed key del failed");
165 if (snat_is_unk_proto_session (s))
168 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
169 s->out2in.addr.as_u32,
173 s->in2out.fib_index);
175 if (is_twice_nat_session (s))
177 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
179 key.protocol = s->in2out.protocol;
180 key.port = s->ext_host_nat_port;
181 a = sm->twice_nat_addresses + i;
182 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
184 snat_free_outside_address_and_port (sm->twice_nat_addresses,
192 if (snat_is_session_static (s))
195 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
198 nat44_delete_session (sm, s, ctx->thread_index);
205 static snat_session_t *
206 create_session_for_static_mapping_ed (snat_main_t * sm,
208 snat_session_key_t l_key,
209 snat_session_key_t e_key,
210 vlib_node_runtime_t * node,
212 twice_nat_type_t twice_nat,
213 lb_nat_type_t lb_nat, f64 now)
219 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
220 clib_bihash_kv_16_8_t kv;
221 snat_session_key_t eh_key;
222 nat44_is_idle_session_ctx_t ctx;
224 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
226 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
227 nat_log_notice ("maximum sessions exceeded");
231 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
234 nat_log_warn ("create NAT user failed");
238 s = nat_ed_session_alloc (sm, u, thread_index, now);
241 nat44_delete_user_with_no_session (sm, u, thread_index);
242 nat_log_warn ("create NAT session failed");
246 ip = vlib_buffer_get_current (b);
247 udp = ip4_next_header (ip);
249 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
250 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
251 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
253 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
254 if (lb_nat == AFFINITY_LB_NAT)
255 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
256 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
259 s->in2out.protocol = s->out2in.protocol;
260 user_session_increment (sm, u, 1);
262 /* Add to lookup tables */
263 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
264 e_key.fib_index, e_key.port, s->ext_host_port);
265 kv.value = s - tsm->sessions;
267 ctx.thread_index = thread_index;
268 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
269 nat44_o2i_ed_is_idle_session_cb,
271 nat_log_notice ("out2in-ed key add failed");
273 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
274 ip->src_address.as_u32 == l_key.addr.as_u32))
276 eh_key.protocol = e_key.protocol;
277 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
278 thread_index, &eh_key,
280 tsm->snat_thread_index))
282 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
283 nat44_delete_session (sm, s, thread_index);
284 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
285 nat_log_notice ("out2in-ed key del failed");
288 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
289 s->ext_host_nat_port = eh_key.port;
290 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
291 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
292 l_key.fib_index, l_key.port, s->ext_host_nat_port);
296 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
297 l_key.fib_index, l_key.port, s->ext_host_port);
299 kv.value = s - tsm->sessions;
300 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
301 nat44_i2o_ed_is_idle_session_cb,
303 nat_log_notice ("in2out-ed key add failed");
308 static_always_inline int
309 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
311 icmp46_header_t *icmp0;
312 nat_ed_ses_key_t key0;
313 icmp_echo_header_t *echo0, *inner_echo0 = 0;
314 ip4_header_t *inner_ip0;
316 icmp46_header_t *inner_icmp0;
318 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
319 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
321 if (!icmp_is_error_message (icmp0))
323 key0.proto = IP_PROTOCOL_ICMP;
324 key0.l_addr = ip0->dst_address;
325 key0.r_addr = ip0->src_address;
326 key0.l_port = echo0->identifier;
331 inner_ip0 = (ip4_header_t *) (echo0 + 1);
332 l4_header = ip4_next_header (inner_ip0);
333 key0.proto = inner_ip0->protocol;
334 key0.l_addr = inner_ip0->src_address;
335 key0.r_addr = inner_ip0->dst_address;
336 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
338 case SNAT_PROTOCOL_ICMP:
339 inner_icmp0 = (icmp46_header_t *) l4_header;
340 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
341 key0.l_port = inner_echo0->identifier;
344 case SNAT_PROTOCOL_UDP:
345 case SNAT_PROTOCOL_TCP:
346 key0.l_port = ((tcp_udp_header_t *) l4_header)->src_port;
347 key0.r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
358 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
359 u16 dst_port, u32 thread_index, u32 rx_fib_index)
361 clib_bihash_kv_16_8_t kv, value;
362 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
364 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
365 rx_fib_index, src_port, dst_port);
366 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
373 create_bypass_for_fwd (snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
376 nat_ed_ses_key_t key;
377 clib_bihash_kv_16_8_t kv, value;
380 snat_session_t *s = 0;
381 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
382 f64 now = vlib_time_now (sm->vlib_main);
384 if (ip->protocol == IP_PROTOCOL_ICMP)
386 if (icmp_get_ed_key (ip, &key))
389 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
391 udp = ip4_next_header (ip);
392 key.r_addr = ip->src_address;
393 key.l_addr = ip->dst_address;
394 key.proto = ip->protocol;
395 key.l_port = udp->dst_port;
396 key.r_port = udp->src_port;
400 key.r_addr = ip->src_address;
401 key.l_addr = ip->dst_address;
402 key.proto = ip->protocol;
403 key.l_port = key.r_port = 0;
406 kv.key[0] = key.as_u64[0];
407 kv.key[1] = key.as_u64[1];
409 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
411 s = pool_elt_at_index (tsm->sessions, value.value);
415 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
418 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
422 nat_log_warn ("create NAT user failed");
426 s = nat_ed_session_alloc (sm, u, thread_index, now);
429 nat44_delete_user_with_no_session (sm, u, thread_index);
430 nat_log_warn ("create NAT session failed");
434 s->ext_host_addr = key.r_addr;
435 s->ext_host_port = key.r_port;
436 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
437 s->out2in.addr = key.l_addr;
438 s->out2in.port = key.l_port;
439 s->out2in.protocol = ip_proto_to_snat_proto (key.proto);
440 s->out2in.fib_index = 0;
441 s->in2out = s->out2in;
442 user_session_increment (sm, u, 0);
444 kv.value = s - tsm->sessions;
445 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
446 nat_log_notice ("in2out_ed key add failed");
449 if (ip->protocol == IP_PROTOCOL_TCP)
451 tcp_header_t *tcp = ip4_next_header (ip);
452 if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
457 nat44_session_update_counters (s, now, 0);
458 /* Per-user LRU list maintenance */
459 nat44_session_update_lru (sm, s, thread_index);
463 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
464 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
465 u8 * p_proto, snat_session_key_t * p_value,
466 u8 * p_dont_translate, void *d, void *e)
468 u32 next = ~0, sw_if_index, rx_fib_index;
469 icmp46_header_t *icmp;
470 nat_ed_ses_key_t key;
471 clib_bihash_kv_16_8_t kv, value;
472 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
473 snat_session_t *s = 0;
474 u8 dont_translate = 0, is_addr_only, identity_nat;
475 snat_session_key_t e_key, l_key;
477 icmp = (icmp46_header_t *) ip4_next_header (ip);
478 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
479 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
481 if (icmp_get_ed_key (ip, &key))
483 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
484 next = NAT44_ED_OUT2IN_NEXT_DROP;
487 key.fib_index = rx_fib_index;
488 kv.key[0] = key.as_u64[0];
489 kv.key[1] = key.as_u64[1];
491 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
493 /* Try to match static mapping */
494 e_key.addr = ip->dst_address;
495 e_key.port = key.l_port;
496 e_key.protocol = ip_proto_to_snat_proto (key.proto);
497 e_key.fib_index = rx_fib_index;
498 if (snat_static_mapping_match
499 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
501 if (!sm->forwarding_enabled)
503 /* Don't NAT packet aimed at the intfc address */
504 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
505 ip->dst_address.as_u32)))
510 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
511 next = NAT44_ED_OUT2IN_NEXT_DROP;
517 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
518 thread_index, rx_fib_index))
520 next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
523 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
528 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
529 (icmp->type != ICMP4_echo_request || !is_addr_only)))
531 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
532 next = NAT44_ED_OUT2IN_NEXT_DROP;
536 if (PREDICT_FALSE (identity_nat))
542 /* Create session initiated by host from external network */
543 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
550 next = NAT44_ED_OUT2IN_NEXT_DROP;
556 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
557 icmp->type != ICMP4_echo_request &&
558 !icmp_is_error_message (icmp)))
560 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
561 next = NAT44_ED_OUT2IN_NEXT_DROP;
565 s = pool_elt_at_index (tsm->sessions, value.value);
568 *p_proto = ip_proto_to_snat_proto (key.proto);
571 *p_value = s->in2out;
572 *p_dont_translate = dont_translate;
574 *(snat_session_t **) d = s;
578 static snat_session_t *
579 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
585 vlib_main_t * vm, vlib_node_runtime_t * node)
587 clib_bihash_kv_8_8_t kv, value;
588 clib_bihash_kv_16_8_t s_kv, s_value;
589 snat_static_mapping_t *m;
590 u32 old_addr, new_addr;
593 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
596 old_addr = ip->dst_address.as_u32;
598 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
601 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
603 s = pool_elt_at_index (tsm->sessions, s_value.value);
604 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
608 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
610 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
611 nat_log_notice ("maximum sessions exceeded");
615 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
616 if (clib_bihash_search_8_8
617 (&sm->static_mapping_by_external, &kv, &value))
619 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
623 m = pool_elt_at_index (sm->static_mappings, value.value);
625 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
627 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
631 nat_log_warn ("create NAT user failed");
635 /* Create a new session */
636 s = nat_ed_session_alloc (sm, u, thread_index, now);
639 nat44_delete_user_with_no_session (sm, u, thread_index);
640 nat_log_warn ("create NAT session failed");
644 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
645 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
646 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
647 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
648 s->out2in.addr.as_u32 = old_addr;
649 s->out2in.fib_index = rx_fib_index;
650 s->in2out.addr.as_u32 = new_addr;
651 s->in2out.fib_index = m->fib_index;
652 s->in2out.port = s->out2in.port = ip->protocol;
653 user_session_increment (sm, u, 1);
655 /* Add to lookup tables */
656 s_kv.value = s - tsm->sessions;
657 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
658 nat_log_notice ("out2in key add failed");
660 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
662 s_kv.value = s - tsm->sessions;
663 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
664 nat_log_notice ("in2out key add failed");
667 /* Update IP checksum */
669 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
670 ip->checksum = ip_csum_fold (sum);
672 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
675 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
676 /* Per-user LRU list maintenance */
677 nat44_session_update_lru (sm, s, thread_index);
683 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
684 vlib_node_runtime_t * node,
685 vlib_frame_t * frame, int is_slow_path)
687 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
688 nat44_ed_out2in_next_t next_index;
689 snat_main_t *sm = &snat_main;
690 f64 now = vlib_time_now (vm);
691 u32 thread_index = vm->thread_index;
692 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
694 stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
695 nat44_ed_out2in_node.index;
697 from = vlib_frame_vector_args (frame);
698 n_left_from = frame->n_vectors;
699 next_index = node->cached_next_index;
701 while (n_left_from > 0)
705 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
707 while (n_left_from >= 4 && n_left_to_next >= 2)
710 vlib_buffer_t *b0, *b1;
711 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
713 u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
715 u16 old_port0, new_port0, old_port1, new_port1;
716 ip4_header_t *ip0, *ip1;
717 udp_header_t *udp0, *udp1;
718 tcp_header_t *tcp0, *tcp1;
719 icmp46_header_t *icmp0, *icmp1;
720 snat_session_t *s0 = 0, *s1 = 0;
721 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
722 ip_csum_t sum0, sum1;
723 snat_session_key_t e_key0, l_key0, e_key1, l_key1;
724 lb_nat_type_t lb_nat0, lb_nat1;
725 twice_nat_type_t twice_nat0, twice_nat1;
726 u8 identity_nat0, identity_nat1;
728 /* Prefetch next iteration. */
730 vlib_buffer_t *p2, *p3;
732 p2 = vlib_get_buffer (vm, from[2]);
733 p3 = vlib_get_buffer (vm, from[3]);
735 vlib_prefetch_buffer_header (p2, LOAD);
736 vlib_prefetch_buffer_header (p3, LOAD);
738 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
739 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
742 /* speculatively enqueue b0 and b1 to the current next frame */
743 to_next[0] = bi0 = from[0];
744 to_next[1] = bi1 = from[1];
750 b0 = vlib_get_buffer (vm, bi0);
751 b1 = vlib_get_buffer (vm, bi1);
753 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
754 vnet_buffer (b0)->snat.flags = 0;
755 ip0 = vlib_buffer_get_current (b0);
757 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
759 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
762 if (PREDICT_FALSE (ip0->ttl == 1))
764 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
765 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
766 ICMP4_time_exceeded_ttl_exceeded_in_transit,
768 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
772 udp0 = ip4_next_header (ip0);
773 tcp0 = (tcp_header_t *) udp0;
774 icmp0 = (icmp46_header_t *) udp0;
775 proto0 = ip_proto_to_snat_proto (ip0->protocol);
779 if (PREDICT_FALSE (proto0 == ~0))
782 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
783 thread_index, now, vm,
785 if (!sm->forwarding_enabled)
788 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
793 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
795 next0 = icmp_out2in_ed_slow_path
796 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
797 next0, now, thread_index, &s0);
803 if (PREDICT_FALSE (proto0 == ~0))
805 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
809 if (ip4_is_fragment (ip0))
811 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
815 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
817 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
822 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
823 ip0->protocol, rx_fib_index0, udp0->dst_port,
826 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
830 /* Try to match static mapping by external address and port,
831 destination address and port in packet */
832 e_key0.addr = ip0->dst_address;
833 e_key0.port = udp0->dst_port;
834 e_key0.protocol = proto0;
835 e_key0.fib_index = rx_fib_index0;
836 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
837 &twice_nat0, &lb_nat0,
842 * Send DHCP packets to the ipv4 stack, or we won't
843 * be able to use dhcp client on the outside interface
845 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
846 && (udp0->dst_port ==
848 (UDP_DST_PORT_dhcp_to_client))))
850 vnet_feature_next (&next0, b0);
854 if (!sm->forwarding_enabled)
857 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
858 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
862 if (next_src_nat (sm, ip0, ip0->protocol,
863 udp0->src_port, udp0->dst_port,
864 thread_index, rx_fib_index0))
866 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
869 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
875 if (PREDICT_FALSE (identity_nat0))
878 /* Create session initiated by host from external network */
879 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
887 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
893 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
899 s0 = pool_elt_at_index (tsm->sessions, value0.value);
902 old_addr0 = ip0->dst_address.as_u32;
903 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
904 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
906 sum0 = ip0->checksum;
907 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
909 if (PREDICT_FALSE (is_twice_nat_session (s0)))
910 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
911 s0->ext_host_nat_addr.as_u32, ip4_header_t,
913 ip0->checksum = ip_csum_fold (sum0);
915 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
917 old_port0 = tcp0->dst_port;
918 new_port0 = tcp0->dst_port = s0->in2out.port;
920 sum0 = tcp0->checksum;
921 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
923 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
925 if (is_twice_nat_session (s0))
927 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
928 s0->ext_host_nat_addr.as_u32,
929 ip4_header_t, dst_address);
930 sum0 = ip_csum_update (sum0, tcp0->src_port,
931 s0->ext_host_nat_port, ip4_header_t,
933 tcp0->src_port = s0->ext_host_nat_port;
934 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
936 tcp0->checksum = ip_csum_fold (sum0);
937 if (nat44_set_tcp_session_state_o2i
938 (sm, s0, tcp0, thread_index))
943 udp0->dst_port = s0->in2out.port;
944 if (is_twice_nat_session (s0))
946 udp0->src_port = s0->ext_host_nat_port;
947 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
953 nat44_session_update_counters (s0, now,
954 vlib_buffer_length_in_chain (vm,
956 /* Per-user LRU list maintenance */
957 nat44_session_update_lru (sm, s0, thread_index);
960 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
961 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
963 nat44_ed_out2in_trace_t *t =
964 vlib_add_trace (vm, node, b0, sizeof (*t));
965 t->is_slow_path = is_slow_path;
966 t->sw_if_index = sw_if_index0;
967 t->next_index = next0;
968 t->session_index = ~0;
970 t->session_index = s0 - tsm->sessions;
973 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
975 next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
976 vnet_buffer (b1)->snat.flags = 0;
977 ip1 = vlib_buffer_get_current (b1);
979 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
981 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
984 if (PREDICT_FALSE (ip1->ttl == 1))
986 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
987 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
988 ICMP4_time_exceeded_ttl_exceeded_in_transit,
990 next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
994 udp1 = ip4_next_header (ip1);
995 tcp1 = (tcp_header_t *) udp1;
996 icmp1 = (icmp46_header_t *) udp1;
997 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1001 if (PREDICT_FALSE (proto1 == ~0))
1004 nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1005 thread_index, now, vm,
1007 if (!sm->forwarding_enabled)
1010 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1015 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1017 next1 = icmp_out2in_ed_slow_path
1018 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1019 next1, now, thread_index, &s1);
1025 if (PREDICT_FALSE (proto1 == ~0))
1027 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1031 if (ip4_is_fragment (ip1))
1033 next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1037 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1039 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1044 make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1045 ip1->protocol, rx_fib_index1, udp1->dst_port,
1048 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1052 /* Try to match static mapping by external address and port,
1053 destination address and port in packet */
1054 e_key1.addr = ip1->dst_address;
1055 e_key1.port = udp1->dst_port;
1056 e_key1.protocol = proto1;
1057 e_key1.fib_index = rx_fib_index1;
1058 if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1059 &twice_nat1, &lb_nat1,
1064 * Send DHCP packets to the ipv4 stack, or we won't
1065 * be able to use dhcp client on the outside interface
1067 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1068 && (udp1->dst_port ==
1069 clib_host_to_net_u16
1070 (UDP_DST_PORT_dhcp_to_client))))
1072 vnet_feature_next (&next1, b1);
1076 if (!sm->forwarding_enabled)
1079 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1080 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1084 if (next_src_nat (sm, ip1, ip1->protocol,
1085 udp1->src_port, udp1->dst_port,
1086 thread_index, rx_fib_index1))
1088 next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1091 create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1097 if (PREDICT_FALSE (identity_nat1))
1100 /* Create session initiated by host from external network */
1101 s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1109 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1115 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1121 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1124 old_addr1 = ip1->dst_address.as_u32;
1125 new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1126 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1128 sum1 = ip1->checksum;
1129 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1131 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1132 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1133 s1->ext_host_nat_addr.as_u32, ip4_header_t,
1135 ip1->checksum = ip_csum_fold (sum1);
1137 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1139 old_port1 = tcp1->dst_port;
1140 new_port1 = tcp1->dst_port = s1->in2out.port;
1142 sum1 = tcp1->checksum;
1143 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1145 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1147 if (is_twice_nat_session (s1))
1149 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1150 s1->ext_host_nat_addr.as_u32,
1151 ip4_header_t, dst_address);
1152 sum1 = ip_csum_update (sum1, tcp1->src_port,
1153 s1->ext_host_nat_port, ip4_header_t,
1155 tcp1->src_port = s1->ext_host_nat_port;
1156 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1158 tcp1->checksum = ip_csum_fold (sum1);
1159 if (nat44_set_tcp_session_state_o2i
1160 (sm, s1, tcp1, thread_index))
1165 udp1->dst_port = s1->in2out.port;
1166 if (is_twice_nat_session (s1))
1168 udp1->src_port = s1->ext_host_nat_port;
1169 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1175 nat44_session_update_counters (s1, now,
1176 vlib_buffer_length_in_chain (vm,
1178 /* Per-user LRU list maintenance */
1179 nat44_session_update_lru (sm, s1, thread_index);
1182 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1183 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1185 nat44_ed_out2in_trace_t *t =
1186 vlib_add_trace (vm, node, b1, sizeof (*t));
1187 t->is_slow_path = is_slow_path;
1188 t->sw_if_index = sw_if_index1;
1189 t->next_index = next1;
1190 t->session_index = ~0;
1192 t->session_index = s1 - tsm->sessions;
1195 pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
1197 /* verify speculative enqueues, maybe switch current next frame */
1198 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1199 to_next, n_left_to_next,
1200 bi0, bi1, next0, next1);
1203 while (n_left_from > 0 && n_left_to_next > 0)
1207 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1209 u16 old_port0, new_port0;
1213 icmp46_header_t *icmp0;
1214 snat_session_t *s0 = 0;
1215 clib_bihash_kv_16_8_t kv0, value0;
1217 snat_session_key_t e_key0, l_key0;
1218 lb_nat_type_t lb_nat0;
1219 twice_nat_type_t twice_nat0;
1222 /* speculatively enqueue b0 to the current next frame */
1228 n_left_to_next -= 1;
1230 b0 = vlib_get_buffer (vm, bi0);
1231 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1232 vnet_buffer (b0)->snat.flags = 0;
1233 ip0 = vlib_buffer_get_current (b0);
1235 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1237 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1240 if (PREDICT_FALSE (ip0->ttl == 1))
1242 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1243 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1244 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1246 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1250 udp0 = ip4_next_header (ip0);
1251 tcp0 = (tcp_header_t *) udp0;
1252 icmp0 = (icmp46_header_t *) udp0;
1253 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1257 if (PREDICT_FALSE (proto0 == ~0))
1260 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1261 thread_index, now, vm,
1263 if (!sm->forwarding_enabled)
1266 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1271 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1273 next0 = icmp_out2in_ed_slow_path
1274 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1275 next0, now, thread_index, &s0);
1281 if (PREDICT_FALSE (proto0 == ~0))
1283 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1287 if (ip4_is_fragment (ip0))
1289 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1293 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1295 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1300 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1301 ip0->protocol, rx_fib_index0, udp0->dst_port,
1304 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1308 /* Try to match static mapping by external address and port,
1309 destination address and port in packet */
1310 e_key0.addr = ip0->dst_address;
1311 e_key0.port = udp0->dst_port;
1312 e_key0.protocol = proto0;
1313 e_key0.fib_index = rx_fib_index0;
1314 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1315 &twice_nat0, &lb_nat0,
1320 * Send DHCP packets to the ipv4 stack, or we won't
1321 * be able to use dhcp client on the outside interface
1323 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1324 && (udp0->dst_port ==
1325 clib_host_to_net_u16
1326 (UDP_DST_PORT_dhcp_to_client))))
1328 vnet_feature_next (&next0, b0);
1332 if (!sm->forwarding_enabled)
1335 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1336 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1340 if (next_src_nat (sm, ip0, ip0->protocol,
1341 udp0->src_port, udp0->dst_port,
1342 thread_index, rx_fib_index0))
1344 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1347 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1353 if (PREDICT_FALSE (identity_nat0))
1356 /* Create session initiated by host from external network */
1357 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1365 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1371 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1377 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1380 old_addr0 = ip0->dst_address.as_u32;
1381 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1382 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1384 sum0 = ip0->checksum;
1385 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1387 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1388 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1389 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1391 ip0->checksum = ip_csum_fold (sum0);
1393 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1395 old_port0 = tcp0->dst_port;
1396 new_port0 = tcp0->dst_port = s0->in2out.port;
1398 sum0 = tcp0->checksum;
1399 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1401 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1403 if (is_twice_nat_session (s0))
1405 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1406 s0->ext_host_nat_addr.as_u32,
1407 ip4_header_t, dst_address);
1408 sum0 = ip_csum_update (sum0, tcp0->src_port,
1409 s0->ext_host_nat_port, ip4_header_t,
1411 tcp0->src_port = s0->ext_host_nat_port;
1412 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1414 tcp0->checksum = ip_csum_fold (sum0);
1415 if (nat44_set_tcp_session_state_o2i
1416 (sm, s0, tcp0, thread_index))
1421 udp0->dst_port = s0->in2out.port;
1422 if (is_twice_nat_session (s0))
1424 udp0->src_port = s0->ext_host_nat_port;
1425 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1431 nat44_session_update_counters (s0, now,
1432 vlib_buffer_length_in_chain (vm,
1434 /* Per-user LRU list maintenance */
1435 nat44_session_update_lru (sm, s0, thread_index);
1438 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1439 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1441 nat44_ed_out2in_trace_t *t =
1442 vlib_add_trace (vm, node, b0, sizeof (*t));
1443 t->is_slow_path = is_slow_path;
1444 t->sw_if_index = sw_if_index0;
1445 t->next_index = next0;
1446 t->session_index = ~0;
1448 t->session_index = s0 - tsm->sessions;
1451 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1452 /* verify speculative enqueue, maybe switch current next frame */
1453 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1454 to_next, n_left_to_next,
1458 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1461 vlib_node_increment_counter (vm, stats_node_index,
1462 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1464 return frame->n_vectors;
1468 nat44_ed_out2in_fast_path_fn (vlib_main_t * vm,
1469 vlib_node_runtime_t * node,
1470 vlib_frame_t * frame)
1472 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1476 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1477 .function = nat44_ed_out2in_fast_path_fn,
1478 .name = "nat44-ed-out2in",
1479 .vector_size = sizeof (u32),
1480 .format_trace = format_nat44_ed_out2in_trace,
1481 .type = VLIB_NODE_TYPE_INTERNAL,
1482 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1483 .error_strings = nat_out2in_ed_error_strings,
1484 .runtime_data_bytes = sizeof (snat_runtime_t),
1485 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1487 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1488 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1489 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1490 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1491 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1492 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1497 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_node,
1498 nat44_ed_out2in_fast_path_fn);
1501 nat44_ed_out2in_slow_path_fn (vlib_main_t * vm,
1502 vlib_node_runtime_t * node,
1503 vlib_frame_t * frame)
1505 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1509 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1510 .function = nat44_ed_out2in_slow_path_fn,
1511 .name = "nat44-ed-out2in-slowpath",
1512 .vector_size = sizeof (u32),
1513 .format_trace = format_nat44_ed_out2in_trace,
1514 .type = VLIB_NODE_TYPE_INTERNAL,
1515 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1516 .error_strings = nat_out2in_ed_error_strings,
1517 .runtime_data_bytes = sizeof (snat_runtime_t),
1518 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1520 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1521 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1522 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1523 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1524 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1525 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1530 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_slowpath_node,
1531 nat44_ed_out2in_slow_path_fn);
1534 nat44_ed_out2in_reass_node_fn (vlib_main_t * vm,
1535 vlib_node_runtime_t * node,
1536 vlib_frame_t * frame)
1538 u32 n_left_from, *from, *to_next;
1539 nat44_ed_out2in_next_t next_index;
1540 u32 pkts_processed = 0;
1541 snat_main_t *sm = &snat_main;
1542 f64 now = vlib_time_now (vm);
1543 u32 thread_index = vm->thread_index;
1544 snat_main_per_thread_data_t *per_thread_data =
1545 &sm->per_thread_data[thread_index];
1546 u32 *fragments_to_drop = 0;
1547 u32 *fragments_to_loopback = 0;
1549 from = vlib_frame_vector_args (frame);
1550 n_left_from = frame->n_vectors;
1551 next_index = node->cached_next_index;
1553 while (n_left_from > 0)
1557 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1559 while (n_left_from > 0 && n_left_to_next > 0)
1561 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1566 nat_reass_ip4_t *reass0;
1569 icmp46_header_t *icmp0;
1570 clib_bihash_kv_16_8_t kv0, value0;
1571 snat_session_t *s0 = 0;
1572 u16 old_port0, new_port0;
1574 snat_session_key_t e_key0, l_key0;
1576 twice_nat_type_t twice_nat0;
1579 /* speculatively enqueue b0 to the current next frame */
1585 n_left_to_next -= 1;
1587 b0 = vlib_get_buffer (vm, bi0);
1588 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1590 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1592 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1595 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1597 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1598 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1602 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1603 udp0 = ip4_next_header (ip0);
1604 tcp0 = (tcp_header_t *) udp0;
1605 icmp0 = (icmp46_header_t *) udp0;
1606 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1608 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1612 1, &fragments_to_drop);
1614 if (PREDICT_FALSE (!reass0))
1616 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1617 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1618 nat_log_notice ("maximum reassemblies exceeded");
1622 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1624 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1626 next0 = icmp_out2in_ed_slow_path
1627 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1628 next0, now, thread_index, &s0);
1630 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1633 reass0->sess_index = s0 - per_thread_data->sessions;
1635 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1636 reass0->thread_index = thread_index;
1637 nat_ip4_reass_get_frags (reass0,
1638 &fragments_to_loopback);
1644 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1645 ip0->protocol, rx_fib_index0, udp0->dst_port,
1648 if (clib_bihash_search_16_8
1649 (&per_thread_data->out2in_ed, &kv0, &value0))
1651 /* Try to match static mapping by external address and port,
1652 destination address and port in packet */
1653 e_key0.addr = ip0->dst_address;
1654 e_key0.port = udp0->dst_port;
1655 e_key0.protocol = proto0;
1656 e_key0.fib_index = rx_fib_index0;
1657 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1658 &twice_nat0, &lb0, 0,
1662 * Send DHCP packets to the ipv4 stack, or we won't
1663 * be able to use dhcp client on the outside interface
1665 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1668 clib_host_to_net_u16
1669 (UDP_DST_PORT_dhcp_to_client))))
1671 vnet_feature_next (&next0, b0);
1675 if (!sm->forwarding_enabled)
1678 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1679 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1683 if (next_src_nat (sm, ip0, ip0->protocol,
1684 udp0->src_port, udp0->dst_port,
1685 thread_index, rx_fib_index0))
1687 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1690 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1692 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1693 nat_ip4_reass_get_frags (reass0,
1694 &fragments_to_loopback);
1699 if (PREDICT_FALSE (identity_nat0))
1701 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1705 /* Create session initiated by host from external network */
1706 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1714 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1715 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1718 reass0->sess_index = s0 - per_thread_data->sessions;
1719 reass0->thread_index = thread_index;
1723 s0 = pool_elt_at_index (per_thread_data->sessions,
1725 reass0->sess_index = value0.value;
1727 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1731 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1733 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1735 if (nat_ip4_reass_add_fragment
1736 (reass0, bi0, &fragments_to_drop))
1738 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1740 ("maximum fragments per reassembly exceeded");
1741 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1747 s0 = pool_elt_at_index (per_thread_data->sessions,
1748 reass0->sess_index);
1751 old_addr0 = ip0->dst_address.as_u32;
1752 ip0->dst_address = s0->in2out.addr;
1753 new_addr0 = ip0->dst_address.as_u32;
1754 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1756 sum0 = ip0->checksum;
1757 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1759 dst_address /* changed member */ );
1760 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1761 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1762 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1764 ip0->checksum = ip_csum_fold (sum0);
1766 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1768 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1770 old_port0 = tcp0->dst_port;
1771 tcp0->dst_port = s0->in2out.port;
1772 new_port0 = tcp0->dst_port;
1774 sum0 = tcp0->checksum;
1775 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1777 dst_address /* changed member */ );
1779 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1780 ip4_header_t /* cheat */ ,
1781 length /* changed member */ );
1782 if (is_twice_nat_session (s0))
1784 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1785 s0->ext_host_nat_addr.as_u32,
1786 ip4_header_t, dst_address);
1787 sum0 = ip_csum_update (sum0, tcp0->src_port,
1788 s0->ext_host_nat_port,
1789 ip4_header_t, length);
1790 tcp0->src_port = s0->ext_host_nat_port;
1791 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1793 tcp0->checksum = ip_csum_fold (sum0);
1797 old_port0 = udp0->dst_port;
1798 udp0->dst_port = s0->in2out.port;
1799 if (is_twice_nat_session (s0))
1801 udp0->src_port = s0->ext_host_nat_port;
1802 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1809 nat44_session_update_counters (s0, now,
1810 vlib_buffer_length_in_chain (vm,
1812 /* Per-user LRU list maintenance */
1813 nat44_session_update_lru (sm, s0, thread_index);
1816 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1817 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1819 nat44_reass_trace_t *t =
1820 vlib_add_trace (vm, node, b0, sizeof (*t));
1821 t->cached = cached0;
1822 t->sw_if_index = sw_if_index0;
1823 t->next_index = next0;
1833 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1835 /* verify speculative enqueue, maybe switch current next frame */
1836 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1837 to_next, n_left_to_next,
1841 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1843 from = vlib_frame_vector_args (frame);
1844 u32 len = vec_len (fragments_to_loopback);
1845 if (len <= VLIB_FRAME_SIZE)
1847 clib_memcpy (from, fragments_to_loopback,
1848 sizeof (u32) * len);
1850 vec_reset_length (fragments_to_loopback);
1855 fragments_to_loopback + (len -
1857 sizeof (u32) * VLIB_FRAME_SIZE);
1858 n_left_from = VLIB_FRAME_SIZE;
1859 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1864 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1867 vlib_node_increment_counter (vm, nat44_ed_out2in_reass_node.index,
1868 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1871 nat_send_all_to_node (vm, fragments_to_drop, node,
1872 &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1873 NAT44_ED_OUT2IN_NEXT_DROP);
1875 vec_free (fragments_to_drop);
1876 vec_free (fragments_to_loopback);
1877 return frame->n_vectors;
1881 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1882 .function = nat44_ed_out2in_reass_node_fn,
1883 .name = "nat44-ed-out2in-reass",
1884 .vector_size = sizeof (u32),
1885 .format_trace = format_nat44_reass_trace,
1886 .type = VLIB_NODE_TYPE_INTERNAL,
1887 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1888 .error_strings = nat_out2in_ed_error_strings,
1889 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1891 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1892 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1893 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1894 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1895 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1896 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1901 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_reass_node,
1902 nat44_ed_out2in_reass_node_fn);
1905 * fd.io coding-style-patch-verification: ON
1908 * eval: (c-set-style "gnu")