2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief NAT44 inside to outside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/udp/udp.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/error.h>
37 #include <vppinfra/elog.h>
45 } snat_in2out_trace_t;
47 /* packet trace format function */
49 format_snat_in2out_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
56 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
58 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
59 t->sw_if_index, t->next_index, t->session_index);
65 format_snat_in2out_fast_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
71 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
72 t->sw_if_index, t->next_index);
77 #define foreach_snat_in2out_error \
78 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
79 _(IN2OUT_PACKETS, "good in2out packets processed") \
80 _(OUT_OF_PORTS, "out of ports") \
81 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
82 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
83 _(NO_TRANSLATION, "no translation") \
84 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
85 _(DROP_FRAGMENT, "drop fragment") \
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,
115 } snat_in2out_next_t;
118 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
119 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
120 u32 rx_fib_index0, u32 thread_index)
122 udp_header_t *udp0 = ip4_next_header (ip0);
123 snat_session_key_t key0, sm0;
124 clib_bihash_kv_8_8_t kv0, value0;
126 key0.addr = ip0->dst_address;
127 key0.port = udp0->dst_port;
128 key0.protocol = proto0;
129 key0.fib_index = sm->outside_fib_index;
130 kv0.key = key0.as_u64;
132 /* NAT packet aimed at external address if */
133 /* has active sessions */
134 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
137 /* or is static mappings */
138 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
144 if (sm->forwarding_enabled)
147 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
152 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
153 u32 proto0, u16 src_port, u16 dst_port,
154 u32 thread_index, u32 sw_if_index)
156 snat_session_key_t key0;
157 clib_bihash_kv_8_8_t kv0, value0;
161 key0.addr = ip0->src_address;
162 key0.port = src_port;
163 key0.protocol = proto0;
164 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
165 kv0.key = key0.as_u64;
167 if (!clib_bihash_search_8_8
168 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
172 key0.addr = ip0->dst_address;
173 key0.port = dst_port;
174 key0.protocol = proto0;
175 kv0.key = key0.as_u64;
176 if (!clib_bihash_search_8_8
177 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
181 pool_foreach (i, sm->output_feature_interfaces,
183 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
193 #ifndef CLIB_MARCH_VARIANT
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_elog_warn ("out2in key del failed");
213 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
214 s->in2out.addr.as_u32,
215 s->out2in.addr.as_u32,
219 s->in2out.fib_index);
221 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
222 &s->in2out.addr, s->in2out.port,
223 &s->out2in.addr, s->out2in.port,
226 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
227 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
230 if (!snat_is_session_static (s))
231 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
234 nat44_delete_session (sm, s, ctx->thread_index);
243 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
246 snat_session_key_t * key0,
247 snat_session_t ** sessionp,
248 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
251 snat_session_t *s = 0;
252 clib_bihash_kv_8_8_t kv0;
253 snat_session_key_t key1;
255 nat_outside_fib_t *outside_fib;
256 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
259 .fp_proto = FIB_PROTOCOL_IP4,
262 .ip4.as_u32 = ip0->dst_address.as_u32,
265 nat44_is_idle_session_ctx_t ctx0;
267 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
269 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
270 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
271 nat_elog_notice ("maximum sessions exceeded");
272 return SNAT_IN2OUT_NEXT_DROP;
275 key1.protocol = key0->protocol;
277 /* First try to match static mapping by local address and port */
278 if (snat_static_mapping_match
279 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
281 /* Try to create dynamic translation */
282 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
286 [thread_index].snat_thread_index))
288 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
289 return SNAT_IN2OUT_NEXT_DROP;
294 if (PREDICT_FALSE (identity_nat))
303 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
307 nat_elog_warn ("create NAT user failed");
308 return SNAT_IN2OUT_NEXT_DROP;
311 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
314 nat44_delete_user_with_no_session (sm, u, thread_index);
315 nat_elog_warn ("create NAT session failed");
316 return SNAT_IN2OUT_NEXT_DROP;
320 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
321 user_session_increment (sm, u, is_sm);
324 s->out2in.protocol = key0->protocol;
325 s->out2in.fib_index = sm->outside_fib_index;
326 switch (vec_len (sm->outside_fibs))
329 s->out2in.fib_index = sm->outside_fib_index;
332 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
336 vec_foreach (outside_fib, sm->outside_fibs)
338 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
339 if (FIB_NODE_INDEX_INVALID != fei)
341 if (fib_entry_get_resolving_interface (fei) != ~0)
343 s->out2in.fib_index = outside_fib->fib_index;
351 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
352 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
355 /* Add to translation hashes */
357 ctx0.thread_index = thread_index;
358 kv0.key = s->in2out.as_u64;
359 kv0.value = s - sm->per_thread_data[thread_index].sessions;
360 if (clib_bihash_add_or_overwrite_stale_8_8
361 (&sm->per_thread_data[thread_index].in2out, &kv0,
362 nat44_i2o_is_idle_session_cb, &ctx0))
363 nat_elog_notice ("in2out key add failed");
365 kv0.key = s->out2in.as_u64;
366 kv0.value = s - sm->per_thread_data[thread_index].sessions;
368 if (clib_bihash_add_or_overwrite_stale_8_8
369 (&sm->per_thread_data[thread_index].out2in, &kv0,
370 nat44_o2i_is_idle_session_cb, &ctx0))
371 nat_elog_notice ("out2in key add failed");
374 snat_ipfix_logging_nat44_ses_create (thread_index,
375 s->in2out.addr.as_u32,
376 s->out2in.addr.as_u32,
379 s->out2in.port, s->in2out.fib_index);
381 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
382 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
383 s->out2in.port, s->in2out.protocol);
385 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
386 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
387 &s->ext_host_nat_addr, s->ext_host_nat_port,
388 s->in2out.protocol, s->in2out.fib_index, s->flags,
394 #ifndef CLIB_MARCH_VARIANT
396 snat_in2out_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
397 snat_session_key_t * p_key0)
399 icmp46_header_t *icmp0;
400 snat_session_key_t key0;
401 icmp_echo_header_t *echo0, *inner_echo0 = 0;
402 ip4_header_t *inner_ip0 = 0;
404 icmp46_header_t *inner_icmp0;
406 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
407 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
409 if (!icmp_type_is_error_message
410 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
412 key0.protocol = SNAT_PROTOCOL_ICMP;
413 key0.addr = ip0->src_address;
414 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO fixme should this be dst port?
418 inner_ip0 = (ip4_header_t *) (echo0 + 1);
419 l4_header = ip4_next_header (inner_ip0);
420 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
421 key0.addr = inner_ip0->dst_address;
422 switch (key0.protocol)
424 case SNAT_PROTOCOL_ICMP:
425 inner_icmp0 = (icmp46_header_t *) l4_header;
426 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
427 key0.port = inner_echo0->identifier;
429 case SNAT_PROTOCOL_UDP:
430 case SNAT_PROTOCOL_TCP:
431 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
434 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
438 return -1; /* success */
442 * Get address and port values to be used for ICMP packet translation
443 * and create session if needed
445 * @param[in,out] sm NAT main
446 * @param[in,out] node NAT node runtime
447 * @param[in] thread_index thread index
448 * @param[in,out] b0 buffer containing packet to be translated
449 * @param[out] p_proto protocol used for matching
450 * @param[out] p_value address and port after NAT translation
451 * @param[out] p_dont_translate if packet should not be translated
452 * @param d optional parameter
453 * @param e optional parameter
456 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
457 u32 thread_index, vlib_buffer_t * b0,
458 ip4_header_t * ip0, u8 * p_proto,
459 snat_session_key_t * p_value,
460 u8 * p_dont_translate, void *d, void *e)
464 snat_session_key_t key0;
465 snat_session_t *s0 = 0;
466 u8 dont_translate = 0;
467 clib_bihash_kv_8_8_t kv0, value0;
471 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
472 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
474 err = icmp_get_key (b0, ip0, &key0);
477 b0->error = node->errors[err];
478 next0 = SNAT_IN2OUT_NEXT_DROP;
481 key0.fib_index = rx_fib_index0;
483 kv0.key = key0.as_u64;
485 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
488 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
490 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
503 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
504 ip0, SNAT_PROTOCOL_ICMP,
514 (icmp_type_is_error_message
515 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
517 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
518 next0 = SNAT_IN2OUT_NEXT_DROP;
522 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
523 thread_index, vlib_time_now (sm->vlib_main));
525 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
537 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
539 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
541 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
542 reass.icmp_type_or_tcp_flags)))
544 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
545 next0 = SNAT_IN2OUT_NEXT_DROP;
549 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
554 *p_proto = key0.protocol;
556 *p_value = s0->out2in;
557 *p_dont_translate = dont_translate;
559 *(snat_session_t **) d = s0;
564 #ifndef CLIB_MARCH_VARIANT
566 * Get address and port values to be used for ICMP packet translation
568 * @param[in] sm NAT main
569 * @param[in,out] node NAT node runtime
570 * @param[in] thread_index thread index
571 * @param[in,out] b0 buffer containing packet to be translated
572 * @param[out] p_proto protocol used for matching
573 * @param[out] p_value address and port after NAT translation
574 * @param[out] p_dont_translate if packet should not be translated
575 * @param d optional parameter
576 * @param e optional parameter
579 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
580 u32 thread_index, vlib_buffer_t * b0,
581 ip4_header_t * ip0, u8 * p_proto,
582 snat_session_key_t * p_value,
583 u8 * p_dont_translate, void *d, void *e)
587 snat_session_key_t key0;
588 snat_session_key_t sm0;
589 u8 dont_translate = 0;
594 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
595 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
597 err = icmp_get_key (b0, ip0, &key0);
600 b0->error = node->errors[err];
601 next0 = SNAT_IN2OUT_NEXT_DROP;
604 key0.fib_index = rx_fib_index0;
606 if (snat_static_mapping_match
607 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
609 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
617 if (icmp_type_is_error_message
618 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
620 next0 = SNAT_IN2OUT_NEXT_DROP;
624 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
625 next0 = SNAT_IN2OUT_NEXT_DROP;
630 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
631 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
632 ICMP4_echo_reply || !is_addr_only)
633 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
634 reass.icmp_type_or_tcp_flags)))
636 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
637 next0 = SNAT_IN2OUT_NEXT_DROP;
644 *p_proto = key0.protocol;
645 *p_dont_translate = dont_translate;
650 #ifndef CLIB_MARCH_VARIANT
652 icmp_in2out (snat_main_t * sm,
655 icmp46_header_t * icmp0,
658 vlib_node_runtime_t * node,
659 u32 next0, u32 thread_index, void *d, void *e)
661 snat_session_key_t sm0;
663 icmp_echo_header_t *echo0, *inner_echo0 = 0;
664 ip4_header_t *inner_ip0;
666 icmp46_header_t *inner_icmp0;
668 u32 new_addr0, old_addr0;
669 u16 old_id0, new_id0;
670 u16 old_checksum0, new_checksum0;
675 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
677 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
678 &protocol, &sm0, &dont_translate, d,
682 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
685 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
687 sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
689 vlib_buffer_get_current (b0),
690 ntohs (ip0->length) -
691 ip4_header_bytes (ip0), 0);
692 checksum0 = ~ip_csum_fold (sum0);
693 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
695 next0 = SNAT_IN2OUT_NEXT_DROP;
700 old_addr0 = ip0->src_address.as_u32;
701 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
703 sum0 = ip0->checksum;
704 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
705 src_address /* changed member */ );
706 ip0->checksum = ip_csum_fold (sum0);
708 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
710 if (icmp0->checksum == 0)
711 icmp0->checksum = 0xffff;
713 if (!icmp_type_is_error_message (icmp0->type))
716 if (PREDICT_FALSE (new_id0 != echo0->identifier))
718 old_id0 = echo0->identifier;
720 echo0->identifier = new_id0;
722 sum0 = icmp0->checksum;
724 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
726 icmp0->checksum = ip_csum_fold (sum0);
731 inner_ip0 = (ip4_header_t *) (echo0 + 1);
732 l4_header = ip4_next_header (inner_ip0);
734 if (!ip4_header_checksum_is_valid (inner_ip0))
736 next0 = SNAT_IN2OUT_NEXT_DROP;
740 /* update inner destination IP address */
741 old_addr0 = inner_ip0->dst_address.as_u32;
742 inner_ip0->dst_address = sm0.addr;
743 new_addr0 = inner_ip0->dst_address.as_u32;
744 sum0 = icmp0->checksum;
745 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
746 dst_address /* changed member */ );
747 icmp0->checksum = ip_csum_fold (sum0);
749 /* update inner IP header checksum */
750 old_checksum0 = inner_ip0->checksum;
751 sum0 = inner_ip0->checksum;
752 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
753 dst_address /* changed member */ );
754 inner_ip0->checksum = ip_csum_fold (sum0);
755 new_checksum0 = inner_ip0->checksum;
756 sum0 = icmp0->checksum;
758 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
760 icmp0->checksum = ip_csum_fold (sum0);
764 case SNAT_PROTOCOL_ICMP:
765 inner_icmp0 = (icmp46_header_t *) l4_header;
766 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
768 old_id0 = inner_echo0->identifier;
770 inner_echo0->identifier = new_id0;
772 sum0 = icmp0->checksum;
774 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
776 icmp0->checksum = ip_csum_fold (sum0);
778 case SNAT_PROTOCOL_UDP:
779 case SNAT_PROTOCOL_TCP:
780 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
782 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
784 sum0 = icmp0->checksum;
785 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
787 icmp0->checksum = ip_csum_fold (sum0);
795 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
797 if (sm->deterministic ||
798 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
799 sm->endpoint_dependent))
800 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
809 icmp_in2out_slow_path (snat_main_t * sm,
812 icmp46_header_t * icmp0,
815 vlib_node_runtime_t * node,
817 f64 now, u32 thread_index, snat_session_t ** p_s0)
819 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
820 next0, thread_index, p_s0, 0);
821 snat_session_t *s0 = *p_s0;
822 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
825 nat44_session_update_counters (s0, now,
826 vlib_buffer_length_in_chain
827 (sm->vlib_main, b0), thread_index);
828 /* Per-user LRU list maintenance */
829 nat44_session_update_lru (sm, s0, thread_index);
835 nat_in2out_sm_unknown_proto (snat_main_t * sm,
837 ip4_header_t * ip, u32 rx_fib_index)
839 clib_bihash_kv_8_8_t kv, value;
840 snat_static_mapping_t *m;
841 snat_session_key_t m_key;
842 u32 old_addr, new_addr;
845 m_key.addr = ip->src_address;
848 m_key.fib_index = rx_fib_index;
849 kv.key = m_key.as_u64;
850 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
853 m = pool_elt_at_index (sm->static_mappings, value.value);
855 old_addr = ip->src_address.as_u32;
856 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
858 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
859 ip->checksum = ip_csum_fold (sum);
863 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
865 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
866 nat_hairpinning_sm_unknown_proto (sm, b, ip);
873 snat_in2out_node_fn_inline (vlib_main_t * vm,
874 vlib_node_runtime_t * node,
875 vlib_frame_t * frame, int is_slow_path,
876 int is_output_feature)
878 u32 n_left_from, *from, *to_next;
879 snat_in2out_next_t next_index;
880 u32 pkts_processed = 0;
881 snat_main_t *sm = &snat_main;
882 f64 now = vlib_time_now (vm);
883 u32 stats_node_index;
884 u32 thread_index = vm->thread_index;
885 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
888 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
889 sm->in2out_node_index;
891 from = vlib_frame_vector_args (frame);
892 n_left_from = frame->n_vectors;
893 next_index = node->cached_next_index;
895 while (n_left_from > 0)
899 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
901 while (n_left_from >= 4 && n_left_to_next >= 2)
904 vlib_buffer_t *b0, *b1;
906 u32 sw_if_index0, sw_if_index1;
907 ip4_header_t *ip0, *ip1;
908 ip_csum_t sum0, sum1;
909 u32 new_addr0, old_addr0, new_addr1, old_addr1;
910 u16 old_port0, new_port0, old_port1, new_port1;
911 udp_header_t *udp0, *udp1;
912 tcp_header_t *tcp0, *tcp1;
913 icmp46_header_t *icmp0, *icmp1;
914 snat_session_key_t key0, key1;
915 u32 rx_fib_index0, rx_fib_index1;
917 snat_session_t *s0 = 0, *s1 = 0;
918 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
919 u32 iph_offset0 = 0, iph_offset1 = 0;
921 /* Prefetch next iteration. */
923 vlib_buffer_t *p2, *p3;
925 p2 = vlib_get_buffer (vm, from[2]);
926 p3 = vlib_get_buffer (vm, from[3]);
928 vlib_prefetch_buffer_header (p2, LOAD);
929 vlib_prefetch_buffer_header (p3, LOAD);
931 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
932 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
935 /* speculatively enqueue b0 and b1 to the current next frame */
936 to_next[0] = bi0 = from[0];
937 to_next[1] = bi1 = from[1];
943 b0 = vlib_get_buffer (vm, bi0);
944 b1 = vlib_get_buffer (vm, bi1);
946 if (is_output_feature)
947 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
949 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
952 udp0 = ip4_next_header (ip0);
953 tcp0 = (tcp_header_t *) udp0;
954 icmp0 = (icmp46_header_t *) udp0;
956 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
957 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
960 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
962 if (PREDICT_FALSE (ip0->ttl == 1))
964 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
965 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
966 ICMP4_time_exceeded_ttl_exceeded_in_transit,
968 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
972 proto0 = ip_proto_to_snat_proto (ip0->protocol);
974 /* Next configured feature, probably ip4-lookup */
977 if (PREDICT_FALSE (proto0 == ~0))
979 if (nat_in2out_sm_unknown_proto
980 (sm, b0, ip0, rx_fib_index0))
982 next0 = SNAT_IN2OUT_NEXT_DROP;
984 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
990 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
992 next0 = icmp_in2out_slow_path
993 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
994 node, next0, now, thread_index, &s0);
1001 if (PREDICT_FALSE (proto0 == ~0))
1003 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1007 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1009 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1014 key0.addr = ip0->src_address;
1015 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1016 key0.protocol = proto0;
1017 key0.fib_index = rx_fib_index0;
1019 kv0.key = key0.as_u64;
1022 (clib_bihash_search_8_8
1023 (&sm->per_thread_data[thread_index].in2out, &kv0,
1028 if (is_output_feature)
1031 (nat_not_translate_output_feature
1033 vnet_buffer (b0)->ip.reass.l4_src_port,
1034 vnet_buffer (b0)->ip.reass.l4_dst_port,
1035 thread_index, sw_if_index0)))
1039 * Send DHCP packets to the ipv4 stack, or we won't
1040 * be able to use dhcp client on the outside interface
1043 (proto0 == SNAT_PROTOCOL_UDP
1044 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1045 clib_host_to_net_u16
1046 (UDP_DST_PORT_dhcp_to_server))
1047 && ip0->dst_address.as_u32 == 0xffffffff))
1054 (sm, node, sw_if_index0, ip0, proto0,
1055 rx_fib_index0, thread_index)))
1059 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1060 &s0, node, next0, thread_index, now);
1061 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1064 if (PREDICT_FALSE (!s0))
1069 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1075 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1078 b0->flags |= VNET_BUFFER_F_IS_NATED;
1080 old_addr0 = ip0->src_address.as_u32;
1081 ip0->src_address = s0->out2in.addr;
1082 new_addr0 = ip0->src_address.as_u32;
1083 if (!is_output_feature)
1084 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1086 sum0 = ip0->checksum;
1087 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1089 src_address /* changed member */ );
1090 ip0->checksum = ip_csum_fold (sum0);
1093 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1095 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1097 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1098 new_port0 = udp0->src_port = s0->out2in.port;
1099 sum0 = tcp0->checksum;
1100 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1102 dst_address /* changed member */ );
1103 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1104 ip4_header_t /* cheat */ ,
1105 length /* changed member */ );
1106 mss_clamping (sm, tcp0, &sum0);
1107 tcp0->checksum = ip_csum_fold (sum0);
1113 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1115 if (PREDICT_FALSE (udp0->checksum))
1117 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1118 new_port0 = udp0->src_port = s0->out2in.port;
1119 sum0 = udp0->checksum;
1120 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1123 ip_csum_update (sum0, old_port0, new_port0,
1124 ip4_header_t /* cheat */ ,
1125 length /* changed member */ );
1126 udp0->checksum = ip_csum_fold (sum0);
1133 nat44_session_update_counters (s0, now,
1134 vlib_buffer_length_in_chain (vm, b0),
1136 /* Per-user LRU list maintenance */
1137 nat44_session_update_lru (sm, s0, thread_index);
1140 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1141 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1143 snat_in2out_trace_t *t =
1144 vlib_add_trace (vm, node, b0, sizeof (*t));
1145 t->is_slow_path = is_slow_path;
1146 t->sw_if_index = sw_if_index0;
1147 t->next_index = next0;
1148 t->session_index = ~0;
1151 s0 - sm->per_thread_data[thread_index].sessions;
1154 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1156 if (is_output_feature)
1157 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1159 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1162 udp1 = ip4_next_header (ip1);
1163 tcp1 = (tcp_header_t *) udp1;
1164 icmp1 = (icmp46_header_t *) udp1;
1166 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1167 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1170 if (PREDICT_FALSE (ip1->ttl == 1))
1172 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1173 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1174 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1176 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1180 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1182 /* Next configured feature, probably ip4-lookup */
1185 if (PREDICT_FALSE (proto1 == ~0))
1187 if (nat_in2out_sm_unknown_proto
1188 (sm, b1, ip1, rx_fib_index1))
1190 next1 = SNAT_IN2OUT_NEXT_DROP;
1192 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1198 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1200 next1 = icmp_in2out_slow_path
1201 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1202 next1, now, thread_index, &s1);
1209 if (PREDICT_FALSE (proto1 == ~0))
1211 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1215 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1217 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1222 key1.addr = ip1->src_address;
1223 key1.port = vnet_buffer (b1)->ip.reass.l4_src_port;
1224 key1.protocol = proto1;
1225 key1.fib_index = rx_fib_index1;
1227 kv1.key = key1.as_u64;
1230 (clib_bihash_search_8_8
1231 (&sm->per_thread_data[thread_index].in2out, &kv1,
1236 if (is_output_feature)
1239 (nat_not_translate_output_feature
1241 vnet_buffer (b1)->ip.reass.l4_src_port,
1242 vnet_buffer (b1)->ip.reass.l4_dst_port,
1243 thread_index, sw_if_index1)))
1247 * Send DHCP packets to the ipv4 stack, or we won't
1248 * be able to use dhcp client on the outside interface
1251 (proto1 == SNAT_PROTOCOL_UDP
1252 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1253 clib_host_to_net_u16
1254 (UDP_DST_PORT_dhcp_to_server))
1255 && ip1->dst_address.as_u32 == 0xffffffff))
1262 (sm, node, sw_if_index1, ip1, proto1,
1263 rx_fib_index1, thread_index)))
1267 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1268 &s1, node, next1, thread_index, now);
1269 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1272 if (PREDICT_FALSE (!s1))
1277 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1283 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1286 b1->flags |= VNET_BUFFER_F_IS_NATED;
1288 old_addr1 = ip1->src_address.as_u32;
1289 ip1->src_address = s1->out2in.addr;
1290 new_addr1 = ip1->src_address.as_u32;
1291 if (!is_output_feature)
1292 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1294 sum1 = ip1->checksum;
1295 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1297 src_address /* changed member */ );
1298 ip1->checksum = ip_csum_fold (sum1);
1300 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1302 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1304 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1305 new_port1 = udp1->src_port = s1->out2in.port;
1306 sum1 = tcp1->checksum;
1307 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1309 dst_address /* changed member */ );
1310 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1311 ip4_header_t /* cheat */ ,
1312 length /* changed member */ );
1313 mss_clamping (sm, tcp1, &sum1);
1314 tcp1->checksum = ip_csum_fold (sum1);
1320 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1322 if (PREDICT_FALSE (udp1->checksum))
1324 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1325 new_port1 = udp1->src_port = s1->out2in.port;
1326 sum1 = udp1->checksum;
1327 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1330 ip_csum_update (sum1, old_port1, new_port1,
1331 ip4_header_t /* cheat */ ,
1332 length /* changed member */ );
1333 udp1->checksum = ip_csum_fold (sum1);
1340 nat44_session_update_counters (s1, now,
1341 vlib_buffer_length_in_chain (vm, b1),
1343 /* Per-user LRU list maintenance */
1344 nat44_session_update_lru (sm, s1, thread_index);
1347 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1348 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1350 snat_in2out_trace_t *t =
1351 vlib_add_trace (vm, node, b1, sizeof (*t));
1352 t->sw_if_index = sw_if_index1;
1353 t->next_index = next1;
1354 t->session_index = ~0;
1357 s1 - sm->per_thread_data[thread_index].sessions;
1360 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1362 /* verify speculative enqueues, maybe switch current next frame */
1363 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1364 to_next, n_left_to_next,
1365 bi0, bi1, next0, next1);
1368 while (n_left_from > 0 && n_left_to_next > 0)
1376 u32 new_addr0, old_addr0;
1377 u16 old_port0, new_port0;
1380 icmp46_header_t *icmp0;
1381 snat_session_key_t key0;
1384 snat_session_t *s0 = 0;
1385 clib_bihash_kv_8_8_t kv0, value0;
1386 u32 iph_offset0 = 0;
1388 /* speculatively enqueue b0 to the current next frame */
1394 n_left_to_next -= 1;
1396 b0 = vlib_get_buffer (vm, bi0);
1397 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1399 if (is_output_feature)
1400 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1402 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1405 udp0 = ip4_next_header (ip0);
1406 tcp0 = (tcp_header_t *) udp0;
1407 icmp0 = (icmp46_header_t *) udp0;
1409 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1410 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1413 if (PREDICT_FALSE (ip0->ttl == 1))
1415 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1416 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1417 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1419 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1423 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1425 /* Next configured feature, probably ip4-lookup */
1428 if (PREDICT_FALSE (proto0 == ~0))
1430 if (nat_in2out_sm_unknown_proto
1431 (sm, b0, ip0, rx_fib_index0))
1433 next0 = SNAT_IN2OUT_NEXT_DROP;
1435 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1441 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1443 next0 = icmp_in2out_slow_path
1444 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1445 next0, now, thread_index, &s0);
1452 if (PREDICT_FALSE (proto0 == ~0))
1454 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1458 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1460 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1465 key0.addr = ip0->src_address;
1466 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1467 key0.protocol = proto0;
1468 key0.fib_index = rx_fib_index0;
1470 kv0.key = key0.as_u64;
1472 if (clib_bihash_search_8_8
1473 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1477 if (is_output_feature)
1480 (nat_not_translate_output_feature
1482 vnet_buffer (b0)->ip.reass.l4_src_port,
1483 vnet_buffer (b0)->ip.reass.l4_dst_port,
1484 thread_index, sw_if_index0)))
1488 * Send DHCP packets to the ipv4 stack, or we won't
1489 * be able to use dhcp client on the outside interface
1492 (proto0 == SNAT_PROTOCOL_UDP
1493 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1494 clib_host_to_net_u16
1495 (UDP_DST_PORT_dhcp_to_server))
1496 && ip0->dst_address.as_u32 == 0xffffffff))
1503 (sm, node, sw_if_index0, ip0, proto0,
1504 rx_fib_index0, thread_index)))
1508 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1509 &s0, node, next0, thread_index, now);
1511 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1514 if (PREDICT_FALSE (!s0))
1519 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1525 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1528 b0->flags |= VNET_BUFFER_F_IS_NATED;
1530 old_addr0 = ip0->src_address.as_u32;
1531 ip0->src_address = s0->out2in.addr;
1532 new_addr0 = ip0->src_address.as_u32;
1533 if (!is_output_feature)
1534 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1536 sum0 = ip0->checksum;
1537 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1539 src_address /* changed member */ );
1540 ip0->checksum = ip_csum_fold (sum0);
1542 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1544 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1546 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1547 new_port0 = udp0->src_port = s0->out2in.port;
1548 sum0 = tcp0->checksum;
1549 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1551 dst_address /* changed member */ );
1552 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1553 ip4_header_t /* cheat */ ,
1554 length /* changed member */ );
1555 mss_clamping (sm, tcp0, &sum0);
1556 tcp0->checksum = ip_csum_fold (sum0);
1562 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1564 if (PREDICT_FALSE (udp0->checksum))
1566 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1567 new_port0 = udp0->src_port = s0->out2in.port;
1568 sum0 = udp0->checksum;
1569 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1572 ip_csum_update (sum0, old_port0, new_port0,
1573 ip4_header_t /* cheat */ ,
1574 length /* changed member */ );
1575 udp0->checksum = ip_csum_fold (sum0);
1582 nat44_session_update_counters (s0, now,
1583 vlib_buffer_length_in_chain (vm, b0),
1585 /* Per-user LRU list maintenance */
1586 nat44_session_update_lru (sm, s0, thread_index);
1589 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1590 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1592 snat_in2out_trace_t *t =
1593 vlib_add_trace (vm, node, b0, sizeof (*t));
1594 t->is_slow_path = is_slow_path;
1595 t->sw_if_index = sw_if_index0;
1596 t->next_index = next0;
1597 t->session_index = ~0;
1600 s0 - sm->per_thread_data[thread_index].sessions;
1603 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1605 /* verify speculative enqueue, maybe switch current next frame */
1606 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1607 to_next, n_left_to_next,
1611 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1614 vlib_node_increment_counter (vm, stats_node_index,
1615 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1617 vlib_node_increment_counter (vm, stats_node_index,
1618 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1619 vlib_node_increment_counter (vm, stats_node_index,
1620 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1621 vlib_node_increment_counter (vm, stats_node_index,
1622 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1623 vlib_node_increment_counter (vm, stats_node_index,
1624 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1626 vlib_node_increment_counter (vm, stats_node_index,
1627 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1629 return frame->n_vectors;
1632 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1633 vlib_node_runtime_t * node,
1634 vlib_frame_t * frame)
1636 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1641 VLIB_REGISTER_NODE (snat_in2out_node) = {
1642 .name = "nat44-in2out",
1643 .vector_size = sizeof (u32),
1644 .format_trace = format_snat_in2out_trace,
1645 .type = VLIB_NODE_TYPE_INTERNAL,
1647 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1648 .error_strings = snat_in2out_error_strings,
1650 .runtime_data_bytes = sizeof (snat_runtime_t),
1652 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1654 /* edit / add dispositions here */
1656 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1657 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1658 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1659 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1664 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1665 vlib_node_runtime_t * node,
1666 vlib_frame_t * frame)
1668 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1673 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1674 .name = "nat44-in2out-output",
1675 .vector_size = sizeof (u32),
1676 .format_trace = format_snat_in2out_trace,
1677 .type = VLIB_NODE_TYPE_INTERNAL,
1679 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1680 .error_strings = snat_in2out_error_strings,
1682 .runtime_data_bytes = sizeof (snat_runtime_t),
1684 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1686 /* edit / add dispositions here */
1688 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1689 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1690 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1691 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1696 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1697 vlib_node_runtime_t * node,
1698 vlib_frame_t * frame)
1700 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1705 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1706 .name = "nat44-in2out-slowpath",
1707 .vector_size = sizeof (u32),
1708 .format_trace = format_snat_in2out_trace,
1709 .type = VLIB_NODE_TYPE_INTERNAL,
1711 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1712 .error_strings = snat_in2out_error_strings,
1714 .runtime_data_bytes = sizeof (snat_runtime_t),
1716 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1718 /* edit / add dispositions here */
1720 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1721 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1722 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1723 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1728 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1729 vlib_node_runtime_t * node,
1730 vlib_frame_t * frame)
1732 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1737 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1738 .name = "nat44-in2out-output-slowpath",
1739 .vector_size = sizeof (u32),
1740 .format_trace = format_snat_in2out_trace,
1741 .type = VLIB_NODE_TYPE_INTERNAL,
1743 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1744 .error_strings = snat_in2out_error_strings,
1746 .runtime_data_bytes = sizeof (snat_runtime_t),
1748 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1750 /* edit / add dispositions here */
1752 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1753 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1754 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1755 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1760 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1761 vlib_node_runtime_t * node,
1762 vlib_frame_t * frame)
1764 u32 n_left_from, *from, *to_next;
1765 snat_in2out_next_t next_index;
1766 u32 pkts_processed = 0;
1767 snat_main_t *sm = &snat_main;
1768 u32 stats_node_index;
1770 stats_node_index = sm->in2out_fast_node_index;
1772 from = vlib_frame_vector_args (frame);
1773 n_left_from = frame->n_vectors;
1774 next_index = node->cached_next_index;
1776 while (n_left_from > 0)
1780 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1782 while (n_left_from > 0 && n_left_to_next > 0)
1790 u32 new_addr0, old_addr0;
1791 u16 old_port0, new_port0;
1794 icmp46_header_t *icmp0;
1795 snat_session_key_t key0, sm0;
1799 /* speculatively enqueue b0 to the current next frame */
1805 n_left_to_next -= 1;
1807 b0 = vlib_get_buffer (vm, bi0);
1808 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1810 ip0 = vlib_buffer_get_current (b0);
1811 udp0 = ip4_next_header (ip0);
1812 tcp0 = (tcp_header_t *) udp0;
1813 icmp0 = (icmp46_header_t *) udp0;
1815 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1817 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1819 if (PREDICT_FALSE (ip0->ttl == 1))
1821 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1822 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1823 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1825 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1829 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1831 if (PREDICT_FALSE (proto0 == ~0))
1834 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1836 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1837 rx_fib_index0, node, next0, ~0, 0, 0);
1841 key0.addr = ip0->src_address;
1842 key0.protocol = proto0;
1843 key0.port = udp0->src_port;
1844 key0.fib_index = rx_fib_index0;
1846 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
1848 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1849 next0 = SNAT_IN2OUT_NEXT_DROP;
1853 new_addr0 = sm0.addr.as_u32;
1854 new_port0 = sm0.port;
1855 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1856 old_addr0 = ip0->src_address.as_u32;
1857 ip0->src_address.as_u32 = new_addr0;
1859 sum0 = ip0->checksum;
1860 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1862 src_address /* changed member */ );
1863 ip0->checksum = ip_csum_fold (sum0);
1865 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1867 old_port0 = udp0->src_port;
1868 udp0->src_port = new_port0;
1870 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1872 sum0 = tcp0->checksum;
1873 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1875 dst_address /* changed member */ );
1876 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1877 ip4_header_t /* cheat */ ,
1878 length /* changed member */ );
1879 mss_clamping (sm, tcp0, &sum0);
1880 tcp0->checksum = ip_csum_fold (sum0);
1882 else if (udp0->checksum)
1884 sum0 = udp0->checksum;
1885 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1887 dst_address /* changed member */ );
1888 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1889 ip4_header_t /* cheat */ ,
1890 length /* changed member */ );
1891 udp0->checksum = ip_csum_fold (sum0);
1896 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1898 sum0 = tcp0->checksum;
1899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1901 dst_address /* changed member */ );
1902 mss_clamping (sm, tcp0, &sum0);
1903 tcp0->checksum = ip_csum_fold (sum0);
1905 else if (udp0->checksum)
1907 sum0 = udp0->checksum;
1908 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1910 dst_address /* changed member */ );
1911 udp0->checksum = ip_csum_fold (sum0);
1916 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
1919 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1920 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1922 snat_in2out_trace_t *t =
1923 vlib_add_trace (vm, node, b0, sizeof (*t));
1924 t->sw_if_index = sw_if_index0;
1925 t->next_index = next0;
1928 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1930 /* verify speculative enqueue, maybe switch current next frame */
1931 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1932 to_next, n_left_to_next,
1936 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1939 vlib_node_increment_counter (vm, stats_node_index,
1940 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1942 return frame->n_vectors;
1947 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1948 .name = "nat44-in2out-fast",
1949 .vector_size = sizeof (u32),
1950 .format_trace = format_snat_in2out_fast_trace,
1951 .type = VLIB_NODE_TYPE_INTERNAL,
1953 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1954 .error_strings = snat_in2out_error_strings,
1956 .runtime_data_bytes = sizeof (snat_runtime_t),
1958 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1960 /* edit / add dispositions here */
1962 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1963 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1964 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1965 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1971 * fd.io coding-style-patch-verification: ON
1974 * eval: (c-set-style "gnu")