2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/udp/udp.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/fib/ip4_fib.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
44 } snat_out2in_trace_t;
46 /* packet trace format function */
48 format_snat_out2in_trace (u8 * s, va_list * args)
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
56 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
57 t->sw_if_index, t->next_index, t->session_index);
62 format_snat_out2in_fast_trace (u8 * s, va_list * args)
64 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66 snat_out2in_trace_t *t = va_arg (*args, snat_out2in_trace_t *);
68 s = format (s, "NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
69 t->sw_if_index, t->next_index);
73 #define foreach_snat_out2in_error \
74 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
75 _(OUT2IN_PACKETS, "good out2in packets processed") \
76 _(OUT_OF_PORTS, "out of ports") \
77 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
78 _(NO_TRANSLATION, "no translation") \
79 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
80 _(DROP_FRAGMENT, "drop fragment") \
81 _(MAX_REASS, "maximum reassemblies exceeded") \
82 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
83 _(TCP_PACKETS, "TCP packets") \
84 _(UDP_PACKETS, "UDP packets") \
85 _(ICMP_PACKETS, "ICMP packets") \
86 _(OTHER_PACKETS, "other protocol packets") \
87 _(FRAGMENTS, "fragments") \
88 _(CACHED_FRAGMENTS, "cached fragments") \
89 _(PROCESSED_FRAGMENTS, "processed fragments")
93 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
94 foreach_snat_out2in_error
97 } snat_out2in_error_t;
99 static char *snat_out2in_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_snat_out2in_error
107 SNAT_OUT2IN_NEXT_DROP,
108 SNAT_OUT2IN_NEXT_LOOKUP,
109 SNAT_OUT2IN_NEXT_ICMP_ERROR,
111 } snat_out2in_next_t;
113 #ifndef CLIB_MARCH_VARIANT
115 nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
117 snat_main_t *sm = &snat_main;
118 nat44_is_idle_session_ctx_t *ctx = arg;
120 u64 sess_timeout_time;
121 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
123 clib_bihash_kv_8_8_t s_kv;
125 s = pool_elt_at_index (tsm->sessions, kv->value);
126 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
127 if (ctx->now >= sess_timeout_time)
129 s_kv.key = s->in2out.as_u64;
130 if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
131 nat_elog_warn ("out2in key del failed");
133 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
134 s->in2out.addr.as_u32,
135 s->out2in.addr.as_u32,
139 s->in2out.fib_index);
141 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
142 &s->in2out.addr, s->in2out.port,
143 &s->out2in.addr, s->out2in.port,
146 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
147 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
150 if (!snat_is_session_static (s))
151 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
154 nat44_delete_session (sm, s, ctx->thread_index);
163 * @brief Create session for static mapping.
165 * Create NAT session initiated by host from external network with static
168 * @param sm NAT main.
169 * @param b0 Vlib buffer.
170 * @param in2out In2out NAT44 session key.
171 * @param out2in Out2in NAT44 session key.
172 * @param node Vlib node.
174 * @returns SNAT session if successfully created otherwise 0.
176 static inline snat_session_t *
177 create_session_for_static_mapping (snat_main_t * sm,
179 snat_session_key_t in2out,
180 snat_session_key_t out2in,
181 vlib_node_runtime_t * node,
182 u32 thread_index, f64 now)
186 clib_bihash_kv_8_8_t kv0;
189 nat44_is_idle_session_ctx_t ctx0;
191 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
193 b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
194 nat_elog_notice ("maximum sessions exceeded");
198 ip0 = vlib_buffer_get_current (b0);
199 udp0 = ip4_next_header (ip0);
202 nat_user_get_or_create (sm, &in2out.addr, in2out.fib_index, thread_index);
205 nat_elog_warn ("create NAT user failed");
209 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
212 nat44_delete_user_with_no_session (sm, u, thread_index);
213 nat_elog_warn ("create NAT session failed");
217 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
218 s->ext_host_addr.as_u32 = ip0->src_address.as_u32;
219 s->ext_host_port = udp0->src_port;
220 user_session_increment (sm, u, 1 /* static */ );
223 s->in2out.protocol = out2in.protocol;
225 /* Add to translation hashes */
227 ctx0.thread_index = thread_index;
228 kv0.key = s->in2out.as_u64;
229 kv0.value = s - sm->per_thread_data[thread_index].sessions;
230 if (clib_bihash_add_or_overwrite_stale_8_8
231 (&sm->per_thread_data[thread_index].in2out, &kv0,
232 nat44_i2o_is_idle_session_cb, &ctx0))
233 nat_elog_notice ("in2out key add failed");
235 kv0.key = s->out2in.as_u64;
237 if (clib_bihash_add_or_overwrite_stale_8_8
238 (&sm->per_thread_data[thread_index].out2in, &kv0,
239 nat44_o2i_is_idle_session_cb, &ctx0))
240 nat_elog_notice ("out2in key add failed");
243 snat_ipfix_logging_nat44_ses_create (thread_index,
244 s->in2out.addr.as_u32,
245 s->out2in.addr.as_u32,
248 s->out2in.port, s->in2out.fib_index);
250 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
251 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
252 s->out2in.port, s->in2out.protocol);
254 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
255 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
256 &s->ext_host_nat_addr, s->ext_host_nat_port,
257 s->in2out.protocol, s->in2out.fib_index, s->flags,
263 #ifndef CLIB_MARCH_VARIANT
265 snat_out2in_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
266 snat_session_key_t * p_key0)
268 icmp46_header_t *icmp0;
269 snat_session_key_t key0;
270 icmp_echo_header_t *echo0, *inner_echo0 = 0;
271 ip4_header_t *inner_ip0;
273 icmp46_header_t *inner_icmp0;
275 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
276 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
278 if (!icmp_type_is_error_message
279 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
281 key0.protocol = SNAT_PROTOCOL_ICMP;
282 key0.addr = ip0->dst_address;
283 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be dst port?
287 inner_ip0 = (ip4_header_t *) (echo0 + 1);
288 l4_header = ip4_next_header (inner_ip0);
289 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
290 key0.addr = inner_ip0->src_address;
291 switch (key0.protocol)
293 case SNAT_PROTOCOL_ICMP:
294 inner_icmp0 = (icmp46_header_t *) l4_header;
295 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
296 key0.port = inner_echo0->identifier;
298 case SNAT_PROTOCOL_UDP:
299 case SNAT_PROTOCOL_TCP:
300 key0.port = ((tcp_udp_header_t *) l4_header)->src_port;
303 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
307 return -1; /* success */
311 * Get address and port values to be used for ICMP packet translation
312 * and create session if needed
314 * @param[in,out] sm NAT main
315 * @param[in,out] node NAT node runtime
316 * @param[in] thread_index thread index
317 * @param[in,out] b0 buffer containing packet to be translated
318 * @param[out] p_proto protocol used for matching
319 * @param[out] p_value address and port after NAT translation
320 * @param[out] p_dont_translate if packet should not be translated
321 * @param d optional parameter
322 * @param e optional parameter
325 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
326 u32 thread_index, vlib_buffer_t * b0,
327 ip4_header_t * ip0, u8 * p_proto,
328 snat_session_key_t * p_value,
329 u8 * p_dont_translate, void *d, void *e)
333 snat_session_key_t key0;
334 snat_session_key_t sm0;
335 snat_session_t *s0 = 0;
336 u8 dont_translate = 0;
337 clib_bihash_kv_8_8_t kv0, value0;
343 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
344 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
348 err = icmp_get_key (b0, ip0, &key0);
351 b0->error = node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
352 next0 = SNAT_OUT2IN_NEXT_DROP;
355 key0.fib_index = rx_fib_index0;
357 kv0.key = key0.as_u64;
359 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
362 /* Try to match static mapping by external address and port,
363 destination address and port in packet */
364 if (snat_static_mapping_match
365 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
367 if (!sm->forwarding_enabled)
369 /* Don't NAT packet aimed at the intfc address */
370 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
371 ip0->dst_address.as_u32)))
376 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
377 next0 = SNAT_OUT2IN_NEXT_DROP;
388 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
390 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
391 ICMP4_echo_request || !is_addr_only)))
393 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
394 next0 = SNAT_OUT2IN_NEXT_DROP;
398 if (PREDICT_FALSE (identity_nat))
403 /* Create session initiated by host from external network */
404 s0 = create_session_for_static_mapping (sm, b0, sm0, key0,
406 vlib_time_now (sm->vlib_main));
410 next0 = SNAT_OUT2IN_NEXT_DROP;
417 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
419 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
421 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
422 reass.icmp_type_or_tcp_flags)))
424 b0->error = node->errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
425 next0 = SNAT_OUT2IN_NEXT_DROP;
429 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
434 *p_proto = key0.protocol;
436 *p_value = s0->in2out;
437 *p_dont_translate = dont_translate;
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[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, u8 * p_proto,
462 snat_session_key_t * p_value,
463 u8 * p_dont_translate, void *d, void *e)
467 snat_session_key_t key0;
468 snat_session_key_t sm0;
469 u8 dont_translate = 0;
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, &key0);
480 b0->error = node->errors[err];
481 next0 = SNAT_OUT2IN_NEXT_DROP;
484 key0.fib_index = rx_fib_index0;
486 if (snat_static_mapping_match
487 (sm, key0, &sm0, 1, &is_addr_only, 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;
515 *p_proto = key0.protocol;
516 *p_dont_translate = dont_translate;
521 #ifndef CLIB_MARCH_VARIANT
523 icmp_out2in (snat_main_t * sm,
526 icmp46_header_t * icmp0,
529 vlib_node_runtime_t * node,
530 u32 next0, u32 thread_index, void *d, void *e)
532 snat_session_key_t sm0;
534 icmp_echo_header_t *echo0, *inner_echo0 = 0;
535 ip4_header_t *inner_ip0 = 0;
537 icmp46_header_t *inner_icmp0;
539 u32 new_addr0, old_addr0;
540 u16 old_id0, new_id0;
545 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
547 next0_tmp = sm->icmp_match_out2in_cb (sm, node, thread_index, b0, ip0,
548 &protocol, &sm0, &dont_translate, d,
552 if (next0 == SNAT_OUT2IN_NEXT_DROP || dont_translate)
555 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
557 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
559 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 = sm0.addr.as_u32;
572 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.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 = sm0.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 SNAT_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 SNAT_PROTOCOL_UDP:
638 case SNAT_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 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
671 next0, thread_index, p_s0, 0);
672 snat_session_t *s0 = *p_s0;
673 if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
676 nat44_session_update_counters (s0, now,
677 vlib_buffer_length_in_chain
678 (sm->vlib_main, 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 snat_session_key_t m_key;
693 u32 old_addr, new_addr;
696 m_key.addr = ip->dst_address;
700 kv.key = m_key.as_u64;
701 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
704 m = pool_elt_at_index (sm->static_mappings, value.value);
706 old_addr = ip->dst_address.as_u32;
707 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
709 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
710 ip->checksum = ip_csum_fold (sum);
712 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
716 VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
717 vlib_node_runtime_t * node,
718 vlib_frame_t * frame)
720 u32 n_left_from, *from, *to_next;
721 snat_out2in_next_t next_index;
722 u32 pkts_processed = 0;
723 snat_main_t *sm = &snat_main;
724 f64 now = vlib_time_now (vm);
725 u32 thread_index = vm->thread_index;
726 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
729 from = vlib_frame_vector_args (frame);
730 n_left_from = frame->n_vectors;
731 next_index = node->cached_next_index;
733 while (n_left_from > 0)
737 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
739 while (n_left_from >= 4 && n_left_to_next >= 2)
742 vlib_buffer_t *b0, *b1;
743 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
744 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
745 u32 sw_if_index0, sw_if_index1;
746 ip4_header_t *ip0, *ip1;
747 ip_csum_t sum0, sum1;
748 u32 new_addr0, old_addr0;
749 u16 new_port0, old_port0;
750 u32 new_addr1, old_addr1;
751 u16 new_port1, old_port1;
752 udp_header_t *udp0, *udp1;
753 tcp_header_t *tcp0, *tcp1;
754 icmp46_header_t *icmp0, *icmp1;
755 snat_session_key_t key0, key1, sm0, sm1;
756 u32 rx_fib_index0, rx_fib_index1;
758 snat_session_t *s0 = 0, *s1 = 0;
759 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
760 u8 identity_nat0, identity_nat1;
762 /* Prefetch next iteration. */
764 vlib_buffer_t *p2, *p3;
766 p2 = vlib_get_buffer (vm, from[2]);
767 p3 = vlib_get_buffer (vm, from[3]);
769 vlib_prefetch_buffer_header (p2, LOAD);
770 vlib_prefetch_buffer_header (p3, LOAD);
772 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
773 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
776 /* speculatively enqueue b0 and b1 to the current next frame */
777 to_next[0] = bi0 = from[0];
778 to_next[1] = bi1 = from[1];
784 b0 = vlib_get_buffer (vm, bi0);
785 b1 = vlib_get_buffer (vm, bi1);
787 vnet_buffer (b0)->snat.flags = 0;
788 vnet_buffer (b1)->snat.flags = 0;
790 ip0 = vlib_buffer_get_current (b0);
791 udp0 = ip4_next_header (ip0);
792 tcp0 = (tcp_header_t *) udp0;
793 icmp0 = (icmp46_header_t *) udp0;
795 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
796 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
799 if (PREDICT_FALSE (ip0->ttl == 1))
801 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
802 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
803 ICMP4_time_exceeded_ttl_exceeded_in_transit,
805 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
809 proto0 = ip_proto_to_snat_proto (ip0->protocol);
811 if (PREDICT_FALSE (proto0 == ~0))
813 if (nat_out2in_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
815 if (!sm->forwarding_enabled)
818 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
819 next0 = SNAT_OUT2IN_NEXT_DROP;
826 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
828 next0 = icmp_out2in_slow_path
829 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
830 next0, now, thread_index, &s0);
835 key0.addr = ip0->dst_address;
836 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
837 key0.protocol = proto0;
838 key0.fib_index = rx_fib_index0;
840 kv0.key = key0.as_u64;
842 if (clib_bihash_search_8_8
843 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
845 /* Try to match static mapping by external address and port,
846 destination address and port in packet */
847 if (snat_static_mapping_match
848 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
851 * Send DHCP packets to the ipv4 stack, or we won't
852 * be able to use dhcp client on the outside interface
855 (proto0 == SNAT_PROTOCOL_UDP
856 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
858 (UDP_DST_PORT_dhcp_to_client))))
860 vnet_feature_next (&next0, b0);
864 if (!sm->forwarding_enabled)
867 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
868 next0 = SNAT_OUT2IN_NEXT_DROP;
873 if (PREDICT_FALSE (identity_nat0))
876 /* Create session initiated by host from external network */
877 s0 = create_session_for_static_mapping (sm, b0, sm0, key0, node,
881 next0 = SNAT_OUT2IN_NEXT_DROP;
887 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
890 old_addr0 = ip0->dst_address.as_u32;
891 ip0->dst_address = s0->in2out.addr;
892 new_addr0 = ip0->dst_address.as_u32;
893 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
895 sum0 = ip0->checksum;
896 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
898 dst_address /* changed member */ );
899 ip0->checksum = ip_csum_fold (sum0);
901 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
903 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
905 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
906 new_port0 = udp0->dst_port = s0->in2out.port;
907 sum0 = tcp0->checksum;
908 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
910 dst_address /* changed member */ );
912 sum0 = ip_csum_update (sum0, old_port0, new_port0,
913 ip4_header_t /* cheat */ ,
914 length /* changed member */ );
915 tcp0->checksum = ip_csum_fold (sum0);
921 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
923 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
924 new_port0 = udp0->dst_port = s0->in2out.port;
925 if (PREDICT_FALSE (udp0->checksum))
927 sum0 = udp0->checksum;
928 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
931 ip_csum_update (sum0, old_port0, new_port0,
932 ip4_header_t /* cheat */ ,
933 length /* changed member */ );
934 udp0->checksum = ip_csum_fold (sum0);
941 nat44_session_update_counters (s0, now,
942 vlib_buffer_length_in_chain (vm, b0),
944 /* Per-user LRU list maintenance */
945 nat44_session_update_lru (sm, s0, thread_index);
948 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
949 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
951 snat_out2in_trace_t *t =
952 vlib_add_trace (vm, node, b0, sizeof (*t));
953 t->sw_if_index = sw_if_index0;
954 t->next_index = next0;
955 t->session_index = ~0;
958 s0 - sm->per_thread_data[thread_index].sessions;
961 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
964 ip1 = vlib_buffer_get_current (b1);
965 udp1 = ip4_next_header (ip1);
966 tcp1 = (tcp_header_t *) udp1;
967 icmp1 = (icmp46_header_t *) udp1;
969 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
970 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
973 if (PREDICT_FALSE (ip1->ttl == 1))
975 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
976 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
977 ICMP4_time_exceeded_ttl_exceeded_in_transit,
979 next1 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
983 proto1 = ip_proto_to_snat_proto (ip1->protocol);
985 if (PREDICT_FALSE (proto1 == ~0))
987 if (nat_out2in_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
989 if (!sm->forwarding_enabled)
992 node->errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
993 next1 = SNAT_OUT2IN_NEXT_DROP;
1000 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1002 next1 = icmp_out2in_slow_path
1003 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1004 next1, now, thread_index, &s1);
1009 key1.addr = ip1->dst_address;
1010 key1.port = vnet_buffer (b1)->ip.reass.l4_dst_port;
1011 key1.protocol = proto1;
1012 key1.fib_index = rx_fib_index1;
1014 kv1.key = key1.as_u64;
1016 if (clib_bihash_search_8_8
1017 (&sm->per_thread_data[thread_index].out2in, &kv1, &value1))
1019 /* Try to match static mapping by external address and port,
1020 destination address and port in packet */
1021 if (snat_static_mapping_match
1022 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
1025 * Send DHCP packets to the ipv4 stack, or we won't
1026 * be able to use dhcp client on the outside interface
1029 (proto1 == SNAT_PROTOCOL_UDP
1030 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1031 clib_host_to_net_u16
1032 (UDP_DST_PORT_dhcp_to_client))))
1034 vnet_feature_next (&next1, b1);
1038 if (!sm->forwarding_enabled)
1041 node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1042 next1 = SNAT_OUT2IN_NEXT_DROP;
1047 if (PREDICT_FALSE (identity_nat1))
1050 /* Create session initiated by host from external network */
1051 s1 = create_session_for_static_mapping (sm, b1, sm1, key1, node,
1055 next1 = SNAT_OUT2IN_NEXT_DROP;
1061 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1064 old_addr1 = ip1->dst_address.as_u32;
1065 ip1->dst_address = s1->in2out.addr;
1066 new_addr1 = ip1->dst_address.as_u32;
1067 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1069 sum1 = ip1->checksum;
1070 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1072 dst_address /* changed member */ );
1073 ip1->checksum = ip_csum_fold (sum1);
1075 if (PREDICT_TRUE (proto1 == SNAT_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);
1096 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1098 old_port1 = vnet_buffer (b1)->ip.reass.l4_dst_port;
1099 new_port1 = udp1->dst_port = s1->in2out.port;
1100 if (PREDICT_FALSE (udp1->checksum))
1103 sum1 = udp1->checksum;
1105 ip_csum_update (sum1, old_addr1, new_addr1,
1107 dst_address /* changed member */ );
1109 ip_csum_update (sum1, old_port1, new_port1,
1110 ip4_header_t /* cheat */ ,
1111 length /* changed member */ );
1112 udp1->checksum = ip_csum_fold (sum1);
1119 nat44_session_update_counters (s1, now,
1120 vlib_buffer_length_in_chain (vm, b1),
1122 /* Per-user LRU list maintenance */
1123 nat44_session_update_lru (sm, s1, thread_index);
1126 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1127 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1129 snat_out2in_trace_t *t =
1130 vlib_add_trace (vm, node, b1, sizeof (*t));
1131 t->sw_if_index = sw_if_index1;
1132 t->next_index = next1;
1133 t->session_index = ~0;
1136 s1 - sm->per_thread_data[thread_index].sessions;
1139 pkts_processed += next1 == SNAT_OUT2IN_NEXT_LOOKUP;
1141 /* verify speculative enqueues, maybe switch current next frame */
1142 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1143 to_next, n_left_to_next,
1144 bi0, bi1, next0, next1);
1147 while (n_left_from > 0 && n_left_to_next > 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;
1160 snat_session_key_t key0, sm0;
1163 snat_session_t *s0 = 0;
1164 clib_bihash_kv_8_8_t kv0, value0;
1167 /* speculatively enqueue b0 to the current next frame */
1173 n_left_to_next -= 1;
1175 b0 = vlib_get_buffer (vm, bi0);
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_snat_proto (ip0->protocol);
1190 if (PREDICT_FALSE (proto0 == ~0))
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;
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 == SNAT_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);
1224 key0.addr = ip0->dst_address;
1225 key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1226 key0.protocol = proto0;
1227 key0.fib_index = rx_fib_index0;
1229 kv0.key = key0.as_u64;
1231 if (clib_bihash_search_8_8
1232 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
1234 /* Try to match static mapping by external address and port,
1235 destination address and port in packet */
1236 if (snat_static_mapping_match
1237 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1240 * Send DHCP packets to the ipv4 stack, or we won't
1241 * be able to use dhcp client on the outside interface
1244 (proto0 == SNAT_PROTOCOL_UDP
1245 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1246 clib_host_to_net_u16
1247 (UDP_DST_PORT_dhcp_to_client))))
1249 vnet_feature_next (&next0, b0);
1253 if (!sm->forwarding_enabled)
1256 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, sm0, key0, node,
1270 next0 = SNAT_OUT2IN_NEXT_DROP;
1276 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1279 old_addr0 = ip0->dst_address.as_u32;
1280 ip0->dst_address = s0->in2out.addr;
1281 new_addr0 = ip0->dst_address.as_u32;
1282 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1284 sum0 = ip0->checksum;
1285 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1287 dst_address /* changed member */ );
1288 ip0->checksum = ip_csum_fold (sum0);
1290 if (PREDICT_TRUE (proto0 == SNAT_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);
1311 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1313 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1314 new_port0 = udp0->dst_port = s0->in2out.port;
1315 if (PREDICT_FALSE (udp0->checksum))
1317 sum0 = udp0->checksum;
1318 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1321 ip_csum_update (sum0, old_port0, new_port0,
1322 ip4_header_t /* cheat */ ,
1323 length /* changed member */ );
1324 udp0->checksum = ip_csum_fold (sum0);
1331 nat44_session_update_counters (s0, now,
1332 vlib_buffer_length_in_chain (vm, b0),
1334 /* Per-user LRU list maintenance */
1335 nat44_session_update_lru (sm, s0, thread_index);
1338 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1339 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1341 snat_out2in_trace_t *t =
1342 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 pkts_processed += next0 == SNAT_OUT2IN_NEXT_LOOKUP;
1353 /* verify speculative enqueue, maybe switch current next frame */
1354 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1355 to_next, n_left_to_next,
1359 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1362 vlib_node_increment_counter (vm, sm->out2in_node_index,
1363 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1365 vlib_node_increment_counter (vm, sm->out2in_node_index,
1366 SNAT_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
1367 vlib_node_increment_counter (vm, sm->out2in_node_index,
1368 SNAT_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
1369 vlib_node_increment_counter (vm, sm->out2in_node_index,
1370 SNAT_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
1371 vlib_node_increment_counter (vm, sm->out2in_node_index,
1372 SNAT_OUT2IN_ERROR_OTHER_PACKETS,
1374 vlib_node_increment_counter (vm, sm->out2in_node_index,
1375 SNAT_OUT2IN_ERROR_FRAGMENTS, fragments);
1377 return frame->n_vectors;
1381 VLIB_REGISTER_NODE (snat_out2in_node) = {
1382 .name = "nat44-out2in",
1383 .vector_size = sizeof (u32),
1384 .format_trace = format_snat_out2in_trace,
1385 .type = VLIB_NODE_TYPE_INTERNAL,
1387 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1388 .error_strings = snat_out2in_error_strings,
1390 .runtime_data_bytes = sizeof (snat_runtime_t),
1392 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1394 /* edit / add dispositions here */
1396 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1397 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1398 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1403 VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm,
1404 vlib_node_runtime_t * node,
1405 vlib_frame_t * frame)
1407 u32 n_left_from, *from, *to_next;
1408 snat_out2in_next_t next_index;
1409 u32 pkts_processed = 0;
1410 snat_main_t *sm = &snat_main;
1412 from = vlib_frame_vector_args (frame);
1413 n_left_from = frame->n_vectors;
1414 next_index = node->cached_next_index;
1416 while (n_left_from > 0)
1420 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1422 while (n_left_from > 0 && n_left_to_next > 0)
1426 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1430 u32 new_addr0, old_addr0;
1431 u16 new_port0, old_port0;
1434 icmp46_header_t *icmp0;
1435 snat_session_key_t key0, sm0;
1439 /* speculatively enqueue b0 to the current next frame */
1445 n_left_to_next -= 1;
1447 b0 = vlib_get_buffer (vm, bi0);
1449 ip0 = vlib_buffer_get_current (b0);
1450 udp0 = ip4_next_header (ip0);
1451 tcp0 = (tcp_header_t *) udp0;
1452 icmp0 = (icmp46_header_t *) udp0;
1454 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1456 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1458 vnet_feature_next (&next0, b0);
1460 if (PREDICT_FALSE (ip0->ttl == 1))
1462 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1463 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1464 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1466 next0 = SNAT_OUT2IN_NEXT_ICMP_ERROR;
1470 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1472 if (PREDICT_FALSE (proto0 == ~0))
1475 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1477 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1478 rx_fib_index0, node, next0, ~0, 0, 0);
1482 key0.addr = ip0->dst_address;
1483 key0.port = udp0->dst_port;
1484 key0.fib_index = rx_fib_index0;
1486 if (snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
1488 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1492 new_addr0 = sm0.addr.as_u32;
1493 new_port0 = sm0.port;
1494 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1495 old_addr0 = ip0->dst_address.as_u32;
1496 ip0->dst_address.as_u32 = new_addr0;
1498 sum0 = ip0->checksum;
1499 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1501 dst_address /* changed member */ );
1502 ip0->checksum = ip_csum_fold (sum0);
1504 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1506 old_port0 = udp0->dst_port;
1507 udp0->dst_port = new_port0;
1509 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1511 sum0 = tcp0->checksum;
1512 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1514 dst_address /* changed member */ );
1515 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1516 ip4_header_t /* cheat */ ,
1517 length /* 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 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1527 ip4_header_t /* cheat */ ,
1528 length /* changed member */ );
1529 udp0->checksum = ip_csum_fold (sum0);
1534 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1536 sum0 = tcp0->checksum;
1537 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1539 dst_address /* changed member */ );
1540 tcp0->checksum = ip_csum_fold (sum0);
1542 else if (udp0->checksum)
1544 sum0 = udp0->checksum;
1545 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1547 dst_address /* changed member */ );
1548 udp0->checksum = ip_csum_fold (sum0);
1554 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1555 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1557 snat_out2in_trace_t *t =
1558 vlib_add_trace (vm, node, b0, sizeof (*t));
1559 t->sw_if_index = sw_if_index0;
1560 t->next_index = next0;
1563 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1565 /* verify speculative enqueue, maybe switch current next frame */
1566 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1567 to_next, n_left_to_next,
1571 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1574 vlib_node_increment_counter (vm, sm->out2in_fast_node_index,
1575 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1577 return frame->n_vectors;
1581 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1582 .name = "nat44-out2in-fast",
1583 .vector_size = sizeof (u32),
1584 .format_trace = format_snat_out2in_fast_trace,
1585 .type = VLIB_NODE_TYPE_INTERNAL,
1587 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1588 .error_strings = snat_out2in_error_strings,
1590 .runtime_data_bytes = sizeof (snat_runtime_t),
1592 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1594 /* edit / add dispositions here */
1596 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1597 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1598 [SNAT_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1604 * fd.io coding-style-patch-verification: ON
1607 * eval: (c-set-style "gnu")