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, u8 * p_dont_translate,
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_type_is_error_message
125 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
127 protocol = SNAT_PROTOCOL_ICMP;
128 in_addr = ip0->src_address;
129 in_port = vnet_buffer (b0)->ip.reass.l4_src_port;
133 /* if error message, then it's not fragmented and we can access it */
134 inner_ip0 = (ip4_header_t *) (echo0 + 1);
135 l4_header = ip4_next_header (inner_ip0);
136 protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
137 in_addr = inner_ip0->dst_address;
140 case SNAT_PROTOCOL_ICMP:
141 inner_icmp0 = (icmp46_header_t *) l4_header;
142 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
143 in_port = inner_echo0->identifier;
145 case SNAT_PROTOCOL_UDP:
146 case SNAT_PROTOCOL_TCP:
147 in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
150 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
151 next0 = NAT_DET_IN2OUT_NEXT_DROP;
156 dm0 = snat_det_map_by_user (sm, &in_addr);
157 if (PREDICT_FALSE (!dm0))
159 nat_log_info ("no match for internal host %U",
160 format_ip4_address, &in_addr);
161 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
168 next0 = NAT_DET_IN2OUT_NEXT_DROP;
169 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
173 snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
175 key0.ext_host_addr = ip0->dst_address;
176 key0.ext_host_port = 0;
178 ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
179 if (PREDICT_FALSE (!ses0))
181 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
188 if (icmp0->type != ICMP4_echo_request)
190 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
191 next0 = NAT_DET_IN2OUT_NEXT_DROP;
194 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
196 key0.out_port = clib_host_to_net_u16 (lo_port0 +
199 (echo0->identifier)) %
200 dm0->ports_per_host));
202 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
206 snat_det_ses_create (thread_index, dm0,
207 &in_addr, echo0->identifier, &key0);
210 if (PREDICT_FALSE (!ses0))
212 next0 = NAT_DET_IN2OUT_NEXT_DROP;
213 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
219 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
220 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
221 reass.icmp_type_or_tcp_flags)))
223 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
224 next0 = NAT_DET_IN2OUT_NEXT_DROP;
228 u32 now = (u32) vlib_time_now (sm->vlib_main);
230 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
231 ses0->expire = now + sm->icmp_timeout;
237 p_value->addr = new_addr0;
238 p_value->fib_index = sm->outside_fib_index;
239 p_value->port = ses0->out.out_port;
241 *p_dont_translate = dont_translate;
243 *(snat_det_session_t **) d = ses0;
245 *(snat_det_map_t **) e = dm0;
250 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
251 vlib_node_runtime_t * node,
252 vlib_frame_t * frame)
254 u32 n_left_from, *from, *to_next;
255 nat_det_in2out_next_t next_index;
256 u32 pkts_processed = 0;
257 snat_main_t *sm = &snat_main;
258 u32 now = (u32) vlib_time_now (vm);
259 u32 thread_index = vm->thread_index;
261 from = vlib_frame_vector_args (frame);
262 n_left_from = frame->n_vectors;
263 next_index = node->cached_next_index;
265 while (n_left_from > 0)
269 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
271 while (n_left_from >= 4 && n_left_to_next >= 2)
274 vlib_buffer_t *b0, *b1;
276 u32 sw_if_index0, sw_if_index1;
277 ip4_header_t *ip0, *ip1;
278 ip_csum_t sum0, sum1;
279 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
280 u16 old_port0, new_port0, lo_port0, i0;
281 u16 old_port1, new_port1, lo_port1, i1;
282 udp_header_t *udp0, *udp1;
283 tcp_header_t *tcp0, *tcp1;
285 snat_det_out_key_t key0, key1;
286 snat_det_map_t *dm0, *dm1;
287 snat_det_session_t *ses0 = 0, *ses1 = 0;
288 u32 rx_fib_index0, rx_fib_index1;
289 icmp46_header_t *icmp0, *icmp1;
291 /* Prefetch next iteration. */
293 vlib_buffer_t *p2, *p3;
295 p2 = vlib_get_buffer (vm, from[2]);
296 p3 = vlib_get_buffer (vm, from[3]);
298 vlib_prefetch_buffer_header (p2, LOAD);
299 vlib_prefetch_buffer_header (p3, LOAD);
301 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
302 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
305 /* speculatively enqueue b0 and b1 to the current next frame */
306 to_next[0] = bi0 = from[0];
307 to_next[1] = bi1 = from[1];
313 b0 = vlib_get_buffer (vm, bi0);
314 b1 = vlib_get_buffer (vm, bi1);
316 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
317 next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
319 ip0 = vlib_buffer_get_current (b0);
320 udp0 = ip4_next_header (ip0);
321 tcp0 = (tcp_header_t *) udp0;
323 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
325 if (PREDICT_FALSE (ip0->ttl == 1))
327 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
328 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
329 ICMP4_time_exceeded_ttl_exceeded_in_transit,
331 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
335 proto0 = ip_proto_to_snat_proto (ip0->protocol);
337 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
340 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
341 icmp0 = (icmp46_header_t *) udp0;
343 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
344 rx_fib_index0, node, next0, thread_index,
349 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
350 if (PREDICT_FALSE (!dm0))
352 nat_log_info ("no match for internal host %U",
353 format_ip4_address, &ip0->src_address);
354 next0 = NAT_DET_IN2OUT_NEXT_DROP;
355 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
359 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
361 key0.ext_host_addr = ip0->dst_address;
362 key0.ext_host_port = tcp0->dst;
365 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
366 if (PREDICT_FALSE (!ses0))
368 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
370 key0.out_port = clib_host_to_net_u16 (lo_port0 +
377 if (snat_det_get_ses_by_out
378 (dm0, &ip0->src_address, key0.as_u64))
382 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
386 if (PREDICT_FALSE (!ses0))
388 /* too many sessions for user, send ICMP error packet */
389 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
390 icmp4_error_set_vnet_buffer (b0,
391 ICMP4_destination_unreachable,
392 ICMP4_destination_unreachable_destination_unreachable_host,
394 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
399 old_port0 = udp0->src_port;
400 udp0->src_port = new_port0 = ses0->out.out_port;
402 old_addr0.as_u32 = ip0->src_address.as_u32;
403 ip0->src_address.as_u32 = new_addr0.as_u32;
404 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
406 sum0 = ip0->checksum;
407 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
409 src_address /* changed member */ );
410 ip0->checksum = ip_csum_fold (sum0);
412 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
414 if (tcp0->flags & TCP_FLAG_SYN)
415 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
416 else if (tcp0->flags & TCP_FLAG_ACK
417 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
418 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
419 else if (tcp0->flags & TCP_FLAG_FIN
420 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
421 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
422 else if (tcp0->flags & TCP_FLAG_ACK
423 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
424 snat_det_ses_close (dm0, ses0);
425 else if (tcp0->flags & TCP_FLAG_FIN
426 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
427 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
428 else if (tcp0->flags == 0
429 && ses0->state == SNAT_SESSION_UNKNOWN)
430 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
432 sum0 = tcp0->checksum;
433 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
435 dst_address /* changed member */ );
436 sum0 = ip_csum_update (sum0, old_port0, new_port0,
437 ip4_header_t /* cheat */ ,
438 length /* changed member */ );
439 mss_clamping (sm, tcp0, &sum0);
440 tcp0->checksum = ip_csum_fold (sum0);
444 ses0->state = SNAT_SESSION_UDP_ACTIVE;
446 if (PREDICT_FALSE (udp0->checksum))
448 sum0 = udp0->checksum;
450 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
452 dst_address /* changed member */ );
454 ip_csum_update (sum0, old_port0, new_port0,
455 ip4_header_t /* cheat */ ,
456 length /* changed member */ );
457 udp0->checksum = ip_csum_fold (sum0);
463 case SNAT_SESSION_UDP_ACTIVE:
464 ses0->expire = now + sm->udp_timeout;
466 case SNAT_SESSION_TCP_SYN_SENT:
467 case SNAT_SESSION_TCP_FIN_WAIT:
468 case SNAT_SESSION_TCP_CLOSE_WAIT:
469 case SNAT_SESSION_TCP_LAST_ACK:
470 ses0->expire = now + sm->tcp_transitory_timeout;
472 case SNAT_SESSION_TCP_ESTABLISHED:
473 ses0->expire = now + sm->tcp_established_timeout;
478 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
479 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
481 nat_det_in2out_trace_t *t =
482 vlib_add_trace (vm, node, b0, sizeof (*t));
483 t->sw_if_index = sw_if_index0;
484 t->next_index = next0;
485 t->session_index = ~0;
487 t->session_index = ses0 - dm0->sessions;
490 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
492 ip1 = vlib_buffer_get_current (b1);
493 udp1 = ip4_next_header (ip1);
494 tcp1 = (tcp_header_t *) udp1;
496 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
498 if (PREDICT_FALSE (ip1->ttl == 1))
500 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
501 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
502 ICMP4_time_exceeded_ttl_exceeded_in_transit,
504 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
508 proto1 = ip_proto_to_snat_proto (ip1->protocol);
510 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
513 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
514 icmp1 = (icmp46_header_t *) udp1;
516 next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
517 rx_fib_index1, node, next1, thread_index,
522 dm1 = snat_det_map_by_user (sm, &ip1->src_address);
523 if (PREDICT_FALSE (!dm1))
525 nat_log_info ("no match for internal host %U",
526 format_ip4_address, &ip0->src_address);
527 next1 = NAT_DET_IN2OUT_NEXT_DROP;
528 b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
532 snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
534 key1.ext_host_addr = ip1->dst_address;
535 key1.ext_host_port = tcp1->dst;
538 snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
539 if (PREDICT_FALSE (!ses1))
541 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
543 key1.out_port = clib_host_to_net_u16 (lo_port1 +
550 if (snat_det_get_ses_by_out
551 (dm1, &ip1->src_address, key1.as_u64))
555 snat_det_ses_create (thread_index, dm1, &ip1->src_address,
559 if (PREDICT_FALSE (!ses1))
561 /* too many sessions for user, send ICMP error packet */
562 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
563 icmp4_error_set_vnet_buffer (b1,
564 ICMP4_destination_unreachable,
565 ICMP4_destination_unreachable_destination_unreachable_host,
567 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
572 old_port1 = udp1->src_port;
573 udp1->src_port = new_port1 = ses1->out.out_port;
575 old_addr1.as_u32 = ip1->src_address.as_u32;
576 ip1->src_address.as_u32 = new_addr1.as_u32;
577 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
579 sum1 = ip1->checksum;
580 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
582 src_address /* changed member */ );
583 ip1->checksum = ip_csum_fold (sum1);
585 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
587 if (tcp1->flags & TCP_FLAG_SYN)
588 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
589 else if (tcp1->flags & TCP_FLAG_ACK
590 && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
591 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
592 else if (tcp1->flags & TCP_FLAG_FIN
593 && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
594 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
595 else if (tcp1->flags & TCP_FLAG_ACK
596 && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
597 snat_det_ses_close (dm1, ses1);
598 else if (tcp1->flags & TCP_FLAG_FIN
599 && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
600 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
601 else if (tcp1->flags == 0
602 && ses1->state == SNAT_SESSION_UNKNOWN)
603 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
605 sum1 = tcp1->checksum;
606 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
608 dst_address /* changed member */ );
609 sum1 = ip_csum_update (sum1, old_port1, new_port1,
610 ip4_header_t /* cheat */ ,
611 length /* changed member */ );
612 mss_clamping (sm, tcp1, &sum1);
613 tcp1->checksum = ip_csum_fold (sum1);
617 ses1->state = SNAT_SESSION_UDP_ACTIVE;
619 if (PREDICT_FALSE (udp1->checksum))
621 sum1 = udp1->checksum;
623 ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
625 dst_address /* changed member */ );
627 ip_csum_update (sum1, old_port1, new_port1,
628 ip4_header_t /* cheat */ ,
629 length /* changed member */ );
630 udp1->checksum = ip_csum_fold (sum1);
636 case SNAT_SESSION_UDP_ACTIVE:
637 ses1->expire = now + sm->udp_timeout;
639 case SNAT_SESSION_TCP_SYN_SENT:
640 case SNAT_SESSION_TCP_FIN_WAIT:
641 case SNAT_SESSION_TCP_CLOSE_WAIT:
642 case SNAT_SESSION_TCP_LAST_ACK:
643 ses1->expire = now + sm->tcp_transitory_timeout;
645 case SNAT_SESSION_TCP_ESTABLISHED:
646 ses1->expire = now + sm->tcp_established_timeout;
651 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
652 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
654 nat_det_in2out_trace_t *t =
655 vlib_add_trace (vm, node, b1, sizeof (*t));
656 t->sw_if_index = sw_if_index1;
657 t->next_index = next1;
658 t->session_index = ~0;
660 t->session_index = ses1 - dm1->sessions;
663 pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
665 /* verify speculative enqueues, maybe switch current next frame */
666 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
667 to_next, n_left_to_next,
668 bi0, bi1, next0, next1);
671 while (n_left_from > 0 && n_left_to_next > 0)
679 ip4_address_t new_addr0, old_addr0;
680 u16 old_port0, new_port0, lo_port0, i0;
684 snat_det_out_key_t key0;
686 snat_det_session_t *ses0 = 0;
688 icmp46_header_t *icmp0;
690 /* speculatively enqueue b0 to the current next frame */
698 b0 = vlib_get_buffer (vm, bi0);
699 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
701 ip0 = vlib_buffer_get_current (b0);
702 udp0 = ip4_next_header (ip0);
703 tcp0 = (tcp_header_t *) udp0;
705 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
707 if (PREDICT_FALSE (ip0->ttl == 1))
709 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
710 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
711 ICMP4_time_exceeded_ttl_exceeded_in_transit,
713 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
717 proto0 = ip_proto_to_snat_proto (ip0->protocol);
719 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
722 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
723 icmp0 = (icmp46_header_t *) udp0;
725 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
726 rx_fib_index0, node, next0, thread_index,
731 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
732 if (PREDICT_FALSE (!dm0))
734 nat_log_info ("no match for internal host %U",
735 format_ip4_address, &ip0->src_address);
736 next0 = NAT_DET_IN2OUT_NEXT_DROP;
737 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
741 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
743 key0.ext_host_addr = ip0->dst_address;
744 key0.ext_host_port = tcp0->dst;
747 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
748 if (PREDICT_FALSE (!ses0))
750 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
752 key0.out_port = clib_host_to_net_u16 (lo_port0 +
759 if (snat_det_get_ses_by_out
760 (dm0, &ip0->src_address, key0.as_u64))
764 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
768 if (PREDICT_FALSE (!ses0))
770 /* too many sessions for user, send ICMP error packet */
771 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
772 icmp4_error_set_vnet_buffer (b0,
773 ICMP4_destination_unreachable,
774 ICMP4_destination_unreachable_destination_unreachable_host,
776 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
781 old_port0 = udp0->src_port;
782 udp0->src_port = new_port0 = ses0->out.out_port;
784 old_addr0.as_u32 = ip0->src_address.as_u32;
785 ip0->src_address.as_u32 = new_addr0.as_u32;
786 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
788 sum0 = ip0->checksum;
789 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
791 src_address /* changed member */ );
792 ip0->checksum = ip_csum_fold (sum0);
794 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
796 if (tcp0->flags & TCP_FLAG_SYN)
797 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
798 else if (tcp0->flags & TCP_FLAG_ACK
799 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
800 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
801 else if (tcp0->flags & TCP_FLAG_FIN
802 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
803 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
804 else if (tcp0->flags & TCP_FLAG_ACK
805 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
806 snat_det_ses_close (dm0, ses0);
807 else if (tcp0->flags & TCP_FLAG_FIN
808 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
809 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
810 else if (tcp0->flags == 0
811 && ses0->state == SNAT_SESSION_UNKNOWN)
812 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
814 sum0 = tcp0->checksum;
815 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
817 dst_address /* changed member */ );
818 sum0 = ip_csum_update (sum0, old_port0, new_port0,
819 ip4_header_t /* cheat */ ,
820 length /* changed member */ );
821 mss_clamping (sm, tcp0, &sum0);
822 tcp0->checksum = ip_csum_fold (sum0);
826 ses0->state = SNAT_SESSION_UDP_ACTIVE;
828 if (PREDICT_FALSE (udp0->checksum))
830 sum0 = udp0->checksum;
832 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
834 dst_address /* changed member */ );
836 ip_csum_update (sum0, old_port0, new_port0,
837 ip4_header_t /* cheat */ ,
838 length /* changed member */ );
839 udp0->checksum = ip_csum_fold (sum0);
845 case SNAT_SESSION_UDP_ACTIVE:
846 ses0->expire = now + sm->udp_timeout;
848 case SNAT_SESSION_TCP_SYN_SENT:
849 case SNAT_SESSION_TCP_FIN_WAIT:
850 case SNAT_SESSION_TCP_CLOSE_WAIT:
851 case SNAT_SESSION_TCP_LAST_ACK:
852 ses0->expire = now + sm->tcp_transitory_timeout;
854 case SNAT_SESSION_TCP_ESTABLISHED:
855 ses0->expire = now + sm->tcp_established_timeout;
860 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
861 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
863 nat_det_in2out_trace_t *t =
864 vlib_add_trace (vm, node, b0, sizeof (*t));
865 t->sw_if_index = sw_if_index0;
866 t->next_index = next0;
867 t->session_index = ~0;
869 t->session_index = ses0 - dm0->sessions;
872 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
874 /* verify speculative enqueue, maybe switch current next frame */
875 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
876 to_next, n_left_to_next,
880 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
883 vlib_node_increment_counter (vm, sm->det_in2out_node_index,
884 NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
886 return frame->n_vectors;
890 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
891 .name = "nat44-det-in2out",
892 .vector_size = sizeof (u32),
893 .format_trace = format_nat_det_in2out_trace,
894 .type = VLIB_NODE_TYPE_INTERNAL,
895 .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
896 .error_strings = nat_det_in2out_error_strings,
897 .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
898 /* edit / add dispositions here */
900 [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
901 [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
902 [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
908 * fd.io coding-style-patch-verification: ON
911 * eval: (c-set-style "gnu")