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 new_port0 = ses0->out.out_port;
397 old_addr0.as_u32 = ip0->src_address.as_u32;
398 ip0->src_address.as_u32 = new_addr0.as_u32;
399 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
401 sum0 = ip0->checksum;
402 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
404 src_address /* changed member */ );
405 ip0->checksum = ip_csum_fold (sum0);
407 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
409 if (tcp0->flags & TCP_FLAG_SYN)
410 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
411 else if (tcp0->flags & TCP_FLAG_ACK
412 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
413 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
414 else if (tcp0->flags & TCP_FLAG_FIN
415 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
416 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
417 else if (tcp0->flags & TCP_FLAG_ACK
418 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
419 snat_det_ses_close (dm0, ses0);
420 else if (tcp0->flags & TCP_FLAG_FIN
421 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
422 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
423 else if (tcp0->flags == 0
424 && ses0->state == SNAT_SESSION_UNKNOWN)
425 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
427 old_port0 = tcp0->src;
428 tcp0->src = new_port0;
430 sum0 = tcp0->checksum;
431 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
433 dst_address /* changed member */ );
434 sum0 = ip_csum_update (sum0, old_port0, new_port0,
435 ip4_header_t /* cheat */ ,
436 length /* changed member */ );
437 mss_clamping (sm, tcp0, &sum0);
438 tcp0->checksum = ip_csum_fold (sum0);
442 ses0->state = SNAT_SESSION_UDP_ACTIVE;
443 old_port0 = udp0->src_port;
444 udp0->src_port = new_port0;
450 case SNAT_SESSION_UDP_ACTIVE:
451 ses0->expire = now + sm->udp_timeout;
453 case SNAT_SESSION_TCP_SYN_SENT:
454 case SNAT_SESSION_TCP_FIN_WAIT:
455 case SNAT_SESSION_TCP_CLOSE_WAIT:
456 case SNAT_SESSION_TCP_LAST_ACK:
457 ses0->expire = now + sm->tcp_transitory_timeout;
459 case SNAT_SESSION_TCP_ESTABLISHED:
460 ses0->expire = now + sm->tcp_established_timeout;
465 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
466 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
468 nat_det_in2out_trace_t *t =
469 vlib_add_trace (vm, node, b0, sizeof (*t));
470 t->sw_if_index = sw_if_index0;
471 t->next_index = next0;
472 t->session_index = ~0;
474 t->session_index = ses0 - dm0->sessions;
477 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
479 ip1 = vlib_buffer_get_current (b1);
480 udp1 = ip4_next_header (ip1);
481 tcp1 = (tcp_header_t *) udp1;
483 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
485 if (PREDICT_FALSE (ip1->ttl == 1))
487 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
488 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
489 ICMP4_time_exceeded_ttl_exceeded_in_transit,
491 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
495 proto1 = ip_proto_to_snat_proto (ip1->protocol);
497 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
500 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
501 icmp1 = (icmp46_header_t *) udp1;
503 next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
504 rx_fib_index1, node, next1, thread_index,
509 dm1 = snat_det_map_by_user (sm, &ip1->src_address);
510 if (PREDICT_FALSE (!dm1))
512 nat_log_info ("no match for internal host %U",
513 format_ip4_address, &ip0->src_address);
514 next1 = NAT_DET_IN2OUT_NEXT_DROP;
515 b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
519 snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
521 key1.ext_host_addr = ip1->dst_address;
522 key1.ext_host_port = tcp1->dst;
525 snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
526 if (PREDICT_FALSE (!ses1))
528 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
530 key1.out_port = clib_host_to_net_u16 (lo_port1 +
537 if (snat_det_get_ses_by_out
538 (dm1, &ip1->src_address, key1.as_u64))
542 snat_det_ses_create (thread_index, dm1, &ip1->src_address,
546 if (PREDICT_FALSE (!ses1))
548 /* too many sessions for user, send ICMP error packet */
549 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
550 icmp4_error_set_vnet_buffer (b1,
551 ICMP4_destination_unreachable,
552 ICMP4_destination_unreachable_destination_unreachable_host,
554 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
559 new_port1 = ses1->out.out_port;
561 old_addr1.as_u32 = ip1->src_address.as_u32;
562 ip1->src_address.as_u32 = new_addr1.as_u32;
563 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
565 sum1 = ip1->checksum;
566 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
568 src_address /* changed member */ );
569 ip1->checksum = ip_csum_fold (sum1);
571 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
573 if (tcp1->flags & TCP_FLAG_SYN)
574 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
575 else if (tcp1->flags & TCP_FLAG_ACK
576 && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
577 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
578 else if (tcp1->flags & TCP_FLAG_FIN
579 && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
580 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
581 else if (tcp1->flags & TCP_FLAG_ACK
582 && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
583 snat_det_ses_close (dm1, ses1);
584 else if (tcp1->flags & TCP_FLAG_FIN
585 && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
586 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
587 else if (tcp1->flags == 0
588 && ses1->state == SNAT_SESSION_UNKNOWN)
589 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
591 old_port1 = tcp1->src;
592 tcp1->src = new_port1;
594 sum1 = tcp1->checksum;
595 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
597 dst_address /* changed member */ );
598 sum1 = ip_csum_update (sum1, old_port1, new_port1,
599 ip4_header_t /* cheat */ ,
600 length /* changed member */ );
601 mss_clamping (sm, tcp1, &sum1);
602 tcp1->checksum = ip_csum_fold (sum1);
606 ses1->state = SNAT_SESSION_UDP_ACTIVE;
607 old_port1 = udp1->src_port;
608 udp1->src_port = new_port1;
614 case SNAT_SESSION_UDP_ACTIVE:
615 ses1->expire = now + sm->udp_timeout;
617 case SNAT_SESSION_TCP_SYN_SENT:
618 case SNAT_SESSION_TCP_FIN_WAIT:
619 case SNAT_SESSION_TCP_CLOSE_WAIT:
620 case SNAT_SESSION_TCP_LAST_ACK:
621 ses1->expire = now + sm->tcp_transitory_timeout;
623 case SNAT_SESSION_TCP_ESTABLISHED:
624 ses1->expire = now + sm->tcp_established_timeout;
629 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
630 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
632 nat_det_in2out_trace_t *t =
633 vlib_add_trace (vm, node, b1, sizeof (*t));
634 t->sw_if_index = sw_if_index1;
635 t->next_index = next1;
636 t->session_index = ~0;
638 t->session_index = ses1 - dm1->sessions;
641 pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
643 /* verify speculative enqueues, maybe switch current next frame */
644 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
645 to_next, n_left_to_next,
646 bi0, bi1, next0, next1);
649 while (n_left_from > 0 && n_left_to_next > 0)
657 ip4_address_t new_addr0, old_addr0;
658 u16 old_port0, new_port0, lo_port0, i0;
662 snat_det_out_key_t key0;
664 snat_det_session_t *ses0 = 0;
666 icmp46_header_t *icmp0;
668 /* speculatively enqueue b0 to the current next frame */
676 b0 = vlib_get_buffer (vm, bi0);
677 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
679 ip0 = vlib_buffer_get_current (b0);
680 udp0 = ip4_next_header (ip0);
681 tcp0 = (tcp_header_t *) udp0;
683 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
685 if (PREDICT_FALSE (ip0->ttl == 1))
687 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
688 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
689 ICMP4_time_exceeded_ttl_exceeded_in_transit,
691 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
695 proto0 = ip_proto_to_snat_proto (ip0->protocol);
697 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
700 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
701 icmp0 = (icmp46_header_t *) udp0;
703 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
704 rx_fib_index0, node, next0, thread_index,
709 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
710 if (PREDICT_FALSE (!dm0))
712 nat_log_info ("no match for internal host %U",
713 format_ip4_address, &ip0->src_address);
714 next0 = NAT_DET_IN2OUT_NEXT_DROP;
715 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
719 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
721 key0.ext_host_addr = ip0->dst_address;
722 key0.ext_host_port = tcp0->dst;
725 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
726 if (PREDICT_FALSE (!ses0))
728 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
730 key0.out_port = clib_host_to_net_u16 (lo_port0 +
737 if (snat_det_get_ses_by_out
738 (dm0, &ip0->src_address, key0.as_u64))
742 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
746 if (PREDICT_FALSE (!ses0))
748 /* too many sessions for user, send ICMP error packet */
749 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
750 icmp4_error_set_vnet_buffer (b0,
751 ICMP4_destination_unreachable,
752 ICMP4_destination_unreachable_destination_unreachable_host,
754 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
759 new_port0 = ses0->out.out_port;
761 old_addr0.as_u32 = ip0->src_address.as_u32;
762 ip0->src_address.as_u32 = new_addr0.as_u32;
763 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
765 sum0 = ip0->checksum;
766 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
768 src_address /* changed member */ );
769 ip0->checksum = ip_csum_fold (sum0);
771 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
773 if (tcp0->flags & TCP_FLAG_SYN)
774 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
775 else if (tcp0->flags & TCP_FLAG_ACK
776 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
777 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
778 else if (tcp0->flags & TCP_FLAG_FIN
779 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
780 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
781 else if (tcp0->flags & TCP_FLAG_ACK
782 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
783 snat_det_ses_close (dm0, ses0);
784 else if (tcp0->flags & TCP_FLAG_FIN
785 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
786 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
787 else if (tcp0->flags == 0
788 && ses0->state == SNAT_SESSION_UNKNOWN)
789 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
791 old_port0 = tcp0->src;
792 tcp0->src = new_port0;
794 sum0 = tcp0->checksum;
795 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
797 dst_address /* changed member */ );
798 sum0 = ip_csum_update (sum0, old_port0, new_port0,
799 ip4_header_t /* cheat */ ,
800 length /* changed member */ );
801 mss_clamping (sm, tcp0, &sum0);
802 tcp0->checksum = ip_csum_fold (sum0);
806 ses0->state = SNAT_SESSION_UDP_ACTIVE;
807 old_port0 = udp0->src_port;
808 udp0->src_port = new_port0;
814 case SNAT_SESSION_UDP_ACTIVE:
815 ses0->expire = now + sm->udp_timeout;
817 case SNAT_SESSION_TCP_SYN_SENT:
818 case SNAT_SESSION_TCP_FIN_WAIT:
819 case SNAT_SESSION_TCP_CLOSE_WAIT:
820 case SNAT_SESSION_TCP_LAST_ACK:
821 ses0->expire = now + sm->tcp_transitory_timeout;
823 case SNAT_SESSION_TCP_ESTABLISHED:
824 ses0->expire = now + sm->tcp_established_timeout;
829 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
830 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
832 nat_det_in2out_trace_t *t =
833 vlib_add_trace (vm, node, b0, sizeof (*t));
834 t->sw_if_index = sw_if_index0;
835 t->next_index = next0;
836 t->session_index = ~0;
838 t->session_index = ses0 - dm0->sessions;
841 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
843 /* verify speculative enqueue, maybe switch current next frame */
844 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
845 to_next, n_left_to_next,
849 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
852 vlib_node_increment_counter (vm, sm->det_in2out_node_index,
853 NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
855 return frame->n_vectors;
859 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
860 .name = "nat44-det-in2out",
861 .vector_size = sizeof (u32),
862 .format_trace = format_nat_det_in2out_trace,
863 .type = VLIB_NODE_TYPE_INTERNAL,
864 .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
865 .error_strings = nat_det_in2out_error_strings,
866 .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
867 /* edit / add dispositions here */
869 [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
870 [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
871 [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
877 * fd.io coding-style-patch-verification: ON
880 * eval: (c-set-style "gnu")