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>
23 #include <vnet/ip/ip.h>
24 #include <vnet/udp/udp_local.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
28 #include <nat/lib/ipfix_logging.h>
29 #include <nat/nat_inlines.h>
30 #include <nat/lib/nat_syslog.h>
31 #include <nat/nat44-ei/nat44_ei_inlines.h>
32 #include <nat/nat44-ei/nat44_ei.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/error.h>
36 #include <vppinfra/elog.h>
43 } snat_out2in_trace_t;
45 /* packet trace format function */
47 format_snat_out2in_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
55 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
56 t->sw_if_index, t->next_index, t->session_index);
61 format_snat_out2in_fast_trace (u8 * s, va_list * args)
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
67 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
68 t->sw_if_index, t->next_index);
72 #define foreach_snat_out2in_error \
73 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
74 _(OUT_OF_PORTS, "out of ports") \
75 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
76 _(NO_TRANSLATION, "no translation") \
77 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
78 _(CANNOT_CREATE_USER, "cannot create NAT user")
82 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
83 foreach_snat_out2in_error
86 } snat_out2in_error_t;
88 static char *snat_out2in_error_strings[] = {
89 #define _(sym,string) string,
90 foreach_snat_out2in_error
96 SNAT_OUT2IN_NEXT_DROP,
97 SNAT_OUT2IN_NEXT_LOOKUP,
98 SNAT_OUT2IN_NEXT_ICMP_ERROR,
100 } snat_out2in_next_t;
102 #ifndef CLIB_MARCH_VARIANT
104 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
106 snat_main_t *sm = &snat_main;
107 nat44_is_idle_session_ctx_t *ctx = arg;
109 u64 sess_timeout_time;
110 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
112 clib_bihash_kv_8_8_t s_kv;
114 s = pool_elt_at_index (tsm->sessions, kv->value);
115 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
116 if (ctx->now >= sess_timeout_time)
118 init_nat_i2o_k (&s_kv, s);
119 if (clib_bihash_add_del_8_8 (&sm->in2out, &s_kv, 0))
120 nat_elog_warn ("out2in key del failed");
122 nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
123 s->in2out.addr.as_u32,
124 s->out2in.addr.as_u32,
128 s->in2out.fib_index);
130 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
131 &s->in2out.addr, s->in2out.port,
132 &s->out2in.addr, s->out2in.port, s->nat_proto);
134 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
135 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
138 if (!snat_is_session_static (s))
139 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
140 &s->out2in.addr, s->out2in.port,
143 nat44_delete_session (sm, s, ctx->thread_index);
152 * @brief Create session for static mapping.
154 * Create NAT session initiated by host from external network with static
157 * @param sm NAT main.
158 * @param b0 Vlib buffer.
159 * @param in2out In2out NAT44 session key.
160 * @param out2in Out2in NAT44 session key.
161 * @param node Vlib node.
163 * @returns SNAT session if successfully created otherwise 0.
165 static inline snat_session_t *
166 create_session_for_static_mapping (snat_main_t * sm,
168 ip4_address_t i2o_addr,
171 ip4_address_t o2i_addr,
174 nat_protocol_t proto,
175 vlib_node_runtime_t * node,
176 u32 thread_index, f64 now)
180 clib_bihash_kv_8_8_t kv0;
183 nat44_is_idle_session_ctx_t ctx0;
185 if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
187 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
188 nat_elog_notice ("maximum sessions exceeded");
192 ip0 = vlib_buffer_get_current (b0);
193 udp0 = ip4_next_header (ip0);
195 u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
198 b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
202 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
205 nat44_delete_user_with_no_session (sm, u, thread_index);
206 nat_elog_warn ("create NAT session failed");
210 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
211 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
212 s->ext_host_port = udp0->src_port;
213 user_session_increment (sm, u, 1 /* static */ );
214 s->in2out.addr = i2o_addr;
215 s->in2out.port = i2o_port;
216 s->in2out.fib_index = i2o_fib_index;
217 s->out2in.addr = o2i_addr;
218 s->out2in.port = o2i_port;
219 s->out2in.fib_index = o2i_fib_index;
220 s->nat_proto = proto;
222 /* Add to translation hashes */
224 ctx0.thread_index = thread_index;
225 init_nat_i2o_kv (&kv0, s, thread_index,
226 s - sm->per_thread_data[thread_index].sessions);
227 if (clib_bihash_add_or_overwrite_stale_8_8 (
228 &sm->in2out, &kv0, nat44_i2o_is_idle_session_cb, &ctx0))
229 nat_elog_notice ("in2out key add failed");
231 init_nat_o2i_kv (&kv0, s, thread_index,
232 s - sm->per_thread_data[thread_index].sessions);
233 if (clib_bihash_add_or_overwrite_stale_8_8 (
234 &sm->out2in, &kv0, nat44_o2i_is_idle_session_cb, &ctx0))
235 nat_elog_notice ("out2in key add failed");
238 nat_ipfix_logging_nat44_ses_create (thread_index,
239 s->in2out.addr.as_u32,
240 s->out2in.addr.as_u32,
243 s->out2in.port, s->in2out.fib_index);
245 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
246 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
247 s->out2in.port, s->nat_proto);
249 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
250 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
251 &s->ext_host_nat_addr, s->ext_host_nat_port,
252 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
257 #ifndef CLIB_MARCH_VARIANT
258 static_always_inline snat_out2in_error_t
259 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
260 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
262 icmp46_header_t *icmp0;
263 icmp_echo_header_t *echo0, *inner_echo0 = 0;
264 ip4_header_t *inner_ip0;
266 icmp46_header_t *inner_icmp0;
268 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
269 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
271 if (!icmp_type_is_error_message
272 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
274 *nat_proto = NAT_PROTOCOL_ICMP;
275 *addr = ip0->dst_address;
276 *port = vnet_buffer (b)->ip.reass.l4_src_port;
280 inner_ip0 = (ip4_header_t *) (echo0 + 1);
281 l4_header = ip4_next_header (inner_ip0);
282 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
283 *addr = inner_ip0->src_address;
286 case NAT_PROTOCOL_ICMP:
287 inner_icmp0 = (icmp46_header_t *) l4_header;
288 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
289 *port = inner_echo0->identifier;
291 case NAT_PROTOCOL_UDP:
292 case NAT_PROTOCOL_TCP:
293 *port = ((tcp_udp_header_t *) l4_header)->src_port;
296 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
299 return -1; /* success */
303 * Get address and port values to be used for ICMP packet translation
304 * and create session if needed
306 * @param[in,out] sm NAT main
307 * @param[in,out] node NAT node runtime
308 * @param[in] thread_index thread index
309 * @param[in,out] b0 buffer containing packet to be translated
310 * @param[in,out] ip0 ip header
311 * @param[out] p_proto protocol used for matching
312 * @param[out] p_value address and port after NAT translation
313 * @param[out] p_dont_translate if packet should not be translated
314 * @param d optional parameter
315 * @param e optional parameter
318 icmp_match_out2in_slow (snat_main_t *sm, vlib_node_runtime_t *node,
319 u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
320 ip4_address_t *addr, u16 *port, u32 *fib_index,
321 nat_protocol_t *proto, snat_session_t **p_s0,
324 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
326 snat_session_t *s0 = 0;
327 clib_bihash_kv_8_8_t kv0, value0;
332 vlib_main_t *vm = vlib_get_main ();
335 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
336 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
340 err = icmp_get_key (b0, ip0, addr, port, proto);
343 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
344 next0 = SNAT_OUT2IN_NEXT_DROP;
348 ip4_address_t mapping_addr;
350 u32 mapping_fib_index;
352 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
353 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
355 /* Try to match static mapping by external address and port,
356 destination address and port in packet */
357 if (nat44_ei_static_mapping_match (
358 *addr, *port, *fib_index, *proto, &mapping_addr, &mapping_port,
359 &mapping_fib_index, 1, &is_addr_only, &identity_nat))
361 if (!sm->forwarding_enabled)
363 /* Don't NAT packet aimed at the intfc address */
364 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
365 ip0->dst_address.as_u32)))
370 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
371 next0 = SNAT_OUT2IN_NEXT_DROP;
382 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
384 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
385 ICMP4_echo_request || !is_addr_only)))
387 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
388 next0 = SNAT_OUT2IN_NEXT_DROP;
392 if (PREDICT_FALSE (identity_nat))
397 /* Create session initiated by host from external network */
399 create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
400 mapping_fib_index, *addr, *port,
401 *fib_index, *proto, node,
402 thread_index, vlib_time_now (vm));
406 next0 = SNAT_OUT2IN_NEXT_DROP;
413 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
415 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
417 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
418 reass.icmp_type_or_tcp_flags)))
420 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
421 next0 = SNAT_OUT2IN_NEXT_DROP;
425 s0 = pool_elt_at_index (tsm->sessions,
426 nat_value_get_session_index (&value0));
432 *addr = s0->in2out.addr;
433 *port = s0->in2out.port;
434 *fib_index = s0->in2out.fib_index;
442 #ifndef CLIB_MARCH_VARIANT
444 * Get address and port values to be used for ICMP packet translation
446 * @param[in] sm NAT main
447 * @param[in,out] node NAT node runtime
448 * @param[in] thread_index thread index
449 * @param[in,out] b0 buffer containing packet to be translated
450 * @param[in,out] ip0 ip header
451 * @param[out] p_proto protocol used for matching
452 * @param[out] p_value address and port after NAT translation
453 * @param[out] p_dont_translate if packet should not be translated
454 * @param d optional parameter
455 * @param e optional parameter
458 icmp_match_out2in_fast (snat_main_t *sm, vlib_node_runtime_t *node,
459 u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
460 ip4_address_t *mapping_addr, u16 *mapping_port,
461 u32 *mapping_fib_index, nat_protocol_t *proto,
462 snat_session_t **p_s0, u8 *dont_translate)
473 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
474 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
476 err = icmp_get_key (b0, ip0, &addr, &port, proto);
479 b0->error = node->errors[err];
480 next0 = SNAT_OUT2IN_NEXT_DROP;
483 if (nat44_ei_static_mapping_match (addr, port, rx_fib_index0, *proto,
484 mapping_addr, mapping_port,
485 mapping_fib_index, 1, &is_addr_only, 0))
487 /* Don't NAT packet aimed at the intfc address */
488 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
493 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
494 next0 = SNAT_OUT2IN_NEXT_DROP;
499 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
500 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
501 ICMP4_echo_request || !is_addr_only)
502 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
503 reass.icmp_type_or_tcp_flags)))
505 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
506 next0 = SNAT_OUT2IN_NEXT_DROP;
515 u32 icmp_out2in (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
516 icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
517 vlib_node_runtime_t *node, u32 next0, u32 thread_index,
518 snat_session_t **p_s0);
520 #ifndef CLIB_MARCH_VARIANT
522 icmp_out2in (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
523 icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
524 vlib_node_runtime_t *node, u32 next0, u32 thread_index,
525 snat_session_t **p_s0)
527 icmp_echo_header_t *echo0, *inner_echo0 = 0;
528 ip4_header_t *inner_ip0 = 0;
530 icmp46_header_t *inner_icmp0;
532 u32 new_addr0, old_addr0;
533 u16 old_id0, new_id0;
537 vlib_main_t *vm = vlib_get_main ();
541 nat_protocol_t proto;
543 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
546 sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0, &addr, &port,
547 &fib_index, &proto, p_s0, &dont_translate);
550 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
553 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
556 ip_incremental_checksum_buffer (vm, b0,
558 (u8 *) vlib_buffer_get_current (b0),
559 ntohs (ip0->length) -
560 ip4_header_bytes (ip0), 0);
561 checksum0 = ~ip_csum_fold (sum0);
562 if (checksum0 != 0 && checksum0 != 0xffff)
564 next0 = SNAT_OUT2IN_NEXT_DROP;
569 old_addr0 = ip0->dst_address.as_u32;
570 new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
571 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
573 sum0 = ip0->checksum;
574 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
575 dst_address /* changed member */ );
576 ip0->checksum = ip_csum_fold (sum0);
579 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
581 if (icmp0->checksum == 0)
582 icmp0->checksum = 0xffff;
584 if (!icmp_type_is_error_message (icmp0->type))
587 if (PREDICT_FALSE (new_id0 != echo0->identifier))
589 old_id0 = echo0->identifier;
591 echo0->identifier = new_id0;
593 sum0 = icmp0->checksum;
595 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
596 identifier /* changed member */ );
597 icmp0->checksum = ip_csum_fold (sum0);
602 inner_ip0 = (ip4_header_t *) (echo0 + 1);
603 l4_header = ip4_next_header (inner_ip0);
605 if (!ip4_header_checksum_is_valid (inner_ip0))
607 next0 = SNAT_OUT2IN_NEXT_DROP;
611 old_addr0 = inner_ip0->src_address.as_u32;
612 inner_ip0->src_address = addr;
613 new_addr0 = inner_ip0->src_address.as_u32;
615 sum0 = icmp0->checksum;
616 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
617 src_address /* changed member */ );
618 icmp0->checksum = ip_csum_fold (sum0);
622 case NAT_PROTOCOL_ICMP:
623 inner_icmp0 = (icmp46_header_t *) l4_header;
624 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
626 old_id0 = inner_echo0->identifier;
628 inner_echo0->identifier = new_id0;
630 sum0 = icmp0->checksum;
632 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
634 icmp0->checksum = ip_csum_fold (sum0);
636 case NAT_PROTOCOL_UDP:
637 case NAT_PROTOCOL_TCP:
638 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
640 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
642 sum0 = icmp0->checksum;
643 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
645 icmp0->checksum = ip_csum_fold (sum0);
659 icmp_out2in_slow_path (snat_main_t * sm,
662 icmp46_header_t * icmp0,
665 vlib_node_runtime_t * node,
667 u32 thread_index, snat_session_t ** p_s0)
669 vlib_main_t *vm = vlib_get_main ();
671 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
672 next0, thread_index, p_s0);
673 snat_session_t *s0 = *p_s0;
674 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
677 nat44_ei_session_update_counters (
678 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
679 /* Per-user LRU list maintenance */
680 nat44_session_update_lru (sm, s0, thread_index);
686 nat_out2in_sm_unknown_proto (snat_main_t * sm,
688 ip4_header_t * ip, u32 rx_fib_index)
690 clib_bihash_kv_8_8_t kv, value;
691 snat_static_mapping_t *m;
692 u32 old_addr, new_addr;
695 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
696 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
699 m = pool_elt_at_index (sm->static_mappings, value.value);
701 old_addr = ip->dst_address.as_u32;
702 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
704 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
705 ip->checksum = ip_csum_fold (sum);
707 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
711 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
712 vlib_node_runtime_t * node,
713 vlib_frame_t * frame)
715 u32 n_left_from, *from;
716 snat_main_t *sm = &snat_main;
717 f64 now = vlib_time_now (vm);
718 u32 thread_index = vm->thread_index;
719 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
721 from = vlib_frame_vector_args (frame);
722 n_left_from = frame->n_vectors;
724 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
725 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
726 vlib_get_buffers (vm, from, b, n_left_from);
728 while (n_left_from >= 2)
730 vlib_buffer_t *b0, *b1;
731 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
732 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
733 u32 sw_if_index0, sw_if_index1;
734 ip4_header_t *ip0, *ip1;
735 ip_csum_t sum0, sum1;
736 u32 new_addr0, old_addr0;
737 u16 new_port0, old_port0;
738 u32 new_addr1, old_addr1;
739 u16 new_port1, old_port1;
740 udp_header_t *udp0, *udp1;
741 tcp_header_t *tcp0, *tcp1;
742 icmp46_header_t *icmp0, *icmp1;
743 u32 rx_fib_index0, rx_fib_index1;
745 snat_session_t *s0 = 0, *s1 = 0;
746 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
747 u8 identity_nat0, identity_nat1;
748 ip4_address_t sm_addr0, sm_addr1;
749 u16 sm_port0, sm_port1;
750 u32 sm_fib_index0, sm_fib_index1;
757 /* Prefetch next iteration. */
758 if (PREDICT_TRUE (n_left_from >= 4))
760 vlib_buffer_t *p2, *p3;
765 vlib_prefetch_buffer_header (p2, LOAD);
766 vlib_prefetch_buffer_header (p3, LOAD);
768 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
769 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
772 vnet_buffer (b0)->snat.flags = 0;
773 vnet_buffer (b1)->snat.flags = 0;
775 ip0 = vlib_buffer_get_current (b0);
776 udp0 = ip4_next_header (ip0);
777 tcp0 = (tcp_header_t *) udp0;
778 icmp0 = (icmp46_header_t *) udp0;
780 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
781 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
784 if (PREDICT_FALSE (ip0->ttl == 1))
786 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
787 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
788 ICMP4_time_exceeded_ttl_exceeded_in_transit,
790 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
794 proto0 = ip_proto_to_nat_proto (ip0->protocol);
796 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
798 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
800 if (!sm->forwarding_enabled)
803 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
804 next0 = SNAT_OUT2IN_NEXT_DROP;
807 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
808 thread_index, sw_if_index0, 1);
813 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
815 next0 = icmp_out2in_slow_path
816 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
817 next0, now, thread_index, &s0);
818 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
819 thread_index, sw_if_index0, 1);
823 init_nat_k (&kv0, ip0->dst_address,
824 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
826 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
828 /* Try to match static mapping by external address and port,
829 destination address and port in packet */
830 if (nat44_ei_static_mapping_match (
831 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
832 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
836 * Send DHCP packets to the ipv4 stack, or we won't
837 * be able to use dhcp client on the outside interface
840 (proto0 == NAT_PROTOCOL_UDP
841 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
842 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
844 vnet_feature_next (&next0, b0);
848 if (!sm->forwarding_enabled)
850 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
851 next0 = SNAT_OUT2IN_NEXT_DROP;
856 if (PREDICT_FALSE (identity_nat0))
859 /* Create session initiated by host from external network */
860 s0 = create_session_for_static_mapping (sm, b0,
864 vnet_buffer (b0)->ip.
866 rx_fib_index0, proto0, node,
870 next0 = SNAT_OUT2IN_NEXT_DROP;
875 s0 = pool_elt_at_index (tsm->sessions,
876 nat_value_get_session_index (&value0));
878 old_addr0 = ip0->dst_address.as_u32;
879 ip0->dst_address = s0->in2out.addr;
880 new_addr0 = ip0->dst_address.as_u32;
881 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
883 sum0 = ip0->checksum;
884 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
885 ip4_header_t, dst_address /* changed member */ );
886 ip0->checksum = ip_csum_fold (sum0);
888 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
890 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
892 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
893 new_port0 = udp0->dst_port = s0->in2out.port;
894 sum0 = tcp0->checksum;
895 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
897 dst_address /* changed member */ );
899 sum0 = ip_csum_update (sum0, old_port0, new_port0,
900 ip4_header_t /* cheat */ ,
901 length /* changed member */ );
902 tcp0->checksum = ip_csum_fold (sum0);
904 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
905 thread_index, sw_if_index0, 1);
909 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
911 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
912 new_port0 = udp0->dst_port = s0->in2out.port;
913 if (PREDICT_FALSE (udp0->checksum))
915 sum0 = udp0->checksum;
916 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
919 ip_csum_update (sum0, old_port0, new_port0,
920 ip4_header_t /* cheat */ ,
921 length /* changed member */ );
922 udp0->checksum = ip_csum_fold (sum0);
925 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
926 thread_index, sw_if_index0, 1);
930 nat44_ei_session_update_counters (
931 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
932 /* Per-user LRU list maintenance */
933 nat44_session_update_lru (sm, s0, thread_index);
936 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
937 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
939 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
940 t->sw_if_index = sw_if_index0;
941 t->next_index = next0;
942 t->session_index = ~0;
945 s0 - sm->per_thread_data[thread_index].sessions;
948 if (next0 == SNAT_OUT2IN_NEXT_DROP)
950 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
951 thread_index, sw_if_index0, 1);
955 ip1 = vlib_buffer_get_current (b1);
956 udp1 = ip4_next_header (ip1);
957 tcp1 = (tcp_header_t *) udp1;
958 icmp1 = (icmp46_header_t *) udp1;
960 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
961 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
964 if (PREDICT_FALSE (ip1->ttl == 1))
966 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
967 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
968 ICMP4_time_exceeded_ttl_exceeded_in_transit,
970 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
974 proto1 = ip_proto_to_nat_proto (ip1->protocol);
976 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
978 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
980 if (!sm->forwarding_enabled)
983 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
984 next1 = SNAT_OUT2IN_NEXT_DROP;
987 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
988 thread_index, sw_if_index1, 1);
992 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
994 next1 = icmp_out2in_slow_path
995 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
996 next1, now, thread_index, &s1);
997 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
998 thread_index, sw_if_index1, 1);
1002 init_nat_k (&kv1, ip1->dst_address,
1003 vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1005 if (clib_bihash_search_8_8 (&sm->out2in, &kv1, &value1))
1007 /* Try to match static mapping by external address and port,
1008 destination address and port in packet */
1009 if (nat44_ei_static_mapping_match (
1010 ip1->dst_address, vnet_buffer (b1)->ip.reass.l4_dst_port,
1011 rx_fib_index1, proto1, &sm_addr1, &sm_port1, &sm_fib_index1, 1,
1015 * Send DHCP packets to the ipv4 stack, or we won't
1016 * be able to use dhcp client on the outside interface
1019 (proto1 == NAT_PROTOCOL_UDP
1020 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1021 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1023 vnet_feature_next (&next1, b1);
1027 if (!sm->forwarding_enabled)
1029 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1030 next1 = SNAT_OUT2IN_NEXT_DROP;
1035 if (PREDICT_FALSE (identity_nat1))
1038 /* Create session initiated by host from external network */
1040 create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1043 vnet_buffer (b1)->ip.
1045 rx_fib_index1, proto1, node,
1049 next1 = SNAT_OUT2IN_NEXT_DROP;
1054 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1055 nat_value_get_session_index (&value1));
1057 old_addr1 = ip1->dst_address.as_u32;
1058 ip1->dst_address = s1->in2out.addr;
1059 new_addr1 = ip1->dst_address.as_u32;
1060 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1062 sum1 = ip1->checksum;
1063 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1064 ip4_header_t, dst_address /* changed member */ );
1065 ip1->checksum = ip_csum_fold (sum1);
1067 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1069 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1071 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1072 new_port1 = udp1->dst_port = s1->in2out.port;
1074 sum1 = tcp1->checksum;
1075 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1077 dst_address /* changed member */ );
1079 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1080 ip4_header_t /* cheat */ ,
1081 length /* changed member */ );
1082 tcp1->checksum = ip_csum_fold (sum1);
1084 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1085 thread_index, sw_if_index1, 1);
1089 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1091 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1092 new_port1 = udp1->dst_port = s1->in2out.port;
1093 if (PREDICT_FALSE (udp1->checksum))
1096 sum1 = udp1->checksum;
1098 ip_csum_update (sum1, old_addr1, new_addr1,
1100 dst_address /* changed member */ );
1102 ip_csum_update (sum1, old_port1, new_port1,
1103 ip4_header_t /* cheat */ ,
1104 length /* changed member */ );
1105 udp1->checksum = ip_csum_fold (sum1);
1108 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1109 thread_index, sw_if_index1, 1);
1113 nat44_ei_session_update_counters (
1114 s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1115 /* Per-user LRU list maintenance */
1116 nat44_session_update_lru (sm, s1, thread_index);
1119 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1120 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1122 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1123 t->sw_if_index = sw_if_index1;
1124 t->next_index = next1;
1125 t->session_index = ~0;
1128 s1 - sm->per_thread_data[thread_index].sessions;
1131 if (next1 == SNAT_OUT2IN_NEXT_DROP)
1133 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1134 thread_index, sw_if_index1, 1);
1143 while (n_left_from > 0)
1146 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1150 u32 new_addr0, old_addr0;
1151 u16 new_port0, old_port0;
1154 icmp46_header_t *icmp0;
1157 snat_session_t *s0 = 0;
1158 clib_bihash_kv_8_8_t kv0, value0;
1160 ip4_address_t sm_addr0;
1167 vnet_buffer (b0)->snat.flags = 0;
1169 ip0 = vlib_buffer_get_current (b0);
1170 udp0 = ip4_next_header (ip0);
1171 tcp0 = (tcp_header_t *) udp0;
1172 icmp0 = (icmp46_header_t *) udp0;
1174 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1175 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1178 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1180 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1182 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1184 if (!sm->forwarding_enabled)
1187 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1188 next0 = SNAT_OUT2IN_NEXT_DROP;
1191 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1192 thread_index, sw_if_index0, 1);
1196 if (PREDICT_FALSE (ip0->ttl == 1))
1198 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1199 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1200 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1202 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1206 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1208 next0 = icmp_out2in_slow_path
1209 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1210 next0, now, thread_index, &s0);
1211 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1212 thread_index, sw_if_index0, 1);
1216 init_nat_k (&kv0, ip0->dst_address,
1217 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1220 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
1222 /* Try to match static mapping by external address and port,
1223 destination address and port in packet */
1224 if (nat44_ei_static_mapping_match (
1225 ip0->dst_address, vnet_buffer (b0)->ip.reass.l4_dst_port,
1226 rx_fib_index0, proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1,
1230 * Send DHCP packets to the ipv4 stack, or we won't
1231 * be able to use dhcp client on the outside interface
1234 (proto0 == NAT_PROTOCOL_UDP
1235 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1236 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1238 vnet_feature_next (&next0, b0);
1242 if (!sm->forwarding_enabled)
1244 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1245 next0 = SNAT_OUT2IN_NEXT_DROP;
1250 if (PREDICT_FALSE (identity_nat0))
1253 /* Create session initiated by host from external network */
1254 s0 = create_session_for_static_mapping (sm, b0,
1258 vnet_buffer (b0)->ip.
1260 rx_fib_index0, proto0, node,
1264 next0 = SNAT_OUT2IN_NEXT_DROP;
1269 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1270 nat_value_get_session_index (&value0));
1272 old_addr0 = ip0->dst_address.as_u32;
1273 ip0->dst_address = s0->in2out.addr;
1274 new_addr0 = ip0->dst_address.as_u32;
1275 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1277 sum0 = ip0->checksum;
1278 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1279 ip4_header_t, dst_address /* changed member */ );
1280 ip0->checksum = ip_csum_fold (sum0);
1282 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1284 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1286 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1287 new_port0 = udp0->dst_port = s0->in2out.port;
1289 sum0 = tcp0->checksum;
1290 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1292 dst_address /* changed member */ );
1294 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1295 ip4_header_t /* cheat */ ,
1296 length /* changed member */ );
1297 tcp0->checksum = ip_csum_fold (sum0);
1299 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1300 thread_index, sw_if_index0, 1);
1304 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1306 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1307 new_port0 = udp0->dst_port = s0->in2out.port;
1308 if (PREDICT_FALSE (udp0->checksum))
1310 sum0 = udp0->checksum;
1311 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1314 ip_csum_update (sum0, old_port0, new_port0,
1315 ip4_header_t /* cheat */ ,
1316 length /* changed member */ );
1317 udp0->checksum = ip_csum_fold (sum0);
1320 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1321 thread_index, sw_if_index0, 1);
1325 nat44_ei_session_update_counters (
1326 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1327 /* Per-user LRU list maintenance */
1328 nat44_session_update_lru (sm, s0, thread_index);
1331 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1332 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1334 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1335 t->sw_if_index = sw_if_index0;
1336 t->next_index = next0;
1337 t->session_index = ~0;
1340 s0 - sm->per_thread_data[thread_index].sessions;
1343 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1345 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1346 thread_index, sw_if_index0, 1);
1354 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1357 return frame->n_vectors;
1361 VLIB_REGISTER_NODE (snat_out2in_node) = {
1362 .name = "nat44-out2in",
1363 .vector_size = sizeof (u32),
1364 .format_trace = format_snat_out2in_trace,
1365 .type = VLIB_NODE_TYPE_INTERNAL,
1367 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1368 .error_strings = snat_out2in_error_strings,
1370 .runtime_data_bytes = sizeof (snat_runtime_t),
1372 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1374 /* edit / add dispositions here */
1376 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1377 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1378 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1383 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1384 vlib_node_runtime_t * node,
1385 vlib_frame_t * frame)
1387 u32 n_left_from, *from;
1388 snat_main_t *sm = &snat_main;
1390 from = vlib_frame_vector_args (frame);
1391 n_left_from = frame->n_vectors;
1393 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1394 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1395 vlib_get_buffers (vm, from, b, n_left_from);
1396 while (n_left_from > 0)
1399 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1403 u32 new_addr0, old_addr0;
1404 u16 new_port0, old_port0;
1407 icmp46_header_t *icmp0;
1410 ip4_address_t sm_addr0;
1417 ip0 = vlib_buffer_get_current (b0);
1418 udp0 = ip4_next_header (ip0);
1419 tcp0 = (tcp_header_t *) udp0;
1420 icmp0 = (icmp46_header_t *) udp0;
1422 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1423 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1425 vnet_feature_next (&next0, b0);
1427 if (PREDICT_FALSE (ip0->ttl == 1))
1429 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1430 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1431 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1433 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1437 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1439 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1442 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1444 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1445 node, next0, ~0, 0);
1449 if (nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
1450 rx_fib_index0, proto0, &sm_addr0,
1451 &sm_port0, &sm_fib_index0, 1, 0, 0))
1453 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1457 new_addr0 = sm_addr0.as_u32;
1458 new_port0 = sm_port0;
1459 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1460 old_addr0 = ip0->dst_address.as_u32;
1461 ip0->dst_address.as_u32 = new_addr0;
1463 sum0 = ip0->checksum;
1464 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1465 ip4_header_t, dst_address /* changed member */ );
1466 ip0->checksum = ip_csum_fold (sum0);
1468 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1470 old_port0 = udp0->dst_port;
1471 udp0->dst_port = new_port0;
1473 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1475 sum0 = tcp0->checksum;
1476 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1478 dst_address /* changed member */ );
1479 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1480 ip4_header_t /* cheat */ ,
1481 length /* changed member */ );
1482 tcp0->checksum = ip_csum_fold (sum0);
1484 else if (udp0->checksum)
1486 sum0 = udp0->checksum;
1487 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1489 dst_address /* changed member */ );
1490 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1491 ip4_header_t /* cheat */ ,
1492 length /* changed member */ );
1493 udp0->checksum = ip_csum_fold (sum0);
1498 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1500 sum0 = tcp0->checksum;
1501 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1503 dst_address /* changed member */ );
1504 tcp0->checksum = ip_csum_fold (sum0);
1506 else if (udp0->checksum)
1508 sum0 = udp0->checksum;
1509 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1511 dst_address /* changed member */ );
1512 udp0->checksum = ip_csum_fold (sum0);
1518 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1519 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1521 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1522 t->sw_if_index = sw_if_index0;
1523 t->next_index = next0;
1526 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1528 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1529 vm->thread_index, sw_if_index0, 1);
1537 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1540 return frame->n_vectors;
1544 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1545 .name = "nat44-out2in-fast",
1546 .vector_size = sizeof (u32),
1547 .format_trace = format_snat_out2in_fast_trace,
1548 .type = VLIB_NODE_TYPE_INTERNAL,
1550 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1551 .error_strings = snat_out2in_error_strings,
1553 .runtime_data_bytes = sizeof (snat_runtime_t),
1555 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1557 /* edit / add dispositions here */
1559 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1560 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1561 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1567 * fd.io coding-style-patch-verification: ON
1570 * eval: (c-set-style "gnu")