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 nat44_session_try_cleanup (&in2out.addr, in2out.fib_index, thread_index,
194 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
196 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
197 nat_elog_notice ("maximum sessions exceeded");
201 ip0 = vlib_buffer_get_current (b0);
202 udp0 = ip4_next_header (ip0);
205 nat_user_get_or_create (sm, &in2out.addr, in2out.fib_index, thread_index);
208 nat_elog_warn ("create NAT user failed");
212 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
215 nat44_delete_user_with_no_session (sm, u, thread_index);
216 nat_elog_warn ("create NAT session failed");
220 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
221 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
222 s->ext_host_port = udp0->src_port;
223 user_session_increment (sm, u, 1 /* static */ );
226 s->in2out.protocol = out2in.protocol;
228 /* Add to translation hashes */
230 ctx0.thread_index = thread_index;
231 kv0.key = s->in2out.as_u64;
232 kv0.value = s - sm->per_thread_data[thread_index].sessions;
233 if (clib_bihash_add_or_overwrite_stale_8_8
234 (&sm->per_thread_data[thread_index].in2out, &kv0,
235 nat44_i2o_is_idle_session_cb, &ctx0))
236 nat_elog_notice ("in2out key add failed");
238 kv0.key = s->out2in.as_u64;
240 if (clib_bihash_add_or_overwrite_stale_8_8
241 (&sm->per_thread_data[thread_index].out2in, &kv0,
242 nat44_o2i_is_idle_session_cb, &ctx0))
243 nat_elog_notice ("out2in key add failed");
246 snat_ipfix_logging_nat44_ses_create (thread_index,
247 s->in2out.addr.as_u32,
248 s->out2in.addr.as_u32,
251 s->out2in.port, s->in2out.fib_index);
253 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
254 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
255 s->out2in.port, s->in2out.protocol);
257 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
258 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
259 &s->ext_host_nat_addr, s->ext_host_nat_port,
260 s->in2out.protocol, s->in2out.fib_index, s->flags,
266 #ifndef CLIB_MARCH_VARIANT
268 snat_out2in_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
269 snat_session_key_t * p_key0)
271 icmp46_header_t *icmp0;
272 snat_session_key_t key0;
273 icmp_echo_header_t *echo0, *inner_echo0 = 0;
274 ip4_header_t *inner_ip0;
276 icmp46_header_t *inner_icmp0;
278 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
279 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
281 if (!icmp_type_is_error_message
282 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
284 key0.protocol = SNAT_PROTOCOL_ICMP;
285 key0.addr = ip0->dst_address;
286 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be dst port?
290 inner_ip0 = (ip4_header_t *) (echo0 + 1);
291 l4_header = ip4_next_header (inner_ip0);
292 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
293 key0.addr = inner_ip0->src_address;
294 switch (key0.protocol)
296 case SNAT_PROTOCOL_ICMP:
297 inner_icmp0 = (icmp46_header_t *) l4_header;
298 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
299 key0.port = inner_echo0->identifier;
301 case SNAT_PROTOCOL_UDP:
302 case SNAT_PROTOCOL_TCP:
303 key0.port = ((tcp_udp_header_t *) l4_header)->src_port;
306 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
310 return -1; /* success */
314 * Get address and port values to be used for ICMP packet translation
315 * and create session if needed
317 * @param[in,out] sm NAT main
318 * @param[in,out] node NAT node runtime
319 * @param[in] thread_index thread index
320 * @param[in,out] b0 buffer containing packet to be translated
321 * @param[out] p_proto protocol used for matching
322 * @param[out] p_value address and port after NAT translation
323 * @param[out] p_dont_translate if packet should not be translated
324 * @param d optional parameter
325 * @param e optional parameter
328 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
329 u32 thread_index, vlib_buffer_t * b0,
330 ip4_header_t * ip0, u8 * p_proto,
331 snat_session_key_t * p_value,
332 u8 * p_dont_translate, void *d, void *e)
336 snat_session_key_t key0;
337 snat_session_key_t sm0;
338 snat_session_t *s0 = 0;
339 u8 dont_translate = 0;
340 clib_bihash_kv_8_8_t kv0, value0;
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 (&sm->per_thread_data[thread_index].out2in, &kv0,
365 /* Try to match static mapping by external address and port,
366 destination address and port in packet */
367 if (snat_static_mapping_match
368 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
370 if (!sm->forwarding_enabled)
372 /* Don't NAT packet aimed at the intfc address */
373 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
374 ip0->dst_address.as_u32)))
379 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
380 next0 = SNAT_OUT2IN_NEXT_DROP;
391 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
393 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
394 ICMP4_echo_request || !is_addr_only)))
396 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
397 next0 = SNAT_OUT2IN_NEXT_DROP;
401 if (PREDICT_FALSE (identity_nat))
406 /* Create session initiated by host from external network */
407 s0 = create_session_for_static_mapping (sm, b0, sm0, key0,
409 vlib_time_now (sm->vlib_main));
413 next0 = SNAT_OUT2IN_NEXT_DROP;
420 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
422 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
424 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
425 reass.icmp_type_or_tcp_flags)))
427 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
428 next0 = SNAT_OUT2IN_NEXT_DROP;
432 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
437 *p_proto = key0.protocol;
439 *p_value = s0->in2out;
440 *p_dont_translate = dont_translate;
442 *(snat_session_t **) d = s0;
447 #ifndef CLIB_MARCH_VARIANT
449 * Get address and port values to be used for ICMP packet translation
451 * @param[in] sm NAT main
452 * @param[in,out] node NAT node runtime
453 * @param[in] thread_index thread index
454 * @param[in,out] b0 buffer containing packet to be translated
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_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
463 u32 thread_index, vlib_buffer_t * b0,
464 ip4_header_t * ip0, u8 * p_proto,
465 snat_session_key_t * p_value,
466 u8 * p_dont_translate, void *d, void *e)
470 snat_session_key_t key0;
471 snat_session_key_t sm0;
472 u8 dont_translate = 0;
477 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
478 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
480 err = icmp_get_key (b0, ip0, &key0);
483 b0->error = node->errors[err];
484 next0 = SNAT_OUT2IN_NEXT_DROP;
487 key0.fib_index = rx_fib_index0;
489 if (snat_static_mapping_match
490 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, 0))
492 /* Don't NAT packet aimed at the intfc address */
493 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
498 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
499 next0 = SNAT_OUT2IN_NEXT_DROP;
504 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
505 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
506 ICMP4_echo_request || !is_addr_only)
507 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
508 reass.icmp_type_or_tcp_flags)))
510 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
511 next0 = SNAT_OUT2IN_NEXT_DROP;
518 *p_proto = key0.protocol;
519 *p_dont_translate = dont_translate;
524 #ifndef CLIB_MARCH_VARIANT
526 icmp_out2in (snat_main_t * sm,
529 icmp46_header_t * icmp0,
532 vlib_node_runtime_t * node,
533 u32 next0, u32 thread_index, void *d, void *e)
535 snat_session_key_t sm0;
537 icmp_echo_header_t *echo0, *inner_echo0 = 0;
538 ip4_header_t *inner_ip0 = 0;
540 icmp46_header_t *inner_icmp0;
542 u32 new_addr0, old_addr0;
543 u16 old_id0, new_id0;
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)))
560 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
562 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 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
674 next0, thread_index, p_s0, 0);
675 snat_session_t *s0 = *p_s0;
676 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
679 nat44_session_update_counters (s0, now,
680 vlib_buffer_length_in_chain
681 (sm->vlib_main, b0), thread_index);
682 /* Per-user LRU list maintenance */
683 nat44_session_update_lru (sm, s0, thread_index);
689 nat_out2in_sm_unknown_proto (snat_main_t * sm,
691 ip4_header_t * ip, u32 rx_fib_index)
693 clib_bihash_kv_8_8_t kv, value;
694 snat_static_mapping_t *m;
695 snat_session_key_t m_key;
696 u32 old_addr, new_addr;
699 m_key.addr = ip->dst_address;
703 kv.key = m_key.as_u64;
704 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
707 m = pool_elt_at_index (sm->static_mappings, value.value);
709 old_addr = ip->dst_address.as_u32;
710 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
712 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
713 ip->checksum = ip_csum_fold (sum);
715 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
719 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
720 vlib_node_runtime_t * node,
721 vlib_frame_t * frame)
723 u32 n_left_from, *from, *to_next;
724 snat_out2in_next_t next_index;
725 u32 pkts_processed = 0;
726 snat_main_t *sm = &snat_main;
727 f64 now = vlib_time_now (vm);
728 u32 thread_index = vm->thread_index;
729 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
732 from = vlib_frame_vector_args (frame);
733 n_left_from = frame->n_vectors;
734 next_index = node->cached_next_index;
736 while (n_left_from > 0)
740 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
742 while (n_left_from >= 4 && n_left_to_next >= 2)
745 vlib_buffer_t *b0, *b1;
746 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
747 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
748 u32 sw_if_index0, sw_if_index1;
749 ip4_header_t *ip0, *ip1;
750 ip_csum_t sum0, sum1;
751 u32 new_addr0, old_addr0;
752 u16 new_port0, old_port0;
753 u32 new_addr1, old_addr1;
754 u16 new_port1, old_port1;
755 udp_header_t *udp0, *udp1;
756 tcp_header_t *tcp0, *tcp1;
757 icmp46_header_t *icmp0, *icmp1;
758 snat_session_key_t key0, key1, sm0, sm1;
759 u32 rx_fib_index0, rx_fib_index1;
761 snat_session_t *s0 = 0, *s1 = 0;
762 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
763 u8 identity_nat0, identity_nat1;
765 /* Prefetch next iteration. */
767 vlib_buffer_t *p2, *p3;
769 p2 = vlib_get_buffer (vm, from[2]);
770 p3 = vlib_get_buffer (vm, from[3]);
772 vlib_prefetch_buffer_header (p2, LOAD);
773 vlib_prefetch_buffer_header (p3, LOAD);
775 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
776 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
779 /* speculatively enqueue b0 and b1 to the current next frame */
780 to_next[0] = bi0 = from[0];
781 to_next[1] = bi1 = from[1];
787 b0 = vlib_get_buffer (vm, bi0);
788 b1 = vlib_get_buffer (vm, bi1);
790 vnet_buffer (b0)->snat.flags = 0;
791 vnet_buffer (b1)->snat.flags = 0;
793 ip0 = vlib_buffer_get_current (b0);
794 udp0 = ip4_next_header (ip0);
795 tcp0 = (tcp_header_t *) udp0;
796 icmp0 = (icmp46_header_t *) udp0;
798 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
799 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
802 if (PREDICT_FALSE (ip0->ttl == 1))
804 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
805 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
806 ICMP4_time_exceeded_ttl_exceeded_in_transit,
808 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
812 proto0 = ip_proto_to_snat_proto (ip0->protocol);
814 if (PREDICT_FALSE (proto0 == ~0))
816 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
818 if (!sm->forwarding_enabled)
821 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
822 next0 = SNAT_OUT2IN_NEXT_DROP;
829 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
831 next0 = icmp_out2in_slow_path
832 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
833 next0, now, thread_index, &s0);
838 key0.addr = ip0->dst_address;
839 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
840 key0.protocol = proto0;
841 key0.fib_index = rx_fib_index0;
843 kv0.key = key0.as_u64;
845 if (clib_bihash_search_8_8
846 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
848 /* Try to match static mapping by external address and port,
849 destination address and port in packet */
850 if (snat_static_mapping_match
851 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
854 * Send DHCP packets to the ipv4 stack, or we won't
855 * be able to use dhcp client on the outside interface
858 (proto0 == SNAT_PROTOCOL_UDP
859 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
861 (UDP_DST_PORT_dhcp_to_client))))
863 vnet_feature_next (&next0, b0);
867 if (!sm->forwarding_enabled)
870 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
871 next0 = SNAT_OUT2IN_NEXT_DROP;
876 if (PREDICT_FALSE (identity_nat0))
879 /* Create session initiated by host from external network */
880 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
884 next0 = SNAT_OUT2IN_NEXT_DROP;
890 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
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")