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 addresss 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))
248 if (sm->forwarding_enabled)
251 return snat_not_translate_fast(sm, node, sw_if_index0, ip0, proto0,
255 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
258 snat_session_key_t * key0,
259 snat_session_t ** sessionp,
260 vlib_node_runtime_t * node,
266 clib_bihash_kv_8_8_t kv0;
267 snat_session_key_t key1;
268 u32 address_index = ~0;
269 u32 outside_fib_index;
271 udp_header_t * udp0 = ip4_next_header (ip0);
273 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
275 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
276 return SNAT_IN2OUT_NEXT_DROP;
279 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
282 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
283 return SNAT_IN2OUT_NEXT_DROP;
285 outside_fib_index = p[0];
287 key1.protocol = key0->protocol;
289 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
293 clib_warning ("create NAT user failed");
294 return SNAT_IN2OUT_NEXT_DROP;
297 s = nat_session_alloc_or_recycle (sm, u, thread_index);
300 clib_warning ("create NAT session failed");
301 return SNAT_IN2OUT_NEXT_DROP;
304 /* First try to match static mapping by local address and port */
305 if (snat_static_mapping_match (sm, *key0, &key1, 0, 0, 0))
307 /* Try to create dynamic translation */
308 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
312 sm->per_thread_data[thread_index].snat_thread_index))
314 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
315 return SNAT_IN2OUT_NEXT_DROP;
321 u->nstaticsessions++;
322 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
325 s->outside_address_index = address_index;
328 s->out2in.protocol = key0->protocol;
329 s->out2in.fib_index = outside_fib_index;
330 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
331 s->ext_host_port = udp0->dst_port;
334 /* Add to translation hashes */
335 kv0.key = s->in2out.as_u64;
336 kv0.value = s - sm->per_thread_data[thread_index].sessions;
337 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
339 clib_warning ("in2out key add failed");
341 kv0.key = s->out2in.as_u64;
342 kv0.value = s - sm->per_thread_data[thread_index].sessions;
344 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
346 clib_warning ("out2in key add failed");
349 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
350 s->out2in.addr.as_u32,
354 s->in2out.fib_index);
359 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
360 snat_session_key_t *p_key0)
362 icmp46_header_t *icmp0;
363 snat_session_key_t key0;
364 icmp_echo_header_t *echo0, *inner_echo0 = 0;
365 ip4_header_t *inner_ip0 = 0;
367 icmp46_header_t *inner_icmp0;
369 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
370 echo0 = (icmp_echo_header_t *)(icmp0+1);
372 if (!icmp_is_error_message (icmp0))
374 key0.protocol = SNAT_PROTOCOL_ICMP;
375 key0.addr = ip0->src_address;
376 key0.port = echo0->identifier;
380 inner_ip0 = (ip4_header_t *)(echo0+1);
381 l4_header = ip4_next_header (inner_ip0);
382 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
383 key0.addr = inner_ip0->dst_address;
384 switch (key0.protocol)
386 case SNAT_PROTOCOL_ICMP:
387 inner_icmp0 = (icmp46_header_t*)l4_header;
388 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
389 key0.port = inner_echo0->identifier;
391 case SNAT_PROTOCOL_UDP:
392 case SNAT_PROTOCOL_TCP:
393 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
396 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
400 return -1; /* success */
404 * Get address and port values to be used for ICMP packet translation
405 * and create session if needed
407 * @param[in,out] sm NAT main
408 * @param[in,out] node NAT node runtime
409 * @param[in] thread_index thread index
410 * @param[in,out] b0 buffer containing packet to be translated
411 * @param[out] p_proto protocol used for matching
412 * @param[out] p_value address and port after NAT translation
413 * @param[out] p_dont_translate if packet should not be translated
414 * @param d optional parameter
415 * @param e optional parameter
417 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
418 u32 thread_index, vlib_buffer_t *b0,
419 ip4_header_t *ip0, u8 *p_proto,
420 snat_session_key_t *p_value,
421 u8 *p_dont_translate, void *d, void *e)
423 icmp46_header_t *icmp0;
426 snat_session_key_t key0;
427 snat_session_t *s0 = 0;
428 u8 dont_translate = 0;
429 clib_bihash_kv_8_8_t kv0, value0;
433 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
434 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
435 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
437 err = icmp_get_key (ip0, &key0);
440 b0->error = node->errors[err];
441 next0 = SNAT_IN2OUT_NEXT_DROP;
444 key0.fib_index = rx_fib_index0;
446 kv0.key = key0.as_u64;
448 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
451 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
452 IP_PROTOCOL_ICMP, rx_fib_index0, thread_index) &&
453 vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0))
459 if (PREDICT_FALSE(icmp_is_error_message (icmp0)))
461 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
462 next0 = SNAT_IN2OUT_NEXT_DROP;
466 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
467 &s0, node, next0, thread_index);
469 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
474 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
475 icmp0->type != ICMP4_echo_reply &&
476 !icmp_is_error_message (icmp0)))
478 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
479 next0 = SNAT_IN2OUT_NEXT_DROP;
483 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
488 *p_proto = key0.protocol;
490 *p_value = s0->out2in;
491 *p_dont_translate = dont_translate;
493 *(snat_session_t**)d = s0;
498 * Get address and port values to be used for ICMP packet translation
500 * @param[in] sm NAT main
501 * @param[in,out] node NAT node runtime
502 * @param[in] thread_index thread index
503 * @param[in,out] b0 buffer containing packet to be translated
504 * @param[out] p_proto protocol used for matching
505 * @param[out] p_value address and port after NAT translation
506 * @param[out] p_dont_translate if packet should not be translated
507 * @param d optional parameter
508 * @param e optional parameter
510 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
511 u32 thread_index, vlib_buffer_t *b0,
512 ip4_header_t *ip0, u8 *p_proto,
513 snat_session_key_t *p_value,
514 u8 *p_dont_translate, void *d, void *e)
516 icmp46_header_t *icmp0;
519 snat_session_key_t key0;
520 snat_session_key_t sm0;
521 u8 dont_translate = 0;
526 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
527 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
528 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
530 err = icmp_get_key (ip0, &key0);
533 b0->error = node->errors[err];
534 next0 = SNAT_IN2OUT_NEXT_DROP;
537 key0.fib_index = rx_fib_index0;
539 if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only, 0))
541 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
542 IP_PROTOCOL_ICMP, rx_fib_index0)))
548 if (icmp_is_error_message (icmp0))
550 next0 = SNAT_IN2OUT_NEXT_DROP;
554 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
555 next0 = SNAT_IN2OUT_NEXT_DROP;
559 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
560 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
561 !icmp_is_error_message (icmp0)))
563 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
564 next0 = SNAT_IN2OUT_NEXT_DROP;
571 *p_proto = key0.protocol;
572 *p_dont_translate = dont_translate;
576 static inline u32 icmp_in2out (snat_main_t *sm,
579 icmp46_header_t * icmp0,
582 vlib_node_runtime_t * node,
588 snat_session_key_t sm0;
590 icmp_echo_header_t *echo0, *inner_echo0 = 0;
591 ip4_header_t *inner_ip0;
593 icmp46_header_t *inner_icmp0;
595 u32 new_addr0, old_addr0;
596 u16 old_id0, new_id0;
601 echo0 = (icmp_echo_header_t *)(icmp0+1);
603 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0, ip0,
604 &protocol, &sm0, &dont_translate, d, e);
607 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
610 sum0 = ip_incremental_checksum (0, icmp0,
611 ntohs(ip0->length) - ip4_header_bytes (ip0));
612 checksum0 = ~ip_csum_fold (sum0);
613 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
615 next0 = SNAT_IN2OUT_NEXT_DROP;
619 old_addr0 = ip0->src_address.as_u32;
620 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
621 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
622 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
624 sum0 = ip0->checksum;
625 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
626 src_address /* changed member */);
627 ip0->checksum = ip_csum_fold (sum0);
629 if (!icmp_is_error_message (icmp0))
632 if (PREDICT_FALSE(new_id0 != echo0->identifier))
634 old_id0 = echo0->identifier;
636 echo0->identifier = new_id0;
638 sum0 = icmp0->checksum;
639 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
641 icmp0->checksum = ip_csum_fold (sum0);
646 inner_ip0 = (ip4_header_t *)(echo0+1);
647 l4_header = ip4_next_header (inner_ip0);
649 if (!ip4_header_checksum_is_valid (inner_ip0))
651 next0 = SNAT_IN2OUT_NEXT_DROP;
655 old_addr0 = inner_ip0->dst_address.as_u32;
656 inner_ip0->dst_address = sm0.addr;
657 new_addr0 = inner_ip0->dst_address.as_u32;
659 sum0 = icmp0->checksum;
660 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
661 dst_address /* changed member */);
662 icmp0->checksum = ip_csum_fold (sum0);
666 case SNAT_PROTOCOL_ICMP:
667 inner_icmp0 = (icmp46_header_t*)l4_header;
668 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
670 old_id0 = inner_echo0->identifier;
672 inner_echo0->identifier = new_id0;
674 sum0 = icmp0->checksum;
675 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
677 icmp0->checksum = ip_csum_fold (sum0);
679 case SNAT_PROTOCOL_UDP:
680 case SNAT_PROTOCOL_TCP:
681 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
683 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
685 sum0 = icmp0->checksum;
686 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
688 icmp0->checksum = ip_csum_fold (sum0);
702 * Hairpinning allows two endpoints on the internal side of the NAT to
703 * communicate even if they only use each other's external IP addresses
706 * @param sm NAT main.
707 * @param b0 Vlib buffer.
708 * @param ip0 IP header.
709 * @param udp0 UDP header.
710 * @param tcp0 TCP header.
711 * @param proto0 NAT protocol.
714 snat_hairpinning (snat_main_t *sm,
721 snat_session_key_t key0, sm0;
723 clib_bihash_kv_8_8_t kv0, value0;
725 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
726 u16 new_dst_port0, old_dst_port0;
728 key0.addr = ip0->dst_address;
729 key0.port = udp0->dst_port;
730 key0.protocol = proto0;
731 key0.fib_index = sm->outside_fib_index;
732 kv0.key = key0.as_u64;
734 /* Check if destination is static mappings */
735 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
737 new_dst_addr0 = sm0.addr.as_u32;
738 new_dst_port0 = sm0.port;
739 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
741 /* or active session */
744 if (sm->num_workers > 1)
745 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
747 ti = sm->num_workers;
749 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
753 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
754 new_dst_addr0 = s0->in2out.addr.as_u32;
755 new_dst_port0 = s0->in2out.port;
756 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
760 /* Destination is behind the same NAT, use internal address and port */
763 old_dst_addr0 = ip0->dst_address.as_u32;
764 ip0->dst_address.as_u32 = new_dst_addr0;
765 sum0 = ip0->checksum;
766 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
767 ip4_header_t, dst_address);
768 ip0->checksum = ip_csum_fold (sum0);
770 old_dst_port0 = tcp0->dst;
771 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
773 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
775 tcp0->dst = new_dst_port0;
776 sum0 = tcp0->checksum;
777 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
778 ip4_header_t, dst_address);
779 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
780 ip4_header_t /* cheat */, length);
781 tcp0->checksum = ip_csum_fold(sum0);
785 udp0->dst_port = new_dst_port0;
791 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
793 sum0 = tcp0->checksum;
794 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
795 ip4_header_t, dst_address);
796 tcp0->checksum = ip_csum_fold(sum0);
805 snat_icmp_hairpinning (snat_main_t *sm,
808 icmp46_header_t * icmp0)
810 snat_session_key_t key0, sm0;
811 clib_bihash_kv_8_8_t kv0, value0;
812 u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
816 if (!icmp_is_error_message (icmp0))
818 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
819 u16 icmp_id0 = echo0->identifier;
820 key0.addr = ip0->dst_address;
821 key0.port = icmp_id0;
822 key0.protocol = SNAT_PROTOCOL_ICMP;
823 key0.fib_index = sm->outside_fib_index;
824 kv0.key = key0.as_u64;
826 if (sm->num_workers > 1)
827 ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
829 ti = sm->num_workers;
831 /* Check if destination is in active sessions */
832 if (clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
835 /* or static mappings */
836 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
838 new_dst_addr0 = sm0.addr.as_u32;
839 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
846 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
847 new_dst_addr0 = s0->in2out.addr.as_u32;
848 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
849 echo0->identifier = s0->in2out.port;
850 sum0 = icmp0->checksum;
851 sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
852 icmp_echo_header_t, identifier);
853 icmp0->checksum = ip_csum_fold (sum0);
856 /* Destination is behind the same NAT, use internal address and port */
859 old_dst_addr0 = ip0->dst_address.as_u32;
860 ip0->dst_address.as_u32 = new_dst_addr0;
861 sum0 = ip0->checksum;
862 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
863 ip4_header_t, dst_address);
864 ip0->checksum = ip_csum_fold (sum0);
870 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
873 icmp46_header_t * icmp0,
876 vlib_node_runtime_t * node,
880 snat_session_t ** p_s0)
882 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
883 next0, thread_index, p_s0, 0);
884 snat_session_t * s0 = *p_s0;
885 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
888 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == 0)
889 snat_icmp_hairpinning(sm, b0, ip0, icmp0);
891 s0->last_heard = now;
893 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
894 /* Per-user LRU list maintenance */
895 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
897 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
898 s0->per_user_list_head_index,
904 snat_hairpinning_unknown_proto (snat_main_t *sm,
908 u32 old_addr, new_addr = 0, ti = 0;
909 clib_bihash_kv_8_8_t kv, value;
910 clib_bihash_kv_16_8_t s_kv, s_value;
911 nat_ed_ses_key_t key;
912 snat_session_key_t m_key;
913 snat_static_mapping_t *m;
917 old_addr = ip->dst_address.as_u32;
918 key.l_addr.as_u32 = ip->dst_address.as_u32;
919 key.r_addr.as_u32 = ip->src_address.as_u32;
920 key.fib_index = sm->outside_fib_index;
921 key.proto = ip->protocol;
924 s_kv.key[0] = key.as_u64[0];
925 s_kv.key[1] = key.as_u64[1];
926 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
928 m_key.addr = ip->dst_address;
929 m_key.fib_index = sm->outside_fib_index;
932 kv.key = m_key.as_u64;
933 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
936 m = pool_elt_at_index (sm->static_mappings, value.value);
937 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
938 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
939 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
943 if (sm->num_workers > 1)
944 ti = sm->worker_out2in_cb (ip, sm->outside_fib_index);
946 ti = sm->num_workers;
948 s = pool_elt_at_index (sm->per_thread_data[ti].sessions, s_value.value);
949 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
950 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
951 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
954 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
955 ip->checksum = ip_csum_fold (sum);
958 static snat_session_t *
959 snat_in2out_unknown_proto (snat_main_t *sm,
966 vlib_node_runtime_t * node)
968 clib_bihash_kv_8_8_t kv, value;
969 clib_bihash_kv_16_8_t s_kv, s_value;
970 snat_static_mapping_t *m;
971 snat_session_key_t m_key;
972 u32 old_addr, new_addr = 0;
975 dlist_elt_t *head, *elt;
976 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
977 u32 elt_index, head_index, ses_index;
979 nat_ed_ses_key_t key;
980 u32 address_index = ~0;
984 old_addr = ip->src_address.as_u32;
986 key.l_addr = ip->src_address;
987 key.r_addr = ip->dst_address;
988 key.fib_index = rx_fib_index;
989 key.proto = ip->protocol;
992 s_kv.key[0] = key.as_u64[0];
993 s_kv.key[1] = key.as_u64[1];
995 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
997 s = pool_elt_at_index (tsm->sessions, s_value.value);
998 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1002 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
1004 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1008 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
1012 clib_warning ("create NAT user failed");
1016 m_key.addr = ip->src_address;
1019 m_key.fib_index = rx_fib_index;
1020 kv.key = m_key.as_u64;
1022 /* Try to find static mapping first */
1023 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
1025 m = pool_elt_at_index (sm->static_mappings, value.value);
1026 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
1030 /* Fallback to 3-tuple key */
1033 /* Choose same out address as for TCP/UDP session to same destination */
1034 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1036 head_index = u->sessions_per_user_list_head_index;
1037 head = pool_elt_at_index (tsm->list_pool, head_index);
1038 elt_index = head->next;
1039 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1040 ses_index = elt->value;
1041 while (ses_index != ~0)
1043 s = pool_elt_at_index (tsm->sessions, ses_index);
1044 elt_index = elt->next;
1045 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1046 ses_index = elt->value;
1048 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
1050 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1051 address_index = s->outside_address_index;
1053 key.fib_index = sm->outside_fib_index;
1054 key.l_addr.as_u32 = new_addr;
1055 s_kv.key[0] = key.as_u64[0];
1056 s_kv.key[1] = key.as_u64[1];
1057 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1064 key.fib_index = sm->outside_fib_index;
1065 for (i = 0; i < vec_len (sm->addresses); i++)
1067 key.l_addr.as_u32 = sm->addresses[i].addr.as_u32;
1068 s_kv.key[0] = key.as_u64[0];
1069 s_kv.key[1] = key.as_u64[1];
1070 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1072 new_addr = ip->src_address.as_u32 = key.l_addr.as_u32;
1081 s = nat_session_alloc_or_recycle (sm, u, thread_index);
1084 clib_warning ("create NAT session failed");
1088 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1089 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
1090 s->outside_address_index = address_index;
1091 s->out2in.addr.as_u32 = new_addr;
1092 s->out2in.fib_index = sm->outside_fib_index;
1093 s->in2out.addr.as_u32 = old_addr;
1094 s->in2out.fib_index = rx_fib_index;
1095 s->in2out.port = s->out2in.port = ip->protocol;
1098 u->nstaticsessions++;
1099 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1106 /* Add to lookup tables */
1107 key.l_addr.as_u32 = old_addr;
1108 key.r_addr = ip->dst_address;
1109 key.proto = ip->protocol;
1110 key.fib_index = rx_fib_index;
1111 s_kv.key[0] = key.as_u64[0];
1112 s_kv.key[1] = key.as_u64[1];
1113 s_kv.value = s - tsm->sessions;
1114 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1115 clib_warning ("in2out key add failed");
1117 key.l_addr.as_u32 = new_addr;
1118 key.fib_index = sm->outside_fib_index;
1119 s_kv.key[0] = key.as_u64[0];
1120 s_kv.key[1] = key.as_u64[1];
1121 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1122 clib_warning ("out2in key add failed");
1125 /* Update IP checksum */
1127 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1128 ip->checksum = ip_csum_fold (sum);
1131 s->last_heard = now;
1133 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1134 /* Per-user LRU list maintenance */
1135 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1136 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1140 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1141 snat_hairpinning_unknown_proto(sm, b, ip);
1143 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1144 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1149 static snat_session_t *
1150 snat_in2out_lb (snat_main_t *sm,
1157 vlib_node_runtime_t * node)
1159 nat_ed_ses_key_t key;
1160 clib_bihash_kv_16_8_t s_kv, s_value;
1161 udp_header_t *udp = ip4_next_header (ip);
1162 tcp_header_t *tcp = (tcp_header_t *) udp;
1163 snat_session_t *s = 0;
1164 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1165 u32 old_addr, new_addr;
1166 u16 new_port, old_port;
1168 u32 proto = ip_proto_to_snat_proto (ip->protocol);
1169 snat_session_key_t e_key, l_key;
1172 old_addr = ip->src_address.as_u32;
1174 key.l_addr = ip->src_address;
1175 key.r_addr = ip->dst_address;
1176 key.fib_index = rx_fib_index;
1177 key.proto = ip->protocol;
1178 key.r_port = udp->dst_port;
1179 key.l_port = udp->src_port;
1180 s_kv.key[0] = key.as_u64[0];
1181 s_kv.key[1] = key.as_u64[1];
1183 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
1185 s = pool_elt_at_index (tsm->sessions, s_value.value);
1189 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
1191 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1195 l_key.addr = ip->src_address;
1196 l_key.port = udp->src_port;
1197 l_key.protocol = proto;
1198 l_key.fib_index = rx_fib_index;
1199 if (snat_static_mapping_match(sm, l_key, &e_key, 0, 0, 0))
1202 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
1206 clib_warning ("create NAT user failed");
1210 s = nat_session_alloc_or_recycle (sm, u, thread_index);
1213 clib_warning ("create NAT session failed");
1217 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1218 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1219 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
1220 s->outside_address_index = ~0;
1223 u->nstaticsessions++;
1225 /* Add to lookup tables */
1226 s_kv.value = s - tsm->sessions;
1227 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1228 clib_warning ("in2out-ed key add failed");
1230 key.l_addr = e_key.addr;
1231 key.fib_index = e_key.fib_index;
1232 key.l_port = e_key.port;
1233 s_kv.key[0] = key.as_u64[0];
1234 s_kv.key[1] = key.as_u64[1];
1235 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1236 clib_warning ("out2in-ed key add failed");
1239 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1241 /* Update IP checksum */
1243 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1244 if (is_twice_nat_session (s))
1245 sum = ip_csum_update (sum, ip->dst_address.as_u32,
1246 s->ext_host_addr.as_u32, ip4_header_t, dst_address);
1247 ip->checksum = ip_csum_fold (sum);
1249 if (PREDICT_TRUE(proto == SNAT_PROTOCOL_TCP))
1251 old_port = tcp->src_port;
1252 tcp->src_port = s->out2in.port;
1253 new_port = tcp->src_port;
1255 sum = tcp->checksum;
1256 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1257 sum = ip_csum_update (sum, old_port, new_port, ip4_header_t, length);
1258 if (is_twice_nat_session (s))
1260 sum = ip_csum_update (sum, ip->dst_address.as_u32,
1261 s->ext_host_addr.as_u32, ip4_header_t,
1263 sum = ip_csum_update (sum, tcp->dst_port, s->ext_host_port,
1264 ip4_header_t, length);
1265 tcp->dst_port = s->ext_host_port;
1266 ip->dst_address.as_u32 = s->ext_host_addr.as_u32;
1268 tcp->checksum = ip_csum_fold(sum);
1272 udp->src_port = s->out2in.port;
1273 if (is_twice_nat_session (s))
1275 udp->dst_port = s->ext_host_port;
1276 ip->dst_address.as_u32 = s->ext_host_addr.as_u32;
1281 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1282 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1285 s->last_heard = now;
1287 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1288 /* Per-user LRU list maintenance */
1289 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1290 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1296 snat_in2out_node_fn_inline (vlib_main_t * vm,
1297 vlib_node_runtime_t * node,
1298 vlib_frame_t * frame, int is_slow_path,
1299 int is_output_feature)
1301 u32 n_left_from, * from, * to_next;
1302 snat_in2out_next_t next_index;
1303 u32 pkts_processed = 0;
1304 snat_main_t * sm = &snat_main;
1305 f64 now = vlib_time_now (vm);
1306 u32 stats_node_index;
1307 u32 thread_index = vlib_get_thread_index ();
1309 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
1310 snat_in2out_node.index;
1312 from = vlib_frame_vector_args (frame);
1313 n_left_from = frame->n_vectors;
1314 next_index = node->cached_next_index;
1316 while (n_left_from > 0)
1320 vlib_get_next_frame (vm, node, next_index,
1321 to_next, n_left_to_next);
1323 while (n_left_from >= 4 && n_left_to_next >= 2)
1326 vlib_buffer_t * b0, * b1;
1328 u32 sw_if_index0, sw_if_index1;
1329 ip4_header_t * ip0, * ip1;
1330 ip_csum_t sum0, sum1;
1331 u32 new_addr0, old_addr0, new_addr1, old_addr1;
1332 u16 old_port0, new_port0, old_port1, new_port1;
1333 udp_header_t * udp0, * udp1;
1334 tcp_header_t * tcp0, * tcp1;
1335 icmp46_header_t * icmp0, * icmp1;
1336 snat_session_key_t key0, key1;
1337 u32 rx_fib_index0, rx_fib_index1;
1339 snat_session_t * s0 = 0, * s1 = 0;
1340 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
1341 u32 iph_offset0 = 0, iph_offset1 = 0;
1343 /* Prefetch next iteration. */
1345 vlib_buffer_t * p2, * p3;
1347 p2 = vlib_get_buffer (vm, from[2]);
1348 p3 = vlib_get_buffer (vm, from[3]);
1350 vlib_prefetch_buffer_header (p2, LOAD);
1351 vlib_prefetch_buffer_header (p3, LOAD);
1353 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1354 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1357 /* speculatively enqueue b0 and b1 to the current next frame */
1358 to_next[0] = bi0 = from[0];
1359 to_next[1] = bi1 = from[1];
1363 n_left_to_next -= 2;
1365 b0 = vlib_get_buffer (vm, bi0);
1366 b1 = vlib_get_buffer (vm, bi1);
1368 if (is_output_feature)
1369 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1371 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1374 udp0 = ip4_next_header (ip0);
1375 tcp0 = (tcp_header_t *) udp0;
1376 icmp0 = (icmp46_header_t *) udp0;
1378 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1379 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1382 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1384 if (PREDICT_FALSE(ip0->ttl == 1))
1386 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1387 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1388 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1390 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1394 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1396 /* Next configured feature, probably ip4-lookup */
1399 if (PREDICT_FALSE (proto0 == ~0))
1401 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
1402 thread_index, now, vm, node);
1404 next0 = SNAT_IN2OUT_NEXT_DROP;
1408 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1410 next0 = icmp_in2out_slow_path
1411 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1412 node, next0, now, thread_index, &s0);
1418 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1420 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1424 if (ip4_is_fragment (ip0))
1426 next0 = SNAT_IN2OUT_NEXT_REASS;
1431 key0.addr = ip0->src_address;
1432 key0.port = udp0->src_port;
1433 key0.protocol = proto0;
1434 key0.fib_index = rx_fib_index0;
1436 kv0.key = key0.as_u64;
1438 if (PREDICT_FALSE (clib_bihash_search_8_8 (
1439 &sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1443 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1444 ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
1447 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1448 &s0, node, next0, thread_index);
1449 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1454 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1460 if (PREDICT_FALSE (value0.value == ~0ULL))
1464 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
1465 thread_index, now, vm, node);
1467 next0 = SNAT_IN2OUT_NEXT_DROP;
1472 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1478 s0 = pool_elt_at_index (
1479 sm->per_thread_data[thread_index].sessions,
1484 b0->flags |= VNET_BUFFER_F_IS_NATED;
1486 old_addr0 = ip0->src_address.as_u32;
1487 ip0->src_address = s0->out2in.addr;
1488 new_addr0 = ip0->src_address.as_u32;
1489 if (!is_output_feature)
1490 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1492 sum0 = ip0->checksum;
1493 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1495 src_address /* changed member */);
1496 ip0->checksum = ip_csum_fold (sum0);
1498 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1500 old_port0 = tcp0->src_port;
1501 tcp0->src_port = s0->out2in.port;
1502 new_port0 = tcp0->src_port;
1504 sum0 = tcp0->checksum;
1505 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1507 dst_address /* changed member */);
1508 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1509 ip4_header_t /* cheat */,
1510 length /* changed member */);
1511 tcp0->checksum = ip_csum_fold(sum0);
1515 old_port0 = udp0->src_port;
1516 udp0->src_port = s0->out2in.port;
1521 s0->last_heard = now;
1523 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1524 /* Per-user LRU list maintenance */
1525 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1526 s0->per_user_index);
1527 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1528 s0->per_user_list_head_index,
1529 s0->per_user_index);
1532 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1533 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1535 snat_in2out_trace_t *t =
1536 vlib_add_trace (vm, node, b0, sizeof (*t));
1537 t->is_slow_path = is_slow_path;
1538 t->sw_if_index = sw_if_index0;
1539 t->next_index = next0;
1540 t->session_index = ~0;
1542 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1545 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1547 if (is_output_feature)
1548 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1550 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1553 udp1 = ip4_next_header (ip1);
1554 tcp1 = (tcp_header_t *) udp1;
1555 icmp1 = (icmp46_header_t *) udp1;
1557 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1558 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1561 if (PREDICT_FALSE(ip1->ttl == 1))
1563 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1564 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1565 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1567 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1571 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1573 /* Next configured feature, probably ip4-lookup */
1576 if (PREDICT_FALSE (proto1 == ~0))
1578 s1 = snat_in2out_unknown_proto (sm, b1, ip1, rx_fib_index1,
1579 thread_index, now, vm, node);
1581 next1 = SNAT_IN2OUT_NEXT_DROP;
1585 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1587 next1 = icmp_in2out_slow_path
1588 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1589 next1, now, thread_index, &s1);
1595 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1597 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1601 if (ip4_is_fragment (ip1))
1603 next1 = SNAT_IN2OUT_NEXT_REASS;
1608 b1->flags |= VNET_BUFFER_F_IS_NATED;
1610 key1.addr = ip1->src_address;
1611 key1.port = udp1->src_port;
1612 key1.protocol = proto1;
1613 key1.fib_index = rx_fib_index1;
1615 kv1.key = key1.as_u64;
1617 if (PREDICT_FALSE(clib_bihash_search_8_8 (
1618 &sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1622 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
1623 ip1, proto1, rx_fib_index1, thread_index)) && !is_output_feature)
1626 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1627 &s1, node, next1, thread_index);
1628 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1633 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1639 if (PREDICT_FALSE (value1.value == ~0ULL))
1643 s1 = snat_in2out_lb(sm, b1, ip1, rx_fib_index1,
1644 thread_index, now, vm, node);
1646 next1 = SNAT_IN2OUT_NEXT_DROP;
1651 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1657 s1 = pool_elt_at_index (
1658 sm->per_thread_data[thread_index].sessions,
1663 old_addr1 = ip1->src_address.as_u32;
1664 ip1->src_address = s1->out2in.addr;
1665 new_addr1 = ip1->src_address.as_u32;
1666 if (!is_output_feature)
1667 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1669 sum1 = ip1->checksum;
1670 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1672 src_address /* changed member */);
1673 ip1->checksum = ip_csum_fold (sum1);
1675 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1677 old_port1 = tcp1->src_port;
1678 tcp1->src_port = s1->out2in.port;
1679 new_port1 = tcp1->src_port;
1681 sum1 = tcp1->checksum;
1682 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1684 dst_address /* changed member */);
1685 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1686 ip4_header_t /* cheat */,
1687 length /* changed member */);
1688 tcp1->checksum = ip_csum_fold(sum1);
1692 old_port1 = udp1->src_port;
1693 udp1->src_port = s1->out2in.port;
1698 s1->last_heard = now;
1700 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1701 /* Per-user LRU list maintenance */
1702 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1703 s1->per_user_index);
1704 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1705 s1->per_user_list_head_index,
1706 s1->per_user_index);
1709 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1710 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1712 snat_in2out_trace_t *t =
1713 vlib_add_trace (vm, node, b1, sizeof (*t));
1714 t->sw_if_index = sw_if_index1;
1715 t->next_index = next1;
1716 t->session_index = ~0;
1718 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1721 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1723 /* verify speculative enqueues, maybe switch current next frame */
1724 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1725 to_next, n_left_to_next,
1726 bi0, bi1, next0, next1);
1729 while (n_left_from > 0 && n_left_to_next > 0)
1737 u32 new_addr0, old_addr0;
1738 u16 old_port0, new_port0;
1739 udp_header_t * udp0;
1740 tcp_header_t * tcp0;
1741 icmp46_header_t * icmp0;
1742 snat_session_key_t key0;
1745 snat_session_t * s0 = 0;
1746 clib_bihash_kv_8_8_t kv0, value0;
1747 u32 iph_offset0 = 0;
1749 /* speculatively enqueue b0 to the current next frame */
1755 n_left_to_next -= 1;
1757 b0 = vlib_get_buffer (vm, bi0);
1758 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1760 if (is_output_feature)
1761 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1763 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1766 udp0 = ip4_next_header (ip0);
1767 tcp0 = (tcp_header_t *) udp0;
1768 icmp0 = (icmp46_header_t *) udp0;
1770 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1771 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1774 if (PREDICT_FALSE(ip0->ttl == 1))
1776 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1777 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1778 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1780 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1784 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1786 /* Next configured feature, probably ip4-lookup */
1789 if (PREDICT_FALSE (proto0 == ~0))
1791 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
1792 thread_index, now, vm, node);
1794 next0 = SNAT_IN2OUT_NEXT_DROP;
1798 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1800 next0 = icmp_in2out_slow_path
1801 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1802 next0, now, thread_index, &s0);
1808 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1810 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1814 if (ip4_is_fragment (ip0))
1816 next0 = SNAT_IN2OUT_NEXT_REASS;
1821 key0.addr = ip0->src_address;
1822 key0.port = udp0->src_port;
1823 key0.protocol = proto0;
1824 key0.fib_index = rx_fib_index0;
1826 kv0.key = key0.as_u64;
1828 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out,
1833 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1834 ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
1837 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1838 &s0, node, next0, thread_index);
1840 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1845 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1851 if (PREDICT_FALSE (value0.value == ~0ULL))
1855 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
1856 thread_index, now, vm, node);
1858 next0 = SNAT_IN2OUT_NEXT_DROP;
1863 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1869 s0 = pool_elt_at_index (
1870 sm->per_thread_data[thread_index].sessions,
1875 b0->flags |= VNET_BUFFER_F_IS_NATED;
1877 old_addr0 = ip0->src_address.as_u32;
1878 ip0->src_address = s0->out2in.addr;
1879 new_addr0 = ip0->src_address.as_u32;
1880 if (!is_output_feature)
1881 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1883 sum0 = ip0->checksum;
1884 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1886 src_address /* changed member */);
1887 ip0->checksum = ip_csum_fold (sum0);
1889 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1891 old_port0 = tcp0->src_port;
1892 tcp0->src_port = s0->out2in.port;
1893 new_port0 = tcp0->src_port;
1895 sum0 = tcp0->checksum;
1896 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1898 dst_address /* changed member */);
1899 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1900 ip4_header_t /* cheat */,
1901 length /* changed member */);
1902 tcp0->checksum = ip_csum_fold(sum0);
1906 old_port0 = udp0->src_port;
1907 udp0->src_port = s0->out2in.port;
1912 s0->last_heard = now;
1914 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1915 /* Per-user LRU list maintenance */
1916 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1917 s0->per_user_index);
1918 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1919 s0->per_user_list_head_index,
1920 s0->per_user_index);
1923 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1924 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1926 snat_in2out_trace_t *t =
1927 vlib_add_trace (vm, node, b0, sizeof (*t));
1928 t->is_slow_path = is_slow_path;
1929 t->sw_if_index = sw_if_index0;
1930 t->next_index = next0;
1931 t->session_index = ~0;
1933 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1936 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1938 /* verify speculative enqueue, maybe switch current next frame */
1939 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1940 to_next, n_left_to_next,
1944 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1947 vlib_node_increment_counter (vm, stats_node_index,
1948 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1950 return frame->n_vectors;
1954 snat_in2out_fast_path_fn (vlib_main_t * vm,
1955 vlib_node_runtime_t * node,
1956 vlib_frame_t * frame)
1958 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 0);
1961 VLIB_REGISTER_NODE (snat_in2out_node) = {
1962 .function = snat_in2out_fast_path_fn,
1963 .name = "nat44-in2out",
1964 .vector_size = sizeof (u32),
1965 .format_trace = format_snat_in2out_trace,
1966 .type = VLIB_NODE_TYPE_INTERNAL,
1968 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1969 .error_strings = snat_in2out_error_strings,
1971 .runtime_data_bytes = sizeof (snat_runtime_t),
1973 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1975 /* edit / add dispositions here */
1977 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1978 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1979 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1980 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1981 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1985 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1988 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1989 vlib_node_runtime_t * node,
1990 vlib_frame_t * frame)
1992 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 1);
1995 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1996 .function = snat_in2out_output_fast_path_fn,
1997 .name = "nat44-in2out-output",
1998 .vector_size = sizeof (u32),
1999 .format_trace = format_snat_in2out_trace,
2000 .type = VLIB_NODE_TYPE_INTERNAL,
2002 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2003 .error_strings = snat_in2out_error_strings,
2005 .runtime_data_bytes = sizeof (snat_runtime_t),
2007 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2009 /* edit / add dispositions here */
2011 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2012 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2013 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2014 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2015 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2019 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
2020 snat_in2out_output_fast_path_fn);
2023 snat_in2out_slow_path_fn (vlib_main_t * vm,
2024 vlib_node_runtime_t * node,
2025 vlib_frame_t * frame)
2027 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 0);
2030 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
2031 .function = snat_in2out_slow_path_fn,
2032 .name = "nat44-in2out-slowpath",
2033 .vector_size = sizeof (u32),
2034 .format_trace = format_snat_in2out_trace,
2035 .type = VLIB_NODE_TYPE_INTERNAL,
2037 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2038 .error_strings = snat_in2out_error_strings,
2040 .runtime_data_bytes = sizeof (snat_runtime_t),
2042 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2044 /* edit / add dispositions here */
2046 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2047 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2048 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2049 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2050 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2054 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
2055 snat_in2out_slow_path_fn);
2058 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
2059 vlib_node_runtime_t * node,
2060 vlib_frame_t * frame)
2062 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 1);
2065 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
2066 .function = snat_in2out_output_slow_path_fn,
2067 .name = "nat44-in2out-output-slowpath",
2068 .vector_size = sizeof (u32),
2069 .format_trace = format_snat_in2out_trace,
2070 .type = VLIB_NODE_TYPE_INTERNAL,
2072 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2073 .error_strings = snat_in2out_error_strings,
2075 .runtime_data_bytes = sizeof (snat_runtime_t),
2077 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2079 /* edit / add dispositions here */
2081 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2082 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2083 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2084 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2085 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2089 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
2090 snat_in2out_output_slow_path_fn);
2092 extern vnet_feature_arc_registration_t vnet_feat_arc_ip4_local;
2095 nat44_hairpinning_fn (vlib_main_t * vm,
2096 vlib_node_runtime_t * node,
2097 vlib_frame_t * frame)
2099 u32 n_left_from, * from, * to_next;
2100 snat_in2out_next_t next_index;
2101 u32 pkts_processed = 0;
2102 snat_main_t * sm = &snat_main;
2103 vnet_feature_main_t *fm = &feature_main;
2104 u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
2105 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
2107 from = vlib_frame_vector_args (frame);
2108 n_left_from = frame->n_vectors;
2109 next_index = node->cached_next_index;
2111 while (n_left_from > 0)
2115 vlib_get_next_frame (vm, node, next_index,
2116 to_next, n_left_to_next);
2118 while (n_left_from > 0 && n_left_to_next > 0)
2125 udp_header_t * udp0;
2126 tcp_header_t * tcp0;
2128 /* speculatively enqueue b0 to the current next frame */
2134 n_left_to_next -= 1;
2136 b0 = vlib_get_buffer (vm, bi0);
2137 ip0 = vlib_buffer_get_current (b0);
2138 udp0 = ip4_next_header (ip0);
2139 tcp0 = (tcp_header_t *) udp0;
2141 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2143 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
2146 if (snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0))
2147 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2149 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2151 /* verify speculative enqueue, maybe switch current next frame */
2152 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2153 to_next, n_left_to_next,
2157 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2160 vlib_node_increment_counter (vm, nat44_hairpinning_node.index,
2161 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2163 return frame->n_vectors;
2166 VLIB_REGISTER_NODE (nat44_hairpinning_node) = {
2167 .function = nat44_hairpinning_fn,
2168 .name = "nat44-hairpinning",
2169 .vector_size = sizeof (u32),
2170 .type = VLIB_NODE_TYPE_INTERNAL,
2171 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2172 .error_strings = snat_in2out_error_strings,
2175 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2176 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2180 VLIB_NODE_FUNCTION_MULTIARCH (nat44_hairpinning_node,
2181 nat44_hairpinning_fn);
2184 nat44_reass_hairpinning (snat_main_t *sm,
2191 snat_session_key_t key0, sm0;
2192 snat_session_t * s0;
2193 clib_bihash_kv_8_8_t kv0, value0;
2195 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
2196 u16 new_dst_port0, old_dst_port0;
2197 udp_header_t * udp0;
2198 tcp_header_t * tcp0;
2200 key0.addr = ip0->dst_address;
2202 key0.protocol = proto0;
2203 key0.fib_index = sm->outside_fib_index;
2204 kv0.key = key0.as_u64;
2206 udp0 = ip4_next_header (ip0);
2208 /* Check if destination is static mappings */
2209 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0))
2211 new_dst_addr0 = sm0.addr.as_u32;
2212 new_dst_port0 = sm0.port;
2213 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2215 /* or active sessions */
2218 if (sm->num_workers > 1)
2219 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
2221 ti = sm->num_workers;
2223 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
2226 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
2227 new_dst_addr0 = s0->in2out.addr.as_u32;
2228 new_dst_port0 = s0->in2out.port;
2229 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
2233 /* Destination is behind the same NAT, use internal address and port */
2236 old_dst_addr0 = ip0->dst_address.as_u32;
2237 ip0->dst_address.as_u32 = new_dst_addr0;
2238 sum0 = ip0->checksum;
2239 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2240 ip4_header_t, dst_address);
2241 ip0->checksum = ip_csum_fold (sum0);
2243 old_dst_port0 = dport;
2244 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0 &&
2245 ip4_is_first_fragment (ip0)))
2247 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2249 tcp0 = ip4_next_header (ip0);
2250 tcp0->dst = new_dst_port0;
2251 sum0 = tcp0->checksum;
2252 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2253 ip4_header_t, dst_address);
2254 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
2255 ip4_header_t /* cheat */, length);
2256 tcp0->checksum = ip_csum_fold(sum0);
2260 udp0->dst_port = new_dst_port0;
2266 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2268 tcp0 = ip4_next_header (ip0);
2269 sum0 = tcp0->checksum;
2270 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
2271 ip4_header_t, dst_address);
2272 tcp0->checksum = ip_csum_fold(sum0);
2279 nat44_in2out_reass_node_fn (vlib_main_t * vm,
2280 vlib_node_runtime_t * node,
2281 vlib_frame_t * frame)
2283 u32 n_left_from, *from, *to_next;
2284 snat_in2out_next_t next_index;
2285 u32 pkts_processed = 0;
2286 snat_main_t *sm = &snat_main;
2287 f64 now = vlib_time_now (vm);
2288 u32 thread_index = vlib_get_thread_index ();
2289 snat_main_per_thread_data_t *per_thread_data =
2290 &sm->per_thread_data[thread_index];
2291 u32 *fragments_to_drop = 0;
2292 u32 *fragments_to_loopback = 0;
2294 from = vlib_frame_vector_args (frame);
2295 n_left_from = frame->n_vectors;
2296 next_index = node->cached_next_index;
2298 while (n_left_from > 0)
2302 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2304 while (n_left_from > 0 && n_left_to_next > 0)
2306 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
2311 nat_reass_ip4_t *reass0;
2312 udp_header_t * udp0;
2313 tcp_header_t * tcp0;
2314 snat_session_key_t key0;
2315 clib_bihash_kv_8_8_t kv0, value0;
2316 snat_session_t * s0 = 0;
2317 u16 old_port0, new_port0;
2320 /* speculatively enqueue b0 to the current next frame */
2326 n_left_to_next -= 1;
2328 b0 = vlib_get_buffer (vm, bi0);
2329 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2331 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2332 rx_fib_index0 = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2335 if (PREDICT_FALSE (nat_reass_is_drop_frag(0)))
2337 next0 = SNAT_IN2OUT_NEXT_DROP;
2338 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
2342 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
2343 udp0 = ip4_next_header (ip0);
2344 tcp0 = (tcp_header_t *) udp0;
2345 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2347 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
2352 &fragments_to_drop);
2354 if (PREDICT_FALSE (!reass0))
2356 next0 = SNAT_IN2OUT_NEXT_DROP;
2357 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
2361 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2363 key0.addr = ip0->src_address;
2364 key0.port = udp0->src_port;
2365 key0.protocol = proto0;
2366 key0.fib_index = rx_fib_index0;
2367 kv0.key = key0.as_u64;
2369 if (clib_bihash_search_8_8 (&per_thread_data->in2out, &kv0, &value0))
2371 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
2372 ip0, proto0, rx_fib_index0, thread_index)))
2375 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
2376 &s0, node, next0, thread_index);
2378 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
2381 reass0->sess_index = s0 - per_thread_data->sessions;
2385 s0 = pool_elt_at_index (per_thread_data->sessions,
2387 reass0->sess_index = value0.value;
2389 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
2393 if (PREDICT_FALSE (reass0->sess_index == (u32) ~0))
2395 if (nat_ip4_reass_add_fragment (reass0, bi0))
2397 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
2398 next0 = SNAT_IN2OUT_NEXT_DROP;
2404 s0 = pool_elt_at_index (per_thread_data->sessions,
2405 reass0->sess_index);
2408 old_addr0 = ip0->src_address.as_u32;
2409 ip0->src_address = s0->out2in.addr;
2410 new_addr0 = ip0->src_address.as_u32;
2411 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
2413 sum0 = ip0->checksum;
2414 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2416 src_address /* changed member */);
2417 ip0->checksum = ip_csum_fold (sum0);
2419 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
2421 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2423 old_port0 = tcp0->src_port;
2424 tcp0->src_port = s0->out2in.port;
2425 new_port0 = tcp0->src_port;
2427 sum0 = tcp0->checksum;
2428 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2430 dst_address /* changed member */);
2431 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2432 ip4_header_t /* cheat */,
2433 length /* changed member */);
2434 tcp0->checksum = ip_csum_fold(sum0);
2438 old_port0 = udp0->src_port;
2439 udp0->src_port = s0->out2in.port;
2445 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2446 s0->ext_host_port, proto0);
2449 s0->last_heard = now;
2451 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
2452 /* Per-user LRU list maintenance */
2453 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
2454 s0->per_user_index);
2455 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
2456 s0->per_user_list_head_index,
2457 s0->per_user_index);
2460 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2461 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2463 nat44_in2out_reass_trace_t *t =
2464 vlib_add_trace (vm, node, b0, sizeof (*t));
2465 t->cached = cached0;
2466 t->sw_if_index = sw_if_index0;
2467 t->next_index = next0;
2477 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2479 /* verify speculative enqueue, maybe switch current next frame */
2480 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2481 to_next, n_left_to_next,
2485 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2487 from = vlib_frame_vector_args (frame);
2488 u32 len = vec_len (fragments_to_loopback);
2489 if (len <= VLIB_FRAME_SIZE)
2491 clib_memcpy (from, fragments_to_loopback, sizeof (u32) * len);
2493 vec_reset_length (fragments_to_loopback);
2498 fragments_to_loopback + (len - VLIB_FRAME_SIZE),
2499 sizeof (u32) * VLIB_FRAME_SIZE);
2500 n_left_from = VLIB_FRAME_SIZE;
2501 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2506 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2509 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
2510 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2513 nat_send_all_to_node (vm, fragments_to_drop, node,
2514 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
2515 SNAT_IN2OUT_NEXT_DROP);
2517 vec_free (fragments_to_drop);
2518 vec_free (fragments_to_loopback);
2519 return frame->n_vectors;
2522 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
2523 .function = nat44_in2out_reass_node_fn,
2524 .name = "nat44-in2out-reass",
2525 .vector_size = sizeof (u32),
2526 .format_trace = format_nat44_in2out_reass_trace,
2527 .type = VLIB_NODE_TYPE_INTERNAL,
2529 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2530 .error_strings = snat_in2out_error_strings,
2532 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2534 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2535 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2536 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2537 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2538 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2542 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2543 nat44_in2out_reass_node_fn);
2545 /**************************/
2546 /*** deterministic mode ***/
2547 /**************************/
2549 snat_det_in2out_node_fn (vlib_main_t * vm,
2550 vlib_node_runtime_t * node,
2551 vlib_frame_t * frame)
2553 u32 n_left_from, * from, * to_next;
2554 snat_in2out_next_t next_index;
2555 u32 pkts_processed = 0;
2556 snat_main_t * sm = &snat_main;
2557 u32 now = (u32) vlib_time_now (vm);
2558 u32 thread_index = vlib_get_thread_index ();
2560 from = vlib_frame_vector_args (frame);
2561 n_left_from = frame->n_vectors;
2562 next_index = node->cached_next_index;
2564 while (n_left_from > 0)
2568 vlib_get_next_frame (vm, node, next_index,
2569 to_next, n_left_to_next);
2571 while (n_left_from >= 4 && n_left_to_next >= 2)
2574 vlib_buffer_t * b0, * b1;
2576 u32 sw_if_index0, sw_if_index1;
2577 ip4_header_t * ip0, * ip1;
2578 ip_csum_t sum0, sum1;
2579 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
2580 u16 old_port0, new_port0, lo_port0, i0;
2581 u16 old_port1, new_port1, lo_port1, i1;
2582 udp_header_t * udp0, * udp1;
2583 tcp_header_t * tcp0, * tcp1;
2585 snat_det_out_key_t key0, key1;
2586 snat_det_map_t * dm0, * dm1;
2587 snat_det_session_t * ses0 = 0, * ses1 = 0;
2588 u32 rx_fib_index0, rx_fib_index1;
2589 icmp46_header_t * icmp0, * icmp1;
2591 /* Prefetch next iteration. */
2593 vlib_buffer_t * p2, * p3;
2595 p2 = vlib_get_buffer (vm, from[2]);
2596 p3 = vlib_get_buffer (vm, from[3]);
2598 vlib_prefetch_buffer_header (p2, LOAD);
2599 vlib_prefetch_buffer_header (p3, LOAD);
2601 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
2602 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
2605 /* speculatively enqueue b0 and b1 to the current next frame */
2606 to_next[0] = bi0 = from[0];
2607 to_next[1] = bi1 = from[1];
2611 n_left_to_next -= 2;
2613 b0 = vlib_get_buffer (vm, bi0);
2614 b1 = vlib_get_buffer (vm, bi1);
2616 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2617 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
2619 ip0 = vlib_buffer_get_current (b0);
2620 udp0 = ip4_next_header (ip0);
2621 tcp0 = (tcp_header_t *) udp0;
2623 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2625 if (PREDICT_FALSE(ip0->ttl == 1))
2627 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2628 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2629 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2631 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2635 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2637 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2639 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2640 icmp0 = (icmp46_header_t *) udp0;
2642 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2643 rx_fib_index0, node, next0, thread_index,
2648 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2649 if (PREDICT_FALSE(!dm0))
2651 clib_warning("no match for internal host %U",
2652 format_ip4_address, &ip0->src_address);
2653 next0 = SNAT_IN2OUT_NEXT_DROP;
2654 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2658 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2660 key0.ext_host_addr = ip0->dst_address;
2661 key0.ext_host_port = tcp0->dst;
2663 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
2664 if (PREDICT_FALSE(!ses0))
2666 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2668 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2669 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
2671 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
2674 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
2677 if (PREDICT_FALSE(!ses0))
2679 /* too many sessions for user, send ICMP error packet */
2681 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2682 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
2683 ICMP4_destination_unreachable_destination_unreachable_host,
2685 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2690 new_port0 = ses0->out.out_port;
2692 old_addr0.as_u32 = ip0->src_address.as_u32;
2693 ip0->src_address.as_u32 = new_addr0.as_u32;
2694 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2696 sum0 = ip0->checksum;
2697 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2699 src_address /* changed member */);
2700 ip0->checksum = ip_csum_fold (sum0);
2702 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2704 if (tcp0->flags & TCP_FLAG_SYN)
2705 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
2706 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
2707 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2708 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2709 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
2710 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
2711 snat_det_ses_close(dm0, ses0);
2712 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2713 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
2714 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
2715 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2717 old_port0 = tcp0->src;
2718 tcp0->src = new_port0;
2720 sum0 = tcp0->checksum;
2721 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2723 dst_address /* changed member */);
2724 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2725 ip4_header_t /* cheat */,
2726 length /* changed member */);
2727 tcp0->checksum = ip_csum_fold(sum0);
2731 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2732 old_port0 = udp0->src_port;
2733 udp0->src_port = new_port0;
2739 case SNAT_SESSION_UDP_ACTIVE:
2740 ses0->expire = now + sm->udp_timeout;
2742 case SNAT_SESSION_TCP_SYN_SENT:
2743 case SNAT_SESSION_TCP_FIN_WAIT:
2744 case SNAT_SESSION_TCP_CLOSE_WAIT:
2745 case SNAT_SESSION_TCP_LAST_ACK:
2746 ses0->expire = now + sm->tcp_transitory_timeout;
2748 case SNAT_SESSION_TCP_ESTABLISHED:
2749 ses0->expire = now + sm->tcp_established_timeout;
2754 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2755 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2757 snat_in2out_trace_t *t =
2758 vlib_add_trace (vm, node, b0, sizeof (*t));
2759 t->is_slow_path = 0;
2760 t->sw_if_index = sw_if_index0;
2761 t->next_index = next0;
2762 t->session_index = ~0;
2764 t->session_index = ses0 - dm0->sessions;
2767 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2769 ip1 = vlib_buffer_get_current (b1);
2770 udp1 = ip4_next_header (ip1);
2771 tcp1 = (tcp_header_t *) udp1;
2773 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
2775 if (PREDICT_FALSE(ip1->ttl == 1))
2777 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2778 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
2779 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2781 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2785 proto1 = ip_proto_to_snat_proto (ip1->protocol);
2787 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
2789 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
2790 icmp1 = (icmp46_header_t *) udp1;
2792 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
2793 rx_fib_index1, node, next1, thread_index,
2798 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
2799 if (PREDICT_FALSE(!dm1))
2801 clib_warning("no match for internal host %U",
2802 format_ip4_address, &ip0->src_address);
2803 next1 = SNAT_IN2OUT_NEXT_DROP;
2804 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2808 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
2810 key1.ext_host_addr = ip1->dst_address;
2811 key1.ext_host_port = tcp1->dst;
2813 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src, key1);
2814 if (PREDICT_FALSE(!ses1))
2816 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
2818 key1.out_port = clib_host_to_net_u16 (lo_port1 +
2819 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
2821 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
2824 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
2827 if (PREDICT_FALSE(!ses1))
2829 /* too many sessions for user, send ICMP error packet */
2831 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2832 icmp4_error_set_vnet_buffer (b1, ICMP4_destination_unreachable,
2833 ICMP4_destination_unreachable_destination_unreachable_host,
2835 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2840 new_port1 = ses1->out.out_port;
2842 old_addr1.as_u32 = ip1->src_address.as_u32;
2843 ip1->src_address.as_u32 = new_addr1.as_u32;
2844 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2846 sum1 = ip1->checksum;
2847 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2849 src_address /* changed member */);
2850 ip1->checksum = ip_csum_fold (sum1);
2852 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
2854 if (tcp1->flags & TCP_FLAG_SYN)
2855 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
2856 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
2857 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
2858 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
2859 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
2860 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
2861 snat_det_ses_close(dm1, ses1);
2862 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2863 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
2864 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
2865 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
2867 old_port1 = tcp1->src;
2868 tcp1->src = new_port1;
2870 sum1 = tcp1->checksum;
2871 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2873 dst_address /* changed member */);
2874 sum1 = ip_csum_update (sum1, old_port1, new_port1,
2875 ip4_header_t /* cheat */,
2876 length /* changed member */);
2877 tcp1->checksum = ip_csum_fold(sum1);
2881 ses1->state = SNAT_SESSION_UDP_ACTIVE;
2882 old_port1 = udp1->src_port;
2883 udp1->src_port = new_port1;
2889 case SNAT_SESSION_UDP_ACTIVE:
2890 ses1->expire = now + sm->udp_timeout;
2892 case SNAT_SESSION_TCP_SYN_SENT:
2893 case SNAT_SESSION_TCP_FIN_WAIT:
2894 case SNAT_SESSION_TCP_CLOSE_WAIT:
2895 case SNAT_SESSION_TCP_LAST_ACK:
2896 ses1->expire = now + sm->tcp_transitory_timeout;
2898 case SNAT_SESSION_TCP_ESTABLISHED:
2899 ses1->expire = now + sm->tcp_established_timeout;
2904 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2905 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
2907 snat_in2out_trace_t *t =
2908 vlib_add_trace (vm, node, b1, sizeof (*t));
2909 t->is_slow_path = 0;
2910 t->sw_if_index = sw_if_index1;
2911 t->next_index = next1;
2912 t->session_index = ~0;
2914 t->session_index = ses1 - dm1->sessions;
2917 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
2919 /* verify speculative enqueues, maybe switch current next frame */
2920 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2921 to_next, n_left_to_next,
2922 bi0, bi1, next0, next1);
2925 while (n_left_from > 0 && n_left_to_next > 0)
2933 ip4_address_t new_addr0, old_addr0;
2934 u16 old_port0, new_port0, lo_port0, i0;
2935 udp_header_t * udp0;
2936 tcp_header_t * tcp0;
2938 snat_det_out_key_t key0;
2939 snat_det_map_t * dm0;
2940 snat_det_session_t * ses0 = 0;
2942 icmp46_header_t * icmp0;
2944 /* speculatively enqueue b0 to the current next frame */
2950 n_left_to_next -= 1;
2952 b0 = vlib_get_buffer (vm, bi0);
2953 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2955 ip0 = vlib_buffer_get_current (b0);
2956 udp0 = ip4_next_header (ip0);
2957 tcp0 = (tcp_header_t *) udp0;
2959 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2961 if (PREDICT_FALSE(ip0->ttl == 1))
2963 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2964 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2965 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2967 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2971 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2973 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2975 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2976 icmp0 = (icmp46_header_t *) udp0;
2978 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2979 rx_fib_index0, node, next0, thread_index,
2984 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2985 if (PREDICT_FALSE(!dm0))
2987 clib_warning("no match for internal host %U",
2988 format_ip4_address, &ip0->src_address);
2989 next0 = SNAT_IN2OUT_NEXT_DROP;
2990 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2994 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2996 key0.ext_host_addr = ip0->dst_address;
2997 key0.ext_host_port = tcp0->dst;
2999 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
3000 if (PREDICT_FALSE(!ses0))
3002 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
3004 key0.out_port = clib_host_to_net_u16 (lo_port0 +
3005 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
3007 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
3010 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
3013 if (PREDICT_FALSE(!ses0))
3015 /* too many sessions for user, send ICMP error packet */
3017 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3018 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
3019 ICMP4_destination_unreachable_destination_unreachable_host,
3021 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3026 new_port0 = ses0->out.out_port;
3028 old_addr0.as_u32 = ip0->src_address.as_u32;
3029 ip0->src_address.as_u32 = new_addr0.as_u32;
3030 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
3032 sum0 = ip0->checksum;
3033 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
3035 src_address /* changed member */);
3036 ip0->checksum = ip_csum_fold (sum0);
3038 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3040 if (tcp0->flags & TCP_FLAG_SYN)
3041 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
3042 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
3043 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
3044 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
3045 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
3046 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
3047 snat_det_ses_close(dm0, ses0);
3048 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
3049 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
3050 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
3051 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
3053 old_port0 = tcp0->src;
3054 tcp0->src = new_port0;
3056 sum0 = tcp0->checksum;
3057 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
3059 dst_address /* changed member */);
3060 sum0 = ip_csum_update (sum0, old_port0, new_port0,
3061 ip4_header_t /* cheat */,
3062 length /* changed member */);
3063 tcp0->checksum = ip_csum_fold(sum0);
3067 ses0->state = SNAT_SESSION_UDP_ACTIVE;
3068 old_port0 = udp0->src_port;
3069 udp0->src_port = new_port0;
3075 case SNAT_SESSION_UDP_ACTIVE:
3076 ses0->expire = now + sm->udp_timeout;
3078 case SNAT_SESSION_TCP_SYN_SENT:
3079 case SNAT_SESSION_TCP_FIN_WAIT:
3080 case SNAT_SESSION_TCP_CLOSE_WAIT:
3081 case SNAT_SESSION_TCP_LAST_ACK:
3082 ses0->expire = now + sm->tcp_transitory_timeout;
3084 case SNAT_SESSION_TCP_ESTABLISHED:
3085 ses0->expire = now + sm->tcp_established_timeout;
3090 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3091 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3093 snat_in2out_trace_t *t =
3094 vlib_add_trace (vm, node, b0, sizeof (*t));
3095 t->is_slow_path = 0;
3096 t->sw_if_index = sw_if_index0;
3097 t->next_index = next0;
3098 t->session_index = ~0;
3100 t->session_index = ses0 - dm0->sessions;
3103 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3105 /* verify speculative enqueue, maybe switch current next frame */
3106 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3107 to_next, n_left_to_next,
3111 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3114 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
3115 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3117 return frame->n_vectors;
3120 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
3121 .function = snat_det_in2out_node_fn,
3122 .name = "nat44-det-in2out",
3123 .vector_size = sizeof (u32),
3124 .format_trace = format_snat_in2out_trace,
3125 .type = VLIB_NODE_TYPE_INTERNAL,
3127 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3128 .error_strings = snat_in2out_error_strings,
3130 .runtime_data_bytes = sizeof (snat_runtime_t),
3134 /* edit / add dispositions here */
3136 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3137 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3138 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3142 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
3145 * Get address and port values to be used for ICMP packet translation
3146 * and create session if needed
3148 * @param[in,out] sm NAT main
3149 * @param[in,out] node NAT node runtime
3150 * @param[in] thread_index thread index
3151 * @param[in,out] b0 buffer containing packet to be translated
3152 * @param[out] p_proto protocol used for matching
3153 * @param[out] p_value address and port after NAT translation
3154 * @param[out] p_dont_translate if packet should not be translated
3155 * @param d optional parameter
3156 * @param e optional parameter
3158 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
3159 u32 thread_index, vlib_buffer_t *b0,
3160 ip4_header_t *ip0, u8 *p_proto,
3161 snat_session_key_t *p_value,
3162 u8 *p_dont_translate, void *d, void *e)
3164 icmp46_header_t *icmp0;
3168 snat_det_out_key_t key0;
3169 u8 dont_translate = 0;
3171 icmp_echo_header_t *echo0, *inner_echo0 = 0;
3172 ip4_header_t *inner_ip0;
3173 void *l4_header = 0;
3174 icmp46_header_t *inner_icmp0;
3175 snat_det_map_t * dm0 = 0;
3176 ip4_address_t new_addr0;
3178 snat_det_session_t * ses0 = 0;
3179 ip4_address_t in_addr;
3182 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
3183 echo0 = (icmp_echo_header_t *)(icmp0+1);
3184 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3185 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
3187 if (!icmp_is_error_message (icmp0))
3189 protocol = SNAT_PROTOCOL_ICMP;
3190 in_addr = ip0->src_address;
3191 in_port = echo0->identifier;
3195 inner_ip0 = (ip4_header_t *)(echo0+1);
3196 l4_header = ip4_next_header (inner_ip0);
3197 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
3198 in_addr = inner_ip0->dst_address;
3201 case SNAT_PROTOCOL_ICMP:
3202 inner_icmp0 = (icmp46_header_t*)l4_header;
3203 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
3204 in_port = inner_echo0->identifier;
3206 case SNAT_PROTOCOL_UDP:
3207 case SNAT_PROTOCOL_TCP:
3208 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
3211 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
3212 next0 = SNAT_IN2OUT_NEXT_DROP;
3217 dm0 = snat_det_map_by_user(sm, &in_addr);
3218 if (PREDICT_FALSE(!dm0))
3220 clib_warning("no match for internal host %U",
3221 format_ip4_address, &in_addr);
3222 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3223 IP_PROTOCOL_ICMP, rx_fib_index0)))
3228 next0 = SNAT_IN2OUT_NEXT_DROP;
3229 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3233 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
3235 key0.ext_host_addr = ip0->dst_address;
3236 key0.ext_host_port = 0;
3238 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port, key0);
3239 if (PREDICT_FALSE(!ses0))
3241 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3242 IP_PROTOCOL_ICMP, rx_fib_index0)))
3247 if (icmp0->type != ICMP4_echo_request)
3249 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3250 next0 = SNAT_IN2OUT_NEXT_DROP;
3253 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
3255 key0.out_port = clib_host_to_net_u16 (lo_port0 +
3256 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
3258 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
3261 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
3264 if (PREDICT_FALSE(!ses0))
3266 next0 = SNAT_IN2OUT_NEXT_DROP;
3267 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
3272 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
3273 !icmp_is_error_message (icmp0)))
3275 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3276 next0 = SNAT_IN2OUT_NEXT_DROP;
3280 u32 now = (u32) vlib_time_now (sm->vlib_main);
3282 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
3283 ses0->expire = now + sm->icmp_timeout;
3286 *p_proto = protocol;
3289 p_value->addr = new_addr0;
3290 p_value->fib_index = sm->outside_fib_index;
3291 p_value->port = ses0->out.out_port;
3293 *p_dont_translate = dont_translate;
3295 *(snat_det_session_t**)d = ses0;
3297 *(snat_det_map_t**)e = dm0;
3301 /**********************/
3302 /*** worker handoff ***/
3303 /**********************/
3305 snat_in2out_worker_handoff_fn_inline (vlib_main_t * vm,
3306 vlib_node_runtime_t * node,
3307 vlib_frame_t * frame,
3310 snat_main_t *sm = &snat_main;
3311 vlib_thread_main_t *tm = vlib_get_thread_main ();
3312 u32 n_left_from, *from, *to_next = 0;
3313 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
3314 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
3316 vlib_frame_queue_elt_t *hf = 0;
3317 vlib_frame_t *f = 0;
3319 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
3320 u32 next_worker_index = 0;
3321 u32 current_worker_index = ~0;
3322 u32 thread_index = vlib_get_thread_index ();
3326 ASSERT (vec_len (sm->workers));
3330 fq_index = sm->fq_in2out_output_index;
3331 to_node_index = sm->in2out_output_node_index;
3335 fq_index = sm->fq_in2out_index;
3336 to_node_index = sm->in2out_node_index;
3339 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
3341 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
3343 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
3344 sm->first_worker_index + sm->num_workers - 1,
3345 (vlib_frame_queue_t *) (~0));
3348 from = vlib_frame_vector_args (frame);
3349 n_left_from = frame->n_vectors;
3351 while (n_left_from > 0)
3364 b0 = vlib_get_buffer (vm, bi0);
3366 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3367 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3369 ip0 = vlib_buffer_get_current (b0);
3371 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
3373 if (PREDICT_FALSE (next_worker_index != thread_index))
3377 if (next_worker_index != current_worker_index)
3380 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3382 hf = vlib_get_worker_handoff_queue_elt (fq_index,
3384 handoff_queue_elt_by_worker_index);
3386 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
3387 to_next_worker = &hf->buffer_index[hf->n_vectors];
3388 current_worker_index = next_worker_index;
3391 /* enqueue to correct worker thread */
3392 to_next_worker[0] = bi0;
3394 n_left_to_next_worker--;
3396 if (n_left_to_next_worker == 0)
3398 hf->n_vectors = VLIB_FRAME_SIZE;
3399 vlib_put_frame_queue_elt (hf);
3400 current_worker_index = ~0;
3401 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
3408 /* if this is 1st frame */
3411 f = vlib_get_frame_to_node (vm, to_node_index);
3412 to_next = vlib_frame_vector_args (f);
3420 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
3421 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3423 snat_in2out_worker_handoff_trace_t *t =
3424 vlib_add_trace (vm, node, b0, sizeof (*t));
3425 t->next_worker_index = next_worker_index;
3426 t->do_handoff = do_handoff;
3431 vlib_put_frame_to_node (vm, to_node_index, f);
3434 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3436 /* Ship frames to the worker nodes */
3437 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
3439 if (handoff_queue_elt_by_worker_index[i])
3441 hf = handoff_queue_elt_by_worker_index[i];
3443 * It works better to let the handoff node
3444 * rate-adapt, always ship the handoff queue element.
3446 if (1 || hf->n_vectors == hf->last_n_vectors)
3448 vlib_put_frame_queue_elt (hf);
3449 handoff_queue_elt_by_worker_index[i] = 0;
3452 hf->last_n_vectors = hf->n_vectors;
3454 congested_handoff_queue_by_worker_index[i] =
3455 (vlib_frame_queue_t *) (~0);
3458 current_worker_index = ~0;
3459 return frame->n_vectors;
3463 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
3464 vlib_node_runtime_t * node,
3465 vlib_frame_t * frame)
3467 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 0);
3470 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
3471 .function = snat_in2out_worker_handoff_fn,
3472 .name = "nat44-in2out-worker-handoff",
3473 .vector_size = sizeof (u32),
3474 .format_trace = format_snat_in2out_worker_handoff_trace,
3475 .type = VLIB_NODE_TYPE_INTERNAL,
3484 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
3485 snat_in2out_worker_handoff_fn);
3488 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
3489 vlib_node_runtime_t * node,
3490 vlib_frame_t * frame)
3492 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 1);
3495 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
3496 .function = snat_in2out_output_worker_handoff_fn,
3497 .name = "nat44-in2out-output-worker-handoff",
3498 .vector_size = sizeof (u32),
3499 .format_trace = format_snat_in2out_worker_handoff_trace,
3500 .type = VLIB_NODE_TYPE_INTERNAL,
3509 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
3510 snat_in2out_output_worker_handoff_fn);
3512 static_always_inline int
3513 is_hairpinning (snat_main_t *sm, ip4_address_t * dst_addr)
3515 snat_address_t * ap;
3516 clib_bihash_kv_8_8_t kv, value;
3517 snat_session_key_t m_key;
3519 vec_foreach (ap, sm->addresses)
3521 if (ap->addr.as_u32 == dst_addr->as_u32)
3525 m_key.addr.as_u32 = dst_addr->as_u32;
3526 m_key.fib_index = sm->outside_fib_index;
3529 kv.key = m_key.as_u64;
3530 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3537 snat_hairpin_dst_fn (vlib_main_t * vm,
3538 vlib_node_runtime_t * node,
3539 vlib_frame_t * frame)
3541 u32 n_left_from, * from, * to_next;
3542 snat_in2out_next_t next_index;
3543 u32 pkts_processed = 0;
3544 snat_main_t * sm = &snat_main;
3546 from = vlib_frame_vector_args (frame);
3547 n_left_from = frame->n_vectors;
3548 next_index = node->cached_next_index;
3550 while (n_left_from > 0)
3554 vlib_get_next_frame (vm, node, next_index,
3555 to_next, n_left_to_next);
3557 while (n_left_from > 0 && n_left_to_next > 0)
3565 /* speculatively enqueue b0 to the current next frame */
3571 n_left_to_next -= 1;
3573 b0 = vlib_get_buffer (vm, bi0);
3574 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3575 ip0 = vlib_buffer_get_current (b0);
3577 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3579 vnet_buffer (b0)->snat.flags = 0;
3580 if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
3582 if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
3584 udp_header_t * udp0 = ip4_next_header (ip0);
3585 tcp_header_t * tcp0 = (tcp_header_t *) udp0;
3587 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
3589 else if (proto0 == SNAT_PROTOCOL_ICMP)
3591 icmp46_header_t * icmp0 = ip4_next_header (ip0);
3593 snat_icmp_hairpinning (sm, b0, ip0, icmp0);
3597 snat_hairpinning_unknown_proto (sm, b0, ip0);
3600 vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
3603 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3605 /* verify speculative enqueue, maybe switch current next frame */
3606 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3607 to_next, n_left_to_next,
3611 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3614 vlib_node_increment_counter (vm, snat_hairpin_dst_node.index,
3615 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3617 return frame->n_vectors;
3620 VLIB_REGISTER_NODE (snat_hairpin_dst_node) = {
3621 .function = snat_hairpin_dst_fn,
3622 .name = "nat44-hairpin-dst",
3623 .vector_size = sizeof (u32),
3624 .type = VLIB_NODE_TYPE_INTERNAL,
3625 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3626 .error_strings = snat_in2out_error_strings,
3629 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3630 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3634 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_dst_node,
3635 snat_hairpin_dst_fn);
3638 snat_hairpin_src_fn (vlib_main_t * vm,
3639 vlib_node_runtime_t * node,
3640 vlib_frame_t * frame)
3642 u32 n_left_from, * from, * to_next;
3643 snat_in2out_next_t next_index;
3644 u32 pkts_processed = 0;
3645 snat_main_t *sm = &snat_main;
3647 from = vlib_frame_vector_args (frame);
3648 n_left_from = frame->n_vectors;
3649 next_index = node->cached_next_index;
3651 while (n_left_from > 0)
3655 vlib_get_next_frame (vm, node, next_index,
3656 to_next, n_left_to_next);
3658 while (n_left_from > 0 && n_left_to_next > 0)
3663 snat_interface_t *i;
3666 /* speculatively enqueue b0 to the current next frame */
3672 n_left_to_next -= 1;
3674 b0 = vlib_get_buffer (vm, bi0);
3675 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3676 next0 = SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT;
3678 pool_foreach (i, sm->output_feature_interfaces,
3680 /* Only packets from NAT inside interface */
3681 if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
3683 if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
3684 SNAT_FLAG_HAIRPINNING))
3686 if (PREDICT_TRUE (sm->num_workers > 1))
3687 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
3689 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
3695 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3697 /* verify speculative enqueue, maybe switch current next frame */
3698 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3699 to_next, n_left_to_next,
3703 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3706 vlib_node_increment_counter (vm, snat_hairpin_src_node.index,
3707 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3709 return frame->n_vectors;
3712 VLIB_REGISTER_NODE (snat_hairpin_src_node) = {
3713 .function = snat_hairpin_src_fn,
3714 .name = "nat44-hairpin-src",
3715 .vector_size = sizeof (u32),
3716 .type = VLIB_NODE_TYPE_INTERNAL,
3717 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3718 .error_strings = snat_in2out_error_strings,
3719 .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
3721 [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
3722 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
3723 [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
3724 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
3728 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_src_node,
3729 snat_hairpin_src_fn);
3732 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
3733 vlib_node_runtime_t * node,
3734 vlib_frame_t * frame)
3736 u32 n_left_from, * from, * to_next;
3737 snat_in2out_next_t next_index;
3738 u32 pkts_processed = 0;
3739 snat_main_t * sm = &snat_main;
3740 u32 stats_node_index;
3742 stats_node_index = snat_in2out_fast_node.index;
3744 from = vlib_frame_vector_args (frame);
3745 n_left_from = frame->n_vectors;
3746 next_index = node->cached_next_index;
3748 while (n_left_from > 0)
3752 vlib_get_next_frame (vm, node, next_index,
3753 to_next, n_left_to_next);
3755 while (n_left_from > 0 && n_left_to_next > 0)
3763 u32 new_addr0, old_addr0;
3764 u16 old_port0, new_port0;
3765 udp_header_t * udp0;
3766 tcp_header_t * tcp0;
3767 icmp46_header_t * icmp0;
3768 snat_session_key_t key0, sm0;
3772 /* speculatively enqueue b0 to the current next frame */
3778 n_left_to_next -= 1;
3780 b0 = vlib_get_buffer (vm, bi0);
3781 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3783 ip0 = vlib_buffer_get_current (b0);
3784 udp0 = ip4_next_header (ip0);
3785 tcp0 = (tcp_header_t *) udp0;
3786 icmp0 = (icmp46_header_t *) udp0;
3788 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3789 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3791 if (PREDICT_FALSE(ip0->ttl == 1))
3793 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3794 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
3795 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3797 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3801 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3803 if (PREDICT_FALSE (proto0 == ~0))
3806 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
3808 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
3809 rx_fib_index0, node, next0, ~0, 0, 0);
3813 key0.addr = ip0->src_address;
3814 key0.protocol = proto0;
3815 key0.port = udp0->src_port;
3816 key0.fib_index = rx_fib_index0;
3818 if (snat_static_mapping_match(sm, key0, &sm0, 0, 0, 0))
3820 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3821 next0= SNAT_IN2OUT_NEXT_DROP;
3825 new_addr0 = sm0.addr.as_u32;
3826 new_port0 = sm0.port;
3827 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
3828 old_addr0 = ip0->src_address.as_u32;
3829 ip0->src_address.as_u32 = new_addr0;
3831 sum0 = ip0->checksum;
3832 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3834 src_address /* changed member */);
3835 ip0->checksum = ip_csum_fold (sum0);
3837 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
3839 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3841 old_port0 = tcp0->src_port;
3842 tcp0->src_port = new_port0;
3844 sum0 = tcp0->checksum;
3845 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3847 dst_address /* changed member */);
3848 sum0 = ip_csum_update (sum0, old_port0, new_port0,
3849 ip4_header_t /* cheat */,
3850 length /* changed member */);
3851 tcp0->checksum = ip_csum_fold(sum0);
3855 old_port0 = udp0->src_port;
3856 udp0->src_port = new_port0;
3862 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3864 sum0 = tcp0->checksum;
3865 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3867 dst_address /* changed member */);
3868 tcp0->checksum = ip_csum_fold(sum0);
3873 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
3876 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3877 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3879 snat_in2out_trace_t *t =
3880 vlib_add_trace (vm, node, b0, sizeof (*t));
3881 t->sw_if_index = sw_if_index0;
3882 t->next_index = next0;
3885 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3887 /* verify speculative enqueue, maybe switch current next frame */
3888 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3889 to_next, n_left_to_next,
3893 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3896 vlib_node_increment_counter (vm, stats_node_index,
3897 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3899 return frame->n_vectors;
3903 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
3904 .function = snat_in2out_fast_static_map_fn,
3905 .name = "nat44-in2out-fast",
3906 .vector_size = sizeof (u32),
3907 .format_trace = format_snat_in2out_fast_trace,
3908 .type = VLIB_NODE_TYPE_INTERNAL,
3910 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3911 .error_strings = snat_in2out_error_strings,
3913 .runtime_data_bytes = sizeof (snat_runtime_t),
3915 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3917 /* edit / add dispositions here */
3919 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3920 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3921 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
3922 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3923 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
3927 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);