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;
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))
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 /* Create session initiated by host from external network */
537 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
544 next = NAT44_ED_OUT2IN_NEXT_DROP;
550 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
551 icmp->type != ICMP4_echo_request &&
552 !icmp_is_error_message (icmp)))
554 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
555 next = NAT44_ED_OUT2IN_NEXT_DROP;
559 s = pool_elt_at_index (tsm->sessions, value.value);
562 *p_proto = ip_proto_to_snat_proto (key.proto);
565 *p_value = s->in2out;
566 *p_dont_translate = dont_translate;
568 *(snat_session_t **) d = s;
572 static snat_session_t *
573 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
579 vlib_main_t * vm, vlib_node_runtime_t * node)
581 clib_bihash_kv_8_8_t kv, value;
582 clib_bihash_kv_16_8_t s_kv, s_value;
583 snat_static_mapping_t *m;
584 u32 old_addr, new_addr;
587 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
590 old_addr = ip->dst_address.as_u32;
592 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
595 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
597 s = pool_elt_at_index (tsm->sessions, s_value.value);
598 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
602 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
604 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
605 nat_log_notice ("maximum sessions exceeded");
609 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
610 if (clib_bihash_search_8_8
611 (&sm->static_mapping_by_external, &kv, &value))
613 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
617 m = pool_elt_at_index (sm->static_mappings, value.value);
619 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
621 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
625 nat_log_warn ("create NAT user failed");
629 /* Create a new session */
630 s = nat_ed_session_alloc (sm, u, thread_index, now);
633 nat44_delete_user_with_no_session (sm, u, thread_index);
634 nat_log_warn ("create NAT session failed");
638 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
639 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
640 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
641 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
642 s->out2in.addr.as_u32 = old_addr;
643 s->out2in.fib_index = rx_fib_index;
644 s->in2out.addr.as_u32 = new_addr;
645 s->in2out.fib_index = m->fib_index;
646 s->in2out.port = s->out2in.port = ip->protocol;
647 user_session_increment (sm, u, 1);
649 /* Add to lookup tables */
650 s_kv.value = s - tsm->sessions;
651 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
652 nat_log_notice ("out2in key add failed");
654 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
656 s_kv.value = s - tsm->sessions;
657 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
658 nat_log_notice ("in2out key add failed");
661 /* Update IP checksum */
663 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
664 ip->checksum = ip_csum_fold (sum);
666 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
669 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
670 /* Per-user LRU list maintenance */
671 nat44_session_update_lru (sm, s, thread_index);
677 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
678 vlib_node_runtime_t * node,
679 vlib_frame_t * frame, int is_slow_path)
681 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
682 nat44_ed_out2in_next_t next_index;
683 snat_main_t *sm = &snat_main;
684 f64 now = vlib_time_now (vm);
685 u32 thread_index = vm->thread_index;
686 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
688 stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
689 nat44_ed_out2in_node.index;
691 from = vlib_frame_vector_args (frame);
692 n_left_from = frame->n_vectors;
693 next_index = node->cached_next_index;
695 while (n_left_from > 0)
699 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
701 while (n_left_from >= 4 && n_left_to_next >= 2)
704 vlib_buffer_t *b0, *b1;
705 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
707 u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
709 u16 old_port0, new_port0, old_port1, new_port1;
710 ip4_header_t *ip0, *ip1;
711 udp_header_t *udp0, *udp1;
712 tcp_header_t *tcp0, *tcp1;
713 icmp46_header_t *icmp0, *icmp1;
714 snat_session_t *s0 = 0, *s1 = 0;
715 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
716 ip_csum_t sum0, sum1;
717 snat_session_key_t e_key0, l_key0, e_key1, l_key1;
718 lb_nat_type_t lb_nat0, lb_nat1;
719 twice_nat_type_t twice_nat0, twice_nat1;
721 /* Prefetch next iteration. */
723 vlib_buffer_t *p2, *p3;
725 p2 = vlib_get_buffer (vm, from[2]);
726 p3 = vlib_get_buffer (vm, from[3]);
728 vlib_prefetch_buffer_header (p2, LOAD);
729 vlib_prefetch_buffer_header (p3, LOAD);
731 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
732 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
735 /* speculatively enqueue b0 and b1 to the current next frame */
736 to_next[0] = bi0 = from[0];
737 to_next[1] = bi1 = from[1];
743 b0 = vlib_get_buffer (vm, bi0);
744 b1 = vlib_get_buffer (vm, bi1);
746 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
747 vnet_buffer (b0)->snat.flags = 0;
748 ip0 = vlib_buffer_get_current (b0);
750 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
752 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
755 if (PREDICT_FALSE (ip0->ttl == 1))
757 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
758 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
759 ICMP4_time_exceeded_ttl_exceeded_in_transit,
761 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
765 udp0 = ip4_next_header (ip0);
766 tcp0 = (tcp_header_t *) udp0;
767 icmp0 = (icmp46_header_t *) udp0;
768 proto0 = ip_proto_to_snat_proto (ip0->protocol);
772 if (PREDICT_FALSE (proto0 == ~0))
775 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
776 thread_index, now, vm,
778 if (!sm->forwarding_enabled)
781 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
786 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
788 next0 = icmp_out2in_ed_slow_path
789 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
790 next0, now, thread_index, &s0);
796 if (PREDICT_FALSE (proto0 == ~0))
798 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
802 if (ip4_is_fragment (ip0))
804 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
808 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
810 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
815 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
816 ip0->protocol, rx_fib_index0, udp0->dst_port,
819 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
823 /* Try to match static mapping by external address and port,
824 destination address and port in packet */
825 e_key0.addr = ip0->dst_address;
826 e_key0.port = udp0->dst_port;
827 e_key0.protocol = proto0;
828 e_key0.fib_index = rx_fib_index0;
829 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
830 &twice_nat0, &lb_nat0,
834 * Send DHCP packets to the ipv4 stack, or we won't
835 * be able to use dhcp client on the outside interface
837 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
838 && (udp0->dst_port ==
840 (UDP_DST_PORT_dhcp_to_client))))
842 vnet_feature_next (&next0, b0);
846 if (!sm->forwarding_enabled)
849 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
850 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
854 if (next_src_nat (sm, ip0, ip0->protocol,
855 udp0->src_port, udp0->dst_port,
856 thread_index, rx_fib_index0))
858 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
861 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
867 /* Create session initiated by host from external network */
868 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
876 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
882 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
888 s0 = pool_elt_at_index (tsm->sessions, value0.value);
891 old_addr0 = ip0->dst_address.as_u32;
892 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
893 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
895 sum0 = ip0->checksum;
896 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
898 if (PREDICT_FALSE (is_twice_nat_session (s0)))
899 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
900 s0->ext_host_nat_addr.as_u32, ip4_header_t,
902 ip0->checksum = ip_csum_fold (sum0);
904 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
906 old_port0 = tcp0->dst_port;
907 new_port0 = tcp0->dst_port = s0->in2out.port;
909 sum0 = tcp0->checksum;
910 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
912 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
914 if (is_twice_nat_session (s0))
916 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
917 s0->ext_host_nat_addr.as_u32,
918 ip4_header_t, dst_address);
919 sum0 = ip_csum_update (sum0, tcp0->src_port,
920 s0->ext_host_nat_port, ip4_header_t,
922 tcp0->src_port = s0->ext_host_nat_port;
923 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
925 tcp0->checksum = ip_csum_fold (sum0);
926 if (nat44_set_tcp_session_state_o2i
927 (sm, s0, tcp0, thread_index))
932 udp0->dst_port = s0->in2out.port;
933 if (is_twice_nat_session (s0))
935 udp0->src_port = s0->ext_host_nat_port;
936 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
942 nat44_session_update_counters (s0, now,
943 vlib_buffer_length_in_chain (vm,
945 /* Per-user LRU list maintenance */
946 nat44_session_update_lru (sm, s0, thread_index);
949 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
950 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
952 nat44_ed_out2in_trace_t *t =
953 vlib_add_trace (vm, node, b0, sizeof (*t));
954 t->is_slow_path = is_slow_path;
955 t->sw_if_index = sw_if_index0;
956 t->next_index = next0;
957 t->session_index = ~0;
959 t->session_index = s0 - tsm->sessions;
962 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
964 next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
965 vnet_buffer (b1)->snat.flags = 0;
966 ip1 = vlib_buffer_get_current (b1);
968 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
970 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
973 if (PREDICT_FALSE (ip1->ttl == 1))
975 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
976 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
977 ICMP4_time_exceeded_ttl_exceeded_in_transit,
979 next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
983 udp1 = ip4_next_header (ip1);
984 tcp1 = (tcp_header_t *) udp1;
985 icmp1 = (icmp46_header_t *) udp1;
986 proto1 = ip_proto_to_snat_proto (ip1->protocol);
990 if (PREDICT_FALSE (proto1 == ~0))
993 nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
994 thread_index, now, vm,
996 if (!sm->forwarding_enabled)
999 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1004 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1006 next1 = icmp_out2in_ed_slow_path
1007 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1008 next1, now, thread_index, &s1);
1014 if (PREDICT_FALSE (proto1 == ~0))
1016 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1020 if (ip4_is_fragment (ip1))
1022 next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1026 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1028 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1033 make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1034 ip1->protocol, rx_fib_index1, udp1->dst_port,
1037 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1041 /* Try to match static mapping by external address and port,
1042 destination address and port in packet */
1043 e_key1.addr = ip1->dst_address;
1044 e_key1.port = udp1->dst_port;
1045 e_key1.protocol = proto1;
1046 e_key1.fib_index = rx_fib_index1;
1047 if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1048 &twice_nat1, &lb_nat1,
1052 * Send DHCP packets to the ipv4 stack, or we won't
1053 * be able to use dhcp client on the outside interface
1055 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1056 && (udp1->dst_port ==
1057 clib_host_to_net_u16
1058 (UDP_DST_PORT_dhcp_to_client))))
1060 vnet_feature_next (&next1, b1);
1064 if (!sm->forwarding_enabled)
1067 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1068 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1072 if (next_src_nat (sm, ip1, ip1->protocol,
1073 udp1->src_port, udp1->dst_port,
1074 thread_index, rx_fib_index1))
1076 next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1079 create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1085 /* Create session initiated by host from external network */
1086 s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1094 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1100 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1106 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1109 old_addr1 = ip1->dst_address.as_u32;
1110 new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1111 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1113 sum1 = ip1->checksum;
1114 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1116 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1117 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1118 s1->ext_host_nat_addr.as_u32, ip4_header_t,
1120 ip1->checksum = ip_csum_fold (sum1);
1122 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1124 old_port1 = tcp1->dst_port;
1125 new_port1 = tcp1->dst_port = s1->in2out.port;
1127 sum1 = tcp1->checksum;
1128 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1130 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1132 if (is_twice_nat_session (s1))
1134 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1135 s1->ext_host_nat_addr.as_u32,
1136 ip4_header_t, dst_address);
1137 sum1 = ip_csum_update (sum1, tcp1->src_port,
1138 s1->ext_host_nat_port, ip4_header_t,
1140 tcp1->src_port = s1->ext_host_nat_port;
1141 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1143 tcp1->checksum = ip_csum_fold (sum1);
1144 if (nat44_set_tcp_session_state_o2i
1145 (sm, s1, tcp1, thread_index))
1150 udp1->dst_port = s1->in2out.port;
1151 if (is_twice_nat_session (s1))
1153 udp1->src_port = s1->ext_host_nat_port;
1154 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1160 nat44_session_update_counters (s1, now,
1161 vlib_buffer_length_in_chain (vm,
1163 /* Per-user LRU list maintenance */
1164 nat44_session_update_lru (sm, s1, thread_index);
1167 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1168 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1170 nat44_ed_out2in_trace_t *t =
1171 vlib_add_trace (vm, node, b1, sizeof (*t));
1172 t->is_slow_path = is_slow_path;
1173 t->sw_if_index = sw_if_index1;
1174 t->next_index = next1;
1175 t->session_index = ~0;
1177 t->session_index = s1 - tsm->sessions;
1180 pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
1182 /* verify speculative enqueues, maybe switch current next frame */
1183 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1184 to_next, n_left_to_next,
1185 bi0, bi1, next0, next1);
1188 while (n_left_from > 0 && n_left_to_next > 0)
1192 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1194 u16 old_port0, new_port0;
1198 icmp46_header_t *icmp0;
1199 snat_session_t *s0 = 0;
1200 clib_bihash_kv_16_8_t kv0, value0;
1202 snat_session_key_t e_key0, l_key0;
1203 lb_nat_type_t lb_nat0;
1204 twice_nat_type_t twice_nat0;
1206 /* speculatively enqueue b0 to the current next frame */
1212 n_left_to_next -= 1;
1214 b0 = vlib_get_buffer (vm, bi0);
1215 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1216 vnet_buffer (b0)->snat.flags = 0;
1217 ip0 = vlib_buffer_get_current (b0);
1219 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1221 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1224 if (PREDICT_FALSE (ip0->ttl == 1))
1226 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1227 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1228 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1230 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1234 udp0 = ip4_next_header (ip0);
1235 tcp0 = (tcp_header_t *) udp0;
1236 icmp0 = (icmp46_header_t *) udp0;
1237 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1241 if (PREDICT_FALSE (proto0 == ~0))
1244 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1245 thread_index, now, vm,
1247 if (!sm->forwarding_enabled)
1250 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1255 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1257 next0 = icmp_out2in_ed_slow_path
1258 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1259 next0, now, thread_index, &s0);
1265 if (PREDICT_FALSE (proto0 == ~0))
1267 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1271 if (ip4_is_fragment (ip0))
1273 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1277 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1279 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1284 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1285 ip0->protocol, rx_fib_index0, udp0->dst_port,
1288 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1292 /* Try to match static mapping by external address and port,
1293 destination address and port in packet */
1294 e_key0.addr = ip0->dst_address;
1295 e_key0.port = udp0->dst_port;
1296 e_key0.protocol = proto0;
1297 e_key0.fib_index = rx_fib_index0;
1298 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1299 &twice_nat0, &lb_nat0,
1303 * Send DHCP packets to the ipv4 stack, or we won't
1304 * be able to use dhcp client on the outside interface
1306 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1307 && (udp0->dst_port ==
1308 clib_host_to_net_u16
1309 (UDP_DST_PORT_dhcp_to_client))))
1311 vnet_feature_next (&next0, b0);
1315 if (!sm->forwarding_enabled)
1318 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1319 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1323 if (next_src_nat (sm, ip0, ip0->protocol,
1324 udp0->src_port, udp0->dst_port,
1325 thread_index, rx_fib_index0))
1327 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1330 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1336 /* Create session initiated by host from external network */
1337 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1345 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1351 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1357 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1360 old_addr0 = ip0->dst_address.as_u32;
1361 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1362 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1364 sum0 = ip0->checksum;
1365 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1367 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1368 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1369 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1371 ip0->checksum = ip_csum_fold (sum0);
1373 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1375 old_port0 = tcp0->dst_port;
1376 new_port0 = tcp0->dst_port = s0->in2out.port;
1378 sum0 = tcp0->checksum;
1379 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1381 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1383 if (is_twice_nat_session (s0))
1385 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1386 s0->ext_host_nat_addr.as_u32,
1387 ip4_header_t, dst_address);
1388 sum0 = ip_csum_update (sum0, tcp0->src_port,
1389 s0->ext_host_nat_port, ip4_header_t,
1391 tcp0->src_port = s0->ext_host_nat_port;
1392 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1394 tcp0->checksum = ip_csum_fold (sum0);
1395 if (nat44_set_tcp_session_state_o2i
1396 (sm, s0, tcp0, thread_index))
1401 udp0->dst_port = s0->in2out.port;
1402 if (is_twice_nat_session (s0))
1404 udp0->src_port = s0->ext_host_nat_port;
1405 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1411 nat44_session_update_counters (s0, now,
1412 vlib_buffer_length_in_chain (vm,
1414 /* Per-user LRU list maintenance */
1415 nat44_session_update_lru (sm, s0, thread_index);
1418 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1419 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1421 nat44_ed_out2in_trace_t *t =
1422 vlib_add_trace (vm, node, b0, sizeof (*t));
1423 t->is_slow_path = is_slow_path;
1424 t->sw_if_index = sw_if_index0;
1425 t->next_index = next0;
1426 t->session_index = ~0;
1428 t->session_index = s0 - tsm->sessions;
1431 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1432 /* verify speculative enqueue, maybe switch current next frame */
1433 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1434 to_next, n_left_to_next,
1438 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1441 vlib_node_increment_counter (vm, stats_node_index,
1442 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1444 return frame->n_vectors;
1448 nat44_ed_out2in_fast_path_fn (vlib_main_t * vm,
1449 vlib_node_runtime_t * node,
1450 vlib_frame_t * frame)
1452 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1456 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1457 .function = nat44_ed_out2in_fast_path_fn,
1458 .name = "nat44-ed-out2in",
1459 .vector_size = sizeof (u32),
1460 .format_trace = format_nat44_ed_out2in_trace,
1461 .type = VLIB_NODE_TYPE_INTERNAL,
1462 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1463 .error_strings = nat_out2in_ed_error_strings,
1464 .runtime_data_bytes = sizeof (snat_runtime_t),
1465 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1467 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1468 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1469 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1470 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1471 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1472 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1477 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_node,
1478 nat44_ed_out2in_fast_path_fn);
1481 nat44_ed_out2in_slow_path_fn (vlib_main_t * vm,
1482 vlib_node_runtime_t * node,
1483 vlib_frame_t * frame)
1485 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1489 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1490 .function = nat44_ed_out2in_slow_path_fn,
1491 .name = "nat44-ed-out2in-slowpath",
1492 .vector_size = sizeof (u32),
1493 .format_trace = format_nat44_ed_out2in_trace,
1494 .type = VLIB_NODE_TYPE_INTERNAL,
1495 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1496 .error_strings = nat_out2in_ed_error_strings,
1497 .runtime_data_bytes = sizeof (snat_runtime_t),
1498 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1500 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1501 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1502 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1503 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1504 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1505 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1510 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_slowpath_node,
1511 nat44_ed_out2in_slow_path_fn);
1514 nat44_ed_out2in_reass_node_fn (vlib_main_t * vm,
1515 vlib_node_runtime_t * node,
1516 vlib_frame_t * frame)
1518 u32 n_left_from, *from, *to_next;
1519 nat44_ed_out2in_next_t next_index;
1520 u32 pkts_processed = 0;
1521 snat_main_t *sm = &snat_main;
1522 f64 now = vlib_time_now (vm);
1523 u32 thread_index = vm->thread_index;
1524 snat_main_per_thread_data_t *per_thread_data =
1525 &sm->per_thread_data[thread_index];
1526 u32 *fragments_to_drop = 0;
1527 u32 *fragments_to_loopback = 0;
1529 from = vlib_frame_vector_args (frame);
1530 n_left_from = frame->n_vectors;
1531 next_index = node->cached_next_index;
1533 while (n_left_from > 0)
1537 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1539 while (n_left_from > 0 && n_left_to_next > 0)
1541 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1546 nat_reass_ip4_t *reass0;
1549 icmp46_header_t *icmp0;
1550 clib_bihash_kv_16_8_t kv0, value0;
1551 snat_session_t *s0 = 0;
1552 u16 old_port0, new_port0;
1554 snat_session_key_t e_key0, l_key0;
1556 twice_nat_type_t twice_nat0;
1558 /* speculatively enqueue b0 to the current next frame */
1564 n_left_to_next -= 1;
1566 b0 = vlib_get_buffer (vm, bi0);
1567 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1569 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1571 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1574 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1576 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1577 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1581 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1582 udp0 = ip4_next_header (ip0);
1583 tcp0 = (tcp_header_t *) udp0;
1584 icmp0 = (icmp46_header_t *) udp0;
1585 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1587 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1591 1, &fragments_to_drop);
1593 if (PREDICT_FALSE (!reass0))
1595 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1596 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1597 nat_log_notice ("maximum reassemblies exceeded");
1601 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1603 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1605 next0 = icmp_out2in_ed_slow_path
1606 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1607 next0, now, thread_index, &s0);
1609 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1612 reass0->sess_index = s0 - per_thread_data->sessions;
1614 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1615 reass0->thread_index = thread_index;
1616 nat_ip4_reass_get_frags (reass0,
1617 &fragments_to_loopback);
1623 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1624 ip0->protocol, rx_fib_index0, udp0->dst_port,
1627 if (clib_bihash_search_16_8
1628 (&per_thread_data->out2in_ed, &kv0, &value0))
1630 /* Try to match static mapping by external address and port,
1631 destination address and port in packet */
1632 e_key0.addr = ip0->dst_address;
1633 e_key0.port = udp0->dst_port;
1634 e_key0.protocol = proto0;
1635 e_key0.fib_index = rx_fib_index0;
1636 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1637 &twice_nat0, &lb0, 0))
1640 * Send DHCP packets to the ipv4 stack, or we won't
1641 * be able to use dhcp client on the outside interface
1643 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1646 clib_host_to_net_u16
1647 (UDP_DST_PORT_dhcp_to_client))))
1649 vnet_feature_next (&next0, b0);
1653 if (!sm->forwarding_enabled)
1656 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1657 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1661 if (next_src_nat (sm, ip0, ip0->protocol,
1662 udp0->src_port, udp0->dst_port,
1663 thread_index, rx_fib_index0))
1665 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1668 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1670 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1671 nat_ip4_reass_get_frags (reass0,
1672 &fragments_to_loopback);
1677 /* Create session initiated by host from external network */
1678 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1686 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1687 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1690 reass0->sess_index = s0 - per_thread_data->sessions;
1691 reass0->thread_index = thread_index;
1695 s0 = pool_elt_at_index (per_thread_data->sessions,
1697 reass0->sess_index = value0.value;
1699 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1703 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1705 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1707 if (nat_ip4_reass_add_fragment
1708 (reass0, bi0, &fragments_to_drop))
1710 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1712 ("maximum fragments per reassembly exceeded");
1713 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1719 s0 = pool_elt_at_index (per_thread_data->sessions,
1720 reass0->sess_index);
1723 old_addr0 = ip0->dst_address.as_u32;
1724 ip0->dst_address = s0->in2out.addr;
1725 new_addr0 = ip0->dst_address.as_u32;
1726 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1728 sum0 = ip0->checksum;
1729 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1731 dst_address /* changed member */ );
1732 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1733 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1734 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1736 ip0->checksum = ip_csum_fold (sum0);
1738 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1740 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1742 old_port0 = tcp0->dst_port;
1743 tcp0->dst_port = s0->in2out.port;
1744 new_port0 = tcp0->dst_port;
1746 sum0 = tcp0->checksum;
1747 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1749 dst_address /* changed member */ );
1751 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1752 ip4_header_t /* cheat */ ,
1753 length /* changed member */ );
1754 if (is_twice_nat_session (s0))
1756 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1757 s0->ext_host_nat_addr.as_u32,
1758 ip4_header_t, dst_address);
1759 sum0 = ip_csum_update (sum0, tcp0->src_port,
1760 s0->ext_host_nat_port,
1761 ip4_header_t, length);
1762 tcp0->src_port = s0->ext_host_nat_port;
1763 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1765 tcp0->checksum = ip_csum_fold (sum0);
1769 old_port0 = udp0->dst_port;
1770 udp0->dst_port = s0->in2out.port;
1771 if (is_twice_nat_session (s0))
1773 udp0->src_port = s0->ext_host_nat_port;
1774 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1781 nat44_session_update_counters (s0, now,
1782 vlib_buffer_length_in_chain (vm,
1784 /* Per-user LRU list maintenance */
1785 nat44_session_update_lru (sm, s0, thread_index);
1788 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1789 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1791 nat44_reass_trace_t *t =
1792 vlib_add_trace (vm, node, b0, sizeof (*t));
1793 t->cached = cached0;
1794 t->sw_if_index = sw_if_index0;
1795 t->next_index = next0;
1805 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1807 /* verify speculative enqueue, maybe switch current next frame */
1808 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1809 to_next, n_left_to_next,
1813 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1815 from = vlib_frame_vector_args (frame);
1816 u32 len = vec_len (fragments_to_loopback);
1817 if (len <= VLIB_FRAME_SIZE)
1819 clib_memcpy (from, fragments_to_loopback,
1820 sizeof (u32) * len);
1822 vec_reset_length (fragments_to_loopback);
1827 fragments_to_loopback + (len -
1829 sizeof (u32) * VLIB_FRAME_SIZE);
1830 n_left_from = VLIB_FRAME_SIZE;
1831 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1836 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1839 vlib_node_increment_counter (vm, nat44_ed_out2in_reass_node.index,
1840 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1843 nat_send_all_to_node (vm, fragments_to_drop, node,
1844 &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1845 NAT44_ED_OUT2IN_NEXT_DROP);
1847 vec_free (fragments_to_drop);
1848 vec_free (fragments_to_loopback);
1849 return frame->n_vectors;
1853 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1854 .function = nat44_ed_out2in_reass_node_fn,
1855 .name = "nat44-ed-out2in-reass",
1856 .vector_size = sizeof (u32),
1857 .format_trace = format_nat44_reass_trace,
1858 .type = VLIB_NODE_TYPE_INTERNAL,
1859 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1860 .error_strings = nat_out2in_ed_error_strings,
1861 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1863 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1864 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1865 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1866 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1867 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1868 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1873 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_reass_node,
1874 nat44_ed_out2in_reass_node_fn);
1877 * fd.io coding-style-patch-verification: ON
1880 * eval: (c-set-style "gnu")