2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief NAT44 inside to outside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/udp/udp.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
38 #include <nat/lib/nat_inlines.h>
47 } snat_in2out_trace_t;
49 /* packet trace format function */
51 format_snat_in2out_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
58 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
60 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
61 t->sw_if_index, t->next_index, t->session_index);
62 if (t->is_hairpinning)
64 s = format (s, ", with-hairpinning");
71 format_snat_in2out_fast_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
77 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
78 t->sw_if_index, t->next_index);
83 #define foreach_snat_in2out_error \
84 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
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 _(CANNOT_CREATE_USER, "cannot create NAT user")
94 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
95 foreach_snat_in2out_error
98 } snat_in2out_error_t;
100 static char *snat_in2out_error_strings[] = {
101 #define _(sym,string) string,
102 foreach_snat_in2out_error
108 SNAT_IN2OUT_NEXT_LOOKUP,
109 SNAT_IN2OUT_NEXT_DROP,
110 SNAT_IN2OUT_NEXT_ICMP_ERROR,
111 SNAT_IN2OUT_NEXT_SLOW_PATH,
113 } snat_in2out_next_t;
116 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
117 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
118 u32 rx_fib_index0, u32 thread_index)
120 udp_header_t *udp0 = ip4_next_header (ip0);
121 clib_bihash_kv_8_8_t kv0, value0;
123 init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
126 /* NAT packet aimed at external address if */
127 /* has active sessions */
128 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
131 /* or is static mappings */
132 ip4_address_t placeholder_addr;
133 u16 placeholder_port;
134 u32 placeholder_fib_index;
135 if (!snat_static_mapping_match
136 (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
137 proto0, &placeholder_addr, &placeholder_port,
138 &placeholder_fib_index, 1, 0, 0, 0, 0, 0))
144 if (sm->forwarding_enabled)
147 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
152 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
153 u32 proto0, u16 src_port, u16 dst_port,
154 u32 thread_index, u32 sw_if_index)
156 clib_bihash_kv_8_8_t kv0, value0;
160 init_nat_k (&kv0, ip0->src_address, src_port,
161 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
163 if (!clib_bihash_search_8_8
164 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
168 init_nat_k (&kv0, ip0->dst_address, dst_port,
169 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
170 if (!clib_bihash_search_8_8
171 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
175 pool_foreach (i, sm->output_feature_interfaces,
177 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
187 #ifndef CLIB_MARCH_VARIANT
189 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
191 snat_main_t *sm = &snat_main;
192 nat44_is_idle_session_ctx_t *ctx = arg;
194 u64 sess_timeout_time;
195 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
197 clib_bihash_kv_8_8_t s_kv;
199 s = pool_elt_at_index (tsm->sessions, kv->value);
200 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
201 if (ctx->now >= sess_timeout_time)
203 init_nat_o2i_k (&s_kv, s);
204 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
205 nat_elog_warn ("out2in key del failed");
207 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
208 s->in2out.addr.as_u32,
209 s->out2in.addr.as_u32,
213 s->in2out.fib_index);
215 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
216 &s->in2out.addr, s->in2out.port,
217 &s->out2in.addr, s->out2in.port, s->nat_proto);
219 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
220 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
223 if (!snat_is_session_static (s))
224 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
226 s->out2in.port, s->nat_proto);
228 nat44_delete_session (sm, s, ctx->thread_index);
237 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
239 ip4_address_t i2o_addr,
242 nat_protocol_t nat_proto,
243 snat_session_t ** sessionp,
244 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
247 snat_session_t *s = 0;
248 clib_bihash_kv_8_8_t kv0;
250 nat_outside_fib_t *outside_fib;
251 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
254 .fp_proto = FIB_PROTOCOL_IP4,
257 .ip4.as_u32 = ip0->dst_address.as_u32,
260 nat44_is_idle_session_ctx_t ctx0;
261 ip4_address_t sm_addr;
265 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
267 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
268 nat_ipfix_logging_max_sessions (thread_index,
269 sm->max_translations_per_thread);
270 nat_elog_notice ("maximum sessions exceeded");
271 return SNAT_IN2OUT_NEXT_DROP;
274 /* First try to match static mapping by local address and port */
275 if (snat_static_mapping_match
276 (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
277 &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat))
279 /* Try to create dynamic translation */
280 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
286 [thread_index].snat_thread_index))
288 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
289 return SNAT_IN2OUT_NEXT_DROP;
294 if (PREDICT_FALSE (identity_nat))
303 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
307 b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
308 return SNAT_IN2OUT_NEXT_DROP;
311 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
314 nat44_delete_user_with_no_session (sm, u, thread_index);
315 nat_elog_warn ("create NAT session failed");
316 return SNAT_IN2OUT_NEXT_DROP;
320 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
321 user_session_increment (sm, u, is_sm);
322 s->in2out.addr = i2o_addr;
323 s->in2out.port = i2o_port;
324 s->in2out.fib_index = rx_fib_index0;
325 s->nat_proto = nat_proto;
326 s->out2in.addr = sm_addr;
327 s->out2in.port = sm_port;
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 = vnet_buffer (b0)->ip.reass.l4_dst_port;
358 /* Add to translation hashes */
360 ctx0.thread_index = thread_index;
361 init_nat_i2o_kv (&kv0, s, 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_elog_notice ("in2out key add failed");
367 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
368 if (clib_bihash_add_or_overwrite_stale_8_8
369 (&sm->per_thread_data[thread_index].out2in, &kv0,
370 nat44_o2i_is_idle_session_cb, &ctx0))
371 nat_elog_notice ("out2in key add failed");
374 snat_ipfix_logging_nat44_ses_create (thread_index,
375 s->in2out.addr.as_u32,
376 s->out2in.addr.as_u32,
379 s->out2in.port, s->in2out.fib_index);
381 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
382 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
383 s->out2in.port, s->nat_proto);
385 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
386 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
387 &s->ext_host_nat_addr, s->ext_host_nat_port,
388 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
393 #ifndef CLIB_MARCH_VARIANT
394 static_always_inline snat_in2out_error_t
395 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
396 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
398 icmp46_header_t *icmp0;
399 icmp_echo_header_t *echo0, *inner_echo0 = 0;
400 ip4_header_t *inner_ip0 = 0;
402 icmp46_header_t *inner_icmp0;
404 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
405 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
407 if (!icmp_type_is_error_message
408 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
410 *nat_proto = NAT_PROTOCOL_ICMP;
411 *addr = ip0->src_address;
412 *port = vnet_buffer (b)->ip.reass.l4_src_port;
416 inner_ip0 = (ip4_header_t *) (echo0 + 1);
417 l4_header = ip4_next_header (inner_ip0);
418 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
419 *addr = inner_ip0->dst_address;
422 case NAT_PROTOCOL_ICMP:
423 inner_icmp0 = (icmp46_header_t *) l4_header;
424 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
425 *port = inner_echo0->identifier;
427 case NAT_PROTOCOL_UDP:
428 case NAT_PROTOCOL_TCP:
429 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
432 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
435 return -1; /* success */
439 * Get address and port values to be used for ICMP packet translation
440 * and create session if needed
442 * @param[in,out] sm NAT main
443 * @param[in,out] node NAT node runtime
444 * @param[in] thread_index thread index
445 * @param[in,out] b0 buffer containing packet to be translated
446 * @param[in,out] ip0 ip header
447 * @param[out] p_proto protocol used for matching
448 * @param[out] p_value address and port after NAT translation
449 * @param[out] p_dont_translate if packet should not be translated
450 * @param d optional parameter
451 * @param e optional parameter
454 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
455 u32 thread_index, vlib_buffer_t * b0,
456 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
457 u32 * fib_index, nat_protocol_t * proto, void *d,
458 void *e, u8 * dont_translate)
460 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
462 snat_session_t *s0 = 0;
463 clib_bihash_kv_8_8_t kv0, value0;
466 vlib_main_t *vm = vlib_get_main ();
469 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
470 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
472 err = icmp_get_key (b0, ip0, addr, port, proto);
475 b0->error = node->errors[err];
476 next0 = SNAT_IN2OUT_NEXT_DROP;
480 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
481 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
483 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
486 (nat_not_translate_output_feature
487 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
495 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
496 ip0, NAT_PROTOCOL_ICMP,
497 *fib_index, thread_index)))
505 (icmp_type_is_error_message
506 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
508 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
509 next0 = SNAT_IN2OUT_NEXT_DROP;
514 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
515 next0, thread_index, vlib_time_now (vm));
517 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
529 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
531 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
533 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
534 reass.icmp_type_or_tcp_flags)))
536 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
537 next0 = SNAT_IN2OUT_NEXT_DROP;
541 s0 = pool_elt_at_index (tsm->sessions, value0.value);
547 *addr = s0->out2in.addr;
548 *port = s0->out2in.port;
549 *fib_index = s0->out2in.fib_index;
552 *(snat_session_t **) (d) = s0;
557 #ifndef CLIB_MARCH_VARIANT
559 * Get address and port values to be used for ICMP packet translation
561 * @param[in] sm NAT main
562 * @param[in,out] node NAT node runtime
563 * @param[in] thread_index thread index
564 * @param[in,out] b0 buffer containing packet to be translated
565 * @param[in,out] ip0 ip header
566 * @param[out] p_proto protocol used for matching
567 * @param[out] p_value address and port after NAT translation
568 * @param[out] p_dont_translate if packet should not be translated
569 * @param d optional parameter
570 * @param e optional parameter
573 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
574 u32 thread_index, vlib_buffer_t * b0,
575 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
576 u32 * fib_index, nat_protocol_t * proto, void *d,
577 void *e, u8 * dont_translate)
585 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
586 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
588 err = icmp_get_key (b0, ip0, addr, port, proto);
591 b0->error = node->errors[err];
592 next0 = SNAT_IN2OUT_NEXT_DROP;
596 ip4_address_t sm_addr;
600 if (snat_static_mapping_match
601 (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
602 &sm_fib_index, 0, &is_addr_only, 0, 0, 0, 0))
604 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
612 if (icmp_type_is_error_message
613 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
615 next0 = SNAT_IN2OUT_NEXT_DROP;
619 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
620 next0 = SNAT_IN2OUT_NEXT_DROP;
625 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
626 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
627 ICMP4_echo_reply || !is_addr_only)
628 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
629 reass.icmp_type_or_tcp_flags)))
631 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
632 next0 = SNAT_IN2OUT_NEXT_DROP;
641 #ifndef CLIB_MARCH_VARIANT
643 icmp_in2out (snat_main_t * sm,
646 icmp46_header_t * icmp0,
649 vlib_node_runtime_t * node,
650 u32 next0, u32 thread_index, void *d, void *e)
652 vlib_main_t *vm = vlib_get_main ();
656 nat_protocol_t protocol;
657 icmp_echo_header_t *echo0, *inner_echo0 = 0;
658 ip4_header_t *inner_ip0;
660 icmp46_header_t *inner_icmp0;
662 u32 new_addr0, old_addr0;
663 u16 old_id0, new_id0;
664 u16 old_checksum0, new_checksum0;
669 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
672 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
673 &fib_index, &protocol, d, e, &dont_translate);
676 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
679 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
682 ip_incremental_checksum_buffer (vm, b0,
684 (u8 *) vlib_buffer_get_current (b0),
685 ntohs (ip0->length) -
686 ip4_header_bytes (ip0), 0);
687 checksum0 = ~ip_csum_fold (sum0);
688 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
690 next0 = SNAT_IN2OUT_NEXT_DROP;
695 old_addr0 = ip0->src_address.as_u32;
696 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
698 sum0 = ip0->checksum;
699 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
700 src_address /* changed member */ );
701 ip0->checksum = ip_csum_fold (sum0);
703 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
705 if (icmp0->checksum == 0)
706 icmp0->checksum = 0xffff;
708 if (!icmp_type_is_error_message (icmp0->type))
711 if (PREDICT_FALSE (new_id0 != echo0->identifier))
713 old_id0 = echo0->identifier;
715 echo0->identifier = new_id0;
717 sum0 = icmp0->checksum;
719 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
721 icmp0->checksum = ip_csum_fold (sum0);
726 inner_ip0 = (ip4_header_t *) (echo0 + 1);
727 l4_header = ip4_next_header (inner_ip0);
729 if (!ip4_header_checksum_is_valid (inner_ip0))
731 next0 = SNAT_IN2OUT_NEXT_DROP;
735 /* update inner destination IP address */
736 old_addr0 = inner_ip0->dst_address.as_u32;
737 inner_ip0->dst_address = addr;
738 new_addr0 = inner_ip0->dst_address.as_u32;
739 sum0 = icmp0->checksum;
740 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
741 dst_address /* changed member */ );
742 icmp0->checksum = ip_csum_fold (sum0);
744 /* update inner IP header checksum */
745 old_checksum0 = inner_ip0->checksum;
746 sum0 = inner_ip0->checksum;
747 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
748 dst_address /* changed member */ );
749 inner_ip0->checksum = ip_csum_fold (sum0);
750 new_checksum0 = inner_ip0->checksum;
751 sum0 = icmp0->checksum;
753 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
755 icmp0->checksum = ip_csum_fold (sum0);
759 case NAT_PROTOCOL_ICMP:
760 inner_icmp0 = (icmp46_header_t *) l4_header;
761 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
763 old_id0 = inner_echo0->identifier;
765 inner_echo0->identifier = new_id0;
767 sum0 = icmp0->checksum;
769 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
771 icmp0->checksum = ip_csum_fold (sum0);
773 case NAT_PROTOCOL_UDP:
774 case NAT_PROTOCOL_TCP:
775 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
777 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
779 sum0 = icmp0->checksum;
780 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
782 icmp0->checksum = ip_csum_fold (sum0);
790 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
792 if (sm->deterministic ||
793 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
794 sm->endpoint_dependent))
795 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
804 icmp_in2out_slow_path (snat_main_t * sm,
807 icmp46_header_t * icmp0,
810 vlib_node_runtime_t * node,
812 f64 now, u32 thread_index, snat_session_t ** p_s0)
814 vlib_main_t *vm = vlib_get_main ();
816 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
817 next0, thread_index, p_s0, 0);
818 snat_session_t *s0 = *p_s0;
819 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
822 nat44_session_update_counters (s0, now,
823 vlib_buffer_length_in_chain
824 (vm, b0), thread_index);
825 /* Per-user LRU list maintenance */
826 nat44_session_update_lru (sm, s0, thread_index);
832 nat_in2out_sm_unknown_proto (snat_main_t * sm,
834 ip4_header_t * ip, u32 rx_fib_index)
836 clib_bihash_kv_8_8_t kv, value;
837 snat_static_mapping_t *m;
838 u32 old_addr, new_addr;
841 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
842 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
845 m = pool_elt_at_index (sm->static_mappings, value.value);
847 old_addr = ip->src_address.as_u32;
848 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
850 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
851 ip->checksum = ip_csum_fold (sum);
855 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
857 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
858 nat_hairpinning_sm_unknown_proto (sm, b, ip);
865 snat_in2out_node_fn_inline (vlib_main_t * vm,
866 vlib_node_runtime_t * node,
867 vlib_frame_t * frame, int is_slow_path,
868 int is_output_feature)
870 u32 n_left_from, *from;
871 snat_main_t *sm = &snat_main;
872 f64 now = vlib_time_now (vm);
873 u32 thread_index = vm->thread_index;
875 from = vlib_frame_vector_args (frame);
876 n_left_from = frame->n_vectors;
878 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
879 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
880 vlib_get_buffers (vm, from, b, n_left_from);
882 while (n_left_from >= 2)
884 vlib_buffer_t *b0, *b1;
886 u32 sw_if_index0, sw_if_index1;
887 ip4_header_t *ip0, *ip1;
888 ip_csum_t sum0, sum1;
889 u32 new_addr0, old_addr0, new_addr1, old_addr1;
890 u16 old_port0, new_port0, old_port1, new_port1;
891 udp_header_t *udp0, *udp1;
892 tcp_header_t *tcp0, *tcp1;
893 icmp46_header_t *icmp0, *icmp1;
894 u32 rx_fib_index0, rx_fib_index1;
896 snat_session_t *s0 = 0, *s1 = 0;
897 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
898 u32 iph_offset0 = 0, iph_offset1 = 0;
905 /* Prefetch next iteration. */
906 if (PREDICT_TRUE (n_left_from >= 4))
908 vlib_buffer_t *p2, *p3;
913 vlib_prefetch_buffer_header (p2, LOAD);
914 vlib_prefetch_buffer_header (p3, LOAD);
916 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
917 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
920 if (is_output_feature)
921 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
923 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
926 udp0 = ip4_next_header (ip0);
927 tcp0 = (tcp_header_t *) udp0;
928 icmp0 = (icmp46_header_t *) udp0;
930 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
931 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
934 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
936 if (PREDICT_FALSE (ip0->ttl == 1))
938 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
939 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
940 ICMP4_time_exceeded_ttl_exceeded_in_transit,
942 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
946 proto0 = ip_proto_to_nat_proto (ip0->protocol);
948 /* Next configured feature, probably ip4-lookup */
951 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
953 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
955 next0 = SNAT_IN2OUT_NEXT_DROP;
957 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
959 vlib_increment_simple_counter (is_slow_path ? &sm->
960 counters.slowpath.in2out.
961 other : &sm->counters.fastpath.
962 in2out.other, thread_index,
967 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
969 next0 = icmp_in2out_slow_path
970 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
971 node, next0, now, thread_index, &s0);
972 vlib_increment_simple_counter (is_slow_path ? &sm->
973 counters.slowpath.in2out.
974 icmp : &sm->counters.fastpath.
975 in2out.icmp, thread_index,
982 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
984 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
988 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
990 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
995 init_nat_k (&kv0, ip0->src_address,
996 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
999 (clib_bihash_search_8_8
1000 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1004 if (is_output_feature)
1007 (nat_not_translate_output_feature
1009 vnet_buffer (b0)->ip.reass.l4_src_port,
1010 vnet_buffer (b0)->ip.reass.l4_dst_port,
1011 thread_index, sw_if_index0)))
1015 * Send DHCP packets to the ipv4 stack, or we won't
1016 * be able to use dhcp client on the outside interface
1019 (proto0 == NAT_PROTOCOL_UDP
1020 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1021 clib_host_to_net_u16
1022 (UDP_DST_PORT_dhcp_to_server))
1023 && ip0->dst_address.as_u32 == 0xffffffff))
1030 (sm, node, sw_if_index0, ip0, proto0,
1031 rx_fib_index0, thread_index)))
1035 next0 = slow_path (sm, b0, ip0,
1037 vnet_buffer (b0)->ip.reass.l4_src_port,
1039 proto0, &s0, node, next0, thread_index, now);
1040 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1043 if (PREDICT_FALSE (!s0))
1048 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1054 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1057 b0->flags |= VNET_BUFFER_F_IS_NATED;
1059 old_addr0 = ip0->src_address.as_u32;
1060 ip0->src_address = s0->out2in.addr;
1061 new_addr0 = ip0->src_address.as_u32;
1062 if (!is_output_feature)
1063 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1065 sum0 = ip0->checksum;
1066 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1067 ip4_header_t, src_address /* changed member */ );
1068 ip0->checksum = ip_csum_fold (sum0);
1071 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1073 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1075 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1076 new_port0 = udp0->src_port = s0->out2in.port;
1077 sum0 = tcp0->checksum;
1078 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1080 dst_address /* changed member */ );
1081 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1082 ip4_header_t /* cheat */ ,
1083 length /* changed member */ );
1084 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1085 tcp0->checksum = ip_csum_fold (sum0);
1087 vlib_increment_simple_counter (is_slow_path ? &sm->
1088 counters.slowpath.in2out.tcp : &sm->
1089 counters.fastpath.in2out.tcp,
1090 thread_index, sw_if_index0, 1);
1094 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1096 udp0->src_port = s0->out2in.port;
1097 if (PREDICT_FALSE (udp0->checksum))
1099 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1100 new_port0 = udp0->src_port;
1101 sum0 = udp0->checksum;
1102 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1105 ip_csum_update (sum0, old_port0, new_port0,
1106 ip4_header_t /* cheat */ ,
1107 length /* changed member */ );
1108 udp0->checksum = ip_csum_fold (sum0);
1111 vlib_increment_simple_counter (is_slow_path ? &sm->
1112 counters.slowpath.in2out.udp : &sm->
1113 counters.fastpath.in2out.udp,
1114 thread_index, sw_if_index0, 1);
1118 nat44_session_update_counters (s0, now,
1119 vlib_buffer_length_in_chain (vm, b0),
1121 /* Per-user LRU list maintenance */
1122 nat44_session_update_lru (sm, s0, thread_index);
1125 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1126 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1128 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1129 t->is_slow_path = is_slow_path;
1130 t->sw_if_index = sw_if_index0;
1131 t->next_index = next0;
1132 t->session_index = ~0;
1135 s0 - sm->per_thread_data[thread_index].sessions;
1138 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1140 vlib_increment_simple_counter (is_slow_path ? &sm->
1141 counters.slowpath.in2out.
1142 drops : &sm->counters.fastpath.
1143 in2out.drops, thread_index,
1147 if (is_output_feature)
1148 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1150 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1153 udp1 = ip4_next_header (ip1);
1154 tcp1 = (tcp_header_t *) udp1;
1155 icmp1 = (icmp46_header_t *) udp1;
1157 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1158 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1161 if (PREDICT_FALSE (ip1->ttl == 1))
1163 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1164 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1165 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1167 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1171 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1173 /* Next configured feature, probably ip4-lookup */
1176 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1178 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1180 next1 = SNAT_IN2OUT_NEXT_DROP;
1182 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1184 vlib_increment_simple_counter (is_slow_path ? &sm->
1185 counters.slowpath.in2out.
1186 other : &sm->counters.fastpath.
1187 in2out.other, thread_index,
1192 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1194 next1 = icmp_in2out_slow_path
1195 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1196 next1, now, thread_index, &s1);
1197 vlib_increment_simple_counter (is_slow_path ? &sm->
1198 counters.slowpath.in2out.
1199 icmp : &sm->counters.fastpath.
1200 in2out.icmp, thread_index,
1207 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1209 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1213 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1215 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1220 init_nat_k (&kv1, ip1->src_address,
1221 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1224 (clib_bihash_search_8_8
1225 (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1229 if (is_output_feature)
1232 (nat_not_translate_output_feature
1234 vnet_buffer (b1)->ip.reass.l4_src_port,
1235 vnet_buffer (b1)->ip.reass.l4_dst_port,
1236 thread_index, sw_if_index1)))
1240 * Send DHCP packets to the ipv4 stack, or we won't
1241 * be able to use dhcp client on the outside interface
1244 (proto1 == NAT_PROTOCOL_UDP
1245 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1246 clib_host_to_net_u16
1247 (UDP_DST_PORT_dhcp_to_server))
1248 && ip1->dst_address.as_u32 == 0xffffffff))
1255 (sm, node, sw_if_index1, ip1, proto1,
1256 rx_fib_index1, thread_index)))
1261 slow_path (sm, b1, ip1, ip1->src_address,
1262 vnet_buffer (b1)->ip.reass.l4_src_port,
1263 rx_fib_index1, proto1, &s1, node, next1,
1265 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1268 if (PREDICT_FALSE (!s1))
1273 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1279 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1282 b1->flags |= VNET_BUFFER_F_IS_NATED;
1284 old_addr1 = ip1->src_address.as_u32;
1285 ip1->src_address = s1->out2in.addr;
1286 new_addr1 = ip1->src_address.as_u32;
1287 if (!is_output_feature)
1288 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1290 sum1 = ip1->checksum;
1291 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1292 ip4_header_t, src_address /* changed member */ );
1293 ip1->checksum = ip_csum_fold (sum1);
1295 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1297 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1299 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1300 new_port1 = udp1->src_port = s1->out2in.port;
1301 sum1 = tcp1->checksum;
1302 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1304 dst_address /* changed member */ );
1305 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1306 ip4_header_t /* cheat */ ,
1307 length /* changed member */ );
1308 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1309 tcp1->checksum = ip_csum_fold (sum1);
1311 vlib_increment_simple_counter (is_slow_path ? &sm->
1312 counters.slowpath.in2out.tcp : &sm->
1313 counters.fastpath.in2out.tcp,
1314 thread_index, sw_if_index1, 1);
1318 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1320 udp1->src_port = s1->out2in.port;
1321 if (PREDICT_FALSE (udp1->checksum))
1323 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1324 new_port1 = udp1->src_port;
1325 sum1 = udp1->checksum;
1326 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1329 ip_csum_update (sum1, old_port1, new_port1,
1330 ip4_header_t /* cheat */ ,
1331 length /* changed member */ );
1332 udp1->checksum = ip_csum_fold (sum1);
1335 vlib_increment_simple_counter (is_slow_path ? &sm->
1336 counters.slowpath.in2out.udp : &sm->
1337 counters.fastpath.in2out.udp,
1338 thread_index, sw_if_index1, 1);
1342 nat44_session_update_counters (s1, now,
1343 vlib_buffer_length_in_chain (vm, b1),
1345 /* Per-user LRU list maintenance */
1346 nat44_session_update_lru (sm, s1, thread_index);
1349 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1350 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1352 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1353 t->sw_if_index = sw_if_index1;
1354 t->next_index = next1;
1355 t->session_index = ~0;
1358 s1 - sm->per_thread_data[thread_index].sessions;
1361 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1363 vlib_increment_simple_counter (is_slow_path ? &sm->
1364 counters.slowpath.in2out.
1365 drops : &sm->counters.fastpath.
1366 in2out.drops, thread_index,
1376 while (n_left_from > 0)
1383 u32 new_addr0, old_addr0;
1384 u16 old_port0, new_port0;
1387 icmp46_header_t *icmp0;
1390 snat_session_t *s0 = 0;
1391 clib_bihash_kv_8_8_t kv0, value0;
1392 u32 iph_offset0 = 0;
1396 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1398 if (is_output_feature)
1399 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1401 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1404 udp0 = ip4_next_header (ip0);
1405 tcp0 = (tcp_header_t *) udp0;
1406 icmp0 = (icmp46_header_t *) udp0;
1408 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1409 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1412 if (PREDICT_FALSE (ip0->ttl == 1))
1414 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1415 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1416 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1418 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1422 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1424 /* Next configured feature, probably ip4-lookup */
1427 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1429 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1431 next0 = SNAT_IN2OUT_NEXT_DROP;
1433 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1435 vlib_increment_simple_counter (is_slow_path ? &sm->
1436 counters.slowpath.in2out.
1437 other : &sm->counters.fastpath.
1438 in2out.other, thread_index,
1443 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1445 next0 = icmp_in2out_slow_path
1446 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1447 next0, now, thread_index, &s0);
1448 vlib_increment_simple_counter (is_slow_path ? &sm->
1449 counters.slowpath.in2out.
1450 icmp : &sm->counters.fastpath.
1451 in2out.icmp, thread_index,
1458 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1460 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1464 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1466 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1471 init_nat_k (&kv0, ip0->src_address,
1472 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1475 if (clib_bihash_search_8_8
1476 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1480 if (is_output_feature)
1483 (nat_not_translate_output_feature
1485 vnet_buffer (b0)->ip.reass.l4_src_port,
1486 vnet_buffer (b0)->ip.reass.l4_dst_port,
1487 thread_index, sw_if_index0)))
1491 * Send DHCP packets to the ipv4 stack, or we won't
1492 * be able to use dhcp client on the outside interface
1495 (proto0 == NAT_PROTOCOL_UDP
1496 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1497 clib_host_to_net_u16
1498 (UDP_DST_PORT_dhcp_to_server))
1499 && ip0->dst_address.as_u32 == 0xffffffff))
1506 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1512 slow_path (sm, b0, ip0, ip0->src_address,
1513 vnet_buffer (b0)->ip.reass.l4_src_port,
1514 rx_fib_index0, proto0, &s0, node, next0,
1517 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1520 if (PREDICT_FALSE (!s0))
1525 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1531 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1534 b0->flags |= VNET_BUFFER_F_IS_NATED;
1536 old_addr0 = ip0->src_address.as_u32;
1537 ip0->src_address = s0->out2in.addr;
1538 new_addr0 = ip0->src_address.as_u32;
1539 if (!is_output_feature)
1540 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1542 sum0 = ip0->checksum;
1543 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1544 ip4_header_t, src_address /* changed member */ );
1545 ip0->checksum = ip_csum_fold (sum0);
1547 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1549 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1551 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1552 new_port0 = udp0->src_port = s0->out2in.port;
1553 sum0 = tcp0->checksum;
1555 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1556 dst_address /* changed member */ );
1558 ip_csum_update (sum0, old_port0, new_port0,
1559 ip4_header_t /* cheat */ ,
1560 length /* changed member */ );
1561 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1562 tcp0->checksum = ip_csum_fold (sum0);
1564 vlib_increment_simple_counter (is_slow_path ? &sm->
1565 counters.slowpath.in2out.tcp : &sm->
1566 counters.fastpath.in2out.tcp,
1567 thread_index, sw_if_index0, 1);
1571 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1573 udp0->src_port = s0->out2in.port;
1574 if (PREDICT_FALSE (udp0->checksum))
1576 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1577 new_port0 = udp0->src_port;
1578 sum0 = udp0->checksum;
1580 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1581 dst_address /* changed member */ );
1583 ip_csum_update (sum0, old_port0, new_port0,
1584 ip4_header_t /* cheat */ ,
1585 length /* changed member */ );
1586 udp0->checksum = ip_csum_fold (sum0);
1589 vlib_increment_simple_counter (is_slow_path ? &sm->
1590 counters.slowpath.in2out.udp : &sm->
1591 counters.fastpath.in2out.udp,
1592 thread_index, sw_if_index0, 1);
1596 nat44_session_update_counters (s0, now,
1597 vlib_buffer_length_in_chain (vm, b0),
1599 /* Per-user LRU list maintenance */
1600 nat44_session_update_lru (sm, s0, thread_index);
1603 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1604 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1606 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1607 t->is_slow_path = is_slow_path;
1608 t->sw_if_index = sw_if_index0;
1609 t->next_index = next0;
1610 t->session_index = ~0;
1613 s0 - sm->per_thread_data[thread_index].sessions;
1616 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1618 vlib_increment_simple_counter (is_slow_path ? &sm->
1619 counters.slowpath.in2out.
1620 drops : &sm->counters.fastpath.
1621 in2out.drops, thread_index,
1630 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1632 return frame->n_vectors;
1635 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1636 vlib_node_runtime_t * node,
1637 vlib_frame_t * frame)
1639 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1644 VLIB_REGISTER_NODE (snat_in2out_node) = {
1645 .name = "nat44-in2out",
1646 .vector_size = sizeof (u32),
1647 .format_trace = format_snat_in2out_trace,
1648 .type = VLIB_NODE_TYPE_INTERNAL,
1650 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1651 .error_strings = snat_in2out_error_strings,
1653 .runtime_data_bytes = sizeof (snat_runtime_t),
1655 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1657 /* edit / add dispositions here */
1659 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1660 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1661 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1662 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1667 VLIB_NODE_FN (snat_in2out_output_node) (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, 0 /* is_slow_path */ ,
1676 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1677 .name = "nat44-in2out-output",
1678 .vector_size = sizeof (u32),
1679 .format_trace = format_snat_in2out_trace,
1680 .type = VLIB_NODE_TYPE_INTERNAL,
1682 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1683 .error_strings = snat_in2out_error_strings,
1685 .runtime_data_bytes = sizeof (snat_runtime_t),
1687 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1689 /* edit / add dispositions here */
1691 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1692 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1693 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1694 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1699 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1700 vlib_node_runtime_t * node,
1701 vlib_frame_t * frame)
1703 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1708 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1709 .name = "nat44-in2out-slowpath",
1710 .vector_size = sizeof (u32),
1711 .format_trace = format_snat_in2out_trace,
1712 .type = VLIB_NODE_TYPE_INTERNAL,
1714 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1715 .error_strings = snat_in2out_error_strings,
1717 .runtime_data_bytes = sizeof (snat_runtime_t),
1719 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1721 /* edit / add dispositions here */
1723 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1724 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1725 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1726 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1731 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1732 vlib_node_runtime_t * node,
1733 vlib_frame_t * frame)
1735 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1740 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1741 .name = "nat44-in2out-output-slowpath",
1742 .vector_size = sizeof (u32),
1743 .format_trace = format_snat_in2out_trace,
1744 .type = VLIB_NODE_TYPE_INTERNAL,
1746 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1747 .error_strings = snat_in2out_error_strings,
1749 .runtime_data_bytes = sizeof (snat_runtime_t),
1751 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1753 /* edit / add dispositions here */
1755 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1756 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1757 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1758 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1763 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1764 vlib_node_runtime_t * node,
1765 vlib_frame_t * frame)
1767 u32 n_left_from, *from, *to_next;
1768 snat_in2out_next_t next_index;
1769 snat_main_t *sm = &snat_main;
1770 int is_hairpinning = 0;
1772 from = vlib_frame_vector_args (frame);
1773 n_left_from = frame->n_vectors;
1774 next_index = node->cached_next_index;
1776 while (n_left_from > 0)
1780 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1782 while (n_left_from > 0 && n_left_to_next > 0)
1790 u32 new_addr0, old_addr0;
1791 u16 old_port0, new_port0;
1794 icmp46_header_t *icmp0;
1797 ip4_address_t sm0_addr;
1802 /* speculatively enqueue b0 to the current next frame */
1808 n_left_to_next -= 1;
1810 b0 = vlib_get_buffer (vm, bi0);
1811 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1813 ip0 = vlib_buffer_get_current (b0);
1814 udp0 = ip4_next_header (ip0);
1815 tcp0 = (tcp_header_t *) udp0;
1816 icmp0 = (icmp46_header_t *) udp0;
1818 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1820 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1822 if (PREDICT_FALSE (ip0->ttl == 1))
1824 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1825 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1826 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1828 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1832 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1834 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1837 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1839 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1840 rx_fib_index0, node, next0, ~0, 0, 0);
1844 if (snat_static_mapping_match
1845 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1846 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1848 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1849 next0 = SNAT_IN2OUT_NEXT_DROP;
1853 new_addr0 = sm0_addr.as_u32;
1854 new_port0 = sm0_port;
1855 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1856 old_addr0 = ip0->src_address.as_u32;
1857 ip0->src_address.as_u32 = new_addr0;
1859 sum0 = ip0->checksum;
1860 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1862 src_address /* changed member */ );
1863 ip0->checksum = ip_csum_fold (sum0);
1865 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1867 old_port0 = udp0->src_port;
1868 udp0->src_port = new_port0;
1870 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1872 sum0 = tcp0->checksum;
1873 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1875 dst_address /* changed member */ );
1876 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1877 ip4_header_t /* cheat */ ,
1878 length /* changed member */ );
1879 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1880 tcp0->checksum = ip_csum_fold (sum0);
1882 else if (udp0->checksum)
1884 sum0 = udp0->checksum;
1885 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1887 dst_address /* changed member */ );
1888 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1889 ip4_header_t /* cheat */ ,
1890 length /* changed member */ );
1891 udp0->checksum = ip_csum_fold (sum0);
1896 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1898 sum0 = tcp0->checksum;
1899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1901 dst_address /* changed member */ );
1902 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1903 tcp0->checksum = ip_csum_fold (sum0);
1905 else if (udp0->checksum)
1907 sum0 = udp0->checksum;
1908 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1910 dst_address /* changed member */ );
1911 udp0->checksum = ip_csum_fold (sum0);
1916 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1921 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1922 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1924 snat_in2out_trace_t *t =
1925 vlib_add_trace (vm, node, b0, sizeof (*t));
1926 t->sw_if_index = sw_if_index0;
1927 t->next_index = next0;
1928 t->is_hairpinning = is_hairpinning;
1931 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1934 vlib_increment_simple_counter (&sm->counters.fastpath.
1935 in2out.other, sw_if_index0,
1936 vm->thread_index, 1);
1939 /* verify speculative enqueue, maybe switch current next frame */
1940 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1941 to_next, n_left_to_next,
1945 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1948 return frame->n_vectors;
1953 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1954 .name = "nat44-in2out-fast",
1955 .vector_size = sizeof (u32),
1956 .format_trace = format_snat_in2out_fast_trace,
1957 .type = VLIB_NODE_TYPE_INTERNAL,
1959 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1960 .error_strings = snat_in2out_error_strings,
1962 .runtime_data_bytes = sizeof (snat_runtime_t),
1964 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1966 /* edit / add dispositions here */
1968 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1969 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1970 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1971 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1977 * fd.io coding-style-patch-verification: ON
1980 * eval: (c-set-style "gnu")