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 _(CANNOT_CREATE_USER, "cannot create NAT user")
92 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
93 foreach_snat_out2in_error
96 } snat_out2in_error_t;
98 static char *snat_out2in_error_strings[] = {
99 #define _(sym,string) string,
100 foreach_snat_out2in_error
106 SNAT_OUT2IN_NEXT_DROP,
107 SNAT_OUT2IN_NEXT_LOOKUP,
108 SNAT_OUT2IN_NEXT_ICMP_ERROR,
110 } snat_out2in_next_t;
112 #ifndef CLIB_MARCH_VARIANT
114 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
116 snat_main_t *sm = &snat_main;
117 nat44_is_idle_session_ctx_t *ctx = arg;
119 u64 sess_timeout_time;
120 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
122 clib_bihash_kv_8_8_t s_kv;
124 s = pool_elt_at_index (tsm->sessions, kv->value);
125 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
126 if (ctx->now >= sess_timeout_time)
128 init_nat_i2o_k (&s_kv, s);
129 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
130 nat_elog_warn ("out2in key del failed");
132 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
133 s->in2out.addr.as_u32,
134 s->out2in.addr.as_u32,
138 s->in2out.fib_index);
140 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
141 &s->in2out.addr, s->in2out.port,
142 &s->out2in.addr, s->out2in.port, s->nat_proto);
144 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
145 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
148 if (!snat_is_session_static (s))
149 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
150 &s->out2in.addr, s->out2in.port,
153 nat44_delete_session (sm, s, ctx->thread_index);
162 * @brief Create session for static mapping.
164 * Create NAT session initiated by host from external network with static
167 * @param sm NAT main.
168 * @param b0 Vlib buffer.
169 * @param in2out In2out NAT44 session key.
170 * @param out2in Out2in NAT44 session key.
171 * @param node Vlib node.
173 * @returns SNAT session if successfully created otherwise 0.
175 static inline snat_session_t *
176 create_session_for_static_mapping (snat_main_t * sm,
178 ip4_address_t i2o_addr,
181 ip4_address_t o2i_addr,
184 nat_protocol_t proto,
185 vlib_node_runtime_t * node,
186 u32 thread_index, f64 now)
190 clib_bihash_kv_8_8_t kv0;
193 nat44_is_idle_session_ctx_t ctx0;
195 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
197 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
198 nat_elog_notice ("maximum sessions exceeded");
202 ip0 = vlib_buffer_get_current (b0);
203 udp0 = ip4_next_header (ip0);
205 u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
208 b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
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 */ );
224 s->in2out.addr = i2o_addr;
225 s->in2out.port = i2o_port;
226 s->in2out.fib_index = i2o_fib_index;
227 s->out2in.addr = o2i_addr;
228 s->out2in.port = o2i_port;
229 s->out2in.fib_index = o2i_fib_index;
230 s->nat_proto = proto;
232 /* Add to translation hashes */
234 ctx0.thread_index = thread_index;
235 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
236 if (clib_bihash_add_or_overwrite_stale_8_8
237 (&sm->per_thread_data[thread_index].in2out, &kv0,
238 nat44_i2o_is_idle_session_cb, &ctx0))
239 nat_elog_notice ("in2out key add failed");
241 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
242 if (clib_bihash_add_or_overwrite_stale_8_8
243 (&sm->per_thread_data[thread_index].out2in, &kv0,
244 nat44_o2i_is_idle_session_cb, &ctx0))
245 nat_elog_notice ("out2in key add failed");
248 snat_ipfix_logging_nat44_ses_create (thread_index,
249 s->in2out.addr.as_u32,
250 s->out2in.addr.as_u32,
253 s->out2in.port, s->in2out.fib_index);
255 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
256 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
257 s->out2in.port, s->nat_proto);
259 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
260 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
261 &s->ext_host_nat_addr, s->ext_host_nat_port,
262 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
267 #ifndef CLIB_MARCH_VARIANT
268 static_always_inline snat_out2in_error_t
269 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
270 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
272 icmp46_header_t *icmp0;
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 *nat_proto = NAT_PROTOCOL_ICMP;
285 *addr = ip0->dst_address;
286 *port = vnet_buffer (b)->ip.reass.l4_src_port;
290 inner_ip0 = (ip4_header_t *) (echo0 + 1);
291 l4_header = ip4_next_header (inner_ip0);
292 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
293 *addr = inner_ip0->src_address;
296 case NAT_PROTOCOL_ICMP:
297 inner_icmp0 = (icmp46_header_t *) l4_header;
298 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
299 *port = inner_echo0->identifier;
301 case NAT_PROTOCOL_UDP:
302 case NAT_PROTOCOL_TCP:
303 *port = ((tcp_udp_header_t *) l4_header)->src_port;
306 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
309 return -1; /* success */
313 * Get address and port values to be used for ICMP packet translation
314 * and create session if needed
316 * @param[in,out] sm NAT main
317 * @param[in,out] node NAT node runtime
318 * @param[in] thread_index thread index
319 * @param[in,out] b0 buffer containing packet to be translated
320 * @param[in,out] ip0 ip header
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, ip4_address_t * addr,
331 u16 * port, u32 * fib_index,
332 nat_protocol_t * proto, void *d, void *e,
335 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
337 snat_session_t *s0 = 0;
338 clib_bihash_kv_8_8_t kv0, value0;
343 vlib_main_t *vm = vlib_get_main ();
346 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
347 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
351 err = icmp_get_key (b0, ip0, addr, port, proto);
354 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
355 next0 = SNAT_OUT2IN_NEXT_DROP;
359 ip4_address_t mapping_addr;
361 u32 mapping_fib_index;
363 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
364 if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
366 /* Try to match static mapping by external address and port,
367 destination address and port in packet */
368 if (snat_static_mapping_match
369 (sm, *addr, *port, *fib_index, *proto,
370 &mapping_addr, &mapping_port, &mapping_fib_index, 1, &is_addr_only,
371 0, 0, 0, &identity_nat))
373 if (!sm->forwarding_enabled)
375 /* Don't NAT packet aimed at the intfc address */
376 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
377 ip0->dst_address.as_u32)))
382 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
383 next0 = SNAT_OUT2IN_NEXT_DROP;
394 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
396 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
397 ICMP4_echo_request || !is_addr_only)))
399 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
400 next0 = SNAT_OUT2IN_NEXT_DROP;
404 if (PREDICT_FALSE (identity_nat))
409 /* Create session initiated by host from external network */
411 create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
412 mapping_fib_index, *addr, *port,
413 *fib_index, *proto, node,
414 thread_index, vlib_time_now (vm));
418 next0 = SNAT_OUT2IN_NEXT_DROP;
425 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
427 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
429 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
430 reass.icmp_type_or_tcp_flags)))
432 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
433 next0 = SNAT_OUT2IN_NEXT_DROP;
437 s0 = pool_elt_at_index (tsm->sessions, value0.value);
443 *addr = s0->in2out.addr;
444 *port = s0->in2out.port;
445 *fib_index = s0->in2out.fib_index;
448 *(snat_session_t **) d = s0;
453 #ifndef CLIB_MARCH_VARIANT
455 * Get address and port values to be used for ICMP packet translation
457 * @param[in] sm NAT main
458 * @param[in,out] node NAT node runtime
459 * @param[in] thread_index thread index
460 * @param[in,out] b0 buffer containing packet to be translated
461 * @param[in,out] ip0 ip header
462 * @param[out] p_proto protocol used for matching
463 * @param[out] p_value address and port after NAT translation
464 * @param[out] p_dont_translate if packet should not be translated
465 * @param d optional parameter
466 * @param e optional parameter
469 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
470 u32 thread_index, vlib_buffer_t * b0,
471 ip4_header_t * ip0, ip4_address_t * mapping_addr,
472 u16 * mapping_port, u32 * mapping_fib_index,
473 nat_protocol_t * proto, void *d, void *e,
485 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
486 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
488 err = icmp_get_key (b0, ip0, &addr, &port, proto);
491 b0->error = node->errors[err];
492 next0 = SNAT_OUT2IN_NEXT_DROP;
495 if (snat_static_mapping_match
496 (sm, addr, port, rx_fib_index0, *proto, mapping_addr, mapping_port,
497 mapping_fib_index, 1, &is_addr_only, 0, 0, 0, 0))
499 /* Don't NAT packet aimed at the intfc address */
500 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
505 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
506 next0 = SNAT_OUT2IN_NEXT_DROP;
511 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
512 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
513 ICMP4_echo_request || !is_addr_only)
514 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
515 reass.icmp_type_or_tcp_flags)))
517 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
518 next0 = SNAT_OUT2IN_NEXT_DROP;
527 #ifndef CLIB_MARCH_VARIANT
529 icmp_out2in (snat_main_t * sm,
532 icmp46_header_t * icmp0,
535 vlib_node_runtime_t * node,
536 u32 next0, u32 thread_index, void *d, void *e)
538 icmp_echo_header_t *echo0, *inner_echo0 = 0;
539 ip4_header_t *inner_ip0 = 0;
541 icmp46_header_t *inner_icmp0;
543 u32 new_addr0, old_addr0;
544 u16 old_id0, new_id0;
548 vlib_main_t *vm = vlib_get_main ();
552 nat_protocol_t proto;
554 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
556 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
557 &addr, &port, &fib_index, &proto,
558 d, e, &dont_translate);
561 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
564 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
567 ip_incremental_checksum_buffer (vm, b0,
569 (u8 *) vlib_buffer_get_current (b0),
570 ntohs (ip0->length) -
571 ip4_header_bytes (ip0), 0);
572 checksum0 = ~ip_csum_fold (sum0);
573 if (checksum0 != 0 && checksum0 != 0xffff)
575 next0 = SNAT_OUT2IN_NEXT_DROP;
580 old_addr0 = ip0->dst_address.as_u32;
581 new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
582 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
584 sum0 = ip0->checksum;
585 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
586 dst_address /* changed member */ );
587 ip0->checksum = ip_csum_fold (sum0);
590 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
592 if (icmp0->checksum == 0)
593 icmp0->checksum = 0xffff;
595 if (!icmp_type_is_error_message (icmp0->type))
598 if (PREDICT_FALSE (new_id0 != echo0->identifier))
600 old_id0 = echo0->identifier;
602 echo0->identifier = new_id0;
604 sum0 = icmp0->checksum;
606 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
607 identifier /* changed member */ );
608 icmp0->checksum = ip_csum_fold (sum0);
613 inner_ip0 = (ip4_header_t *) (echo0 + 1);
614 l4_header = ip4_next_header (inner_ip0);
616 if (!ip4_header_checksum_is_valid (inner_ip0))
618 next0 = SNAT_OUT2IN_NEXT_DROP;
622 old_addr0 = inner_ip0->src_address.as_u32;
623 inner_ip0->src_address = addr;
624 new_addr0 = inner_ip0->src_address.as_u32;
626 sum0 = icmp0->checksum;
627 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
628 src_address /* changed member */ );
629 icmp0->checksum = ip_csum_fold (sum0);
633 case NAT_PROTOCOL_ICMP:
634 inner_icmp0 = (icmp46_header_t *) l4_header;
635 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
637 old_id0 = inner_echo0->identifier;
639 inner_echo0->identifier = new_id0;
641 sum0 = icmp0->checksum;
643 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
645 icmp0->checksum = ip_csum_fold (sum0);
647 case NAT_PROTOCOL_UDP:
648 case NAT_PROTOCOL_TCP:
649 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
651 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
653 sum0 = icmp0->checksum;
654 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
656 icmp0->checksum = ip_csum_fold (sum0);
670 icmp_out2in_slow_path (snat_main_t * sm,
673 icmp46_header_t * icmp0,
676 vlib_node_runtime_t * node,
678 u32 thread_index, snat_session_t ** p_s0)
680 vlib_main_t *vm = vlib_get_main ();
682 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
683 next0, thread_index, p_s0, 0);
684 snat_session_t *s0 = *p_s0;
685 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
688 nat44_session_update_counters (s0, now,
689 vlib_buffer_length_in_chain
690 (vm, b0), thread_index);
691 /* Per-user LRU list maintenance */
692 nat44_session_update_lru (sm, s0, thread_index);
698 nat_out2in_sm_unknown_proto (snat_main_t * sm,
700 ip4_header_t * ip, u32 rx_fib_index)
702 clib_bihash_kv_8_8_t kv, value;
703 snat_static_mapping_t *m;
704 u32 old_addr, new_addr;
707 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
708 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
711 m = pool_elt_at_index (sm->static_mappings, value.value);
713 old_addr = ip->dst_address.as_u32;
714 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
716 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
717 ip->checksum = ip_csum_fold (sum);
719 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
723 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
724 vlib_node_runtime_t * node,
725 vlib_frame_t * frame)
727 u32 n_left_from, *from, *to_next;
728 snat_out2in_next_t next_index;
729 u32 pkts_processed = 0;
730 snat_main_t *sm = &snat_main;
731 f64 now = vlib_time_now (vm);
732 u32 thread_index = vm->thread_index;
733 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
734 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
737 from = vlib_frame_vector_args (frame);
738 n_left_from = frame->n_vectors;
739 next_index = node->cached_next_index;
741 while (n_left_from > 0)
745 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
747 while (n_left_from >= 4 && n_left_to_next >= 2)
750 vlib_buffer_t *b0, *b1;
751 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
752 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
753 u32 sw_if_index0, sw_if_index1;
754 ip4_header_t *ip0, *ip1;
755 ip_csum_t sum0, sum1;
756 u32 new_addr0, old_addr0;
757 u16 new_port0, old_port0;
758 u32 new_addr1, old_addr1;
759 u16 new_port1, old_port1;
760 udp_header_t *udp0, *udp1;
761 tcp_header_t *tcp0, *tcp1;
762 icmp46_header_t *icmp0, *icmp1;
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;
768 ip4_address_t sm_addr0, sm_addr1;
769 u16 sm_port0, sm_port1;
770 u32 sm_fib_index0, sm_fib_index1;
772 /* Prefetch next iteration. */
774 vlib_buffer_t *p2, *p3;
776 p2 = vlib_get_buffer (vm, from[2]);
777 p3 = vlib_get_buffer (vm, from[3]);
779 vlib_prefetch_buffer_header (p2, LOAD);
780 vlib_prefetch_buffer_header (p3, LOAD);
782 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
783 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
786 /* speculatively enqueue b0 and b1 to the current next frame */
787 to_next[0] = bi0 = from[0];
788 to_next[1] = bi1 = from[1];
794 b0 = vlib_get_buffer (vm, bi0);
795 b1 = vlib_get_buffer (vm, bi1);
797 vnet_buffer (b0)->snat.flags = 0;
798 vnet_buffer (b1)->snat.flags = 0;
800 ip0 = vlib_buffer_get_current (b0);
801 udp0 = ip4_next_header (ip0);
802 tcp0 = (tcp_header_t *) udp0;
803 icmp0 = (icmp46_header_t *) udp0;
805 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
806 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
809 if (PREDICT_FALSE (ip0->ttl == 1))
811 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
812 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
813 ICMP4_time_exceeded_ttl_exceeded_in_transit,
815 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
819 proto0 = ip_proto_to_nat_proto (ip0->protocol);
821 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
823 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
825 if (!sm->forwarding_enabled)
828 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
829 next0 = SNAT_OUT2IN_NEXT_DROP;
836 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
838 next0 = icmp_out2in_slow_path
839 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
840 next0, now, thread_index, &s0);
845 init_nat_k (&kv0, ip0->dst_address,
846 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
848 if (clib_bihash_search_8_8
849 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
851 /* Try to match static mapping by external address and port,
852 destination address and port in packet */
853 if (snat_static_mapping_match
854 (sm, ip0->dst_address,
855 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
856 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
860 * Send DHCP packets to the ipv4 stack, or we won't
861 * be able to use dhcp client on the outside interface
864 (proto0 == NAT_PROTOCOL_UDP
865 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
867 (UDP_DST_PORT_dhcp_to_client))))
869 vnet_feature_next (&next0, b0);
873 if (!sm->forwarding_enabled)
876 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
877 next0 = SNAT_OUT2IN_NEXT_DROP;
882 if (PREDICT_FALSE (identity_nat0))
885 /* Create session initiated by host from external network */
886 s0 = create_session_for_static_mapping (sm, b0,
891 ip.reass.l4_dst_port,
892 rx_fib_index0, proto0,
897 next0 = SNAT_OUT2IN_NEXT_DROP;
902 s0 = pool_elt_at_index (tsm->sessions, value0.value);
904 old_addr0 = ip0->dst_address.as_u32;
905 ip0->dst_address = s0->in2out.addr;
906 new_addr0 = ip0->dst_address.as_u32;
907 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
909 sum0 = ip0->checksum;
910 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
912 dst_address /* changed member */ );
913 ip0->checksum = ip_csum_fold (sum0);
915 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
917 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
919 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
920 new_port0 = udp0->dst_port = s0->in2out.port;
921 sum0 = tcp0->checksum;
922 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
924 dst_address /* changed member */ );
926 sum0 = ip_csum_update (sum0, old_port0, new_port0,
927 ip4_header_t /* cheat */ ,
928 length /* changed member */ );
929 tcp0->checksum = ip_csum_fold (sum0);
935 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
937 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
938 new_port0 = udp0->dst_port = s0->in2out.port;
939 if (PREDICT_FALSE (udp0->checksum))
941 sum0 = udp0->checksum;
942 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
945 ip_csum_update (sum0, old_port0, new_port0,
946 ip4_header_t /* cheat */ ,
947 length /* changed member */ );
948 udp0->checksum = ip_csum_fold (sum0);
955 nat44_session_update_counters (s0, now,
956 vlib_buffer_length_in_chain (vm, b0),
958 /* Per-user LRU list maintenance */
959 nat44_session_update_lru (sm, s0, thread_index);
962 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
963 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
965 snat_out2in_trace_t *t =
966 vlib_add_trace (vm, node, b0, sizeof (*t));
967 t->sw_if_index = sw_if_index0;
968 t->next_index = next0;
969 t->session_index = ~0;
972 s0 - sm->per_thread_data[thread_index].sessions;
975 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
978 ip1 = vlib_buffer_get_current (b1);
979 udp1 = ip4_next_header (ip1);
980 tcp1 = (tcp_header_t *) udp1;
981 icmp1 = (icmp46_header_t *) udp1;
983 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
984 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
987 if (PREDICT_FALSE (ip1->ttl == 1))
989 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
990 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
991 ICMP4_time_exceeded_ttl_exceeded_in_transit,
993 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
997 proto1 = ip_proto_to_nat_proto (ip1->protocol);
999 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1001 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1003 if (!sm->forwarding_enabled)
1006 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1007 next1 = SNAT_OUT2IN_NEXT_DROP;
1014 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1016 next1 = icmp_out2in_slow_path
1017 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1018 next1, now, thread_index, &s1);
1023 init_nat_k (&kv1, ip1->dst_address,
1024 vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1027 if (clib_bihash_search_8_8
1028 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1030 /* Try to match static mapping by external address and port,
1031 destination address and port in packet */
1032 if (snat_static_mapping_match
1033 (sm, ip1->dst_address,
1034 vnet_buffer (b1)->ip.reass.l4_dst_port, proto1,
1035 rx_fib_index1, &sm_addr1, &sm_port1, &sm_fib_index1, 1, 0,
1036 0, 0, 0, &identity_nat1))
1039 * Send DHCP packets to the ipv4 stack, or we won't
1040 * be able to use dhcp client on the outside interface
1043 (proto1 == NAT_PROTOCOL_UDP
1044 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1045 clib_host_to_net_u16
1046 (UDP_DST_PORT_dhcp_to_client))))
1048 vnet_feature_next (&next1, b1);
1052 if (!sm->forwarding_enabled)
1055 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1056 next1 = SNAT_OUT2IN_NEXT_DROP;
1061 if (PREDICT_FALSE (identity_nat1))
1064 /* Create session initiated by host from external network */
1066 create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1069 vnet_buffer (b1)->ip.
1071 rx_fib_index1, proto1,
1072 node, thread_index, now);
1075 next1 = SNAT_OUT2IN_NEXT_DROP;
1081 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1084 old_addr1 = ip1->dst_address.as_u32;
1085 ip1->dst_address = s1->in2out.addr;
1086 new_addr1 = ip1->dst_address.as_u32;
1087 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1089 sum1 = ip1->checksum;
1090 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1092 dst_address /* changed member */ );
1093 ip1->checksum = ip_csum_fold (sum1);
1095 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1097 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1099 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1100 new_port1 = udp1->dst_port = s1->in2out.port;
1102 sum1 = tcp1->checksum;
1103 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1105 dst_address /* changed member */ );
1107 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1108 ip4_header_t /* cheat */ ,
1109 length /* changed member */ );
1110 tcp1->checksum = ip_csum_fold (sum1);
1116 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1118 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1119 new_port1 = udp1->dst_port = s1->in2out.port;
1120 if (PREDICT_FALSE (udp1->checksum))
1123 sum1 = udp1->checksum;
1125 ip_csum_update (sum1, old_addr1, new_addr1,
1127 dst_address /* changed member */ );
1129 ip_csum_update (sum1, old_port1, new_port1,
1130 ip4_header_t /* cheat */ ,
1131 length /* changed member */ );
1132 udp1->checksum = ip_csum_fold (sum1);
1139 nat44_session_update_counters (s1, now,
1140 vlib_buffer_length_in_chain (vm, b1),
1142 /* Per-user LRU list maintenance */
1143 nat44_session_update_lru (sm, s1, thread_index);
1146 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1147 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1149 snat_out2in_trace_t *t =
1150 vlib_add_trace (vm, node, b1, sizeof (*t));
1151 t->sw_if_index = sw_if_index1;
1152 t->next_index = next1;
1153 t->session_index = ~0;
1156 s1 - sm->per_thread_data[thread_index].sessions;
1159 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1161 /* verify speculative enqueues, maybe switch current next frame */
1162 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1163 to_next, n_left_to_next,
1164 bi0, bi1, next0, next1);
1167 while (n_left_from > 0 && n_left_to_next > 0)
1171 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1175 u32 new_addr0, old_addr0;
1176 u16 new_port0, old_port0;
1179 icmp46_header_t *icmp0;
1182 snat_session_t *s0 = 0;
1183 clib_bihash_kv_8_8_t kv0, value0;
1185 ip4_address_t sm_addr0;
1189 /* speculatively enqueue b0 to the current next frame */
1195 n_left_to_next -= 1;
1197 b0 = vlib_get_buffer (vm, bi0);
1199 vnet_buffer (b0)->snat.flags = 0;
1201 ip0 = vlib_buffer_get_current (b0);
1202 udp0 = ip4_next_header (ip0);
1203 tcp0 = (tcp_header_t *) udp0;
1204 icmp0 = (icmp46_header_t *) udp0;
1206 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1207 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1210 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1212 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1214 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1216 if (!sm->forwarding_enabled)
1219 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1220 next0 = SNAT_OUT2IN_NEXT_DROP;
1227 if (PREDICT_FALSE (ip0->ttl == 1))
1229 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1230 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1231 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1233 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1237 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1239 next0 = icmp_out2in_slow_path
1240 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1241 next0, now, thread_index, &s0);
1246 init_nat_k (&kv0, ip0->dst_address,
1247 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1250 if (clib_bihash_search_8_8
1251 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1253 /* Try to match static mapping by external address and port,
1254 destination address and port in packet */
1255 if (snat_static_mapping_match
1256 (sm, ip0->dst_address,
1257 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1258 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
1262 * Send DHCP packets to the ipv4 stack, or we won't
1263 * be able to use dhcp client on the outside interface
1266 (proto0 == NAT_PROTOCOL_UDP
1267 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1268 clib_host_to_net_u16
1269 (UDP_DST_PORT_dhcp_to_client))))
1271 vnet_feature_next (&next0, b0);
1275 if (!sm->forwarding_enabled)
1278 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1279 next0 = SNAT_OUT2IN_NEXT_DROP;
1284 if (PREDICT_FALSE (identity_nat0))
1287 /* Create session initiated by host from external network */
1288 s0 = create_session_for_static_mapping (sm, b0,
1293 ip.reass.l4_dst_port,
1294 rx_fib_index0, proto0,
1299 next0 = SNAT_OUT2IN_NEXT_DROP;
1305 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1308 old_addr0 = ip0->dst_address.as_u32;
1309 ip0->dst_address = s0->in2out.addr;
1310 new_addr0 = ip0->dst_address.as_u32;
1311 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1313 sum0 = ip0->checksum;
1314 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1316 dst_address /* changed member */ );
1317 ip0->checksum = ip_csum_fold (sum0);
1319 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1321 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1323 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1324 new_port0 = udp0->dst_port = s0->in2out.port;
1326 sum0 = tcp0->checksum;
1327 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1329 dst_address /* changed member */ );
1331 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1332 ip4_header_t /* cheat */ ,
1333 length /* changed member */ );
1334 tcp0->checksum = ip_csum_fold (sum0);
1340 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1342 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1343 new_port0 = udp0->dst_port = s0->in2out.port;
1344 if (PREDICT_FALSE (udp0->checksum))
1346 sum0 = udp0->checksum;
1347 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1350 ip_csum_update (sum0, old_port0, new_port0,
1351 ip4_header_t /* cheat */ ,
1352 length /* changed member */ );
1353 udp0->checksum = ip_csum_fold (sum0);
1360 nat44_session_update_counters (s0, now,
1361 vlib_buffer_length_in_chain (vm, b0),
1363 /* Per-user LRU list maintenance */
1364 nat44_session_update_lru (sm, s0, thread_index);
1367 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1368 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1370 snat_out2in_trace_t *t =
1371 vlib_add_trace (vm, node, b0, sizeof (*t));
1372 t->sw_if_index = sw_if_index0;
1373 t->next_index = next0;
1374 t->session_index = ~0;
1377 s0 - sm->per_thread_data[thread_index].sessions;
1380 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1382 /* verify speculative enqueue, maybe switch current next frame */
1383 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1384 to_next, n_left_to_next,
1388 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1391 vlib_node_increment_counter (vm, sm->out2in_node_index,
1392 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1394 vlib_node_increment_counter (vm, sm->out2in_node_index,
1395 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1396 vlib_node_increment_counter (vm, sm->out2in_node_index,
1397 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1398 vlib_node_increment_counter (vm, sm->out2in_node_index,
1399 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1400 vlib_node_increment_counter (vm, sm->out2in_node_index,
1401 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1403 vlib_node_increment_counter (vm, sm->out2in_node_index,
1404 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1406 return frame->n_vectors;
1410 VLIB_REGISTER_NODE (snat_out2in_node) = {
1411 .name = "nat44-out2in",
1412 .vector_size = sizeof (u32),
1413 .format_trace = format_snat_out2in_trace,
1414 .type = VLIB_NODE_TYPE_INTERNAL,
1416 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1417 .error_strings = snat_out2in_error_strings,
1419 .runtime_data_bytes = sizeof (snat_runtime_t),
1421 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1423 /* edit / add dispositions here */
1425 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1426 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1427 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1432 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1433 vlib_node_runtime_t * node,
1434 vlib_frame_t * frame)
1436 u32 n_left_from, *from, *to_next;
1437 snat_out2in_next_t next_index;
1438 u32 pkts_processed = 0;
1439 snat_main_t *sm = &snat_main;
1441 from = vlib_frame_vector_args (frame);
1442 n_left_from = frame->n_vectors;
1443 next_index = node->cached_next_index;
1445 while (n_left_from > 0)
1449 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1451 while (n_left_from > 0 && n_left_to_next > 0)
1455 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1459 u32 new_addr0, old_addr0;
1460 u16 new_port0, old_port0;
1463 icmp46_header_t *icmp0;
1466 ip4_address_t sm_addr0;
1470 /* speculatively enqueue b0 to the current next frame */
1476 n_left_to_next -= 1;
1478 b0 = vlib_get_buffer (vm, bi0);
1480 ip0 = vlib_buffer_get_current (b0);
1481 udp0 = ip4_next_header (ip0);
1482 tcp0 = (tcp_header_t *) udp0;
1483 icmp0 = (icmp46_header_t *) udp0;
1485 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1487 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1489 vnet_feature_next (&next0, b0);
1491 if (PREDICT_FALSE (ip0->ttl == 1))
1493 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1494 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1495 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1497 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1501 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1503 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1506 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1508 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1509 rx_fib_index0, node, next0, ~0, 0, 0);
1513 if (snat_static_mapping_match
1514 (sm, ip0->dst_address, udp0->dst_port, rx_fib_index0, proto0,
1515 &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0, 0, 0))
1517 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1521 new_addr0 = sm_addr0.as_u32;
1522 new_port0 = sm_port0;
1523 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1524 old_addr0 = ip0->dst_address.as_u32;
1525 ip0->dst_address.as_u32 = new_addr0;
1527 sum0 = ip0->checksum;
1528 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1530 dst_address /* changed member */ );
1531 ip0->checksum = ip_csum_fold (sum0);
1533 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1535 old_port0 = udp0->dst_port;
1536 udp0->dst_port = new_port0;
1538 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1540 sum0 = tcp0->checksum;
1541 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1543 dst_address /* changed member */ );
1544 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1545 ip4_header_t /* cheat */ ,
1546 length /* changed member */ );
1547 tcp0->checksum = ip_csum_fold (sum0);
1549 else if (udp0->checksum)
1551 sum0 = udp0->checksum;
1552 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1554 dst_address /* changed member */ );
1555 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1556 ip4_header_t /* cheat */ ,
1557 length /* changed member */ );
1558 udp0->checksum = ip_csum_fold (sum0);
1563 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1565 sum0 = tcp0->checksum;
1566 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1568 dst_address /* changed member */ );
1569 tcp0->checksum = ip_csum_fold (sum0);
1571 else if (udp0->checksum)
1573 sum0 = udp0->checksum;
1574 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1576 dst_address /* changed member */ );
1577 udp0->checksum = ip_csum_fold (sum0);
1583 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1584 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1586 snat_out2in_trace_t *t =
1587 vlib_add_trace (vm, node, b0, sizeof (*t));
1588 t->sw_if_index = sw_if_index0;
1589 t->next_index = next0;
1592 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1594 /* verify speculative enqueue, maybe switch current next frame */
1595 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1596 to_next, n_left_to_next,
1600 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1603 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1604 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1606 return frame->n_vectors;
1610 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1611 .name = "nat44-out2in-fast",
1612 .vector_size = sizeof (u32),
1613 .format_trace = format_snat_out2in_fast_trace,
1614 .type = VLIB_NODE_TYPE_INTERNAL,
1616 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1617 .error_strings = snat_out2in_error_strings,
1619 .runtime_data_bytes = sizeof (snat_runtime_t),
1621 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1623 /* edit / add dispositions here */
1625 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1626 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1627 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1633 * fd.io coding-style-patch-verification: ON
1636 * eval: (c-set-style "gnu")