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 (s->in2out.addr.as_u32,
221 s->out2in.addr.as_u32,
225 s->in2out.fib_index);
227 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
228 &s->in2out.addr, s->in2out.port,
229 &s->out2in.addr, s->out2in.port,
232 if (!snat_is_session_static (s))
233 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
236 nat44_delete_session (sm, s, ctx->thread_index);
244 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
247 snat_session_key_t * key0,
248 snat_session_t ** sessionp,
249 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
252 snat_session_t *s = 0;
253 clib_bihash_kv_8_8_t kv0;
254 snat_session_key_t key1;
255 udp_header_t *udp0 = ip4_next_header (ip0);
257 nat_outside_fib_t *outside_fib;
258 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
261 .fp_proto = FIB_PROTOCOL_IP4,
264 .ip4.as_u32 = ip0->dst_address.as_u32,
267 nat44_is_idle_session_ctx_t ctx0;
269 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
271 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
272 nat_ipfix_logging_max_sessions (sm->max_translations);
273 nat_log_notice ("maximum sessions exceeded");
274 return SNAT_IN2OUT_NEXT_DROP;
277 key1.protocol = key0->protocol;
279 /* First try to match static mapping by local address and port */
280 if (snat_static_mapping_match
281 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
283 /* Try to create dynamic translation */
284 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
288 [thread_index].snat_thread_index))
290 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
291 return SNAT_IN2OUT_NEXT_DROP;
296 if (PREDICT_FALSE (identity_nat))
305 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
309 nat_log_warn ("create NAT user failed");
310 return SNAT_IN2OUT_NEXT_DROP;
313 s = nat_session_alloc_or_recycle (sm, u, thread_index);
316 nat44_delete_user_with_no_session (sm, u, thread_index);
317 nat_log_warn ("create NAT session failed");
318 return SNAT_IN2OUT_NEXT_DROP;
322 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
323 user_session_increment (sm, u, is_sm);
326 s->out2in.protocol = key0->protocol;
327 s->out2in.fib_index = sm->outside_fib_index;
328 switch (vec_len (sm->outside_fibs))
331 s->out2in.fib_index = sm->outside_fib_index;
334 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
338 vec_foreach (outside_fib, sm->outside_fibs)
340 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
341 if (FIB_NODE_INDEX_INVALID != fei)
343 if (fib_entry_get_resolving_interface (fei) != ~0)
345 s->out2in.fib_index = outside_fib->fib_index;
353 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
354 s->ext_host_port = udp0->dst_port;
357 /* Add to translation hashes */
359 ctx0.thread_index = thread_index;
360 kv0.key = s->in2out.as_u64;
361 kv0.value = s - sm->per_thread_data[thread_index].sessions;
362 if (clib_bihash_add_or_overwrite_stale_8_8
363 (&sm->per_thread_data[thread_index].in2out, &kv0,
364 nat44_i2o_is_idle_session_cb, &ctx0))
365 nat_log_notice ("in2out key add failed");
367 kv0.key = s->out2in.as_u64;
368 kv0.value = s - sm->per_thread_data[thread_index].sessions;
370 if (clib_bihash_add_or_overwrite_stale_8_8
371 (&sm->per_thread_data[thread_index].out2in, &kv0,
372 nat44_o2i_is_idle_session_cb, &ctx0))
373 nat_log_notice ("out2in key add failed");
376 snat_ipfix_logging_nat44_ses_create (s->in2out.addr.as_u32,
377 s->out2in.addr.as_u32,
380 s->out2in.port, s->in2out.fib_index);
382 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
383 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
384 s->out2in.port, s->in2out.protocol);
390 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
391 snat_session_key_t * p_key0)
393 icmp46_header_t *icmp0;
394 snat_session_key_t key0;
395 icmp_echo_header_t *echo0, *inner_echo0 = 0;
396 ip4_header_t *inner_ip0 = 0;
398 icmp46_header_t *inner_icmp0;
400 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
401 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
403 if (!icmp_is_error_message (icmp0))
405 key0.protocol = SNAT_PROTOCOL_ICMP;
406 key0.addr = ip0->src_address;
407 key0.port = echo0->identifier;
411 inner_ip0 = (ip4_header_t *) (echo0 + 1);
412 l4_header = ip4_next_header (inner_ip0);
413 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
414 key0.addr = inner_ip0->dst_address;
415 switch (key0.protocol)
417 case SNAT_PROTOCOL_ICMP:
418 inner_icmp0 = (icmp46_header_t *) l4_header;
419 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
420 key0.port = inner_echo0->identifier;
422 case SNAT_PROTOCOL_UDP:
423 case SNAT_PROTOCOL_TCP:
424 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
427 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
431 return -1; /* success */
435 * Get address and port values to be used for ICMP packet translation
436 * and create session if needed
438 * @param[in,out] sm NAT main
439 * @param[in,out] node NAT node runtime
440 * @param[in] thread_index thread index
441 * @param[in,out] b0 buffer containing packet to be translated
442 * @param[out] p_proto protocol used for matching
443 * @param[out] p_value address and port after NAT translation
444 * @param[out] p_dont_translate if packet should not be translated
445 * @param d optional parameter
446 * @param e optional parameter
449 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
450 u32 thread_index, vlib_buffer_t * b0,
451 ip4_header_t * ip0, u8 * p_proto,
452 snat_session_key_t * p_value,
453 u8 * p_dont_translate, void *d, void *e)
455 icmp46_header_t *icmp0;
458 snat_session_key_t key0;
459 snat_session_t *s0 = 0;
460 u8 dont_translate = 0;
461 clib_bihash_kv_8_8_t kv0, value0;
465 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
466 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
467 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
469 err = icmp_get_key (ip0, &key0);
472 b0->error = node->errors[err];
473 next0 = SNAT_IN2OUT_NEXT_DROP;
476 key0.fib_index = rx_fib_index0;
478 kv0.key = key0.as_u64;
480 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
483 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
485 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
498 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
499 ip0, SNAT_PROTOCOL_ICMP,
508 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
510 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
511 next0 = SNAT_IN2OUT_NEXT_DROP;
515 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
516 thread_index, vlib_time_now (sm->vlib_main));
518 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
529 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
530 icmp0->type != ICMP4_echo_reply &&
531 !icmp_is_error_message (icmp0)))
533 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
534 next0 = SNAT_IN2OUT_NEXT_DROP;
538 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
543 *p_proto = key0.protocol;
545 *p_value = s0->out2in;
546 *p_dont_translate = dont_translate;
548 *(snat_session_t **) d = s0;
553 * Get address and port values to be used for ICMP packet translation
555 * @param[in] sm NAT main
556 * @param[in,out] node NAT node runtime
557 * @param[in] thread_index thread index
558 * @param[in,out] b0 buffer containing packet to be translated
559 * @param[out] p_proto protocol used for matching
560 * @param[out] p_value address and port after NAT translation
561 * @param[out] p_dont_translate if packet should not be translated
562 * @param d optional parameter
563 * @param e optional parameter
566 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
567 u32 thread_index, vlib_buffer_t * b0,
568 ip4_header_t * ip0, u8 * p_proto,
569 snat_session_key_t * p_value,
570 u8 * p_dont_translate, void *d, void *e)
572 icmp46_header_t *icmp0;
575 snat_session_key_t key0;
576 snat_session_key_t sm0;
577 u8 dont_translate = 0;
582 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
583 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
584 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
586 err = icmp_get_key (ip0, &key0);
589 b0->error = node->errors[err];
590 next0 = SNAT_IN2OUT_NEXT_DROP;
593 key0.fib_index = rx_fib_index0;
595 if (snat_static_mapping_match
596 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
598 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
606 if (icmp_is_error_message (icmp0))
608 next0 = SNAT_IN2OUT_NEXT_DROP;
612 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
613 next0 = SNAT_IN2OUT_NEXT_DROP;
617 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
618 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
619 !icmp_is_error_message (icmp0)))
621 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
622 next0 = SNAT_IN2OUT_NEXT_DROP;
629 *p_proto = key0.protocol;
630 *p_dont_translate = dont_translate;
635 icmp_in2out (snat_main_t * sm,
638 icmp46_header_t * icmp0,
641 vlib_node_runtime_t * node,
642 u32 next0, u32 thread_index, void *d, void *e)
644 snat_session_key_t sm0;
646 icmp_echo_header_t *echo0, *inner_echo0 = 0;
647 ip4_header_t *inner_ip0;
649 icmp46_header_t *inner_icmp0;
651 u32 new_addr0, old_addr0;
652 u16 old_id0, new_id0;
653 u16 old_checksum0, new_checksum0;
658 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
660 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
661 &protocol, &sm0, &dont_translate, d,
665 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
668 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
670 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
672 vlib_buffer_get_current (b0),
673 ntohs (ip0->length) -
674 ip4_header_bytes (ip0), 0);
675 checksum0 = ~ip_csum_fold (sum0);
676 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
678 next0 = SNAT_IN2OUT_NEXT_DROP;
683 old_addr0 = ip0->src_address.as_u32;
684 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
686 sum0 = ip0->checksum;
687 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
688 src_address /* changed member */ );
689 ip0->checksum = ip_csum_fold (sum0);
691 if (icmp0->checksum == 0)
692 icmp0->checksum = 0xffff;
694 if (!icmp_is_error_message (icmp0))
697 if (PREDICT_FALSE (new_id0 != echo0->identifier))
699 old_id0 = echo0->identifier;
701 echo0->identifier = new_id0;
703 sum0 = icmp0->checksum;
704 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
706 icmp0->checksum = ip_csum_fold (sum0);
711 inner_ip0 = (ip4_header_t *) (echo0 + 1);
712 l4_header = ip4_next_header (inner_ip0);
714 if (!ip4_header_checksum_is_valid (inner_ip0))
716 next0 = SNAT_IN2OUT_NEXT_DROP;
720 /* update inner destination IP address */
721 old_addr0 = inner_ip0->dst_address.as_u32;
722 inner_ip0->dst_address = sm0.addr;
723 new_addr0 = inner_ip0->dst_address.as_u32;
724 sum0 = icmp0->checksum;
725 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
726 dst_address /* changed member */ );
727 icmp0->checksum = ip_csum_fold (sum0);
729 /* update inner IP header checksum */
730 old_checksum0 = inner_ip0->checksum;
731 sum0 = inner_ip0->checksum;
732 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
733 dst_address /* changed member */ );
734 inner_ip0->checksum = ip_csum_fold (sum0);
735 new_checksum0 = inner_ip0->checksum;
736 sum0 = icmp0->checksum;
737 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
739 icmp0->checksum = ip_csum_fold (sum0);
743 case SNAT_PROTOCOL_ICMP:
744 inner_icmp0 = (icmp46_header_t *) l4_header;
745 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
747 old_id0 = inner_echo0->identifier;
749 inner_echo0->identifier = new_id0;
751 sum0 = icmp0->checksum;
752 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
754 icmp0->checksum = ip_csum_fold (sum0);
756 case SNAT_PROTOCOL_UDP:
757 case SNAT_PROTOCOL_TCP:
758 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
760 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
762 sum0 = icmp0->checksum;
763 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
765 icmp0->checksum = ip_csum_fold (sum0);
772 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
774 if (sm->deterministic ||
775 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
776 sm->endpoint_dependent))
777 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
785 icmp_in2out_slow_path (snat_main_t * sm,
788 icmp46_header_t * icmp0,
791 vlib_node_runtime_t * node,
793 f64 now, u32 thread_index, snat_session_t ** p_s0)
795 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
796 next0, thread_index, p_s0, 0);
797 snat_session_t *s0 = *p_s0;
798 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
801 nat44_session_update_counters (s0, now,
802 vlib_buffer_length_in_chain
803 (sm->vlib_main, b0));
804 /* Per-user LRU list maintenance */
805 nat44_session_update_lru (sm, s0, thread_index);
811 nat_in2out_sm_unknown_proto (snat_main_t * sm,
813 ip4_header_t * ip, u32 rx_fib_index)
815 clib_bihash_kv_8_8_t kv, value;
816 snat_static_mapping_t *m;
817 snat_session_key_t m_key;
818 u32 old_addr, new_addr;
821 m_key.addr = ip->src_address;
824 m_key.fib_index = rx_fib_index;
825 kv.key = m_key.as_u64;
826 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
829 m = pool_elt_at_index (sm->static_mappings, value.value);
831 old_addr = ip->src_address.as_u32;
832 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
834 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
835 ip->checksum = ip_csum_fold (sum);
839 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
841 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
842 nat_hairpinning_sm_unknown_proto (sm, b, ip);
849 snat_in2out_node_fn_inline (vlib_main_t * vm,
850 vlib_node_runtime_t * node,
851 vlib_frame_t * frame, int is_slow_path,
852 int is_output_feature)
854 u32 n_left_from, *from, *to_next;
855 snat_in2out_next_t next_index;
856 u32 pkts_processed = 0;
857 snat_main_t *sm = &snat_main;
858 f64 now = vlib_time_now (vm);
859 u32 stats_node_index;
860 u32 thread_index = vm->thread_index;
861 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
864 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
865 snat_in2out_node.index;
867 from = vlib_frame_vector_args (frame);
868 n_left_from = frame->n_vectors;
869 next_index = node->cached_next_index;
871 while (n_left_from > 0)
875 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
877 while (n_left_from >= 4 && n_left_to_next >= 2)
880 vlib_buffer_t *b0, *b1;
882 u32 sw_if_index0, sw_if_index1;
883 ip4_header_t *ip0, *ip1;
884 ip_csum_t sum0, sum1;
885 u32 new_addr0, old_addr0, new_addr1, old_addr1;
886 u16 old_port0, new_port0, old_port1, new_port1;
887 udp_header_t *udp0, *udp1;
888 tcp_header_t *tcp0, *tcp1;
889 icmp46_header_t *icmp0, *icmp1;
890 snat_session_key_t key0, key1;
891 u32 rx_fib_index0, rx_fib_index1;
893 snat_session_t *s0 = 0, *s1 = 0;
894 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
895 u32 iph_offset0 = 0, iph_offset1 = 0;
897 /* Prefetch next iteration. */
899 vlib_buffer_t *p2, *p3;
901 p2 = vlib_get_buffer (vm, from[2]);
902 p3 = vlib_get_buffer (vm, from[3]);
904 vlib_prefetch_buffer_header (p2, LOAD);
905 vlib_prefetch_buffer_header (p3, LOAD);
907 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
908 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
911 /* speculatively enqueue b0 and b1 to the current next frame */
912 to_next[0] = bi0 = from[0];
913 to_next[1] = bi1 = from[1];
919 b0 = vlib_get_buffer (vm, bi0);
920 b1 = vlib_get_buffer (vm, bi1);
922 if (is_output_feature)
923 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
925 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
928 udp0 = ip4_next_header (ip0);
929 tcp0 = (tcp_header_t *) udp0;
930 icmp0 = (icmp46_header_t *) udp0;
932 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
933 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
936 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
938 if (PREDICT_FALSE (ip0->ttl == 1))
940 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
941 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
942 ICMP4_time_exceeded_ttl_exceeded_in_transit,
944 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
948 proto0 = ip_proto_to_snat_proto (ip0->protocol);
950 /* Next configured feature, probably ip4-lookup */
953 if (PREDICT_FALSE (proto0 == ~0))
955 if (nat_in2out_sm_unknown_proto
956 (sm, b0, ip0, rx_fib_index0))
958 next0 = SNAT_IN2OUT_NEXT_DROP;
960 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
966 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
968 next0 = icmp_in2out_slow_path
969 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
970 node, next0, now, thread_index, &s0);
977 if (PREDICT_FALSE (proto0 == ~0))
979 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
983 if (ip4_is_fragment (ip0))
985 next0 = SNAT_IN2OUT_NEXT_REASS;
990 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
992 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
997 key0.addr = ip0->src_address;
998 key0.port = udp0->src_port;
999 key0.protocol = proto0;
1000 key0.fib_index = rx_fib_index0;
1002 kv0.key = key0.as_u64;
1005 (clib_bihash_search_8_8
1006 (&sm->per_thread_data[thread_index].in2out, &kv0,
1011 if (is_output_feature)
1013 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1026 (sm, node, sw_if_index0, ip0, proto0,
1027 rx_fib_index0, thread_index)))
1031 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1032 &s0, node, next0, thread_index, now);
1033 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1036 if (PREDICT_FALSE (!s0))
1041 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1047 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1050 b0->flags |= VNET_BUFFER_F_IS_NATED;
1052 old_addr0 = ip0->src_address.as_u32;
1053 ip0->src_address = s0->out2in.addr;
1054 new_addr0 = ip0->src_address.as_u32;
1055 if (!is_output_feature)
1056 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1058 sum0 = ip0->checksum;
1059 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1061 src_address /* changed member */ );
1062 ip0->checksum = ip_csum_fold (sum0);
1064 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1066 old_port0 = tcp0->src_port;
1067 tcp0->src_port = s0->out2in.port;
1068 new_port0 = tcp0->src_port;
1070 sum0 = tcp0->checksum;
1071 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1073 dst_address /* changed member */ );
1074 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1075 ip4_header_t /* cheat */ ,
1076 length /* changed member */ );
1077 mss_clamping (sm, tcp0, &sum0);
1078 tcp0->checksum = ip_csum_fold (sum0);
1083 old_port0 = udp0->src_port;
1084 udp0->src_port = s0->out2in.port;
1090 nat44_session_update_counters (s0, now,
1091 vlib_buffer_length_in_chain (vm,
1093 /* Per-user LRU list maintenance */
1094 nat44_session_update_lru (sm, s0, thread_index);
1097 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1098 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1100 snat_in2out_trace_t *t =
1101 vlib_add_trace (vm, node, b0, sizeof (*t));
1102 t->is_slow_path = is_slow_path;
1103 t->sw_if_index = sw_if_index0;
1104 t->next_index = next0;
1105 t->session_index = ~0;
1108 s0 - sm->per_thread_data[thread_index].sessions;
1111 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1113 if (is_output_feature)
1114 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1116 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1119 udp1 = ip4_next_header (ip1);
1120 tcp1 = (tcp_header_t *) udp1;
1121 icmp1 = (icmp46_header_t *) udp1;
1123 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1124 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1127 if (PREDICT_FALSE (ip1->ttl == 1))
1129 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1130 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1131 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1133 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1137 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1139 /* Next configured feature, probably ip4-lookup */
1142 if (PREDICT_FALSE (proto1 == ~0))
1144 if (nat_in2out_sm_unknown_proto
1145 (sm, b1, ip1, rx_fib_index1))
1147 next1 = SNAT_IN2OUT_NEXT_DROP;
1149 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1155 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1157 next1 = icmp_in2out_slow_path
1158 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1159 next1, now, thread_index, &s1);
1166 if (PREDICT_FALSE (proto1 == ~0))
1168 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1172 if (ip4_is_fragment (ip1))
1174 next1 = SNAT_IN2OUT_NEXT_REASS;
1179 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1181 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1186 key1.addr = ip1->src_address;
1187 key1.port = udp1->src_port;
1188 key1.protocol = proto1;
1189 key1.fib_index = rx_fib_index1;
1191 kv1.key = key1.as_u64;
1194 (clib_bihash_search_8_8
1195 (&sm->per_thread_data[thread_index].in2out, &kv1,
1200 if (is_output_feature)
1202 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1215 (sm, node, sw_if_index1, ip1, proto1,
1216 rx_fib_index1, thread_index)))
1220 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1221 &s1, node, next1, thread_index, now);
1222 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1225 if (PREDICT_FALSE (!s1))
1230 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1236 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1239 b1->flags |= VNET_BUFFER_F_IS_NATED;
1241 old_addr1 = ip1->src_address.as_u32;
1242 ip1->src_address = s1->out2in.addr;
1243 new_addr1 = ip1->src_address.as_u32;
1244 if (!is_output_feature)
1245 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1247 sum1 = ip1->checksum;
1248 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1250 src_address /* changed member */ );
1251 ip1->checksum = ip_csum_fold (sum1);
1253 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1255 old_port1 = tcp1->src_port;
1256 tcp1->src_port = s1->out2in.port;
1257 new_port1 = tcp1->src_port;
1259 sum1 = tcp1->checksum;
1260 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1262 dst_address /* changed member */ );
1263 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1264 ip4_header_t /* cheat */ ,
1265 length /* changed member */ );
1266 mss_clamping (sm, tcp1, &sum1);
1267 tcp1->checksum = ip_csum_fold (sum1);
1272 old_port1 = udp1->src_port;
1273 udp1->src_port = s1->out2in.port;
1279 nat44_session_update_counters (s1, now,
1280 vlib_buffer_length_in_chain (vm,
1282 /* Per-user LRU list maintenance */
1283 nat44_session_update_lru (sm, s1, thread_index);
1286 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1287 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1289 snat_in2out_trace_t *t =
1290 vlib_add_trace (vm, node, b1, sizeof (*t));
1291 t->sw_if_index = sw_if_index1;
1292 t->next_index = next1;
1293 t->session_index = ~0;
1296 s1 - sm->per_thread_data[thread_index].sessions;
1299 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1301 /* verify speculative enqueues, maybe switch current next frame */
1302 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1303 to_next, n_left_to_next,
1304 bi0, bi1, next0, next1);
1307 while (n_left_from > 0 && n_left_to_next > 0)
1315 u32 new_addr0, old_addr0;
1316 u16 old_port0, new_port0;
1319 icmp46_header_t *icmp0;
1320 snat_session_key_t key0;
1323 snat_session_t *s0 = 0;
1324 clib_bihash_kv_8_8_t kv0, value0;
1325 u32 iph_offset0 = 0;
1327 /* speculatively enqueue b0 to the current next frame */
1333 n_left_to_next -= 1;
1335 b0 = vlib_get_buffer (vm, bi0);
1336 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1338 if (is_output_feature)
1339 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1341 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1344 udp0 = ip4_next_header (ip0);
1345 tcp0 = (tcp_header_t *) udp0;
1346 icmp0 = (icmp46_header_t *) udp0;
1348 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1349 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1352 if (PREDICT_FALSE (ip0->ttl == 1))
1354 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1355 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1356 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1358 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1362 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1364 /* Next configured feature, probably ip4-lookup */
1367 if (PREDICT_FALSE (proto0 == ~0))
1369 if (nat_in2out_sm_unknown_proto
1370 (sm, b0, ip0, rx_fib_index0))
1372 next0 = SNAT_IN2OUT_NEXT_DROP;
1374 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1380 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1382 next0 = icmp_in2out_slow_path
1383 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1384 next0, now, thread_index, &s0);
1391 if (PREDICT_FALSE (proto0 == ~0))
1393 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1397 if (ip4_is_fragment (ip0))
1399 next0 = SNAT_IN2OUT_NEXT_REASS;
1404 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1406 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1411 key0.addr = ip0->src_address;
1412 key0.port = udp0->src_port;
1413 key0.protocol = proto0;
1414 key0.fib_index = rx_fib_index0;
1416 kv0.key = key0.as_u64;
1418 if (clib_bihash_search_8_8
1419 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1423 if (is_output_feature)
1425 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1438 (sm, node, sw_if_index0, ip0, proto0,
1439 rx_fib_index0, thread_index)))
1443 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1444 &s0, node, next0, thread_index, now);
1446 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1449 if (PREDICT_FALSE (!s0))
1454 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1460 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1463 b0->flags |= VNET_BUFFER_F_IS_NATED;
1465 old_addr0 = ip0->src_address.as_u32;
1466 ip0->src_address = s0->out2in.addr;
1467 new_addr0 = ip0->src_address.as_u32;
1468 if (!is_output_feature)
1469 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1471 sum0 = ip0->checksum;
1472 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1474 src_address /* changed member */ );
1475 ip0->checksum = ip_csum_fold (sum0);
1477 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1479 old_port0 = tcp0->src_port;
1480 tcp0->src_port = s0->out2in.port;
1481 new_port0 = tcp0->src_port;
1483 sum0 = tcp0->checksum;
1484 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1486 dst_address /* changed member */ );
1487 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1488 ip4_header_t /* cheat */ ,
1489 length /* changed member */ );
1490 mss_clamping (sm, tcp0, &sum0);
1491 tcp0->checksum = ip_csum_fold (sum0);
1496 old_port0 = udp0->src_port;
1497 udp0->src_port = s0->out2in.port;
1503 nat44_session_update_counters (s0, now,
1504 vlib_buffer_length_in_chain (vm,
1506 /* Per-user LRU list maintenance */
1507 nat44_session_update_lru (sm, s0, thread_index);
1510 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1511 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1513 snat_in2out_trace_t *t =
1514 vlib_add_trace (vm, node, b0, sizeof (*t));
1515 t->is_slow_path = is_slow_path;
1516 t->sw_if_index = sw_if_index0;
1517 t->next_index = next0;
1518 t->session_index = ~0;
1521 s0 - sm->per_thread_data[thread_index].sessions;
1524 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1526 /* verify speculative enqueue, maybe switch current next frame */
1527 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1528 to_next, n_left_to_next,
1532 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1535 vlib_node_increment_counter (vm, stats_node_index,
1536 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1538 vlib_node_increment_counter (vm, stats_node_index,
1539 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1540 vlib_node_increment_counter (vm, stats_node_index,
1541 SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1542 vlib_node_increment_counter (vm, stats_node_index,
1543 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1544 vlib_node_increment_counter (vm, stats_node_index,
1545 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1547 vlib_node_increment_counter (vm, stats_node_index,
1548 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1550 return frame->n_vectors;
1554 snat_in2out_fast_path_fn (vlib_main_t * vm,
1555 vlib_node_runtime_t * node, vlib_frame_t * frame)
1557 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1562 VLIB_REGISTER_NODE (snat_in2out_node) = {
1563 .function = snat_in2out_fast_path_fn,
1564 .name = "nat44-in2out",
1565 .vector_size = sizeof (u32),
1566 .format_trace = format_snat_in2out_trace,
1567 .type = VLIB_NODE_TYPE_INTERNAL,
1569 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1570 .error_strings = snat_in2out_error_strings,
1572 .runtime_data_bytes = sizeof (snat_runtime_t),
1574 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1576 /* edit / add dispositions here */
1578 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1579 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1580 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1581 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1582 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1587 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1590 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1591 vlib_node_runtime_t * node,
1592 vlib_frame_t * frame)
1594 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1599 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1600 .function = snat_in2out_output_fast_path_fn,
1601 .name = "nat44-in2out-output",
1602 .vector_size = sizeof (u32),
1603 .format_trace = format_snat_in2out_trace,
1604 .type = VLIB_NODE_TYPE_INTERNAL,
1606 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1607 .error_strings = snat_in2out_error_strings,
1609 .runtime_data_bytes = sizeof (snat_runtime_t),
1611 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1613 /* edit / add dispositions here */
1615 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1616 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1617 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1618 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1619 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1624 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
1625 snat_in2out_output_fast_path_fn);
1628 snat_in2out_slow_path_fn (vlib_main_t * vm,
1629 vlib_node_runtime_t * node, vlib_frame_t * frame)
1631 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1636 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1637 .function = snat_in2out_slow_path_fn,
1638 .name = "nat44-in2out-slowpath",
1639 .vector_size = sizeof (u32),
1640 .format_trace = format_snat_in2out_trace,
1641 .type = VLIB_NODE_TYPE_INTERNAL,
1643 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1644 .error_strings = snat_in2out_error_strings,
1646 .runtime_data_bytes = sizeof (snat_runtime_t),
1648 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1650 /* edit / add dispositions here */
1652 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1653 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1654 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1655 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1656 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1661 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
1662 snat_in2out_slow_path_fn);
1665 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
1666 vlib_node_runtime_t * node,
1667 vlib_frame_t * frame)
1669 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1674 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1675 .function = snat_in2out_output_slow_path_fn,
1676 .name = "nat44-in2out-output-slowpath",
1677 .vector_size = sizeof (u32),
1678 .format_trace = format_snat_in2out_trace,
1679 .type = VLIB_NODE_TYPE_INTERNAL,
1681 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1682 .error_strings = snat_in2out_error_strings,
1684 .runtime_data_bytes = sizeof (snat_runtime_t),
1686 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1688 /* edit / add dispositions here */
1690 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1691 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1692 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1693 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1694 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1699 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
1700 snat_in2out_output_slow_path_fn);
1703 nat44_in2out_reass_node_fn (vlib_main_t * vm,
1704 vlib_node_runtime_t * node, vlib_frame_t * frame)
1706 u32 n_left_from, *from, *to_next;
1707 snat_in2out_next_t next_index;
1708 u32 pkts_processed = 0, cached_fragments = 0;
1709 snat_main_t *sm = &snat_main;
1710 f64 now = vlib_time_now (vm);
1711 u32 thread_index = vm->thread_index;
1712 snat_main_per_thread_data_t *per_thread_data =
1713 &sm->per_thread_data[thread_index];
1714 u32 *fragments_to_drop = 0;
1715 u32 *fragments_to_loopback = 0;
1717 from = vlib_frame_vector_args (frame);
1718 n_left_from = frame->n_vectors;
1719 next_index = node->cached_next_index;
1721 while (n_left_from > 0)
1725 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1727 while (n_left_from > 0 && n_left_to_next > 0)
1729 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1734 nat_reass_ip4_t *reass0;
1737 icmp46_header_t *icmp0;
1738 snat_session_key_t key0;
1739 clib_bihash_kv_8_8_t kv0, value0;
1740 snat_session_t *s0 = 0;
1741 u16 old_port0, new_port0;
1744 /* speculatively enqueue b0 to the current next frame */
1750 n_left_to_next -= 1;
1752 b0 = vlib_get_buffer (vm, bi0);
1753 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1755 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1757 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1760 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1762 next0 = SNAT_IN2OUT_NEXT_DROP;
1763 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1767 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1768 udp0 = ip4_next_header (ip0);
1769 tcp0 = (tcp_header_t *) udp0;
1770 icmp0 = (icmp46_header_t *) udp0;
1771 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1773 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1777 1, &fragments_to_drop);
1779 if (PREDICT_FALSE (!reass0))
1781 next0 = SNAT_IN2OUT_NEXT_DROP;
1782 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1783 nat_log_notice ("maximum reassemblies exceeded");
1787 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1789 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1791 next0 = icmp_in2out_slow_path
1792 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1793 next0, now, thread_index, &s0);
1795 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1798 reass0->sess_index = s0 - per_thread_data->sessions;
1800 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1801 nat_ip4_reass_get_frags (reass0,
1802 &fragments_to_loopback);
1808 key0.addr = ip0->src_address;
1809 key0.port = udp0->src_port;
1810 key0.protocol = proto0;
1811 key0.fib_index = rx_fib_index0;
1812 kv0.key = key0.as_u64;
1814 if (clib_bihash_search_8_8
1815 (&per_thread_data->in2out, &kv0, &value0))
1819 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1823 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1824 &s0, node, next0, thread_index, now);
1826 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1829 if (PREDICT_FALSE (!s0))
1832 reass0->sess_index = s0 - per_thread_data->sessions;
1836 s0 = pool_elt_at_index (per_thread_data->sessions,
1838 reass0->sess_index = value0.value;
1840 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1844 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1846 if (nat_ip4_reass_add_fragment
1847 (reass0, bi0, &fragments_to_drop))
1849 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1851 ("maximum fragments per reassembly exceeded");
1852 next0 = SNAT_IN2OUT_NEXT_DROP;
1858 s0 = pool_elt_at_index (per_thread_data->sessions,
1859 reass0->sess_index);
1862 old_addr0 = ip0->src_address.as_u32;
1863 ip0->src_address = s0->out2in.addr;
1864 new_addr0 = ip0->src_address.as_u32;
1865 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1867 sum0 = ip0->checksum;
1868 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1870 src_address /* changed member */ );
1871 ip0->checksum = ip_csum_fold (sum0);
1873 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1875 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1877 old_port0 = tcp0->src_port;
1878 tcp0->src_port = s0->out2in.port;
1879 new_port0 = tcp0->src_port;
1881 sum0 = tcp0->checksum;
1882 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1884 dst_address /* changed member */ );
1885 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1886 ip4_header_t /* cheat */ ,
1887 length /* changed member */ );
1888 tcp0->checksum = ip_csum_fold (sum0);
1892 old_port0 = udp0->src_port;
1893 udp0->src_port = s0->out2in.port;
1899 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1900 s0->ext_host_port, proto0, 0);
1903 nat44_session_update_counters (s0, now,
1904 vlib_buffer_length_in_chain (vm,
1906 /* Per-user LRU list maintenance */
1907 nat44_session_update_lru (sm, s0, thread_index);
1910 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1911 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1913 nat44_reass_trace_t *t =
1914 vlib_add_trace (vm, node, b0, sizeof (*t));
1915 t->cached = cached0;
1916 t->sw_if_index = sw_if_index0;
1917 t->next_index = next0;
1928 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1930 /* verify speculative enqueue, maybe switch current next frame */
1931 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1932 to_next, n_left_to_next,
1936 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1938 from = vlib_frame_vector_args (frame);
1939 u32 len = vec_len (fragments_to_loopback);
1940 if (len <= VLIB_FRAME_SIZE)
1942 clib_memcpy_fast (from, fragments_to_loopback,
1943 sizeof (u32) * len);
1945 vec_reset_length (fragments_to_loopback);
1949 clib_memcpy_fast (from, fragments_to_loopback +
1950 (len - VLIB_FRAME_SIZE),
1951 sizeof (u32) * VLIB_FRAME_SIZE);
1952 n_left_from = VLIB_FRAME_SIZE;
1953 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1958 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1961 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1962 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1964 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1965 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
1968 nat_send_all_to_node (vm, fragments_to_drop, node,
1969 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1970 SNAT_IN2OUT_NEXT_DROP);
1972 vec_free (fragments_to_drop);
1973 vec_free (fragments_to_loopback);
1974 return frame->n_vectors;
1978 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1979 .function = nat44_in2out_reass_node_fn,
1980 .name = "nat44-in2out-reass",
1981 .vector_size = sizeof (u32),
1982 .format_trace = format_nat44_reass_trace,
1983 .type = VLIB_NODE_TYPE_INTERNAL,
1985 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1986 .error_strings = snat_in2out_error_strings,
1988 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1990 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1991 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1992 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1993 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1994 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1999 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2000 nat44_in2out_reass_node_fn);
2003 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2004 vlib_node_runtime_t * node,
2005 vlib_frame_t * frame)
2007 u32 n_left_from, *from, *to_next;
2008 snat_in2out_next_t next_index;
2009 u32 pkts_processed = 0;
2010 snat_main_t *sm = &snat_main;
2011 u32 stats_node_index;
2013 stats_node_index = snat_in2out_fast_node.index;
2015 from = vlib_frame_vector_args (frame);
2016 n_left_from = frame->n_vectors;
2017 next_index = node->cached_next_index;
2019 while (n_left_from > 0)
2023 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2025 while (n_left_from > 0 && n_left_to_next > 0)
2033 u32 new_addr0, old_addr0;
2034 u16 old_port0, new_port0;
2037 icmp46_header_t *icmp0;
2038 snat_session_key_t key0, sm0;
2042 /* speculatively enqueue b0 to the current next frame */
2048 n_left_to_next -= 1;
2050 b0 = vlib_get_buffer (vm, bi0);
2051 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2053 ip0 = vlib_buffer_get_current (b0);
2054 udp0 = ip4_next_header (ip0);
2055 tcp0 = (tcp_header_t *) udp0;
2056 icmp0 = (icmp46_header_t *) udp0;
2058 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2060 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2062 if (PREDICT_FALSE (ip0->ttl == 1))
2064 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2065 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2066 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2068 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2072 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2074 if (PREDICT_FALSE (proto0 == ~0))
2077 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2079 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2080 rx_fib_index0, node, next0, ~0, 0, 0);
2084 key0.addr = ip0->src_address;
2085 key0.protocol = proto0;
2086 key0.port = udp0->src_port;
2087 key0.fib_index = rx_fib_index0;
2089 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2091 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2092 next0 = SNAT_IN2OUT_NEXT_DROP;
2096 new_addr0 = sm0.addr.as_u32;
2097 new_port0 = sm0.port;
2098 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2099 old_addr0 = ip0->src_address.as_u32;
2100 ip0->src_address.as_u32 = new_addr0;
2102 sum0 = ip0->checksum;
2103 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2105 src_address /* changed member */ );
2106 ip0->checksum = ip_csum_fold (sum0);
2108 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2110 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2112 old_port0 = tcp0->src_port;
2113 tcp0->src_port = new_port0;
2115 sum0 = tcp0->checksum;
2116 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2118 dst_address /* changed member */ );
2119 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2120 ip4_header_t /* cheat */ ,
2121 length /* changed member */ );
2122 mss_clamping (sm, tcp0, &sum0);
2123 tcp0->checksum = ip_csum_fold (sum0);
2127 old_port0 = udp0->src_port;
2128 udp0->src_port = new_port0;
2134 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2136 sum0 = tcp0->checksum;
2137 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2139 dst_address /* changed member */ );
2140 mss_clamping (sm, tcp0, &sum0);
2141 tcp0->checksum = ip_csum_fold (sum0);
2146 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2149 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2150 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2152 snat_in2out_trace_t *t =
2153 vlib_add_trace (vm, node, b0, sizeof (*t));
2154 t->sw_if_index = sw_if_index0;
2155 t->next_index = next0;
2158 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2160 /* verify speculative enqueue, maybe switch current next frame */
2161 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2162 to_next, n_left_to_next,
2166 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2169 vlib_node_increment_counter (vm, stats_node_index,
2170 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2172 return frame->n_vectors;
2177 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2178 .function = snat_in2out_fast_static_map_fn,
2179 .name = "nat44-in2out-fast",
2180 .vector_size = sizeof (u32),
2181 .format_trace = format_snat_in2out_fast_trace,
2182 .type = VLIB_NODE_TYPE_INTERNAL,
2184 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2185 .error_strings = snat_in2out_error_strings,
2187 .runtime_data_bytes = sizeof (snat_runtime_t),
2189 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2191 /* edit / add dispositions here */
2193 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2194 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2195 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2196 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2197 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2202 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node,
2203 snat_in2out_fast_static_map_fn);
2206 * fd.io coding-style-patch-verification: ON
2209 * eval: (c-set-style "gnu")