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>
24 #include <snat/snat.h>
25 #include <snat/snat_ipfix_logging.h>
26 #include <snat/snat_det.h>
28 #include <vppinfra/hash.h>
29 #include <vppinfra/error.h>
30 #include <vppinfra/elog.h>
37 } snat_in2out_trace_t;
40 u32 next_worker_index;
42 } snat_in2out_worker_handoff_trace_t;
44 /* packet trace format function */
45 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
52 tag = t->is_slow_path ? "SNAT_IN2OUT_SLOW_PATH" : "SNAT_IN2OUT_FAST_PATH";
54 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
55 t->sw_if_index, t->next_index, t->session_index);
60 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
66 s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
67 t->sw_if_index, t->next_index);
72 static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args)
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 snat_in2out_worker_handoff_trace_t * t =
77 va_arg (*args, snat_in2out_worker_handoff_trace_t *);
80 m = t->do_handoff ? "next worker" : "same worker";
81 s = format (s, "SNAT_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
86 vlib_node_registration_t snat_in2out_node;
87 vlib_node_registration_t snat_in2out_slowpath_node;
88 vlib_node_registration_t snat_in2out_fast_node;
89 vlib_node_registration_t snat_in2out_worker_handoff_node;
90 vlib_node_registration_t snat_det_in2out_node;
92 #define foreach_snat_in2out_error \
93 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
94 _(IN2OUT_PACKETS, "Good in2out packets processed") \
95 _(OUT_OF_PORTS, "Out of ports") \
96 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
97 _(BAD_ICMP_TYPE, "icmp type not echo-request") \
98 _(NO_TRANSLATION, "No translation")
101 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
102 foreach_snat_in2out_error
105 } snat_in2out_error_t;
107 static char * snat_in2out_error_strings[] = {
108 #define _(sym,string) string,
109 foreach_snat_in2out_error
114 SNAT_IN2OUT_NEXT_LOOKUP,
115 SNAT_IN2OUT_NEXT_DROP,
116 SNAT_IN2OUT_NEXT_ICMP_ERROR,
117 SNAT_IN2OUT_NEXT_SLOW_PATH,
119 } snat_in2out_next_t;
122 * @brief Check if packet should be translated
124 * Packets aimed at outside interface and external addresss with active session
125 * should be translated.
127 * @param sm SNAT main
128 * @param rt SNAT runtime data
129 * @param sw_if_index0 index of the inside interface
130 * @param ip0 IPv4 header
131 * @param proto0 SNAT protocol
132 * @param rx_fib_index0 RX FIB index
134 * @returns 0 if packet should be translated otherwise 1
137 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
138 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
141 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
143 .fp_proto = FIB_PROTOCOL_IP4,
146 .ip4.as_u32 = ip0->dst_address.as_u32,
150 /* Don't NAT packet aimed at the intfc address */
151 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
152 ip0->dst_address.as_u32)))
155 fei = fib_table_lookup (rx_fib_index0, &pfx);
156 if (FIB_NODE_INDEX_INVALID != fei)
158 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
159 if (sw_if_index == ~0)
161 fei = fib_table_lookup (sm->outside_fib_index, &pfx);
162 if (FIB_NODE_INDEX_INVALID != fei)
163 sw_if_index = fib_entry_get_resolving_interface (fei);
166 pool_foreach (i, sm->interfaces,
168 /* NAT packet aimed at outside interface */
169 if ((i->is_inside == 0) && (sw_if_index == i->sw_if_index))
178 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
179 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
182 udp_header_t * udp0 = ip4_next_header (ip0);
183 snat_session_key_t key0, sm0;
184 clib_bihash_kv_8_8_t kv0, value0;
186 key0.addr = ip0->dst_address;
187 key0.port = udp0->dst_port;
188 key0.protocol = proto0;
189 key0.fib_index = sm->outside_fib_index;
190 kv0.key = key0.as_u64;
192 /* NAT packet aimed at external address if */
193 /* has active sessions */
194 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
196 /* or is static mappings */
197 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
203 return snat_not_translate_fast(sm, node, sw_if_index0, ip0, proto0,
207 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
210 snat_session_key_t * key0,
211 snat_session_t ** sessionp,
212 vlib_node_runtime_t * node,
217 snat_user_key_t user_key;
219 clib_bihash_kv_8_8_t kv0, value0;
220 u32 oldest_per_user_translation_list_index;
221 dlist_elt_t * oldest_per_user_translation_list_elt;
222 dlist_elt_t * per_user_translation_list_elt;
223 dlist_elt_t * per_user_list_head_elt;
225 snat_session_key_t key1;
226 u32 address_index = ~0;
227 u32 outside_fib_index;
229 snat_worker_key_t worker_by_out_key;
231 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
234 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
235 return SNAT_IN2OUT_NEXT_DROP;
237 outside_fib_index = p[0];
239 key1.protocol = key0->protocol;
240 user_key.addr = ip0->src_address;
241 user_key.fib_index = rx_fib_index0;
242 kv0.key = user_key.as_u64;
244 /* Ever heard of the "user" = src ip4 address before? */
245 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
247 /* no, make a new one */
248 pool_get (sm->per_thread_data[thread_index].users, u);
249 memset (u, 0, sizeof (*u));
250 u->addr = ip0->src_address;
251 u->fib_index = rx_fib_index0;
253 pool_get (sm->per_thread_data[thread_index].list_pool, per_user_list_head_elt);
255 u->sessions_per_user_list_head_index = per_user_list_head_elt -
256 sm->per_thread_data[thread_index].list_pool;
258 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
259 u->sessions_per_user_list_head_index);
261 kv0.value = u - sm->per_thread_data[thread_index].users;
264 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
268 u = pool_elt_at_index (sm->per_thread_data[thread_index].users,
272 /* Over quota? Recycle the least recently used dynamic translation */
273 if (u->nsessions >= sm->max_translations_per_user)
275 /* Remove the oldest dynamic translation */
277 oldest_per_user_translation_list_index =
278 clib_dlist_remove_head (sm->per_thread_data[thread_index].list_pool,
279 u->sessions_per_user_list_head_index);
281 ASSERT (oldest_per_user_translation_list_index != ~0);
283 /* add it back to the end of the LRU list */
284 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
285 u->sessions_per_user_list_head_index,
286 oldest_per_user_translation_list_index);
287 /* Get the list element */
288 oldest_per_user_translation_list_elt =
289 pool_elt_at_index (sm->per_thread_data[thread_index].list_pool,
290 oldest_per_user_translation_list_index);
292 /* Get the session index from the list element */
293 session_index = oldest_per_user_translation_list_elt->value;
295 /* Get the session */
296 s = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
298 } while (snat_is_session_static (s));
300 /* Remove in2out, out2in keys */
301 kv0.key = s->in2out.as_u64;
302 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */))
303 clib_warning ("in2out key delete failed");
304 kv0.key = s->out2in.as_u64;
305 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */))
306 clib_warning ("out2in key delete failed");
309 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
310 s->out2in.addr.as_u32,
314 s->in2out.fib_index);
316 snat_free_outside_address_and_port
317 (sm, &s->out2in, s->outside_address_index);
318 s->outside_address_index = ~0;
320 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0, &key1,
325 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
326 return SNAT_IN2OUT_NEXT_DROP;
328 s->outside_address_index = address_index;
332 u8 static_mapping = 1;
334 /* First try to match static mapping by local address and port */
335 if (snat_static_mapping_match (sm, *key0, &key1, 0))
338 /* Try to create dynamic translation */
339 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0, &key1,
342 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
343 return SNAT_IN2OUT_NEXT_DROP;
347 /* Create a new session */
348 pool_get (sm->per_thread_data[thread_index].sessions, s);
349 memset (s, 0, sizeof (*s));
351 s->outside_address_index = address_index;
355 u->nstaticsessions++;
356 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
363 /* Create list elts */
364 pool_get (sm->per_thread_data[thread_index].list_pool,
365 per_user_translation_list_elt);
366 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
367 per_user_translation_list_elt -
368 sm->per_thread_data[thread_index].list_pool);
370 per_user_translation_list_elt->value =
371 s - sm->per_thread_data[thread_index].sessions;
372 s->per_user_index = per_user_translation_list_elt -
373 sm->per_thread_data[thread_index].list_pool;
374 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
376 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
377 s->per_user_list_head_index,
378 per_user_translation_list_elt -
379 sm->per_thread_data[thread_index].list_pool);
384 s->out2in.protocol = key0->protocol;
385 s->out2in.fib_index = outside_fib_index;
388 /* Add to translation hashes */
389 kv0.key = s->in2out.as_u64;
390 kv0.value = s - sm->per_thread_data[thread_index].sessions;
391 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
392 clib_warning ("in2out key add failed");
394 kv0.key = s->out2in.as_u64;
395 kv0.value = s - sm->per_thread_data[thread_index].sessions;
397 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
398 clib_warning ("out2in key add failed");
400 /* Add to translated packets worker lookup */
401 worker_by_out_key.addr = s->out2in.addr;
402 worker_by_out_key.port = s->out2in.port;
403 worker_by_out_key.fib_index = s->out2in.fib_index;
404 kv0.key = worker_by_out_key.as_u64;
405 kv0.value = thread_index;
406 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
409 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
410 s->out2in.addr.as_u32,
414 s->in2out.fib_index);
419 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
420 snat_session_key_t *p_key0)
422 icmp46_header_t *icmp0;
423 snat_session_key_t key0;
424 icmp_echo_header_t *echo0, *inner_echo0 = 0;
425 ip4_header_t *inner_ip0 = 0;
427 icmp46_header_t *inner_icmp0;
429 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
430 echo0 = (icmp_echo_header_t *)(icmp0+1);
432 if (!icmp_is_error_message (icmp0))
434 key0.protocol = SNAT_PROTOCOL_ICMP;
435 key0.addr = ip0->src_address;
436 key0.port = echo0->identifier;
440 inner_ip0 = (ip4_header_t *)(echo0+1);
441 l4_header = ip4_next_header (inner_ip0);
442 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
443 key0.addr = inner_ip0->dst_address;
444 switch (key0.protocol)
446 case SNAT_PROTOCOL_ICMP:
447 inner_icmp0 = (icmp46_header_t*)l4_header;
448 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
449 key0.port = inner_echo0->identifier;
451 case SNAT_PROTOCOL_UDP:
452 case SNAT_PROTOCOL_TCP:
453 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
456 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
460 return -1; /* success */
464 * Get address and port values to be used for packet SNAT translation
465 * and create session if needed
467 * @param[in,out] sm SNAT main
468 * @param[in,out] node SNAT node runtime
469 * @param[in] thread_index thread index
470 * @param[in,out] b0 buffer containing packet to be translated
471 * @param[out] p_proto protocol used for matching
472 * @param[out] p_value address and port after NAT translation
473 * @param[out] p_dont_translate if packet should not be translated
474 * @param d optional parameter
475 * @param e optional parameter
477 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
478 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
479 snat_session_key_t *p_value,
480 u8 *p_dont_translate, void *d, void *e)
483 icmp46_header_t *icmp0;
486 snat_session_key_t key0;
487 snat_session_t *s0 = 0;
488 u8 dont_translate = 0;
489 clib_bihash_kv_8_8_t kv0, value0;
493 ip0 = vlib_buffer_get_current (b0);
494 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
495 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
496 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
498 err = icmp_get_key (ip0, &key0);
501 b0->error = node->errors[err];
502 next0 = SNAT_IN2OUT_NEXT_DROP;
505 key0.fib_index = rx_fib_index0;
507 kv0.key = key0.as_u64;
509 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
511 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
512 IP_PROTOCOL_ICMP, rx_fib_index0)))
518 if (icmp_is_error_message (icmp0))
520 next0 = SNAT_IN2OUT_NEXT_DROP;
524 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
525 &s0, node, next0, thread_index);
527 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
531 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
534 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
535 !icmp_is_error_message (icmp0)))
537 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
538 next0 = SNAT_IN2OUT_NEXT_DROP;
543 *p_proto = key0.protocol;
545 *p_value = s0->out2in;
546 *p_dont_translate = dont_translate;
548 *(snat_session_t**)d = s0;
553 * Get address and port values to be used for packet SNAT translation
555 * @param[in] sm SNAT main
556 * @param[in,out] node SNAT node runtime
557 * @param[in] thread_index thread index
558 * @param[in,out] b0 buffer containing packet to be translated
559 * @param[out] p_proto protocol used for matching
560 * @param[out] p_value address and port after NAT translation
561 * @param[out] p_dont_translate if packet should not be translated
562 * @param d optional parameter
563 * @param e optional parameter
565 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
566 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
567 snat_session_key_t *p_value,
568 u8 *p_dont_translate, void *d, void *e)
571 icmp46_header_t *icmp0;
574 snat_session_key_t key0;
575 snat_session_key_t sm0;
576 u8 dont_translate = 0;
580 ip0 = vlib_buffer_get_current (b0);
581 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
582 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
583 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
585 err = icmp_get_key (ip0, &key0);
588 b0->error = node->errors[err];
589 next0 = SNAT_IN2OUT_NEXT_DROP;
592 key0.fib_index = rx_fib_index0;
594 if (snat_static_mapping_match(sm, key0, &sm0, 0))
596 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
597 IP_PROTOCOL_ICMP, rx_fib_index0)))
603 if (icmp_is_error_message (icmp0))
605 next0 = SNAT_IN2OUT_NEXT_DROP;
609 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
610 next0 = SNAT_IN2OUT_NEXT_DROP;
614 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
615 !icmp_is_error_message (icmp0)))
617 if (icmp0->type != ICMP4_echo_reply || key0.port != sm0.port)
619 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
620 next0 = SNAT_IN2OUT_NEXT_DROP;
628 *p_proto = key0.protocol;
629 *p_dont_translate = dont_translate;
633 static inline u32 icmp_in2out (snat_main_t *sm,
636 icmp46_header_t * icmp0,
639 vlib_node_runtime_t * node,
645 snat_session_key_t sm0;
647 icmp_echo_header_t *echo0, *inner_echo0 = 0;
648 ip4_header_t *inner_ip0;
650 icmp46_header_t *inner_icmp0;
652 u32 new_addr0, old_addr0;
653 u16 old_id0, new_id0;
658 echo0 = (icmp_echo_header_t *)(icmp0+1);
660 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0,
661 &protocol, &sm0, &dont_translate, d, e);
664 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
667 sum0 = ip_incremental_checksum (0, icmp0,
668 ntohs(ip0->length) - ip4_header_bytes (ip0));
669 checksum0 = ~ip_csum_fold (sum0);
670 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
672 next0 = SNAT_IN2OUT_NEXT_DROP;
676 old_addr0 = ip0->src_address.as_u32;
677 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
678 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
680 sum0 = ip0->checksum;
681 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
682 src_address /* changed member */);
683 ip0->checksum = ip_csum_fold (sum0);
685 if (!icmp_is_error_message (icmp0))
688 if (PREDICT_FALSE(new_id0 != echo0->identifier))
690 old_id0 = echo0->identifier;
692 echo0->identifier = new_id0;
694 sum0 = icmp0->checksum;
695 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
697 icmp0->checksum = ip_csum_fold (sum0);
702 inner_ip0 = (ip4_header_t *)(echo0+1);
703 l4_header = ip4_next_header (inner_ip0);
705 if (!ip4_header_checksum_is_valid (inner_ip0))
707 next0 = SNAT_IN2OUT_NEXT_DROP;
711 old_addr0 = inner_ip0->dst_address.as_u32;
712 inner_ip0->dst_address = sm0.addr;
713 new_addr0 = inner_ip0->dst_address.as_u32;
715 sum0 = icmp0->checksum;
716 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
717 dst_address /* changed member */);
718 icmp0->checksum = ip_csum_fold (sum0);
722 case SNAT_PROTOCOL_ICMP:
723 inner_icmp0 = (icmp46_header_t*)l4_header;
724 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
726 old_id0 = inner_echo0->identifier;
728 inner_echo0->identifier = new_id0;
730 sum0 = icmp0->checksum;
731 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
733 icmp0->checksum = ip_csum_fold (sum0);
735 case SNAT_PROTOCOL_UDP:
736 case SNAT_PROTOCOL_TCP:
737 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
739 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
741 sum0 = icmp0->checksum;
742 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
744 icmp0->checksum = ip_csum_fold (sum0);
758 * Hairpinning allows two endpoints on the internal side of the NAT to
759 * communicate even if they only use each other's external IP addresses
762 * @param sm SNAT main.
763 * @param b0 Vlib buffer.
764 * @param ip0 IP header.
765 * @param udp0 UDP header.
766 * @param tcp0 TCP header.
767 * @param proto0 SNAT protocol.
770 snat_hairpinning (snat_main_t *sm,
777 snat_session_key_t key0, sm0;
778 snat_worker_key_t k0;
780 clib_bihash_kv_8_8_t kv0, value0;
782 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
783 u16 new_dst_port0, old_dst_port0;
785 key0.addr = ip0->dst_address;
786 key0.port = udp0->dst_port;
787 key0.protocol = proto0;
788 key0.fib_index = sm->outside_fib_index;
789 kv0.key = key0.as_u64;
791 /* Check if destination is in active sessions */
792 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
794 /* or static mappings */
795 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
797 new_dst_addr0 = sm0.addr.as_u32;
798 new_dst_port0 = sm0.port;
799 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
805 if (sm->num_workers > 1)
807 k0.addr = ip0->dst_address;
808 k0.port = udp0->dst_port;
809 k0.fib_index = sm->outside_fib_index;
811 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
817 ti = sm->num_workers;
819 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
820 new_dst_addr0 = s0->in2out.addr.as_u32;
821 new_dst_port0 = s0->in2out.port;
822 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
825 /* Destination is behind the same NAT, use internal address and port */
828 old_dst_addr0 = ip0->dst_address.as_u32;
829 ip0->dst_address.as_u32 = new_dst_addr0;
830 sum0 = ip0->checksum;
831 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
832 ip4_header_t, dst_address);
833 ip0->checksum = ip_csum_fold (sum0);
835 old_dst_port0 = tcp0->dst;
836 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
838 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
840 tcp0->dst = new_dst_port0;
841 sum0 = tcp0->checksum;
842 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
843 ip4_header_t, dst_address);
844 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
845 ip4_header_t /* cheat */, length);
846 tcp0->checksum = ip_csum_fold(sum0);
850 udp0->dst_port = new_dst_port0;
857 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
860 icmp46_header_t * icmp0,
863 vlib_node_runtime_t * node,
867 snat_session_t ** p_s0)
869 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
870 next0, thread_index, p_s0, 0);
871 snat_session_t * s0 = *p_s0;
872 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
875 s0->last_heard = now;
877 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
878 /* Per-user LRU list maintenance for dynamic translations */
879 if (!snat_is_session_static (s0))
881 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
883 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
884 s0->per_user_list_head_index,
892 snat_in2out_node_fn_inline (vlib_main_t * vm,
893 vlib_node_runtime_t * node,
894 vlib_frame_t * frame, int is_slow_path)
896 u32 n_left_from, * from, * to_next;
897 snat_in2out_next_t next_index;
898 u32 pkts_processed = 0;
899 snat_main_t * sm = &snat_main;
900 f64 now = vlib_time_now (vm);
901 u32 stats_node_index;
902 u32 thread_index = vlib_get_thread_index ();
904 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
905 snat_in2out_node.index;
907 from = vlib_frame_vector_args (frame);
908 n_left_from = frame->n_vectors;
909 next_index = node->cached_next_index;
911 while (n_left_from > 0)
915 vlib_get_next_frame (vm, node, next_index,
916 to_next, n_left_to_next);
918 while (n_left_from >= 4 && n_left_to_next >= 2)
921 vlib_buffer_t * b0, * b1;
923 u32 sw_if_index0, sw_if_index1;
924 ip4_header_t * ip0, * ip1;
925 ip_csum_t sum0, sum1;
926 u32 new_addr0, old_addr0, new_addr1, old_addr1;
927 u16 old_port0, new_port0, old_port1, new_port1;
928 udp_header_t * udp0, * udp1;
929 tcp_header_t * tcp0, * tcp1;
930 icmp46_header_t * icmp0, * icmp1;
931 snat_session_key_t key0, key1;
932 u32 rx_fib_index0, rx_fib_index1;
934 snat_session_t * s0 = 0, * s1 = 0;
935 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
937 /* Prefetch next iteration. */
939 vlib_buffer_t * p2, * p3;
941 p2 = vlib_get_buffer (vm, from[2]);
942 p3 = vlib_get_buffer (vm, from[3]);
944 vlib_prefetch_buffer_header (p2, LOAD);
945 vlib_prefetch_buffer_header (p3, LOAD);
947 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
948 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
951 /* speculatively enqueue b0 and b1 to the current next frame */
952 to_next[0] = bi0 = from[0];
953 to_next[1] = bi1 = from[1];
959 b0 = vlib_get_buffer (vm, bi0);
960 b1 = vlib_get_buffer (vm, bi1);
962 ip0 = vlib_buffer_get_current (b0);
963 udp0 = ip4_next_header (ip0);
964 tcp0 = (tcp_header_t *) udp0;
965 icmp0 = (icmp46_header_t *) udp0;
967 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
968 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
971 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
973 if (PREDICT_FALSE(ip0->ttl == 1))
975 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
976 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
977 ICMP4_time_exceeded_ttl_exceeded_in_transit,
979 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
983 proto0 = ip_proto_to_snat_proto (ip0->protocol);
985 /* Next configured feature, probably ip4-lookup */
988 if (PREDICT_FALSE (proto0 == ~0))
991 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
993 next0 = icmp_in2out_slow_path
994 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
995 node, next0, now, thread_index, &s0);
1001 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1003 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1008 key0.addr = ip0->src_address;
1009 key0.port = udp0->src_port;
1010 key0.protocol = proto0;
1011 key0.fib_index = rx_fib_index0;
1013 kv0.key = key0.as_u64;
1015 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
1019 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
1020 proto0, rx_fib_index0)))
1023 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1024 &s0, node, next0, thread_index);
1025 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1030 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1035 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1038 old_addr0 = ip0->src_address.as_u32;
1039 ip0->src_address = s0->out2in.addr;
1040 new_addr0 = ip0->src_address.as_u32;
1041 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1043 sum0 = ip0->checksum;
1044 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1046 src_address /* changed member */);
1047 ip0->checksum = ip_csum_fold (sum0);
1049 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1051 old_port0 = tcp0->src_port;
1052 tcp0->src_port = s0->out2in.port;
1053 new_port0 = tcp0->src_port;
1055 sum0 = tcp0->checksum;
1056 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1058 dst_address /* changed member */);
1059 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1060 ip4_header_t /* cheat */,
1061 length /* changed member */);
1062 tcp0->checksum = ip_csum_fold(sum0);
1066 old_port0 = udp0->src_port;
1067 udp0->src_port = s0->out2in.port;
1072 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1075 s0->last_heard = now;
1077 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1078 /* Per-user LRU list maintenance for dynamic translation */
1079 if (!snat_is_session_static (s0))
1081 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1082 s0->per_user_index);
1083 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1084 s0->per_user_list_head_index,
1085 s0->per_user_index);
1089 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1090 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1092 snat_in2out_trace_t *t =
1093 vlib_add_trace (vm, node, b0, sizeof (*t));
1094 t->is_slow_path = is_slow_path;
1095 t->sw_if_index = sw_if_index0;
1096 t->next_index = next0;
1097 t->session_index = ~0;
1099 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1102 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1104 ip1 = vlib_buffer_get_current (b1);
1105 udp1 = ip4_next_header (ip1);
1106 tcp1 = (tcp_header_t *) udp1;
1107 icmp1 = (icmp46_header_t *) udp1;
1109 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1110 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1113 if (PREDICT_FALSE(ip1->ttl == 1))
1115 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1116 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1117 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1119 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1123 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1125 /* Next configured feature, probably ip4-lookup */
1128 if (PREDICT_FALSE (proto1 == ~0))
1131 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1133 next1 = icmp_in2out_slow_path
1134 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1135 next1, now, thread_index, &s1);
1141 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1143 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1148 key1.addr = ip1->src_address;
1149 key1.port = udp1->src_port;
1150 key1.protocol = proto1;
1151 key1.fib_index = rx_fib_index1;
1153 kv1.key = key1.as_u64;
1155 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
1159 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1, ip1,
1160 proto1, rx_fib_index1)))
1163 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1164 &s1, node, next1, thread_index);
1165 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1170 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1175 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1178 old_addr1 = ip1->src_address.as_u32;
1179 ip1->src_address = s1->out2in.addr;
1180 new_addr1 = ip1->src_address.as_u32;
1181 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1183 sum1 = ip1->checksum;
1184 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1186 src_address /* changed member */);
1187 ip1->checksum = ip_csum_fold (sum1);
1189 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1191 old_port1 = tcp1->src_port;
1192 tcp1->src_port = s1->out2in.port;
1193 new_port1 = tcp1->src_port;
1195 sum1 = tcp1->checksum;
1196 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1198 dst_address /* changed member */);
1199 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1200 ip4_header_t /* cheat */,
1201 length /* changed member */);
1202 tcp1->checksum = ip_csum_fold(sum1);
1206 old_port1 = udp1->src_port;
1207 udp1->src_port = s1->out2in.port;
1212 snat_hairpinning (sm, b1, ip1, udp1, tcp1, proto1);
1215 s1->last_heard = now;
1217 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1218 /* Per-user LRU list maintenance for dynamic translation */
1219 if (!snat_is_session_static (s1))
1221 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1222 s1->per_user_index);
1223 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1224 s1->per_user_list_head_index,
1225 s1->per_user_index);
1229 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1230 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1232 snat_in2out_trace_t *t =
1233 vlib_add_trace (vm, node, b1, sizeof (*t));
1234 t->sw_if_index = sw_if_index1;
1235 t->next_index = next1;
1236 t->session_index = ~0;
1238 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1241 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1243 /* verify speculative enqueues, maybe switch current next frame */
1244 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1245 to_next, n_left_to_next,
1246 bi0, bi1, next0, next1);
1249 while (n_left_from > 0 && n_left_to_next > 0)
1257 u32 new_addr0, old_addr0;
1258 u16 old_port0, new_port0;
1259 udp_header_t * udp0;
1260 tcp_header_t * tcp0;
1261 icmp46_header_t * icmp0;
1262 snat_session_key_t key0;
1265 snat_session_t * s0 = 0;
1266 clib_bihash_kv_8_8_t kv0, value0;
1268 /* speculatively enqueue b0 to the current next frame */
1274 n_left_to_next -= 1;
1276 b0 = vlib_get_buffer (vm, bi0);
1277 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1279 ip0 = vlib_buffer_get_current (b0);
1280 udp0 = ip4_next_header (ip0);
1281 tcp0 = (tcp_header_t *) udp0;
1282 icmp0 = (icmp46_header_t *) udp0;
1284 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1285 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1288 if (PREDICT_FALSE(ip0->ttl == 1))
1290 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1291 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1292 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1294 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1298 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1300 /* Next configured feature, probably ip4-lookup */
1303 if (PREDICT_FALSE (proto0 == ~0))
1306 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1308 next0 = icmp_in2out_slow_path
1309 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1310 next0, now, thread_index, &s0);
1316 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1318 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1323 key0.addr = ip0->src_address;
1324 key0.port = udp0->src_port;
1325 key0.protocol = proto0;
1326 key0.fib_index = rx_fib_index0;
1328 kv0.key = key0.as_u64;
1330 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
1334 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
1335 proto0, rx_fib_index0)))
1338 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1339 &s0, node, next0, thread_index);
1341 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1346 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1351 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1354 old_addr0 = ip0->src_address.as_u32;
1355 ip0->src_address = s0->out2in.addr;
1356 new_addr0 = ip0->src_address.as_u32;
1357 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1359 sum0 = ip0->checksum;
1360 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1362 src_address /* changed member */);
1363 ip0->checksum = ip_csum_fold (sum0);
1365 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1367 old_port0 = tcp0->src_port;
1368 tcp0->src_port = s0->out2in.port;
1369 new_port0 = tcp0->src_port;
1371 sum0 = tcp0->checksum;
1372 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1374 dst_address /* changed member */);
1375 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1376 ip4_header_t /* cheat */,
1377 length /* changed member */);
1378 tcp0->checksum = ip_csum_fold(sum0);
1382 old_port0 = udp0->src_port;
1383 udp0->src_port = s0->out2in.port;
1388 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1391 s0->last_heard = now;
1393 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1394 /* Per-user LRU list maintenance for dynamic translation */
1395 if (!snat_is_session_static (s0))
1397 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1398 s0->per_user_index);
1399 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1400 s0->per_user_list_head_index,
1401 s0->per_user_index);
1405 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1406 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1408 snat_in2out_trace_t *t =
1409 vlib_add_trace (vm, node, b0, sizeof (*t));
1410 t->is_slow_path = is_slow_path;
1411 t->sw_if_index = sw_if_index0;
1412 t->next_index = next0;
1413 t->session_index = ~0;
1415 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1418 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1420 /* verify speculative enqueue, maybe switch current next frame */
1421 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1422 to_next, n_left_to_next,
1426 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1429 vlib_node_increment_counter (vm, stats_node_index,
1430 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1432 return frame->n_vectors;
1436 snat_in2out_fast_path_fn (vlib_main_t * vm,
1437 vlib_node_runtime_t * node,
1438 vlib_frame_t * frame)
1440 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
1443 VLIB_REGISTER_NODE (snat_in2out_node) = {
1444 .function = snat_in2out_fast_path_fn,
1445 .name = "snat-in2out",
1446 .vector_size = sizeof (u32),
1447 .format_trace = format_snat_in2out_trace,
1448 .type = VLIB_NODE_TYPE_INTERNAL,
1450 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1451 .error_strings = snat_in2out_error_strings,
1453 .runtime_data_bytes = sizeof (snat_runtime_t),
1455 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1457 /* edit / add dispositions here */
1459 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1460 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1461 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1462 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1466 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1469 snat_in2out_slow_path_fn (vlib_main_t * vm,
1470 vlib_node_runtime_t * node,
1471 vlib_frame_t * frame)
1473 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
1476 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1477 .function = snat_in2out_slow_path_fn,
1478 .name = "snat-in2out-slowpath",
1479 .vector_size = sizeof (u32),
1480 .format_trace = format_snat_in2out_trace,
1481 .type = VLIB_NODE_TYPE_INTERNAL,
1483 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1484 .error_strings = snat_in2out_error_strings,
1486 .runtime_data_bytes = sizeof (snat_runtime_t),
1488 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1490 /* edit / add dispositions here */
1492 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1493 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1494 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1495 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1499 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1501 /**************************/
1502 /*** deterministic mode ***/
1503 /**************************/
1505 snat_det_in2out_node_fn (vlib_main_t * vm,
1506 vlib_node_runtime_t * node,
1507 vlib_frame_t * frame)
1509 u32 n_left_from, * from, * to_next;
1510 snat_in2out_next_t next_index;
1511 u32 pkts_processed = 0;
1512 snat_main_t * sm = &snat_main;
1513 u32 now = (u32) vlib_time_now (vm);
1514 u32 thread_index = os_get_cpu_number ();
1516 from = vlib_frame_vector_args (frame);
1517 n_left_from = frame->n_vectors;
1518 next_index = node->cached_next_index;
1520 while (n_left_from > 0)
1524 vlib_get_next_frame (vm, node, next_index,
1525 to_next, n_left_to_next);
1527 while (n_left_from >= 4 && n_left_to_next >= 2)
1530 vlib_buffer_t * b0, * b1;
1532 u32 sw_if_index0, sw_if_index1;
1533 ip4_header_t * ip0, * ip1;
1534 ip_csum_t sum0, sum1;
1535 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
1536 u16 old_port0, new_port0, lo_port0, i0;
1537 u16 old_port1, new_port1, lo_port1, i1;
1538 udp_header_t * udp0, * udp1;
1539 tcp_header_t * tcp0, * tcp1;
1541 snat_det_out_key_t key0, key1;
1542 snat_det_map_t * dm0, * dm1;
1543 snat_det_session_t * ses0 = 0, * ses1 = 0;
1544 u32 rx_fib_index0, rx_fib_index1;
1545 icmp46_header_t * icmp0, * icmp1;
1547 /* Prefetch next iteration. */
1549 vlib_buffer_t * p2, * p3;
1551 p2 = vlib_get_buffer (vm, from[2]);
1552 p3 = vlib_get_buffer (vm, from[3]);
1554 vlib_prefetch_buffer_header (p2, LOAD);
1555 vlib_prefetch_buffer_header (p3, LOAD);
1557 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1558 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1561 /* speculatively enqueue b0 and b1 to the current next frame */
1562 to_next[0] = bi0 = from[0];
1563 to_next[1] = bi1 = from[1];
1567 n_left_to_next -= 2;
1569 b0 = vlib_get_buffer (vm, bi0);
1570 b1 = vlib_get_buffer (vm, bi1);
1572 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1573 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1575 ip0 = vlib_buffer_get_current (b0);
1576 udp0 = ip4_next_header (ip0);
1577 tcp0 = (tcp_header_t *) udp0;
1579 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1581 if (PREDICT_FALSE(ip0->ttl == 1))
1583 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1584 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1585 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1587 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1591 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1593 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
1595 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1596 icmp0 = (icmp46_header_t *) udp0;
1598 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
1599 rx_fib_index0, node, next0, thread_index,
1604 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
1605 if (PREDICT_FALSE(!dm0))
1607 clib_warning("no match for internal host %U",
1608 format_ip4_address, &ip0->src_address);
1609 next0 = SNAT_IN2OUT_NEXT_DROP;
1610 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1614 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
1616 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src);
1617 if (PREDICT_FALSE(!ses0))
1619 key0.ext_host_addr = ip0->dst_address;
1620 key0.ext_host_port = tcp0->dst;
1621 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
1623 key0.out_port = clib_host_to_net_u16 (lo_port0 +
1624 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
1626 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
1629 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
1632 if (PREDICT_FALSE(!ses0))
1634 next0 = SNAT_IN2OUT_NEXT_DROP;
1635 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1640 new_port0 = ses0->out.out_port;
1642 old_addr0.as_u32 = ip0->src_address.as_u32;
1643 ip0->src_address.as_u32 = new_addr0.as_u32;
1644 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1646 sum0 = ip0->checksum;
1647 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1649 src_address /* changed member */);
1650 ip0->checksum = ip_csum_fold (sum0);
1652 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1654 if (tcp0->flags & TCP_FLAG_SYN)
1655 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
1656 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
1657 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1658 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
1659 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
1660 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
1661 snat_det_ses_close(dm0, ses0);
1662 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1663 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
1664 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
1665 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1667 old_port0 = tcp0->src;
1668 tcp0->src = new_port0;
1670 sum0 = tcp0->checksum;
1671 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1673 dst_address /* changed member */);
1674 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1675 ip4_header_t /* cheat */,
1676 length /* changed member */);
1677 tcp0->checksum = ip_csum_fold(sum0);
1681 ses0->state = SNAT_SESSION_UDP_ACTIVE;
1682 old_port0 = udp0->src_port;
1683 udp0->src_port = new_port0;
1689 case SNAT_SESSION_UDP_ACTIVE:
1690 ses0->expire = now + SNAT_UDP_TIMEOUT;
1692 case SNAT_SESSION_TCP_SYN_SENT:
1693 case SNAT_SESSION_TCP_FIN_WAIT:
1694 case SNAT_SESSION_TCP_CLOSE_WAIT:
1695 case SNAT_SESSION_TCP_LAST_ACK:
1696 ses0->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
1698 case SNAT_SESSION_TCP_ESTABLISHED:
1699 ses0->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
1704 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1705 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1707 snat_in2out_trace_t *t =
1708 vlib_add_trace (vm, node, b0, sizeof (*t));
1709 t->is_slow_path = 0;
1710 t->sw_if_index = sw_if_index0;
1711 t->next_index = next0;
1712 t->session_index = ~0;
1714 t->session_index = ses0 - dm0->sessions;
1717 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1719 ip1 = vlib_buffer_get_current (b1);
1720 udp1 = ip4_next_header (ip1);
1721 tcp1 = (tcp_header_t *) udp1;
1723 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1725 if (PREDICT_FALSE(ip1->ttl == 1))
1727 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1728 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1729 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1731 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1735 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1737 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
1739 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
1740 icmp1 = (icmp46_header_t *) udp1;
1742 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
1743 rx_fib_index1, node, next1, thread_index,
1748 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
1749 if (PREDICT_FALSE(!dm1))
1751 clib_warning("no match for internal host %U",
1752 format_ip4_address, &ip0->src_address);
1753 next1 = SNAT_IN2OUT_NEXT_DROP;
1754 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1758 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
1760 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src);
1761 if (PREDICT_FALSE(!ses1))
1763 key1.ext_host_addr = ip1->dst_address;
1764 key1.ext_host_port = tcp1->dst;
1765 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
1767 key1.out_port = clib_host_to_net_u16 (lo_port1 +
1768 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
1770 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
1773 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
1776 if (PREDICT_FALSE(!ses1))
1778 next1 = SNAT_IN2OUT_NEXT_DROP;
1779 b1->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1784 new_port1 = ses1->out.out_port;
1786 old_addr1.as_u32 = ip1->src_address.as_u32;
1787 ip1->src_address.as_u32 = new_addr1.as_u32;
1788 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1790 sum1 = ip1->checksum;
1791 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1793 src_address /* changed member */);
1794 ip1->checksum = ip_csum_fold (sum1);
1796 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1798 if (tcp1->flags & TCP_FLAG_SYN)
1799 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
1800 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
1801 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1802 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
1803 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
1804 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
1805 snat_det_ses_close(dm1, ses1);
1806 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1807 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
1808 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
1809 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1811 old_port1 = tcp1->src;
1812 tcp1->src = new_port1;
1814 sum1 = tcp1->checksum;
1815 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1817 dst_address /* changed member */);
1818 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1819 ip4_header_t /* cheat */,
1820 length /* changed member */);
1821 tcp1->checksum = ip_csum_fold(sum1);
1825 ses1->state = SNAT_SESSION_UDP_ACTIVE;
1826 old_port1 = udp1->src_port;
1827 udp1->src_port = new_port1;
1833 case SNAT_SESSION_UDP_ACTIVE:
1834 ses1->expire = now + SNAT_UDP_TIMEOUT;
1836 case SNAT_SESSION_TCP_SYN_SENT:
1837 case SNAT_SESSION_TCP_FIN_WAIT:
1838 case SNAT_SESSION_TCP_CLOSE_WAIT:
1839 case SNAT_SESSION_TCP_LAST_ACK:
1840 ses1->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
1842 case SNAT_SESSION_TCP_ESTABLISHED:
1843 ses1->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
1848 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1849 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1851 snat_in2out_trace_t *t =
1852 vlib_add_trace (vm, node, b1, sizeof (*t));
1853 t->is_slow_path = 0;
1854 t->sw_if_index = sw_if_index1;
1855 t->next_index = next1;
1856 t->session_index = ~0;
1858 t->session_index = ses1 - dm1->sessions;
1861 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1863 /* verify speculative enqueues, maybe switch current next frame */
1864 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1865 to_next, n_left_to_next,
1866 bi0, bi1, next0, next1);
1869 while (n_left_from > 0 && n_left_to_next > 0)
1877 ip4_address_t new_addr0, old_addr0;
1878 u16 old_port0, new_port0, lo_port0, i0;
1879 udp_header_t * udp0;
1880 tcp_header_t * tcp0;
1882 snat_det_out_key_t key0;
1883 snat_det_map_t * dm0;
1884 snat_det_session_t * ses0 = 0;
1886 icmp46_header_t * icmp0;
1888 /* speculatively enqueue b0 to the current next frame */
1894 n_left_to_next -= 1;
1896 b0 = vlib_get_buffer (vm, bi0);
1897 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1899 ip0 = vlib_buffer_get_current (b0);
1900 udp0 = ip4_next_header (ip0);
1901 tcp0 = (tcp_header_t *) udp0;
1903 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1905 if (PREDICT_FALSE(ip0->ttl == 1))
1907 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1908 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1909 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1911 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1915 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1917 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
1919 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1920 icmp0 = (icmp46_header_t *) udp0;
1922 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
1923 rx_fib_index0, node, next0, thread_index,
1928 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
1929 if (PREDICT_FALSE(!dm0))
1931 clib_warning("no match for internal host %U",
1932 format_ip4_address, &ip0->src_address);
1933 next0 = SNAT_IN2OUT_NEXT_DROP;
1934 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1938 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
1940 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src);
1941 if (PREDICT_FALSE(!ses0))
1943 key0.ext_host_addr = ip0->dst_address;
1944 key0.ext_host_port = tcp0->dst;
1945 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
1947 key0.out_port = clib_host_to_net_u16 (lo_port0 +
1948 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
1950 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
1953 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
1956 if (PREDICT_FALSE(!ses0))
1958 next0 = SNAT_IN2OUT_NEXT_DROP;
1959 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1964 new_port0 = ses0->out.out_port;
1966 old_addr0.as_u32 = ip0->src_address.as_u32;
1967 ip0->src_address.as_u32 = new_addr0.as_u32;
1968 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1970 sum0 = ip0->checksum;
1971 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1973 src_address /* changed member */);
1974 ip0->checksum = ip_csum_fold (sum0);
1976 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1978 if (tcp0->flags & TCP_FLAG_SYN)
1979 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
1980 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
1981 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1982 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
1983 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
1984 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
1985 snat_det_ses_close(dm0, ses0);
1986 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1987 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
1988 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
1989 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1991 old_port0 = tcp0->src;
1992 tcp0->src = new_port0;
1994 sum0 = tcp0->checksum;
1995 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1997 dst_address /* changed member */);
1998 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1999 ip4_header_t /* cheat */,
2000 length /* changed member */);
2001 tcp0->checksum = ip_csum_fold(sum0);
2005 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2006 old_port0 = udp0->src_port;
2007 udp0->src_port = new_port0;
2013 case SNAT_SESSION_UDP_ACTIVE:
2014 ses0->expire = now + SNAT_UDP_TIMEOUT;
2016 case SNAT_SESSION_TCP_SYN_SENT:
2017 case SNAT_SESSION_TCP_FIN_WAIT:
2018 case SNAT_SESSION_TCP_CLOSE_WAIT:
2019 case SNAT_SESSION_TCP_LAST_ACK:
2020 ses0->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
2022 case SNAT_SESSION_TCP_ESTABLISHED:
2023 ses0->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
2028 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2029 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2031 snat_in2out_trace_t *t =
2032 vlib_add_trace (vm, node, b0, sizeof (*t));
2033 t->is_slow_path = 0;
2034 t->sw_if_index = sw_if_index0;
2035 t->next_index = next0;
2036 t->session_index = ~0;
2038 t->session_index = ses0 - dm0->sessions;
2041 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2043 /* verify speculative enqueue, maybe switch current next frame */
2044 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2045 to_next, n_left_to_next,
2049 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2052 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
2053 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2055 return frame->n_vectors;
2058 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
2059 .function = snat_det_in2out_node_fn,
2060 .name = "snat-det-in2out",
2061 .vector_size = sizeof (u32),
2062 .format_trace = format_snat_in2out_trace,
2063 .type = VLIB_NODE_TYPE_INTERNAL,
2065 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2066 .error_strings = snat_in2out_error_strings,
2068 .runtime_data_bytes = sizeof (snat_runtime_t),
2072 /* edit / add dispositions here */
2074 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2075 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2076 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2080 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
2083 * Get address and port values to be used for packet SNAT translation
2084 * and create session if needed
2086 * @param[in,out] sm SNAT main
2087 * @param[in,out] node SNAT node runtime
2088 * @param[in] thread_index thread index
2089 * @param[in,out] b0 buffer containing packet to be translated
2090 * @param[out] p_proto protocol used for matching
2091 * @param[out] p_value address and port after NAT translation
2092 * @param[out] p_dont_translate if packet should not be translated
2093 * @param d optional parameter
2094 * @param e optional parameter
2096 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
2097 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
2098 snat_session_key_t *p_value,
2099 u8 *p_dont_translate, void *d, void *e)
2102 icmp46_header_t *icmp0;
2106 snat_det_out_key_t key0;
2107 u8 dont_translate = 0;
2109 icmp_echo_header_t *echo0, *inner_echo0 = 0;
2110 ip4_header_t *inner_ip0;
2111 void *l4_header = 0;
2112 icmp46_header_t *inner_icmp0;
2113 snat_det_map_t * dm0 = 0;
2114 ip4_address_t new_addr0;
2116 snat_det_session_t * ses0 = 0;
2117 ip4_address_t in_addr;
2120 ip0 = vlib_buffer_get_current (b0);
2121 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
2122 echo0 = (icmp_echo_header_t *)(icmp0+1);
2123 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2124 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2126 if (!icmp_is_error_message (icmp0))
2128 protocol = SNAT_PROTOCOL_ICMP;
2129 in_addr = ip0->src_address;
2130 in_port = echo0->identifier;
2134 inner_ip0 = (ip4_header_t *)(echo0+1);
2135 l4_header = ip4_next_header (inner_ip0);
2136 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
2137 in_addr = inner_ip0->dst_address;
2140 case SNAT_PROTOCOL_ICMP:
2141 inner_icmp0 = (icmp46_header_t*)l4_header;
2142 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
2143 in_port = inner_echo0->identifier;
2145 case SNAT_PROTOCOL_UDP:
2146 case SNAT_PROTOCOL_TCP:
2147 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
2150 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
2151 next0 = SNAT_IN2OUT_NEXT_DROP;
2156 dm0 = snat_det_map_by_user(sm, &in_addr);
2157 if (PREDICT_FALSE(!dm0))
2159 clib_warning("no match for internal host %U",
2160 format_ip4_address, &in_addr);
2161 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
2162 IP_PROTOCOL_ICMP, rx_fib_index0)))
2167 next0 = SNAT_IN2OUT_NEXT_DROP;
2168 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2172 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
2174 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port);
2175 if (PREDICT_FALSE(!ses0))
2177 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
2178 IP_PROTOCOL_ICMP, rx_fib_index0)))
2183 if (icmp0->type != ICMP4_echo_request)
2185 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2186 next0 = SNAT_IN2OUT_NEXT_DROP;
2189 key0.ext_host_addr = ip0->dst_address;
2190 key0.ext_host_port = 0;
2191 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2193 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2194 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
2196 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
2199 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
2202 if (PREDICT_FALSE(!ses0))
2204 next0 = SNAT_IN2OUT_NEXT_DROP;
2205 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
2210 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
2211 !icmp_is_error_message (icmp0)))
2213 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2214 next0 = SNAT_IN2OUT_NEXT_DROP;
2218 u32 now = (u32) vlib_time_now (sm->vlib_main);
2220 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
2221 ses0->expire = now + SNAT_ICMP_TIMEOUT;
2224 *p_proto = protocol;
2227 p_value->addr = new_addr0;
2228 p_value->fib_index = sm->outside_fib_index;
2229 p_value->port = ses0->out.out_port;
2231 *p_dont_translate = dont_translate;
2233 *(snat_det_session_t**)d = ses0;
2235 *(snat_det_map_t**)e = dm0;
2239 /**********************/
2240 /*** worker handoff ***/
2241 /**********************/
2243 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
2244 vlib_node_runtime_t * node,
2245 vlib_frame_t * frame)
2247 snat_main_t *sm = &snat_main;
2248 vlib_thread_main_t *tm = vlib_get_thread_main ();
2249 u32 n_left_from, *from, *to_next = 0;
2250 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
2251 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
2253 vlib_frame_queue_elt_t *hf = 0;
2254 vlib_frame_t *f = 0;
2256 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
2257 u32 next_worker_index = 0;
2258 u32 current_worker_index = ~0;
2259 u32 thread_index = vlib_get_thread_index ();
2261 ASSERT (vec_len (sm->workers));
2263 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
2265 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
2267 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
2268 sm->first_worker_index + sm->num_workers - 1,
2269 (vlib_frame_queue_t *) (~0));
2272 from = vlib_frame_vector_args (frame);
2273 n_left_from = frame->n_vectors;
2275 while (n_left_from > 0)
2288 b0 = vlib_get_buffer (vm, bi0);
2290 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2291 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2293 ip0 = vlib_buffer_get_current (b0);
2295 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
2297 if (PREDICT_FALSE (next_worker_index != thread_index))
2301 if (next_worker_index != current_worker_index)
2304 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2306 hf = vlib_get_worker_handoff_queue_elt (sm->fq_in2out_index,
2308 handoff_queue_elt_by_worker_index);
2310 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
2311 to_next_worker = &hf->buffer_index[hf->n_vectors];
2312 current_worker_index = next_worker_index;
2315 /* enqueue to correct worker thread */
2316 to_next_worker[0] = bi0;
2318 n_left_to_next_worker--;
2320 if (n_left_to_next_worker == 0)
2322 hf->n_vectors = VLIB_FRAME_SIZE;
2323 vlib_put_frame_queue_elt (hf);
2324 current_worker_index = ~0;
2325 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
2332 /* if this is 1st frame */
2335 f = vlib_get_frame_to_node (vm, sm->in2out_node_index);
2336 to_next = vlib_frame_vector_args (f);
2344 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2345 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2347 snat_in2out_worker_handoff_trace_t *t =
2348 vlib_add_trace (vm, node, b0, sizeof (*t));
2349 t->next_worker_index = next_worker_index;
2350 t->do_handoff = do_handoff;
2355 vlib_put_frame_to_node (vm, sm->in2out_node_index, f);
2358 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2360 /* Ship frames to the worker nodes */
2361 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
2363 if (handoff_queue_elt_by_worker_index[i])
2365 hf = handoff_queue_elt_by_worker_index[i];
2367 * It works better to let the handoff node
2368 * rate-adapt, always ship the handoff queue element.
2370 if (1 || hf->n_vectors == hf->last_n_vectors)
2372 vlib_put_frame_queue_elt (hf);
2373 handoff_queue_elt_by_worker_index[i] = 0;
2376 hf->last_n_vectors = hf->n_vectors;
2378 congested_handoff_queue_by_worker_index[i] =
2379 (vlib_frame_queue_t *) (~0);
2382 current_worker_index = ~0;
2383 return frame->n_vectors;
2386 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
2387 .function = snat_in2out_worker_handoff_fn,
2388 .name = "snat-in2out-worker-handoff",
2389 .vector_size = sizeof (u32),
2390 .format_trace = format_snat_in2out_worker_handoff_trace,
2391 .type = VLIB_NODE_TYPE_INTERNAL,
2400 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn);
2403 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2404 vlib_node_runtime_t * node,
2405 vlib_frame_t * frame)
2407 u32 n_left_from, * from, * to_next;
2408 snat_in2out_next_t next_index;
2409 u32 pkts_processed = 0;
2410 snat_main_t * sm = &snat_main;
2411 u32 stats_node_index;
2413 stats_node_index = snat_in2out_fast_node.index;
2415 from = vlib_frame_vector_args (frame);
2416 n_left_from = frame->n_vectors;
2417 next_index = node->cached_next_index;
2419 while (n_left_from > 0)
2423 vlib_get_next_frame (vm, node, next_index,
2424 to_next, n_left_to_next);
2426 while (n_left_from > 0 && n_left_to_next > 0)
2434 u32 new_addr0, old_addr0;
2435 u16 old_port0, new_port0;
2436 udp_header_t * udp0;
2437 tcp_header_t * tcp0;
2438 icmp46_header_t * icmp0;
2439 snat_session_key_t key0, sm0;
2443 /* speculatively enqueue b0 to the current next frame */
2449 n_left_to_next -= 1;
2451 b0 = vlib_get_buffer (vm, bi0);
2452 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2454 ip0 = vlib_buffer_get_current (b0);
2455 udp0 = ip4_next_header (ip0);
2456 tcp0 = (tcp_header_t *) udp0;
2457 icmp0 = (icmp46_header_t *) udp0;
2459 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2460 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2462 if (PREDICT_FALSE(ip0->ttl == 1))
2464 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2465 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2466 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2468 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2472 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2474 if (PREDICT_FALSE (proto0 == ~0))
2477 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2479 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2480 rx_fib_index0, node, next0, ~0, 0, 0);
2484 key0.addr = ip0->src_address;
2485 key0.port = udp0->src_port;
2486 key0.fib_index = rx_fib_index0;
2488 if (snat_static_mapping_match(sm, key0, &sm0, 0))
2490 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2491 next0= SNAT_IN2OUT_NEXT_DROP;
2495 new_addr0 = sm0.addr.as_u32;
2496 new_port0 = sm0.port;
2497 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2498 old_addr0 = ip0->src_address.as_u32;
2499 ip0->src_address.as_u32 = new_addr0;
2501 sum0 = ip0->checksum;
2502 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2504 src_address /* changed member */);
2505 ip0->checksum = ip_csum_fold (sum0);
2507 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
2509 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2511 old_port0 = tcp0->src_port;
2512 tcp0->src_port = new_port0;
2514 sum0 = tcp0->checksum;
2515 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2517 dst_address /* changed member */);
2518 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2519 ip4_header_t /* cheat */,
2520 length /* changed member */);
2521 tcp0->checksum = ip_csum_fold(sum0);
2525 old_port0 = udp0->src_port;
2526 udp0->src_port = new_port0;
2532 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2534 sum0 = tcp0->checksum;
2535 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2537 dst_address /* changed member */);
2538 tcp0->checksum = ip_csum_fold(sum0);
2543 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
2546 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2547 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2549 snat_in2out_trace_t *t =
2550 vlib_add_trace (vm, node, b0, sizeof (*t));
2551 t->sw_if_index = sw_if_index0;
2552 t->next_index = next0;
2555 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2557 /* verify speculative enqueue, maybe switch current next frame */
2558 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2559 to_next, n_left_to_next,
2563 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2566 vlib_node_increment_counter (vm, stats_node_index,
2567 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2569 return frame->n_vectors;
2573 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2574 .function = snat_in2out_fast_static_map_fn,
2575 .name = "snat-in2out-fast",
2576 .vector_size = sizeof (u32),
2577 .format_trace = format_snat_in2out_fast_trace,
2578 .type = VLIB_NODE_TYPE_INTERNAL,
2580 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2581 .error_strings = snat_in2out_error_strings,
2583 .runtime_data_bytes = sizeof (snat_runtime_t),
2585 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2587 /* edit / add dispositions here */
2589 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2590 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2591 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
2592 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2596 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);