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>
38 #include <nat/lib/nat_inlines.h>
47 } snat_in2out_trace_t;
49 /* packet trace format function */
51 format_snat_in2out_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
58 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
60 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
61 t->sw_if_index, t->next_index, t->session_index);
62 if (t->is_hairpinning)
64 s = format (s, ", with-hairpinning");
71 format_snat_in2out_fast_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
77 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
78 t->sw_if_index, t->next_index);
83 #define foreach_snat_in2out_error \
84 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
85 _(IN2OUT_PACKETS, "good in2out packets processed") \
86 _(OUT_OF_PORTS, "out of ports") \
87 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
88 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
89 _(NO_TRANSLATION, "no translation") \
90 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
91 _(DROP_FRAGMENT, "drop fragment") \
92 _(TCP_PACKETS, "TCP packets") \
93 _(UDP_PACKETS, "UDP packets") \
94 _(ICMP_PACKETS, "ICMP packets") \
95 _(OTHER_PACKETS, "other protocol packets") \
96 _(FRAGMENTS, "fragments") \
97 _(CACHED_FRAGMENTS, "cached fragments") \
98 _(PROCESSED_FRAGMENTS, "processed fragments")
102 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
103 foreach_snat_in2out_error
106 } snat_in2out_error_t;
108 static char *snat_in2out_error_strings[] = {
109 #define _(sym,string) string,
110 foreach_snat_in2out_error
116 SNAT_IN2OUT_NEXT_LOOKUP,
117 SNAT_IN2OUT_NEXT_DROP,
118 SNAT_IN2OUT_NEXT_ICMP_ERROR,
119 SNAT_IN2OUT_NEXT_SLOW_PATH,
121 } snat_in2out_next_t;
124 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
125 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
126 u32 rx_fib_index0, u32 thread_index)
128 udp_header_t *udp0 = ip4_next_header (ip0);
129 clib_bihash_kv_8_8_t kv0, value0;
131 init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
134 /* NAT packet aimed at external address if */
135 /* has active sessions */
136 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
139 /* or is static mappings */
140 ip4_address_t dummy_addr;
143 if (!snat_static_mapping_match
144 (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
145 proto0, &dummy_addr, &dummy_port, &dummy_fib_index, 1, 0, 0, 0, 0,
152 if (sm->forwarding_enabled)
155 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
160 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
161 u32 proto0, u16 src_port, u16 dst_port,
162 u32 thread_index, u32 sw_if_index)
164 clib_bihash_kv_8_8_t kv0, value0;
168 init_nat_k (&kv0, ip0->src_address, src_port,
169 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
171 if (!clib_bihash_search_8_8
172 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
176 init_nat_k (&kv0, ip0->dst_address, dst_port,
177 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
178 if (!clib_bihash_search_8_8
179 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
183 pool_foreach (i, sm->output_feature_interfaces,
185 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
195 #ifndef CLIB_MARCH_VARIANT
197 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
199 snat_main_t *sm = &snat_main;
200 nat44_is_idle_session_ctx_t *ctx = arg;
202 u64 sess_timeout_time;
203 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
205 clib_bihash_kv_8_8_t s_kv;
207 s = pool_elt_at_index (tsm->sessions, kv->value);
208 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
209 if (ctx->now >= sess_timeout_time)
211 init_nat_o2i_k (&s_kv, s);
212 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
213 nat_elog_warn ("out2in key del failed");
215 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
216 s->in2out.addr.as_u32,
217 s->out2in.addr.as_u32,
221 s->in2out.fib_index);
223 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
224 &s->in2out.addr, s->in2out.port,
225 &s->out2in.addr, s->out2in.port, s->nat_proto);
227 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
228 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
231 if (!snat_is_session_static (s))
232 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
234 s->out2in.port, s->nat_proto);
236 nat44_delete_session (sm, s, ctx->thread_index);
245 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
247 ip4_address_t i2o_addr,
250 nat_protocol_t nat_proto,
251 snat_session_t ** sessionp,
252 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
255 snat_session_t *s = 0;
256 clib_bihash_kv_8_8_t kv0;
258 nat_outside_fib_t *outside_fib;
259 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
262 .fp_proto = FIB_PROTOCOL_IP4,
265 .ip4.as_u32 = ip0->dst_address.as_u32,
268 nat44_is_idle_session_ctx_t ctx0;
269 ip4_address_t sm_addr;
273 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
275 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
276 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
277 nat_elog_notice ("maximum sessions exceeded");
278 return SNAT_IN2OUT_NEXT_DROP;
281 /* First try to match static mapping by local address and port */
282 if (snat_static_mapping_match
283 (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
284 &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat))
286 /* Try to create dynamic translation */
287 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
293 [thread_index].snat_thread_index))
295 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
296 return SNAT_IN2OUT_NEXT_DROP;
301 if (PREDICT_FALSE (identity_nat))
310 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
314 nat_elog_warn ("create NAT user failed");
315 return SNAT_IN2OUT_NEXT_DROP;
318 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
321 nat44_delete_user_with_no_session (sm, u, thread_index);
322 nat_elog_warn ("create NAT session failed");
323 return SNAT_IN2OUT_NEXT_DROP;
327 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
328 user_session_increment (sm, u, is_sm);
329 s->in2out.addr = i2o_addr;
330 s->in2out.port = i2o_port;
331 s->in2out.fib_index = rx_fib_index0;
332 s->nat_proto = nat_proto;
333 s->out2in.addr = sm_addr;
334 s->out2in.port = sm_port;
335 s->out2in.fib_index = sm->outside_fib_index;
336 switch (vec_len (sm->outside_fibs))
339 s->out2in.fib_index = sm->outside_fib_index;
342 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
346 vec_foreach (outside_fib, sm->outside_fibs)
348 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
349 if (FIB_NODE_INDEX_INVALID != fei)
351 if (fib_entry_get_resolving_interface (fei) != ~0)
353 s->out2in.fib_index = outside_fib->fib_index;
361 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
362 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
365 /* Add to translation hashes */
367 ctx0.thread_index = thread_index;
368 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
369 if (clib_bihash_add_or_overwrite_stale_8_8
370 (&sm->per_thread_data[thread_index].in2out, &kv0,
371 nat44_i2o_is_idle_session_cb, &ctx0))
372 nat_elog_notice ("in2out key add failed");
374 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
375 if (clib_bihash_add_or_overwrite_stale_8_8
376 (&sm->per_thread_data[thread_index].out2in, &kv0,
377 nat44_o2i_is_idle_session_cb, &ctx0))
378 nat_elog_notice ("out2in key add failed");
381 snat_ipfix_logging_nat44_ses_create (thread_index,
382 s->in2out.addr.as_u32,
383 s->out2in.addr.as_u32,
386 s->out2in.port, s->in2out.fib_index);
388 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
389 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
390 s->out2in.port, s->nat_proto);
392 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
393 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
394 &s->ext_host_nat_addr, s->ext_host_nat_port,
395 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
400 #ifndef CLIB_MARCH_VARIANT
401 static_always_inline snat_in2out_error_t
402 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
403 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
405 icmp46_header_t *icmp0;
406 icmp_echo_header_t *echo0, *inner_echo0 = 0;
407 ip4_header_t *inner_ip0 = 0;
409 icmp46_header_t *inner_icmp0;
411 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
412 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
414 if (!icmp_type_is_error_message
415 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
417 *nat_proto = NAT_PROTOCOL_ICMP;
418 *addr = ip0->src_address;
419 *port = vnet_buffer (b)->ip.reass.l4_src_port;
423 inner_ip0 = (ip4_header_t *) (echo0 + 1);
424 l4_header = ip4_next_header (inner_ip0);
425 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
426 *addr = inner_ip0->dst_address;
429 case NAT_PROTOCOL_ICMP:
430 inner_icmp0 = (icmp46_header_t *) l4_header;
431 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
432 *port = inner_echo0->identifier;
434 case NAT_PROTOCOL_UDP:
435 case NAT_PROTOCOL_TCP:
436 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
439 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
442 return -1; /* success */
446 * Get address and port values to be used for ICMP packet translation
447 * and create session if needed
449 * @param[in,out] sm NAT main
450 * @param[in,out] node NAT node runtime
451 * @param[in] thread_index thread index
452 * @param[in,out] b0 buffer containing packet to be translated
453 * @param[in,out] ip0 ip header
454 * @param[out] p_proto protocol used for matching
455 * @param[out] p_value address and port after NAT translation
456 * @param[out] p_dont_translate if packet should not be translated
457 * @param d optional parameter
458 * @param e optional parameter
461 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
462 u32 thread_index, vlib_buffer_t * b0,
463 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
464 u32 * fib_index, nat_protocol_t * proto, void *d,
465 void *e, u8 * dont_translate)
467 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
469 snat_session_t *s0 = 0;
470 clib_bihash_kv_8_8_t kv0, value0;
473 vlib_main_t *vm = vlib_get_main ();
476 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
477 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
479 err = icmp_get_key (b0, ip0, addr, port, proto);
482 b0->error = node->errors[err];
483 next0 = SNAT_IN2OUT_NEXT_DROP;
487 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
488 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
490 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
493 (nat_not_translate_output_feature
494 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
502 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
503 ip0, NAT_PROTOCOL_ICMP,
504 *fib_index, thread_index)))
512 (icmp_type_is_error_message
513 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
515 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
516 next0 = SNAT_IN2OUT_NEXT_DROP;
521 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
522 next0, thread_index, vlib_time_now (vm));
524 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
536 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
538 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
540 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
541 reass.icmp_type_or_tcp_flags)))
543 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
544 next0 = SNAT_IN2OUT_NEXT_DROP;
548 s0 = pool_elt_at_index (tsm->sessions, value0.value);
554 *addr = s0->out2in.addr;
555 *port = s0->out2in.port;
556 *fib_index = s0->out2in.fib_index;
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[in,out] ip0 ip header
573 * @param[out] p_proto protocol used for matching
574 * @param[out] p_value address and port after NAT translation
575 * @param[out] p_dont_translate if packet should not be translated
576 * @param d optional parameter
577 * @param e optional parameter
580 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
581 u32 thread_index, vlib_buffer_t * b0,
582 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
583 u32 * fib_index, nat_protocol_t * proto, void *d,
584 void *e, u8 * dont_translate)
592 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
593 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
595 err = icmp_get_key (b0, ip0, addr, port, proto);
598 b0->error = node->errors[err];
599 next0 = SNAT_IN2OUT_NEXT_DROP;
603 ip4_address_t sm_addr;
607 if (snat_static_mapping_match
608 (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
609 &sm_fib_index, 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;
648 #ifndef CLIB_MARCH_VARIANT
650 icmp_in2out (snat_main_t * sm,
653 icmp46_header_t * icmp0,
656 vlib_node_runtime_t * node,
657 u32 next0, u32 thread_index, void *d, void *e)
659 vlib_main_t *vm = vlib_get_main ();
663 nat_protocol_t protocol;
664 icmp_echo_header_t *echo0, *inner_echo0 = 0;
665 ip4_header_t *inner_ip0;
667 icmp46_header_t *inner_icmp0;
669 u32 new_addr0, old_addr0;
670 u16 old_id0, new_id0;
671 u16 old_checksum0, new_checksum0;
676 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
679 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
680 &fib_index, &protocol, d, e, &dont_translate);
683 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
686 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
689 ip_incremental_checksum_buffer (vm, b0,
691 (u8 *) vlib_buffer_get_current (b0),
692 ntohs (ip0->length) -
693 ip4_header_bytes (ip0), 0);
694 checksum0 = ~ip_csum_fold (sum0);
695 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
697 next0 = SNAT_IN2OUT_NEXT_DROP;
702 old_addr0 = ip0->src_address.as_u32;
703 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
705 sum0 = ip0->checksum;
706 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
707 src_address /* changed member */ );
708 ip0->checksum = ip_csum_fold (sum0);
710 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
712 if (icmp0->checksum == 0)
713 icmp0->checksum = 0xffff;
715 if (!icmp_type_is_error_message (icmp0->type))
718 if (PREDICT_FALSE (new_id0 != echo0->identifier))
720 old_id0 = echo0->identifier;
722 echo0->identifier = new_id0;
724 sum0 = icmp0->checksum;
726 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
728 icmp0->checksum = ip_csum_fold (sum0);
733 inner_ip0 = (ip4_header_t *) (echo0 + 1);
734 l4_header = ip4_next_header (inner_ip0);
736 if (!ip4_header_checksum_is_valid (inner_ip0))
738 next0 = SNAT_IN2OUT_NEXT_DROP;
742 /* update inner destination IP address */
743 old_addr0 = inner_ip0->dst_address.as_u32;
744 inner_ip0->dst_address = addr;
745 new_addr0 = inner_ip0->dst_address.as_u32;
746 sum0 = icmp0->checksum;
747 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
748 dst_address /* changed member */ );
749 icmp0->checksum = ip_csum_fold (sum0);
751 /* update inner IP header checksum */
752 old_checksum0 = inner_ip0->checksum;
753 sum0 = inner_ip0->checksum;
754 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
755 dst_address /* changed member */ );
756 inner_ip0->checksum = ip_csum_fold (sum0);
757 new_checksum0 = inner_ip0->checksum;
758 sum0 = icmp0->checksum;
760 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
762 icmp0->checksum = ip_csum_fold (sum0);
766 case NAT_PROTOCOL_ICMP:
767 inner_icmp0 = (icmp46_header_t *) l4_header;
768 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
770 old_id0 = inner_echo0->identifier;
772 inner_echo0->identifier = new_id0;
774 sum0 = icmp0->checksum;
776 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
778 icmp0->checksum = ip_csum_fold (sum0);
780 case NAT_PROTOCOL_UDP:
781 case NAT_PROTOCOL_TCP:
782 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
784 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
786 sum0 = icmp0->checksum;
787 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
789 icmp0->checksum = ip_csum_fold (sum0);
797 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
799 if (sm->deterministic ||
800 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
801 sm->endpoint_dependent))
802 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
811 icmp_in2out_slow_path (snat_main_t * sm,
814 icmp46_header_t * icmp0,
817 vlib_node_runtime_t * node,
819 f64 now, u32 thread_index, snat_session_t ** p_s0)
821 vlib_main_t *vm = vlib_get_main ();
823 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
824 next0, thread_index, p_s0, 0);
825 snat_session_t *s0 = *p_s0;
826 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
829 nat44_session_update_counters (s0, now,
830 vlib_buffer_length_in_chain
831 (vm, b0), thread_index);
832 /* Per-user LRU list maintenance */
833 nat44_session_update_lru (sm, s0, thread_index);
839 nat_in2out_sm_unknown_proto (snat_main_t * sm,
841 ip4_header_t * ip, u32 rx_fib_index)
843 clib_bihash_kv_8_8_t kv, value;
844 snat_static_mapping_t *m;
845 u32 old_addr, new_addr;
848 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
849 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
852 m = pool_elt_at_index (sm->static_mappings, value.value);
854 old_addr = ip->src_address.as_u32;
855 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
857 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
858 ip->checksum = ip_csum_fold (sum);
862 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
864 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
865 nat_hairpinning_sm_unknown_proto (sm, b, ip);
872 snat_in2out_node_fn_inline (vlib_main_t * vm,
873 vlib_node_runtime_t * node,
874 vlib_frame_t * frame, int is_slow_path,
875 int is_output_feature)
877 u32 n_left_from, *from, *to_next;
878 snat_in2out_next_t next_index;
879 u32 pkts_processed = 0;
880 snat_main_t *sm = &snat_main;
881 f64 now = vlib_time_now (vm);
882 u32 stats_node_index;
883 u32 thread_index = vm->thread_index;
884 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
887 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
888 sm->in2out_node_index;
890 from = vlib_frame_vector_args (frame);
891 n_left_from = frame->n_vectors;
892 next_index = node->cached_next_index;
894 while (n_left_from > 0)
898 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
900 while (n_left_from >= 4 && n_left_to_next >= 2)
903 vlib_buffer_t *b0, *b1;
905 u32 sw_if_index0, sw_if_index1;
906 ip4_header_t *ip0, *ip1;
907 ip_csum_t sum0, sum1;
908 u32 new_addr0, old_addr0, new_addr1, old_addr1;
909 u16 old_port0, new_port0, old_port1, new_port1;
910 udp_header_t *udp0, *udp1;
911 tcp_header_t *tcp0, *tcp1;
912 icmp46_header_t *icmp0, *icmp1;
913 u32 rx_fib_index0, rx_fib_index1;
915 snat_session_t *s0 = 0, *s1 = 0;
916 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
917 u32 iph_offset0 = 0, iph_offset1 = 0;
919 /* Prefetch next iteration. */
921 vlib_buffer_t *p2, *p3;
923 p2 = vlib_get_buffer (vm, from[2]);
924 p3 = vlib_get_buffer (vm, from[3]);
926 vlib_prefetch_buffer_header (p2, LOAD);
927 vlib_prefetch_buffer_header (p3, LOAD);
929 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
930 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
933 /* speculatively enqueue b0 and b1 to the current next frame */
934 to_next[0] = bi0 = from[0];
935 to_next[1] = bi1 = from[1];
941 b0 = vlib_get_buffer (vm, bi0);
942 b1 = vlib_get_buffer (vm, bi1);
944 if (is_output_feature)
945 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
947 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
950 udp0 = ip4_next_header (ip0);
951 tcp0 = (tcp_header_t *) udp0;
952 icmp0 = (icmp46_header_t *) udp0;
954 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
955 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
958 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
960 if (PREDICT_FALSE (ip0->ttl == 1))
962 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
963 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
964 ICMP4_time_exceeded_ttl_exceeded_in_transit,
966 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
970 proto0 = ip_proto_to_nat_proto (ip0->protocol);
972 /* Next configured feature, probably ip4-lookup */
975 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
977 if (nat_in2out_sm_unknown_proto
978 (sm, b0, ip0, rx_fib_index0))
980 next0 = SNAT_IN2OUT_NEXT_DROP;
982 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
988 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
990 next0 = icmp_in2out_slow_path
991 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
992 node, next0, now, thread_index, &s0);
999 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1001 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1005 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1007 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1012 init_nat_k (&kv0, ip0->src_address,
1013 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1016 (clib_bihash_search_8_8
1017 (&sm->per_thread_data[thread_index].in2out, &kv0,
1022 if (is_output_feature)
1025 (nat_not_translate_output_feature
1027 vnet_buffer (b0)->ip.reass.l4_src_port,
1028 vnet_buffer (b0)->ip.reass.l4_dst_port,
1029 thread_index, sw_if_index0)))
1033 * Send DHCP packets to the ipv4 stack, or we won't
1034 * be able to use dhcp client on the outside interface
1037 (proto0 == NAT_PROTOCOL_UDP
1038 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1039 clib_host_to_net_u16
1040 (UDP_DST_PORT_dhcp_to_server))
1041 && ip0->dst_address.as_u32 == 0xffffffff))
1048 (sm, node, sw_if_index0, ip0, proto0,
1049 rx_fib_index0, thread_index)))
1053 next0 = slow_path (sm, b0, ip0,
1055 vnet_buffer (b0)->ip.reass.l4_src_port,
1058 &s0, node, next0, thread_index, now);
1059 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1062 if (PREDICT_FALSE (!s0))
1067 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1073 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1076 b0->flags |= VNET_BUFFER_F_IS_NATED;
1078 old_addr0 = ip0->src_address.as_u32;
1079 ip0->src_address = s0->out2in.addr;
1080 new_addr0 = ip0->src_address.as_u32;
1081 if (!is_output_feature)
1082 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1084 sum0 = ip0->checksum;
1085 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1087 src_address /* changed member */ );
1088 ip0->checksum = ip_csum_fold (sum0);
1091 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1093 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1095 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1096 new_port0 = udp0->src_port = s0->out2in.port;
1097 sum0 = tcp0->checksum;
1098 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1100 dst_address /* changed member */ );
1101 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1102 ip4_header_t /* cheat */ ,
1103 length /* changed member */ );
1104 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1105 tcp0->checksum = ip_csum_fold (sum0);
1111 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1113 udp0->src_port = s0->out2in.port;
1114 if (PREDICT_FALSE (udp0->checksum))
1116 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1117 new_port0 = udp0->src_port;
1118 sum0 = udp0->checksum;
1119 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1122 ip_csum_update (sum0, old_port0, new_port0,
1123 ip4_header_t /* cheat */ ,
1124 length /* changed member */ );
1125 udp0->checksum = ip_csum_fold (sum0);
1132 nat44_session_update_counters (s0, now,
1133 vlib_buffer_length_in_chain (vm, b0),
1135 /* Per-user LRU list maintenance */
1136 nat44_session_update_lru (sm, s0, thread_index);
1139 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1140 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1142 snat_in2out_trace_t *t =
1143 vlib_add_trace (vm, node, b0, sizeof (*t));
1144 t->is_slow_path = is_slow_path;
1145 t->sw_if_index = sw_if_index0;
1146 t->next_index = next0;
1147 t->session_index = ~0;
1150 s0 - sm->per_thread_data[thread_index].sessions;
1153 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1155 if (is_output_feature)
1156 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1158 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1161 udp1 = ip4_next_header (ip1);
1162 tcp1 = (tcp_header_t *) udp1;
1163 icmp1 = (icmp46_header_t *) udp1;
1165 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1166 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1169 if (PREDICT_FALSE (ip1->ttl == 1))
1171 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1172 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1173 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1175 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1179 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1181 /* Next configured feature, probably ip4-lookup */
1184 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1186 if (nat_in2out_sm_unknown_proto
1187 (sm, b1, ip1, rx_fib_index1))
1189 next1 = SNAT_IN2OUT_NEXT_DROP;
1191 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1197 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1199 next1 = icmp_in2out_slow_path
1200 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1201 next1, now, thread_index, &s1);
1208 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1210 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1214 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1216 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1221 init_nat_k (&kv1, ip1->src_address,
1222 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1225 (clib_bihash_search_8_8
1226 (&sm->per_thread_data[thread_index].in2out, &kv1,
1231 if (is_output_feature)
1234 (nat_not_translate_output_feature
1236 vnet_buffer (b1)->ip.reass.l4_src_port,
1237 vnet_buffer (b1)->ip.reass.l4_dst_port,
1238 thread_index, sw_if_index1)))
1242 * Send DHCP packets to the ipv4 stack, or we won't
1243 * be able to use dhcp client on the outside interface
1246 (proto1 == NAT_PROTOCOL_UDP
1247 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1248 clib_host_to_net_u16
1249 (UDP_DST_PORT_dhcp_to_server))
1250 && ip1->dst_address.as_u32 == 0xffffffff))
1257 (sm, node, sw_if_index1, ip1, proto1,
1258 rx_fib_index1, thread_index)))
1263 slow_path (sm, b1, ip1, ip1->src_address,
1264 vnet_buffer (b1)->ip.reass.l4_src_port,
1265 rx_fib_index1, proto1, &s1, node, next1,
1267 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1270 if (PREDICT_FALSE (!s1))
1275 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1281 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1284 b1->flags |= VNET_BUFFER_F_IS_NATED;
1286 old_addr1 = ip1->src_address.as_u32;
1287 ip1->src_address = s1->out2in.addr;
1288 new_addr1 = ip1->src_address.as_u32;
1289 if (!is_output_feature)
1290 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1292 sum1 = ip1->checksum;
1293 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1295 src_address /* changed member */ );
1296 ip1->checksum = ip_csum_fold (sum1);
1298 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1300 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1302 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1303 new_port1 = udp1->src_port = s1->out2in.port;
1304 sum1 = tcp1->checksum;
1305 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1307 dst_address /* changed member */ );
1308 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1309 ip4_header_t /* cheat */ ,
1310 length /* changed member */ );
1311 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1312 tcp1->checksum = ip_csum_fold (sum1);
1318 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1320 udp1->src_port = s1->out2in.port;
1321 if (PREDICT_FALSE (udp1->checksum))
1323 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1324 new_port1 = udp1->src_port;
1325 sum1 = udp1->checksum;
1326 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1329 ip_csum_update (sum1, old_port1, new_port1,
1330 ip4_header_t /* cheat */ ,
1331 length /* changed member */ );
1332 udp1->checksum = ip_csum_fold (sum1);
1339 nat44_session_update_counters (s1, now,
1340 vlib_buffer_length_in_chain (vm, b1),
1342 /* Per-user LRU list maintenance */
1343 nat44_session_update_lru (sm, s1, thread_index);
1346 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1347 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1349 snat_in2out_trace_t *t =
1350 vlib_add_trace (vm, node, b1, sizeof (*t));
1351 t->sw_if_index = sw_if_index1;
1352 t->next_index = next1;
1353 t->session_index = ~0;
1356 s1 - sm->per_thread_data[thread_index].sessions;
1359 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1361 /* verify speculative enqueues, maybe switch current next frame */
1362 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1363 to_next, n_left_to_next,
1364 bi0, bi1, next0, next1);
1367 while (n_left_from > 0 && n_left_to_next > 0)
1375 u32 new_addr0, old_addr0;
1376 u16 old_port0, new_port0;
1379 icmp46_header_t *icmp0;
1382 snat_session_t *s0 = 0;
1383 clib_bihash_kv_8_8_t kv0, value0;
1384 u32 iph_offset0 = 0;
1386 /* speculatively enqueue b0 to the current next frame */
1392 n_left_to_next -= 1;
1394 b0 = vlib_get_buffer (vm, bi0);
1395 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1397 if (is_output_feature)
1398 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1400 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1403 udp0 = ip4_next_header (ip0);
1404 tcp0 = (tcp_header_t *) udp0;
1405 icmp0 = (icmp46_header_t *) udp0;
1407 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1408 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1411 if (PREDICT_FALSE (ip0->ttl == 1))
1413 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1414 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1415 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1417 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1421 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1423 /* Next configured feature, probably ip4-lookup */
1426 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1428 if (nat_in2out_sm_unknown_proto
1429 (sm, b0, ip0, rx_fib_index0))
1431 next0 = SNAT_IN2OUT_NEXT_DROP;
1433 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1439 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1441 next0 = icmp_in2out_slow_path
1442 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1443 next0, now, thread_index, &s0);
1450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1452 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1456 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1458 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1463 init_nat_k (&kv0, ip0->src_address,
1464 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1467 if (clib_bihash_search_8_8
1468 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1472 if (is_output_feature)
1475 (nat_not_translate_output_feature
1477 vnet_buffer (b0)->ip.reass.l4_src_port,
1478 vnet_buffer (b0)->ip.reass.l4_dst_port,
1479 thread_index, sw_if_index0)))
1483 * Send DHCP packets to the ipv4 stack, or we won't
1484 * be able to use dhcp client on the outside interface
1487 (proto0 == NAT_PROTOCOL_UDP
1488 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1489 clib_host_to_net_u16
1490 (UDP_DST_PORT_dhcp_to_server))
1491 && ip0->dst_address.as_u32 == 0xffffffff))
1498 (sm, node, sw_if_index0, ip0, proto0,
1499 rx_fib_index0, thread_index)))
1503 next0 = slow_path (sm, b0, ip0,
1505 vnet_buffer (b0)->ip.reass.l4_src_port,
1508 &s0, node, next0, thread_index, now);
1510 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1513 if (PREDICT_FALSE (!s0))
1518 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1524 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1527 b0->flags |= VNET_BUFFER_F_IS_NATED;
1529 old_addr0 = ip0->src_address.as_u32;
1530 ip0->src_address = s0->out2in.addr;
1531 new_addr0 = ip0->src_address.as_u32;
1532 if (!is_output_feature)
1533 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1535 sum0 = ip0->checksum;
1536 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1538 src_address /* changed member */ );
1539 ip0->checksum = ip_csum_fold (sum0);
1541 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1543 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1545 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1546 new_port0 = udp0->src_port = s0->out2in.port;
1547 sum0 = tcp0->checksum;
1548 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1550 dst_address /* changed member */ );
1551 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1552 ip4_header_t /* cheat */ ,
1553 length /* changed member */ );
1554 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1555 tcp0->checksum = ip_csum_fold (sum0);
1561 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1563 udp0->src_port = s0->out2in.port;
1564 if (PREDICT_FALSE (udp0->checksum))
1566 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1567 new_port0 = udp0->src_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;
1769 int is_hairpinning = 0;
1771 stats_node_index = sm->in2out_fast_node_index;
1773 from = vlib_frame_vector_args (frame);
1774 n_left_from = frame->n_vectors;
1775 next_index = node->cached_next_index;
1777 while (n_left_from > 0)
1781 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1783 while (n_left_from > 0 && n_left_to_next > 0)
1791 u32 new_addr0, old_addr0;
1792 u16 old_port0, new_port0;
1795 icmp46_header_t *icmp0;
1798 ip4_address_t sm0_addr;
1803 /* speculatively enqueue b0 to the current next frame */
1809 n_left_to_next -= 1;
1811 b0 = vlib_get_buffer (vm, bi0);
1812 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1814 ip0 = vlib_buffer_get_current (b0);
1815 udp0 = ip4_next_header (ip0);
1816 tcp0 = (tcp_header_t *) udp0;
1817 icmp0 = (icmp46_header_t *) udp0;
1819 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1821 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1823 if (PREDICT_FALSE (ip0->ttl == 1))
1825 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1826 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1827 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1829 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1833 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1835 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1838 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1840 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1841 rx_fib_index0, node, next0, ~0, 0, 0);
1845 if (snat_static_mapping_match
1846 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1847 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1849 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1850 next0 = SNAT_IN2OUT_NEXT_DROP;
1854 new_addr0 = sm0_addr.as_u32;
1855 new_port0 = sm0_port;
1856 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1857 old_addr0 = ip0->src_address.as_u32;
1858 ip0->src_address.as_u32 = new_addr0;
1860 sum0 = ip0->checksum;
1861 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1863 src_address /* changed member */ );
1864 ip0->checksum = ip_csum_fold (sum0);
1866 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1868 old_port0 = udp0->src_port;
1869 udp0->src_port = new_port0;
1871 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1873 sum0 = tcp0->checksum;
1874 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1876 dst_address /* changed member */ );
1877 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1878 ip4_header_t /* cheat */ ,
1879 length /* changed member */ );
1880 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1881 tcp0->checksum = ip_csum_fold (sum0);
1883 else if (udp0->checksum)
1885 sum0 = udp0->checksum;
1886 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1888 dst_address /* changed member */ );
1889 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1890 ip4_header_t /* cheat */ ,
1891 length /* changed member */ );
1892 udp0->checksum = ip_csum_fold (sum0);
1897 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1899 sum0 = tcp0->checksum;
1900 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1902 dst_address /* changed member */ );
1903 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1904 tcp0->checksum = ip_csum_fold (sum0);
1906 else if (udp0->checksum)
1908 sum0 = udp0->checksum;
1909 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1911 dst_address /* changed member */ );
1912 udp0->checksum = ip_csum_fold (sum0);
1917 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1922 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1923 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1925 snat_in2out_trace_t *t =
1926 vlib_add_trace (vm, node, b0, sizeof (*t));
1927 t->sw_if_index = sw_if_index0;
1928 t->next_index = next0;
1929 t->is_hairpinning = is_hairpinning;
1932 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1934 /* verify speculative enqueue, maybe switch current next frame */
1935 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1936 to_next, n_left_to_next,
1940 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1943 vlib_node_increment_counter (vm, stats_node_index,
1944 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1946 return frame->n_vectors;
1951 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1952 .name = "nat44-in2out-fast",
1953 .vector_size = sizeof (u32),
1954 .format_trace = format_snat_in2out_fast_trace,
1955 .type = VLIB_NODE_TYPE_INTERNAL,
1957 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1958 .error_strings = snat_in2out_error_strings,
1960 .runtime_data_bytes = sizeof (snat_runtime_t),
1962 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1964 /* edit / add dispositions here */
1966 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1967 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1968 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1969 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1975 * fd.io coding-style-patch-verification: ON
1978 * eval: (c-set-style "gnu")