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/nat44/inlines.h>
31 #include <nat/lib/nat_syslog.h>
32 #include <nat/nat_ha.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 (&tsm->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_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, s - sm->per_thread_data[thread_index].sessions);
226 if (clib_bihash_add_or_overwrite_stale_8_8
227 (&sm->per_thread_data[thread_index].in2out, &kv0,
228 nat44_i2o_is_idle_session_cb, &ctx0))
229 nat_elog_notice ("in2out key add failed");
231 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
232 if (clib_bihash_add_or_overwrite_stale_8_8
233 (&sm->per_thread_data[thread_index].out2in, &kv0,
234 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,
320 ip4_header_t * ip0, ip4_address_t * addr,
321 u16 * port, u32 * fib_index,
322 nat_protocol_t * proto, void *d, void *e,
325 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
327 snat_session_t *s0 = 0;
328 clib_bihash_kv_8_8_t kv0, value0;
333 vlib_main_t *vm = vlib_get_main ();
336 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
337 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
341 err = icmp_get_key (b0, ip0, addr, port, proto);
344 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
345 next0 = SNAT_OUT2IN_NEXT_DROP;
349 ip4_address_t mapping_addr;
351 u32 mapping_fib_index;
353 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
354 if (clib_bihash_search_8_8 (&tsm->out2in, &kv0, &value0))
356 /* Try to match static mapping by external address and port,
357 destination address and port in packet */
358 if (snat_static_mapping_match
359 (sm, *addr, *port, *fib_index, *proto,
360 &mapping_addr, &mapping_port, &mapping_fib_index, 1, &is_addr_only,
361 0, 0, 0, &identity_nat, 0))
363 if (!sm->forwarding_enabled)
365 /* Don't NAT packet aimed at the intfc address */
366 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
367 ip0->dst_address.as_u32)))
372 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
373 next0 = SNAT_OUT2IN_NEXT_DROP;
384 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
386 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
387 ICMP4_echo_request || !is_addr_only)))
389 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
390 next0 = SNAT_OUT2IN_NEXT_DROP;
394 if (PREDICT_FALSE (identity_nat))
399 /* Create session initiated by host from external network */
401 create_session_for_static_mapping (sm, b0, mapping_addr, mapping_port,
402 mapping_fib_index, *addr, *port,
403 *fib_index, *proto, node,
404 thread_index, vlib_time_now (vm));
408 next0 = SNAT_OUT2IN_NEXT_DROP;
415 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
417 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
419 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
420 reass.icmp_type_or_tcp_flags)))
422 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
423 next0 = SNAT_OUT2IN_NEXT_DROP;
427 s0 = pool_elt_at_index (tsm->sessions, value0.value);
433 *addr = s0->in2out.addr;
434 *port = s0->in2out.port;
435 *fib_index = s0->in2out.fib_index;
438 *(snat_session_t **) d = s0;
443 #ifndef CLIB_MARCH_VARIANT
445 * Get address and port values to be used for ICMP packet translation
447 * @param[in] sm NAT main
448 * @param[in,out] node NAT node runtime
449 * @param[in] thread_index thread index
450 * @param[in,out] b0 buffer containing packet to be translated
451 * @param[in,out] ip0 ip header
452 * @param[out] p_proto protocol used for matching
453 * @param[out] p_value address and port after NAT translation
454 * @param[out] p_dont_translate if packet should not be translated
455 * @param d optional parameter
456 * @param e optional parameter
459 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
460 u32 thread_index, vlib_buffer_t * b0,
461 ip4_header_t * ip0, ip4_address_t * mapping_addr,
462 u16 * mapping_port, u32 * mapping_fib_index,
463 nat_protocol_t * proto, void *d, void *e,
475 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
476 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
478 err = icmp_get_key (b0, ip0, &addr, &port, proto);
481 b0->error = node->errors[err];
482 next0 = SNAT_OUT2IN_NEXT_DROP;
485 if (snat_static_mapping_match
486 (sm, addr, port, rx_fib_index0, *proto, mapping_addr, mapping_port,
487 mapping_fib_index, 1, &is_addr_only, 0, 0, 0, 0, 0))
489 /* Don't NAT packet aimed at the intfc address */
490 if (is_interface_addr (sm, node, sw_if_index0, ip0->dst_address.as_u32))
495 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
496 next0 = SNAT_OUT2IN_NEXT_DROP;
501 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
502 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
503 ICMP4_echo_request || !is_addr_only)
504 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
505 reass.icmp_type_or_tcp_flags)))
507 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
508 next0 = SNAT_OUT2IN_NEXT_DROP;
517 #ifndef CLIB_MARCH_VARIANT
519 icmp_out2in (snat_main_t * sm,
522 icmp46_header_t * icmp0,
525 vlib_node_runtime_t * node,
526 u32 next0, u32 thread_index, void *d, void *e)
528 icmp_echo_header_t *echo0, *inner_echo0 = 0;
529 ip4_header_t *inner_ip0 = 0;
531 icmp46_header_t *inner_icmp0;
533 u32 new_addr0, old_addr0;
534 u16 old_id0, new_id0;
538 vlib_main_t *vm = vlib_get_main ();
542 nat_protocol_t proto;
544 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
546 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
547 &addr, &port, &fib_index, &proto,
548 d, e, &dont_translate);
551 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
554 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
557 ip_incremental_checksum_buffer (vm, b0,
559 (u8 *) vlib_buffer_get_current (b0),
560 ntohs (ip0->length) -
561 ip4_header_bytes (ip0), 0);
562 checksum0 = ~ip_csum_fold (sum0);
563 if (checksum0 != 0 && checksum0 != 0xffff)
565 next0 = SNAT_OUT2IN_NEXT_DROP;
570 old_addr0 = ip0->dst_address.as_u32;
571 new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
572 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
574 sum0 = ip0->checksum;
575 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
576 dst_address /* changed member */ );
577 ip0->checksum = ip_csum_fold (sum0);
580 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
582 if (icmp0->checksum == 0)
583 icmp0->checksum = 0xffff;
585 if (!icmp_type_is_error_message (icmp0->type))
588 if (PREDICT_FALSE (new_id0 != echo0->identifier))
590 old_id0 = echo0->identifier;
592 echo0->identifier = new_id0;
594 sum0 = icmp0->checksum;
596 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
597 identifier /* changed member */ );
598 icmp0->checksum = ip_csum_fold (sum0);
603 inner_ip0 = (ip4_header_t *) (echo0 + 1);
604 l4_header = ip4_next_header (inner_ip0);
606 if (!ip4_header_checksum_is_valid (inner_ip0))
608 next0 = SNAT_OUT2IN_NEXT_DROP;
612 old_addr0 = inner_ip0->src_address.as_u32;
613 inner_ip0->src_address = addr;
614 new_addr0 = inner_ip0->src_address.as_u32;
616 sum0 = icmp0->checksum;
617 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
618 src_address /* changed member */ );
619 icmp0->checksum = ip_csum_fold (sum0);
623 case NAT_PROTOCOL_ICMP:
624 inner_icmp0 = (icmp46_header_t *) l4_header;
625 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
627 old_id0 = inner_echo0->identifier;
629 inner_echo0->identifier = new_id0;
631 sum0 = icmp0->checksum;
633 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
635 icmp0->checksum = ip_csum_fold (sum0);
637 case NAT_PROTOCOL_UDP:
638 case NAT_PROTOCOL_TCP:
639 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
641 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
643 sum0 = icmp0->checksum;
644 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
646 icmp0->checksum = ip_csum_fold (sum0);
660 icmp_out2in_slow_path (snat_main_t * sm,
663 icmp46_header_t * icmp0,
666 vlib_node_runtime_t * node,
668 u32 thread_index, snat_session_t ** p_s0)
670 vlib_main_t *vm = vlib_get_main ();
672 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
673 next0, thread_index, p_s0, 0);
674 snat_session_t *s0 = *p_s0;
675 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
678 nat44_session_update_counters (s0, now,
679 vlib_buffer_length_in_chain
680 (vm, b0), thread_index);
681 /* Per-user LRU list maintenance */
682 nat44_session_update_lru (sm, s0, thread_index);
688 nat_out2in_sm_unknown_proto (snat_main_t * sm,
690 ip4_header_t * ip, u32 rx_fib_index)
692 clib_bihash_kv_8_8_t kv, value;
693 snat_static_mapping_t *m;
694 u32 old_addr, new_addr;
697 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
698 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
701 m = pool_elt_at_index (sm->static_mappings, value.value);
703 old_addr = ip->dst_address.as_u32;
704 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
706 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
707 ip->checksum = ip_csum_fold (sum);
709 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
713 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
714 vlib_node_runtime_t * node,
715 vlib_frame_t * frame)
717 u32 n_left_from, *from;
718 snat_main_t *sm = &snat_main;
719 f64 now = vlib_time_now (vm);
720 u32 thread_index = vm->thread_index;
721 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
723 from = vlib_frame_vector_args (frame);
724 n_left_from = frame->n_vectors;
726 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
727 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
728 vlib_get_buffers (vm, from, b, n_left_from);
730 while (n_left_from >= 2)
732 vlib_buffer_t *b0, *b1;
733 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
734 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
735 u32 sw_if_index0, sw_if_index1;
736 ip4_header_t *ip0, *ip1;
737 ip_csum_t sum0, sum1;
738 u32 new_addr0, old_addr0;
739 u16 new_port0, old_port0;
740 u32 new_addr1, old_addr1;
741 u16 new_port1, old_port1;
742 udp_header_t *udp0, *udp1;
743 tcp_header_t *tcp0, *tcp1;
744 icmp46_header_t *icmp0, *icmp1;
745 u32 rx_fib_index0, rx_fib_index1;
747 snat_session_t *s0 = 0, *s1 = 0;
748 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
749 u8 identity_nat0, identity_nat1;
750 ip4_address_t sm_addr0, sm_addr1;
751 u16 sm_port0, sm_port1;
752 u32 sm_fib_index0, sm_fib_index1;
759 /* Prefetch next iteration. */
760 if (PREDICT_TRUE (n_left_from >= 4))
762 vlib_buffer_t *p2, *p3;
767 vlib_prefetch_buffer_header (p2, LOAD);
768 vlib_prefetch_buffer_header (p3, LOAD);
770 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
771 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
774 vnet_buffer (b0)->snat.flags = 0;
775 vnet_buffer (b1)->snat.flags = 0;
777 ip0 = vlib_buffer_get_current (b0);
778 udp0 = ip4_next_header (ip0);
779 tcp0 = (tcp_header_t *) udp0;
780 icmp0 = (icmp46_header_t *) udp0;
782 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
783 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
786 if (PREDICT_FALSE (ip0->ttl == 1))
788 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
789 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
790 ICMP4_time_exceeded_ttl_exceeded_in_transit,
792 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
796 proto0 = ip_proto_to_nat_proto (ip0->protocol);
798 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
800 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
802 if (!sm->forwarding_enabled)
805 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
806 next0 = SNAT_OUT2IN_NEXT_DROP;
809 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
810 thread_index, sw_if_index0, 1);
815 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
817 next0 = icmp_out2in_slow_path
818 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
819 next0, now, thread_index, &s0);
820 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
821 thread_index, sw_if_index0, 1);
825 init_nat_k (&kv0, ip0->dst_address,
826 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
828 if (clib_bihash_search_8_8
829 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
831 /* Try to match static mapping by external address and port,
832 destination address and port in packet */
833 if (snat_static_mapping_match
834 (sm, ip0->dst_address,
835 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
836 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
837 0, &identity_nat0, 0))
840 * Send DHCP packets to the ipv4 stack, or we won't
841 * be able to use dhcp client on the outside interface
844 (proto0 == NAT_PROTOCOL_UDP
845 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
846 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
848 vnet_feature_next (&next0, b0);
852 if (!sm->forwarding_enabled)
854 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
855 next0 = SNAT_OUT2IN_NEXT_DROP;
860 if (PREDICT_FALSE (identity_nat0))
863 /* Create session initiated by host from external network */
864 s0 = create_session_for_static_mapping (sm, b0,
868 vnet_buffer (b0)->ip.
870 rx_fib_index0, proto0, node,
874 next0 = SNAT_OUT2IN_NEXT_DROP;
879 s0 = pool_elt_at_index (tsm->sessions, value0.value);
881 old_addr0 = ip0->dst_address.as_u32;
882 ip0->dst_address = s0->in2out.addr;
883 new_addr0 = ip0->dst_address.as_u32;
884 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
886 sum0 = ip0->checksum;
887 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
888 ip4_header_t, dst_address /* changed member */ );
889 ip0->checksum = ip_csum_fold (sum0);
891 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
893 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
895 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
896 new_port0 = udp0->dst_port = s0->in2out.port;
897 sum0 = tcp0->checksum;
898 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
900 dst_address /* changed member */ );
902 sum0 = ip_csum_update (sum0, old_port0, new_port0,
903 ip4_header_t /* cheat */ ,
904 length /* changed member */ );
905 tcp0->checksum = ip_csum_fold (sum0);
907 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
908 thread_index, sw_if_index0, 1);
912 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
914 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
915 new_port0 = udp0->dst_port = s0->in2out.port;
916 if (PREDICT_FALSE (udp0->checksum))
918 sum0 = udp0->checksum;
919 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
922 ip_csum_update (sum0, old_port0, new_port0,
923 ip4_header_t /* cheat */ ,
924 length /* changed member */ );
925 udp0->checksum = ip_csum_fold (sum0);
928 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
929 thread_index, sw_if_index0, 1);
933 nat44_session_update_counters (s0, now,
934 vlib_buffer_length_in_chain (vm, b0),
936 /* Per-user LRU list maintenance */
937 nat44_session_update_lru (sm, s0, thread_index);
940 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
941 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
943 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
944 t->sw_if_index = sw_if_index0;
945 t->next_index = next0;
946 t->session_index = ~0;
949 s0 - sm->per_thread_data[thread_index].sessions;
952 if (next0 == SNAT_OUT2IN_NEXT_DROP)
954 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
955 thread_index, sw_if_index0, 1);
959 ip1 = vlib_buffer_get_current (b1);
960 udp1 = ip4_next_header (ip1);
961 tcp1 = (tcp_header_t *) udp1;
962 icmp1 = (icmp46_header_t *) udp1;
964 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
965 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
968 if (PREDICT_FALSE (ip1->ttl == 1))
970 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
971 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
972 ICMP4_time_exceeded_ttl_exceeded_in_transit,
974 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
978 proto1 = ip_proto_to_nat_proto (ip1->protocol);
980 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
982 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
984 if (!sm->forwarding_enabled)
987 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
988 next1 = SNAT_OUT2IN_NEXT_DROP;
991 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
992 thread_index, sw_if_index1, 1);
996 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
998 next1 = icmp_out2in_slow_path
999 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1000 next1, now, thread_index, &s1);
1001 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1002 thread_index, sw_if_index1, 1);
1006 init_nat_k (&kv1, ip1->dst_address,
1007 vnet_buffer (b1)->ip.reass.l4_dst_port, rx_fib_index1,
1010 if (clib_bihash_search_8_8
1011 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1013 /* Try to match static mapping by external address and port,
1014 destination address and port in packet */
1015 if (snat_static_mapping_match
1016 (sm, ip1->dst_address,
1017 vnet_buffer (b1)->ip.reass.l4_dst_port, proto1,
1018 rx_fib_index1, &sm_addr1, &sm_port1, &sm_fib_index1, 1, 0,
1019 0, 0, 0, &identity_nat1, 0))
1022 * Send DHCP packets to the ipv4 stack, or we won't
1023 * be able to use dhcp client on the outside interface
1026 (proto1 == NAT_PROTOCOL_UDP
1027 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1028 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1030 vnet_feature_next (&next1, b1);
1034 if (!sm->forwarding_enabled)
1036 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1037 next1 = SNAT_OUT2IN_NEXT_DROP;
1042 if (PREDICT_FALSE (identity_nat1))
1045 /* Create session initiated by host from external network */
1047 create_session_for_static_mapping (sm, b1, sm_addr1, sm_port1,
1050 vnet_buffer (b1)->ip.
1052 rx_fib_index1, proto1, node,
1056 next1 = SNAT_OUT2IN_NEXT_DROP;
1062 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1065 old_addr1 = ip1->dst_address.as_u32;
1066 ip1->dst_address = s1->in2out.addr;
1067 new_addr1 = ip1->dst_address.as_u32;
1068 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1070 sum1 = ip1->checksum;
1071 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1072 ip4_header_t, dst_address /* changed member */ );
1073 ip1->checksum = ip_csum_fold (sum1);
1075 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1077 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1079 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1080 new_port1 = udp1->dst_port = s1->in2out.port;
1082 sum1 = tcp1->checksum;
1083 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1085 dst_address /* changed member */ );
1087 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1088 ip4_header_t /* cheat */ ,
1089 length /* changed member */ );
1090 tcp1->checksum = ip_csum_fold (sum1);
1092 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1093 thread_index, sw_if_index1, 1);
1097 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1099 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1100 new_port1 = udp1->dst_port = s1->in2out.port;
1101 if (PREDICT_FALSE (udp1->checksum))
1104 sum1 = udp1->checksum;
1106 ip_csum_update (sum1, old_addr1, new_addr1,
1108 dst_address /* changed member */ );
1110 ip_csum_update (sum1, old_port1, new_port1,
1111 ip4_header_t /* cheat */ ,
1112 length /* changed member */ );
1113 udp1->checksum = ip_csum_fold (sum1);
1116 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1117 thread_index, sw_if_index1, 1);
1121 nat44_session_update_counters (s1, now,
1122 vlib_buffer_length_in_chain (vm, b1),
1124 /* Per-user LRU list maintenance */
1125 nat44_session_update_lru (sm, s1, thread_index);
1128 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1129 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1131 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1132 t->sw_if_index = sw_if_index1;
1133 t->next_index = next1;
1134 t->session_index = ~0;
1137 s1 - sm->per_thread_data[thread_index].sessions;
1140 if (next1 == SNAT_OUT2IN_NEXT_DROP)
1142 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1143 thread_index, sw_if_index1, 1);
1152 while (n_left_from > 0)
1155 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
1159 u32 new_addr0, old_addr0;
1160 u16 new_port0, old_port0;
1163 icmp46_header_t *icmp0;
1166 snat_session_t *s0 = 0;
1167 clib_bihash_kv_8_8_t kv0, value0;
1169 ip4_address_t sm_addr0;
1176 vnet_buffer (b0)->snat.flags = 0;
1178 ip0 = vlib_buffer_get_current (b0);
1179 udp0 = ip4_next_header (ip0);
1180 tcp0 = (tcp_header_t *) udp0;
1181 icmp0 = (icmp46_header_t *) udp0;
1183 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1184 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1187 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1189 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1191 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1193 if (!sm->forwarding_enabled)
1196 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1197 next0 = SNAT_OUT2IN_NEXT_DROP;
1200 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1201 thread_index, sw_if_index0, 1);
1205 if (PREDICT_FALSE (ip0->ttl == 1))
1207 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1208 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1209 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1211 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1215 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1217 next0 = icmp_out2in_slow_path
1218 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1219 next0, now, thread_index, &s0);
1220 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1221 thread_index, sw_if_index0, 1);
1225 init_nat_k (&kv0, ip0->dst_address,
1226 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1229 if (clib_bihash_search_8_8
1230 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1232 /* Try to match static mapping by external address and port,
1233 destination address and port in packet */
1234 if (snat_static_mapping_match
1235 (sm, ip0->dst_address,
1236 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1237 proto0, &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0,
1238 0, &identity_nat0, 0))
1241 * Send DHCP packets to the ipv4 stack, or we won't
1242 * be able to use dhcp client on the outside interface
1245 (proto0 == NAT_PROTOCOL_UDP
1246 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1247 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
1249 vnet_feature_next (&next0, b0);
1253 if (!sm->forwarding_enabled)
1255 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1256 next0 = SNAT_OUT2IN_NEXT_DROP;
1261 if (PREDICT_FALSE (identity_nat0))
1264 /* Create session initiated by host from external network */
1265 s0 = create_session_for_static_mapping (sm, b0,
1269 vnet_buffer (b0)->ip.
1271 rx_fib_index0, proto0, node,
1275 next0 = SNAT_OUT2IN_NEXT_DROP;
1281 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1284 old_addr0 = ip0->dst_address.as_u32;
1285 ip0->dst_address = s0->in2out.addr;
1286 new_addr0 = ip0->dst_address.as_u32;
1287 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1289 sum0 = ip0->checksum;
1290 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1291 ip4_header_t, dst_address /* changed member */ );
1292 ip0->checksum = ip_csum_fold (sum0);
1294 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1296 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1298 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1299 new_port0 = udp0->dst_port = s0->in2out.port;
1301 sum0 = tcp0->checksum;
1302 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1304 dst_address /* changed member */ );
1306 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1307 ip4_header_t /* cheat */ ,
1308 length /* changed member */ );
1309 tcp0->checksum = ip_csum_fold (sum0);
1311 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1312 thread_index, sw_if_index0, 1);
1316 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1318 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1319 new_port0 = udp0->dst_port = s0->in2out.port;
1320 if (PREDICT_FALSE (udp0->checksum))
1322 sum0 = udp0->checksum;
1323 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1326 ip_csum_update (sum0, old_port0, new_port0,
1327 ip4_header_t /* cheat */ ,
1328 length /* changed member */ );
1329 udp0->checksum = ip_csum_fold (sum0);
1332 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1333 thread_index, sw_if_index0, 1);
1337 nat44_session_update_counters (s0, now,
1338 vlib_buffer_length_in_chain (vm, b0),
1340 /* Per-user LRU list maintenance */
1341 nat44_session_update_lru (sm, s0, thread_index);
1344 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1345 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1347 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1348 t->sw_if_index = sw_if_index0;
1349 t->next_index = next0;
1350 t->session_index = ~0;
1353 s0 - sm->per_thread_data[thread_index].sessions;
1356 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1358 vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1359 thread_index, sw_if_index0, 1);
1367 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1370 return frame->n_vectors;
1374 VLIB_REGISTER_NODE (snat_out2in_node) = {
1375 .name = "nat44-out2in",
1376 .vector_size = sizeof (u32),
1377 .format_trace = format_snat_out2in_trace,
1378 .type = VLIB_NODE_TYPE_INTERNAL,
1380 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1381 .error_strings = snat_out2in_error_strings,
1383 .runtime_data_bytes = sizeof (snat_runtime_t),
1385 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1387 /* edit / add dispositions here */
1389 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1390 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1391 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1396 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1397 vlib_node_runtime_t * node,
1398 vlib_frame_t * frame)
1400 u32 n_left_from, *from;
1401 snat_main_t *sm = &snat_main;
1403 from = vlib_frame_vector_args (frame);
1404 n_left_from = frame->n_vectors;
1406 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1407 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1408 vlib_get_buffers (vm, from, b, n_left_from);
1409 while (n_left_from > 0)
1412 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1416 u32 new_addr0, old_addr0;
1417 u16 new_port0, old_port0;
1420 icmp46_header_t *icmp0;
1423 ip4_address_t sm_addr0;
1430 ip0 = vlib_buffer_get_current (b0);
1431 udp0 = ip4_next_header (ip0);
1432 tcp0 = (tcp_header_t *) udp0;
1433 icmp0 = (icmp46_header_t *) udp0;
1435 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1436 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1438 vnet_feature_next (&next0, b0);
1440 if (PREDICT_FALSE (ip0->ttl == 1))
1442 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1443 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1444 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1446 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1450 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1452 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1455 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1457 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1458 rx_fib_index0, node, next0, ~0, 0, 0);
1462 if (snat_static_mapping_match
1463 (sm, ip0->dst_address, udp0->dst_port, rx_fib_index0, proto0,
1464 &sm_addr0, &sm_port0, &sm_fib_index0, 1, 0, 0, 0, 0, 0, 0))
1466 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1470 new_addr0 = sm_addr0.as_u32;
1471 new_port0 = sm_port0;
1472 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm_fib_index0;
1473 old_addr0 = ip0->dst_address.as_u32;
1474 ip0->dst_address.as_u32 = new_addr0;
1476 sum0 = ip0->checksum;
1477 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1478 ip4_header_t, dst_address /* changed member */ );
1479 ip0->checksum = ip_csum_fold (sum0);
1481 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1483 old_port0 = udp0->dst_port;
1484 udp0->dst_port = new_port0;
1486 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1488 sum0 = tcp0->checksum;
1489 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1491 dst_address /* changed member */ );
1492 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1493 ip4_header_t /* cheat */ ,
1494 length /* changed member */ );
1495 tcp0->checksum = ip_csum_fold (sum0);
1497 else if (udp0->checksum)
1499 sum0 = udp0->checksum;
1500 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1502 dst_address /* changed member */ );
1503 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1504 ip4_header_t /* cheat */ ,
1505 length /* changed member */ );
1506 udp0->checksum = ip_csum_fold (sum0);
1511 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1513 sum0 = tcp0->checksum;
1514 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1516 dst_address /* changed member */ );
1517 tcp0->checksum = ip_csum_fold (sum0);
1519 else if (udp0->checksum)
1521 sum0 = udp0->checksum;
1522 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1524 dst_address /* changed member */ );
1525 udp0->checksum = ip_csum_fold (sum0);
1531 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1532 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1534 snat_out2in_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1535 t->sw_if_index = sw_if_index0;
1536 t->next_index = next0;
1539 if (next0 == SNAT_OUT2IN_NEXT_DROP)
1541 vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1542 vm->thread_index, sw_if_index0, 1);
1550 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1553 return frame->n_vectors;
1557 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1558 .name = "nat44-out2in-fast",
1559 .vector_size = sizeof (u32),
1560 .format_trace = format_snat_out2in_fast_trace,
1561 .type = VLIB_NODE_TYPE_INTERNAL,
1563 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1564 .error_strings = snat_out2in_error_strings,
1566 .runtime_data_bytes = sizeof (snat_runtime_t),
1568 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1570 /* edit / add dispositions here */
1572 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1573 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1574 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1580 * fd.io coding-style-patch-verification: ON
1583 * eval: (c-set-style "gnu")