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 _(OUT_OF_PORTS, "out of ports") \
76 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
77 _(NO_TRANSLATION, "no translation") \
78 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
79 _(CANNOT_CREATE_USER, "cannot create NAT user")
83 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
84 foreach_snat_out2in_error
87 } snat_out2in_error_t;
89 static char *snat_out2in_error_strings[] = {
90 #define _(sym,string) string,
91 foreach_snat_out2in_error
97 SNAT_OUT2IN_NEXT_DROP,
98 SNAT_OUT2IN_NEXT_LOOKUP,
99 SNAT_OUT2IN_NEXT_ICMP_ERROR,
101 } snat_out2in_next_t;
103 #ifndef CLIB_MARCH_VARIANT
105 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
107 snat_main_t *sm = &snat_main;
108 nat44_is_idle_session_ctx_t *ctx = arg;
110 u64 sess_timeout_time;
111 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
113 clib_bihash_kv_8_8_t s_kv;
115 s = pool_elt_at_index (tsm->sessions, kv->value);
116 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
117 if (ctx->now >= sess_timeout_time)
119 init_nat_i2o_k (&s_kv, s);
120 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
121 nat_elog_warn ("out2in key del failed");
123 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
124 s->in2out.addr.as_u32,
125 s->out2in.addr.as_u32,
129 s->in2out.fib_index);
131 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
132 &s->in2out.addr, s->in2out.port,
133 &s->out2in.addr, s->out2in.port, s->nat_proto);
135 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
136 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
139 if (!snat_is_session_static (s))
140 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
141 &s->out2in.addr, s->out2in.port,
144 nat44_delete_session (sm, s, ctx->thread_index);
153 * @brief Create session for static mapping.
155 * Create NAT session initiated by host from external network with static
158 * @param sm NAT main.
159 * @param b0 Vlib buffer.
160 * @param in2out In2out NAT44 session key.
161 * @param out2in Out2in NAT44 session key.
162 * @param node Vlib node.
164 * @returns SNAT session if successfully created otherwise 0.
166 static inline snat_session_t *
167 create_session_for_static_mapping (snat_main_t * sm,
169 ip4_address_t i2o_addr,
172 ip4_address_t o2i_addr,
175 nat_protocol_t proto,
176 vlib_node_runtime_t * node,
177 u32 thread_index, f64 now)
181 clib_bihash_kv_8_8_t kv0;
184 nat44_is_idle_session_ctx_t ctx0;
186 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
188 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
189 nat_elog_notice ("maximum sessions exceeded");
193 ip0 = vlib_buffer_get_current (b0);
194 udp0 = ip4_next_header (ip0);
196 u = nat_user_get_or_create (sm, &i2o_addr, i2o_fib_index, thread_index);
199 b0->error = node->errors[SNAT_OUT2IN_ERROR_CANNOT_CREATE_USER];
203 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
206 nat44_delete_user_with_no_session (sm, u, thread_index);
207 nat_elog_warn ("create NAT session failed");
211 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
212 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
213 s->ext_host_port = udp0->src_port;
214 user_session_increment (sm, u, 1 /* static */ );
215 s->in2out.addr = i2o_addr;
216 s->in2out.port = i2o_port;
217 s->in2out.fib_index = i2o_fib_index;
218 s->out2in.addr = o2i_addr;
219 s->out2in.port = o2i_port;
220 s->out2in.fib_index = o2i_fib_index;
221 s->nat_proto = proto;
223 /* Add to translation hashes */
225 ctx0.thread_index = thread_index;
226 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
227 if (clib_bihash_add_or_overwrite_stale_8_8
228 (&sm->per_thread_data[thread_index].in2out, &kv0,
229 nat44_i2o_is_idle_session_cb, &ctx0))
230 nat_elog_notice ("in2out key add failed");
232 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
233 if (clib_bihash_add_or_overwrite_stale_8_8
234 (&sm->per_thread_data[thread_index].out2in, &kv0,
235 nat44_o2i_is_idle_session_cb, &ctx0))
236 nat_elog_notice ("out2in key add failed");
239 snat_ipfix_logging_nat44_ses_create (thread_index,
240 s->in2out.addr.as_u32,
241 s->out2in.addr.as_u32,
244 s->out2in.port, s->in2out.fib_index);
246 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
247 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
248 s->out2in.port, s->nat_proto);
250 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
251 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
252 &s->ext_host_nat_addr, s->ext_host_nat_port,
253 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
258 #ifndef CLIB_MARCH_VARIANT
259 static_always_inline snat_out2in_error_t
260 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
261 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
263 icmp46_header_t *icmp0;
264 icmp_echo_header_t *echo0, *inner_echo0 = 0;
265 ip4_header_t *inner_ip0;
267 icmp46_header_t *inner_icmp0;
269 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
270 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
272 if (!icmp_type_is_error_message
273 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
275 *nat_proto = NAT_PROTOCOL_ICMP;
276 *addr = ip0->dst_address;
277 *port = vnet_buffer (b)->ip.reass.l4_src_port;
281 inner_ip0 = (ip4_header_t *) (echo0 + 1);
282 l4_header = ip4_next_header (inner_ip0);
283 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
284 *addr = inner_ip0->src_address;
287 case NAT_PROTOCOL_ICMP:
288 inner_icmp0 = (icmp46_header_t *) l4_header;
289 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
290 *port = inner_echo0->identifier;
292 case NAT_PROTOCOL_UDP:
293 case NAT_PROTOCOL_TCP:
294 *port = ((tcp_udp_header_t *) l4_header)->src_port;
297 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
300 return -1; /* success */
304 * Get address and port values to be used for ICMP packet translation
305 * and create session if needed
307 * @param[in,out] sm NAT main
308 * @param[in,out] node NAT node runtime
309 * @param[in] thread_index thread index
310 * @param[in,out] b0 buffer containing packet to be translated
311 * @param[in,out] ip0 ip header
312 * @param[out] p_proto protocol used for matching
313 * @param[out] p_value address and port after NAT translation
314 * @param[out] p_dont_translate if packet should not be translated
315 * @param d optional parameter
316 * @param e optional parameter
319 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
320 u32 thread_index, vlib_buffer_t * b0,
321 ip4_header_t * ip0, ip4_address_t * addr,
322 u16 * port, u32 * fib_index,
323 nat_protocol_t * proto, void *d, void *e,
326 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
328 snat_session_t *s0 = 0;
329 clib_bihash_kv_8_8_t kv0, value0;
334 vlib_main_t *vm = vlib_get_main ();
337 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
338 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
342 err = icmp_get_key (b0, ip0, addr, port, proto);
345 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
346 next0 = SNAT_OUT2IN_NEXT_DROP;
350 ip4_address_t mapping_addr;
352 u32 mapping_fib_index;
354 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
355 if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
357 /* Try to match static mapping by external address and port,
358 destination address and port in packet */
359 if (snat_static_mapping_match
360 (sm, *addr, *port, *fib_index, *proto,
361 &mapping_addr, &mapping_port, &mapping_fib_index, 1, &is_addr_only,
362 0, 0, 0, &identity_nat, 0))
364 if (!sm->forwarding_enabled)
366 /* Don't NAT packet aimed at the intfc address */
367 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
368 ip0->dst_address.as_u32)))
373 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
374 next0 = SNAT_OUT2IN_NEXT_DROP;
385 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
387 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
388 ICMP4_echo_request || !is_addr_only)))
390 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
391 next0 = SNAT_OUT2IN_NEXT_DROP;
395 if (PREDICT_FALSE (identity_nat))
400 /* Create session initiated by host from external network */
402 create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
403 mapping_fib_index, *addr, *port,
404 *fib_index, *proto, node,
405 thread_index, vlib_time_now (vm));
409 next0 = SNAT_OUT2IN_NEXT_DROP;
416 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
418 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
420 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
421 reass.icmp_type_or_tcp_flags)))
423 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
424 next0 = SNAT_OUT2IN_NEXT_DROP;
428 s0 = pool_elt_at_index (tsm->sessions, value0.value);
434 *addr = s0->in2out.addr;
435 *port = s0->in2out.port;
436 *fib_index = s0->in2out.fib_index;
439 *(snat_session_t **) d = s0;
444 #ifndef CLIB_MARCH_VARIANT
446 * Get address and port values to be used for ICMP packet translation
448 * @param[in] sm NAT main
449 * @param[in,out] node NAT node runtime
450 * @param[in] thread_index thread index
451 * @param[in,out] b0 buffer containing packet to be translated
452 * @param[in,out] ip0 ip header
453 * @param[out] p_proto protocol used for matching
454 * @param[out] p_value address and port after NAT translation
455 * @param[out] p_dont_translate if packet should not be translated
456 * @param d optional parameter
457 * @param e optional parameter
460 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
461 u32 thread_index, vlib_buffer_t * b0,
462 ip4_header_t * ip0, ip4_address_t * mapping_addr,
463 u16 * mapping_port, u32 * mapping_fib_index,
464 nat_protocol_t * proto, void *d, void *e,
476 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
477 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
479 err = icmp_get_key (b0, ip0, &addr, &port, proto);
482 b0->error = node->errors[err];
483 next0 = SNAT_OUT2IN_NEXT_DROP;
486 if (snat_static_mapping_match
487 (sm, addr, port, rx_fib_index0, *proto, mapping_addr, mapping_port,
488 mapping_fib_index, 1, &is_addr_only, 0, 0, 0, 0, 0))
490 /* Don't NAT packet aimed at the intfc address */
491 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
496 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
497 next0 = SNAT_OUT2IN_NEXT_DROP;
502 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
503 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
504 ICMP4_echo_request || !is_addr_only)
505 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
506 reass.icmp_type_or_tcp_flags)))
508 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
509 next0 = SNAT_OUT2IN_NEXT_DROP;
518 #ifndef CLIB_MARCH_VARIANT
520 icmp_out2in (snat_main_t * sm,
523 icmp46_header_t * icmp0,
526 vlib_node_runtime_t * node,
527 u32 next0, u32 thread_index, void *d, void *e)
529 icmp_echo_header_t *echo0, *inner_echo0 = 0;
530 ip4_header_t *inner_ip0 = 0;
532 icmp46_header_t *inner_icmp0;
534 u32 new_addr0, old_addr0;
535 u16 old_id0, new_id0;
539 vlib_main_t *vm = vlib_get_main ();
543 nat_protocol_t proto;
545 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
547 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
548 &addr, &port, &fib_index, &proto,
549 d, e, &dont_translate);
552 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
555 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
558 ip_incremental_checksum_buffer (vm, b0,
560 (u8 *) vlib_buffer_get_current (b0),
561 ntohs (ip0->length) -
562 ip4_header_bytes (ip0), 0);
563 checksum0 = ~ip_csum_fold (sum0);
564 if (checksum0 != 0 && checksum0 != 0xffff)
566 next0 = SNAT_OUT2IN_NEXT_DROP;
571 old_addr0 = ip0->dst_address.as_u32;
572 new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
573 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
575 sum0 = ip0->checksum;
576 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
577 dst_address /* changed member */ );
578 ip0->checksum = ip_csum_fold (sum0);
581 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
583 if (icmp0->checksum == 0)
584 icmp0->checksum = 0xffff;
586 if (!icmp_type_is_error_message (icmp0->type))
589 if (PREDICT_FALSE (new_id0 != echo0->identifier))
591 old_id0 = echo0->identifier;
593 echo0->identifier = new_id0;
595 sum0 = icmp0->checksum;
597 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
598 identifier /* changed member */ );
599 icmp0->checksum = ip_csum_fold (sum0);
604 inner_ip0 = (ip4_header_t *) (echo0 + 1);
605 l4_header = ip4_next_header (inner_ip0);
607 if (!ip4_header_checksum_is_valid (inner_ip0))
609 next0 = SNAT_OUT2IN_NEXT_DROP;
613 old_addr0 = inner_ip0->src_address.as_u32;
614 inner_ip0->src_address = addr;
615 new_addr0 = inner_ip0->src_address.as_u32;
617 sum0 = icmp0->checksum;
618 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
619 src_address /* changed member */ );
620 icmp0->checksum = ip_csum_fold (sum0);
624 case NAT_PROTOCOL_ICMP:
625 inner_icmp0 = (icmp46_header_t *) l4_header;
626 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
628 old_id0 = inner_echo0->identifier;
630 inner_echo0->identifier = new_id0;
632 sum0 = icmp0->checksum;
634 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
636 icmp0->checksum = ip_csum_fold (sum0);
638 case NAT_PROTOCOL_UDP:
639 case NAT_PROTOCOL_TCP:
640 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
642 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
644 sum0 = icmp0->checksum;
645 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
647 icmp0->checksum = ip_csum_fold (sum0);
661 icmp_out2in_slow_path (snat_main_t * sm,
664 icmp46_header_t * icmp0,
667 vlib_node_runtime_t * node,
669 u32 thread_index, snat_session_t ** p_s0)
671 vlib_main_t *vm = vlib_get_main ();
673 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
674 next0, thread_index, p_s0, 0);
675 snat_session_t *s0 = *p_s0;
676 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
679 nat44_session_update_counters (s0, now,
680 vlib_buffer_length_in_chain
681 (vm, b0), thread_index);
682 /* Per-user LRU list maintenance */
683 nat44_session_update_lru (sm, s0, thread_index);
689 nat_out2in_sm_unknown_proto (snat_main_t * sm,
691 ip4_header_t * ip, u32 rx_fib_index)
693 clib_bihash_kv_8_8_t kv, value;
694 snat_static_mapping_t *m;
695 u32 old_addr, new_addr;
698 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
699 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
702 m = pool_elt_at_index (sm->static_mappings, value.value);
704 old_addr = ip->dst_address.as_u32;
705 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
707 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
708 ip->checksum = ip_csum_fold (sum);
710 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
714 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
715 vlib_node_runtime_t * node,
716 vlib_frame_t * frame)
718 u32 n_left_from, *from;
719 snat_main_t *sm = &snat_main;
720 f64 now = vlib_time_now (vm);
721 u32 thread_index = vm->thread_index;
722 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
724 from = vlib_frame_vector_args (frame);
725 n_left_from = frame->n_vectors;
727 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
728 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
729 vlib_get_buffers (vm, from, b, n_left_from);
731 while (n_left_from >= 2)
733 vlib_buffer_t *b0, *b1;
734 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
735 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
736 u32 sw_if_index0, sw_if_index1;
737 ip4_header_t *ip0, *ip1;
738 ip_csum_t sum0, sum1;
739 u32 new_addr0, old_addr0;
740 u16 new_port0, old_port0;
741 u32 new_addr1, old_addr1;
742 u16 new_port1, old_port1;
743 udp_header_t *udp0, *udp1;
744 tcp_header_t *tcp0, *tcp1;
745 icmp46_header_t *icmp0, *icmp1;
746 u32 rx_fib_index0, rx_fib_index1;
748 snat_session_t *s0 = 0, *s1 = 0;
749 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
750 u8 identity_nat0, identity_nat1;
751 ip4_address_t sm_addr0, sm_addr1;
752 u16 sm_port0, sm_port1;
753 u32 sm_fib_index0, sm_fib_index1;
760 /* Prefetch next iteration. */
761 if (PREDICT_TRUE (n_left_from >= 4))
763 vlib_buffer_t *p2, *p3;
768 vlib_prefetch_buffer_header (p2, LOAD);
769 vlib_prefetch_buffer_header (p3, LOAD);
771 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
772 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
775 vnet_buffer (b0)->snat.flags = 0;
776 vnet_buffer (b1)->snat.flags = 0;
778 ip0 = vlib_buffer_get_current (b0);
779 udp0 = ip4_next_header (ip0);
780 tcp0 = (tcp_header_t *) udp0;
781 icmp0 = (icmp46_header_t *) udp0;
783 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
784 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
787 if (PREDICT_FALSE (ip0->ttl == 1))
789 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
790 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
791 ICMP4_time_exceeded_ttl_exceeded_in_transit,
793 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
797 proto0 = ip_proto_to_nat_proto (ip0->protocol);
799 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
801 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
803 if (!sm->forwarding_enabled)
806 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
807 next0 = SNAT_OUT2IN_NEXT_DROP;
810 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
811 thread_index, sw_if_index0, 1);
816 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
818 next0 = icmp_out2in_slow_path
819 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
820 next0, now, thread_index, &s0);
821 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
822 thread_index, sw_if_index0, 1);
826 init_nat_k (&kv0, ip0->dst_address,
827 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
829 if (clib_bihash_search_8_8
830 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
832 /* Try to match static mapping by external address and port,
833 destination address and port in packet */
834 if (snat_static_mapping_match
835 (sm, ip0->dst_address,
836 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
837 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
838 0, &identity_nat0, 0))
841 * Send DHCP packets to the ipv4 stack, or we won't
842 * be able to use dhcp client on the outside interface
845 (proto0 == NAT_PROTOCOL_UDP
846 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
847 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
849 vnet_feature_next (&next0, b0);
853 if (!sm->forwarding_enabled)
855 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
856 next0 = SNAT_OUT2IN_NEXT_DROP;
861 if (PREDICT_FALSE (identity_nat0))
864 /* Create session initiated by host from external network */
865 s0 = create_session_for_static_mapping (sm, b0,
869 vnet_buffer (b0)->ip.
871 rx_fib_index0, proto0, node,
875 next0 = SNAT_OUT2IN_NEXT_DROP;
880 s0 = pool_elt_at_index (tsm->sessions, value0.value);
882 old_addr0 = ip0->dst_address.as_u32;
883 ip0->dst_address = s0->in2out.addr;
884 new_addr0 = ip0->dst_address.as_u32;
885 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
887 sum0 = ip0->checksum;
888 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
889 ip4_header_t, dst_address /* changed member */ );
890 ip0->checksum = ip_csum_fold (sum0);
892 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
894 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
896 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
897 new_port0 = udp0->dst_port = s0->in2out.port;
898 sum0 = tcp0->checksum;
899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
901 dst_address /* changed member */ );
903 sum0 = ip_csum_update (sum0, old_port0, new_port0,
904 ip4_header_t /* cheat */ ,
905 length /* changed member */ );
906 tcp0->checksum = ip_csum_fold (sum0);
908 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
909 thread_index, sw_if_index0, 1);
913 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
915 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
916 new_port0 = udp0->dst_port = s0->in2out.port;
917 if (PREDICT_FALSE (udp0->checksum))
919 sum0 = udp0->checksum;
920 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
923 ip_csum_update (sum0, old_port0, new_port0,
924 ip4_header_t /* cheat */ ,
925 length /* changed member */ );
926 udp0->checksum = ip_csum_fold (sum0);
929 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
930 thread_index, sw_if_index0, 1);
934 nat44_session_update_counters (s0, now,
935 vlib_buffer_length_in_chain (vm, b0),
937 /* Per-user LRU list maintenance */
938 nat44_session_update_lru (sm, s0, thread_index);
941 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
942 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
944 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
945 t->sw_if_index = sw_if_index0;
946 t->next_index = next0;
947 t->session_index = ~0;
950 s0 - sm->per_thread_data[thread_index].sessions;
953 if (next0 == SNAT_OUT2IN_NEXT_DROP)
955 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
956 thread_index, sw_if_index0, 1);
960 ip1 = vlib_buffer_get_current (b1);
961 udp1 = ip4_next_header (ip1);
962 tcp1 = (tcp_header_t *) udp1;
963 icmp1 = (icmp46_header_t *) udp1;
965 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
966 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
969 if (PREDICT_FALSE (ip1->ttl == 1))
971 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
972 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
973 ICMP4_time_exceeded_ttl_exceeded_in_transit,
975 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
979 proto1 = ip_proto_to_nat_proto (ip1->protocol);
981 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
983 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
985 if (!sm->forwarding_enabled)
988 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
989 next1 = SNAT_OUT2IN_NEXT_DROP;
992 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
993 thread_index, sw_if_index1, 1);
997 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
999 next1 = icmp_out2in_slow_path
1000 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1001 next1, now, thread_index, &s1);
1002 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1003 thread_index, sw_if_index1, 1);
1007 init_nat_k (&kv1, ip1->dst_address,
1008 vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1011 if (clib_bihash_search_8_8
1012 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1014 /* Try to match static mapping by external address and port,
1015 destination address and port in packet */
1016 if (snat_static_mapping_match
1017 (sm, ip1->dst_address,
1018 vnet_buffer (b1)->ip.reass.l4_dst_port, proto1,
1019 rx_fib_index1, &sm_addr1, &sm_port1, &sm_fib_index1, 1, 0,
1020 0, 0, 0, &identity_nat1, 0))
1023 * Send DHCP packets to the ipv4 stack, or we won't
1024 * be able to use dhcp client on the outside interface
1027 (proto1 == NAT_PROTOCOL_UDP
1028 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1029 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1031 vnet_feature_next (&next1, b1);
1035 if (!sm->forwarding_enabled)
1037 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1038 next1 = SNAT_OUT2IN_NEXT_DROP;
1043 if (PREDICT_FALSE (identity_nat1))
1046 /* Create session initiated by host from external network */
1048 create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1051 vnet_buffer (b1)->ip.
1053 rx_fib_index1, proto1, node,
1057 next1 = SNAT_OUT2IN_NEXT_DROP;
1063 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1066 old_addr1 = ip1->dst_address.as_u32;
1067 ip1->dst_address = s1->in2out.addr;
1068 new_addr1 = ip1->dst_address.as_u32;
1069 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1071 sum1 = ip1->checksum;
1072 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1073 ip4_header_t, dst_address /* changed member */ );
1074 ip1->checksum = ip_csum_fold (sum1);
1076 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1078 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1080 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1081 new_port1 = udp1->dst_port = s1->in2out.port;
1083 sum1 = tcp1->checksum;
1084 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1086 dst_address /* changed member */ );
1088 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1089 ip4_header_t /* cheat */ ,
1090 length /* changed member */ );
1091 tcp1->checksum = ip_csum_fold (sum1);
1093 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1094 thread_index, sw_if_index1, 1);
1098 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1100 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1101 new_port1 = udp1->dst_port = s1->in2out.port;
1102 if (PREDICT_FALSE (udp1->checksum))
1105 sum1 = udp1->checksum;
1107 ip_csum_update (sum1, old_addr1, new_addr1,
1109 dst_address /* changed member */ );
1111 ip_csum_update (sum1, old_port1, new_port1,
1112 ip4_header_t /* cheat */ ,
1113 length /* changed member */ );
1114 udp1->checksum = ip_csum_fold (sum1);
1117 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1118 thread_index, sw_if_index1, 1);
1122 nat44_session_update_counters (s1, now,
1123 vlib_buffer_length_in_chain (vm, b1),
1125 /* Per-user LRU list maintenance */
1126 nat44_session_update_lru (sm, s1, thread_index);
1129 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1130 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1132 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1133 t->sw_if_index = sw_if_index1;
1134 t->next_index = next1;
1135 t->session_index = ~0;
1138 s1 - sm->per_thread_data[thread_index].sessions;
1141 if (next1 == SNAT_OUT2IN_NEXT_DROP)
1143 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1144 thread_index, sw_if_index1, 1);
1153 while (n_left_from > 0)
1156 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1160 u32 new_addr0, old_addr0;
1161 u16 new_port0, old_port0;
1164 icmp46_header_t *icmp0;
1167 snat_session_t *s0 = 0;
1168 clib_bihash_kv_8_8_t kv0, value0;
1170 ip4_address_t sm_addr0;
1177 vnet_buffer (b0)->snat.flags = 0;
1179 ip0 = vlib_buffer_get_current (b0);
1180 udp0 = ip4_next_header (ip0);
1181 tcp0 = (tcp_header_t *) udp0;
1182 icmp0 = (icmp46_header_t *) udp0;
1184 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1185 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1188 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1190 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1192 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1194 if (!sm->forwarding_enabled)
1197 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1198 next0 = SNAT_OUT2IN_NEXT_DROP;
1201 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1202 thread_index, sw_if_index0, 1);
1206 if (PREDICT_FALSE (ip0->ttl == 1))
1208 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1209 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1210 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1212 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1216 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1218 next0 = icmp_out2in_slow_path
1219 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1220 next0, now, thread_index, &s0);
1221 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1222 thread_index, sw_if_index0, 1);
1226 init_nat_k (&kv0, ip0->dst_address,
1227 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1230 if (clib_bihash_search_8_8
1231 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1233 /* Try to match static mapping by external address and port,
1234 destination address and port in packet */
1235 if (snat_static_mapping_match
1236 (sm, ip0->dst_address,
1237 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1238 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
1239 0, &identity_nat0, 0))
1242 * Send DHCP packets to the ipv4 stack, or we won't
1243 * be able to use dhcp client on the outside interface
1246 (proto0 == NAT_PROTOCOL_UDP
1247 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1248 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1250 vnet_feature_next (&next0, b0);
1254 if (!sm->forwarding_enabled)
1256 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1257 next0 = SNAT_OUT2IN_NEXT_DROP;
1262 if (PREDICT_FALSE (identity_nat0))
1265 /* Create session initiated by host from external network */
1266 s0 = create_session_for_static_mapping (sm, b0,
1270 vnet_buffer (b0)->ip.
1272 rx_fib_index0, proto0, node,
1276 next0 = SNAT_OUT2IN_NEXT_DROP;
1282 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1285 old_addr0 = ip0->dst_address.as_u32;
1286 ip0->dst_address = s0->in2out.addr;
1287 new_addr0 = ip0->dst_address.as_u32;
1288 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1290 sum0 = ip0->checksum;
1291 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1292 ip4_header_t, dst_address /* changed member */ );
1293 ip0->checksum = ip_csum_fold (sum0);
1295 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1297 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1299 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1300 new_port0 = udp0->dst_port = s0->in2out.port;
1302 sum0 = tcp0->checksum;
1303 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1305 dst_address /* changed member */ );
1307 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1308 ip4_header_t /* cheat */ ,
1309 length /* changed member */ );
1310 tcp0->checksum = ip_csum_fold (sum0);
1312 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1313 thread_index, sw_if_index0, 1);
1317 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1319 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1320 new_port0 = udp0->dst_port = s0->in2out.port;
1321 if (PREDICT_FALSE (udp0->checksum))
1323 sum0 = udp0->checksum;
1324 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1327 ip_csum_update (sum0, old_port0, new_port0,
1328 ip4_header_t /* cheat */ ,
1329 length /* changed member */ );
1330 udp0->checksum = ip_csum_fold (sum0);
1333 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1334 thread_index, sw_if_index0, 1);
1338 nat44_session_update_counters (s0, now,
1339 vlib_buffer_length_in_chain (vm, b0),
1341 /* Per-user LRU list maintenance */
1342 nat44_session_update_lru (sm, s0, thread_index);
1345 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1346 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1348 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1349 t->sw_if_index = sw_if_index0;
1350 t->next_index = next0;
1351 t->session_index = ~0;
1354 s0 - sm->per_thread_data[thread_index].sessions;
1357 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1359 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1360 thread_index, sw_if_index0, 1);
1368 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1371 return frame->n_vectors;
1375 VLIB_REGISTER_NODE (snat_out2in_node) = {
1376 .name = "nat44-out2in",
1377 .vector_size = sizeof (u32),
1378 .format_trace = format_snat_out2in_trace,
1379 .type = VLIB_NODE_TYPE_INTERNAL,
1381 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1382 .error_strings = snat_out2in_error_strings,
1384 .runtime_data_bytes = sizeof (snat_runtime_t),
1386 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1388 /* edit / add dispositions here */
1390 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1391 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1392 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1397 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1398 vlib_node_runtime_t * node,
1399 vlib_frame_t * frame)
1401 u32 n_left_from, *from;
1402 snat_main_t *sm = &snat_main;
1404 from = vlib_frame_vector_args (frame);
1405 n_left_from = frame->n_vectors;
1407 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1408 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1409 vlib_get_buffers (vm, from, b, n_left_from);
1410 while (n_left_from > 0)
1413 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1417 u32 new_addr0, old_addr0;
1418 u16 new_port0, old_port0;
1421 icmp46_header_t *icmp0;
1424 ip4_address_t sm_addr0;
1431 ip0 = vlib_buffer_get_current (b0);
1432 udp0 = ip4_next_header (ip0);
1433 tcp0 = (tcp_header_t *) udp0;
1434 icmp0 = (icmp46_header_t *) udp0;
1436 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1437 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1439 vnet_feature_next (&next0, b0);
1441 if (PREDICT_FALSE (ip0->ttl == 1))
1443 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1444 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1445 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1447 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1451 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1453 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1456 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1458 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1459 rx_fib_index0, node, next0, ~0, 0, 0);
1463 if (snat_static_mapping_match
1464 (sm, ip0->dst_address, udp0->dst_port, rx_fib_index0, proto0,
1465 &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0, 0, 0, 0))
1467 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1471 new_addr0 = sm_addr0.as_u32;
1472 new_port0 = sm_port0;
1473 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1474 old_addr0 = ip0->dst_address.as_u32;
1475 ip0->dst_address.as_u32 = new_addr0;
1477 sum0 = ip0->checksum;
1478 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1479 ip4_header_t, dst_address /* changed member */ );
1480 ip0->checksum = ip_csum_fold (sum0);
1482 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1484 old_port0 = udp0->dst_port;
1485 udp0->dst_port = new_port0;
1487 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1489 sum0 = tcp0->checksum;
1490 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1492 dst_address /* changed member */ );
1493 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1494 ip4_header_t /* cheat */ ,
1495 length /* changed member */ );
1496 tcp0->checksum = ip_csum_fold (sum0);
1498 else if (udp0->checksum)
1500 sum0 = udp0->checksum;
1501 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1503 dst_address /* changed member */ );
1504 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1505 ip4_header_t /* cheat */ ,
1506 length /* changed member */ );
1507 udp0->checksum = ip_csum_fold (sum0);
1512 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1514 sum0 = tcp0->checksum;
1515 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1517 dst_address /* changed member */ );
1518 tcp0->checksum = ip_csum_fold (sum0);
1520 else if (udp0->checksum)
1522 sum0 = udp0->checksum;
1523 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1525 dst_address /* changed member */ );
1526 udp0->checksum = ip_csum_fold (sum0);
1532 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1533 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1535 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1536 t->sw_if_index = sw_if_index0;
1537 t->next_index = next0;
1540 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1542 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1543 vm->thread_index, sw_if_index0, 1);
1551 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1554 return frame->n_vectors;
1558 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1559 .name = "nat44-out2in-fast",
1560 .vector_size = sizeof (u32),
1561 .format_trace = format_snat_out2in_fast_trace,
1562 .type = VLIB_NODE_TYPE_INTERNAL,
1564 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1565 .error_strings = snat_out2in_error_strings,
1567 .runtime_data_bytes = sizeof (snat_runtime_t),
1569 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1571 /* edit / add dispositions here */
1573 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1574 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1575 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1581 * fd.io coding-style-patch-verification: ON
1584 * eval: (c-set-style "gnu")