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>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/error.h>
31 #include <vppinfra/elog.h>
38 } snat_in2out_trace_t;
41 u32 next_worker_index;
43 } snat_in2out_worker_handoff_trace_t;
45 /* packet trace format function */
46 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
53 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
55 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
56 t->sw_if_index, t->next_index, t->session_index);
61 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
67 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
68 t->sw_if_index, t->next_index);
73 static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args)
75 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
76 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
77 snat_in2out_worker_handoff_trace_t * t =
78 va_arg (*args, snat_in2out_worker_handoff_trace_t *);
81 m = t->do_handoff ? "next worker" : "same worker";
82 s = format (s, "NAT44_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
91 } nat44_in2out_reass_trace_t;
93 static u8 * format_nat44_in2out_reass_trace (u8 * s, va_list * args)
95 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
96 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
97 nat44_in2out_reass_trace_t * t = va_arg (*args, nat44_in2out_reass_trace_t *);
99 s = format (s, "NAT44_IN2OUT_REASS: sw_if_index %d, next index %d, status %s",
100 t->sw_if_index, t->next_index,
101 t->cached ? "cached" : "translated");
106 vlib_node_registration_t snat_in2out_node;
107 vlib_node_registration_t snat_in2out_slowpath_node;
108 vlib_node_registration_t snat_in2out_fast_node;
109 vlib_node_registration_t snat_in2out_worker_handoff_node;
110 vlib_node_registration_t snat_det_in2out_node;
111 vlib_node_registration_t snat_in2out_output_node;
112 vlib_node_registration_t snat_in2out_output_slowpath_node;
113 vlib_node_registration_t snat_in2out_output_worker_handoff_node;
114 vlib_node_registration_t snat_hairpin_dst_node;
115 vlib_node_registration_t snat_hairpin_src_node;
116 vlib_node_registration_t nat44_hairpinning_node;
117 vlib_node_registration_t nat44_in2out_reass_node;
120 #define foreach_snat_in2out_error \
121 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
122 _(IN2OUT_PACKETS, "Good in2out packets processed") \
123 _(OUT_OF_PORTS, "Out of ports") \
124 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
125 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
126 _(NO_TRANSLATION, "No translation") \
127 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
128 _(DROP_FRAGMENT, "Drop fragment") \
129 _(MAX_REASS, "Maximum reassemblies exceeded") \
130 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
133 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
134 foreach_snat_in2out_error
137 } snat_in2out_error_t;
139 static char * snat_in2out_error_strings[] = {
140 #define _(sym,string) string,
141 foreach_snat_in2out_error
146 SNAT_IN2OUT_NEXT_LOOKUP,
147 SNAT_IN2OUT_NEXT_DROP,
148 SNAT_IN2OUT_NEXT_ICMP_ERROR,
149 SNAT_IN2OUT_NEXT_SLOW_PATH,
150 SNAT_IN2OUT_NEXT_REASS,
152 } snat_in2out_next_t;
155 SNAT_HAIRPIN_SRC_NEXT_DROP,
156 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT,
157 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH,
158 SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT,
159 SNAT_HAIRPIN_SRC_N_NEXT,
160 } snat_hairpin_next_t;
163 * @brief Check if packet should be translated
165 * Packets aimed at outside interface and external address with active session
166 * should be translated.
169 * @param rt NAT runtime data
170 * @param sw_if_index0 index of the inside interface
171 * @param ip0 IPv4 header
172 * @param proto0 NAT protocol
173 * @param rx_fib_index0 RX FIB index
175 * @returns 0 if packet should be translated otherwise 1
178 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
179 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
185 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
187 .fp_proto = FIB_PROTOCOL_IP4,
190 .ip4.as_u32 = ip0->dst_address.as_u32,
194 /* Don't NAT packet aimed at the intfc address */
195 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
196 ip0->dst_address.as_u32)))
199 fei = fib_table_lookup (rx_fib_index0, &pfx);
200 if (FIB_NODE_INDEX_INVALID != fei)
202 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
203 if (sw_if_index == ~0)
205 fei = fib_table_lookup (sm->outside_fib_index, &pfx);
206 if (FIB_NODE_INDEX_INVALID != fei)
207 sw_if_index = fib_entry_get_resolving_interface (fei);
210 pool_foreach (i, sm->interfaces,
212 /* NAT packet aimed at outside interface */
213 if ((nat_interface_is_outside(i)) && (sw_if_index == i->sw_if_index))
222 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
223 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
224 u32 rx_fib_index0, u32 thread_index)
226 udp_header_t * udp0 = ip4_next_header (ip0);
227 snat_session_key_t key0, sm0;
228 clib_bihash_kv_8_8_t kv0, value0;
230 key0.addr = ip0->dst_address;
231 key0.port = udp0->dst_port;
232 key0.protocol = proto0;
233 key0.fib_index = sm->outside_fib_index;
234 kv0.key = key0.as_u64;
236 /* NAT packet aimed at external address if */
237 /* has active sessions */
238 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
241 /* or is static mappings */
242 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
248 if (sm->forwarding_enabled)
251 return snat_not_translate_fast(sm, node, sw_if_index0, ip0, proto0,
256 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
257 u32 proto0, u16 src_port, u16 dst_port,
258 u32 thread_index, u32 sw_if_index)
260 snat_session_key_t key0;
261 clib_bihash_kv_8_8_t kv0, value0;
265 key0.addr = ip0->src_address;
266 key0.port = src_port;
267 key0.protocol = proto0;
268 key0.fib_index = sm->outside_fib_index;
269 kv0.key = key0.as_u64;
271 if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
276 key0.addr = ip0->dst_address;
277 key0.port = dst_port;
278 key0.protocol = proto0;
279 key0.fib_index = sm->inside_fib_index;
280 kv0.key = key0.as_u64;
281 if (!clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
285 pool_foreach (i, sm->output_feature_interfaces,
287 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
296 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
299 snat_session_key_t * key0,
300 snat_session_t ** sessionp,
301 vlib_node_runtime_t * node,
307 clib_bihash_kv_8_8_t kv0;
308 snat_session_key_t key1;
309 u32 address_index = ~0;
310 u32 outside_fib_index;
312 udp_header_t * udp0 = ip4_next_header (ip0);
315 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
317 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
318 nat_ipfix_logging_max_sessions(sm->max_translations);
319 return SNAT_IN2OUT_NEXT_DROP;
322 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
325 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
326 return SNAT_IN2OUT_NEXT_DROP;
328 outside_fib_index = p[0];
330 key1.protocol = key0->protocol;
332 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
336 clib_warning ("create NAT user failed");
337 return SNAT_IN2OUT_NEXT_DROP;
340 /* First try to match static mapping by local address and port */
341 if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0, 0))
343 /* Try to create dynamic translation */
344 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
348 sm->per_thread_data[thread_index].snat_thread_index))
350 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
351 return SNAT_IN2OUT_NEXT_DROP;
357 s = nat_session_alloc_or_recycle (sm, u, thread_index);
360 clib_warning ("create NAT session failed");
361 return SNAT_IN2OUT_NEXT_DROP;
365 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
366 user_session_increment (sm, u, is_sm);
367 s->outside_address_index = address_index;
370 s->out2in.protocol = key0->protocol;
371 s->out2in.fib_index = outside_fib_index;
372 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
373 s->ext_host_port = udp0->dst_port;
376 /* Add to translation hashes */
377 kv0.key = s->in2out.as_u64;
378 kv0.value = s - sm->per_thread_data[thread_index].sessions;
379 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
381 clib_warning ("in2out key add failed");
383 kv0.key = s->out2in.as_u64;
384 kv0.value = s - sm->per_thread_data[thread_index].sessions;
386 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
388 clib_warning ("out2in key add failed");
391 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
392 s->out2in.addr.as_u32,
396 s->in2out.fib_index);
401 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
402 snat_session_key_t *p_key0)
404 icmp46_header_t *icmp0;
405 snat_session_key_t key0;
406 icmp_echo_header_t *echo0, *inner_echo0 = 0;
407 ip4_header_t *inner_ip0 = 0;
409 icmp46_header_t *inner_icmp0;
411 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
412 echo0 = (icmp_echo_header_t *)(icmp0+1);
414 if (!icmp_is_error_message (icmp0))
416 key0.protocol = SNAT_PROTOCOL_ICMP;
417 key0.addr = ip0->src_address;
418 key0.port = echo0->identifier;
422 inner_ip0 = (ip4_header_t *)(echo0+1);
423 l4_header = ip4_next_header (inner_ip0);
424 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
425 key0.addr = inner_ip0->dst_address;
426 switch (key0.protocol)
428 case SNAT_PROTOCOL_ICMP:
429 inner_icmp0 = (icmp46_header_t*)l4_header;
430 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
431 key0.port = inner_echo0->identifier;
433 case SNAT_PROTOCOL_UDP:
434 case SNAT_PROTOCOL_TCP:
435 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
438 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
442 return -1; /* success */
445 static_always_inline int
446 icmp_get_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
448 icmp46_header_t *icmp0;
449 nat_ed_ses_key_t key0;
450 icmp_echo_header_t *echo0, *inner_echo0 = 0;
451 ip4_header_t *inner_ip0 = 0;
453 icmp46_header_t *inner_icmp0;
455 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
456 echo0 = (icmp_echo_header_t *)(icmp0+1);
458 if (!icmp_is_error_message (icmp0))
460 key0.proto = IP_PROTOCOL_ICMP;
461 key0.l_addr = ip0->src_address;
462 key0.r_addr = ip0->dst_address;
463 key0.l_port = key0.r_port = echo0->identifier;
467 inner_ip0 = (ip4_header_t *)(echo0+1);
468 l4_header = ip4_next_header (inner_ip0);
469 key0.proto = inner_ip0->protocol;
470 key0.r_addr = inner_ip0->src_address;
471 key0.l_addr = inner_ip0->dst_address;
472 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
474 case SNAT_PROTOCOL_ICMP:
475 inner_icmp0 = (icmp46_header_t*)l4_header;
476 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
477 key0.r_port = key0.l_port = inner_echo0->identifier;
479 case SNAT_PROTOCOL_UDP:
480 case SNAT_PROTOCOL_TCP:
481 key0.l_port = ((tcp_udp_header_t*)l4_header)->dst_port;
482 key0.r_port = ((tcp_udp_header_t*)l4_header)->src_port;
485 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
493 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
496 nat_ed_ses_key_t key;
497 clib_bihash_kv_16_8_t kv, value;
499 snat_session_t *s = 0;
500 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
502 if (!sm->forwarding_enabled)
505 if (ip->protocol == IP_PROTOCOL_ICMP)
507 if (icmp_get_ed_key (ip, &key))
510 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
512 udp = ip4_next_header(ip);
513 key.l_addr = ip->src_address;
514 key.r_addr = ip->dst_address;
515 key.proto = ip->protocol;
516 key.r_port = udp->dst_port;
517 key.l_port = udp->src_port;
521 key.l_addr = ip->src_address;
522 key.r_addr = ip->dst_address;
523 key.proto = ip->protocol;
524 key.l_port = key.r_port = 0;
527 kv.key[0] = key.as_u64[0];
528 kv.key[1] = key.as_u64[1];
530 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &kv, &value))
532 s = pool_elt_at_index (sm->per_thread_data[thread_index].sessions, value.value);
533 if (is_fwd_bypass_session (s))
535 /* Per-user LRU list maintenance */
536 clib_dlist_remove (tsm->list_pool, s->per_user_index);
537 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
549 * Get address and port values to be used for ICMP packet translation
550 * and create session if needed
552 * @param[in,out] sm NAT main
553 * @param[in,out] node NAT node runtime
554 * @param[in] thread_index thread index
555 * @param[in,out] b0 buffer containing packet to be translated
556 * @param[out] p_proto protocol used for matching
557 * @param[out] p_value address and port after NAT translation
558 * @param[out] p_dont_translate if packet should not be translated
559 * @param d optional parameter
560 * @param e optional parameter
562 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
563 u32 thread_index, vlib_buffer_t *b0,
564 ip4_header_t *ip0, u8 *p_proto,
565 snat_session_key_t *p_value,
566 u8 *p_dont_translate, void *d, void *e)
568 icmp46_header_t *icmp0;
571 snat_session_key_t key0;
572 snat_session_t *s0 = 0;
573 u8 dont_translate = 0;
574 clib_bihash_kv_8_8_t kv0, value0;
578 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
579 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
580 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
582 err = icmp_get_key (ip0, &key0);
585 b0->error = node->errors[err];
586 next0 = SNAT_IN2OUT_NEXT_DROP;
589 key0.fib_index = rx_fib_index0;
591 kv0.key = key0.as_u64;
593 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
596 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] != ~0)
598 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
599 ip0, SNAT_PROTOCOL_ICMP, key0.port, key0.port, thread_index, sw_if_index0)))
607 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
608 ip0, SNAT_PROTOCOL_ICMP, rx_fib_index0, thread_index)))
615 if (PREDICT_FALSE(icmp_is_error_message (icmp0)))
617 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
618 next0 = SNAT_IN2OUT_NEXT_DROP;
622 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
623 &s0, node, next0, thread_index);
625 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
630 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
631 icmp0->type != ICMP4_echo_reply &&
632 !icmp_is_error_message (icmp0)))
634 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
635 next0 = SNAT_IN2OUT_NEXT_DROP;
639 if (PREDICT_FALSE (value0.value == ~0ULL))
641 nat_ed_ses_key_t key;
642 clib_bihash_kv_16_8_t s_kv, s_value;
646 if (icmp_get_ed_key (ip0, &key))
648 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
649 next0 = SNAT_IN2OUT_NEXT_DROP;
652 key.fib_index = rx_fib_index0;
653 s_kv.key[0] = key.as_u64[0];
654 s_kv.key[1] = key.as_u64[1];
655 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
656 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
660 next0 = SNAT_IN2OUT_NEXT_DROP;
665 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
670 *p_proto = key0.protocol;
672 *p_value = s0->out2in;
673 *p_dont_translate = dont_translate;
675 *(snat_session_t**)d = s0;
680 * Get address and port values to be used for ICMP packet translation
682 * @param[in] sm NAT main
683 * @param[in,out] node NAT node runtime
684 * @param[in] thread_index thread index
685 * @param[in,out] b0 buffer containing packet to be translated
686 * @param[out] p_proto protocol used for matching
687 * @param[out] p_value address and port after NAT translation
688 * @param[out] p_dont_translate if packet should not be translated
689 * @param d optional parameter
690 * @param e optional parameter
692 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
693 u32 thread_index, vlib_buffer_t *b0,
694 ip4_header_t *ip0, u8 *p_proto,
695 snat_session_key_t *p_value,
696 u8 *p_dont_translate, void *d, void *e)
698 icmp46_header_t *icmp0;
701 snat_session_key_t key0;
702 snat_session_key_t sm0;
703 u8 dont_translate = 0;
708 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
709 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
710 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
712 err = icmp_get_key (ip0, &key0);
715 b0->error = node->errors[err];
716 next0 = SNAT_IN2OUT_NEXT_DROP;
719 key0.fib_index = rx_fib_index0;
721 if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0, 0))
723 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
724 IP_PROTOCOL_ICMP, rx_fib_index0)))
730 if (icmp_is_error_message (icmp0))
732 next0 = SNAT_IN2OUT_NEXT_DROP;
736 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
737 next0 = SNAT_IN2OUT_NEXT_DROP;
741 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
742 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
743 !icmp_is_error_message (icmp0)))
745 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
746 next0 = SNAT_IN2OUT_NEXT_DROP;
753 *p_proto = key0.protocol;
754 *p_dont_translate = dont_translate;
758 static inline u32 icmp_in2out (snat_main_t *sm,
761 icmp46_header_t * icmp0,
764 vlib_node_runtime_t * node,
770 snat_session_key_t sm0;
772 icmp_echo_header_t *echo0, *inner_echo0 = 0;
773 ip4_header_t *inner_ip0;
775 icmp46_header_t *inner_icmp0;
777 u32 new_addr0, old_addr0;
778 u16 old_id0, new_id0;
783 echo0 = (icmp_echo_header_t *)(icmp0+1);
785 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0, ip0,
786 &protocol, &sm0, &dont_translate, d, e);
789 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
792 sum0 = ip_incremental_checksum (0, icmp0,
793 ntohs(ip0->length) - ip4_header_bytes (ip0));
794 checksum0 = ~ip_csum_fold (sum0);
795 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
797 next0 = SNAT_IN2OUT_NEXT_DROP;
801 old_addr0 = ip0->src_address.as_u32;
802 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
803 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
804 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
806 sum0 = ip0->checksum;
807 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
808 src_address /* changed member */);
809 ip0->checksum = ip_csum_fold (sum0);
811 if (icmp0->checksum == 0)
812 icmp0->checksum = 0xffff;
814 if (!icmp_is_error_message (icmp0))
817 if (PREDICT_FALSE(new_id0 != echo0->identifier))
819 old_id0 = echo0->identifier;
821 echo0->identifier = new_id0;
823 sum0 = icmp0->checksum;
824 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
826 icmp0->checksum = ip_csum_fold (sum0);
831 inner_ip0 = (ip4_header_t *)(echo0+1);
832 l4_header = ip4_next_header (inner_ip0);
834 if (!ip4_header_checksum_is_valid (inner_ip0))
836 next0 = SNAT_IN2OUT_NEXT_DROP;
840 old_addr0 = inner_ip0->dst_address.as_u32;
841 inner_ip0->dst_address = sm0.addr;
842 new_addr0 = inner_ip0->dst_address.as_u32;
844 sum0 = icmp0->checksum;
845 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
846 dst_address /* changed member */);
847 icmp0->checksum = ip_csum_fold (sum0);
851 case SNAT_PROTOCOL_ICMP:
852 inner_icmp0 = (icmp46_header_t*)l4_header;
853 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
855 old_id0 = inner_echo0->identifier;
857 inner_echo0->identifier = new_id0;
859 sum0 = icmp0->checksum;
860 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
862 icmp0->checksum = ip_csum_fold (sum0);
864 case SNAT_PROTOCOL_UDP:
865 case SNAT_PROTOCOL_TCP:
866 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
868 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
870 sum0 = icmp0->checksum;
871 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
873 icmp0->checksum = ip_csum_fold (sum0);
887 * Hairpinning allows two endpoints on the internal side of the NAT to
888 * communicate even if they only use each other's external IP addresses
891 * @param sm NAT main.
892 * @param b0 Vlib buffer.
893 * @param ip0 IP header.
894 * @param udp0 UDP header.
895 * @param tcp0 TCP header.
896 * @param proto0 NAT protocol.
899 snat_hairpinning (snat_main_t *sm,
906 snat_session_key_t key0, sm0;
908 clib_bihash_kv_8_8_t kv0, value0;
910 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
911 u16 new_dst_port0, old_dst_port0;
913 key0.addr = ip0->dst_address;
914 key0.port = udp0->dst_port;
915 key0.protocol = proto0;
916 key0.fib_index = sm->outside_fib_index;
917 kv0.key = key0.as_u64;
919 /* Check if destination is static mappings */
920 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
922 new_dst_addr0 = sm0.addr.as_u32;
923 new_dst_port0 = sm0.port;
924 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
926 /* or active session */
929 if (sm->num_workers > 1)
930 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
932 ti = sm->num_workers;
934 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
938 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
939 new_dst_addr0 = s0->in2out.addr.as_u32;
940 new_dst_port0 = s0->in2out.port;
941 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
945 /* Destination is behind the same NAT, use internal address and port */
948 old_dst_addr0 = ip0->dst_address.as_u32;
949 ip0->dst_address.as_u32 = new_dst_addr0;
950 sum0 = ip0->checksum;
951 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
952 ip4_header_t, dst_address);
953 ip0->checksum = ip_csum_fold (sum0);
955 old_dst_port0 = tcp0->dst;
956 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
958 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
960 tcp0->dst = new_dst_port0;
961 sum0 = tcp0->checksum;
962 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
963 ip4_header_t, dst_address);
964 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
965 ip4_header_t /* cheat */, length);
966 tcp0->checksum = ip_csum_fold(sum0);
970 udp0->dst_port = new_dst_port0;
976 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
978 sum0 = tcp0->checksum;
979 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
980 ip4_header_t, dst_address);
981 tcp0->checksum = ip_csum_fold(sum0);
990 snat_icmp_hairpinning (snat_main_t *sm,
993 icmp46_header_t * icmp0)
995 snat_session_key_t key0, sm0;
996 clib_bihash_kv_8_8_t kv0, value0;
997 u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
1001 if (!icmp_is_error_message (icmp0))
1003 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1004 u16 icmp_id0 = echo0->identifier;
1005 key0.addr = ip0->dst_address;
1006 key0.port = icmp_id0;
1007 key0.protocol = SNAT_PROTOCOL_ICMP;
1008 key0.fib_index = sm->outside_fib_index;
1009 kv0.key = key0.as_u64;
1011 if (sm->num_workers > 1)
1012 ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
1014 ti = sm->num_workers;
1016 /* Check if destination is in active sessions */
1017 if (clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
1020 /* or static mappings */
1021 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
1023 new_dst_addr0 = sm0.addr.as_u32;
1024 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1031 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
1032 new_dst_addr0 = s0->in2out.addr.as_u32;
1033 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1034 echo0->identifier = s0->in2out.port;
1035 sum0 = icmp0->checksum;
1036 sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
1037 icmp_echo_header_t, identifier);
1038 icmp0->checksum = ip_csum_fold (sum0);
1041 /* Destination is behind the same NAT, use internal address and port */
1044 old_dst_addr0 = ip0->dst_address.as_u32;
1045 ip0->dst_address.as_u32 = new_dst_addr0;
1046 sum0 = ip0->checksum;
1047 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
1048 ip4_header_t, dst_address);
1049 ip0->checksum = ip_csum_fold (sum0);
1055 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
1058 icmp46_header_t * icmp0,
1061 vlib_node_runtime_t * node,
1065 snat_session_t ** p_s0)
1067 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1068 next0, thread_index, p_s0, 0);
1069 snat_session_t * s0 = *p_s0;
1070 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
1073 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == 0)
1074 snat_icmp_hairpinning(sm, b0, ip0, icmp0);
1076 s0->last_heard = now;
1078 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
1079 /* Per-user LRU list maintenance */
1080 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1081 s0->per_user_index);
1082 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1083 s0->per_user_list_head_index,
1084 s0->per_user_index);
1089 snat_hairpinning_unknown_proto (snat_main_t *sm,
1093 u32 old_addr, new_addr = 0, ti = 0;
1094 clib_bihash_kv_8_8_t kv, value;
1095 clib_bihash_kv_16_8_t s_kv, s_value;
1096 nat_ed_ses_key_t key;
1097 snat_session_key_t m_key;
1098 snat_static_mapping_t *m;
1102 old_addr = ip->dst_address.as_u32;
1103 key.l_addr.as_u32 = ip->dst_address.as_u32;
1104 key.r_addr.as_u32 = ip->src_address.as_u32;
1105 key.fib_index = sm->outside_fib_index;
1106 key.proto = ip->protocol;
1109 s_kv.key[0] = key.as_u64[0];
1110 s_kv.key[1] = key.as_u64[1];
1111 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1113 m_key.addr = ip->dst_address;
1114 m_key.fib_index = sm->outside_fib_index;
1117 kv.key = m_key.as_u64;
1118 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1121 m = pool_elt_at_index (sm->static_mappings, value.value);
1122 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1123 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
1124 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
1128 if (sm->num_workers > 1)
1129 ti = sm->worker_out2in_cb (ip, sm->outside_fib_index);
1131 ti = sm->num_workers;
1133 s = pool_elt_at_index (sm->per_thread_data[ti].sessions, s_value.value);
1134 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1135 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
1136 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
1139 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
1140 ip->checksum = ip_csum_fold (sum);
1143 static snat_session_t *
1144 snat_in2out_unknown_proto (snat_main_t *sm,
1151 vlib_node_runtime_t * node)
1153 clib_bihash_kv_8_8_t kv, value;
1154 clib_bihash_kv_16_8_t s_kv, s_value;
1155 snat_static_mapping_t *m;
1156 snat_session_key_t m_key;
1157 u32 old_addr, new_addr = 0;
1160 dlist_elt_t *head, *elt;
1161 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1162 u32 elt_index, head_index, ses_index;
1164 nat_ed_ses_key_t key;
1165 u32 address_index = ~0;
1169 old_addr = ip->src_address.as_u32;
1171 key.l_addr = ip->src_address;
1172 key.r_addr = ip->dst_address;
1173 key.fib_index = rx_fib_index;
1174 key.proto = ip->protocol;
1177 s_kv.key[0] = key.as_u64[0];
1178 s_kv.key[1] = key.as_u64[1];
1180 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
1182 s = pool_elt_at_index (tsm->sessions, s_value.value);
1183 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1187 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
1189 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1190 nat_ipfix_logging_max_sessions(sm->max_translations);
1194 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
1198 clib_warning ("create NAT user failed");
1202 m_key.addr = ip->src_address;
1205 m_key.fib_index = rx_fib_index;
1206 kv.key = m_key.as_u64;
1208 /* Try to find static mapping first */
1209 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
1211 m = pool_elt_at_index (sm->static_mappings, value.value);
1212 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
1216 /* Fallback to 3-tuple key */
1219 /* Choose same out address as for TCP/UDP session to same destination */
1220 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1222 head_index = u->sessions_per_user_list_head_index;
1223 head = pool_elt_at_index (tsm->list_pool, head_index);
1224 elt_index = head->next;
1225 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1226 ses_index = elt->value;
1227 while (ses_index != ~0)
1229 s = pool_elt_at_index (tsm->sessions, ses_index);
1230 elt_index = elt->next;
1231 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1232 ses_index = elt->value;
1234 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
1236 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1237 address_index = s->outside_address_index;
1239 key.fib_index = sm->outside_fib_index;
1240 key.l_addr.as_u32 = new_addr;
1241 s_kv.key[0] = key.as_u64[0];
1242 s_kv.key[1] = key.as_u64[1];
1243 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1250 key.fib_index = sm->outside_fib_index;
1251 for (i = 0; i < vec_len (sm->addresses); i++)
1253 key.l_addr.as_u32 = sm->addresses[i].addr.as_u32;
1254 s_kv.key[0] = key.as_u64[0];
1255 s_kv.key[1] = key.as_u64[1];
1256 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1258 new_addr = ip->src_address.as_u32 = key.l_addr.as_u32;
1267 s = nat_session_alloc_or_recycle (sm, u, thread_index);
1270 clib_warning ("create NAT session failed");
1274 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1275 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
1276 s->outside_address_index = address_index;
1277 s->out2in.addr.as_u32 = new_addr;
1278 s->out2in.fib_index = sm->outside_fib_index;
1279 s->in2out.addr.as_u32 = old_addr;
1280 s->in2out.fib_index = rx_fib_index;
1281 s->in2out.port = s->out2in.port = ip->protocol;
1283 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1284 user_session_increment (sm, u, is_sm);
1286 /* Add to lookup tables */
1287 key.l_addr.as_u32 = old_addr;
1288 key.r_addr = ip->dst_address;
1289 key.proto = ip->protocol;
1290 key.fib_index = rx_fib_index;
1291 s_kv.key[0] = key.as_u64[0];
1292 s_kv.key[1] = key.as_u64[1];
1293 s_kv.value = s - tsm->sessions;
1294 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1295 clib_warning ("in2out key add failed");
1297 key.l_addr.as_u32 = new_addr;
1298 key.fib_index = sm->outside_fib_index;
1299 s_kv.key[0] = key.as_u64[0];
1300 s_kv.key[1] = key.as_u64[1];
1301 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1302 clib_warning ("out2in key add failed");
1305 /* Update IP checksum */
1307 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1308 ip->checksum = ip_csum_fold (sum);
1311 s->last_heard = now;
1313 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1314 /* Per-user LRU list maintenance */
1315 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1316 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1320 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1321 snat_hairpinning_unknown_proto(sm, b, ip);
1323 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1324 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1329 static snat_session_t *
1330 snat_in2out_lb (snat_main_t *sm,
1337 vlib_node_runtime_t * node)
1339 nat_ed_ses_key_t key;
1340 clib_bihash_kv_16_8_t s_kv, s_value;
1341 udp_header_t *udp = ip4_next_header (ip);
1342 tcp_header_t *tcp = (tcp_header_t *) udp;
1343 snat_session_t *s = 0;
1344 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1345 u32 old_addr, new_addr;
1346 u16 new_port, old_port;
1348 u32 proto = ip_proto_to_snat_proto (ip->protocol);
1349 snat_session_key_t e_key, l_key;
1353 old_addr = ip->src_address.as_u32;
1355 key.l_addr = ip->src_address;
1356 key.r_addr = ip->dst_address;
1357 key.fib_index = rx_fib_index;
1358 key.proto = ip->protocol;
1359 key.r_port = udp->dst_port;
1360 key.l_port = udp->src_port;
1361 s_kv.key[0] = key.as_u64[0];
1362 s_kv.key[1] = key.as_u64[1];
1364 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
1366 s = pool_elt_at_index (tsm->sessions, s_value.value);
1367 if (is_fwd_bypass_session (s))
1372 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
1374 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1375 nat_ipfix_logging_max_sessions(sm->max_translations);
1379 l_key.addr = ip->src_address;
1380 l_key.port = udp->src_port;
1381 l_key.protocol = proto;
1382 l_key.fib_index = rx_fib_index;
1383 if (snat_static_mapping_match(sm, l_key, &e_key, 0, 0, 0, &lb))
1386 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
1390 clib_warning ("create NAT user failed");
1394 s = nat_session_alloc_or_recycle (sm, u, thread_index);
1397 clib_warning ("create NAT session failed");
1401 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1402 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1404 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
1405 s->outside_address_index = ~0;
1408 s->out2in.protocol = l_key.protocol;
1409 user_session_increment (sm, u, 1 /* static */);
1411 /* Add to lookup tables */
1412 s_kv.value = s - tsm->sessions;
1413 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1414 clib_warning ("in2out-ed key add failed");
1416 key.l_addr = e_key.addr;
1417 key.fib_index = e_key.fib_index;
1418 key.l_port = e_key.port;
1419 s_kv.key[0] = key.as_u64[0];
1420 s_kv.key[1] = key.as_u64[1];
1421 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1422 clib_warning ("out2in-ed key add failed");
1425 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1427 /* Update IP checksum */
1429 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1430 if (is_twice_nat_session (s))
1431 sum = ip_csum_update (sum, ip->dst_address.as_u32,
1432 s->ext_host_addr.as_u32, ip4_header_t, dst_address);
1433 ip->checksum = ip_csum_fold (sum);
1435 if (PREDICT_TRUE(proto == SNAT_PROTOCOL_TCP))
1437 old_port = tcp->src_port;
1438 tcp->src_port = s->out2in.port;
1439 new_port = tcp->src_port;
1441 sum = tcp->checksum;
1442 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1443 sum = ip_csum_update (sum, old_port, new_port, ip4_header_t, length);
1444 if (is_twice_nat_session (s))
1446 sum = ip_csum_update (sum, ip->dst_address.as_u32,
1447 s->ext_host_addr.as_u32, ip4_header_t,
1449 sum = ip_csum_update (sum, tcp->dst_port, s->ext_host_port,
1450 ip4_header_t, length);
1451 tcp->dst_port = s->ext_host_port;
1452 ip->dst_address.as_u32 = s->ext_host_addr.as_u32;
1454 tcp->checksum = ip_csum_fold(sum);
1458 udp->src_port = s->out2in.port;
1459 if (is_twice_nat_session (s))
1461 udp->dst_port = s->ext_host_port;
1462 ip->dst_address.as_u32 = s->ext_host_addr.as_u32;
1467 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1468 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1471 s->last_heard = now;
1473 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1474 /* Per-user LRU list maintenance */
1475 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1476 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1482 snat_in2out_node_fn_inline (vlib_main_t * vm,
1483 vlib_node_runtime_t * node,
1484 vlib_frame_t * frame, int is_slow_path,
1485 int is_output_feature)
1487 u32 n_left_from, * from, * to_next;
1488 snat_in2out_next_t next_index;
1489 u32 pkts_processed = 0;
1490 snat_main_t * sm = &snat_main;
1491 f64 now = vlib_time_now (vm);
1492 u32 stats_node_index;
1493 u32 thread_index = vlib_get_thread_index ();
1495 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
1496 snat_in2out_node.index;
1498 from = vlib_frame_vector_args (frame);
1499 n_left_from = frame->n_vectors;
1500 next_index = node->cached_next_index;
1502 while (n_left_from > 0)
1506 vlib_get_next_frame (vm, node, next_index,
1507 to_next, n_left_to_next);
1509 while (n_left_from >= 4 && n_left_to_next >= 2)
1512 vlib_buffer_t * b0, * b1;
1514 u32 sw_if_index0, sw_if_index1;
1515 ip4_header_t * ip0, * ip1;
1516 ip_csum_t sum0, sum1;
1517 u32 new_addr0, old_addr0, new_addr1, old_addr1;
1518 u16 old_port0, new_port0, old_port1, new_port1;
1519 udp_header_t * udp0, * udp1;
1520 tcp_header_t * tcp0, * tcp1;
1521 icmp46_header_t * icmp0, * icmp1;
1522 snat_session_key_t key0, key1;
1523 u32 rx_fib_index0, rx_fib_index1;
1525 snat_session_t * s0 = 0, * s1 = 0;
1526 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
1527 u32 iph_offset0 = 0, iph_offset1 = 0;
1529 /* Prefetch next iteration. */
1531 vlib_buffer_t * p2, * p3;
1533 p2 = vlib_get_buffer (vm, from[2]);
1534 p3 = vlib_get_buffer (vm, from[3]);
1536 vlib_prefetch_buffer_header (p2, LOAD);
1537 vlib_prefetch_buffer_header (p3, LOAD);
1539 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1540 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1543 /* speculatively enqueue b0 and b1 to the current next frame */
1544 to_next[0] = bi0 = from[0];
1545 to_next[1] = bi1 = from[1];
1549 n_left_to_next -= 2;
1551 b0 = vlib_get_buffer (vm, bi0);
1552 b1 = vlib_get_buffer (vm, bi1);
1554 if (is_output_feature)
1555 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1557 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1560 udp0 = ip4_next_header (ip0);
1561 tcp0 = (tcp_header_t *) udp0;
1562 icmp0 = (icmp46_header_t *) udp0;
1564 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1565 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1568 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1570 if (PREDICT_FALSE(ip0->ttl == 1))
1572 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1573 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1574 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1576 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1580 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1582 /* Next configured feature, probably ip4-lookup */
1585 if (PREDICT_FALSE (proto0 == ~0))
1587 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
1588 thread_index, now, vm, node);
1590 next0 = SNAT_IN2OUT_NEXT_DROP;
1594 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1596 next0 = icmp_in2out_slow_path
1597 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1598 node, next0, now, thread_index, &s0);
1604 if (is_output_feature)
1606 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(sm, ip0, thread_index)))
1610 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1612 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1616 if (ip4_is_fragment (ip0))
1618 next0 = SNAT_IN2OUT_NEXT_REASS;
1623 key0.addr = ip0->src_address;
1624 key0.port = udp0->src_port;
1625 key0.protocol = proto0;
1626 key0.fib_index = rx_fib_index0;
1628 kv0.key = key0.as_u64;
1630 if (PREDICT_FALSE (clib_bihash_search_8_8 (
1631 &sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1635 if (is_output_feature)
1637 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
1638 ip0, proto0, udp0->src_port, udp0->dst_port, thread_index, sw_if_index0)))
1643 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1644 ip0, proto0, rx_fib_index0, thread_index)))
1648 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1649 &s0, node, next0, thread_index);
1650 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1655 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1661 if (PREDICT_FALSE (value0.value == ~0ULL))
1665 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
1666 thread_index, now, vm, node);
1667 if (!s0 && !sm->forwarding_enabled)
1668 next0 = SNAT_IN2OUT_NEXT_DROP;
1673 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1679 s0 = pool_elt_at_index (
1680 sm->per_thread_data[thread_index].sessions,
1685 b0->flags |= VNET_BUFFER_F_IS_NATED;
1687 old_addr0 = ip0->src_address.as_u32;
1688 ip0->src_address = s0->out2in.addr;
1689 new_addr0 = ip0->src_address.as_u32;
1690 if (!is_output_feature)
1691 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1693 sum0 = ip0->checksum;
1694 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1696 src_address /* changed member */);
1697 ip0->checksum = ip_csum_fold (sum0);
1699 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1701 old_port0 = tcp0->src_port;
1702 tcp0->src_port = s0->out2in.port;
1703 new_port0 = tcp0->src_port;
1705 sum0 = tcp0->checksum;
1706 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1708 dst_address /* changed member */);
1709 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1710 ip4_header_t /* cheat */,
1711 length /* changed member */);
1712 tcp0->checksum = ip_csum_fold(sum0);
1716 old_port0 = udp0->src_port;
1717 udp0->src_port = s0->out2in.port;
1722 s0->last_heard = now;
1724 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1725 /* Per-user LRU list maintenance */
1726 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1727 s0->per_user_index);
1728 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1729 s0->per_user_list_head_index,
1730 s0->per_user_index);
1733 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1734 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1736 snat_in2out_trace_t *t =
1737 vlib_add_trace (vm, node, b0, sizeof (*t));
1738 t->is_slow_path = is_slow_path;
1739 t->sw_if_index = sw_if_index0;
1740 t->next_index = next0;
1741 t->session_index = ~0;
1743 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1746 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1748 if (is_output_feature)
1749 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1751 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1754 udp1 = ip4_next_header (ip1);
1755 tcp1 = (tcp_header_t *) udp1;
1756 icmp1 = (icmp46_header_t *) udp1;
1758 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1759 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1762 if (PREDICT_FALSE(ip1->ttl == 1))
1764 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1765 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1766 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1768 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1772 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1774 /* Next configured feature, probably ip4-lookup */
1777 if (PREDICT_FALSE (proto1 == ~0))
1779 s1 = snat_in2out_unknown_proto (sm, b1, ip1, rx_fib_index1,
1780 thread_index, now, vm, node);
1782 next1 = SNAT_IN2OUT_NEXT_DROP;
1786 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1788 next1 = icmp_in2out_slow_path
1789 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1790 next1, now, thread_index, &s1);
1796 if (is_output_feature)
1798 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(sm, ip1, thread_index)))
1802 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1804 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1808 if (ip4_is_fragment (ip1))
1810 next1 = SNAT_IN2OUT_NEXT_REASS;
1815 key1.addr = ip1->src_address;
1816 key1.port = udp1->src_port;
1817 key1.protocol = proto1;
1818 key1.fib_index = rx_fib_index1;
1820 kv1.key = key1.as_u64;
1822 if (PREDICT_FALSE(clib_bihash_search_8_8 (
1823 &sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1827 if (is_output_feature)
1829 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
1830 ip1, proto1, udp1->src_port, udp1->dst_port, thread_index, sw_if_index1)))
1835 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
1836 ip1, proto1, rx_fib_index1, thread_index)))
1840 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1841 &s1, node, next1, thread_index);
1842 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1847 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1853 if (PREDICT_FALSE (value1.value == ~0ULL))
1857 s1 = snat_in2out_lb(sm, b1, ip1, rx_fib_index1,
1858 thread_index, now, vm, node);
1859 if (!s1 && !sm->forwarding_enabled)
1860 next1 = SNAT_IN2OUT_NEXT_DROP;
1865 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1871 s1 = pool_elt_at_index (
1872 sm->per_thread_data[thread_index].sessions,
1877 b1->flags |= VNET_BUFFER_F_IS_NATED;
1879 old_addr1 = ip1->src_address.as_u32;
1880 ip1->src_address = s1->out2in.addr;
1881 new_addr1 = ip1->src_address.as_u32;
1882 if (!is_output_feature)
1883 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1885 sum1 = ip1->checksum;
1886 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1888 src_address /* changed member */);
1889 ip1->checksum = ip_csum_fold (sum1);
1891 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1893 old_port1 = tcp1->src_port;
1894 tcp1->src_port = s1->out2in.port;
1895 new_port1 = tcp1->src_port;
1897 sum1 = tcp1->checksum;
1898 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1900 dst_address /* changed member */);
1901 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1902 ip4_header_t /* cheat */,
1903 length /* changed member */);
1904 tcp1->checksum = ip_csum_fold(sum1);
1908 old_port1 = udp1->src_port;
1909 udp1->src_port = s1->out2in.port;
1914 s1->last_heard = now;
1916 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1917 /* Per-user LRU list maintenance */
1918 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1919 s1->per_user_index);
1920 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1921 s1->per_user_list_head_index,
1922 s1->per_user_index);
1925 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1926 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1928 snat_in2out_trace_t *t =
1929 vlib_add_trace (vm, node, b1, sizeof (*t));
1930 t->sw_if_index = sw_if_index1;
1931 t->next_index = next1;
1932 t->session_index = ~0;
1934 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1937 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1939 /* verify speculative enqueues, maybe switch current next frame */
1940 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1941 to_next, n_left_to_next,
1942 bi0, bi1, next0, next1);
1945 while (n_left_from > 0 && n_left_to_next > 0)
1953 u32 new_addr0, old_addr0;
1954 u16 old_port0, new_port0;
1955 udp_header_t * udp0;
1956 tcp_header_t * tcp0;
1957 icmp46_header_t * icmp0;
1958 snat_session_key_t key0;
1961 snat_session_t * s0 = 0;
1962 clib_bihash_kv_8_8_t kv0, value0;
1963 u32 iph_offset0 = 0;
1965 /* speculatively enqueue b0 to the current next frame */
1971 n_left_to_next -= 1;
1973 b0 = vlib_get_buffer (vm, bi0);
1974 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1976 if (is_output_feature)
1977 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1979 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1982 udp0 = ip4_next_header (ip0);
1983 tcp0 = (tcp_header_t *) udp0;
1984 icmp0 = (icmp46_header_t *) udp0;
1986 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1987 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1990 if (PREDICT_FALSE(ip0->ttl == 1))
1992 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1993 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1994 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1996 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2000 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2002 /* Next configured feature, probably ip4-lookup */
2005 if (PREDICT_FALSE (proto0 == ~0))
2007 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
2008 thread_index, now, vm, node);
2010 next0 = SNAT_IN2OUT_NEXT_DROP;
2014 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2016 next0 = icmp_in2out_slow_path
2017 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
2018 next0, now, thread_index, &s0);
2024 if (is_output_feature)
2026 if (PREDICT_FALSE(nat_not_translate_output_feature_fwd(sm, ip0, thread_index)))
2030 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
2032 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2036 if (ip4_is_fragment (ip0))
2038 next0 = SNAT_IN2OUT_NEXT_REASS;
2043 key0.addr = ip0->src_address;
2044 key0.port = udp0->src_port;
2045 key0.protocol = proto0;
2046 key0.fib_index = rx_fib_index0;
2048 kv0.key = key0.as_u64;
2050 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out,
2055 if (is_output_feature)
2057 if (PREDICT_FALSE(nat_not_translate_output_feature(sm,
2058 ip0, proto0, udp0->src_port, udp0->dst_port, thread_index, sw_if_index0)))
2063 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
2064 ip0, proto0, rx_fib_index0, thread_index)))
2068 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
2069 &s0, node, next0, thread_index);
2071 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
2076 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2082 if (PREDICT_FALSE (value0.value == ~0ULL))
2086 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
2087 thread_index, now, vm, node);
2088 if (!s0 && !sm->forwarding_enabled)
2089 next0 = SNAT_IN2OUT_NEXT_DROP;
2094 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2100 s0 = pool_elt_at_index (
2101 sm->per_thread_data[thread_index].sessions,
2106 b0->flags |= VNET_BUFFER_F_IS_NATED;
2108 old_addr0 = ip0->src_address.as_u32;
2109 ip0->src_address = s0->out2in.addr;
2110 new_addr0 = ip0->src_address.as_u32;
2111 if (!is_output_feature)
2112 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
2114 sum0 = ip0->checksum;
2115 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2117 src_address /* changed member */);
2118 ip0->checksum = ip_csum_fold (sum0);
2120 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2122 old_port0 = tcp0->src_port;
2123 tcp0->src_port = s0->out2in.port;
2124 new_port0 = tcp0->src_port;
2126 sum0 = tcp0->checksum;
2127 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2129 dst_address /* changed member */);
2130 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2131 ip4_header_t /* cheat */,
2132 length /* changed member */);
2133 tcp0->checksum = ip_csum_fold(sum0);
2137 old_port0 = udp0->src_port;
2138 udp0->src_port = s0->out2in.port;
2143 s0->last_heard = now;
2145 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
2146 /* Per-user LRU list maintenance */
2147 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
2148 s0->per_user_index);
2149 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
2150 s0->per_user_list_head_index,
2151 s0->per_user_index);
2154 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2155 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2157 snat_in2out_trace_t *t =
2158 vlib_add_trace (vm, node, b0, sizeof (*t));
2159 t->is_slow_path = is_slow_path;
2160 t->sw_if_index = sw_if_index0;
2161 t->next_index = next0;
2162 t->session_index = ~0;
2164 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
2167 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2169 /* verify speculative enqueue, maybe switch current next frame */
2170 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2171 to_next, n_left_to_next,
2175 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2178 vlib_node_increment_counter (vm, stats_node_index,
2179 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2181 return frame->n_vectors;
2185 snat_in2out_fast_path_fn (vlib_main_t * vm,
2186 vlib_node_runtime_t * node,
2187 vlib_frame_t * frame)
2189 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 0);
2192 VLIB_REGISTER_NODE (snat_in2out_node) = {
2193 .function = snat_in2out_fast_path_fn,
2194 .name = "nat44-in2out",
2195 .vector_size = sizeof (u32),
2196 .format_trace = format_snat_in2out_trace,
2197 .type = VLIB_NODE_TYPE_INTERNAL,
2199 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2200 .error_strings = snat_in2out_error_strings,
2202 .runtime_data_bytes = sizeof (snat_runtime_t),
2204 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2206 /* edit / add dispositions here */
2208 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2209 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2210 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2211 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2212 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2216 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
2219 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
2220 vlib_node_runtime_t * node,
2221 vlib_frame_t * frame)
2223 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 1);
2226 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
2227 .function = snat_in2out_output_fast_path_fn,
2228 .name = "nat44-in2out-output",
2229 .vector_size = sizeof (u32),
2230 .format_trace = format_snat_in2out_trace,
2231 .type = VLIB_NODE_TYPE_INTERNAL,
2233 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2234 .error_strings = snat_in2out_error_strings,
2236 .runtime_data_bytes = sizeof (snat_runtime_t),
2238 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2240 /* edit / add dispositions here */
2242 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2243 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2244 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2245 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2246 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2250 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
2251 snat_in2out_output_fast_path_fn);
2254 snat_in2out_slow_path_fn (vlib_main_t * vm,
2255 vlib_node_runtime_t * node,
2256 vlib_frame_t * frame)
2258 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 0);
2261 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
2262 .function = snat_in2out_slow_path_fn,
2263 .name = "nat44-in2out-slowpath",
2264 .vector_size = sizeof (u32),
2265 .format_trace = format_snat_in2out_trace,
2266 .type = VLIB_NODE_TYPE_INTERNAL,
2268 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2269 .error_strings = snat_in2out_error_strings,
2271 .runtime_data_bytes = sizeof (snat_runtime_t),
2273 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2275 /* edit / add dispositions here */
2277 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2278 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2279 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2280 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2281 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2285 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
2286 snat_in2out_slow_path_fn);
2289 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
2290 vlib_node_runtime_t * node,
2291 vlib_frame_t * frame)
2293 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 1);
2296 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
2297 .function = snat_in2out_output_slow_path_fn,
2298 .name = "nat44-in2out-output-slowpath",
2299 .vector_size = sizeof (u32),
2300 .format_trace = format_snat_in2out_trace,
2301 .type = VLIB_NODE_TYPE_INTERNAL,
2303 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2304 .error_strings = snat_in2out_error_strings,
2306 .runtime_data_bytes = sizeof (snat_runtime_t),
2308 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2310 /* edit / add dispositions here */
2312 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2313 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2314 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2315 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2316 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2320 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
2321 snat_in2out_output_slow_path_fn);
2323 extern vnet_feature_arc_registration_t vnet_feat_arc_ip4_local;
2326 nat44_hairpinning_fn (vlib_main_t * vm,
2327 vlib_node_runtime_t * node,
2328 vlib_frame_t * frame)
2330 u32 n_left_from, * from, * to_next;
2331 snat_in2out_next_t next_index;
2332 u32 pkts_processed = 0;
2333 snat_main_t * sm = &snat_main;
2334 vnet_feature_main_t *fm = &feature_main;
2335 u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
2336 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
2338 from = vlib_frame_vector_args (frame);
2339 n_left_from = frame->n_vectors;
2340 next_index = node->cached_next_index;
2342 while (n_left_from > 0)
2346 vlib_get_next_frame (vm, node, next_index,
2347 to_next, n_left_to_next);
2349 while (n_left_from > 0 && n_left_to_next > 0)
2356 udp_header_t * udp0;
2357 tcp_header_t * tcp0;
2359 /* speculatively enqueue b0 to the current next frame */
2365 n_left_to_next -= 1;
2367 b0 = vlib_get_buffer (vm, bi0);
2368 ip0 = vlib_buffer_get_current (b0);
2369 udp0 = ip4_next_header (ip0);
2370 tcp0 = (tcp_header_t *) udp0;
2372 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2374 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
2377 if (snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0))
2378 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2380 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2382 /* verify speculative enqueue, maybe switch current next frame */
2383 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2384 to_next, n_left_to_next,
2388 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2391 vlib_node_increment_counter (vm, nat44_hairpinning_node.index,
2392 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2394 return frame->n_vectors;
2397 VLIB_REGISTER_NODE (nat44_hairpinning_node) = {
2398 .function = nat44_hairpinning_fn,
2399 .name = "nat44-hairpinning",
2400 .vector_size = sizeof (u32),
2401 .type = VLIB_NODE_TYPE_INTERNAL,
2402 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2403 .error_strings = snat_in2out_error_strings,
2406 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2407 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2411 VLIB_NODE_FUNCTION_MULTIARCH (nat44_hairpinning_node,
2412 nat44_hairpinning_fn);
2415 nat44_reass_hairpinning (snat_main_t *sm,
2422 snat_session_key_t key0, sm0;
2423 snat_session_t * s0;
2424 clib_bihash_kv_8_8_t kv0, value0;
2426 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
2427 u16 new_dst_port0, old_dst_port0;
2428 udp_header_t * udp0;
2429 tcp_header_t * tcp0;
2431 key0.addr = ip0->dst_address;
2433 key0.protocol = proto0;
2434 key0.fib_index = sm->outside_fib_index;
2435 kv0.key = key0.as_u64;
2437 udp0 = ip4_next_header (ip0);
2439 /* Check if destination is static mappings */
2440 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
2442 new_dst_addr0 = sm0.addr.as_u32;
2443 new_dst_port0 = sm0.port;
2444 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2446 /* or active sessions */
2449 if (sm->num_workers > 1)
2450 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
2452 ti = sm->num_workers;
2454 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
2457 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
2458 new_dst_addr0 = s0->in2out.addr.as_u32;
2459 new_dst_port0 = s0->in2out.port;
2460 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
2464 /* Destination is behind the same NAT, use internal address and port */
2467 old_dst_addr0 = ip0->dst_address.as_u32;
2468 ip0->dst_address.as_u32 = new_dst_addr0;
2469 sum0 = ip0->checksum;
2470 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2471 ip4_header_t, dst_address);
2472 ip0->checksum = ip_csum_fold (sum0);
2474 old_dst_port0 = dport;
2475 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0 &&
2476 ip4_is_first_fragment (ip0)))
2478 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2480 tcp0 = ip4_next_header (ip0);
2481 tcp0->dst = new_dst_port0;
2482 sum0 = tcp0->checksum;
2483 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2484 ip4_header_t, dst_address);
2485 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
2486 ip4_header_t /* cheat */, length);
2487 tcp0->checksum = ip_csum_fold(sum0);
2491 udp0->dst_port = new_dst_port0;
2497 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2499 tcp0 = ip4_next_header (ip0);
2500 sum0 = tcp0->checksum;
2501 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2502 ip4_header_t, dst_address);
2503 tcp0->checksum = ip_csum_fold(sum0);
2510 nat44_in2out_reass_node_fn (vlib_main_t * vm,
2511 vlib_node_runtime_t * node,
2512 vlib_frame_t * frame)
2514 u32 n_left_from, *from, *to_next;
2515 snat_in2out_next_t next_index;
2516 u32 pkts_processed = 0;
2517 snat_main_t *sm = &snat_main;
2518 f64 now = vlib_time_now (vm);
2519 u32 thread_index = vlib_get_thread_index ();
2520 snat_main_per_thread_data_t *per_thread_data =
2521 &sm->per_thread_data[thread_index];
2522 u32 *fragments_to_drop = 0;
2523 u32 *fragments_to_loopback = 0;
2525 from = vlib_frame_vector_args (frame);
2526 n_left_from = frame->n_vectors;
2527 next_index = node->cached_next_index;
2529 while (n_left_from > 0)
2533 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2535 while (n_left_from > 0 && n_left_to_next > 0)
2537 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
2542 nat_reass_ip4_t *reass0;
2543 udp_header_t * udp0;
2544 tcp_header_t * tcp0;
2545 snat_session_key_t key0;
2546 clib_bihash_kv_8_8_t kv0, value0;
2547 snat_session_t * s0 = 0;
2548 u16 old_port0, new_port0;
2551 /* speculatively enqueue b0 to the current next frame */
2557 n_left_to_next -= 1;
2559 b0 = vlib_get_buffer (vm, bi0);
2560 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2562 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2563 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2566 if (PREDICT_FALSE (nat_reass_is_drop_frag(0)))
2568 next0 = SNAT_IN2OUT_NEXT_DROP;
2569 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
2573 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
2574 udp0 = ip4_next_header (ip0);
2575 tcp0 = (tcp_header_t *) udp0;
2576 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2578 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
2583 &fragments_to_drop);
2585 if (PREDICT_FALSE (!reass0))
2587 next0 = SNAT_IN2OUT_NEXT_DROP;
2588 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
2592 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2594 key0.addr = ip0->src_address;
2595 key0.port = udp0->src_port;
2596 key0.protocol = proto0;
2597 key0.fib_index = rx_fib_index0;
2598 kv0.key = key0.as_u64;
2600 if (clib_bihash_search_8_8 (&per_thread_data->in2out, &kv0, &value0))
2602 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
2603 ip0, proto0, rx_fib_index0, thread_index)))
2606 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
2607 &s0, node, next0, thread_index);
2609 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
2612 reass0->sess_index = s0 - per_thread_data->sessions;
2616 s0 = pool_elt_at_index (per_thread_data->sessions,
2618 reass0->sess_index = value0.value;
2620 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
2624 if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
2626 if (nat_ip4_reass_add_fragment (reass0, bi0))
2628 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
2629 next0 = SNAT_IN2OUT_NEXT_DROP;
2635 s0 = pool_elt_at_index (per_thread_data->sessions,
2636 reass0->sess_index);
2639 old_addr0 = ip0->src_address.as_u32;
2640 ip0->src_address = s0->out2in.addr;
2641 new_addr0 = ip0->src_address.as_u32;
2642 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
2644 sum0 = ip0->checksum;
2645 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2647 src_address /* changed member */);
2648 ip0->checksum = ip_csum_fold (sum0);
2650 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2652 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2654 old_port0 = tcp0->src_port;
2655 tcp0->src_port = s0->out2in.port;
2656 new_port0 = tcp0->src_port;
2658 sum0 = tcp0->checksum;
2659 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2661 dst_address /* changed member */);
2662 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2663 ip4_header_t /* cheat */,
2664 length /* changed member */);
2665 tcp0->checksum = ip_csum_fold(sum0);
2669 old_port0 = udp0->src_port;
2670 udp0->src_port = s0->out2in.port;
2676 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2677 s0->ext_host_port, proto0);
2680 s0->last_heard = now;
2682 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
2683 /* Per-user LRU list maintenance */
2684 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
2685 s0->per_user_index);
2686 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
2687 s0->per_user_list_head_index,
2688 s0->per_user_index);
2691 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2692 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2694 nat44_in2out_reass_trace_t *t =
2695 vlib_add_trace (vm, node, b0, sizeof (*t));
2696 t->cached = cached0;
2697 t->sw_if_index = sw_if_index0;
2698 t->next_index = next0;
2708 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2710 /* verify speculative enqueue, maybe switch current next frame */
2711 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2712 to_next, n_left_to_next,
2716 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2718 from = vlib_frame_vector_args (frame);
2719 u32 len = vec_len (fragments_to_loopback);
2720 if (len <= VLIB_FRAME_SIZE)
2722 clib_memcpy (from, fragments_to_loopback, sizeof (u32) * len);
2724 vec_reset_length (fragments_to_loopback);
2729 fragments_to_loopback + (len - VLIB_FRAME_SIZE),
2730 sizeof (u32) * VLIB_FRAME_SIZE);
2731 n_left_from = VLIB_FRAME_SIZE;
2732 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2737 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2740 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
2741 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2744 nat_send_all_to_node (vm, fragments_to_drop, node,
2745 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
2746 SNAT_IN2OUT_NEXT_DROP);
2748 vec_free (fragments_to_drop);
2749 vec_free (fragments_to_loopback);
2750 return frame->n_vectors;
2753 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
2754 .function = nat44_in2out_reass_node_fn,
2755 .name = "nat44-in2out-reass",
2756 .vector_size = sizeof (u32),
2757 .format_trace = format_nat44_in2out_reass_trace,
2758 .type = VLIB_NODE_TYPE_INTERNAL,
2760 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2761 .error_strings = snat_in2out_error_strings,
2763 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2765 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2766 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2767 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2768 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2769 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2773 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2774 nat44_in2out_reass_node_fn);
2776 /**************************/
2777 /*** deterministic mode ***/
2778 /**************************/
2780 snat_det_in2out_node_fn (vlib_main_t * vm,
2781 vlib_node_runtime_t * node,
2782 vlib_frame_t * frame)
2784 u32 n_left_from, * from, * to_next;
2785 snat_in2out_next_t next_index;
2786 u32 pkts_processed = 0;
2787 snat_main_t * sm = &snat_main;
2788 u32 now = (u32) vlib_time_now (vm);
2789 u32 thread_index = vlib_get_thread_index ();
2791 from = vlib_frame_vector_args (frame);
2792 n_left_from = frame->n_vectors;
2793 next_index = node->cached_next_index;
2795 while (n_left_from > 0)
2799 vlib_get_next_frame (vm, node, next_index,
2800 to_next, n_left_to_next);
2802 while (n_left_from >= 4 && n_left_to_next >= 2)
2805 vlib_buffer_t * b0, * b1;
2807 u32 sw_if_index0, sw_if_index1;
2808 ip4_header_t * ip0, * ip1;
2809 ip_csum_t sum0, sum1;
2810 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
2811 u16 old_port0, new_port0, lo_port0, i0;
2812 u16 old_port1, new_port1, lo_port1, i1;
2813 udp_header_t * udp0, * udp1;
2814 tcp_header_t * tcp0, * tcp1;
2816 snat_det_out_key_t key0, key1;
2817 snat_det_map_t * dm0, * dm1;
2818 snat_det_session_t * ses0 = 0, * ses1 = 0;
2819 u32 rx_fib_index0, rx_fib_index1;
2820 icmp46_header_t * icmp0, * icmp1;
2822 /* Prefetch next iteration. */
2824 vlib_buffer_t * p2, * p3;
2826 p2 = vlib_get_buffer (vm, from[2]);
2827 p3 = vlib_get_buffer (vm, from[3]);
2829 vlib_prefetch_buffer_header (p2, LOAD);
2830 vlib_prefetch_buffer_header (p3, LOAD);
2832 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
2833 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
2836 /* speculatively enqueue b0 and b1 to the current next frame */
2837 to_next[0] = bi0 = from[0];
2838 to_next[1] = bi1 = from[1];
2842 n_left_to_next -= 2;
2844 b0 = vlib_get_buffer (vm, bi0);
2845 b1 = vlib_get_buffer (vm, bi1);
2847 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2848 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
2850 ip0 = vlib_buffer_get_current (b0);
2851 udp0 = ip4_next_header (ip0);
2852 tcp0 = (tcp_header_t *) udp0;
2854 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2856 if (PREDICT_FALSE(ip0->ttl == 1))
2858 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2859 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2860 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2862 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2866 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2868 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2870 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2871 icmp0 = (icmp46_header_t *) udp0;
2873 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2874 rx_fib_index0, node, next0, thread_index,
2879 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2880 if (PREDICT_FALSE(!dm0))
2882 clib_warning("no match for internal host %U",
2883 format_ip4_address, &ip0->src_address);
2884 next0 = SNAT_IN2OUT_NEXT_DROP;
2885 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2889 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2891 key0.ext_host_addr = ip0->dst_address;
2892 key0.ext_host_port = tcp0->dst;
2894 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
2895 if (PREDICT_FALSE(!ses0))
2897 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2899 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2900 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
2902 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
2905 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
2908 if (PREDICT_FALSE(!ses0))
2910 /* too many sessions for user, send ICMP error packet */
2912 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2913 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
2914 ICMP4_destination_unreachable_destination_unreachable_host,
2916 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2921 new_port0 = ses0->out.out_port;
2923 old_addr0.as_u32 = ip0->src_address.as_u32;
2924 ip0->src_address.as_u32 = new_addr0.as_u32;
2925 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2927 sum0 = ip0->checksum;
2928 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2930 src_address /* changed member */);
2931 ip0->checksum = ip_csum_fold (sum0);
2933 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2935 if (tcp0->flags & TCP_FLAG_SYN)
2936 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
2937 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
2938 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2939 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2940 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
2941 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
2942 snat_det_ses_close(dm0, ses0);
2943 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2944 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
2945 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
2946 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2948 old_port0 = tcp0->src;
2949 tcp0->src = new_port0;
2951 sum0 = tcp0->checksum;
2952 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2954 dst_address /* changed member */);
2955 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2956 ip4_header_t /* cheat */,
2957 length /* changed member */);
2958 tcp0->checksum = ip_csum_fold(sum0);
2962 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2963 old_port0 = udp0->src_port;
2964 udp0->src_port = new_port0;
2970 case SNAT_SESSION_UDP_ACTIVE:
2971 ses0->expire = now + sm->udp_timeout;
2973 case SNAT_SESSION_TCP_SYN_SENT:
2974 case SNAT_SESSION_TCP_FIN_WAIT:
2975 case SNAT_SESSION_TCP_CLOSE_WAIT:
2976 case SNAT_SESSION_TCP_LAST_ACK:
2977 ses0->expire = now + sm->tcp_transitory_timeout;
2979 case SNAT_SESSION_TCP_ESTABLISHED:
2980 ses0->expire = now + sm->tcp_established_timeout;
2985 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2986 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2988 snat_in2out_trace_t *t =
2989 vlib_add_trace (vm, node, b0, sizeof (*t));
2990 t->is_slow_path = 0;
2991 t->sw_if_index = sw_if_index0;
2992 t->next_index = next0;
2993 t->session_index = ~0;
2995 t->session_index = ses0 - dm0->sessions;
2998 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3000 ip1 = vlib_buffer_get_current (b1);
3001 udp1 = ip4_next_header (ip1);
3002 tcp1 = (tcp_header_t *) udp1;
3004 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
3006 if (PREDICT_FALSE(ip1->ttl == 1))
3008 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3009 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
3010 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3012 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3016 proto1 = ip_proto_to_snat_proto (ip1->protocol);
3018 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
3020 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
3021 icmp1 = (icmp46_header_t *) udp1;
3023 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
3024 rx_fib_index1, node, next1, thread_index,
3029 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
3030 if (PREDICT_FALSE(!dm1))
3032 clib_warning("no match for internal host %U",
3033 format_ip4_address, &ip0->src_address);
3034 next1 = SNAT_IN2OUT_NEXT_DROP;
3035 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3039 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
3041 key1.ext_host_addr = ip1->dst_address;
3042 key1.ext_host_port = tcp1->dst;
3044 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src, key1);
3045 if (PREDICT_FALSE(!ses1))
3047 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
3049 key1.out_port = clib_host_to_net_u16 (lo_port1 +
3050 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
3052 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
3055 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
3058 if (PREDICT_FALSE(!ses1))
3060 /* too many sessions for user, send ICMP error packet */
3062 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3063 icmp4_error_set_vnet_buffer (b1, ICMP4_destination_unreachable,
3064 ICMP4_destination_unreachable_destination_unreachable_host,
3066 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3071 new_port1 = ses1->out.out_port;
3073 old_addr1.as_u32 = ip1->src_address.as_u32;
3074 ip1->src_address.as_u32 = new_addr1.as_u32;
3075 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
3077 sum1 = ip1->checksum;
3078 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
3080 src_address /* changed member */);
3081 ip1->checksum = ip_csum_fold (sum1);
3083 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
3085 if (tcp1->flags & TCP_FLAG_SYN)
3086 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
3087 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
3088 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
3089 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
3090 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
3091 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
3092 snat_det_ses_close(dm1, ses1);
3093 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
3094 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
3095 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
3096 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
3098 old_port1 = tcp1->src;
3099 tcp1->src = new_port1;
3101 sum1 = tcp1->checksum;
3102 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
3104 dst_address /* changed member */);
3105 sum1 = ip_csum_update (sum1, old_port1, new_port1,
3106 ip4_header_t /* cheat */,
3107 length /* changed member */);
3108 tcp1->checksum = ip_csum_fold(sum1);
3112 ses1->state = SNAT_SESSION_UDP_ACTIVE;
3113 old_port1 = udp1->src_port;
3114 udp1->src_port = new_port1;
3120 case SNAT_SESSION_UDP_ACTIVE:
3121 ses1->expire = now + sm->udp_timeout;
3123 case SNAT_SESSION_TCP_SYN_SENT:
3124 case SNAT_SESSION_TCP_FIN_WAIT:
3125 case SNAT_SESSION_TCP_CLOSE_WAIT:
3126 case SNAT_SESSION_TCP_LAST_ACK:
3127 ses1->expire = now + sm->tcp_transitory_timeout;
3129 case SNAT_SESSION_TCP_ESTABLISHED:
3130 ses1->expire = now + sm->tcp_established_timeout;
3135 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3136 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
3138 snat_in2out_trace_t *t =
3139 vlib_add_trace (vm, node, b1, sizeof (*t));
3140 t->is_slow_path = 0;
3141 t->sw_if_index = sw_if_index1;
3142 t->next_index = next1;
3143 t->session_index = ~0;
3145 t->session_index = ses1 - dm1->sessions;
3148 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
3150 /* verify speculative enqueues, maybe switch current next frame */
3151 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
3152 to_next, n_left_to_next,
3153 bi0, bi1, next0, next1);
3156 while (n_left_from > 0 && n_left_to_next > 0)
3164 ip4_address_t new_addr0, old_addr0;
3165 u16 old_port0, new_port0, lo_port0, i0;
3166 udp_header_t * udp0;
3167 tcp_header_t * tcp0;
3169 snat_det_out_key_t key0;
3170 snat_det_map_t * dm0;
3171 snat_det_session_t * ses0 = 0;
3173 icmp46_header_t * icmp0;
3175 /* speculatively enqueue b0 to the current next frame */
3181 n_left_to_next -= 1;
3183 b0 = vlib_get_buffer (vm, bi0);
3184 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3186 ip0 = vlib_buffer_get_current (b0);
3187 udp0 = ip4_next_header (ip0);
3188 tcp0 = (tcp_header_t *) udp0;
3190 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3192 if (PREDICT_FALSE(ip0->ttl == 1))
3194 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3195 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
3196 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3198 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3202 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3204 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
3206 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3207 icmp0 = (icmp46_header_t *) udp0;
3209 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
3210 rx_fib_index0, node, next0, thread_index,
3215 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
3216 if (PREDICT_FALSE(!dm0))
3218 clib_warning("no match for internal host %U",
3219 format_ip4_address, &ip0->src_address);
3220 next0 = SNAT_IN2OUT_NEXT_DROP;
3221 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3225 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
3227 key0.ext_host_addr = ip0->dst_address;
3228 key0.ext_host_port = tcp0->dst;
3230 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
3231 if (PREDICT_FALSE(!ses0))
3233 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
3235 key0.out_port = clib_host_to_net_u16 (lo_port0 +
3236 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
3238 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
3241 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
3244 if (PREDICT_FALSE(!ses0))
3246 /* too many sessions for user, send ICMP error packet */
3248 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3249 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
3250 ICMP4_destination_unreachable_destination_unreachable_host,
3252 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3257 new_port0 = ses0->out.out_port;
3259 old_addr0.as_u32 = ip0->src_address.as_u32;
3260 ip0->src_address.as_u32 = new_addr0.as_u32;
3261 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
3263 sum0 = ip0->checksum;
3264 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
3266 src_address /* changed member */);
3267 ip0->checksum = ip_csum_fold (sum0);
3269 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3271 if (tcp0->flags & TCP_FLAG_SYN)
3272 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
3273 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
3274 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
3275 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
3276 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
3277 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
3278 snat_det_ses_close(dm0, ses0);
3279 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
3280 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
3281 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
3282 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
3284 old_port0 = tcp0->src;
3285 tcp0->src = new_port0;
3287 sum0 = tcp0->checksum;
3288 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
3290 dst_address /* changed member */);
3291 sum0 = ip_csum_update (sum0, old_port0, new_port0,
3292 ip4_header_t /* cheat */,
3293 length /* changed member */);
3294 tcp0->checksum = ip_csum_fold(sum0);
3298 ses0->state = SNAT_SESSION_UDP_ACTIVE;
3299 old_port0 = udp0->src_port;
3300 udp0->src_port = new_port0;
3306 case SNAT_SESSION_UDP_ACTIVE:
3307 ses0->expire = now + sm->udp_timeout;
3309 case SNAT_SESSION_TCP_SYN_SENT:
3310 case SNAT_SESSION_TCP_FIN_WAIT:
3311 case SNAT_SESSION_TCP_CLOSE_WAIT:
3312 case SNAT_SESSION_TCP_LAST_ACK:
3313 ses0->expire = now + sm->tcp_transitory_timeout;
3315 case SNAT_SESSION_TCP_ESTABLISHED:
3316 ses0->expire = now + sm->tcp_established_timeout;
3321 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3322 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3324 snat_in2out_trace_t *t =
3325 vlib_add_trace (vm, node, b0, sizeof (*t));
3326 t->is_slow_path = 0;
3327 t->sw_if_index = sw_if_index0;
3328 t->next_index = next0;
3329 t->session_index = ~0;
3331 t->session_index = ses0 - dm0->sessions;
3334 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3336 /* verify speculative enqueue, maybe switch current next frame */
3337 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3338 to_next, n_left_to_next,
3342 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3345 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
3346 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3348 return frame->n_vectors;
3351 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
3352 .function = snat_det_in2out_node_fn,
3353 .name = "nat44-det-in2out",
3354 .vector_size = sizeof (u32),
3355 .format_trace = format_snat_in2out_trace,
3356 .type = VLIB_NODE_TYPE_INTERNAL,
3358 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3359 .error_strings = snat_in2out_error_strings,
3361 .runtime_data_bytes = sizeof (snat_runtime_t),
3365 /* edit / add dispositions here */
3367 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3368 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3369 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3373 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
3376 * Get address and port values to be used for ICMP packet translation
3377 * and create session if needed
3379 * @param[in,out] sm NAT main
3380 * @param[in,out] node NAT node runtime
3381 * @param[in] thread_index thread index
3382 * @param[in,out] b0 buffer containing packet to be translated
3383 * @param[out] p_proto protocol used for matching
3384 * @param[out] p_value address and port after NAT translation
3385 * @param[out] p_dont_translate if packet should not be translated
3386 * @param d optional parameter
3387 * @param e optional parameter
3389 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
3390 u32 thread_index, vlib_buffer_t *b0,
3391 ip4_header_t *ip0, u8 *p_proto,
3392 snat_session_key_t *p_value,
3393 u8 *p_dont_translate, void *d, void *e)
3395 icmp46_header_t *icmp0;
3399 snat_det_out_key_t key0;
3400 u8 dont_translate = 0;
3402 icmp_echo_header_t *echo0, *inner_echo0 = 0;
3403 ip4_header_t *inner_ip0;
3404 void *l4_header = 0;
3405 icmp46_header_t *inner_icmp0;
3406 snat_det_map_t * dm0 = 0;
3407 ip4_address_t new_addr0;
3409 snat_det_session_t * ses0 = 0;
3410 ip4_address_t in_addr;
3413 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
3414 echo0 = (icmp_echo_header_t *)(icmp0+1);
3415 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3416 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
3418 if (!icmp_is_error_message (icmp0))
3420 protocol = SNAT_PROTOCOL_ICMP;
3421 in_addr = ip0->src_address;
3422 in_port = echo0->identifier;
3426 inner_ip0 = (ip4_header_t *)(echo0+1);
3427 l4_header = ip4_next_header (inner_ip0);
3428 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
3429 in_addr = inner_ip0->dst_address;
3432 case SNAT_PROTOCOL_ICMP:
3433 inner_icmp0 = (icmp46_header_t*)l4_header;
3434 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
3435 in_port = inner_echo0->identifier;
3437 case SNAT_PROTOCOL_UDP:
3438 case SNAT_PROTOCOL_TCP:
3439 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
3442 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
3443 next0 = SNAT_IN2OUT_NEXT_DROP;
3448 dm0 = snat_det_map_by_user(sm, &in_addr);
3449 if (PREDICT_FALSE(!dm0))
3451 clib_warning("no match for internal host %U",
3452 format_ip4_address, &in_addr);
3453 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3454 IP_PROTOCOL_ICMP, rx_fib_index0)))
3459 next0 = SNAT_IN2OUT_NEXT_DROP;
3460 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3464 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
3466 key0.ext_host_addr = ip0->dst_address;
3467 key0.ext_host_port = 0;
3469 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port, key0);
3470 if (PREDICT_FALSE(!ses0))
3472 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3473 IP_PROTOCOL_ICMP, rx_fib_index0)))
3478 if (icmp0->type != ICMP4_echo_request)
3480 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3481 next0 = SNAT_IN2OUT_NEXT_DROP;
3484 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
3486 key0.out_port = clib_host_to_net_u16 (lo_port0 +
3487 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
3489 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
3492 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
3495 if (PREDICT_FALSE(!ses0))
3497 next0 = SNAT_IN2OUT_NEXT_DROP;
3498 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
3503 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
3504 !icmp_is_error_message (icmp0)))
3506 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3507 next0 = SNAT_IN2OUT_NEXT_DROP;
3511 u32 now = (u32) vlib_time_now (sm->vlib_main);
3513 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
3514 ses0->expire = now + sm->icmp_timeout;
3517 *p_proto = protocol;
3520 p_value->addr = new_addr0;
3521 p_value->fib_index = sm->outside_fib_index;
3522 p_value->port = ses0->out.out_port;
3524 *p_dont_translate = dont_translate;
3526 *(snat_det_session_t**)d = ses0;
3528 *(snat_det_map_t**)e = dm0;
3532 /**********************/
3533 /*** worker handoff ***/
3534 /**********************/
3536 snat_in2out_worker_handoff_fn_inline (vlib_main_t * vm,
3537 vlib_node_runtime_t * node,
3538 vlib_frame_t * frame,
3541 snat_main_t *sm = &snat_main;
3542 vlib_thread_main_t *tm = vlib_get_thread_main ();
3543 u32 n_left_from, *from, *to_next = 0;
3544 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
3545 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
3547 vlib_frame_queue_elt_t *hf = 0;
3548 vlib_frame_t *f = 0;
3550 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
3551 u32 next_worker_index = 0;
3552 u32 current_worker_index = ~0;
3553 u32 thread_index = vlib_get_thread_index ();
3557 ASSERT (vec_len (sm->workers));
3561 fq_index = sm->fq_in2out_output_index;
3562 to_node_index = sm->in2out_output_node_index;
3566 fq_index = sm->fq_in2out_index;
3567 to_node_index = sm->in2out_node_index;
3570 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
3572 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
3574 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
3575 sm->first_worker_index + sm->num_workers - 1,
3576 (vlib_frame_queue_t *) (~0));
3579 from = vlib_frame_vector_args (frame);
3580 n_left_from = frame->n_vectors;
3582 while (n_left_from > 0)
3595 b0 = vlib_get_buffer (vm, bi0);
3597 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3598 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3600 ip0 = vlib_buffer_get_current (b0);
3602 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
3604 if (PREDICT_FALSE (next_worker_index != thread_index))
3608 if (next_worker_index != current_worker_index)
3611 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3613 hf = vlib_get_worker_handoff_queue_elt (fq_index,
3615 handoff_queue_elt_by_worker_index);
3617 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
3618 to_next_worker = &hf->buffer_index[hf->n_vectors];
3619 current_worker_index = next_worker_index;
3622 /* enqueue to correct worker thread */
3623 to_next_worker[0] = bi0;
3625 n_left_to_next_worker--;
3627 if (n_left_to_next_worker == 0)
3629 hf->n_vectors = VLIB_FRAME_SIZE;
3630 vlib_put_frame_queue_elt (hf);
3631 current_worker_index = ~0;
3632 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
3639 /* if this is 1st frame */
3642 f = vlib_get_frame_to_node (vm, to_node_index);
3643 to_next = vlib_frame_vector_args (f);
3651 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
3652 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3654 snat_in2out_worker_handoff_trace_t *t =
3655 vlib_add_trace (vm, node, b0, sizeof (*t));
3656 t->next_worker_index = next_worker_index;
3657 t->do_handoff = do_handoff;
3662 vlib_put_frame_to_node (vm, to_node_index, f);
3665 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3667 /* Ship frames to the worker nodes */
3668 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
3670 if (handoff_queue_elt_by_worker_index[i])
3672 hf = handoff_queue_elt_by_worker_index[i];
3674 * It works better to let the handoff node
3675 * rate-adapt, always ship the handoff queue element.
3677 if (1 || hf->n_vectors == hf->last_n_vectors)
3679 vlib_put_frame_queue_elt (hf);
3680 handoff_queue_elt_by_worker_index[i] = 0;
3683 hf->last_n_vectors = hf->n_vectors;
3685 congested_handoff_queue_by_worker_index[i] =
3686 (vlib_frame_queue_t *) (~0);
3689 current_worker_index = ~0;
3690 return frame->n_vectors;
3694 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
3695 vlib_node_runtime_t * node,
3696 vlib_frame_t * frame)
3698 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 0);
3701 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
3702 .function = snat_in2out_worker_handoff_fn,
3703 .name = "nat44-in2out-worker-handoff",
3704 .vector_size = sizeof (u32),
3705 .format_trace = format_snat_in2out_worker_handoff_trace,
3706 .type = VLIB_NODE_TYPE_INTERNAL,
3715 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
3716 snat_in2out_worker_handoff_fn);
3719 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
3720 vlib_node_runtime_t * node,
3721 vlib_frame_t * frame)
3723 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 1);
3726 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
3727 .function = snat_in2out_output_worker_handoff_fn,
3728 .name = "nat44-in2out-output-worker-handoff",
3729 .vector_size = sizeof (u32),
3730 .format_trace = format_snat_in2out_worker_handoff_trace,
3731 .type = VLIB_NODE_TYPE_INTERNAL,
3740 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
3741 snat_in2out_output_worker_handoff_fn);
3743 static_always_inline int
3744 is_hairpinning (snat_main_t *sm, ip4_address_t * dst_addr)
3746 snat_address_t * ap;
3747 clib_bihash_kv_8_8_t kv, value;
3748 snat_session_key_t m_key;
3750 vec_foreach (ap, sm->addresses)
3752 if (ap->addr.as_u32 == dst_addr->as_u32)
3756 m_key.addr.as_u32 = dst_addr->as_u32;
3757 m_key.fib_index = sm->outside_fib_index;
3760 kv.key = m_key.as_u64;
3761 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3768 snat_hairpin_dst_fn (vlib_main_t * vm,
3769 vlib_node_runtime_t * node,
3770 vlib_frame_t * frame)
3772 u32 n_left_from, * from, * to_next;
3773 snat_in2out_next_t next_index;
3774 u32 pkts_processed = 0;
3775 snat_main_t * sm = &snat_main;
3777 from = vlib_frame_vector_args (frame);
3778 n_left_from = frame->n_vectors;
3779 next_index = node->cached_next_index;
3781 while (n_left_from > 0)
3785 vlib_get_next_frame (vm, node, next_index,
3786 to_next, n_left_to_next);
3788 while (n_left_from > 0 && n_left_to_next > 0)
3796 /* speculatively enqueue b0 to the current next frame */
3802 n_left_to_next -= 1;
3804 b0 = vlib_get_buffer (vm, bi0);
3805 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3806 ip0 = vlib_buffer_get_current (b0);
3808 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3810 vnet_buffer (b0)->snat.flags = 0;
3811 if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
3813 if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
3815 udp_header_t * udp0 = ip4_next_header (ip0);
3816 tcp_header_t * tcp0 = (tcp_header_t *) udp0;
3818 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
3820 else if (proto0 == SNAT_PROTOCOL_ICMP)
3822 icmp46_header_t * icmp0 = ip4_next_header (ip0);
3824 snat_icmp_hairpinning (sm, b0, ip0, icmp0);
3828 snat_hairpinning_unknown_proto (sm, b0, ip0);
3831 vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
3834 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3836 /* verify speculative enqueue, maybe switch current next frame */
3837 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3838 to_next, n_left_to_next,
3842 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3845 vlib_node_increment_counter (vm, snat_hairpin_dst_node.index,
3846 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3848 return frame->n_vectors;
3851 VLIB_REGISTER_NODE (snat_hairpin_dst_node) = {
3852 .function = snat_hairpin_dst_fn,
3853 .name = "nat44-hairpin-dst",
3854 .vector_size = sizeof (u32),
3855 .type = VLIB_NODE_TYPE_INTERNAL,
3856 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3857 .error_strings = snat_in2out_error_strings,
3860 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3861 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3865 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_dst_node,
3866 snat_hairpin_dst_fn);
3869 snat_hairpin_src_fn (vlib_main_t * vm,
3870 vlib_node_runtime_t * node,
3871 vlib_frame_t * frame)
3873 u32 n_left_from, * from, * to_next;
3874 snat_in2out_next_t next_index;
3875 u32 pkts_processed = 0;
3876 snat_main_t *sm = &snat_main;
3878 from = vlib_frame_vector_args (frame);
3879 n_left_from = frame->n_vectors;
3880 next_index = node->cached_next_index;
3882 while (n_left_from > 0)
3886 vlib_get_next_frame (vm, node, next_index,
3887 to_next, n_left_to_next);
3889 while (n_left_from > 0 && n_left_to_next > 0)
3894 snat_interface_t *i;
3897 /* speculatively enqueue b0 to the current next frame */
3903 n_left_to_next -= 1;
3905 b0 = vlib_get_buffer (vm, bi0);
3906 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3907 next0 = SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT;
3909 pool_foreach (i, sm->output_feature_interfaces,
3911 /* Only packets from NAT inside interface */
3912 if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
3914 if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
3915 SNAT_FLAG_HAIRPINNING))
3917 if (PREDICT_TRUE (sm->num_workers > 1))
3918 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
3920 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
3926 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3928 /* verify speculative enqueue, maybe switch current next frame */
3929 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3930 to_next, n_left_to_next,
3934 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3937 vlib_node_increment_counter (vm, snat_hairpin_src_node.index,
3938 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3940 return frame->n_vectors;
3943 VLIB_REGISTER_NODE (snat_hairpin_src_node) = {
3944 .function = snat_hairpin_src_fn,
3945 .name = "nat44-hairpin-src",
3946 .vector_size = sizeof (u32),
3947 .type = VLIB_NODE_TYPE_INTERNAL,
3948 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3949 .error_strings = snat_in2out_error_strings,
3950 .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
3952 [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
3953 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
3954 [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
3955 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
3959 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_src_node,
3960 snat_hairpin_src_fn);
3963 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
3964 vlib_node_runtime_t * node,
3965 vlib_frame_t * frame)
3967 u32 n_left_from, * from, * to_next;
3968 snat_in2out_next_t next_index;
3969 u32 pkts_processed = 0;
3970 snat_main_t * sm = &snat_main;
3971 u32 stats_node_index;
3973 stats_node_index = snat_in2out_fast_node.index;
3975 from = vlib_frame_vector_args (frame);
3976 n_left_from = frame->n_vectors;
3977 next_index = node->cached_next_index;
3979 while (n_left_from > 0)
3983 vlib_get_next_frame (vm, node, next_index,
3984 to_next, n_left_to_next);
3986 while (n_left_from > 0 && n_left_to_next > 0)
3994 u32 new_addr0, old_addr0;
3995 u16 old_port0, new_port0;
3996 udp_header_t * udp0;
3997 tcp_header_t * tcp0;
3998 icmp46_header_t * icmp0;
3999 snat_session_key_t key0, sm0;
4003 /* speculatively enqueue b0 to the current next frame */
4009 n_left_to_next -= 1;
4011 b0 = vlib_get_buffer (vm, bi0);
4012 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
4014 ip0 = vlib_buffer_get_current (b0);
4015 udp0 = ip4_next_header (ip0);
4016 tcp0 = (tcp_header_t *) udp0;
4017 icmp0 = (icmp46_header_t *) udp0;
4019 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
4020 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
4022 if (PREDICT_FALSE(ip0->ttl == 1))
4024 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
4025 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
4026 ICMP4_time_exceeded_ttl_exceeded_in_transit,
4028 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
4032 proto0 = ip_proto_to_snat_proto (ip0->protocol);
4034 if (PREDICT_FALSE (proto0 == ~0))
4037 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
4039 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
4040 rx_fib_index0, node, next0, ~0, 0, 0);
4044 key0.addr = ip0->src_address;
4045 key0.protocol = proto0;
4046 key0.port = udp0->src_port;
4047 key0.fib_index = rx_fib_index0;
4049 if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0, 0))
4051 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
4052 next0= SNAT_IN2OUT_NEXT_DROP;
4056 new_addr0 = sm0.addr.as_u32;
4057 new_port0 = sm0.port;
4058 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
4059 old_addr0 = ip0->src_address.as_u32;
4060 ip0->src_address.as_u32 = new_addr0;
4062 sum0 = ip0->checksum;
4063 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
4065 src_address /* changed member */);
4066 ip0->checksum = ip_csum_fold (sum0);
4068 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
4070 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
4072 old_port0 = tcp0->src_port;
4073 tcp0->src_port = new_port0;
4075 sum0 = tcp0->checksum;
4076 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
4078 dst_address /* changed member */);
4079 sum0 = ip_csum_update (sum0, old_port0, new_port0,
4080 ip4_header_t /* cheat */,
4081 length /* changed member */);
4082 tcp0->checksum = ip_csum_fold(sum0);
4086 old_port0 = udp0->src_port;
4087 udp0->src_port = new_port0;
4093 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
4095 sum0 = tcp0->checksum;
4096 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
4098 dst_address /* changed member */);
4099 tcp0->checksum = ip_csum_fold(sum0);
4104 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
4107 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
4108 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
4110 snat_in2out_trace_t *t =
4111 vlib_add_trace (vm, node, b0, sizeof (*t));
4112 t->sw_if_index = sw_if_index0;
4113 t->next_index = next0;
4116 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
4118 /* verify speculative enqueue, maybe switch current next frame */
4119 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
4120 to_next, n_left_to_next,
4124 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
4127 vlib_node_increment_counter (vm, stats_node_index,
4128 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
4130 return frame->n_vectors;
4134 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
4135 .function = snat_in2out_fast_static_map_fn,
4136 .name = "nat44-in2out-fast",
4137 .vector_size = sizeof (u32),
4138 .format_trace = format_snat_in2out_fast_trace,
4139 .type = VLIB_NODE_TYPE_INTERNAL,
4141 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
4142 .error_strings = snat_in2out_error_strings,
4144 .runtime_data_bytes = sizeof (snat_runtime_t),
4146 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
4148 /* edit / add dispositions here */
4150 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
4151 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
4152 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
4153 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4154 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
4158 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);