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_reass.h>
31 #include <nat/nat_inlines.h>
32 #include <nat/nat44_inlines.h>
33 #include <nat/nat_syslog.h>
34 #include <nat/nat_ha.h>
36 #include <vppinfra/hash.h>
37 #include <vppinfra/error.h>
38 #include <vppinfra/elog.h>
46 } snat_in2out_trace_t;
48 /* packet trace format function */
50 format_snat_in2out_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
57 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
59 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
60 t->sw_if_index, t->next_index, t->session_index);
66 format_snat_in2out_fast_trace (u8 * s, va_list * args)
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
72 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
73 t->sw_if_index, t->next_index);
78 #define foreach_snat_in2out_error \
79 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
80 _(IN2OUT_PACKETS, "good in2out packets processed") \
81 _(OUT_OF_PORTS, "out of ports") \
82 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
83 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
84 _(NO_TRANSLATION, "no translation") \
85 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
86 _(DROP_FRAGMENT, "drop fragment") \
87 _(MAX_REASS, "maximum reassemblies exceeded") \
88 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
89 _(TCP_PACKETS, "TCP packets") \
90 _(UDP_PACKETS, "UDP packets") \
91 _(ICMP_PACKETS, "ICMP packets") \
92 _(OTHER_PACKETS, "other protocol packets") \
93 _(FRAGMENTS, "fragments") \
94 _(CACHED_FRAGMENTS, "cached fragments") \
95 _(PROCESSED_FRAGMENTS, "processed fragments")
99 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
100 foreach_snat_in2out_error
103 } snat_in2out_error_t;
105 static char *snat_in2out_error_strings[] = {
106 #define _(sym,string) string,
107 foreach_snat_in2out_error
113 SNAT_IN2OUT_NEXT_LOOKUP,
114 SNAT_IN2OUT_NEXT_DROP,
115 SNAT_IN2OUT_NEXT_ICMP_ERROR,
116 SNAT_IN2OUT_NEXT_SLOW_PATH,
117 SNAT_IN2OUT_NEXT_REASS,
119 } snat_in2out_next_t;
122 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
123 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
124 u32 rx_fib_index0, u32 thread_index)
126 udp_header_t *udp0 = ip4_next_header (ip0);
127 snat_session_key_t key0, sm0;
128 clib_bihash_kv_8_8_t kv0, value0;
130 key0.addr = ip0->dst_address;
131 key0.port = udp0->dst_port;
132 key0.protocol = proto0;
133 key0.fib_index = sm->outside_fib_index;
134 kv0.key = key0.as_u64;
136 /* NAT packet aimed at external address if */
137 /* has active sessions */
138 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
141 /* or is static mappings */
142 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
148 if (sm->forwarding_enabled)
151 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
156 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
157 u32 proto0, u16 src_port, u16 dst_port,
158 u32 thread_index, u32 sw_if_index)
160 snat_session_key_t key0;
161 clib_bihash_kv_8_8_t kv0, value0;
165 key0.addr = ip0->src_address;
166 key0.port = src_port;
167 key0.protocol = proto0;
168 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
169 kv0.key = key0.as_u64;
171 if (!clib_bihash_search_8_8
172 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
176 key0.addr = ip0->dst_address;
177 key0.port = dst_port;
178 key0.protocol = proto0;
179 kv0.key = key0.as_u64;
180 if (!clib_bihash_search_8_8
181 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
185 pool_foreach (i, sm->output_feature_interfaces,
187 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
197 #ifndef CLIB_MARCH_VARIANT
199 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
201 snat_main_t *sm = &snat_main;
202 nat44_is_idle_session_ctx_t *ctx = arg;
204 u64 sess_timeout_time;
205 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
207 clib_bihash_kv_8_8_t s_kv;
209 s = pool_elt_at_index (tsm->sessions, kv->value);
210 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
211 if (ctx->now >= sess_timeout_time)
213 s_kv.key = s->out2in.as_u64;
214 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
215 nat_elog_warn ("out2in key del failed");
217 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
218 s->in2out.addr.as_u32,
219 s->out2in.addr.as_u32,
223 s->in2out.fib_index);
225 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
226 &s->in2out.addr, s->in2out.port,
227 &s->out2in.addr, s->out2in.port,
230 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
231 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
234 if (!snat_is_session_static (s))
235 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
238 nat44_delete_session (sm, s, ctx->thread_index);
247 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
250 snat_session_key_t * key0,
251 snat_session_t ** sessionp,
252 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
255 snat_session_t *s = 0;
256 clib_bihash_kv_8_8_t kv0;
257 snat_session_key_t key1;
258 udp_header_t *udp0 = ip4_next_header (ip0);
260 nat_outside_fib_t *outside_fib;
261 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
264 .fp_proto = FIB_PROTOCOL_IP4,
267 .ip4.as_u32 = ip0->dst_address.as_u32,
270 nat44_is_idle_session_ctx_t ctx0;
272 nat44_session_try_cleanup (&ip0->src_address, rx_fib_index0, thread_index,
275 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
277 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
278 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
279 nat_elog_notice ("maximum sessions exceeded");
280 return SNAT_IN2OUT_NEXT_DROP;
283 key1.protocol = key0->protocol;
285 /* First try to match static mapping by local address and port */
286 if (snat_static_mapping_match
287 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
289 /* Try to create dynamic translation */
290 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
294 [thread_index].snat_thread_index))
296 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
297 return SNAT_IN2OUT_NEXT_DROP;
302 if (PREDICT_FALSE (identity_nat))
311 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
315 nat_elog_warn ("create NAT user failed");
316 return SNAT_IN2OUT_NEXT_DROP;
319 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
322 nat44_delete_user_with_no_session (sm, u, thread_index);
323 nat_elog_warn ("create NAT session failed");
324 return SNAT_IN2OUT_NEXT_DROP;
328 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
329 user_session_increment (sm, u, is_sm);
332 s->out2in.protocol = key0->protocol;
333 s->out2in.fib_index = sm->outside_fib_index;
334 switch (vec_len (sm->outside_fibs))
337 s->out2in.fib_index = sm->outside_fib_index;
340 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
344 vec_foreach (outside_fib, sm->outside_fibs)
346 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
347 if (FIB_NODE_INDEX_INVALID != fei)
349 if (fib_entry_get_resolving_interface (fei) != ~0)
351 s->out2in.fib_index = outside_fib->fib_index;
359 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
360 s->ext_host_port = udp0->dst_port;
363 /* Add to translation hashes */
365 ctx0.thread_index = thread_index;
366 kv0.key = s->in2out.as_u64;
367 kv0.value = s - sm->per_thread_data[thread_index].sessions;
368 if (clib_bihash_add_or_overwrite_stale_8_8
369 (&sm->per_thread_data[thread_index].in2out, &kv0,
370 nat44_i2o_is_idle_session_cb, &ctx0))
371 nat_elog_notice ("in2out key add failed");
373 kv0.key = s->out2in.as_u64;
374 kv0.value = s - sm->per_thread_data[thread_index].sessions;
376 if (clib_bihash_add_or_overwrite_stale_8_8
377 (&sm->per_thread_data[thread_index].out2in, &kv0,
378 nat44_o2i_is_idle_session_cb, &ctx0))
379 nat_elog_notice ("out2in key add failed");
382 snat_ipfix_logging_nat44_ses_create (thread_index,
383 s->in2out.addr.as_u32,
384 s->out2in.addr.as_u32,
387 s->out2in.port, s->in2out.fib_index);
389 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
390 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
391 s->out2in.port, s->in2out.protocol);
393 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
394 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
395 &s->ext_host_nat_addr, s->ext_host_nat_port,
396 s->in2out.protocol, s->in2out.fib_index, s->flags,
402 #ifndef CLIB_MARCH_VARIANT
404 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
405 snat_session_key_t * p_key0)
407 icmp46_header_t *icmp0;
408 snat_session_key_t key0;
409 icmp_echo_header_t *echo0, *inner_echo0 = 0;
410 ip4_header_t *inner_ip0 = 0;
412 icmp46_header_t *inner_icmp0;
414 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
415 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
417 if (!icmp_is_error_message (icmp0))
419 key0.protocol = SNAT_PROTOCOL_ICMP;
420 key0.addr = ip0->src_address;
421 key0.port = echo0->identifier;
425 inner_ip0 = (ip4_header_t *) (echo0 + 1);
426 l4_header = ip4_next_header (inner_ip0);
427 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
428 key0.addr = inner_ip0->dst_address;
429 switch (key0.protocol)
431 case SNAT_PROTOCOL_ICMP:
432 inner_icmp0 = (icmp46_header_t *) l4_header;
433 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
434 key0.port = inner_echo0->identifier;
436 case SNAT_PROTOCOL_UDP:
437 case SNAT_PROTOCOL_TCP:
438 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
441 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
445 return -1; /* success */
449 * Get address and port values to be used for ICMP packet translation
450 * and create session if needed
452 * @param[in,out] sm NAT main
453 * @param[in,out] node NAT node runtime
454 * @param[in] thread_index thread index
455 * @param[in,out] b0 buffer containing packet to be translated
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 icmp46_header_t *icmp0;
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;
479 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
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 (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 (&sm->per_thread_data[thread_index].in2out, &kv0,
497 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
499 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
512 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
513 ip0, SNAT_PROTOCOL_ICMP,
522 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
524 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
525 next0 = SNAT_IN2OUT_NEXT_DROP;
529 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
530 thread_index, vlib_time_now (sm->vlib_main));
532 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
543 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
544 icmp0->type != ICMP4_echo_reply &&
545 !icmp_is_error_message (icmp0)))
547 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
548 next0 = SNAT_IN2OUT_NEXT_DROP;
552 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
557 *p_proto = key0.protocol;
559 *p_value = s0->out2in;
560 *p_dont_translate = dont_translate;
562 *(snat_session_t **) d = s0;
567 #ifndef CLIB_MARCH_VARIANT
569 * Get address and port values to be used for ICMP packet translation
571 * @param[in] sm NAT main
572 * @param[in,out] node NAT node runtime
573 * @param[in] thread_index thread index
574 * @param[in,out] b0 buffer containing packet to be translated
575 * @param[out] p_proto protocol used for matching
576 * @param[out] p_value address and port after NAT translation
577 * @param[out] p_dont_translate if packet should not be translated
578 * @param d optional parameter
579 * @param e optional parameter
582 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
583 u32 thread_index, vlib_buffer_t * b0,
584 ip4_header_t * ip0, u8 * p_proto,
585 snat_session_key_t * p_value,
586 u8 * p_dont_translate, void *d, void *e)
588 icmp46_header_t *icmp0;
591 snat_session_key_t key0;
592 snat_session_key_t sm0;
593 u8 dont_translate = 0;
598 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
599 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
600 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
602 err = icmp_get_key (ip0, &key0);
605 b0->error = node->errors[err];
606 next0 = SNAT_IN2OUT_NEXT_DROP;
609 key0.fib_index = rx_fib_index0;
611 if (snat_static_mapping_match
612 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
614 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
622 if (icmp_is_error_message (icmp0))
624 next0 = SNAT_IN2OUT_NEXT_DROP;
628 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
629 next0 = SNAT_IN2OUT_NEXT_DROP;
633 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
634 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
635 !icmp_is_error_message (icmp0)))
637 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
638 next0 = SNAT_IN2OUT_NEXT_DROP;
645 *p_proto = key0.protocol;
646 *p_dont_translate = dont_translate;
651 #ifndef CLIB_MARCH_VARIANT
653 icmp_in2out (snat_main_t * sm,
656 icmp46_header_t * icmp0,
659 vlib_node_runtime_t * node,
660 u32 next0, u32 thread_index, void *d, void *e)
662 snat_session_key_t sm0;
664 icmp_echo_header_t *echo0, *inner_echo0 = 0;
665 ip4_header_t *inner_ip0;
667 icmp46_header_t *inner_icmp0;
669 u32 new_addr0, old_addr0;
670 u16 old_id0, new_id0;
671 u16 old_checksum0, new_checksum0;
676 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
678 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
679 &protocol, &sm0, &dont_translate, d,
683 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
686 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
688 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
690 vlib_buffer_get_current (b0),
691 ntohs (ip0->length) -
692 ip4_header_bytes (ip0), 0);
693 checksum0 = ~ip_csum_fold (sum0);
694 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
696 next0 = SNAT_IN2OUT_NEXT_DROP;
701 old_addr0 = ip0->src_address.as_u32;
702 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
704 sum0 = ip0->checksum;
705 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
706 src_address /* changed member */ );
707 ip0->checksum = ip_csum_fold (sum0);
709 if (icmp0->checksum == 0)
710 icmp0->checksum = 0xffff;
712 if (!icmp_is_error_message (icmp0))
715 if (PREDICT_FALSE (new_id0 != echo0->identifier))
717 old_id0 = echo0->identifier;
719 echo0->identifier = new_id0;
721 sum0 = icmp0->checksum;
722 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
724 icmp0->checksum = ip_csum_fold (sum0);
729 inner_ip0 = (ip4_header_t *) (echo0 + 1);
730 l4_header = ip4_next_header (inner_ip0);
732 if (!ip4_header_checksum_is_valid (inner_ip0))
734 next0 = SNAT_IN2OUT_NEXT_DROP;
738 /* update inner destination IP address */
739 old_addr0 = inner_ip0->dst_address.as_u32;
740 inner_ip0->dst_address = sm0.addr;
741 new_addr0 = inner_ip0->dst_address.as_u32;
742 sum0 = icmp0->checksum;
743 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
744 dst_address /* changed member */ );
745 icmp0->checksum = ip_csum_fold (sum0);
747 /* update inner IP header checksum */
748 old_checksum0 = inner_ip0->checksum;
749 sum0 = inner_ip0->checksum;
750 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
751 dst_address /* changed member */ );
752 inner_ip0->checksum = ip_csum_fold (sum0);
753 new_checksum0 = inner_ip0->checksum;
754 sum0 = icmp0->checksum;
755 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
757 icmp0->checksum = ip_csum_fold (sum0);
761 case SNAT_PROTOCOL_ICMP:
762 inner_icmp0 = (icmp46_header_t *) l4_header;
763 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
765 old_id0 = inner_echo0->identifier;
767 inner_echo0->identifier = new_id0;
769 sum0 = icmp0->checksum;
770 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
772 icmp0->checksum = ip_csum_fold (sum0);
774 case SNAT_PROTOCOL_UDP:
775 case SNAT_PROTOCOL_TCP:
776 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
778 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
780 sum0 = icmp0->checksum;
781 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
783 icmp0->checksum = ip_csum_fold (sum0);
790 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
792 if (sm->deterministic ||
793 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
794 sm->endpoint_dependent))
795 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
804 icmp_in2out_slow_path (snat_main_t * sm,
807 icmp46_header_t * icmp0,
810 vlib_node_runtime_t * node,
812 f64 now, u32 thread_index, snat_session_t ** p_s0)
814 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
815 next0, thread_index, p_s0, 0);
816 snat_session_t *s0 = *p_s0;
817 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
820 nat44_session_update_counters (s0, now,
821 vlib_buffer_length_in_chain
822 (sm->vlib_main, b0), thread_index);
823 /* Per-user LRU list maintenance */
824 nat44_session_update_lru (sm, s0, thread_index);
830 nat_in2out_sm_unknown_proto (snat_main_t * sm,
832 ip4_header_t * ip, u32 rx_fib_index)
834 clib_bihash_kv_8_8_t kv, value;
835 snat_static_mapping_t *m;
836 snat_session_key_t m_key;
837 u32 old_addr, new_addr;
840 m_key.addr = ip->src_address;
843 m_key.fib_index = rx_fib_index;
844 kv.key = m_key.as_u64;
845 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
848 m = pool_elt_at_index (sm->static_mappings, value.value);
850 old_addr = ip->src_address.as_u32;
851 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
853 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
854 ip->checksum = ip_csum_fold (sum);
858 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
860 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
861 nat_hairpinning_sm_unknown_proto (sm, b, ip);
868 snat_in2out_node_fn_inline (vlib_main_t * vm,
869 vlib_node_runtime_t * node,
870 vlib_frame_t * frame, int is_slow_path,
871 int is_output_feature)
873 u32 n_left_from, *from, *to_next;
874 snat_in2out_next_t next_index;
875 u32 pkts_processed = 0;
876 snat_main_t *sm = &snat_main;
877 f64 now = vlib_time_now (vm);
878 u32 stats_node_index;
879 u32 thread_index = vm->thread_index;
880 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
883 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
884 sm->in2out_node_index;
886 from = vlib_frame_vector_args (frame);
887 n_left_from = frame->n_vectors;
888 next_index = node->cached_next_index;
890 while (n_left_from > 0)
894 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
896 while (n_left_from >= 4 && n_left_to_next >= 2)
899 vlib_buffer_t *b0, *b1;
901 u32 sw_if_index0, sw_if_index1;
902 ip4_header_t *ip0, *ip1;
903 ip_csum_t sum0, sum1;
904 u32 new_addr0, old_addr0, new_addr1, old_addr1;
905 u16 old_port0, new_port0, old_port1, new_port1;
906 udp_header_t *udp0, *udp1;
907 tcp_header_t *tcp0, *tcp1;
908 icmp46_header_t *icmp0, *icmp1;
909 snat_session_key_t key0, key1;
910 u32 rx_fib_index0, rx_fib_index1;
912 snat_session_t *s0 = 0, *s1 = 0;
913 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
914 u32 iph_offset0 = 0, iph_offset1 = 0;
916 /* Prefetch next iteration. */
918 vlib_buffer_t *p2, *p3;
920 p2 = vlib_get_buffer (vm, from[2]);
921 p3 = vlib_get_buffer (vm, from[3]);
923 vlib_prefetch_buffer_header (p2, LOAD);
924 vlib_prefetch_buffer_header (p3, LOAD);
926 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
927 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
930 /* speculatively enqueue b0 and b1 to the current next frame */
931 to_next[0] = bi0 = from[0];
932 to_next[1] = bi1 = from[1];
938 b0 = vlib_get_buffer (vm, bi0);
939 b1 = vlib_get_buffer (vm, bi1);
941 if (is_output_feature)
942 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
944 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
947 udp0 = ip4_next_header (ip0);
948 tcp0 = (tcp_header_t *) udp0;
949 icmp0 = (icmp46_header_t *) udp0;
951 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
952 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
955 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
957 if (PREDICT_FALSE (ip0->ttl == 1))
959 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
960 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
961 ICMP4_time_exceeded_ttl_exceeded_in_transit,
963 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
967 proto0 = ip_proto_to_snat_proto (ip0->protocol);
969 /* Next configured feature, probably ip4-lookup */
972 if (PREDICT_FALSE (proto0 == ~0))
974 if (nat_in2out_sm_unknown_proto
975 (sm, b0, ip0, rx_fib_index0))
977 next0 = SNAT_IN2OUT_NEXT_DROP;
979 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
985 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
987 next0 = icmp_in2out_slow_path
988 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
989 node, next0, now, thread_index, &s0);
996 if (PREDICT_FALSE (proto0 == ~0))
998 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1002 if (ip4_is_fragment (ip0))
1004 next0 = SNAT_IN2OUT_NEXT_REASS;
1009 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1011 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1016 key0.addr = ip0->src_address;
1017 key0.port = udp0->src_port;
1018 key0.protocol = proto0;
1019 key0.fib_index = rx_fib_index0;
1021 kv0.key = key0.as_u64;
1024 (clib_bihash_search_8_8
1025 (&sm->per_thread_data[thread_index].in2out, &kv0,
1030 if (is_output_feature)
1032 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1042 * Send DHCP packets to the ipv4 stack, or we won't
1043 * be able to use dhcp client on the outside interface
1046 ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1047 && proto0 == SNAT_PROTOCOL_UDP
1048 && (udp0->dst_port ==
1049 clib_host_to_net_u16
1050 (UDP_DST_PORT_dhcp_to_server))))
1057 (sm, node, sw_if_index0, ip0, proto0,
1058 rx_fib_index0, thread_index)))
1062 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1063 &s0, node, next0, thread_index, now);
1064 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1067 if (PREDICT_FALSE (!s0))
1072 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1078 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1081 b0->flags |= VNET_BUFFER_F_IS_NATED;
1083 old_addr0 = ip0->src_address.as_u32;
1084 ip0->src_address = s0->out2in.addr;
1085 new_addr0 = ip0->src_address.as_u32;
1086 if (!is_output_feature)
1087 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1089 sum0 = ip0->checksum;
1090 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1092 src_address /* changed member */ );
1093 ip0->checksum = ip_csum_fold (sum0);
1095 old_port0 = udp0->src_port;
1096 new_port0 = udp0->src_port = s0->out2in.port;
1098 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1100 sum0 = tcp0->checksum;
1101 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1103 dst_address /* changed member */ );
1104 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1105 ip4_header_t /* cheat */ ,
1106 length /* changed member */ );
1107 mss_clamping (sm, tcp0, &sum0);
1108 tcp0->checksum = ip_csum_fold (sum0);
1113 if (PREDICT_FALSE (udp0->checksum))
1115 sum0 = udp0->checksum;
1116 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1118 dst_address /* changed member */ );
1119 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1120 ip4_header_t /* cheat */ ,
1121 length /* changed member */ );
1122 udp0->checksum = ip_csum_fold (sum0);
1128 nat44_session_update_counters (s0, now,
1129 vlib_buffer_length_in_chain (vm, b0),
1131 /* Per-user LRU list maintenance */
1132 nat44_session_update_lru (sm, s0, thread_index);
1135 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1136 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1138 snat_in2out_trace_t *t =
1139 vlib_add_trace (vm, node, b0, sizeof (*t));
1140 t->is_slow_path = is_slow_path;
1141 t->sw_if_index = sw_if_index0;
1142 t->next_index = next0;
1143 t->session_index = ~0;
1146 s0 - sm->per_thread_data[thread_index].sessions;
1149 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1151 if (is_output_feature)
1152 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1154 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1157 udp1 = ip4_next_header (ip1);
1158 tcp1 = (tcp_header_t *) udp1;
1159 icmp1 = (icmp46_header_t *) udp1;
1161 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1162 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1165 if (PREDICT_FALSE (ip1->ttl == 1))
1167 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1168 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1169 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1171 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1175 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1177 /* Next configured feature, probably ip4-lookup */
1180 if (PREDICT_FALSE (proto1 == ~0))
1182 if (nat_in2out_sm_unknown_proto
1183 (sm, b1, ip1, rx_fib_index1))
1185 next1 = SNAT_IN2OUT_NEXT_DROP;
1187 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1193 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1195 next1 = icmp_in2out_slow_path
1196 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1197 next1, now, thread_index, &s1);
1204 if (PREDICT_FALSE (proto1 == ~0))
1206 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1210 if (ip4_is_fragment (ip1))
1212 next1 = SNAT_IN2OUT_NEXT_REASS;
1217 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1219 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1224 key1.addr = ip1->src_address;
1225 key1.port = udp1->src_port;
1226 key1.protocol = proto1;
1227 key1.fib_index = rx_fib_index1;
1229 kv1.key = key1.as_u64;
1232 (clib_bihash_search_8_8
1233 (&sm->per_thread_data[thread_index].in2out, &kv1,
1238 if (is_output_feature)
1240 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1250 * Send DHCP packets to the ipv4 stack, or we won't
1251 * be able to use dhcp client on the outside interface
1254 ((b1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1255 && proto1 == SNAT_PROTOCOL_UDP
1256 && (udp1->dst_port ==
1257 clib_host_to_net_u16
1258 (UDP_DST_PORT_dhcp_to_server))))
1265 (sm, node, sw_if_index1, ip1, proto1,
1266 rx_fib_index1, thread_index)))
1270 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1271 &s1, node, next1, thread_index, now);
1272 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1275 if (PREDICT_FALSE (!s1))
1280 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1286 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1289 b1->flags |= VNET_BUFFER_F_IS_NATED;
1291 old_addr1 = ip1->src_address.as_u32;
1292 ip1->src_address = s1->out2in.addr;
1293 new_addr1 = ip1->src_address.as_u32;
1294 if (!is_output_feature)
1295 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1297 sum1 = ip1->checksum;
1298 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1300 src_address /* changed member */ );
1301 ip1->checksum = ip_csum_fold (sum1);
1303 old_port1 = udp1->src_port;
1304 new_port1 = udp1->src_port = s1->out2in.port;
1306 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1308 sum1 = tcp1->checksum;
1309 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1311 dst_address /* changed member */ );
1312 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1313 ip4_header_t /* cheat */ ,
1314 length /* changed member */ );
1315 mss_clamping (sm, tcp1, &sum1);
1316 tcp1->checksum = ip_csum_fold (sum1);
1321 if (PREDICT_FALSE (udp1->checksum))
1323 sum1 = udp1->checksum;
1324 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1326 dst_address /* changed member */ );
1327 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1328 ip4_header_t /* cheat */ ,
1329 length /* changed member */ );
1330 udp1->checksum = ip_csum_fold (sum1);
1336 nat44_session_update_counters (s1, now,
1337 vlib_buffer_length_in_chain (vm, b1),
1339 /* Per-user LRU list maintenance */
1340 nat44_session_update_lru (sm, s1, thread_index);
1343 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1344 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1346 snat_in2out_trace_t *t =
1347 vlib_add_trace (vm, node, b1, sizeof (*t));
1348 t->sw_if_index = sw_if_index1;
1349 t->next_index = next1;
1350 t->session_index = ~0;
1353 s1 - sm->per_thread_data[thread_index].sessions;
1356 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1358 /* verify speculative enqueues, maybe switch current next frame */
1359 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1360 to_next, n_left_to_next,
1361 bi0, bi1, next0, next1);
1364 while (n_left_from > 0 && n_left_to_next > 0)
1372 u32 new_addr0, old_addr0;
1373 u16 old_port0, new_port0;
1376 icmp46_header_t *icmp0;
1377 snat_session_key_t key0;
1380 snat_session_t *s0 = 0;
1381 clib_bihash_kv_8_8_t kv0, value0;
1382 u32 iph_offset0 = 0;
1384 /* speculatively enqueue b0 to the current next frame */
1390 n_left_to_next -= 1;
1392 b0 = vlib_get_buffer (vm, bi0);
1393 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1395 if (is_output_feature)
1396 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1398 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1401 udp0 = ip4_next_header (ip0);
1402 tcp0 = (tcp_header_t *) udp0;
1403 icmp0 = (icmp46_header_t *) udp0;
1405 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1406 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1409 if (PREDICT_FALSE (ip0->ttl == 1))
1411 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1412 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1413 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1415 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1419 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1421 /* Next configured feature, probably ip4-lookup */
1424 if (PREDICT_FALSE (proto0 == ~0))
1426 if (nat_in2out_sm_unknown_proto
1427 (sm, b0, ip0, rx_fib_index0))
1429 next0 = SNAT_IN2OUT_NEXT_DROP;
1431 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1437 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1439 next0 = icmp_in2out_slow_path
1440 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1441 next0, now, thread_index, &s0);
1448 if (PREDICT_FALSE (proto0 == ~0))
1450 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1454 if (ip4_is_fragment (ip0))
1456 next0 = SNAT_IN2OUT_NEXT_REASS;
1461 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1463 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1468 key0.addr = ip0->src_address;
1469 key0.port = udp0->src_port;
1470 key0.protocol = proto0;
1471 key0.fib_index = rx_fib_index0;
1473 kv0.key = key0.as_u64;
1475 if (clib_bihash_search_8_8
1476 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1480 if (is_output_feature)
1482 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1492 * Send DHCP packets to the ipv4 stack, or we won't
1493 * be able to use dhcp client on the outside interface
1496 ((b0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)
1497 && proto0 == SNAT_PROTOCOL_UDP
1498 && (udp0->dst_port ==
1499 clib_host_to_net_u16
1500 (UDP_DST_PORT_dhcp_to_server))))
1507 (sm, node, sw_if_index0, ip0, proto0,
1508 rx_fib_index0, thread_index)))
1512 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1513 &s0, node, next0, thread_index, now);
1515 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1518 if (PREDICT_FALSE (!s0))
1523 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1529 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1532 b0->flags |= VNET_BUFFER_F_IS_NATED;
1534 old_addr0 = ip0->src_address.as_u32;
1535 ip0->src_address = s0->out2in.addr;
1536 new_addr0 = ip0->src_address.as_u32;
1537 if (!is_output_feature)
1538 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1540 sum0 = ip0->checksum;
1541 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1543 src_address /* changed member */ );
1544 ip0->checksum = ip_csum_fold (sum0);
1546 old_port0 = udp0->src_port;
1547 new_port0 = udp0->src_port = s0->out2in.port;
1549 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1551 sum0 = tcp0->checksum;
1552 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1554 dst_address /* changed member */ );
1555 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1556 ip4_header_t /* cheat */ ,
1557 length /* changed member */ );
1558 mss_clamping (sm, tcp0, &sum0);
1559 tcp0->checksum = ip_csum_fold (sum0);
1564 if (PREDICT_FALSE (udp0->checksum))
1566 sum0 = udp0->checksum;
1567 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1569 dst_address /* changed member */ );
1570 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1571 ip4_header_t /* cheat */ ,
1572 length /* changed member */ );
1573 udp0->checksum = ip_csum_fold (sum0);
1579 nat44_session_update_counters (s0, now,
1580 vlib_buffer_length_in_chain (vm, b0),
1582 /* Per-user LRU list maintenance */
1583 nat44_session_update_lru (sm, s0, thread_index);
1586 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1587 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1589 snat_in2out_trace_t *t =
1590 vlib_add_trace (vm, node, b0, sizeof (*t));
1591 t->is_slow_path = is_slow_path;
1592 t->sw_if_index = sw_if_index0;
1593 t->next_index = next0;
1594 t->session_index = ~0;
1597 s0 - sm->per_thread_data[thread_index].sessions;
1600 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1602 /* verify speculative enqueue, maybe switch current next frame */
1603 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1604 to_next, n_left_to_next,
1608 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1611 vlib_node_increment_counter (vm, stats_node_index,
1612 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1614 vlib_node_increment_counter (vm, stats_node_index,
1615 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1616 vlib_node_increment_counter (vm, stats_node_index,
1617 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1618 vlib_node_increment_counter (vm, stats_node_index,
1619 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1620 vlib_node_increment_counter (vm, stats_node_index,
1621 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1623 vlib_node_increment_counter (vm, stats_node_index,
1624 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1626 return frame->n_vectors;
1629 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1630 vlib_node_runtime_t * node,
1631 vlib_frame_t * frame)
1633 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1638 VLIB_REGISTER_NODE (snat_in2out_node) = {
1639 .name = "nat44-in2out",
1640 .vector_size = sizeof (u32),
1641 .format_trace = format_snat_in2out_trace,
1642 .type = VLIB_NODE_TYPE_INTERNAL,
1644 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1645 .error_strings = snat_in2out_error_strings,
1647 .runtime_data_bytes = sizeof (snat_runtime_t),
1649 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1651 /* edit / add dispositions here */
1653 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1654 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1655 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1656 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1657 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1662 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1663 vlib_node_runtime_t * node,
1664 vlib_frame_t * frame)
1666 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1671 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1672 .name = "nat44-in2out-output",
1673 .vector_size = sizeof (u32),
1674 .format_trace = format_snat_in2out_trace,
1675 .type = VLIB_NODE_TYPE_INTERNAL,
1677 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1678 .error_strings = snat_in2out_error_strings,
1680 .runtime_data_bytes = sizeof (snat_runtime_t),
1682 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1684 /* edit / add dispositions here */
1686 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1687 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1688 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1689 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1690 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1695 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1696 vlib_node_runtime_t * node,
1697 vlib_frame_t * frame)
1699 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1704 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1705 .name = "nat44-in2out-slowpath",
1706 .vector_size = sizeof (u32),
1707 .format_trace = format_snat_in2out_trace,
1708 .type = VLIB_NODE_TYPE_INTERNAL,
1710 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1711 .error_strings = snat_in2out_error_strings,
1713 .runtime_data_bytes = sizeof (snat_runtime_t),
1715 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1717 /* edit / add dispositions here */
1719 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1720 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1721 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1722 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1723 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1728 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1729 vlib_node_runtime_t * node,
1730 vlib_frame_t * frame)
1732 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1737 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1738 .name = "nat44-in2out-output-slowpath",
1739 .vector_size = sizeof (u32),
1740 .format_trace = format_snat_in2out_trace,
1741 .type = VLIB_NODE_TYPE_INTERNAL,
1743 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1744 .error_strings = snat_in2out_error_strings,
1746 .runtime_data_bytes = sizeof (snat_runtime_t),
1748 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1750 /* edit / add dispositions here */
1752 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1753 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1754 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1755 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1756 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1761 VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
1762 vlib_node_runtime_t * node,
1763 vlib_frame_t * frame)
1765 u32 n_left_from, *from, *to_next;
1766 snat_in2out_next_t next_index;
1767 u32 pkts_processed = 0, cached_fragments = 0;
1768 snat_main_t *sm = &snat_main;
1769 f64 now = vlib_time_now (vm);
1770 u32 thread_index = vm->thread_index;
1771 snat_main_per_thread_data_t *per_thread_data =
1772 &sm->per_thread_data[thread_index];
1773 u32 *fragments_to_drop = 0;
1774 u32 *fragments_to_loopback = 0;
1776 from = vlib_frame_vector_args (frame);
1777 n_left_from = frame->n_vectors;
1778 next_index = node->cached_next_index;
1780 while (n_left_from > 0)
1784 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1786 while (n_left_from > 0 && n_left_to_next > 0)
1788 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1793 nat_reass_ip4_t *reass0;
1796 icmp46_header_t *icmp0;
1797 snat_session_key_t key0;
1798 clib_bihash_kv_8_8_t kv0, value0;
1799 snat_session_t *s0 = 0;
1800 u16 old_port0, new_port0;
1803 /* speculatively enqueue b0 to the current next frame */
1809 n_left_to_next -= 1;
1811 b0 = vlib_get_buffer (vm, bi0);
1812 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1814 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1816 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1819 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1821 next0 = SNAT_IN2OUT_NEXT_DROP;
1822 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1826 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1827 udp0 = ip4_next_header (ip0);
1828 tcp0 = (tcp_header_t *) udp0;
1829 icmp0 = (icmp46_header_t *) udp0;
1830 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1832 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1836 1, &fragments_to_drop);
1838 if (PREDICT_FALSE (!reass0))
1840 next0 = SNAT_IN2OUT_NEXT_DROP;
1841 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1842 nat_elog_notice ("maximum reassemblies exceeded");
1846 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1848 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1850 next0 = icmp_in2out_slow_path
1851 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1852 next0, now, thread_index, &s0);
1854 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1857 reass0->sess_index = s0 - per_thread_data->sessions;
1859 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1860 nat_ip4_reass_get_frags (reass0,
1861 &fragments_to_loopback);
1867 key0.addr = ip0->src_address;
1868 key0.port = udp0->src_port;
1869 key0.protocol = proto0;
1870 key0.fib_index = rx_fib_index0;
1871 kv0.key = key0.as_u64;
1873 if (clib_bihash_search_8_8
1874 (&per_thread_data->in2out, &kv0, &value0))
1878 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1882 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1883 &s0, node, next0, thread_index, now);
1885 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1888 if (PREDICT_FALSE (!s0))
1891 reass0->sess_index = s0 - per_thread_data->sessions;
1895 s0 = pool_elt_at_index (per_thread_data->sessions,
1897 reass0->sess_index = value0.value;
1899 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1903 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1905 if (nat_ip4_reass_add_fragment
1906 (thread_index, reass0, bi0, &fragments_to_drop))
1908 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1910 ("maximum fragments per reassembly exceeded");
1911 next0 = SNAT_IN2OUT_NEXT_DROP;
1917 s0 = pool_elt_at_index (per_thread_data->sessions,
1918 reass0->sess_index);
1921 old_addr0 = ip0->src_address.as_u32;
1922 ip0->src_address = s0->out2in.addr;
1923 new_addr0 = ip0->src_address.as_u32;
1924 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1926 sum0 = ip0->checksum;
1927 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1929 src_address /* changed member */ );
1930 ip0->checksum = ip_csum_fold (sum0);
1932 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1934 old_port0 = udp0->src_port;
1935 new_port0 = udp0->src_port = s0->out2in.port;
1937 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1939 sum0 = tcp0->checksum;
1940 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1942 dst_address /* changed member */ );
1943 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1944 ip4_header_t /* cheat */ ,
1945 length /* changed member */ );
1946 tcp0->checksum = ip_csum_fold (sum0);
1948 else if (PREDICT_FALSE (udp0->checksum))
1950 sum0 = udp0->checksum;
1951 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1953 dst_address /* changed member */ );
1954 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1955 ip4_header_t /* cheat */ ,
1956 length /* changed member */ );
1957 udp0->checksum = ip_csum_fold (sum0);
1962 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1963 s0->ext_host_port, proto0, 0);
1966 nat44_session_update_counters (s0, now,
1967 vlib_buffer_length_in_chain (vm, b0),
1969 /* Per-user LRU list maintenance */
1970 nat44_session_update_lru (sm, s0, thread_index);
1973 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1974 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1976 nat44_reass_trace_t *t =
1977 vlib_add_trace (vm, node, b0, sizeof (*t));
1978 t->cached = cached0;
1979 t->sw_if_index = sw_if_index0;
1980 t->next_index = next0;
1991 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1993 /* verify speculative enqueue, maybe switch current next frame */
1994 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1995 to_next, n_left_to_next,
1999 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2001 from = vlib_frame_vector_args (frame);
2002 u32 len = vec_len (fragments_to_loopback);
2003 if (len <= VLIB_FRAME_SIZE)
2005 clib_memcpy_fast (from, fragments_to_loopback,
2006 sizeof (u32) * len);
2008 vec_reset_length (fragments_to_loopback);
2012 clib_memcpy_fast (from, fragments_to_loopback +
2013 (len - VLIB_FRAME_SIZE),
2014 sizeof (u32) * VLIB_FRAME_SIZE);
2015 n_left_from = VLIB_FRAME_SIZE;
2016 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2021 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2024 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
2025 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
2027 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
2028 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
2031 nat_send_all_to_node (vm, fragments_to_drop, node,
2032 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
2033 SNAT_IN2OUT_NEXT_DROP);
2035 vec_free (fragments_to_drop);
2036 vec_free (fragments_to_loopback);
2037 return frame->n_vectors;
2041 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
2042 .name = "nat44-in2out-reass",
2043 .vector_size = sizeof (u32),
2044 .format_trace = format_nat44_reass_trace,
2045 .type = VLIB_NODE_TYPE_INTERNAL,
2047 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2048 .error_strings = snat_in2out_error_strings,
2050 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2052 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2053 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2054 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2055 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2056 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2061 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
2062 vlib_node_runtime_t * node,
2063 vlib_frame_t * frame)
2065 u32 n_left_from, *from, *to_next;
2066 snat_in2out_next_t next_index;
2067 u32 pkts_processed = 0;
2068 snat_main_t *sm = &snat_main;
2069 u32 stats_node_index;
2071 stats_node_index = sm->in2out_fast_node_index;
2073 from = vlib_frame_vector_args (frame);
2074 n_left_from = frame->n_vectors;
2075 next_index = node->cached_next_index;
2077 while (n_left_from > 0)
2081 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2083 while (n_left_from > 0 && n_left_to_next > 0)
2091 u32 new_addr0, old_addr0;
2092 u16 old_port0, new_port0;
2095 icmp46_header_t *icmp0;
2096 snat_session_key_t key0, sm0;
2100 /* speculatively enqueue b0 to the current next frame */
2106 n_left_to_next -= 1;
2108 b0 = vlib_get_buffer (vm, bi0);
2109 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2111 ip0 = vlib_buffer_get_current (b0);
2112 udp0 = ip4_next_header (ip0);
2113 tcp0 = (tcp_header_t *) udp0;
2114 icmp0 = (icmp46_header_t *) udp0;
2116 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2118 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2120 if (PREDICT_FALSE (ip0->ttl == 1))
2122 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2123 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2124 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2126 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2130 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2132 if (PREDICT_FALSE (proto0 == ~0))
2135 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2137 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2138 rx_fib_index0, node, next0, ~0, 0, 0);
2142 key0.addr = ip0->src_address;
2143 key0.protocol = proto0;
2144 key0.port = udp0->src_port;
2145 key0.fib_index = rx_fib_index0;
2147 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2149 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2150 next0 = SNAT_IN2OUT_NEXT_DROP;
2154 new_addr0 = sm0.addr.as_u32;
2155 new_port0 = sm0.port;
2156 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2157 old_addr0 = ip0->src_address.as_u32;
2158 ip0->src_address.as_u32 = new_addr0;
2160 sum0 = ip0->checksum;
2161 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2163 src_address /* changed member */ );
2164 ip0->checksum = ip_csum_fold (sum0);
2166 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2168 old_port0 = udp0->src_port;
2169 udp0->src_port = new_port0;
2171 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2173 sum0 = tcp0->checksum;
2174 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2176 dst_address /* changed member */ );
2177 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2178 ip4_header_t /* cheat */ ,
2179 length /* changed member */ );
2180 mss_clamping (sm, tcp0, &sum0);
2181 tcp0->checksum = ip_csum_fold (sum0);
2183 else if (udp0->checksum)
2185 sum0 = udp0->checksum;
2186 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2188 dst_address /* changed member */ );
2189 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2190 ip4_header_t /* cheat */ ,
2191 length /* changed member */ );
2192 udp0->checksum = ip_csum_fold (sum0);
2197 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2199 sum0 = tcp0->checksum;
2200 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2202 dst_address /* changed member */ );
2203 mss_clamping (sm, tcp0, &sum0);
2204 tcp0->checksum = ip_csum_fold (sum0);
2206 else if (udp0->checksum)
2208 sum0 = udp0->checksum;
2209 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2211 dst_address /* changed member */ );
2212 udp0->checksum = ip_csum_fold (sum0);
2217 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2220 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2221 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2223 snat_in2out_trace_t *t =
2224 vlib_add_trace (vm, node, b0, sizeof (*t));
2225 t->sw_if_index = sw_if_index0;
2226 t->next_index = next0;
2229 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2231 /* verify speculative enqueue, maybe switch current next frame */
2232 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2233 to_next, n_left_to_next,
2237 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2240 vlib_node_increment_counter (vm, stats_node_index,
2241 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2243 return frame->n_vectors;
2248 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2249 .name = "nat44-in2out-fast",
2250 .vector_size = sizeof (u32),
2251 .format_trace = format_snat_in2out_fast_trace,
2252 .type = VLIB_NODE_TYPE_INTERNAL,
2254 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2255 .error_strings = snat_in2out_error_strings,
2257 .runtime_data_bytes = sizeof (snat_runtime_t),
2259 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2261 /* edit / add dispositions here */
2263 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2264 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2265 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2266 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2267 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2273 * fd.io coding-style-patch-verification: ON
2276 * eval: (c-set-style "gnu")