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>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
43 } snat_in2out_trace_t;
45 /* packet trace format function */
47 format_snat_in2out_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
54 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
56 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
57 t->sw_if_index, t->next_index, t->session_index);
63 format_snat_in2out_fast_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
69 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
70 t->sw_if_index, t->next_index);
75 vlib_node_registration_t snat_in2out_node;
76 vlib_node_registration_t snat_in2out_slowpath_node;
77 vlib_node_registration_t snat_in2out_fast_node;
78 vlib_node_registration_t snat_in2out_output_node;
79 vlib_node_registration_t snat_in2out_output_slowpath_node;
80 vlib_node_registration_t nat44_in2out_reass_node;
82 #define foreach_snat_in2out_error \
83 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
84 _(IN2OUT_PACKETS, "good in2out packets processed") \
85 _(OUT_OF_PORTS, "out of ports") \
86 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
87 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
88 _(NO_TRANSLATION, "no translation") \
89 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
90 _(DROP_FRAGMENT, "drop fragment") \
91 _(MAX_REASS, "maximum reassemblies exceeded") \
92 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
93 _(TCP_PACKETS, "TCP packets") \
94 _(UDP_PACKETS, "UDP packets") \
95 _(ICMP_PACKETS, "ICMP packets") \
96 _(OTHER_PACKETS, "other protocol packets") \
97 _(FRAGMENTS, "fragments") \
98 _(CACHED_FRAGMENTS, "cached fragments") \
99 _(PROCESSED_FRAGMENTS, "processed fragments")
103 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
104 foreach_snat_in2out_error
107 } snat_in2out_error_t;
109 static char *snat_in2out_error_strings[] = {
110 #define _(sym,string) string,
111 foreach_snat_in2out_error
117 SNAT_IN2OUT_NEXT_LOOKUP,
118 SNAT_IN2OUT_NEXT_DROP,
119 SNAT_IN2OUT_NEXT_ICMP_ERROR,
120 SNAT_IN2OUT_NEXT_SLOW_PATH,
121 SNAT_IN2OUT_NEXT_REASS,
123 } snat_in2out_next_t;
126 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
127 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
128 u32 rx_fib_index0, u32 thread_index)
130 udp_header_t *udp0 = ip4_next_header (ip0);
131 snat_session_key_t key0, sm0;
132 clib_bihash_kv_8_8_t kv0, value0;
134 key0.addr = ip0->dst_address;
135 key0.port = udp0->dst_port;
136 key0.protocol = proto0;
137 key0.fib_index = sm->outside_fib_index;
138 kv0.key = key0.as_u64;
140 /* NAT packet aimed at external address if */
141 /* has active sessions */
142 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
145 /* or is static mappings */
146 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
152 if (sm->forwarding_enabled)
155 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
160 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
161 u32 proto0, u16 src_port, u16 dst_port,
162 u32 thread_index, u32 sw_if_index)
164 snat_session_key_t key0;
165 clib_bihash_kv_8_8_t kv0, value0;
169 key0.addr = ip0->src_address;
170 key0.port = src_port;
171 key0.protocol = proto0;
172 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
173 kv0.key = key0.as_u64;
175 if (!clib_bihash_search_8_8
176 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
180 key0.addr = ip0->dst_address;
181 key0.port = dst_port;
182 key0.protocol = proto0;
183 kv0.key = key0.as_u64;
184 if (!clib_bihash_search_8_8
185 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
189 pool_foreach (i, sm->output_feature_interfaces,
191 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
202 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
204 snat_main_t *sm = &snat_main;
205 nat44_is_idle_session_ctx_t *ctx = arg;
207 u64 sess_timeout_time;
208 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
210 clib_bihash_kv_8_8_t s_kv;
212 s = pool_elt_at_index (tsm->sessions, kv->value);
213 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
214 if (ctx->now >= sess_timeout_time)
216 s_kv.key = s->out2in.as_u64;
217 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
218 nat_log_warn ("out2in key del failed");
220 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
221 s->in2out.addr.as_u32,
222 s->out2in.addr.as_u32,
226 s->in2out.fib_index);
228 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
229 &s->in2out.addr, s->in2out.port,
230 &s->out2in.addr, s->out2in.port,
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);
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);
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);
392 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
393 snat_session_key_t * p_key0)
395 icmp46_header_t *icmp0;
396 snat_session_key_t key0;
397 icmp_echo_header_t *echo0, *inner_echo0 = 0;
398 ip4_header_t *inner_ip0 = 0;
400 icmp46_header_t *inner_icmp0;
402 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
403 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
405 if (!icmp_is_error_message (icmp0))
407 key0.protocol = SNAT_PROTOCOL_ICMP;
408 key0.addr = ip0->src_address;
409 key0.port = echo0->identifier;
413 inner_ip0 = (ip4_header_t *) (echo0 + 1);
414 l4_header = ip4_next_header (inner_ip0);
415 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
416 key0.addr = inner_ip0->dst_address;
417 switch (key0.protocol)
419 case SNAT_PROTOCOL_ICMP:
420 inner_icmp0 = (icmp46_header_t *) l4_header;
421 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
422 key0.port = inner_echo0->identifier;
424 case SNAT_PROTOCOL_UDP:
425 case SNAT_PROTOCOL_TCP:
426 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
429 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
433 return -1; /* success */
437 * Get address and port values to be used for ICMP packet translation
438 * and create session if needed
440 * @param[in,out] sm NAT main
441 * @param[in,out] node NAT node runtime
442 * @param[in] thread_index thread index
443 * @param[in,out] b0 buffer containing packet to be translated
444 * @param[out] p_proto protocol used for matching
445 * @param[out] p_value address and port after NAT translation
446 * @param[out] p_dont_translate if packet should not be translated
447 * @param d optional parameter
448 * @param e optional parameter
451 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
452 u32 thread_index, vlib_buffer_t * b0,
453 ip4_header_t * ip0, u8 * p_proto,
454 snat_session_key_t * p_value,
455 u8 * p_dont_translate, void *d, void *e)
457 icmp46_header_t *icmp0;
460 snat_session_key_t key0;
461 snat_session_t *s0 = 0;
462 u8 dont_translate = 0;
463 clib_bihash_kv_8_8_t kv0, value0;
467 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
468 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
469 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
471 err = icmp_get_key (ip0, &key0);
474 b0->error = node->errors[err];
475 next0 = SNAT_IN2OUT_NEXT_DROP;
478 key0.fib_index = rx_fib_index0;
480 kv0.key = key0.as_u64;
482 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
485 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
487 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
500 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
501 ip0, SNAT_PROTOCOL_ICMP,
510 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
512 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
513 next0 = SNAT_IN2OUT_NEXT_DROP;
517 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
518 thread_index, vlib_time_now (sm->vlib_main));
520 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
531 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
532 icmp0->type != ICMP4_echo_reply &&
533 !icmp_is_error_message (icmp0)))
535 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
536 next0 = SNAT_IN2OUT_NEXT_DROP;
540 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
545 *p_proto = key0.protocol;
547 *p_value = s0->out2in;
548 *p_dont_translate = dont_translate;
550 *(snat_session_t **) d = s0;
555 * Get address and port values to be used for ICMP packet translation
557 * @param[in] sm NAT main
558 * @param[in,out] node NAT node runtime
559 * @param[in] thread_index thread index
560 * @param[in,out] b0 buffer containing packet to be translated
561 * @param[out] p_proto protocol used for matching
562 * @param[out] p_value address and port after NAT translation
563 * @param[out] p_dont_translate if packet should not be translated
564 * @param d optional parameter
565 * @param e optional parameter
568 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
569 u32 thread_index, vlib_buffer_t * b0,
570 ip4_header_t * ip0, u8 * p_proto,
571 snat_session_key_t * p_value,
572 u8 * p_dont_translate, void *d, void *e)
574 icmp46_header_t *icmp0;
577 snat_session_key_t key0;
578 snat_session_key_t sm0;
579 u8 dont_translate = 0;
584 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
585 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
586 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
588 err = icmp_get_key (ip0, &key0);
591 b0->error = node->errors[err];
592 next0 = SNAT_IN2OUT_NEXT_DROP;
595 key0.fib_index = rx_fib_index0;
597 if (snat_static_mapping_match
598 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
600 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
608 if (icmp_is_error_message (icmp0))
610 next0 = SNAT_IN2OUT_NEXT_DROP;
614 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
615 next0 = SNAT_IN2OUT_NEXT_DROP;
619 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
620 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
621 !icmp_is_error_message (icmp0)))
623 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
624 next0 = SNAT_IN2OUT_NEXT_DROP;
631 *p_proto = key0.protocol;
632 *p_dont_translate = dont_translate;
637 icmp_in2out (snat_main_t * sm,
640 icmp46_header_t * icmp0,
643 vlib_node_runtime_t * node,
644 u32 next0, u32 thread_index, void *d, void *e)
646 snat_session_key_t sm0;
648 icmp_echo_header_t *echo0, *inner_echo0 = 0;
649 ip4_header_t *inner_ip0;
651 icmp46_header_t *inner_icmp0;
653 u32 new_addr0, old_addr0;
654 u16 old_id0, new_id0;
655 u16 old_checksum0, new_checksum0;
660 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
662 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
663 &protocol, &sm0, &dont_translate, d,
667 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
670 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
672 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
674 vlib_buffer_get_current (b0),
675 ntohs (ip0->length) -
676 ip4_header_bytes (ip0), 0);
677 checksum0 = ~ip_csum_fold (sum0);
678 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
680 next0 = SNAT_IN2OUT_NEXT_DROP;
685 old_addr0 = ip0->src_address.as_u32;
686 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
688 sum0 = ip0->checksum;
689 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
690 src_address /* changed member */ );
691 ip0->checksum = ip_csum_fold (sum0);
693 if (icmp0->checksum == 0)
694 icmp0->checksum = 0xffff;
696 if (!icmp_is_error_message (icmp0))
699 if (PREDICT_FALSE (new_id0 != echo0->identifier))
701 old_id0 = echo0->identifier;
703 echo0->identifier = new_id0;
705 sum0 = icmp0->checksum;
706 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
708 icmp0->checksum = ip_csum_fold (sum0);
713 inner_ip0 = (ip4_header_t *) (echo0 + 1);
714 l4_header = ip4_next_header (inner_ip0);
716 if (!ip4_header_checksum_is_valid (inner_ip0))
718 next0 = SNAT_IN2OUT_NEXT_DROP;
722 /* update inner destination IP address */
723 old_addr0 = inner_ip0->dst_address.as_u32;
724 inner_ip0->dst_address = sm0.addr;
725 new_addr0 = inner_ip0->dst_address.as_u32;
726 sum0 = icmp0->checksum;
727 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
728 dst_address /* changed member */ );
729 icmp0->checksum = ip_csum_fold (sum0);
731 /* update inner IP header checksum */
732 old_checksum0 = inner_ip0->checksum;
733 sum0 = inner_ip0->checksum;
734 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
735 dst_address /* changed member */ );
736 inner_ip0->checksum = ip_csum_fold (sum0);
737 new_checksum0 = inner_ip0->checksum;
738 sum0 = icmp0->checksum;
739 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
741 icmp0->checksum = ip_csum_fold (sum0);
745 case SNAT_PROTOCOL_ICMP:
746 inner_icmp0 = (icmp46_header_t *) l4_header;
747 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
749 old_id0 = inner_echo0->identifier;
751 inner_echo0->identifier = new_id0;
753 sum0 = icmp0->checksum;
754 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
756 icmp0->checksum = ip_csum_fold (sum0);
758 case SNAT_PROTOCOL_UDP:
759 case SNAT_PROTOCOL_TCP:
760 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
762 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
764 sum0 = icmp0->checksum;
765 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
767 icmp0->checksum = ip_csum_fold (sum0);
774 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
776 if (sm->deterministic ||
777 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
778 sm->endpoint_dependent))
779 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
787 icmp_in2out_slow_path (snat_main_t * sm,
790 icmp46_header_t * icmp0,
793 vlib_node_runtime_t * node,
795 f64 now, u32 thread_index, snat_session_t ** p_s0)
797 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
798 next0, thread_index, p_s0, 0);
799 snat_session_t *s0 = *p_s0;
800 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
803 nat44_session_update_counters (s0, now,
804 vlib_buffer_length_in_chain
805 (sm->vlib_main, b0));
806 /* Per-user LRU list maintenance */
807 nat44_session_update_lru (sm, s0, thread_index);
813 nat_in2out_sm_unknown_proto (snat_main_t * sm,
815 ip4_header_t * ip, u32 rx_fib_index)
817 clib_bihash_kv_8_8_t kv, value;
818 snat_static_mapping_t *m;
819 snat_session_key_t m_key;
820 u32 old_addr, new_addr;
823 m_key.addr = ip->src_address;
826 m_key.fib_index = rx_fib_index;
827 kv.key = m_key.as_u64;
828 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
831 m = pool_elt_at_index (sm->static_mappings, value.value);
833 old_addr = ip->src_address.as_u32;
834 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
836 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
837 ip->checksum = ip_csum_fold (sum);
841 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
843 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
844 nat_hairpinning_sm_unknown_proto (sm, b, ip);
851 snat_in2out_node_fn_inline (vlib_main_t * vm,
852 vlib_node_runtime_t * node,
853 vlib_frame_t * frame, int is_slow_path,
854 int is_output_feature)
856 u32 n_left_from, *from, *to_next;
857 snat_in2out_next_t next_index;
858 u32 pkts_processed = 0;
859 snat_main_t *sm = &snat_main;
860 f64 now = vlib_time_now (vm);
861 u32 stats_node_index;
862 u32 thread_index = vm->thread_index;
863 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
866 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
867 snat_in2out_node.index;
869 from = vlib_frame_vector_args (frame);
870 n_left_from = frame->n_vectors;
871 next_index = node->cached_next_index;
873 while (n_left_from > 0)
877 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
879 while (n_left_from >= 4 && n_left_to_next >= 2)
882 vlib_buffer_t *b0, *b1;
884 u32 sw_if_index0, sw_if_index1;
885 ip4_header_t *ip0, *ip1;
886 ip_csum_t sum0, sum1;
887 u32 new_addr0, old_addr0, new_addr1, old_addr1;
888 u16 old_port0, new_port0, old_port1, new_port1;
889 udp_header_t *udp0, *udp1;
890 tcp_header_t *tcp0, *tcp1;
891 icmp46_header_t *icmp0, *icmp1;
892 snat_session_key_t key0, key1;
893 u32 rx_fib_index0, rx_fib_index1;
895 snat_session_t *s0 = 0, *s1 = 0;
896 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
897 u32 iph_offset0 = 0, iph_offset1 = 0;
899 /* Prefetch next iteration. */
901 vlib_buffer_t *p2, *p3;
903 p2 = vlib_get_buffer (vm, from[2]);
904 p3 = vlib_get_buffer (vm, from[3]);
906 vlib_prefetch_buffer_header (p2, LOAD);
907 vlib_prefetch_buffer_header (p3, LOAD);
909 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
910 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
913 /* speculatively enqueue b0 and b1 to the current next frame */
914 to_next[0] = bi0 = from[0];
915 to_next[1] = bi1 = from[1];
921 b0 = vlib_get_buffer (vm, bi0);
922 b1 = vlib_get_buffer (vm, bi1);
924 if (is_output_feature)
925 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
927 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
930 udp0 = ip4_next_header (ip0);
931 tcp0 = (tcp_header_t *) udp0;
932 icmp0 = (icmp46_header_t *) udp0;
934 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
935 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
938 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
940 if (PREDICT_FALSE (ip0->ttl == 1))
942 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
943 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
944 ICMP4_time_exceeded_ttl_exceeded_in_transit,
946 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
950 proto0 = ip_proto_to_snat_proto (ip0->protocol);
952 /* Next configured feature, probably ip4-lookup */
955 if (PREDICT_FALSE (proto0 == ~0))
957 if (nat_in2out_sm_unknown_proto
958 (sm, b0, ip0, rx_fib_index0))
960 next0 = SNAT_IN2OUT_NEXT_DROP;
962 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
968 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
970 next0 = icmp_in2out_slow_path
971 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
972 node, next0, now, thread_index, &s0);
979 if (PREDICT_FALSE (proto0 == ~0))
981 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
985 if (ip4_is_fragment (ip0))
987 next0 = SNAT_IN2OUT_NEXT_REASS;
992 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
994 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
999 key0.addr = ip0->src_address;
1000 key0.port = udp0->src_port;
1001 key0.protocol = proto0;
1002 key0.fib_index = rx_fib_index0;
1004 kv0.key = key0.as_u64;
1007 (clib_bihash_search_8_8
1008 (&sm->per_thread_data[thread_index].in2out, &kv0,
1013 if (is_output_feature)
1015 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1028 (sm, node, sw_if_index0, ip0, proto0,
1029 rx_fib_index0, thread_index)))
1033 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1034 &s0, node, next0, thread_index, now);
1035 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1038 if (PREDICT_FALSE (!s0))
1043 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1049 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1052 b0->flags |= VNET_BUFFER_F_IS_NATED;
1054 old_addr0 = ip0->src_address.as_u32;
1055 ip0->src_address = s0->out2in.addr;
1056 new_addr0 = ip0->src_address.as_u32;
1057 if (!is_output_feature)
1058 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1060 sum0 = ip0->checksum;
1061 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1063 src_address /* changed member */ );
1064 ip0->checksum = ip_csum_fold (sum0);
1066 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1068 old_port0 = tcp0->src_port;
1069 tcp0->src_port = s0->out2in.port;
1070 new_port0 = tcp0->src_port;
1072 sum0 = tcp0->checksum;
1073 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1075 dst_address /* changed member */ );
1076 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1077 ip4_header_t /* cheat */ ,
1078 length /* changed member */ );
1079 mss_clamping (sm, tcp0, &sum0);
1080 tcp0->checksum = ip_csum_fold (sum0);
1085 old_port0 = udp0->src_port;
1086 udp0->src_port = s0->out2in.port;
1092 nat44_session_update_counters (s0, now,
1093 vlib_buffer_length_in_chain (vm,
1095 /* Per-user LRU list maintenance */
1096 nat44_session_update_lru (sm, s0, thread_index);
1099 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1100 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1102 snat_in2out_trace_t *t =
1103 vlib_add_trace (vm, node, b0, sizeof (*t));
1104 t->is_slow_path = is_slow_path;
1105 t->sw_if_index = sw_if_index0;
1106 t->next_index = next0;
1107 t->session_index = ~0;
1110 s0 - sm->per_thread_data[thread_index].sessions;
1113 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1115 if (is_output_feature)
1116 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1118 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1121 udp1 = ip4_next_header (ip1);
1122 tcp1 = (tcp_header_t *) udp1;
1123 icmp1 = (icmp46_header_t *) udp1;
1125 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1126 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1129 if (PREDICT_FALSE (ip1->ttl == 1))
1131 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1132 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1133 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1135 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1139 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1141 /* Next configured feature, probably ip4-lookup */
1144 if (PREDICT_FALSE (proto1 == ~0))
1146 if (nat_in2out_sm_unknown_proto
1147 (sm, b1, ip1, rx_fib_index1))
1149 next1 = SNAT_IN2OUT_NEXT_DROP;
1151 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1157 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1159 next1 = icmp_in2out_slow_path
1160 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1161 next1, now, thread_index, &s1);
1168 if (PREDICT_FALSE (proto1 == ~0))
1170 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1174 if (ip4_is_fragment (ip1))
1176 next1 = SNAT_IN2OUT_NEXT_REASS;
1181 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1183 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1188 key1.addr = ip1->src_address;
1189 key1.port = udp1->src_port;
1190 key1.protocol = proto1;
1191 key1.fib_index = rx_fib_index1;
1193 kv1.key = key1.as_u64;
1196 (clib_bihash_search_8_8
1197 (&sm->per_thread_data[thread_index].in2out, &kv1,
1202 if (is_output_feature)
1204 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1217 (sm, node, sw_if_index1, ip1, proto1,
1218 rx_fib_index1, thread_index)))
1222 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1223 &s1, node, next1, thread_index, now);
1224 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1227 if (PREDICT_FALSE (!s1))
1232 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1238 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1241 b1->flags |= VNET_BUFFER_F_IS_NATED;
1243 old_addr1 = ip1->src_address.as_u32;
1244 ip1->src_address = s1->out2in.addr;
1245 new_addr1 = ip1->src_address.as_u32;
1246 if (!is_output_feature)
1247 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1249 sum1 = ip1->checksum;
1250 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1252 src_address /* changed member */ );
1253 ip1->checksum = ip_csum_fold (sum1);
1255 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1257 old_port1 = tcp1->src_port;
1258 tcp1->src_port = s1->out2in.port;
1259 new_port1 = tcp1->src_port;
1261 sum1 = tcp1->checksum;
1262 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1264 dst_address /* changed member */ );
1265 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1266 ip4_header_t /* cheat */ ,
1267 length /* changed member */ );
1268 mss_clamping (sm, tcp1, &sum1);
1269 tcp1->checksum = ip_csum_fold (sum1);
1274 old_port1 = udp1->src_port;
1275 udp1->src_port = s1->out2in.port;
1281 nat44_session_update_counters (s1, now,
1282 vlib_buffer_length_in_chain (vm,
1284 /* Per-user LRU list maintenance */
1285 nat44_session_update_lru (sm, s1, thread_index);
1288 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1289 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1291 snat_in2out_trace_t *t =
1292 vlib_add_trace (vm, node, b1, sizeof (*t));
1293 t->sw_if_index = sw_if_index1;
1294 t->next_index = next1;
1295 t->session_index = ~0;
1298 s1 - sm->per_thread_data[thread_index].sessions;
1301 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1303 /* verify speculative enqueues, maybe switch current next frame */
1304 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1305 to_next, n_left_to_next,
1306 bi0, bi1, next0, next1);
1309 while (n_left_from > 0 && n_left_to_next > 0)
1317 u32 new_addr0, old_addr0;
1318 u16 old_port0, new_port0;
1321 icmp46_header_t *icmp0;
1322 snat_session_key_t key0;
1325 snat_session_t *s0 = 0;
1326 clib_bihash_kv_8_8_t kv0, value0;
1327 u32 iph_offset0 = 0;
1329 /* speculatively enqueue b0 to the current next frame */
1335 n_left_to_next -= 1;
1337 b0 = vlib_get_buffer (vm, bi0);
1338 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1340 if (is_output_feature)
1341 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1343 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1346 udp0 = ip4_next_header (ip0);
1347 tcp0 = (tcp_header_t *) udp0;
1348 icmp0 = (icmp46_header_t *) udp0;
1350 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1351 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1354 if (PREDICT_FALSE (ip0->ttl == 1))
1356 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1357 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1358 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1360 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1364 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1366 /* Next configured feature, probably ip4-lookup */
1369 if (PREDICT_FALSE (proto0 == ~0))
1371 if (nat_in2out_sm_unknown_proto
1372 (sm, b0, ip0, rx_fib_index0))
1374 next0 = SNAT_IN2OUT_NEXT_DROP;
1376 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1382 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1384 next0 = icmp_in2out_slow_path
1385 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1386 next0, now, thread_index, &s0);
1393 if (PREDICT_FALSE (proto0 == ~0))
1395 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1399 if (ip4_is_fragment (ip0))
1401 next0 = SNAT_IN2OUT_NEXT_REASS;
1406 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1408 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1413 key0.addr = ip0->src_address;
1414 key0.port = udp0->src_port;
1415 key0.protocol = proto0;
1416 key0.fib_index = rx_fib_index0;
1418 kv0.key = key0.as_u64;
1420 if (clib_bihash_search_8_8
1421 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1425 if (is_output_feature)
1427 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1440 (sm, node, sw_if_index0, ip0, proto0,
1441 rx_fib_index0, thread_index)))
1445 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1446 &s0, node, next0, thread_index, now);
1448 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1451 if (PREDICT_FALSE (!s0))
1456 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1462 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1465 b0->flags |= VNET_BUFFER_F_IS_NATED;
1467 old_addr0 = ip0->src_address.as_u32;
1468 ip0->src_address = s0->out2in.addr;
1469 new_addr0 = ip0->src_address.as_u32;
1470 if (!is_output_feature)
1471 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1473 sum0 = ip0->checksum;
1474 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1476 src_address /* changed member */ );
1477 ip0->checksum = ip_csum_fold (sum0);
1479 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1481 old_port0 = tcp0->src_port;
1482 tcp0->src_port = s0->out2in.port;
1483 new_port0 = tcp0->src_port;
1485 sum0 = tcp0->checksum;
1486 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1488 dst_address /* changed member */ );
1489 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1490 ip4_header_t /* cheat */ ,
1491 length /* changed member */ );
1492 mss_clamping (sm, tcp0, &sum0);
1493 tcp0->checksum = ip_csum_fold (sum0);
1498 old_port0 = udp0->src_port;
1499 udp0->src_port = s0->out2in.port;
1505 nat44_session_update_counters (s0, now,
1506 vlib_buffer_length_in_chain (vm,
1508 /* Per-user LRU list maintenance */
1509 nat44_session_update_lru (sm, s0, thread_index);
1512 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1513 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1515 snat_in2out_trace_t *t =
1516 vlib_add_trace (vm, node, b0, sizeof (*t));
1517 t->is_slow_path = is_slow_path;
1518 t->sw_if_index = sw_if_index0;
1519 t->next_index = next0;
1520 t->session_index = ~0;
1523 s0 - sm->per_thread_data[thread_index].sessions;
1526 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1528 /* verify speculative enqueue, maybe switch current next frame */
1529 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1530 to_next, n_left_to_next,
1534 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1537 vlib_node_increment_counter (vm, stats_node_index,
1538 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1540 vlib_node_increment_counter (vm, stats_node_index,
1541 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1542 vlib_node_increment_counter (vm, stats_node_index,
1543 SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1544 vlib_node_increment_counter (vm, stats_node_index,
1545 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1546 vlib_node_increment_counter (vm, stats_node_index,
1547 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1549 vlib_node_increment_counter (vm, stats_node_index,
1550 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1552 return frame->n_vectors;
1556 snat_in2out_fast_path_fn (vlib_main_t * vm,
1557 vlib_node_runtime_t * node, vlib_frame_t * frame)
1559 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1564 VLIB_REGISTER_NODE (snat_in2out_node) = {
1565 .function = snat_in2out_fast_path_fn,
1566 .name = "nat44-in2out",
1567 .vector_size = sizeof (u32),
1568 .format_trace = format_snat_in2out_trace,
1569 .type = VLIB_NODE_TYPE_INTERNAL,
1571 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1572 .error_strings = snat_in2out_error_strings,
1574 .runtime_data_bytes = sizeof (snat_runtime_t),
1576 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1578 /* edit / add dispositions here */
1580 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1581 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1582 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1583 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1584 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1589 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1592 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1593 vlib_node_runtime_t * node,
1594 vlib_frame_t * frame)
1596 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1601 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1602 .function = snat_in2out_output_fast_path_fn,
1603 .name = "nat44-in2out-output",
1604 .vector_size = sizeof (u32),
1605 .format_trace = format_snat_in2out_trace,
1606 .type = VLIB_NODE_TYPE_INTERNAL,
1608 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1609 .error_strings = snat_in2out_error_strings,
1611 .runtime_data_bytes = sizeof (snat_runtime_t),
1613 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1615 /* edit / add dispositions here */
1617 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1618 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1619 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1620 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1621 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1626 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
1627 snat_in2out_output_fast_path_fn);
1630 snat_in2out_slow_path_fn (vlib_main_t * vm,
1631 vlib_node_runtime_t * node, vlib_frame_t * frame)
1633 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1638 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1639 .function = snat_in2out_slow_path_fn,
1640 .name = "nat44-in2out-slowpath",
1641 .vector_size = sizeof (u32),
1642 .format_trace = format_snat_in2out_trace,
1643 .type = VLIB_NODE_TYPE_INTERNAL,
1645 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1646 .error_strings = snat_in2out_error_strings,
1648 .runtime_data_bytes = sizeof (snat_runtime_t),
1650 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1652 /* edit / add dispositions here */
1654 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1655 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1656 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1657 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1658 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1663 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
1664 snat_in2out_slow_path_fn);
1667 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
1668 vlib_node_runtime_t * node,
1669 vlib_frame_t * frame)
1671 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1676 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1677 .function = snat_in2out_output_slow_path_fn,
1678 .name = "nat44-in2out-output-slowpath",
1679 .vector_size = sizeof (u32),
1680 .format_trace = format_snat_in2out_trace,
1681 .type = VLIB_NODE_TYPE_INTERNAL,
1683 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1684 .error_strings = snat_in2out_error_strings,
1686 .runtime_data_bytes = sizeof (snat_runtime_t),
1688 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1690 /* edit / add dispositions here */
1692 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1693 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1694 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1695 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1696 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1701 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
1702 snat_in2out_output_slow_path_fn);
1705 nat44_in2out_reass_node_fn (vlib_main_t * vm,
1706 vlib_node_runtime_t * node, vlib_frame_t * frame)
1708 u32 n_left_from, *from, *to_next;
1709 snat_in2out_next_t next_index;
1710 u32 pkts_processed = 0, cached_fragments = 0;
1711 snat_main_t *sm = &snat_main;
1712 f64 now = vlib_time_now (vm);
1713 u32 thread_index = vm->thread_index;
1714 snat_main_per_thread_data_t *per_thread_data =
1715 &sm->per_thread_data[thread_index];
1716 u32 *fragments_to_drop = 0;
1717 u32 *fragments_to_loopback = 0;
1719 from = vlib_frame_vector_args (frame);
1720 n_left_from = frame->n_vectors;
1721 next_index = node->cached_next_index;
1723 while (n_left_from > 0)
1727 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1729 while (n_left_from > 0 && n_left_to_next > 0)
1731 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1736 nat_reass_ip4_t *reass0;
1739 icmp46_header_t *icmp0;
1740 snat_session_key_t key0;
1741 clib_bihash_kv_8_8_t kv0, value0;
1742 snat_session_t *s0 = 0;
1743 u16 old_port0, new_port0;
1746 /* speculatively enqueue b0 to the current next frame */
1752 n_left_to_next -= 1;
1754 b0 = vlib_get_buffer (vm, bi0);
1755 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1757 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1759 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1762 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1764 next0 = SNAT_IN2OUT_NEXT_DROP;
1765 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1769 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1770 udp0 = ip4_next_header (ip0);
1771 tcp0 = (tcp_header_t *) udp0;
1772 icmp0 = (icmp46_header_t *) udp0;
1773 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1775 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1779 1, &fragments_to_drop);
1781 if (PREDICT_FALSE (!reass0))
1783 next0 = SNAT_IN2OUT_NEXT_DROP;
1784 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1785 nat_log_notice ("maximum reassemblies exceeded");
1789 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1791 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1793 next0 = icmp_in2out_slow_path
1794 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1795 next0, now, thread_index, &s0);
1797 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1800 reass0->sess_index = s0 - per_thread_data->sessions;
1802 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1803 nat_ip4_reass_get_frags (reass0,
1804 &fragments_to_loopback);
1810 key0.addr = ip0->src_address;
1811 key0.port = udp0->src_port;
1812 key0.protocol = proto0;
1813 key0.fib_index = rx_fib_index0;
1814 kv0.key = key0.as_u64;
1816 if (clib_bihash_search_8_8
1817 (&per_thread_data->in2out, &kv0, &value0))
1821 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1825 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1826 &s0, node, next0, thread_index, now);
1828 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1831 if (PREDICT_FALSE (!s0))
1834 reass0->sess_index = s0 - per_thread_data->sessions;
1838 s0 = pool_elt_at_index (per_thread_data->sessions,
1840 reass0->sess_index = value0.value;
1842 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1846 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1848 if (nat_ip4_reass_add_fragment
1849 (thread_index, reass0, bi0, &fragments_to_drop))
1851 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1853 ("maximum fragments per reassembly exceeded");
1854 next0 = SNAT_IN2OUT_NEXT_DROP;
1860 s0 = pool_elt_at_index (per_thread_data->sessions,
1861 reass0->sess_index);
1864 old_addr0 = ip0->src_address.as_u32;
1865 ip0->src_address = s0->out2in.addr;
1866 new_addr0 = ip0->src_address.as_u32;
1867 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1869 sum0 = ip0->checksum;
1870 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1872 src_address /* changed member */ );
1873 ip0->checksum = ip_csum_fold (sum0);
1875 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1877 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1879 old_port0 = tcp0->src_port;
1880 tcp0->src_port = s0->out2in.port;
1881 new_port0 = tcp0->src_port;
1883 sum0 = tcp0->checksum;
1884 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1886 dst_address /* changed member */ );
1887 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1888 ip4_header_t /* cheat */ ,
1889 length /* changed member */ );
1890 tcp0->checksum = ip_csum_fold (sum0);
1894 old_port0 = udp0->src_port;
1895 udp0->src_port = s0->out2in.port;
1901 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1902 s0->ext_host_port, proto0, 0);
1905 nat44_session_update_counters (s0, now,
1906 vlib_buffer_length_in_chain (vm,
1908 /* Per-user LRU list maintenance */
1909 nat44_session_update_lru (sm, s0, thread_index);
1912 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1913 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1915 nat44_reass_trace_t *t =
1916 vlib_add_trace (vm, node, b0, sizeof (*t));
1917 t->cached = cached0;
1918 t->sw_if_index = sw_if_index0;
1919 t->next_index = next0;
1930 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1932 /* verify speculative enqueue, maybe switch current next frame */
1933 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1934 to_next, n_left_to_next,
1938 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1940 from = vlib_frame_vector_args (frame);
1941 u32 len = vec_len (fragments_to_loopback);
1942 if (len <= VLIB_FRAME_SIZE)
1944 clib_memcpy_fast (from, fragments_to_loopback,
1945 sizeof (u32) * len);
1947 vec_reset_length (fragments_to_loopback);
1951 clib_memcpy_fast (from, fragments_to_loopback +
1952 (len - VLIB_FRAME_SIZE),
1953 sizeof (u32) * VLIB_FRAME_SIZE);
1954 n_left_from = VLIB_FRAME_SIZE;
1955 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1960 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1963 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1964 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1966 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1967 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
1970 nat_send_all_to_node (vm, fragments_to_drop, node,
1971 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1972 SNAT_IN2OUT_NEXT_DROP);
1974 vec_free (fragments_to_drop);
1975 vec_free (fragments_to_loopback);
1976 return frame->n_vectors;
1980 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1981 .function = nat44_in2out_reass_node_fn,
1982 .name = "nat44-in2out-reass",
1983 .vector_size = sizeof (u32),
1984 .format_trace = format_nat44_reass_trace,
1985 .type = VLIB_NODE_TYPE_INTERNAL,
1987 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1988 .error_strings = snat_in2out_error_strings,
1990 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1992 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1993 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1994 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1995 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1996 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2001 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2002 nat44_in2out_reass_node_fn);
2005 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2006 vlib_node_runtime_t * node,
2007 vlib_frame_t * frame)
2009 u32 n_left_from, *from, *to_next;
2010 snat_in2out_next_t next_index;
2011 u32 pkts_processed = 0;
2012 snat_main_t *sm = &snat_main;
2013 u32 stats_node_index;
2015 stats_node_index = snat_in2out_fast_node.index;
2017 from = vlib_frame_vector_args (frame);
2018 n_left_from = frame->n_vectors;
2019 next_index = node->cached_next_index;
2021 while (n_left_from > 0)
2025 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2027 while (n_left_from > 0 && n_left_to_next > 0)
2035 u32 new_addr0, old_addr0;
2036 u16 old_port0, new_port0;
2039 icmp46_header_t *icmp0;
2040 snat_session_key_t key0, sm0;
2044 /* speculatively enqueue b0 to the current next frame */
2050 n_left_to_next -= 1;
2052 b0 = vlib_get_buffer (vm, bi0);
2053 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2055 ip0 = vlib_buffer_get_current (b0);
2056 udp0 = ip4_next_header (ip0);
2057 tcp0 = (tcp_header_t *) udp0;
2058 icmp0 = (icmp46_header_t *) udp0;
2060 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2062 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2064 if (PREDICT_FALSE (ip0->ttl == 1))
2066 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2067 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2068 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2070 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2074 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2076 if (PREDICT_FALSE (proto0 == ~0))
2079 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2081 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2082 rx_fib_index0, node, next0, ~0, 0, 0);
2086 key0.addr = ip0->src_address;
2087 key0.protocol = proto0;
2088 key0.port = udp0->src_port;
2089 key0.fib_index = rx_fib_index0;
2091 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2093 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2094 next0 = SNAT_IN2OUT_NEXT_DROP;
2098 new_addr0 = sm0.addr.as_u32;
2099 new_port0 = sm0.port;
2100 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2101 old_addr0 = ip0->src_address.as_u32;
2102 ip0->src_address.as_u32 = new_addr0;
2104 sum0 = ip0->checksum;
2105 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2107 src_address /* changed member */ );
2108 ip0->checksum = ip_csum_fold (sum0);
2110 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2112 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2114 old_port0 = tcp0->src_port;
2115 tcp0->src_port = new_port0;
2117 sum0 = tcp0->checksum;
2118 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2120 dst_address /* changed member */ );
2121 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2122 ip4_header_t /* cheat */ ,
2123 length /* changed member */ );
2124 mss_clamping (sm, tcp0, &sum0);
2125 tcp0->checksum = ip_csum_fold (sum0);
2129 old_port0 = udp0->src_port;
2130 udp0->src_port = new_port0;
2136 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2138 sum0 = tcp0->checksum;
2139 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2141 dst_address /* changed member */ );
2142 mss_clamping (sm, tcp0, &sum0);
2143 tcp0->checksum = ip_csum_fold (sum0);
2148 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2151 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2152 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2154 snat_in2out_trace_t *t =
2155 vlib_add_trace (vm, node, b0, sizeof (*t));
2156 t->sw_if_index = sw_if_index0;
2157 t->next_index = next0;
2160 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2162 /* verify speculative enqueue, maybe switch current next frame */
2163 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2164 to_next, n_left_to_next,
2168 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2171 vlib_node_increment_counter (vm, stats_node_index,
2172 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2174 return frame->n_vectors;
2179 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2180 .function = snat_in2out_fast_static_map_fn,
2181 .name = "nat44-in2out-fast",
2182 .vector_size = sizeof (u32),
2183 .format_trace = format_snat_in2out_fast_trace,
2184 .type = VLIB_NODE_TYPE_INTERNAL,
2186 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2187 .error_strings = snat_in2out_error_strings,
2189 .runtime_data_bytes = sizeof (snat_runtime_t),
2191 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2193 /* edit / add dispositions here */
2195 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2196 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2197 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2198 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2199 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2204 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node,
2205 snat_in2out_fast_static_map_fn);
2208 * fd.io coding-style-patch-verification: ON
2211 * eval: (c-set-style "gnu")