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[out] p_proto protocol used for matching
89 * @param[out] p_value address and port after NAT translation
90 * @param[out] p_dont_translate if packet should not be translated
91 * @param d optional parameter
92 * @param e optional parameter
95 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
96 u32 thread_index, vlib_buffer_t * b0,
97 ip4_header_t * ip0, u8 * p_proto,
98 snat_session_key_t * p_value,
99 u8 * p_dont_translate, void *d, void *e)
101 icmp46_header_t *icmp0;
105 snat_det_out_key_t key0;
106 u8 dont_translate = 0;
108 icmp_echo_header_t *echo0, *inner_echo0 = 0;
109 ip4_header_t *inner_ip0;
111 icmp46_header_t *inner_icmp0;
112 snat_det_map_t *dm0 = 0;
113 ip4_address_t new_addr0;
115 snat_det_session_t *ses0 = 0;
116 ip4_address_t in_addr;
119 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
120 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
121 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
122 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
124 if (!icmp_is_error_message (icmp0))
126 protocol = SNAT_PROTOCOL_ICMP;
127 in_addr = ip0->src_address;
128 in_port = echo0->identifier;
132 inner_ip0 = (ip4_header_t *) (echo0 + 1);
133 l4_header = ip4_next_header (inner_ip0);
134 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
135 in_addr = inner_ip0->dst_address;
138 case SNAT_PROTOCOL_ICMP:
139 inner_icmp0 = (icmp46_header_t *) l4_header;
140 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
141 in_port = inner_echo0->identifier;
143 case SNAT_PROTOCOL_UDP:
144 case SNAT_PROTOCOL_TCP:
145 in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
148 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
149 next0 = NAT_DET_IN2OUT_NEXT_DROP;
154 dm0 = snat_det_map_by_user (sm, &in_addr);
155 if (PREDICT_FALSE (!dm0))
157 nat_log_info ("no match for internal host %U",
158 format_ip4_address, &in_addr);
159 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
166 next0 = NAT_DET_IN2OUT_NEXT_DROP;
167 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
171 snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
173 key0.ext_host_addr = ip0->dst_address;
174 key0.ext_host_port = 0;
176 ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
177 if (PREDICT_FALSE (!ses0))
179 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
186 if (icmp0->type != ICMP4_echo_request)
188 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
189 next0 = NAT_DET_IN2OUT_NEXT_DROP;
192 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
194 key0.out_port = clib_host_to_net_u16 (lo_port0 +
197 (echo0->identifier)) %
198 dm0->ports_per_host));
200 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
204 snat_det_ses_create (thread_index, dm0,
205 &in_addr, echo0->identifier, &key0);
208 if (PREDICT_FALSE (!ses0))
210 next0 = NAT_DET_IN2OUT_NEXT_DROP;
211 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
216 if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
217 !icmp_is_error_message (icmp0)))
219 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
220 next0 = NAT_DET_IN2OUT_NEXT_DROP;
224 u32 now = (u32) vlib_time_now (sm->vlib_main);
226 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
227 ses0->expire = now + sm->icmp_timeout;
233 p_value->addr = new_addr0;
234 p_value->fib_index = sm->outside_fib_index;
235 p_value->port = ses0->out.out_port;
237 *p_dont_translate = dont_translate;
239 *(snat_det_session_t **) d = ses0;
241 *(snat_det_map_t **) e = dm0;
246 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
247 vlib_node_runtime_t * node,
248 vlib_frame_t * frame)
250 u32 n_left_from, *from, *to_next;
251 nat_det_in2out_next_t next_index;
252 u32 pkts_processed = 0;
253 snat_main_t *sm = &snat_main;
254 u32 now = (u32) vlib_time_now (vm);
255 u32 thread_index = vm->thread_index;
257 from = vlib_frame_vector_args (frame);
258 n_left_from = frame->n_vectors;
259 next_index = node->cached_next_index;
261 while (n_left_from > 0)
265 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
267 while (n_left_from >= 4 && n_left_to_next >= 2)
270 vlib_buffer_t *b0, *b1;
272 u32 sw_if_index0, sw_if_index1;
273 ip4_header_t *ip0, *ip1;
274 ip_csum_t sum0, sum1;
275 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
276 u16 old_port0, new_port0, lo_port0, i0;
277 u16 old_port1, new_port1, lo_port1, i1;
278 udp_header_t *udp0, *udp1;
279 tcp_header_t *tcp0, *tcp1;
281 snat_det_out_key_t key0, key1;
282 snat_det_map_t *dm0, *dm1;
283 snat_det_session_t *ses0 = 0, *ses1 = 0;
284 u32 rx_fib_index0, rx_fib_index1;
285 icmp46_header_t *icmp0, *icmp1;
287 /* Prefetch next iteration. */
289 vlib_buffer_t *p2, *p3;
291 p2 = vlib_get_buffer (vm, from[2]);
292 p3 = vlib_get_buffer (vm, from[3]);
294 vlib_prefetch_buffer_header (p2, LOAD);
295 vlib_prefetch_buffer_header (p3, LOAD);
297 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
298 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
301 /* speculatively enqueue b0 and b1 to the current next frame */
302 to_next[0] = bi0 = from[0];
303 to_next[1] = bi1 = from[1];
309 b0 = vlib_get_buffer (vm, bi0);
310 b1 = vlib_get_buffer (vm, bi1);
312 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
313 next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
315 ip0 = vlib_buffer_get_current (b0);
316 udp0 = ip4_next_header (ip0);
317 tcp0 = (tcp_header_t *) udp0;
319 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
321 if (PREDICT_FALSE (ip0->ttl == 1))
323 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
324 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
325 ICMP4_time_exceeded_ttl_exceeded_in_transit,
327 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
331 proto0 = ip_proto_to_snat_proto (ip0->protocol);
333 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
336 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
337 icmp0 = (icmp46_header_t *) udp0;
339 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
340 rx_fib_index0, node, next0, thread_index,
345 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
346 if (PREDICT_FALSE (!dm0))
348 nat_log_info ("no match for internal host %U",
349 format_ip4_address, &ip0->src_address);
350 next0 = NAT_DET_IN2OUT_NEXT_DROP;
351 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
355 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
357 key0.ext_host_addr = ip0->dst_address;
358 key0.ext_host_port = tcp0->dst;
361 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
362 if (PREDICT_FALSE (!ses0))
364 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
366 key0.out_port = clib_host_to_net_u16 (lo_port0 +
373 if (snat_det_get_ses_by_out
374 (dm0, &ip0->src_address, key0.as_u64))
378 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
382 if (PREDICT_FALSE (!ses0))
384 /* too many sessions for user, send ICMP error packet */
385 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
386 icmp4_error_set_vnet_buffer (b0,
387 ICMP4_destination_unreachable,
388 ICMP4_destination_unreachable_destination_unreachable_host,
390 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
395 old_port0 = udp0->src_port;
396 udp0->src_port = new_port0 = ses0->out.out_port;
398 old_addr0.as_u32 = ip0->src_address.as_u32;
399 ip0->src_address.as_u32 = new_addr0.as_u32;
400 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
402 sum0 = ip0->checksum;
403 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
405 src_address /* changed member */ );
406 ip0->checksum = ip_csum_fold (sum0);
408 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
410 if (tcp0->flags & TCP_FLAG_SYN)
411 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
412 else if (tcp0->flags & TCP_FLAG_ACK
413 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
414 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
415 else if (tcp0->flags & TCP_FLAG_FIN
416 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
417 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
418 else if (tcp0->flags & TCP_FLAG_ACK
419 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
420 snat_det_ses_close (dm0, ses0);
421 else if (tcp0->flags & TCP_FLAG_FIN
422 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
423 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
424 else if (tcp0->flags == 0
425 && ses0->state == SNAT_SESSION_UNKNOWN)
426 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
428 sum0 = tcp0->checksum;
429 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
431 dst_address /* changed member */ );
432 sum0 = ip_csum_update (sum0, old_port0, new_port0,
433 ip4_header_t /* cheat */ ,
434 length /* changed member */ );
435 mss_clamping (sm, tcp0, &sum0);
436 tcp0->checksum = ip_csum_fold (sum0);
440 ses0->state = SNAT_SESSION_UDP_ACTIVE;
442 if (PREDICT_FALSE (udp0->checksum))
444 sum0 = udp0->checksum;
446 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
448 dst_address /* changed member */ );
450 ip_csum_update (sum0, old_port0, new_port0,
451 ip4_header_t /* cheat */ ,
452 length /* changed member */ );
453 udp0->checksum = ip_csum_fold (sum0);
459 case SNAT_SESSION_UDP_ACTIVE:
460 ses0->expire = now + sm->udp_timeout;
462 case SNAT_SESSION_TCP_SYN_SENT:
463 case SNAT_SESSION_TCP_FIN_WAIT:
464 case SNAT_SESSION_TCP_CLOSE_WAIT:
465 case SNAT_SESSION_TCP_LAST_ACK:
466 ses0->expire = now + sm->tcp_transitory_timeout;
468 case SNAT_SESSION_TCP_ESTABLISHED:
469 ses0->expire = now + sm->tcp_established_timeout;
474 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
475 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
477 nat_det_in2out_trace_t *t =
478 vlib_add_trace (vm, node, b0, sizeof (*t));
479 t->sw_if_index = sw_if_index0;
480 t->next_index = next0;
481 t->session_index = ~0;
483 t->session_index = ses0 - dm0->sessions;
486 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
488 ip1 = vlib_buffer_get_current (b1);
489 udp1 = ip4_next_header (ip1);
490 tcp1 = (tcp_header_t *) udp1;
492 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
494 if (PREDICT_FALSE (ip1->ttl == 1))
496 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
497 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
498 ICMP4_time_exceeded_ttl_exceeded_in_transit,
500 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
504 proto1 = ip_proto_to_snat_proto (ip1->protocol);
506 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
509 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
510 icmp1 = (icmp46_header_t *) udp1;
512 next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
513 rx_fib_index1, node, next1, thread_index,
518 dm1 = snat_det_map_by_user (sm, &ip1->src_address);
519 if (PREDICT_FALSE (!dm1))
521 nat_log_info ("no match for internal host %U",
522 format_ip4_address, &ip0->src_address);
523 next1 = NAT_DET_IN2OUT_NEXT_DROP;
524 b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
528 snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
530 key1.ext_host_addr = ip1->dst_address;
531 key1.ext_host_port = tcp1->dst;
534 snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
535 if (PREDICT_FALSE (!ses1))
537 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
539 key1.out_port = clib_host_to_net_u16 (lo_port1 +
546 if (snat_det_get_ses_by_out
547 (dm1, &ip1->src_address, key1.as_u64))
551 snat_det_ses_create (thread_index, dm1, &ip1->src_address,
555 if (PREDICT_FALSE (!ses1))
557 /* too many sessions for user, send ICMP error packet */
558 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
559 icmp4_error_set_vnet_buffer (b1,
560 ICMP4_destination_unreachable,
561 ICMP4_destination_unreachable_destination_unreachable_host,
563 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
568 old_port1 = udp1->src_port;
569 udp1->src_port = new_port1 = ses1->out.out_port;
571 old_addr1.as_u32 = ip1->src_address.as_u32;
572 ip1->src_address.as_u32 = new_addr1.as_u32;
573 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
575 sum1 = ip1->checksum;
576 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
578 src_address /* changed member */ );
579 ip1->checksum = ip_csum_fold (sum1);
581 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
583 if (tcp1->flags & TCP_FLAG_SYN)
584 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
585 else if (tcp1->flags & TCP_FLAG_ACK
586 && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
587 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
588 else if (tcp1->flags & TCP_FLAG_FIN
589 && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
590 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
591 else if (tcp1->flags & TCP_FLAG_ACK
592 && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
593 snat_det_ses_close (dm1, ses1);
594 else if (tcp1->flags & TCP_FLAG_FIN
595 && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
596 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
597 else if (tcp1->flags == 0
598 && ses1->state == SNAT_SESSION_UNKNOWN)
599 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
601 sum1 = tcp1->checksum;
602 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
604 dst_address /* changed member */ );
605 sum1 = ip_csum_update (sum1, old_port1, new_port1,
606 ip4_header_t /* cheat */ ,
607 length /* changed member */ );
608 mss_clamping (sm, tcp1, &sum1);
609 tcp1->checksum = ip_csum_fold (sum1);
613 ses1->state = SNAT_SESSION_UDP_ACTIVE;
615 if (PREDICT_FALSE (udp1->checksum))
617 sum1 = udp1->checksum;
619 ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
621 dst_address /* changed member */ );
623 ip_csum_update (sum1, old_port1, new_port1,
624 ip4_header_t /* cheat */ ,
625 length /* changed member */ );
626 udp1->checksum = ip_csum_fold (sum1);
632 case SNAT_SESSION_UDP_ACTIVE:
633 ses1->expire = now + sm->udp_timeout;
635 case SNAT_SESSION_TCP_SYN_SENT:
636 case SNAT_SESSION_TCP_FIN_WAIT:
637 case SNAT_SESSION_TCP_CLOSE_WAIT:
638 case SNAT_SESSION_TCP_LAST_ACK:
639 ses1->expire = now + sm->tcp_transitory_timeout;
641 case SNAT_SESSION_TCP_ESTABLISHED:
642 ses1->expire = now + sm->tcp_established_timeout;
647 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
648 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
650 nat_det_in2out_trace_t *t =
651 vlib_add_trace (vm, node, b1, sizeof (*t));
652 t->sw_if_index = sw_if_index1;
653 t->next_index = next1;
654 t->session_index = ~0;
656 t->session_index = ses1 - dm1->sessions;
659 pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
661 /* verify speculative enqueues, maybe switch current next frame */
662 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
663 to_next, n_left_to_next,
664 bi0, bi1, next0, next1);
667 while (n_left_from > 0 && n_left_to_next > 0)
675 ip4_address_t new_addr0, old_addr0;
676 u16 old_port0, new_port0, lo_port0, i0;
680 snat_det_out_key_t key0;
682 snat_det_session_t *ses0 = 0;
684 icmp46_header_t *icmp0;
686 /* speculatively enqueue b0 to the current next frame */
694 b0 = vlib_get_buffer (vm, bi0);
695 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
697 ip0 = vlib_buffer_get_current (b0);
698 udp0 = ip4_next_header (ip0);
699 tcp0 = (tcp_header_t *) udp0;
701 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
703 if (PREDICT_FALSE (ip0->ttl == 1))
705 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
706 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
707 ICMP4_time_exceeded_ttl_exceeded_in_transit,
709 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
713 proto0 = ip_proto_to_snat_proto (ip0->protocol);
715 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
718 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
719 icmp0 = (icmp46_header_t *) udp0;
721 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
722 rx_fib_index0, node, next0, thread_index,
727 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
728 if (PREDICT_FALSE (!dm0))
730 nat_log_info ("no match for internal host %U",
731 format_ip4_address, &ip0->src_address);
732 next0 = NAT_DET_IN2OUT_NEXT_DROP;
733 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
737 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
739 key0.ext_host_addr = ip0->dst_address;
740 key0.ext_host_port = tcp0->dst;
743 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
744 if (PREDICT_FALSE (!ses0))
746 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
748 key0.out_port = clib_host_to_net_u16 (lo_port0 +
755 if (snat_det_get_ses_by_out
756 (dm0, &ip0->src_address, key0.as_u64))
760 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
764 if (PREDICT_FALSE (!ses0))
766 /* too many sessions for user, send ICMP error packet */
767 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
768 icmp4_error_set_vnet_buffer (b0,
769 ICMP4_destination_unreachable,
770 ICMP4_destination_unreachable_destination_unreachable_host,
772 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
777 old_port0 = udp0->src_port;
778 udp0->src_port = new_port0 = ses0->out.out_port;
780 old_addr0.as_u32 = ip0->src_address.as_u32;
781 ip0->src_address.as_u32 = new_addr0.as_u32;
782 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
784 sum0 = ip0->checksum;
785 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
787 src_address /* changed member */ );
788 ip0->checksum = ip_csum_fold (sum0);
790 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
792 if (tcp0->flags & TCP_FLAG_SYN)
793 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
794 else if (tcp0->flags & TCP_FLAG_ACK
795 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
796 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
797 else if (tcp0->flags & TCP_FLAG_FIN
798 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
799 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
800 else if (tcp0->flags & TCP_FLAG_ACK
801 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
802 snat_det_ses_close (dm0, ses0);
803 else if (tcp0->flags & TCP_FLAG_FIN
804 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
805 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
806 else if (tcp0->flags == 0
807 && ses0->state == SNAT_SESSION_UNKNOWN)
808 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
810 sum0 = tcp0->checksum;
811 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
813 dst_address /* changed member */ );
814 sum0 = ip_csum_update (sum0, old_port0, new_port0,
815 ip4_header_t /* cheat */ ,
816 length /* changed member */ );
817 mss_clamping (sm, tcp0, &sum0);
818 tcp0->checksum = ip_csum_fold (sum0);
822 ses0->state = SNAT_SESSION_UDP_ACTIVE;
824 if (PREDICT_FALSE (udp0->checksum))
826 sum0 = udp0->checksum;
828 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
830 dst_address /* changed member */ );
832 ip_csum_update (sum0, old_port0, new_port0,
833 ip4_header_t /* cheat */ ,
834 length /* changed member */ );
835 udp0->checksum = ip_csum_fold (sum0);
841 case SNAT_SESSION_UDP_ACTIVE:
842 ses0->expire = now + sm->udp_timeout;
844 case SNAT_SESSION_TCP_SYN_SENT:
845 case SNAT_SESSION_TCP_FIN_WAIT:
846 case SNAT_SESSION_TCP_CLOSE_WAIT:
847 case SNAT_SESSION_TCP_LAST_ACK:
848 ses0->expire = now + sm->tcp_transitory_timeout;
850 case SNAT_SESSION_TCP_ESTABLISHED:
851 ses0->expire = now + sm->tcp_established_timeout;
856 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
857 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
859 nat_det_in2out_trace_t *t =
860 vlib_add_trace (vm, node, b0, sizeof (*t));
861 t->sw_if_index = sw_if_index0;
862 t->next_index = next0;
863 t->session_index = ~0;
865 t->session_index = ses0 - dm0->sessions;
868 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
870 /* verify speculative enqueue, maybe switch current next frame */
871 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
872 to_next, n_left_to_next,
876 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
879 vlib_node_increment_counter (vm, sm->det_in2out_node_index,
880 NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
882 return frame->n_vectors;
886 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
887 .name = "nat44-det-in2out",
888 .vector_size = sizeof (u32),
889 .format_trace = format_nat_det_in2out_trace,
890 .type = VLIB_NODE_TYPE_INTERNAL,
891 .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
892 .error_strings = nat_det_in2out_error_strings,
893 .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
894 /* edit / add dispositions here */
896 [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
897 [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
898 [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
904 * fd.io coding-style-patch-verification: ON
907 * eval: (c-set-style "gnu")