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>
46 } snat_in2out_trace_t;
48 /* packet trace format function */
50 format_snat_in2out_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
57 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
59 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
60 t->sw_if_index, t->next_index, t->session_index);
66 format_snat_in2out_fast_trace (u8 * s, va_list * args)
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
72 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
73 t->sw_if_index, t->next_index);
78 #define foreach_snat_in2out_error \
79 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
80 _(IN2OUT_PACKETS, "good in2out packets processed") \
81 _(OUT_OF_PORTS, "out of ports") \
82 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
83 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
84 _(NO_TRANSLATION, "no translation") \
85 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
86 _(DROP_FRAGMENT, "drop fragment") \
87 _(TCP_PACKETS, "TCP packets") \
88 _(UDP_PACKETS, "UDP packets") \
89 _(ICMP_PACKETS, "ICMP packets") \
90 _(OTHER_PACKETS, "other protocol packets") \
91 _(FRAGMENTS, "fragments") \
92 _(CACHED_FRAGMENTS, "cached fragments") \
93 _(PROCESSED_FRAGMENTS, "processed fragments")
97 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
98 foreach_snat_in2out_error
101 } snat_in2out_error_t;
103 static char *snat_in2out_error_strings[] = {
104 #define _(sym,string) string,
105 foreach_snat_in2out_error
111 SNAT_IN2OUT_NEXT_LOOKUP,
112 SNAT_IN2OUT_NEXT_DROP,
113 SNAT_IN2OUT_NEXT_ICMP_ERROR,
114 SNAT_IN2OUT_NEXT_SLOW_PATH,
116 } snat_in2out_next_t;
119 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
120 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
121 u32 rx_fib_index0, u32 thread_index)
123 udp_header_t *udp0 = ip4_next_header (ip0);
124 snat_session_key_t key0, sm0;
125 clib_bihash_kv_8_8_t kv0, value0;
127 key0.addr = ip0->dst_address;
128 key0.port = udp0->dst_port;
129 key0.protocol = proto0;
130 key0.fib_index = sm->outside_fib_index;
131 kv0.key = key0.as_u64;
133 /* NAT packet aimed at external address if */
134 /* has active sessions */
135 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
138 /* or is static mappings */
139 if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
145 if (sm->forwarding_enabled)
148 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
153 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
154 u32 proto0, u16 src_port, u16 dst_port,
155 u32 thread_index, u32 sw_if_index)
157 snat_session_key_t key0;
158 clib_bihash_kv_8_8_t kv0, value0;
162 key0.addr = ip0->src_address;
163 key0.port = src_port;
164 key0.protocol = proto0;
165 key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
166 kv0.key = key0.as_u64;
168 if (!clib_bihash_search_8_8
169 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
173 key0.addr = ip0->dst_address;
174 key0.port = dst_port;
175 key0.protocol = proto0;
176 kv0.key = key0.as_u64;
177 if (!clib_bihash_search_8_8
178 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
182 pool_foreach (i, sm->output_feature_interfaces,
184 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
194 #ifndef CLIB_MARCH_VARIANT
196 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
198 snat_main_t *sm = &snat_main;
199 nat44_is_idle_session_ctx_t *ctx = arg;
201 u64 sess_timeout_time;
202 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
204 clib_bihash_kv_8_8_t s_kv;
206 s = pool_elt_at_index (tsm->sessions, kv->value);
207 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
208 if (ctx->now >= sess_timeout_time)
210 s_kv.key = s->out2in.as_u64;
211 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
212 nat_elog_warn ("out2in key del failed");
214 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
215 s->in2out.addr.as_u32,
216 s->out2in.addr.as_u32,
220 s->in2out.fib_index);
222 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
223 &s->in2out.addr, s->in2out.port,
224 &s->out2in.addr, s->out2in.port,
227 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
228 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
231 if (!snat_is_session_static (s))
232 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
235 nat44_delete_session (sm, s, ctx->thread_index);
244 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
247 snat_session_key_t * key0,
248 snat_session_t ** sessionp,
249 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
252 snat_session_t *s = 0;
253 clib_bihash_kv_8_8_t kv0;
254 snat_session_key_t key1;
256 nat_outside_fib_t *outside_fib;
257 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
260 .fp_proto = FIB_PROTOCOL_IP4,
263 .ip4.as_u32 = ip0->dst_address.as_u32,
266 nat44_is_idle_session_ctx_t ctx0;
268 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
270 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
271 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
272 nat_elog_notice ("maximum sessions exceeded");
273 return SNAT_IN2OUT_NEXT_DROP;
276 key1.protocol = key0->protocol;
278 /* First try to match static mapping by local address and port */
279 if (snat_static_mapping_match
280 (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
282 /* Try to create dynamic translation */
283 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
287 [thread_index].snat_thread_index))
289 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
290 return SNAT_IN2OUT_NEXT_DROP;
295 if (PREDICT_FALSE (identity_nat))
304 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
308 nat_elog_warn ("create NAT user failed");
309 return SNAT_IN2OUT_NEXT_DROP;
312 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
315 nat44_delete_user_with_no_session (sm, u, thread_index);
316 nat_elog_warn ("create NAT session failed");
317 return SNAT_IN2OUT_NEXT_DROP;
321 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
322 user_session_increment (sm, u, is_sm);
325 s->out2in.protocol = key0->protocol;
326 s->out2in.fib_index = sm->outside_fib_index;
327 switch (vec_len (sm->outside_fibs))
330 s->out2in.fib_index = sm->outside_fib_index;
333 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
337 vec_foreach (outside_fib, sm->outside_fibs)
339 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
340 if (FIB_NODE_INDEX_INVALID != fei)
342 if (fib_entry_get_resolving_interface (fei) != ~0)
344 s->out2in.fib_index = outside_fib->fib_index;
352 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
353 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
356 /* Add to translation hashes */
358 ctx0.thread_index = thread_index;
359 kv0.key = s->in2out.as_u64;
360 kv0.value = s - sm->per_thread_data[thread_index].sessions;
361 if (clib_bihash_add_or_overwrite_stale_8_8
362 (&sm->per_thread_data[thread_index].in2out, &kv0,
363 nat44_i2o_is_idle_session_cb, &ctx0))
364 nat_elog_notice ("in2out key add failed");
366 kv0.key = s->out2in.as_u64;
367 kv0.value = 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].out2in, &kv0,
371 nat44_o2i_is_idle_session_cb, &ctx0))
372 nat_elog_notice ("out2in key add failed");
375 snat_ipfix_logging_nat44_ses_create (thread_index,
376 s->in2out.addr.as_u32,
377 s->out2in.addr.as_u32,
380 s->out2in.port, s->in2out.fib_index);
382 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
383 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
384 s->out2in.port, s->in2out.protocol);
386 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
387 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
388 &s->ext_host_nat_addr, s->ext_host_nat_port,
389 s->in2out.protocol, s->in2out.fib_index, s->flags,
395 #ifndef CLIB_MARCH_VARIANT
397 snat_in2out_error_t icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
398 snat_session_key_t * p_key0)
400 icmp46_header_t *icmp0;
401 snat_session_key_t key0;
402 icmp_echo_header_t *echo0, *inner_echo0 = 0;
403 ip4_header_t *inner_ip0 = 0;
405 icmp46_header_t *inner_icmp0;
407 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
408 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
410 if (!icmp_type_is_error_message
411 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
413 key0.protocol = NAT_PROTOCOL_ICMP;
414 key0.addr = ip0->src_address;
415 key0.port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO fixme should this be dst port?
419 inner_ip0 = (ip4_header_t *) (echo0 + 1);
420 l4_header = ip4_next_header (inner_ip0);
421 key0.protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
422 key0.addr = inner_ip0->dst_address;
423 switch (key0.protocol)
425 case NAT_PROTOCOL_ICMP:
426 inner_icmp0 = (icmp46_header_t *) l4_header;
427 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
428 key0.port = inner_echo0->identifier;
430 case NAT_PROTOCOL_UDP:
431 case NAT_PROTOCOL_TCP:
432 key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
435 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
439 return -1; /* success */
443 * Get address and port values to be used for ICMP packet translation
444 * and create session if needed
446 * @param[in,out] sm NAT main
447 * @param[in,out] node NAT node runtime
448 * @param[in] thread_index thread index
449 * @param[in,out] b0 buffer containing packet to be translated
450 * @param[in,out] ip0 ip header
451 * @param[out] p_proto protocol used for matching
452 * @param[out] p_value address and port after NAT translation
453 * @param[out] p_dont_translate if packet should not be translated
454 * @param d optional parameter
455 * @param e optional parameter
458 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
459 u32 thread_index, vlib_buffer_t * b0,
460 ip4_header_t * ip0, u8 * p_proto,
461 snat_session_key_t * p_value,
462 u8 * p_dont_translate, void *d, void *e)
464 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
467 snat_session_key_t key0;
468 snat_session_t *s0 = 0;
469 u8 dont_translate = 0;
470 clib_bihash_kv_8_8_t kv0, value0;
473 vlib_main_t *vm = vlib_get_main ();
475 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
476 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
478 err = icmp_get_key (b0, ip0, &key0);
481 b0->error = node->errors[err];
482 next0 = SNAT_IN2OUT_NEXT_DROP;
485 key0.fib_index = rx_fib_index0;
487 kv0.key = key0.as_u64;
489 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
491 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
493 if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
506 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
507 ip0, NAT_PROTOCOL_ICMP,
517 (icmp_type_is_error_message
518 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
520 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
521 next0 = SNAT_IN2OUT_NEXT_DROP;
525 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
526 thread_index, vlib_time_now (vm));
528 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
540 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
542 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
544 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
545 reass.icmp_type_or_tcp_flags)))
547 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
548 next0 = SNAT_IN2OUT_NEXT_DROP;
552 s0 = pool_elt_at_index (tsm->sessions, value0.value);
556 *p_proto = key0.protocol;
558 *p_value = s0->out2in;
559 *p_dont_translate = dont_translate;
561 *(snat_session_t **) d = s0;
566 #ifndef CLIB_MARCH_VARIANT
568 * Get address and port values to be used for ICMP packet translation
570 * @param[in] sm NAT main
571 * @param[in,out] node NAT node runtime
572 * @param[in] thread_index thread index
573 * @param[in,out] b0 buffer containing packet to be translated
574 * @param[in,out] ip0 ip header
575 * @param[out] p_proto protocol used for matching
576 * @param[out] p_value address and port after NAT translation
577 * @param[out] p_dont_translate if packet should not be translated
578 * @param d optional parameter
579 * @param e optional parameter
582 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
583 u32 thread_index, vlib_buffer_t * b0,
584 ip4_header_t * ip0, u8 * p_proto,
585 snat_session_key_t * p_value,
586 u8 * p_dont_translate, void *d, void *e)
590 snat_session_key_t key0;
591 snat_session_key_t sm0;
592 u8 dont_translate = 0;
597 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
598 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
600 err = icmp_get_key (b0, ip0, &key0);
603 b0->error = node->errors[err];
604 next0 = SNAT_IN2OUT_NEXT_DROP;
607 key0.fib_index = rx_fib_index0;
609 if (snat_static_mapping_match
610 (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
612 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
620 if (icmp_type_is_error_message
621 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
623 next0 = SNAT_IN2OUT_NEXT_DROP;
627 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
628 next0 = SNAT_IN2OUT_NEXT_DROP;
633 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
634 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
635 ICMP4_echo_reply || !is_addr_only)
636 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
637 reass.icmp_type_or_tcp_flags)))
639 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
640 next0 = SNAT_IN2OUT_NEXT_DROP;
647 *p_proto = key0.protocol;
648 *p_dont_translate = dont_translate;
653 #ifndef CLIB_MARCH_VARIANT
655 icmp_in2out (snat_main_t * sm,
658 icmp46_header_t * icmp0,
661 vlib_node_runtime_t * node,
662 u32 next0, u32 thread_index, void *d, void *e)
664 vlib_main_t *vm = vlib_get_main ();
665 snat_session_key_t sm0;
667 icmp_echo_header_t *echo0, *inner_echo0 = 0;
668 ip4_header_t *inner_ip0;
670 icmp46_header_t *inner_icmp0;
672 u32 new_addr0, old_addr0;
673 u16 old_id0, new_id0;
674 u16 old_checksum0, new_checksum0;
679 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
681 next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
682 &protocol, &sm0, &dont_translate, d,
686 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
689 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
692 ip_incremental_checksum_buffer (vm, b0,
694 (u8 *) vlib_buffer_get_current (b0),
695 ntohs (ip0->length) -
696 ip4_header_bytes (ip0), 0);
697 checksum0 = ~ip_csum_fold (sum0);
698 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
700 next0 = SNAT_IN2OUT_NEXT_DROP;
705 old_addr0 = ip0->src_address.as_u32;
706 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
708 sum0 = ip0->checksum;
709 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
710 src_address /* changed member */ );
711 ip0->checksum = ip_csum_fold (sum0);
713 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
715 if (icmp0->checksum == 0)
716 icmp0->checksum = 0xffff;
718 if (!icmp_type_is_error_message (icmp0->type))
721 if (PREDICT_FALSE (new_id0 != echo0->identifier))
723 old_id0 = echo0->identifier;
725 echo0->identifier = new_id0;
727 sum0 = icmp0->checksum;
729 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
731 icmp0->checksum = ip_csum_fold (sum0);
736 inner_ip0 = (ip4_header_t *) (echo0 + 1);
737 l4_header = ip4_next_header (inner_ip0);
739 if (!ip4_header_checksum_is_valid (inner_ip0))
741 next0 = SNAT_IN2OUT_NEXT_DROP;
745 /* update inner destination IP address */
746 old_addr0 = inner_ip0->dst_address.as_u32;
747 inner_ip0->dst_address = sm0.addr;
748 new_addr0 = inner_ip0->dst_address.as_u32;
749 sum0 = icmp0->checksum;
750 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
751 dst_address /* changed member */ );
752 icmp0->checksum = ip_csum_fold (sum0);
754 /* update inner IP header checksum */
755 old_checksum0 = inner_ip0->checksum;
756 sum0 = inner_ip0->checksum;
757 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
758 dst_address /* changed member */ );
759 inner_ip0->checksum = ip_csum_fold (sum0);
760 new_checksum0 = inner_ip0->checksum;
761 sum0 = icmp0->checksum;
763 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
765 icmp0->checksum = ip_csum_fold (sum0);
769 case NAT_PROTOCOL_ICMP:
770 inner_icmp0 = (icmp46_header_t *) l4_header;
771 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
773 old_id0 = inner_echo0->identifier;
775 inner_echo0->identifier = new_id0;
777 sum0 = icmp0->checksum;
779 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
781 icmp0->checksum = ip_csum_fold (sum0);
783 case NAT_PROTOCOL_UDP:
784 case NAT_PROTOCOL_TCP:
785 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
787 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
789 sum0 = icmp0->checksum;
790 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
792 icmp0->checksum = ip_csum_fold (sum0);
800 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
802 if (sm->deterministic ||
803 0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
804 sm->endpoint_dependent))
805 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
814 icmp_in2out_slow_path (snat_main_t * sm,
817 icmp46_header_t * icmp0,
820 vlib_node_runtime_t * node,
822 f64 now, u32 thread_index, snat_session_t ** p_s0)
824 vlib_main_t *vm = vlib_get_main ();
826 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
827 next0, thread_index, p_s0, 0);
828 snat_session_t *s0 = *p_s0;
829 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
832 nat44_session_update_counters (s0, now,
833 vlib_buffer_length_in_chain
834 (vm, b0), thread_index);
835 /* Per-user LRU list maintenance */
836 nat44_session_update_lru (sm, s0, thread_index);
842 nat_in2out_sm_unknown_proto (snat_main_t * sm,
844 ip4_header_t * ip, u32 rx_fib_index)
846 clib_bihash_kv_8_8_t kv, value;
847 snat_static_mapping_t *m;
848 snat_session_key_t m_key;
849 u32 old_addr, new_addr;
852 m_key.addr = ip->src_address;
855 m_key.fib_index = rx_fib_index;
856 kv.key = m_key.as_u64;
857 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
860 m = pool_elt_at_index (sm->static_mappings, value.value);
862 old_addr = ip->src_address.as_u32;
863 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
865 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
866 ip->checksum = ip_csum_fold (sum);
870 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
872 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
873 nat_hairpinning_sm_unknown_proto (sm, b, ip);
880 snat_in2out_node_fn_inline (vlib_main_t * vm,
881 vlib_node_runtime_t * node,
882 vlib_frame_t * frame, int is_slow_path,
883 int is_output_feature)
885 u32 n_left_from, *from, *to_next;
886 snat_in2out_next_t next_index;
887 u32 pkts_processed = 0;
888 snat_main_t *sm = &snat_main;
889 f64 now = vlib_time_now (vm);
890 u32 stats_node_index;
891 u32 thread_index = vm->thread_index;
892 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
895 stats_node_index = is_slow_path ? sm->in2out_slowpath_node_index :
896 sm->in2out_node_index;
898 from = vlib_frame_vector_args (frame);
899 n_left_from = frame->n_vectors;
900 next_index = node->cached_next_index;
902 while (n_left_from > 0)
906 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
908 while (n_left_from >= 4 && n_left_to_next >= 2)
911 vlib_buffer_t *b0, *b1;
913 u32 sw_if_index0, sw_if_index1;
914 ip4_header_t *ip0, *ip1;
915 ip_csum_t sum0, sum1;
916 u32 new_addr0, old_addr0, new_addr1, old_addr1;
917 u16 old_port0, new_port0, old_port1, new_port1;
918 udp_header_t *udp0, *udp1;
919 tcp_header_t *tcp0, *tcp1;
920 icmp46_header_t *icmp0, *icmp1;
921 snat_session_key_t key0, key1;
922 u32 rx_fib_index0, rx_fib_index1;
924 snat_session_t *s0 = 0, *s1 = 0;
925 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
926 u32 iph_offset0 = 0, iph_offset1 = 0;
928 /* Prefetch next iteration. */
930 vlib_buffer_t *p2, *p3;
932 p2 = vlib_get_buffer (vm, from[2]);
933 p3 = vlib_get_buffer (vm, from[3]);
935 vlib_prefetch_buffer_header (p2, LOAD);
936 vlib_prefetch_buffer_header (p3, LOAD);
938 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
939 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
942 /* speculatively enqueue b0 and b1 to the current next frame */
943 to_next[0] = bi0 = from[0];
944 to_next[1] = bi1 = from[1];
950 b0 = vlib_get_buffer (vm, bi0);
951 b1 = vlib_get_buffer (vm, bi1);
953 if (is_output_feature)
954 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
956 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
959 udp0 = ip4_next_header (ip0);
960 tcp0 = (tcp_header_t *) udp0;
961 icmp0 = (icmp46_header_t *) udp0;
963 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
964 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
967 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
969 if (PREDICT_FALSE (ip0->ttl == 1))
971 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
972 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
973 ICMP4_time_exceeded_ttl_exceeded_in_transit,
975 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
979 proto0 = ip_proto_to_nat_proto (ip0->protocol);
981 /* Next configured feature, probably ip4-lookup */
984 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
986 if (nat_in2out_sm_unknown_proto
987 (sm, b0, ip0, rx_fib_index0))
989 next0 = SNAT_IN2OUT_NEXT_DROP;
991 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
997 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
999 next0 = icmp_in2out_slow_path
1000 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1001 node, next0, now, thread_index, &s0);
1008 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1010 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1014 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1016 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1021 key0.addr = ip0->src_address;
1022 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1023 key0.protocol = proto0;
1024 key0.fib_index = rx_fib_index0;
1026 kv0.key = key0.as_u64;
1029 (clib_bihash_search_8_8
1030 (&sm->per_thread_data[thread_index].in2out, &kv0,
1035 if (is_output_feature)
1038 (nat_not_translate_output_feature
1040 vnet_buffer (b0)->ip.reass.l4_src_port,
1041 vnet_buffer (b0)->ip.reass.l4_dst_port,
1042 thread_index, sw_if_index0)))
1046 * Send DHCP packets to the ipv4 stack, or we won't
1047 * be able to use dhcp client on the outside interface
1050 (proto0 == NAT_PROTOCOL_UDP
1051 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1052 clib_host_to_net_u16
1053 (UDP_DST_PORT_dhcp_to_server))
1054 && ip0->dst_address.as_u32 == 0xffffffff))
1061 (sm, node, sw_if_index0, ip0, proto0,
1062 rx_fib_index0, thread_index)))
1066 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1067 &s0, node, next0, thread_index, now);
1068 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1071 if (PREDICT_FALSE (!s0))
1076 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1082 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1085 b0->flags |= VNET_BUFFER_F_IS_NATED;
1087 old_addr0 = ip0->src_address.as_u32;
1088 ip0->src_address = s0->out2in.addr;
1089 new_addr0 = ip0->src_address.as_u32;
1090 if (!is_output_feature)
1091 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1093 sum0 = ip0->checksum;
1094 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1096 src_address /* changed member */ );
1097 ip0->checksum = ip_csum_fold (sum0);
1100 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1102 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1104 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1105 new_port0 = udp0->src_port = s0->out2in.port;
1106 sum0 = tcp0->checksum;
1107 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1109 dst_address /* changed member */ );
1110 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1111 ip4_header_t /* cheat */ ,
1112 length /* changed member */ );
1113 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1114 tcp0->checksum = ip_csum_fold (sum0);
1120 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1122 udp0->src_port = s0->out2in.port;
1123 if (PREDICT_FALSE (udp0->checksum))
1125 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1126 new_port0 = udp0->src_port;
1127 sum0 = udp0->checksum;
1128 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1131 ip_csum_update (sum0, old_port0, new_port0,
1132 ip4_header_t /* cheat */ ,
1133 length /* changed member */ );
1134 udp0->checksum = ip_csum_fold (sum0);
1141 nat44_session_update_counters (s0, now,
1142 vlib_buffer_length_in_chain (vm, b0),
1144 /* Per-user LRU list maintenance */
1145 nat44_session_update_lru (sm, s0, thread_index);
1148 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1149 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1151 snat_in2out_trace_t *t =
1152 vlib_add_trace (vm, node, b0, sizeof (*t));
1153 t->is_slow_path = is_slow_path;
1154 t->sw_if_index = sw_if_index0;
1155 t->next_index = next0;
1156 t->session_index = ~0;
1159 s0 - sm->per_thread_data[thread_index].sessions;
1162 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1164 if (is_output_feature)
1165 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1167 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1170 udp1 = ip4_next_header (ip1);
1171 tcp1 = (tcp_header_t *) udp1;
1172 icmp1 = (icmp46_header_t *) udp1;
1174 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1175 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1178 if (PREDICT_FALSE (ip1->ttl == 1))
1180 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1181 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1182 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1184 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1188 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1190 /* Next configured feature, probably ip4-lookup */
1193 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1195 if (nat_in2out_sm_unknown_proto
1196 (sm, b1, ip1, rx_fib_index1))
1198 next1 = SNAT_IN2OUT_NEXT_DROP;
1200 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1206 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1208 next1 = icmp_in2out_slow_path
1209 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1210 next1, now, thread_index, &s1);
1217 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1219 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1223 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1225 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1230 key1.addr = ip1->src_address;
1231 key1.port = vnet_buffer (b1)->ip.reass.l4_src_port;
1232 key1.protocol = proto1;
1233 key1.fib_index = rx_fib_index1;
1235 kv1.key = key1.as_u64;
1238 (clib_bihash_search_8_8
1239 (&sm->per_thread_data[thread_index].in2out, &kv1,
1244 if (is_output_feature)
1247 (nat_not_translate_output_feature
1249 vnet_buffer (b1)->ip.reass.l4_src_port,
1250 vnet_buffer (b1)->ip.reass.l4_dst_port,
1251 thread_index, sw_if_index1)))
1255 * Send DHCP packets to the ipv4 stack, or we won't
1256 * be able to use dhcp client on the outside interface
1259 (proto1 == NAT_PROTOCOL_UDP
1260 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1261 clib_host_to_net_u16
1262 (UDP_DST_PORT_dhcp_to_server))
1263 && ip1->dst_address.as_u32 == 0xffffffff))
1270 (sm, node, sw_if_index1, ip1, proto1,
1271 rx_fib_index1, thread_index)))
1275 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1276 &s1, node, next1, thread_index, now);
1277 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1280 if (PREDICT_FALSE (!s1))
1285 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1291 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1294 b1->flags |= VNET_BUFFER_F_IS_NATED;
1296 old_addr1 = ip1->src_address.as_u32;
1297 ip1->src_address = s1->out2in.addr;
1298 new_addr1 = ip1->src_address.as_u32;
1299 if (!is_output_feature)
1300 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1302 sum1 = ip1->checksum;
1303 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1305 src_address /* changed member */ );
1306 ip1->checksum = ip_csum_fold (sum1);
1308 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1310 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1312 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1313 new_port1 = udp1->src_port = s1->out2in.port;
1314 sum1 = tcp1->checksum;
1315 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1317 dst_address /* changed member */ );
1318 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1319 ip4_header_t /* cheat */ ,
1320 length /* changed member */ );
1321 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1322 tcp1->checksum = ip_csum_fold (sum1);
1328 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1330 udp1->src_port = s1->out2in.port;
1331 if (PREDICT_FALSE (udp1->checksum))
1333 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1334 new_port1 = udp1->src_port;
1335 sum1 = udp1->checksum;
1336 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1339 ip_csum_update (sum1, old_port1, new_port1,
1340 ip4_header_t /* cheat */ ,
1341 length /* changed member */ );
1342 udp1->checksum = ip_csum_fold (sum1);
1349 nat44_session_update_counters (s1, now,
1350 vlib_buffer_length_in_chain (vm, b1),
1352 /* Per-user LRU list maintenance */
1353 nat44_session_update_lru (sm, s1, thread_index);
1356 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1357 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1359 snat_in2out_trace_t *t =
1360 vlib_add_trace (vm, node, b1, sizeof (*t));
1361 t->sw_if_index = sw_if_index1;
1362 t->next_index = next1;
1363 t->session_index = ~0;
1366 s1 - sm->per_thread_data[thread_index].sessions;
1369 pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1371 /* verify speculative enqueues, maybe switch current next frame */
1372 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1373 to_next, n_left_to_next,
1374 bi0, bi1, next0, next1);
1377 while (n_left_from > 0 && n_left_to_next > 0)
1385 u32 new_addr0, old_addr0;
1386 u16 old_port0, new_port0;
1389 icmp46_header_t *icmp0;
1390 snat_session_key_t key0;
1393 snat_session_t *s0 = 0;
1394 clib_bihash_kv_8_8_t kv0, value0;
1395 u32 iph_offset0 = 0;
1397 /* speculatively enqueue b0 to the current next frame */
1403 n_left_to_next -= 1;
1405 b0 = vlib_get_buffer (vm, bi0);
1406 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1408 if (is_output_feature)
1409 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1411 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1414 udp0 = ip4_next_header (ip0);
1415 tcp0 = (tcp_header_t *) udp0;
1416 icmp0 = (icmp46_header_t *) udp0;
1418 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1419 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1422 if (PREDICT_FALSE (ip0->ttl == 1))
1424 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1425 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1426 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1428 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1432 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1434 /* Next configured feature, probably ip4-lookup */
1437 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1439 if (nat_in2out_sm_unknown_proto
1440 (sm, b0, ip0, rx_fib_index0))
1442 next0 = SNAT_IN2OUT_NEXT_DROP;
1444 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1452 next0 = icmp_in2out_slow_path
1453 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1454 next0, now, thread_index, &s0);
1461 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1463 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1467 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1469 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1474 key0.addr = ip0->src_address;
1475 key0.port = vnet_buffer (b0)->ip.reass.l4_src_port;
1476 key0.protocol = proto0;
1477 key0.fib_index = rx_fib_index0;
1479 kv0.key = key0.as_u64;
1481 if (clib_bihash_search_8_8
1482 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1486 if (is_output_feature)
1489 (nat_not_translate_output_feature
1491 vnet_buffer (b0)->ip.reass.l4_src_port,
1492 vnet_buffer (b0)->ip.reass.l4_dst_port,
1493 thread_index, sw_if_index0)))
1497 * Send DHCP packets to the ipv4 stack, or we won't
1498 * be able to use dhcp client on the outside interface
1501 (proto0 == NAT_PROTOCOL_UDP
1502 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1503 clib_host_to_net_u16
1504 (UDP_DST_PORT_dhcp_to_server))
1505 && ip0->dst_address.as_u32 == 0xffffffff))
1512 (sm, node, sw_if_index0, ip0, proto0,
1513 rx_fib_index0, thread_index)))
1517 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1518 &s0, node, next0, thread_index, now);
1520 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1523 if (PREDICT_FALSE (!s0))
1528 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1534 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1537 b0->flags |= VNET_BUFFER_F_IS_NATED;
1539 old_addr0 = ip0->src_address.as_u32;
1540 ip0->src_address = s0->out2in.addr;
1541 new_addr0 = ip0->src_address.as_u32;
1542 if (!is_output_feature)
1543 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1545 sum0 = ip0->checksum;
1546 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1548 src_address /* changed member */ );
1549 ip0->checksum = ip_csum_fold (sum0);
1551 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1553 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1555 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1556 new_port0 = udp0->src_port = s0->out2in.port;
1557 sum0 = tcp0->checksum;
1558 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1560 dst_address /* changed member */ );
1561 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1562 ip4_header_t /* cheat */ ,
1563 length /* changed member */ );
1564 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1565 tcp0->checksum = ip_csum_fold (sum0);
1571 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1573 udp0->src_port = s0->out2in.port;
1574 if (PREDICT_FALSE (udp0->checksum))
1576 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1577 new_port0 = udp0->src_port;
1578 sum0 = udp0->checksum;
1579 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1582 ip_csum_update (sum0, old_port0, new_port0,
1583 ip4_header_t /* cheat */ ,
1584 length /* changed member */ );
1585 udp0->checksum = ip_csum_fold (sum0);
1592 nat44_session_update_counters (s0, now,
1593 vlib_buffer_length_in_chain (vm, b0),
1595 /* Per-user LRU list maintenance */
1596 nat44_session_update_lru (sm, s0, thread_index);
1599 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1600 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1602 snat_in2out_trace_t *t =
1603 vlib_add_trace (vm, node, b0, sizeof (*t));
1604 t->is_slow_path = is_slow_path;
1605 t->sw_if_index = sw_if_index0;
1606 t->next_index = next0;
1607 t->session_index = ~0;
1610 s0 - sm->per_thread_data[thread_index].sessions;
1613 pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1615 /* verify speculative enqueue, maybe switch current next frame */
1616 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1617 to_next, n_left_to_next,
1621 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1624 vlib_node_increment_counter (vm, stats_node_index,
1625 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1627 vlib_node_increment_counter (vm, stats_node_index,
1628 SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1629 vlib_node_increment_counter (vm, stats_node_index,
1630 SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1631 vlib_node_increment_counter (vm, stats_node_index,
1632 SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1633 vlib_node_increment_counter (vm, stats_node_index,
1634 SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1636 vlib_node_increment_counter (vm, stats_node_index,
1637 SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1639 return frame->n_vectors;
1642 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1643 vlib_node_runtime_t * node,
1644 vlib_frame_t * frame)
1646 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1651 VLIB_REGISTER_NODE (snat_in2out_node) = {
1652 .name = "nat44-in2out",
1653 .vector_size = sizeof (u32),
1654 .format_trace = format_snat_in2out_trace,
1655 .type = VLIB_NODE_TYPE_INTERNAL,
1657 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1658 .error_strings = snat_in2out_error_strings,
1660 .runtime_data_bytes = sizeof (snat_runtime_t),
1662 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1664 /* edit / add dispositions here */
1666 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1667 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1668 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1669 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1674 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1675 vlib_node_runtime_t * node,
1676 vlib_frame_t * frame)
1678 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1683 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1684 .name = "nat44-in2out-output",
1685 .vector_size = sizeof (u32),
1686 .format_trace = format_snat_in2out_trace,
1687 .type = VLIB_NODE_TYPE_INTERNAL,
1689 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1690 .error_strings = snat_in2out_error_strings,
1692 .runtime_data_bytes = sizeof (snat_runtime_t),
1694 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1696 /* edit / add dispositions here */
1698 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1699 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1700 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1701 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1706 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1707 vlib_node_runtime_t * node,
1708 vlib_frame_t * frame)
1710 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1715 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1716 .name = "nat44-in2out-slowpath",
1717 .vector_size = sizeof (u32),
1718 .format_trace = format_snat_in2out_trace,
1719 .type = VLIB_NODE_TYPE_INTERNAL,
1721 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1722 .error_strings = snat_in2out_error_strings,
1724 .runtime_data_bytes = sizeof (snat_runtime_t),
1726 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1728 /* edit / add dispositions here */
1730 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1731 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1732 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1733 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1738 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1739 vlib_node_runtime_t * node,
1740 vlib_frame_t * frame)
1742 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1747 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1748 .name = "nat44-in2out-output-slowpath",
1749 .vector_size = sizeof (u32),
1750 .format_trace = format_snat_in2out_trace,
1751 .type = VLIB_NODE_TYPE_INTERNAL,
1753 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1754 .error_strings = snat_in2out_error_strings,
1756 .runtime_data_bytes = sizeof (snat_runtime_t),
1758 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1760 /* edit / add dispositions here */
1762 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1763 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1764 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1765 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1770 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1771 vlib_node_runtime_t * node,
1772 vlib_frame_t * frame)
1774 u32 n_left_from, *from, *to_next;
1775 snat_in2out_next_t next_index;
1776 u32 pkts_processed = 0;
1777 snat_main_t *sm = &snat_main;
1778 u32 stats_node_index;
1780 stats_node_index = sm->in2out_fast_node_index;
1782 from = vlib_frame_vector_args (frame);
1783 n_left_from = frame->n_vectors;
1784 next_index = node->cached_next_index;
1786 while (n_left_from > 0)
1790 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1792 while (n_left_from > 0 && n_left_to_next > 0)
1800 u32 new_addr0, old_addr0;
1801 u16 old_port0, new_port0;
1804 icmp46_header_t *icmp0;
1805 snat_session_key_t key0, sm0;
1809 /* speculatively enqueue b0 to the current next frame */
1815 n_left_to_next -= 1;
1817 b0 = vlib_get_buffer (vm, bi0);
1818 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1820 ip0 = vlib_buffer_get_current (b0);
1821 udp0 = ip4_next_header (ip0);
1822 tcp0 = (tcp_header_t *) udp0;
1823 icmp0 = (icmp46_header_t *) udp0;
1825 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1827 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1829 if (PREDICT_FALSE (ip0->ttl == 1))
1831 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1832 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1833 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1835 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1839 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1841 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1844 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1846 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1847 rx_fib_index0, node, next0, ~0, 0, 0);
1851 key0.addr = ip0->src_address;
1852 key0.protocol = proto0;
1853 key0.port = udp0->src_port;
1854 key0.fib_index = rx_fib_index0;
1856 if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
1858 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1859 next0 = SNAT_IN2OUT_NEXT_DROP;
1863 new_addr0 = sm0.addr.as_u32;
1864 new_port0 = sm0.port;
1865 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1866 old_addr0 = ip0->src_address.as_u32;
1867 ip0->src_address.as_u32 = new_addr0;
1869 sum0 = ip0->checksum;
1870 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1872 src_address /* changed member */ );
1873 ip0->checksum = ip_csum_fold (sum0);
1875 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1877 old_port0 = udp0->src_port;
1878 udp0->src_port = new_port0;
1880 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1882 sum0 = tcp0->checksum;
1883 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1885 dst_address /* changed member */ );
1886 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1887 ip4_header_t /* cheat */ ,
1888 length /* changed member */ );
1889 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1890 tcp0->checksum = ip_csum_fold (sum0);
1892 else if (udp0->checksum)
1894 sum0 = udp0->checksum;
1895 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1897 dst_address /* changed member */ );
1898 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1899 ip4_header_t /* cheat */ ,
1900 length /* changed member */ );
1901 udp0->checksum = ip_csum_fold (sum0);
1906 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1908 sum0 = tcp0->checksum;
1909 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1911 dst_address /* changed member */ );
1912 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1913 tcp0->checksum = ip_csum_fold (sum0);
1915 else if (udp0->checksum)
1917 sum0 = udp0->checksum;
1918 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1920 dst_address /* changed member */ );
1921 udp0->checksum = ip_csum_fold (sum0);
1926 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
1929 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1930 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1932 snat_in2out_trace_t *t =
1933 vlib_add_trace (vm, node, b0, sizeof (*t));
1934 t->sw_if_index = sw_if_index0;
1935 t->next_index = next0;
1938 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1940 /* verify speculative enqueue, maybe switch current next frame */
1941 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1942 to_next, n_left_to_next,
1946 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1949 vlib_node_increment_counter (vm, stats_node_index,
1950 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1952 return frame->n_vectors;
1957 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1958 .name = "nat44-in2out-fast",
1959 .vector_size = sizeof (u32),
1960 .format_trace = format_snat_in2out_fast_trace,
1961 .type = VLIB_NODE_TYPE_INTERNAL,
1963 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1964 .error_strings = snat_in2out_error_strings,
1966 .runtime_data_bytes = sizeof (snat_runtime_t),
1968 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1970 /* edit / add dispositions here */
1972 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1973 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1974 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1975 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1981 * fd.io coding-style-patch-verification: ON
1984 * eval: (c-set-style "gnu")