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 endpoint-dependent outside to inside 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/udp/udp.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/fib/ip4_fib.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>
44 } snat_out2in_trace_t;
46 /* packet trace format function */
48 format_snat_out2in_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
56 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
57 t->sw_if_index, t->next_index, t->session_index);
62 format_snat_out2in_fast_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
68 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
69 t->sw_if_index, t->next_index);
73 #define foreach_snat_out2in_error \
74 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
75 _(OUT2IN_PACKETS, "good out2in packets processed") \
76 _(OUT_OF_PORTS, "out of ports") \
77 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
78 _(NO_TRANSLATION, "no translation") \
79 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
80 _(DROP_FRAGMENT, "drop fragment") \
81 _(MAX_REASS, "maximum reassemblies exceeded") \
82 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
83 _(TCP_PACKETS, "TCP packets") \
84 _(UDP_PACKETS, "UDP packets") \
85 _(ICMP_PACKETS, "ICMP packets") \
86 _(OTHER_PACKETS, "other protocol packets") \
87 _(FRAGMENTS, "fragments") \
88 _(CACHED_FRAGMENTS, "cached fragments") \
89 _(PROCESSED_FRAGMENTS, "processed fragments")
93 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
94 foreach_snat_out2in_error
97 } snat_out2in_error_t;
99 static char *snat_out2in_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_snat_out2in_error
107 SNAT_OUT2IN_NEXT_DROP,
108 SNAT_OUT2IN_NEXT_LOOKUP,
109 SNAT_OUT2IN_NEXT_ICMP_ERROR,
111 } snat_out2in_next_t;
113 #ifndef CLIB_MARCH_VARIANT
115 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
117 snat_main_t *sm = &snat_main;
118 nat44_is_idle_session_ctx_t *ctx = arg;
120 u64 sess_timeout_time;
121 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
123 clib_bihash_kv_8_8_t s_kv;
125 s = pool_elt_at_index (tsm->sessions, kv->value);
126 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
127 if (ctx->now >= sess_timeout_time)
129 s_kv.key = s->in2out.as_u64;
130 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
131 nat_elog_warn ("out2in key del failed");
133 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
134 s->in2out.addr.as_u32,
135 s->out2in.addr.as_u32,
139 s->in2out.fib_index);
141 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
142 &s->in2out.addr, s->in2out.port,
143 &s->out2in.addr, s->out2in.port,
146 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
147 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
150 if (!snat_is_session_static (s))
151 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
154 nat44_delete_session (sm, s, ctx->thread_index);
163 * @brief Create session for static mapping.
165 * Create NAT session initiated by host from external network with static
168 * @param sm NAT main.
169 * @param b0 Vlib buffer.
170 * @param in2out In2out NAT44 session key.
171 * @param out2in Out2in NAT44 session key.
172 * @param node Vlib node.
174 * @returns SNAT session if successfully created otherwise 0.
176 static inline snat_session_t *
177 create_session_for_static_mapping (snat_main_t * sm,
179 snat_session_key_t in2out,
180 snat_session_key_t out2in,
181 vlib_node_runtime_t * node,
182 u32 thread_index, f64 now)
186 clib_bihash_kv_8_8_t kv0;
189 nat44_is_idle_session_ctx_t ctx0;
191 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
193 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
194 nat_elog_notice ("maximum sessions exceeded");
198 ip0 = vlib_buffer_get_current (b0);
199 udp0 = ip4_next_header (ip0);
202 nat_user_get_or_create (sm, &in2out.addr, in2out.fib_index, thread_index);
205 nat_elog_warn ("create NAT user failed");
209 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
212 nat44_delete_user_with_no_session (sm, u, thread_index);
213 nat_elog_warn ("create NAT session failed");
217 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
218 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
219 s->ext_host_port = udp0->src_port;
220 user_session_increment (sm, u, 1 /* static */ );
223 s->in2out.protocol = out2in.protocol;
225 /* Add to translation hashes */
227 ctx0.thread_index = thread_index;
228 kv0.key = s->in2out.as_u64;
229 kv0.value = s - sm->per_thread_data[thread_index].sessions;
230 if (clib_bihash_add_or_overwrite_stale_8_8
231 (&sm->per_thread_data[thread_index].in2out, &kv0,
232 nat44_i2o_is_idle_session_cb, &ctx0))
233 nat_elog_notice ("in2out key add failed");
235 kv0.key = s->out2in.as_u64;
237 if (clib_bihash_add_or_overwrite_stale_8_8
238 (&sm->per_thread_data[thread_index].out2in, &kv0,
239 nat44_o2i_is_idle_session_cb, &ctx0))
240 nat_elog_notice ("out2in key add failed");
243 snat_ipfix_logging_nat44_ses_create (thread_index,
244 s->in2out.addr.as_u32,
245 s->out2in.addr.as_u32,
248 s->out2in.port, s->in2out.fib_index);
250 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
251 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
252 s->out2in.port, s->in2out.protocol);
254 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
255 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
256 &s->ext_host_nat_addr, s->ext_host_nat_port,
257 s->in2out.protocol, s->in2out.fib_index, s->flags,
263 #ifndef CLIB_MARCH_VARIANT
265 snat_out2in_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
266 snat_session_key_t * p_key0)
268 icmp46_header_t *icmp0;
269 snat_session_key_t key0;
270 icmp_echo_header_t *echo0, *inner_echo0 = 0;
271 ip4_header_t *inner_ip0;
273 icmp46_header_t *inner_icmp0;
275 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
276 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
278 if (!icmp_type_is_error_message
279 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
281 key0.protocol = SNAT_PROTOCOL_ICMP;
282 key0.addr = ip0->dst_address;
283 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be dst port?
287 inner_ip0 = (ip4_header_t *) (echo0 + 1);
288 l4_header = ip4_next_header (inner_ip0);
289 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
290 key0.addr = inner_ip0->src_address;
291 switch (key0.protocol)
293 case SNAT_PROTOCOL_ICMP:
294 inner_icmp0 = (icmp46_header_t *) l4_header;
295 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
296 key0.port = inner_echo0->identifier;
298 case SNAT_PROTOCOL_UDP:
299 case SNAT_PROTOCOL_TCP:
300 key0.port = ((tcp_udp_header_t *) l4_header)->src_port;
303 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
307 return -1; /* success */
311 * Get address and port values to be used for ICMP packet translation
312 * and create session if needed
314 * @param[in,out] sm NAT main
315 * @param[in,out] node NAT node runtime
316 * @param[in] thread_index thread index
317 * @param[in,out] b0 buffer containing packet to be translated
318 * @param[in,out] ip0 ip header
319 * @param[out] p_proto protocol used for matching
320 * @param[out] p_value address and port after NAT translation
321 * @param[out] p_dont_translate if packet should not be translated
322 * @param d optional parameter
323 * @param e optional parameter
326 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
327 u32 thread_index, vlib_buffer_t * b0,
328 ip4_header_t * ip0, u8 * p_proto,
329 snat_session_key_t * p_value,
330 u8 * p_dont_translate, void *d, void *e)
332 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
335 snat_session_key_t key0;
336 snat_session_key_t sm0;
337 snat_session_t *s0 = 0;
338 u8 dont_translate = 0;
339 clib_bihash_kv_8_8_t kv0, value0;
345 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
346 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
350 err = icmp_get_key (b0, ip0, &key0);
353 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
354 next0 = SNAT_OUT2IN_NEXT_DROP;
357 key0.fib_index = rx_fib_index0;
359 kv0.key = key0.as_u64;
361 if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
363 /* Try to match static mapping by external address and port,
364 destination address and port in packet */
365 if (snat_static_mapping_match
366 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
368 if (!sm->forwarding_enabled)
370 /* Don't NAT packet aimed at the intfc address */
371 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
372 ip0->dst_address.as_u32)))
377 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
378 next0 = SNAT_OUT2IN_NEXT_DROP;
389 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
391 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
392 ICMP4_echo_request || !is_addr_only)))
394 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
395 next0 = SNAT_OUT2IN_NEXT_DROP;
399 if (PREDICT_FALSE (identity_nat))
404 /* Create session initiated by host from external network */
405 s0 = create_session_for_static_mapping (sm, b0, sm0, key0,
407 vlib_time_now (tsm->vlib_main));
411 next0 = SNAT_OUT2IN_NEXT_DROP;
418 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
420 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
422 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
423 reass.icmp_type_or_tcp_flags)))
425 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
426 next0 = SNAT_OUT2IN_NEXT_DROP;
430 s0 = pool_elt_at_index (tsm->sessions, value0.value);
434 *p_proto = key0.protocol;
436 *p_value = s0->in2out;
437 *p_dont_translate = dont_translate;
439 *(snat_session_t **) d = s0;
444 #ifndef CLIB_MARCH_VARIANT
446 * Get address and port values to be used for ICMP packet translation
448 * @param[in] sm NAT main
449 * @param[in,out] node NAT node runtime
450 * @param[in] thread_index thread index
451 * @param[in,out] b0 buffer containing packet to be translated
452 * @param[in,out] ip0 ip header
453 * @param[out] p_proto protocol used for matching
454 * @param[out] p_value address and port after NAT translation
455 * @param[out] p_dont_translate if packet should not be translated
456 * @param d optional parameter
457 * @param e optional parameter
460 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
461 u32 thread_index, vlib_buffer_t * b0,
462 ip4_header_t * ip0, u8 * p_proto,
463 snat_session_key_t * p_value,
464 u8 * p_dont_translate, void *d, void *e)
468 snat_session_key_t key0;
469 snat_session_key_t sm0;
470 u8 dont_translate = 0;
475 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
476 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
478 err = icmp_get_key (b0, ip0, &key0);
481 b0->error = node->errors[err];
482 next0 = SNAT_OUT2IN_NEXT_DROP;
485 key0.fib_index = rx_fib_index0;
487 if (snat_static_mapping_match
488 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, 0))
490 /* Don't NAT packet aimed at the intfc address */
491 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
496 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
497 next0 = SNAT_OUT2IN_NEXT_DROP;
502 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
503 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
504 ICMP4_echo_request || !is_addr_only)
505 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
506 reass.icmp_type_or_tcp_flags)))
508 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
509 next0 = SNAT_OUT2IN_NEXT_DROP;
516 *p_proto = key0.protocol;
517 *p_dont_translate = dont_translate;
522 #ifndef CLIB_MARCH_VARIANT
524 icmp_out2in (snat_main_t * sm,
527 icmp46_header_t * icmp0,
530 vlib_node_runtime_t * node,
531 u32 next0, u32 thread_index, void *d, void *e)
533 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
534 snat_session_key_t sm0;
536 icmp_echo_header_t *echo0, *inner_echo0 = 0;
537 ip4_header_t *inner_ip0 = 0;
539 icmp46_header_t *inner_icmp0;
541 u32 new_addr0, old_addr0;
542 u16 old_id0, new_id0;
547 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
549 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
550 &protocol, &sm0, &dont_translate, d,
554 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
557 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
560 ip_incremental_checksum_buffer (tsm->vlib_main, b0,
562 (u8 *) vlib_buffer_get_current (b0),
563 ntohs (ip0->length) -
564 ip4_header_bytes (ip0), 0);
565 checksum0 = ~ip_csum_fold (sum0);
566 if (checksum0 != 0 && checksum0 != 0xffff)
568 next0 = SNAT_OUT2IN_NEXT_DROP;
573 old_addr0 = ip0->dst_address.as_u32;
574 new_addr0 = ip0->dst_address.as_u32 = sm0.addr.as_u32;
575 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
577 sum0 = ip0->checksum;
578 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
579 dst_address /* changed member */ );
580 ip0->checksum = ip_csum_fold (sum0);
583 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
585 if (icmp0->checksum == 0)
586 icmp0->checksum = 0xffff;
588 if (!icmp_type_is_error_message (icmp0->type))
591 if (PREDICT_FALSE (new_id0 != echo0->identifier))
593 old_id0 = echo0->identifier;
595 echo0->identifier = new_id0;
597 sum0 = icmp0->checksum;
599 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
600 identifier /* changed member */ );
601 icmp0->checksum = ip_csum_fold (sum0);
606 inner_ip0 = (ip4_header_t *) (echo0 + 1);
607 l4_header = ip4_next_header (inner_ip0);
609 if (!ip4_header_checksum_is_valid (inner_ip0))
611 next0 = SNAT_OUT2IN_NEXT_DROP;
615 old_addr0 = inner_ip0->src_address.as_u32;
616 inner_ip0->src_address = sm0.addr;
617 new_addr0 = inner_ip0->src_address.as_u32;
619 sum0 = icmp0->checksum;
620 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
621 src_address /* changed member */ );
622 icmp0->checksum = ip_csum_fold (sum0);
626 case SNAT_PROTOCOL_ICMP:
627 inner_icmp0 = (icmp46_header_t *) l4_header;
628 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
630 old_id0 = inner_echo0->identifier;
632 inner_echo0->identifier = new_id0;
634 sum0 = icmp0->checksum;
636 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
638 icmp0->checksum = ip_csum_fold (sum0);
640 case SNAT_PROTOCOL_UDP:
641 case SNAT_PROTOCOL_TCP:
642 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
644 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
646 sum0 = icmp0->checksum;
647 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
649 icmp0->checksum = ip_csum_fold (sum0);
663 icmp_out2in_slow_path (snat_main_t * sm,
666 icmp46_header_t * icmp0,
669 vlib_node_runtime_t * node,
671 u32 thread_index, snat_session_t ** p_s0)
673 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
674 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
675 next0, thread_index, p_s0, 0);
676 snat_session_t *s0 = *p_s0;
677 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
680 nat44_session_update_counters (s0, now,
681 vlib_buffer_length_in_chain
682 (tsm->vlib_main, b0), thread_index);
683 /* Per-user LRU list maintenance */
684 nat44_session_update_lru (sm, s0, thread_index);
690 nat_out2in_sm_unknown_proto (snat_main_t * sm,
692 ip4_header_t * ip, u32 rx_fib_index)
694 clib_bihash_kv_8_8_t kv, value;
695 snat_static_mapping_t *m;
696 snat_session_key_t m_key;
697 u32 old_addr, new_addr;
700 m_key.addr = ip->dst_address;
704 kv.key = m_key.as_u64;
705 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
708 m = pool_elt_at_index (sm->static_mappings, value.value);
710 old_addr = ip->dst_address.as_u32;
711 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
713 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
714 ip->checksum = ip_csum_fold (sum);
716 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
720 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
721 vlib_node_runtime_t * node,
722 vlib_frame_t * frame)
724 u32 n_left_from, *from, *to_next;
725 snat_out2in_next_t next_index;
726 u32 pkts_processed = 0;
727 snat_main_t *sm = &snat_main;
728 f64 now = vlib_time_now (vm);
729 u32 thread_index = vm->thread_index;
730 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
731 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
734 from = vlib_frame_vector_args (frame);
735 n_left_from = frame->n_vectors;
736 next_index = node->cached_next_index;
738 while (n_left_from > 0)
742 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
744 while (n_left_from >= 4 && n_left_to_next >= 2)
747 vlib_buffer_t *b0, *b1;
748 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
749 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
750 u32 sw_if_index0, sw_if_index1;
751 ip4_header_t *ip0, *ip1;
752 ip_csum_t sum0, sum1;
753 u32 new_addr0, old_addr0;
754 u16 new_port0, old_port0;
755 u32 new_addr1, old_addr1;
756 u16 new_port1, old_port1;
757 udp_header_t *udp0, *udp1;
758 tcp_header_t *tcp0, *tcp1;
759 icmp46_header_t *icmp0, *icmp1;
760 snat_session_key_t key0, key1, sm0, sm1;
761 u32 rx_fib_index0, rx_fib_index1;
763 snat_session_t *s0 = 0, *s1 = 0;
764 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
765 u8 identity_nat0, identity_nat1;
767 /* Prefetch next iteration. */
769 vlib_buffer_t *p2, *p3;
771 p2 = vlib_get_buffer (vm, from[2]);
772 p3 = vlib_get_buffer (vm, from[3]);
774 vlib_prefetch_buffer_header (p2, LOAD);
775 vlib_prefetch_buffer_header (p3, LOAD);
777 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
778 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
781 /* speculatively enqueue b0 and b1 to the current next frame */
782 to_next[0] = bi0 = from[0];
783 to_next[1] = bi1 = from[1];
789 b0 = vlib_get_buffer (vm, bi0);
790 b1 = vlib_get_buffer (vm, bi1);
792 vnet_buffer (b0)->snat.flags = 0;
793 vnet_buffer (b1)->snat.flags = 0;
795 ip0 = vlib_buffer_get_current (b0);
796 udp0 = ip4_next_header (ip0);
797 tcp0 = (tcp_header_t *) udp0;
798 icmp0 = (icmp46_header_t *) udp0;
800 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
801 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
804 if (PREDICT_FALSE (ip0->ttl == 1))
806 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
807 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
808 ICMP4_time_exceeded_ttl_exceeded_in_transit,
810 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
814 proto0 = ip_proto_to_snat_proto (ip0->protocol);
816 if (PREDICT_FALSE (proto0 == ~0))
818 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
820 if (!sm->forwarding_enabled)
823 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
824 next0 = SNAT_OUT2IN_NEXT_DROP;
831 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
833 next0 = icmp_out2in_slow_path
834 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
835 next0, now, thread_index, &s0);
840 key0.addr = ip0->dst_address;
841 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
842 key0.protocol = proto0;
843 key0.fib_index = rx_fib_index0;
845 kv0.key = key0.as_u64;
847 if (clib_bihash_search_8_8
848 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
850 /* Try to match static mapping by external address and port,
851 destination address and port in packet */
852 if (snat_static_mapping_match
853 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
856 * Send DHCP packets to the ipv4 stack, or we won't
857 * be able to use dhcp client on the outside interface
860 (proto0 == SNAT_PROTOCOL_UDP
861 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
863 (UDP_DST_PORT_dhcp_to_client))))
865 vnet_feature_next (&next0, b0);
869 if (!sm->forwarding_enabled)
872 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
873 next0 = SNAT_OUT2IN_NEXT_DROP;
878 if (PREDICT_FALSE (identity_nat0))
881 /* Create session initiated by host from external network */
882 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
886 next0 = SNAT_OUT2IN_NEXT_DROP;
891 s0 = pool_elt_at_index (tsm->sessions, value0.value);
893 old_addr0 = ip0->dst_address.as_u32;
894 ip0->dst_address = s0->in2out.addr;
895 new_addr0 = ip0->dst_address.as_u32;
896 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
898 sum0 = ip0->checksum;
899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
901 dst_address /* changed member */ );
902 ip0->checksum = ip_csum_fold (sum0);
904 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
906 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
908 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
909 new_port0 = udp0->dst_port = s0->in2out.port;
910 sum0 = tcp0->checksum;
911 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
913 dst_address /* changed member */ );
915 sum0 = ip_csum_update (sum0, old_port0, new_port0,
916 ip4_header_t /* cheat */ ,
917 length /* changed member */ );
918 tcp0->checksum = ip_csum_fold (sum0);
924 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
926 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
927 new_port0 = udp0->dst_port = s0->in2out.port;
928 if (PREDICT_FALSE (udp0->checksum))
930 sum0 = udp0->checksum;
931 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
934 ip_csum_update (sum0, old_port0, new_port0,
935 ip4_header_t /* cheat */ ,
936 length /* changed member */ );
937 udp0->checksum = ip_csum_fold (sum0);
944 nat44_session_update_counters (s0, now,
945 vlib_buffer_length_in_chain (vm, b0),
947 /* Per-user LRU list maintenance */
948 nat44_session_update_lru (sm, s0, thread_index);
951 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
952 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
954 snat_out2in_trace_t *t =
955 vlib_add_trace (vm, node, b0, sizeof (*t));
956 t->sw_if_index = sw_if_index0;
957 t->next_index = next0;
958 t->session_index = ~0;
961 s0 - sm->per_thread_data[thread_index].sessions;
964 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
967 ip1 = vlib_buffer_get_current (b1);
968 udp1 = ip4_next_header (ip1);
969 tcp1 = (tcp_header_t *) udp1;
970 icmp1 = (icmp46_header_t *) udp1;
972 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
973 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
976 if (PREDICT_FALSE (ip1->ttl == 1))
978 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
979 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
980 ICMP4_time_exceeded_ttl_exceeded_in_transit,
982 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
986 proto1 = ip_proto_to_snat_proto (ip1->protocol);
988 if (PREDICT_FALSE (proto1 == ~0))
990 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
992 if (!sm->forwarding_enabled)
995 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
996 next1 = SNAT_OUT2IN_NEXT_DROP;
1003 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1005 next1 = icmp_out2in_slow_path
1006 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1007 next1, now, thread_index, &s1);
1012 key1.addr = ip1->dst_address;
1013 key1.port = vnet_buffer (b1)->ip.reass.l4_dst_port;
1014 key1.protocol = proto1;
1015 key1.fib_index = rx_fib_index1;
1017 kv1.key = key1.as_u64;
1019 if (clib_bihash_search_8_8
1020 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1022 /* Try to match static mapping by external address and port,
1023 destination address and port in packet */
1024 if (snat_static_mapping_match
1025 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
1028 * Send DHCP packets to the ipv4 stack, or we won't
1029 * be able to use dhcp client on the outside interface
1032 (proto1 == SNAT_PROTOCOL_UDP
1033 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1034 clib_host_to_net_u16
1035 (UDP_DST_PORT_dhcp_to_client))))
1037 vnet_feature_next (&next1, b1);
1041 if (!sm->forwarding_enabled)
1044 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1045 next1 = SNAT_OUT2IN_NEXT_DROP;
1050 if (PREDICT_FALSE (identity_nat1))
1053 /* Create session initiated by host from external network */
1054 s1 = create_session_for_static_mapping (sm, b1, sm1, key1, node,
1058 next1 = SNAT_OUT2IN_NEXT_DROP;
1064 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1067 old_addr1 = ip1->dst_address.as_u32;
1068 ip1->dst_address = s1->in2out.addr;
1069 new_addr1 = ip1->dst_address.as_u32;
1070 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1072 sum1 = ip1->checksum;
1073 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1075 dst_address /* changed member */ );
1076 ip1->checksum = ip_csum_fold (sum1);
1078 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1080 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1082 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1083 new_port1 = udp1->dst_port = s1->in2out.port;
1085 sum1 = tcp1->checksum;
1086 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1088 dst_address /* changed member */ );
1090 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1091 ip4_header_t /* cheat */ ,
1092 length /* changed member */ );
1093 tcp1->checksum = ip_csum_fold (sum1);
1099 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1101 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1102 new_port1 = udp1->dst_port = s1->in2out.port;
1103 if (PREDICT_FALSE (udp1->checksum))
1106 sum1 = udp1->checksum;
1108 ip_csum_update (sum1, old_addr1, new_addr1,
1110 dst_address /* changed member */ );
1112 ip_csum_update (sum1, old_port1, new_port1,
1113 ip4_header_t /* cheat */ ,
1114 length /* changed member */ );
1115 udp1->checksum = ip_csum_fold (sum1);
1122 nat44_session_update_counters (s1, now,
1123 vlib_buffer_length_in_chain (vm, b1),
1125 /* Per-user LRU list maintenance */
1126 nat44_session_update_lru (sm, s1, thread_index);
1129 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1130 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1132 snat_out2in_trace_t *t =
1133 vlib_add_trace (vm, node, b1, sizeof (*t));
1134 t->sw_if_index = sw_if_index1;
1135 t->next_index = next1;
1136 t->session_index = ~0;
1139 s1 - sm->per_thread_data[thread_index].sessions;
1142 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1144 /* verify speculative enqueues, maybe switch current next frame */
1145 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1146 to_next, n_left_to_next,
1147 bi0, bi1, next0, next1);
1150 while (n_left_from > 0 && n_left_to_next > 0)
1154 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1158 u32 new_addr0, old_addr0;
1159 u16 new_port0, old_port0;
1162 icmp46_header_t *icmp0;
1163 snat_session_key_t key0, sm0;
1166 snat_session_t *s0 = 0;
1167 clib_bihash_kv_8_8_t kv0, value0;
1170 /* speculatively enqueue b0 to the current next frame */
1176 n_left_to_next -= 1;
1178 b0 = vlib_get_buffer (vm, bi0);
1180 vnet_buffer (b0)->snat.flags = 0;
1182 ip0 = vlib_buffer_get_current (b0);
1183 udp0 = ip4_next_header (ip0);
1184 tcp0 = (tcp_header_t *) udp0;
1185 icmp0 = (icmp46_header_t *) udp0;
1187 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1188 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1191 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1193 if (PREDICT_FALSE (proto0 == ~0))
1195 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1197 if (!sm->forwarding_enabled)
1200 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1201 next0 = SNAT_OUT2IN_NEXT_DROP;
1208 if (PREDICT_FALSE (ip0->ttl == 1))
1210 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1211 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1212 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1214 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1218 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1220 next0 = icmp_out2in_slow_path
1221 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1222 next0, now, thread_index, &s0);
1227 key0.addr = ip0->dst_address;
1228 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1229 key0.protocol = proto0;
1230 key0.fib_index = rx_fib_index0;
1232 kv0.key = key0.as_u64;
1234 if (clib_bihash_search_8_8
1235 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1237 /* Try to match static mapping by external address and port,
1238 destination address and port in packet */
1239 if (snat_static_mapping_match
1240 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1243 * Send DHCP packets to the ipv4 stack, or we won't
1244 * be able to use dhcp client on the outside interface
1247 (proto0 == SNAT_PROTOCOL_UDP
1248 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1249 clib_host_to_net_u16
1250 (UDP_DST_PORT_dhcp_to_client))))
1252 vnet_feature_next (&next0, b0);
1256 if (!sm->forwarding_enabled)
1259 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1260 next0 = SNAT_OUT2IN_NEXT_DROP;
1265 if (PREDICT_FALSE (identity_nat0))
1268 /* Create session initiated by host from external network */
1269 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
1273 next0 = SNAT_OUT2IN_NEXT_DROP;
1279 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1282 old_addr0 = ip0->dst_address.as_u32;
1283 ip0->dst_address = s0->in2out.addr;
1284 new_addr0 = ip0->dst_address.as_u32;
1285 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1287 sum0 = ip0->checksum;
1288 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1290 dst_address /* changed member */ );
1291 ip0->checksum = ip_csum_fold (sum0);
1293 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1295 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1297 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1298 new_port0 = udp0->dst_port = s0->in2out.port;
1300 sum0 = tcp0->checksum;
1301 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1303 dst_address /* changed member */ );
1305 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1306 ip4_header_t /* cheat */ ,
1307 length /* changed member */ );
1308 tcp0->checksum = ip_csum_fold (sum0);
1314 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1316 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1317 new_port0 = udp0->dst_port = s0->in2out.port;
1318 if (PREDICT_FALSE (udp0->checksum))
1320 sum0 = udp0->checksum;
1321 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1324 ip_csum_update (sum0, old_port0, new_port0,
1325 ip4_header_t /* cheat */ ,
1326 length /* changed member */ );
1327 udp0->checksum = ip_csum_fold (sum0);
1334 nat44_session_update_counters (s0, now,
1335 vlib_buffer_length_in_chain (vm, b0),
1337 /* Per-user LRU list maintenance */
1338 nat44_session_update_lru (sm, s0, thread_index);
1341 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1342 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1344 snat_out2in_trace_t *t =
1345 vlib_add_trace (vm, node, b0, sizeof (*t));
1346 t->sw_if_index = sw_if_index0;
1347 t->next_index = next0;
1348 t->session_index = ~0;
1351 s0 - sm->per_thread_data[thread_index].sessions;
1354 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1356 /* verify speculative enqueue, maybe switch current next frame */
1357 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1358 to_next, n_left_to_next,
1362 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1365 vlib_node_increment_counter (vm, sm->out2in_node_index,
1366 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1368 vlib_node_increment_counter (vm, sm->out2in_node_index,
1369 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1370 vlib_node_increment_counter (vm, sm->out2in_node_index,
1371 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1372 vlib_node_increment_counter (vm, sm->out2in_node_index,
1373 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1374 vlib_node_increment_counter (vm, sm->out2in_node_index,
1375 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1377 vlib_node_increment_counter (vm, sm->out2in_node_index,
1378 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1380 return frame->n_vectors;
1384 VLIB_REGISTER_NODE (snat_out2in_node) = {
1385 .name = "nat44-out2in",
1386 .vector_size = sizeof (u32),
1387 .format_trace = format_snat_out2in_trace,
1388 .type = VLIB_NODE_TYPE_INTERNAL,
1390 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1391 .error_strings = snat_out2in_error_strings,
1393 .runtime_data_bytes = sizeof (snat_runtime_t),
1395 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1397 /* edit / add dispositions here */
1399 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1400 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1401 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1406 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1407 vlib_node_runtime_t * node,
1408 vlib_frame_t * frame)
1410 u32 n_left_from, *from, *to_next;
1411 snat_out2in_next_t next_index;
1412 u32 pkts_processed = 0;
1413 snat_main_t *sm = &snat_main;
1415 from = vlib_frame_vector_args (frame);
1416 n_left_from = frame->n_vectors;
1417 next_index = node->cached_next_index;
1419 while (n_left_from > 0)
1423 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1425 while (n_left_from > 0 && n_left_to_next > 0)
1429 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1433 u32 new_addr0, old_addr0;
1434 u16 new_port0, old_port0;
1437 icmp46_header_t *icmp0;
1438 snat_session_key_t key0, sm0;
1442 /* speculatively enqueue b0 to the current next frame */
1448 n_left_to_next -= 1;
1450 b0 = vlib_get_buffer (vm, bi0);
1452 ip0 = vlib_buffer_get_current (b0);
1453 udp0 = ip4_next_header (ip0);
1454 tcp0 = (tcp_header_t *) udp0;
1455 icmp0 = (icmp46_header_t *) udp0;
1457 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1459 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1461 vnet_feature_next (&next0, b0);
1463 if (PREDICT_FALSE (ip0->ttl == 1))
1465 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1466 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1467 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1469 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1473 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1475 if (PREDICT_FALSE (proto0 == ~0))
1478 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1480 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1481 rx_fib_index0, node, next0, ~0, 0, 0);
1485 key0.addr = ip0->dst_address;
1486 key0.port = udp0->dst_port;
1487 key0.fib_index = rx_fib_index0;
1489 if (snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
1491 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1495 new_addr0 = sm0.addr.as_u32;
1496 new_port0 = sm0.port;
1497 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1498 old_addr0 = ip0->dst_address.as_u32;
1499 ip0->dst_address.as_u32 = new_addr0;
1501 sum0 = ip0->checksum;
1502 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1504 dst_address /* changed member */ );
1505 ip0->checksum = ip_csum_fold (sum0);
1507 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1509 old_port0 = udp0->dst_port;
1510 udp0->dst_port = new_port0;
1512 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1514 sum0 = tcp0->checksum;
1515 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1517 dst_address /* changed member */ );
1518 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1519 ip4_header_t /* cheat */ ,
1520 length /* changed member */ );
1521 tcp0->checksum = ip_csum_fold (sum0);
1523 else if (udp0->checksum)
1525 sum0 = udp0->checksum;
1526 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1528 dst_address /* changed member */ );
1529 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1530 ip4_header_t /* cheat */ ,
1531 length /* changed member */ );
1532 udp0->checksum = ip_csum_fold (sum0);
1537 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1539 sum0 = tcp0->checksum;
1540 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1542 dst_address /* changed member */ );
1543 tcp0->checksum = ip_csum_fold (sum0);
1545 else if (udp0->checksum)
1547 sum0 = udp0->checksum;
1548 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1550 dst_address /* changed member */ );
1551 udp0->checksum = ip_csum_fold (sum0);
1557 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1558 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1560 snat_out2in_trace_t *t =
1561 vlib_add_trace (vm, node, b0, sizeof (*t));
1562 t->sw_if_index = sw_if_index0;
1563 t->next_index = next0;
1566 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1568 /* verify speculative enqueue, maybe switch current next frame */
1569 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1570 to_next, n_left_to_next,
1574 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1577 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1578 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1580 return frame->n_vectors;
1584 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1585 .name = "nat44-out2in-fast",
1586 .vector_size = sizeof (u32),
1587 .format_trace = format_snat_out2in_fast_trace,
1588 .type = VLIB_NODE_TYPE_INTERNAL,
1590 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1591 .error_strings = snat_out2in_error_strings,
1593 .runtime_data_bytes = sizeof (snat_runtime_t),
1595 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1597 /* edit / add dispositions here */
1599 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1600 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1601 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1607 * fd.io coding-style-patch-verification: ON
1610 * eval: (c-set-style "gnu")