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 inside to outside 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/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/udp/udp.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>
38 #include <nat/lib/nat_inlines.h>
47 } snat_in2out_trace_t;
49 /* packet trace format function */
51 format_snat_in2out_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
58 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
60 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
61 t->sw_if_index, t->next_index, t->session_index);
62 if (t->is_hairpinning)
64 s = format (s, ", with-hairpinning");
71 format_snat_in2out_fast_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
77 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
78 t->sw_if_index, t->next_index);
83 #define foreach_snat_in2out_error \
84 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
85 _(IN2OUT_PACKETS, "good in2out packets processed") \
86 _(OUT_OF_PORTS, "out of ports") \
87 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
88 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
89 _(NO_TRANSLATION, "no translation") \
90 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
91 _(DROP_FRAGMENT, "drop fragment") \
92 _(TCP_PACKETS, "TCP packets") \
93 _(UDP_PACKETS, "UDP packets") \
94 _(ICMP_PACKETS, "ICMP packets") \
95 _(OTHER_PACKETS, "other protocol packets") \
96 _(FRAGMENTS, "fragments") \
97 _(CACHED_FRAGMENTS, "cached fragments") \
98 _(PROCESSED_FRAGMENTS, "processed fragments")
102 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
103 foreach_snat_in2out_error
106 } snat_in2out_error_t;
108 static char *snat_in2out_error_strings[] = {
109 #define _(sym,string) string,
110 foreach_snat_in2out_error
116 SNAT_IN2OUT_NEXT_LOOKUP,
117 SNAT_IN2OUT_NEXT_DROP,
118 SNAT_IN2OUT_NEXT_ICMP_ERROR,
119 SNAT_IN2OUT_NEXT_SLOW_PATH,
121 } snat_in2out_next_t;
124 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
125 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
126 u32 rx_fib_index0, u32 thread_index)
128 udp_header_t *udp0 = ip4_next_header (ip0);
129 snat_session_key_t key0, sm0;
130 clib_bihash_kv_8_8_t kv0, value0;
132 key0.addr = ip0->dst_address;
133 key0.port = udp0->dst_port;
134 key0.protocol = proto0;
135 key0.fib_index = sm->outside_fib_index;
136 kv0.key = key0.as_u64;
138 /* NAT packet aimed at external address if */
139 /* has active sessions */
140 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
143 /* or is static mappings */
144 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
150 if (sm->forwarding_enabled)
153 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
158 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
159 u32 proto0, u16 src_port, u16 dst_port,
160 u32 thread_index, u32 sw_if_index)
162 snat_session_key_t key0;
163 clib_bihash_kv_8_8_t kv0, value0;
167 key0.addr = ip0->src_address;
168 key0.port = src_port;
169 key0.protocol = proto0;
170 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
171 kv0.key = key0.as_u64;
173 if (!clib_bihash_search_8_8
174 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
178 key0.addr = ip0->dst_address;
179 key0.port = dst_port;
180 key0.protocol = proto0;
181 kv0.key = key0.as_u64;
182 if (!clib_bihash_search_8_8
183 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
187 pool_foreach (i, sm->output_feature_interfaces,
189 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
199 #ifndef CLIB_MARCH_VARIANT
201 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
203 snat_main_t *sm = &snat_main;
204 nat44_is_idle_session_ctx_t *ctx = arg;
206 u64 sess_timeout_time;
207 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
209 clib_bihash_kv_8_8_t s_kv;
211 s = pool_elt_at_index (tsm->sessions, kv->value);
212 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
213 if (ctx->now >= sess_timeout_time)
215 s_kv.key = s->out2in.as_u64;
216 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
217 nat_elog_warn ("out2in key del failed");
219 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
220 s->in2out.addr.as_u32,
221 s->out2in.addr.as_u32,
225 s->in2out.fib_index);
227 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
228 &s->in2out.addr, s->in2out.port,
229 &s->out2in.addr, s->out2in.port,
232 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
233 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
236 if (!snat_is_session_static (s))
237 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
240 nat44_delete_session (sm, s, ctx->thread_index);
249 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
252 snat_session_key_t * key0,
253 snat_session_t ** sessionp,
254 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
257 snat_session_t *s = 0;
258 clib_bihash_kv_8_8_t kv0;
259 snat_session_key_t key1;
261 nat_outside_fib_t *outside_fib;
262 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
265 .fp_proto = FIB_PROTOCOL_IP4,
268 .ip4.as_u32 = ip0->dst_address.as_u32,
271 nat44_is_idle_session_ctx_t ctx0;
273 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
275 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
276 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
277 nat_elog_notice ("maximum sessions exceeded");
278 return SNAT_IN2OUT_NEXT_DROP;
281 key1.protocol = key0->protocol;
283 /* First try to match static mapping by local address and port */
284 if (snat_static_mapping_match
285 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
287 /* Try to create dynamic translation */
288 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
292 [thread_index].snat_thread_index))
294 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
295 return SNAT_IN2OUT_NEXT_DROP;
300 if (PREDICT_FALSE (identity_nat))
309 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
313 nat_elog_warn ("create NAT user failed");
314 return SNAT_IN2OUT_NEXT_DROP;
317 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
320 nat44_delete_user_with_no_session (sm, u, thread_index);
321 nat_elog_warn ("create NAT session failed");
322 return SNAT_IN2OUT_NEXT_DROP;
326 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
327 user_session_increment (sm, u, is_sm);
330 s->out2in.protocol = key0->protocol;
331 s->out2in.fib_index = sm->outside_fib_index;
332 switch (vec_len (sm->outside_fibs))
335 s->out2in.fib_index = sm->outside_fib_index;
338 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
342 vec_foreach (outside_fib, sm->outside_fibs)
344 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
345 if (FIB_NODE_INDEX_INVALID != fei)
347 if (fib_entry_get_resolving_interface (fei) != ~0)
349 s->out2in.fib_index = outside_fib->fib_index;
357 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
358 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
361 /* Add to translation hashes */
363 ctx0.thread_index = thread_index;
364 kv0.key = s->in2out.as_u64;
365 kv0.value = s - sm->per_thread_data[thread_index].sessions;
366 if (clib_bihash_add_or_overwrite_stale_8_8
367 (&sm->per_thread_data[thread_index].in2out, &kv0,
368 nat44_i2o_is_idle_session_cb, &ctx0))
369 nat_elog_notice ("in2out key add failed");
371 kv0.key = s->out2in.as_u64;
372 kv0.value = s - sm->per_thread_data[thread_index].sessions;
374 if (clib_bihash_add_or_overwrite_stale_8_8
375 (&sm->per_thread_data[thread_index].out2in, &kv0,
376 nat44_o2i_is_idle_session_cb, &ctx0))
377 nat_elog_notice ("out2in key add failed");
380 snat_ipfix_logging_nat44_ses_create (thread_index,
381 s->in2out.addr.as_u32,
382 s->out2in.addr.as_u32,
385 s->out2in.port, s->in2out.fib_index);
387 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
388 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
389 s->out2in.port, s->in2out.protocol);
391 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
392 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
393 &s->ext_host_nat_addr, s->ext_host_nat_port,
394 s->in2out.protocol, s->in2out.fib_index, s->flags,
400 #ifndef CLIB_MARCH_VARIANT
402 snat_in2out_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
403 snat_session_key_t * p_key0)
405 icmp46_header_t *icmp0;
406 snat_session_key_t key0;
407 icmp_echo_header_t *echo0, *inner_echo0 = 0;
408 ip4_header_t *inner_ip0 = 0;
410 icmp46_header_t *inner_icmp0;
412 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
413 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
415 if (!icmp_type_is_error_message
416 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
418 key0.protocol = NAT_PROTOCOL_ICMP;
419 key0.addr = ip0->src_address;
420 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO fixme should this be dst port?
424 inner_ip0 = (ip4_header_t *) (echo0 + 1);
425 l4_header = ip4_next_header (inner_ip0);
426 key0.protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
427 key0.addr = inner_ip0->dst_address;
428 switch (key0.protocol)
430 case NAT_PROTOCOL_ICMP:
431 inner_icmp0 = (icmp46_header_t *) l4_header;
432 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
433 key0.port = inner_echo0->identifier;
435 case NAT_PROTOCOL_UDP:
436 case NAT_PROTOCOL_TCP:
437 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
440 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
444 return -1; /* success */
448 * Get address and port values to be used for ICMP packet translation
449 * and create session if needed
451 * @param[in,out] sm NAT main
452 * @param[in,out] node NAT node runtime
453 * @param[in] thread_index thread index
454 * @param[in,out] b0 buffer containing packet to be translated
455 * @param[in,out] ip0 ip header
456 * @param[out] p_proto protocol used for matching
457 * @param[out] p_value address and port after NAT translation
458 * @param[out] p_dont_translate if packet should not be translated
459 * @param d optional parameter
460 * @param e optional parameter
463 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
464 u32 thread_index, vlib_buffer_t * b0,
465 ip4_header_t * ip0, u8 * p_proto,
466 snat_session_key_t * p_value,
467 u8 * p_dont_translate, void *d, void *e)
469 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
472 snat_session_key_t key0;
473 snat_session_t *s0 = 0;
474 u8 dont_translate = 0;
475 clib_bihash_kv_8_8_t kv0, value0;
478 vlib_main_t *vm = vlib_get_main ();
480 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
481 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
483 err = icmp_get_key (b0, ip0, &key0);
486 b0->error = node->errors[err];
487 next0 = SNAT_IN2OUT_NEXT_DROP;
490 key0.fib_index = rx_fib_index0;
492 kv0.key = key0.as_u64;
494 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
496 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
498 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
511 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
512 ip0, NAT_PROTOCOL_ICMP,
522 (icmp_type_is_error_message
523 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
525 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
526 next0 = SNAT_IN2OUT_NEXT_DROP;
530 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
531 thread_index, vlib_time_now (vm));
533 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
545 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
547 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
549 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
550 reass.icmp_type_or_tcp_flags)))
552 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
553 next0 = SNAT_IN2OUT_NEXT_DROP;
557 s0 = pool_elt_at_index (tsm->sessions, value0.value);
561 *p_proto = key0.protocol;
563 *p_value = s0->out2in;
564 *p_dont_translate = dont_translate;
566 *(snat_session_t **) d = s0;
571 #ifndef CLIB_MARCH_VARIANT
573 * Get address and port values to be used for ICMP packet translation
575 * @param[in] sm NAT main
576 * @param[in,out] node NAT node runtime
577 * @param[in] thread_index thread index
578 * @param[in,out] b0 buffer containing packet to be translated
579 * @param[in,out] ip0 ip header
580 * @param[out] p_proto protocol used for matching
581 * @param[out] p_value address and port after NAT translation
582 * @param[out] p_dont_translate if packet should not be translated
583 * @param d optional parameter
584 * @param e optional parameter
587 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
588 u32 thread_index, vlib_buffer_t * b0,
589 ip4_header_t * ip0, u8 * p_proto,
590 snat_session_key_t * p_value,
591 u8 * p_dont_translate, void *d, void *e)
595 snat_session_key_t key0;
596 snat_session_key_t sm0;
597 u8 dont_translate = 0;
602 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
603 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
605 err = icmp_get_key (b0, ip0, &key0);
608 b0->error = node->errors[err];
609 next0 = SNAT_IN2OUT_NEXT_DROP;
612 key0.fib_index = rx_fib_index0;
614 if (snat_static_mapping_match
615 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
617 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
625 if (icmp_type_is_error_message
626 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
628 next0 = SNAT_IN2OUT_NEXT_DROP;
632 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
633 next0 = SNAT_IN2OUT_NEXT_DROP;
638 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
639 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
640 ICMP4_echo_reply || !is_addr_only)
641 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
642 reass.icmp_type_or_tcp_flags)))
644 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
645 next0 = SNAT_IN2OUT_NEXT_DROP;
652 *p_proto = key0.protocol;
653 *p_dont_translate = dont_translate;
658 #ifndef CLIB_MARCH_VARIANT
660 icmp_in2out (snat_main_t * sm,
663 icmp46_header_t * icmp0,
666 vlib_node_runtime_t * node,
667 u32 next0, u32 thread_index, void *d, void *e)
669 vlib_main_t *vm = vlib_get_main ();
670 snat_session_key_t sm0;
672 icmp_echo_header_t *echo0, *inner_echo0 = 0;
673 ip4_header_t *inner_ip0;
675 icmp46_header_t *inner_icmp0;
677 u32 new_addr0, old_addr0;
678 u16 old_id0, new_id0;
679 u16 old_checksum0, new_checksum0;
684 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
686 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
687 &protocol, &sm0, &dont_translate, d,
691 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
694 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
697 ip_incremental_checksum_buffer (vm, b0,
699 (u8 *) vlib_buffer_get_current (b0),
700 ntohs (ip0->length) -
701 ip4_header_bytes (ip0), 0);
702 checksum0 = ~ip_csum_fold (sum0);
703 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
705 next0 = SNAT_IN2OUT_NEXT_DROP;
710 old_addr0 = ip0->src_address.as_u32;
711 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
713 sum0 = ip0->checksum;
714 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
715 src_address /* changed member */ );
716 ip0->checksum = ip_csum_fold (sum0);
718 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
720 if (icmp0->checksum == 0)
721 icmp0->checksum = 0xffff;
723 if (!icmp_type_is_error_message (icmp0->type))
726 if (PREDICT_FALSE (new_id0 != echo0->identifier))
728 old_id0 = echo0->identifier;
730 echo0->identifier = new_id0;
732 sum0 = icmp0->checksum;
734 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
736 icmp0->checksum = ip_csum_fold (sum0);
741 inner_ip0 = (ip4_header_t *) (echo0 + 1);
742 l4_header = ip4_next_header (inner_ip0);
744 if (!ip4_header_checksum_is_valid (inner_ip0))
746 next0 = SNAT_IN2OUT_NEXT_DROP;
750 /* update inner destination IP address */
751 old_addr0 = inner_ip0->dst_address.as_u32;
752 inner_ip0->dst_address = sm0.addr;
753 new_addr0 = inner_ip0->dst_address.as_u32;
754 sum0 = icmp0->checksum;
755 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
756 dst_address /* changed member */ );
757 icmp0->checksum = ip_csum_fold (sum0);
759 /* update inner IP header checksum */
760 old_checksum0 = inner_ip0->checksum;
761 sum0 = inner_ip0->checksum;
762 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
763 dst_address /* changed member */ );
764 inner_ip0->checksum = ip_csum_fold (sum0);
765 new_checksum0 = inner_ip0->checksum;
766 sum0 = icmp0->checksum;
768 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
770 icmp0->checksum = ip_csum_fold (sum0);
774 case NAT_PROTOCOL_ICMP:
775 inner_icmp0 = (icmp46_header_t *) l4_header;
776 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
778 old_id0 = inner_echo0->identifier;
780 inner_echo0->identifier = new_id0;
782 sum0 = icmp0->checksum;
784 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
786 icmp0->checksum = ip_csum_fold (sum0);
788 case NAT_PROTOCOL_UDP:
789 case NAT_PROTOCOL_TCP:
790 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
792 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
794 sum0 = icmp0->checksum;
795 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
797 icmp0->checksum = ip_csum_fold (sum0);
805 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
807 if (sm->deterministic ||
808 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
809 sm->endpoint_dependent))
810 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
819 icmp_in2out_slow_path (snat_main_t * sm,
822 icmp46_header_t * icmp0,
825 vlib_node_runtime_t * node,
827 f64 now, u32 thread_index, snat_session_t ** p_s0)
829 vlib_main_t *vm = vlib_get_main ();
831 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
832 next0, thread_index, p_s0, 0);
833 snat_session_t *s0 = *p_s0;
834 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
837 nat44_session_update_counters (s0, now,
838 vlib_buffer_length_in_chain
839 (vm, b0), thread_index);
840 /* Per-user LRU list maintenance */
841 nat44_session_update_lru (sm, s0, thread_index);
847 nat_in2out_sm_unknown_proto (snat_main_t * sm,
849 ip4_header_t * ip, u32 rx_fib_index)
851 clib_bihash_kv_8_8_t kv, value;
852 snat_static_mapping_t *m;
853 snat_session_key_t m_key;
854 u32 old_addr, new_addr;
857 m_key.addr = ip->src_address;
860 m_key.fib_index = rx_fib_index;
861 kv.key = m_key.as_u64;
862 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
865 m = pool_elt_at_index (sm->static_mappings, value.value);
867 old_addr = ip->src_address.as_u32;
868 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
870 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
871 ip->checksum = ip_csum_fold (sum);
875 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
877 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
878 nat_hairpinning_sm_unknown_proto (sm, b, ip);
885 snat_in2out_node_fn_inline (vlib_main_t * vm,
886 vlib_node_runtime_t * node,
887 vlib_frame_t * frame, int is_slow_path,
888 int is_output_feature)
890 u32 n_left_from, *from, *to_next;
891 snat_in2out_next_t next_index;
892 u32 pkts_processed = 0;
893 snat_main_t *sm = &snat_main;
894 f64 now = vlib_time_now (vm);
895 u32 stats_node_index;
896 u32 thread_index = vm->thread_index;
897 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
900 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
901 sm->in2out_node_index;
903 from = vlib_frame_vector_args (frame);
904 n_left_from = frame->n_vectors;
905 next_index = node->cached_next_index;
907 while (n_left_from > 0)
911 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
913 while (n_left_from >= 4 && n_left_to_next >= 2)
916 vlib_buffer_t *b0, *b1;
918 u32 sw_if_index0, sw_if_index1;
919 ip4_header_t *ip0, *ip1;
920 ip_csum_t sum0, sum1;
921 u32 new_addr0, old_addr0, new_addr1, old_addr1;
922 u16 old_port0, new_port0, old_port1, new_port1;
923 udp_header_t *udp0, *udp1;
924 tcp_header_t *tcp0, *tcp1;
925 icmp46_header_t *icmp0, *icmp1;
926 snat_session_key_t key0, key1;
927 u32 rx_fib_index0, rx_fib_index1;
929 snat_session_t *s0 = 0, *s1 = 0;
930 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
931 u32 iph_offset0 = 0, iph_offset1 = 0;
933 /* Prefetch next iteration. */
935 vlib_buffer_t *p2, *p3;
937 p2 = vlib_get_buffer (vm, from[2]);
938 p3 = vlib_get_buffer (vm, from[3]);
940 vlib_prefetch_buffer_header (p2, LOAD);
941 vlib_prefetch_buffer_header (p3, LOAD);
943 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
944 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
947 /* speculatively enqueue b0 and b1 to the current next frame */
948 to_next[0] = bi0 = from[0];
949 to_next[1] = bi1 = from[1];
955 b0 = vlib_get_buffer (vm, bi0);
956 b1 = vlib_get_buffer (vm, bi1);
958 if (is_output_feature)
959 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
961 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
964 udp0 = ip4_next_header (ip0);
965 tcp0 = (tcp_header_t *) udp0;
966 icmp0 = (icmp46_header_t *) udp0;
968 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
969 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
972 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
974 if (PREDICT_FALSE (ip0->ttl == 1))
976 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
977 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
978 ICMP4_time_exceeded_ttl_exceeded_in_transit,
980 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
984 proto0 = ip_proto_to_nat_proto (ip0->protocol);
986 /* Next configured feature, probably ip4-lookup */
989 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
991 if (nat_in2out_sm_unknown_proto
992 (sm, b0, ip0, rx_fib_index0))
994 next0 = SNAT_IN2OUT_NEXT_DROP;
996 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1002 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1004 next0 = icmp_in2out_slow_path
1005 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1006 node, next0, now, thread_index, &s0);
1013 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1015 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1019 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1021 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1026 key0.addr = ip0->src_address;
1027 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1028 key0.protocol = proto0;
1029 key0.fib_index = rx_fib_index0;
1031 kv0.key = key0.as_u64;
1034 (clib_bihash_search_8_8
1035 (&sm->per_thread_data[thread_index].in2out, &kv0,
1040 if (is_output_feature)
1043 (nat_not_translate_output_feature
1045 vnet_buffer (b0)->ip.reass.l4_src_port,
1046 vnet_buffer (b0)->ip.reass.l4_dst_port,
1047 thread_index, sw_if_index0)))
1051 * Send DHCP packets to the ipv4 stack, or we won't
1052 * be able to use dhcp client on the outside interface
1055 (proto0 == NAT_PROTOCOL_UDP
1056 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1057 clib_host_to_net_u16
1058 (UDP_DST_PORT_dhcp_to_server))
1059 && ip0->dst_address.as_u32 == 0xffffffff))
1066 (sm, node, sw_if_index0, ip0, proto0,
1067 rx_fib_index0, thread_index)))
1071 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1072 &s0, node, next0, thread_index, now);
1073 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1076 if (PREDICT_FALSE (!s0))
1081 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1087 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1090 b0->flags |= VNET_BUFFER_F_IS_NATED;
1092 old_addr0 = ip0->src_address.as_u32;
1093 ip0->src_address = s0->out2in.addr;
1094 new_addr0 = ip0->src_address.as_u32;
1095 if (!is_output_feature)
1096 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1098 sum0 = ip0->checksum;
1099 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1101 src_address /* changed member */ );
1102 ip0->checksum = ip_csum_fold (sum0);
1105 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1107 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1109 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1110 new_port0 = udp0->src_port = s0->out2in.port;
1111 sum0 = tcp0->checksum;
1112 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1114 dst_address /* changed member */ );
1115 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1116 ip4_header_t /* cheat */ ,
1117 length /* changed member */ );
1118 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1119 tcp0->checksum = ip_csum_fold (sum0);
1125 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1127 udp0->src_port = s0->out2in.port;
1128 if (PREDICT_FALSE (udp0->checksum))
1130 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1131 new_port0 = udp0->src_port;
1132 sum0 = udp0->checksum;
1133 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1136 ip_csum_update (sum0, old_port0, new_port0,
1137 ip4_header_t /* cheat */ ,
1138 length /* changed member */ );
1139 udp0->checksum = ip_csum_fold (sum0);
1146 nat44_session_update_counters (s0, now,
1147 vlib_buffer_length_in_chain (vm, b0),
1149 /* Per-user LRU list maintenance */
1150 nat44_session_update_lru (sm, s0, thread_index);
1153 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1154 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1156 snat_in2out_trace_t *t =
1157 vlib_add_trace (vm, node, b0, sizeof (*t));
1158 t->is_slow_path = is_slow_path;
1159 t->sw_if_index = sw_if_index0;
1160 t->next_index = next0;
1161 t->session_index = ~0;
1164 s0 - sm->per_thread_data[thread_index].sessions;
1167 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1169 if (is_output_feature)
1170 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1172 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1175 udp1 = ip4_next_header (ip1);
1176 tcp1 = (tcp_header_t *) udp1;
1177 icmp1 = (icmp46_header_t *) udp1;
1179 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1180 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1183 if (PREDICT_FALSE (ip1->ttl == 1))
1185 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1186 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1187 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1189 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1193 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1195 /* Next configured feature, probably ip4-lookup */
1198 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1200 if (nat_in2out_sm_unknown_proto
1201 (sm, b1, ip1, rx_fib_index1))
1203 next1 = SNAT_IN2OUT_NEXT_DROP;
1205 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1211 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1213 next1 = icmp_in2out_slow_path
1214 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1215 next1, now, thread_index, &s1);
1222 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1224 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1228 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1230 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1235 key1.addr = ip1->src_address;
1236 key1.port = vnet_buffer (b1)->ip.reass.l4_src_port;
1237 key1.protocol = proto1;
1238 key1.fib_index = rx_fib_index1;
1240 kv1.key = key1.as_u64;
1243 (clib_bihash_search_8_8
1244 (&sm->per_thread_data[thread_index].in2out, &kv1,
1249 if (is_output_feature)
1252 (nat_not_translate_output_feature
1254 vnet_buffer (b1)->ip.reass.l4_src_port,
1255 vnet_buffer (b1)->ip.reass.l4_dst_port,
1256 thread_index, sw_if_index1)))
1260 * Send DHCP packets to the ipv4 stack, or we won't
1261 * be able to use dhcp client on the outside interface
1264 (proto1 == NAT_PROTOCOL_UDP
1265 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1266 clib_host_to_net_u16
1267 (UDP_DST_PORT_dhcp_to_server))
1268 && ip1->dst_address.as_u32 == 0xffffffff))
1275 (sm, node, sw_if_index1, ip1, proto1,
1276 rx_fib_index1, thread_index)))
1280 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1281 &s1, node, next1, thread_index, now);
1282 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1285 if (PREDICT_FALSE (!s1))
1290 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1296 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1299 b1->flags |= VNET_BUFFER_F_IS_NATED;
1301 old_addr1 = ip1->src_address.as_u32;
1302 ip1->src_address = s1->out2in.addr;
1303 new_addr1 = ip1->src_address.as_u32;
1304 if (!is_output_feature)
1305 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1307 sum1 = ip1->checksum;
1308 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1310 src_address /* changed member */ );
1311 ip1->checksum = ip_csum_fold (sum1);
1313 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1315 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1317 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1318 new_port1 = udp1->src_port = s1->out2in.port;
1319 sum1 = tcp1->checksum;
1320 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1322 dst_address /* changed member */ );
1323 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1324 ip4_header_t /* cheat */ ,
1325 length /* changed member */ );
1326 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1327 tcp1->checksum = ip_csum_fold (sum1);
1333 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1335 udp1->src_port = s1->out2in.port;
1336 if (PREDICT_FALSE (udp1->checksum))
1338 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1339 new_port1 = udp1->src_port;
1340 sum1 = udp1->checksum;
1341 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1344 ip_csum_update (sum1, old_port1, new_port1,
1345 ip4_header_t /* cheat */ ,
1346 length /* changed member */ );
1347 udp1->checksum = ip_csum_fold (sum1);
1354 nat44_session_update_counters (s1, now,
1355 vlib_buffer_length_in_chain (vm, b1),
1357 /* Per-user LRU list maintenance */
1358 nat44_session_update_lru (sm, s1, thread_index);
1361 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1362 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1364 snat_in2out_trace_t *t =
1365 vlib_add_trace (vm, node, b1, sizeof (*t));
1366 t->sw_if_index = sw_if_index1;
1367 t->next_index = next1;
1368 t->session_index = ~0;
1371 s1 - sm->per_thread_data[thread_index].sessions;
1374 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1376 /* verify speculative enqueues, maybe switch current next frame */
1377 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1378 to_next, n_left_to_next,
1379 bi0, bi1, next0, next1);
1382 while (n_left_from > 0 && n_left_to_next > 0)
1390 u32 new_addr0, old_addr0;
1391 u16 old_port0, new_port0;
1394 icmp46_header_t *icmp0;
1395 snat_session_key_t key0;
1398 snat_session_t *s0 = 0;
1399 clib_bihash_kv_8_8_t kv0, value0;
1400 u32 iph_offset0 = 0;
1402 /* speculatively enqueue b0 to the current next frame */
1408 n_left_to_next -= 1;
1410 b0 = vlib_get_buffer (vm, bi0);
1411 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1413 if (is_output_feature)
1414 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1416 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1419 udp0 = ip4_next_header (ip0);
1420 tcp0 = (tcp_header_t *) udp0;
1421 icmp0 = (icmp46_header_t *) udp0;
1423 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1424 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1427 if (PREDICT_FALSE (ip0->ttl == 1))
1429 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1430 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1431 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1433 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1437 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1439 /* Next configured feature, probably ip4-lookup */
1442 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1444 if (nat_in2out_sm_unknown_proto
1445 (sm, b0, ip0, rx_fib_index0))
1447 next0 = SNAT_IN2OUT_NEXT_DROP;
1449 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1455 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1457 next0 = icmp_in2out_slow_path
1458 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1459 next0, now, thread_index, &s0);
1466 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1468 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1472 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1474 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1479 key0.addr = ip0->src_address;
1480 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1481 key0.protocol = proto0;
1482 key0.fib_index = rx_fib_index0;
1484 kv0.key = key0.as_u64;
1486 if (clib_bihash_search_8_8
1487 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1491 if (is_output_feature)
1494 (nat_not_translate_output_feature
1496 vnet_buffer (b0)->ip.reass.l4_src_port,
1497 vnet_buffer (b0)->ip.reass.l4_dst_port,
1498 thread_index, sw_if_index0)))
1502 * Send DHCP packets to the ipv4 stack, or we won't
1503 * be able to use dhcp client on the outside interface
1506 (proto0 == NAT_PROTOCOL_UDP
1507 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1508 clib_host_to_net_u16
1509 (UDP_DST_PORT_dhcp_to_server))
1510 && ip0->dst_address.as_u32 == 0xffffffff))
1517 (sm, node, sw_if_index0, ip0, proto0,
1518 rx_fib_index0, thread_index)))
1522 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1523 &s0, node, next0, thread_index, now);
1525 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1528 if (PREDICT_FALSE (!s0))
1533 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1539 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1542 b0->flags |= VNET_BUFFER_F_IS_NATED;
1544 old_addr0 = ip0->src_address.as_u32;
1545 ip0->src_address = s0->out2in.addr;
1546 new_addr0 = ip0->src_address.as_u32;
1547 if (!is_output_feature)
1548 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1550 sum0 = ip0->checksum;
1551 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1553 src_address /* changed member */ );
1554 ip0->checksum = ip_csum_fold (sum0);
1556 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1558 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1560 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1561 new_port0 = udp0->src_port = s0->out2in.port;
1562 sum0 = tcp0->checksum;
1563 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1565 dst_address /* changed member */ );
1566 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1567 ip4_header_t /* cheat */ ,
1568 length /* changed member */ );
1569 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1570 tcp0->checksum = ip_csum_fold (sum0);
1576 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1578 udp0->src_port = s0->out2in.port;
1579 if (PREDICT_FALSE (udp0->checksum))
1581 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1582 new_port0 = udp0->src_port;
1583 sum0 = udp0->checksum;
1584 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1587 ip_csum_update (sum0, old_port0, new_port0,
1588 ip4_header_t /* cheat */ ,
1589 length /* changed member */ );
1590 udp0->checksum = ip_csum_fold (sum0);
1597 nat44_session_update_counters (s0, now,
1598 vlib_buffer_length_in_chain (vm, b0),
1600 /* Per-user LRU list maintenance */
1601 nat44_session_update_lru (sm, s0, thread_index);
1604 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1605 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1607 snat_in2out_trace_t *t =
1608 vlib_add_trace (vm, node, b0, sizeof (*t));
1609 t->is_slow_path = is_slow_path;
1610 t->sw_if_index = sw_if_index0;
1611 t->next_index = next0;
1612 t->session_index = ~0;
1615 s0 - sm->per_thread_data[thread_index].sessions;
1618 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1620 /* verify speculative enqueue, maybe switch current next frame */
1621 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1622 to_next, n_left_to_next,
1626 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1629 vlib_node_increment_counter (vm, stats_node_index,
1630 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1632 vlib_node_increment_counter (vm, stats_node_index,
1633 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1634 vlib_node_increment_counter (vm, stats_node_index,
1635 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1636 vlib_node_increment_counter (vm, stats_node_index,
1637 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1638 vlib_node_increment_counter (vm, stats_node_index,
1639 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1641 vlib_node_increment_counter (vm, stats_node_index,
1642 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1644 return frame->n_vectors;
1647 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1648 vlib_node_runtime_t * node,
1649 vlib_frame_t * frame)
1651 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1656 VLIB_REGISTER_NODE (snat_in2out_node) = {
1657 .name = "nat44-in2out",
1658 .vector_size = sizeof (u32),
1659 .format_trace = format_snat_in2out_trace,
1660 .type = VLIB_NODE_TYPE_INTERNAL,
1662 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1663 .error_strings = snat_in2out_error_strings,
1665 .runtime_data_bytes = sizeof (snat_runtime_t),
1667 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1669 /* edit / add dispositions here */
1671 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1672 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1673 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1674 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1679 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1680 vlib_node_runtime_t * node,
1681 vlib_frame_t * frame)
1683 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1688 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1689 .name = "nat44-in2out-output",
1690 .vector_size = sizeof (u32),
1691 .format_trace = format_snat_in2out_trace,
1692 .type = VLIB_NODE_TYPE_INTERNAL,
1694 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1695 .error_strings = snat_in2out_error_strings,
1697 .runtime_data_bytes = sizeof (snat_runtime_t),
1699 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1701 /* edit / add dispositions here */
1703 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1704 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1705 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1706 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1711 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1712 vlib_node_runtime_t * node,
1713 vlib_frame_t * frame)
1715 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1720 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1721 .name = "nat44-in2out-slowpath",
1722 .vector_size = sizeof (u32),
1723 .format_trace = format_snat_in2out_trace,
1724 .type = VLIB_NODE_TYPE_INTERNAL,
1726 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1727 .error_strings = snat_in2out_error_strings,
1729 .runtime_data_bytes = sizeof (snat_runtime_t),
1731 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1733 /* edit / add dispositions here */
1735 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1736 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1737 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1738 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1743 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1744 vlib_node_runtime_t * node,
1745 vlib_frame_t * frame)
1747 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1752 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1753 .name = "nat44-in2out-output-slowpath",
1754 .vector_size = sizeof (u32),
1755 .format_trace = format_snat_in2out_trace,
1756 .type = VLIB_NODE_TYPE_INTERNAL,
1758 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1759 .error_strings = snat_in2out_error_strings,
1761 .runtime_data_bytes = sizeof (snat_runtime_t),
1763 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1765 /* edit / add dispositions here */
1767 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1768 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1769 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1770 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1775 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1776 vlib_node_runtime_t * node,
1777 vlib_frame_t * frame)
1779 u32 n_left_from, *from, *to_next;
1780 snat_in2out_next_t next_index;
1781 u32 pkts_processed = 0;
1782 snat_main_t *sm = &snat_main;
1783 u32 stats_node_index;
1784 int is_hairpinning = 0;
1786 stats_node_index = sm->in2out_fast_node_index;
1788 from = vlib_frame_vector_args (frame);
1789 n_left_from = frame->n_vectors;
1790 next_index = node->cached_next_index;
1792 while (n_left_from > 0)
1796 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1798 while (n_left_from > 0 && n_left_to_next > 0)
1806 u32 new_addr0, old_addr0;
1807 u16 old_port0, new_port0;
1810 icmp46_header_t *icmp0;
1811 snat_session_key_t key0, sm0;
1815 /* speculatively enqueue b0 to the current next frame */
1821 n_left_to_next -= 1;
1823 b0 = vlib_get_buffer (vm, bi0);
1824 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1826 ip0 = vlib_buffer_get_current (b0);
1827 udp0 = ip4_next_header (ip0);
1828 tcp0 = (tcp_header_t *) udp0;
1829 icmp0 = (icmp46_header_t *) udp0;
1831 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1833 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1835 if (PREDICT_FALSE (ip0->ttl == 1))
1837 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1838 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1839 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1841 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1845 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1847 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1850 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1852 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1853 rx_fib_index0, node, next0, ~0, 0, 0);
1857 key0.addr = ip0->src_address;
1858 key0.protocol = proto0;
1859 key0.port = udp0->src_port;
1860 key0.fib_index = rx_fib_index0;
1862 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
1864 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1865 next0 = SNAT_IN2OUT_NEXT_DROP;
1869 new_addr0 = sm0.addr.as_u32;
1870 new_port0 = sm0.port;
1871 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1872 old_addr0 = ip0->src_address.as_u32;
1873 ip0->src_address.as_u32 = new_addr0;
1875 sum0 = ip0->checksum;
1876 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1878 src_address /* changed member */ );
1879 ip0->checksum = ip_csum_fold (sum0);
1881 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1883 old_port0 = udp0->src_port;
1884 udp0->src_port = new_port0;
1886 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1888 sum0 = tcp0->checksum;
1889 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1891 dst_address /* changed member */ );
1892 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1893 ip4_header_t /* cheat */ ,
1894 length /* changed member */ );
1895 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1896 tcp0->checksum = ip_csum_fold (sum0);
1898 else if (udp0->checksum)
1900 sum0 = udp0->checksum;
1901 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1903 dst_address /* changed member */ );
1904 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1905 ip4_header_t /* cheat */ ,
1906 length /* changed member */ );
1907 udp0->checksum = ip_csum_fold (sum0);
1912 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1914 sum0 = tcp0->checksum;
1915 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1917 dst_address /* changed member */ );
1918 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1919 tcp0->checksum = ip_csum_fold (sum0);
1921 else if (udp0->checksum)
1923 sum0 = udp0->checksum;
1924 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1926 dst_address /* changed member */ );
1927 udp0->checksum = ip_csum_fold (sum0);
1932 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1937 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1938 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1940 snat_in2out_trace_t *t =
1941 vlib_add_trace (vm, node, b0, sizeof (*t));
1942 t->sw_if_index = sw_if_index0;
1943 t->next_index = next0;
1944 t->is_hairpinning = is_hairpinning;
1947 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1949 /* verify speculative enqueue, maybe switch current next frame */
1950 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1951 to_next, n_left_to_next,
1955 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1958 vlib_node_increment_counter (vm, stats_node_index,
1959 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1961 return frame->n_vectors;
1966 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1967 .name = "nat44-in2out-fast",
1968 .vector_size = sizeof (u32),
1969 .format_trace = format_snat_in2out_fast_trace,
1970 .type = VLIB_NODE_TYPE_INTERNAL,
1972 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1973 .error_strings = snat_in2out_error_strings,
1975 .runtime_data_bytes = sizeof (snat_runtime_t),
1977 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1979 /* edit / add dispositions here */
1981 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1982 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1983 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1984 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1990 * fd.io coding-style-patch-verification: ON
1993 * eval: (c-set-style "gnu")