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.
17 * @brief NAT44 inside to outside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_syslog.h>
32 #include <nat/nat_ha.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/error.h>
36 #include <vppinfra/elog.h>
44 } snat_in2out_trace_t;
46 /* packet trace format function */
48 format_snat_in2out_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
55 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
57 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
58 t->sw_if_index, t->next_index, t->session_index);
64 format_snat_in2out_fast_trace (u8 * s, va_list * args)
66 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
67 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
68 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
70 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
71 t->sw_if_index, t->next_index);
76 #define foreach_snat_in2out_error \
77 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
78 _(IN2OUT_PACKETS, "good in2out packets processed") \
79 _(OUT_OF_PORTS, "out of ports") \
80 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
81 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
82 _(NO_TRANSLATION, "no translation") \
83 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
84 _(DROP_FRAGMENT, "drop fragment") \
85 _(MAX_REASS, "maximum reassemblies exceeded") \
86 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
87 _(TCP_PACKETS, "TCP packets") \
88 _(UDP_PACKETS, "UDP packets") \
89 _(ICMP_PACKETS, "ICMP packets") \
90 _(OTHER_PACKETS, "other protocol packets") \
91 _(FRAGMENTS, "fragments") \
92 _(CACHED_FRAGMENTS, "cached fragments") \
93 _(PROCESSED_FRAGMENTS, "processed fragments")
97 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
98 foreach_snat_in2out_error
101 } snat_in2out_error_t;
103 static char *snat_in2out_error_strings[] = {
104 #define _(sym,string) string,
105 foreach_snat_in2out_error
111 SNAT_IN2OUT_NEXT_LOOKUP,
112 SNAT_IN2OUT_NEXT_DROP,
113 SNAT_IN2OUT_NEXT_ICMP_ERROR,
114 SNAT_IN2OUT_NEXT_SLOW_PATH,
115 SNAT_IN2OUT_NEXT_REASS,
117 } snat_in2out_next_t;
120 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
121 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
122 u32 rx_fib_index0, u32 thread_index)
124 udp_header_t *udp0 = ip4_next_header (ip0);
125 snat_session_key_t key0, sm0;
126 clib_bihash_kv_8_8_t kv0, value0;
128 key0.addr = ip0->dst_address;
129 key0.port = udp0->dst_port;
130 key0.protocol = proto0;
131 key0.fib_index = sm->outside_fib_index;
132 kv0.key = key0.as_u64;
134 /* NAT packet aimed at external address if */
135 /* has active sessions */
136 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
139 /* or is static mappings */
140 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
146 if (sm->forwarding_enabled)
149 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
154 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
155 u32 proto0, u16 src_port, u16 dst_port,
156 u32 thread_index, u32 sw_if_index)
158 snat_session_key_t key0;
159 clib_bihash_kv_8_8_t kv0, value0;
163 key0.addr = ip0->src_address;
164 key0.port = src_port;
165 key0.protocol = proto0;
166 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
167 kv0.key = key0.as_u64;
169 if (!clib_bihash_search_8_8
170 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
174 key0.addr = ip0->dst_address;
175 key0.port = dst_port;
176 key0.protocol = proto0;
177 kv0.key = key0.as_u64;
178 if (!clib_bihash_search_8_8
179 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
183 pool_foreach (i, sm->output_feature_interfaces,
185 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
195 #ifndef CLIB_MARCH_VARIANT
197 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
199 snat_main_t *sm = &snat_main;
200 nat44_is_idle_session_ctx_t *ctx = arg;
202 u64 sess_timeout_time;
203 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
205 clib_bihash_kv_8_8_t s_kv;
207 s = pool_elt_at_index (tsm->sessions, kv->value);
208 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
209 if (ctx->now >= sess_timeout_time)
211 s_kv.key = s->out2in.as_u64;
212 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
213 nat_log_warn ("out2in key del failed");
215 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
216 s->in2out.addr.as_u32,
217 s->out2in.addr.as_u32,
221 s->in2out.fib_index);
223 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
224 &s->in2out.addr, s->in2out.port,
225 &s->out2in.addr, s->out2in.port,
228 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
229 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
232 if (!snat_is_session_static (s))
233 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
236 nat44_delete_session (sm, s, ctx->thread_index);
245 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
248 snat_session_key_t * key0,
249 snat_session_t ** sessionp,
250 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
253 snat_session_t *s = 0;
254 clib_bihash_kv_8_8_t kv0;
255 snat_session_key_t key1;
256 udp_header_t *udp0 = ip4_next_header (ip0);
258 nat_outside_fib_t *outside_fib;
259 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
262 .fp_proto = FIB_PROTOCOL_IP4,
265 .ip4.as_u32 = ip0->dst_address.as_u32,
268 nat44_is_idle_session_ctx_t ctx0;
270 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
272 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
273 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
274 nat_log_notice ("maximum sessions exceeded");
275 return SNAT_IN2OUT_NEXT_DROP;
278 key1.protocol = key0->protocol;
280 /* First try to match static mapping by local address and port */
281 if (snat_static_mapping_match
282 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
284 /* Try to create dynamic translation */
285 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
289 [thread_index].snat_thread_index))
291 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
292 return SNAT_IN2OUT_NEXT_DROP;
297 if (PREDICT_FALSE (identity_nat))
306 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
310 nat_log_warn ("create NAT user failed");
311 return SNAT_IN2OUT_NEXT_DROP;
314 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
317 nat44_delete_user_with_no_session (sm, u, thread_index);
318 nat_log_warn ("create NAT session failed");
319 return SNAT_IN2OUT_NEXT_DROP;
323 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
324 user_session_increment (sm, u, is_sm);
327 s->out2in.protocol = key0->protocol;
328 s->out2in.fib_index = sm->outside_fib_index;
329 switch (vec_len (sm->outside_fibs))
332 s->out2in.fib_index = sm->outside_fib_index;
335 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
339 vec_foreach (outside_fib, sm->outside_fibs)
341 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
342 if (FIB_NODE_INDEX_INVALID != fei)
344 if (fib_entry_get_resolving_interface (fei) != ~0)
346 s->out2in.fib_index = outside_fib->fib_index;
354 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
355 s->ext_host_port = udp0->dst_port;
358 /* Add to translation hashes */
360 ctx0.thread_index = thread_index;
361 kv0.key = s->in2out.as_u64;
362 kv0.value = s - sm->per_thread_data[thread_index].sessions;
363 if (clib_bihash_add_or_overwrite_stale_8_8
364 (&sm->per_thread_data[thread_index].in2out, &kv0,
365 nat44_i2o_is_idle_session_cb, &ctx0))
366 nat_log_notice ("in2out key add failed");
368 kv0.key = s->out2in.as_u64;
369 kv0.value = s - sm->per_thread_data[thread_index].sessions;
371 if (clib_bihash_add_or_overwrite_stale_8_8
372 (&sm->per_thread_data[thread_index].out2in, &kv0,
373 nat44_o2i_is_idle_session_cb, &ctx0))
374 nat_log_notice ("out2in key add failed");
377 snat_ipfix_logging_nat44_ses_create (thread_index,
378 s->in2out.addr.as_u32,
379 s->out2in.addr.as_u32,
382 s->out2in.port, s->in2out.fib_index);
384 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
385 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
386 s->out2in.port, s->in2out.protocol);
388 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
389 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
390 &s->ext_host_nat_addr, s->ext_host_nat_port,
391 s->in2out.protocol, s->in2out.fib_index, s->flags,
397 #ifndef CLIB_MARCH_VARIANT
399 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
400 snat_session_key_t * p_key0)
402 icmp46_header_t *icmp0;
403 snat_session_key_t key0;
404 icmp_echo_header_t *echo0, *inner_echo0 = 0;
405 ip4_header_t *inner_ip0 = 0;
407 icmp46_header_t *inner_icmp0;
409 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
410 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
412 if (!icmp_is_error_message (icmp0))
414 key0.protocol = SNAT_PROTOCOL_ICMP;
415 key0.addr = ip0->src_address;
416 key0.port = echo0->identifier;
420 inner_ip0 = (ip4_header_t *) (echo0 + 1);
421 l4_header = ip4_next_header (inner_ip0);
422 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
423 key0.addr = inner_ip0->dst_address;
424 switch (key0.protocol)
426 case SNAT_PROTOCOL_ICMP:
427 inner_icmp0 = (icmp46_header_t *) l4_header;
428 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
429 key0.port = inner_echo0->identifier;
431 case SNAT_PROTOCOL_UDP:
432 case SNAT_PROTOCOL_TCP:
433 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
436 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
440 return -1; /* success */
444 * Get address and port values to be used for ICMP packet translation
445 * and create session if needed
447 * @param[in,out] sm NAT main
448 * @param[in,out] node NAT node runtime
449 * @param[in] thread_index thread index
450 * @param[in,out] b0 buffer containing packet to be translated
451 * @param[out] p_proto protocol used for matching
452 * @param[out] p_value address and port after NAT translation
453 * @param[out] p_dont_translate if packet should not be translated
454 * @param d optional parameter
455 * @param e optional parameter
458 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
459 u32 thread_index, vlib_buffer_t * b0,
460 ip4_header_t * ip0, u8 * p_proto,
461 snat_session_key_t * p_value,
462 u8 * p_dont_translate, void *d, void *e)
464 icmp46_header_t *icmp0;
467 snat_session_key_t key0;
468 snat_session_t *s0 = 0;
469 u8 dont_translate = 0;
470 clib_bihash_kv_8_8_t kv0, value0;
474 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
475 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
476 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
478 err = icmp_get_key (ip0, &key0);
481 b0->error = node->errors[err];
482 next0 = SNAT_IN2OUT_NEXT_DROP;
485 key0.fib_index = rx_fib_index0;
487 kv0.key = key0.as_u64;
489 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
492 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
494 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
507 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
508 ip0, SNAT_PROTOCOL_ICMP,
517 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
519 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
520 next0 = SNAT_IN2OUT_NEXT_DROP;
524 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
525 thread_index, vlib_time_now (sm->vlib_main));
527 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
538 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
539 icmp0->type != ICMP4_echo_reply &&
540 !icmp_is_error_message (icmp0)))
542 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
543 next0 = SNAT_IN2OUT_NEXT_DROP;
547 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
552 *p_proto = key0.protocol;
554 *p_value = s0->out2in;
555 *p_dont_translate = dont_translate;
557 *(snat_session_t **) d = s0;
562 #ifndef CLIB_MARCH_VARIANT
564 * Get address and port values to be used for ICMP packet translation
566 * @param[in] sm NAT main
567 * @param[in,out] node NAT node runtime
568 * @param[in] thread_index thread index
569 * @param[in,out] b0 buffer containing packet to be translated
570 * @param[out] p_proto protocol used for matching
571 * @param[out] p_value address and port after NAT translation
572 * @param[out] p_dont_translate if packet should not be translated
573 * @param d optional parameter
574 * @param e optional parameter
577 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
578 u32 thread_index, vlib_buffer_t * b0,
579 ip4_header_t * ip0, u8 * p_proto,
580 snat_session_key_t * p_value,
581 u8 * p_dont_translate, void *d, void *e)
583 icmp46_header_t *icmp0;
586 snat_session_key_t key0;
587 snat_session_key_t sm0;
588 u8 dont_translate = 0;
593 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
594 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
595 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
597 err = icmp_get_key (ip0, &key0);
600 b0->error = node->errors[err];
601 next0 = SNAT_IN2OUT_NEXT_DROP;
604 key0.fib_index = rx_fib_index0;
606 if (snat_static_mapping_match
607 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
609 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
617 if (icmp_is_error_message (icmp0))
619 next0 = SNAT_IN2OUT_NEXT_DROP;
623 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
624 next0 = SNAT_IN2OUT_NEXT_DROP;
628 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
629 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
630 !icmp_is_error_message (icmp0)))
632 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
633 next0 = SNAT_IN2OUT_NEXT_DROP;
640 *p_proto = key0.protocol;
641 *p_dont_translate = dont_translate;
646 #ifndef CLIB_MARCH_VARIANT
648 icmp_in2out (snat_main_t * sm,
651 icmp46_header_t * icmp0,
654 vlib_node_runtime_t * node,
655 u32 next0, u32 thread_index, void *d, void *e)
657 snat_session_key_t sm0;
659 icmp_echo_header_t *echo0, *inner_echo0 = 0;
660 ip4_header_t *inner_ip0;
662 icmp46_header_t *inner_icmp0;
664 u32 new_addr0, old_addr0;
665 u16 old_id0, new_id0;
666 u16 old_checksum0, new_checksum0;
671 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
673 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
674 &protocol, &sm0, &dont_translate, d,
678 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
681 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
683 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
685 vlib_buffer_get_current (b0),
686 ntohs (ip0->length) -
687 ip4_header_bytes (ip0), 0);
688 checksum0 = ~ip_csum_fold (sum0);
689 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
691 next0 = SNAT_IN2OUT_NEXT_DROP;
696 old_addr0 = ip0->src_address.as_u32;
697 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
699 sum0 = ip0->checksum;
700 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
701 src_address /* changed member */ );
702 ip0->checksum = ip_csum_fold (sum0);
704 if (icmp0->checksum == 0)
705 icmp0->checksum = 0xffff;
707 if (!icmp_is_error_message (icmp0))
710 if (PREDICT_FALSE (new_id0 != echo0->identifier))
712 old_id0 = echo0->identifier;
714 echo0->identifier = new_id0;
716 sum0 = icmp0->checksum;
717 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
719 icmp0->checksum = ip_csum_fold (sum0);
724 inner_ip0 = (ip4_header_t *) (echo0 + 1);
725 l4_header = ip4_next_header (inner_ip0);
727 if (!ip4_header_checksum_is_valid (inner_ip0))
729 next0 = SNAT_IN2OUT_NEXT_DROP;
733 /* update inner destination IP address */
734 old_addr0 = inner_ip0->dst_address.as_u32;
735 inner_ip0->dst_address = sm0.addr;
736 new_addr0 = inner_ip0->dst_address.as_u32;
737 sum0 = icmp0->checksum;
738 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
739 dst_address /* changed member */ );
740 icmp0->checksum = ip_csum_fold (sum0);
742 /* update inner IP header checksum */
743 old_checksum0 = inner_ip0->checksum;
744 sum0 = inner_ip0->checksum;
745 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
746 dst_address /* changed member */ );
747 inner_ip0->checksum = ip_csum_fold (sum0);
748 new_checksum0 = inner_ip0->checksum;
749 sum0 = icmp0->checksum;
750 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
752 icmp0->checksum = ip_csum_fold (sum0);
756 case SNAT_PROTOCOL_ICMP:
757 inner_icmp0 = (icmp46_header_t *) l4_header;
758 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
760 old_id0 = inner_echo0->identifier;
762 inner_echo0->identifier = new_id0;
764 sum0 = icmp0->checksum;
765 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
767 icmp0->checksum = ip_csum_fold (sum0);
769 case SNAT_PROTOCOL_UDP:
770 case SNAT_PROTOCOL_TCP:
771 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
773 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
775 sum0 = icmp0->checksum;
776 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
778 icmp0->checksum = ip_csum_fold (sum0);
785 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
787 if (sm->deterministic ||
788 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
789 sm->endpoint_dependent))
790 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
799 icmp_in2out_slow_path (snat_main_t * sm,
802 icmp46_header_t * icmp0,
805 vlib_node_runtime_t * node,
807 f64 now, u32 thread_index, snat_session_t ** p_s0)
809 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
810 next0, thread_index, p_s0, 0);
811 snat_session_t *s0 = *p_s0;
812 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
815 nat44_session_update_counters (s0, now,
816 vlib_buffer_length_in_chain
817 (sm->vlib_main, b0), thread_index);
818 /* Per-user LRU list maintenance */
819 nat44_session_update_lru (sm, s0, thread_index);
825 nat_in2out_sm_unknown_proto (snat_main_t * sm,
827 ip4_header_t * ip, u32 rx_fib_index)
829 clib_bihash_kv_8_8_t kv, value;
830 snat_static_mapping_t *m;
831 snat_session_key_t m_key;
832 u32 old_addr, new_addr;
835 m_key.addr = ip->src_address;
838 m_key.fib_index = rx_fib_index;
839 kv.key = m_key.as_u64;
840 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
843 m = pool_elt_at_index (sm->static_mappings, value.value);
845 old_addr = ip->src_address.as_u32;
846 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
848 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
849 ip->checksum = ip_csum_fold (sum);
853 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
855 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
856 nat_hairpinning_sm_unknown_proto (sm, b, ip);
863 snat_in2out_node_fn_inline (vlib_main_t * vm,
864 vlib_node_runtime_t * node,
865 vlib_frame_t * frame, int is_slow_path,
866 int is_output_feature)
868 u32 n_left_from, *from, *to_next;
869 snat_in2out_next_t next_index;
870 u32 pkts_processed = 0;
871 snat_main_t *sm = &snat_main;
872 f64 now = vlib_time_now (vm);
873 u32 stats_node_index;
874 u32 thread_index = vm->thread_index;
875 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
878 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
879 sm->in2out_node_index;
881 from = vlib_frame_vector_args (frame);
882 n_left_from = frame->n_vectors;
883 next_index = node->cached_next_index;
885 while (n_left_from > 0)
889 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
891 while (n_left_from >= 4 && n_left_to_next >= 2)
894 vlib_buffer_t *b0, *b1;
896 u32 sw_if_index0, sw_if_index1;
897 ip4_header_t *ip0, *ip1;
898 ip_csum_t sum0, sum1;
899 u32 new_addr0, old_addr0, new_addr1, old_addr1;
900 u16 old_port0, new_port0, old_port1, new_port1;
901 udp_header_t *udp0, *udp1;
902 tcp_header_t *tcp0, *tcp1;
903 icmp46_header_t *icmp0, *icmp1;
904 snat_session_key_t key0, key1;
905 u32 rx_fib_index0, rx_fib_index1;
907 snat_session_t *s0 = 0, *s1 = 0;
908 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
909 u32 iph_offset0 = 0, iph_offset1 = 0;
911 /* Prefetch next iteration. */
913 vlib_buffer_t *p2, *p3;
915 p2 = vlib_get_buffer (vm, from[2]);
916 p3 = vlib_get_buffer (vm, from[3]);
918 vlib_prefetch_buffer_header (p2, LOAD);
919 vlib_prefetch_buffer_header (p3, LOAD);
921 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
922 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
925 /* speculatively enqueue b0 and b1 to the current next frame */
926 to_next[0] = bi0 = from[0];
927 to_next[1] = bi1 = from[1];
933 b0 = vlib_get_buffer (vm, bi0);
934 b1 = vlib_get_buffer (vm, bi1);
936 if (is_output_feature)
937 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
939 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
942 udp0 = ip4_next_header (ip0);
943 tcp0 = (tcp_header_t *) udp0;
944 icmp0 = (icmp46_header_t *) udp0;
946 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
947 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
950 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
952 if (PREDICT_FALSE (ip0->ttl == 1))
954 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
955 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
956 ICMP4_time_exceeded_ttl_exceeded_in_transit,
958 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
962 proto0 = ip_proto_to_snat_proto (ip0->protocol);
964 /* Next configured feature, probably ip4-lookup */
967 if (PREDICT_FALSE (proto0 == ~0))
969 if (nat_in2out_sm_unknown_proto
970 (sm, b0, ip0, rx_fib_index0))
972 next0 = SNAT_IN2OUT_NEXT_DROP;
974 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
980 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
982 next0 = icmp_in2out_slow_path
983 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
984 node, next0, now, thread_index, &s0);
991 if (PREDICT_FALSE (proto0 == ~0))
993 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
997 if (ip4_is_fragment (ip0))
999 next0 = SNAT_IN2OUT_NEXT_REASS;
1004 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1006 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1011 key0.addr = ip0->src_address;
1012 key0.port = udp0->src_port;
1013 key0.protocol = proto0;
1014 key0.fib_index = rx_fib_index0;
1016 kv0.key = key0.as_u64;
1019 (clib_bihash_search_8_8
1020 (&sm->per_thread_data[thread_index].in2out, &kv0,
1025 if (is_output_feature)
1027 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1040 (sm, node, sw_if_index0, ip0, proto0,
1041 rx_fib_index0, thread_index)))
1045 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1046 &s0, node, next0, thread_index, now);
1047 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1050 if (PREDICT_FALSE (!s0))
1055 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1061 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1064 b0->flags |= VNET_BUFFER_F_IS_NATED;
1066 old_addr0 = ip0->src_address.as_u32;
1067 ip0->src_address = s0->out2in.addr;
1068 new_addr0 = ip0->src_address.as_u32;
1069 if (!is_output_feature)
1070 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1072 sum0 = ip0->checksum;
1073 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1075 src_address /* changed member */ );
1076 ip0->checksum = ip_csum_fold (sum0);
1078 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1080 old_port0 = tcp0->src_port;
1081 tcp0->src_port = s0->out2in.port;
1082 new_port0 = tcp0->src_port;
1084 sum0 = tcp0->checksum;
1085 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1087 dst_address /* changed member */ );
1088 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1089 ip4_header_t /* cheat */ ,
1090 length /* changed member */ );
1091 mss_clamping (sm, tcp0, &sum0);
1092 tcp0->checksum = ip_csum_fold (sum0);
1097 old_port0 = udp0->src_port;
1098 udp0->src_port = s0->out2in.port;
1104 nat44_session_update_counters (s0, now,
1105 vlib_buffer_length_in_chain (vm, b0),
1107 /* Per-user LRU list maintenance */
1108 nat44_session_update_lru (sm, s0, thread_index);
1111 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1112 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1114 snat_in2out_trace_t *t =
1115 vlib_add_trace (vm, node, b0, sizeof (*t));
1116 t->is_slow_path = is_slow_path;
1117 t->sw_if_index = sw_if_index0;
1118 t->next_index = next0;
1119 t->session_index = ~0;
1122 s0 - sm->per_thread_data[thread_index].sessions;
1125 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1127 if (is_output_feature)
1128 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1130 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1133 udp1 = ip4_next_header (ip1);
1134 tcp1 = (tcp_header_t *) udp1;
1135 icmp1 = (icmp46_header_t *) udp1;
1137 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1138 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1141 if (PREDICT_FALSE (ip1->ttl == 1))
1143 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1144 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1145 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1147 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1151 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1153 /* Next configured feature, probably ip4-lookup */
1156 if (PREDICT_FALSE (proto1 == ~0))
1158 if (nat_in2out_sm_unknown_proto
1159 (sm, b1, ip1, rx_fib_index1))
1161 next1 = SNAT_IN2OUT_NEXT_DROP;
1163 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1169 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1171 next1 = icmp_in2out_slow_path
1172 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1173 next1, now, thread_index, &s1);
1180 if (PREDICT_FALSE (proto1 == ~0))
1182 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1186 if (ip4_is_fragment (ip1))
1188 next1 = SNAT_IN2OUT_NEXT_REASS;
1193 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1195 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1200 key1.addr = ip1->src_address;
1201 key1.port = udp1->src_port;
1202 key1.protocol = proto1;
1203 key1.fib_index = rx_fib_index1;
1205 kv1.key = key1.as_u64;
1208 (clib_bihash_search_8_8
1209 (&sm->per_thread_data[thread_index].in2out, &kv1,
1214 if (is_output_feature)
1216 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1229 (sm, node, sw_if_index1, ip1, proto1,
1230 rx_fib_index1, thread_index)))
1234 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1235 &s1, node, next1, thread_index, now);
1236 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1239 if (PREDICT_FALSE (!s1))
1244 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1250 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1253 b1->flags |= VNET_BUFFER_F_IS_NATED;
1255 old_addr1 = ip1->src_address.as_u32;
1256 ip1->src_address = s1->out2in.addr;
1257 new_addr1 = ip1->src_address.as_u32;
1258 if (!is_output_feature)
1259 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1261 sum1 = ip1->checksum;
1262 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1264 src_address /* changed member */ );
1265 ip1->checksum = ip_csum_fold (sum1);
1267 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1269 old_port1 = tcp1->src_port;
1270 tcp1->src_port = s1->out2in.port;
1271 new_port1 = tcp1->src_port;
1273 sum1 = tcp1->checksum;
1274 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1276 dst_address /* changed member */ );
1277 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1278 ip4_header_t /* cheat */ ,
1279 length /* changed member */ );
1280 mss_clamping (sm, tcp1, &sum1);
1281 tcp1->checksum = ip_csum_fold (sum1);
1286 old_port1 = udp1->src_port;
1287 udp1->src_port = s1->out2in.port;
1293 nat44_session_update_counters (s1, now,
1294 vlib_buffer_length_in_chain (vm, b1),
1296 /* Per-user LRU list maintenance */
1297 nat44_session_update_lru (sm, s1, thread_index);
1300 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1301 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1303 snat_in2out_trace_t *t =
1304 vlib_add_trace (vm, node, b1, sizeof (*t));
1305 t->sw_if_index = sw_if_index1;
1306 t->next_index = next1;
1307 t->session_index = ~0;
1310 s1 - sm->per_thread_data[thread_index].sessions;
1313 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1315 /* verify speculative enqueues, maybe switch current next frame */
1316 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1317 to_next, n_left_to_next,
1318 bi0, bi1, next0, next1);
1321 while (n_left_from > 0 && n_left_to_next > 0)
1329 u32 new_addr0, old_addr0;
1330 u16 old_port0, new_port0;
1333 icmp46_header_t *icmp0;
1334 snat_session_key_t key0;
1337 snat_session_t *s0 = 0;
1338 clib_bihash_kv_8_8_t kv0, value0;
1339 u32 iph_offset0 = 0;
1341 /* speculatively enqueue b0 to the current next frame */
1347 n_left_to_next -= 1;
1349 b0 = vlib_get_buffer (vm, bi0);
1350 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1352 if (is_output_feature)
1353 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1355 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1358 udp0 = ip4_next_header (ip0);
1359 tcp0 = (tcp_header_t *) udp0;
1360 icmp0 = (icmp46_header_t *) udp0;
1362 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1363 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1366 if (PREDICT_FALSE (ip0->ttl == 1))
1368 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1369 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1370 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1372 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1376 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1378 /* Next configured feature, probably ip4-lookup */
1381 if (PREDICT_FALSE (proto0 == ~0))
1383 if (nat_in2out_sm_unknown_proto
1384 (sm, b0, ip0, rx_fib_index0))
1386 next0 = SNAT_IN2OUT_NEXT_DROP;
1388 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1394 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1396 next0 = icmp_in2out_slow_path
1397 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1398 next0, now, thread_index, &s0);
1405 if (PREDICT_FALSE (proto0 == ~0))
1407 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1411 if (ip4_is_fragment (ip0))
1413 next0 = SNAT_IN2OUT_NEXT_REASS;
1418 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1420 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1425 key0.addr = ip0->src_address;
1426 key0.port = udp0->src_port;
1427 key0.protocol = proto0;
1428 key0.fib_index = rx_fib_index0;
1430 kv0.key = key0.as_u64;
1432 if (clib_bihash_search_8_8
1433 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1437 if (is_output_feature)
1439 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1452 (sm, node, sw_if_index0, ip0, proto0,
1453 rx_fib_index0, thread_index)))
1457 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1458 &s0, node, next0, thread_index, now);
1460 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1463 if (PREDICT_FALSE (!s0))
1468 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1474 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1477 b0->flags |= VNET_BUFFER_F_IS_NATED;
1479 old_addr0 = ip0->src_address.as_u32;
1480 ip0->src_address = s0->out2in.addr;
1481 new_addr0 = ip0->src_address.as_u32;
1482 if (!is_output_feature)
1483 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1485 sum0 = ip0->checksum;
1486 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1488 src_address /* changed member */ );
1489 ip0->checksum = ip_csum_fold (sum0);
1491 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1493 old_port0 = tcp0->src_port;
1494 tcp0->src_port = s0->out2in.port;
1495 new_port0 = tcp0->src_port;
1497 sum0 = tcp0->checksum;
1498 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1500 dst_address /* changed member */ );
1501 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1502 ip4_header_t /* cheat */ ,
1503 length /* changed member */ );
1504 mss_clamping (sm, tcp0, &sum0);
1505 tcp0->checksum = ip_csum_fold (sum0);
1510 old_port0 = udp0->src_port;
1511 udp0->src_port = s0->out2in.port;
1517 nat44_session_update_counters (s0, now,
1518 vlib_buffer_length_in_chain (vm, b0),
1520 /* Per-user LRU list maintenance */
1521 nat44_session_update_lru (sm, s0, thread_index);
1524 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1525 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1527 snat_in2out_trace_t *t =
1528 vlib_add_trace (vm, node, b0, sizeof (*t));
1529 t->is_slow_path = is_slow_path;
1530 t->sw_if_index = sw_if_index0;
1531 t->next_index = next0;
1532 t->session_index = ~0;
1535 s0 - sm->per_thread_data[thread_index].sessions;
1538 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1540 /* verify speculative enqueue, maybe switch current next frame */
1541 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1542 to_next, n_left_to_next,
1546 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1549 vlib_node_increment_counter (vm, stats_node_index,
1550 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1552 vlib_node_increment_counter (vm, stats_node_index,
1553 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1554 vlib_node_increment_counter (vm, stats_node_index,
1555 SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1556 vlib_node_increment_counter (vm, stats_node_index,
1557 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1558 vlib_node_increment_counter (vm, stats_node_index,
1559 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1561 vlib_node_increment_counter (vm, stats_node_index,
1562 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1564 return frame->n_vectors;
1567 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1568 vlib_node_runtime_t * node,
1569 vlib_frame_t * frame)
1571 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1576 VLIB_REGISTER_NODE (snat_in2out_node) = {
1577 .name = "nat44-in2out",
1578 .vector_size = sizeof (u32),
1579 .format_trace = format_snat_in2out_trace,
1580 .type = VLIB_NODE_TYPE_INTERNAL,
1582 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1583 .error_strings = snat_in2out_error_strings,
1585 .runtime_data_bytes = sizeof (snat_runtime_t),
1587 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1589 /* edit / add dispositions here */
1591 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1592 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1593 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1594 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1595 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1600 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1601 vlib_node_runtime_t * node,
1602 vlib_frame_t * frame)
1604 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1609 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1610 .name = "nat44-in2out-output",
1611 .vector_size = sizeof (u32),
1612 .format_trace = format_snat_in2out_trace,
1613 .type = VLIB_NODE_TYPE_INTERNAL,
1615 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1616 .error_strings = snat_in2out_error_strings,
1618 .runtime_data_bytes = sizeof (snat_runtime_t),
1620 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1622 /* edit / add dispositions here */
1624 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1625 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1626 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1627 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1628 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1633 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1634 vlib_node_runtime_t * node,
1635 vlib_frame_t * frame)
1637 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1642 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1643 .name = "nat44-in2out-slowpath",
1644 .vector_size = sizeof (u32),
1645 .format_trace = format_snat_in2out_trace,
1646 .type = VLIB_NODE_TYPE_INTERNAL,
1648 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1649 .error_strings = snat_in2out_error_strings,
1651 .runtime_data_bytes = sizeof (snat_runtime_t),
1653 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1655 /* edit / add dispositions here */
1657 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1658 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1659 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1660 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1661 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1666 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1667 vlib_node_runtime_t * node,
1668 vlib_frame_t * frame)
1670 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1675 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1676 .name = "nat44-in2out-output-slowpath",
1677 .vector_size = sizeof (u32),
1678 .format_trace = format_snat_in2out_trace,
1679 .type = VLIB_NODE_TYPE_INTERNAL,
1681 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1682 .error_strings = snat_in2out_error_strings,
1684 .runtime_data_bytes = sizeof (snat_runtime_t),
1686 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1688 /* edit / add dispositions here */
1690 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1691 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1692 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1693 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1694 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1699 VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
1700 vlib_node_runtime_t * node,
1701 vlib_frame_t * frame)
1703 u32 n_left_from, *from, *to_next;
1704 snat_in2out_next_t next_index;
1705 u32 pkts_processed = 0, cached_fragments = 0;
1706 snat_main_t *sm = &snat_main;
1707 f64 now = vlib_time_now (vm);
1708 u32 thread_index = vm->thread_index;
1709 snat_main_per_thread_data_t *per_thread_data =
1710 &sm->per_thread_data[thread_index];
1711 u32 *fragments_to_drop = 0;
1712 u32 *fragments_to_loopback = 0;
1714 from = vlib_frame_vector_args (frame);
1715 n_left_from = frame->n_vectors;
1716 next_index = node->cached_next_index;
1718 while (n_left_from > 0)
1722 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1724 while (n_left_from > 0 && n_left_to_next > 0)
1726 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1731 nat_reass_ip4_t *reass0;
1734 icmp46_header_t *icmp0;
1735 snat_session_key_t key0;
1736 clib_bihash_kv_8_8_t kv0, value0;
1737 snat_session_t *s0 = 0;
1738 u16 old_port0, new_port0;
1741 /* speculatively enqueue b0 to the current next frame */
1747 n_left_to_next -= 1;
1749 b0 = vlib_get_buffer (vm, bi0);
1750 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1752 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1754 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1757 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1759 next0 = SNAT_IN2OUT_NEXT_DROP;
1760 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1764 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1765 udp0 = ip4_next_header (ip0);
1766 tcp0 = (tcp_header_t *) udp0;
1767 icmp0 = (icmp46_header_t *) udp0;
1768 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1770 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1774 1, &fragments_to_drop);
1776 if (PREDICT_FALSE (!reass0))
1778 next0 = SNAT_IN2OUT_NEXT_DROP;
1779 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1780 nat_log_notice ("maximum reassemblies exceeded");
1784 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1786 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1788 next0 = icmp_in2out_slow_path
1789 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1790 next0, now, thread_index, &s0);
1792 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1795 reass0->sess_index = s0 - per_thread_data->sessions;
1797 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1798 nat_ip4_reass_get_frags (reass0,
1799 &fragments_to_loopback);
1805 key0.addr = ip0->src_address;
1806 key0.port = udp0->src_port;
1807 key0.protocol = proto0;
1808 key0.fib_index = rx_fib_index0;
1809 kv0.key = key0.as_u64;
1811 if (clib_bihash_search_8_8
1812 (&per_thread_data->in2out, &kv0, &value0))
1816 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1820 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1821 &s0, node, next0, thread_index, now);
1823 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1826 if (PREDICT_FALSE (!s0))
1829 reass0->sess_index = s0 - per_thread_data->sessions;
1833 s0 = pool_elt_at_index (per_thread_data->sessions,
1835 reass0->sess_index = value0.value;
1837 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1841 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1843 if (nat_ip4_reass_add_fragment
1844 (thread_index, reass0, bi0, &fragments_to_drop))
1846 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1848 ("maximum fragments per reassembly exceeded");
1849 next0 = SNAT_IN2OUT_NEXT_DROP;
1855 s0 = pool_elt_at_index (per_thread_data->sessions,
1856 reass0->sess_index);
1859 old_addr0 = ip0->src_address.as_u32;
1860 ip0->src_address = s0->out2in.addr;
1861 new_addr0 = ip0->src_address.as_u32;
1862 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1864 sum0 = ip0->checksum;
1865 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1867 src_address /* changed member */ );
1868 ip0->checksum = ip_csum_fold (sum0);
1870 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1872 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1874 old_port0 = tcp0->src_port;
1875 tcp0->src_port = s0->out2in.port;
1876 new_port0 = tcp0->src_port;
1878 sum0 = tcp0->checksum;
1879 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1881 dst_address /* changed member */ );
1882 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1883 ip4_header_t /* cheat */ ,
1884 length /* changed member */ );
1885 tcp0->checksum = ip_csum_fold (sum0);
1889 old_port0 = udp0->src_port;
1890 udp0->src_port = s0->out2in.port;
1896 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1897 s0->ext_host_port, proto0, 0);
1900 nat44_session_update_counters (s0, now,
1901 vlib_buffer_length_in_chain (vm, b0),
1903 /* Per-user LRU list maintenance */
1904 nat44_session_update_lru (sm, s0, thread_index);
1907 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1908 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1910 nat44_reass_trace_t *t =
1911 vlib_add_trace (vm, node, b0, sizeof (*t));
1912 t->cached = cached0;
1913 t->sw_if_index = sw_if_index0;
1914 t->next_index = next0;
1925 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1927 /* verify speculative enqueue, maybe switch current next frame */
1928 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1929 to_next, n_left_to_next,
1933 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1935 from = vlib_frame_vector_args (frame);
1936 u32 len = vec_len (fragments_to_loopback);
1937 if (len <= VLIB_FRAME_SIZE)
1939 clib_memcpy_fast (from, fragments_to_loopback,
1940 sizeof (u32) * len);
1942 vec_reset_length (fragments_to_loopback);
1946 clib_memcpy_fast (from, fragments_to_loopback +
1947 (len - VLIB_FRAME_SIZE),
1948 sizeof (u32) * VLIB_FRAME_SIZE);
1949 n_left_from = VLIB_FRAME_SIZE;
1950 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1955 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1958 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
1959 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1961 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
1962 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
1965 nat_send_all_to_node (vm, fragments_to_drop, node,
1966 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1967 SNAT_IN2OUT_NEXT_DROP);
1969 vec_free (fragments_to_drop);
1970 vec_free (fragments_to_loopback);
1971 return frame->n_vectors;
1975 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1976 .name = "nat44-in2out-reass",
1977 .vector_size = sizeof (u32),
1978 .format_trace = format_nat44_reass_trace,
1979 .type = VLIB_NODE_TYPE_INTERNAL,
1981 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1982 .error_strings = snat_in2out_error_strings,
1984 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1986 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1987 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1988 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1989 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1990 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1995 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1996 vlib_node_runtime_t * node,
1997 vlib_frame_t * frame)
1999 u32 n_left_from, *from, *to_next;
2000 snat_in2out_next_t next_index;
2001 u32 pkts_processed = 0;
2002 snat_main_t *sm = &snat_main;
2003 u32 stats_node_index;
2005 stats_node_index = sm->in2out_fast_node_index;
2007 from = vlib_frame_vector_args (frame);
2008 n_left_from = frame->n_vectors;
2009 next_index = node->cached_next_index;
2011 while (n_left_from > 0)
2015 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2017 while (n_left_from > 0 && n_left_to_next > 0)
2025 u32 new_addr0, old_addr0;
2026 u16 old_port0, new_port0;
2029 icmp46_header_t *icmp0;
2030 snat_session_key_t key0, sm0;
2034 /* speculatively enqueue b0 to the current next frame */
2040 n_left_to_next -= 1;
2042 b0 = vlib_get_buffer (vm, bi0);
2043 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2045 ip0 = vlib_buffer_get_current (b0);
2046 udp0 = ip4_next_header (ip0);
2047 tcp0 = (tcp_header_t *) udp0;
2048 icmp0 = (icmp46_header_t *) udp0;
2050 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2052 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2054 if (PREDICT_FALSE (ip0->ttl == 1))
2056 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2057 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2058 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2060 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2064 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2066 if (PREDICT_FALSE (proto0 == ~0))
2069 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2071 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2072 rx_fib_index0, node, next0, ~0, 0, 0);
2076 key0.addr = ip0->src_address;
2077 key0.protocol = proto0;
2078 key0.port = udp0->src_port;
2079 key0.fib_index = rx_fib_index0;
2081 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2083 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2084 next0 = SNAT_IN2OUT_NEXT_DROP;
2088 new_addr0 = sm0.addr.as_u32;
2089 new_port0 = sm0.port;
2090 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2091 old_addr0 = ip0->src_address.as_u32;
2092 ip0->src_address.as_u32 = new_addr0;
2094 sum0 = ip0->checksum;
2095 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2097 src_address /* changed member */ );
2098 ip0->checksum = ip_csum_fold (sum0);
2100 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2102 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2104 old_port0 = tcp0->src_port;
2105 tcp0->src_port = new_port0;
2107 sum0 = tcp0->checksum;
2108 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2110 dst_address /* changed member */ );
2111 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2112 ip4_header_t /* cheat */ ,
2113 length /* changed member */ );
2114 mss_clamping (sm, tcp0, &sum0);
2115 tcp0->checksum = ip_csum_fold (sum0);
2119 old_port0 = udp0->src_port;
2120 udp0->src_port = new_port0;
2126 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2128 sum0 = tcp0->checksum;
2129 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2131 dst_address /* changed member */ );
2132 mss_clamping (sm, tcp0, &sum0);
2133 tcp0->checksum = ip_csum_fold (sum0);
2138 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2141 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2142 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2144 snat_in2out_trace_t *t =
2145 vlib_add_trace (vm, node, b0, sizeof (*t));
2146 t->sw_if_index = sw_if_index0;
2147 t->next_index = next0;
2150 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2152 /* verify speculative enqueue, maybe switch current next frame */
2153 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2154 to_next, n_left_to_next,
2158 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2161 vlib_node_increment_counter (vm, stats_node_index,
2162 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2164 return frame->n_vectors;
2169 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2170 .name = "nat44-in2out-fast",
2171 .vector_size = sizeof (u32),
2172 .format_trace = format_snat_in2out_fast_trace,
2173 .type = VLIB_NODE_TYPE_INTERNAL,
2175 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2176 .error_strings = snat_in2out_error_strings,
2178 .runtime_data_bytes = sizeof (snat_runtime_t),
2180 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2182 /* edit / add dispositions here */
2184 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2185 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2186 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2187 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2188 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2194 * fd.io coding-style-patch-verification: ON
2197 * eval: (c-set-style "gnu")