2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/handoff.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/fib/ip4_fib.h>
25 #include <nat/nat_ipfix_logging.h>
26 #include <nat/nat_det.h>
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 ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_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, "NAT44_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, "NAT44_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;
91 vlib_node_registration_t snat_in2out_output_node;
92 vlib_node_registration_t snat_in2out_output_slowpath_node;
93 vlib_node_registration_t snat_in2out_output_worker_handoff_node;
94 vlib_node_registration_t snat_hairpin_dst_node;
95 vlib_node_registration_t snat_hairpin_src_node;
96 vlib_node_registration_t nat44_hairpinning_node;
99 #define foreach_snat_in2out_error \
100 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
101 _(IN2OUT_PACKETS, "Good in2out packets processed") \
102 _(OUT_OF_PORTS, "Out of ports") \
103 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
104 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
105 _(NO_TRANSLATION, "No translation") \
106 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded")
109 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
110 foreach_snat_in2out_error
113 } snat_in2out_error_t;
115 static char * snat_in2out_error_strings[] = {
116 #define _(sym,string) string,
117 foreach_snat_in2out_error
122 SNAT_IN2OUT_NEXT_LOOKUP,
123 SNAT_IN2OUT_NEXT_DROP,
124 SNAT_IN2OUT_NEXT_ICMP_ERROR,
125 SNAT_IN2OUT_NEXT_SLOW_PATH,
127 } snat_in2out_next_t;
130 SNAT_HAIRPIN_SRC_NEXT_DROP,
131 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT,
132 SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH,
133 SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT,
134 SNAT_HAIRPIN_SRC_N_NEXT,
135 } snat_hairpin_next_t;
138 * @brief Check if packet should be translated
140 * Packets aimed at outside interface and external addresss with active session
141 * should be translated.
144 * @param rt NAT runtime data
145 * @param sw_if_index0 index of the inside interface
146 * @param ip0 IPv4 header
147 * @param proto0 NAT protocol
148 * @param rx_fib_index0 RX FIB index
150 * @returns 0 if packet should be translated otherwise 1
153 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t *node,
154 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
157 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
159 .fp_proto = FIB_PROTOCOL_IP4,
162 .ip4.as_u32 = ip0->dst_address.as_u32,
166 /* Don't NAT packet aimed at the intfc address */
167 if (PREDICT_FALSE(is_interface_addr(sm, node, sw_if_index0,
168 ip0->dst_address.as_u32)))
171 fei = fib_table_lookup (rx_fib_index0, &pfx);
172 if (FIB_NODE_INDEX_INVALID != fei)
174 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
175 if (sw_if_index == ~0)
177 fei = fib_table_lookup (sm->outside_fib_index, &pfx);
178 if (FIB_NODE_INDEX_INVALID != fei)
179 sw_if_index = fib_entry_get_resolving_interface (fei);
182 pool_foreach (i, sm->interfaces,
184 /* NAT packet aimed at outside interface */
185 if ((nat_interface_is_outside(i)) && (sw_if_index == i->sw_if_index))
194 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t *node,
195 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
196 u32 rx_fib_index0, u32 thread_index)
198 udp_header_t * udp0 = ip4_next_header (ip0);
199 snat_session_key_t key0, sm0;
200 clib_bihash_kv_8_8_t kv0, value0;
202 key0.addr = ip0->dst_address;
203 key0.port = udp0->dst_port;
204 key0.protocol = proto0;
205 key0.fib_index = sm->outside_fib_index;
206 kv0.key = key0.as_u64;
208 /* NAT packet aimed at external address if */
209 /* has active sessions */
210 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
213 /* or is static mappings */
214 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0))
220 return snat_not_translate_fast(sm, node, sw_if_index0, ip0, proto0,
224 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
227 snat_session_key_t * key0,
228 snat_session_t ** sessionp,
229 vlib_node_runtime_t * node,
234 snat_user_key_t user_key;
236 clib_bihash_kv_8_8_t kv0, value0;
237 u32 oldest_per_user_translation_list_index;
238 dlist_elt_t * oldest_per_user_translation_list_elt;
239 dlist_elt_t * per_user_translation_list_elt;
240 dlist_elt_t * per_user_list_head_elt;
242 snat_session_key_t key1;
243 u32 address_index = ~0;
244 u32 outside_fib_index;
247 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
249 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
250 return SNAT_IN2OUT_NEXT_DROP;
253 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
256 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
257 return SNAT_IN2OUT_NEXT_DROP;
259 outside_fib_index = p[0];
261 key1.protocol = key0->protocol;
262 user_key.addr = ip0->src_address;
263 user_key.fib_index = rx_fib_index0;
264 kv0.key = user_key.as_u64;
266 /* Ever heard of the "user" = src ip4 address before? */
267 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].user_hash,
270 /* no, make a new one */
271 pool_get (sm->per_thread_data[thread_index].users, u);
272 memset (u, 0, sizeof (*u));
273 u->addr = ip0->src_address;
274 u->fib_index = rx_fib_index0;
276 pool_get (sm->per_thread_data[thread_index].list_pool, per_user_list_head_elt);
278 u->sessions_per_user_list_head_index = per_user_list_head_elt -
279 sm->per_thread_data[thread_index].list_pool;
281 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
282 u->sessions_per_user_list_head_index);
284 kv0.value = u - sm->per_thread_data[thread_index].users;
287 clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].user_hash,
288 &kv0, 1 /* is_add */);
292 u = pool_elt_at_index (sm->per_thread_data[thread_index].users,
296 /* Over quota? Recycle the least recently used dynamic translation */
297 if (u->nsessions >= sm->max_translations_per_user)
299 /* Remove the oldest dynamic translation */
301 oldest_per_user_translation_list_index =
302 clib_dlist_remove_head (sm->per_thread_data[thread_index].list_pool,
303 u->sessions_per_user_list_head_index);
305 ASSERT (oldest_per_user_translation_list_index != ~0);
307 /* add it back to the end of the LRU list */
308 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
309 u->sessions_per_user_list_head_index,
310 oldest_per_user_translation_list_index);
311 /* Get the list element */
312 oldest_per_user_translation_list_elt =
313 pool_elt_at_index (sm->per_thread_data[thread_index].list_pool,
314 oldest_per_user_translation_list_index);
316 /* Get the session index from the list element */
317 session_index = oldest_per_user_translation_list_elt->value;
319 /* Get the session */
320 s = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
322 } while (snat_is_session_static (s));
324 if (snat_is_unk_proto_session (s))
326 clib_bihash_kv_16_8_t up_kv;
327 nat_ed_ses_key_t key;
329 /* Remove from lookup tables */
330 key.l_addr = s->in2out.addr;
331 key.r_addr = s->ext_host_addr;
332 key.fib_index = s->in2out.fib_index;
333 key.proto = s->in2out.port;
336 up_kv.key[0] = key.as_u64[0];
337 up_kv.key[1] = key.as_u64[1];
338 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &up_kv, 0))
339 clib_warning ("in2out key del failed");
341 key.l_addr = s->out2in.addr;
342 key.fib_index = s->out2in.fib_index;
343 up_kv.key[0] = key.as_u64[0];
344 up_kv.key[1] = key.as_u64[1];
345 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &up_kv, 0))
346 clib_warning ("out2in key del failed");
350 /* Remove in2out, out2in keys */
351 kv0.key = s->in2out.as_u64;
352 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out,
353 &kv0, 0 /* is_add */))
354 clib_warning ("in2out key delete failed");
355 kv0.key = s->out2in.as_u64;
356 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in,
357 &kv0, 0 /* is_add */))
358 clib_warning ("out2in key delete failed");
361 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
362 s->out2in.addr.as_u32,
366 s->in2out.fib_index);
368 snat_free_outside_address_and_port
369 (sm, thread_index, &s->out2in, s->outside_address_index);
371 s->outside_address_index = ~0;
373 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0, thread_index,
374 &key1, &address_index))
378 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
379 return SNAT_IN2OUT_NEXT_DROP;
381 s->outside_address_index = address_index;
385 u8 static_mapping = 1;
387 /* First try to match static mapping by local address and port */
388 if (snat_static_mapping_match (sm, *key0, &key1, 0, 0))
391 /* Try to create dynamic translation */
392 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0,
396 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
397 return SNAT_IN2OUT_NEXT_DROP;
401 /* Create a new session */
402 pool_get (sm->per_thread_data[thread_index].sessions, s);
403 memset (s, 0, sizeof (*s));
405 s->outside_address_index = address_index;
409 u->nstaticsessions++;
410 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
417 /* Create list elts */
418 pool_get (sm->per_thread_data[thread_index].list_pool,
419 per_user_translation_list_elt);
420 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
421 per_user_translation_list_elt -
422 sm->per_thread_data[thread_index].list_pool);
424 per_user_translation_list_elt->value =
425 s - sm->per_thread_data[thread_index].sessions;
426 s->per_user_index = per_user_translation_list_elt -
427 sm->per_thread_data[thread_index].list_pool;
428 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
430 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
431 s->per_user_list_head_index,
432 per_user_translation_list_elt -
433 sm->per_thread_data[thread_index].list_pool);
438 s->out2in.protocol = key0->protocol;
439 s->out2in.fib_index = outside_fib_index;
440 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
443 /* Add to translation hashes */
444 kv0.key = s->in2out.as_u64;
445 kv0.value = s - sm->per_thread_data[thread_index].sessions;
446 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
448 clib_warning ("in2out key add failed");
450 kv0.key = s->out2in.as_u64;
451 kv0.value = s - sm->per_thread_data[thread_index].sessions;
453 if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
455 clib_warning ("out2in key add failed");
458 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
459 s->out2in.addr.as_u32,
463 s->in2out.fib_index);
468 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
469 snat_session_key_t *p_key0)
471 icmp46_header_t *icmp0;
472 snat_session_key_t key0;
473 icmp_echo_header_t *echo0, *inner_echo0 = 0;
474 ip4_header_t *inner_ip0 = 0;
476 icmp46_header_t *inner_icmp0;
478 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
479 echo0 = (icmp_echo_header_t *)(icmp0+1);
481 if (!icmp_is_error_message (icmp0))
483 key0.protocol = SNAT_PROTOCOL_ICMP;
484 key0.addr = ip0->src_address;
485 key0.port = echo0->identifier;
489 inner_ip0 = (ip4_header_t *)(echo0+1);
490 l4_header = ip4_next_header (inner_ip0);
491 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
492 key0.addr = inner_ip0->dst_address;
493 switch (key0.protocol)
495 case SNAT_PROTOCOL_ICMP:
496 inner_icmp0 = (icmp46_header_t*)l4_header;
497 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
498 key0.port = inner_echo0->identifier;
500 case SNAT_PROTOCOL_UDP:
501 case SNAT_PROTOCOL_TCP:
502 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
505 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
509 return -1; /* success */
513 * Get address and port values to be used for ICMP packet translation
514 * and create session if needed
516 * @param[in,out] sm NAT main
517 * @param[in,out] node NAT node runtime
518 * @param[in] thread_index thread index
519 * @param[in,out] b0 buffer containing packet to be translated
520 * @param[out] p_proto protocol used for matching
521 * @param[out] p_value address and port after NAT translation
522 * @param[out] p_dont_translate if packet should not be translated
523 * @param d optional parameter
524 * @param e optional parameter
526 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
527 u32 thread_index, vlib_buffer_t *b0,
528 ip4_header_t *ip0, u8 *p_proto,
529 snat_session_key_t *p_value,
530 u8 *p_dont_translate, void *d, void *e)
532 icmp46_header_t *icmp0;
535 snat_session_key_t key0;
536 snat_session_t *s0 = 0;
537 u8 dont_translate = 0;
538 clib_bihash_kv_8_8_t kv0, value0;
542 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
543 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
544 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
546 err = icmp_get_key (ip0, &key0);
549 b0->error = node->errors[err];
550 next0 = SNAT_IN2OUT_NEXT_DROP;
553 key0.fib_index = rx_fib_index0;
555 kv0.key = key0.as_u64;
557 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
560 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0, ip0,
561 IP_PROTOCOL_ICMP, rx_fib_index0, thread_index) &&
562 vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0))
568 if (PREDICT_FALSE(icmp_is_error_message (icmp0)))
570 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
571 next0 = SNAT_IN2OUT_NEXT_DROP;
575 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
576 &s0, node, next0, thread_index);
578 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
583 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
584 icmp0->type != ICMP4_echo_reply &&
585 !icmp_is_error_message (icmp0)))
587 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
588 next0 = SNAT_IN2OUT_NEXT_DROP;
592 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
597 *p_proto = key0.protocol;
599 *p_value = s0->out2in;
600 *p_dont_translate = dont_translate;
602 *(snat_session_t**)d = s0;
607 * Get address and port values to be used for ICMP packet translation
609 * @param[in] sm NAT main
610 * @param[in,out] node NAT node runtime
611 * @param[in] thread_index thread index
612 * @param[in,out] b0 buffer containing packet to be translated
613 * @param[out] p_proto protocol used for matching
614 * @param[out] p_value address and port after NAT translation
615 * @param[out] p_dont_translate if packet should not be translated
616 * @param d optional parameter
617 * @param e optional parameter
619 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
620 u32 thread_index, vlib_buffer_t *b0,
621 ip4_header_t *ip0, u8 *p_proto,
622 snat_session_key_t *p_value,
623 u8 *p_dont_translate, void *d, void *e)
625 icmp46_header_t *icmp0;
628 snat_session_key_t key0;
629 snat_session_key_t sm0;
630 u8 dont_translate = 0;
635 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
636 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
637 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
639 err = icmp_get_key (ip0, &key0);
642 b0->error = node->errors[err];
643 next0 = SNAT_IN2OUT_NEXT_DROP;
646 key0.fib_index = rx_fib_index0;
648 if (snat_static_mapping_match(sm, key0, &sm0, 0, &is_addr_only))
650 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
651 IP_PROTOCOL_ICMP, rx_fib_index0)))
657 if (icmp_is_error_message (icmp0))
659 next0 = SNAT_IN2OUT_NEXT_DROP;
663 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
664 next0 = SNAT_IN2OUT_NEXT_DROP;
668 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
669 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
670 !icmp_is_error_message (icmp0)))
672 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
673 next0 = SNAT_IN2OUT_NEXT_DROP;
680 *p_proto = key0.protocol;
681 *p_dont_translate = dont_translate;
685 static inline u32 icmp_in2out (snat_main_t *sm,
688 icmp46_header_t * icmp0,
691 vlib_node_runtime_t * node,
697 snat_session_key_t sm0;
699 icmp_echo_header_t *echo0, *inner_echo0 = 0;
700 ip4_header_t *inner_ip0;
702 icmp46_header_t *inner_icmp0;
704 u32 new_addr0, old_addr0;
705 u16 old_id0, new_id0;
710 echo0 = (icmp_echo_header_t *)(icmp0+1);
712 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0, ip0,
713 &protocol, &sm0, &dont_translate, d, e);
716 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
719 sum0 = ip_incremental_checksum (0, icmp0,
720 ntohs(ip0->length) - ip4_header_bytes (ip0));
721 checksum0 = ~ip_csum_fold (sum0);
722 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
724 next0 = SNAT_IN2OUT_NEXT_DROP;
728 old_addr0 = ip0->src_address.as_u32;
729 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
730 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == ~0)
731 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
733 sum0 = ip0->checksum;
734 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
735 src_address /* changed member */);
736 ip0->checksum = ip_csum_fold (sum0);
738 if (!icmp_is_error_message (icmp0))
741 if (PREDICT_FALSE(new_id0 != echo0->identifier))
743 old_id0 = echo0->identifier;
745 echo0->identifier = new_id0;
747 sum0 = icmp0->checksum;
748 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
750 icmp0->checksum = ip_csum_fold (sum0);
755 inner_ip0 = (ip4_header_t *)(echo0+1);
756 l4_header = ip4_next_header (inner_ip0);
758 if (!ip4_header_checksum_is_valid (inner_ip0))
760 next0 = SNAT_IN2OUT_NEXT_DROP;
764 old_addr0 = inner_ip0->dst_address.as_u32;
765 inner_ip0->dst_address = sm0.addr;
766 new_addr0 = inner_ip0->dst_address.as_u32;
768 sum0 = icmp0->checksum;
769 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
770 dst_address /* changed member */);
771 icmp0->checksum = ip_csum_fold (sum0);
775 case SNAT_PROTOCOL_ICMP:
776 inner_icmp0 = (icmp46_header_t*)l4_header;
777 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
779 old_id0 = inner_echo0->identifier;
781 inner_echo0->identifier = new_id0;
783 sum0 = icmp0->checksum;
784 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
786 icmp0->checksum = ip_csum_fold (sum0);
788 case SNAT_PROTOCOL_UDP:
789 case SNAT_PROTOCOL_TCP:
790 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
792 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
794 sum0 = icmp0->checksum;
795 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
797 icmp0->checksum = ip_csum_fold (sum0);
811 * Hairpinning allows two endpoints on the internal side of the NAT to
812 * communicate even if they only use each other's external IP addresses
815 * @param sm NAT main.
816 * @param b0 Vlib buffer.
817 * @param ip0 IP header.
818 * @param udp0 UDP header.
819 * @param tcp0 TCP header.
820 * @param proto0 NAT protocol.
823 snat_hairpinning (snat_main_t *sm,
830 snat_session_key_t key0, sm0;
832 clib_bihash_kv_8_8_t kv0, value0;
834 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
835 u16 new_dst_port0, old_dst_port0;
837 key0.addr = ip0->dst_address;
838 key0.port = udp0->dst_port;
839 key0.protocol = proto0;
840 key0.fib_index = sm->outside_fib_index;
841 kv0.key = key0.as_u64;
843 /* Check if destination is static mappings */
844 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0))
846 new_dst_addr0 = sm0.addr.as_u32;
847 new_dst_port0 = sm0.port;
848 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
850 /* or active session */
853 if (sm->num_workers > 1)
854 ti = (clib_net_to_host_u16 (udp0->dst_port) - 1024) / sm->port_per_thread;
856 ti = sm->num_workers;
858 if (!clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0, &value0))
862 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
863 new_dst_addr0 = s0->in2out.addr.as_u32;
864 new_dst_port0 = s0->in2out.port;
865 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
869 /* Destination is behind the same NAT, use internal address and port */
872 old_dst_addr0 = ip0->dst_address.as_u32;
873 ip0->dst_address.as_u32 = new_dst_addr0;
874 sum0 = ip0->checksum;
875 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
876 ip4_header_t, dst_address);
877 ip0->checksum = ip_csum_fold (sum0);
879 old_dst_port0 = tcp0->dst;
880 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
882 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
884 tcp0->dst = new_dst_port0;
885 sum0 = tcp0->checksum;
886 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
887 ip4_header_t, dst_address);
888 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
889 ip4_header_t /* cheat */, length);
890 tcp0->checksum = ip_csum_fold(sum0);
894 udp0->dst_port = new_dst_port0;
900 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
902 sum0 = tcp0->checksum;
903 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
904 ip4_header_t, dst_address);
905 tcp0->checksum = ip_csum_fold(sum0);
914 snat_icmp_hairpinning (snat_main_t *sm,
917 icmp46_header_t * icmp0)
919 snat_session_key_t key0, sm0;
920 clib_bihash_kv_8_8_t kv0, value0;
921 u32 new_dst_addr0 = 0, old_dst_addr0, si, ti = 0;
925 if (!icmp_is_error_message (icmp0))
927 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
928 u16 icmp_id0 = echo0->identifier;
929 key0.addr = ip0->dst_address;
930 key0.port = icmp_id0;
931 key0.protocol = SNAT_PROTOCOL_ICMP;
932 key0.fib_index = sm->outside_fib_index;
933 kv0.key = key0.as_u64;
935 if (sm->num_workers > 1)
936 ti = (clib_net_to_host_u16 (icmp_id0) - 1024) / sm->port_per_thread;
938 ti = sm->num_workers;
940 /* Check if destination is in active sessions */
941 if (clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
944 /* or static mappings */
945 if (!snat_static_mapping_match(sm, key0, &sm0, 1, 0))
947 new_dst_addr0 = sm0.addr.as_u32;
948 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
955 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
956 new_dst_addr0 = s0->in2out.addr.as_u32;
957 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
958 echo0->identifier = s0->in2out.port;
959 sum0 = icmp0->checksum;
960 sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
961 icmp_echo_header_t, identifier);
962 icmp0->checksum = ip_csum_fold (sum0);
965 /* Destination is behind the same NAT, use internal address and port */
968 old_dst_addr0 = ip0->dst_address.as_u32;
969 ip0->dst_address.as_u32 = new_dst_addr0;
970 sum0 = ip0->checksum;
971 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
972 ip4_header_t, dst_address);
973 ip0->checksum = ip_csum_fold (sum0);
979 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
982 icmp46_header_t * icmp0,
985 vlib_node_runtime_t * node,
989 snat_session_t ** p_s0)
991 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
992 next0, thread_index, p_s0, 0);
993 snat_session_t * s0 = *p_s0;
994 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
997 if (vnet_buffer(b0)->sw_if_index[VLIB_TX] == 0)
998 snat_icmp_hairpinning(sm, b0, ip0, icmp0);
1000 s0->last_heard = now;
1002 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
1003 /* Per-user LRU list maintenance for dynamic translations */
1004 if (!snat_is_session_static (s0))
1006 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1007 s0->per_user_index);
1008 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1009 s0->per_user_list_head_index,
1010 s0->per_user_index);
1016 snat_hairpinning_unknown_proto (snat_main_t *sm,
1020 u32 old_addr, new_addr = 0, ti = 0;
1021 clib_bihash_kv_8_8_t kv, value;
1022 clib_bihash_kv_16_8_t s_kv, s_value;
1023 nat_ed_ses_key_t key;
1024 snat_session_key_t m_key;
1025 snat_static_mapping_t *m;
1029 old_addr = ip->dst_address.as_u32;
1030 key.l_addr.as_u32 = ip->dst_address.as_u32;
1031 key.r_addr.as_u32 = ip->src_address.as_u32;
1032 key.fib_index = sm->outside_fib_index;
1033 key.proto = ip->protocol;
1036 s_kv.key[0] = key.as_u64[0];
1037 s_kv.key[1] = key.as_u64[1];
1038 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1040 m_key.addr = ip->dst_address;
1041 m_key.fib_index = sm->outside_fib_index;
1044 kv.key = m_key.as_u64;
1045 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1048 m = pool_elt_at_index (sm->static_mappings, value.value);
1049 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1050 vnet_buffer(b)->sw_if_index[VLIB_TX] = m->fib_index;
1051 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
1055 if (sm->num_workers > 1)
1056 ti = sm->worker_out2in_cb (ip, sm->outside_fib_index);
1058 ti = sm->num_workers;
1060 s = pool_elt_at_index (sm->per_thread_data[ti].sessions, s_value.value);
1061 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1062 vnet_buffer(b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
1063 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
1066 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
1067 ip->checksum = ip_csum_fold (sum);
1070 static snat_session_t *
1071 snat_in2out_unknown_proto (snat_main_t *sm,
1078 vlib_node_runtime_t * node)
1080 clib_bihash_kv_8_8_t kv, value;
1081 clib_bihash_kv_16_8_t s_kv, s_value;
1082 snat_static_mapping_t *m;
1083 snat_session_key_t m_key;
1084 u32 old_addr, new_addr = 0;
1086 snat_user_key_t u_key;
1088 dlist_elt_t *head, *elt, *oldest;
1089 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1090 u32 elt_index, head_index, ses_index, oldest_index;
1092 nat_ed_ses_key_t key;
1093 u32 address_index = ~0;
1097 old_addr = ip->src_address.as_u32;
1099 key.l_addr = ip->src_address;
1100 key.r_addr = ip->dst_address;
1101 key.fib_index = rx_fib_index;
1102 key.proto = ip->protocol;
1105 s_kv.key[0] = key.as_u64[0];
1106 s_kv.key[1] = key.as_u64[1];
1108 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
1110 s = pool_elt_at_index (tsm->sessions, s_value.value);
1111 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1115 if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
1117 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1121 u_key.addr = ip->src_address;
1122 u_key.fib_index = rx_fib_index;
1123 kv.key = u_key.as_u64;
1125 /* Ever heard of the "user" = src ip4 address before? */
1126 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1128 /* no, make a new one */
1129 pool_get (tsm->users, u);
1130 memset (u, 0, sizeof (*u));
1131 u->addr = ip->src_address;
1132 u->fib_index = rx_fib_index;
1134 pool_get (tsm->list_pool, head);
1135 u->sessions_per_user_list_head_index = head - tsm->list_pool;
1137 clib_dlist_init (tsm->list_pool,
1138 u->sessions_per_user_list_head_index);
1140 kv.value = u - tsm->users;
1143 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1);
1147 u = pool_elt_at_index (tsm->users, value.value);
1150 m_key.addr = ip->src_address;
1153 m_key.fib_index = rx_fib_index;
1154 kv.key = m_key.as_u64;
1156 /* Try to find static mapping first */
1157 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
1159 m = pool_elt_at_index (sm->static_mappings, value.value);
1160 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
1164 /* Fallback to 3-tuple key */
1167 /* Choose same out address as for TCP/UDP session to same destination */
1168 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1170 head_index = u->sessions_per_user_list_head_index;
1171 head = pool_elt_at_index (tsm->list_pool, head_index);
1172 elt_index = head->next;
1173 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1174 ses_index = elt->value;
1175 while (ses_index != ~0)
1177 s = pool_elt_at_index (tsm->sessions, ses_index);
1178 elt_index = elt->next;
1179 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1180 ses_index = elt->value;
1182 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
1184 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1185 address_index = s->outside_address_index;
1187 key.fib_index = sm->outside_fib_index;
1188 key.l_addr.as_u32 = new_addr;
1189 s_kv.key[0] = key.as_u64[0];
1190 s_kv.key[1] = key.as_u64[1];
1191 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1198 key.fib_index = sm->outside_fib_index;
1199 for (i = 0; i < vec_len (sm->addresses); i++)
1201 key.l_addr.as_u32 = sm->addresses[i].addr.as_u32;
1202 s_kv.key[0] = key.as_u64[0];
1203 s_kv.key[1] = key.as_u64[1];
1204 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
1206 new_addr = ip->src_address.as_u32 = key.l_addr.as_u32;
1215 /* Over quota? Recycle the least recently used dynamic translation */
1216 if (u->nsessions >= sm->max_translations_per_user && !is_sm)
1218 /* Remove the oldest dynamic translation */
1220 oldest_index = clib_dlist_remove_head (
1221 tsm->list_pool, u->sessions_per_user_list_head_index);
1223 ASSERT (oldest_index != ~0);
1225 /* add it back to the end of the LRU list */
1226 clib_dlist_addtail (tsm->list_pool,
1227 u->sessions_per_user_list_head_index,
1229 /* Get the list element */
1230 oldest = pool_elt_at_index (tsm->list_pool, oldest_index);
1232 /* Get the session index from the list element */
1233 ses_index = oldest->value;
1235 /* Get the session */
1236 s = pool_elt_at_index (tsm->sessions, ses_index);
1237 } while (snat_is_session_static (s));
1239 if (snat_is_unk_proto_session (s))
1241 /* Remove from lookup tables */
1242 key.l_addr = s->in2out.addr;
1243 key.r_addr = s->ext_host_addr;
1244 key.fib_index = s->in2out.fib_index;
1245 key.proto = s->in2out.port;
1246 s_kv.key[0] = key.as_u64[0];
1247 s_kv.key[1] = key.as_u64[1];
1248 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 0))
1249 clib_warning ("in2out key del failed");
1251 key.l_addr = s->out2in.addr;
1252 key.fib_index = s->out2in.fib_index;
1253 s_kv.key[0] = key.as_u64[0];
1254 s_kv.key[1] = key.as_u64[1];
1255 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 0))
1256 clib_warning ("out2in key del failed");
1261 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
1262 s->out2in.addr.as_u32,
1266 s->in2out.fib_index);
1268 snat_free_outside_address_and_port (sm, thread_index, &s->out2in,
1269 s->outside_address_index);
1271 /* Remove in2out, out2in keys */
1272 kv.key = s->in2out.as_u64;
1273 if (clib_bihash_add_del_8_8 (
1274 &sm->per_thread_data[thread_index].in2out, &kv, 0))
1275 clib_warning ("in2out key del failed");
1276 kv.key = s->out2in.as_u64;
1277 if (clib_bihash_add_del_8_8 (
1278 &sm->per_thread_data[thread_index].out2in, &kv, 0))
1279 clib_warning ("out2in key del failed");
1284 /* Create a new session */
1285 pool_get (tsm->sessions, s);
1286 memset (s, 0, sizeof (*s));
1288 /* Create list elts */
1289 pool_get (tsm->list_pool, elt);
1290 clib_dlist_init (tsm->list_pool, elt - tsm->list_pool);
1291 elt->value = s - tsm->sessions;
1292 s->per_user_index = elt - tsm->list_pool;
1293 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1294 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1298 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1299 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
1300 s->outside_address_index = address_index;
1301 s->out2in.addr.as_u32 = new_addr;
1302 s->out2in.fib_index = sm->outside_fib_index;
1303 s->in2out.addr.as_u32 = old_addr;
1304 s->in2out.fib_index = rx_fib_index;
1305 s->in2out.port = s->out2in.port = ip->protocol;
1308 u->nstaticsessions++;
1309 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1316 /* Add to lookup tables */
1317 key.l_addr.as_u32 = old_addr;
1318 key.r_addr = ip->dst_address;
1319 key.proto = ip->protocol;
1320 key.fib_index = rx_fib_index;
1321 s_kv.key[0] = key.as_u64[0];
1322 s_kv.key[1] = key.as_u64[1];
1323 s_kv.value = s - tsm->sessions;
1324 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1325 clib_warning ("in2out key add failed");
1327 key.l_addr.as_u32 = new_addr;
1328 key.fib_index = sm->outside_fib_index;
1329 s_kv.key[0] = key.as_u64[0];
1330 s_kv.key[1] = key.as_u64[1];
1331 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1332 clib_warning ("out2in key add failed");
1335 /* Update IP checksum */
1337 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1338 ip->checksum = ip_csum_fold (sum);
1341 s->last_heard = now;
1343 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1344 /* Per-user LRU list maintenance */
1345 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1346 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1350 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1351 snat_hairpinning_unknown_proto(sm, b, ip);
1353 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1354 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1359 static snat_session_t *
1360 snat_in2out_lb (snat_main_t *sm,
1367 vlib_node_runtime_t * node)
1369 nat_ed_ses_key_t key;
1370 clib_bihash_kv_16_8_t s_kv, s_value;
1371 udp_header_t *udp = ip4_next_header (ip);
1372 tcp_header_t *tcp = (tcp_header_t *) udp;
1373 snat_session_t *s = 0;
1374 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1375 u32 old_addr, new_addr;
1376 u16 new_port, old_port;
1378 u32 proto = ip_proto_to_snat_proto (ip->protocol);
1379 snat_session_key_t e_key, l_key;
1380 clib_bihash_kv_8_8_t kv, value;
1381 snat_user_key_t u_key;
1383 dlist_elt_t *head, *elt;
1385 old_addr = ip->src_address.as_u32;
1387 key.l_addr = ip->src_address;
1388 key.r_addr = ip->dst_address;
1389 key.fib_index = rx_fib_index;
1390 key.proto = ip->protocol;
1392 key.l_port = udp->src_port;
1393 s_kv.key[0] = key.as_u64[0];
1394 s_kv.key[1] = key.as_u64[1];
1396 if (!clib_bihash_search_16_8 (&sm->in2out_ed, &s_kv, &s_value))
1398 s = pool_elt_at_index (tsm->sessions, s_value.value);
1402 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
1404 b->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
1408 l_key.addr = ip->src_address;
1409 l_key.port = udp->src_port;
1410 l_key.protocol = proto;
1411 l_key.fib_index = rx_fib_index;
1412 if (snat_static_mapping_match(sm, l_key, &e_key, 0, 0))
1415 u_key.addr = ip->src_address;
1416 u_key.fib_index = rx_fib_index;
1417 kv.key = u_key.as_u64;
1419 /* Ever heard of the "user" = src ip4 address before? */
1420 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1422 /* no, make a new one */
1423 pool_get (tsm->users, u);
1424 memset (u, 0, sizeof (*u));
1425 u->addr = ip->src_address;
1426 u->fib_index = rx_fib_index;
1428 pool_get (tsm->list_pool, head);
1429 u->sessions_per_user_list_head_index = head - tsm->list_pool;
1431 clib_dlist_init (tsm->list_pool,
1432 u->sessions_per_user_list_head_index);
1434 kv.value = u - tsm->users;
1437 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
1438 clib_warning ("user key add failed");
1442 u = pool_elt_at_index (tsm->users, value.value);
1445 /* Create a new session */
1446 pool_get (tsm->sessions, s);
1447 memset (s, 0, sizeof (*s));
1449 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
1450 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
1451 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
1452 s->outside_address_index = ~0;
1455 u->nstaticsessions++;
1457 /* Create list elts */
1458 pool_get (tsm->list_pool, elt);
1459 clib_dlist_init (tsm->list_pool, elt - tsm->list_pool);
1460 elt->value = s - tsm->sessions;
1461 s->per_user_index = elt - tsm->list_pool;
1462 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1463 clib_dlist_addtail (tsm->list_pool, s->per_user_list_head_index,
1466 /* Add to lookup tables */
1467 s_kv.value = s - tsm->sessions;
1468 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &s_kv, 1))
1469 clib_warning ("in2out-ed key add failed");
1471 key.l_addr = e_key.addr;
1472 key.fib_index = e_key.fib_index;
1473 key.l_port = e_key.port;
1474 s_kv.key[0] = key.as_u64[0];
1475 s_kv.key[1] = key.as_u64[1];
1476 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
1477 clib_warning ("out2in-ed key add failed");
1480 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
1482 /* Update IP checksum */
1484 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1485 ip->checksum = ip_csum_fold (sum);
1487 if (PREDICT_TRUE(proto == SNAT_PROTOCOL_TCP))
1489 old_port = tcp->src_port;
1490 tcp->src_port = s->out2in.port;
1491 new_port = tcp->src_port;
1493 sum = tcp->checksum;
1494 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
1495 sum = ip_csum_update (sum, old_port, new_port, ip4_header_t, length);
1496 tcp->checksum = ip_csum_fold(sum);
1500 udp->src_port = s->out2in.port;
1504 if (vnet_buffer(b)->sw_if_index[VLIB_TX] == ~0)
1505 vnet_buffer(b)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1508 s->last_heard = now;
1510 s->total_bytes += vlib_buffer_length_in_chain (vm, b);
1515 snat_in2out_node_fn_inline (vlib_main_t * vm,
1516 vlib_node_runtime_t * node,
1517 vlib_frame_t * frame, int is_slow_path,
1518 int is_output_feature)
1520 u32 n_left_from, * from, * to_next;
1521 snat_in2out_next_t next_index;
1522 u32 pkts_processed = 0;
1523 snat_main_t * sm = &snat_main;
1524 f64 now = vlib_time_now (vm);
1525 u32 stats_node_index;
1526 u32 thread_index = vlib_get_thread_index ();
1528 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
1529 snat_in2out_node.index;
1531 from = vlib_frame_vector_args (frame);
1532 n_left_from = frame->n_vectors;
1533 next_index = node->cached_next_index;
1535 while (n_left_from > 0)
1539 vlib_get_next_frame (vm, node, next_index,
1540 to_next, n_left_to_next);
1542 while (n_left_from >= 4 && n_left_to_next >= 2)
1545 vlib_buffer_t * b0, * b1;
1547 u32 sw_if_index0, sw_if_index1;
1548 ip4_header_t * ip0, * ip1;
1549 ip_csum_t sum0, sum1;
1550 u32 new_addr0, old_addr0, new_addr1, old_addr1;
1551 u16 old_port0, new_port0, old_port1, new_port1;
1552 udp_header_t * udp0, * udp1;
1553 tcp_header_t * tcp0, * tcp1;
1554 icmp46_header_t * icmp0, * icmp1;
1555 snat_session_key_t key0, key1;
1556 u32 rx_fib_index0, rx_fib_index1;
1558 snat_session_t * s0 = 0, * s1 = 0;
1559 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
1560 u32 iph_offset0 = 0, iph_offset1 = 0;
1562 /* Prefetch next iteration. */
1564 vlib_buffer_t * p2, * p3;
1566 p2 = vlib_get_buffer (vm, from[2]);
1567 p3 = vlib_get_buffer (vm, from[3]);
1569 vlib_prefetch_buffer_header (p2, LOAD);
1570 vlib_prefetch_buffer_header (p3, LOAD);
1572 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1573 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1576 /* speculatively enqueue b0 and b1 to the current next frame */
1577 to_next[0] = bi0 = from[0];
1578 to_next[1] = bi1 = from[1];
1582 n_left_to_next -= 2;
1584 b0 = vlib_get_buffer (vm, bi0);
1585 b1 = vlib_get_buffer (vm, bi1);
1587 if (is_output_feature)
1588 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1590 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1593 udp0 = ip4_next_header (ip0);
1594 tcp0 = (tcp_header_t *) udp0;
1595 icmp0 = (icmp46_header_t *) udp0;
1597 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1598 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1601 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1603 if (PREDICT_FALSE(ip0->ttl == 1))
1605 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1606 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1607 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1609 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1613 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1615 /* Next configured feature, probably ip4-lookup */
1618 if (PREDICT_FALSE (proto0 == ~0))
1620 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
1621 thread_index, now, vm, node);
1623 next0 = SNAT_IN2OUT_NEXT_DROP;
1627 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1629 next0 = icmp_in2out_slow_path
1630 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1631 node, next0, now, thread_index, &s0);
1637 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1639 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1644 key0.addr = ip0->src_address;
1645 key0.port = udp0->src_port;
1646 key0.protocol = proto0;
1647 key0.fib_index = rx_fib_index0;
1649 kv0.key = key0.as_u64;
1651 if (PREDICT_FALSE (clib_bihash_search_8_8 (
1652 &sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1656 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
1657 ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
1660 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1661 &s0, node, next0, thread_index);
1662 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1667 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1673 if (PREDICT_FALSE (value0.value == ~0ULL))
1677 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
1678 thread_index, now, vm, node);
1680 next0 = SNAT_IN2OUT_NEXT_DROP;
1685 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1691 s0 = pool_elt_at_index (
1692 sm->per_thread_data[thread_index].sessions,
1697 b0->flags |= VNET_BUFFER_F_IS_NATED;
1699 old_addr0 = ip0->src_address.as_u32;
1700 ip0->src_address = s0->out2in.addr;
1701 new_addr0 = ip0->src_address.as_u32;
1702 if (!is_output_feature)
1703 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1705 sum0 = ip0->checksum;
1706 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1708 src_address /* changed member */);
1709 ip0->checksum = ip_csum_fold (sum0);
1711 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1713 old_port0 = tcp0->src_port;
1714 tcp0->src_port = s0->out2in.port;
1715 new_port0 = tcp0->src_port;
1717 sum0 = tcp0->checksum;
1718 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1720 dst_address /* changed member */);
1721 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1722 ip4_header_t /* cheat */,
1723 length /* changed member */);
1724 tcp0->checksum = ip_csum_fold(sum0);
1728 old_port0 = udp0->src_port;
1729 udp0->src_port = s0->out2in.port;
1734 s0->last_heard = now;
1736 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1737 /* Per-user LRU list maintenance for dynamic translation */
1738 if (!snat_is_session_static (s0))
1740 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1741 s0->per_user_index);
1742 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1743 s0->per_user_list_head_index,
1744 s0->per_user_index);
1748 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1749 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1751 snat_in2out_trace_t *t =
1752 vlib_add_trace (vm, node, b0, sizeof (*t));
1753 t->is_slow_path = is_slow_path;
1754 t->sw_if_index = sw_if_index0;
1755 t->next_index = next0;
1756 t->session_index = ~0;
1758 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1761 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1763 if (is_output_feature)
1764 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1766 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1769 udp1 = ip4_next_header (ip1);
1770 tcp1 = (tcp_header_t *) udp1;
1771 icmp1 = (icmp46_header_t *) udp1;
1773 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1774 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1777 if (PREDICT_FALSE(ip1->ttl == 1))
1779 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1780 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1781 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1783 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1787 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1789 /* Next configured feature, probably ip4-lookup */
1792 if (PREDICT_FALSE (proto1 == ~0))
1794 s1 = snat_in2out_unknown_proto (sm, b1, ip1, rx_fib_index1,
1795 thread_index, now, vm, node);
1797 next1 = SNAT_IN2OUT_NEXT_DROP;
1801 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1803 next1 = icmp_in2out_slow_path
1804 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1805 next1, now, thread_index, &s1);
1811 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1813 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1818 b1->flags |= VNET_BUFFER_F_IS_NATED;
1820 key1.addr = ip1->src_address;
1821 key1.port = udp1->src_port;
1822 key1.protocol = proto1;
1823 key1.fib_index = rx_fib_index1;
1825 kv1.key = key1.as_u64;
1827 if (PREDICT_FALSE(clib_bihash_search_8_8 (
1828 &sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1832 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index1,
1833 ip1, proto1, rx_fib_index1, thread_index)) && !is_output_feature)
1836 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1837 &s1, node, next1, thread_index);
1838 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1843 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1849 if (PREDICT_FALSE (value1.value == ~0ULL))
1853 s1 = snat_in2out_lb(sm, b1, ip1, rx_fib_index1,
1854 thread_index, now, vm, node);
1856 next1 = SNAT_IN2OUT_NEXT_DROP;
1861 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1867 s1 = pool_elt_at_index (
1868 sm->per_thread_data[thread_index].sessions,
1873 old_addr1 = ip1->src_address.as_u32;
1874 ip1->src_address = s1->out2in.addr;
1875 new_addr1 = ip1->src_address.as_u32;
1876 if (!is_output_feature)
1877 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1879 sum1 = ip1->checksum;
1880 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1882 src_address /* changed member */);
1883 ip1->checksum = ip_csum_fold (sum1);
1885 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1887 old_port1 = tcp1->src_port;
1888 tcp1->src_port = s1->out2in.port;
1889 new_port1 = tcp1->src_port;
1891 sum1 = tcp1->checksum;
1892 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1894 dst_address /* changed member */);
1895 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1896 ip4_header_t /* cheat */,
1897 length /* changed member */);
1898 tcp1->checksum = ip_csum_fold(sum1);
1902 old_port1 = udp1->src_port;
1903 udp1->src_port = s1->out2in.port;
1908 s1->last_heard = now;
1910 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1911 /* Per-user LRU list maintenance for dynamic translation */
1912 if (!snat_is_session_static (s1))
1914 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1915 s1->per_user_index);
1916 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1917 s1->per_user_list_head_index,
1918 s1->per_user_index);
1922 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1923 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1925 snat_in2out_trace_t *t =
1926 vlib_add_trace (vm, node, b1, sizeof (*t));
1927 t->sw_if_index = sw_if_index1;
1928 t->next_index = next1;
1929 t->session_index = ~0;
1931 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1934 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1936 /* verify speculative enqueues, maybe switch current next frame */
1937 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1938 to_next, n_left_to_next,
1939 bi0, bi1, next0, next1);
1942 while (n_left_from > 0 && n_left_to_next > 0)
1950 u32 new_addr0, old_addr0;
1951 u16 old_port0, new_port0;
1952 udp_header_t * udp0;
1953 tcp_header_t * tcp0;
1954 icmp46_header_t * icmp0;
1955 snat_session_key_t key0;
1958 snat_session_t * s0 = 0;
1959 clib_bihash_kv_8_8_t kv0, value0;
1960 u32 iph_offset0 = 0;
1962 /* speculatively enqueue b0 to the current next frame */
1968 n_left_to_next -= 1;
1970 b0 = vlib_get_buffer (vm, bi0);
1971 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1973 if (is_output_feature)
1974 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1976 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1979 udp0 = ip4_next_header (ip0);
1980 tcp0 = (tcp_header_t *) udp0;
1981 icmp0 = (icmp46_header_t *) udp0;
1983 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1984 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1987 if (PREDICT_FALSE(ip0->ttl == 1))
1989 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1990 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1991 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1993 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1997 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1999 /* Next configured feature, probably ip4-lookup */
2002 if (PREDICT_FALSE (proto0 == ~0))
2004 s0 = snat_in2out_unknown_proto (sm, b0, ip0, rx_fib_index0,
2005 thread_index, now, vm, node);
2007 next0 = SNAT_IN2OUT_NEXT_DROP;
2011 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2013 next0 = icmp_in2out_slow_path
2014 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
2015 next0, now, thread_index, &s0);
2021 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
2023 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2028 key0.addr = ip0->src_address;
2029 key0.port = udp0->src_port;
2030 key0.protocol = proto0;
2031 key0.fib_index = rx_fib_index0;
2033 kv0.key = key0.as_u64;
2035 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out,
2040 if (PREDICT_FALSE(snat_not_translate(sm, node, sw_if_index0,
2041 ip0, proto0, rx_fib_index0, thread_index)) && !is_output_feature)
2044 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
2045 &s0, node, next0, thread_index);
2047 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
2052 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2058 if (PREDICT_FALSE (value0.value == ~0ULL))
2062 s0 = snat_in2out_lb(sm, b0, ip0, rx_fib_index0,
2063 thread_index, now, vm, node);
2065 next0 = SNAT_IN2OUT_NEXT_DROP;
2070 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
2076 s0 = pool_elt_at_index (
2077 sm->per_thread_data[thread_index].sessions,
2082 b0->flags |= VNET_BUFFER_F_IS_NATED;
2084 old_addr0 = ip0->src_address.as_u32;
2085 ip0->src_address = s0->out2in.addr;
2086 new_addr0 = ip0->src_address.as_u32;
2087 if (!is_output_feature)
2088 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
2090 sum0 = ip0->checksum;
2091 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2093 src_address /* changed member */);
2094 ip0->checksum = ip_csum_fold (sum0);
2096 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2098 old_port0 = tcp0->src_port;
2099 tcp0->src_port = s0->out2in.port;
2100 new_port0 = tcp0->src_port;
2102 sum0 = tcp0->checksum;
2103 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2105 dst_address /* changed member */);
2106 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2107 ip4_header_t /* cheat */,
2108 length /* changed member */);
2109 tcp0->checksum = ip_csum_fold(sum0);
2113 old_port0 = udp0->src_port;
2114 udp0->src_port = s0->out2in.port;
2119 s0->last_heard = now;
2121 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
2122 /* Per-user LRU list maintenance for dynamic translation */
2123 if (!snat_is_session_static (s0))
2125 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
2126 s0->per_user_index);
2127 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
2128 s0->per_user_list_head_index,
2129 s0->per_user_index);
2133 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2134 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2136 snat_in2out_trace_t *t =
2137 vlib_add_trace (vm, node, b0, sizeof (*t));
2138 t->is_slow_path = is_slow_path;
2139 t->sw_if_index = sw_if_index0;
2140 t->next_index = next0;
2141 t->session_index = ~0;
2143 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
2146 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2148 /* verify speculative enqueue, maybe switch current next frame */
2149 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2150 to_next, n_left_to_next,
2154 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2157 vlib_node_increment_counter (vm, stats_node_index,
2158 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2160 return frame->n_vectors;
2164 snat_in2out_fast_path_fn (vlib_main_t * vm,
2165 vlib_node_runtime_t * node,
2166 vlib_frame_t * frame)
2168 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 0);
2171 VLIB_REGISTER_NODE (snat_in2out_node) = {
2172 .function = snat_in2out_fast_path_fn,
2173 .name = "nat44-in2out",
2174 .vector_size = sizeof (u32),
2175 .format_trace = format_snat_in2out_trace,
2176 .type = VLIB_NODE_TYPE_INTERNAL,
2178 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2179 .error_strings = snat_in2out_error_strings,
2181 .runtime_data_bytes = sizeof (snat_runtime_t),
2183 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2185 /* edit / add dispositions here */
2187 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2188 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2189 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2190 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2194 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
2197 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
2198 vlib_node_runtime_t * node,
2199 vlib_frame_t * frame)
2201 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */, 1);
2204 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
2205 .function = snat_in2out_output_fast_path_fn,
2206 .name = "nat44-in2out-output",
2207 .vector_size = sizeof (u32),
2208 .format_trace = format_snat_in2out_trace,
2209 .type = VLIB_NODE_TYPE_INTERNAL,
2211 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2212 .error_strings = snat_in2out_error_strings,
2214 .runtime_data_bytes = sizeof (snat_runtime_t),
2216 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2218 /* edit / add dispositions here */
2220 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2221 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2222 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2223 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2227 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
2228 snat_in2out_output_fast_path_fn);
2231 snat_in2out_slow_path_fn (vlib_main_t * vm,
2232 vlib_node_runtime_t * node,
2233 vlib_frame_t * frame)
2235 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 0);
2238 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
2239 .function = snat_in2out_slow_path_fn,
2240 .name = "nat44-in2out-slowpath",
2241 .vector_size = sizeof (u32),
2242 .format_trace = format_snat_in2out_trace,
2243 .type = VLIB_NODE_TYPE_INTERNAL,
2245 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2246 .error_strings = snat_in2out_error_strings,
2248 .runtime_data_bytes = sizeof (snat_runtime_t),
2250 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2252 /* edit / add dispositions here */
2254 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2255 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2256 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2257 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2261 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
2262 snat_in2out_slow_path_fn);
2265 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
2266 vlib_node_runtime_t * node,
2267 vlib_frame_t * frame)
2269 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */, 1);
2272 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
2273 .function = snat_in2out_output_slow_path_fn,
2274 .name = "nat44-in2out-output-slowpath",
2275 .vector_size = sizeof (u32),
2276 .format_trace = format_snat_in2out_trace,
2277 .type = VLIB_NODE_TYPE_INTERNAL,
2279 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2280 .error_strings = snat_in2out_error_strings,
2282 .runtime_data_bytes = sizeof (snat_runtime_t),
2284 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2286 /* edit / add dispositions here */
2288 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2289 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
2290 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
2291 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2295 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
2296 snat_in2out_output_slow_path_fn);
2298 extern vnet_feature_arc_registration_t vnet_feat_arc_ip4_local;
2301 nat44_hairpinning_fn (vlib_main_t * vm,
2302 vlib_node_runtime_t * node,
2303 vlib_frame_t * frame)
2305 u32 n_left_from, * from, * to_next;
2306 snat_in2out_next_t next_index;
2307 u32 pkts_processed = 0;
2308 snat_main_t * sm = &snat_main;
2309 vnet_feature_main_t *fm = &feature_main;
2310 u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
2311 vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc_index];
2313 from = vlib_frame_vector_args (frame);
2314 n_left_from = frame->n_vectors;
2315 next_index = node->cached_next_index;
2317 while (n_left_from > 0)
2321 vlib_get_next_frame (vm, node, next_index,
2322 to_next, n_left_to_next);
2324 while (n_left_from > 0 && n_left_to_next > 0)
2331 udp_header_t * udp0;
2332 tcp_header_t * tcp0;
2334 /* speculatively enqueue b0 to the current next frame */
2340 n_left_to_next -= 1;
2342 b0 = vlib_get_buffer (vm, bi0);
2343 ip0 = vlib_buffer_get_current (b0);
2344 udp0 = ip4_next_header (ip0);
2345 tcp0 = (tcp_header_t *) udp0;
2347 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2349 vnet_get_config_data (&cm->config_main, &b0->current_config_index,
2352 if (snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0))
2353 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2355 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2357 /* verify speculative enqueue, maybe switch current next frame */
2358 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2359 to_next, n_left_to_next,
2363 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2366 vlib_node_increment_counter (vm, nat44_hairpinning_node.index,
2367 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2369 return frame->n_vectors;
2372 VLIB_REGISTER_NODE (nat44_hairpinning_node) = {
2373 .function = nat44_hairpinning_fn,
2374 .name = "nat44-hairpinning",
2375 .vector_size = sizeof (u32),
2376 .type = VLIB_NODE_TYPE_INTERNAL,
2377 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2378 .error_strings = snat_in2out_error_strings,
2381 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2382 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2386 VLIB_NODE_FUNCTION_MULTIARCH (nat44_hairpinning_node,
2387 nat44_hairpinning_fn);
2389 /**************************/
2390 /*** deterministic mode ***/
2391 /**************************/
2393 snat_det_in2out_node_fn (vlib_main_t * vm,
2394 vlib_node_runtime_t * node,
2395 vlib_frame_t * frame)
2397 u32 n_left_from, * from, * to_next;
2398 snat_in2out_next_t next_index;
2399 u32 pkts_processed = 0;
2400 snat_main_t * sm = &snat_main;
2401 u32 now = (u32) vlib_time_now (vm);
2402 u32 thread_index = vlib_get_thread_index ();
2404 from = vlib_frame_vector_args (frame);
2405 n_left_from = frame->n_vectors;
2406 next_index = node->cached_next_index;
2408 while (n_left_from > 0)
2412 vlib_get_next_frame (vm, node, next_index,
2413 to_next, n_left_to_next);
2415 while (n_left_from >= 4 && n_left_to_next >= 2)
2418 vlib_buffer_t * b0, * b1;
2420 u32 sw_if_index0, sw_if_index1;
2421 ip4_header_t * ip0, * ip1;
2422 ip_csum_t sum0, sum1;
2423 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
2424 u16 old_port0, new_port0, lo_port0, i0;
2425 u16 old_port1, new_port1, lo_port1, i1;
2426 udp_header_t * udp0, * udp1;
2427 tcp_header_t * tcp0, * tcp1;
2429 snat_det_out_key_t key0, key1;
2430 snat_det_map_t * dm0, * dm1;
2431 snat_det_session_t * ses0 = 0, * ses1 = 0;
2432 u32 rx_fib_index0, rx_fib_index1;
2433 icmp46_header_t * icmp0, * icmp1;
2435 /* Prefetch next iteration. */
2437 vlib_buffer_t * p2, * p3;
2439 p2 = vlib_get_buffer (vm, from[2]);
2440 p3 = vlib_get_buffer (vm, from[3]);
2442 vlib_prefetch_buffer_header (p2, LOAD);
2443 vlib_prefetch_buffer_header (p3, LOAD);
2445 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
2446 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
2449 /* speculatively enqueue b0 and b1 to the current next frame */
2450 to_next[0] = bi0 = from[0];
2451 to_next[1] = bi1 = from[1];
2455 n_left_to_next -= 2;
2457 b0 = vlib_get_buffer (vm, bi0);
2458 b1 = vlib_get_buffer (vm, bi1);
2460 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2461 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
2463 ip0 = vlib_buffer_get_current (b0);
2464 udp0 = ip4_next_header (ip0);
2465 tcp0 = (tcp_header_t *) udp0;
2467 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2469 if (PREDICT_FALSE(ip0->ttl == 1))
2471 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2472 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2473 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2475 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2479 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2481 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2483 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2484 icmp0 = (icmp46_header_t *) udp0;
2486 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2487 rx_fib_index0, node, next0, thread_index,
2492 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2493 if (PREDICT_FALSE(!dm0))
2495 clib_warning("no match for internal host %U",
2496 format_ip4_address, &ip0->src_address);
2497 next0 = SNAT_IN2OUT_NEXT_DROP;
2498 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2502 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2504 key0.ext_host_addr = ip0->dst_address;
2505 key0.ext_host_port = tcp0->dst;
2507 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
2508 if (PREDICT_FALSE(!ses0))
2510 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2512 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2513 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
2515 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
2518 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
2521 if (PREDICT_FALSE(!ses0))
2523 /* too many sessions for user, send ICMP error packet */
2525 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2526 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
2527 ICMP4_destination_unreachable_destination_unreachable_host,
2529 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2534 new_port0 = ses0->out.out_port;
2536 old_addr0.as_u32 = ip0->src_address.as_u32;
2537 ip0->src_address.as_u32 = new_addr0.as_u32;
2538 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2540 sum0 = ip0->checksum;
2541 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2543 src_address /* changed member */);
2544 ip0->checksum = ip_csum_fold (sum0);
2546 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2548 if (tcp0->flags & TCP_FLAG_SYN)
2549 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
2550 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
2551 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2552 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2553 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
2554 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
2555 snat_det_ses_close(dm0, ses0);
2556 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2557 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
2558 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
2559 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2561 old_port0 = tcp0->src;
2562 tcp0->src = new_port0;
2564 sum0 = tcp0->checksum;
2565 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2567 dst_address /* changed member */);
2568 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2569 ip4_header_t /* cheat */,
2570 length /* changed member */);
2571 tcp0->checksum = ip_csum_fold(sum0);
2575 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2576 old_port0 = udp0->src_port;
2577 udp0->src_port = new_port0;
2583 case SNAT_SESSION_UDP_ACTIVE:
2584 ses0->expire = now + sm->udp_timeout;
2586 case SNAT_SESSION_TCP_SYN_SENT:
2587 case SNAT_SESSION_TCP_FIN_WAIT:
2588 case SNAT_SESSION_TCP_CLOSE_WAIT:
2589 case SNAT_SESSION_TCP_LAST_ACK:
2590 ses0->expire = now + sm->tcp_transitory_timeout;
2592 case SNAT_SESSION_TCP_ESTABLISHED:
2593 ses0->expire = now + sm->tcp_established_timeout;
2598 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2599 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2601 snat_in2out_trace_t *t =
2602 vlib_add_trace (vm, node, b0, sizeof (*t));
2603 t->is_slow_path = 0;
2604 t->sw_if_index = sw_if_index0;
2605 t->next_index = next0;
2606 t->session_index = ~0;
2608 t->session_index = ses0 - dm0->sessions;
2611 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2613 ip1 = vlib_buffer_get_current (b1);
2614 udp1 = ip4_next_header (ip1);
2615 tcp1 = (tcp_header_t *) udp1;
2617 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
2619 if (PREDICT_FALSE(ip1->ttl == 1))
2621 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2622 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
2623 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2625 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2629 proto1 = ip_proto_to_snat_proto (ip1->protocol);
2631 if (PREDICT_FALSE(proto1 == SNAT_PROTOCOL_ICMP))
2633 rx_fib_index1 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index1);
2634 icmp1 = (icmp46_header_t *) udp1;
2636 next1 = icmp_in2out(sm, b1, ip1, icmp1, sw_if_index1,
2637 rx_fib_index1, node, next1, thread_index,
2642 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
2643 if (PREDICT_FALSE(!dm1))
2645 clib_warning("no match for internal host %U",
2646 format_ip4_address, &ip0->src_address);
2647 next1 = SNAT_IN2OUT_NEXT_DROP;
2648 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2652 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
2654 key1.ext_host_addr = ip1->dst_address;
2655 key1.ext_host_port = tcp1->dst;
2657 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src, key1);
2658 if (PREDICT_FALSE(!ses1))
2660 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
2662 key1.out_port = clib_host_to_net_u16 (lo_port1 +
2663 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
2665 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
2668 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
2671 if (PREDICT_FALSE(!ses1))
2673 /* too many sessions for user, send ICMP error packet */
2675 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2676 icmp4_error_set_vnet_buffer (b1, ICMP4_destination_unreachable,
2677 ICMP4_destination_unreachable_destination_unreachable_host,
2679 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2684 new_port1 = ses1->out.out_port;
2686 old_addr1.as_u32 = ip1->src_address.as_u32;
2687 ip1->src_address.as_u32 = new_addr1.as_u32;
2688 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2690 sum1 = ip1->checksum;
2691 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2693 src_address /* changed member */);
2694 ip1->checksum = ip_csum_fold (sum1);
2696 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
2698 if (tcp1->flags & TCP_FLAG_SYN)
2699 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
2700 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
2701 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
2702 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
2703 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
2704 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
2705 snat_det_ses_close(dm1, ses1);
2706 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2707 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
2708 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
2709 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
2711 old_port1 = tcp1->src;
2712 tcp1->src = new_port1;
2714 sum1 = tcp1->checksum;
2715 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
2717 dst_address /* changed member */);
2718 sum1 = ip_csum_update (sum1, old_port1, new_port1,
2719 ip4_header_t /* cheat */,
2720 length /* changed member */);
2721 tcp1->checksum = ip_csum_fold(sum1);
2725 ses1->state = SNAT_SESSION_UDP_ACTIVE;
2726 old_port1 = udp1->src_port;
2727 udp1->src_port = new_port1;
2733 case SNAT_SESSION_UDP_ACTIVE:
2734 ses1->expire = now + sm->udp_timeout;
2736 case SNAT_SESSION_TCP_SYN_SENT:
2737 case SNAT_SESSION_TCP_FIN_WAIT:
2738 case SNAT_SESSION_TCP_CLOSE_WAIT:
2739 case SNAT_SESSION_TCP_LAST_ACK:
2740 ses1->expire = now + sm->tcp_transitory_timeout;
2742 case SNAT_SESSION_TCP_ESTABLISHED:
2743 ses1->expire = now + sm->tcp_established_timeout;
2748 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2749 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
2751 snat_in2out_trace_t *t =
2752 vlib_add_trace (vm, node, b1, sizeof (*t));
2753 t->is_slow_path = 0;
2754 t->sw_if_index = sw_if_index1;
2755 t->next_index = next1;
2756 t->session_index = ~0;
2758 t->session_index = ses1 - dm1->sessions;
2761 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
2763 /* verify speculative enqueues, maybe switch current next frame */
2764 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2765 to_next, n_left_to_next,
2766 bi0, bi1, next0, next1);
2769 while (n_left_from > 0 && n_left_to_next > 0)
2777 ip4_address_t new_addr0, old_addr0;
2778 u16 old_port0, new_port0, lo_port0, i0;
2779 udp_header_t * udp0;
2780 tcp_header_t * tcp0;
2782 snat_det_out_key_t key0;
2783 snat_det_map_t * dm0;
2784 snat_det_session_t * ses0 = 0;
2786 icmp46_header_t * icmp0;
2788 /* speculatively enqueue b0 to the current next frame */
2794 n_left_to_next -= 1;
2796 b0 = vlib_get_buffer (vm, bi0);
2797 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2799 ip0 = vlib_buffer_get_current (b0);
2800 udp0 = ip4_next_header (ip0);
2801 tcp0 = (tcp_header_t *) udp0;
2803 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2805 if (PREDICT_FALSE(ip0->ttl == 1))
2807 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2808 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2809 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2811 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2815 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2817 if (PREDICT_FALSE(proto0 == SNAT_PROTOCOL_ICMP))
2819 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2820 icmp0 = (icmp46_header_t *) udp0;
2822 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2823 rx_fib_index0, node, next0, thread_index,
2828 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
2829 if (PREDICT_FALSE(!dm0))
2831 clib_warning("no match for internal host %U",
2832 format_ip4_address, &ip0->src_address);
2833 next0 = SNAT_IN2OUT_NEXT_DROP;
2834 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2838 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
2840 key0.ext_host_addr = ip0->dst_address;
2841 key0.ext_host_port = tcp0->dst;
2843 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src, key0);
2844 if (PREDICT_FALSE(!ses0))
2846 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
2848 key0.out_port = clib_host_to_net_u16 (lo_port0 +
2849 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
2851 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
2854 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
2857 if (PREDICT_FALSE(!ses0))
2859 /* too many sessions for user, send ICMP error packet */
2861 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2862 icmp4_error_set_vnet_buffer (b0, ICMP4_destination_unreachable,
2863 ICMP4_destination_unreachable_destination_unreachable_host,
2865 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2870 new_port0 = ses0->out.out_port;
2872 old_addr0.as_u32 = ip0->src_address.as_u32;
2873 ip0->src_address.as_u32 = new_addr0.as_u32;
2874 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
2876 sum0 = ip0->checksum;
2877 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2879 src_address /* changed member */);
2880 ip0->checksum = ip_csum_fold (sum0);
2882 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2884 if (tcp0->flags & TCP_FLAG_SYN)
2885 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
2886 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
2887 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2888 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
2889 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
2890 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
2891 snat_det_ses_close(dm0, ses0);
2892 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
2893 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
2894 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
2895 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
2897 old_port0 = tcp0->src;
2898 tcp0->src = new_port0;
2900 sum0 = tcp0->checksum;
2901 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
2903 dst_address /* changed member */);
2904 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2905 ip4_header_t /* cheat */,
2906 length /* changed member */);
2907 tcp0->checksum = ip_csum_fold(sum0);
2911 ses0->state = SNAT_SESSION_UDP_ACTIVE;
2912 old_port0 = udp0->src_port;
2913 udp0->src_port = new_port0;
2919 case SNAT_SESSION_UDP_ACTIVE:
2920 ses0->expire = now + sm->udp_timeout;
2922 case SNAT_SESSION_TCP_SYN_SENT:
2923 case SNAT_SESSION_TCP_FIN_WAIT:
2924 case SNAT_SESSION_TCP_CLOSE_WAIT:
2925 case SNAT_SESSION_TCP_LAST_ACK:
2926 ses0->expire = now + sm->tcp_transitory_timeout;
2928 case SNAT_SESSION_TCP_ESTABLISHED:
2929 ses0->expire = now + sm->tcp_established_timeout;
2934 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2935 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2937 snat_in2out_trace_t *t =
2938 vlib_add_trace (vm, node, b0, sizeof (*t));
2939 t->is_slow_path = 0;
2940 t->sw_if_index = sw_if_index0;
2941 t->next_index = next0;
2942 t->session_index = ~0;
2944 t->session_index = ses0 - dm0->sessions;
2947 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2949 /* verify speculative enqueue, maybe switch current next frame */
2950 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2951 to_next, n_left_to_next,
2955 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2958 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
2959 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2961 return frame->n_vectors;
2964 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
2965 .function = snat_det_in2out_node_fn,
2966 .name = "nat44-det-in2out",
2967 .vector_size = sizeof (u32),
2968 .format_trace = format_snat_in2out_trace,
2969 .type = VLIB_NODE_TYPE_INTERNAL,
2971 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2972 .error_strings = snat_in2out_error_strings,
2974 .runtime_data_bytes = sizeof (snat_runtime_t),
2978 /* edit / add dispositions here */
2980 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2981 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2982 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2986 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
2989 * Get address and port values to be used for ICMP packet translation
2990 * and create session if needed
2992 * @param[in,out] sm NAT main
2993 * @param[in,out] node NAT node runtime
2994 * @param[in] thread_index thread index
2995 * @param[in,out] b0 buffer containing packet to be translated
2996 * @param[out] p_proto protocol used for matching
2997 * @param[out] p_value address and port after NAT translation
2998 * @param[out] p_dont_translate if packet should not be translated
2999 * @param d optional parameter
3000 * @param e optional parameter
3002 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
3003 u32 thread_index, vlib_buffer_t *b0,
3004 ip4_header_t *ip0, u8 *p_proto,
3005 snat_session_key_t *p_value,
3006 u8 *p_dont_translate, void *d, void *e)
3008 icmp46_header_t *icmp0;
3012 snat_det_out_key_t key0;
3013 u8 dont_translate = 0;
3015 icmp_echo_header_t *echo0, *inner_echo0 = 0;
3016 ip4_header_t *inner_ip0;
3017 void *l4_header = 0;
3018 icmp46_header_t *inner_icmp0;
3019 snat_det_map_t * dm0 = 0;
3020 ip4_address_t new_addr0;
3022 snat_det_session_t * ses0 = 0;
3023 ip4_address_t in_addr;
3026 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
3027 echo0 = (icmp_echo_header_t *)(icmp0+1);
3028 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3029 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
3031 if (!icmp_is_error_message (icmp0))
3033 protocol = SNAT_PROTOCOL_ICMP;
3034 in_addr = ip0->src_address;
3035 in_port = echo0->identifier;
3039 inner_ip0 = (ip4_header_t *)(echo0+1);
3040 l4_header = ip4_next_header (inner_ip0);
3041 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
3042 in_addr = inner_ip0->dst_address;
3045 case SNAT_PROTOCOL_ICMP:
3046 inner_icmp0 = (icmp46_header_t*)l4_header;
3047 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
3048 in_port = inner_echo0->identifier;
3050 case SNAT_PROTOCOL_UDP:
3051 case SNAT_PROTOCOL_TCP:
3052 in_port = ((tcp_udp_header_t*)l4_header)->dst_port;
3055 b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
3056 next0 = SNAT_IN2OUT_NEXT_DROP;
3061 dm0 = snat_det_map_by_user(sm, &in_addr);
3062 if (PREDICT_FALSE(!dm0))
3064 clib_warning("no match for internal host %U",
3065 format_ip4_address, &in_addr);
3066 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3067 IP_PROTOCOL_ICMP, rx_fib_index0)))
3072 next0 = SNAT_IN2OUT_NEXT_DROP;
3073 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3077 snat_det_forward(dm0, &in_addr, &new_addr0, &lo_port0);
3079 key0.ext_host_addr = ip0->dst_address;
3080 key0.ext_host_port = 0;
3082 ses0 = snat_det_find_ses_by_in(dm0, &in_addr, in_port, key0);
3083 if (PREDICT_FALSE(!ses0))
3085 if (PREDICT_FALSE(snat_not_translate_fast(sm, node, sw_if_index0, ip0,
3086 IP_PROTOCOL_ICMP, rx_fib_index0)))
3091 if (icmp0->type != ICMP4_echo_request)
3093 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3094 next0 = SNAT_IN2OUT_NEXT_DROP;
3097 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
3099 key0.out_port = clib_host_to_net_u16 (lo_port0 +
3100 ((i0 + clib_net_to_host_u16 (echo0->identifier)) % dm0->ports_per_host));
3102 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
3105 ses0 = snat_det_ses_create(dm0, &in_addr, echo0->identifier, &key0);
3108 if (PREDICT_FALSE(!ses0))
3110 next0 = SNAT_IN2OUT_NEXT_DROP;
3111 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
3116 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request &&
3117 !icmp_is_error_message (icmp0)))
3119 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
3120 next0 = SNAT_IN2OUT_NEXT_DROP;
3124 u32 now = (u32) vlib_time_now (sm->vlib_main);
3126 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
3127 ses0->expire = now + sm->icmp_timeout;
3130 *p_proto = protocol;
3133 p_value->addr = new_addr0;
3134 p_value->fib_index = sm->outside_fib_index;
3135 p_value->port = ses0->out.out_port;
3137 *p_dont_translate = dont_translate;
3139 *(snat_det_session_t**)d = ses0;
3141 *(snat_det_map_t**)e = dm0;
3145 /**********************/
3146 /*** worker handoff ***/
3147 /**********************/
3149 snat_in2out_worker_handoff_fn_inline (vlib_main_t * vm,
3150 vlib_node_runtime_t * node,
3151 vlib_frame_t * frame,
3154 snat_main_t *sm = &snat_main;
3155 vlib_thread_main_t *tm = vlib_get_thread_main ();
3156 u32 n_left_from, *from, *to_next = 0;
3157 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
3158 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
3160 vlib_frame_queue_elt_t *hf = 0;
3161 vlib_frame_t *f = 0;
3163 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
3164 u32 next_worker_index = 0;
3165 u32 current_worker_index = ~0;
3166 u32 thread_index = vlib_get_thread_index ();
3170 ASSERT (vec_len (sm->workers));
3174 fq_index = sm->fq_in2out_output_index;
3175 to_node_index = sm->in2out_output_node_index;
3179 fq_index = sm->fq_in2out_index;
3180 to_node_index = sm->in2out_node_index;
3183 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
3185 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
3187 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
3188 sm->first_worker_index + sm->num_workers - 1,
3189 (vlib_frame_queue_t *) (~0));
3192 from = vlib_frame_vector_args (frame);
3193 n_left_from = frame->n_vectors;
3195 while (n_left_from > 0)
3208 b0 = vlib_get_buffer (vm, bi0);
3210 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
3211 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3213 ip0 = vlib_buffer_get_current (b0);
3215 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
3217 if (PREDICT_FALSE (next_worker_index != thread_index))
3221 if (next_worker_index != current_worker_index)
3224 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3226 hf = vlib_get_worker_handoff_queue_elt (fq_index,
3228 handoff_queue_elt_by_worker_index);
3230 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
3231 to_next_worker = &hf->buffer_index[hf->n_vectors];
3232 current_worker_index = next_worker_index;
3235 /* enqueue to correct worker thread */
3236 to_next_worker[0] = bi0;
3238 n_left_to_next_worker--;
3240 if (n_left_to_next_worker == 0)
3242 hf->n_vectors = VLIB_FRAME_SIZE;
3243 vlib_put_frame_queue_elt (hf);
3244 current_worker_index = ~0;
3245 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
3252 /* if this is 1st frame */
3255 f = vlib_get_frame_to_node (vm, to_node_index);
3256 to_next = vlib_frame_vector_args (f);
3264 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
3265 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3267 snat_in2out_worker_handoff_trace_t *t =
3268 vlib_add_trace (vm, node, b0, sizeof (*t));
3269 t->next_worker_index = next_worker_index;
3270 t->do_handoff = do_handoff;
3275 vlib_put_frame_to_node (vm, to_node_index, f);
3278 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
3280 /* Ship frames to the worker nodes */
3281 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
3283 if (handoff_queue_elt_by_worker_index[i])
3285 hf = handoff_queue_elt_by_worker_index[i];
3287 * It works better to let the handoff node
3288 * rate-adapt, always ship the handoff queue element.
3290 if (1 || hf->n_vectors == hf->last_n_vectors)
3292 vlib_put_frame_queue_elt (hf);
3293 handoff_queue_elt_by_worker_index[i] = 0;
3296 hf->last_n_vectors = hf->n_vectors;
3298 congested_handoff_queue_by_worker_index[i] =
3299 (vlib_frame_queue_t *) (~0);
3302 current_worker_index = ~0;
3303 return frame->n_vectors;
3307 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
3308 vlib_node_runtime_t * node,
3309 vlib_frame_t * frame)
3311 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 0);
3314 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
3315 .function = snat_in2out_worker_handoff_fn,
3316 .name = "nat44-in2out-worker-handoff",
3317 .vector_size = sizeof (u32),
3318 .format_trace = format_snat_in2out_worker_handoff_trace,
3319 .type = VLIB_NODE_TYPE_INTERNAL,
3328 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node,
3329 snat_in2out_worker_handoff_fn);
3332 snat_in2out_output_worker_handoff_fn (vlib_main_t * vm,
3333 vlib_node_runtime_t * node,
3334 vlib_frame_t * frame)
3336 return snat_in2out_worker_handoff_fn_inline (vm, node, frame, 1);
3339 VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node) = {
3340 .function = snat_in2out_output_worker_handoff_fn,
3341 .name = "nat44-in2out-output-worker-handoff",
3342 .vector_size = sizeof (u32),
3343 .format_trace = format_snat_in2out_worker_handoff_trace,
3344 .type = VLIB_NODE_TYPE_INTERNAL,
3353 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_worker_handoff_node,
3354 snat_in2out_output_worker_handoff_fn);
3356 static_always_inline int
3357 is_hairpinning (snat_main_t *sm, ip4_address_t * dst_addr)
3359 snat_address_t * ap;
3360 clib_bihash_kv_8_8_t kv, value;
3361 snat_session_key_t m_key;
3363 vec_foreach (ap, sm->addresses)
3365 if (ap->addr.as_u32 == dst_addr->as_u32)
3369 m_key.addr.as_u32 = dst_addr->as_u32;
3370 m_key.fib_index = sm->outside_fib_index;
3373 kv.key = m_key.as_u64;
3374 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3381 snat_hairpin_dst_fn (vlib_main_t * vm,
3382 vlib_node_runtime_t * node,
3383 vlib_frame_t * frame)
3385 u32 n_left_from, * from, * to_next;
3386 snat_in2out_next_t next_index;
3387 u32 pkts_processed = 0;
3388 snat_main_t * sm = &snat_main;
3390 from = vlib_frame_vector_args (frame);
3391 n_left_from = frame->n_vectors;
3392 next_index = node->cached_next_index;
3394 while (n_left_from > 0)
3398 vlib_get_next_frame (vm, node, next_index,
3399 to_next, n_left_to_next);
3401 while (n_left_from > 0 && n_left_to_next > 0)
3409 /* speculatively enqueue b0 to the current next frame */
3415 n_left_to_next -= 1;
3417 b0 = vlib_get_buffer (vm, bi0);
3418 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3419 ip0 = vlib_buffer_get_current (b0);
3421 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3423 vnet_buffer (b0)->snat.flags = 0;
3424 if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
3426 if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
3428 udp_header_t * udp0 = ip4_next_header (ip0);
3429 tcp_header_t * tcp0 = (tcp_header_t *) udp0;
3431 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
3433 else if (proto0 == SNAT_PROTOCOL_ICMP)
3435 icmp46_header_t * icmp0 = ip4_next_header (ip0);
3437 snat_icmp_hairpinning (sm, b0, ip0, icmp0);
3441 snat_hairpinning_unknown_proto (sm, b0, ip0);
3444 vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
3445 clib_warning("is hairpinning");
3448 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3450 /* verify speculative enqueue, maybe switch current next frame */
3451 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3452 to_next, n_left_to_next,
3456 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3459 vlib_node_increment_counter (vm, snat_hairpin_dst_node.index,
3460 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3462 return frame->n_vectors;
3465 VLIB_REGISTER_NODE (snat_hairpin_dst_node) = {
3466 .function = snat_hairpin_dst_fn,
3467 .name = "nat44-hairpin-dst",
3468 .vector_size = sizeof (u32),
3469 .type = VLIB_NODE_TYPE_INTERNAL,
3470 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3471 .error_strings = snat_in2out_error_strings,
3474 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3475 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3479 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_dst_node,
3480 snat_hairpin_dst_fn);
3483 snat_hairpin_src_fn (vlib_main_t * vm,
3484 vlib_node_runtime_t * node,
3485 vlib_frame_t * frame)
3487 u32 n_left_from, * from, * to_next;
3488 snat_in2out_next_t next_index;
3489 u32 pkts_processed = 0;
3490 snat_main_t *sm = &snat_main;
3492 from = vlib_frame_vector_args (frame);
3493 n_left_from = frame->n_vectors;
3494 next_index = node->cached_next_index;
3496 while (n_left_from > 0)
3500 vlib_get_next_frame (vm, node, next_index,
3501 to_next, n_left_to_next);
3503 while (n_left_from > 0 && n_left_to_next > 0)
3508 snat_interface_t *i;
3511 /* speculatively enqueue b0 to the current next frame */
3517 n_left_to_next -= 1;
3519 b0 = vlib_get_buffer (vm, bi0);
3520 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3521 next0 = SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT;
3523 pool_foreach (i, sm->output_feature_interfaces,
3525 /* Only packets from NAT inside interface */
3526 if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
3528 if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
3529 SNAT_FLAG_HAIRPINNING))
3531 if (PREDICT_TRUE (sm->num_workers > 1))
3532 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
3534 next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
3540 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3542 /* verify speculative enqueue, maybe switch current next frame */
3543 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3544 to_next, n_left_to_next,
3548 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3551 vlib_node_increment_counter (vm, snat_hairpin_src_node.index,
3552 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3554 return frame->n_vectors;
3557 VLIB_REGISTER_NODE (snat_hairpin_src_node) = {
3558 .function = snat_hairpin_src_fn,
3559 .name = "nat44-hairpin-src",
3560 .vector_size = sizeof (u32),
3561 .type = VLIB_NODE_TYPE_INTERNAL,
3562 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3563 .error_strings = snat_in2out_error_strings,
3564 .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
3566 [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
3567 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
3568 [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
3569 [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
3573 VLIB_NODE_FUNCTION_MULTIARCH (snat_hairpin_src_node,
3574 snat_hairpin_src_fn);
3577 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
3578 vlib_node_runtime_t * node,
3579 vlib_frame_t * frame)
3581 u32 n_left_from, * from, * to_next;
3582 snat_in2out_next_t next_index;
3583 u32 pkts_processed = 0;
3584 snat_main_t * sm = &snat_main;
3585 u32 stats_node_index;
3587 stats_node_index = snat_in2out_fast_node.index;
3589 from = vlib_frame_vector_args (frame);
3590 n_left_from = frame->n_vectors;
3591 next_index = node->cached_next_index;
3593 while (n_left_from > 0)
3597 vlib_get_next_frame (vm, node, next_index,
3598 to_next, n_left_to_next);
3600 while (n_left_from > 0 && n_left_to_next > 0)
3608 u32 new_addr0, old_addr0;
3609 u16 old_port0, new_port0;
3610 udp_header_t * udp0;
3611 tcp_header_t * tcp0;
3612 icmp46_header_t * icmp0;
3613 snat_session_key_t key0, sm0;
3617 /* speculatively enqueue b0 to the current next frame */
3623 n_left_to_next -= 1;
3625 b0 = vlib_get_buffer (vm, bi0);
3626 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
3628 ip0 = vlib_buffer_get_current (b0);
3629 udp0 = ip4_next_header (ip0);
3630 tcp0 = (tcp_header_t *) udp0;
3631 icmp0 = (icmp46_header_t *) udp0;
3633 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
3634 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
3636 if (PREDICT_FALSE(ip0->ttl == 1))
3638 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
3639 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
3640 ICMP4_time_exceeded_ttl_exceeded_in_transit,
3642 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
3646 proto0 = ip_proto_to_snat_proto (ip0->protocol);
3648 if (PREDICT_FALSE (proto0 == ~0))
3651 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
3653 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
3654 rx_fib_index0, node, next0, ~0, 0, 0);
3658 key0.addr = ip0->src_address;
3659 key0.protocol = proto0;
3660 key0.port = udp0->src_port;
3661 key0.fib_index = rx_fib_index0;
3663 if (snat_static_mapping_match(sm, key0, &sm0, 0, 0))
3665 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
3666 next0= SNAT_IN2OUT_NEXT_DROP;
3670 new_addr0 = sm0.addr.as_u32;
3671 new_port0 = sm0.port;
3672 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
3673 old_addr0 = ip0->src_address.as_u32;
3674 ip0->src_address.as_u32 = new_addr0;
3676 sum0 = ip0->checksum;
3677 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3679 src_address /* changed member */);
3680 ip0->checksum = ip_csum_fold (sum0);
3682 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
3684 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3686 old_port0 = tcp0->src_port;
3687 tcp0->src_port = new_port0;
3689 sum0 = tcp0->checksum;
3690 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3692 dst_address /* changed member */);
3693 sum0 = ip_csum_update (sum0, old_port0, new_port0,
3694 ip4_header_t /* cheat */,
3695 length /* changed member */);
3696 tcp0->checksum = ip_csum_fold(sum0);
3700 old_port0 = udp0->src_port;
3701 udp0->src_port = new_port0;
3707 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
3709 sum0 = tcp0->checksum;
3710 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
3712 dst_address /* changed member */);
3713 tcp0->checksum = ip_csum_fold(sum0);
3718 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
3721 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
3722 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
3724 snat_in2out_trace_t *t =
3725 vlib_add_trace (vm, node, b0, sizeof (*t));
3726 t->sw_if_index = sw_if_index0;
3727 t->next_index = next0;
3730 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
3732 /* verify speculative enqueue, maybe switch current next frame */
3733 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
3734 to_next, n_left_to_next,
3738 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3741 vlib_node_increment_counter (vm, stats_node_index,
3742 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
3744 return frame->n_vectors;
3748 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
3749 .function = snat_in2out_fast_static_map_fn,
3750 .name = "nat44-in2out-fast",
3751 .vector_size = sizeof (u32),
3752 .format_trace = format_snat_in2out_fast_trace,
3753 .type = VLIB_NODE_TYPE_INTERNAL,
3755 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
3756 .error_strings = snat_in2out_error_strings,
3758 .runtime_data_bytes = sizeof (snat_runtime_t),
3760 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
3762 /* edit / add dispositions here */
3764 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
3765 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
3766 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
3767 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3771 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);