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>
27 #include <vnet/udp/udp.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_reass.h>
31 #include <nat/nat_inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
45 } snat_in2out_trace_t;
47 /* packet trace format function */
49 format_snat_in2out_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
56 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
58 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
59 t->sw_if_index, t->next_index, t->session_index);
65 format_snat_in2out_fast_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
71 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
72 t->sw_if_index, t->next_index);
77 #define foreach_snat_in2out_error \
78 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
79 _(IN2OUT_PACKETS, "good in2out packets processed") \
80 _(OUT_OF_PORTS, "out of ports") \
81 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
82 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
83 _(NO_TRANSLATION, "no translation") \
84 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
85 _(DROP_FRAGMENT, "drop fragment") \
86 _(MAX_REASS, "maximum reassemblies exceeded") \
87 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
88 _(TCP_PACKETS, "TCP packets") \
89 _(UDP_PACKETS, "UDP packets") \
90 _(ICMP_PACKETS, "ICMP packets") \
91 _(OTHER_PACKETS, "other protocol packets") \
92 _(FRAGMENTS, "fragments") \
93 _(CACHED_FRAGMENTS, "cached fragments") \
94 _(PROCESSED_FRAGMENTS, "processed fragments")
98 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
99 foreach_snat_in2out_error
102 } snat_in2out_error_t;
104 static char *snat_in2out_error_strings[] = {
105 #define _(sym,string) string,
106 foreach_snat_in2out_error
112 SNAT_IN2OUT_NEXT_LOOKUP,
113 SNAT_IN2OUT_NEXT_DROP,
114 SNAT_IN2OUT_NEXT_ICMP_ERROR,
115 SNAT_IN2OUT_NEXT_SLOW_PATH,
116 SNAT_IN2OUT_NEXT_REASS,
118 } snat_in2out_next_t;
121 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
122 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
123 u32 rx_fib_index0, u32 thread_index)
125 udp_header_t *udp0 = ip4_next_header (ip0);
126 snat_session_key_t key0, sm0;
127 clib_bihash_kv_8_8_t kv0, value0;
129 key0.addr = ip0->dst_address;
130 key0.port = udp0->dst_port;
131 key0.protocol = proto0;
132 key0.fib_index = sm->outside_fib_index;
133 kv0.key = key0.as_u64;
135 /* NAT packet aimed at external address if */
136 /* has active sessions */
137 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
140 /* or is static mappings */
141 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
147 if (sm->forwarding_enabled)
150 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
155 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
156 u32 proto0, u16 src_port, u16 dst_port,
157 u32 thread_index, u32 sw_if_index)
159 snat_session_key_t key0;
160 clib_bihash_kv_8_8_t kv0, value0;
164 key0.addr = ip0->src_address;
165 key0.port = src_port;
166 key0.protocol = proto0;
167 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
168 kv0.key = key0.as_u64;
170 if (!clib_bihash_search_8_8
171 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
175 key0.addr = ip0->dst_address;
176 key0.port = dst_port;
177 key0.protocol = proto0;
178 kv0.key = key0.as_u64;
179 if (!clib_bihash_search_8_8
180 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
184 pool_foreach (i, sm->output_feature_interfaces,
186 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
196 #ifndef CLIB_MARCH_VARIANT
198 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
200 snat_main_t *sm = &snat_main;
201 nat44_is_idle_session_ctx_t *ctx = arg;
203 u64 sess_timeout_time;
204 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
206 clib_bihash_kv_8_8_t s_kv;
208 s = pool_elt_at_index (tsm->sessions, kv->value);
209 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
210 if (ctx->now >= sess_timeout_time)
212 s_kv.key = s->out2in.as_u64;
213 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
214 nat_elog_warn ("out2in key del failed");
216 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
217 s->in2out.addr.as_u32,
218 s->out2in.addr.as_u32,
222 s->in2out.fib_index);
224 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
225 &s->in2out.addr, s->in2out.port,
226 &s->out2in.addr, s->out2in.port,
229 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
230 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
233 if (!snat_is_session_static (s))
234 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
237 nat44_delete_session (sm, s, ctx->thread_index);
246 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
249 snat_session_key_t * key0,
250 snat_session_t ** sessionp,
251 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
254 snat_session_t *s = 0;
255 clib_bihash_kv_8_8_t kv0;
256 snat_session_key_t key1;
257 udp_header_t *udp0 = ip4_next_header (ip0);
259 nat_outside_fib_t *outside_fib;
260 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
263 .fp_proto = FIB_PROTOCOL_IP4,
266 .ip4.as_u32 = ip0->dst_address.as_u32,
269 nat44_is_idle_session_ctx_t ctx0;
271 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
273 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
274 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
275 nat_elog_notice ("maximum sessions exceeded");
276 return SNAT_IN2OUT_NEXT_DROP;
279 key1.protocol = key0->protocol;
281 /* First try to match static mapping by local address and port */
282 if (snat_static_mapping_match
283 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
285 /* Try to create dynamic translation */
286 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
290 [thread_index].snat_thread_index))
292 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
293 return SNAT_IN2OUT_NEXT_DROP;
298 if (PREDICT_FALSE (identity_nat))
307 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
311 nat_elog_warn ("create NAT user failed");
312 return SNAT_IN2OUT_NEXT_DROP;
315 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
318 nat44_delete_user_with_no_session (sm, u, thread_index);
319 nat_elog_warn ("create NAT session failed");
320 return SNAT_IN2OUT_NEXT_DROP;
324 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
325 user_session_increment (sm, u, is_sm);
328 s->out2in.protocol = key0->protocol;
329 s->out2in.fib_index = sm->outside_fib_index;
330 switch (vec_len (sm->outside_fibs))
333 s->out2in.fib_index = sm->outside_fib_index;
336 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
340 vec_foreach (outside_fib, sm->outside_fibs)
342 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
343 if (FIB_NODE_INDEX_INVALID != fei)
345 if (fib_entry_get_resolving_interface (fei) != ~0)
347 s->out2in.fib_index = outside_fib->fib_index;
355 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
356 s->ext_host_port = udp0->dst_port;
359 /* Add to translation hashes */
361 ctx0.thread_index = thread_index;
362 kv0.key = s->in2out.as_u64;
363 kv0.value = s - sm->per_thread_data[thread_index].sessions;
364 if (clib_bihash_add_or_overwrite_stale_8_8
365 (&sm->per_thread_data[thread_index].in2out, &kv0,
366 nat44_i2o_is_idle_session_cb, &ctx0))
367 nat_elog_notice ("in2out key add failed");
369 kv0.key = s->out2in.as_u64;
370 kv0.value = s - sm->per_thread_data[thread_index].sessions;
372 if (clib_bihash_add_or_overwrite_stale_8_8
373 (&sm->per_thread_data[thread_index].out2in, &kv0,
374 nat44_o2i_is_idle_session_cb, &ctx0))
375 nat_elog_notice ("out2in key add failed");
378 snat_ipfix_logging_nat44_ses_create (thread_index,
379 s->in2out.addr.as_u32,
380 s->out2in.addr.as_u32,
383 s->out2in.port, s->in2out.fib_index);
385 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
386 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
387 s->out2in.port, s->in2out.protocol);
389 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
390 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
391 &s->ext_host_nat_addr, s->ext_host_nat_port,
392 s->in2out.protocol, s->in2out.fib_index, s->flags,
398 #ifndef CLIB_MARCH_VARIANT
400 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
401 snat_session_key_t * p_key0)
403 icmp46_header_t *icmp0;
404 snat_session_key_t key0;
405 icmp_echo_header_t *echo0, *inner_echo0 = 0;
406 ip4_header_t *inner_ip0 = 0;
408 icmp46_header_t *inner_icmp0;
410 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
411 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
413 if (!icmp_is_error_message (icmp0))
415 key0.protocol = SNAT_PROTOCOL_ICMP;
416 key0.addr = ip0->src_address;
417 key0.port = echo0->identifier;
421 inner_ip0 = (ip4_header_t *) (echo0 + 1);
422 l4_header = ip4_next_header (inner_ip0);
423 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
424 key0.addr = inner_ip0->dst_address;
425 switch (key0.protocol)
427 case SNAT_PROTOCOL_ICMP:
428 inner_icmp0 = (icmp46_header_t *) l4_header;
429 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
430 key0.port = inner_echo0->identifier;
432 case SNAT_PROTOCOL_UDP:
433 case SNAT_PROTOCOL_TCP:
434 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
437 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
441 return -1; /* success */
445 * Get address and port values to be used for ICMP packet translation
446 * and create session if needed
448 * @param[in,out] sm NAT main
449 * @param[in,out] node NAT node runtime
450 * @param[in] thread_index thread index
451 * @param[in,out] b0 buffer containing packet to be translated
452 * @param[out] p_proto protocol used for matching
453 * @param[out] p_value address and port after NAT translation
454 * @param[out] p_dont_translate if packet should not be translated
455 * @param d optional parameter
456 * @param e optional parameter
459 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
460 u32 thread_index, vlib_buffer_t * b0,
461 ip4_header_t * ip0, u8 * p_proto,
462 snat_session_key_t * p_value,
463 u8 * p_dont_translate, void *d, void *e)
465 icmp46_header_t *icmp0;
468 snat_session_key_t key0;
469 snat_session_t *s0 = 0;
470 u8 dont_translate = 0;
471 clib_bihash_kv_8_8_t kv0, value0;
475 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
476 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
477 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
479 err = icmp_get_key (ip0, &key0);
482 b0->error = node->errors[err];
483 next0 = SNAT_IN2OUT_NEXT_DROP;
486 key0.fib_index = rx_fib_index0;
488 kv0.key = key0.as_u64;
490 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
493 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
495 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
508 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
509 ip0, SNAT_PROTOCOL_ICMP,
518 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
520 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
521 next0 = SNAT_IN2OUT_NEXT_DROP;
525 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
526 thread_index, vlib_time_now (sm->vlib_main));
528 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
539 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
540 icmp0->type != ICMP4_echo_reply &&
541 !icmp_is_error_message (icmp0)))
543 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
544 next0 = SNAT_IN2OUT_NEXT_DROP;
548 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
553 *p_proto = key0.protocol;
555 *p_value = s0->out2in;
556 *p_dont_translate = dont_translate;
558 *(snat_session_t **) d = s0;
563 #ifndef CLIB_MARCH_VARIANT
565 * Get address and port values to be used for ICMP packet translation
567 * @param[in] sm NAT main
568 * @param[in,out] node NAT node runtime
569 * @param[in] thread_index thread index
570 * @param[in,out] b0 buffer containing packet to be translated
571 * @param[out] p_proto protocol used for matching
572 * @param[out] p_value address and port after NAT translation
573 * @param[out] p_dont_translate if packet should not be translated
574 * @param d optional parameter
575 * @param e optional parameter
578 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
579 u32 thread_index, vlib_buffer_t * b0,
580 ip4_header_t * ip0, u8 * p_proto,
581 snat_session_key_t * p_value,
582 u8 * p_dont_translate, void *d, void *e)
584 icmp46_header_t *icmp0;
587 snat_session_key_t key0;
588 snat_session_key_t sm0;
589 u8 dont_translate = 0;
594 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
595 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
596 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
598 err = icmp_get_key (ip0, &key0);
601 b0->error = node->errors[err];
602 next0 = SNAT_IN2OUT_NEXT_DROP;
605 key0.fib_index = rx_fib_index0;
607 if (snat_static_mapping_match
608 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
610 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
618 if (icmp_is_error_message (icmp0))
620 next0 = SNAT_IN2OUT_NEXT_DROP;
624 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
625 next0 = SNAT_IN2OUT_NEXT_DROP;
629 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
630 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
631 !icmp_is_error_message (icmp0)))
633 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
634 next0 = SNAT_IN2OUT_NEXT_DROP;
641 *p_proto = key0.protocol;
642 *p_dont_translate = dont_translate;
647 #ifndef CLIB_MARCH_VARIANT
649 icmp_in2out (snat_main_t * sm,
652 icmp46_header_t * icmp0,
655 vlib_node_runtime_t * node,
656 u32 next0, u32 thread_index, void *d, void *e)
658 snat_session_key_t sm0;
660 icmp_echo_header_t *echo0, *inner_echo0 = 0;
661 ip4_header_t *inner_ip0;
663 icmp46_header_t *inner_icmp0;
665 u32 new_addr0, old_addr0;
666 u16 old_id0, new_id0;
667 u16 old_checksum0, new_checksum0;
672 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
674 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
675 &protocol, &sm0, &dont_translate, d,
679 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
682 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
684 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
686 vlib_buffer_get_current (b0),
687 ntohs (ip0->length) -
688 ip4_header_bytes (ip0), 0);
689 checksum0 = ~ip_csum_fold (sum0);
690 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
692 next0 = SNAT_IN2OUT_NEXT_DROP;
697 old_addr0 = ip0->src_address.as_u32;
698 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
700 sum0 = ip0->checksum;
701 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
702 src_address /* changed member */ );
703 ip0->checksum = ip_csum_fold (sum0);
705 if (icmp0->checksum == 0)
706 icmp0->checksum = 0xffff;
708 if (!icmp_is_error_message (icmp0))
711 if (PREDICT_FALSE (new_id0 != echo0->identifier))
713 old_id0 = echo0->identifier;
715 echo0->identifier = new_id0;
717 sum0 = icmp0->checksum;
718 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
720 icmp0->checksum = ip_csum_fold (sum0);
725 inner_ip0 = (ip4_header_t *) (echo0 + 1);
726 l4_header = ip4_next_header (inner_ip0);
728 if (!ip4_header_checksum_is_valid (inner_ip0))
730 next0 = SNAT_IN2OUT_NEXT_DROP;
734 /* update inner destination IP address */
735 old_addr0 = inner_ip0->dst_address.as_u32;
736 inner_ip0->dst_address = sm0.addr;
737 new_addr0 = inner_ip0->dst_address.as_u32;
738 sum0 = icmp0->checksum;
739 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
740 dst_address /* changed member */ );
741 icmp0->checksum = ip_csum_fold (sum0);
743 /* update inner IP header checksum */
744 old_checksum0 = inner_ip0->checksum;
745 sum0 = inner_ip0->checksum;
746 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
747 dst_address /* changed member */ );
748 inner_ip0->checksum = ip_csum_fold (sum0);
749 new_checksum0 = inner_ip0->checksum;
750 sum0 = icmp0->checksum;
751 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
753 icmp0->checksum = ip_csum_fold (sum0);
757 case SNAT_PROTOCOL_ICMP:
758 inner_icmp0 = (icmp46_header_t *) l4_header;
759 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
761 old_id0 = inner_echo0->identifier;
763 inner_echo0->identifier = new_id0;
765 sum0 = icmp0->checksum;
766 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
768 icmp0->checksum = ip_csum_fold (sum0);
770 case SNAT_PROTOCOL_UDP:
771 case SNAT_PROTOCOL_TCP:
772 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
774 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
776 sum0 = icmp0->checksum;
777 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
779 icmp0->checksum = ip_csum_fold (sum0);
786 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
788 if (sm->deterministic ||
789 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
790 sm->endpoint_dependent))
791 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
800 icmp_in2out_slow_path (snat_main_t * sm,
803 icmp46_header_t * icmp0,
806 vlib_node_runtime_t * node,
808 f64 now, u32 thread_index, snat_session_t ** p_s0)
810 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
811 next0, thread_index, p_s0, 0);
812 snat_session_t *s0 = *p_s0;
813 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
816 nat44_session_update_counters (s0, now,
817 vlib_buffer_length_in_chain
818 (sm->vlib_main, b0), thread_index);
819 /* Per-user LRU list maintenance */
820 nat44_session_update_lru (sm, s0, thread_index);
826 nat_in2out_sm_unknown_proto (snat_main_t * sm,
828 ip4_header_t * ip, u32 rx_fib_index)
830 clib_bihash_kv_8_8_t kv, value;
831 snat_static_mapping_t *m;
832 snat_session_key_t m_key;
833 u32 old_addr, new_addr;
836 m_key.addr = ip->src_address;
839 m_key.fib_index = rx_fib_index;
840 kv.key = m_key.as_u64;
841 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
844 m = pool_elt_at_index (sm->static_mappings, value.value);
846 old_addr = ip->src_address.as_u32;
847 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
849 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
850 ip->checksum = ip_csum_fold (sum);
854 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
856 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
857 nat_hairpinning_sm_unknown_proto (sm, b, ip);
864 snat_in2out_node_fn_inline (vlib_main_t * vm,
865 vlib_node_runtime_t * node,
866 vlib_frame_t * frame, int is_slow_path,
867 int is_output_feature)
869 u32 n_left_from, *from, *to_next;
870 snat_in2out_next_t next_index;
871 u32 pkts_processed = 0;
872 snat_main_t *sm = &snat_main;
873 f64 now = vlib_time_now (vm);
874 u32 stats_node_index;
875 u32 thread_index = vm->thread_index;
876 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
879 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
880 sm->in2out_node_index;
882 from = vlib_frame_vector_args (frame);
883 n_left_from = frame->n_vectors;
884 next_index = node->cached_next_index;
886 while (n_left_from > 0)
890 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
892 while (n_left_from >= 4 && n_left_to_next >= 2)
895 vlib_buffer_t *b0, *b1;
897 u32 sw_if_index0, sw_if_index1;
898 ip4_header_t *ip0, *ip1;
899 ip_csum_t sum0, sum1;
900 u32 new_addr0, old_addr0, new_addr1, old_addr1;
901 u16 old_port0, new_port0, old_port1, new_port1;
902 udp_header_t *udp0, *udp1;
903 tcp_header_t *tcp0, *tcp1;
904 icmp46_header_t *icmp0, *icmp1;
905 snat_session_key_t key0, key1;
906 u32 rx_fib_index0, rx_fib_index1;
908 snat_session_t *s0 = 0, *s1 = 0;
909 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
910 u32 iph_offset0 = 0, iph_offset1 = 0;
912 /* Prefetch next iteration. */
914 vlib_buffer_t *p2, *p3;
916 p2 = vlib_get_buffer (vm, from[2]);
917 p3 = vlib_get_buffer (vm, from[3]);
919 vlib_prefetch_buffer_header (p2, LOAD);
920 vlib_prefetch_buffer_header (p3, LOAD);
922 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
923 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
926 /* speculatively enqueue b0 and b1 to the current next frame */
927 to_next[0] = bi0 = from[0];
928 to_next[1] = bi1 = from[1];
934 b0 = vlib_get_buffer (vm, bi0);
935 b1 = vlib_get_buffer (vm, bi1);
937 if (is_output_feature)
938 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
940 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
943 udp0 = ip4_next_header (ip0);
944 tcp0 = (tcp_header_t *) udp0;
945 icmp0 = (icmp46_header_t *) udp0;
947 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
948 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
951 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
953 if (PREDICT_FALSE (ip0->ttl == 1))
955 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
956 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
957 ICMP4_time_exceeded_ttl_exceeded_in_transit,
959 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
963 proto0 = ip_proto_to_snat_proto (ip0->protocol);
965 /* Next configured feature, probably ip4-lookup */
968 if (PREDICT_FALSE (proto0 == ~0))
970 if (nat_in2out_sm_unknown_proto
971 (sm, b0, ip0, rx_fib_index0))
973 next0 = SNAT_IN2OUT_NEXT_DROP;
975 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
981 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
983 next0 = icmp_in2out_slow_path
984 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
985 node, next0, now, thread_index, &s0);
992 if (PREDICT_FALSE (proto0 == ~0))
994 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
998 if (ip4_is_fragment (ip0))
1000 next0 = SNAT_IN2OUT_NEXT_REASS;
1005 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1007 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1012 key0.addr = ip0->src_address;
1013 key0.port = udp0->src_port;
1014 key0.protocol = proto0;
1015 key0.fib_index = rx_fib_index0;
1017 kv0.key = key0.as_u64;
1020 (clib_bihash_search_8_8
1021 (&sm->per_thread_data[thread_index].in2out, &kv0,
1026 if (is_output_feature)
1028 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1038 * Send DHCP packets to the ipv4 stack, or we won't
1039 * be able to use dhcp client on the outside interface
1042 ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1043 && proto0 == SNAT_PROTOCOL_UDP
1044 && (udp0->dst_port ==
1045 clib_host_to_net_u16
1046 (UDP_DST_PORT_dhcp_to_server))))
1053 (sm, node, sw_if_index0, ip0, proto0,
1054 rx_fib_index0, thread_index)))
1058 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1059 &s0, node, next0, thread_index, now);
1060 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1063 if (PREDICT_FALSE (!s0))
1068 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1074 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1077 b0->flags |= VNET_BUFFER_F_IS_NATED;
1079 old_addr0 = ip0->src_address.as_u32;
1080 ip0->src_address = s0->out2in.addr;
1081 new_addr0 = ip0->src_address.as_u32;
1082 if (!is_output_feature)
1083 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1085 sum0 = ip0->checksum;
1086 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1088 src_address /* changed member */ );
1089 ip0->checksum = ip_csum_fold (sum0);
1091 old_port0 = udp0->src_port;
1092 new_port0 = udp0->src_port = s0->out2in.port;
1094 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1096 sum0 = tcp0->checksum;
1097 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1099 dst_address /* changed member */ );
1100 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1101 ip4_header_t /* cheat */ ,
1102 length /* changed member */ );
1103 mss_clamping (sm, tcp0, &sum0);
1104 tcp0->checksum = ip_csum_fold (sum0);
1109 if (PREDICT_FALSE (udp0->checksum))
1111 sum0 = udp0->checksum;
1112 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1114 dst_address /* changed member */ );
1115 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1116 ip4_header_t /* cheat */ ,
1117 length /* changed member */ );
1118 udp0->checksum = ip_csum_fold (sum0);
1124 nat44_session_update_counters (s0, now,
1125 vlib_buffer_length_in_chain (vm, b0),
1127 /* Per-user LRU list maintenance */
1128 nat44_session_update_lru (sm, s0, thread_index);
1131 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1132 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1134 snat_in2out_trace_t *t =
1135 vlib_add_trace (vm, node, b0, sizeof (*t));
1136 t->is_slow_path = is_slow_path;
1137 t->sw_if_index = sw_if_index0;
1138 t->next_index = next0;
1139 t->session_index = ~0;
1142 s0 - sm->per_thread_data[thread_index].sessions;
1145 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1147 if (is_output_feature)
1148 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1150 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1153 udp1 = ip4_next_header (ip1);
1154 tcp1 = (tcp_header_t *) udp1;
1155 icmp1 = (icmp46_header_t *) udp1;
1157 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1158 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1161 if (PREDICT_FALSE (ip1->ttl == 1))
1163 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1164 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1165 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1167 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1171 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1173 /* Next configured feature, probably ip4-lookup */
1176 if (PREDICT_FALSE (proto1 == ~0))
1178 if (nat_in2out_sm_unknown_proto
1179 (sm, b1, ip1, rx_fib_index1))
1181 next1 = SNAT_IN2OUT_NEXT_DROP;
1183 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1189 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1191 next1 = icmp_in2out_slow_path
1192 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1193 next1, now, thread_index, &s1);
1200 if (PREDICT_FALSE (proto1 == ~0))
1202 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1206 if (ip4_is_fragment (ip1))
1208 next1 = SNAT_IN2OUT_NEXT_REASS;
1213 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1215 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1220 key1.addr = ip1->src_address;
1221 key1.port = udp1->src_port;
1222 key1.protocol = proto1;
1223 key1.fib_index = rx_fib_index1;
1225 kv1.key = key1.as_u64;
1228 (clib_bihash_search_8_8
1229 (&sm->per_thread_data[thread_index].in2out, &kv1,
1234 if (is_output_feature)
1236 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1246 * Send DHCP packets to the ipv4 stack, or we won't
1247 * be able to use dhcp client on the outside interface
1250 ((b1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1251 && proto1 == SNAT_PROTOCOL_UDP
1252 && (udp1->dst_port ==
1253 clib_host_to_net_u16
1254 (UDP_DST_PORT_dhcp_to_server))))
1261 (sm, node, sw_if_index1, ip1, proto1,
1262 rx_fib_index1, thread_index)))
1266 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1267 &s1, node, next1, thread_index, now);
1268 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1271 if (PREDICT_FALSE (!s1))
1276 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1282 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1285 b1->flags |= VNET_BUFFER_F_IS_NATED;
1287 old_addr1 = ip1->src_address.as_u32;
1288 ip1->src_address = s1->out2in.addr;
1289 new_addr1 = ip1->src_address.as_u32;
1290 if (!is_output_feature)
1291 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1293 sum1 = ip1->checksum;
1294 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1296 src_address /* changed member */ );
1297 ip1->checksum = ip_csum_fold (sum1);
1299 old_port1 = udp1->src_port;
1300 new_port1 = udp1->src_port = s1->out2in.port;
1302 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1304 sum1 = tcp1->checksum;
1305 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1307 dst_address /* changed member */ );
1308 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1309 ip4_header_t /* cheat */ ,
1310 length /* changed member */ );
1311 mss_clamping (sm, tcp1, &sum1);
1312 tcp1->checksum = ip_csum_fold (sum1);
1317 if (PREDICT_FALSE (udp1->checksum))
1319 sum1 = udp1->checksum;
1320 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1322 dst_address /* changed member */ );
1323 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1324 ip4_header_t /* cheat */ ,
1325 length /* changed member */ );
1326 udp1->checksum = ip_csum_fold (sum1);
1332 nat44_session_update_counters (s1, now,
1333 vlib_buffer_length_in_chain (vm, b1),
1335 /* Per-user LRU list maintenance */
1336 nat44_session_update_lru (sm, s1, thread_index);
1339 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1340 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1342 snat_in2out_trace_t *t =
1343 vlib_add_trace (vm, node, b1, sizeof (*t));
1344 t->sw_if_index = sw_if_index1;
1345 t->next_index = next1;
1346 t->session_index = ~0;
1349 s1 - sm->per_thread_data[thread_index].sessions;
1352 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1354 /* verify speculative enqueues, maybe switch current next frame */
1355 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1356 to_next, n_left_to_next,
1357 bi0, bi1, next0, next1);
1360 while (n_left_from > 0 && n_left_to_next > 0)
1368 u32 new_addr0, old_addr0;
1369 u16 old_port0, new_port0;
1372 icmp46_header_t *icmp0;
1373 snat_session_key_t key0;
1376 snat_session_t *s0 = 0;
1377 clib_bihash_kv_8_8_t kv0, value0;
1378 u32 iph_offset0 = 0;
1380 /* speculatively enqueue b0 to the current next frame */
1386 n_left_to_next -= 1;
1388 b0 = vlib_get_buffer (vm, bi0);
1389 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1391 if (is_output_feature)
1392 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1394 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1397 udp0 = ip4_next_header (ip0);
1398 tcp0 = (tcp_header_t *) udp0;
1399 icmp0 = (icmp46_header_t *) udp0;
1401 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1402 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1405 if (PREDICT_FALSE (ip0->ttl == 1))
1407 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1408 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1409 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1411 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1415 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1417 /* Next configured feature, probably ip4-lookup */
1420 if (PREDICT_FALSE (proto0 == ~0))
1422 if (nat_in2out_sm_unknown_proto
1423 (sm, b0, ip0, rx_fib_index0))
1425 next0 = SNAT_IN2OUT_NEXT_DROP;
1427 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1433 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1435 next0 = icmp_in2out_slow_path
1436 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1437 next0, now, thread_index, &s0);
1444 if (PREDICT_FALSE (proto0 == ~0))
1446 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1450 if (ip4_is_fragment (ip0))
1452 next0 = SNAT_IN2OUT_NEXT_REASS;
1457 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1459 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1464 key0.addr = ip0->src_address;
1465 key0.port = udp0->src_port;
1466 key0.protocol = proto0;
1467 key0.fib_index = rx_fib_index0;
1469 kv0.key = key0.as_u64;
1471 if (clib_bihash_search_8_8
1472 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1476 if (is_output_feature)
1478 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1488 * Send DHCP packets to the ipv4 stack, or we won't
1489 * be able to use dhcp client on the outside interface
1492 ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1493 && proto0 == SNAT_PROTOCOL_UDP
1494 && (udp0->dst_port ==
1495 clib_host_to_net_u16
1496 (UDP_DST_PORT_dhcp_to_server))))
1503 (sm, node, sw_if_index0, ip0, proto0,
1504 rx_fib_index0, thread_index)))
1508 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1509 &s0, node, next0, thread_index, now);
1511 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1514 if (PREDICT_FALSE (!s0))
1519 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1525 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1528 b0->flags |= VNET_BUFFER_F_IS_NATED;
1530 old_addr0 = ip0->src_address.as_u32;
1531 ip0->src_address = s0->out2in.addr;
1532 new_addr0 = ip0->src_address.as_u32;
1533 if (!is_output_feature)
1534 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1536 sum0 = ip0->checksum;
1537 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1539 src_address /* changed member */ );
1540 ip0->checksum = ip_csum_fold (sum0);
1542 old_port0 = udp0->src_port;
1543 new_port0 = udp0->src_port = s0->out2in.port;
1545 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1547 sum0 = tcp0->checksum;
1548 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1550 dst_address /* changed member */ );
1551 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1552 ip4_header_t /* cheat */ ,
1553 length /* changed member */ );
1554 mss_clamping (sm, tcp0, &sum0);
1555 tcp0->checksum = ip_csum_fold (sum0);
1560 if (PREDICT_FALSE (udp0->checksum))
1562 sum0 = udp0->checksum;
1563 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1565 dst_address /* changed member */ );
1566 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1567 ip4_header_t /* cheat */ ,
1568 length /* changed member */ );
1569 udp0->checksum = ip_csum_fold (sum0);
1575 nat44_session_update_counters (s0, now,
1576 vlib_buffer_length_in_chain (vm, b0),
1578 /* Per-user LRU list maintenance */
1579 nat44_session_update_lru (sm, s0, thread_index);
1582 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1583 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1585 snat_in2out_trace_t *t =
1586 vlib_add_trace (vm, node, b0, sizeof (*t));
1587 t->is_slow_path = is_slow_path;
1588 t->sw_if_index = sw_if_index0;
1589 t->next_index = next0;
1590 t->session_index = ~0;
1593 s0 - sm->per_thread_data[thread_index].sessions;
1596 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1598 /* verify speculative enqueue, maybe switch current next frame */
1599 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1600 to_next, n_left_to_next,
1604 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1607 vlib_node_increment_counter (vm, stats_node_index,
1608 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1610 vlib_node_increment_counter (vm, stats_node_index,
1611 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1612 vlib_node_increment_counter (vm, stats_node_index,
1613 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1614 vlib_node_increment_counter (vm, stats_node_index,
1615 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1616 vlib_node_increment_counter (vm, stats_node_index,
1617 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1619 vlib_node_increment_counter (vm, stats_node_index,
1620 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1622 return frame->n_vectors;
1625 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1626 vlib_node_runtime_t * node,
1627 vlib_frame_t * frame)
1629 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1634 VLIB_REGISTER_NODE (snat_in2out_node) = {
1635 .name = "nat44-in2out",
1636 .vector_size = sizeof (u32),
1637 .format_trace = format_snat_in2out_trace,
1638 .type = VLIB_NODE_TYPE_INTERNAL,
1640 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1641 .error_strings = snat_in2out_error_strings,
1643 .runtime_data_bytes = sizeof (snat_runtime_t),
1645 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1647 /* edit / add dispositions here */
1649 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1650 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1651 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1652 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1653 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1658 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1659 vlib_node_runtime_t * node,
1660 vlib_frame_t * frame)
1662 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1667 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1668 .name = "nat44-in2out-output",
1669 .vector_size = sizeof (u32),
1670 .format_trace = format_snat_in2out_trace,
1671 .type = VLIB_NODE_TYPE_INTERNAL,
1673 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1674 .error_strings = snat_in2out_error_strings,
1676 .runtime_data_bytes = sizeof (snat_runtime_t),
1678 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1680 /* edit / add dispositions here */
1682 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1683 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1684 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1685 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1686 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1691 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1692 vlib_node_runtime_t * node,
1693 vlib_frame_t * frame)
1695 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1700 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1701 .name = "nat44-in2out-slowpath",
1702 .vector_size = sizeof (u32),
1703 .format_trace = format_snat_in2out_trace,
1704 .type = VLIB_NODE_TYPE_INTERNAL,
1706 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1707 .error_strings = snat_in2out_error_strings,
1709 .runtime_data_bytes = sizeof (snat_runtime_t),
1711 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1713 /* edit / add dispositions here */
1715 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1716 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1717 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1718 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1719 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1724 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1725 vlib_node_runtime_t * node,
1726 vlib_frame_t * frame)
1728 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1733 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1734 .name = "nat44-in2out-output-slowpath",
1735 .vector_size = sizeof (u32),
1736 .format_trace = format_snat_in2out_trace,
1737 .type = VLIB_NODE_TYPE_INTERNAL,
1739 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1740 .error_strings = snat_in2out_error_strings,
1742 .runtime_data_bytes = sizeof (snat_runtime_t),
1744 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1746 /* edit / add dispositions here */
1748 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1749 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1750 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1751 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1752 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1757 VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
1758 vlib_node_runtime_t * node,
1759 vlib_frame_t * frame)
1761 u32 n_left_from, *from, *to_next;
1762 snat_in2out_next_t next_index;
1763 u32 pkts_processed = 0, cached_fragments = 0;
1764 snat_main_t *sm = &snat_main;
1765 f64 now = vlib_time_now (vm);
1766 u32 thread_index = vm->thread_index;
1767 snat_main_per_thread_data_t *per_thread_data =
1768 &sm->per_thread_data[thread_index];
1769 u32 *fragments_to_drop = 0;
1770 u32 *fragments_to_loopback = 0;
1772 from = vlib_frame_vector_args (frame);
1773 n_left_from = frame->n_vectors;
1774 next_index = node->cached_next_index;
1776 while (n_left_from > 0)
1780 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1782 while (n_left_from > 0 && n_left_to_next > 0)
1784 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1789 nat_reass_ip4_t *reass0;
1792 icmp46_header_t *icmp0;
1793 snat_session_key_t key0;
1794 clib_bihash_kv_8_8_t kv0, value0;
1795 snat_session_t *s0 = 0;
1796 u16 old_port0, new_port0;
1799 /* speculatively enqueue b0 to the current next frame */
1805 n_left_to_next -= 1;
1807 b0 = vlib_get_buffer (vm, bi0);
1808 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1810 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1812 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1815 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1817 next0 = SNAT_IN2OUT_NEXT_DROP;
1818 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1822 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1823 udp0 = ip4_next_header (ip0);
1824 tcp0 = (tcp_header_t *) udp0;
1825 icmp0 = (icmp46_header_t *) udp0;
1826 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1828 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1832 1, &fragments_to_drop);
1834 if (PREDICT_FALSE (!reass0))
1836 next0 = SNAT_IN2OUT_NEXT_DROP;
1837 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1838 nat_elog_notice ("maximum reassemblies exceeded");
1842 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1844 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1846 next0 = icmp_in2out_slow_path
1847 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1848 next0, now, thread_index, &s0);
1850 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1853 reass0->sess_index = s0 - per_thread_data->sessions;
1855 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1856 nat_ip4_reass_get_frags (reass0,
1857 &fragments_to_loopback);
1863 key0.addr = ip0->src_address;
1864 key0.port = udp0->src_port;
1865 key0.protocol = proto0;
1866 key0.fib_index = rx_fib_index0;
1867 kv0.key = key0.as_u64;
1869 if (clib_bihash_search_8_8
1870 (&per_thread_data->in2out, &kv0, &value0))
1874 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1878 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1879 &s0, node, next0, thread_index, now);
1881 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1884 if (PREDICT_FALSE (!s0))
1887 reass0->sess_index = s0 - per_thread_data->sessions;
1891 s0 = pool_elt_at_index (per_thread_data->sessions,
1893 reass0->sess_index = value0.value;
1895 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1899 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1901 if (nat_ip4_reass_add_fragment
1902 (thread_index, reass0, bi0, &fragments_to_drop))
1904 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1906 ("maximum fragments per reassembly exceeded");
1907 next0 = SNAT_IN2OUT_NEXT_DROP;
1913 s0 = pool_elt_at_index (per_thread_data->sessions,
1914 reass0->sess_index);
1917 old_addr0 = ip0->src_address.as_u32;
1918 ip0->src_address = s0->out2in.addr;
1919 new_addr0 = ip0->src_address.as_u32;
1920 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1922 sum0 = ip0->checksum;
1923 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1925 src_address /* changed member */ );
1926 ip0->checksum = ip_csum_fold (sum0);
1928 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1930 old_port0 = udp0->src_port;
1931 new_port0 = udp0->src_port = s0->out2in.port;
1933 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1935 sum0 = tcp0->checksum;
1936 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1938 dst_address /* changed member */ );
1939 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1940 ip4_header_t /* cheat */ ,
1941 length /* changed member */ );
1942 tcp0->checksum = ip_csum_fold (sum0);
1944 else if (PREDICT_FALSE (udp0->checksum))
1946 sum0 = udp0->checksum;
1947 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1949 dst_address /* changed member */ );
1950 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1951 ip4_header_t /* cheat */ ,
1952 length /* changed member */ );
1953 udp0->checksum = ip_csum_fold (sum0);
1958 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1959 s0->ext_host_port, proto0, 0);
1962 nat44_session_update_counters (s0, now,
1963 vlib_buffer_length_in_chain (vm, b0),
1965 /* Per-user LRU list maintenance */
1966 nat44_session_update_lru (sm, s0, thread_index);
1969 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1970 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1972 nat44_reass_trace_t *t =
1973 vlib_add_trace (vm, node, b0, sizeof (*t));
1974 t->cached = cached0;
1975 t->sw_if_index = sw_if_index0;
1976 t->next_index = next0;
1987 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1989 /* verify speculative enqueue, maybe switch current next frame */
1990 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1991 to_next, n_left_to_next,
1995 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1997 from = vlib_frame_vector_args (frame);
1998 u32 len = vec_len (fragments_to_loopback);
1999 if (len <= VLIB_FRAME_SIZE)
2001 clib_memcpy_fast (from, fragments_to_loopback,
2002 sizeof (u32) * len);
2004 vec_reset_length (fragments_to_loopback);
2008 clib_memcpy_fast (from, fragments_to_loopback +
2009 (len - VLIB_FRAME_SIZE),
2010 sizeof (u32) * VLIB_FRAME_SIZE);
2011 n_left_from = VLIB_FRAME_SIZE;
2012 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2017 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2020 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
2021 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
2023 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
2024 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
2027 nat_send_all_to_node (vm, fragments_to_drop, node,
2028 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
2029 SNAT_IN2OUT_NEXT_DROP);
2031 vec_free (fragments_to_drop);
2032 vec_free (fragments_to_loopback);
2033 return frame->n_vectors;
2037 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
2038 .name = "nat44-in2out-reass",
2039 .vector_size = sizeof (u32),
2040 .format_trace = format_nat44_reass_trace,
2041 .type = VLIB_NODE_TYPE_INTERNAL,
2043 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2044 .error_strings = snat_in2out_error_strings,
2046 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2048 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2049 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2050 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2051 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2052 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2057 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
2058 vlib_node_runtime_t * node,
2059 vlib_frame_t * frame)
2061 u32 n_left_from, *from, *to_next;
2062 snat_in2out_next_t next_index;
2063 u32 pkts_processed = 0;
2064 snat_main_t *sm = &snat_main;
2065 u32 stats_node_index;
2067 stats_node_index = sm->in2out_fast_node_index;
2069 from = vlib_frame_vector_args (frame);
2070 n_left_from = frame->n_vectors;
2071 next_index = node->cached_next_index;
2073 while (n_left_from > 0)
2077 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2079 while (n_left_from > 0 && n_left_to_next > 0)
2087 u32 new_addr0, old_addr0;
2088 u16 old_port0, new_port0;
2091 icmp46_header_t *icmp0;
2092 snat_session_key_t key0, sm0;
2096 /* speculatively enqueue b0 to the current next frame */
2102 n_left_to_next -= 1;
2104 b0 = vlib_get_buffer (vm, bi0);
2105 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2107 ip0 = vlib_buffer_get_current (b0);
2108 udp0 = ip4_next_header (ip0);
2109 tcp0 = (tcp_header_t *) udp0;
2110 icmp0 = (icmp46_header_t *) udp0;
2112 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2114 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2116 if (PREDICT_FALSE (ip0->ttl == 1))
2118 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2119 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2120 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2122 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2126 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2128 if (PREDICT_FALSE (proto0 == ~0))
2131 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2133 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2134 rx_fib_index0, node, next0, ~0, 0, 0);
2138 key0.addr = ip0->src_address;
2139 key0.protocol = proto0;
2140 key0.port = udp0->src_port;
2141 key0.fib_index = rx_fib_index0;
2143 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2145 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2146 next0 = SNAT_IN2OUT_NEXT_DROP;
2150 new_addr0 = sm0.addr.as_u32;
2151 new_port0 = sm0.port;
2152 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2153 old_addr0 = ip0->src_address.as_u32;
2154 ip0->src_address.as_u32 = new_addr0;
2156 sum0 = ip0->checksum;
2157 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2159 src_address /* changed member */ );
2160 ip0->checksum = ip_csum_fold (sum0);
2162 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2164 old_port0 = udp0->src_port;
2165 udp0->src_port = new_port0;
2167 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2169 sum0 = tcp0->checksum;
2170 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2172 dst_address /* changed member */ );
2173 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2174 ip4_header_t /* cheat */ ,
2175 length /* changed member */ );
2176 mss_clamping (sm, tcp0, &sum0);
2177 tcp0->checksum = ip_csum_fold (sum0);
2179 else if (udp0->checksum)
2181 sum0 = udp0->checksum;
2182 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2184 dst_address /* changed member */ );
2185 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2186 ip4_header_t /* cheat */ ,
2187 length /* changed member */ );
2188 udp0->checksum = ip_csum_fold (sum0);
2193 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2195 sum0 = tcp0->checksum;
2196 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2198 dst_address /* changed member */ );
2199 mss_clamping (sm, tcp0, &sum0);
2200 tcp0->checksum = ip_csum_fold (sum0);
2202 else if (udp0->checksum)
2204 sum0 = udp0->checksum;
2205 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2207 dst_address /* changed member */ );
2208 udp0->checksum = ip_csum_fold (sum0);
2213 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2216 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2217 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2219 snat_in2out_trace_t *t =
2220 vlib_add_trace (vm, node, b0, sizeof (*t));
2221 t->sw_if_index = sw_if_index0;
2222 t->next_index = next0;
2225 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2227 /* verify speculative enqueue, maybe switch current next frame */
2228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2229 to_next, n_left_to_next,
2233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2236 vlib_node_increment_counter (vm, stats_node_index,
2237 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2239 return frame->n_vectors;
2244 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2245 .name = "nat44-in2out-fast",
2246 .vector_size = sizeof (u32),
2247 .format_trace = format_snat_in2out_fast_trace,
2248 .type = VLIB_NODE_TYPE_INTERNAL,
2250 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2251 .error_strings = snat_in2out_error_strings,
2253 .runtime_data_bytes = sizeof (snat_runtime_t),
2255 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2257 /* edit / add dispositions here */
2259 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2260 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2261 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2262 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2263 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2269 * fd.io coding-style-patch-verification: ON
2272 * eval: (c-set-style "gnu")