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 = NAT_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_nat_proto (inner_ip0->protocol);
421 key0.addr = inner_ip0->dst_address;
422 switch (key0.protocol)
424 case NAT_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 NAT_PROTOCOL_UDP:
430 case NAT_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[in,out] ip0 ip header
450 * @param[out] p_proto protocol used for matching
451 * @param[out] p_value address and port after NAT translation
452 * @param[out] p_dont_translate if packet should not be translated
453 * @param d optional parameter
454 * @param e optional parameter
457 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
458 u32 thread_index, vlib_buffer_t * b0,
459 ip4_header_t * ip0, u8 * p_proto,
460 snat_session_key_t * p_value,
461 u8 * p_dont_translate, void *d, void *e)
463 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
466 snat_session_key_t key0;
467 snat_session_t *s0 = 0;
468 u8 dont_translate = 0;
469 clib_bihash_kv_8_8_t kv0, value0;
472 vlib_main_t *vm = vlib_get_main ();
474 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
475 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
477 err = icmp_get_key (b0, ip0, &key0);
480 b0->error = node->errors[err];
481 next0 = SNAT_IN2OUT_NEXT_DROP;
484 key0.fib_index = rx_fib_index0;
486 kv0.key = key0.as_u64;
488 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
490 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
492 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
505 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
506 ip0, NAT_PROTOCOL_ICMP,
516 (icmp_type_is_error_message
517 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
519 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
520 next0 = SNAT_IN2OUT_NEXT_DROP;
524 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
525 thread_index, vlib_time_now (vm));
527 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
539 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
541 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
543 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
544 reass.icmp_type_or_tcp_flags)))
546 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
547 next0 = SNAT_IN2OUT_NEXT_DROP;
551 s0 = pool_elt_at_index (tsm->sessions, value0.value);
555 *p_proto = key0.protocol;
557 *p_value = s0->out2in;
558 *p_dont_translate = dont_translate;
560 *(snat_session_t **) d = s0;
565 #ifndef CLIB_MARCH_VARIANT
567 * Get address and port values to be used for ICMP packet translation
569 * @param[in] sm NAT main
570 * @param[in,out] node NAT node runtime
571 * @param[in] thread_index thread index
572 * @param[in,out] b0 buffer containing packet to be translated
573 * @param[in,out] ip0 ip header
574 * @param[out] p_proto protocol used for matching
575 * @param[out] p_value address and port after NAT translation
576 * @param[out] p_dont_translate if packet should not be translated
577 * @param d optional parameter
578 * @param e optional parameter
581 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
582 u32 thread_index, vlib_buffer_t * b0,
583 ip4_header_t * ip0, u8 * p_proto,
584 snat_session_key_t * p_value,
585 u8 * p_dont_translate, void *d, void *e)
589 snat_session_key_t key0;
590 snat_session_key_t sm0;
591 u8 dont_translate = 0;
596 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
597 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
599 err = icmp_get_key (b0, ip0, &key0);
602 b0->error = node->errors[err];
603 next0 = SNAT_IN2OUT_NEXT_DROP;
606 key0.fib_index = rx_fib_index0;
608 if (snat_static_mapping_match
609 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
611 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
619 if (icmp_type_is_error_message
620 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
622 next0 = SNAT_IN2OUT_NEXT_DROP;
626 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
627 next0 = SNAT_IN2OUT_NEXT_DROP;
632 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
633 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
634 ICMP4_echo_reply || !is_addr_only)
635 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
636 reass.icmp_type_or_tcp_flags)))
638 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
639 next0 = SNAT_IN2OUT_NEXT_DROP;
646 *p_proto = key0.protocol;
647 *p_dont_translate = dont_translate;
652 #ifndef CLIB_MARCH_VARIANT
654 icmp_in2out (snat_main_t * sm,
657 icmp46_header_t * icmp0,
660 vlib_node_runtime_t * node,
661 u32 next0, u32 thread_index, void *d, void *e)
663 vlib_main_t *vm = vlib_get_main ();
664 snat_session_key_t sm0;
666 icmp_echo_header_t *echo0, *inner_echo0 = 0;
667 ip4_header_t *inner_ip0;
669 icmp46_header_t *inner_icmp0;
671 u32 new_addr0, old_addr0;
672 u16 old_id0, new_id0;
673 u16 old_checksum0, new_checksum0;
678 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
680 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
681 &protocol, &sm0, &dont_translate, d,
685 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
688 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
691 ip_incremental_checksum_buffer (vm, b0,
693 (u8 *) vlib_buffer_get_current (b0),
694 ntohs (ip0->length) -
695 ip4_header_bytes (ip0), 0);
696 checksum0 = ~ip_csum_fold (sum0);
697 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
699 next0 = SNAT_IN2OUT_NEXT_DROP;
704 old_addr0 = ip0->src_address.as_u32;
705 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
707 sum0 = ip0->checksum;
708 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
709 src_address /* changed member */ );
710 ip0->checksum = ip_csum_fold (sum0);
712 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
714 if (icmp0->checksum == 0)
715 icmp0->checksum = 0xffff;
717 if (!icmp_type_is_error_message (icmp0->type))
720 if (PREDICT_FALSE (new_id0 != echo0->identifier))
722 old_id0 = echo0->identifier;
724 echo0->identifier = new_id0;
726 sum0 = icmp0->checksum;
728 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
730 icmp0->checksum = ip_csum_fold (sum0);
735 inner_ip0 = (ip4_header_t *) (echo0 + 1);
736 l4_header = ip4_next_header (inner_ip0);
738 if (!ip4_header_checksum_is_valid (inner_ip0))
740 next0 = SNAT_IN2OUT_NEXT_DROP;
744 /* update inner destination IP address */
745 old_addr0 = inner_ip0->dst_address.as_u32;
746 inner_ip0->dst_address = sm0.addr;
747 new_addr0 = inner_ip0->dst_address.as_u32;
748 sum0 = icmp0->checksum;
749 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
750 dst_address /* changed member */ );
751 icmp0->checksum = ip_csum_fold (sum0);
753 /* update inner IP header checksum */
754 old_checksum0 = inner_ip0->checksum;
755 sum0 = inner_ip0->checksum;
756 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
757 dst_address /* changed member */ );
758 inner_ip0->checksum = ip_csum_fold (sum0);
759 new_checksum0 = inner_ip0->checksum;
760 sum0 = icmp0->checksum;
762 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
764 icmp0->checksum = ip_csum_fold (sum0);
768 case NAT_PROTOCOL_ICMP:
769 inner_icmp0 = (icmp46_header_t *) l4_header;
770 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
772 old_id0 = inner_echo0->identifier;
774 inner_echo0->identifier = new_id0;
776 sum0 = icmp0->checksum;
778 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
780 icmp0->checksum = ip_csum_fold (sum0);
782 case NAT_PROTOCOL_UDP:
783 case NAT_PROTOCOL_TCP:
784 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
786 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
788 sum0 = icmp0->checksum;
789 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
791 icmp0->checksum = ip_csum_fold (sum0);
799 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
801 if (sm->deterministic ||
802 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
803 sm->endpoint_dependent))
804 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
813 icmp_in2out_slow_path (snat_main_t * sm,
816 icmp46_header_t * icmp0,
819 vlib_node_runtime_t * node,
821 f64 now, u32 thread_index, snat_session_t ** p_s0)
823 vlib_main_t *vm = vlib_get_main ();
825 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
826 next0, thread_index, p_s0, 0);
827 snat_session_t *s0 = *p_s0;
828 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
831 nat44_session_update_counters (s0, now,
832 vlib_buffer_length_in_chain
833 (vm, b0), thread_index);
834 /* Per-user LRU list maintenance */
835 nat44_session_update_lru (sm, s0, thread_index);
841 nat_in2out_sm_unknown_proto (snat_main_t * sm,
843 ip4_header_t * ip, u32 rx_fib_index)
845 clib_bihash_kv_8_8_t kv, value;
846 snat_static_mapping_t *m;
847 snat_session_key_t m_key;
848 u32 old_addr, new_addr;
851 m_key.addr = ip->src_address;
854 m_key.fib_index = rx_fib_index;
855 kv.key = m_key.as_u64;
856 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
859 m = pool_elt_at_index (sm->static_mappings, value.value);
861 old_addr = ip->src_address.as_u32;
862 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
864 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
865 ip->checksum = ip_csum_fold (sum);
869 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
871 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
872 nat_hairpinning_sm_unknown_proto (sm, b, ip);
879 snat_in2out_node_fn_inline (vlib_main_t * vm,
880 vlib_node_runtime_t * node,
881 vlib_frame_t * frame, int is_slow_path,
882 int is_output_feature)
884 u32 n_left_from, *from, *to_next;
885 snat_in2out_next_t next_index;
886 u32 pkts_processed = 0;
887 snat_main_t *sm = &snat_main;
888 f64 now = vlib_time_now (vm);
889 u32 stats_node_index;
890 u32 thread_index = vm->thread_index;
891 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
894 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
895 sm->in2out_node_index;
897 from = vlib_frame_vector_args (frame);
898 n_left_from = frame->n_vectors;
899 next_index = node->cached_next_index;
901 while (n_left_from > 0)
905 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
907 while (n_left_from >= 4 && n_left_to_next >= 2)
910 vlib_buffer_t *b0, *b1;
912 u32 sw_if_index0, sw_if_index1;
913 ip4_header_t *ip0, *ip1;
914 ip_csum_t sum0, sum1;
915 u32 new_addr0, old_addr0, new_addr1, old_addr1;
916 u16 old_port0, new_port0, old_port1, new_port1;
917 udp_header_t *udp0, *udp1;
918 tcp_header_t *tcp0, *tcp1;
919 icmp46_header_t *icmp0, *icmp1;
920 snat_session_key_t key0, key1;
921 u32 rx_fib_index0, rx_fib_index1;
923 snat_session_t *s0 = 0, *s1 = 0;
924 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
925 u32 iph_offset0 = 0, iph_offset1 = 0;
927 /* Prefetch next iteration. */
929 vlib_buffer_t *p2, *p3;
931 p2 = vlib_get_buffer (vm, from[2]);
932 p3 = vlib_get_buffer (vm, from[3]);
934 vlib_prefetch_buffer_header (p2, LOAD);
935 vlib_prefetch_buffer_header (p3, LOAD);
937 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
938 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
941 /* speculatively enqueue b0 and b1 to the current next frame */
942 to_next[0] = bi0 = from[0];
943 to_next[1] = bi1 = from[1];
949 b0 = vlib_get_buffer (vm, bi0);
950 b1 = vlib_get_buffer (vm, bi1);
952 if (is_output_feature)
953 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
955 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
958 udp0 = ip4_next_header (ip0);
959 tcp0 = (tcp_header_t *) udp0;
960 icmp0 = (icmp46_header_t *) udp0;
962 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
963 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
966 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
968 if (PREDICT_FALSE (ip0->ttl == 1))
970 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
971 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
972 ICMP4_time_exceeded_ttl_exceeded_in_transit,
974 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
978 proto0 = ip_proto_to_nat_proto (ip0->protocol);
980 /* Next configured feature, probably ip4-lookup */
983 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
985 if (nat_in2out_sm_unknown_proto
986 (sm, b0, ip0, rx_fib_index0))
988 next0 = SNAT_IN2OUT_NEXT_DROP;
990 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
996 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
998 next0 = icmp_in2out_slow_path
999 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1000 node, next0, now, thread_index, &s0);
1007 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1009 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1013 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1015 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1020 key0.addr = ip0->src_address;
1021 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1022 key0.protocol = proto0;
1023 key0.fib_index = rx_fib_index0;
1025 kv0.key = key0.as_u64;
1028 (clib_bihash_search_8_8
1029 (&sm->per_thread_data[thread_index].in2out, &kv0,
1034 if (is_output_feature)
1037 (nat_not_translate_output_feature
1039 vnet_buffer (b0)->ip.reass.l4_src_port,
1040 vnet_buffer (b0)->ip.reass.l4_dst_port,
1041 thread_index, sw_if_index0)))
1045 * Send DHCP packets to the ipv4 stack, or we won't
1046 * be able to use dhcp client on the outside interface
1049 (proto0 == NAT_PROTOCOL_UDP
1050 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1051 clib_host_to_net_u16
1052 (UDP_DST_PORT_dhcp_to_server))
1053 && ip0->dst_address.as_u32 == 0xffffffff))
1060 (sm, node, sw_if_index0, ip0, proto0,
1061 rx_fib_index0, thread_index)))
1065 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1066 &s0, node, next0, thread_index, now);
1067 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1070 if (PREDICT_FALSE (!s0))
1075 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1081 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1084 b0->flags |= VNET_BUFFER_F_IS_NATED;
1086 old_addr0 = ip0->src_address.as_u32;
1087 ip0->src_address = s0->out2in.addr;
1088 new_addr0 = ip0->src_address.as_u32;
1089 if (!is_output_feature)
1090 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1092 sum0 = ip0->checksum;
1093 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1095 src_address /* changed member */ );
1096 ip0->checksum = ip_csum_fold (sum0);
1099 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1101 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1103 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1104 new_port0 = udp0->src_port = s0->out2in.port;
1105 sum0 = tcp0->checksum;
1106 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1108 dst_address /* changed member */ );
1109 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1110 ip4_header_t /* cheat */ ,
1111 length /* changed member */ );
1112 mss_clamping (sm, tcp0, &sum0);
1113 tcp0->checksum = ip_csum_fold (sum0);
1119 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1121 if (PREDICT_FALSE (udp0->checksum))
1123 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1124 new_port0 = udp0->src_port = s0->out2in.port;
1125 sum0 = udp0->checksum;
1126 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1129 ip_csum_update (sum0, old_port0, new_port0,
1130 ip4_header_t /* cheat */ ,
1131 length /* changed member */ );
1132 udp0->checksum = ip_csum_fold (sum0);
1139 nat44_session_update_counters (s0, now,
1140 vlib_buffer_length_in_chain (vm, b0),
1142 /* Per-user LRU list maintenance */
1143 nat44_session_update_lru (sm, s0, thread_index);
1146 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1147 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1149 snat_in2out_trace_t *t =
1150 vlib_add_trace (vm, node, b0, sizeof (*t));
1151 t->is_slow_path = is_slow_path;
1152 t->sw_if_index = sw_if_index0;
1153 t->next_index = next0;
1154 t->session_index = ~0;
1157 s0 - sm->per_thread_data[thread_index].sessions;
1160 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1162 if (is_output_feature)
1163 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1165 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1168 udp1 = ip4_next_header (ip1);
1169 tcp1 = (tcp_header_t *) udp1;
1170 icmp1 = (icmp46_header_t *) udp1;
1172 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1173 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1176 if (PREDICT_FALSE (ip1->ttl == 1))
1178 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1179 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1180 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1182 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1186 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1188 /* Next configured feature, probably ip4-lookup */
1191 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1193 if (nat_in2out_sm_unknown_proto
1194 (sm, b1, ip1, rx_fib_index1))
1196 next1 = SNAT_IN2OUT_NEXT_DROP;
1198 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1204 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1206 next1 = icmp_in2out_slow_path
1207 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1208 next1, now, thread_index, &s1);
1215 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1217 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1221 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1223 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1228 key1.addr = ip1->src_address;
1229 key1.port = vnet_buffer (b1)->ip.reass.l4_src_port;
1230 key1.protocol = proto1;
1231 key1.fib_index = rx_fib_index1;
1233 kv1.key = key1.as_u64;
1236 (clib_bihash_search_8_8
1237 (&sm->per_thread_data[thread_index].in2out, &kv1,
1242 if (is_output_feature)
1245 (nat_not_translate_output_feature
1247 vnet_buffer (b1)->ip.reass.l4_src_port,
1248 vnet_buffer (b1)->ip.reass.l4_dst_port,
1249 thread_index, sw_if_index1)))
1253 * Send DHCP packets to the ipv4 stack, or we won't
1254 * be able to use dhcp client on the outside interface
1257 (proto1 == NAT_PROTOCOL_UDP
1258 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1259 clib_host_to_net_u16
1260 (UDP_DST_PORT_dhcp_to_server))
1261 && ip1->dst_address.as_u32 == 0xffffffff))
1268 (sm, node, sw_if_index1, ip1, proto1,
1269 rx_fib_index1, thread_index)))
1273 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1274 &s1, node, next1, thread_index, now);
1275 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1278 if (PREDICT_FALSE (!s1))
1283 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1289 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1292 b1->flags |= VNET_BUFFER_F_IS_NATED;
1294 old_addr1 = ip1->src_address.as_u32;
1295 ip1->src_address = s1->out2in.addr;
1296 new_addr1 = ip1->src_address.as_u32;
1297 if (!is_output_feature)
1298 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1300 sum1 = ip1->checksum;
1301 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1303 src_address /* changed member */ );
1304 ip1->checksum = ip_csum_fold (sum1);
1306 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1308 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1310 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1311 new_port1 = udp1->src_port = s1->out2in.port;
1312 sum1 = tcp1->checksum;
1313 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1315 dst_address /* changed member */ );
1316 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1317 ip4_header_t /* cheat */ ,
1318 length /* changed member */ );
1319 mss_clamping (sm, tcp1, &sum1);
1320 tcp1->checksum = ip_csum_fold (sum1);
1326 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1328 if (PREDICT_FALSE (udp1->checksum))
1330 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1331 new_port1 = udp1->src_port = s1->out2in.port;
1332 sum1 = udp1->checksum;
1333 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1336 ip_csum_update (sum1, old_port1, new_port1,
1337 ip4_header_t /* cheat */ ,
1338 length /* changed member */ );
1339 udp1->checksum = ip_csum_fold (sum1);
1346 nat44_session_update_counters (s1, now,
1347 vlib_buffer_length_in_chain (vm, b1),
1349 /* Per-user LRU list maintenance */
1350 nat44_session_update_lru (sm, s1, thread_index);
1353 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1354 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1356 snat_in2out_trace_t *t =
1357 vlib_add_trace (vm, node, b1, sizeof (*t));
1358 t->sw_if_index = sw_if_index1;
1359 t->next_index = next1;
1360 t->session_index = ~0;
1363 s1 - sm->per_thread_data[thread_index].sessions;
1366 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1368 /* verify speculative enqueues, maybe switch current next frame */
1369 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1370 to_next, n_left_to_next,
1371 bi0, bi1, next0, next1);
1374 while (n_left_from > 0 && n_left_to_next > 0)
1382 u32 new_addr0, old_addr0;
1383 u16 old_port0, new_port0;
1386 icmp46_header_t *icmp0;
1387 snat_session_key_t key0;
1390 snat_session_t *s0 = 0;
1391 clib_bihash_kv_8_8_t kv0, value0;
1392 u32 iph_offset0 = 0;
1394 /* speculatively enqueue b0 to the current next frame */
1400 n_left_to_next -= 1;
1402 b0 = vlib_get_buffer (vm, bi0);
1403 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1405 if (is_output_feature)
1406 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1408 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1411 udp0 = ip4_next_header (ip0);
1412 tcp0 = (tcp_header_t *) udp0;
1413 icmp0 = (icmp46_header_t *) udp0;
1415 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1416 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1419 if (PREDICT_FALSE (ip0->ttl == 1))
1421 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1422 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1423 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1425 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1429 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1431 /* Next configured feature, probably ip4-lookup */
1434 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1436 if (nat_in2out_sm_unknown_proto
1437 (sm, b0, ip0, rx_fib_index0))
1439 next0 = SNAT_IN2OUT_NEXT_DROP;
1441 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1447 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1449 next0 = icmp_in2out_slow_path
1450 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1451 next0, now, thread_index, &s0);
1458 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1460 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1464 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1466 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1471 key0.addr = ip0->src_address;
1472 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1473 key0.protocol = proto0;
1474 key0.fib_index = rx_fib_index0;
1476 kv0.key = key0.as_u64;
1478 if (clib_bihash_search_8_8
1479 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1483 if (is_output_feature)
1486 (nat_not_translate_output_feature
1488 vnet_buffer (b0)->ip.reass.l4_src_port,
1489 vnet_buffer (b0)->ip.reass.l4_dst_port,
1490 thread_index, sw_if_index0)))
1494 * Send DHCP packets to the ipv4 stack, or we won't
1495 * be able to use dhcp client on the outside interface
1498 (proto0 == NAT_PROTOCOL_UDP
1499 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1500 clib_host_to_net_u16
1501 (UDP_DST_PORT_dhcp_to_server))
1502 && ip0->dst_address.as_u32 == 0xffffffff))
1509 (sm, node, sw_if_index0, ip0, proto0,
1510 rx_fib_index0, thread_index)))
1514 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1515 &s0, node, next0, thread_index, now);
1517 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1520 if (PREDICT_FALSE (!s0))
1525 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1531 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1534 b0->flags |= VNET_BUFFER_F_IS_NATED;
1536 old_addr0 = ip0->src_address.as_u32;
1537 ip0->src_address = s0->out2in.addr;
1538 new_addr0 = ip0->src_address.as_u32;
1539 if (!is_output_feature)
1540 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1542 sum0 = ip0->checksum;
1543 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1545 src_address /* changed member */ );
1546 ip0->checksum = ip_csum_fold (sum0);
1548 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1550 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1552 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1553 new_port0 = udp0->src_port = s0->out2in.port;
1554 sum0 = tcp0->checksum;
1555 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1557 dst_address /* changed member */ );
1558 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1559 ip4_header_t /* cheat */ ,
1560 length /* changed member */ );
1561 mss_clamping (sm, tcp0, &sum0);
1562 tcp0->checksum = ip_csum_fold (sum0);
1568 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1570 if (PREDICT_FALSE (udp0->checksum))
1572 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1573 new_port0 = udp0->src_port = s0->out2in.port;
1574 sum0 = udp0->checksum;
1575 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1578 ip_csum_update (sum0, old_port0, new_port0,
1579 ip4_header_t /* cheat */ ,
1580 length /* changed member */ );
1581 udp0->checksum = ip_csum_fold (sum0);
1588 nat44_session_update_counters (s0, now,
1589 vlib_buffer_length_in_chain (vm, b0),
1591 /* Per-user LRU list maintenance */
1592 nat44_session_update_lru (sm, s0, thread_index);
1595 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1596 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1598 snat_in2out_trace_t *t =
1599 vlib_add_trace (vm, node, b0, sizeof (*t));
1600 t->is_slow_path = is_slow_path;
1601 t->sw_if_index = sw_if_index0;
1602 t->next_index = next0;
1603 t->session_index = ~0;
1606 s0 - sm->per_thread_data[thread_index].sessions;
1609 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1611 /* verify speculative enqueue, maybe switch current next frame */
1612 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1613 to_next, n_left_to_next,
1617 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1620 vlib_node_increment_counter (vm, stats_node_index,
1621 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1623 vlib_node_increment_counter (vm, stats_node_index,
1624 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1625 vlib_node_increment_counter (vm, stats_node_index,
1626 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1627 vlib_node_increment_counter (vm, stats_node_index,
1628 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1629 vlib_node_increment_counter (vm, stats_node_index,
1630 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1632 vlib_node_increment_counter (vm, stats_node_index,
1633 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1635 return frame->n_vectors;
1638 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1639 vlib_node_runtime_t * node,
1640 vlib_frame_t * frame)
1642 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1647 VLIB_REGISTER_NODE (snat_in2out_node) = {
1648 .name = "nat44-in2out",
1649 .vector_size = sizeof (u32),
1650 .format_trace = format_snat_in2out_trace,
1651 .type = VLIB_NODE_TYPE_INTERNAL,
1653 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1654 .error_strings = snat_in2out_error_strings,
1656 .runtime_data_bytes = sizeof (snat_runtime_t),
1658 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1660 /* edit / add dispositions here */
1662 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1663 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1664 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1665 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1670 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1671 vlib_node_runtime_t * node,
1672 vlib_frame_t * frame)
1674 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1679 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1680 .name = "nat44-in2out-output",
1681 .vector_size = sizeof (u32),
1682 .format_trace = format_snat_in2out_trace,
1683 .type = VLIB_NODE_TYPE_INTERNAL,
1685 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1686 .error_strings = snat_in2out_error_strings,
1688 .runtime_data_bytes = sizeof (snat_runtime_t),
1690 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1692 /* edit / add dispositions here */
1694 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1695 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1696 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1697 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1702 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1703 vlib_node_runtime_t * node,
1704 vlib_frame_t * frame)
1706 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1711 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1712 .name = "nat44-in2out-slowpath",
1713 .vector_size = sizeof (u32),
1714 .format_trace = format_snat_in2out_trace,
1715 .type = VLIB_NODE_TYPE_INTERNAL,
1717 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1718 .error_strings = snat_in2out_error_strings,
1720 .runtime_data_bytes = sizeof (snat_runtime_t),
1722 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1724 /* edit / add dispositions here */
1726 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1727 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1728 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1729 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1734 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1735 vlib_node_runtime_t * node,
1736 vlib_frame_t * frame)
1738 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1743 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1744 .name = "nat44-in2out-output-slowpath",
1745 .vector_size = sizeof (u32),
1746 .format_trace = format_snat_in2out_trace,
1747 .type = VLIB_NODE_TYPE_INTERNAL,
1749 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1750 .error_strings = snat_in2out_error_strings,
1752 .runtime_data_bytes = sizeof (snat_runtime_t),
1754 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1756 /* edit / add dispositions here */
1758 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1759 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1760 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1761 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1766 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1767 vlib_node_runtime_t * node,
1768 vlib_frame_t * frame)
1770 u32 n_left_from, *from, *to_next;
1771 snat_in2out_next_t next_index;
1772 u32 pkts_processed = 0;
1773 snat_main_t *sm = &snat_main;
1774 u32 stats_node_index;
1776 stats_node_index = sm->in2out_fast_node_index;
1778 from = vlib_frame_vector_args (frame);
1779 n_left_from = frame->n_vectors;
1780 next_index = node->cached_next_index;
1782 while (n_left_from > 0)
1786 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1788 while (n_left_from > 0 && n_left_to_next > 0)
1796 u32 new_addr0, old_addr0;
1797 u16 old_port0, new_port0;
1800 icmp46_header_t *icmp0;
1801 snat_session_key_t key0, sm0;
1805 /* speculatively enqueue b0 to the current next frame */
1811 n_left_to_next -= 1;
1813 b0 = vlib_get_buffer (vm, bi0);
1814 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1816 ip0 = vlib_buffer_get_current (b0);
1817 udp0 = ip4_next_header (ip0);
1818 tcp0 = (tcp_header_t *) udp0;
1819 icmp0 = (icmp46_header_t *) udp0;
1821 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1823 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1825 if (PREDICT_FALSE (ip0->ttl == 1))
1827 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1828 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1829 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1831 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1835 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1837 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1840 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1842 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1843 rx_fib_index0, node, next0, ~0, 0, 0);
1847 key0.addr = ip0->src_address;
1848 key0.protocol = proto0;
1849 key0.port = udp0->src_port;
1850 key0.fib_index = rx_fib_index0;
1852 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
1854 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1855 next0 = SNAT_IN2OUT_NEXT_DROP;
1859 new_addr0 = sm0.addr.as_u32;
1860 new_port0 = sm0.port;
1861 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1862 old_addr0 = ip0->src_address.as_u32;
1863 ip0->src_address.as_u32 = new_addr0;
1865 sum0 = ip0->checksum;
1866 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1868 src_address /* changed member */ );
1869 ip0->checksum = ip_csum_fold (sum0);
1871 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1873 old_port0 = udp0->src_port;
1874 udp0->src_port = new_port0;
1876 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1878 sum0 = tcp0->checksum;
1879 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1881 dst_address /* changed member */ );
1882 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1883 ip4_header_t /* cheat */ ,
1884 length /* changed member */ );
1885 mss_clamping (sm, tcp0, &sum0);
1886 tcp0->checksum = ip_csum_fold (sum0);
1888 else if (udp0->checksum)
1890 sum0 = udp0->checksum;
1891 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1893 dst_address /* changed member */ );
1894 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1895 ip4_header_t /* cheat */ ,
1896 length /* changed member */ );
1897 udp0->checksum = ip_csum_fold (sum0);
1902 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1904 sum0 = tcp0->checksum;
1905 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1907 dst_address /* changed member */ );
1908 mss_clamping (sm, tcp0, &sum0);
1909 tcp0->checksum = ip_csum_fold (sum0);
1911 else if (udp0->checksum)
1913 sum0 = udp0->checksum;
1914 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1916 dst_address /* changed member */ );
1917 udp0->checksum = ip_csum_fold (sum0);
1922 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
1925 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1926 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1928 snat_in2out_trace_t *t =
1929 vlib_add_trace (vm, node, b0, sizeof (*t));
1930 t->sw_if_index = sw_if_index0;
1931 t->next_index = next0;
1934 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1936 /* verify speculative enqueue, maybe switch current next frame */
1937 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1938 to_next, n_left_to_next,
1942 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1945 vlib_node_increment_counter (vm, stats_node_index,
1946 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1948 return frame->n_vectors;
1953 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1954 .name = "nat44-in2out-fast",
1955 .vector_size = sizeof (u32),
1956 .format_trace = format_snat_in2out_fast_trace,
1957 .type = VLIB_NODE_TYPE_INTERNAL,
1959 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1960 .error_strings = snat_in2out_error_strings,
1962 .runtime_data_bytes = sizeof (snat_runtime_t),
1964 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1966 /* edit / add dispositions here */
1968 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1969 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1970 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1971 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1977 * fd.io coding-style-patch-verification: ON
1980 * eval: (c-set-style "gnu")