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 = NAT_PROTOCOL_ICMP;
282 key0.addr = ip0->dst_address;
283 key0.port = vnet_buffer (b)->ip.reass.l4_src_port;
287 inner_ip0 = (ip4_header_t *) (echo0 + 1);
288 l4_header = ip4_next_header (inner_ip0);
289 key0.protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
290 key0.addr = inner_ip0->src_address;
291 switch (key0.protocol)
293 case NAT_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 NAT_PROTOCOL_UDP:
299 case NAT_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;
344 vlib_main_t *vm = vlib_get_main ();
346 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
347 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
351 err = icmp_get_key (b0, ip0, &key0);
354 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
355 next0 = SNAT_OUT2IN_NEXT_DROP;
358 key0.fib_index = rx_fib_index0;
360 kv0.key = key0.as_u64;
362 if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
364 /* Try to match static mapping by external address and port,
365 destination address and port in packet */
366 if (snat_static_mapping_match
367 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
369 if (!sm->forwarding_enabled)
371 /* Don't NAT packet aimed at the intfc address */
372 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
373 ip0->dst_address.as_u32)))
378 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
379 next0 = SNAT_OUT2IN_NEXT_DROP;
390 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
392 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
393 ICMP4_echo_request || !is_addr_only)))
395 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
396 next0 = SNAT_OUT2IN_NEXT_DROP;
400 if (PREDICT_FALSE (identity_nat))
405 /* Create session initiated by host from external network */
406 s0 = create_session_for_static_mapping (sm, b0, sm0, key0,
412 next0 = SNAT_OUT2IN_NEXT_DROP;
419 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
421 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
423 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
424 reass.icmp_type_or_tcp_flags)))
426 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
427 next0 = SNAT_OUT2IN_NEXT_DROP;
431 s0 = pool_elt_at_index (tsm->sessions, value0.value);
435 *p_proto = key0.protocol;
437 *p_value = s0->in2out;
438 *p_dont_translate = dont_translate;
440 *(snat_session_t **) d = s0;
445 #ifndef CLIB_MARCH_VARIANT
447 * Get address and port values to be used for ICMP packet translation
449 * @param[in] sm NAT main
450 * @param[in,out] node NAT node runtime
451 * @param[in] thread_index thread index
452 * @param[in,out] b0 buffer containing packet to be translated
453 * @param[in,out] ip0 ip header
454 * @param[out] p_proto protocol used for matching
455 * @param[out] p_value address and port after NAT translation
456 * @param[out] p_dont_translate if packet should not be translated
457 * @param d optional parameter
458 * @param e optional parameter
461 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
462 u32 thread_index, vlib_buffer_t * b0,
463 ip4_header_t * ip0, u8 * p_proto,
464 snat_session_key_t * p_value,
465 u8 * p_dont_translate, void *d, void *e)
469 snat_session_key_t key0;
470 snat_session_key_t sm0;
471 u8 dont_translate = 0;
476 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
477 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
479 err = icmp_get_key (b0, ip0, &key0);
482 b0->error = node->errors[err];
483 next0 = SNAT_OUT2IN_NEXT_DROP;
486 key0.fib_index = rx_fib_index0;
488 if (snat_static_mapping_match
489 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, 0))
491 /* Don't NAT packet aimed at the intfc address */
492 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
497 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
498 next0 = SNAT_OUT2IN_NEXT_DROP;
503 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
504 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
505 ICMP4_echo_request || !is_addr_only)
506 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
507 reass.icmp_type_or_tcp_flags)))
509 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
510 next0 = SNAT_OUT2IN_NEXT_DROP;
517 *p_proto = key0.protocol;
518 *p_dont_translate = dont_translate;
523 #ifndef CLIB_MARCH_VARIANT
525 icmp_out2in (snat_main_t * sm,
528 icmp46_header_t * icmp0,
531 vlib_node_runtime_t * node,
532 u32 next0, u32 thread_index, void *d, void *e)
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;
546 vlib_main_t *vm = vlib_get_main ();
548 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
550 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
551 &protocol, &sm0, &dont_translate, d,
555 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
558 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
561 ip_incremental_checksum_buffer (vm, b0,
563 (u8 *) vlib_buffer_get_current (b0),
564 ntohs (ip0->length) -
565 ip4_header_bytes (ip0), 0);
566 checksum0 = ~ip_csum_fold (sum0);
567 if (checksum0 != 0 && checksum0 != 0xffff)
569 next0 = SNAT_OUT2IN_NEXT_DROP;
574 old_addr0 = ip0->dst_address.as_u32;
575 new_addr0 = ip0->dst_address.as_u32 = sm0.addr.as_u32;
576 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
578 sum0 = ip0->checksum;
579 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
580 dst_address /* changed member */ );
581 ip0->checksum = ip_csum_fold (sum0);
584 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
586 if (icmp0->checksum == 0)
587 icmp0->checksum = 0xffff;
589 if (!icmp_type_is_error_message (icmp0->type))
592 if (PREDICT_FALSE (new_id0 != echo0->identifier))
594 old_id0 = echo0->identifier;
596 echo0->identifier = new_id0;
598 sum0 = icmp0->checksum;
600 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
601 identifier /* changed member */ );
602 icmp0->checksum = ip_csum_fold (sum0);
607 inner_ip0 = (ip4_header_t *) (echo0 + 1);
608 l4_header = ip4_next_header (inner_ip0);
610 if (!ip4_header_checksum_is_valid (inner_ip0))
612 next0 = SNAT_OUT2IN_NEXT_DROP;
616 old_addr0 = inner_ip0->src_address.as_u32;
617 inner_ip0->src_address = sm0.addr;
618 new_addr0 = inner_ip0->src_address.as_u32;
620 sum0 = icmp0->checksum;
621 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
622 src_address /* changed member */ );
623 icmp0->checksum = ip_csum_fold (sum0);
627 case NAT_PROTOCOL_ICMP:
628 inner_icmp0 = (icmp46_header_t *) l4_header;
629 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
631 old_id0 = inner_echo0->identifier;
633 inner_echo0->identifier = new_id0;
635 sum0 = icmp0->checksum;
637 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
639 icmp0->checksum = ip_csum_fold (sum0);
641 case NAT_PROTOCOL_UDP:
642 case NAT_PROTOCOL_TCP:
643 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
645 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
647 sum0 = icmp0->checksum;
648 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
650 icmp0->checksum = ip_csum_fold (sum0);
664 icmp_out2in_slow_path (snat_main_t * sm,
667 icmp46_header_t * icmp0,
670 vlib_node_runtime_t * node,
672 u32 thread_index, snat_session_t ** p_s0)
674 vlib_main_t *vm = vlib_get_main ();
676 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
677 next0, thread_index, p_s0, 0);
678 snat_session_t *s0 = *p_s0;
679 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
682 nat44_session_update_counters (s0, now,
683 vlib_buffer_length_in_chain
684 (vm, b0), thread_index);
685 /* Per-user LRU list maintenance */
686 nat44_session_update_lru (sm, s0, thread_index);
692 nat_out2in_sm_unknown_proto (snat_main_t * sm,
694 ip4_header_t * ip, u32 rx_fib_index)
696 clib_bihash_kv_8_8_t kv, value;
697 snat_static_mapping_t *m;
698 snat_session_key_t m_key;
699 u32 old_addr, new_addr;
702 m_key.addr = ip->dst_address;
706 kv.key = m_key.as_u64;
707 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
710 m = pool_elt_at_index (sm->static_mappings, value.value);
712 old_addr = ip->dst_address.as_u32;
713 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
715 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
716 ip->checksum = ip_csum_fold (sum);
718 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
722 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
723 vlib_node_runtime_t * node,
724 vlib_frame_t * frame)
726 u32 n_left_from, *from, *to_next;
727 snat_out2in_next_t next_index;
728 u32 pkts_processed = 0;
729 snat_main_t *sm = &snat_main;
730 f64 now = vlib_time_now (vm);
731 u32 thread_index = vm->thread_index;
732 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
733 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
736 from = vlib_frame_vector_args (frame);
737 n_left_from = frame->n_vectors;
738 next_index = node->cached_next_index;
740 while (n_left_from > 0)
744 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
746 while (n_left_from >= 4 && n_left_to_next >= 2)
749 vlib_buffer_t *b0, *b1;
750 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
751 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
752 u32 sw_if_index0, sw_if_index1;
753 ip4_header_t *ip0, *ip1;
754 ip_csum_t sum0, sum1;
755 u32 new_addr0, old_addr0;
756 u16 new_port0, old_port0;
757 u32 new_addr1, old_addr1;
758 u16 new_port1, old_port1;
759 udp_header_t *udp0, *udp1;
760 tcp_header_t *tcp0, *tcp1;
761 icmp46_header_t *icmp0, *icmp1;
762 snat_session_key_t key0, key1, sm0, sm1;
763 u32 rx_fib_index0, rx_fib_index1;
765 snat_session_t *s0 = 0, *s1 = 0;
766 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
767 u8 identity_nat0, identity_nat1;
769 /* Prefetch next iteration. */
771 vlib_buffer_t *p2, *p3;
773 p2 = vlib_get_buffer (vm, from[2]);
774 p3 = vlib_get_buffer (vm, from[3]);
776 vlib_prefetch_buffer_header (p2, LOAD);
777 vlib_prefetch_buffer_header (p3, LOAD);
779 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
780 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
783 /* speculatively enqueue b0 and b1 to the current next frame */
784 to_next[0] = bi0 = from[0];
785 to_next[1] = bi1 = from[1];
791 b0 = vlib_get_buffer (vm, bi0);
792 b1 = vlib_get_buffer (vm, bi1);
794 vnet_buffer (b0)->snat.flags = 0;
795 vnet_buffer (b1)->snat.flags = 0;
797 ip0 = vlib_buffer_get_current (b0);
798 udp0 = ip4_next_header (ip0);
799 tcp0 = (tcp_header_t *) udp0;
800 icmp0 = (icmp46_header_t *) udp0;
802 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
803 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
806 if (PREDICT_FALSE (ip0->ttl == 1))
808 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
809 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
810 ICMP4_time_exceeded_ttl_exceeded_in_transit,
812 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
816 proto0 = ip_proto_to_nat_proto (ip0->protocol);
818 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
820 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
822 if (!sm->forwarding_enabled)
825 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
826 next0 = SNAT_OUT2IN_NEXT_DROP;
833 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
835 next0 = icmp_out2in_slow_path
836 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
837 next0, now, thread_index, &s0);
842 key0.addr = ip0->dst_address;
843 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
844 key0.protocol = proto0;
845 key0.fib_index = rx_fib_index0;
847 kv0.key = key0.as_u64;
849 if (clib_bihash_search_8_8
850 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
852 /* Try to match static mapping by external address and port,
853 destination address and port in packet */
854 if (snat_static_mapping_match
855 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
858 * Send DHCP packets to the ipv4 stack, or we won't
859 * be able to use dhcp client on the outside interface
862 (proto0 == NAT_PROTOCOL_UDP
863 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
865 (UDP_DST_PORT_dhcp_to_client))))
867 vnet_feature_next (&next0, b0);
871 if (!sm->forwarding_enabled)
874 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
875 next0 = SNAT_OUT2IN_NEXT_DROP;
880 if (PREDICT_FALSE (identity_nat0))
883 /* Create session initiated by host from external network */
884 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
888 next0 = SNAT_OUT2IN_NEXT_DROP;
893 s0 = pool_elt_at_index (tsm->sessions, value0.value);
895 old_addr0 = ip0->dst_address.as_u32;
896 ip0->dst_address = s0->in2out.addr;
897 new_addr0 = ip0->dst_address.as_u32;
898 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
900 sum0 = ip0->checksum;
901 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
903 dst_address /* changed member */ );
904 ip0->checksum = ip_csum_fold (sum0);
906 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
908 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
910 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
911 new_port0 = udp0->dst_port = s0->in2out.port;
912 sum0 = tcp0->checksum;
913 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
915 dst_address /* changed member */ );
917 sum0 = ip_csum_update (sum0, old_port0, new_port0,
918 ip4_header_t /* cheat */ ,
919 length /* changed member */ );
920 tcp0->checksum = ip_csum_fold (sum0);
926 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
928 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
929 new_port0 = udp0->dst_port = s0->in2out.port;
930 if (PREDICT_FALSE (udp0->checksum))
932 sum0 = udp0->checksum;
933 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
936 ip_csum_update (sum0, old_port0, new_port0,
937 ip4_header_t /* cheat */ ,
938 length /* changed member */ );
939 udp0->checksum = ip_csum_fold (sum0);
946 nat44_session_update_counters (s0, now,
947 vlib_buffer_length_in_chain (vm, b0),
949 /* Per-user LRU list maintenance */
950 nat44_session_update_lru (sm, s0, thread_index);
953 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
954 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
956 snat_out2in_trace_t *t =
957 vlib_add_trace (vm, node, b0, sizeof (*t));
958 t->sw_if_index = sw_if_index0;
959 t->next_index = next0;
960 t->session_index = ~0;
963 s0 - sm->per_thread_data[thread_index].sessions;
966 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
969 ip1 = vlib_buffer_get_current (b1);
970 udp1 = ip4_next_header (ip1);
971 tcp1 = (tcp_header_t *) udp1;
972 icmp1 = (icmp46_header_t *) udp1;
974 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
975 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
978 if (PREDICT_FALSE (ip1->ttl == 1))
980 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
981 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
982 ICMP4_time_exceeded_ttl_exceeded_in_transit,
984 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
988 proto1 = ip_proto_to_nat_proto (ip1->protocol);
990 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
992 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
994 if (!sm->forwarding_enabled)
997 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
998 next1 = SNAT_OUT2IN_NEXT_DROP;
1005 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1007 next1 = icmp_out2in_slow_path
1008 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1009 next1, now, thread_index, &s1);
1014 key1.addr = ip1->dst_address;
1015 key1.port = vnet_buffer (b1)->ip.reass.l4_dst_port;
1016 key1.protocol = proto1;
1017 key1.fib_index = rx_fib_index1;
1019 kv1.key = key1.as_u64;
1021 if (clib_bihash_search_8_8
1022 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1024 /* Try to match static mapping by external address and port,
1025 destination address and port in packet */
1026 if (snat_static_mapping_match
1027 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
1030 * Send DHCP packets to the ipv4 stack, or we won't
1031 * be able to use dhcp client on the outside interface
1034 (proto1 == NAT_PROTOCOL_UDP
1035 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1036 clib_host_to_net_u16
1037 (UDP_DST_PORT_dhcp_to_client))))
1039 vnet_feature_next (&next1, b1);
1043 if (!sm->forwarding_enabled)
1046 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1047 next1 = SNAT_OUT2IN_NEXT_DROP;
1052 if (PREDICT_FALSE (identity_nat1))
1055 /* Create session initiated by host from external network */
1056 s1 = create_session_for_static_mapping (sm, b1, sm1, key1, node,
1060 next1 = SNAT_OUT2IN_NEXT_DROP;
1066 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1069 old_addr1 = ip1->dst_address.as_u32;
1070 ip1->dst_address = s1->in2out.addr;
1071 new_addr1 = ip1->dst_address.as_u32;
1072 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1074 sum1 = ip1->checksum;
1075 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1077 dst_address /* changed member */ );
1078 ip1->checksum = ip_csum_fold (sum1);
1080 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1082 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1084 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1085 new_port1 = udp1->dst_port = s1->in2out.port;
1087 sum1 = tcp1->checksum;
1088 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1090 dst_address /* changed member */ );
1092 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1093 ip4_header_t /* cheat */ ,
1094 length /* changed member */ );
1095 tcp1->checksum = ip_csum_fold (sum1);
1101 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1103 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1104 new_port1 = udp1->dst_port = s1->in2out.port;
1105 if (PREDICT_FALSE (udp1->checksum))
1108 sum1 = udp1->checksum;
1110 ip_csum_update (sum1, old_addr1, new_addr1,
1112 dst_address /* changed member */ );
1114 ip_csum_update (sum1, old_port1, new_port1,
1115 ip4_header_t /* cheat */ ,
1116 length /* changed member */ );
1117 udp1->checksum = ip_csum_fold (sum1);
1124 nat44_session_update_counters (s1, now,
1125 vlib_buffer_length_in_chain (vm, b1),
1127 /* Per-user LRU list maintenance */
1128 nat44_session_update_lru (sm, s1, thread_index);
1131 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1132 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1134 snat_out2in_trace_t *t =
1135 vlib_add_trace (vm, node, b1, sizeof (*t));
1136 t->sw_if_index = sw_if_index1;
1137 t->next_index = next1;
1138 t->session_index = ~0;
1141 s1 - sm->per_thread_data[thread_index].sessions;
1144 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1146 /* verify speculative enqueues, maybe switch current next frame */
1147 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1148 to_next, n_left_to_next,
1149 bi0, bi1, next0, next1);
1152 while (n_left_from > 0 && n_left_to_next > 0)
1156 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1160 u32 new_addr0, old_addr0;
1161 u16 new_port0, old_port0;
1164 icmp46_header_t *icmp0;
1165 snat_session_key_t key0, sm0;
1168 snat_session_t *s0 = 0;
1169 clib_bihash_kv_8_8_t kv0, value0;
1172 /* speculatively enqueue b0 to the current next frame */
1178 n_left_to_next -= 1;
1180 b0 = vlib_get_buffer (vm, bi0);
1182 vnet_buffer (b0)->snat.flags = 0;
1184 ip0 = vlib_buffer_get_current (b0);
1185 udp0 = ip4_next_header (ip0);
1186 tcp0 = (tcp_header_t *) udp0;
1187 icmp0 = (icmp46_header_t *) udp0;
1189 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1190 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1193 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1195 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1197 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1199 if (!sm->forwarding_enabled)
1202 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1203 next0 = SNAT_OUT2IN_NEXT_DROP;
1210 if (PREDICT_FALSE (ip0->ttl == 1))
1212 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1213 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1214 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1216 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1220 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1222 next0 = icmp_out2in_slow_path
1223 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1224 next0, now, thread_index, &s0);
1229 key0.addr = ip0->dst_address;
1230 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1231 key0.protocol = proto0;
1232 key0.fib_index = rx_fib_index0;
1234 kv0.key = key0.as_u64;
1236 if (clib_bihash_search_8_8
1237 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1239 /* Try to match static mapping by external address and port,
1240 destination address and port in packet */
1241 if (snat_static_mapping_match
1242 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1245 * Send DHCP packets to the ipv4 stack, or we won't
1246 * be able to use dhcp client on the outside interface
1249 (proto0 == NAT_PROTOCOL_UDP
1250 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1251 clib_host_to_net_u16
1252 (UDP_DST_PORT_dhcp_to_client))))
1254 vnet_feature_next (&next0, b0);
1258 if (!sm->forwarding_enabled)
1261 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1262 next0 = SNAT_OUT2IN_NEXT_DROP;
1267 if (PREDICT_FALSE (identity_nat0))
1270 /* Create session initiated by host from external network */
1271 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
1275 next0 = SNAT_OUT2IN_NEXT_DROP;
1281 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1284 old_addr0 = ip0->dst_address.as_u32;
1285 ip0->dst_address = s0->in2out.addr;
1286 new_addr0 = ip0->dst_address.as_u32;
1287 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1289 sum0 = ip0->checksum;
1290 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1292 dst_address /* changed member */ );
1293 ip0->checksum = ip_csum_fold (sum0);
1295 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1297 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1299 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1300 new_port0 = udp0->dst_port = s0->in2out.port;
1302 sum0 = tcp0->checksum;
1303 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1305 dst_address /* changed member */ );
1307 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1308 ip4_header_t /* cheat */ ,
1309 length /* changed member */ );
1310 tcp0->checksum = ip_csum_fold (sum0);
1316 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1318 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1319 new_port0 = udp0->dst_port = s0->in2out.port;
1320 if (PREDICT_FALSE (udp0->checksum))
1322 sum0 = udp0->checksum;
1323 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1326 ip_csum_update (sum0, old_port0, new_port0,
1327 ip4_header_t /* cheat */ ,
1328 length /* changed member */ );
1329 udp0->checksum = ip_csum_fold (sum0);
1336 nat44_session_update_counters (s0, now,
1337 vlib_buffer_length_in_chain (vm, b0),
1339 /* Per-user LRU list maintenance */
1340 nat44_session_update_lru (sm, s0, thread_index);
1343 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1344 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1346 snat_out2in_trace_t *t =
1347 vlib_add_trace (vm, node, b0, sizeof (*t));
1348 t->sw_if_index = sw_if_index0;
1349 t->next_index = next0;
1350 t->session_index = ~0;
1353 s0 - sm->per_thread_data[thread_index].sessions;
1356 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1358 /* verify speculative enqueue, maybe switch current next frame */
1359 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1360 to_next, n_left_to_next,
1364 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1367 vlib_node_increment_counter (vm, sm->out2in_node_index,
1368 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1370 vlib_node_increment_counter (vm, sm->out2in_node_index,
1371 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1372 vlib_node_increment_counter (vm, sm->out2in_node_index,
1373 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1374 vlib_node_increment_counter (vm, sm->out2in_node_index,
1375 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1376 vlib_node_increment_counter (vm, sm->out2in_node_index,
1377 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1379 vlib_node_increment_counter (vm, sm->out2in_node_index,
1380 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1382 return frame->n_vectors;
1386 VLIB_REGISTER_NODE (snat_out2in_node) = {
1387 .name = "nat44-out2in",
1388 .vector_size = sizeof (u32),
1389 .format_trace = format_snat_out2in_trace,
1390 .type = VLIB_NODE_TYPE_INTERNAL,
1392 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1393 .error_strings = snat_out2in_error_strings,
1395 .runtime_data_bytes = sizeof (snat_runtime_t),
1397 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1399 /* edit / add dispositions here */
1401 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1402 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1403 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1408 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1409 vlib_node_runtime_t * node,
1410 vlib_frame_t * frame)
1412 u32 n_left_from, *from, *to_next;
1413 snat_out2in_next_t next_index;
1414 u32 pkts_processed = 0;
1415 snat_main_t *sm = &snat_main;
1417 from = vlib_frame_vector_args (frame);
1418 n_left_from = frame->n_vectors;
1419 next_index = node->cached_next_index;
1421 while (n_left_from > 0)
1425 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1427 while (n_left_from > 0 && n_left_to_next > 0)
1431 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1435 u32 new_addr0, old_addr0;
1436 u16 new_port0, old_port0;
1439 icmp46_header_t *icmp0;
1440 snat_session_key_t key0, sm0;
1444 /* speculatively enqueue b0 to the current next frame */
1450 n_left_to_next -= 1;
1452 b0 = vlib_get_buffer (vm, bi0);
1454 ip0 = vlib_buffer_get_current (b0);
1455 udp0 = ip4_next_header (ip0);
1456 tcp0 = (tcp_header_t *) udp0;
1457 icmp0 = (icmp46_header_t *) udp0;
1459 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1461 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1463 vnet_feature_next (&next0, b0);
1465 if (PREDICT_FALSE (ip0->ttl == 1))
1467 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1468 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1469 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1471 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1475 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1477 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1480 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1482 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1483 rx_fib_index0, node, next0, ~0, 0, 0);
1487 key0.addr = ip0->dst_address;
1488 key0.port = udp0->dst_port;
1489 key0.fib_index = rx_fib_index0;
1491 if (snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
1493 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1497 new_addr0 = sm0.addr.as_u32;
1498 new_port0 = sm0.port;
1499 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1500 old_addr0 = ip0->dst_address.as_u32;
1501 ip0->dst_address.as_u32 = new_addr0;
1503 sum0 = ip0->checksum;
1504 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1506 dst_address /* changed member */ );
1507 ip0->checksum = ip_csum_fold (sum0);
1509 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1511 old_port0 = udp0->dst_port;
1512 udp0->dst_port = new_port0;
1514 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1516 sum0 = tcp0->checksum;
1517 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1519 dst_address /* changed member */ );
1520 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1521 ip4_header_t /* cheat */ ,
1522 length /* changed member */ );
1523 tcp0->checksum = ip_csum_fold (sum0);
1525 else if (udp0->checksum)
1527 sum0 = udp0->checksum;
1528 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1530 dst_address /* changed member */ );
1531 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1532 ip4_header_t /* cheat */ ,
1533 length /* changed member */ );
1534 udp0->checksum = ip_csum_fold (sum0);
1539 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1541 sum0 = tcp0->checksum;
1542 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1544 dst_address /* changed member */ );
1545 tcp0->checksum = ip_csum_fold (sum0);
1547 else if (udp0->checksum)
1549 sum0 = udp0->checksum;
1550 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1552 dst_address /* changed member */ );
1553 udp0->checksum = ip_csum_fold (sum0);
1559 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1560 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1562 snat_out2in_trace_t *t =
1563 vlib_add_trace (vm, node, b0, sizeof (*t));
1564 t->sw_if_index = sw_if_index0;
1565 t->next_index = next0;
1568 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1570 /* verify speculative enqueue, maybe switch current next frame */
1571 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1572 to_next, n_left_to_next,
1576 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1579 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1580 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1582 return frame->n_vectors;
1586 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1587 .name = "nat44-out2in-fast",
1588 .vector_size = sizeof (u32),
1589 .format_trace = format_snat_out2in_fast_trace,
1590 .type = VLIB_NODE_TYPE_INTERNAL,
1592 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1593 .error_strings = snat_out2in_error_strings,
1595 .runtime_data_bytes = sizeof (snat_runtime_t),
1597 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1599 /* edit / add dispositions here */
1601 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1602 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1603 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1609 * fd.io coding-style-patch-verification: ON
1612 * eval: (c-set-style "gnu")