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 _(IN2OUT_PACKETS, "good in2out packets processed") \
86 _(OUT_OF_PORTS, "out of ports") \
87 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
88 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
89 _(NO_TRANSLATION, "no translation") \
90 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
91 _(DROP_FRAGMENT, "drop fragment") \
92 _(TCP_PACKETS, "TCP packets") \
93 _(UDP_PACKETS, "UDP packets") \
94 _(ICMP_PACKETS, "ICMP packets") \
95 _(OTHER_PACKETS, "other protocol packets") \
96 _(FRAGMENTS, "fragments") \
97 _(CACHED_FRAGMENTS, "cached fragments") \
98 _(PROCESSED_FRAGMENTS, "processed fragments")
102 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
103 foreach_snat_in2out_error
106 } snat_in2out_error_t;
108 static char *snat_in2out_error_strings[] = {
109 #define _(sym,string) string,
110 foreach_snat_in2out_error
116 SNAT_IN2OUT_NEXT_LOOKUP,
117 SNAT_IN2OUT_NEXT_DROP,
118 SNAT_IN2OUT_NEXT_ICMP_ERROR,
119 SNAT_IN2OUT_NEXT_SLOW_PATH,
121 } snat_in2out_next_t;
124 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
125 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
126 u32 rx_fib_index0, u32 thread_index)
128 udp_header_t *udp0 = ip4_next_header (ip0);
129 clib_bihash_kv_8_8_t kv0, value0;
131 init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
134 /* NAT packet aimed at external address if */
135 /* has active sessions */
136 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
139 /* or is static mappings */
140 ip4_address_t dummy_addr;
143 if (!snat_static_mapping_match
144 (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
145 proto0, &dummy_addr, &dummy_port, &dummy_fib_index, 1, 0, 0, 0, 0,
152 if (sm->forwarding_enabled)
155 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
160 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
161 u32 proto0, u16 src_port, u16 dst_port,
162 u32 thread_index, u32 sw_if_index)
164 clib_bihash_kv_8_8_t kv0, value0;
168 init_nat_k (&kv0, ip0->src_address, src_port,
169 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
171 if (!clib_bihash_search_8_8
172 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
176 init_nat_k (&kv0, ip0->dst_address, dst_port,
177 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
178 if (!clib_bihash_search_8_8
179 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
183 pool_foreach (i, sm->output_feature_interfaces,
185 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
195 #ifndef CLIB_MARCH_VARIANT
197 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
199 snat_main_t *sm = &snat_main;
200 nat44_is_idle_session_ctx_t *ctx = arg;
202 u64 sess_timeout_time;
203 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
205 clib_bihash_kv_8_8_t s_kv;
207 s = pool_elt_at_index (tsm->sessions, kv->value);
208 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
209 if (ctx->now >= sess_timeout_time)
211 init_nat_o2i_k (&s_kv, s);
212 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
213 nat_elog_warn ("out2in key del failed");
215 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
216 s->in2out.addr.as_u32,
217 s->out2in.addr.as_u32,
221 s->in2out.fib_index);
223 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
224 &s->in2out.addr, s->in2out.port,
225 &s->out2in.addr, s->out2in.port, s->nat_proto);
227 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
228 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
231 if (!snat_is_session_static (s))
232 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
234 s->out2in.port, s->nat_proto);
236 nat44_delete_session (sm, s, ctx->thread_index);
245 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
247 ip4_address_t i2o_addr,
250 nat_protocol_t nat_proto,
251 snat_session_t ** sessionp,
252 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
255 snat_session_t *s = 0;
256 clib_bihash_kv_8_8_t kv0;
258 nat_outside_fib_t *outside_fib;
259 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
262 .fp_proto = FIB_PROTOCOL_IP4,
265 .ip4.as_u32 = ip0->dst_address.as_u32,
268 nat44_is_idle_session_ctx_t ctx0;
269 ip4_address_t sm_addr;
273 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
275 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
276 nat_ipfix_logging_max_sessions (thread_index,
277 sm->max_translations_per_thread);
278 nat_elog_notice ("maximum sessions exceeded");
279 return SNAT_IN2OUT_NEXT_DROP;
282 /* First try to match static mapping by local address and port */
283 if (snat_static_mapping_match
284 (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
285 &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat))
287 /* Try to create dynamic translation */
288 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
294 [thread_index].snat_thread_index))
296 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
297 return SNAT_IN2OUT_NEXT_DROP;
302 if (PREDICT_FALSE (identity_nat))
311 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
315 nat_elog_warn ("create NAT user failed");
316 return SNAT_IN2OUT_NEXT_DROP;
319 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
322 nat44_delete_user_with_no_session (sm, u, thread_index);
323 nat_elog_warn ("create NAT session failed");
324 return SNAT_IN2OUT_NEXT_DROP;
328 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
329 user_session_increment (sm, u, is_sm);
330 s->in2out.addr = i2o_addr;
331 s->in2out.port = i2o_port;
332 s->in2out.fib_index = rx_fib_index0;
333 s->nat_proto = nat_proto;
334 s->out2in.addr = sm_addr;
335 s->out2in.port = sm_port;
336 s->out2in.fib_index = sm->outside_fib_index;
337 switch (vec_len (sm->outside_fibs))
340 s->out2in.fib_index = sm->outside_fib_index;
343 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
347 vec_foreach (outside_fib, sm->outside_fibs)
349 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
350 if (FIB_NODE_INDEX_INVALID != fei)
352 if (fib_entry_get_resolving_interface (fei) != ~0)
354 s->out2in.fib_index = outside_fib->fib_index;
362 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
363 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
366 /* Add to translation hashes */
368 ctx0.thread_index = thread_index;
369 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
370 if (clib_bihash_add_or_overwrite_stale_8_8
371 (&sm->per_thread_data[thread_index].in2out, &kv0,
372 nat44_i2o_is_idle_session_cb, &ctx0))
373 nat_elog_notice ("in2out key add failed");
375 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
376 if (clib_bihash_add_or_overwrite_stale_8_8
377 (&sm->per_thread_data[thread_index].out2in, &kv0,
378 nat44_o2i_is_idle_session_cb, &ctx0))
379 nat_elog_notice ("out2in key add failed");
382 snat_ipfix_logging_nat44_ses_create (thread_index,
383 s->in2out.addr.as_u32,
384 s->out2in.addr.as_u32,
387 s->out2in.port, s->in2out.fib_index);
389 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
390 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
391 s->out2in.port, s->nat_proto);
393 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
394 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
395 &s->ext_host_nat_addr, s->ext_host_nat_port,
396 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
401 #ifndef CLIB_MARCH_VARIANT
402 static_always_inline snat_in2out_error_t
403 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
404 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
406 icmp46_header_t *icmp0;
407 icmp_echo_header_t *echo0, *inner_echo0 = 0;
408 ip4_header_t *inner_ip0 = 0;
410 icmp46_header_t *inner_icmp0;
412 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
413 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
415 if (!icmp_type_is_error_message
416 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
418 *nat_proto = NAT_PROTOCOL_ICMP;
419 *addr = ip0->src_address;
420 *port = vnet_buffer (b)->ip.reass.l4_src_port;
424 inner_ip0 = (ip4_header_t *) (echo0 + 1);
425 l4_header = ip4_next_header (inner_ip0);
426 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
427 *addr = inner_ip0->dst_address;
430 case NAT_PROTOCOL_ICMP:
431 inner_icmp0 = (icmp46_header_t *) l4_header;
432 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
433 *port = inner_echo0->identifier;
435 case NAT_PROTOCOL_UDP:
436 case NAT_PROTOCOL_TCP:
437 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
440 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
443 return -1; /* success */
447 * Get address and port values to be used for ICMP packet translation
448 * and create session if needed
450 * @param[in,out] sm NAT main
451 * @param[in,out] node NAT node runtime
452 * @param[in] thread_index thread index
453 * @param[in,out] b0 buffer containing packet to be translated
454 * @param[in,out] ip0 ip header
455 * @param[out] p_proto protocol used for matching
456 * @param[out] p_value address and port after NAT translation
457 * @param[out] p_dont_translate if packet should not be translated
458 * @param d optional parameter
459 * @param e optional parameter
462 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
463 u32 thread_index, vlib_buffer_t * b0,
464 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
465 u32 * fib_index, nat_protocol_t * proto, void *d,
466 void *e, u8 * dont_translate)
468 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
470 snat_session_t *s0 = 0;
471 clib_bihash_kv_8_8_t kv0, value0;
474 vlib_main_t *vm = vlib_get_main ();
477 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
478 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
480 err = icmp_get_key (b0, ip0, addr, port, proto);
483 b0->error = node->errors[err];
484 next0 = SNAT_IN2OUT_NEXT_DROP;
488 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
489 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
491 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
494 (nat_not_translate_output_feature
495 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
503 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
504 ip0, NAT_PROTOCOL_ICMP,
505 *fib_index, thread_index)))
513 (icmp_type_is_error_message
514 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
516 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
517 next0 = SNAT_IN2OUT_NEXT_DROP;
522 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
523 next0, thread_index, vlib_time_now (vm));
525 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
537 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
539 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
541 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
542 reass.icmp_type_or_tcp_flags)))
544 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
545 next0 = SNAT_IN2OUT_NEXT_DROP;
549 s0 = pool_elt_at_index (tsm->sessions, value0.value);
555 *addr = s0->out2in.addr;
556 *port = s0->out2in.port;
557 *fib_index = s0->out2in.fib_index;
560 *(snat_session_t **) (d) = s0;
565 #ifndef CLIB_MARCH_VARIANT
567 * Get address and port values to be used for ICMP packet translation
569 * @param[in] sm NAT main
570 * @param[in,out] node NAT node runtime
571 * @param[in] thread_index thread index
572 * @param[in,out] b0 buffer containing packet to be translated
573 * @param[in,out] ip0 ip header
574 * @param[out] p_proto protocol used for matching
575 * @param[out] p_value address and port after NAT translation
576 * @param[out] p_dont_translate if packet should not be translated
577 * @param d optional parameter
578 * @param e optional parameter
581 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
582 u32 thread_index, vlib_buffer_t * b0,
583 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
584 u32 * fib_index, nat_protocol_t * proto, void *d,
585 void *e, u8 * dont_translate)
593 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
594 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
596 err = icmp_get_key (b0, ip0, addr, port, proto);
599 b0->error = node->errors[err];
600 next0 = SNAT_IN2OUT_NEXT_DROP;
604 ip4_address_t sm_addr;
608 if (snat_static_mapping_match
609 (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
610 &sm_fib_index, 0, &is_addr_only, 0, 0, 0, 0))
612 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
620 if (icmp_type_is_error_message
621 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
623 next0 = SNAT_IN2OUT_NEXT_DROP;
627 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
628 next0 = SNAT_IN2OUT_NEXT_DROP;
633 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
634 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
635 ICMP4_echo_reply || !is_addr_only)
636 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
637 reass.icmp_type_or_tcp_flags)))
639 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
640 next0 = SNAT_IN2OUT_NEXT_DROP;
649 #ifndef CLIB_MARCH_VARIANT
651 icmp_in2out (snat_main_t * sm,
654 icmp46_header_t * icmp0,
657 vlib_node_runtime_t * node,
658 u32 next0, u32 thread_index, void *d, void *e)
660 vlib_main_t *vm = vlib_get_main ();
664 nat_protocol_t protocol;
665 icmp_echo_header_t *echo0, *inner_echo0 = 0;
666 ip4_header_t *inner_ip0;
668 icmp46_header_t *inner_icmp0;
670 u32 new_addr0, old_addr0;
671 u16 old_id0, new_id0;
672 u16 old_checksum0, new_checksum0;
677 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
680 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
681 &fib_index, &protocol, d, e, &dont_translate);
684 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
687 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
690 ip_incremental_checksum_buffer (vm, b0,
692 (u8 *) vlib_buffer_get_current (b0),
693 ntohs (ip0->length) -
694 ip4_header_bytes (ip0), 0);
695 checksum0 = ~ip_csum_fold (sum0);
696 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
698 next0 = SNAT_IN2OUT_NEXT_DROP;
703 old_addr0 = ip0->src_address.as_u32;
704 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
706 sum0 = ip0->checksum;
707 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
708 src_address /* changed member */ );
709 ip0->checksum = ip_csum_fold (sum0);
711 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
713 if (icmp0->checksum == 0)
714 icmp0->checksum = 0xffff;
716 if (!icmp_type_is_error_message (icmp0->type))
719 if (PREDICT_FALSE (new_id0 != echo0->identifier))
721 old_id0 = echo0->identifier;
723 echo0->identifier = new_id0;
725 sum0 = icmp0->checksum;
727 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
729 icmp0->checksum = ip_csum_fold (sum0);
734 inner_ip0 = (ip4_header_t *) (echo0 + 1);
735 l4_header = ip4_next_header (inner_ip0);
737 if (!ip4_header_checksum_is_valid (inner_ip0))
739 next0 = SNAT_IN2OUT_NEXT_DROP;
743 /* update inner destination IP address */
744 old_addr0 = inner_ip0->dst_address.as_u32;
745 inner_ip0->dst_address = addr;
746 new_addr0 = inner_ip0->dst_address.as_u32;
747 sum0 = icmp0->checksum;
748 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
749 dst_address /* changed member */ );
750 icmp0->checksum = ip_csum_fold (sum0);
752 /* update inner IP header checksum */
753 old_checksum0 = inner_ip0->checksum;
754 sum0 = inner_ip0->checksum;
755 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
756 dst_address /* changed member */ );
757 inner_ip0->checksum = ip_csum_fold (sum0);
758 new_checksum0 = inner_ip0->checksum;
759 sum0 = icmp0->checksum;
761 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
763 icmp0->checksum = ip_csum_fold (sum0);
767 case NAT_PROTOCOL_ICMP:
768 inner_icmp0 = (icmp46_header_t *) l4_header;
769 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
771 old_id0 = inner_echo0->identifier;
773 inner_echo0->identifier = new_id0;
775 sum0 = icmp0->checksum;
777 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
779 icmp0->checksum = ip_csum_fold (sum0);
781 case NAT_PROTOCOL_UDP:
782 case NAT_PROTOCOL_TCP:
783 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
785 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
787 sum0 = icmp0->checksum;
788 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
790 icmp0->checksum = ip_csum_fold (sum0);
798 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
800 if (sm->deterministic ||
801 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
802 sm->endpoint_dependent))
803 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
812 icmp_in2out_slow_path (snat_main_t * sm,
815 icmp46_header_t * icmp0,
818 vlib_node_runtime_t * node,
820 f64 now, u32 thread_index, snat_session_t ** p_s0)
822 vlib_main_t *vm = vlib_get_main ();
824 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
825 next0, thread_index, p_s0, 0);
826 snat_session_t *s0 = *p_s0;
827 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
830 nat44_session_update_counters (s0, now,
831 vlib_buffer_length_in_chain
832 (vm, b0), thread_index);
833 /* Per-user LRU list maintenance */
834 nat44_session_update_lru (sm, s0, thread_index);
840 nat_in2out_sm_unknown_proto (snat_main_t * sm,
842 ip4_header_t * ip, u32 rx_fib_index)
844 clib_bihash_kv_8_8_t kv, value;
845 snat_static_mapping_t *m;
846 u32 old_addr, new_addr;
849 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
850 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
853 m = pool_elt_at_index (sm->static_mappings, value.value);
855 old_addr = ip->src_address.as_u32;
856 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
858 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
859 ip->checksum = ip_csum_fold (sum);
863 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
865 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
866 nat_hairpinning_sm_unknown_proto (sm, b, ip);
873 snat_in2out_node_fn_inline (vlib_main_t * vm,
874 vlib_node_runtime_t * node,
875 vlib_frame_t * frame, int is_slow_path,
876 int is_output_feature)
878 u32 n_left_from, *from, *to_next;
879 snat_in2out_next_t next_index;
880 u32 pkts_processed = 0;
881 snat_main_t *sm = &snat_main;
882 f64 now = vlib_time_now (vm);
883 u32 stats_node_index;
884 u32 thread_index = vm->thread_index;
885 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
888 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
889 sm->in2out_node_index;
891 from = vlib_frame_vector_args (frame);
892 n_left_from = frame->n_vectors;
893 next_index = node->cached_next_index;
895 while (n_left_from > 0)
899 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
901 while (n_left_from >= 4 && n_left_to_next >= 2)
904 vlib_buffer_t *b0, *b1;
906 u32 sw_if_index0, sw_if_index1;
907 ip4_header_t *ip0, *ip1;
908 ip_csum_t sum0, sum1;
909 u32 new_addr0, old_addr0, new_addr1, old_addr1;
910 u16 old_port0, new_port0, old_port1, new_port1;
911 udp_header_t *udp0, *udp1;
912 tcp_header_t *tcp0, *tcp1;
913 icmp46_header_t *icmp0, *icmp1;
914 u32 rx_fib_index0, rx_fib_index1;
916 snat_session_t *s0 = 0, *s1 = 0;
917 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
918 u32 iph_offset0 = 0, iph_offset1 = 0;
920 /* Prefetch next iteration. */
922 vlib_buffer_t *p2, *p3;
924 p2 = vlib_get_buffer (vm, from[2]);
925 p3 = vlib_get_buffer (vm, from[3]);
927 vlib_prefetch_buffer_header (p2, LOAD);
928 vlib_prefetch_buffer_header (p3, LOAD);
930 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
931 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
934 /* speculatively enqueue b0 and b1 to the current next frame */
935 to_next[0] = bi0 = from[0];
936 to_next[1] = bi1 = from[1];
942 b0 = vlib_get_buffer (vm, bi0);
943 b1 = vlib_get_buffer (vm, bi1);
945 if (is_output_feature)
946 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
948 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
951 udp0 = ip4_next_header (ip0);
952 tcp0 = (tcp_header_t *) udp0;
953 icmp0 = (icmp46_header_t *) udp0;
955 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
956 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
959 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
961 if (PREDICT_FALSE (ip0->ttl == 1))
963 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
964 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
965 ICMP4_time_exceeded_ttl_exceeded_in_transit,
967 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
971 proto0 = ip_proto_to_nat_proto (ip0->protocol);
973 /* Next configured feature, probably ip4-lookup */
976 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
978 if (nat_in2out_sm_unknown_proto
979 (sm, b0, ip0, rx_fib_index0))
981 next0 = SNAT_IN2OUT_NEXT_DROP;
983 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
989 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
991 next0 = icmp_in2out_slow_path
992 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
993 node, next0, now, thread_index, &s0);
1000 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1002 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1006 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1008 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1013 init_nat_k (&kv0, ip0->src_address,
1014 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1017 (clib_bihash_search_8_8
1018 (&sm->per_thread_data[thread_index].in2out, &kv0,
1023 if (is_output_feature)
1026 (nat_not_translate_output_feature
1028 vnet_buffer (b0)->ip.reass.l4_src_port,
1029 vnet_buffer (b0)->ip.reass.l4_dst_port,
1030 thread_index, sw_if_index0)))
1034 * Send DHCP packets to the ipv4 stack, or we won't
1035 * be able to use dhcp client on the outside interface
1038 (proto0 == NAT_PROTOCOL_UDP
1039 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1040 clib_host_to_net_u16
1041 (UDP_DST_PORT_dhcp_to_server))
1042 && ip0->dst_address.as_u32 == 0xffffffff))
1049 (sm, node, sw_if_index0, ip0, proto0,
1050 rx_fib_index0, thread_index)))
1054 next0 = slow_path (sm, b0, ip0,
1056 vnet_buffer (b0)->ip.reass.l4_src_port,
1059 &s0, node, next0, thread_index, now);
1060 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1063 if (PREDICT_FALSE (!s0))
1068 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1074 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1077 b0->flags |= VNET_BUFFER_F_IS_NATED;
1079 old_addr0 = ip0->src_address.as_u32;
1080 ip0->src_address = s0->out2in.addr;
1081 new_addr0 = ip0->src_address.as_u32;
1082 if (!is_output_feature)
1083 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1085 sum0 = ip0->checksum;
1086 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1088 src_address /* changed member */ );
1089 ip0->checksum = ip_csum_fold (sum0);
1092 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1094 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1096 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1097 new_port0 = udp0->src_port = s0->out2in.port;
1098 sum0 = tcp0->checksum;
1099 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1101 dst_address /* changed member */ );
1102 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1103 ip4_header_t /* cheat */ ,
1104 length /* changed member */ );
1105 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1106 tcp0->checksum = ip_csum_fold (sum0);
1112 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1114 udp0->src_port = s0->out2in.port;
1115 if (PREDICT_FALSE (udp0->checksum))
1117 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1118 new_port0 = udp0->src_port;
1119 sum0 = udp0->checksum;
1120 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1123 ip_csum_update (sum0, old_port0, new_port0,
1124 ip4_header_t /* cheat */ ,
1125 length /* changed member */ );
1126 udp0->checksum = ip_csum_fold (sum0);
1133 nat44_session_update_counters (s0, now,
1134 vlib_buffer_length_in_chain (vm, b0),
1136 /* Per-user LRU list maintenance */
1137 nat44_session_update_lru (sm, s0, thread_index);
1140 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1141 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1143 snat_in2out_trace_t *t =
1144 vlib_add_trace (vm, node, b0, sizeof (*t));
1145 t->is_slow_path = is_slow_path;
1146 t->sw_if_index = sw_if_index0;
1147 t->next_index = next0;
1148 t->session_index = ~0;
1151 s0 - sm->per_thread_data[thread_index].sessions;
1154 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1156 if (is_output_feature)
1157 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1159 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1162 udp1 = ip4_next_header (ip1);
1163 tcp1 = (tcp_header_t *) udp1;
1164 icmp1 = (icmp46_header_t *) udp1;
1166 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1167 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1170 if (PREDICT_FALSE (ip1->ttl == 1))
1172 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1173 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1174 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1176 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1180 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1182 /* Next configured feature, probably ip4-lookup */
1185 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1187 if (nat_in2out_sm_unknown_proto
1188 (sm, b1, ip1, rx_fib_index1))
1190 next1 = SNAT_IN2OUT_NEXT_DROP;
1192 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1198 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1200 next1 = icmp_in2out_slow_path
1201 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1202 next1, now, thread_index, &s1);
1209 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1211 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1215 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1217 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1222 init_nat_k (&kv1, ip1->src_address,
1223 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1226 (clib_bihash_search_8_8
1227 (&sm->per_thread_data[thread_index].in2out, &kv1,
1232 if (is_output_feature)
1235 (nat_not_translate_output_feature
1237 vnet_buffer (b1)->ip.reass.l4_src_port,
1238 vnet_buffer (b1)->ip.reass.l4_dst_port,
1239 thread_index, sw_if_index1)))
1243 * Send DHCP packets to the ipv4 stack, or we won't
1244 * be able to use dhcp client on the outside interface
1247 (proto1 == NAT_PROTOCOL_UDP
1248 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1249 clib_host_to_net_u16
1250 (UDP_DST_PORT_dhcp_to_server))
1251 && ip1->dst_address.as_u32 == 0xffffffff))
1258 (sm, node, sw_if_index1, ip1, proto1,
1259 rx_fib_index1, thread_index)))
1264 slow_path (sm, b1, ip1, ip1->src_address,
1265 vnet_buffer (b1)->ip.reass.l4_src_port,
1266 rx_fib_index1, proto1, &s1, node, next1,
1268 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1271 if (PREDICT_FALSE (!s1))
1276 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1282 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1285 b1->flags |= VNET_BUFFER_F_IS_NATED;
1287 old_addr1 = ip1->src_address.as_u32;
1288 ip1->src_address = s1->out2in.addr;
1289 new_addr1 = ip1->src_address.as_u32;
1290 if (!is_output_feature)
1291 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1293 sum1 = ip1->checksum;
1294 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1296 src_address /* changed member */ );
1297 ip1->checksum = ip_csum_fold (sum1);
1299 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1301 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1303 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1304 new_port1 = udp1->src_port = s1->out2in.port;
1305 sum1 = tcp1->checksum;
1306 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1308 dst_address /* changed member */ );
1309 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1310 ip4_header_t /* cheat */ ,
1311 length /* changed member */ );
1312 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1313 tcp1->checksum = ip_csum_fold (sum1);
1319 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1321 udp1->src_port = s1->out2in.port;
1322 if (PREDICT_FALSE (udp1->checksum))
1324 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1325 new_port1 = udp1->src_port;
1326 sum1 = udp1->checksum;
1327 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1330 ip_csum_update (sum1, old_port1, new_port1,
1331 ip4_header_t /* cheat */ ,
1332 length /* changed member */ );
1333 udp1->checksum = ip_csum_fold (sum1);
1340 nat44_session_update_counters (s1, now,
1341 vlib_buffer_length_in_chain (vm, b1),
1343 /* Per-user LRU list maintenance */
1344 nat44_session_update_lru (sm, s1, thread_index);
1347 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1348 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1350 snat_in2out_trace_t *t =
1351 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 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1362 /* verify speculative enqueues, maybe switch current next frame */
1363 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1364 to_next, n_left_to_next,
1365 bi0, bi1, next0, next1);
1368 while (n_left_from > 0 && n_left_to_next > 0)
1376 u32 new_addr0, old_addr0;
1377 u16 old_port0, new_port0;
1380 icmp46_header_t *icmp0;
1383 snat_session_t *s0 = 0;
1384 clib_bihash_kv_8_8_t kv0, value0;
1385 u32 iph_offset0 = 0;
1387 /* speculatively enqueue b0 to the current next frame */
1393 n_left_to_next -= 1;
1395 b0 = vlib_get_buffer (vm, bi0);
1396 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1398 if (is_output_feature)
1399 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1401 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1404 udp0 = ip4_next_header (ip0);
1405 tcp0 = (tcp_header_t *) udp0;
1406 icmp0 = (icmp46_header_t *) udp0;
1408 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1409 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1412 if (PREDICT_FALSE (ip0->ttl == 1))
1414 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1415 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1416 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1418 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1422 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1424 /* Next configured feature, probably ip4-lookup */
1427 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1429 if (nat_in2out_sm_unknown_proto
1430 (sm, b0, ip0, rx_fib_index0))
1432 next0 = SNAT_IN2OUT_NEXT_DROP;
1434 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1440 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1442 next0 = icmp_in2out_slow_path
1443 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1444 next0, now, thread_index, &s0);
1451 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1453 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1457 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1459 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1464 init_nat_k (&kv0, ip0->src_address,
1465 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1468 if (clib_bihash_search_8_8
1469 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1473 if (is_output_feature)
1476 (nat_not_translate_output_feature
1478 vnet_buffer (b0)->ip.reass.l4_src_port,
1479 vnet_buffer (b0)->ip.reass.l4_dst_port,
1480 thread_index, sw_if_index0)))
1484 * Send DHCP packets to the ipv4 stack, or we won't
1485 * be able to use dhcp client on the outside interface
1488 (proto0 == NAT_PROTOCOL_UDP
1489 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1490 clib_host_to_net_u16
1491 (UDP_DST_PORT_dhcp_to_server))
1492 && ip0->dst_address.as_u32 == 0xffffffff))
1499 (sm, node, sw_if_index0, ip0, proto0,
1500 rx_fib_index0, thread_index)))
1504 next0 = slow_path (sm, b0, ip0,
1506 vnet_buffer (b0)->ip.reass.l4_src_port,
1509 &s0, node, next0, thread_index, now);
1511 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1514 if (PREDICT_FALSE (!s0))
1519 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1525 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1528 b0->flags |= VNET_BUFFER_F_IS_NATED;
1530 old_addr0 = ip0->src_address.as_u32;
1531 ip0->src_address = s0->out2in.addr;
1532 new_addr0 = ip0->src_address.as_u32;
1533 if (!is_output_feature)
1534 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1536 sum0 = ip0->checksum;
1537 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1539 src_address /* changed member */ );
1540 ip0->checksum = ip_csum_fold (sum0);
1542 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1544 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1546 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1547 new_port0 = udp0->src_port = s0->out2in.port;
1548 sum0 = tcp0->checksum;
1549 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1551 dst_address /* changed member */ );
1552 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1553 ip4_header_t /* cheat */ ,
1554 length /* changed member */ );
1555 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1556 tcp0->checksum = ip_csum_fold (sum0);
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;
1570 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1573 ip_csum_update (sum0, old_port0, new_port0,
1574 ip4_header_t /* cheat */ ,
1575 length /* changed member */ );
1576 udp0->checksum = ip_csum_fold (sum0);
1583 nat44_session_update_counters (s0, now,
1584 vlib_buffer_length_in_chain (vm, b0),
1586 /* Per-user LRU list maintenance */
1587 nat44_session_update_lru (sm, s0, thread_index);
1590 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1591 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1593 snat_in2out_trace_t *t =
1594 vlib_add_trace (vm, node, b0, sizeof (*t));
1595 t->is_slow_path = is_slow_path;
1596 t->sw_if_index = sw_if_index0;
1597 t->next_index = next0;
1598 t->session_index = ~0;
1601 s0 - sm->per_thread_data[thread_index].sessions;
1604 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1606 /* verify speculative enqueue, maybe switch current next frame */
1607 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1608 to_next, n_left_to_next,
1612 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1615 vlib_node_increment_counter (vm, stats_node_index,
1616 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1618 vlib_node_increment_counter (vm, stats_node_index,
1619 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1620 vlib_node_increment_counter (vm, stats_node_index,
1621 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1622 vlib_node_increment_counter (vm, stats_node_index,
1623 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1624 vlib_node_increment_counter (vm, stats_node_index,
1625 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1627 vlib_node_increment_counter (vm, stats_node_index,
1628 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1630 return frame->n_vectors;
1633 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1634 vlib_node_runtime_t * node,
1635 vlib_frame_t * frame)
1637 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1642 VLIB_REGISTER_NODE (snat_in2out_node) = {
1643 .name = "nat44-in2out",
1644 .vector_size = sizeof (u32),
1645 .format_trace = format_snat_in2out_trace,
1646 .type = VLIB_NODE_TYPE_INTERNAL,
1648 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1649 .error_strings = snat_in2out_error_strings,
1651 .runtime_data_bytes = sizeof (snat_runtime_t),
1653 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1655 /* edit / add dispositions here */
1657 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1658 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1659 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1660 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1665 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1666 vlib_node_runtime_t * node,
1667 vlib_frame_t * frame)
1669 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1674 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1675 .name = "nat44-in2out-output",
1676 .vector_size = sizeof (u32),
1677 .format_trace = format_snat_in2out_trace,
1678 .type = VLIB_NODE_TYPE_INTERNAL,
1680 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1681 .error_strings = snat_in2out_error_strings,
1683 .runtime_data_bytes = sizeof (snat_runtime_t),
1685 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1687 /* edit / add dispositions here */
1689 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1690 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1691 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1692 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1697 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1698 vlib_node_runtime_t * node,
1699 vlib_frame_t * frame)
1701 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1706 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1707 .name = "nat44-in2out-slowpath",
1708 .vector_size = sizeof (u32),
1709 .format_trace = format_snat_in2out_trace,
1710 .type = VLIB_NODE_TYPE_INTERNAL,
1712 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1713 .error_strings = snat_in2out_error_strings,
1715 .runtime_data_bytes = sizeof (snat_runtime_t),
1717 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1719 /* edit / add dispositions here */
1721 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1722 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1723 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1724 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1729 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1730 vlib_node_runtime_t * node,
1731 vlib_frame_t * frame)
1733 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1738 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1739 .name = "nat44-in2out-output-slowpath",
1740 .vector_size = sizeof (u32),
1741 .format_trace = format_snat_in2out_trace,
1742 .type = VLIB_NODE_TYPE_INTERNAL,
1744 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1745 .error_strings = snat_in2out_error_strings,
1747 .runtime_data_bytes = sizeof (snat_runtime_t),
1749 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1751 /* edit / add dispositions here */
1753 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1754 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1755 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1756 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1761 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1762 vlib_node_runtime_t * node,
1763 vlib_frame_t * frame)
1765 u32 n_left_from, *from, *to_next;
1766 snat_in2out_next_t next_index;
1767 u32 pkts_processed = 0;
1768 snat_main_t *sm = &snat_main;
1769 u32 stats_node_index;
1770 int is_hairpinning = 0;
1772 stats_node_index = sm->in2out_fast_node_index;
1774 from = vlib_frame_vector_args (frame);
1775 n_left_from = frame->n_vectors;
1776 next_index = node->cached_next_index;
1778 while (n_left_from > 0)
1782 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1784 while (n_left_from > 0 && n_left_to_next > 0)
1792 u32 new_addr0, old_addr0;
1793 u16 old_port0, new_port0;
1796 icmp46_header_t *icmp0;
1799 ip4_address_t sm0_addr;
1804 /* speculatively enqueue b0 to the current next frame */
1810 n_left_to_next -= 1;
1812 b0 = vlib_get_buffer (vm, bi0);
1813 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1815 ip0 = vlib_buffer_get_current (b0);
1816 udp0 = ip4_next_header (ip0);
1817 tcp0 = (tcp_header_t *) udp0;
1818 icmp0 = (icmp46_header_t *) udp0;
1820 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1822 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1824 if (PREDICT_FALSE (ip0->ttl == 1))
1826 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1827 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1828 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1830 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1834 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1836 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1839 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1841 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1842 rx_fib_index0, node, next0, ~0, 0, 0);
1846 if (snat_static_mapping_match
1847 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1848 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1850 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1851 next0 = SNAT_IN2OUT_NEXT_DROP;
1855 new_addr0 = sm0_addr.as_u32;
1856 new_port0 = sm0_port;
1857 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1858 old_addr0 = ip0->src_address.as_u32;
1859 ip0->src_address.as_u32 = new_addr0;
1861 sum0 = ip0->checksum;
1862 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1864 src_address /* changed member */ );
1865 ip0->checksum = ip_csum_fold (sum0);
1867 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1869 old_port0 = udp0->src_port;
1870 udp0->src_port = new_port0;
1872 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1874 sum0 = tcp0->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 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1882 tcp0->checksum = ip_csum_fold (sum0);
1884 else if (udp0->checksum)
1886 sum0 = udp0->checksum;
1887 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1889 dst_address /* changed member */ );
1890 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1891 ip4_header_t /* cheat */ ,
1892 length /* changed member */ );
1893 udp0->checksum = ip_csum_fold (sum0);
1898 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1900 sum0 = tcp0->checksum;
1901 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1903 dst_address /* changed member */ );
1904 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1905 tcp0->checksum = ip_csum_fold (sum0);
1907 else if (udp0->checksum)
1909 sum0 = udp0->checksum;
1910 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1912 dst_address /* changed member */ );
1913 udp0->checksum = ip_csum_fold (sum0);
1918 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1923 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1924 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1926 snat_in2out_trace_t *t =
1927 vlib_add_trace (vm, node, b0, sizeof (*t));
1928 t->sw_if_index = sw_if_index0;
1929 t->next_index = next0;
1930 t->is_hairpinning = is_hairpinning;
1933 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1935 /* verify speculative enqueue, maybe switch current next frame */
1936 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1937 to_next, n_left_to_next,
1941 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1944 vlib_node_increment_counter (vm, stats_node_index,
1945 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
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")