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 inside to outside 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 <vppinfra/error.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
32 #define foreach_nat_in2out_ed_error \
33 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
34 _(IN2OUT_PACKETS, "Good in2out packets processed") \
35 _(OUT_OF_PORTS, "Out of ports") \
36 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
37 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
38 _(DROP_FRAGMENT, "Drop fragment") \
39 _(MAX_REASS, "Maximum reassemblies exceeded") \
40 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
44 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
45 foreach_nat_in2out_ed_error
47 NAT_IN2OUT_ED_N_ERROR,
48 } nat_in2out_ed_error_t;
50 static char *nat_in2out_ed_error_strings[] = {
51 #define _(sym,string) string,
52 foreach_nat_in2out_ed_error
58 NAT_IN2OUT_ED_NEXT_LOOKUP,
59 NAT_IN2OUT_ED_NEXT_DROP,
60 NAT_IN2OUT_ED_NEXT_ICMP_ERROR,
61 NAT_IN2OUT_ED_NEXT_SLOW_PATH,
62 NAT_IN2OUT_ED_NEXT_REASS,
64 } nat_in2out_ed_next_t;
72 } nat_in2out_ed_trace_t;
74 vlib_node_registration_t nat44_ed_in2out_node;
75 vlib_node_registration_t nat44_ed_in2out_slowpath_node;
76 vlib_node_registration_t nat44_ed_in2out_output_node;
77 vlib_node_registration_t nat44_ed_in2out_output_slowpath_node;
78 vlib_node_registration_t nat44_ed_in2out_reass_node;
81 format_nat_in2out_ed_trace (u8 * s, va_list * args)
83 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
84 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
85 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
89 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
90 "NAT44_IN2OUT_ED_FAST_PATH";
92 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
93 t->sw_if_index, t->next_index, t->session_index);
98 static_always_inline int
99 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
101 icmp46_header_t *icmp0;
102 nat_ed_ses_key_t key0;
103 icmp_echo_header_t *echo0, *inner_echo0 = 0;
104 ip4_header_t *inner_ip0 = 0;
106 icmp46_header_t *inner_icmp0;
108 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
109 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
111 if (!icmp_is_error_message (icmp0))
113 key0.proto = IP_PROTOCOL_ICMP;
114 key0.l_addr = ip0->src_address;
115 key0.r_addr = ip0->dst_address;
116 key0.l_port = echo0->identifier;
121 inner_ip0 = (ip4_header_t *) (echo0 + 1);
122 l4_header = ip4_next_header (inner_ip0);
123 key0.proto = inner_ip0->protocol;
124 key0.r_addr = inner_ip0->src_address;
125 key0.l_addr = inner_ip0->dst_address;
126 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
128 case SNAT_PROTOCOL_ICMP:
129 inner_icmp0 = (icmp46_header_t *) l4_header;
130 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
132 key0.l_port = inner_echo0->identifier;
134 case SNAT_PROTOCOL_UDP:
135 case SNAT_PROTOCOL_TCP:
136 key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
137 key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
140 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
148 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
150 snat_main_t *sm = &snat_main;
151 nat44_is_idle_session_ctx_t *ctx = arg;
153 u64 sess_timeout_time;
154 nat_ed_ses_key_t ed_key;
155 clib_bihash_kv_16_8_t ed_kv;
158 snat_session_key_t key;
159 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
162 s = pool_elt_at_index (tsm->sessions, kv->value);
163 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
164 if (ctx->now >= sess_timeout_time)
166 if (is_fwd_bypass_session (s))
169 ed_key.l_addr = s->out2in.addr;
170 ed_key.r_addr = s->ext_host_addr;
171 ed_key.fib_index = s->out2in.fib_index;
172 if (snat_is_unk_proto_session (s))
174 ed_key.proto = s->in2out.port;
180 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
181 ed_key.l_port = s->out2in.port;
182 ed_key.r_port = s->ext_host_port;
184 ed_kv.key[0] = ed_key.as_u64[0];
185 ed_kv.key[1] = ed_key.as_u64[1];
186 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
187 nat_log_warn ("out2in_ed key del failed");
189 if (snat_is_unk_proto_session (s))
192 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
193 s->out2in.addr.as_u32,
197 s->in2out.fib_index);
199 if (is_twice_nat_session (s))
201 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
203 key.protocol = s->in2out.protocol;
204 key.port = s->ext_host_nat_port;
205 a = sm->twice_nat_addresses + i;
206 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
208 snat_free_outside_address_and_port (sm->twice_nat_addresses,
216 if (snat_is_session_static (s))
219 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
222 nat44_delete_session (sm, s, ctx->thread_index);
230 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
231 ip4_header_t * ip0, icmp46_header_t * icmp0,
232 u32 sw_if_index0, u32 rx_fib_index0,
233 vlib_node_runtime_t * node, u32 next0, f64 now,
234 u32 thread_index, snat_session_t ** p_s0)
236 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
237 next0, thread_index, p_s0, 0);
238 snat_session_t *s0 = *p_s0;
239 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP && s0))
242 nat44_session_update_counters (s0, now,
243 vlib_buffer_length_in_chain
244 (sm->vlib_main, b0));
245 /* Per-user LRU list maintenance */
246 nat44_session_update_lru (sm, s0, thread_index);
252 slow_path_ed (snat_main_t * sm,
255 clib_bihash_kv_16_8_t * kv,
256 snat_session_t ** sessionp,
257 vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
261 snat_session_key_t key0, key1;
262 lb_nat_type_t lb = 0, is_sm = 0;
263 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
264 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
265 u32 proto = ip_proto_to_snat_proto (key->proto);
266 nat_outside_fib_t *outside_fib;
267 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
269 .fp_proto = FIB_PROTOCOL_IP4,
272 .ip4.as_u32 = key->r_addr.as_u32,
275 nat44_is_idle_session_ctx_t ctx;
277 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
279 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
280 nat_ipfix_logging_max_sessions (sm->max_translations);
281 nat_log_notice ("maximum sessions exceeded");
282 return NAT_IN2OUT_ED_NEXT_DROP;
285 key0.addr = key->l_addr;
286 key0.port = key->l_port;
287 key1.protocol = key0.protocol = proto;
288 key0.fib_index = rx_fib_index;
289 key1.fib_index = sm->outside_fib_index;
290 /* First try to match static mapping by local address and port */
291 if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb, 0))
293 /* Try to create dynamic translation */
294 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
297 tsm->snat_thread_index))
299 nat_log_notice ("addresses exhausted");
300 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
301 return NAT_IN2OUT_ED_NEXT_DROP;
307 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
310 nat_log_warn ("create NAT user failed");
312 snat_free_outside_address_and_port (sm->addresses,
313 thread_index, &key1);
314 return NAT_IN2OUT_ED_NEXT_DROP;
317 s = nat_ed_session_alloc (sm, u, thread_index, now);
320 nat44_delete_user_with_no_session (sm, u, thread_index);
321 nat_log_warn ("create NAT session failed");
323 snat_free_outside_address_and_port (sm->addresses,
324 thread_index, &key1);
325 return NAT_IN2OUT_ED_NEXT_DROP;
328 user_session_increment (sm, u, is_sm);
330 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
332 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
333 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
334 s->ext_host_addr = key->r_addr;
335 s->ext_host_port = key->r_port;
338 s->out2in.protocol = key0.protocol;
340 switch (vec_len (sm->outside_fibs))
343 s->out2in.fib_index = sm->outside_fib_index;
346 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
350 vec_foreach (outside_fib, sm->outside_fibs)
352 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
353 if (FIB_NODE_INDEX_INVALID != fei)
355 if (fib_entry_get_resolving_interface (fei) != ~0)
357 s->out2in.fib_index = outside_fib->fib_index;
366 /* Add to lookup tables */
367 kv->value = s - tsm->sessions;
369 ctx.thread_index = thread_index;
370 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
371 nat44_i2o_ed_is_idle_session_cb,
373 nat_log_notice ("in2out-ed key add failed");
375 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
376 key1.port, key->r_port);
377 kv->value = s - tsm->sessions;
378 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
379 nat44_o2i_ed_is_idle_session_cb,
381 nat_log_notice ("out2in-ed key add failed");
386 snat_ipfix_logging_nat44_ses_create (s->in2out.addr.as_u32,
387 s->out2in.addr.as_u32,
390 s->out2in.port, s->in2out.fib_index);
394 static_always_inline int
395 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
396 u32 sw_if_index, ip4_header_t * ip, u32 proto,
397 u32 rx_fib_index, u32 thread_index)
399 udp_header_t *udp = ip4_next_header (ip);
400 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
401 clib_bihash_kv_16_8_t kv, value;
402 snat_session_key_t key0, key1;
404 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
405 sm->outside_fib_index, udp->dst_port, udp->src_port);
407 /* NAT packet aimed at external address if */
408 /* has active sessions */
409 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
411 key0.addr = ip->dst_address;
412 key0.port = udp->dst_port;
413 key0.protocol = proto;
414 key0.fib_index = sm->outside_fib_index;
415 /* or is static mappings */
416 if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0))
422 if (sm->forwarding_enabled)
425 return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
429 static_always_inline int
430 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
431 u32 thread_index, f64 now,
432 vlib_main_t * vm, vlib_buffer_t * b)
434 nat_ed_ses_key_t key;
435 clib_bihash_kv_16_8_t kv, value;
437 snat_session_t *s = 0;
438 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
440 if (!sm->forwarding_enabled)
443 if (ip->protocol == IP_PROTOCOL_ICMP)
445 key.as_u64[0] = key.as_u64[1] = 0;
446 if (icmp_get_ed_key (ip, &key))
449 kv.key[0] = key.as_u64[0];
450 kv.key[1] = key.as_u64[1];
452 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
454 udp = ip4_next_header (ip);
455 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
456 udp->src_port, udp->dst_port);
460 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
464 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
466 s = pool_elt_at_index (tsm->sessions, value.value);
467 if (is_fwd_bypass_session (s))
469 if (ip->protocol == IP_PROTOCOL_TCP)
471 tcp_header_t *tcp = ip4_next_header (ip);
472 if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
476 nat44_session_update_counters (s, now,
477 vlib_buffer_length_in_chain (vm, b));
478 /* Per-user LRU list maintenance */
479 nat44_session_update_lru (sm, s, thread_index);
489 static_always_inline int
490 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
491 u8 proto, u16 src_port, u16 dst_port,
492 u32 thread_index, u32 rx_sw_if_index,
495 clib_bihash_kv_16_8_t kv, value;
496 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
499 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
500 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
503 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
505 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
509 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
511 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
513 s = pool_elt_at_index (tsm->sessions, value.value);
514 if (is_fwd_bypass_session (s))
519 pool_foreach (i, sm->output_feature_interfaces,
521 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
532 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
533 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
534 u8 * p_proto, snat_session_key_t * p_value,
535 u8 * p_dont_translate, void *d, void *e)
537 icmp46_header_t *icmp;
540 nat_ed_ses_key_t key;
541 snat_session_t *s = 0;
542 u8 dont_translate = 0;
543 clib_bihash_kv_16_8_t kv, value;
546 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
548 icmp = (icmp46_header_t *) ip4_next_header (ip);
549 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
550 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
552 key.as_u64[0] = key.as_u64[1] = 0;
553 err = icmp_get_ed_key (ip, &key);
556 b->error = node->errors[err];
557 next = NAT_IN2OUT_ED_NEXT_DROP;
560 key.fib_index = rx_fib_index;
562 kv.key[0] = key.as_u64[0];
563 kv.key[1] = key.as_u64[1];
565 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
567 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
569 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
588 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
589 ip, SNAT_PROTOCOL_ICMP,
598 if (PREDICT_FALSE (icmp_is_error_message (icmp)))
600 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
601 next = NAT_IN2OUT_ED_NEXT_DROP;
605 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
606 thread_index, vlib_time_now (sm->vlib_main));
608 if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
613 if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
614 icmp->type != ICMP4_echo_reply &&
615 !icmp_is_error_message (icmp)))
617 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
618 next = NAT_IN2OUT_ED_NEXT_DROP;
622 s = pool_elt_at_index (tsm->sessions, value.value);
625 *p_proto = ip_proto_to_snat_proto (key.proto);
628 *p_value = s->out2in;
629 *p_dont_translate = dont_translate;
631 *(snat_session_t **) d = s;
635 static snat_session_t *
636 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
642 vlib_main_t * vm, vlib_node_runtime_t * node)
644 clib_bihash_kv_8_8_t kv, value;
645 clib_bihash_kv_16_8_t s_kv, s_value;
646 snat_static_mapping_t *m;
647 u32 old_addr, new_addr = 0;
650 dlist_elt_t *head, *elt;
651 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
652 u32 elt_index, head_index, ses_index;
654 u32 outside_fib_index = sm->outside_fib_index;
657 nat_outside_fib_t *outside_fib;
658 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
660 .fp_proto = FIB_PROTOCOL_IP4,
663 .ip4.as_u32 = ip->dst_address.as_u32,
667 switch (vec_len (sm->outside_fibs))
670 outside_fib_index = sm->outside_fib_index;
673 outside_fib_index = sm->outside_fibs[0].fib_index;
677 vec_foreach (outside_fib, sm->outside_fibs)
679 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
680 if (FIB_NODE_INDEX_INVALID != fei)
682 if (fib_entry_get_resolving_interface (fei) != ~0)
684 outside_fib_index = outside_fib->fib_index;
692 old_addr = ip->src_address.as_u32;
694 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
697 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
699 s = pool_elt_at_index (tsm->sessions, s_value.value);
700 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
704 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
706 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
707 nat_ipfix_logging_max_sessions (sm->max_translations);
708 nat_log_notice ("maximum sessions exceeded");
712 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
716 nat_log_warn ("create NAT user failed");
720 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
722 /* Try to find static mapping first */
723 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
725 m = pool_elt_at_index (sm->static_mappings, value.value);
726 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
730 /* Fallback to 3-tuple key */
733 /* Choose same out address as for TCP/UDP session to same destination */
734 head_index = u->sessions_per_user_list_head_index;
735 head = pool_elt_at_index (tsm->list_pool, head_index);
736 elt_index = head->next;
737 if (PREDICT_FALSE (elt_index == ~0))
741 elt = pool_elt_at_index (tsm->list_pool, elt_index);
742 ses_index = elt->value;
745 while (ses_index != ~0)
747 s = pool_elt_at_index (tsm->sessions, ses_index);
748 elt_index = elt->next;
749 elt = pool_elt_at_index (tsm->list_pool, elt_index);
750 ses_index = elt->value;
752 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
754 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
756 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
757 ip->protocol, outside_fib_index, 0, 0);
758 if (clib_bihash_search_16_8
759 (&tsm->out2in_ed, &s_kv, &s_value))
766 for (i = 0; i < vec_len (sm->addresses); i++)
768 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
769 ip->protocol, outside_fib_index, 0, 0);
770 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
772 new_addr = ip->src_address.as_u32 =
773 sm->addresses[i].addr.as_u32;
781 s = nat_ed_session_alloc (sm, u, thread_index, now);
784 nat44_delete_user_with_no_session (sm, u, thread_index);
785 nat_log_warn ("create NAT session failed");
789 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
790 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
791 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
792 s->out2in.addr.as_u32 = new_addr;
793 s->out2in.fib_index = outside_fib_index;
794 s->in2out.addr.as_u32 = old_addr;
795 s->in2out.fib_index = rx_fib_index;
796 s->in2out.port = s->out2in.port = ip->protocol;
798 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
799 user_session_increment (sm, u, is_sm);
801 /* Add to lookup tables */
802 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
804 s_kv.value = s - tsm->sessions;
805 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
806 nat_log_notice ("in2out key add failed");
808 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
809 outside_fib_index, 0, 0);
810 s_kv.value = s - tsm->sessions;
811 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
812 nat_log_notice ("out2in key add failed");
815 /* Update IP checksum */
817 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
818 ip->checksum = ip_csum_fold (sum);
821 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
822 /* Per-user LRU list maintenance */
823 nat44_session_update_lru (sm, s, thread_index);
826 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
827 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
829 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
830 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
836 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
837 vlib_node_runtime_t * node,
838 vlib_frame_t * frame, int is_slow_path,
839 int is_output_feature)
841 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
842 nat_in2out_ed_next_t next_index;
843 snat_main_t *sm = &snat_main;
844 f64 now = vlib_time_now (vm);
845 u32 thread_index = vm->thread_index;
846 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
848 stats_node_index = is_slow_path ? nat44_ed_in2out_slowpath_node.index :
849 nat44_ed_in2out_node.index;
851 from = vlib_frame_vector_args (frame);
852 n_left_from = frame->n_vectors;
853 next_index = node->cached_next_index;
855 while (n_left_from > 0)
859 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
861 while (n_left_from >= 4 && n_left_to_next >= 2)
864 vlib_buffer_t *b0, *b1;
865 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
866 new_addr0, old_addr0;
867 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
868 new_addr1, old_addr1;
869 u16 old_port0, new_port0, old_port1, new_port1;
870 ip4_header_t *ip0, *ip1;
871 udp_header_t *udp0, *udp1;
872 tcp_header_t *tcp0, *tcp1;
873 icmp46_header_t *icmp0, *icmp1;
874 snat_session_t *s0 = 0, *s1 = 0;
875 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
876 ip_csum_t sum0, sum1;
878 /* Prefetch next iteration. */
880 vlib_buffer_t *p2, *p3;
882 p2 = vlib_get_buffer (vm, from[2]);
883 p3 = vlib_get_buffer (vm, from[3]);
885 vlib_prefetch_buffer_header (p2, LOAD);
886 vlib_prefetch_buffer_header (p3, LOAD);
888 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
889 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
892 /* speculatively enqueue b0 and b1 to the current next frame */
893 to_next[0] = bi0 = from[0];
894 to_next[1] = bi1 = from[1];
900 b0 = vlib_get_buffer (vm, bi0);
901 b1 = vlib_get_buffer (vm, bi1);
903 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
905 if (is_output_feature)
906 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
908 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
911 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
913 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
916 if (PREDICT_FALSE (ip0->ttl == 1))
918 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
919 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
920 ICMP4_time_exceeded_ttl_exceeded_in_transit,
922 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
926 udp0 = ip4_next_header (ip0);
927 tcp0 = (tcp_header_t *) udp0;
928 icmp0 = (icmp46_header_t *) udp0;
929 proto0 = ip_proto_to_snat_proto (ip0->protocol);
933 if (PREDICT_FALSE (proto0 == ~0))
935 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
937 thread_index, now, vm,
940 next0 = NAT_IN2OUT_ED_NEXT_DROP;
944 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
946 next0 = icmp_in2out_ed_slow_path
947 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
948 next0, now, thread_index, &s0);
954 if (PREDICT_FALSE (proto0 == ~0))
956 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
960 if (ip4_is_fragment (ip0))
962 next0 = NAT_IN2OUT_ED_NEXT_REASS;
966 if (is_output_feature)
969 (nat_not_translate_output_feature_fwd
970 (sm, ip0, thread_index, now, vm, b0)))
974 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
976 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
981 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
982 ip0->protocol, rx_fib_index0, udp0->src_port,
985 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
989 if (is_output_feature)
992 (nat44_ed_not_translate_output_feature
993 (sm, ip0, ip0->protocol, udp0->src_port,
994 udp0->dst_port, thread_index, sw_if_index0,
995 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1000 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1009 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1010 next0, thread_index, now);
1012 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1017 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1023 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1026 b0->flags |= VNET_BUFFER_F_IS_NATED;
1028 if (!is_output_feature)
1029 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1031 old_addr0 = ip0->src_address.as_u32;
1032 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1033 sum0 = ip0->checksum;
1034 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1036 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1037 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1038 s0->ext_host_addr.as_u32, ip4_header_t,
1040 ip0->checksum = ip_csum_fold (sum0);
1042 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1044 old_port0 = tcp0->src_port;
1045 new_port0 = tcp0->src_port = s0->out2in.port;
1047 sum0 = tcp0->checksum;
1048 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1050 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1052 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1054 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1055 s0->ext_host_addr.as_u32,
1056 ip4_header_t, dst_address);
1057 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1058 s0->ext_host_port, ip4_header_t,
1060 tcp0->dst_port = s0->ext_host_port;
1061 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1063 mss_clamping (sm, tcp0, &sum0);
1064 tcp0->checksum = ip_csum_fold (sum0);
1065 if (nat44_set_tcp_session_state_i2o
1066 (sm, s0, tcp0, thread_index))
1071 udp0->src_port = s0->out2in.port;
1073 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1075 udp0->dst_port = s0->ext_host_port;
1076 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1081 nat44_session_update_counters (s0, now,
1082 vlib_buffer_length_in_chain (vm,
1084 /* Per-user LRU list maintenance */
1085 nat44_session_update_lru (sm, s0, thread_index);
1088 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1089 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1091 nat_in2out_ed_trace_t *t =
1092 vlib_add_trace (vm, node, b0, sizeof (*t));
1093 t->is_slow_path = is_slow_path;
1094 t->sw_if_index = sw_if_index0;
1095 t->next_index = next0;
1096 t->session_index = ~0;
1098 t->session_index = s0 - tsm->sessions;
1101 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
1104 next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1106 if (is_output_feature)
1107 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1109 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1112 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1114 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1117 if (PREDICT_FALSE (ip1->ttl == 1))
1119 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1120 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1121 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1123 next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1127 udp1 = ip4_next_header (ip1);
1128 tcp1 = (tcp_header_t *) udp1;
1129 icmp1 = (icmp46_header_t *) udp1;
1130 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1134 if (PREDICT_FALSE (proto1 == ~0))
1136 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1138 thread_index, now, vm,
1141 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1145 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1147 next1 = icmp_in2out_ed_slow_path
1148 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1149 next1, now, thread_index, &s1);
1155 if (PREDICT_FALSE (proto1 == ~0))
1157 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1161 if (ip4_is_fragment (ip1))
1163 next1 = NAT_IN2OUT_ED_NEXT_REASS;
1167 if (is_output_feature)
1170 (nat_not_translate_output_feature_fwd
1171 (sm, ip1, thread_index, now, vm, b1)))
1175 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1177 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1182 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1183 ip1->protocol, rx_fib_index1, udp1->src_port,
1186 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1190 if (is_output_feature)
1193 (nat44_ed_not_translate_output_feature
1194 (sm, ip1, ip1->protocol, udp1->src_port,
1195 udp1->dst_port, thread_index, sw_if_index1,
1196 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1201 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1210 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1211 next1, thread_index, now);
1213 if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1218 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1224 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1227 b1->flags |= VNET_BUFFER_F_IS_NATED;
1229 if (!is_output_feature)
1230 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1232 old_addr1 = ip1->src_address.as_u32;
1233 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1234 sum1 = ip1->checksum;
1235 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1237 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1238 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1239 s1->ext_host_addr.as_u32, ip4_header_t,
1241 ip1->checksum = ip_csum_fold (sum1);
1243 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1245 old_port1 = tcp1->src_port;
1246 new_port1 = tcp1->src_port = s1->out2in.port;
1248 sum1 = tcp1->checksum;
1249 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1251 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1253 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1255 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1256 s1->ext_host_addr.as_u32,
1257 ip4_header_t, dst_address);
1258 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1259 s1->ext_host_port, ip4_header_t,
1261 tcp1->dst_port = s1->ext_host_port;
1262 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1264 tcp1->checksum = ip_csum_fold (sum1);
1265 mss_clamping (sm, tcp1, &sum1);
1266 if (nat44_set_tcp_session_state_i2o
1267 (sm, s1, tcp1, thread_index))
1272 udp1->src_port = s1->out2in.port;
1274 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1276 udp1->dst_port = s1->ext_host_port;
1277 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1282 nat44_session_update_counters (s1, now,
1283 vlib_buffer_length_in_chain (vm,
1285 /* Per-user LRU list maintenance */
1286 nat44_session_update_lru (sm, s1, thread_index);
1289 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1290 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1292 nat_in2out_ed_trace_t *t =
1293 vlib_add_trace (vm, node, b1, sizeof (*t));
1294 t->is_slow_path = is_slow_path;
1295 t->sw_if_index = sw_if_index1;
1296 t->next_index = next1;
1297 t->session_index = ~0;
1299 t->session_index = s1 - tsm->sessions;
1302 pkts_processed += next1 != NAT_IN2OUT_ED_NEXT_DROP;
1304 /* verify speculative enqueues, maybe switch current next frame */
1305 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1306 to_next, n_left_to_next,
1307 bi0, bi1, next0, next1);
1310 while (n_left_from > 0 && n_left_to_next > 0)
1314 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1315 new_addr0, old_addr0;
1316 u16 old_port0, new_port0;
1320 icmp46_header_t *icmp0;
1321 snat_session_t *s0 = 0;
1322 clib_bihash_kv_16_8_t kv0, value0;
1325 /* speculatively enqueue b0 to the current next frame */
1331 n_left_to_next -= 1;
1333 b0 = vlib_get_buffer (vm, bi0);
1334 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1336 if (is_output_feature)
1337 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1339 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1342 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1344 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1347 if (PREDICT_FALSE (ip0->ttl == 1))
1349 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1350 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1351 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1353 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1357 udp0 = ip4_next_header (ip0);
1358 tcp0 = (tcp_header_t *) udp0;
1359 icmp0 = (icmp46_header_t *) udp0;
1360 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1364 if (PREDICT_FALSE (proto0 == ~0))
1366 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1368 thread_index, now, vm,
1371 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1375 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1377 next0 = icmp_in2out_ed_slow_path
1378 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1379 next0, now, thread_index, &s0);
1385 if (PREDICT_FALSE (proto0 == ~0))
1387 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1391 if (ip4_is_fragment (ip0))
1393 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1397 if (is_output_feature)
1400 (nat_not_translate_output_feature_fwd
1401 (sm, ip0, thread_index, now, vm, b0)))
1405 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1407 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1412 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1413 ip0->protocol, rx_fib_index0, udp0->src_port,
1416 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1420 if (is_output_feature)
1423 (nat44_ed_not_translate_output_feature
1424 (sm, ip0, ip0->protocol, udp0->src_port,
1425 udp0->dst_port, thread_index, sw_if_index0,
1426 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1431 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1440 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1441 next0, thread_index, now);
1443 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1448 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1454 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1457 b0->flags |= VNET_BUFFER_F_IS_NATED;
1459 if (!is_output_feature)
1460 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1462 old_addr0 = ip0->src_address.as_u32;
1463 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1464 sum0 = ip0->checksum;
1465 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1467 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1468 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1469 s0->ext_host_addr.as_u32, ip4_header_t,
1471 ip0->checksum = ip_csum_fold (sum0);
1473 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1475 old_port0 = tcp0->src_port;
1476 new_port0 = tcp0->src_port = s0->out2in.port;
1478 sum0 = tcp0->checksum;
1479 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1481 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1483 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1485 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1486 s0->ext_host_addr.as_u32,
1487 ip4_header_t, dst_address);
1488 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1489 s0->ext_host_port, ip4_header_t,
1491 tcp0->dst_port = s0->ext_host_port;
1492 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1494 mss_clamping (sm, tcp0, &sum0);
1495 tcp0->checksum = ip_csum_fold (sum0);
1496 if (nat44_set_tcp_session_state_i2o
1497 (sm, s0, tcp0, thread_index))
1502 udp0->src_port = s0->out2in.port;
1504 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1506 udp0->dst_port = s0->ext_host_port;
1507 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1512 nat44_session_update_counters (s0, now,
1513 vlib_buffer_length_in_chain (vm,
1515 /* Per-user LRU list maintenance */
1516 nat44_session_update_lru (sm, s0, thread_index);
1519 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1520 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1522 nat_in2out_ed_trace_t *t =
1523 vlib_add_trace (vm, node, b0, sizeof (*t));
1524 t->is_slow_path = is_slow_path;
1525 t->sw_if_index = sw_if_index0;
1526 t->next_index = next0;
1527 t->session_index = ~0;
1529 t->session_index = s0 - tsm->sessions;
1532 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
1534 /* verify speculative enqueue, maybe switch current next frame */
1535 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1536 to_next, n_left_to_next,
1540 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1543 vlib_node_increment_counter (vm, stats_node_index,
1544 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1546 return frame->n_vectors;
1550 nat44_ed_in2out_fast_path_fn (vlib_main_t * vm,
1551 vlib_node_runtime_t * node,
1552 vlib_frame_t * frame)
1554 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1558 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1559 .function = nat44_ed_in2out_fast_path_fn,
1560 .name = "nat44-ed-in2out",
1561 .vector_size = sizeof (u32),
1562 .format_trace = format_nat_in2out_ed_trace,
1563 .type = VLIB_NODE_TYPE_INTERNAL,
1564 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1565 .error_strings = nat_in2out_ed_error_strings,
1566 .runtime_data_bytes = sizeof (snat_runtime_t),
1567 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1569 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1570 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1571 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1572 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1573 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1578 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_node,
1579 nat44_ed_in2out_fast_path_fn);
1582 nat44_ed_in2out_output_fast_path_fn (vlib_main_t * vm,
1583 vlib_node_runtime_t * node,
1584 vlib_frame_t * frame)
1586 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1590 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1591 .function = nat44_ed_in2out_output_fast_path_fn,
1592 .name = "nat44-ed-in2out-output",
1593 .vector_size = sizeof (u32),
1594 .format_trace = format_nat_in2out_ed_trace,
1595 .type = VLIB_NODE_TYPE_INTERNAL,
1596 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1597 .error_strings = nat_in2out_ed_error_strings,
1598 .runtime_data_bytes = sizeof (snat_runtime_t),
1599 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1601 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1602 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1603 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1604 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1605 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1610 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_node,
1611 nat44_ed_in2out_output_fast_path_fn);
1614 nat44_ed_in2out_slow_path_fn (vlib_main_t * vm,
1615 vlib_node_runtime_t * node,
1616 vlib_frame_t * frame)
1618 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1622 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1623 .function = nat44_ed_in2out_slow_path_fn,
1624 .name = "nat44-ed-in2out-slowpath",
1625 .vector_size = sizeof (u32),
1626 .format_trace = format_nat_in2out_ed_trace,
1627 .type = VLIB_NODE_TYPE_INTERNAL,
1628 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1629 .error_strings = nat_in2out_ed_error_strings,
1630 .runtime_data_bytes = sizeof (snat_runtime_t),
1631 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1633 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1634 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1635 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1636 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1637 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1642 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_slowpath_node,
1643 nat44_ed_in2out_slow_path_fn);
1646 nat44_ed_in2out_output_slow_path_fn (vlib_main_t * vm,
1647 vlib_node_runtime_t * node,
1648 vlib_frame_t * frame)
1650 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1654 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1655 .function = nat44_ed_in2out_output_slow_path_fn,
1656 .name = "nat44-ed-in2out-output-slowpath",
1657 .vector_size = sizeof (u32),
1658 .format_trace = format_nat_in2out_ed_trace,
1659 .type = VLIB_NODE_TYPE_INTERNAL,
1660 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1661 .error_strings = nat_in2out_ed_error_strings,
1662 .runtime_data_bytes = sizeof (snat_runtime_t),
1663 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1665 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1666 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1667 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1668 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1669 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1674 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_slowpath_node,
1675 nat44_ed_in2out_output_slow_path_fn);
1678 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1679 vlib_node_runtime_t * node,
1680 vlib_frame_t * frame,
1681 int is_output_feature)
1683 u32 n_left_from, *from, *to_next;
1684 nat_in2out_ed_next_t next_index;
1685 u32 pkts_processed = 0;
1686 snat_main_t *sm = &snat_main;
1687 f64 now = vlib_time_now (vm);
1688 u32 thread_index = vm->thread_index;
1689 snat_main_per_thread_data_t *per_thread_data =
1690 &sm->per_thread_data[thread_index];
1691 u32 *fragments_to_drop = 0;
1692 u32 *fragments_to_loopback = 0;
1694 from = vlib_frame_vector_args (frame);
1695 n_left_from = frame->n_vectors;
1696 next_index = node->cached_next_index;
1698 while (n_left_from > 0)
1702 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1704 while (n_left_from > 0 && n_left_to_next > 0)
1706 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1707 u32 iph_offset0 = 0;
1711 ip4_header_t *ip0 = 0;
1712 nat_reass_ip4_t *reass0;
1715 icmp46_header_t *icmp0;
1716 clib_bihash_kv_16_8_t kv0, value0;
1717 snat_session_t *s0 = 0;
1718 u16 old_port0, new_port0;
1721 /* speculatively enqueue b0 to the current next frame */
1727 n_left_to_next -= 1;
1729 b0 = vlib_get_buffer (vm, bi0);
1731 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1733 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1735 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1738 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1740 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1741 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1745 if (is_output_feature)
1746 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1748 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1751 udp0 = ip4_next_header (ip0);
1752 tcp0 = (tcp_header_t *) udp0;
1753 icmp0 = (icmp46_header_t *) udp0;
1754 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1756 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1760 1, &fragments_to_drop);
1762 if (PREDICT_FALSE (!reass0))
1764 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1765 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1766 nat_log_notice ("maximum reassemblies exceeded");
1770 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1772 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1774 if (is_output_feature)
1777 (nat_not_translate_output_feature_fwd
1778 (sm, ip0, thread_index, now, vm, b0)))
1779 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1783 next0 = icmp_in2out_ed_slow_path
1784 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1785 next0, now, thread_index, &s0);
1787 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1790 reass0->sess_index = s0 - per_thread_data->sessions;
1792 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1793 nat_ip4_reass_get_frags (reass0,
1794 &fragments_to_loopback);
1800 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1801 ip0->protocol, rx_fib_index0, udp0->src_port,
1804 if (clib_bihash_search_16_8
1805 (&per_thread_data->in2out_ed, &kv0, &value0))
1807 if (is_output_feature)
1810 (nat44_ed_not_translate_output_feature
1811 (sm, ip0, ip0->protocol, udp0->src_port,
1812 udp0->dst_port, thread_index, sw_if_index0,
1813 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1815 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1816 nat_ip4_reass_get_frags (reass0,
1817 &fragments_to_loopback);
1823 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1829 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1830 nat_ip4_reass_get_frags (reass0,
1831 &fragments_to_loopback);
1836 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1837 &s0, node, next0, thread_index, now);
1839 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1842 reass0->sess_index = s0 - per_thread_data->sessions;
1846 s0 = pool_elt_at_index (per_thread_data->sessions,
1848 reass0->sess_index = value0.value;
1850 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1854 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1856 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1858 if (nat_ip4_reass_add_fragment
1859 (reass0, bi0, &fragments_to_drop))
1861 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1863 ("maximum fragments per reassembly exceeded");
1864 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1870 s0 = pool_elt_at_index (per_thread_data->sessions,
1871 reass0->sess_index);
1874 old_addr0 = ip0->src_address.as_u32;
1875 ip0->src_address = s0->out2in.addr;
1876 new_addr0 = ip0->src_address.as_u32;
1877 if (!is_output_feature)
1878 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1880 sum0 = ip0->checksum;
1881 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1883 src_address /* changed member */ );
1884 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1885 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1886 s0->ext_host_addr.as_u32, ip4_header_t,
1888 ip0->checksum = ip_csum_fold (sum0);
1890 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1892 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1894 old_port0 = tcp0->src_port;
1895 tcp0->src_port = s0->out2in.port;
1896 new_port0 = tcp0->src_port;
1898 sum0 = tcp0->checksum;
1899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1901 dst_address /* changed member */ );
1902 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1903 ip4_header_t /* cheat */ ,
1904 length /* changed member */ );
1905 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1907 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1908 s0->ext_host_addr.as_u32,
1909 ip4_header_t, dst_address);
1910 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1911 s0->ext_host_port, ip4_header_t,
1913 tcp0->dst_port = s0->ext_host_port;
1914 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1916 tcp0->checksum = ip_csum_fold (sum0);
1920 old_port0 = udp0->src_port;
1921 udp0->src_port = s0->out2in.port;
1923 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1925 udp0->dst_port = s0->ext_host_port;
1926 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1932 if (PREDICT_TRUE (proto0 != SNAT_PROTOCOL_ICMP))
1933 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1934 s0->ext_host_port, proto0, 1);
1936 snat_icmp_hairpinning (sm, b0, ip0, icmp0, 1);
1939 nat44_session_update_counters (s0, now,
1940 vlib_buffer_length_in_chain (vm,
1942 /* Per-user LRU list maintenance */
1943 nat44_session_update_lru (sm, s0, thread_index);
1946 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1947 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1949 nat44_reass_trace_t *t =
1950 vlib_add_trace (vm, node, b0, sizeof (*t));
1951 t->cached = cached0;
1952 t->sw_if_index = sw_if_index0;
1953 t->next_index = next0;
1963 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
1965 /* verify speculative enqueue, maybe switch current next frame */
1966 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1967 to_next, n_left_to_next,
1971 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1973 from = vlib_frame_vector_args (frame);
1974 u32 len = vec_len (fragments_to_loopback);
1975 if (len <= VLIB_FRAME_SIZE)
1977 clib_memcpy (from, fragments_to_loopback,
1978 sizeof (u32) * len);
1980 vec_reset_length (fragments_to_loopback);
1985 fragments_to_loopback + (len -
1987 sizeof (u32) * VLIB_FRAME_SIZE);
1988 n_left_from = VLIB_FRAME_SIZE;
1989 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1994 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1997 vlib_node_increment_counter (vm, nat44_ed_in2out_reass_node.index,
1998 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
2001 nat_send_all_to_node (vm, fragments_to_drop, node,
2002 &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2003 NAT_IN2OUT_ED_NEXT_DROP);
2005 vec_free (fragments_to_drop);
2006 vec_free (fragments_to_loopback);
2007 return frame->n_vectors;
2011 nat44_ed_in2out_reass_node_fn (vlib_main_t * vm,
2012 vlib_node_runtime_t * node,
2013 vlib_frame_t * frame)
2015 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2019 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2020 .function = nat44_ed_in2out_reass_node_fn,
2021 .name = "nat44-ed-in2out-reass",
2022 .vector_size = sizeof (u32),
2023 .format_trace = format_nat44_reass_trace,
2024 .type = VLIB_NODE_TYPE_INTERNAL,
2025 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2026 .error_strings = nat_in2out_ed_error_strings,
2027 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2029 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2030 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2031 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2032 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2033 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2038 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_node,
2039 nat44_ed_in2out_reass_node_fn);
2042 nat44_ed_in2out_reass_output_node_fn (vlib_main_t * vm,
2043 vlib_node_runtime_t * node,
2044 vlib_frame_t * frame)
2046 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2050 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2051 .function = nat44_ed_in2out_reass_output_node_fn,
2052 .name = "nat44-ed-in2out-reass-output",
2053 .vector_size = sizeof (u32),
2054 .format_trace = format_nat44_reass_trace,
2055 .type = VLIB_NODE_TYPE_INTERNAL,
2056 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2057 .error_strings = nat_in2out_ed_error_strings,
2058 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2060 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2061 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2062 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2063 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2064 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2069 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_output_node,
2070 nat44_ed_in2out_reass_output_node_fn);
2073 * fd.io coding-style-patch-verification: ON
2076 * eval: (c-set-style "gnu")