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 vlib_node_registration_t snat_in2out_node;
76 vlib_node_registration_t snat_in2out_slowpath_node;
77 vlib_node_registration_t snat_in2out_fast_node;
78 vlib_node_registration_t snat_in2out_output_node;
79 vlib_node_registration_t snat_in2out_output_slowpath_node;
80 vlib_node_registration_t nat44_in2out_reass_node;
82 #define foreach_snat_in2out_error \
83 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
84 _(IN2OUT_PACKETS, "Good in2out packets processed") \
85 _(OUT_OF_PORTS, "Out of ports") \
86 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
87 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
88 _(NO_TRANSLATION, "No translation") \
89 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
90 _(DROP_FRAGMENT, "Drop fragment") \
91 _(MAX_REASS, "Maximum reassemblies exceeded") \
92 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
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))
195 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
197 snat_main_t *sm = &snat_main;
198 nat44_is_idle_session_ctx_t *ctx = arg;
200 u64 sess_timeout_time;
201 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
203 clib_bihash_kv_8_8_t s_kv;
205 s = pool_elt_at_index (tsm->sessions, kv->value);
206 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
207 if (ctx->now >= sess_timeout_time)
209 s_kv.key = s->out2in.as_u64;
210 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
211 nat_log_warn ("out2in key del failed");
213 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
214 s->out2in.addr.as_u32,
218 s->in2out.fib_index);
220 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
221 &s->in2out.addr, s->in2out.port,
222 &s->out2in.addr, s->out2in.port,
225 if (!snat_is_session_static (s))
226 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
229 nat44_delete_session (sm, s, ctx->thread_index);
237 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
240 snat_session_key_t * key0,
241 snat_session_t ** sessionp,
242 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
245 snat_session_t *s = 0;
246 clib_bihash_kv_8_8_t kv0;
247 snat_session_key_t key1;
248 udp_header_t *udp0 = ip4_next_header (ip0);
250 nat_outside_fib_t *outside_fib;
251 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
254 .fp_proto = FIB_PROTOCOL_IP4,
257 .ip4.as_u32 = ip0->dst_address.as_u32,
260 nat44_is_idle_session_ctx_t ctx0;
262 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
264 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
265 nat_ipfix_logging_max_sessions (sm->max_translations);
266 nat_log_notice ("maximum sessions exceeded");
267 return SNAT_IN2OUT_NEXT_DROP;
270 key1.protocol = key0->protocol;
272 /* First try to match static mapping by local address and port */
273 if (snat_static_mapping_match
274 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
276 /* Try to create dynamic translation */
277 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
281 [thread_index].snat_thread_index))
283 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
284 return SNAT_IN2OUT_NEXT_DROP;
289 if (PREDICT_FALSE (identity_nat))
298 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
302 nat_log_warn ("create NAT user failed");
303 return SNAT_IN2OUT_NEXT_DROP;
306 s = nat_session_alloc_or_recycle (sm, u, thread_index);
309 nat44_delete_user_with_no_session (sm, u, thread_index);
310 nat_log_warn ("create NAT session failed");
311 return SNAT_IN2OUT_NEXT_DROP;
315 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
316 user_session_increment (sm, u, is_sm);
319 s->out2in.protocol = key0->protocol;
320 s->out2in.fib_index = sm->outside_fib_index;
321 switch (vec_len (sm->outside_fibs))
324 s->out2in.fib_index = sm->outside_fib_index;
327 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
331 vec_foreach (outside_fib, sm->outside_fibs)
333 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
334 if (FIB_NODE_INDEX_INVALID != fei)
336 if (fib_entry_get_resolving_interface (fei) != ~0)
338 s->out2in.fib_index = outside_fib->fib_index;
346 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
347 s->ext_host_port = udp0->dst_port;
350 /* Add to translation hashes */
352 ctx0.thread_index = thread_index;
353 kv0.key = s->in2out.as_u64;
354 kv0.value = s - sm->per_thread_data[thread_index].sessions;
355 if (clib_bihash_add_or_overwrite_stale_8_8
356 (&sm->per_thread_data[thread_index].in2out, &kv0,
357 nat44_i2o_is_idle_session_cb, &ctx0))
358 nat_log_notice ("in2out key add failed");
360 kv0.key = s->out2in.as_u64;
361 kv0.value = s - sm->per_thread_data[thread_index].sessions;
363 if (clib_bihash_add_or_overwrite_stale_8_8
364 (&sm->per_thread_data[thread_index].out2in, &kv0,
365 nat44_o2i_is_idle_session_cb, &ctx0))
366 nat_log_notice ("out2in key add failed");
369 snat_ipfix_logging_nat44_ses_create (s->in2out.addr.as_u32,
370 s->out2in.addr.as_u32,
373 s->out2in.port, s->in2out.fib_index);
375 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
376 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
377 s->out2in.port, s->in2out.protocol);
383 snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
384 snat_session_key_t * p_key0)
386 icmp46_header_t *icmp0;
387 snat_session_key_t key0;
388 icmp_echo_header_t *echo0, *inner_echo0 = 0;
389 ip4_header_t *inner_ip0 = 0;
391 icmp46_header_t *inner_icmp0;
393 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
394 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
396 if (!icmp_is_error_message (icmp0))
398 key0.protocol = SNAT_PROTOCOL_ICMP;
399 key0.addr = ip0->src_address;
400 key0.port = echo0->identifier;
404 inner_ip0 = (ip4_header_t *) (echo0 + 1);
405 l4_header = ip4_next_header (inner_ip0);
406 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
407 key0.addr = inner_ip0->dst_address;
408 switch (key0.protocol)
410 case SNAT_PROTOCOL_ICMP:
411 inner_icmp0 = (icmp46_header_t *) l4_header;
412 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
413 key0.port = inner_echo0->identifier;
415 case SNAT_PROTOCOL_UDP:
416 case SNAT_PROTOCOL_TCP:
417 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
420 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
424 return -1; /* success */
428 * Get address and port values to be used for ICMP packet translation
429 * and create session if needed
431 * @param[in,out] sm NAT main
432 * @param[in,out] node NAT node runtime
433 * @param[in] thread_index thread index
434 * @param[in,out] b0 buffer containing packet to be translated
435 * @param[out] p_proto protocol used for matching
436 * @param[out] p_value address and port after NAT translation
437 * @param[out] p_dont_translate if packet should not be translated
438 * @param d optional parameter
439 * @param e optional parameter
442 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
443 u32 thread_index, vlib_buffer_t * b0,
444 ip4_header_t * ip0, u8 * p_proto,
445 snat_session_key_t * p_value,
446 u8 * p_dont_translate, void *d, void *e)
448 icmp46_header_t *icmp0;
451 snat_session_key_t key0;
452 snat_session_t *s0 = 0;
453 u8 dont_translate = 0;
454 clib_bihash_kv_8_8_t kv0, value0;
458 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
459 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
460 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
462 err = icmp_get_key (ip0, &key0);
465 b0->error = node->errors[err];
466 next0 = SNAT_IN2OUT_NEXT_DROP;
469 key0.fib_index = rx_fib_index0;
471 kv0.key = key0.as_u64;
473 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
476 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
478 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
491 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
492 ip0, SNAT_PROTOCOL_ICMP,
501 if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
503 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
504 next0 = SNAT_IN2OUT_NEXT_DROP;
508 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
509 thread_index, vlib_time_now (sm->vlib_main));
511 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
522 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
523 icmp0->type != ICMP4_echo_reply &&
524 !icmp_is_error_message (icmp0)))
526 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
527 next0 = SNAT_IN2OUT_NEXT_DROP;
531 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
536 *p_proto = key0.protocol;
538 *p_value = s0->out2in;
539 *p_dont_translate = dont_translate;
541 *(snat_session_t **) d = s0;
546 * Get address and port values to be used for ICMP packet translation
548 * @param[in] sm NAT main
549 * @param[in,out] node NAT node runtime
550 * @param[in] thread_index thread index
551 * @param[in,out] b0 buffer containing packet to be translated
552 * @param[out] p_proto protocol used for matching
553 * @param[out] p_value address and port after NAT translation
554 * @param[out] p_dont_translate if packet should not be translated
555 * @param d optional parameter
556 * @param e optional parameter
559 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
560 u32 thread_index, vlib_buffer_t * b0,
561 ip4_header_t * ip0, u8 * p_proto,
562 snat_session_key_t * p_value,
563 u8 * p_dont_translate, void *d, void *e)
565 icmp46_header_t *icmp0;
568 snat_session_key_t key0;
569 snat_session_key_t sm0;
570 u8 dont_translate = 0;
575 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
576 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
577 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
579 err = icmp_get_key (ip0, &key0);
582 b0->error = node->errors[err];
583 next0 = SNAT_IN2OUT_NEXT_DROP;
586 key0.fib_index = rx_fib_index0;
588 if (snat_static_mapping_match
589 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
591 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
599 if (icmp_is_error_message (icmp0))
601 next0 = SNAT_IN2OUT_NEXT_DROP;
605 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
606 next0 = SNAT_IN2OUT_NEXT_DROP;
610 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
611 (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
612 !icmp_is_error_message (icmp0)))
614 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
615 next0 = SNAT_IN2OUT_NEXT_DROP;
622 *p_proto = key0.protocol;
623 *p_dont_translate = dont_translate;
628 icmp_in2out (snat_main_t * sm,
631 icmp46_header_t * icmp0,
634 vlib_node_runtime_t * node,
635 u32 next0, u32 thread_index, void *d, void *e)
637 snat_session_key_t sm0;
639 icmp_echo_header_t *echo0, *inner_echo0 = 0;
640 ip4_header_t *inner_ip0;
642 icmp46_header_t *inner_icmp0;
644 u32 new_addr0, old_addr0;
645 u16 old_id0, new_id0;
646 u16 old_checksum0, new_checksum0;
651 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
653 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
654 &protocol, &sm0, &dont_translate, d,
658 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
661 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
663 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
665 vlib_buffer_get_current (b0),
666 ntohs (ip0->length) -
667 ip4_header_bytes (ip0), 0);
668 checksum0 = ~ip_csum_fold (sum0);
669 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
671 next0 = SNAT_IN2OUT_NEXT_DROP;
676 old_addr0 = ip0->src_address.as_u32;
677 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
679 sum0 = ip0->checksum;
680 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
681 src_address /* changed member */ );
682 ip0->checksum = ip_csum_fold (sum0);
684 if (icmp0->checksum == 0)
685 icmp0->checksum = 0xffff;
687 if (!icmp_is_error_message (icmp0))
690 if (PREDICT_FALSE (new_id0 != echo0->identifier))
692 old_id0 = echo0->identifier;
694 echo0->identifier = new_id0;
696 sum0 = icmp0->checksum;
697 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
699 icmp0->checksum = ip_csum_fold (sum0);
704 inner_ip0 = (ip4_header_t *) (echo0 + 1);
705 l4_header = ip4_next_header (inner_ip0);
707 if (!ip4_header_checksum_is_valid (inner_ip0))
709 next0 = SNAT_IN2OUT_NEXT_DROP;
713 /* update inner destination IP address */
714 old_addr0 = inner_ip0->dst_address.as_u32;
715 inner_ip0->dst_address = sm0.addr;
716 new_addr0 = inner_ip0->dst_address.as_u32;
717 sum0 = icmp0->checksum;
718 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
719 dst_address /* changed member */ );
720 icmp0->checksum = ip_csum_fold (sum0);
722 /* update inner IP header checksum */
723 old_checksum0 = inner_ip0->checksum;
724 sum0 = inner_ip0->checksum;
725 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
726 dst_address /* changed member */ );
727 inner_ip0->checksum = ip_csum_fold (sum0);
728 new_checksum0 = inner_ip0->checksum;
729 sum0 = icmp0->checksum;
730 sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
732 icmp0->checksum = ip_csum_fold (sum0);
736 case SNAT_PROTOCOL_ICMP:
737 inner_icmp0 = (icmp46_header_t *) l4_header;
738 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
740 old_id0 = inner_echo0->identifier;
742 inner_echo0->identifier = new_id0;
744 sum0 = icmp0->checksum;
745 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
747 icmp0->checksum = ip_csum_fold (sum0);
749 case SNAT_PROTOCOL_UDP:
750 case SNAT_PROTOCOL_TCP:
751 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
753 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
755 sum0 = icmp0->checksum;
756 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
758 icmp0->checksum = ip_csum_fold (sum0);
765 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
767 if (sm->deterministic ||
768 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
769 sm->endpoint_dependent))
770 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
778 icmp_in2out_slow_path (snat_main_t * sm,
781 icmp46_header_t * icmp0,
784 vlib_node_runtime_t * node,
786 f64 now, u32 thread_index, snat_session_t ** p_s0)
788 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
789 next0, thread_index, p_s0, 0);
790 snat_session_t *s0 = *p_s0;
791 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
794 nat44_session_update_counters (s0, now,
795 vlib_buffer_length_in_chain
796 (sm->vlib_main, b0));
797 /* Per-user LRU list maintenance */
798 nat44_session_update_lru (sm, s0, thread_index);
804 nat_in2out_sm_unknown_proto (snat_main_t * sm,
806 ip4_header_t * ip, u32 rx_fib_index)
808 clib_bihash_kv_8_8_t kv, value;
809 snat_static_mapping_t *m;
810 snat_session_key_t m_key;
811 u32 old_addr, new_addr;
814 m_key.addr = ip->src_address;
817 m_key.fib_index = rx_fib_index;
818 kv.key = m_key.as_u64;
819 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
822 m = pool_elt_at_index (sm->static_mappings, value.value);
824 old_addr = ip->src_address.as_u32;
825 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
827 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
828 ip->checksum = ip_csum_fold (sum);
832 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
834 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
835 nat_hairpinning_sm_unknown_proto (sm, b, ip);
842 snat_in2out_node_fn_inline (vlib_main_t * vm,
843 vlib_node_runtime_t * node,
844 vlib_frame_t * frame, int is_slow_path,
845 int is_output_feature)
847 u32 n_left_from, *from, *to_next;
848 snat_in2out_next_t next_index;
849 u32 pkts_processed = 0;
850 snat_main_t *sm = &snat_main;
851 f64 now = vlib_time_now (vm);
852 u32 stats_node_index;
853 u32 thread_index = vm->thread_index;
855 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
856 snat_in2out_node.index;
858 from = vlib_frame_vector_args (frame);
859 n_left_from = frame->n_vectors;
860 next_index = node->cached_next_index;
862 while (n_left_from > 0)
866 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
868 while (n_left_from >= 4 && n_left_to_next >= 2)
871 vlib_buffer_t *b0, *b1;
873 u32 sw_if_index0, sw_if_index1;
874 ip4_header_t *ip0, *ip1;
875 ip_csum_t sum0, sum1;
876 u32 new_addr0, old_addr0, new_addr1, old_addr1;
877 u16 old_port0, new_port0, old_port1, new_port1;
878 udp_header_t *udp0, *udp1;
879 tcp_header_t *tcp0, *tcp1;
880 icmp46_header_t *icmp0, *icmp1;
881 snat_session_key_t key0, key1;
882 u32 rx_fib_index0, rx_fib_index1;
884 snat_session_t *s0 = 0, *s1 = 0;
885 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
886 u32 iph_offset0 = 0, iph_offset1 = 0;
888 /* Prefetch next iteration. */
890 vlib_buffer_t *p2, *p3;
892 p2 = vlib_get_buffer (vm, from[2]);
893 p3 = vlib_get_buffer (vm, from[3]);
895 vlib_prefetch_buffer_header (p2, LOAD);
896 vlib_prefetch_buffer_header (p3, LOAD);
898 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
899 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
902 /* speculatively enqueue b0 and b1 to the current next frame */
903 to_next[0] = bi0 = from[0];
904 to_next[1] = bi1 = from[1];
910 b0 = vlib_get_buffer (vm, bi0);
911 b1 = vlib_get_buffer (vm, bi1);
913 if (is_output_feature)
914 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
916 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
919 udp0 = ip4_next_header (ip0);
920 tcp0 = (tcp_header_t *) udp0;
921 icmp0 = (icmp46_header_t *) udp0;
923 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
924 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
927 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
929 if (PREDICT_FALSE (ip0->ttl == 1))
931 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
932 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
933 ICMP4_time_exceeded_ttl_exceeded_in_transit,
935 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
939 proto0 = ip_proto_to_snat_proto (ip0->protocol);
941 /* Next configured feature, probably ip4-lookup */
944 if (PREDICT_FALSE (proto0 == ~0))
946 if (nat_in2out_sm_unknown_proto
947 (sm, b0, ip0, rx_fib_index0))
949 next0 = SNAT_IN2OUT_NEXT_DROP;
951 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
956 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
958 next0 = icmp_in2out_slow_path
959 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
960 node, next0, now, thread_index, &s0);
966 if (PREDICT_FALSE (proto0 == ~0))
968 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
972 if (ip4_is_fragment (ip0))
974 next0 = SNAT_IN2OUT_NEXT_REASS;
978 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
980 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
985 key0.addr = ip0->src_address;
986 key0.port = udp0->src_port;
987 key0.protocol = proto0;
988 key0.fib_index = rx_fib_index0;
990 kv0.key = key0.as_u64;
993 (clib_bihash_search_8_8
994 (&sm->per_thread_data[thread_index].in2out, &kv0,
999 if (is_output_feature)
1001 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1014 (sm, node, sw_if_index0, ip0, proto0,
1015 rx_fib_index0, thread_index)))
1019 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1020 &s0, node, next0, thread_index, now);
1021 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1024 if (PREDICT_FALSE (!s0))
1029 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1035 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1038 b0->flags |= VNET_BUFFER_F_IS_NATED;
1040 old_addr0 = ip0->src_address.as_u32;
1041 ip0->src_address = s0->out2in.addr;
1042 new_addr0 = ip0->src_address.as_u32;
1043 if (!is_output_feature)
1044 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1046 sum0 = ip0->checksum;
1047 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1049 src_address /* changed member */ );
1050 ip0->checksum = ip_csum_fold (sum0);
1052 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1054 old_port0 = tcp0->src_port;
1055 tcp0->src_port = s0->out2in.port;
1056 new_port0 = tcp0->src_port;
1058 sum0 = tcp0->checksum;
1059 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1061 dst_address /* changed member */ );
1062 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1063 ip4_header_t /* cheat */ ,
1064 length /* changed member */ );
1065 mss_clamping (sm, tcp0, &sum0);
1066 tcp0->checksum = ip_csum_fold (sum0);
1070 old_port0 = udp0->src_port;
1071 udp0->src_port = s0->out2in.port;
1076 nat44_session_update_counters (s0, now,
1077 vlib_buffer_length_in_chain (vm,
1079 /* Per-user LRU list maintenance */
1080 nat44_session_update_lru (sm, s0, thread_index);
1083 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1084 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1086 snat_in2out_trace_t *t =
1087 vlib_add_trace (vm, node, b0, sizeof (*t));
1088 t->is_slow_path = is_slow_path;
1089 t->sw_if_index = sw_if_index0;
1090 t->next_index = next0;
1091 t->session_index = ~0;
1094 s0 - sm->per_thread_data[thread_index].sessions;
1097 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1099 if (is_output_feature)
1100 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1102 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1105 udp1 = ip4_next_header (ip1);
1106 tcp1 = (tcp_header_t *) udp1;
1107 icmp1 = (icmp46_header_t *) udp1;
1109 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1110 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1113 if (PREDICT_FALSE (ip1->ttl == 1))
1115 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1116 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1117 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1119 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1123 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1125 /* Next configured feature, probably ip4-lookup */
1128 if (PREDICT_FALSE (proto1 == ~0))
1130 if (nat_in2out_sm_unknown_proto
1131 (sm, b1, ip1, rx_fib_index1))
1133 next1 = SNAT_IN2OUT_NEXT_DROP;
1135 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1140 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1142 next1 = icmp_in2out_slow_path
1143 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1144 next1, now, thread_index, &s1);
1150 if (PREDICT_FALSE (proto1 == ~0))
1152 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1156 if (ip4_is_fragment (ip1))
1158 next1 = SNAT_IN2OUT_NEXT_REASS;
1162 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1164 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1169 key1.addr = ip1->src_address;
1170 key1.port = udp1->src_port;
1171 key1.protocol = proto1;
1172 key1.fib_index = rx_fib_index1;
1174 kv1.key = key1.as_u64;
1177 (clib_bihash_search_8_8
1178 (&sm->per_thread_data[thread_index].in2out, &kv1,
1183 if (is_output_feature)
1185 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1198 (sm, node, sw_if_index1, ip1, proto1,
1199 rx_fib_index1, thread_index)))
1203 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1204 &s1, node, next1, thread_index, now);
1205 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1208 if (PREDICT_FALSE (!s1))
1213 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1219 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1222 b1->flags |= VNET_BUFFER_F_IS_NATED;
1224 old_addr1 = ip1->src_address.as_u32;
1225 ip1->src_address = s1->out2in.addr;
1226 new_addr1 = ip1->src_address.as_u32;
1227 if (!is_output_feature)
1228 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1230 sum1 = ip1->checksum;
1231 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1233 src_address /* changed member */ );
1234 ip1->checksum = ip_csum_fold (sum1);
1236 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1238 old_port1 = tcp1->src_port;
1239 tcp1->src_port = s1->out2in.port;
1240 new_port1 = tcp1->src_port;
1242 sum1 = tcp1->checksum;
1243 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1245 dst_address /* changed member */ );
1246 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1247 ip4_header_t /* cheat */ ,
1248 length /* changed member */ );
1249 mss_clamping (sm, tcp1, &sum1);
1250 tcp1->checksum = ip_csum_fold (sum1);
1254 old_port1 = udp1->src_port;
1255 udp1->src_port = s1->out2in.port;
1260 nat44_session_update_counters (s1, now,
1261 vlib_buffer_length_in_chain (vm,
1263 /* Per-user LRU list maintenance */
1264 nat44_session_update_lru (sm, s1, thread_index);
1267 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1268 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1270 snat_in2out_trace_t *t =
1271 vlib_add_trace (vm, node, b1, sizeof (*t));
1272 t->sw_if_index = sw_if_index1;
1273 t->next_index = next1;
1274 t->session_index = ~0;
1277 s1 - sm->per_thread_data[thread_index].sessions;
1280 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1282 /* verify speculative enqueues, maybe switch current next frame */
1283 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1284 to_next, n_left_to_next,
1285 bi0, bi1, next0, next1);
1288 while (n_left_from > 0 && n_left_to_next > 0)
1296 u32 new_addr0, old_addr0;
1297 u16 old_port0, new_port0;
1300 icmp46_header_t *icmp0;
1301 snat_session_key_t key0;
1304 snat_session_t *s0 = 0;
1305 clib_bihash_kv_8_8_t kv0, value0;
1306 u32 iph_offset0 = 0;
1308 /* speculatively enqueue b0 to the current next frame */
1314 n_left_to_next -= 1;
1316 b0 = vlib_get_buffer (vm, bi0);
1317 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1319 if (is_output_feature)
1320 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1322 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1325 udp0 = ip4_next_header (ip0);
1326 tcp0 = (tcp_header_t *) udp0;
1327 icmp0 = (icmp46_header_t *) udp0;
1329 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1330 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1333 if (PREDICT_FALSE (ip0->ttl == 1))
1335 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1336 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1337 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1339 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1343 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1345 /* Next configured feature, probably ip4-lookup */
1348 if (PREDICT_FALSE (proto0 == ~0))
1350 if (nat_in2out_sm_unknown_proto
1351 (sm, b0, ip0, rx_fib_index0))
1353 next0 = SNAT_IN2OUT_NEXT_DROP;
1355 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1360 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1362 next0 = icmp_in2out_slow_path
1363 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1364 next0, now, thread_index, &s0);
1370 if (PREDICT_FALSE (proto0 == ~0))
1372 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1376 if (ip4_is_fragment (ip0))
1378 next0 = SNAT_IN2OUT_NEXT_REASS;
1382 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1384 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1389 key0.addr = ip0->src_address;
1390 key0.port = udp0->src_port;
1391 key0.protocol = proto0;
1392 key0.fib_index = rx_fib_index0;
1394 kv0.key = key0.as_u64;
1396 if (clib_bihash_search_8_8
1397 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1401 if (is_output_feature)
1403 if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1416 (sm, node, sw_if_index0, ip0, proto0,
1417 rx_fib_index0, thread_index)))
1421 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1422 &s0, node, next0, thread_index, now);
1424 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1427 if (PREDICT_FALSE (!s0))
1432 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1438 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1441 b0->flags |= VNET_BUFFER_F_IS_NATED;
1443 old_addr0 = ip0->src_address.as_u32;
1444 ip0->src_address = s0->out2in.addr;
1445 new_addr0 = ip0->src_address.as_u32;
1446 if (!is_output_feature)
1447 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1449 sum0 = ip0->checksum;
1450 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1452 src_address /* changed member */ );
1453 ip0->checksum = ip_csum_fold (sum0);
1455 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1457 old_port0 = tcp0->src_port;
1458 tcp0->src_port = s0->out2in.port;
1459 new_port0 = tcp0->src_port;
1461 sum0 = tcp0->checksum;
1462 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1464 dst_address /* changed member */ );
1465 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1466 ip4_header_t /* cheat */ ,
1467 length /* changed member */ );
1468 mss_clamping (sm, tcp0, &sum0);
1469 tcp0->checksum = ip_csum_fold (sum0);
1473 old_port0 = udp0->src_port;
1474 udp0->src_port = s0->out2in.port;
1479 nat44_session_update_counters (s0, now,
1480 vlib_buffer_length_in_chain (vm,
1482 /* Per-user LRU list maintenance */
1483 nat44_session_update_lru (sm, s0, thread_index);
1486 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1487 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1489 snat_in2out_trace_t *t =
1490 vlib_add_trace (vm, node, b0, sizeof (*t));
1491 t->is_slow_path = is_slow_path;
1492 t->sw_if_index = sw_if_index0;
1493 t->next_index = next0;
1494 t->session_index = ~0;
1497 s0 - sm->per_thread_data[thread_index].sessions;
1500 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1502 /* verify speculative enqueue, maybe switch current next frame */
1503 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1504 to_next, n_left_to_next,
1508 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1511 vlib_node_increment_counter (vm, stats_node_index,
1512 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1514 return frame->n_vectors;
1518 snat_in2out_fast_path_fn (vlib_main_t * vm,
1519 vlib_node_runtime_t * node, vlib_frame_t * frame)
1521 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1526 VLIB_REGISTER_NODE (snat_in2out_node) = {
1527 .function = snat_in2out_fast_path_fn,
1528 .name = "nat44-in2out",
1529 .vector_size = sizeof (u32),
1530 .format_trace = format_snat_in2out_trace,
1531 .type = VLIB_NODE_TYPE_INTERNAL,
1533 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1534 .error_strings = snat_in2out_error_strings,
1536 .runtime_data_bytes = sizeof (snat_runtime_t),
1538 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1540 /* edit / add dispositions here */
1542 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1543 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1544 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1545 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1546 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1551 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1554 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1555 vlib_node_runtime_t * node,
1556 vlib_frame_t * frame)
1558 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1563 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1564 .function = snat_in2out_output_fast_path_fn,
1565 .name = "nat44-in2out-output",
1566 .vector_size = sizeof (u32),
1567 .format_trace = format_snat_in2out_trace,
1568 .type = VLIB_NODE_TYPE_INTERNAL,
1570 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1571 .error_strings = snat_in2out_error_strings,
1573 .runtime_data_bytes = sizeof (snat_runtime_t),
1575 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1577 /* edit / add dispositions here */
1579 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1580 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1581 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1582 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1583 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1588 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
1589 snat_in2out_output_fast_path_fn);
1592 snat_in2out_slow_path_fn (vlib_main_t * vm,
1593 vlib_node_runtime_t * node, vlib_frame_t * frame)
1595 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1600 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1601 .function = snat_in2out_slow_path_fn,
1602 .name = "nat44-in2out-slowpath",
1603 .vector_size = sizeof (u32),
1604 .format_trace = format_snat_in2out_trace,
1605 .type = VLIB_NODE_TYPE_INTERNAL,
1607 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1608 .error_strings = snat_in2out_error_strings,
1610 .runtime_data_bytes = sizeof (snat_runtime_t),
1612 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1614 /* edit / add dispositions here */
1616 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1617 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1618 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1619 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1620 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1625 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
1626 snat_in2out_slow_path_fn);
1629 snat_in2out_output_slow_path_fn (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, 1 /* is_slow_path */ ,
1638 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1639 .function = snat_in2out_output_slow_path_fn,
1640 .name = "nat44-in2out-output-slowpath",
1641 .vector_size = sizeof (u32),
1642 .format_trace = format_snat_in2out_trace,
1643 .type = VLIB_NODE_TYPE_INTERNAL,
1645 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1646 .error_strings = snat_in2out_error_strings,
1648 .runtime_data_bytes = sizeof (snat_runtime_t),
1650 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1652 /* edit / add dispositions here */
1654 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1655 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1656 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1657 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1658 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1663 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
1664 snat_in2out_output_slow_path_fn);
1667 nat44_in2out_reass_node_fn (vlib_main_t * vm,
1668 vlib_node_runtime_t * node, vlib_frame_t * frame)
1670 u32 n_left_from, *from, *to_next;
1671 snat_in2out_next_t next_index;
1672 u32 pkts_processed = 0;
1673 snat_main_t *sm = &snat_main;
1674 f64 now = vlib_time_now (vm);
1675 u32 thread_index = vm->thread_index;
1676 snat_main_per_thread_data_t *per_thread_data =
1677 &sm->per_thread_data[thread_index];
1678 u32 *fragments_to_drop = 0;
1679 u32 *fragments_to_loopback = 0;
1681 from = vlib_frame_vector_args (frame);
1682 n_left_from = frame->n_vectors;
1683 next_index = node->cached_next_index;
1685 while (n_left_from > 0)
1689 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1691 while (n_left_from > 0 && n_left_to_next > 0)
1693 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1698 nat_reass_ip4_t *reass0;
1701 icmp46_header_t *icmp0;
1702 snat_session_key_t key0;
1703 clib_bihash_kv_8_8_t kv0, value0;
1704 snat_session_t *s0 = 0;
1705 u16 old_port0, new_port0;
1708 /* speculatively enqueue b0 to the current next frame */
1714 n_left_to_next -= 1;
1716 b0 = vlib_get_buffer (vm, bi0);
1717 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1719 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1721 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1724 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1726 next0 = SNAT_IN2OUT_NEXT_DROP;
1727 b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1731 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1732 udp0 = ip4_next_header (ip0);
1733 tcp0 = (tcp_header_t *) udp0;
1734 icmp0 = (icmp46_header_t *) udp0;
1735 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1737 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1741 1, &fragments_to_drop);
1743 if (PREDICT_FALSE (!reass0))
1745 next0 = SNAT_IN2OUT_NEXT_DROP;
1746 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1747 nat_log_notice ("maximum reassemblies exceeded");
1751 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1753 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1755 next0 = icmp_in2out_slow_path
1756 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1757 next0, now, thread_index, &s0);
1759 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1762 reass0->sess_index = s0 - per_thread_data->sessions;
1764 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1765 nat_ip4_reass_get_frags (reass0,
1766 &fragments_to_loopback);
1772 key0.addr = ip0->src_address;
1773 key0.port = udp0->src_port;
1774 key0.protocol = proto0;
1775 key0.fib_index = rx_fib_index0;
1776 kv0.key = key0.as_u64;
1778 if (clib_bihash_search_8_8
1779 (&per_thread_data->in2out, &kv0, &value0))
1783 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1787 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1788 &s0, node, next0, thread_index, now);
1790 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1793 if (PREDICT_FALSE (!s0))
1796 reass0->sess_index = s0 - per_thread_data->sessions;
1800 s0 = pool_elt_at_index (per_thread_data->sessions,
1802 reass0->sess_index = value0.value;
1804 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1808 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1810 if (nat_ip4_reass_add_fragment
1811 (reass0, bi0, &fragments_to_drop))
1813 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1815 ("maximum fragments per reassembly exceeded");
1816 next0 = SNAT_IN2OUT_NEXT_DROP;
1822 s0 = pool_elt_at_index (per_thread_data->sessions,
1823 reass0->sess_index);
1826 old_addr0 = ip0->src_address.as_u32;
1827 ip0->src_address = s0->out2in.addr;
1828 new_addr0 = ip0->src_address.as_u32;
1829 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1831 sum0 = ip0->checksum;
1832 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1834 src_address /* changed member */ );
1835 ip0->checksum = ip_csum_fold (sum0);
1837 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1839 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1841 old_port0 = tcp0->src_port;
1842 tcp0->src_port = s0->out2in.port;
1843 new_port0 = tcp0->src_port;
1845 sum0 = tcp0->checksum;
1846 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1848 dst_address /* changed member */ );
1849 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1850 ip4_header_t /* cheat */ ,
1851 length /* changed member */ );
1852 tcp0->checksum = ip_csum_fold (sum0);
1856 old_port0 = udp0->src_port;
1857 udp0->src_port = s0->out2in.port;
1863 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1864 s0->ext_host_port, proto0, 0);
1867 nat44_session_update_counters (s0, now,
1868 vlib_buffer_length_in_chain (vm,
1870 /* Per-user LRU list maintenance */
1871 nat44_session_update_lru (sm, s0, thread_index);
1874 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1875 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1877 nat44_reass_trace_t *t =
1878 vlib_add_trace (vm, node, b0, sizeof (*t));
1879 t->cached = cached0;
1880 t->sw_if_index = sw_if_index0;
1881 t->next_index = next0;
1891 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1893 /* verify speculative enqueue, maybe switch current next frame */
1894 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1895 to_next, n_left_to_next,
1899 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1901 from = vlib_frame_vector_args (frame);
1902 u32 len = vec_len (fragments_to_loopback);
1903 if (len <= VLIB_FRAME_SIZE)
1905 clib_memcpy_fast (from, fragments_to_loopback,
1906 sizeof (u32) * len);
1908 vec_reset_length (fragments_to_loopback);
1912 clib_memcpy_fast (from, fragments_to_loopback +
1913 (len - VLIB_FRAME_SIZE),
1914 sizeof (u32) * VLIB_FRAME_SIZE);
1915 n_left_from = VLIB_FRAME_SIZE;
1916 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1921 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1924 vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1925 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1928 nat_send_all_to_node (vm, fragments_to_drop, node,
1929 &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1930 SNAT_IN2OUT_NEXT_DROP);
1932 vec_free (fragments_to_drop);
1933 vec_free (fragments_to_loopback);
1934 return frame->n_vectors;
1938 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1939 .function = nat44_in2out_reass_node_fn,
1940 .name = "nat44-in2out-reass",
1941 .vector_size = sizeof (u32),
1942 .format_trace = format_nat44_reass_trace,
1943 .type = VLIB_NODE_TYPE_INTERNAL,
1945 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1946 .error_strings = snat_in2out_error_strings,
1948 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1950 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1951 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1952 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1953 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1954 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1959 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
1960 nat44_in2out_reass_node_fn);
1963 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
1964 vlib_node_runtime_t * node,
1965 vlib_frame_t * frame)
1967 u32 n_left_from, *from, *to_next;
1968 snat_in2out_next_t next_index;
1969 u32 pkts_processed = 0;
1970 snat_main_t *sm = &snat_main;
1971 u32 stats_node_index;
1973 stats_node_index = snat_in2out_fast_node.index;
1975 from = vlib_frame_vector_args (frame);
1976 n_left_from = frame->n_vectors;
1977 next_index = node->cached_next_index;
1979 while (n_left_from > 0)
1983 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1985 while (n_left_from > 0 && n_left_to_next > 0)
1993 u32 new_addr0, old_addr0;
1994 u16 old_port0, new_port0;
1997 icmp46_header_t *icmp0;
1998 snat_session_key_t key0, sm0;
2002 /* speculatively enqueue b0 to the current next frame */
2008 n_left_to_next -= 1;
2010 b0 = vlib_get_buffer (vm, bi0);
2011 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2013 ip0 = vlib_buffer_get_current (b0);
2014 udp0 = ip4_next_header (ip0);
2015 tcp0 = (tcp_header_t *) udp0;
2016 icmp0 = (icmp46_header_t *) udp0;
2018 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2020 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2022 if (PREDICT_FALSE (ip0->ttl == 1))
2024 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2025 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2026 ICMP4_time_exceeded_ttl_exceeded_in_transit,
2028 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2032 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2034 if (PREDICT_FALSE (proto0 == ~0))
2037 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2039 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2040 rx_fib_index0, node, next0, ~0, 0, 0);
2044 key0.addr = ip0->src_address;
2045 key0.protocol = proto0;
2046 key0.port = udp0->src_port;
2047 key0.fib_index = rx_fib_index0;
2049 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2051 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2052 next0 = SNAT_IN2OUT_NEXT_DROP;
2056 new_addr0 = sm0.addr.as_u32;
2057 new_port0 = sm0.port;
2058 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2059 old_addr0 = ip0->src_address.as_u32;
2060 ip0->src_address.as_u32 = new_addr0;
2062 sum0 = ip0->checksum;
2063 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2065 src_address /* changed member */ );
2066 ip0->checksum = ip_csum_fold (sum0);
2068 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2070 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2072 old_port0 = tcp0->src_port;
2073 tcp0->src_port = new_port0;
2075 sum0 = tcp0->checksum;
2076 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2078 dst_address /* changed member */ );
2079 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2080 ip4_header_t /* cheat */ ,
2081 length /* changed member */ );
2082 mss_clamping (sm, tcp0, &sum0);
2083 tcp0->checksum = ip_csum_fold (sum0);
2087 old_port0 = udp0->src_port;
2088 udp0->src_port = new_port0;
2094 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2096 sum0 = tcp0->checksum;
2097 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2099 dst_address /* changed member */ );
2100 mss_clamping (sm, tcp0, &sum0);
2101 tcp0->checksum = ip_csum_fold (sum0);
2106 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2109 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2110 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2112 snat_in2out_trace_t *t =
2113 vlib_add_trace (vm, node, b0, sizeof (*t));
2114 t->sw_if_index = sw_if_index0;
2115 t->next_index = next0;
2118 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2120 /* verify speculative enqueue, maybe switch current next frame */
2121 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2122 to_next, n_left_to_next,
2126 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2129 vlib_node_increment_counter (vm, stats_node_index,
2130 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2132 return frame->n_vectors;
2137 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2138 .function = snat_in2out_fast_static_map_fn,
2139 .name = "nat44-in2out-fast",
2140 .vector_size = sizeof (u32),
2141 .format_trace = format_snat_in2out_fast_trace,
2142 .type = VLIB_NODE_TYPE_INTERNAL,
2144 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2145 .error_strings = snat_in2out_error_strings,
2147 .runtime_data_bytes = sizeof (snat_runtime_t),
2149 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2151 /* edit / add dispositions here */
2153 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2154 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2155 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2156 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2157 [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2162 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node,
2163 snat_in2out_fast_static_map_fn);
2166 * fd.io coding-style-patch-verification: ON
2169 * eval: (c-set-style "gnu")