2 * Copyright (c) 2016 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.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/handoff.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/fib/ip4_fib.h>
25 #include <nat/nat_ipfix_logging.h>
26 #include <nat/nat_det.h>
27 #include <nat/nat_reass.h>
28 #include <nat/nat_inlines.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/error.h>
32 #include <vppinfra/elog.h>
39 } snat_in2out_trace_t;
42 u32 next_worker_index;
44 } snat_in2out_worker_handoff_trace_t;
46 /* packet trace format function */
47 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
54 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
56 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
57 t->sw_if_index, t->next_index, t->session_index);
62 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
68 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
69 t->sw_if_index, t->next_index);
74 static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args)
76 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
77 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
78 snat_in2out_worker_handoff_trace_t * t =
79 va_arg (*args, snat_in2out_worker_handoff_trace_t *);
82 m = t->do_handoff ? "next worker" : "same worker";
83 s = format (s, "NAT44_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
92 } nat44_in2out_reass_trace_t;
94 static u8 * format_nat44_in2out_reass_trace (u8 * s, va_list * args)
96 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
97 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
98 nat44_in2out_reass_trace_t * t = va_arg (*args, nat44_in2out_reass_trace_t *);
100 s = format (s, "NAT44_IN2OUT_REASS: sw_if_index %d, next index %d, status %s",
101 t->sw_if_index, t->next_index,
102 t->cached ? "cached" : "translated");
107 vlib_node_registration_t snat_in2out_node;
108 vlib_node_registration_t snat_in2out_slowpath_node;
109 vlib_node_registration_t snat_in2out_fast_node;
110 vlib_node_registration_t snat_in2out_worker_handoff_node;
111 vlib_node_registration_t snat_det_in2out_node;
112 vlib_node_registration_t snat_in2out_output_node;
113 vlib_node_registration_t snat_in2out_output_slowpath_node;
114 vlib_node_registration_t snat_in2out_output_worker_handoff_node;
115 vlib_node_registration_t snat_hairpin_dst_node;
116 vlib_node_registration_t snat_hairpin_src_node;
117 vlib_node_registration_t nat44_hairpinning_node;
118 vlib_node_registration_t nat44_in2out_reass_node;
119 vlib_node_registration_t nat44_ed_in2out_node;
120 vlib_node_registration_t nat44_ed_in2out_slowpath_node;
121 vlib_node_registration_t nat44_ed_in2out_output_node;
122 vlib_node_registration_t nat44_ed_in2out_output_slowpath_node;
123 vlib_node_registration_t nat44_ed_hairpin_dst_node;
124 vlib_node_registration_t nat44_ed_hairpin_src_node;
125 vlib_node_registration_t nat44_ed_hairpinning_node;
127 #define foreach_snat_in2out_error \
128 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
129 _(IN2OUT_PACKETS, "Good in2out packets processed") \
130 _(OUT_OF_PORTS, "Out of ports") \
131 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
132 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
133 _(NO_TRANSLATION, "No translation") \
134 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
135 _(DROP_FRAGMENT, "Drop fragment") \
136 _(MAX_REASS, "Maximum reassemblies exceeded") \
137 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")\
138 _(FQ_CONGESTED, "Handoff frame queue congested")
141 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
142 foreach_snat_in2out_error
145 } snat_in2out_error_t;
147 static char * snat_in2out_error_strings[] = {
148 #define _(sym,string) string,
149 foreach_snat_in2out_error
154 SNAT_IN2OUT_NEXT_LOOKUP,
155 SNAT_IN2OUT_NEXT_DROP,
156 SNAT_IN2OUT_NEXT_ICMP_ERROR,
157 SNAT_IN2OUT_NEXT_SLOW_PATH,
158 SNAT_IN2OUT_NEXT_REASS,
160 } snat_in2out_next_t;
163 SNAT_HAIRPIN_SRC_NEXT_DROP,
164 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT,
165 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH,
166 SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT,
167 SNAT_HAIRPIN_SRC_N_NEXT,
168 } snat_hairpin_next_t;
171 * @brief Check if packet should be translated
173 * Packets aimed at outside interface and external address with active session
174 * should be translated.
177 * @param rt NAT runtime data
178 * @param sw_if_index0 index of the inside interface
179 * @param ip0 IPv4 header
180 * @param proto0 NAT protocol
181 * @param rx_fib_index0 RX FIB index
183 * @returns 0 if packet should be translated otherwise 1
186 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
187 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
193 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
194 nat_outside_fib_t *outside_fib;
196 .fp_proto = FIB_PROTOCOL_IP4,
199 .ip4.as_u32 = ip0->dst_address.as_u32,
203 /* Don't NAT packet aimed at the intfc address */
204 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
205 ip0->dst_address.as_u32)))
208 fei = fib_table_lookup (rx_fib_index0, &pfx);
209 if (FIB_NODE_INDEX_INVALID != fei)
211 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
212 if (sw_if_index == ~0)
214 vec_foreach (outside_fib, sm->outside_fibs)
216 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
217 if (FIB_NODE_INDEX_INVALID != fei)
219 sw_if_index = fib_entry_get_resolving_interface (fei);
220 if (sw_if_index != ~0)
225 if (sw_if_index == ~0)
229 pool_foreach (i, sm->interfaces,
231 /* NAT packet aimed at outside interface */
232 if ((nat_interface_is_outside(i)) && (sw_if_index == i->sw_if_index))
241 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
242 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
243 u32 rx_fib_index0, u32 thread_index)
245 udp_header_t * udp0 = ip4_next_header (ip0);
246 snat_session_key_t key0, sm0;
247 clib_bihash_kv_8_8_t kv0, value0;
249 key0.addr = ip0->dst_address;
250 key0.port = udp0->dst_port;
251 key0.protocol = proto0;
252 key0.fib_index = sm->outside_fib_index;
253 kv0.key = key0.as_u64;
255 /* NAT packet aimed at external address if */
256 /* has active sessions */
257 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
260 /* or is static mappings */
261 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
267 if (sm->forwarding_enabled)
270 return snat_not_translate_fast(sm, node, sw_if_index0, ip0, proto0,
275 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
276 u32 proto0, u16 src_port, u16 dst_port,
277 u32 thread_index, u32 sw_if_index)
279 snat_session_key_t key0;
280 clib_bihash_kv_8_8_t kv0, value0;
284 key0.addr = ip0->src_address;
285 key0.port = src_port;
286 key0.protocol = proto0;
287 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
288 kv0.key = key0.as_u64;
290 if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
295 key0.addr = ip0->dst_address;
296 key0.port = dst_port;
297 key0.protocol = proto0;
298 kv0.key = key0.as_u64;
299 if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
303 pool_foreach (i, sm->output_feature_interfaces,
305 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
315 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void * arg)
317 snat_main_t *sm = &snat_main;
318 nat44_is_idle_session_ctx_t *ctx = arg;
320 u64 sess_timeout_time;
321 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
323 clib_bihash_kv_8_8_t s_kv;
325 s = pool_elt_at_index (tsm->sessions, kv->value);
326 sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
327 if (ctx->now >= sess_timeout_time)
329 s_kv.key = s->out2in.as_u64;
330 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
331 nat_log_warn ("out2in key del failed");
333 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
334 s->out2in.addr.as_u32,
338 s->in2out.fib_index);
340 if (!snat_is_session_static (s))
341 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
344 nat44_delete_session (sm, s, ctx->thread_index);
351 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
354 snat_session_key_t * key0,
355 snat_session_t ** sessionp,
356 vlib_node_runtime_t * node,
363 clib_bihash_kv_8_8_t kv0;
364 snat_session_key_t key1;
365 u32 address_index = ~0;
366 udp_header_t * udp0 = ip4_next_header (ip0);
368 nat_outside_fib_t *outside_fib;
369 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
371 .fp_proto = FIB_PROTOCOL_IP4,
374 .ip4.as_u32 = ip0->dst_address.as_u32,
377 nat44_is_idle_session_ctx_t ctx0;
379 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
381 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
382 nat_ipfix_logging_max_sessions(sm->max_translations);
383 nat_log_notice ("maximum sessions exceeded");
384 return SNAT_IN2OUT_NEXT_DROP;
387 key1.protocol = key0->protocol;
389 /* First try to match static mapping by local address and port */
390 if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0))
392 /* Try to create dynamic translation */
393 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
397 sm->per_thread_data[thread_index].snat_thread_index))
399 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
400 return SNAT_IN2OUT_NEXT_DROP;
406 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
410 nat_log_warn ("create NAT user failed");
411 return SNAT_IN2OUT_NEXT_DROP;
414 s = nat_session_alloc_or_recycle (sm, u, thread_index);
417 nat44_delete_user_with_no_session (sm, u, thread_index);
418 nat_log_warn ("create NAT session failed");
419 return SNAT_IN2OUT_NEXT_DROP;
423 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
424 user_session_increment (sm, u, is_sm);
425 s->outside_address_index = address_index;
428 s->out2in.protocol = key0->protocol;
429 s->out2in.fib_index = sm->outside_fib_index;
430 switch (vec_len (sm->outside_fibs))
433 s->out2in.fib_index = sm->outside_fib_index;
436 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
439 vec_foreach (outside_fib, sm->outside_fibs)
441 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
442 if (FIB_NODE_INDEX_INVALID != fei)
444 if (fib_entry_get_resolving_interface (fei) != ~0)
446 s->out2in.fib_index = outside_fib->fib_index;
453 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
454 s->ext_host_port = udp0->dst_port;
457 /* Add to translation hashes */
459 ctx0.thread_index = thread_index;
460 kv0.key = s->in2out.as_u64;
461 kv0.value = s - sm->per_thread_data[thread_index].sessions;
462 if (clib_bihash_add_or_overwrite_stale_8_8 (
463 &sm->per_thread_data[thread_index].in2out, &kv0,
464 nat44_i2o_is_idle_session_cb, &ctx0))
465 nat_log_notice ("in2out key add failed");
467 kv0.key = s->out2in.as_u64;
468 kv0.value = s - sm->per_thread_data[thread_index].sessions;
470 if (clib_bihash_add_or_overwrite_stale_8_8 (
471 &sm->per_thread_data[thread_index].out2in, &kv0,
472 nat44_o2i_is_idle_session_cb, &ctx0))
473 nat_log_notice ("out2in key add failed");
476 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
477 s->out2in.addr.as_u32,
481 s->in2out.fib_index);
486 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
487 snat_session_key_t *p_key0)
489 icmp46_header_t *icmp0;
490 snat_session_key_t key0;
491 icmp_echo_header_t *echo0, *inner_echo0 = 0;
492 ip4_header_t *inner_ip0 = 0;
494 icmp46_header_t *inner_icmp0;
496 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
497 echo0 = (icmp_echo_header_t *)(icmp0+1);
499 if (!icmp_is_error_message (icmp0))
501 key0.protocol = SNAT_PROTOCOL_ICMP;
502 key0.addr = ip0->src_address;
503 key0.port = echo0->identifier;
507 inner_ip0 = (ip4_header_t *)(echo0+1);
508 l4_header = ip4_next_header (inner_ip0);
509 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
510 key0.addr = inner_ip0->dst_address;
511 switch (key0.protocol)
513 case SNAT_PROTOCOL_ICMP:
514 inner_icmp0 = (icmp46_header_t*)l4_header;
515 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
516 key0.port = inner_echo0->identifier;
518 case SNAT_PROTOCOL_UDP:
519 case SNAT_PROTOCOL_TCP:
520 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
523 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
527 return -1; /* success */
531 * Get address and port values to be used for ICMP packet translation
532 * and create session if needed
534 * @param[in,out] sm NAT main
535 * @param[in,out] node NAT node runtime
536 * @param[in] thread_index thread index
537 * @param[in,out] b0 buffer containing packet to be translated
538 * @param[out] p_proto protocol used for matching
539 * @param[out] p_value address and port after NAT translation
540 * @param[out] p_dont_translate if packet should not be translated
541 * @param d optional parameter
542 * @param e optional parameter
544 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
545 u32 thread_index, vlib_buffer_t *b0,
546 ip4_header_t *ip0, u8 *p_proto,
547 snat_session_key_t *p_value,
548 u8 *p_dont_translate, void *d, void *e)
550 icmp46_header_t *icmp0;
553 snat_session_key_t key0;
554 snat_session_t *s0 = 0;
555 u8 dont_translate = 0;
556 clib_bihash_kv_8_8_t kv0, value0;
560 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
561 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
562 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
564 err = icmp_get_key (ip0, &key0);
567 b0->error = node->errors[err];
568 next0 = SNAT_IN2OUT_NEXT_DROP;
571 key0.fib_index = rx_fib_index0;
573 kv0.key = key0.as_u64;
575 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
578 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] != ~0)
580 if (PREDICT_FALSE(nat_not_translate_output_feature(sm, ip0,
581 key0.protocol, key0.port, key0.port, thread_index, sw_if_index0)))
589 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
590 ip0, SNAT_PROTOCOL_ICMP, rx_fib_index0, thread_index)))
597 if (PREDICT_FALSE(icmp_is_error_message (icmp0)))
599 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
600 next0 = SNAT_IN2OUT_NEXT_DROP;
604 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
605 thread_index, vlib_time_now (sm->vlib_main));
607 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
612 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
613 icmp0->type != ICMP4_echo_reply &&
614 !icmp_is_error_message (icmp0)))
616 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
617 next0 = SNAT_IN2OUT_NEXT_DROP;
621 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
626 *p_proto = key0.protocol;
628 *p_value = s0->out2in;
629 *p_dont_translate = dont_translate;
631 *(snat_session_t**)d = s0;
636 * Get address and port values to be used for ICMP packet translation
638 * @param[in] sm NAT main
639 * @param[in,out] node NAT node runtime
640 * @param[in] thread_index thread index
641 * @param[in,out] b0 buffer containing packet to be translated
642 * @param[out] p_proto protocol used for matching
643 * @param[out] p_value address and port after NAT translation
644 * @param[out] p_dont_translate if packet should not be translated
645 * @param d optional parameter
646 * @param e optional parameter
648 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
649 u32 thread_index, vlib_buffer_t *b0,
650 ip4_header_t *ip0, u8 *p_proto,
651 snat_session_key_t *p_value,
652 u8 *p_dont_translate, void *d, void *e)
654 icmp46_header_t *icmp0;
657 snat_session_key_t key0;
658 snat_session_key_t sm0;
659 u8 dont_translate = 0;
664 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
665 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
666 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
668 err = icmp_get_key (ip0, &key0);
671 b0->error = node->errors[err];
672 next0 = SNAT_IN2OUT_NEXT_DROP;
675 key0.fib_index = rx_fib_index0;
677 if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0))
679 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
680 IP_PROTOCOL_ICMP, rx_fib_index0)))
686 if (icmp_is_error_message (icmp0))
688 next0 = SNAT_IN2OUT_NEXT_DROP;
692 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
693 next0 = SNAT_IN2OUT_NEXT_DROP;
697 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
698 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
699 !icmp_is_error_message (icmp0)))
701 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
702 next0 = SNAT_IN2OUT_NEXT_DROP;
709 *p_proto = key0.protocol;
710 *p_dont_translate = dont_translate;
714 static inline u32 icmp_in2out (snat_main_t *sm,
717 icmp46_header_t * icmp0,
720 vlib_node_runtime_t * node,
726 snat_session_key_t sm0;
728 icmp_echo_header_t *echo0, *inner_echo0 = 0;
729 ip4_header_t *inner_ip0;
731 icmp46_header_t *inner_icmp0;
733 u32 new_addr0, old_addr0;
734 u16 old_id0, new_id0;
739 echo0 = (icmp_echo_header_t *)(icmp0+1);
741 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0, ip0,
742 &protocol, &sm0, &dont_translate, d, e);
745 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
748 sum0 = ip_incremental_checksum (0, icmp0,
749 ntohs(ip0->length) - ip4_header_bytes (ip0));
750 checksum0 = ~ip_csum_fold (sum0);
751 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
753 next0 = SNAT_IN2OUT_NEXT_DROP;
757 old_addr0 = ip0->src_address.as_u32;
758 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
759 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
760 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
762 sum0 = ip0->checksum;
763 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
764 src_address /* changed member */);
765 ip0->checksum = ip_csum_fold (sum0);
767 if (icmp0->checksum == 0)
768 icmp0->checksum = 0xffff;
770 if (!icmp_is_error_message (icmp0))
773 if (PREDICT_FALSE(new_id0 != echo0->identifier))
775 old_id0 = echo0->identifier;
777 echo0->identifier = new_id0;
779 sum0 = icmp0->checksum;
780 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
782 icmp0->checksum = ip_csum_fold (sum0);
787 inner_ip0 = (ip4_header_t *)(echo0+1);
788 l4_header = ip4_next_header (inner_ip0);
790 if (!ip4_header_checksum_is_valid (inner_ip0))
792 next0 = SNAT_IN2OUT_NEXT_DROP;
796 old_addr0 = inner_ip0->dst_address.as_u32;
797 inner_ip0->dst_address = sm0.addr;
798 new_addr0 = inner_ip0->dst_address.as_u32;
800 sum0 = icmp0->checksum;
801 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
802 dst_address /* changed member */);
803 icmp0->checksum = ip_csum_fold (sum0);
807 case SNAT_PROTOCOL_ICMP:
808 inner_icmp0 = (icmp46_header_t*)l4_header;
809 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
811 old_id0 = inner_echo0->identifier;
813 inner_echo0->identifier = new_id0;
815 sum0 = icmp0->checksum;
816 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
818 icmp0->checksum = ip_csum_fold (sum0);
820 case SNAT_PROTOCOL_UDP:
821 case SNAT_PROTOCOL_TCP:
822 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
824 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
826 sum0 = icmp0->checksum;
827 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
829 icmp0->checksum = ip_csum_fold (sum0);
843 * Hairpinning allows two endpoints on the internal side of the NAT to
844 * communicate even if they only use each other's external IP addresses
847 * @param sm NAT main.
848 * @param b0 Vlib buffer.
849 * @param ip0 IP header.
850 * @param udp0 UDP header.
851 * @param tcp0 TCP header.
852 * @param proto0 NAT protocol.
855 snat_hairpinning (snat_main_t *sm,
863 snat_session_key_t key0, sm0;
865 clib_bihash_kv_8_8_t kv0, value0;
867 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
868 u16 new_dst_port0, old_dst_port0;
871 key0.addr = ip0->dst_address;
872 key0.port = udp0->dst_port;
873 key0.protocol = proto0;
874 key0.fib_index = sm->outside_fib_index;
875 kv0.key = key0.as_u64;
877 /* Check if destination is static mappings */
878 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
880 new_dst_addr0 = sm0.addr.as_u32;
881 new_dst_port0 = sm0.port;
882 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
884 /* or active session */
887 if (sm->num_workers > 1)
888 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
890 ti = sm->num_workers;
894 clib_bihash_kv_16_8_t ed_kv, ed_value;
895 make_ed_kv (&ed_kv, &ip0->dst_address, &ip0->src_address,
896 ip0->protocol, sm->outside_fib_index, udp0->dst_port,
898 rv = clib_bihash_search_16_8 (&sm->per_thread_data[ti].out2in_ed,
904 rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
911 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
912 new_dst_addr0 = s0->in2out.addr.as_u32;
913 new_dst_port0 = s0->in2out.port;
914 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
917 /* Destination is behind the same NAT, use internal address and port */
920 old_dst_addr0 = ip0->dst_address.as_u32;
921 ip0->dst_address.as_u32 = new_dst_addr0;
922 sum0 = ip0->checksum;
923 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
924 ip4_header_t, dst_address);
925 ip0->checksum = ip_csum_fold (sum0);
927 old_dst_port0 = tcp0->dst;
928 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
930 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
932 tcp0->dst = new_dst_port0;
933 sum0 = tcp0->checksum;
934 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
935 ip4_header_t, dst_address);
936 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
937 ip4_header_t /* cheat */, length);
938 tcp0->checksum = ip_csum_fold(sum0);
942 udp0->dst_port = new_dst_port0;
948 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
950 sum0 = tcp0->checksum;
951 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
952 ip4_header_t, dst_address);
953 tcp0->checksum = ip_csum_fold(sum0);
962 snat_icmp_hairpinning (snat_main_t *sm,
965 icmp46_header_t * icmp0,
968 snat_session_key_t key0, sm0;
969 clib_bihash_kv_8_8_t kv0, value0;
970 u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
975 if (!icmp_is_error_message (icmp0))
977 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
978 u16 icmp_id0 = echo0->identifier;
979 key0.addr = ip0->dst_address;
980 key0.port = icmp_id0;
981 key0.protocol = SNAT_PROTOCOL_ICMP;
982 key0.fib_index = sm->outside_fib_index;
983 kv0.key = key0.as_u64;
985 if (sm->num_workers > 1)
986 ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
988 ti = sm->num_workers;
990 /* Check if destination is in active sessions */
993 clib_bihash_kv_16_8_t ed_kv, ed_value;
994 make_ed_kv (&ed_kv, &ip0->dst_address, &ip0->src_address,
995 IP_PROTOCOL_ICMP, sm->outside_fib_index, icmp_id0, 0);
996 rv = clib_bihash_search_16_8 (&sm->per_thread_data[ti].out2in_ed,
1002 rv = clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
1008 /* or static mappings */
1009 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
1011 new_dst_addr0 = sm0.addr.as_u32;
1012 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1017 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
1018 new_dst_addr0 = s0->in2out.addr.as_u32;
1019 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1020 echo0->identifier = s0->in2out.port;
1021 sum0 = icmp0->checksum;
1022 sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
1023 icmp_echo_header_t, identifier);
1024 icmp0->checksum = ip_csum_fold (sum0);
1027 /* Destination is behind the same NAT, use internal address and port */
1030 old_dst_addr0 = ip0->dst_address.as_u32;
1031 ip0->dst_address.as_u32 = new_dst_addr0;
1032 sum0 = ip0->checksum;
1033 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
1034 ip4_header_t, dst_address);
1035 ip0->checksum = ip_csum_fold (sum0);
1041 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
1044 icmp46_header_t * icmp0,
1047 vlib_node_runtime_t * node,
1051 snat_session_t ** p_s0)
1053 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1054 next0, thread_index, p_s0, 0);
1055 snat_session_t * s0 = *p_s0;
1056 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
1059 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == 0)
1060 snat_icmp_hairpinning(sm, b0, ip0, icmp0, sm->endpoint_dependent);
1062 nat44_session_update_counters (s0, now,
1063 vlib_buffer_length_in_chain (sm->vlib_main, b0));
1064 /* Per-user LRU list maintenance */
1065 nat44_session_update_lru (sm, s0, thread_index);
1071 nat_hairpinning_sm_unknown_proto (snat_main_t * sm,
1075 clib_bihash_kv_8_8_t kv, value;
1076 snat_static_mapping_t *m;
1077 u32 old_addr, new_addr;
1080 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
1081 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1084 m = pool_elt_at_index (sm->static_mappings, value.value);
1086 old_addr = ip->dst_address.as_u32;
1087 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
1089 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
1090 ip->checksum = ip_csum_fold (sum);
1092 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1093 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
1097 nat_in2out_sm_unknown_proto (snat_main_t *sm,
1102 clib_bihash_kv_8_8_t kv, value;
1103 snat_static_mapping_t *m;
1104 snat_session_key_t m_key;
1105 u32 old_addr, new_addr;
1108 m_key.addr = ip->src_address;
1111 m_key.fib_index = rx_fib_index;
1112 kv.key = m_key.as_u64;
1113 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
1116 m = pool_elt_at_index (sm->static_mappings, value.value);
1118 old_addr = ip->src_address.as_u32;
1119 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
1121 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1122 ip->checksum = ip_csum_fold (sum);
1126 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1128 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
1129 nat_hairpinning_sm_unknown_proto (sm, b, ip);
1136 snat_in2out_node_fn_inline (vlib_main_t * vm,
1137 vlib_node_runtime_t * node,
1138 vlib_frame_t * frame, int is_slow_path,
1139 int is_output_feature)
1141 u32 n_left_from, * from, * to_next;
1142 snat_in2out_next_t next_index;
1143 u32 pkts_processed = 0;
1144 snat_main_t * sm = &snat_main;
1145 f64 now = vlib_time_now (vm);
1146 u32 stats_node_index;
1147 u32 thread_index = vm->thread_index;
1149 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
1150 snat_in2out_node.index;
1152 from = vlib_frame_vector_args (frame);
1153 n_left_from = frame->n_vectors;
1154 next_index = node->cached_next_index;
1156 while (n_left_from > 0)
1160 vlib_get_next_frame (vm, node, next_index,
1161 to_next, n_left_to_next);
1163 while (n_left_from >= 4 && n_left_to_next >= 2)
1166 vlib_buffer_t * b0, * b1;
1168 u32 sw_if_index0, sw_if_index1;
1169 ip4_header_t * ip0, * ip1;
1170 ip_csum_t sum0, sum1;
1171 u32 new_addr0, old_addr0, new_addr1, old_addr1;
1172 u16 old_port0, new_port0, old_port1, new_port1;
1173 udp_header_t * udp0, * udp1;
1174 tcp_header_t * tcp0, * tcp1;
1175 icmp46_header_t * icmp0, * icmp1;
1176 snat_session_key_t key0, key1;
1177 u32 rx_fib_index0, rx_fib_index1;
1179 snat_session_t * s0 = 0, * s1 = 0;
1180 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
1181 u32 iph_offset0 = 0, iph_offset1 = 0;
1183 /* Prefetch next iteration. */
1185 vlib_buffer_t * p2, * p3;
1187 p2 = vlib_get_buffer (vm, from[2]);
1188 p3 = vlib_get_buffer (vm, from[3]);
1190 vlib_prefetch_buffer_header (p2, LOAD);
1191 vlib_prefetch_buffer_header (p3, LOAD);
1193 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1194 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1197 /* speculatively enqueue b0 and b1 to the current next frame */
1198 to_next[0] = bi0 = from[0];
1199 to_next[1] = bi1 = from[1];
1203 n_left_to_next -= 2;
1205 b0 = vlib_get_buffer (vm, bi0);
1206 b1 = vlib_get_buffer (vm, bi1);
1208 if (is_output_feature)
1209 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1211 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1214 udp0 = ip4_next_header (ip0);
1215 tcp0 = (tcp_header_t *) udp0;
1216 icmp0 = (icmp46_header_t *) udp0;
1218 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1219 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1222 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
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 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1234 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1236 /* Next configured feature, probably ip4-lookup */
1239 if (PREDICT_FALSE (proto0 == ~0))
1241 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1243 next0 = SNAT_IN2OUT_NEXT_DROP;
1244 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1249 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1251 next0 = icmp_in2out_slow_path
1252 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1253 node, next0, now, thread_index, &s0);
1259 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1261 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1265 if (ip4_is_fragment (ip0))
1267 next0 = SNAT_IN2OUT_NEXT_REASS;
1272 key0.addr = ip0->src_address;
1273 key0.port = udp0->src_port;
1274 key0.protocol = proto0;
1275 key0.fib_index = rx_fib_index0;
1277 kv0.key = key0.as_u64;
1279 if (PREDICT_FALSE (clib_bihash_search_8_8 (
1280 &sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1284 if (is_output_feature)
1286 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
1287 ip0, proto0, udp0->src_port, udp0->dst_port, thread_index, sw_if_index0)))
1292 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1293 ip0, proto0, rx_fib_index0, thread_index)))
1297 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1298 &s0, node, next0, thread_index, now);
1299 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1304 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1309 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1312 b0->flags |= VNET_BUFFER_F_IS_NATED;
1314 old_addr0 = ip0->src_address.as_u32;
1315 ip0->src_address = s0->out2in.addr;
1316 new_addr0 = ip0->src_address.as_u32;
1317 if (!is_output_feature)
1318 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1320 sum0 = ip0->checksum;
1321 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1323 src_address /* changed member */);
1324 ip0->checksum = ip_csum_fold (sum0);
1326 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1328 old_port0 = tcp0->src_port;
1329 tcp0->src_port = s0->out2in.port;
1330 new_port0 = tcp0->src_port;
1332 sum0 = tcp0->checksum;
1333 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1335 dst_address /* changed member */);
1336 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1337 ip4_header_t /* cheat */,
1338 length /* changed member */);
1339 tcp0->checksum = ip_csum_fold(sum0);
1343 old_port0 = udp0->src_port;
1344 udp0->src_port = s0->out2in.port;
1349 nat44_session_update_counters (s0, now,
1350 vlib_buffer_length_in_chain (vm, b0));
1351 /* Per-user LRU list maintenance */
1352 nat44_session_update_lru (sm, s0, thread_index);
1355 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1356 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1358 snat_in2out_trace_t *t =
1359 vlib_add_trace (vm, node, b0, sizeof (*t));
1360 t->is_slow_path = is_slow_path;
1361 t->sw_if_index = sw_if_index0;
1362 t->next_index = next0;
1363 t->session_index = ~0;
1365 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1368 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1370 if (is_output_feature)
1371 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1373 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1376 udp1 = ip4_next_header (ip1);
1377 tcp1 = (tcp_header_t *) udp1;
1378 icmp1 = (icmp46_header_t *) udp1;
1380 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1381 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1384 if (PREDICT_FALSE(ip1->ttl == 1))
1386 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1387 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1388 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1390 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1394 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1396 /* Next configured feature, probably ip4-lookup */
1399 if (PREDICT_FALSE (proto1 == ~0))
1401 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1403 next1 = SNAT_IN2OUT_NEXT_DROP;
1404 b1->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1409 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1411 next1 = icmp_in2out_slow_path
1412 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1413 next1, now, thread_index, &s1);
1419 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1421 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1425 if (ip4_is_fragment (ip1))
1427 next1 = SNAT_IN2OUT_NEXT_REASS;
1432 key1.addr = ip1->src_address;
1433 key1.port = udp1->src_port;
1434 key1.protocol = proto1;
1435 key1.fib_index = rx_fib_index1;
1437 kv1.key = key1.as_u64;
1439 if (PREDICT_FALSE(clib_bihash_search_8_8 (
1440 &sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1444 if (is_output_feature)
1446 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
1447 ip1, proto1, udp1->src_port, udp1->dst_port, thread_index, sw_if_index1)))
1452 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
1453 ip1, proto1, rx_fib_index1, thread_index)))
1457 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1458 &s1, node, next1, thread_index, now);
1459 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1464 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1469 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1472 b1->flags |= VNET_BUFFER_F_IS_NATED;
1474 old_addr1 = ip1->src_address.as_u32;
1475 ip1->src_address = s1->out2in.addr;
1476 new_addr1 = ip1->src_address.as_u32;
1477 if (!is_output_feature)
1478 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1480 sum1 = ip1->checksum;
1481 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1483 src_address /* changed member */);
1484 ip1->checksum = ip_csum_fold (sum1);
1486 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1488 old_port1 = tcp1->src_port;
1489 tcp1->src_port = s1->out2in.port;
1490 new_port1 = tcp1->src_port;
1492 sum1 = tcp1->checksum;
1493 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1495 dst_address /* changed member */);
1496 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1497 ip4_header_t /* cheat */,
1498 length /* changed member */);
1499 tcp1->checksum = ip_csum_fold(sum1);
1503 old_port1 = udp1->src_port;
1504 udp1->src_port = s1->out2in.port;
1509 nat44_session_update_counters (s1, now,
1510 vlib_buffer_length_in_chain (vm, b1));
1511 /* Per-user LRU list maintenance */
1512 nat44_session_update_lru (sm, s1, thread_index);
1515 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1516 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1518 snat_in2out_trace_t *t =
1519 vlib_add_trace (vm, node, b1, sizeof (*t));
1520 t->sw_if_index = sw_if_index1;
1521 t->next_index = next1;
1522 t->session_index = ~0;
1524 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1527 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1529 /* verify speculative enqueues, maybe switch current next frame */
1530 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1531 to_next, n_left_to_next,
1532 bi0, bi1, next0, next1);
1535 while (n_left_from > 0 && n_left_to_next > 0)
1543 u32 new_addr0, old_addr0;
1544 u16 old_port0, new_port0;
1545 udp_header_t * udp0;
1546 tcp_header_t * tcp0;
1547 icmp46_header_t * icmp0;
1548 snat_session_key_t key0;
1551 snat_session_t * s0 = 0;
1552 clib_bihash_kv_8_8_t kv0, value0;
1553 u32 iph_offset0 = 0;
1555 /* speculatively enqueue b0 to the current next frame */
1561 n_left_to_next -= 1;
1563 b0 = vlib_get_buffer (vm, bi0);
1564 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1566 if (is_output_feature)
1567 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1569 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1572 udp0 = ip4_next_header (ip0);
1573 tcp0 = (tcp_header_t *) udp0;
1574 icmp0 = (icmp46_header_t *) udp0;
1576 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1577 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1580 if (PREDICT_FALSE(ip0->ttl == 1))
1582 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1583 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1584 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1586 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1590 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1592 /* Next configured feature, probably ip4-lookup */
1595 if (PREDICT_FALSE (proto0 == ~0))
1597 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1599 next0 = SNAT_IN2OUT_NEXT_DROP;
1600 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1605 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1607 next0 = icmp_in2out_slow_path
1608 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1609 next0, now, thread_index, &s0);
1615 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1617 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1621 if (ip4_is_fragment (ip0))
1623 next0 = SNAT_IN2OUT_NEXT_REASS;
1628 key0.addr = ip0->src_address;
1629 key0.port = udp0->src_port;
1630 key0.protocol = proto0;
1631 key0.fib_index = rx_fib_index0;
1633 kv0.key = key0.as_u64;
1635 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out,
1640 if (is_output_feature)
1642 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
1643 ip0, proto0, udp0->src_port, udp0->dst_port, thread_index, sw_if_index0)))
1648 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1649 ip0, proto0, rx_fib_index0, thread_index)))
1653 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1654 &s0, node, next0, thread_index, now);
1656 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1661 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1666 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1669 b0->flags |= VNET_BUFFER_F_IS_NATED;
1671 old_addr0 = ip0->src_address.as_u32;
1672 ip0->src_address = s0->out2in.addr;
1673 new_addr0 = ip0->src_address.as_u32;
1674 if (!is_output_feature)
1675 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1677 sum0 = ip0->checksum;
1678 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1680 src_address /* changed member */);
1681 ip0->checksum = ip_csum_fold (sum0);
1683 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1685 old_port0 = tcp0->src_port;
1686 tcp0->src_port = s0->out2in.port;
1687 new_port0 = tcp0->src_port;
1689 sum0 = tcp0->checksum;
1690 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1692 dst_address /* changed member */);
1693 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1694 ip4_header_t /* cheat */,
1695 length /* changed member */);
1696 tcp0->checksum = ip_csum_fold(sum0);
1700 old_port0 = udp0->src_port;
1701 udp0->src_port = s0->out2in.port;
1706 nat44_session_update_counters (s0, now,
1707 vlib_buffer_length_in_chain (vm, b0));
1708 /* Per-user LRU list maintenance */
1709 nat44_session_update_lru (sm, s0, thread_index);
1712 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1713 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1715 snat_in2out_trace_t *t =
1716 vlib_add_trace (vm, node, b0, sizeof (*t));
1717 t->is_slow_path = is_slow_path;
1718 t->sw_if_index = sw_if_index0;
1719 t->next_index = next0;
1720 t->session_index = ~0;
1722 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1725 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1727 /* verify speculative enqueue, maybe switch current next frame */
1728 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1729 to_next, n_left_to_next,
1733 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1736 vlib_node_increment_counter (vm, stats_node_index,
1737 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1739 return frame->n_vectors;
1743 snat_in2out_fast_path_fn (vlib_main_t * vm,
1744 vlib_node_runtime_t * node,
1745 vlib_frame_t * frame)
1747 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 0);
1750 VLIB_REGISTER_NODE (snat_in2out_node) = {
1751 .function = snat_in2out_fast_path_fn,
1752 .name = "nat44-in2out",
1753 .vector_size = sizeof (u32),
1754 .format_trace = format_snat_in2out_trace,
1755 .type = VLIB_NODE_TYPE_INTERNAL,
1757 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1758 .error_strings = snat_in2out_error_strings,
1760 .runtime_data_bytes = sizeof (snat_runtime_t),
1762 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1764 /* edit / add dispositions here */
1766 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1767 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1768 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1769 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1770 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1774 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1777 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1778 vlib_node_runtime_t * node,
1779 vlib_frame_t * frame)
1781 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 1);
1784 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1785 .function = snat_in2out_output_fast_path_fn,
1786 .name = "nat44-in2out-output",
1787 .vector_size = sizeof (u32),
1788 .format_trace = format_snat_in2out_trace,
1789 .type = VLIB_NODE_TYPE_INTERNAL,
1791 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1792 .error_strings = snat_in2out_error_strings,
1794 .runtime_data_bytes = sizeof (snat_runtime_t),
1796 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1798 /* edit / add dispositions here */
1800 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1801 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1802 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1803 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1804 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1808 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
1809 snat_in2out_output_fast_path_fn);
1812 snat_in2out_slow_path_fn (vlib_main_t * vm,
1813 vlib_node_runtime_t * node,
1814 vlib_frame_t * frame)
1816 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 0);
1819 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1820 .function = snat_in2out_slow_path_fn,
1821 .name = "nat44-in2out-slowpath",
1822 .vector_size = sizeof (u32),
1823 .format_trace = format_snat_in2out_trace,
1824 .type = VLIB_NODE_TYPE_INTERNAL,
1826 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1827 .error_strings = snat_in2out_error_strings,
1829 .runtime_data_bytes = sizeof (snat_runtime_t),
1831 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1833 /* edit / add dispositions here */
1835 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1836 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1837 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1838 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1839 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1843 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
1844 snat_in2out_slow_path_fn);
1847 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
1848 vlib_node_runtime_t * node,
1849 vlib_frame_t * frame)
1851 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 1);
1854 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1855 .function = snat_in2out_output_slow_path_fn,
1856 .name = "nat44-in2out-output-slowpath",
1857 .vector_size = sizeof (u32),
1858 .format_trace = format_snat_in2out_trace,
1859 .type = VLIB_NODE_TYPE_INTERNAL,
1861 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1862 .error_strings = snat_in2out_error_strings,
1864 .runtime_data_bytes = sizeof (snat_runtime_t),
1866 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1868 /* edit / add dispositions here */
1870 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1871 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1872 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1873 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1874 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1878 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
1879 snat_in2out_output_slow_path_fn);
1881 extern vnet_feature_arc_registration_t vnet_feat_arc_ip4_local;
1884 nat44_hairpinning_fn_inline (vlib_main_t * vm,
1885 vlib_node_runtime_t * node,
1886 vlib_frame_t * frame,
1889 u32 n_left_from, * from, * to_next, stats_node_index;
1890 snat_in2out_next_t next_index;
1891 u32 pkts_processed = 0;
1892 snat_main_t * sm = &snat_main;
1893 vnet_feature_main_t *fm = &feature_main;
1894 u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
1895 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
1897 stats_node_index = is_ed ? nat44_ed_hairpinning_node.index :
1898 nat44_hairpinning_node.index;
1899 from = vlib_frame_vector_args (frame);
1900 n_left_from = frame->n_vectors;
1901 next_index = node->cached_next_index;
1903 while (n_left_from > 0)
1907 vlib_get_next_frame (vm, node, next_index,
1908 to_next, n_left_to_next);
1910 while (n_left_from > 0 && n_left_to_next > 0)
1917 udp_header_t * udp0;
1918 tcp_header_t * tcp0;
1920 /* speculatively enqueue b0 to the current next frame */
1926 n_left_to_next -= 1;
1928 b0 = vlib_get_buffer (vm, bi0);
1929 ip0 = vlib_buffer_get_current (b0);
1930 udp0 = ip4_next_header (ip0);
1931 tcp0 = (tcp_header_t *) udp0;
1933 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1935 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
1938 if (snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, is_ed))
1939 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1941 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1943 /* verify speculative enqueue, maybe switch current next frame */
1944 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1945 to_next, n_left_to_next,
1949 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1952 vlib_node_increment_counter (vm, stats_node_index,
1953 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1955 return frame->n_vectors;
1959 nat44_hairpinning_fn (vlib_main_t * vm,
1960 vlib_node_runtime_t * node,
1961 vlib_frame_t * frame)
1963 return nat44_hairpinning_fn_inline (vm, node, frame, 0);
1966 VLIB_REGISTER_NODE (nat44_hairpinning_node) = {
1967 .function = nat44_hairpinning_fn,
1968 .name = "nat44-hairpinning",
1969 .vector_size = sizeof (u32),
1970 .type = VLIB_NODE_TYPE_INTERNAL,
1971 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1972 .error_strings = snat_in2out_error_strings,
1975 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1976 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1980 VLIB_NODE_FUNCTION_MULTIARCH (nat44_hairpinning_node,
1981 nat44_hairpinning_fn);
1984 nat44_ed_hairpinning_fn (vlib_main_t * vm,
1985 vlib_node_runtime_t * node,
1986 vlib_frame_t * frame)
1988 return nat44_hairpinning_fn_inline (vm, node, frame, 1);
1991 VLIB_REGISTER_NODE (nat44_ed_hairpinning_node) = {
1992 .function = nat44_ed_hairpinning_fn,
1993 .name = "nat44-ed-hairpinning",
1994 .vector_size = sizeof (u32),
1995 .type = VLIB_NODE_TYPE_INTERNAL,
1996 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1997 .error_strings = snat_in2out_error_strings,
2000 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2001 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2005 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_hairpinning_node,
2006 nat44_ed_hairpinning_fn);
2009 nat44_reass_hairpinning (snat_main_t *sm,
2016 snat_session_key_t key0, sm0;
2017 snat_session_t * s0;
2018 clib_bihash_kv_8_8_t kv0, value0;
2020 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
2021 u16 new_dst_port0, old_dst_port0;
2022 udp_header_t * udp0;
2023 tcp_header_t * tcp0;
2025 key0.addr = ip0->dst_address;
2027 key0.protocol = proto0;
2028 key0.fib_index = sm->outside_fib_index;
2029 kv0.key = key0.as_u64;
2031 udp0 = ip4_next_header (ip0);
2033 /* Check if destination is static mappings */
2034 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
2036 new_dst_addr0 = sm0.addr.as_u32;
2037 new_dst_port0 = sm0.port;
2038 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2040 /* or active sessions */
2043 if (sm->num_workers > 1)
2044 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
2046 ti = sm->num_workers;
2048 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
2051 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
2052 new_dst_addr0 = s0->in2out.addr.as_u32;
2053 new_dst_port0 = s0->in2out.port;
2054 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
2058 /* Destination is behind the same NAT, use internal address and port */
2061 old_dst_addr0 = ip0->dst_address.as_u32;
2062 ip0->dst_address.as_u32 = new_dst_addr0;
2063 sum0 = ip0->checksum;
2064 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2065 ip4_header_t, dst_address);
2066 ip0->checksum = ip_csum_fold (sum0);
2068 old_dst_port0 = dport;
2069 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0 &&
2070 ip4_is_first_fragment (ip0)))
2072 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2074 tcp0 = ip4_next_header (ip0);
2075 tcp0->dst = new_dst_port0;
2076 sum0 = tcp0->checksum;
2077 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2078 ip4_header_t, dst_address);
2079 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
2080 ip4_header_t /* cheat */, length);
2081 tcp0->checksum = ip_csum_fold(sum0);
2085 udp0->dst_port = new_dst_port0;
2091 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2093 tcp0 = ip4_next_header (ip0);
2094 sum0 = tcp0->checksum;
2095 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2096 ip4_header_t, dst_address);
2097 tcp0->checksum = ip_csum_fold(sum0);
2104 nat44_in2out_reass_node_fn (vlib_main_t * vm,
2105 vlib_node_runtime_t * node,
2106 vlib_frame_t * frame)
2108 u32 n_left_from, *from, *to_next;
2109 snat_in2out_next_t next_index;
2110 u32 pkts_processed = 0;
2111 snat_main_t *sm = &snat_main;
2112 f64 now = vlib_time_now (vm);
2113 u32 thread_index = vm->thread_index;
2114 snat_main_per_thread_data_t *per_thread_data =
2115 &sm->per_thread_data[thread_index];
2116 u32 *fragments_to_drop = 0;
2117 u32 *fragments_to_loopback = 0;
2119 from = vlib_frame_vector_args (frame);
2120 n_left_from = frame->n_vectors;
2121 next_index = node->cached_next_index;
2123 while (n_left_from > 0)
2127 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2129 while (n_left_from > 0 && n_left_to_next > 0)
2131 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
2136 nat_reass_ip4_t *reass0;
2137 udp_header_t * udp0;
2138 tcp_header_t * tcp0;
2139 snat_session_key_t key0;
2140 clib_bihash_kv_8_8_t kv0, value0;
2141 snat_session_t * s0 = 0;
2142 u16 old_port0, new_port0;
2145 /* speculatively enqueue b0 to the current next frame */
2151 n_left_to_next -= 1;
2153 b0 = vlib_get_buffer (vm, bi0);
2154 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2156 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2157 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2160 if (PREDICT_FALSE (nat_reass_is_drop_frag(0)))
2162 next0 = SNAT_IN2OUT_NEXT_DROP;
2163 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
2167 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
2168 udp0 = ip4_next_header (ip0);
2169 tcp0 = (tcp_header_t *) udp0;
2170 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2172 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
2177 &fragments_to_drop);
2179 if (PREDICT_FALSE (!reass0))
2181 next0 = SNAT_IN2OUT_NEXT_DROP;
2182 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
2183 nat_log_notice ("maximum reassemblies exceeded");
2187 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2189 key0.addr = ip0->src_address;
2190 key0.port = udp0->src_port;
2191 key0.protocol = proto0;
2192 key0.fib_index = rx_fib_index0;
2193 kv0.key = key0.as_u64;
2195 if (clib_bihash_search_8_8 (&per_thread_data->in2out, &kv0, &value0))
2197 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
2198 ip0, proto0, rx_fib_index0, thread_index)))
2201 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
2202 &s0, node, next0, thread_index, now);
2204 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
2207 reass0->sess_index = s0 - per_thread_data->sessions;
2211 s0 = pool_elt_at_index (per_thread_data->sessions,
2213 reass0->sess_index = value0.value;
2215 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
2219 if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
2221 if (nat_ip4_reass_add_fragment (reass0, bi0))
2223 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
2224 nat_log_notice ("maximum fragments per reassembly exceeded");
2225 next0 = SNAT_IN2OUT_NEXT_DROP;
2231 s0 = pool_elt_at_index (per_thread_data->sessions,
2232 reass0->sess_index);
2235 old_addr0 = ip0->src_address.as_u32;
2236 ip0->src_address = s0->out2in.addr;
2237 new_addr0 = ip0->src_address.as_u32;
2238 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
2240 sum0 = ip0->checksum;
2241 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2243 src_address /* changed member */);
2244 ip0->checksum = ip_csum_fold (sum0);
2246 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2248 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2250 old_port0 = tcp0->src_port;
2251 tcp0->src_port = s0->out2in.port;
2252 new_port0 = tcp0->src_port;
2254 sum0 = tcp0->checksum;
2255 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2257 dst_address /* changed member */);
2258 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2259 ip4_header_t /* cheat */,
2260 length /* changed member */);
2261 tcp0->checksum = ip_csum_fold(sum0);
2265 old_port0 = udp0->src_port;
2266 udp0->src_port = s0->out2in.port;
2272 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2273 s0->ext_host_port, proto0);
2276 nat44_session_update_counters (s0, now,
2277 vlib_buffer_length_in_chain (vm, b0));
2278 /* Per-user LRU list maintenance */
2279 nat44_session_update_lru (sm, s0, thread_index);
2282 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2283 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2285 nat44_in2out_reass_trace_t *t =
2286 vlib_add_trace (vm, node, b0, sizeof (*t));
2287 t->cached = cached0;
2288 t->sw_if_index = sw_if_index0;
2289 t->next_index = next0;
2299 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2301 /* verify speculative enqueue, maybe switch current next frame */
2302 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2303 to_next, n_left_to_next,
2307 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2309 from = vlib_frame_vector_args (frame);
2310 u32 len = vec_len (fragments_to_loopback);
2311 if (len <= VLIB_FRAME_SIZE)
2313 clib_memcpy (from, fragments_to_loopback, sizeof (u32) * len);
2315 vec_reset_length (fragments_to_loopback);
2320 fragments_to_loopback + (len - VLIB_FRAME_SIZE),
2321 sizeof (u32) * VLIB_FRAME_SIZE);
2322 n_left_from = VLIB_FRAME_SIZE;
2323 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2328 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2331 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
2332 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2335 nat_send_all_to_node (vm, fragments_to_drop, node,
2336 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
2337 SNAT_IN2OUT_NEXT_DROP);
2339 vec_free (fragments_to_drop);
2340 vec_free (fragments_to_loopback);
2341 return frame->n_vectors;
2344 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
2345 .function = nat44_in2out_reass_node_fn,
2346 .name = "nat44-in2out-reass",
2347 .vector_size = sizeof (u32),
2348 .format_trace = format_nat44_in2out_reass_trace,
2349 .type = VLIB_NODE_TYPE_INTERNAL,
2351 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2352 .error_strings = snat_in2out_error_strings,
2354 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2356 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2357 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2358 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2359 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2360 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2364 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2365 nat44_in2out_reass_node_fn);
2367 /*******************************/
2368 /*** endpoint-dependent mode ***/
2369 /*******************************/
2371 static_always_inline int
2372 icmp_get_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
2374 icmp46_header_t *icmp0;
2375 nat_ed_ses_key_t key0;
2376 icmp_echo_header_t *echo0, *inner_echo0 = 0;
2377 ip4_header_t *inner_ip0 = 0;
2378 void *l4_header = 0;
2379 icmp46_header_t *inner_icmp0;
2381 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
2382 echo0 = (icmp_echo_header_t *)(icmp0+1);
2384 if (!icmp_is_error_message (icmp0))
2386 key0.proto = IP_PROTOCOL_ICMP;
2387 key0.l_addr = ip0->src_address;
2388 key0.r_addr = ip0->dst_address;
2389 key0.l_port = echo0->identifier;
2394 inner_ip0 = (ip4_header_t *)(echo0+1);
2395 l4_header = ip4_next_header (inner_ip0);
2396 key0.proto = inner_ip0->protocol;
2397 key0.r_addr = inner_ip0->src_address;
2398 key0.l_addr = inner_ip0->dst_address;
2399 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
2401 case SNAT_PROTOCOL_ICMP:
2402 inner_icmp0 = (icmp46_header_t*)l4_header;
2403 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
2405 key0.l_port = inner_echo0->identifier;
2407 case SNAT_PROTOCOL_UDP:
2408 case SNAT_PROTOCOL_TCP:
2409 key0.l_port = ((tcp_udp_header_t*)l4_header)->dst_port;
2410 key0.r_port = ((tcp_udp_header_t*)l4_header)->src_port;
2413 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
2421 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void * arg)
2423 snat_main_t *sm = &snat_main;
2424 nat44_is_idle_session_ctx_t *ctx = arg;
2426 u64 sess_timeout_time;
2427 nat_ed_ses_key_t ed_key;
2428 clib_bihash_kv_16_8_t ed_kv;
2431 snat_session_key_t key;
2432 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
2435 s = pool_elt_at_index (tsm->sessions, kv->value);
2436 sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
2437 if (ctx->now >= sess_timeout_time)
2439 if (is_fwd_bypass_session (s))
2442 ed_key.l_addr = s->out2in.addr;
2443 ed_key.r_addr = s->ext_host_addr;
2444 ed_key.fib_index = s->out2in.fib_index;
2445 if (snat_is_unk_proto_session (s))
2447 ed_key.proto = s->in2out.port;
2453 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
2454 ed_key.l_port = s->out2in.port;
2455 ed_key.r_port = s->ext_host_port;
2457 ed_kv.key[0] = ed_key.as_u64[0];
2458 ed_kv.key[1] = ed_key.as_u64[1];
2459 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
2460 nat_log_warn ("out2in_ed key del failed");
2462 if (snat_is_unk_proto_session (s))
2465 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
2466 s->out2in.addr.as_u32,
2470 s->in2out.fib_index);
2472 if (is_twice_nat_session (s))
2474 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
2476 key.protocol = s->in2out.protocol;
2477 key.port = s->ext_host_nat_port;
2478 a = sm->twice_nat_addresses + i;
2479 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
2481 snat_free_outside_address_and_port (sm->twice_nat_addresses,
2482 ctx->thread_index, &key);
2488 if (snat_is_session_static (s))
2491 if (s->outside_address_index != ~0)
2492 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
2495 nat44_delete_session (sm, s, ctx->thread_index);
2503 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
2504 ip4_header_t * ip0, icmp46_header_t * icmp0,
2505 u32 sw_if_index0, u32 rx_fib_index0,
2506 vlib_node_runtime_t * node, u32 next0, f64 now,
2507 u32 thread_index, snat_session_t ** p_s0)
2509 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
2510 next0, thread_index, p_s0, 0);
2511 snat_session_t * s0 = *p_s0;
2512 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
2515 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
2516 snat_icmp_hairpinning(sm, b0, ip0, icmp0, sm->endpoint_dependent);
2518 nat44_session_update_counters (s0, now,
2519 vlib_buffer_length_in_chain (sm->vlib_main, b0));
2525 slow_path_ed (snat_main_t *sm,
2528 clib_bihash_kv_16_8_t *kv,
2529 snat_session_t ** sessionp,
2530 vlib_node_runtime_t * node,
2537 snat_session_key_t key0, key1;
2538 u8 lb = 0, is_sm = 0;
2539 u32 address_index = ~0;
2540 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2541 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
2542 u32 proto = ip_proto_to_snat_proto (key->proto);
2543 nat_outside_fib_t *outside_fib;
2544 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
2545 fib_prefix_t pfx = {
2546 .fp_proto = FIB_PROTOCOL_IP4,
2549 .ip4.as_u32 = key->r_addr.as_u32,
2552 nat44_is_idle_session_ctx_t ctx;
2554 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
2556 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
2557 nat_ipfix_logging_max_sessions(sm->max_translations);
2558 nat_log_notice ("maximum sessions exceeded");
2559 return SNAT_IN2OUT_NEXT_DROP;
2562 key0.addr = key->l_addr;
2563 key0.port = key->l_port;
2564 key1.protocol = key0.protocol = proto;
2565 key0.fib_index = rx_fib_index;
2566 key1.fib_index = sm->outside_fib_index;
2567 /* First try to match static mapping by local address and port */
2568 if (snat_static_mapping_match (sm, key0, &key1, 0, 0, 0, &lb))
2570 /* Try to create dynamic translation */
2571 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
2572 thread_index, &key1,
2574 sm->port_per_thread,
2575 tsm->snat_thread_index))
2577 nat_log_notice ("addresses exhausted");
2578 b->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
2579 return SNAT_IN2OUT_NEXT_DROP;
2585 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
2588 nat_log_warn ("create NAT user failed");
2590 snat_free_outside_address_and_port (sm->addresses,
2591 thread_index, &key1);
2592 return SNAT_IN2OUT_NEXT_DROP;
2595 s = nat_ed_session_alloc (sm, u, thread_index);
2598 nat44_delete_user_with_no_session (sm, u, thread_index);
2599 nat_log_warn ("create NAT session failed");
2601 snat_free_outside_address_and_port (sm->addresses,
2602 thread_index, &key1);
2603 return SNAT_IN2OUT_NEXT_DROP;
2606 user_session_increment (sm, u, is_sm);
2608 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
2610 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
2611 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
2612 s->outside_address_index = address_index;
2613 s->ext_host_addr = key->r_addr;
2614 s->ext_host_port = key->r_port;
2617 s->out2in.protocol = key0.protocol;
2619 switch (vec_len (sm->outside_fibs))
2622 s->out2in.fib_index = sm->outside_fib_index;
2625 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
2628 vec_foreach (outside_fib, sm->outside_fibs)
2630 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
2631 if (FIB_NODE_INDEX_INVALID != fei)
2633 if (fib_entry_get_resolving_interface (fei) != ~0)
2635 s->out2in.fib_index = outside_fib->fib_index;
2643 /* Add to lookup tables */
2644 kv->value = s - tsm->sessions;
2646 ctx.thread_index = thread_index;
2647 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
2648 nat44_i2o_ed_is_idle_session_cb,
2650 nat_log_notice ("in2out-ed key add failed");
2652 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
2653 key1.port, key->r_port);
2654 kv->value = s - tsm->sessions;
2655 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
2656 nat44_o2i_ed_is_idle_session_cb,
2658 nat_log_notice ("out2in-ed key add failed");
2663 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
2664 s->out2in.addr.as_u32,
2668 s->in2out.fib_index);
2672 static_always_inline int
2673 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
2674 u32 sw_if_index, ip4_header_t * ip, u32 proto,
2675 u32 rx_fib_index, u32 thread_index)
2677 udp_header_t *udp = ip4_next_header (ip);
2678 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2679 clib_bihash_kv_16_8_t kv, value;
2680 snat_session_key_t key0, key1;
2682 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
2683 sm->outside_fib_index, udp->dst_port, udp->src_port);
2685 /* NAT packet aimed at external address if */
2686 /* has active sessions */
2687 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
2689 key0.addr = ip->dst_address;
2690 key0.port = udp->dst_port;
2691 key0.protocol = proto;
2692 key0.fib_index = sm->outside_fib_index;
2693 /* or is static mappings */
2694 if (!snat_static_mapping_match(sm, key0, &key1, 1, 0, 0, 0))
2700 if (sm->forwarding_enabled)
2703 return snat_not_translate_fast(sm, node, sw_if_index, ip, proto, rx_fib_index);
2706 static_always_inline int
2707 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
2708 u32 thread_index, f64 now,
2709 vlib_main_t * vm, vlib_buffer_t * b)
2711 nat_ed_ses_key_t key;
2712 clib_bihash_kv_16_8_t kv, value;
2714 snat_session_t *s = 0;
2715 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2717 if (!sm->forwarding_enabled)
2720 if (ip->protocol == IP_PROTOCOL_ICMP)
2722 key.as_u64[0] = key.as_u64[1] = 0;
2723 if (icmp_get_ed_key (ip, &key))
2726 kv.key[0] = key.as_u64[0];
2727 kv.key[1] = key.as_u64[1];
2729 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
2731 udp = ip4_next_header(ip);
2732 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
2733 udp->src_port, udp->dst_port);
2737 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
2741 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
2743 s = pool_elt_at_index (tsm->sessions, value.value);
2744 if (is_fwd_bypass_session (s))
2746 if (ip->protocol == IP_PROTOCOL_TCP)
2748 tcp_header_t *tcp = ip4_next_header(ip);
2749 if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
2753 nat44_session_update_counters (s, now,
2754 vlib_buffer_length_in_chain (vm, b));
2764 static_always_inline int
2765 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
2766 u8 proto, u16 src_port, u16 dst_port,
2767 u32 thread_index, u32 sw_if_index)
2769 clib_bihash_kv_16_8_t kv, value;
2770 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2771 snat_interface_t *i;
2773 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
2776 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, fib_index,
2777 src_port, dst_port);
2778 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
2782 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, fib_index,
2783 dst_port, src_port);
2784 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
2786 s = pool_elt_at_index (tsm->sessions, value.value);
2787 if (is_fwd_bypass_session (s))
2791 pool_foreach (i, sm->output_feature_interfaces,
2793 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
2803 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node,
2804 u32 thread_index, vlib_buffer_t *b, ip4_header_t *ip,
2805 u8 *p_proto, snat_session_key_t *p_value,
2806 u8 *p_dont_translate, void *d, void *e)
2808 icmp46_header_t *icmp;
2811 nat_ed_ses_key_t key;
2812 snat_session_t *s = 0;
2813 u8 dont_translate = 0;
2814 clib_bihash_kv_16_8_t kv, value;
2817 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2819 icmp = (icmp46_header_t *) ip4_next_header (ip);
2820 sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
2821 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
2823 key.as_u64[0] = key.as_u64[1] = 0;
2824 err = icmp_get_ed_key (ip, &key);
2827 b->error = node->errors[err];
2828 next = SNAT_IN2OUT_NEXT_DROP;
2831 key.fib_index = rx_fib_index;
2833 kv.key[0] = key.as_u64[0];
2834 kv.key[1] = key.as_u64[1];
2836 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
2838 if (vnet_buffer(b)->sw_if_index[VLIB_TX] != ~0)
2840 if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(sm, ip,
2841 key.proto, key.l_port, key.r_port, thread_index, sw_if_index)))
2849 if (PREDICT_FALSE(nat44_ed_not_translate(sm, node, sw_if_index,
2850 ip, SNAT_PROTOCOL_ICMP, rx_fib_index, thread_index)))
2857 if (PREDICT_FALSE(icmp_is_error_message (icmp)))
2859 b->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2860 next = SNAT_IN2OUT_NEXT_DROP;
2864 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
2865 thread_index, vlib_time_now (sm->vlib_main));
2867 if (PREDICT_FALSE (next == SNAT_IN2OUT_NEXT_DROP))
2872 if (PREDICT_FALSE(icmp->type != ICMP4_echo_request &&
2873 icmp->type != ICMP4_echo_reply &&
2874 !icmp_is_error_message (icmp)))
2876 b->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2877 next = SNAT_IN2OUT_NEXT_DROP;
2881 s = pool_elt_at_index (tsm->sessions, value.value);
2884 *p_proto = ip_proto_to_snat_proto (key.proto);
2887 *p_value = s->out2in;
2888 *p_dont_translate = dont_translate;
2890 *(snat_session_t**)d = s;
2895 nat44_ed_hairpinning_unknown_proto (snat_main_t *sm,
2899 u32 old_addr, new_addr = 0, ti = 0;
2900 clib_bihash_kv_8_8_t kv, value;
2901 clib_bihash_kv_16_8_t s_kv, s_value;
2902 snat_static_mapping_t *m;
2905 snat_main_per_thread_data_t *tsm;
2907 if (sm->num_workers > 1)
2908 ti = sm->worker_out2in_cb (ip, sm->outside_fib_index);
2910 ti = sm->num_workers;
2911 tsm = &sm->per_thread_data[ti];
2913 old_addr = ip->dst_address.as_u32;
2914 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
2915 sm->outside_fib_index, 0, 0);
2916 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
2918 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
2919 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2922 m = pool_elt_at_index (sm->static_mappings, value.value);
2923 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
2924 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
2925 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
2929 s = pool_elt_at_index (sm->per_thread_data[ti].sessions, s_value.value);
2930 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
2931 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
2932 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
2935 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
2936 ip->checksum = ip_csum_fold (sum);
2939 static snat_session_t *
2940 nat44_ed_in2out_unknown_proto (snat_main_t *sm,
2947 vlib_node_runtime_t * node)
2949 clib_bihash_kv_8_8_t kv, value;
2950 clib_bihash_kv_16_8_t s_kv, s_value;
2951 snat_static_mapping_t *m;
2952 u32 old_addr, new_addr = 0;
2955 dlist_elt_t *head, *elt;
2956 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
2957 u32 elt_index, head_index, ses_index;
2959 u32 address_index = ~0, outside_fib_index = sm->outside_fib_index;
2962 nat_outside_fib_t *outside_fib;
2963 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
2964 fib_prefix_t pfx = {
2965 .fp_proto = FIB_PROTOCOL_IP4,
2968 .ip4.as_u32 = ip->dst_address.as_u32,
2972 switch (vec_len (sm->outside_fibs))
2975 outside_fib_index = sm->outside_fib_index;
2978 outside_fib_index = sm->outside_fibs[0].fib_index;
2981 vec_foreach (outside_fib, sm->outside_fibs)
2983 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
2984 if (FIB_NODE_INDEX_INVALID != fei)
2986 if (fib_entry_get_resolving_interface (fei) != ~0)
2988 outside_fib_index = outside_fib->fib_index;
2995 old_addr = ip->src_address.as_u32;
2997 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
2998 rx_fib_index, 0, 0);
3000 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
3002 s = pool_elt_at_index (tsm->sessions, s_value.value);
3003 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
3007 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
3009 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
3010 nat_ipfix_logging_max_sessions(sm->max_translations);
3011 nat_log_notice ("maximum sessions exceeded");
3015 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
3019 nat_log_warn ("create NAT user failed");
3023 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
3025 /* Try to find static mapping first */
3026 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
3028 m = pool_elt_at_index (sm->static_mappings, value.value);
3029 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
3033 /* Fallback to 3-tuple key */
3036 /* Choose same out address as for TCP/UDP session to same destination */
3037 head_index = u->sessions_per_user_list_head_index;
3038 head = pool_elt_at_index (tsm->list_pool, head_index);
3039 elt_index = head->next;
3040 if (PREDICT_FALSE (elt_index == ~0))
3044 elt = pool_elt_at_index (tsm->list_pool, elt_index);
3045 ses_index = elt->value;
3048 while (ses_index != ~0)
3050 s = pool_elt_at_index (tsm->sessions, ses_index);
3051 elt_index = elt->next;
3052 elt = pool_elt_at_index (tsm->list_pool, elt_index);
3053 ses_index = elt->value;
3055 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
3057 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
3058 address_index = s->outside_address_index;
3060 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
3061 ip->protocol, outside_fib_index, 0, 0);
3062 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
3069 for (i = 0; i < vec_len (sm->addresses); i++)
3071 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
3072 ip->protocol, outside_fib_index, 0, 0);
3073 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
3075 new_addr = ip->src_address.as_u32 =
3076 sm->addresses[i].addr.as_u32;
3085 s = nat_ed_session_alloc (sm, u, thread_index);
3088 nat44_delete_user_with_no_session (sm, u, thread_index);
3089 nat_log_warn ("create NAT session failed");
3093 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
3094 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
3095 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
3096 s->outside_address_index = address_index;
3097 s->out2in.addr.as_u32 = new_addr;
3098 s->out2in.fib_index = outside_fib_index;
3099 s->in2out.addr.as_u32 = old_addr;
3100 s->in2out.fib_index = rx_fib_index;
3101 s->in2out.port = s->out2in.port = ip->protocol;
3103 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
3104 user_session_increment (sm, u, is_sm);
3106 /* Add to lookup tables */
3107 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
3108 rx_fib_index, 0, 0);
3109 s_kv.value = s - tsm->sessions;
3110 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
3111 nat_log_notice ("in2out key add failed");
3113 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
3114 outside_fib_index, 0, 0);
3115 s_kv.value = s - tsm->sessions;
3116 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
3117 nat_log_notice ("out2in key add failed");
3120 /* Update IP checksum */
3122 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
3123 ip->checksum = ip_csum_fold (sum);
3126 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
3129 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
3130 nat44_ed_hairpinning_unknown_proto(sm, b, ip);
3132 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
3133 vnet_buffer(b)->sw_if_index[VLIB_TX] = outside_fib_index;
3139 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
3140 vlib_node_runtime_t * node,
3141 vlib_frame_t * frame, int is_slow_path,
3142 int is_output_feature)
3144 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
3145 snat_in2out_next_t next_index;
3146 snat_main_t *sm = &snat_main;
3147 f64 now = vlib_time_now (vm);
3148 u32 thread_index = vm->thread_index;
3149 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3151 stats_node_index = is_slow_path ? nat44_ed_in2out_slowpath_node.index :
3152 nat44_ed_in2out_node.index;
3154 from = vlib_frame_vector_args (frame);
3155 n_left_from = frame->n_vectors;
3156 next_index = node->cached_next_index;
3158 while (n_left_from > 0)
3162 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3164 while (n_left_from >= 4 && n_left_to_next >= 2)
3167 vlib_buffer_t *b0, *b1;
3168 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
3169 new_addr0, old_addr0;
3170 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
3171 new_addr1, old_addr1;
3172 u16 old_port0, new_port0, old_port1, new_port1;
3173 ip4_header_t *ip0, *ip1;
3174 udp_header_t *udp0, *udp1;
3175 tcp_header_t *tcp0, *tcp1;
3176 icmp46_header_t *icmp0, *icmp1;
3177 snat_session_t *s0 = 0, *s1 = 0;
3178 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
3179 ip_csum_t sum0, sum1;
3181 /* Prefetch next iteration. */
3183 vlib_buffer_t * p2, * p3;
3185 p2 = vlib_get_buffer (vm, from[2]);
3186 p3 = vlib_get_buffer (vm, from[3]);
3188 vlib_prefetch_buffer_header (p2, LOAD);
3189 vlib_prefetch_buffer_header (p3, LOAD);
3191 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
3192 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
3195 /* speculatively enqueue b0 and b1 to the current next frame */
3196 to_next[0] = bi0 = from[0];
3197 to_next[1] = bi1 = from[1];
3201 n_left_to_next -= 2;
3203 b0 = vlib_get_buffer (vm, bi0);
3204 b1 = vlib_get_buffer (vm, bi1);
3206 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3208 if (is_output_feature)
3209 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
3211 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
3214 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3215 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3218 if (PREDICT_FALSE(ip0->ttl == 1))
3220 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3221 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
3222 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3224 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3228 udp0 = ip4_next_header (ip0);
3229 tcp0 = (tcp_header_t *) udp0;
3230 icmp0 = (icmp46_header_t *) udp0;
3231 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3235 if (PREDICT_FALSE (proto0 == ~0))
3237 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
3239 thread_index, now, vm,
3242 next0 = SNAT_IN2OUT_NEXT_DROP;
3246 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
3248 next0 = icmp_in2out_ed_slow_path
3249 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
3250 next0, now, thread_index, &s0);
3256 if (is_output_feature)
3258 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
3259 sm, ip0, thread_index, now, vm, b0)))
3263 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
3265 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3269 if (ip4_is_fragment (ip0))
3271 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
3272 next0 = SNAT_IN2OUT_NEXT_DROP;
3277 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address, ip0->protocol,
3278 rx_fib_index0, udp0->src_port, udp0->dst_port);
3280 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
3284 if (is_output_feature)
3286 if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
3287 sm, ip0, ip0->protocol, udp0->src_port,
3288 udp0->dst_port, thread_index, sw_if_index0)))
3293 if (PREDICT_FALSE(nat44_ed_not_translate(sm, node,
3294 sw_if_index0, ip0, proto0, rx_fib_index0,
3299 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
3300 next0, thread_index, now);
3302 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
3307 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3313 s0 = pool_elt_at_index (tsm->sessions, value0.value);
3316 b0->flags |= VNET_BUFFER_F_IS_NATED;
3318 if (!is_output_feature)
3319 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
3321 old_addr0 = ip0->src_address.as_u32;
3322 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
3323 sum0 = ip0->checksum;
3324 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
3326 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3327 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
3328 s0->ext_host_addr.as_u32, ip4_header_t,
3330 ip0->checksum = ip_csum_fold (sum0);
3332 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
3334 old_port0 = tcp0->src_port;
3335 new_port0 = tcp0->src_port = s0->out2in.port;
3337 sum0 = tcp0->checksum;
3338 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
3340 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
3342 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3344 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
3345 s0->ext_host_addr.as_u32,
3346 ip4_header_t, dst_address);
3347 sum0 = ip_csum_update (sum0, tcp0->dst_port,
3348 s0->ext_host_port, ip4_header_t,
3350 tcp0->dst_port = s0->ext_host_port;
3351 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
3353 tcp0->checksum = ip_csum_fold(sum0);
3354 if (nat44_set_tcp_session_state_i2o (sm, s0, tcp0, thread_index))
3359 udp0->src_port = s0->out2in.port;
3361 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3363 udp0->dst_port = s0->ext_host_port;
3364 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
3369 nat44_session_update_counters (s0, now,
3370 vlib_buffer_length_in_chain (vm, b0));
3373 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3374 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3376 snat_in2out_trace_t *t =
3377 vlib_add_trace (vm, node, b0, sizeof (*t));
3378 t->is_slow_path = is_slow_path;
3379 t->sw_if_index = sw_if_index0;
3380 t->next_index = next0;
3381 t->session_index = ~0;
3383 t->session_index = s0 - tsm->sessions;
3386 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3389 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
3391 if (is_output_feature)
3392 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
3394 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
3397 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
3398 rx_fib_index1 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3401 if (PREDICT_FALSE(ip1->ttl == 1))
3403 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3404 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
3405 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3407 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3411 udp1 = ip4_next_header (ip1);
3412 tcp1 = (tcp_header_t *) udp1;
3413 icmp1 = (icmp46_header_t *) udp1;
3414 proto1 = ip_proto_to_snat_proto (ip1->protocol);
3418 if (PREDICT_FALSE (proto1 == ~0))
3420 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
3422 thread_index, now, vm,
3425 next1 = SNAT_IN2OUT_NEXT_DROP;
3429 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
3431 next1 = icmp_in2out_ed_slow_path
3432 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
3433 next1, now, thread_index, &s1);
3439 if (is_output_feature)
3441 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
3442 sm, ip1, thread_index, now, vm, b1)))
3446 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
3448 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3452 if (ip4_is_fragment (ip1))
3454 b1->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
3455 next1 = SNAT_IN2OUT_NEXT_DROP;
3460 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address, ip1->protocol,
3461 rx_fib_index1, udp1->src_port, udp1->dst_port);
3463 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
3467 if (is_output_feature)
3469 if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
3470 sm, ip1, ip1->protocol, udp1->src_port,
3471 udp1->dst_port, thread_index, sw_if_index1)))
3476 if (PREDICT_FALSE(nat44_ed_not_translate(sm, node,
3477 sw_if_index1, ip1, proto1, rx_fib_index1,
3482 next1 = slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
3483 next1, thread_index, now);
3485 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
3490 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3496 s1 = pool_elt_at_index (tsm->sessions, value1.value);
3499 b1->flags |= VNET_BUFFER_F_IS_NATED;
3501 if (!is_output_feature)
3502 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
3504 old_addr1 = ip1->src_address.as_u32;
3505 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
3506 sum1 = ip1->checksum;
3507 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
3509 if (PREDICT_FALSE (is_twice_nat_session (s1)))
3510 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
3511 s1->ext_host_addr.as_u32, ip4_header_t,
3513 ip1->checksum = ip_csum_fold (sum1);
3515 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
3517 old_port1 = tcp1->src_port;
3518 new_port1 = tcp1->src_port = s1->out2in.port;
3520 sum1 = tcp1->checksum;
3521 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
3523 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
3525 if (PREDICT_FALSE (is_twice_nat_session (s1)))
3527 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
3528 s1->ext_host_addr.as_u32,
3529 ip4_header_t, dst_address);
3530 sum1 = ip_csum_update (sum1, tcp1->dst_port,
3531 s1->ext_host_port, ip4_header_t,
3533 tcp1->dst_port = s1->ext_host_port;
3534 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
3536 tcp1->checksum = ip_csum_fold(sum1);
3537 if (nat44_set_tcp_session_state_i2o (sm, s1, tcp1, thread_index))
3542 udp1->src_port = s1->out2in.port;
3544 if (PREDICT_FALSE (is_twice_nat_session (s1)))
3546 udp1->dst_port = s1->ext_host_port;
3547 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
3552 nat44_session_update_counters (s1, now,
3553 vlib_buffer_length_in_chain (vm, b1));
3556 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3557 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
3559 snat_in2out_trace_t *t =
3560 vlib_add_trace (vm, node, b1, sizeof (*t));
3561 t->is_slow_path = is_slow_path;
3562 t->sw_if_index = sw_if_index1;
3563 t->next_index = next1;
3564 t->session_index = ~0;
3566 t->session_index = s1 - tsm->sessions;
3569 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
3571 /* verify speculative enqueues, maybe switch current next frame */
3572 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
3573 to_next, n_left_to_next,
3574 bi0, bi1, next0, next1);
3577 while (n_left_from > 0 && n_left_to_next > 0)
3581 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
3582 new_addr0, old_addr0;
3583 u16 old_port0, new_port0;
3587 icmp46_header_t * icmp0;
3588 snat_session_t *s0 = 0;
3589 clib_bihash_kv_16_8_t kv0, value0;
3592 /* speculatively enqueue b0 to the current next frame */
3598 n_left_to_next -= 1;
3600 b0 = vlib_get_buffer (vm, bi0);
3601 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3603 if (is_output_feature)
3604 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
3606 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
3609 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3610 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3613 if (PREDICT_FALSE(ip0->ttl == 1))
3615 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3616 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
3617 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3619 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3623 udp0 = ip4_next_header (ip0);
3624 tcp0 = (tcp_header_t *) udp0;
3625 icmp0 = (icmp46_header_t *) udp0;
3626 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3630 if (PREDICT_FALSE (proto0 == ~0))
3632 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
3634 thread_index, now, vm,
3637 next0 = SNAT_IN2OUT_NEXT_DROP;
3641 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
3643 next0 = icmp_in2out_ed_slow_path
3644 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
3645 next0, now, thread_index, &s0);
3651 if (is_output_feature)
3653 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(
3654 sm, ip0, thread_index, now, vm, b0)))
3658 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
3660 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3664 if (ip4_is_fragment (ip0))
3666 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
3667 next0 = SNAT_IN2OUT_NEXT_DROP;
3672 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address, ip0->protocol,
3673 rx_fib_index0, udp0->src_port, udp0->dst_port);
3675 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
3679 if (is_output_feature)
3681 if (PREDICT_FALSE(nat44_ed_not_translate_output_feature(
3682 sm, ip0, ip0->protocol, udp0->src_port,
3683 udp0->dst_port, thread_index, sw_if_index0)))
3688 if (PREDICT_FALSE(nat44_ed_not_translate(sm, node,
3689 sw_if_index0, ip0, proto0, rx_fib_index0,
3694 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
3695 next0, thread_index, now);
3697 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
3702 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
3708 s0 = pool_elt_at_index (tsm->sessions, value0.value);
3711 b0->flags |= VNET_BUFFER_F_IS_NATED;
3713 if (!is_output_feature)
3714 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
3716 old_addr0 = ip0->src_address.as_u32;
3717 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
3718 sum0 = ip0->checksum;
3719 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
3721 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3722 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
3723 s0->ext_host_addr.as_u32, ip4_header_t,
3725 ip0->checksum = ip_csum_fold (sum0);
3727 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
3729 old_port0 = tcp0->src_port;
3730 new_port0 = tcp0->src_port = s0->out2in.port;
3732 sum0 = tcp0->checksum;
3733 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
3735 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
3737 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3739 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
3740 s0->ext_host_addr.as_u32,
3741 ip4_header_t, dst_address);
3742 sum0 = ip_csum_update (sum0, tcp0->dst_port,
3743 s0->ext_host_port, ip4_header_t,
3745 tcp0->dst_port = s0->ext_host_port;
3746 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
3748 tcp0->checksum = ip_csum_fold(sum0);
3749 if (nat44_set_tcp_session_state_i2o (sm, s0, tcp0, thread_index))
3754 udp0->src_port = s0->out2in.port;
3756 if (PREDICT_FALSE (is_twice_nat_session (s0)))
3758 udp0->dst_port = s0->ext_host_port;
3759 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
3764 nat44_session_update_counters (s0, now,
3765 vlib_buffer_length_in_chain (vm, b0));
3768 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3769 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3771 snat_in2out_trace_t *t =
3772 vlib_add_trace (vm, node, b0, sizeof (*t));
3773 t->is_slow_path = is_slow_path;
3774 t->sw_if_index = sw_if_index0;
3775 t->next_index = next0;
3776 t->session_index = ~0;
3778 t->session_index = s0 - tsm->sessions;
3781 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3783 /* verify speculative enqueue, maybe switch current next frame */
3784 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3785 to_next, n_left_to_next,
3789 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3792 vlib_node_increment_counter (vm, stats_node_index,
3793 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3795 return frame->n_vectors;
3799 nat44_ed_in2out_fast_path_fn (vlib_main_t * vm,
3800 vlib_node_runtime_t * node,
3801 vlib_frame_t * frame)
3803 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
3806 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
3807 .function = nat44_ed_in2out_fast_path_fn,
3808 .name = "nat44-ed-in2out",
3809 .vector_size = sizeof (u32),
3810 .format_trace = format_snat_in2out_trace,
3811 .type = VLIB_NODE_TYPE_INTERNAL,
3813 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3814 .error_strings = snat_in2out_error_strings,
3816 .runtime_data_bytes = sizeof (snat_runtime_t),
3818 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3820 /* edit / add dispositions here */
3822 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3823 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3824 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3825 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3826 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
3830 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_node, nat44_ed_in2out_fast_path_fn);
3833 nat44_ed_in2out_output_fast_path_fn (vlib_main_t * vm,
3834 vlib_node_runtime_t * node,
3835 vlib_frame_t * frame)
3837 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
3840 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
3841 .function = nat44_ed_in2out_output_fast_path_fn,
3842 .name = "nat44-ed-in2out-output",
3843 .vector_size = sizeof (u32),
3844 .format_trace = format_snat_in2out_trace,
3845 .type = VLIB_NODE_TYPE_INTERNAL,
3847 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3848 .error_strings = snat_in2out_error_strings,
3850 .runtime_data_bytes = sizeof (snat_runtime_t),
3852 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3854 /* edit / add dispositions here */
3856 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3857 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
3858 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3859 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3860 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
3864 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_node,
3865 nat44_ed_in2out_output_fast_path_fn);
3868 nat44_ed_in2out_slow_path_fn (vlib_main_t * vm,
3869 vlib_node_runtime_t * node,
3870 vlib_frame_t * frame)
3872 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
3875 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
3876 .function = nat44_ed_in2out_slow_path_fn,
3877 .name = "nat44-ed-in2out-slowpath",
3878 .vector_size = sizeof (u32),
3879 .format_trace = format_snat_in2out_trace,
3880 .type = VLIB_NODE_TYPE_INTERNAL,
3882 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3883 .error_strings = snat_in2out_error_strings,
3885 .runtime_data_bytes = sizeof (snat_runtime_t),
3887 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3889 /* edit / add dispositions here */
3891 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3892 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3893 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3894 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3895 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
3899 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_slowpath_node,
3900 nat44_ed_in2out_slow_path_fn);
3903 nat44_ed_in2out_output_slow_path_fn (vlib_main_t * vm,
3904 vlib_node_runtime_t * node,
3905 vlib_frame_t * frame)
3907 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
3910 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
3911 .function = nat44_ed_in2out_output_slow_path_fn,
3912 .name = "nat44-ed-in2out-output-slowpath",
3913 .vector_size = sizeof (u32),
3914 .format_trace = format_snat_in2out_trace,
3915 .type = VLIB_NODE_TYPE_INTERNAL,
3917 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3918 .error_strings = snat_in2out_error_strings,
3920 .runtime_data_bytes = sizeof (snat_runtime_t),
3922 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3924 /* edit / add dispositions here */
3926 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3927 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
3928 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3929 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3930 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
3934 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_slowpath_node,
3935 nat44_ed_in2out_output_slow_path_fn);
3937 /**************************/
3938 /*** deterministic mode ***/
3939 /**************************/
3941 snat_det_in2out_node_fn (vlib_main_t * vm,
3942 vlib_node_runtime_t * node,
3943 vlib_frame_t * frame)
3945 u32 n_left_from, * from, * to_next;
3946 snat_in2out_next_t next_index;
3947 u32 pkts_processed = 0;
3948 snat_main_t * sm = &snat_main;
3949 u32 now = (u32) vlib_time_now (vm);
3950 u32 thread_index = vm->thread_index;
3952 from = vlib_frame_vector_args (frame);
3953 n_left_from = frame->n_vectors;
3954 next_index = node->cached_next_index;
3956 while (n_left_from > 0)
3960 vlib_get_next_frame (vm, node, next_index,
3961 to_next, n_left_to_next);
3963 while (n_left_from >= 4 && n_left_to_next >= 2)
3966 vlib_buffer_t * b0, * b1;
3968 u32 sw_if_index0, sw_if_index1;
3969 ip4_header_t * ip0, * ip1;
3970 ip_csum_t sum0, sum1;
3971 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
3972 u16 old_port0, new_port0, lo_port0, i0;
3973 u16 old_port1, new_port1, lo_port1, i1;
3974 udp_header_t * udp0, * udp1;
3975 tcp_header_t * tcp0, * tcp1;
3977 snat_det_out_key_t key0, key1;
3978 snat_det_map_t * dm0, * dm1;
3979 snat_det_session_t * ses0 = 0, * ses1 = 0;
3980 u32 rx_fib_index0, rx_fib_index1;
3981 icmp46_header_t * icmp0, * icmp1;
3983 /* Prefetch next iteration. */
3985 vlib_buffer_t * p2, * p3;
3987 p2 = vlib_get_buffer (vm, from[2]);
3988 p3 = vlib_get_buffer (vm, from[3]);
3990 vlib_prefetch_buffer_header (p2, LOAD);
3991 vlib_prefetch_buffer_header (p3, LOAD);
3993 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
3994 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
3997 /* speculatively enqueue b0 and b1 to the current next frame */
3998 to_next[0] = bi0 = from[0];
3999 to_next[1] = bi1 = from[1];
4003 n_left_to_next -= 2;
4005 b0 = vlib_get_buffer (vm, bi0);
4006 b1 = vlib_get_buffer (vm, bi1);
4008 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
4009 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
4011 ip0 = vlib_buffer_get_current (b0);
4012 udp0 = ip4_next_header (ip0);
4013 tcp0 = (tcp_header_t *) udp0;
4015 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
4017 if (PREDICT_FALSE(ip0->ttl == 1))
4019 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4020 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
4021 ICMP4_time_exceeded_ttl_exceeded_in_transit,
4023 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4027 proto0 = ip_proto_to_snat_proto (ip0->protocol);
4029 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
4031 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
4032 icmp0 = (icmp46_header_t *) udp0;
4034 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
4035 rx_fib_index0, node, next0, thread_index,
4040 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
4041 if (PREDICT_FALSE(!dm0))
4043 nat_log_info ("no match for internal host %U",
4044 format_ip4_address, &ip0->src_address);
4045 next0 = SNAT_IN2OUT_NEXT_DROP;
4046 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
4050 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
4052 key0.ext_host_addr = ip0->dst_address;
4053 key0.ext_host_port = tcp0->dst;
4055 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
4056 if (PREDICT_FALSE(!ses0))
4058 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
4060 key0.out_port = clib_host_to_net_u16 (lo_port0 +
4061 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
4063 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
4066 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
4069 if (PREDICT_FALSE(!ses0))
4071 /* too many sessions for user, send ICMP error packet */
4073 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4074 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
4075 ICMP4_destination_unreachable_destination_unreachable_host,
4077 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4082 new_port0 = ses0->out.out_port;
4084 old_addr0.as_u32 = ip0->src_address.as_u32;
4085 ip0->src_address.as_u32 = new_addr0.as_u32;
4086 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
4088 sum0 = ip0->checksum;
4089 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
4091 src_address /* changed member */);
4092 ip0->checksum = ip_csum_fold (sum0);
4094 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
4096 if (tcp0->flags & TCP_FLAG_SYN)
4097 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
4098 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
4099 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
4100 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
4101 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
4102 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
4103 snat_det_ses_close(dm0, ses0);
4104 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
4105 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
4106 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
4107 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
4109 old_port0 = tcp0->src;
4110 tcp0->src = new_port0;
4112 sum0 = tcp0->checksum;
4113 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
4115 dst_address /* changed member */);
4116 sum0 = ip_csum_update (sum0, old_port0, new_port0,
4117 ip4_header_t /* cheat */,
4118 length /* changed member */);
4119 tcp0->checksum = ip_csum_fold(sum0);
4123 ses0->state = SNAT_SESSION_UDP_ACTIVE;
4124 old_port0 = udp0->src_port;
4125 udp0->src_port = new_port0;
4131 case SNAT_SESSION_UDP_ACTIVE:
4132 ses0->expire = now + sm->udp_timeout;
4134 case SNAT_SESSION_TCP_SYN_SENT:
4135 case SNAT_SESSION_TCP_FIN_WAIT:
4136 case SNAT_SESSION_TCP_CLOSE_WAIT:
4137 case SNAT_SESSION_TCP_LAST_ACK:
4138 ses0->expire = now + sm->tcp_transitory_timeout;
4140 case SNAT_SESSION_TCP_ESTABLISHED:
4141 ses0->expire = now + sm->tcp_established_timeout;
4146 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
4147 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
4149 snat_in2out_trace_t *t =
4150 vlib_add_trace (vm, node, b0, sizeof (*t));
4151 t->is_slow_path = 0;
4152 t->sw_if_index = sw_if_index0;
4153 t->next_index = next0;
4154 t->session_index = ~0;
4156 t->session_index = ses0 - dm0->sessions;
4159 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
4161 ip1 = vlib_buffer_get_current (b1);
4162 udp1 = ip4_next_header (ip1);
4163 tcp1 = (tcp_header_t *) udp1;
4165 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
4167 if (PREDICT_FALSE(ip1->ttl == 1))
4169 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4170 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
4171 ICMP4_time_exceeded_ttl_exceeded_in_transit,
4173 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4177 proto1 = ip_proto_to_snat_proto (ip1->protocol);
4179 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
4181 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
4182 icmp1 = (icmp46_header_t *) udp1;
4184 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
4185 rx_fib_index1, node, next1, thread_index,
4190 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
4191 if (PREDICT_FALSE(!dm1))
4193 nat_log_info ("no match for internal host %U",
4194 format_ip4_address, &ip0->src_address);
4195 next1 = SNAT_IN2OUT_NEXT_DROP;
4196 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
4200 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
4202 key1.ext_host_addr = ip1->dst_address;
4203 key1.ext_host_port = tcp1->dst;
4205 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src, key1);
4206 if (PREDICT_FALSE(!ses1))
4208 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
4210 key1.out_port = clib_host_to_net_u16 (lo_port1 +
4211 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
4213 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
4216 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
4219 if (PREDICT_FALSE(!ses1))
4221 /* too many sessions for user, send ICMP error packet */
4223 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4224 icmp4_error_set_vnet_buffer (b1, ICMP4_destination_unreachable,
4225 ICMP4_destination_unreachable_destination_unreachable_host,
4227 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4232 new_port1 = ses1->out.out_port;
4234 old_addr1.as_u32 = ip1->src_address.as_u32;
4235 ip1->src_address.as_u32 = new_addr1.as_u32;
4236 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
4238 sum1 = ip1->checksum;
4239 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
4241 src_address /* changed member */);
4242 ip1->checksum = ip_csum_fold (sum1);
4244 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
4246 if (tcp1->flags & TCP_FLAG_SYN)
4247 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
4248 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
4249 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
4250 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
4251 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
4252 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
4253 snat_det_ses_close(dm1, ses1);
4254 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
4255 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
4256 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
4257 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
4259 old_port1 = tcp1->src;
4260 tcp1->src = new_port1;
4262 sum1 = tcp1->checksum;
4263 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
4265 dst_address /* changed member */);
4266 sum1 = ip_csum_update (sum1, old_port1, new_port1,
4267 ip4_header_t /* cheat */,
4268 length /* changed member */);
4269 tcp1->checksum = ip_csum_fold(sum1);
4273 ses1->state = SNAT_SESSION_UDP_ACTIVE;
4274 old_port1 = udp1->src_port;
4275 udp1->src_port = new_port1;
4281 case SNAT_SESSION_UDP_ACTIVE:
4282 ses1->expire = now + sm->udp_timeout;
4284 case SNAT_SESSION_TCP_SYN_SENT:
4285 case SNAT_SESSION_TCP_FIN_WAIT:
4286 case SNAT_SESSION_TCP_CLOSE_WAIT:
4287 case SNAT_SESSION_TCP_LAST_ACK:
4288 ses1->expire = now + sm->tcp_transitory_timeout;
4290 case SNAT_SESSION_TCP_ESTABLISHED:
4291 ses1->expire = now + sm->tcp_established_timeout;
4296 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
4297 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
4299 snat_in2out_trace_t *t =
4300 vlib_add_trace (vm, node, b1, sizeof (*t));
4301 t->is_slow_path = 0;
4302 t->sw_if_index = sw_if_index1;
4303 t->next_index = next1;
4304 t->session_index = ~0;
4306 t->session_index = ses1 - dm1->sessions;
4309 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
4311 /* verify speculative enqueues, maybe switch current next frame */
4312 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
4313 to_next, n_left_to_next,
4314 bi0, bi1, next0, next1);
4317 while (n_left_from > 0 && n_left_to_next > 0)
4325 ip4_address_t new_addr0, old_addr0;
4326 u16 old_port0, new_port0, lo_port0, i0;
4327 udp_header_t * udp0;
4328 tcp_header_t * tcp0;
4330 snat_det_out_key_t key0;
4331 snat_det_map_t * dm0;
4332 snat_det_session_t * ses0 = 0;
4334 icmp46_header_t * icmp0;
4336 /* speculatively enqueue b0 to the current next frame */
4342 n_left_to_next -= 1;
4344 b0 = vlib_get_buffer (vm, bi0);
4345 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
4347 ip0 = vlib_buffer_get_current (b0);
4348 udp0 = ip4_next_header (ip0);
4349 tcp0 = (tcp_header_t *) udp0;
4351 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
4353 if (PREDICT_FALSE(ip0->ttl == 1))
4355 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4356 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
4357 ICMP4_time_exceeded_ttl_exceeded_in_transit,
4359 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4363 proto0 = ip_proto_to_snat_proto (ip0->protocol);
4365 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
4367 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
4368 icmp0 = (icmp46_header_t *) udp0;
4370 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
4371 rx_fib_index0, node, next0, thread_index,
4376 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
4377 if (PREDICT_FALSE(!dm0))
4379 nat_log_info ("no match for internal host %U",
4380 format_ip4_address, &ip0->src_address);
4381 next0 = SNAT_IN2OUT_NEXT_DROP;
4382 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
4386 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
4388 key0.ext_host_addr = ip0->dst_address;
4389 key0.ext_host_port = tcp0->dst;
4391 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
4392 if (PREDICT_FALSE(!ses0))
4394 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
4396 key0.out_port = clib_host_to_net_u16 (lo_port0 +
4397 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
4399 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
4402 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
4405 if (PREDICT_FALSE(!ses0))
4407 /* too many sessions for user, send ICMP error packet */
4409 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4410 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
4411 ICMP4_destination_unreachable_destination_unreachable_host,
4413 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4418 new_port0 = ses0->out.out_port;
4420 old_addr0.as_u32 = ip0->src_address.as_u32;
4421 ip0->src_address.as_u32 = new_addr0.as_u32;
4422 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
4424 sum0 = ip0->checksum;
4425 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
4427 src_address /* changed member */);
4428 ip0->checksum = ip_csum_fold (sum0);
4430 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
4432 if (tcp0->flags & TCP_FLAG_SYN)
4433 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
4434 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
4435 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
4436 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
4437 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
4438 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
4439 snat_det_ses_close(dm0, ses0);
4440 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
4441 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
4442 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
4443 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
4445 old_port0 = tcp0->src;
4446 tcp0->src = new_port0;
4448 sum0 = tcp0->checksum;
4449 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
4451 dst_address /* changed member */);
4452 sum0 = ip_csum_update (sum0, old_port0, new_port0,
4453 ip4_header_t /* cheat */,
4454 length /* changed member */);
4455 tcp0->checksum = ip_csum_fold(sum0);
4459 ses0->state = SNAT_SESSION_UDP_ACTIVE;
4460 old_port0 = udp0->src_port;
4461 udp0->src_port = new_port0;
4467 case SNAT_SESSION_UDP_ACTIVE:
4468 ses0->expire = now + sm->udp_timeout;
4470 case SNAT_SESSION_TCP_SYN_SENT:
4471 case SNAT_SESSION_TCP_FIN_WAIT:
4472 case SNAT_SESSION_TCP_CLOSE_WAIT:
4473 case SNAT_SESSION_TCP_LAST_ACK:
4474 ses0->expire = now + sm->tcp_transitory_timeout;
4476 case SNAT_SESSION_TCP_ESTABLISHED:
4477 ses0->expire = now + sm->tcp_established_timeout;
4482 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
4483 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
4485 snat_in2out_trace_t *t =
4486 vlib_add_trace (vm, node, b0, sizeof (*t));
4487 t->is_slow_path = 0;
4488 t->sw_if_index = sw_if_index0;
4489 t->next_index = next0;
4490 t->session_index = ~0;
4492 t->session_index = ses0 - dm0->sessions;
4495 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
4497 /* verify speculative enqueue, maybe switch current next frame */
4498 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
4499 to_next, n_left_to_next,
4503 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
4506 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
4507 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
4509 return frame->n_vectors;
4512 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
4513 .function = snat_det_in2out_node_fn,
4514 .name = "nat44-det-in2out",
4515 .vector_size = sizeof (u32),
4516 .format_trace = format_snat_in2out_trace,
4517 .type = VLIB_NODE_TYPE_INTERNAL,
4519 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
4520 .error_strings = snat_in2out_error_strings,
4522 .runtime_data_bytes = sizeof (snat_runtime_t),
4526 /* edit / add dispositions here */
4528 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
4529 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
4530 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4534 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
4537 * Get address and port values to be used for ICMP packet translation
4538 * and create session if needed
4540 * @param[in,out] sm NAT main
4541 * @param[in,out] node NAT node runtime
4542 * @param[in] thread_index thread index
4543 * @param[in,out] b0 buffer containing packet to be translated
4544 * @param[out] p_proto protocol used for matching
4545 * @param[out] p_value address and port after NAT translation
4546 * @param[out] p_dont_translate if packet should not be translated
4547 * @param d optional parameter
4548 * @param e optional parameter
4550 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
4551 u32 thread_index, vlib_buffer_t *b0,
4552 ip4_header_t *ip0, u8 *p_proto,
4553 snat_session_key_t *p_value,
4554 u8 *p_dont_translate, void *d, void *e)
4556 icmp46_header_t *icmp0;
4560 snat_det_out_key_t key0;
4561 u8 dont_translate = 0;
4563 icmp_echo_header_t *echo0, *inner_echo0 = 0;
4564 ip4_header_t *inner_ip0;
4565 void *l4_header = 0;
4566 icmp46_header_t *inner_icmp0;
4567 snat_det_map_t * dm0 = 0;
4568 ip4_address_t new_addr0;
4570 snat_det_session_t * ses0 = 0;
4571 ip4_address_t in_addr;
4574 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
4575 echo0 = (icmp_echo_header_t *)(icmp0+1);
4576 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
4577 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
4579 if (!icmp_is_error_message (icmp0))
4581 protocol = SNAT_PROTOCOL_ICMP;
4582 in_addr = ip0->src_address;
4583 in_port = echo0->identifier;
4587 inner_ip0 = (ip4_header_t *)(echo0+1);
4588 l4_header = ip4_next_header (inner_ip0);
4589 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
4590 in_addr = inner_ip0->dst_address;
4593 case SNAT_PROTOCOL_ICMP:
4594 inner_icmp0 = (icmp46_header_t*)l4_header;
4595 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
4596 in_port = inner_echo0->identifier;
4598 case SNAT_PROTOCOL_UDP:
4599 case SNAT_PROTOCOL_TCP:
4600 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
4603 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
4604 next0 = SNAT_IN2OUT_NEXT_DROP;
4609 dm0 = snat_det_map_by_user(sm, &in_addr);
4610 if (PREDICT_FALSE(!dm0))
4612 nat_log_info ("no match for internal host %U",
4613 format_ip4_address, &in_addr);
4614 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
4615 IP_PROTOCOL_ICMP, rx_fib_index0)))
4620 next0 = SNAT_IN2OUT_NEXT_DROP;
4621 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
4625 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
4627 key0.ext_host_addr = ip0->dst_address;
4628 key0.ext_host_port = 0;
4630 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port, key0);
4631 if (PREDICT_FALSE(!ses0))
4633 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
4634 IP_PROTOCOL_ICMP, rx_fib_index0)))
4639 if (icmp0->type != ICMP4_echo_request)
4641 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
4642 next0 = SNAT_IN2OUT_NEXT_DROP;
4645 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
4647 key0.out_port = clib_host_to_net_u16 (lo_port0 +
4648 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
4650 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
4653 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
4656 if (PREDICT_FALSE(!ses0))
4658 next0 = SNAT_IN2OUT_NEXT_DROP;
4659 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
4664 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
4665 !icmp_is_error_message (icmp0)))
4667 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
4668 next0 = SNAT_IN2OUT_NEXT_DROP;
4672 u32 now = (u32) vlib_time_now (sm->vlib_main);
4674 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
4675 ses0->expire = now + sm->icmp_timeout;
4678 *p_proto = protocol;
4681 p_value->addr = new_addr0;
4682 p_value->fib_index = sm->outside_fib_index;
4683 p_value->port = ses0->out.out_port;
4685 *p_dont_translate = dont_translate;
4687 *(snat_det_session_t**)d = ses0;
4689 *(snat_det_map_t**)e = dm0;
4693 /**********************/
4694 /*** worker handoff ***/
4695 /**********************/
4697 snat_in2out_worker_handoff_fn_inline (vlib_main_t * vm,
4698 vlib_node_runtime_t * node,
4699 vlib_frame_t * frame,
4702 snat_main_t *sm = &snat_main;
4703 vlib_thread_main_t *tm = vlib_get_thread_main ();
4704 u32 n_left_from, *from, *to_next = 0, *to_next_drop = 0;
4705 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
4706 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
4708 vlib_frame_queue_elt_t *hf = 0;
4709 vlib_frame_queue_t *fq;
4710 vlib_frame_t *f = 0;
4712 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
4713 u32 next_worker_index = 0;
4714 u32 current_worker_index = ~0;
4715 u32 thread_index = vm->thread_index;
4718 vlib_frame_t *d = 0;
4720 ASSERT (vec_len (sm->workers));
4724 fq_index = sm->fq_in2out_output_index;
4725 to_node_index = sm->in2out_output_node_index;
4729 fq_index = sm->fq_in2out_index;
4730 to_node_index = sm->in2out_node_index;
4733 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
4735 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
4737 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
4738 tm->n_vlib_mains - 1,
4739 (vlib_frame_queue_t *) (~0));
4742 from = vlib_frame_vector_args (frame);
4743 n_left_from = frame->n_vectors;
4745 while (n_left_from > 0)
4758 b0 = vlib_get_buffer (vm, bi0);
4760 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
4761 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
4763 ip0 = vlib_buffer_get_current (b0);
4765 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
4767 if (PREDICT_FALSE (next_worker_index != thread_index))
4771 if (next_worker_index != current_worker_index)
4773 fq = is_vlib_frame_queue_congested (
4774 fq_index, next_worker_index, NAT_FQ_NELTS - 2,
4775 congested_handoff_queue_by_worker_index);
4779 /* if this is 1st frame */
4782 d = vlib_get_frame_to_node (vm, sm->error_node_index);
4783 to_next_drop = vlib_frame_vector_args (d);
4786 to_next_drop[0] = bi0;
4789 b0->error = node->errors[SNAT_IN2OUT_ERROR_FQ_CONGESTED];
4794 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
4796 hf = vlib_get_worker_handoff_queue_elt (fq_index,
4798 handoff_queue_elt_by_worker_index);
4800 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
4801 to_next_worker = &hf->buffer_index[hf->n_vectors];
4802 current_worker_index = next_worker_index;
4805 /* enqueue to correct worker thread */
4806 to_next_worker[0] = bi0;
4808 n_left_to_next_worker--;
4810 if (n_left_to_next_worker == 0)
4812 hf->n_vectors = VLIB_FRAME_SIZE;
4813 vlib_put_frame_queue_elt (hf);
4814 current_worker_index = ~0;
4815 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
4822 /* if this is 1st frame */
4825 f = vlib_get_frame_to_node (vm, to_node_index);
4826 to_next = vlib_frame_vector_args (f);
4835 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
4836 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
4838 snat_in2out_worker_handoff_trace_t *t =
4839 vlib_add_trace (vm, node, b0, sizeof (*t));
4840 t->next_worker_index = next_worker_index;
4841 t->do_handoff = do_handoff;
4846 vlib_put_frame_to_node (vm, to_node_index, f);
4849 vlib_put_frame_to_node (vm, sm->error_node_index, d);
4852 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
4854 /* Ship frames to the worker nodes */
4855 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
4857 if (handoff_queue_elt_by_worker_index[i])
4859 hf = handoff_queue_elt_by_worker_index[i];
4861 * It works better to let the handoff node
4862 * rate-adapt, always ship the handoff queue element.
4864 if (1 || hf->n_vectors == hf->last_n_vectors)
4866 vlib_put_frame_queue_elt (hf);
4867 handoff_queue_elt_by_worker_index[i] = 0;
4870 hf->last_n_vectors = hf->n_vectors;
4872 congested_handoff_queue_by_worker_index[i] =
4873 (vlib_frame_queue_t *) (~0);
4876 current_worker_index = ~0;
4877 return frame->n_vectors;
4881 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
4882 vlib_node_runtime_t * node,
4883 vlib_frame_t * frame)
4885 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 0);
4888 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
4889 .function = snat_in2out_worker_handoff_fn,
4890 .name = "nat44-in2out-worker-handoff",
4891 .vector_size = sizeof (u32),
4892 .format_trace = format_snat_in2out_worker_handoff_trace,
4893 .type = VLIB_NODE_TYPE_INTERNAL,
4895 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
4896 .error_strings = snat_in2out_error_strings,
4905 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
4906 snat_in2out_worker_handoff_fn);
4909 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
4910 vlib_node_runtime_t * node,
4911 vlib_frame_t * frame)
4913 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 1);
4916 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
4917 .function = snat_in2out_output_worker_handoff_fn,
4918 .name = "nat44-in2out-output-worker-handoff",
4919 .vector_size = sizeof (u32),
4920 .format_trace = format_snat_in2out_worker_handoff_trace,
4921 .type = VLIB_NODE_TYPE_INTERNAL,
4930 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
4931 snat_in2out_output_worker_handoff_fn);
4933 static_always_inline int
4934 is_hairpinning (snat_main_t *sm, ip4_address_t * dst_addr)
4936 snat_address_t * ap;
4937 clib_bihash_kv_8_8_t kv, value;
4938 snat_session_key_t m_key;
4940 vec_foreach (ap, sm->addresses)
4942 if (ap->addr.as_u32 == dst_addr->as_u32)
4946 m_key.addr.as_u32 = dst_addr->as_u32;
4947 m_key.fib_index = 0;
4950 kv.key = m_key.as_u64;
4951 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
4958 snat_hairpin_dst_fn_inline (vlib_main_t * vm,
4959 vlib_node_runtime_t * node,
4960 vlib_frame_t * frame,
4963 u32 n_left_from, * from, * to_next, stats_node_index;
4964 snat_in2out_next_t next_index;
4965 u32 pkts_processed = 0;
4966 snat_main_t * sm = &snat_main;
4968 stats_node_index = is_ed ? nat44_ed_hairpin_dst_node.index :
4969 snat_hairpin_dst_node.index;
4971 from = vlib_frame_vector_args (frame);
4972 n_left_from = frame->n_vectors;
4973 next_index = node->cached_next_index;
4975 while (n_left_from > 0)
4979 vlib_get_next_frame (vm, node, next_index,
4980 to_next, n_left_to_next);
4982 while (n_left_from > 0 && n_left_to_next > 0)
4990 /* speculatively enqueue b0 to the current next frame */
4996 n_left_to_next -= 1;
4998 b0 = vlib_get_buffer (vm, bi0);
4999 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
5000 ip0 = vlib_buffer_get_current (b0);
5002 proto0 = ip_proto_to_snat_proto (ip0->protocol);
5004 vnet_buffer (b0)->snat.flags = 0;
5005 if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
5007 if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
5009 udp_header_t * udp0 = ip4_next_header (ip0);
5010 tcp_header_t * tcp0 = (tcp_header_t *) udp0;
5012 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, is_ed);
5014 else if (proto0 == SNAT_PROTOCOL_ICMP)
5016 icmp46_header_t * icmp0 = ip4_next_header (ip0);
5018 snat_icmp_hairpinning (sm, b0, ip0, icmp0, is_ed);
5023 nat44_ed_hairpinning_unknown_proto (sm, b0, ip0);
5025 nat_hairpinning_sm_unknown_proto (sm, b0, ip0);
5028 vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
5031 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
5033 /* verify speculative enqueue, maybe switch current next frame */
5034 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
5035 to_next, n_left_to_next,
5039 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
5042 vlib_node_increment_counter (vm, stats_node_index,
5043 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
5045 return frame->n_vectors;
5049 snat_hairpin_dst_fn (vlib_main_t * vm,
5050 vlib_node_runtime_t * node,
5051 vlib_frame_t * frame)
5053 return snat_hairpin_dst_fn_inline (vm, node, frame, 0);
5056 VLIB_REGISTER_NODE (snat_hairpin_dst_node) = {
5057 .function = snat_hairpin_dst_fn,
5058 .name = "nat44-hairpin-dst",
5059 .vector_size = sizeof (u32),
5060 .type = VLIB_NODE_TYPE_INTERNAL,
5061 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
5062 .error_strings = snat_in2out_error_strings,
5065 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
5066 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
5070 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_dst_node,
5071 snat_hairpin_dst_fn);
5074 nat44_ed_hairpin_dst_fn (vlib_main_t * vm,
5075 vlib_node_runtime_t * node,
5076 vlib_frame_t * frame)
5078 return snat_hairpin_dst_fn_inline (vm, node, frame, 1);
5081 VLIB_REGISTER_NODE (nat44_ed_hairpin_dst_node) = {
5082 .function = nat44_ed_hairpin_dst_fn,
5083 .name = "nat44-ed-hairpin-dst",
5084 .vector_size = sizeof (u32),
5085 .type = VLIB_NODE_TYPE_INTERNAL,
5086 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
5087 .error_strings = snat_in2out_error_strings,
5090 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
5091 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
5095 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_hairpin_dst_node,
5096 nat44_ed_hairpin_dst_fn);
5099 snat_hairpin_src_fn_inline (vlib_main_t * vm,
5100 vlib_node_runtime_t * node,
5101 vlib_frame_t * frame,
5104 u32 n_left_from, * from, * to_next, stats_node_index;
5105 snat_in2out_next_t next_index;
5106 u32 pkts_processed = 0;
5107 snat_main_t *sm = &snat_main;
5109 stats_node_index = is_ed ? nat44_ed_hairpin_src_node.index :
5110 snat_hairpin_src_node.index;
5112 from = vlib_frame_vector_args (frame);
5113 n_left_from = frame->n_vectors;
5114 next_index = node->cached_next_index;
5116 while (n_left_from > 0)
5120 vlib_get_next_frame (vm, node, next_index,
5121 to_next, n_left_to_next);
5123 while (n_left_from > 0 && n_left_to_next > 0)
5128 snat_interface_t *i;
5131 /* speculatively enqueue b0 to the current next frame */
5137 n_left_to_next -= 1;
5139 b0 = vlib_get_buffer (vm, bi0);
5140 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
5141 next0 = SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT;
5143 pool_foreach (i, sm->output_feature_interfaces,
5145 /* Only packets from NAT inside interface */
5146 if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
5148 if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
5149 SNAT_FLAG_HAIRPINNING))
5151 if (PREDICT_TRUE (sm->num_workers > 1))
5152 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
5154 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
5160 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
5162 /* verify speculative enqueue, maybe switch current next frame */
5163 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
5164 to_next, n_left_to_next,
5168 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
5171 vlib_node_increment_counter (vm, stats_node_index,
5172 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
5174 return frame->n_vectors;
5178 snat_hairpin_src_fn (vlib_main_t * vm,
5179 vlib_node_runtime_t * node,
5180 vlib_frame_t * frame)
5182 return snat_hairpin_src_fn_inline (vm, node, frame, 0);
5185 VLIB_REGISTER_NODE (snat_hairpin_src_node) = {
5186 .function = snat_hairpin_src_fn,
5187 .name = "nat44-hairpin-src",
5188 .vector_size = sizeof (u32),
5189 .type = VLIB_NODE_TYPE_INTERNAL,
5190 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
5191 .error_strings = snat_in2out_error_strings,
5192 .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
5194 [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
5195 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
5196 [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
5197 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
5201 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_src_node,
5202 snat_hairpin_src_fn);
5205 nat44_ed_hairpin_src_fn (vlib_main_t * vm,
5206 vlib_node_runtime_t * node,
5207 vlib_frame_t * frame)
5209 return snat_hairpin_src_fn_inline (vm, node, frame, 1);
5212 VLIB_REGISTER_NODE (nat44_ed_hairpin_src_node) = {
5213 .function = nat44_ed_hairpin_src_fn,
5214 .name = "nat44-ed-hairpin-src",
5215 .vector_size = sizeof (u32),
5216 .type = VLIB_NODE_TYPE_INTERNAL,
5217 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
5218 .error_strings = snat_in2out_error_strings,
5219 .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
5221 [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
5222 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-ed-in2out-output",
5223 [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
5224 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
5228 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_hairpin_src_node,
5229 nat44_ed_hairpin_src_fn);
5232 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
5233 vlib_node_runtime_t * node,
5234 vlib_frame_t * frame)
5236 u32 n_left_from, * from, * to_next;
5237 snat_in2out_next_t next_index;
5238 u32 pkts_processed = 0;
5239 snat_main_t * sm = &snat_main;
5240 u32 stats_node_index;
5242 stats_node_index = snat_in2out_fast_node.index;
5244 from = vlib_frame_vector_args (frame);
5245 n_left_from = frame->n_vectors;
5246 next_index = node->cached_next_index;
5248 while (n_left_from > 0)
5252 vlib_get_next_frame (vm, node, next_index,
5253 to_next, n_left_to_next);
5255 while (n_left_from > 0 && n_left_to_next > 0)
5263 u32 new_addr0, old_addr0;
5264 u16 old_port0, new_port0;
5265 udp_header_t * udp0;
5266 tcp_header_t * tcp0;
5267 icmp46_header_t * icmp0;
5268 snat_session_key_t key0, sm0;
5272 /* speculatively enqueue b0 to the current next frame */
5278 n_left_to_next -= 1;
5280 b0 = vlib_get_buffer (vm, bi0);
5281 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
5283 ip0 = vlib_buffer_get_current (b0);
5284 udp0 = ip4_next_header (ip0);
5285 tcp0 = (tcp_header_t *) udp0;
5286 icmp0 = (icmp46_header_t *) udp0;
5288 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
5289 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
5291 if (PREDICT_FALSE(ip0->ttl == 1))
5293 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
5294 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
5295 ICMP4_time_exceeded_ttl_exceeded_in_transit,
5297 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
5301 proto0 = ip_proto_to_snat_proto (ip0->protocol);
5303 if (PREDICT_FALSE (proto0 == ~0))
5306 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
5308 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
5309 rx_fib_index0, node, next0, ~0, 0, 0);
5313 key0.addr = ip0->src_address;
5314 key0.protocol = proto0;
5315 key0.port = udp0->src_port;
5316 key0.fib_index = rx_fib_index0;
5318 if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0))
5320 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
5321 next0= SNAT_IN2OUT_NEXT_DROP;
5325 new_addr0 = sm0.addr.as_u32;
5326 new_port0 = sm0.port;
5327 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
5328 old_addr0 = ip0->src_address.as_u32;
5329 ip0->src_address.as_u32 = new_addr0;
5331 sum0 = ip0->checksum;
5332 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
5334 src_address /* changed member */);
5335 ip0->checksum = ip_csum_fold (sum0);
5337 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
5339 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
5341 old_port0 = tcp0->src_port;
5342 tcp0->src_port = new_port0;
5344 sum0 = tcp0->checksum;
5345 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
5347 dst_address /* changed member */);
5348 sum0 = ip_csum_update (sum0, old_port0, new_port0,
5349 ip4_header_t /* cheat */,
5350 length /* changed member */);
5351 tcp0->checksum = ip_csum_fold(sum0);
5355 old_port0 = udp0->src_port;
5356 udp0->src_port = new_port0;
5362 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
5364 sum0 = tcp0->checksum;
5365 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
5367 dst_address /* changed member */);
5368 tcp0->checksum = ip_csum_fold(sum0);
5373 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
5376 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
5377 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
5379 snat_in2out_trace_t *t =
5380 vlib_add_trace (vm, node, b0, sizeof (*t));
5381 t->sw_if_index = sw_if_index0;
5382 t->next_index = next0;
5385 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
5387 /* verify speculative enqueue, maybe switch current next frame */
5388 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
5389 to_next, n_left_to_next,
5393 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
5396 vlib_node_increment_counter (vm, stats_node_index,
5397 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
5399 return frame->n_vectors;
5403 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
5404 .function = snat_in2out_fast_static_map_fn,
5405 .name = "nat44-in2out-fast",
5406 .vector_size = sizeof (u32),
5407 .format_trace = format_snat_in2out_fast_trace,
5408 .type = VLIB_NODE_TYPE_INTERNAL,
5410 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
5411 .error_strings = snat_in2out_error_strings,
5413 .runtime_data_bytes = sizeof (snat_runtime_t),
5415 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
5417 /* edit / add dispositions here */
5419 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
5420 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
5421 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
5422 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
5423 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
5427 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);