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>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_syslog.h>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
43 } snat_in2out_trace_t;
45 /* packet trace format function */
47 format_snat_in2out_trace (u8 * s, va_list * args)
49 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
54 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
56 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
57 t->sw_if_index, t->next_index, t->session_index);
63 format_snat_in2out_fast_trace (u8 * s, va_list * args)
65 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
69 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
70 t->sw_if_index, t->next_index);
75 #define foreach_snat_in2out_error \
76 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
77 _(IN2OUT_PACKETS, "good in2out packets processed") \
78 _(OUT_OF_PORTS, "out of ports") \
79 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
80 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
81 _(NO_TRANSLATION, "no translation") \
82 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
83 _(DROP_FRAGMENT, "drop fragment") \
84 _(MAX_REASS, "maximum reassemblies exceeded") \
85 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
86 _(TCP_PACKETS, "TCP packets") \
87 _(UDP_PACKETS, "UDP packets") \
88 _(ICMP_PACKETS, "ICMP packets") \
89 _(OTHER_PACKETS, "other protocol packets") \
90 _(FRAGMENTS, "fragments") \
91 _(CACHED_FRAGMENTS, "cached fragments") \
92 _(PROCESSED_FRAGMENTS, "processed fragments")
96 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
97 foreach_snat_in2out_error
100 } snat_in2out_error_t;
102 static char *snat_in2out_error_strings[] = {
103 #define _(sym,string) string,
104 foreach_snat_in2out_error
110 SNAT_IN2OUT_NEXT_LOOKUP,
111 SNAT_IN2OUT_NEXT_DROP,
112 SNAT_IN2OUT_NEXT_ICMP_ERROR,
113 SNAT_IN2OUT_NEXT_SLOW_PATH,
114 SNAT_IN2OUT_NEXT_REASS,
116 } snat_in2out_next_t;
119 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
120 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
121 u32 rx_fib_index0, u32 thread_index)
123 udp_header_t *udp0 = ip4_next_header (ip0);
124 snat_session_key_t key0, sm0;
125 clib_bihash_kv_8_8_t kv0, value0;
127 key0.addr = ip0->dst_address;
128 key0.port = udp0->dst_port;
129 key0.protocol = proto0;
130 key0.fib_index = sm->outside_fib_index;
131 kv0.key = key0.as_u64;
133 /* NAT packet aimed at external address if */
134 /* has active sessions */
135 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
138 /* or is static mappings */
139 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
145 if (sm->forwarding_enabled)
148 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
153 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
154 u32 proto0, u16 src_port, u16 dst_port,
155 u32 thread_index, u32 sw_if_index)
157 snat_session_key_t key0;
158 clib_bihash_kv_8_8_t kv0, value0;
162 key0.addr = ip0->src_address;
163 key0.port = src_port;
164 key0.protocol = proto0;
165 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
166 kv0.key = key0.as_u64;
168 if (!clib_bihash_search_8_8
169 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
173 key0.addr = ip0->dst_address;
174 key0.port = dst_port;
175 key0.protocol = proto0;
176 kv0.key = key0.as_u64;
177 if (!clib_bihash_search_8_8
178 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
182 pool_foreach (i, sm->output_feature_interfaces,
184 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
194 #ifndef CLIB_MARCH_VARIANT
196 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
198 snat_main_t *sm = &snat_main;
199 nat44_is_idle_session_ctx_t *ctx = arg;
201 u64 sess_timeout_time;
202 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
204 clib_bihash_kv_8_8_t s_kv;
206 s = pool_elt_at_index (tsm->sessions, kv->value);
207 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
208 if (ctx->now >= sess_timeout_time)
210 s_kv.key = s->out2in.as_u64;
211 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
212 nat_log_warn ("out2in key del failed");
214 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
215 s->in2out.addr.as_u32,
216 s->out2in.addr.as_u32,
220 s->in2out.fib_index);
222 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
223 &s->in2out.addr, s->in2out.port,
224 &s->out2in.addr, s->out2in.port,
227 if (!snat_is_session_static (s))
228 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
231 nat44_delete_session (sm, s, ctx->thread_index);
240 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
243 snat_session_key_t * key0,
244 snat_session_t ** sessionp,
245 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
248 snat_session_t *s = 0;
249 clib_bihash_kv_8_8_t kv0;
250 snat_session_key_t key1;
251 udp_header_t *udp0 = ip4_next_header (ip0);
253 nat_outside_fib_t *outside_fib;
254 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
257 .fp_proto = FIB_PROTOCOL_IP4,
260 .ip4.as_u32 = ip0->dst_address.as_u32,
263 nat44_is_idle_session_ctx_t ctx0;
265 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
267 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
268 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
269 nat_log_notice ("maximum sessions exceeded");
270 return SNAT_IN2OUT_NEXT_DROP;
273 key1.protocol = key0->protocol;
275 /* First try to match static mapping by local address and port */
276 if (snat_static_mapping_match
277 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
279 /* Try to create dynamic translation */
280 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
284 [thread_index].snat_thread_index))
286 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
287 return SNAT_IN2OUT_NEXT_DROP;
292 if (PREDICT_FALSE (identity_nat))
301 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
305 nat_log_warn ("create NAT user failed");
306 return SNAT_IN2OUT_NEXT_DROP;
309 s = nat_session_alloc_or_recycle (sm, u, thread_index);
312 nat44_delete_user_with_no_session (sm, u, thread_index);
313 nat_log_warn ("create NAT session failed");
314 return SNAT_IN2OUT_NEXT_DROP;
318 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
319 user_session_increment (sm, u, is_sm);
322 s->out2in.protocol = key0->protocol;
323 s->out2in.fib_index = sm->outside_fib_index;
324 switch (vec_len (sm->outside_fibs))
327 s->out2in.fib_index = sm->outside_fib_index;
330 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
334 vec_foreach (outside_fib, sm->outside_fibs)
336 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
337 if (FIB_NODE_INDEX_INVALID != fei)
339 if (fib_entry_get_resolving_interface (fei) != ~0)
341 s->out2in.fib_index = outside_fib->fib_index;
349 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
350 s->ext_host_port = udp0->dst_port;
353 /* Add to translation hashes */
355 ctx0.thread_index = thread_index;
356 kv0.key = s->in2out.as_u64;
357 kv0.value = s - sm->per_thread_data[thread_index].sessions;
358 if (clib_bihash_add_or_overwrite_stale_8_8
359 (&sm->per_thread_data[thread_index].in2out, &kv0,
360 nat44_i2o_is_idle_session_cb, &ctx0))
361 nat_log_notice ("in2out key add failed");
363 kv0.key = s->out2in.as_u64;
364 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].out2in, &kv0,
368 nat44_o2i_is_idle_session_cb, &ctx0))
369 nat_log_notice ("out2in key add failed");
372 snat_ipfix_logging_nat44_ses_create (thread_index,
373 s->in2out.addr.as_u32,
374 s->out2in.addr.as_u32,
377 s->out2in.port, s->in2out.fib_index);
379 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
380 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
381 s->out2in.port, s->in2out.protocol);
386 #ifndef CLIB_MARCH_VARIANT
388 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
389 snat_session_key_t * p_key0)
391 icmp46_header_t *icmp0;
392 snat_session_key_t key0;
393 icmp_echo_header_t *echo0, *inner_echo0 = 0;
394 ip4_header_t *inner_ip0 = 0;
396 icmp46_header_t *inner_icmp0;
398 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
399 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
401 if (!icmp_is_error_message (icmp0))
403 key0.protocol = SNAT_PROTOCOL_ICMP;
404 key0.addr = ip0->src_address;
405 key0.port = echo0->identifier;
409 inner_ip0 = (ip4_header_t *) (echo0 + 1);
410 l4_header = ip4_next_header (inner_ip0);
411 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
412 key0.addr = inner_ip0->dst_address;
413 switch (key0.protocol)
415 case SNAT_PROTOCOL_ICMP:
416 inner_icmp0 = (icmp46_header_t *) l4_header;
417 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
418 key0.port = inner_echo0->identifier;
420 case SNAT_PROTOCOL_UDP:
421 case SNAT_PROTOCOL_TCP:
422 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
425 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
429 return -1; /* success */
433 * Get address and port values to be used for ICMP packet translation
434 * and create session if needed
436 * @param[in,out] sm NAT main
437 * @param[in,out] node NAT node runtime
438 * @param[in] thread_index thread index
439 * @param[in,out] b0 buffer containing packet to be translated
440 * @param[out] p_proto protocol used for matching
441 * @param[out] p_value address and port after NAT translation
442 * @param[out] p_dont_translate if packet should not be translated
443 * @param d optional parameter
444 * @param e optional parameter
447 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
448 u32 thread_index, vlib_buffer_t * b0,
449 ip4_header_t * ip0, u8 * p_proto,
450 snat_session_key_t * p_value,
451 u8 * p_dont_translate, void *d, void *e)
453 icmp46_header_t *icmp0;
456 snat_session_key_t key0;
457 snat_session_t *s0 = 0;
458 u8 dont_translate = 0;
459 clib_bihash_kv_8_8_t kv0, value0;
463 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
464 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
465 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
467 err = icmp_get_key (ip0, &key0);
470 b0->error = node->errors[err];
471 next0 = SNAT_IN2OUT_NEXT_DROP;
474 key0.fib_index = rx_fib_index0;
476 kv0.key = key0.as_u64;
478 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
481 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
483 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
496 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
497 ip0, SNAT_PROTOCOL_ICMP,
506 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
508 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
509 next0 = SNAT_IN2OUT_NEXT_DROP;
513 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
514 thread_index, vlib_time_now (sm->vlib_main));
516 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
527 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
528 icmp0->type != ICMP4_echo_reply &&
529 !icmp_is_error_message (icmp0)))
531 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
532 next0 = SNAT_IN2OUT_NEXT_DROP;
536 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
541 *p_proto = key0.protocol;
543 *p_value = s0->out2in;
544 *p_dont_translate = dont_translate;
546 *(snat_session_t **) d = s0;
551 #ifndef CLIB_MARCH_VARIANT
553 * Get address and port values to be used for ICMP packet translation
555 * @param[in] sm NAT main
556 * @param[in,out] node NAT node runtime
557 * @param[in] thread_index thread index
558 * @param[in,out] b0 buffer containing packet to be translated
559 * @param[out] p_proto protocol used for matching
560 * @param[out] p_value address and port after NAT translation
561 * @param[out] p_dont_translate if packet should not be translated
562 * @param d optional parameter
563 * @param e optional parameter
566 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
567 u32 thread_index, vlib_buffer_t * b0,
568 ip4_header_t * ip0, u8 * p_proto,
569 snat_session_key_t * p_value,
570 u8 * p_dont_translate, void *d, void *e)
572 icmp46_header_t *icmp0;
575 snat_session_key_t key0;
576 snat_session_key_t sm0;
577 u8 dont_translate = 0;
582 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
583 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
584 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
586 err = icmp_get_key (ip0, &key0);
589 b0->error = node->errors[err];
590 next0 = SNAT_IN2OUT_NEXT_DROP;
593 key0.fib_index = rx_fib_index0;
595 if (snat_static_mapping_match
596 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
598 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
606 if (icmp_is_error_message (icmp0))
608 next0 = SNAT_IN2OUT_NEXT_DROP;
612 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
613 next0 = SNAT_IN2OUT_NEXT_DROP;
617 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
618 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
619 !icmp_is_error_message (icmp0)))
621 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
622 next0 = SNAT_IN2OUT_NEXT_DROP;
629 *p_proto = key0.protocol;
630 *p_dont_translate = dont_translate;
635 #ifndef CLIB_MARCH_VARIANT
637 icmp_in2out (snat_main_t * sm,
640 icmp46_header_t * icmp0,
643 vlib_node_runtime_t * node,
644 u32 next0, u32 thread_index, void *d, void *e)
646 snat_session_key_t sm0;
648 icmp_echo_header_t *echo0, *inner_echo0 = 0;
649 ip4_header_t *inner_ip0;
651 icmp46_header_t *inner_icmp0;
653 u32 new_addr0, old_addr0;
654 u16 old_id0, new_id0;
655 u16 old_checksum0, new_checksum0;
660 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
662 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
663 &protocol, &sm0, &dont_translate, d,
667 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
670 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
672 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
674 vlib_buffer_get_current (b0),
675 ntohs (ip0->length) -
676 ip4_header_bytes (ip0), 0);
677 checksum0 = ~ip_csum_fold (sum0);
678 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
680 next0 = SNAT_IN2OUT_NEXT_DROP;
685 old_addr0 = ip0->src_address.as_u32;
686 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
688 sum0 = ip0->checksum;
689 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
690 src_address /* changed member */ );
691 ip0->checksum = ip_csum_fold (sum0);
693 if (icmp0->checksum == 0)
694 icmp0->checksum = 0xffff;
696 if (!icmp_is_error_message (icmp0))
699 if (PREDICT_FALSE (new_id0 != echo0->identifier))
701 old_id0 = echo0->identifier;
703 echo0->identifier = new_id0;
705 sum0 = icmp0->checksum;
706 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
708 icmp0->checksum = ip_csum_fold (sum0);
713 inner_ip0 = (ip4_header_t *) (echo0 + 1);
714 l4_header = ip4_next_header (inner_ip0);
716 if (!ip4_header_checksum_is_valid (inner_ip0))
718 next0 = SNAT_IN2OUT_NEXT_DROP;
722 /* update inner destination IP address */
723 old_addr0 = inner_ip0->dst_address.as_u32;
724 inner_ip0->dst_address = sm0.addr;
725 new_addr0 = inner_ip0->dst_address.as_u32;
726 sum0 = icmp0->checksum;
727 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
728 dst_address /* changed member */ );
729 icmp0->checksum = ip_csum_fold (sum0);
731 /* update inner IP header checksum */
732 old_checksum0 = inner_ip0->checksum;
733 sum0 = inner_ip0->checksum;
734 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
735 dst_address /* changed member */ );
736 inner_ip0->checksum = ip_csum_fold (sum0);
737 new_checksum0 = inner_ip0->checksum;
738 sum0 = icmp0->checksum;
739 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
741 icmp0->checksum = ip_csum_fold (sum0);
745 case SNAT_PROTOCOL_ICMP:
746 inner_icmp0 = (icmp46_header_t *) l4_header;
747 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
749 old_id0 = inner_echo0->identifier;
751 inner_echo0->identifier = new_id0;
753 sum0 = icmp0->checksum;
754 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
756 icmp0->checksum = ip_csum_fold (sum0);
758 case SNAT_PROTOCOL_UDP:
759 case SNAT_PROTOCOL_TCP:
760 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
762 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
764 sum0 = icmp0->checksum;
765 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
767 icmp0->checksum = ip_csum_fold (sum0);
774 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
776 if (sm->deterministic ||
777 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
778 sm->endpoint_dependent))
779 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
788 icmp_in2out_slow_path (snat_main_t * sm,
791 icmp46_header_t * icmp0,
794 vlib_node_runtime_t * node,
796 f64 now, u32 thread_index, snat_session_t ** p_s0)
798 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
799 next0, thread_index, p_s0, 0);
800 snat_session_t *s0 = *p_s0;
801 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
804 nat44_session_update_counters (s0, now,
805 vlib_buffer_length_in_chain
806 (sm->vlib_main, b0));
807 /* Per-user LRU list maintenance */
808 nat44_session_update_lru (sm, s0, thread_index);
814 nat_in2out_sm_unknown_proto (snat_main_t * sm,
816 ip4_header_t * ip, u32 rx_fib_index)
818 clib_bihash_kv_8_8_t kv, value;
819 snat_static_mapping_t *m;
820 snat_session_key_t m_key;
821 u32 old_addr, new_addr;
824 m_key.addr = ip->src_address;
827 m_key.fib_index = rx_fib_index;
828 kv.key = m_key.as_u64;
829 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
832 m = pool_elt_at_index (sm->static_mappings, value.value);
834 old_addr = ip->src_address.as_u32;
835 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
837 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
838 ip->checksum = ip_csum_fold (sum);
842 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
844 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
845 nat_hairpinning_sm_unknown_proto (sm, b, ip);
852 snat_in2out_node_fn_inline (vlib_main_t * vm,
853 vlib_node_runtime_t * node,
854 vlib_frame_t * frame, int is_slow_path,
855 int is_output_feature)
857 u32 n_left_from, *from, *to_next;
858 snat_in2out_next_t next_index;
859 u32 pkts_processed = 0;
860 snat_main_t *sm = &snat_main;
861 f64 now = vlib_time_now (vm);
862 u32 stats_node_index;
863 u32 thread_index = vm->thread_index;
864 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
867 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
868 sm->in2out_node_index;
870 from = vlib_frame_vector_args (frame);
871 n_left_from = frame->n_vectors;
872 next_index = node->cached_next_index;
874 while (n_left_from > 0)
878 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
880 while (n_left_from >= 4 && n_left_to_next >= 2)
883 vlib_buffer_t *b0, *b1;
885 u32 sw_if_index0, sw_if_index1;
886 ip4_header_t *ip0, *ip1;
887 ip_csum_t sum0, sum1;
888 u32 new_addr0, old_addr0, new_addr1, old_addr1;
889 u16 old_port0, new_port0, old_port1, new_port1;
890 udp_header_t *udp0, *udp1;
891 tcp_header_t *tcp0, *tcp1;
892 icmp46_header_t *icmp0, *icmp1;
893 snat_session_key_t key0, key1;
894 u32 rx_fib_index0, rx_fib_index1;
896 snat_session_t *s0 = 0, *s1 = 0;
897 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
898 u32 iph_offset0 = 0, iph_offset1 = 0;
900 /* Prefetch next iteration. */
902 vlib_buffer_t *p2, *p3;
904 p2 = vlib_get_buffer (vm, from[2]);
905 p3 = vlib_get_buffer (vm, from[3]);
907 vlib_prefetch_buffer_header (p2, LOAD);
908 vlib_prefetch_buffer_header (p3, LOAD);
910 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
911 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
914 /* speculatively enqueue b0 and b1 to the current next frame */
915 to_next[0] = bi0 = from[0];
916 to_next[1] = bi1 = from[1];
922 b0 = vlib_get_buffer (vm, bi0);
923 b1 = vlib_get_buffer (vm, bi1);
925 if (is_output_feature)
926 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
928 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
931 udp0 = ip4_next_header (ip0);
932 tcp0 = (tcp_header_t *) udp0;
933 icmp0 = (icmp46_header_t *) udp0;
935 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
936 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
939 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
941 if (PREDICT_FALSE (ip0->ttl == 1))
943 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
944 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
945 ICMP4_time_exceeded_ttl_exceeded_in_transit,
947 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
951 proto0 = ip_proto_to_snat_proto (ip0->protocol);
953 /* Next configured feature, probably ip4-lookup */
956 if (PREDICT_FALSE (proto0 == ~0))
958 if (nat_in2out_sm_unknown_proto
959 (sm, b0, ip0, rx_fib_index0))
961 next0 = SNAT_IN2OUT_NEXT_DROP;
963 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
969 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
971 next0 = icmp_in2out_slow_path
972 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
973 node, next0, now, thread_index, &s0);
980 if (PREDICT_FALSE (proto0 == ~0))
982 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
986 if (ip4_is_fragment (ip0))
988 next0 = SNAT_IN2OUT_NEXT_REASS;
993 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
995 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1000 key0.addr = ip0->src_address;
1001 key0.port = udp0->src_port;
1002 key0.protocol = proto0;
1003 key0.fib_index = rx_fib_index0;
1005 kv0.key = key0.as_u64;
1008 (clib_bihash_search_8_8
1009 (&sm->per_thread_data[thread_index].in2out, &kv0,
1014 if (is_output_feature)
1016 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1029 (sm, node, sw_if_index0, ip0, proto0,
1030 rx_fib_index0, thread_index)))
1034 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1035 &s0, node, next0, thread_index, now);
1036 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1039 if (PREDICT_FALSE (!s0))
1044 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1050 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1053 b0->flags |= VNET_BUFFER_F_IS_NATED;
1055 old_addr0 = ip0->src_address.as_u32;
1056 ip0->src_address = s0->out2in.addr;
1057 new_addr0 = ip0->src_address.as_u32;
1058 if (!is_output_feature)
1059 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1061 sum0 = ip0->checksum;
1062 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1064 src_address /* changed member */ );
1065 ip0->checksum = ip_csum_fold (sum0);
1067 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1069 old_port0 = tcp0->src_port;
1070 tcp0->src_port = s0->out2in.port;
1071 new_port0 = tcp0->src_port;
1073 sum0 = tcp0->checksum;
1074 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1076 dst_address /* changed member */ );
1077 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1078 ip4_header_t /* cheat */ ,
1079 length /* changed member */ );
1080 mss_clamping (sm, tcp0, &sum0);
1081 tcp0->checksum = ip_csum_fold (sum0);
1086 old_port0 = udp0->src_port;
1087 udp0->src_port = s0->out2in.port;
1093 nat44_session_update_counters (s0, now,
1094 vlib_buffer_length_in_chain (vm,
1096 /* Per-user LRU list maintenance */
1097 nat44_session_update_lru (sm, s0, thread_index);
1100 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1101 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1103 snat_in2out_trace_t *t =
1104 vlib_add_trace (vm, node, b0, sizeof (*t));
1105 t->is_slow_path = is_slow_path;
1106 t->sw_if_index = sw_if_index0;
1107 t->next_index = next0;
1108 t->session_index = ~0;
1111 s0 - sm->per_thread_data[thread_index].sessions;
1114 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1116 if (is_output_feature)
1117 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1119 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1122 udp1 = ip4_next_header (ip1);
1123 tcp1 = (tcp_header_t *) udp1;
1124 icmp1 = (icmp46_header_t *) udp1;
1126 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1127 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1130 if (PREDICT_FALSE (ip1->ttl == 1))
1132 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1133 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1134 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1136 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1140 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1142 /* Next configured feature, probably ip4-lookup */
1145 if (PREDICT_FALSE (proto1 == ~0))
1147 if (nat_in2out_sm_unknown_proto
1148 (sm, b1, ip1, rx_fib_index1))
1150 next1 = SNAT_IN2OUT_NEXT_DROP;
1152 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1158 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1160 next1 = icmp_in2out_slow_path
1161 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1162 next1, now, thread_index, &s1);
1169 if (PREDICT_FALSE (proto1 == ~0))
1171 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1175 if (ip4_is_fragment (ip1))
1177 next1 = SNAT_IN2OUT_NEXT_REASS;
1182 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1184 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1189 key1.addr = ip1->src_address;
1190 key1.port = udp1->src_port;
1191 key1.protocol = proto1;
1192 key1.fib_index = rx_fib_index1;
1194 kv1.key = key1.as_u64;
1197 (clib_bihash_search_8_8
1198 (&sm->per_thread_data[thread_index].in2out, &kv1,
1203 if (is_output_feature)
1205 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1218 (sm, node, sw_if_index1, ip1, proto1,
1219 rx_fib_index1, thread_index)))
1223 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1224 &s1, node, next1, thread_index, now);
1225 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1228 if (PREDICT_FALSE (!s1))
1233 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1239 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1242 b1->flags |= VNET_BUFFER_F_IS_NATED;
1244 old_addr1 = ip1->src_address.as_u32;
1245 ip1->src_address = s1->out2in.addr;
1246 new_addr1 = ip1->src_address.as_u32;
1247 if (!is_output_feature)
1248 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1250 sum1 = ip1->checksum;
1251 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1253 src_address /* changed member */ );
1254 ip1->checksum = ip_csum_fold (sum1);
1256 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1258 old_port1 = tcp1->src_port;
1259 tcp1->src_port = s1->out2in.port;
1260 new_port1 = tcp1->src_port;
1262 sum1 = tcp1->checksum;
1263 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1265 dst_address /* changed member */ );
1266 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1267 ip4_header_t /* cheat */ ,
1268 length /* changed member */ );
1269 mss_clamping (sm, tcp1, &sum1);
1270 tcp1->checksum = ip_csum_fold (sum1);
1275 old_port1 = udp1->src_port;
1276 udp1->src_port = s1->out2in.port;
1282 nat44_session_update_counters (s1, now,
1283 vlib_buffer_length_in_chain (vm,
1285 /* Per-user LRU list maintenance */
1286 nat44_session_update_lru (sm, s1, thread_index);
1289 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1290 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1292 snat_in2out_trace_t *t =
1293 vlib_add_trace (vm, node, b1, sizeof (*t));
1294 t->sw_if_index = sw_if_index1;
1295 t->next_index = next1;
1296 t->session_index = ~0;
1299 s1 - sm->per_thread_data[thread_index].sessions;
1302 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1304 /* verify speculative enqueues, maybe switch current next frame */
1305 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1306 to_next, n_left_to_next,
1307 bi0, bi1, next0, next1);
1310 while (n_left_from > 0 && n_left_to_next > 0)
1318 u32 new_addr0, old_addr0;
1319 u16 old_port0, new_port0;
1322 icmp46_header_t *icmp0;
1323 snat_session_key_t key0;
1326 snat_session_t *s0 = 0;
1327 clib_bihash_kv_8_8_t kv0, value0;
1328 u32 iph_offset0 = 0;
1330 /* speculatively enqueue b0 to the current next frame */
1336 n_left_to_next -= 1;
1338 b0 = vlib_get_buffer (vm, bi0);
1339 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1341 if (is_output_feature)
1342 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1344 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1347 udp0 = ip4_next_header (ip0);
1348 tcp0 = (tcp_header_t *) udp0;
1349 icmp0 = (icmp46_header_t *) udp0;
1351 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1352 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1355 if (PREDICT_FALSE (ip0->ttl == 1))
1357 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1358 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1359 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1361 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1365 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1367 /* Next configured feature, probably ip4-lookup */
1370 if (PREDICT_FALSE (proto0 == ~0))
1372 if (nat_in2out_sm_unknown_proto
1373 (sm, b0, ip0, rx_fib_index0))
1375 next0 = SNAT_IN2OUT_NEXT_DROP;
1377 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1383 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1385 next0 = icmp_in2out_slow_path
1386 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1387 next0, now, thread_index, &s0);
1394 if (PREDICT_FALSE (proto0 == ~0))
1396 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1400 if (ip4_is_fragment (ip0))
1402 next0 = SNAT_IN2OUT_NEXT_REASS;
1407 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1409 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1414 key0.addr = ip0->src_address;
1415 key0.port = udp0->src_port;
1416 key0.protocol = proto0;
1417 key0.fib_index = rx_fib_index0;
1419 kv0.key = key0.as_u64;
1421 if (clib_bihash_search_8_8
1422 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1426 if (is_output_feature)
1428 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1441 (sm, node, sw_if_index0, ip0, proto0,
1442 rx_fib_index0, thread_index)))
1446 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1447 &s0, node, next0, thread_index, now);
1449 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1452 if (PREDICT_FALSE (!s0))
1457 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1463 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1466 b0->flags |= VNET_BUFFER_F_IS_NATED;
1468 old_addr0 = ip0->src_address.as_u32;
1469 ip0->src_address = s0->out2in.addr;
1470 new_addr0 = ip0->src_address.as_u32;
1471 if (!is_output_feature)
1472 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1474 sum0 = ip0->checksum;
1475 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1477 src_address /* changed member */ );
1478 ip0->checksum = ip_csum_fold (sum0);
1480 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1482 old_port0 = tcp0->src_port;
1483 tcp0->src_port = s0->out2in.port;
1484 new_port0 = tcp0->src_port;
1486 sum0 = tcp0->checksum;
1487 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1489 dst_address /* changed member */ );
1490 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1491 ip4_header_t /* cheat */ ,
1492 length /* changed member */ );
1493 mss_clamping (sm, tcp0, &sum0);
1494 tcp0->checksum = ip_csum_fold (sum0);
1499 old_port0 = udp0->src_port;
1500 udp0->src_port = s0->out2in.port;
1506 nat44_session_update_counters (s0, now,
1507 vlib_buffer_length_in_chain (vm,
1509 /* Per-user LRU list maintenance */
1510 nat44_session_update_lru (sm, s0, thread_index);
1513 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1514 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1516 snat_in2out_trace_t *t =
1517 vlib_add_trace (vm, node, b0, sizeof (*t));
1518 t->is_slow_path = is_slow_path;
1519 t->sw_if_index = sw_if_index0;
1520 t->next_index = next0;
1521 t->session_index = ~0;
1524 s0 - sm->per_thread_data[thread_index].sessions;
1527 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1529 /* verify speculative enqueue, maybe switch current next frame */
1530 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1531 to_next, n_left_to_next,
1535 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1538 vlib_node_increment_counter (vm, stats_node_index,
1539 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1541 vlib_node_increment_counter (vm, stats_node_index,
1542 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1543 vlib_node_increment_counter (vm, stats_node_index,
1544 SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1545 vlib_node_increment_counter (vm, stats_node_index,
1546 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1547 vlib_node_increment_counter (vm, stats_node_index,
1548 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1550 vlib_node_increment_counter (vm, stats_node_index,
1551 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1553 return frame->n_vectors;
1556 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1557 vlib_node_runtime_t * node,
1558 vlib_frame_t * frame)
1560 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1565 VLIB_REGISTER_NODE (snat_in2out_node) = {
1566 .name = "nat44-in2out",
1567 .vector_size = sizeof (u32),
1568 .format_trace = format_snat_in2out_trace,
1569 .type = VLIB_NODE_TYPE_INTERNAL,
1571 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1572 .error_strings = snat_in2out_error_strings,
1574 .runtime_data_bytes = sizeof (snat_runtime_t),
1576 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1578 /* edit / add dispositions here */
1580 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1581 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1582 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1583 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1584 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1589 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1590 vlib_node_runtime_t * node,
1591 vlib_frame_t * frame)
1593 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1598 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1599 .name = "nat44-in2out-output",
1600 .vector_size = sizeof (u32),
1601 .format_trace = format_snat_in2out_trace,
1602 .type = VLIB_NODE_TYPE_INTERNAL,
1604 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1605 .error_strings = snat_in2out_error_strings,
1607 .runtime_data_bytes = sizeof (snat_runtime_t),
1609 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1611 /* edit / add dispositions here */
1613 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1614 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1615 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1616 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1617 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1622 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1623 vlib_node_runtime_t * node,
1624 vlib_frame_t * frame)
1626 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1631 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1632 .name = "nat44-in2out-slowpath",
1633 .vector_size = sizeof (u32),
1634 .format_trace = format_snat_in2out_trace,
1635 .type = VLIB_NODE_TYPE_INTERNAL,
1637 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1638 .error_strings = snat_in2out_error_strings,
1640 .runtime_data_bytes = sizeof (snat_runtime_t),
1642 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1644 /* edit / add dispositions here */
1646 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1647 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1648 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1649 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1650 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1655 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1656 vlib_node_runtime_t * node,
1657 vlib_frame_t * frame)
1659 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1664 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1665 .name = "nat44-in2out-output-slowpath",
1666 .vector_size = sizeof (u32),
1667 .format_trace = format_snat_in2out_trace,
1668 .type = VLIB_NODE_TYPE_INTERNAL,
1670 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1671 .error_strings = snat_in2out_error_strings,
1673 .runtime_data_bytes = sizeof (snat_runtime_t),
1675 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1677 /* edit / add dispositions here */
1679 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1680 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1681 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1682 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1683 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1688 VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm,
1689 vlib_node_runtime_t * node,
1690 vlib_frame_t * frame)
1692 u32 n_left_from, *from, *to_next;
1693 snat_in2out_next_t next_index;
1694 u32 pkts_processed = 0, cached_fragments = 0;
1695 snat_main_t *sm = &snat_main;
1696 f64 now = vlib_time_now (vm);
1697 u32 thread_index = vm->thread_index;
1698 snat_main_per_thread_data_t *per_thread_data =
1699 &sm->per_thread_data[thread_index];
1700 u32 *fragments_to_drop = 0;
1701 u32 *fragments_to_loopback = 0;
1703 from = vlib_frame_vector_args (frame);
1704 n_left_from = frame->n_vectors;
1705 next_index = node->cached_next_index;
1707 while (n_left_from > 0)
1711 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1713 while (n_left_from > 0 && n_left_to_next > 0)
1715 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1720 nat_reass_ip4_t *reass0;
1723 icmp46_header_t *icmp0;
1724 snat_session_key_t key0;
1725 clib_bihash_kv_8_8_t kv0, value0;
1726 snat_session_t *s0 = 0;
1727 u16 old_port0, new_port0;
1730 /* speculatively enqueue b0 to the current next frame */
1736 n_left_to_next -= 1;
1738 b0 = vlib_get_buffer (vm, bi0);
1739 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1741 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1743 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1746 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1748 next0 = SNAT_IN2OUT_NEXT_DROP;
1749 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1753 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1754 udp0 = ip4_next_header (ip0);
1755 tcp0 = (tcp_header_t *) udp0;
1756 icmp0 = (icmp46_header_t *) udp0;
1757 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1759 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1763 1, &fragments_to_drop);
1765 if (PREDICT_FALSE (!reass0))
1767 next0 = SNAT_IN2OUT_NEXT_DROP;
1768 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1769 nat_log_notice ("maximum reassemblies exceeded");
1773 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1775 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1777 next0 = icmp_in2out_slow_path
1778 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1779 next0, now, thread_index, &s0);
1781 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1784 reass0->sess_index = s0 - per_thread_data->sessions;
1786 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1787 nat_ip4_reass_get_frags (reass0,
1788 &fragments_to_loopback);
1794 key0.addr = ip0->src_address;
1795 key0.port = udp0->src_port;
1796 key0.protocol = proto0;
1797 key0.fib_index = rx_fib_index0;
1798 kv0.key = key0.as_u64;
1800 if (clib_bihash_search_8_8
1801 (&per_thread_data->in2out, &kv0, &value0))
1805 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1809 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1810 &s0, node, next0, thread_index, now);
1812 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1815 if (PREDICT_FALSE (!s0))
1818 reass0->sess_index = s0 - per_thread_data->sessions;
1822 s0 = pool_elt_at_index (per_thread_data->sessions,
1824 reass0->sess_index = value0.value;
1826 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1830 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1832 if (nat_ip4_reass_add_fragment
1833 (thread_index, reass0, bi0, &fragments_to_drop))
1835 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1837 ("maximum fragments per reassembly exceeded");
1838 next0 = SNAT_IN2OUT_NEXT_DROP;
1844 s0 = pool_elt_at_index (per_thread_data->sessions,
1845 reass0->sess_index);
1848 old_addr0 = ip0->src_address.as_u32;
1849 ip0->src_address = s0->out2in.addr;
1850 new_addr0 = ip0->src_address.as_u32;
1851 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1853 sum0 = ip0->checksum;
1854 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1856 src_address /* changed member */ );
1857 ip0->checksum = ip_csum_fold (sum0);
1859 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1861 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1863 old_port0 = tcp0->src_port;
1864 tcp0->src_port = s0->out2in.port;
1865 new_port0 = tcp0->src_port;
1867 sum0 = tcp0->checksum;
1868 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1870 dst_address /* changed member */ );
1871 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1872 ip4_header_t /* cheat */ ,
1873 length /* changed member */ );
1874 tcp0->checksum = ip_csum_fold (sum0);
1878 old_port0 = udp0->src_port;
1879 udp0->src_port = s0->out2in.port;
1885 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1886 s0->ext_host_port, proto0, 0);
1889 nat44_session_update_counters (s0, now,
1890 vlib_buffer_length_in_chain (vm,
1892 /* Per-user LRU list maintenance */
1893 nat44_session_update_lru (sm, s0, thread_index);
1896 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1897 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1899 nat44_reass_trace_t *t =
1900 vlib_add_trace (vm, node, b0, sizeof (*t));
1901 t->cached = cached0;
1902 t->sw_if_index = sw_if_index0;
1903 t->next_index = next0;
1914 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1916 /* verify speculative enqueue, maybe switch current next frame */
1917 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1918 to_next, n_left_to_next,
1922 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1924 from = vlib_frame_vector_args (frame);
1925 u32 len = vec_len (fragments_to_loopback);
1926 if (len <= VLIB_FRAME_SIZE)
1928 clib_memcpy_fast (from, fragments_to_loopback,
1929 sizeof (u32) * len);
1931 vec_reset_length (fragments_to_loopback);
1935 clib_memcpy_fast (from, fragments_to_loopback +
1936 (len - VLIB_FRAME_SIZE),
1937 sizeof (u32) * VLIB_FRAME_SIZE);
1938 n_left_from = VLIB_FRAME_SIZE;
1939 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1944 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1947 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
1948 SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1950 vlib_node_increment_counter (vm, sm->in2out_reass_node_index,
1951 SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
1954 nat_send_all_to_node (vm, fragments_to_drop, node,
1955 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1956 SNAT_IN2OUT_NEXT_DROP);
1958 vec_free (fragments_to_drop);
1959 vec_free (fragments_to_loopback);
1960 return frame->n_vectors;
1964 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1965 .name = "nat44-in2out-reass",
1966 .vector_size = sizeof (u32),
1967 .format_trace = format_nat44_reass_trace,
1968 .type = VLIB_NODE_TYPE_INTERNAL,
1970 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1971 .error_strings = snat_in2out_error_strings,
1973 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1975 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1976 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1977 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1978 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1979 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1984 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1985 vlib_node_runtime_t * node,
1986 vlib_frame_t * frame)
1988 u32 n_left_from, *from, *to_next;
1989 snat_in2out_next_t next_index;
1990 u32 pkts_processed = 0;
1991 snat_main_t *sm = &snat_main;
1992 u32 stats_node_index;
1994 stats_node_index = sm->in2out_fast_node_index;
1996 from = vlib_frame_vector_args (frame);
1997 n_left_from = frame->n_vectors;
1998 next_index = node->cached_next_index;
2000 while (n_left_from > 0)
2004 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2006 while (n_left_from > 0 && n_left_to_next > 0)
2014 u32 new_addr0, old_addr0;
2015 u16 old_port0, new_port0;
2018 icmp46_header_t *icmp0;
2019 snat_session_key_t key0, sm0;
2023 /* speculatively enqueue b0 to the current next frame */
2029 n_left_to_next -= 1;
2031 b0 = vlib_get_buffer (vm, bi0);
2032 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2034 ip0 = vlib_buffer_get_current (b0);
2035 udp0 = ip4_next_header (ip0);
2036 tcp0 = (tcp_header_t *) udp0;
2037 icmp0 = (icmp46_header_t *) udp0;
2039 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2041 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2043 if (PREDICT_FALSE (ip0->ttl == 1))
2045 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2046 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2047 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2049 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2053 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2055 if (PREDICT_FALSE (proto0 == ~0))
2058 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2060 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2061 rx_fib_index0, node, next0, ~0, 0, 0);
2065 key0.addr = ip0->src_address;
2066 key0.protocol = proto0;
2067 key0.port = udp0->src_port;
2068 key0.fib_index = rx_fib_index0;
2070 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2072 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2073 next0 = SNAT_IN2OUT_NEXT_DROP;
2077 new_addr0 = sm0.addr.as_u32;
2078 new_port0 = sm0.port;
2079 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2080 old_addr0 = ip0->src_address.as_u32;
2081 ip0->src_address.as_u32 = new_addr0;
2083 sum0 = ip0->checksum;
2084 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2086 src_address /* changed member */ );
2087 ip0->checksum = ip_csum_fold (sum0);
2089 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2091 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2093 old_port0 = tcp0->src_port;
2094 tcp0->src_port = new_port0;
2096 sum0 = tcp0->checksum;
2097 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2099 dst_address /* changed member */ );
2100 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2101 ip4_header_t /* cheat */ ,
2102 length /* changed member */ );
2103 mss_clamping (sm, tcp0, &sum0);
2104 tcp0->checksum = ip_csum_fold (sum0);
2108 old_port0 = udp0->src_port;
2109 udp0->src_port = new_port0;
2115 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2117 sum0 = tcp0->checksum;
2118 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2120 dst_address /* changed member */ );
2121 mss_clamping (sm, tcp0, &sum0);
2122 tcp0->checksum = ip_csum_fold (sum0);
2127 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2130 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2131 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2133 snat_in2out_trace_t *t =
2134 vlib_add_trace (vm, node, b0, sizeof (*t));
2135 t->sw_if_index = sw_if_index0;
2136 t->next_index = next0;
2139 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2141 /* verify speculative enqueue, maybe switch current next frame */
2142 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2143 to_next, n_left_to_next,
2147 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2150 vlib_node_increment_counter (vm, stats_node_index,
2151 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2153 return frame->n_vectors;
2158 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2159 .name = "nat44-in2out-fast",
2160 .vector_size = sizeof (u32),
2161 .format_trace = format_snat_in2out_fast_trace,
2162 .type = VLIB_NODE_TYPE_INTERNAL,
2164 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2165 .error_strings = snat_in2out_error_strings,
2167 .runtime_data_bytes = sizeof (snat_runtime_t),
2169 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2171 /* edit / add dispositions here */
2173 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2174 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2175 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2176 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2177 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2183 * fd.io coding-style-patch-verification: ON
2186 * eval: (c-set-style "gnu")