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>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp_local.h>
28 #include <nat/lib/ipfix_logging.h>
29 #include <nat/nat_inlines.h>
30 #include <nat/lib/nat_syslog.h>
31 #include <nat/nat44-ei/nat44_ei_inlines.h>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
36 #include <nat/lib/nat_inlines.h>
45 } snat_in2out_trace_t;
47 /* packet trace format function */
49 format_snat_in2out_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
56 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
58 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
59 t->sw_if_index, t->next_index, t->session_index);
60 if (t->is_hairpinning)
62 s = format (s, ", with-hairpinning");
69 format_snat_in2out_fast_trace (u8 * s, va_list * args)
71 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
75 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
76 t->sw_if_index, t->next_index);
81 #define foreach_snat_in2out_error \
82 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
83 _(OUT_OF_PORTS, "out of ports") \
84 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
85 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
86 _(NO_TRANSLATION, "no translation") \
87 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
88 _(CANNOT_CREATE_USER, "cannot create NAT user")
92 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
93 foreach_snat_in2out_error
96 } snat_in2out_error_t;
98 static char *snat_in2out_error_strings[] = {
99 #define _(sym,string) string,
100 foreach_snat_in2out_error
106 SNAT_IN2OUT_NEXT_LOOKUP,
107 SNAT_IN2OUT_NEXT_DROP,
108 SNAT_IN2OUT_NEXT_ICMP_ERROR,
109 SNAT_IN2OUT_NEXT_SLOW_PATH,
111 } snat_in2out_next_t;
114 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
115 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
116 u32 rx_fib_index0, u32 thread_index)
118 udp_header_t *udp0 = ip4_next_header (ip0);
119 clib_bihash_kv_8_8_t kv0, value0;
121 init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
124 /* NAT packet aimed at external address if */
125 /* has active sessions */
126 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
129 /* or is static mappings */
130 ip4_address_t placeholder_addr;
131 u16 placeholder_port;
132 u32 placeholder_fib_index;
133 if (!snat_static_mapping_match
134 (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
135 proto0, &placeholder_addr, &placeholder_port,
136 &placeholder_fib_index, 1, 0, 0, 0, 0, 0, 0))
142 if (sm->forwarding_enabled)
145 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
150 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
151 u32 proto0, u16 src_port, u16 dst_port,
152 u32 thread_index, u32 sw_if_index)
154 clib_bihash_kv_8_8_t kv0, value0;
158 init_nat_k (&kv0, ip0->src_address, src_port,
159 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
161 if (!clib_bihash_search_8_8
162 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
166 init_nat_k (&kv0, ip0->dst_address, dst_port,
167 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
168 if (!clib_bihash_search_8_8
169 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
173 pool_foreach (i, sm->output_feature_interfaces)
175 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
185 #ifndef CLIB_MARCH_VARIANT
187 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
189 snat_main_t *sm = &snat_main;
190 nat44_is_idle_session_ctx_t *ctx = arg;
192 u64 sess_timeout_time;
193 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
195 clib_bihash_kv_8_8_t s_kv;
197 s = pool_elt_at_index (tsm->sessions, kv->value);
198 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
199 if (ctx->now >= sess_timeout_time)
201 init_nat_o2i_k (&s_kv, s);
202 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
203 nat_elog_warn ("out2in key del failed");
205 nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
206 s->in2out.addr.as_u32,
207 s->out2in.addr.as_u32,
211 s->in2out.fib_index);
213 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
214 &s->in2out.addr, s->in2out.port,
215 &s->out2in.addr, s->out2in.port, s->nat_proto);
217 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
218 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
221 if (!snat_is_session_static (s))
222 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
224 s->out2in.port, s->nat_proto);
226 nat44_delete_session (sm, s, ctx->thread_index);
235 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
237 ip4_address_t i2o_addr,
240 nat_protocol_t nat_proto,
241 snat_session_t ** sessionp,
242 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
245 snat_session_t *s = 0;
246 clib_bihash_kv_8_8_t kv0;
248 nat_outside_fib_t *outside_fib;
249 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
252 .fp_proto = FIB_PROTOCOL_IP4,
255 .ip4.as_u32 = ip0->dst_address.as_u32,
258 nat44_is_idle_session_ctx_t ctx0;
259 ip4_address_t sm_addr;
263 if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
265 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
266 nat_ipfix_logging_max_sessions (thread_index,
267 sm->max_translations_per_thread);
268 nat_elog_notice ("maximum sessions exceeded");
269 return SNAT_IN2OUT_NEXT_DROP;
272 /* First try to match static mapping by local address and port */
273 if (snat_static_mapping_match
274 (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
275 &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat, 0))
277 /* Try to create dynamic translation */
278 if (sm->alloc_addr_and_port (
279 sm->addresses, rx_fib_index0, thread_index, nat_proto, &sm_addr,
280 &sm_port, sm->port_per_thread,
281 sm->per_thread_data[thread_index].snat_thread_index))
283 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
284 return SNAT_IN2OUT_NEXT_DROP;
289 if (PREDICT_FALSE (identity_nat))
298 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
302 b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
303 return SNAT_IN2OUT_NEXT_DROP;
306 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
309 nat44_delete_user_with_no_session (sm, u, thread_index);
310 nat_elog_warn ("create NAT session failed");
311 return SNAT_IN2OUT_NEXT_DROP;
315 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
316 user_session_increment (sm, u, is_sm);
317 s->in2out.addr = i2o_addr;
318 s->in2out.port = i2o_port;
319 s->in2out.fib_index = rx_fib_index0;
320 s->nat_proto = nat_proto;
321 s->out2in.addr = sm_addr;
322 s->out2in.port = sm_port;
323 s->out2in.fib_index = sm->outside_fib_index;
324 switch (vec_len (sm->outside_fibs))
327 s->out2in.fib_index = sm->outside_fib_index;
330 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
334 vec_foreach (outside_fib, sm->outside_fibs)
336 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
337 if (FIB_NODE_INDEX_INVALID != fei)
339 if (fib_entry_get_resolving_interface (fei) != ~0)
341 s->out2in.fib_index = outside_fib->fib_index;
349 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
350 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
353 /* Add to translation hashes */
355 ctx0.thread_index = thread_index;
356 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
357 if (clib_bihash_add_or_overwrite_stale_8_8
358 (&sm->per_thread_data[thread_index].in2out, &kv0,
359 nat44_i2o_is_idle_session_cb, &ctx0))
360 nat_elog_notice ("in2out key add failed");
362 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
363 if (clib_bihash_add_or_overwrite_stale_8_8
364 (&sm->per_thread_data[thread_index].out2in, &kv0,
365 nat44_o2i_is_idle_session_cb, &ctx0))
366 nat_elog_notice ("out2in key add failed");
369 nat_ipfix_logging_nat44_ses_create (thread_index,
370 s->in2out.addr.as_u32,
371 s->out2in.addr.as_u32,
374 s->out2in.port, s->in2out.fib_index);
376 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
377 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
378 s->out2in.port, s->nat_proto);
380 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
381 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
382 &s->ext_host_nat_addr, s->ext_host_nat_port,
383 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
388 #ifndef CLIB_MARCH_VARIANT
389 static_always_inline snat_in2out_error_t
390 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
391 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
393 icmp46_header_t *icmp0;
394 icmp_echo_header_t *echo0, *inner_echo0 = 0;
395 ip4_header_t *inner_ip0 = 0;
397 icmp46_header_t *inner_icmp0;
399 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
400 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
402 if (!icmp_type_is_error_message
403 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
405 *nat_proto = NAT_PROTOCOL_ICMP;
406 *addr = ip0->src_address;
407 *port = vnet_buffer (b)->ip.reass.l4_src_port;
411 inner_ip0 = (ip4_header_t *) (echo0 + 1);
412 l4_header = ip4_next_header (inner_ip0);
413 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
414 *addr = inner_ip0->dst_address;
417 case NAT_PROTOCOL_ICMP:
418 inner_icmp0 = (icmp46_header_t *) l4_header;
419 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
420 *port = inner_echo0->identifier;
422 case NAT_PROTOCOL_UDP:
423 case NAT_PROTOCOL_TCP:
424 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
427 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
430 return -1; /* success */
434 * Get address and port values to be used for ICMP packet translation
435 * and create session if needed
437 * @param[in,out] sm NAT main
438 * @param[in,out] node NAT node runtime
439 * @param[in] thread_index thread index
440 * @param[in,out] b0 buffer containing packet to be translated
441 * @param[in,out] ip0 ip header
442 * @param[out] p_proto protocol used for matching
443 * @param[out] p_value address and port after NAT translation
444 * @param[out] p_dont_translate if packet should not be translated
445 * @param d optional parameter
446 * @param e optional parameter
449 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
450 u32 thread_index, vlib_buffer_t * b0,
451 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
452 u32 * fib_index, nat_protocol_t * proto, void *d,
453 void *e, u8 * dont_translate)
455 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
457 snat_session_t *s0 = 0;
458 clib_bihash_kv_8_8_t kv0, value0;
461 vlib_main_t *vm = vlib_get_main ();
464 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
465 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
467 err = icmp_get_key (b0, ip0, addr, port, proto);
470 b0->error = node->errors[err];
471 next0 = SNAT_IN2OUT_NEXT_DROP;
475 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
476 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
478 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
481 (nat_not_translate_output_feature
482 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
490 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
491 ip0, NAT_PROTOCOL_ICMP,
492 *fib_index, thread_index)))
500 (icmp_type_is_error_message
501 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
503 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
504 next0 = SNAT_IN2OUT_NEXT_DROP;
509 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
510 next0, thread_index, vlib_time_now (vm));
512 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
524 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
526 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
528 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
529 reass.icmp_type_or_tcp_flags)))
531 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
532 next0 = SNAT_IN2OUT_NEXT_DROP;
536 s0 = pool_elt_at_index (tsm->sessions, value0.value);
542 *addr = s0->out2in.addr;
543 *port = s0->out2in.port;
544 *fib_index = s0->out2in.fib_index;
547 *(snat_session_t **) (d) = s0;
552 #ifndef CLIB_MARCH_VARIANT
554 * Get address and port values to be used for ICMP packet translation
556 * @param[in] sm NAT main
557 * @param[in,out] node NAT node runtime
558 * @param[in] thread_index thread index
559 * @param[in,out] b0 buffer containing packet to be translated
560 * @param[in,out] ip0 ip header
561 * @param[out] p_proto protocol used for matching
562 * @param[out] p_value address and port after NAT translation
563 * @param[out] p_dont_translate if packet should not be translated
564 * @param d optional parameter
565 * @param e optional parameter
568 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
569 u32 thread_index, vlib_buffer_t * b0,
570 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
571 u32 * fib_index, nat_protocol_t * proto, void *d,
572 void *e, u8 * dont_translate)
580 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
581 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
583 err = icmp_get_key (b0, ip0, addr, port, proto);
586 b0->error = node->errors[err];
587 next0 = SNAT_IN2OUT_NEXT_DROP;
591 ip4_address_t sm_addr;
595 if (snat_static_mapping_match
596 (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
597 &sm_fib_index, 0, &is_addr_only, 0, 0, 0, 0, 0))
599 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
607 if (icmp_type_is_error_message
608 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
610 next0 = SNAT_IN2OUT_NEXT_DROP;
614 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
615 next0 = SNAT_IN2OUT_NEXT_DROP;
620 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
621 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
622 ICMP4_echo_reply || !is_addr_only)
623 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
624 reass.icmp_type_or_tcp_flags)))
626 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
627 next0 = SNAT_IN2OUT_NEXT_DROP;
636 #ifndef CLIB_MARCH_VARIANT
638 icmp_in2out (snat_main_t * sm,
641 icmp46_header_t * icmp0,
644 vlib_node_runtime_t * node,
645 u32 next0, u32 thread_index, void *d, void *e)
647 vlib_main_t *vm = vlib_get_main ();
651 nat_protocol_t protocol;
652 icmp_echo_header_t *echo0, *inner_echo0 = 0;
653 ip4_header_t *inner_ip0;
655 icmp46_header_t *inner_icmp0;
657 u32 new_addr0, old_addr0;
658 u16 old_id0, new_id0;
659 u16 old_checksum0, new_checksum0;
664 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
667 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
668 &fib_index, &protocol, d, e, &dont_translate);
671 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
674 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
677 ip_incremental_checksum_buffer (vm, b0,
679 (u8 *) vlib_buffer_get_current (b0),
680 ntohs (ip0->length) -
681 ip4_header_bytes (ip0), 0);
682 checksum0 = ~ip_csum_fold (sum0);
683 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
685 next0 = SNAT_IN2OUT_NEXT_DROP;
690 old_addr0 = ip0->src_address.as_u32;
691 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
693 sum0 = ip0->checksum;
694 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
695 src_address /* changed member */ );
696 ip0->checksum = ip_csum_fold (sum0);
698 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
700 if (icmp0->checksum == 0)
701 icmp0->checksum = 0xffff;
703 if (!icmp_type_is_error_message (icmp0->type))
706 if (PREDICT_FALSE (new_id0 != echo0->identifier))
708 old_id0 = echo0->identifier;
710 echo0->identifier = new_id0;
712 sum0 = icmp0->checksum;
714 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
716 icmp0->checksum = ip_csum_fold (sum0);
721 inner_ip0 = (ip4_header_t *) (echo0 + 1);
722 l4_header = ip4_next_header (inner_ip0);
724 if (!ip4_header_checksum_is_valid (inner_ip0))
726 next0 = SNAT_IN2OUT_NEXT_DROP;
730 /* update inner destination IP address */
731 old_addr0 = inner_ip0->dst_address.as_u32;
732 inner_ip0->dst_address = addr;
733 new_addr0 = inner_ip0->dst_address.as_u32;
734 sum0 = icmp0->checksum;
735 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
736 dst_address /* changed member */ );
737 icmp0->checksum = ip_csum_fold (sum0);
739 /* update inner IP header checksum */
740 old_checksum0 = inner_ip0->checksum;
741 sum0 = inner_ip0->checksum;
742 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
743 dst_address /* changed member */ );
744 inner_ip0->checksum = ip_csum_fold (sum0);
745 new_checksum0 = inner_ip0->checksum;
746 sum0 = icmp0->checksum;
748 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
750 icmp0->checksum = ip_csum_fold (sum0);
754 case NAT_PROTOCOL_ICMP:
755 inner_icmp0 = (icmp46_header_t *) l4_header;
756 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
758 old_id0 = inner_echo0->identifier;
760 inner_echo0->identifier = new_id0;
762 sum0 = icmp0->checksum;
764 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
766 icmp0->checksum = ip_csum_fold (sum0);
768 case NAT_PROTOCOL_UDP:
769 case NAT_PROTOCOL_TCP:
770 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
772 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
774 sum0 = icmp0->checksum;
775 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
777 icmp0->checksum = ip_csum_fold (sum0);
785 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
787 if (0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
788 sm->endpoint_dependent))
789 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
798 icmp_in2out_slow_path (snat_main_t * sm,
801 icmp46_header_t * icmp0,
804 vlib_node_runtime_t * node,
806 f64 now, u32 thread_index, snat_session_t ** p_s0)
808 vlib_main_t *vm = vlib_get_main ();
810 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
811 next0, thread_index, p_s0, 0);
812 snat_session_t *s0 = *p_s0;
813 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
816 nat44_ei_session_update_counters (
817 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
818 /* Per-user LRU list maintenance */
819 nat44_session_update_lru (sm, s0, thread_index);
825 nat_in2out_sm_unknown_proto (snat_main_t * sm,
827 ip4_header_t * ip, u32 rx_fib_index)
829 clib_bihash_kv_8_8_t kv, value;
830 snat_static_mapping_t *m;
831 u32 old_addr, new_addr;
834 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
835 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
838 m = pool_elt_at_index (sm->static_mappings, value.value);
840 old_addr = ip->src_address.as_u32;
841 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
843 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
844 ip->checksum = ip_csum_fold (sum);
848 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
850 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
851 nat_hairpinning_sm_unknown_proto (sm, b, ip);
858 snat_in2out_node_fn_inline (vlib_main_t * vm,
859 vlib_node_runtime_t * node,
860 vlib_frame_t * frame, int is_slow_path,
861 int is_output_feature)
863 u32 n_left_from, *from;
864 snat_main_t *sm = &snat_main;
865 f64 now = vlib_time_now (vm);
866 u32 thread_index = vm->thread_index;
868 from = vlib_frame_vector_args (frame);
869 n_left_from = frame->n_vectors;
871 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
872 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
873 vlib_get_buffers (vm, from, b, n_left_from);
875 while (n_left_from >= 2)
877 vlib_buffer_t *b0, *b1;
879 u32 sw_if_index0, sw_if_index1;
880 ip4_header_t *ip0, *ip1;
881 ip_csum_t sum0, sum1;
882 u32 new_addr0, old_addr0, new_addr1, old_addr1;
883 u16 old_port0, new_port0, old_port1, new_port1;
884 udp_header_t *udp0, *udp1;
885 tcp_header_t *tcp0, *tcp1;
886 icmp46_header_t *icmp0, *icmp1;
887 u32 rx_fib_index0, rx_fib_index1;
889 snat_session_t *s0 = 0, *s1 = 0;
890 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
891 u32 iph_offset0 = 0, iph_offset1 = 0;
898 /* Prefetch next iteration. */
899 if (PREDICT_TRUE (n_left_from >= 4))
901 vlib_buffer_t *p2, *p3;
906 vlib_prefetch_buffer_header (p2, LOAD);
907 vlib_prefetch_buffer_header (p3, LOAD);
909 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
910 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
913 if (is_output_feature)
914 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
916 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
919 udp0 = ip4_next_header (ip0);
920 tcp0 = (tcp_header_t *) udp0;
921 icmp0 = (icmp46_header_t *) udp0;
923 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
924 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
927 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
929 if (PREDICT_FALSE (ip0->ttl == 1))
931 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
932 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
933 ICMP4_time_exceeded_ttl_exceeded_in_transit,
935 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
939 proto0 = ip_proto_to_nat_proto (ip0->protocol);
941 /* Next configured feature, probably ip4-lookup */
944 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
946 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
948 next0 = SNAT_IN2OUT_NEXT_DROP;
950 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
952 vlib_increment_simple_counter (is_slow_path ? &sm->
953 counters.slowpath.in2out.
954 other : &sm->counters.fastpath.
955 in2out.other, thread_index,
960 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
962 next0 = icmp_in2out_slow_path
963 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
964 node, next0, now, thread_index, &s0);
965 vlib_increment_simple_counter (is_slow_path ? &sm->
966 counters.slowpath.in2out.
967 icmp : &sm->counters.fastpath.
968 in2out.icmp, thread_index,
975 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
977 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
981 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
983 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
988 init_nat_k (&kv0, ip0->src_address,
989 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
992 (clib_bihash_search_8_8
993 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
997 if (is_output_feature)
1000 (nat_not_translate_output_feature
1002 vnet_buffer (b0)->ip.reass.l4_src_port,
1003 vnet_buffer (b0)->ip.reass.l4_dst_port,
1004 thread_index, sw_if_index0)))
1008 * Send DHCP packets to the ipv4 stack, or we won't
1009 * be able to use dhcp client on the outside interface
1012 (proto0 == NAT_PROTOCOL_UDP
1013 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1014 clib_host_to_net_u16
1015 (UDP_DST_PORT_dhcp_to_server))
1016 && ip0->dst_address.as_u32 == 0xffffffff))
1023 (sm, node, sw_if_index0, ip0, proto0,
1024 rx_fib_index0, thread_index)))
1028 next0 = slow_path (sm, b0, ip0,
1030 vnet_buffer (b0)->ip.reass.l4_src_port,
1032 proto0, &s0, node, next0, thread_index, now);
1033 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1036 if (PREDICT_FALSE (!s0))
1041 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1047 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1050 b0->flags |= VNET_BUFFER_F_IS_NATED;
1052 old_addr0 = ip0->src_address.as_u32;
1053 ip0->src_address = s0->out2in.addr;
1054 new_addr0 = ip0->src_address.as_u32;
1055 if (!is_output_feature)
1056 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1058 sum0 = ip0->checksum;
1059 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1060 ip4_header_t, src_address /* changed member */ );
1061 ip0->checksum = ip_csum_fold (sum0);
1064 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1066 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1068 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1069 new_port0 = udp0->src_port = s0->out2in.port;
1070 sum0 = tcp0->checksum;
1071 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1073 dst_address /* changed member */ );
1074 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1075 ip4_header_t /* cheat */ ,
1076 length /* changed member */ );
1077 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1078 tcp0->checksum = ip_csum_fold (sum0);
1080 vlib_increment_simple_counter (is_slow_path ? &sm->
1081 counters.slowpath.in2out.tcp : &sm->
1082 counters.fastpath.in2out.tcp,
1083 thread_index, sw_if_index0, 1);
1087 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1089 udp0->src_port = s0->out2in.port;
1090 if (PREDICT_FALSE (udp0->checksum))
1092 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1093 new_port0 = udp0->src_port;
1094 sum0 = udp0->checksum;
1095 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1098 ip_csum_update (sum0, old_port0, new_port0,
1099 ip4_header_t /* cheat */ ,
1100 length /* changed member */ );
1101 udp0->checksum = ip_csum_fold (sum0);
1104 vlib_increment_simple_counter (is_slow_path ? &sm->
1105 counters.slowpath.in2out.udp : &sm->
1106 counters.fastpath.in2out.udp,
1107 thread_index, sw_if_index0, 1);
1111 nat44_ei_session_update_counters (
1112 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1113 /* Per-user LRU list maintenance */
1114 nat44_session_update_lru (sm, s0, thread_index);
1117 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1118 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1120 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1121 t->is_slow_path = is_slow_path;
1122 t->sw_if_index = sw_if_index0;
1123 t->next_index = next0;
1124 t->session_index = ~0;
1127 s0 - sm->per_thread_data[thread_index].sessions;
1130 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1132 vlib_increment_simple_counter (is_slow_path ? &sm->
1133 counters.slowpath.in2out.
1134 drops : &sm->counters.fastpath.
1135 in2out.drops, thread_index,
1139 if (is_output_feature)
1140 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1142 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1145 udp1 = ip4_next_header (ip1);
1146 tcp1 = (tcp_header_t *) udp1;
1147 icmp1 = (icmp46_header_t *) udp1;
1149 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1150 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1153 if (PREDICT_FALSE (ip1->ttl == 1))
1155 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1156 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1157 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1159 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1163 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1165 /* Next configured feature, probably ip4-lookup */
1168 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1170 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1172 next1 = SNAT_IN2OUT_NEXT_DROP;
1174 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1176 vlib_increment_simple_counter (is_slow_path ? &sm->
1177 counters.slowpath.in2out.
1178 other : &sm->counters.fastpath.
1179 in2out.other, thread_index,
1184 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1186 next1 = icmp_in2out_slow_path
1187 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1188 next1, now, thread_index, &s1);
1189 vlib_increment_simple_counter (is_slow_path ? &sm->
1190 counters.slowpath.in2out.
1191 icmp : &sm->counters.fastpath.
1192 in2out.icmp, thread_index,
1199 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1201 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1205 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1207 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1212 init_nat_k (&kv1, ip1->src_address,
1213 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1216 (clib_bihash_search_8_8
1217 (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1221 if (is_output_feature)
1224 (nat_not_translate_output_feature
1226 vnet_buffer (b1)->ip.reass.l4_src_port,
1227 vnet_buffer (b1)->ip.reass.l4_dst_port,
1228 thread_index, sw_if_index1)))
1232 * Send DHCP packets to the ipv4 stack, or we won't
1233 * be able to use dhcp client on the outside interface
1236 (proto1 == NAT_PROTOCOL_UDP
1237 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1238 clib_host_to_net_u16
1239 (UDP_DST_PORT_dhcp_to_server))
1240 && ip1->dst_address.as_u32 == 0xffffffff))
1247 (sm, node, sw_if_index1, ip1, proto1,
1248 rx_fib_index1, thread_index)))
1253 slow_path (sm, b1, ip1, ip1->src_address,
1254 vnet_buffer (b1)->ip.reass.l4_src_port,
1255 rx_fib_index1, proto1, &s1, node, next1,
1257 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1260 if (PREDICT_FALSE (!s1))
1265 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1271 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1274 b1->flags |= VNET_BUFFER_F_IS_NATED;
1276 old_addr1 = ip1->src_address.as_u32;
1277 ip1->src_address = s1->out2in.addr;
1278 new_addr1 = ip1->src_address.as_u32;
1279 if (!is_output_feature)
1280 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1282 sum1 = ip1->checksum;
1283 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1284 ip4_header_t, src_address /* changed member */ );
1285 ip1->checksum = ip_csum_fold (sum1);
1287 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1289 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1291 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1292 new_port1 = udp1->src_port = s1->out2in.port;
1293 sum1 = tcp1->checksum;
1294 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1296 dst_address /* changed member */ );
1297 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1298 ip4_header_t /* cheat */ ,
1299 length /* changed member */ );
1300 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1301 tcp1->checksum = ip_csum_fold (sum1);
1303 vlib_increment_simple_counter (is_slow_path ? &sm->
1304 counters.slowpath.in2out.tcp : &sm->
1305 counters.fastpath.in2out.tcp,
1306 thread_index, sw_if_index1, 1);
1310 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1312 udp1->src_port = s1->out2in.port;
1313 if (PREDICT_FALSE (udp1->checksum))
1315 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1316 new_port1 = udp1->src_port;
1317 sum1 = udp1->checksum;
1318 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1321 ip_csum_update (sum1, old_port1, new_port1,
1322 ip4_header_t /* cheat */ ,
1323 length /* changed member */ );
1324 udp1->checksum = ip_csum_fold (sum1);
1327 vlib_increment_simple_counter (is_slow_path ? &sm->
1328 counters.slowpath.in2out.udp : &sm->
1329 counters.fastpath.in2out.udp,
1330 thread_index, sw_if_index1, 1);
1334 nat44_ei_session_update_counters (
1335 s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1336 /* Per-user LRU list maintenance */
1337 nat44_session_update_lru (sm, s1, thread_index);
1340 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1341 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1343 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1344 t->sw_if_index = sw_if_index1;
1345 t->next_index = next1;
1346 t->session_index = ~0;
1349 s1 - sm->per_thread_data[thread_index].sessions;
1352 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1354 vlib_increment_simple_counter (is_slow_path ? &sm->
1355 counters.slowpath.in2out.
1356 drops : &sm->counters.fastpath.
1357 in2out.drops, thread_index,
1367 while (n_left_from > 0)
1374 u32 new_addr0, old_addr0;
1375 u16 old_port0, new_port0;
1378 icmp46_header_t *icmp0;
1381 snat_session_t *s0 = 0;
1382 clib_bihash_kv_8_8_t kv0, value0;
1383 u32 iph_offset0 = 0;
1387 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1389 if (is_output_feature)
1390 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1392 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1395 udp0 = ip4_next_header (ip0);
1396 tcp0 = (tcp_header_t *) udp0;
1397 icmp0 = (icmp46_header_t *) udp0;
1399 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1400 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1403 if (PREDICT_FALSE (ip0->ttl == 1))
1405 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1406 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1407 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1409 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1413 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1415 /* Next configured feature, probably ip4-lookup */
1418 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1420 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1422 next0 = SNAT_IN2OUT_NEXT_DROP;
1424 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1426 vlib_increment_simple_counter (is_slow_path ? &sm->
1427 counters.slowpath.in2out.
1428 other : &sm->counters.fastpath.
1429 in2out.other, thread_index,
1434 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1436 next0 = icmp_in2out_slow_path
1437 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1438 next0, now, thread_index, &s0);
1439 vlib_increment_simple_counter (is_slow_path ? &sm->
1440 counters.slowpath.in2out.
1441 icmp : &sm->counters.fastpath.
1442 in2out.icmp, thread_index,
1449 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1451 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1455 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1457 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1462 init_nat_k (&kv0, ip0->src_address,
1463 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1466 if (clib_bihash_search_8_8
1467 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1471 if (is_output_feature)
1474 (nat_not_translate_output_feature
1476 vnet_buffer (b0)->ip.reass.l4_src_port,
1477 vnet_buffer (b0)->ip.reass.l4_dst_port,
1478 thread_index, sw_if_index0)))
1482 * Send DHCP packets to the ipv4 stack, or we won't
1483 * be able to use dhcp client on the outside interface
1486 (proto0 == NAT_PROTOCOL_UDP
1487 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1488 clib_host_to_net_u16
1489 (UDP_DST_PORT_dhcp_to_server))
1490 && ip0->dst_address.as_u32 == 0xffffffff))
1497 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1503 slow_path (sm, b0, ip0, ip0->src_address,
1504 vnet_buffer (b0)->ip.reass.l4_src_port,
1505 rx_fib_index0, proto0, &s0, node, next0,
1508 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1511 if (PREDICT_FALSE (!s0))
1516 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1522 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1525 b0->flags |= VNET_BUFFER_F_IS_NATED;
1527 old_addr0 = ip0->src_address.as_u32;
1528 ip0->src_address = s0->out2in.addr;
1529 new_addr0 = ip0->src_address.as_u32;
1530 if (!is_output_feature)
1531 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1533 sum0 = ip0->checksum;
1534 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1535 ip4_header_t, src_address /* changed member */ );
1536 ip0->checksum = ip_csum_fold (sum0);
1538 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1540 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1542 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1543 new_port0 = udp0->src_port = s0->out2in.port;
1544 sum0 = tcp0->checksum;
1546 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1547 dst_address /* changed member */ );
1549 ip_csum_update (sum0, old_port0, new_port0,
1550 ip4_header_t /* cheat */ ,
1551 length /* changed member */ );
1552 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1553 tcp0->checksum = ip_csum_fold (sum0);
1555 vlib_increment_simple_counter (is_slow_path ? &sm->
1556 counters.slowpath.in2out.tcp : &sm->
1557 counters.fastpath.in2out.tcp,
1558 thread_index, sw_if_index0, 1);
1562 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1564 udp0->src_port = s0->out2in.port;
1565 if (PREDICT_FALSE (udp0->checksum))
1567 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1568 new_port0 = udp0->src_port;
1569 sum0 = udp0->checksum;
1571 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1572 dst_address /* changed member */ );
1574 ip_csum_update (sum0, old_port0, new_port0,
1575 ip4_header_t /* cheat */ ,
1576 length /* changed member */ );
1577 udp0->checksum = ip_csum_fold (sum0);
1580 vlib_increment_simple_counter (is_slow_path ? &sm->
1581 counters.slowpath.in2out.udp : &sm->
1582 counters.fastpath.in2out.udp,
1583 thread_index, sw_if_index0, 1);
1587 nat44_ei_session_update_counters (
1588 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1589 /* Per-user LRU list maintenance */
1590 nat44_session_update_lru (sm, s0, thread_index);
1593 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1594 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1596 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1597 t->is_slow_path = is_slow_path;
1598 t->sw_if_index = sw_if_index0;
1599 t->next_index = next0;
1600 t->session_index = ~0;
1603 s0 - sm->per_thread_data[thread_index].sessions;
1606 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1608 vlib_increment_simple_counter (is_slow_path ? &sm->
1609 counters.slowpath.in2out.
1610 drops : &sm->counters.fastpath.
1611 in2out.drops, thread_index,
1620 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1622 return frame->n_vectors;
1625 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1626 vlib_node_runtime_t * node,
1627 vlib_frame_t * frame)
1629 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1634 VLIB_REGISTER_NODE (snat_in2out_node) = {
1635 .name = "nat44-in2out",
1636 .vector_size = sizeof (u32),
1637 .format_trace = format_snat_in2out_trace,
1638 .type = VLIB_NODE_TYPE_INTERNAL,
1640 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1641 .error_strings = snat_in2out_error_strings,
1643 .runtime_data_bytes = sizeof (snat_runtime_t),
1645 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1647 /* edit / add dispositions here */
1649 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1650 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1651 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1652 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1657 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1658 vlib_node_runtime_t * node,
1659 vlib_frame_t * frame)
1661 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1666 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1667 .name = "nat44-in2out-output",
1668 .vector_size = sizeof (u32),
1669 .format_trace = format_snat_in2out_trace,
1670 .type = VLIB_NODE_TYPE_INTERNAL,
1672 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1673 .error_strings = snat_in2out_error_strings,
1675 .runtime_data_bytes = sizeof (snat_runtime_t),
1677 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1679 /* edit / add dispositions here */
1681 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1682 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1683 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1684 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1689 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1690 vlib_node_runtime_t * node,
1691 vlib_frame_t * frame)
1693 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1698 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1699 .name = "nat44-in2out-slowpath",
1700 .vector_size = sizeof (u32),
1701 .format_trace = format_snat_in2out_trace,
1702 .type = VLIB_NODE_TYPE_INTERNAL,
1704 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1705 .error_strings = snat_in2out_error_strings,
1707 .runtime_data_bytes = sizeof (snat_runtime_t),
1709 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1711 /* edit / add dispositions here */
1713 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1714 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1715 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1716 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1721 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1722 vlib_node_runtime_t * node,
1723 vlib_frame_t * frame)
1725 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1730 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1731 .name = "nat44-in2out-output-slowpath",
1732 .vector_size = sizeof (u32),
1733 .format_trace = format_snat_in2out_trace,
1734 .type = VLIB_NODE_TYPE_INTERNAL,
1736 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1737 .error_strings = snat_in2out_error_strings,
1739 .runtime_data_bytes = sizeof (snat_runtime_t),
1741 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1743 /* edit / add dispositions here */
1745 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1746 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1747 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1748 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1753 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1754 vlib_node_runtime_t * node,
1755 vlib_frame_t * frame)
1757 u32 n_left_from, *from, *to_next;
1758 snat_in2out_next_t next_index;
1759 snat_main_t *sm = &snat_main;
1760 int is_hairpinning = 0;
1762 from = vlib_frame_vector_args (frame);
1763 n_left_from = frame->n_vectors;
1764 next_index = node->cached_next_index;
1766 while (n_left_from > 0)
1770 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1772 while (n_left_from > 0 && n_left_to_next > 0)
1780 u32 new_addr0, old_addr0;
1781 u16 old_port0, new_port0;
1784 icmp46_header_t *icmp0;
1787 ip4_address_t sm0_addr;
1792 /* speculatively enqueue b0 to the current next frame */
1798 n_left_to_next -= 1;
1800 b0 = vlib_get_buffer (vm, bi0);
1801 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1803 ip0 = vlib_buffer_get_current (b0);
1804 udp0 = ip4_next_header (ip0);
1805 tcp0 = (tcp_header_t *) udp0;
1806 icmp0 = (icmp46_header_t *) udp0;
1808 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1810 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1812 if (PREDICT_FALSE (ip0->ttl == 1))
1814 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1815 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1816 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1818 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1822 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1824 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1827 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1829 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1830 rx_fib_index0, node, next0, ~0, 0, 0);
1834 if (snat_static_mapping_match
1835 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1836 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0, 0))
1838 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1839 next0 = SNAT_IN2OUT_NEXT_DROP;
1843 new_addr0 = sm0_addr.as_u32;
1844 new_port0 = sm0_port;
1845 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1846 old_addr0 = ip0->src_address.as_u32;
1847 ip0->src_address.as_u32 = new_addr0;
1849 sum0 = ip0->checksum;
1850 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1852 src_address /* changed member */ );
1853 ip0->checksum = ip_csum_fold (sum0);
1855 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1857 old_port0 = udp0->src_port;
1858 udp0->src_port = new_port0;
1860 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1862 sum0 = tcp0->checksum;
1863 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1865 dst_address /* changed member */ );
1866 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1867 ip4_header_t /* cheat */ ,
1868 length /* changed member */ );
1869 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1870 tcp0->checksum = ip_csum_fold (sum0);
1872 else if (udp0->checksum)
1874 sum0 = udp0->checksum;
1875 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1877 dst_address /* changed member */ );
1878 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1879 ip4_header_t /* cheat */ ,
1880 length /* changed member */ );
1881 udp0->checksum = ip_csum_fold (sum0);
1886 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1888 sum0 = tcp0->checksum;
1889 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1891 dst_address /* changed member */ );
1892 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1893 tcp0->checksum = ip_csum_fold (sum0);
1895 else if (udp0->checksum)
1897 sum0 = udp0->checksum;
1898 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1900 dst_address /* changed member */ );
1901 udp0->checksum = ip_csum_fold (sum0);
1907 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1911 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1912 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1914 snat_in2out_trace_t *t =
1915 vlib_add_trace (vm, node, b0, sizeof (*t));
1916 t->sw_if_index = sw_if_index0;
1917 t->next_index = next0;
1918 t->is_hairpinning = is_hairpinning;
1921 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1924 vlib_increment_simple_counter (&sm->counters.fastpath.
1925 in2out.other, sw_if_index0,
1926 vm->thread_index, 1);
1929 /* verify speculative enqueue, maybe switch current next frame */
1930 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1931 to_next, n_left_to_next,
1935 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1938 return frame->n_vectors;
1943 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1944 .name = "nat44-in2out-fast",
1945 .vector_size = sizeof (u32),
1946 .format_trace = format_snat_in2out_fast_trace,
1947 .type = VLIB_NODE_TYPE_INTERNAL,
1949 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1950 .error_strings = snat_in2out_error_strings,
1952 .runtime_data_bytes = sizeof (snat_runtime_t),
1954 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1956 /* edit / add dispositions here */
1958 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1959 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1960 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1961 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1967 * fd.io coding-style-patch-verification: ON
1970 * eval: (c-set-style "gnu")