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>
29 #include <nat/lib/nat_inlines.h>
36 } nat_det_in2out_trace_t;
40 NAT_DET_IN2OUT_NEXT_LOOKUP,
41 NAT_DET_IN2OUT_NEXT_DROP,
42 NAT_DET_IN2OUT_NEXT_ICMP_ERROR,
43 NAT_DET_IN2OUT_N_NEXT,
44 } nat_det_in2out_next_t;
46 #define foreach_nat_det_in2out_error \
47 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
48 _(NO_TRANSLATION, "No translation") \
49 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
50 _(OUT_OF_PORTS, "Out of ports") \
51 _(IN2OUT_PACKETS, "Good in2out packets processed")
55 #define _(sym,str) NAT_DET_IN2OUT_ERROR_##sym,
56 foreach_nat_det_in2out_error
58 NAT_DET_IN2OUT_N_ERROR,
59 } nat_det_in2out_error_t;
61 static char *nat_det_in2out_error_strings[] = {
62 #define _(sym,string) string,
63 foreach_nat_det_in2out_error
68 format_nat_det_in2out_trace (u8 * s, va_list * args)
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72 nat_det_in2out_trace_t *t = va_arg (*args, nat_det_in2out_trace_t *);
74 s = format (s, "NAT_DET_IN2OUT: sw_if_index %d, next index %d, session %d",
75 t->sw_if_index, t->next_index, t->session_index);
80 #ifndef CLIB_MARCH_VARIANT
82 * Get address and port values to be used for ICMP packet translation
83 * and create session if needed
85 * @param[in,out] sm NAT main
86 * @param[in,out] node NAT node runtime
87 * @param[in] thread_index thread index
88 * @param[in,out] b0 buffer containing packet to be translated
89 * @param[in,out] ip0 ip header
90 * @param[out] p_proto protocol used for matching
91 * @param[out] p_value address and port after NAT translation
92 * @param[out] p_dont_translate if packet should not be translated
93 * @param d optional parameter
94 * @param e optional parameter
97 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
98 u32 thread_index, vlib_buffer_t * b0,
99 ip4_header_t * ip0, ip4_address_t * addr,
100 u16 * port, u32 * fib_index,
101 nat_protocol_t * proto, void *d, void *e,
104 vlib_main_t *vm = vlib_get_main ();
105 icmp46_header_t *icmp0;
108 nat_protocol_t protocol;
109 snat_det_out_key_t key0;
111 icmp_echo_header_t *echo0, *inner_echo0 = 0;
112 ip4_header_t *inner_ip0;
114 icmp46_header_t *inner_icmp0;
115 snat_det_map_t *dm0 = 0;
116 ip4_address_t new_addr0;
118 snat_det_session_t *ses0 = 0;
119 ip4_address_t in_addr;
123 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
124 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
125 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
126 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
128 if (!icmp_type_is_error_message
129 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
131 protocol = NAT_PROTOCOL_ICMP;
132 in_addr = ip0->src_address;
133 in_port = vnet_buffer (b0)->ip.reass.l4_src_port;
137 /* if error message, then it's not fragmented and we can access it */
138 inner_ip0 = (ip4_header_t *) (echo0 + 1);
139 l4_header = ip4_next_header (inner_ip0);
140 protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
141 in_addr = inner_ip0->dst_address;
144 case NAT_PROTOCOL_ICMP:
145 inner_icmp0 = (icmp46_header_t *) l4_header;
146 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
147 in_port = inner_echo0->identifier;
149 case NAT_PROTOCOL_UDP:
150 case NAT_PROTOCOL_TCP:
151 in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
154 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
155 next0 = NAT_DET_IN2OUT_NEXT_DROP;
160 dm0 = snat_det_map_by_user (sm, &in_addr);
161 if (PREDICT_FALSE (!dm0))
163 nat_log_info ("no match for internal host %U",
164 format_ip4_address, &in_addr);
165 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
172 next0 = NAT_DET_IN2OUT_NEXT_DROP;
173 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
177 snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
179 key0.ext_host_addr = ip0->dst_address;
180 key0.ext_host_port = 0;
182 ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
183 if (PREDICT_FALSE (!ses0))
185 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
192 if (icmp0->type != ICMP4_echo_request)
194 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
195 next0 = NAT_DET_IN2OUT_NEXT_DROP;
198 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
200 key0.out_port = clib_host_to_net_u16 (lo_port0 +
203 (echo0->identifier)) %
204 dm0->ports_per_host));
206 if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
210 snat_det_ses_create (thread_index, dm0,
211 &in_addr, echo0->identifier, &key0);
214 if (PREDICT_FALSE (!ses0))
216 next0 = NAT_DET_IN2OUT_NEXT_DROP;
217 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
223 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
224 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
225 reass.icmp_type_or_tcp_flags)))
227 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
228 next0 = NAT_DET_IN2OUT_NEXT_DROP;
232 u32 now = (u32) vlib_time_now (vm);
234 ses0->state = SNAT_SESSION_ICMP_ACTIVE;
235 ses0->expire = now + sm->icmp_timeout;
242 *fib_index = sm->outside_fib_index;
243 *port = ses0->out.out_port;
246 *(snat_det_session_t **) d = ses0;
248 *(snat_det_map_t **) e = dm0;
253 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
254 vlib_node_runtime_t * node,
255 vlib_frame_t * frame)
257 u32 n_left_from, *from, *to_next;
258 nat_det_in2out_next_t next_index;
259 u32 pkts_processed = 0;
260 snat_main_t *sm = &snat_main;
261 u32 now = (u32) vlib_time_now (vm);
262 u32 thread_index = vm->thread_index;
264 from = vlib_frame_vector_args (frame);
265 n_left_from = frame->n_vectors;
266 next_index = node->cached_next_index;
268 while (n_left_from > 0)
272 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
274 while (n_left_from >= 4 && n_left_to_next >= 2)
277 vlib_buffer_t *b0, *b1;
279 u32 sw_if_index0, sw_if_index1;
280 ip4_header_t *ip0, *ip1;
281 ip_csum_t sum0, sum1;
282 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
283 u16 old_port0, new_port0, lo_port0, i0;
284 u16 old_port1, new_port1, lo_port1, i1;
285 udp_header_t *udp0, *udp1;
286 tcp_header_t *tcp0, *tcp1;
288 snat_det_out_key_t key0, key1;
289 snat_det_map_t *dm0, *dm1;
290 snat_det_session_t *ses0 = 0, *ses1 = 0;
291 u32 rx_fib_index0, rx_fib_index1;
292 icmp46_header_t *icmp0, *icmp1;
294 /* Prefetch next iteration. */
296 vlib_buffer_t *p2, *p3;
298 p2 = vlib_get_buffer (vm, from[2]);
299 p3 = vlib_get_buffer (vm, from[3]);
301 vlib_prefetch_buffer_header (p2, LOAD);
302 vlib_prefetch_buffer_header (p3, LOAD);
304 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
305 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
308 /* speculatively enqueue b0 and b1 to the current next frame */
309 to_next[0] = bi0 = from[0];
310 to_next[1] = bi1 = from[1];
316 b0 = vlib_get_buffer (vm, bi0);
317 b1 = vlib_get_buffer (vm, bi1);
319 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
320 next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
322 ip0 = vlib_buffer_get_current (b0);
323 udp0 = ip4_next_header (ip0);
324 tcp0 = (tcp_header_t *) udp0;
326 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
328 if (PREDICT_FALSE (ip0->ttl == 1))
330 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
331 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
332 ICMP4_time_exceeded_ttl_exceeded_in_transit,
334 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
338 proto0 = ip_proto_to_nat_proto (ip0->protocol);
340 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
343 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
344 icmp0 = (icmp46_header_t *) udp0;
346 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
347 rx_fib_index0, node, next0, thread_index,
352 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
353 if (PREDICT_FALSE (!dm0))
355 nat_log_info ("no match for internal host %U",
356 format_ip4_address, &ip0->src_address);
357 next0 = NAT_DET_IN2OUT_NEXT_DROP;
358 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
362 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
364 key0.ext_host_addr = ip0->dst_address;
365 key0.ext_host_port = tcp0->dst;
368 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
369 if (PREDICT_FALSE (!ses0))
371 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
373 key0.out_port = clib_host_to_net_u16 (lo_port0 +
380 if (snat_det_get_ses_by_out
381 (dm0, &ip0->src_address, key0.as_u64))
385 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
389 if (PREDICT_FALSE (!ses0))
391 /* too many sessions for user, send ICMP error packet */
392 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
393 icmp4_error_set_vnet_buffer (b0,
394 ICMP4_destination_unreachable,
395 ICMP4_destination_unreachable_destination_unreachable_host,
397 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
402 old_port0 = udp0->src_port;
403 udp0->src_port = new_port0 = ses0->out.out_port;
405 old_addr0.as_u32 = ip0->src_address.as_u32;
406 ip0->src_address.as_u32 = new_addr0.as_u32;
407 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
409 sum0 = ip0->checksum;
410 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
412 src_address /* changed member */ );
413 ip0->checksum = ip_csum_fold (sum0);
415 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
417 if (tcp0->flags & TCP_FLAG_SYN)
418 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
419 else if (tcp0->flags & TCP_FLAG_ACK
420 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
421 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
422 else if (tcp0->flags & TCP_FLAG_FIN
423 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
424 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
425 else if (tcp0->flags & TCP_FLAG_ACK
426 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
427 snat_det_ses_close (dm0, ses0);
428 else if (tcp0->flags & TCP_FLAG_FIN
429 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
430 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
431 else if (tcp0->flags == 0
432 && ses0->state == SNAT_SESSION_UNKNOWN)
433 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
435 sum0 = tcp0->checksum;
436 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
438 dst_address /* changed member */ );
439 sum0 = ip_csum_update (sum0, old_port0, new_port0,
440 ip4_header_t /* cheat */ ,
441 length /* changed member */ );
442 mss_clamping (sm->mss_clamping, tcp0, &sum0);
443 tcp0->checksum = ip_csum_fold (sum0);
447 ses0->state = SNAT_SESSION_UDP_ACTIVE;
449 if (PREDICT_FALSE (udp0->checksum))
451 sum0 = udp0->checksum;
453 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
455 dst_address /* changed member */ );
457 ip_csum_update (sum0, old_port0, new_port0,
458 ip4_header_t /* cheat */ ,
459 length /* changed member */ );
460 udp0->checksum = ip_csum_fold (sum0);
466 case SNAT_SESSION_UDP_ACTIVE:
467 ses0->expire = now + sm->udp_timeout;
469 case SNAT_SESSION_TCP_SYN_SENT:
470 case SNAT_SESSION_TCP_FIN_WAIT:
471 case SNAT_SESSION_TCP_CLOSE_WAIT:
472 case SNAT_SESSION_TCP_LAST_ACK:
473 ses0->expire = now + sm->tcp_transitory_timeout;
475 case SNAT_SESSION_TCP_ESTABLISHED:
476 ses0->expire = now + sm->tcp_established_timeout;
481 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
482 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
484 nat_det_in2out_trace_t *t =
485 vlib_add_trace (vm, node, b0, sizeof (*t));
486 t->sw_if_index = sw_if_index0;
487 t->next_index = next0;
488 t->session_index = ~0;
490 t->session_index = ses0 - dm0->sessions;
493 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
495 ip1 = vlib_buffer_get_current (b1);
496 udp1 = ip4_next_header (ip1);
497 tcp1 = (tcp_header_t *) udp1;
499 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
501 if (PREDICT_FALSE (ip1->ttl == 1))
503 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
504 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
505 ICMP4_time_exceeded_ttl_exceeded_in_transit,
507 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
511 proto1 = ip_proto_to_nat_proto (ip1->protocol);
513 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
516 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
517 icmp1 = (icmp46_header_t *) udp1;
519 next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
520 rx_fib_index1, node, next1, thread_index,
525 dm1 = snat_det_map_by_user (sm, &ip1->src_address);
526 if (PREDICT_FALSE (!dm1))
528 nat_log_info ("no match for internal host %U",
529 format_ip4_address, &ip0->src_address);
530 next1 = NAT_DET_IN2OUT_NEXT_DROP;
531 b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
535 snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
537 key1.ext_host_addr = ip1->dst_address;
538 key1.ext_host_port = tcp1->dst;
541 snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
542 if (PREDICT_FALSE (!ses1))
544 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
546 key1.out_port = clib_host_to_net_u16 (lo_port1 +
553 if (snat_det_get_ses_by_out
554 (dm1, &ip1->src_address, key1.as_u64))
558 snat_det_ses_create (thread_index, dm1, &ip1->src_address,
562 if (PREDICT_FALSE (!ses1))
564 /* too many sessions for user, send ICMP error packet */
565 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
566 icmp4_error_set_vnet_buffer (b1,
567 ICMP4_destination_unreachable,
568 ICMP4_destination_unreachable_destination_unreachable_host,
570 next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
575 old_port1 = udp1->src_port;
576 udp1->src_port = new_port1 = ses1->out.out_port;
578 old_addr1.as_u32 = ip1->src_address.as_u32;
579 ip1->src_address.as_u32 = new_addr1.as_u32;
580 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
582 sum1 = ip1->checksum;
583 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
585 src_address /* changed member */ );
586 ip1->checksum = ip_csum_fold (sum1);
588 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
590 if (tcp1->flags & TCP_FLAG_SYN)
591 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
592 else if (tcp1->flags & TCP_FLAG_ACK
593 && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
594 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
595 else if (tcp1->flags & TCP_FLAG_FIN
596 && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
597 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
598 else if (tcp1->flags & TCP_FLAG_ACK
599 && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
600 snat_det_ses_close (dm1, ses1);
601 else if (tcp1->flags & TCP_FLAG_FIN
602 && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
603 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
604 else if (tcp1->flags == 0
605 && ses1->state == SNAT_SESSION_UNKNOWN)
606 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
608 sum1 = tcp1->checksum;
609 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
611 dst_address /* changed member */ );
612 sum1 = ip_csum_update (sum1, old_port1, new_port1,
613 ip4_header_t /* cheat */ ,
614 length /* changed member */ );
615 mss_clamping (sm->mss_clamping, tcp1, &sum1);
616 tcp1->checksum = ip_csum_fold (sum1);
620 ses1->state = SNAT_SESSION_UDP_ACTIVE;
622 if (PREDICT_FALSE (udp1->checksum))
624 sum1 = udp1->checksum;
626 ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
628 dst_address /* changed member */ );
630 ip_csum_update (sum1, old_port1, new_port1,
631 ip4_header_t /* cheat */ ,
632 length /* changed member */ );
633 udp1->checksum = ip_csum_fold (sum1);
639 case SNAT_SESSION_UDP_ACTIVE:
640 ses1->expire = now + sm->udp_timeout;
642 case SNAT_SESSION_TCP_SYN_SENT:
643 case SNAT_SESSION_TCP_FIN_WAIT:
644 case SNAT_SESSION_TCP_CLOSE_WAIT:
645 case SNAT_SESSION_TCP_LAST_ACK:
646 ses1->expire = now + sm->tcp_transitory_timeout;
648 case SNAT_SESSION_TCP_ESTABLISHED:
649 ses1->expire = now + sm->tcp_established_timeout;
654 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
655 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
657 nat_det_in2out_trace_t *t =
658 vlib_add_trace (vm, node, b1, sizeof (*t));
659 t->sw_if_index = sw_if_index1;
660 t->next_index = next1;
661 t->session_index = ~0;
663 t->session_index = ses1 - dm1->sessions;
666 pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
668 /* verify speculative enqueues, maybe switch current next frame */
669 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
670 to_next, n_left_to_next,
671 bi0, bi1, next0, next1);
674 while (n_left_from > 0 && n_left_to_next > 0)
682 ip4_address_t new_addr0, old_addr0;
683 u16 old_port0, new_port0, lo_port0, i0;
687 snat_det_out_key_t key0;
689 snat_det_session_t *ses0 = 0;
691 icmp46_header_t *icmp0;
693 /* speculatively enqueue b0 to the current next frame */
701 b0 = vlib_get_buffer (vm, bi0);
702 next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
704 ip0 = vlib_buffer_get_current (b0);
705 udp0 = ip4_next_header (ip0);
706 tcp0 = (tcp_header_t *) udp0;
708 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
710 if (PREDICT_FALSE (ip0->ttl == 1))
712 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
713 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
714 ICMP4_time_exceeded_ttl_exceeded_in_transit,
716 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
720 proto0 = ip_proto_to_nat_proto (ip0->protocol);
722 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
725 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
726 icmp0 = (icmp46_header_t *) udp0;
728 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
729 rx_fib_index0, node, next0, thread_index,
734 dm0 = snat_det_map_by_user (sm, &ip0->src_address);
735 if (PREDICT_FALSE (!dm0))
737 nat_log_info ("no match for internal host %U",
738 format_ip4_address, &ip0->src_address);
739 next0 = NAT_DET_IN2OUT_NEXT_DROP;
740 b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
744 snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
746 key0.ext_host_addr = ip0->dst_address;
747 key0.ext_host_port = tcp0->dst;
750 snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
751 if (PREDICT_FALSE (!ses0))
753 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
755 key0.out_port = clib_host_to_net_u16 (lo_port0 +
762 if (snat_det_get_ses_by_out
763 (dm0, &ip0->src_address, key0.as_u64))
767 snat_det_ses_create (thread_index, dm0, &ip0->src_address,
771 if (PREDICT_FALSE (!ses0))
773 /* too many sessions for user, send ICMP error packet */
774 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
775 icmp4_error_set_vnet_buffer (b0,
776 ICMP4_destination_unreachable,
777 ICMP4_destination_unreachable_destination_unreachable_host,
779 next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
784 old_port0 = udp0->src_port;
785 udp0->src_port = new_port0 = ses0->out.out_port;
787 old_addr0.as_u32 = ip0->src_address.as_u32;
788 ip0->src_address.as_u32 = new_addr0.as_u32;
789 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
791 sum0 = ip0->checksum;
792 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
794 src_address /* changed member */ );
795 ip0->checksum = ip_csum_fold (sum0);
797 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
799 if (tcp0->flags & TCP_FLAG_SYN)
800 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
801 else if (tcp0->flags & TCP_FLAG_ACK
802 && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
803 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
804 else if (tcp0->flags & TCP_FLAG_FIN
805 && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
806 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
807 else if (tcp0->flags & TCP_FLAG_ACK
808 && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
809 snat_det_ses_close (dm0, ses0);
810 else if (tcp0->flags & TCP_FLAG_FIN
811 && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
812 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
813 else if (tcp0->flags == 0
814 && ses0->state == SNAT_SESSION_UNKNOWN)
815 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
817 sum0 = tcp0->checksum;
818 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
820 dst_address /* changed member */ );
821 sum0 = ip_csum_update (sum0, old_port0, new_port0,
822 ip4_header_t /* cheat */ ,
823 length /* changed member */ );
824 mss_clamping (sm->mss_clamping, tcp0, &sum0);
825 tcp0->checksum = ip_csum_fold (sum0);
829 ses0->state = SNAT_SESSION_UDP_ACTIVE;
831 if (PREDICT_FALSE (udp0->checksum))
833 sum0 = udp0->checksum;
835 ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
837 dst_address /* changed member */ );
839 ip_csum_update (sum0, old_port0, new_port0,
840 ip4_header_t /* cheat */ ,
841 length /* changed member */ );
842 udp0->checksum = ip_csum_fold (sum0);
848 case SNAT_SESSION_UDP_ACTIVE:
849 ses0->expire = now + sm->udp_timeout;
851 case SNAT_SESSION_TCP_SYN_SENT:
852 case SNAT_SESSION_TCP_FIN_WAIT:
853 case SNAT_SESSION_TCP_CLOSE_WAIT:
854 case SNAT_SESSION_TCP_LAST_ACK:
855 ses0->expire = now + sm->tcp_transitory_timeout;
857 case SNAT_SESSION_TCP_ESTABLISHED:
858 ses0->expire = now + sm->tcp_established_timeout;
863 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
864 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
866 nat_det_in2out_trace_t *t =
867 vlib_add_trace (vm, node, b0, sizeof (*t));
868 t->sw_if_index = sw_if_index0;
869 t->next_index = next0;
870 t->session_index = ~0;
872 t->session_index = ses0 - dm0->sessions;
875 pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
877 /* verify speculative enqueue, maybe switch current next frame */
878 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
879 to_next, n_left_to_next,
883 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
886 vlib_node_increment_counter (vm, sm->det_in2out_node_index,
887 NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
889 return frame->n_vectors;
893 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
894 .name = "nat44-det-in2out",
895 .vector_size = sizeof (u32),
896 .format_trace = format_nat_det_in2out_trace,
897 .type = VLIB_NODE_TYPE_INTERNAL,
898 .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
899 .error_strings = nat_det_in2out_error_strings,
900 .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
901 /* edit / add dispositions here */
903 [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
904 [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
905 [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
911 * fd.io coding-style-patch-verification: ON
914 * eval: (c-set-style "gnu")