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>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
42 } snat_out2in_trace_t;
44 /* packet trace format function */
46 format_snat_out2in_trace (u8 * s, va_list * args)
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
54 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
55 t->sw_if_index, t->next_index, t->session_index);
60 format_snat_out2in_fast_trace (u8 * s, va_list * args)
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
66 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
67 t->sw_if_index, t->next_index);
71 #define foreach_snat_out2in_error \
72 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
73 _(OUT_OF_PORTS, "out of ports") \
74 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
75 _(NO_TRANSLATION, "no translation") \
76 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
77 _(CANNOT_CREATE_USER, "cannot create NAT user")
81 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
82 foreach_snat_out2in_error
85 } snat_out2in_error_t;
87 static char *snat_out2in_error_strings[] = {
88 #define _(sym,string) string,
89 foreach_snat_out2in_error
95 SNAT_OUT2IN_NEXT_DROP,
96 SNAT_OUT2IN_NEXT_LOOKUP,
97 SNAT_OUT2IN_NEXT_ICMP_ERROR,
101 #ifndef CLIB_MARCH_VARIANT
103 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
105 snat_main_t *sm = &snat_main;
106 nat44_is_idle_session_ctx_t *ctx = arg;
108 u64 sess_timeout_time;
109 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
111 clib_bihash_kv_8_8_t s_kv;
113 s = pool_elt_at_index (tsm->sessions, kv->value);
114 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
115 if (ctx->now >= sess_timeout_time)
117 init_nat_i2o_k (&s_kv, s);
118 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
119 nat_elog_warn ("out2in key del failed");
121 nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
122 s->in2out.addr.as_u32,
123 s->out2in.addr.as_u32,
127 s->in2out.fib_index);
129 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
130 &s->in2out.addr, s->in2out.port,
131 &s->out2in.addr, s->out2in.port, s->nat_proto);
133 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
134 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
137 if (!snat_is_session_static (s))
138 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
139 &s->out2in.addr, s->out2in.port,
142 nat44_delete_session (sm, s, ctx->thread_index);
151 * @brief Create session for static mapping.
153 * Create NAT session initiated by host from external network with static
156 * @param sm NAT main.
157 * @param b0 Vlib buffer.
158 * @param in2out In2out NAT44 session key.
159 * @param out2in Out2in NAT44 session key.
160 * @param node Vlib node.
162 * @returns SNAT session if successfully created otherwise 0.
164 static inline snat_session_t *
165 create_session_for_static_mapping (snat_main_t * sm,
167 ip4_address_t i2o_addr,
170 ip4_address_t o2i_addr,
173 nat_protocol_t proto,
174 vlib_node_runtime_t * node,
175 u32 thread_index, f64 now)
179 clib_bihash_kv_8_8_t kv0;
182 nat44_is_idle_session_ctx_t ctx0;
184 if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
186 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
187 nat_elog_notice ("maximum sessions exceeded");
191 ip0 = vlib_buffer_get_current (b0);
192 udp0 = ip4_next_header (ip0);
194 u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
197 b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
201 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
204 nat44_delete_user_with_no_session (sm, u, thread_index);
205 nat_elog_warn ("create NAT session failed");
209 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
210 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
211 s->ext_host_port = udp0->src_port;
212 user_session_increment (sm, u, 1 /* static */ );
213 s->in2out.addr = i2o_addr;
214 s->in2out.port = i2o_port;
215 s->in2out.fib_index = i2o_fib_index;
216 s->out2in.addr = o2i_addr;
217 s->out2in.port = o2i_port;
218 s->out2in.fib_index = o2i_fib_index;
219 s->nat_proto = proto;
221 /* Add to translation hashes */
223 ctx0.thread_index = thread_index;
224 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
225 if (clib_bihash_add_or_overwrite_stale_8_8
226 (&sm->per_thread_data[thread_index].in2out, &kv0,
227 nat44_i2o_is_idle_session_cb, &ctx0))
228 nat_elog_notice ("in2out key add failed");
230 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
231 if (clib_bihash_add_or_overwrite_stale_8_8
232 (&sm->per_thread_data[thread_index].out2in, &kv0,
233 nat44_o2i_is_idle_session_cb, &ctx0))
234 nat_elog_notice ("out2in key add failed");
237 nat_ipfix_logging_nat44_ses_create (thread_index,
238 s->in2out.addr.as_u32,
239 s->out2in.addr.as_u32,
242 s->out2in.port, s->in2out.fib_index);
244 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
245 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
246 s->out2in.port, s->nat_proto);
248 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
249 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
250 &s->ext_host_nat_addr, s->ext_host_nat_port,
251 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
256 #ifndef CLIB_MARCH_VARIANT
257 static_always_inline snat_out2in_error_t
258 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
259 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
261 icmp46_header_t *icmp0;
262 icmp_echo_header_t *echo0, *inner_echo0 = 0;
263 ip4_header_t *inner_ip0;
265 icmp46_header_t *inner_icmp0;
267 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
268 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
270 if (!icmp_type_is_error_message
271 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
273 *nat_proto = NAT_PROTOCOL_ICMP;
274 *addr = ip0->dst_address;
275 *port = vnet_buffer (b)->ip.reass.l4_src_port;
279 inner_ip0 = (ip4_header_t *) (echo0 + 1);
280 l4_header = ip4_next_header (inner_ip0);
281 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
282 *addr = inner_ip0->src_address;
285 case NAT_PROTOCOL_ICMP:
286 inner_icmp0 = (icmp46_header_t *) l4_header;
287 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
288 *port = inner_echo0->identifier;
290 case NAT_PROTOCOL_UDP:
291 case NAT_PROTOCOL_TCP:
292 *port = ((tcp_udp_header_t *) l4_header)->src_port;
295 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
298 return -1; /* success */
302 * Get address and port values to be used for ICMP packet translation
303 * and create session if needed
305 * @param[in,out] sm NAT main
306 * @param[in,out] node NAT node runtime
307 * @param[in] thread_index thread index
308 * @param[in,out] b0 buffer containing packet to be translated
309 * @param[in,out] ip0 ip header
310 * @param[out] p_proto protocol used for matching
311 * @param[out] p_value address and port after NAT translation
312 * @param[out] p_dont_translate if packet should not be translated
313 * @param d optional parameter
314 * @param e optional parameter
317 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
318 u32 thread_index, vlib_buffer_t * b0,
319 ip4_header_t * ip0, ip4_address_t * addr,
320 u16 * port, u32 * fib_index,
321 nat_protocol_t * proto, void *d, void *e,
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 (&tsm->out2in, &kv0, &value0))
355 /* Try to match static mapping by external address and port,
356 destination address and port in packet */
357 if (snat_static_mapping_match
358 (sm, *addr, *port, *fib_index, *proto,
359 &mapping_addr, &mapping_port, &mapping_fib_index, 1, &is_addr_only,
360 0, 0, 0, &identity_nat, 0))
362 if (!sm->forwarding_enabled)
364 /* Don't NAT packet aimed at the intfc address */
365 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
366 ip0->dst_address.as_u32)))
371 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
372 next0 = SNAT_OUT2IN_NEXT_DROP;
383 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
385 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
386 ICMP4_echo_request || !is_addr_only)))
388 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
389 next0 = SNAT_OUT2IN_NEXT_DROP;
393 if (PREDICT_FALSE (identity_nat))
398 /* Create session initiated by host from external network */
400 create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
401 mapping_fib_index, *addr, *port,
402 *fib_index, *proto, node,
403 thread_index, vlib_time_now (vm));
407 next0 = SNAT_OUT2IN_NEXT_DROP;
414 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
416 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
418 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
419 reass.icmp_type_or_tcp_flags)))
421 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
422 next0 = SNAT_OUT2IN_NEXT_DROP;
426 s0 = pool_elt_at_index (tsm->sessions, value0.value);
432 *addr = s0->in2out.addr;
433 *port = s0->in2out.port;
434 *fib_index = s0->in2out.fib_index;
437 *(snat_session_t **) d = s0;
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,
460 ip4_header_t * ip0, ip4_address_t * mapping_addr,
461 u16 * mapping_port, u32 * mapping_fib_index,
462 nat_protocol_t * proto, void *d, void *e,
474 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
475 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
477 err = icmp_get_key (b0, ip0, &addr, &port, proto);
480 b0->error = node->errors[err];
481 next0 = SNAT_OUT2IN_NEXT_DROP;
484 if (snat_static_mapping_match
485 (sm, addr, port, rx_fib_index0, *proto, mapping_addr, mapping_port,
486 mapping_fib_index, 1, &is_addr_only, 0, 0, 0, 0, 0))
488 /* Don't NAT packet aimed at the intfc address */
489 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
494 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
495 next0 = SNAT_OUT2IN_NEXT_DROP;
500 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
501 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
502 ICMP4_echo_request || !is_addr_only)
503 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
504 reass.icmp_type_or_tcp_flags)))
506 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
507 next0 = SNAT_OUT2IN_NEXT_DROP;
516 #ifndef CLIB_MARCH_VARIANT
518 icmp_out2in (snat_main_t * sm,
521 icmp46_header_t * icmp0,
524 vlib_node_runtime_t * node,
525 u32 next0, u32 thread_index, void *d, void *e)
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);
545 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
546 &addr, &port, &fib_index, &proto,
547 d, e, &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, 0);
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
827 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
829 /* Try to match static mapping by external address and port,
830 destination address and port in packet */
831 if (snat_static_mapping_match
832 (sm, ip0->dst_address,
833 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
834 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
835 0, &identity_nat0, 0))
838 * Send DHCP packets to the ipv4 stack, or we won't
839 * be able to use dhcp client on the outside interface
842 (proto0 == NAT_PROTOCOL_UDP
843 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
844 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
846 vnet_feature_next (&next0, b0);
850 if (!sm->forwarding_enabled)
852 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
853 next0 = SNAT_OUT2IN_NEXT_DROP;
858 if (PREDICT_FALSE (identity_nat0))
861 /* Create session initiated by host from external network */
862 s0 = create_session_for_static_mapping (sm, b0,
866 vnet_buffer (b0)->ip.
868 rx_fib_index0, proto0, node,
872 next0 = SNAT_OUT2IN_NEXT_DROP;
877 s0 = pool_elt_at_index (tsm->sessions, value0.value);
879 old_addr0 = ip0->dst_address.as_u32;
880 ip0->dst_address = s0->in2out.addr;
881 new_addr0 = ip0->dst_address.as_u32;
882 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
884 sum0 = ip0->checksum;
885 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
886 ip4_header_t, dst_address /* changed member */ );
887 ip0->checksum = ip_csum_fold (sum0);
889 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
891 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
893 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
894 new_port0 = udp0->dst_port = s0->in2out.port;
895 sum0 = tcp0->checksum;
896 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
898 dst_address /* changed member */ );
900 sum0 = ip_csum_update (sum0, old_port0, new_port0,
901 ip4_header_t /* cheat */ ,
902 length /* changed member */ );
903 tcp0->checksum = ip_csum_fold (sum0);
905 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
906 thread_index, sw_if_index0, 1);
910 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
912 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
913 new_port0 = udp0->dst_port = s0->in2out.port;
914 if (PREDICT_FALSE (udp0->checksum))
916 sum0 = udp0->checksum;
917 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
920 ip_csum_update (sum0, old_port0, new_port0,
921 ip4_header_t /* cheat */ ,
922 length /* changed member */ );
923 udp0->checksum = ip_csum_fold (sum0);
926 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
927 thread_index, sw_if_index0, 1);
931 nat44_ei_session_update_counters (
932 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
933 /* Per-user LRU list maintenance */
934 nat44_session_update_lru (sm, s0, thread_index);
937 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
938 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
940 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
941 t->sw_if_index = sw_if_index0;
942 t->next_index = next0;
943 t->session_index = ~0;
946 s0 - sm->per_thread_data[thread_index].sessions;
949 if (next0 == SNAT_OUT2IN_NEXT_DROP)
951 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
952 thread_index, sw_if_index0, 1);
956 ip1 = vlib_buffer_get_current (b1);
957 udp1 = ip4_next_header (ip1);
958 tcp1 = (tcp_header_t *) udp1;
959 icmp1 = (icmp46_header_t *) udp1;
961 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
962 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
965 if (PREDICT_FALSE (ip1->ttl == 1))
967 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
968 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
969 ICMP4_time_exceeded_ttl_exceeded_in_transit,
971 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
975 proto1 = ip_proto_to_nat_proto (ip1->protocol);
977 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
979 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
981 if (!sm->forwarding_enabled)
984 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
985 next1 = SNAT_OUT2IN_NEXT_DROP;
988 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
989 thread_index, sw_if_index1, 1);
993 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
995 next1 = icmp_out2in_slow_path
996 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
997 next1, now, thread_index, &s1);
998 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
999 thread_index, sw_if_index1, 1);
1003 init_nat_k (&kv1, ip1->dst_address,
1004 vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1007 if (clib_bihash_search_8_8
1008 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1010 /* Try to match static mapping by external address and port,
1011 destination address and port in packet */
1012 if (snat_static_mapping_match
1013 (sm, ip1->dst_address,
1014 vnet_buffer (b1)->ip.reass.l4_dst_port, proto1,
1015 rx_fib_index1, &sm_addr1, &sm_port1, &sm_fib_index1, 1, 0,
1016 0, 0, 0, &identity_nat1, 0))
1019 * Send DHCP packets to the ipv4 stack, or we won't
1020 * be able to use dhcp client on the outside interface
1023 (proto1 == NAT_PROTOCOL_UDP
1024 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1025 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1027 vnet_feature_next (&next1, b1);
1031 if (!sm->forwarding_enabled)
1033 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1034 next1 = SNAT_OUT2IN_NEXT_DROP;
1039 if (PREDICT_FALSE (identity_nat1))
1042 /* Create session initiated by host from external network */
1044 create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1047 vnet_buffer (b1)->ip.
1049 rx_fib_index1, proto1, node,
1053 next1 = SNAT_OUT2IN_NEXT_DROP;
1059 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1062 old_addr1 = ip1->dst_address.as_u32;
1063 ip1->dst_address = s1->in2out.addr;
1064 new_addr1 = ip1->dst_address.as_u32;
1065 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1067 sum1 = ip1->checksum;
1068 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1069 ip4_header_t, dst_address /* changed member */ );
1070 ip1->checksum = ip_csum_fold (sum1);
1072 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1074 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1076 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1077 new_port1 = udp1->dst_port = s1->in2out.port;
1079 sum1 = tcp1->checksum;
1080 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1082 dst_address /* changed member */ );
1084 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1085 ip4_header_t /* cheat */ ,
1086 length /* changed member */ );
1087 tcp1->checksum = ip_csum_fold (sum1);
1089 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1090 thread_index, sw_if_index1, 1);
1094 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1096 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1097 new_port1 = udp1->dst_port = s1->in2out.port;
1098 if (PREDICT_FALSE (udp1->checksum))
1101 sum1 = udp1->checksum;
1103 ip_csum_update (sum1, old_addr1, new_addr1,
1105 dst_address /* changed member */ );
1107 ip_csum_update (sum1, old_port1, new_port1,
1108 ip4_header_t /* cheat */ ,
1109 length /* changed member */ );
1110 udp1->checksum = ip_csum_fold (sum1);
1113 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1114 thread_index, sw_if_index1, 1);
1118 nat44_ei_session_update_counters (
1119 s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1120 /* Per-user LRU list maintenance */
1121 nat44_session_update_lru (sm, s1, thread_index);
1124 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1125 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1127 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1128 t->sw_if_index = sw_if_index1;
1129 t->next_index = next1;
1130 t->session_index = ~0;
1133 s1 - sm->per_thread_data[thread_index].sessions;
1136 if (next1 == SNAT_OUT2IN_NEXT_DROP)
1138 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1139 thread_index, sw_if_index1, 1);
1148 while (n_left_from > 0)
1151 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1155 u32 new_addr0, old_addr0;
1156 u16 new_port0, old_port0;
1159 icmp46_header_t *icmp0;
1162 snat_session_t *s0 = 0;
1163 clib_bihash_kv_8_8_t kv0, value0;
1165 ip4_address_t sm_addr0;
1172 vnet_buffer (b0)->snat.flags = 0;
1174 ip0 = vlib_buffer_get_current (b0);
1175 udp0 = ip4_next_header (ip0);
1176 tcp0 = (tcp_header_t *) udp0;
1177 icmp0 = (icmp46_header_t *) udp0;
1179 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1180 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1183 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1185 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1187 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1189 if (!sm->forwarding_enabled)
1192 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1193 next0 = SNAT_OUT2IN_NEXT_DROP;
1196 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1197 thread_index, sw_if_index0, 1);
1201 if (PREDICT_FALSE (ip0->ttl == 1))
1203 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1204 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1205 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1207 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1211 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1213 next0 = icmp_out2in_slow_path
1214 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1215 next0, now, thread_index, &s0);
1216 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1217 thread_index, sw_if_index0, 1);
1221 init_nat_k (&kv0, ip0->dst_address,
1222 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1225 if (clib_bihash_search_8_8
1226 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1228 /* Try to match static mapping by external address and port,
1229 destination address and port in packet */
1230 if (snat_static_mapping_match
1231 (sm, ip0->dst_address,
1232 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1233 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
1234 0, &identity_nat0, 0))
1237 * Send DHCP packets to the ipv4 stack, or we won't
1238 * be able to use dhcp client on the outside interface
1241 (proto0 == NAT_PROTOCOL_UDP
1242 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1243 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1245 vnet_feature_next (&next0, b0);
1249 if (!sm->forwarding_enabled)
1251 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1252 next0 = SNAT_OUT2IN_NEXT_DROP;
1257 if (PREDICT_FALSE (identity_nat0))
1260 /* Create session initiated by host from external network */
1261 s0 = create_session_for_static_mapping (sm, b0,
1265 vnet_buffer (b0)->ip.
1267 rx_fib_index0, proto0, node,
1271 next0 = SNAT_OUT2IN_NEXT_DROP;
1277 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1280 old_addr0 = ip0->dst_address.as_u32;
1281 ip0->dst_address = s0->in2out.addr;
1282 new_addr0 = ip0->dst_address.as_u32;
1283 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1285 sum0 = ip0->checksum;
1286 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1287 ip4_header_t, dst_address /* changed member */ );
1288 ip0->checksum = ip_csum_fold (sum0);
1290 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1292 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1294 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1295 new_port0 = udp0->dst_port = s0->in2out.port;
1297 sum0 = tcp0->checksum;
1298 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1300 dst_address /* changed member */ );
1302 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1303 ip4_header_t /* cheat */ ,
1304 length /* changed member */ );
1305 tcp0->checksum = ip_csum_fold (sum0);
1307 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1308 thread_index, sw_if_index0, 1);
1312 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1314 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1315 new_port0 = udp0->dst_port = s0->in2out.port;
1316 if (PREDICT_FALSE (udp0->checksum))
1318 sum0 = udp0->checksum;
1319 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1322 ip_csum_update (sum0, old_port0, new_port0,
1323 ip4_header_t /* cheat */ ,
1324 length /* changed member */ );
1325 udp0->checksum = ip_csum_fold (sum0);
1328 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1329 thread_index, sw_if_index0, 1);
1333 nat44_ei_session_update_counters (
1334 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1335 /* Per-user LRU list maintenance */
1336 nat44_session_update_lru (sm, s0, thread_index);
1339 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1340 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1342 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1343 t->sw_if_index = sw_if_index0;
1344 t->next_index = next0;
1345 t->session_index = ~0;
1348 s0 - sm->per_thread_data[thread_index].sessions;
1351 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1353 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1354 thread_index, sw_if_index0, 1);
1362 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1365 return frame->n_vectors;
1369 VLIB_REGISTER_NODE (snat_out2in_node) = {
1370 .name = "nat44-out2in",
1371 .vector_size = sizeof (u32),
1372 .format_trace = format_snat_out2in_trace,
1373 .type = VLIB_NODE_TYPE_INTERNAL,
1375 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1376 .error_strings = snat_out2in_error_strings,
1378 .runtime_data_bytes = sizeof (snat_runtime_t),
1380 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1382 /* edit / add dispositions here */
1384 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1385 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1386 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1391 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1392 vlib_node_runtime_t * node,
1393 vlib_frame_t * frame)
1395 u32 n_left_from, *from;
1396 snat_main_t *sm = &snat_main;
1398 from = vlib_frame_vector_args (frame);
1399 n_left_from = frame->n_vectors;
1401 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1402 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1403 vlib_get_buffers (vm, from, b, n_left_from);
1404 while (n_left_from > 0)
1407 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1411 u32 new_addr0, old_addr0;
1412 u16 new_port0, old_port0;
1415 icmp46_header_t *icmp0;
1418 ip4_address_t sm_addr0;
1425 ip0 = vlib_buffer_get_current (b0);
1426 udp0 = ip4_next_header (ip0);
1427 tcp0 = (tcp_header_t *) udp0;
1428 icmp0 = (icmp46_header_t *) udp0;
1430 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1431 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1433 vnet_feature_next (&next0, b0);
1435 if (PREDICT_FALSE (ip0->ttl == 1))
1437 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1438 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1439 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1441 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1445 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1447 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1452 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1453 rx_fib_index0, node, next0, ~0, 0, 0);
1457 if (snat_static_mapping_match
1458 (sm, ip0->dst_address, udp0->dst_port, rx_fib_index0, proto0,
1459 &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0, 0, 0, 0))
1461 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1465 new_addr0 = sm_addr0.as_u32;
1466 new_port0 = sm_port0;
1467 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1468 old_addr0 = ip0->dst_address.as_u32;
1469 ip0->dst_address.as_u32 = new_addr0;
1471 sum0 = ip0->checksum;
1472 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1473 ip4_header_t, dst_address /* changed member */ );
1474 ip0->checksum = ip_csum_fold (sum0);
1476 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1478 old_port0 = udp0->dst_port;
1479 udp0->dst_port = new_port0;
1481 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1483 sum0 = tcp0->checksum;
1484 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1486 dst_address /* changed member */ );
1487 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1488 ip4_header_t /* cheat */ ,
1489 length /* changed member */ );
1490 tcp0->checksum = ip_csum_fold (sum0);
1492 else if (udp0->checksum)
1494 sum0 = udp0->checksum;
1495 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1497 dst_address /* changed member */ );
1498 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1499 ip4_header_t /* cheat */ ,
1500 length /* changed member */ );
1501 udp0->checksum = ip_csum_fold (sum0);
1506 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1508 sum0 = tcp0->checksum;
1509 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1511 dst_address /* changed member */ );
1512 tcp0->checksum = ip_csum_fold (sum0);
1514 else if (udp0->checksum)
1516 sum0 = udp0->checksum;
1517 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1519 dst_address /* changed member */ );
1520 udp0->checksum = ip_csum_fold (sum0);
1526 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1527 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1529 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1530 t->sw_if_index = sw_if_index0;
1531 t->next_index = next0;
1534 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1536 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1537 vm->thread_index, sw_if_index0, 1);
1545 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1548 return frame->n_vectors;
1552 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1553 .name = "nat44-out2in-fast",
1554 .vector_size = sizeof (u32),
1555 .format_trace = format_snat_out2in_fast_trace,
1556 .type = VLIB_NODE_TYPE_INTERNAL,
1558 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1559 .error_strings = snat_out2in_error_strings,
1561 .runtime_data_bytes = sizeof (snat_runtime_t),
1563 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1565 /* edit / add dispositions here */
1567 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1568 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1569 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1575 * fd.io coding-style-patch-verification: ON
1578 * eval: (c-set-style "gnu")