2 * Copyright (c) 2018 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 Deterministic/CGN NAT44 inside to outside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
27 #include <nat/nat_det.h>
28 #include <nat/nat_inlines.h>
35 } nat_det_in2out_trace_t;
39 NAT_DET_IN2OUT_NEXT_LOOKUP,
40 NAT_DET_IN2OUT_NEXT_DROP,
41 NAT_DET_IN2OUT_NEXT_ICMP_ERROR,
42 NAT_DET_IN2OUT_N_NEXT,
43 } nat_det_in2out_next_t;
45 #define foreach_nat_det_in2out_error \
46 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
47 _(NO_TRANSLATION, "No translation") \
48 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
49 _(OUT_OF_PORTS, "Out of ports") \
50 _(IN2OUT_PACKETS, "Good in2out packets processed")
54 #define _(sym,str) NAT_DET_IN2OUT_ERROR_##sym,
55 foreach_nat_det_in2out_error
57 NAT_DET_IN2OUT_N_ERROR,
58 } nat_det_in2out_error_t;
60 static char *nat_det_in2out_error_strings[] = {
61 #define _(sym,string) string,
62 foreach_nat_det_in2out_error
67 format_nat_det_in2out_trace (u8 * s, va_list * args)
69 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71 nat_det_in2out_trace_t *t = va_arg (*args, nat_det_in2out_trace_t *);
73 s = format (s, "NAT_DET_IN2OUT: sw_if_index %d, next index %d, session %d",
74 t->sw_if_index, t->next_index, t->session_index);
79 #ifndef CLIB_MARCH_VARIANT
81 * Get address and port values to be used for ICMP packet translation
82 * and create session if needed
84 * @param[in,out] sm NAT main
85 * @param[in,out] node NAT node runtime
86 * @param[in] thread_index thread index
87 * @param[in,out] b0 buffer containing packet to be translated
88 * @param[in,out] ip0 ip header
89 * @param[out] p_proto protocol used for matching
90 * @param[out] p_value address and port after NAT translation
91 * @param[out] p_dont_translate if packet should not be translated
92 * @param d optional parameter
93 * @param e optional parameter
96 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
97 u32 thread_index, vlib_buffer_t * b0,
98 ip4_header_t * ip0, u8 * p_proto,
99 snat_session_key_t * p_value, u8 * p_dont_translate,
102 vlib_main_t *vm = vlib_get_main ();
103 icmp46_header_t *icmp0;
107 snat_det_out_key_t key0;
108 u8 dont_translate = 0;
110 icmp_echo_header_t *echo0, *inner_echo0 = 0;
111 ip4_header_t *inner_ip0;
113 icmp46_header_t *inner_icmp0;
114 snat_det_map_t *dm0 = 0;
115 ip4_address_t new_addr0;
117 snat_det_session_t *ses0 = 0;
118 ip4_address_t in_addr;
121 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
122 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
123 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
124 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
126 if (!icmp_type_is_error_message
127 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
129 protocol = NAT_PROTOCOL_ICMP;
130 in_addr = ip0->src_address;
131 in_port = vnet_buffer (b0)->ip.reass.l4_src_port;
135 /* if error message, then it's not fragmented and we can access it */
136 inner_ip0 = (ip4_header_t *) (echo0 + 1);
137 l4_header = ip4_next_header (inner_ip0);
138 protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
139 in_addr = inner_ip0->dst_address;
142 case NAT_PROTOCOL_ICMP:
143 inner_icmp0 = (icmp46_header_t *) l4_header;
144 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
145 in_port = inner_echo0->identifier;
147 case NAT_PROTOCOL_UDP:
148 case NAT_PROTOCOL_TCP:
149 in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
152 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
153 next0 = NAT_DET_IN2OUT_NEXT_DROP;
158 dm0 = snat_det_map_by_user (sm, &in_addr);
159 if (PREDICT_FALSE (!dm0))
161 nat_log_info ("no match for internal host %U",
162 format_ip4_address, &in_addr);
163 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
170 next0 = NAT_DET_IN2OUT_NEXT_DROP;
171 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
175 snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
177 key0.ext_host_addr = ip0->dst_address;
178 key0.ext_host_port = 0;
180 ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
181 if (PREDICT_FALSE (!ses0))
183 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
190 if (icmp0->type != ICMP4_echo_request)
192 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
193 next0 = NAT_DET_IN2OUT_NEXT_DROP;
196 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
198 key0.out_port = clib_host_to_net_u16 (lo_port0 +
201 (echo0->identifier)) %
202 dm0->ports_per_host));
204 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
208 snat_det_ses_create (thread_index, dm0,
209 &in_addr, echo0->identifier, &key0);
212 if (PREDICT_FALSE (!ses0))
214 next0 = NAT_DET_IN2OUT_NEXT_DROP;
215 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
221 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
222 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
223 reass.icmp_type_or_tcp_flags)))
225 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
226 next0 = NAT_DET_IN2OUT_NEXT_DROP;
230 u32 now = (u32) vlib_time_now (vm);
232 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
233 ses0->expire = now + sm->icmp_timeout;
239 p_value->addr = new_addr0;
240 p_value->fib_index = sm->outside_fib_index;
241 p_value->port = ses0->out.out_port;
243 *p_dont_translate = dont_translate;
245 *(snat_det_session_t **) d = ses0;
247 *(snat_det_map_t **) e = dm0;
252 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
253 vlib_node_runtime_t * node,
254 vlib_frame_t * frame)
256 u32 n_left_from, *from, *to_next;
257 nat_det_in2out_next_t next_index;
258 u32 pkts_processed = 0;
259 snat_main_t *sm = &snat_main;
260 u32 now = (u32) vlib_time_now (vm);
261 u32 thread_index = vm->thread_index;
263 from = vlib_frame_vector_args (frame);
264 n_left_from = frame->n_vectors;
265 next_index = node->cached_next_index;
267 while (n_left_from > 0)
271 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
273 while (n_left_from >= 4 && n_left_to_next >= 2)
276 vlib_buffer_t *b0, *b1;
278 u32 sw_if_index0, sw_if_index1;
279 ip4_header_t *ip0, *ip1;
280 ip_csum_t sum0, sum1;
281 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
282 u16 old_port0, new_port0, lo_port0, i0;
283 u16 old_port1, new_port1, lo_port1, i1;
284 udp_header_t *udp0, *udp1;
285 tcp_header_t *tcp0, *tcp1;
287 snat_det_out_key_t key0, key1;
288 snat_det_map_t *dm0, *dm1;
289 snat_det_session_t *ses0 = 0, *ses1 = 0;
290 u32 rx_fib_index0, rx_fib_index1;
291 icmp46_header_t *icmp0, *icmp1;
293 /* Prefetch next iteration. */
295 vlib_buffer_t *p2, *p3;
297 p2 = vlib_get_buffer (vm, from[2]);
298 p3 = vlib_get_buffer (vm, from[3]);
300 vlib_prefetch_buffer_header (p2, LOAD);
301 vlib_prefetch_buffer_header (p3, LOAD);
303 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
304 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
307 /* speculatively enqueue b0 and b1 to the current next frame */
308 to_next[0] = bi0 = from[0];
309 to_next[1] = bi1 = from[1];
315 b0 = vlib_get_buffer (vm, bi0);
316 b1 = vlib_get_buffer (vm, bi1);
318 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
319 next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
321 ip0 = vlib_buffer_get_current (b0);
322 udp0 = ip4_next_header (ip0);
323 tcp0 = (tcp_header_t *) udp0;
325 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
327 if (PREDICT_FALSE (ip0->ttl == 1))
329 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
330 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
331 ICMP4_time_exceeded_ttl_exceeded_in_transit,
333 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
337 proto0 = ip_proto_to_nat_proto (ip0->protocol);
339 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
342 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
343 icmp0 = (icmp46_header_t *) udp0;
345 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
346 rx_fib_index0, node, next0, thread_index,
351 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
352 if (PREDICT_FALSE (!dm0))
354 nat_log_info ("no match for internal host %U",
355 format_ip4_address, &ip0->src_address);
356 next0 = NAT_DET_IN2OUT_NEXT_DROP;
357 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
361 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
363 key0.ext_host_addr = ip0->dst_address;
364 key0.ext_host_port = tcp0->dst;
367 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
368 if (PREDICT_FALSE (!ses0))
370 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
372 key0.out_port = clib_host_to_net_u16 (lo_port0 +
379 if (snat_det_get_ses_by_out
380 (dm0, &ip0->src_address, key0.as_u64))
384 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
388 if (PREDICT_FALSE (!ses0))
390 /* too many sessions for user, send ICMP error packet */
391 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
392 icmp4_error_set_vnet_buffer (b0,
393 ICMP4_destination_unreachable,
394 ICMP4_destination_unreachable_destination_unreachable_host,
396 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
401 old_port0 = udp0->src_port;
402 udp0->src_port = new_port0 = ses0->out.out_port;
404 old_addr0.as_u32 = ip0->src_address.as_u32;
405 ip0->src_address.as_u32 = new_addr0.as_u32;
406 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
408 sum0 = ip0->checksum;
409 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
411 src_address /* changed member */ );
412 ip0->checksum = ip_csum_fold (sum0);
414 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
416 if (tcp0->flags & TCP_FLAG_SYN)
417 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
418 else if (tcp0->flags & TCP_FLAG_ACK
419 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
420 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
421 else if (tcp0->flags & TCP_FLAG_FIN
422 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
423 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
424 else if (tcp0->flags & TCP_FLAG_ACK
425 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
426 snat_det_ses_close (dm0, ses0);
427 else if (tcp0->flags & TCP_FLAG_FIN
428 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
429 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
430 else if (tcp0->flags == 0
431 && ses0->state == SNAT_SESSION_UNKNOWN)
432 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
434 sum0 = tcp0->checksum;
435 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
437 dst_address /* changed member */ );
438 sum0 = ip_csum_update (sum0, old_port0, new_port0,
439 ip4_header_t /* cheat */ ,
440 length /* changed member */ );
441 mss_clamping (sm, tcp0, &sum0);
442 tcp0->checksum = ip_csum_fold (sum0);
446 ses0->state = SNAT_SESSION_UDP_ACTIVE;
448 if (PREDICT_FALSE (udp0->checksum))
450 sum0 = udp0->checksum;
452 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
454 dst_address /* changed member */ );
456 ip_csum_update (sum0, old_port0, new_port0,
457 ip4_header_t /* cheat */ ,
458 length /* changed member */ );
459 udp0->checksum = ip_csum_fold (sum0);
465 case SNAT_SESSION_UDP_ACTIVE:
466 ses0->expire = now + sm->udp_timeout;
468 case SNAT_SESSION_TCP_SYN_SENT:
469 case SNAT_SESSION_TCP_FIN_WAIT:
470 case SNAT_SESSION_TCP_CLOSE_WAIT:
471 case SNAT_SESSION_TCP_LAST_ACK:
472 ses0->expire = now + sm->tcp_transitory_timeout;
474 case SNAT_SESSION_TCP_ESTABLISHED:
475 ses0->expire = now + sm->tcp_established_timeout;
480 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
481 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
483 nat_det_in2out_trace_t *t =
484 vlib_add_trace (vm, node, b0, sizeof (*t));
485 t->sw_if_index = sw_if_index0;
486 t->next_index = next0;
487 t->session_index = ~0;
489 t->session_index = ses0 - dm0->sessions;
492 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
494 ip1 = vlib_buffer_get_current (b1);
495 udp1 = ip4_next_header (ip1);
496 tcp1 = (tcp_header_t *) udp1;
498 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
500 if (PREDICT_FALSE (ip1->ttl == 1))
502 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
503 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
504 ICMP4_time_exceeded_ttl_exceeded_in_transit,
506 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
510 proto1 = ip_proto_to_nat_proto (ip1->protocol);
512 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
515 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
516 icmp1 = (icmp46_header_t *) udp1;
518 next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
519 rx_fib_index1, node, next1, thread_index,
524 dm1 = snat_det_map_by_user (sm, &ip1->src_address);
525 if (PREDICT_FALSE (!dm1))
527 nat_log_info ("no match for internal host %U",
528 format_ip4_address, &ip0->src_address);
529 next1 = NAT_DET_IN2OUT_NEXT_DROP;
530 b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
534 snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
536 key1.ext_host_addr = ip1->dst_address;
537 key1.ext_host_port = tcp1->dst;
540 snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
541 if (PREDICT_FALSE (!ses1))
543 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
545 key1.out_port = clib_host_to_net_u16 (lo_port1 +
552 if (snat_det_get_ses_by_out
553 (dm1, &ip1->src_address, key1.as_u64))
557 snat_det_ses_create (thread_index, dm1, &ip1->src_address,
561 if (PREDICT_FALSE (!ses1))
563 /* too many sessions for user, send ICMP error packet */
564 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
565 icmp4_error_set_vnet_buffer (b1,
566 ICMP4_destination_unreachable,
567 ICMP4_destination_unreachable_destination_unreachable_host,
569 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
574 old_port1 = udp1->src_port;
575 udp1->src_port = new_port1 = ses1->out.out_port;
577 old_addr1.as_u32 = ip1->src_address.as_u32;
578 ip1->src_address.as_u32 = new_addr1.as_u32;
579 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
581 sum1 = ip1->checksum;
582 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
584 src_address /* changed member */ );
585 ip1->checksum = ip_csum_fold (sum1);
587 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
589 if (tcp1->flags & TCP_FLAG_SYN)
590 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
591 else if (tcp1->flags & TCP_FLAG_ACK
592 && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
593 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
594 else if (tcp1->flags & TCP_FLAG_FIN
595 && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
596 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
597 else if (tcp1->flags & TCP_FLAG_ACK
598 && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
599 snat_det_ses_close (dm1, ses1);
600 else if (tcp1->flags & TCP_FLAG_FIN
601 && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
602 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
603 else if (tcp1->flags == 0
604 && ses1->state == SNAT_SESSION_UNKNOWN)
605 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
607 sum1 = tcp1->checksum;
608 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
610 dst_address /* changed member */ );
611 sum1 = ip_csum_update (sum1, old_port1, new_port1,
612 ip4_header_t /* cheat */ ,
613 length /* changed member */ );
614 mss_clamping (sm, tcp1, &sum1);
615 tcp1->checksum = ip_csum_fold (sum1);
619 ses1->state = SNAT_SESSION_UDP_ACTIVE;
621 if (PREDICT_FALSE (udp1->checksum))
623 sum1 = udp1->checksum;
625 ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
627 dst_address /* changed member */ );
629 ip_csum_update (sum1, old_port1, new_port1,
630 ip4_header_t /* cheat */ ,
631 length /* changed member */ );
632 udp1->checksum = ip_csum_fold (sum1);
638 case SNAT_SESSION_UDP_ACTIVE:
639 ses1->expire = now + sm->udp_timeout;
641 case SNAT_SESSION_TCP_SYN_SENT:
642 case SNAT_SESSION_TCP_FIN_WAIT:
643 case SNAT_SESSION_TCP_CLOSE_WAIT:
644 case SNAT_SESSION_TCP_LAST_ACK:
645 ses1->expire = now + sm->tcp_transitory_timeout;
647 case SNAT_SESSION_TCP_ESTABLISHED:
648 ses1->expire = now + sm->tcp_established_timeout;
653 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
654 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
656 nat_det_in2out_trace_t *t =
657 vlib_add_trace (vm, node, b1, sizeof (*t));
658 t->sw_if_index = sw_if_index1;
659 t->next_index = next1;
660 t->session_index = ~0;
662 t->session_index = ses1 - dm1->sessions;
665 pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
667 /* verify speculative enqueues, maybe switch current next frame */
668 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
669 to_next, n_left_to_next,
670 bi0, bi1, next0, next1);
673 while (n_left_from > 0 && n_left_to_next > 0)
681 ip4_address_t new_addr0, old_addr0;
682 u16 old_port0, new_port0, lo_port0, i0;
686 snat_det_out_key_t key0;
688 snat_det_session_t *ses0 = 0;
690 icmp46_header_t *icmp0;
692 /* speculatively enqueue b0 to the current next frame */
700 b0 = vlib_get_buffer (vm, bi0);
701 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
703 ip0 = vlib_buffer_get_current (b0);
704 udp0 = ip4_next_header (ip0);
705 tcp0 = (tcp_header_t *) udp0;
707 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
709 if (PREDICT_FALSE (ip0->ttl == 1))
711 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
712 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
713 ICMP4_time_exceeded_ttl_exceeded_in_transit,
715 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
719 proto0 = ip_proto_to_nat_proto (ip0->protocol);
721 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
724 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
725 icmp0 = (icmp46_header_t *) udp0;
727 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
728 rx_fib_index0, node, next0, thread_index,
733 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
734 if (PREDICT_FALSE (!dm0))
736 nat_log_info ("no match for internal host %U",
737 format_ip4_address, &ip0->src_address);
738 next0 = NAT_DET_IN2OUT_NEXT_DROP;
739 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
743 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
745 key0.ext_host_addr = ip0->dst_address;
746 key0.ext_host_port = tcp0->dst;
749 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
750 if (PREDICT_FALSE (!ses0))
752 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
754 key0.out_port = clib_host_to_net_u16 (lo_port0 +
761 if (snat_det_get_ses_by_out
762 (dm0, &ip0->src_address, key0.as_u64))
766 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
770 if (PREDICT_FALSE (!ses0))
772 /* too many sessions for user, send ICMP error packet */
773 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
774 icmp4_error_set_vnet_buffer (b0,
775 ICMP4_destination_unreachable,
776 ICMP4_destination_unreachable_destination_unreachable_host,
778 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
783 old_port0 = udp0->src_port;
784 udp0->src_port = new_port0 = ses0->out.out_port;
786 old_addr0.as_u32 = ip0->src_address.as_u32;
787 ip0->src_address.as_u32 = new_addr0.as_u32;
788 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
790 sum0 = ip0->checksum;
791 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
793 src_address /* changed member */ );
794 ip0->checksum = ip_csum_fold (sum0);
796 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
798 if (tcp0->flags & TCP_FLAG_SYN)
799 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
800 else if (tcp0->flags & TCP_FLAG_ACK
801 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
802 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
803 else if (tcp0->flags & TCP_FLAG_FIN
804 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
805 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
806 else if (tcp0->flags & TCP_FLAG_ACK
807 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
808 snat_det_ses_close (dm0, ses0);
809 else if (tcp0->flags & TCP_FLAG_FIN
810 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
811 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
812 else if (tcp0->flags == 0
813 && ses0->state == SNAT_SESSION_UNKNOWN)
814 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
816 sum0 = tcp0->checksum;
817 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
819 dst_address /* changed member */ );
820 sum0 = ip_csum_update (sum0, old_port0, new_port0,
821 ip4_header_t /* cheat */ ,
822 length /* changed member */ );
823 mss_clamping (sm, tcp0, &sum0);
824 tcp0->checksum = ip_csum_fold (sum0);
828 ses0->state = SNAT_SESSION_UDP_ACTIVE;
830 if (PREDICT_FALSE (udp0->checksum))
832 sum0 = udp0->checksum;
834 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
836 dst_address /* changed member */ );
838 ip_csum_update (sum0, old_port0, new_port0,
839 ip4_header_t /* cheat */ ,
840 length /* changed member */ );
841 udp0->checksum = ip_csum_fold (sum0);
847 case SNAT_SESSION_UDP_ACTIVE:
848 ses0->expire = now + sm->udp_timeout;
850 case SNAT_SESSION_TCP_SYN_SENT:
851 case SNAT_SESSION_TCP_FIN_WAIT:
852 case SNAT_SESSION_TCP_CLOSE_WAIT:
853 case SNAT_SESSION_TCP_LAST_ACK:
854 ses0->expire = now + sm->tcp_transitory_timeout;
856 case SNAT_SESSION_TCP_ESTABLISHED:
857 ses0->expire = now + sm->tcp_established_timeout;
862 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
863 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
865 nat_det_in2out_trace_t *t =
866 vlib_add_trace (vm, node, b0, sizeof (*t));
867 t->sw_if_index = sw_if_index0;
868 t->next_index = next0;
869 t->session_index = ~0;
871 t->session_index = ses0 - dm0->sessions;
874 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
876 /* verify speculative enqueue, maybe switch current next frame */
877 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
878 to_next, n_left_to_next,
882 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
885 vlib_node_increment_counter (vm, sm->det_in2out_node_index,
886 NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
888 return frame->n_vectors;
892 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
893 .name = "nat44-det-in2out",
894 .vector_size = sizeof (u32),
895 .format_trace = format_nat_det_in2out_trace,
896 .type = VLIB_NODE_TYPE_INTERNAL,
897 .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
898 .error_strings = nat_det_in2out_error_strings,
899 .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
900 /* edit / add dispositions here */
902 [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
903 [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
904 [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
910 * fd.io coding-style-patch-verification: ON
913 * eval: (c-set-style "gnu")