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, "unsupported ICMP type") \
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, 0))
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, 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 (PREDICT_FALSE(icmp_is_error_message (icmp0)))
520 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
521 next0 = SNAT_IN2OUT_NEXT_DROP;
525 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
526 &s0, node, next0, thread_index);
528 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
533 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
534 icmp0->type != ICMP4_echo_reply &&
535 !icmp_is_error_message (icmp0)))
537 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
538 next0 = SNAT_IN2OUT_NEXT_DROP;
542 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
547 *p_proto = key0.protocol;
549 *p_value = s0->out2in;
550 *p_dont_translate = dont_translate;
552 *(snat_session_t**)d = s0;
557 * Get address and port values to be used for packet SNAT translation
559 * @param[in] sm SNAT main
560 * @param[in,out] node SNAT node runtime
561 * @param[in] thread_index thread index
562 * @param[in,out] b0 buffer containing packet to be translated
563 * @param[out] p_proto protocol used for matching
564 * @param[out] p_value address and port after NAT translation
565 * @param[out] p_dont_translate if packet should not be translated
566 * @param d optional parameter
567 * @param e optional parameter
569 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
570 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
571 snat_session_key_t *p_value,
572 u8 *p_dont_translate, void *d, void *e)
575 icmp46_header_t *icmp0;
578 snat_session_key_t key0;
579 snat_session_key_t sm0;
580 u8 dont_translate = 0;
585 ip0 = vlib_buffer_get_current (b0);
586 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
587 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
588 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
590 err = icmp_get_key (ip0, &key0);
593 b0->error = node->errors[err];
594 next0 = SNAT_IN2OUT_NEXT_DROP;
597 key0.fib_index = rx_fib_index0;
599 if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only))
601 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
602 IP_PROTOCOL_ICMP, rx_fib_index0)))
608 if (icmp_is_error_message (icmp0))
610 next0 = SNAT_IN2OUT_NEXT_DROP;
614 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
615 next0 = SNAT_IN2OUT_NEXT_DROP;
619 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
620 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
621 !icmp_is_error_message (icmp0)))
623 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
624 next0 = SNAT_IN2OUT_NEXT_DROP;
631 *p_proto = key0.protocol;
632 *p_dont_translate = dont_translate;
636 static inline u32 icmp_in2out (snat_main_t *sm,
639 icmp46_header_t * icmp0,
642 vlib_node_runtime_t * node,
648 snat_session_key_t sm0;
650 icmp_echo_header_t *echo0, *inner_echo0 = 0;
651 ip4_header_t *inner_ip0;
653 icmp46_header_t *inner_icmp0;
655 u32 new_addr0, old_addr0;
656 u16 old_id0, new_id0;
661 echo0 = (icmp_echo_header_t *)(icmp0+1);
663 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0,
664 &protocol, &sm0, &dont_translate, d, e);
667 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
670 sum0 = ip_incremental_checksum (0, icmp0,
671 ntohs(ip0->length) - ip4_header_bytes (ip0));
672 checksum0 = ~ip_csum_fold (sum0);
673 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
675 next0 = SNAT_IN2OUT_NEXT_DROP;
679 old_addr0 = ip0->src_address.as_u32;
680 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
681 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
683 sum0 = ip0->checksum;
684 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
685 src_address /* changed member */);
686 ip0->checksum = ip_csum_fold (sum0);
688 if (!icmp_is_error_message (icmp0))
691 if (PREDICT_FALSE(new_id0 != echo0->identifier))
693 old_id0 = echo0->identifier;
695 echo0->identifier = new_id0;
697 sum0 = icmp0->checksum;
698 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
700 icmp0->checksum = ip_csum_fold (sum0);
705 inner_ip0 = (ip4_header_t *)(echo0+1);
706 l4_header = ip4_next_header (inner_ip0);
708 if (!ip4_header_checksum_is_valid (inner_ip0))
710 next0 = SNAT_IN2OUT_NEXT_DROP;
714 old_addr0 = inner_ip0->dst_address.as_u32;
715 inner_ip0->dst_address = sm0.addr;
716 new_addr0 = inner_ip0->dst_address.as_u32;
718 sum0 = icmp0->checksum;
719 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
720 dst_address /* changed member */);
721 icmp0->checksum = ip_csum_fold (sum0);
725 case SNAT_PROTOCOL_ICMP:
726 inner_icmp0 = (icmp46_header_t*)l4_header;
727 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
729 old_id0 = inner_echo0->identifier;
731 inner_echo0->identifier = new_id0;
733 sum0 = icmp0->checksum;
734 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
736 icmp0->checksum = ip_csum_fold (sum0);
738 case SNAT_PROTOCOL_UDP:
739 case SNAT_PROTOCOL_TCP:
740 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
742 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
744 sum0 = icmp0->checksum;
745 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
747 icmp0->checksum = ip_csum_fold (sum0);
761 * Hairpinning allows two endpoints on the internal side of the NAT to
762 * communicate even if they only use each other's external IP addresses
765 * @param sm SNAT main.
766 * @param b0 Vlib buffer.
767 * @param ip0 IP header.
768 * @param udp0 UDP header.
769 * @param tcp0 TCP header.
770 * @param proto0 SNAT protocol.
773 snat_hairpinning (snat_main_t *sm,
780 snat_session_key_t key0, sm0;
781 snat_worker_key_t k0;
783 clib_bihash_kv_8_8_t kv0, value0;
785 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
786 u16 new_dst_port0, old_dst_port0;
788 key0.addr = ip0->dst_address;
789 key0.port = udp0->dst_port;
790 key0.protocol = proto0;
791 key0.fib_index = sm->outside_fib_index;
792 kv0.key = key0.as_u64;
794 /* Check if destination is in active sessions */
795 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
797 /* or static mappings */
798 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0))
800 new_dst_addr0 = sm0.addr.as_u32;
801 new_dst_port0 = sm0.port;
802 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
808 if (sm->num_workers > 1)
810 k0.addr = ip0->dst_address;
811 k0.port = udp0->dst_port;
812 k0.fib_index = sm->outside_fib_index;
814 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
820 ti = sm->num_workers;
822 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
823 new_dst_addr0 = s0->in2out.addr.as_u32;
824 new_dst_port0 = s0->in2out.port;
825 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
828 /* Destination is behind the same NAT, use internal address and port */
831 old_dst_addr0 = ip0->dst_address.as_u32;
832 ip0->dst_address.as_u32 = new_dst_addr0;
833 sum0 = ip0->checksum;
834 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
835 ip4_header_t, dst_address);
836 ip0->checksum = ip_csum_fold (sum0);
838 old_dst_port0 = tcp0->dst;
839 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
841 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
843 tcp0->dst = new_dst_port0;
844 sum0 = tcp0->checksum;
845 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
846 ip4_header_t, dst_address);
847 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
848 ip4_header_t /* cheat */, length);
849 tcp0->checksum = ip_csum_fold(sum0);
853 udp0->dst_port = new_dst_port0;
860 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
863 icmp46_header_t * icmp0,
866 vlib_node_runtime_t * node,
870 snat_session_t ** p_s0)
872 snat_session_key_t key0, sm0;
873 clib_bihash_kv_8_8_t kv0, value0;
874 snat_worker_key_t k0;
875 u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
878 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
879 next0, thread_index, p_s0, 0);
880 snat_session_t * s0 = *p_s0;
881 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
884 if (!icmp_is_error_message (icmp0))
886 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
887 u16 icmp_id0 = echo0->identifier;
888 key0.addr = ip0->dst_address;
889 key0.port = icmp_id0;
890 key0.protocol = SNAT_PROTOCOL_ICMP;
891 key0.fib_index = sm->outside_fib_index;
892 kv0.key = key0.as_u64;
894 /* Check if destination is in active sessions */
895 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
897 /* or static mappings */
898 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0))
900 new_dst_addr0 = sm0.addr.as_u32;
901 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
907 if (sm->num_workers > 1)
909 k0.addr = ip0->dst_address;
911 k0.fib_index = sm->outside_fib_index;
913 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
919 ti = sm->num_workers;
921 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
922 new_dst_addr0 = s0->in2out.addr.as_u32;
923 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
924 echo0->identifier = s0->in2out.port;
925 sum0 = icmp0->checksum;
926 sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
927 icmp_echo_header_t, identifier);
928 icmp0->checksum = ip_csum_fold (sum0);
931 /* Destination is behind the same NAT, use internal address and port */
934 old_dst_addr0 = ip0->dst_address.as_u32;
935 ip0->dst_address.as_u32 = new_dst_addr0;
936 sum0 = ip0->checksum;
937 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
938 ip4_header_t, dst_address);
939 ip0->checksum = ip_csum_fold (sum0);
944 s0->last_heard = now;
946 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
947 /* Per-user LRU list maintenance for dynamic translations */
948 if (!snat_is_session_static (s0))
950 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
952 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
953 s0->per_user_list_head_index,
961 snat_in2out_node_fn_inline (vlib_main_t * vm,
962 vlib_node_runtime_t * node,
963 vlib_frame_t * frame, int is_slow_path)
965 u32 n_left_from, * from, * to_next;
966 snat_in2out_next_t next_index;
967 u32 pkts_processed = 0;
968 snat_main_t * sm = &snat_main;
969 f64 now = vlib_time_now (vm);
970 u32 stats_node_index;
971 u32 thread_index = vlib_get_thread_index ();
973 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
974 snat_in2out_node.index;
976 from = vlib_frame_vector_args (frame);
977 n_left_from = frame->n_vectors;
978 next_index = node->cached_next_index;
980 while (n_left_from > 0)
984 vlib_get_next_frame (vm, node, next_index,
985 to_next, n_left_to_next);
987 while (n_left_from >= 4 && n_left_to_next >= 2)
990 vlib_buffer_t * b0, * b1;
992 u32 sw_if_index0, sw_if_index1;
993 ip4_header_t * ip0, * ip1;
994 ip_csum_t sum0, sum1;
995 u32 new_addr0, old_addr0, new_addr1, old_addr1;
996 u16 old_port0, new_port0, old_port1, new_port1;
997 udp_header_t * udp0, * udp1;
998 tcp_header_t * tcp0, * tcp1;
999 icmp46_header_t * icmp0, * icmp1;
1000 snat_session_key_t key0, key1;
1001 u32 rx_fib_index0, rx_fib_index1;
1003 snat_session_t * s0 = 0, * s1 = 0;
1004 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
1006 /* Prefetch next iteration. */
1008 vlib_buffer_t * p2, * p3;
1010 p2 = vlib_get_buffer (vm, from[2]);
1011 p3 = vlib_get_buffer (vm, from[3]);
1013 vlib_prefetch_buffer_header (p2, LOAD);
1014 vlib_prefetch_buffer_header (p3, LOAD);
1016 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1017 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1020 /* speculatively enqueue b0 and b1 to the current next frame */
1021 to_next[0] = bi0 = from[0];
1022 to_next[1] = bi1 = from[1];
1026 n_left_to_next -= 2;
1028 b0 = vlib_get_buffer (vm, bi0);
1029 b1 = vlib_get_buffer (vm, bi1);
1031 ip0 = vlib_buffer_get_current (b0);
1032 udp0 = ip4_next_header (ip0);
1033 tcp0 = (tcp_header_t *) udp0;
1034 icmp0 = (icmp46_header_t *) udp0;
1036 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1037 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1040 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1042 if (PREDICT_FALSE(ip0->ttl == 1))
1044 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1045 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1046 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1048 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1052 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1054 /* Next configured feature, probably ip4-lookup */
1057 if (PREDICT_FALSE (proto0 == ~0))
1060 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1062 next0 = icmp_in2out_slow_path
1063 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1064 node, next0, now, thread_index, &s0);
1070 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1072 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1077 key0.addr = ip0->src_address;
1078 key0.port = udp0->src_port;
1079 key0.protocol = proto0;
1080 key0.fib_index = rx_fib_index0;
1082 kv0.key = key0.as_u64;
1084 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
1088 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
1089 proto0, rx_fib_index0)))
1092 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1093 &s0, node, next0, thread_index);
1094 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1099 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1104 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1107 old_addr0 = ip0->src_address.as_u32;
1108 ip0->src_address = s0->out2in.addr;
1109 new_addr0 = ip0->src_address.as_u32;
1110 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1112 sum0 = ip0->checksum;
1113 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1115 src_address /* changed member */);
1116 ip0->checksum = ip_csum_fold (sum0);
1118 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1120 old_port0 = tcp0->src_port;
1121 tcp0->src_port = s0->out2in.port;
1122 new_port0 = tcp0->src_port;
1124 sum0 = tcp0->checksum;
1125 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1127 dst_address /* changed member */);
1128 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1129 ip4_header_t /* cheat */,
1130 length /* changed member */);
1131 tcp0->checksum = ip_csum_fold(sum0);
1135 old_port0 = udp0->src_port;
1136 udp0->src_port = s0->out2in.port;
1141 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1144 s0->last_heard = now;
1146 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1147 /* Per-user LRU list maintenance for dynamic translation */
1148 if (!snat_is_session_static (s0))
1150 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1151 s0->per_user_index);
1152 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1153 s0->per_user_list_head_index,
1154 s0->per_user_index);
1158 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1159 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1161 snat_in2out_trace_t *t =
1162 vlib_add_trace (vm, node, b0, sizeof (*t));
1163 t->is_slow_path = is_slow_path;
1164 t->sw_if_index = sw_if_index0;
1165 t->next_index = next0;
1166 t->session_index = ~0;
1168 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1171 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1173 ip1 = vlib_buffer_get_current (b1);
1174 udp1 = ip4_next_header (ip1);
1175 tcp1 = (tcp_header_t *) udp1;
1176 icmp1 = (icmp46_header_t *) udp1;
1178 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1179 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1182 if (PREDICT_FALSE(ip1->ttl == 1))
1184 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1185 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1186 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1188 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1192 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1194 /* Next configured feature, probably ip4-lookup */
1197 if (PREDICT_FALSE (proto1 == ~0))
1200 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1202 next1 = icmp_in2out_slow_path
1203 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1204 next1, now, thread_index, &s1);
1210 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1212 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1217 key1.addr = ip1->src_address;
1218 key1.port = udp1->src_port;
1219 key1.protocol = proto1;
1220 key1.fib_index = rx_fib_index1;
1222 kv1.key = key1.as_u64;
1224 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
1228 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1, ip1,
1229 proto1, rx_fib_index1)))
1232 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1233 &s1, node, next1, thread_index);
1234 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1239 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1244 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1247 old_addr1 = ip1->src_address.as_u32;
1248 ip1->src_address = s1->out2in.addr;
1249 new_addr1 = ip1->src_address.as_u32;
1250 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1252 sum1 = ip1->checksum;
1253 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1255 src_address /* changed member */);
1256 ip1->checksum = ip_csum_fold (sum1);
1258 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1260 old_port1 = tcp1->src_port;
1261 tcp1->src_port = s1->out2in.port;
1262 new_port1 = tcp1->src_port;
1264 sum1 = tcp1->checksum;
1265 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1267 dst_address /* changed member */);
1268 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1269 ip4_header_t /* cheat */,
1270 length /* changed member */);
1271 tcp1->checksum = ip_csum_fold(sum1);
1275 old_port1 = udp1->src_port;
1276 udp1->src_port = s1->out2in.port;
1281 snat_hairpinning (sm, b1, ip1, udp1, tcp1, proto1);
1284 s1->last_heard = now;
1286 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1287 /* Per-user LRU list maintenance for dynamic translation */
1288 if (!snat_is_session_static (s1))
1290 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1291 s1->per_user_index);
1292 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1293 s1->per_user_list_head_index,
1294 s1->per_user_index);
1298 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1299 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1301 snat_in2out_trace_t *t =
1302 vlib_add_trace (vm, node, b1, sizeof (*t));
1303 t->sw_if_index = sw_if_index1;
1304 t->next_index = next1;
1305 t->session_index = ~0;
1307 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1310 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1312 /* verify speculative enqueues, maybe switch current next frame */
1313 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1314 to_next, n_left_to_next,
1315 bi0, bi1, next0, next1);
1318 while (n_left_from > 0 && n_left_to_next > 0)
1326 u32 new_addr0, old_addr0;
1327 u16 old_port0, new_port0;
1328 udp_header_t * udp0;
1329 tcp_header_t * tcp0;
1330 icmp46_header_t * icmp0;
1331 snat_session_key_t key0;
1334 snat_session_t * s0 = 0;
1335 clib_bihash_kv_8_8_t kv0, value0;
1337 /* speculatively enqueue b0 to the current next frame */
1343 n_left_to_next -= 1;
1345 b0 = vlib_get_buffer (vm, bi0);
1346 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1348 ip0 = vlib_buffer_get_current (b0);
1349 udp0 = ip4_next_header (ip0);
1350 tcp0 = (tcp_header_t *) udp0;
1351 icmp0 = (icmp46_header_t *) udp0;
1353 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1354 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1357 if (PREDICT_FALSE(ip0->ttl == 1))
1359 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1360 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1361 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1363 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1367 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1369 /* Next configured feature, probably ip4-lookup */
1372 if (PREDICT_FALSE (proto0 == ~0))
1375 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1377 next0 = icmp_in2out_slow_path
1378 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1379 next0, now, thread_index, &s0);
1385 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1387 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1392 key0.addr = ip0->src_address;
1393 key0.port = udp0->src_port;
1394 key0.protocol = proto0;
1395 key0.fib_index = rx_fib_index0;
1397 kv0.key = key0.as_u64;
1399 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
1403 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
1404 proto0, rx_fib_index0)))
1407 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1408 &s0, node, next0, thread_index);
1410 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1415 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1420 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1423 old_addr0 = ip0->src_address.as_u32;
1424 ip0->src_address = s0->out2in.addr;
1425 new_addr0 = ip0->src_address.as_u32;
1426 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1428 sum0 = ip0->checksum;
1429 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1431 src_address /* changed member */);
1432 ip0->checksum = ip_csum_fold (sum0);
1434 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1436 old_port0 = tcp0->src_port;
1437 tcp0->src_port = s0->out2in.port;
1438 new_port0 = tcp0->src_port;
1440 sum0 = tcp0->checksum;
1441 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1443 dst_address /* changed member */);
1444 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1445 ip4_header_t /* cheat */,
1446 length /* changed member */);
1447 tcp0->checksum = ip_csum_fold(sum0);
1451 old_port0 = udp0->src_port;
1452 udp0->src_port = s0->out2in.port;
1457 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1460 s0->last_heard = now;
1462 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1463 /* Per-user LRU list maintenance for dynamic translation */
1464 if (!snat_is_session_static (s0))
1466 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1467 s0->per_user_index);
1468 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1469 s0->per_user_list_head_index,
1470 s0->per_user_index);
1474 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1475 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1477 snat_in2out_trace_t *t =
1478 vlib_add_trace (vm, node, b0, sizeof (*t));
1479 t->is_slow_path = is_slow_path;
1480 t->sw_if_index = sw_if_index0;
1481 t->next_index = next0;
1482 t->session_index = ~0;
1484 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1487 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1489 /* verify speculative enqueue, maybe switch current next frame */
1490 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1491 to_next, n_left_to_next,
1495 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1498 vlib_node_increment_counter (vm, stats_node_index,
1499 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1501 return frame->n_vectors;
1505 snat_in2out_fast_path_fn (vlib_main_t * vm,
1506 vlib_node_runtime_t * node,
1507 vlib_frame_t * frame)
1509 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
1512 VLIB_REGISTER_NODE (snat_in2out_node) = {
1513 .function = snat_in2out_fast_path_fn,
1514 .name = "snat-in2out",
1515 .vector_size = sizeof (u32),
1516 .format_trace = format_snat_in2out_trace,
1517 .type = VLIB_NODE_TYPE_INTERNAL,
1519 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1520 .error_strings = snat_in2out_error_strings,
1522 .runtime_data_bytes = sizeof (snat_runtime_t),
1524 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1526 /* edit / add dispositions here */
1528 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1529 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1530 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1531 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1535 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1538 snat_in2out_slow_path_fn (vlib_main_t * vm,
1539 vlib_node_runtime_t * node,
1540 vlib_frame_t * frame)
1542 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
1545 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1546 .function = snat_in2out_slow_path_fn,
1547 .name = "snat-in2out-slowpath",
1548 .vector_size = sizeof (u32),
1549 .format_trace = format_snat_in2out_trace,
1550 .type = VLIB_NODE_TYPE_INTERNAL,
1552 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1553 .error_strings = snat_in2out_error_strings,
1555 .runtime_data_bytes = sizeof (snat_runtime_t),
1557 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1559 /* edit / add dispositions here */
1561 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1562 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1563 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1564 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1568 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1570 /**************************/
1571 /*** deterministic mode ***/
1572 /**************************/
1574 snat_det_in2out_node_fn (vlib_main_t * vm,
1575 vlib_node_runtime_t * node,
1576 vlib_frame_t * frame)
1578 u32 n_left_from, * from, * to_next;
1579 snat_in2out_next_t next_index;
1580 u32 pkts_processed = 0;
1581 snat_main_t * sm = &snat_main;
1582 u32 now = (u32) vlib_time_now (vm);
1583 u32 thread_index = vlib_get_thread_index ();
1585 from = vlib_frame_vector_args (frame);
1586 n_left_from = frame->n_vectors;
1587 next_index = node->cached_next_index;
1589 while (n_left_from > 0)
1593 vlib_get_next_frame (vm, node, next_index,
1594 to_next, n_left_to_next);
1596 while (n_left_from >= 4 && n_left_to_next >= 2)
1599 vlib_buffer_t * b0, * b1;
1601 u32 sw_if_index0, sw_if_index1;
1602 ip4_header_t * ip0, * ip1;
1603 ip_csum_t sum0, sum1;
1604 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
1605 u16 old_port0, new_port0, lo_port0, i0;
1606 u16 old_port1, new_port1, lo_port1, i1;
1607 udp_header_t * udp0, * udp1;
1608 tcp_header_t * tcp0, * tcp1;
1610 snat_det_out_key_t key0, key1;
1611 snat_det_map_t * dm0, * dm1;
1612 snat_det_session_t * ses0 = 0, * ses1 = 0;
1613 u32 rx_fib_index0, rx_fib_index1;
1614 icmp46_header_t * icmp0, * icmp1;
1616 /* Prefetch next iteration. */
1618 vlib_buffer_t * p2, * p3;
1620 p2 = vlib_get_buffer (vm, from[2]);
1621 p3 = vlib_get_buffer (vm, from[3]);
1623 vlib_prefetch_buffer_header (p2, LOAD);
1624 vlib_prefetch_buffer_header (p3, LOAD);
1626 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1627 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1630 /* speculatively enqueue b0 and b1 to the current next frame */
1631 to_next[0] = bi0 = from[0];
1632 to_next[1] = bi1 = from[1];
1636 n_left_to_next -= 2;
1638 b0 = vlib_get_buffer (vm, bi0);
1639 b1 = vlib_get_buffer (vm, bi1);
1641 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1642 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1644 ip0 = vlib_buffer_get_current (b0);
1645 udp0 = ip4_next_header (ip0);
1646 tcp0 = (tcp_header_t *) udp0;
1648 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1650 if (PREDICT_FALSE(ip0->ttl == 1))
1652 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1653 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1654 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1656 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1660 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1662 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
1664 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1665 icmp0 = (icmp46_header_t *) udp0;
1667 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
1668 rx_fib_index0, node, next0, thread_index,
1673 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
1674 if (PREDICT_FALSE(!dm0))
1676 clib_warning("no match for internal host %U",
1677 format_ip4_address, &ip0->src_address);
1678 next0 = SNAT_IN2OUT_NEXT_DROP;
1679 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1683 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
1685 key0.ext_host_addr = ip0->dst_address;
1686 key0.ext_host_port = tcp0->dst;
1688 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
1689 if (PREDICT_FALSE(!ses0))
1691 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
1693 key0.out_port = clib_host_to_net_u16 (lo_port0 +
1694 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
1696 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
1699 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
1702 if (PREDICT_FALSE(!ses0))
1704 /* too many sessions for user, send ICMP error packet */
1706 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1707 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
1708 ICMP4_destination_unreachable_destination_unreachable_host,
1710 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1715 new_port0 = ses0->out.out_port;
1717 old_addr0.as_u32 = ip0->src_address.as_u32;
1718 ip0->src_address.as_u32 = new_addr0.as_u32;
1719 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1721 sum0 = ip0->checksum;
1722 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1724 src_address /* changed member */);
1725 ip0->checksum = ip_csum_fold (sum0);
1727 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1729 if (tcp0->flags & TCP_FLAG_SYN)
1730 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
1731 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
1732 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1733 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
1734 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
1735 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
1736 snat_det_ses_close(dm0, ses0);
1737 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1738 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
1739 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
1740 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1742 old_port0 = tcp0->src;
1743 tcp0->src = new_port0;
1745 sum0 = tcp0->checksum;
1746 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1748 dst_address /* changed member */);
1749 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1750 ip4_header_t /* cheat */,
1751 length /* changed member */);
1752 tcp0->checksum = ip_csum_fold(sum0);
1756 ses0->state = SNAT_SESSION_UDP_ACTIVE;
1757 old_port0 = udp0->src_port;
1758 udp0->src_port = new_port0;
1764 case SNAT_SESSION_UDP_ACTIVE:
1765 ses0->expire = now + sm->udp_timeout;
1767 case SNAT_SESSION_TCP_SYN_SENT:
1768 case SNAT_SESSION_TCP_FIN_WAIT:
1769 case SNAT_SESSION_TCP_CLOSE_WAIT:
1770 case SNAT_SESSION_TCP_LAST_ACK:
1771 ses0->expire = now + sm->tcp_transitory_timeout;
1773 case SNAT_SESSION_TCP_ESTABLISHED:
1774 ses0->expire = now + sm->tcp_established_timeout;
1779 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1780 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1782 snat_in2out_trace_t *t =
1783 vlib_add_trace (vm, node, b0, sizeof (*t));
1784 t->is_slow_path = 0;
1785 t->sw_if_index = sw_if_index0;
1786 t->next_index = next0;
1787 t->session_index = ~0;
1789 t->session_index = ses0 - dm0->sessions;
1792 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1794 ip1 = vlib_buffer_get_current (b1);
1795 udp1 = ip4_next_header (ip1);
1796 tcp1 = (tcp_header_t *) udp1;
1798 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1800 if (PREDICT_FALSE(ip1->ttl == 1))
1802 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1803 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1804 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1806 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1810 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1812 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
1814 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
1815 icmp1 = (icmp46_header_t *) udp1;
1817 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
1818 rx_fib_index1, node, next1, thread_index,
1823 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
1824 if (PREDICT_FALSE(!dm1))
1826 clib_warning("no match for internal host %U",
1827 format_ip4_address, &ip0->src_address);
1828 next1 = SNAT_IN2OUT_NEXT_DROP;
1829 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1833 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
1835 key1.ext_host_addr = ip1->dst_address;
1836 key1.ext_host_port = tcp1->dst;
1838 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src, key1);
1839 if (PREDICT_FALSE(!ses1))
1841 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
1843 key1.out_port = clib_host_to_net_u16 (lo_port1 +
1844 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
1846 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
1849 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
1852 if (PREDICT_FALSE(!ses1))
1854 /* too many sessions for user, send ICMP error packet */
1856 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1857 icmp4_error_set_vnet_buffer (b1, ICMP4_destination_unreachable,
1858 ICMP4_destination_unreachable_destination_unreachable_host,
1860 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1865 new_port1 = ses1->out.out_port;
1867 old_addr1.as_u32 = ip1->src_address.as_u32;
1868 ip1->src_address.as_u32 = new_addr1.as_u32;
1869 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1871 sum1 = ip1->checksum;
1872 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1874 src_address /* changed member */);
1875 ip1->checksum = ip_csum_fold (sum1);
1877 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1879 if (tcp1->flags & TCP_FLAG_SYN)
1880 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
1881 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
1882 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1883 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
1884 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
1885 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
1886 snat_det_ses_close(dm1, ses1);
1887 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1888 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
1889 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
1890 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1892 old_port1 = tcp1->src;
1893 tcp1->src = new_port1;
1895 sum1 = tcp1->checksum;
1896 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1898 dst_address /* changed member */);
1899 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1900 ip4_header_t /* cheat */,
1901 length /* changed member */);
1902 tcp1->checksum = ip_csum_fold(sum1);
1906 ses1->state = SNAT_SESSION_UDP_ACTIVE;
1907 old_port1 = udp1->src_port;
1908 udp1->src_port = new_port1;
1914 case SNAT_SESSION_UDP_ACTIVE:
1915 ses1->expire = now + sm->udp_timeout;
1917 case SNAT_SESSION_TCP_SYN_SENT:
1918 case SNAT_SESSION_TCP_FIN_WAIT:
1919 case SNAT_SESSION_TCP_CLOSE_WAIT:
1920 case SNAT_SESSION_TCP_LAST_ACK:
1921 ses1->expire = now + sm->tcp_transitory_timeout;
1923 case SNAT_SESSION_TCP_ESTABLISHED:
1924 ses1->expire = now + sm->tcp_established_timeout;
1929 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1930 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1932 snat_in2out_trace_t *t =
1933 vlib_add_trace (vm, node, b1, sizeof (*t));
1934 t->is_slow_path = 0;
1935 t->sw_if_index = sw_if_index1;
1936 t->next_index = next1;
1937 t->session_index = ~0;
1939 t->session_index = ses1 - dm1->sessions;
1942 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1944 /* verify speculative enqueues, maybe switch current next frame */
1945 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1946 to_next, n_left_to_next,
1947 bi0, bi1, next0, next1);
1950 while (n_left_from > 0 && n_left_to_next > 0)
1958 ip4_address_t new_addr0, old_addr0;
1959 u16 old_port0, new_port0, lo_port0, i0;
1960 udp_header_t * udp0;
1961 tcp_header_t * tcp0;
1963 snat_det_out_key_t key0;
1964 snat_det_map_t * dm0;
1965 snat_det_session_t * ses0 = 0;
1967 icmp46_header_t * icmp0;
1969 /* speculatively enqueue b0 to the current next frame */
1975 n_left_to_next -= 1;
1977 b0 = vlib_get_buffer (vm, bi0);
1978 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1980 ip0 = vlib_buffer_get_current (b0);
1981 udp0 = ip4_next_header (ip0);
1982 tcp0 = (tcp_header_t *) udp0;
1984 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1986 if (PREDICT_FALSE(ip0->ttl == 1))
1988 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1989 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1990 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1992 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1996 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1998 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2000 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2001 icmp0 = (icmp46_header_t *) udp0;
2003 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2004 rx_fib_index0, node, next0, thread_index,
2009 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2010 if (PREDICT_FALSE(!dm0))
2012 clib_warning("no match for internal host %U",
2013 format_ip4_address, &ip0->src_address);
2014 next0 = SNAT_IN2OUT_NEXT_DROP;
2015 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2019 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2021 key0.ext_host_addr = ip0->dst_address;
2022 key0.ext_host_port = tcp0->dst;
2024 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
2025 if (PREDICT_FALSE(!ses0))
2027 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2029 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2030 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
2032 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
2035 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
2038 if (PREDICT_FALSE(!ses0))
2040 /* too many sessions for user, send ICMP error packet */
2042 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2043 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
2044 ICMP4_destination_unreachable_destination_unreachable_host,
2046 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2051 new_port0 = ses0->out.out_port;
2053 old_addr0.as_u32 = ip0->src_address.as_u32;
2054 ip0->src_address.as_u32 = new_addr0.as_u32;
2055 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2057 sum0 = ip0->checksum;
2058 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2060 src_address /* changed member */);
2061 ip0->checksum = ip_csum_fold (sum0);
2063 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2065 if (tcp0->flags & TCP_FLAG_SYN)
2066 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
2067 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
2068 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2069 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2070 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
2071 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
2072 snat_det_ses_close(dm0, ses0);
2073 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2074 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
2075 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
2076 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2078 old_port0 = tcp0->src;
2079 tcp0->src = new_port0;
2081 sum0 = tcp0->checksum;
2082 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2084 dst_address /* changed member */);
2085 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2086 ip4_header_t /* cheat */,
2087 length /* changed member */);
2088 tcp0->checksum = ip_csum_fold(sum0);
2092 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2093 old_port0 = udp0->src_port;
2094 udp0->src_port = new_port0;
2100 case SNAT_SESSION_UDP_ACTIVE:
2101 ses0->expire = now + sm->udp_timeout;
2103 case SNAT_SESSION_TCP_SYN_SENT:
2104 case SNAT_SESSION_TCP_FIN_WAIT:
2105 case SNAT_SESSION_TCP_CLOSE_WAIT:
2106 case SNAT_SESSION_TCP_LAST_ACK:
2107 ses0->expire = now + sm->tcp_transitory_timeout;
2109 case SNAT_SESSION_TCP_ESTABLISHED:
2110 ses0->expire = now + sm->tcp_established_timeout;
2115 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2116 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2118 snat_in2out_trace_t *t =
2119 vlib_add_trace (vm, node, b0, sizeof (*t));
2120 t->is_slow_path = 0;
2121 t->sw_if_index = sw_if_index0;
2122 t->next_index = next0;
2123 t->session_index = ~0;
2125 t->session_index = ses0 - dm0->sessions;
2128 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2130 /* verify speculative enqueue, maybe switch current next frame */
2131 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2132 to_next, n_left_to_next,
2136 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2139 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
2140 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2142 return frame->n_vectors;
2145 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
2146 .function = snat_det_in2out_node_fn,
2147 .name = "snat-det-in2out",
2148 .vector_size = sizeof (u32),
2149 .format_trace = format_snat_in2out_trace,
2150 .type = VLIB_NODE_TYPE_INTERNAL,
2152 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2153 .error_strings = snat_in2out_error_strings,
2155 .runtime_data_bytes = sizeof (snat_runtime_t),
2159 /* edit / add dispositions here */
2161 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2162 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2163 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2167 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
2170 * Get address and port values to be used for packet SNAT translation
2171 * and create session if needed
2173 * @param[in,out] sm SNAT main
2174 * @param[in,out] node SNAT node runtime
2175 * @param[in] thread_index thread index
2176 * @param[in,out] b0 buffer containing packet to be translated
2177 * @param[out] p_proto protocol used for matching
2178 * @param[out] p_value address and port after NAT translation
2179 * @param[out] p_dont_translate if packet should not be translated
2180 * @param d optional parameter
2181 * @param e optional parameter
2183 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
2184 u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
2185 snat_session_key_t *p_value,
2186 u8 *p_dont_translate, void *d, void *e)
2189 icmp46_header_t *icmp0;
2193 snat_det_out_key_t key0;
2194 u8 dont_translate = 0;
2196 icmp_echo_header_t *echo0, *inner_echo0 = 0;
2197 ip4_header_t *inner_ip0;
2198 void *l4_header = 0;
2199 icmp46_header_t *inner_icmp0;
2200 snat_det_map_t * dm0 = 0;
2201 ip4_address_t new_addr0;
2203 snat_det_session_t * ses0 = 0;
2204 ip4_address_t in_addr;
2207 ip0 = vlib_buffer_get_current (b0);
2208 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
2209 echo0 = (icmp_echo_header_t *)(icmp0+1);
2210 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2211 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2213 if (!icmp_is_error_message (icmp0))
2215 protocol = SNAT_PROTOCOL_ICMP;
2216 in_addr = ip0->src_address;
2217 in_port = echo0->identifier;
2221 inner_ip0 = (ip4_header_t *)(echo0+1);
2222 l4_header = ip4_next_header (inner_ip0);
2223 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
2224 in_addr = inner_ip0->dst_address;
2227 case SNAT_PROTOCOL_ICMP:
2228 inner_icmp0 = (icmp46_header_t*)l4_header;
2229 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
2230 in_port = inner_echo0->identifier;
2232 case SNAT_PROTOCOL_UDP:
2233 case SNAT_PROTOCOL_TCP:
2234 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
2237 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
2238 next0 = SNAT_IN2OUT_NEXT_DROP;
2243 dm0 = snat_det_map_by_user(sm, &in_addr);
2244 if (PREDICT_FALSE(!dm0))
2246 clib_warning("no match for internal host %U",
2247 format_ip4_address, &in_addr);
2248 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
2249 IP_PROTOCOL_ICMP, rx_fib_index0)))
2254 next0 = SNAT_IN2OUT_NEXT_DROP;
2255 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2259 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
2261 key0.ext_host_addr = ip0->dst_address;
2262 key0.ext_host_port = 0;
2264 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port, key0);
2265 if (PREDICT_FALSE(!ses0))
2267 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
2268 IP_PROTOCOL_ICMP, rx_fib_index0)))
2273 if (icmp0->type != ICMP4_echo_request)
2275 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2276 next0 = SNAT_IN2OUT_NEXT_DROP;
2279 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2281 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2282 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
2284 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
2287 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
2290 if (PREDICT_FALSE(!ses0))
2292 next0 = SNAT_IN2OUT_NEXT_DROP;
2293 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
2298 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
2299 !icmp_is_error_message (icmp0)))
2301 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
2302 next0 = SNAT_IN2OUT_NEXT_DROP;
2306 u32 now = (u32) vlib_time_now (sm->vlib_main);
2308 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
2309 ses0->expire = now + sm->icmp_timeout;
2312 *p_proto = protocol;
2315 p_value->addr = new_addr0;
2316 p_value->fib_index = sm->outside_fib_index;
2317 p_value->port = ses0->out.out_port;
2319 *p_dont_translate = dont_translate;
2321 *(snat_det_session_t**)d = ses0;
2323 *(snat_det_map_t**)e = dm0;
2327 /**********************/
2328 /*** worker handoff ***/
2329 /**********************/
2331 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
2332 vlib_node_runtime_t * node,
2333 vlib_frame_t * frame)
2335 snat_main_t *sm = &snat_main;
2336 vlib_thread_main_t *tm = vlib_get_thread_main ();
2337 u32 n_left_from, *from, *to_next = 0;
2338 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
2339 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
2341 vlib_frame_queue_elt_t *hf = 0;
2342 vlib_frame_t *f = 0;
2344 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
2345 u32 next_worker_index = 0;
2346 u32 current_worker_index = ~0;
2347 u32 thread_index = vlib_get_thread_index ();
2349 ASSERT (vec_len (sm->workers));
2351 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
2353 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
2355 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
2356 sm->first_worker_index + sm->num_workers - 1,
2357 (vlib_frame_queue_t *) (~0));
2360 from = vlib_frame_vector_args (frame);
2361 n_left_from = frame->n_vectors;
2363 while (n_left_from > 0)
2376 b0 = vlib_get_buffer (vm, bi0);
2378 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2379 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2381 ip0 = vlib_buffer_get_current (b0);
2383 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
2385 if (PREDICT_FALSE (next_worker_index != thread_index))
2389 if (next_worker_index != current_worker_index)
2392 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2394 hf = vlib_get_worker_handoff_queue_elt (sm->fq_in2out_index,
2396 handoff_queue_elt_by_worker_index);
2398 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
2399 to_next_worker = &hf->buffer_index[hf->n_vectors];
2400 current_worker_index = next_worker_index;
2403 /* enqueue to correct worker thread */
2404 to_next_worker[0] = bi0;
2406 n_left_to_next_worker--;
2408 if (n_left_to_next_worker == 0)
2410 hf->n_vectors = VLIB_FRAME_SIZE;
2411 vlib_put_frame_queue_elt (hf);
2412 current_worker_index = ~0;
2413 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
2420 /* if this is 1st frame */
2423 f = vlib_get_frame_to_node (vm, sm->in2out_node_index);
2424 to_next = vlib_frame_vector_args (f);
2432 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2433 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2435 snat_in2out_worker_handoff_trace_t *t =
2436 vlib_add_trace (vm, node, b0, sizeof (*t));
2437 t->next_worker_index = next_worker_index;
2438 t->do_handoff = do_handoff;
2443 vlib_put_frame_to_node (vm, sm->in2out_node_index, f);
2446 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2448 /* Ship frames to the worker nodes */
2449 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
2451 if (handoff_queue_elt_by_worker_index[i])
2453 hf = handoff_queue_elt_by_worker_index[i];
2455 * It works better to let the handoff node
2456 * rate-adapt, always ship the handoff queue element.
2458 if (1 || hf->n_vectors == hf->last_n_vectors)
2460 vlib_put_frame_queue_elt (hf);
2461 handoff_queue_elt_by_worker_index[i] = 0;
2464 hf->last_n_vectors = hf->n_vectors;
2466 congested_handoff_queue_by_worker_index[i] =
2467 (vlib_frame_queue_t *) (~0);
2470 current_worker_index = ~0;
2471 return frame->n_vectors;
2474 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
2475 .function = snat_in2out_worker_handoff_fn,
2476 .name = "snat-in2out-worker-handoff",
2477 .vector_size = sizeof (u32),
2478 .format_trace = format_snat_in2out_worker_handoff_trace,
2479 .type = VLIB_NODE_TYPE_INTERNAL,
2488 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn);
2491 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2492 vlib_node_runtime_t * node,
2493 vlib_frame_t * frame)
2495 u32 n_left_from, * from, * to_next;
2496 snat_in2out_next_t next_index;
2497 u32 pkts_processed = 0;
2498 snat_main_t * sm = &snat_main;
2499 u32 stats_node_index;
2501 stats_node_index = snat_in2out_fast_node.index;
2503 from = vlib_frame_vector_args (frame);
2504 n_left_from = frame->n_vectors;
2505 next_index = node->cached_next_index;
2507 while (n_left_from > 0)
2511 vlib_get_next_frame (vm, node, next_index,
2512 to_next, n_left_to_next);
2514 while (n_left_from > 0 && n_left_to_next > 0)
2522 u32 new_addr0, old_addr0;
2523 u16 old_port0, new_port0;
2524 udp_header_t * udp0;
2525 tcp_header_t * tcp0;
2526 icmp46_header_t * icmp0;
2527 snat_session_key_t key0, sm0;
2531 /* speculatively enqueue b0 to the current next frame */
2537 n_left_to_next -= 1;
2539 b0 = vlib_get_buffer (vm, bi0);
2540 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2542 ip0 = vlib_buffer_get_current (b0);
2543 udp0 = ip4_next_header (ip0);
2544 tcp0 = (tcp_header_t *) udp0;
2545 icmp0 = (icmp46_header_t *) udp0;
2547 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2548 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2550 if (PREDICT_FALSE(ip0->ttl == 1))
2552 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2553 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2554 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2556 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2560 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2562 if (PREDICT_FALSE (proto0 == ~0))
2565 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2567 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2568 rx_fib_index0, node, next0, ~0, 0, 0);
2572 key0.addr = ip0->src_address;
2573 key0.port = udp0->src_port;
2574 key0.fib_index = rx_fib_index0;
2576 if (snat_static_mapping_match(sm, key0, &sm0, 0, 0))
2578 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2579 next0= SNAT_IN2OUT_NEXT_DROP;
2583 new_addr0 = sm0.addr.as_u32;
2584 new_port0 = sm0.port;
2585 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2586 old_addr0 = ip0->src_address.as_u32;
2587 ip0->src_address.as_u32 = new_addr0;
2589 sum0 = ip0->checksum;
2590 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2592 src_address /* changed member */);
2593 ip0->checksum = ip_csum_fold (sum0);
2595 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
2597 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2599 old_port0 = tcp0->src_port;
2600 tcp0->src_port = new_port0;
2602 sum0 = tcp0->checksum;
2603 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2605 dst_address /* changed member */);
2606 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2607 ip4_header_t /* cheat */,
2608 length /* changed member */);
2609 tcp0->checksum = ip_csum_fold(sum0);
2613 old_port0 = udp0->src_port;
2614 udp0->src_port = new_port0;
2620 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2622 sum0 = tcp0->checksum;
2623 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2625 dst_address /* changed member */);
2626 tcp0->checksum = ip_csum_fold(sum0);
2631 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
2634 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2635 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2637 snat_in2out_trace_t *t =
2638 vlib_add_trace (vm, node, b0, sizeof (*t));
2639 t->sw_if_index = sw_if_index0;
2640 t->next_index = next0;
2643 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2645 /* verify speculative enqueue, maybe switch current next frame */
2646 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2647 to_next, n_left_to_next,
2651 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2654 vlib_node_increment_counter (vm, stats_node_index,
2655 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2657 return frame->n_vectors;
2661 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2662 .function = snat_in2out_fast_static_map_fn,
2663 .name = "snat-in2out-fast",
2664 .vector_size = sizeof (u32),
2665 .format_trace = format_snat_in2out_fast_trace,
2666 .type = VLIB_NODE_TYPE_INTERNAL,
2668 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2669 .error_strings = snat_in2out_error_strings,
2671 .runtime_data_bytes = sizeof (snat_runtime_t),
2673 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2675 /* edit / add dispositions here */
2677 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2678 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2679 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
2680 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2684 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);