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 (0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
793 sm->endpoint_dependent))
794 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
803 icmp_in2out_slow_path (snat_main_t * sm,
806 icmp46_header_t * icmp0,
809 vlib_node_runtime_t * node,
811 f64 now, u32 thread_index, snat_session_t ** p_s0)
813 vlib_main_t *vm = vlib_get_main ();
815 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
816 next0, thread_index, p_s0, 0);
817 snat_session_t *s0 = *p_s0;
818 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
821 nat44_session_update_counters (s0, now,
822 vlib_buffer_length_in_chain
823 (vm, b0), thread_index);
824 /* Per-user LRU list maintenance */
825 nat44_session_update_lru (sm, s0, thread_index);
831 nat_in2out_sm_unknown_proto (snat_main_t * sm,
833 ip4_header_t * ip, u32 rx_fib_index)
835 clib_bihash_kv_8_8_t kv, value;
836 snat_static_mapping_t *m;
837 u32 old_addr, new_addr;
840 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
841 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
844 m = pool_elt_at_index (sm->static_mappings, value.value);
846 old_addr = ip->src_address.as_u32;
847 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
849 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
850 ip->checksum = ip_csum_fold (sum);
854 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
856 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
857 nat_hairpinning_sm_unknown_proto (sm, b, ip);
864 snat_in2out_node_fn_inline (vlib_main_t * vm,
865 vlib_node_runtime_t * node,
866 vlib_frame_t * frame, int is_slow_path,
867 int is_output_feature)
869 u32 n_left_from, *from;
870 snat_main_t *sm = &snat_main;
871 f64 now = vlib_time_now (vm);
872 u32 thread_index = vm->thread_index;
874 from = vlib_frame_vector_args (frame);
875 n_left_from = frame->n_vectors;
877 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
878 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
879 vlib_get_buffers (vm, from, b, n_left_from);
881 while (n_left_from >= 2)
883 vlib_buffer_t *b0, *b1;
885 u32 sw_if_index0, sw_if_index1;
886 ip4_header_t *ip0, *ip1;
887 ip_csum_t sum0, sum1;
888 u32 new_addr0, old_addr0, new_addr1, old_addr1;
889 u16 old_port0, new_port0, old_port1, new_port1;
890 udp_header_t *udp0, *udp1;
891 tcp_header_t *tcp0, *tcp1;
892 icmp46_header_t *icmp0, *icmp1;
893 u32 rx_fib_index0, rx_fib_index1;
895 snat_session_t *s0 = 0, *s1 = 0;
896 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
897 u32 iph_offset0 = 0, iph_offset1 = 0;
904 /* Prefetch next iteration. */
905 if (PREDICT_TRUE (n_left_from >= 4))
907 vlib_buffer_t *p2, *p3;
912 vlib_prefetch_buffer_header (p2, LOAD);
913 vlib_prefetch_buffer_header (p3, LOAD);
915 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
916 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
919 if (is_output_feature)
920 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
922 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
925 udp0 = ip4_next_header (ip0);
926 tcp0 = (tcp_header_t *) udp0;
927 icmp0 = (icmp46_header_t *) udp0;
929 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
930 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
933 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
935 if (PREDICT_FALSE (ip0->ttl == 1))
937 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
938 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
939 ICMP4_time_exceeded_ttl_exceeded_in_transit,
941 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
945 proto0 = ip_proto_to_nat_proto (ip0->protocol);
947 /* Next configured feature, probably ip4-lookup */
950 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
952 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
954 next0 = SNAT_IN2OUT_NEXT_DROP;
956 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
958 vlib_increment_simple_counter (is_slow_path ? &sm->
959 counters.slowpath.in2out.
960 other : &sm->counters.fastpath.
961 in2out.other, thread_index,
966 if (PREDICT_FALSE (proto0 == NAT_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);
971 vlib_increment_simple_counter (is_slow_path ? &sm->
972 counters.slowpath.in2out.
973 icmp : &sm->counters.fastpath.
974 in2out.icmp, thread_index,
981 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
983 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
987 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
989 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
994 init_nat_k (&kv0, ip0->src_address,
995 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
998 (clib_bihash_search_8_8
999 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1003 if (is_output_feature)
1006 (nat_not_translate_output_feature
1008 vnet_buffer (b0)->ip.reass.l4_src_port,
1009 vnet_buffer (b0)->ip.reass.l4_dst_port,
1010 thread_index, sw_if_index0)))
1014 * Send DHCP packets to the ipv4 stack, or we won't
1015 * be able to use dhcp client on the outside interface
1018 (proto0 == NAT_PROTOCOL_UDP
1019 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1020 clib_host_to_net_u16
1021 (UDP_DST_PORT_dhcp_to_server))
1022 && ip0->dst_address.as_u32 == 0xffffffff))
1029 (sm, node, sw_if_index0, ip0, proto0,
1030 rx_fib_index0, thread_index)))
1034 next0 = slow_path (sm, b0, ip0,
1036 vnet_buffer (b0)->ip.reass.l4_src_port,
1038 proto0, &s0, node, next0, thread_index, now);
1039 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1042 if (PREDICT_FALSE (!s0))
1047 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1053 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1056 b0->flags |= VNET_BUFFER_F_IS_NATED;
1058 old_addr0 = ip0->src_address.as_u32;
1059 ip0->src_address = s0->out2in.addr;
1060 new_addr0 = ip0->src_address.as_u32;
1061 if (!is_output_feature)
1062 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1064 sum0 = ip0->checksum;
1065 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1066 ip4_header_t, src_address /* changed member */ );
1067 ip0->checksum = ip_csum_fold (sum0);
1070 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1072 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1074 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1075 new_port0 = udp0->src_port = s0->out2in.port;
1076 sum0 = tcp0->checksum;
1077 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1079 dst_address /* changed member */ );
1080 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1081 ip4_header_t /* cheat */ ,
1082 length /* changed member */ );
1083 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1084 tcp0->checksum = ip_csum_fold (sum0);
1086 vlib_increment_simple_counter (is_slow_path ? &sm->
1087 counters.slowpath.in2out.tcp : &sm->
1088 counters.fastpath.in2out.tcp,
1089 thread_index, sw_if_index0, 1);
1093 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1095 udp0->src_port = s0->out2in.port;
1096 if (PREDICT_FALSE (udp0->checksum))
1098 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1099 new_port0 = udp0->src_port;
1100 sum0 = udp0->checksum;
1101 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1104 ip_csum_update (sum0, old_port0, new_port0,
1105 ip4_header_t /* cheat */ ,
1106 length /* changed member */ );
1107 udp0->checksum = ip_csum_fold (sum0);
1110 vlib_increment_simple_counter (is_slow_path ? &sm->
1111 counters.slowpath.in2out.udp : &sm->
1112 counters.fastpath.in2out.udp,
1113 thread_index, sw_if_index0, 1);
1117 nat44_session_update_counters (s0, now,
1118 vlib_buffer_length_in_chain (vm, b0),
1120 /* Per-user LRU list maintenance */
1121 nat44_session_update_lru (sm, s0, thread_index);
1124 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1125 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1127 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1128 t->is_slow_path = is_slow_path;
1129 t->sw_if_index = sw_if_index0;
1130 t->next_index = next0;
1131 t->session_index = ~0;
1134 s0 - sm->per_thread_data[thread_index].sessions;
1137 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1139 vlib_increment_simple_counter (is_slow_path ? &sm->
1140 counters.slowpath.in2out.
1141 drops : &sm->counters.fastpath.
1142 in2out.drops, thread_index,
1146 if (is_output_feature)
1147 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1149 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1152 udp1 = ip4_next_header (ip1);
1153 tcp1 = (tcp_header_t *) udp1;
1154 icmp1 = (icmp46_header_t *) udp1;
1156 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1157 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1160 if (PREDICT_FALSE (ip1->ttl == 1))
1162 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1163 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1164 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1166 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1170 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1172 /* Next configured feature, probably ip4-lookup */
1175 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1177 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1179 next1 = SNAT_IN2OUT_NEXT_DROP;
1181 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1183 vlib_increment_simple_counter (is_slow_path ? &sm->
1184 counters.slowpath.in2out.
1185 other : &sm->counters.fastpath.
1186 in2out.other, thread_index,
1191 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1193 next1 = icmp_in2out_slow_path
1194 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1195 next1, now, thread_index, &s1);
1196 vlib_increment_simple_counter (is_slow_path ? &sm->
1197 counters.slowpath.in2out.
1198 icmp : &sm->counters.fastpath.
1199 in2out.icmp, thread_index,
1206 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1208 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1212 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1214 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1219 init_nat_k (&kv1, ip1->src_address,
1220 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1223 (clib_bihash_search_8_8
1224 (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1228 if (is_output_feature)
1231 (nat_not_translate_output_feature
1233 vnet_buffer (b1)->ip.reass.l4_src_port,
1234 vnet_buffer (b1)->ip.reass.l4_dst_port,
1235 thread_index, sw_if_index1)))
1239 * Send DHCP packets to the ipv4 stack, or we won't
1240 * be able to use dhcp client on the outside interface
1243 (proto1 == NAT_PROTOCOL_UDP
1244 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1245 clib_host_to_net_u16
1246 (UDP_DST_PORT_dhcp_to_server))
1247 && ip1->dst_address.as_u32 == 0xffffffff))
1254 (sm, node, sw_if_index1, ip1, proto1,
1255 rx_fib_index1, thread_index)))
1260 slow_path (sm, b1, ip1, ip1->src_address,
1261 vnet_buffer (b1)->ip.reass.l4_src_port,
1262 rx_fib_index1, proto1, &s1, node, next1,
1264 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1267 if (PREDICT_FALSE (!s1))
1272 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1278 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1281 b1->flags |= VNET_BUFFER_F_IS_NATED;
1283 old_addr1 = ip1->src_address.as_u32;
1284 ip1->src_address = s1->out2in.addr;
1285 new_addr1 = ip1->src_address.as_u32;
1286 if (!is_output_feature)
1287 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1289 sum1 = ip1->checksum;
1290 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1291 ip4_header_t, src_address /* changed member */ );
1292 ip1->checksum = ip_csum_fold (sum1);
1294 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1296 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1298 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1299 new_port1 = udp1->src_port = s1->out2in.port;
1300 sum1 = tcp1->checksum;
1301 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1303 dst_address /* changed member */ );
1304 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1305 ip4_header_t /* cheat */ ,
1306 length /* changed member */ );
1307 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1308 tcp1->checksum = ip_csum_fold (sum1);
1310 vlib_increment_simple_counter (is_slow_path ? &sm->
1311 counters.slowpath.in2out.tcp : &sm->
1312 counters.fastpath.in2out.tcp,
1313 thread_index, sw_if_index1, 1);
1317 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1319 udp1->src_port = s1->out2in.port;
1320 if (PREDICT_FALSE (udp1->checksum))
1322 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1323 new_port1 = udp1->src_port;
1324 sum1 = udp1->checksum;
1325 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1328 ip_csum_update (sum1, old_port1, new_port1,
1329 ip4_header_t /* cheat */ ,
1330 length /* changed member */ );
1331 udp1->checksum = ip_csum_fold (sum1);
1334 vlib_increment_simple_counter (is_slow_path ? &sm->
1335 counters.slowpath.in2out.udp : &sm->
1336 counters.fastpath.in2out.udp,
1337 thread_index, sw_if_index1, 1);
1341 nat44_session_update_counters (s1, now,
1342 vlib_buffer_length_in_chain (vm, b1),
1344 /* Per-user LRU list maintenance */
1345 nat44_session_update_lru (sm, s1, thread_index);
1348 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1349 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1351 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1352 t->sw_if_index = sw_if_index1;
1353 t->next_index = next1;
1354 t->session_index = ~0;
1357 s1 - sm->per_thread_data[thread_index].sessions;
1360 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1362 vlib_increment_simple_counter (is_slow_path ? &sm->
1363 counters.slowpath.in2out.
1364 drops : &sm->counters.fastpath.
1365 in2out.drops, thread_index,
1375 while (n_left_from > 0)
1382 u32 new_addr0, old_addr0;
1383 u16 old_port0, new_port0;
1386 icmp46_header_t *icmp0;
1389 snat_session_t *s0 = 0;
1390 clib_bihash_kv_8_8_t kv0, value0;
1391 u32 iph_offset0 = 0;
1395 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1397 if (is_output_feature)
1398 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1400 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1403 udp0 = ip4_next_header (ip0);
1404 tcp0 = (tcp_header_t *) udp0;
1405 icmp0 = (icmp46_header_t *) udp0;
1407 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1408 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1411 if (PREDICT_FALSE (ip0->ttl == 1))
1413 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1414 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1415 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1417 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1421 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1423 /* Next configured feature, probably ip4-lookup */
1426 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1428 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1430 next0 = SNAT_IN2OUT_NEXT_DROP;
1432 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1434 vlib_increment_simple_counter (is_slow_path ? &sm->
1435 counters.slowpath.in2out.
1436 other : &sm->counters.fastpath.
1437 in2out.other, thread_index,
1442 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1444 next0 = icmp_in2out_slow_path
1445 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1446 next0, now, thread_index, &s0);
1447 vlib_increment_simple_counter (is_slow_path ? &sm->
1448 counters.slowpath.in2out.
1449 icmp : &sm->counters.fastpath.
1450 in2out.icmp, thread_index,
1457 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1459 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1463 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1465 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1470 init_nat_k (&kv0, ip0->src_address,
1471 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1474 if (clib_bihash_search_8_8
1475 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1479 if (is_output_feature)
1482 (nat_not_translate_output_feature
1484 vnet_buffer (b0)->ip.reass.l4_src_port,
1485 vnet_buffer (b0)->ip.reass.l4_dst_port,
1486 thread_index, sw_if_index0)))
1490 * Send DHCP packets to the ipv4 stack, or we won't
1491 * be able to use dhcp client on the outside interface
1494 (proto0 == NAT_PROTOCOL_UDP
1495 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1496 clib_host_to_net_u16
1497 (UDP_DST_PORT_dhcp_to_server))
1498 && ip0->dst_address.as_u32 == 0xffffffff))
1505 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1511 slow_path (sm, b0, ip0, ip0->src_address,
1512 vnet_buffer (b0)->ip.reass.l4_src_port,
1513 rx_fib_index0, proto0, &s0, node, next0,
1516 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1519 if (PREDICT_FALSE (!s0))
1524 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1530 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1533 b0->flags |= VNET_BUFFER_F_IS_NATED;
1535 old_addr0 = ip0->src_address.as_u32;
1536 ip0->src_address = s0->out2in.addr;
1537 new_addr0 = ip0->src_address.as_u32;
1538 if (!is_output_feature)
1539 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1541 sum0 = ip0->checksum;
1542 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1543 ip4_header_t, src_address /* changed member */ );
1544 ip0->checksum = ip_csum_fold (sum0);
1546 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1548 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1550 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1551 new_port0 = udp0->src_port = s0->out2in.port;
1552 sum0 = tcp0->checksum;
1554 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1555 dst_address /* changed member */ );
1557 ip_csum_update (sum0, old_port0, new_port0,
1558 ip4_header_t /* cheat */ ,
1559 length /* changed member */ );
1560 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1561 tcp0->checksum = ip_csum_fold (sum0);
1563 vlib_increment_simple_counter (is_slow_path ? &sm->
1564 counters.slowpath.in2out.tcp : &sm->
1565 counters.fastpath.in2out.tcp,
1566 thread_index, sw_if_index0, 1);
1570 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1572 udp0->src_port = s0->out2in.port;
1573 if (PREDICT_FALSE (udp0->checksum))
1575 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1576 new_port0 = udp0->src_port;
1577 sum0 = udp0->checksum;
1579 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1580 dst_address /* changed member */ );
1582 ip_csum_update (sum0, old_port0, new_port0,
1583 ip4_header_t /* cheat */ ,
1584 length /* changed member */ );
1585 udp0->checksum = ip_csum_fold (sum0);
1588 vlib_increment_simple_counter (is_slow_path ? &sm->
1589 counters.slowpath.in2out.udp : &sm->
1590 counters.fastpath.in2out.udp,
1591 thread_index, sw_if_index0, 1);
1595 nat44_session_update_counters (s0, now,
1596 vlib_buffer_length_in_chain (vm, b0),
1598 /* Per-user LRU list maintenance */
1599 nat44_session_update_lru (sm, s0, thread_index);
1602 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1603 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1605 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1606 t->is_slow_path = is_slow_path;
1607 t->sw_if_index = sw_if_index0;
1608 t->next_index = next0;
1609 t->session_index = ~0;
1612 s0 - sm->per_thread_data[thread_index].sessions;
1615 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1617 vlib_increment_simple_counter (is_slow_path ? &sm->
1618 counters.slowpath.in2out.
1619 drops : &sm->counters.fastpath.
1620 in2out.drops, thread_index,
1629 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1631 return frame->n_vectors;
1634 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1635 vlib_node_runtime_t * node,
1636 vlib_frame_t * frame)
1638 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1643 VLIB_REGISTER_NODE (snat_in2out_node) = {
1644 .name = "nat44-in2out",
1645 .vector_size = sizeof (u32),
1646 .format_trace = format_snat_in2out_trace,
1647 .type = VLIB_NODE_TYPE_INTERNAL,
1649 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1650 .error_strings = snat_in2out_error_strings,
1652 .runtime_data_bytes = sizeof (snat_runtime_t),
1654 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1656 /* edit / add dispositions here */
1658 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1659 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1660 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1661 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1666 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1667 vlib_node_runtime_t * node,
1668 vlib_frame_t * frame)
1670 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1675 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1676 .name = "nat44-in2out-output",
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",
1698 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1699 vlib_node_runtime_t * node,
1700 vlib_frame_t * frame)
1702 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1707 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1708 .name = "nat44-in2out-slowpath",
1709 .vector_size = sizeof (u32),
1710 .format_trace = format_snat_in2out_trace,
1711 .type = VLIB_NODE_TYPE_INTERNAL,
1713 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1714 .error_strings = snat_in2out_error_strings,
1716 .runtime_data_bytes = sizeof (snat_runtime_t),
1718 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1720 /* edit / add dispositions here */
1722 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1723 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1724 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1725 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1730 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1731 vlib_node_runtime_t * node,
1732 vlib_frame_t * frame)
1734 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1739 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1740 .name = "nat44-in2out-output-slowpath",
1741 .vector_size = sizeof (u32),
1742 .format_trace = format_snat_in2out_trace,
1743 .type = VLIB_NODE_TYPE_INTERNAL,
1745 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1746 .error_strings = snat_in2out_error_strings,
1748 .runtime_data_bytes = sizeof (snat_runtime_t),
1750 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1752 /* edit / add dispositions here */
1754 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1755 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1756 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1757 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1762 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1763 vlib_node_runtime_t * node,
1764 vlib_frame_t * frame)
1766 u32 n_left_from, *from, *to_next;
1767 snat_in2out_next_t next_index;
1768 snat_main_t *sm = &snat_main;
1769 int is_hairpinning = 0;
1771 from = vlib_frame_vector_args (frame);
1772 n_left_from = frame->n_vectors;
1773 next_index = node->cached_next_index;
1775 while (n_left_from > 0)
1779 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1781 while (n_left_from > 0 && n_left_to_next > 0)
1789 u32 new_addr0, old_addr0;
1790 u16 old_port0, new_port0;
1793 icmp46_header_t *icmp0;
1796 ip4_address_t sm0_addr;
1801 /* speculatively enqueue b0 to the current next frame */
1807 n_left_to_next -= 1;
1809 b0 = vlib_get_buffer (vm, bi0);
1810 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1812 ip0 = vlib_buffer_get_current (b0);
1813 udp0 = ip4_next_header (ip0);
1814 tcp0 = (tcp_header_t *) udp0;
1815 icmp0 = (icmp46_header_t *) udp0;
1817 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1819 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1821 if (PREDICT_FALSE (ip0->ttl == 1))
1823 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1824 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1825 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1827 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1831 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1833 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1836 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1838 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1839 rx_fib_index0, node, next0, ~0, 0, 0);
1843 if (snat_static_mapping_match
1844 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1845 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1847 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1848 next0 = SNAT_IN2OUT_NEXT_DROP;
1852 new_addr0 = sm0_addr.as_u32;
1853 new_port0 = sm0_port;
1854 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1855 old_addr0 = ip0->src_address.as_u32;
1856 ip0->src_address.as_u32 = new_addr0;
1858 sum0 = ip0->checksum;
1859 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1861 src_address /* changed member */ );
1862 ip0->checksum = ip_csum_fold (sum0);
1864 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1866 old_port0 = udp0->src_port;
1867 udp0->src_port = new_port0;
1869 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1871 sum0 = tcp0->checksum;
1872 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1874 dst_address /* changed member */ );
1875 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1876 ip4_header_t /* cheat */ ,
1877 length /* changed member */ );
1878 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1879 tcp0->checksum = ip_csum_fold (sum0);
1881 else if (udp0->checksum)
1883 sum0 = udp0->checksum;
1884 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1886 dst_address /* changed member */ );
1887 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1888 ip4_header_t /* cheat */ ,
1889 length /* changed member */ );
1890 udp0->checksum = ip_csum_fold (sum0);
1895 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1897 sum0 = tcp0->checksum;
1898 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1900 dst_address /* changed member */ );
1901 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1902 tcp0->checksum = ip_csum_fold (sum0);
1904 else if (udp0->checksum)
1906 sum0 = udp0->checksum;
1907 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1909 dst_address /* changed member */ );
1910 udp0->checksum = ip_csum_fold (sum0);
1915 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1920 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1921 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1923 snat_in2out_trace_t *t =
1924 vlib_add_trace (vm, node, b0, sizeof (*t));
1925 t->sw_if_index = sw_if_index0;
1926 t->next_index = next0;
1927 t->is_hairpinning = is_hairpinning;
1930 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1933 vlib_increment_simple_counter (&sm->counters.fastpath.
1934 in2out.other, sw_if_index0,
1935 vm->thread_index, 1);
1938 /* verify speculative enqueue, maybe switch current next frame */
1939 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1940 to_next, n_left_to_next,
1944 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1947 return frame->n_vectors;
1952 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1953 .name = "nat44-in2out-fast",
1954 .vector_size = sizeof (u32),
1955 .format_trace = format_snat_in2out_fast_trace,
1956 .type = VLIB_NODE_TYPE_INTERNAL,
1958 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1959 .error_strings = snat_in2out_error_strings,
1961 .runtime_data_bytes = sizeof (snat_runtime_t),
1963 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1965 /* edit / add dispositions here */
1967 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1968 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1969 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1970 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1976 * fd.io coding-style-patch-verification: ON
1979 * eval: (c-set-style "gnu")