2 * Copyright (c) 2020 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.
18 * @brief Deterministic NAT (CGN) outside to inside translation
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
28 #include <nat/det44/det44.h>
29 #include <nat/det44/det44_inlines.h>
31 #include <nat/lib/lib.h>
32 #include <nat/lib/inlines.h>
33 #include <nat/lib/nat_inlines.h>
37 DET44_OUT2IN_NEXT_DROP,
38 DET44_OUT2IN_NEXT_LOOKUP,
39 DET44_OUT2IN_NEXT_ICMP_ERROR,
41 } det44_out2in_next_t;
48 } det44_out2in_trace_t;
50 #define foreach_det44_out2in_error \
51 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
52 _(NO_TRANSLATION, "No translation") \
53 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
54 _(OUT2IN_PACKETS, "Good out2in packets processed")
58 #define _(sym,str) DET44_OUT2IN_ERROR_##sym,
59 foreach_det44_out2in_error
62 } det44_out2in_error_t;
64 static char *det44_out2in_error_strings[] = {
65 #define _(sym,string) string,
66 foreach_det44_out2in_error
71 format_det44_out2in_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75 det44_out2in_trace_t *t = va_arg (*args, det44_out2in_trace_t *);
79 "DET44_OUT2IN: sw_if_index %d, next index %d, session index %d",
80 t->sw_if_index, t->next_index, t->session_index);
84 #ifndef CLIB_MARCH_VARIANT
86 * Get address and port values to be used for ICMP packet translation
87 * and create session if needed
89 * @param[in,out] node NAT node runtime
90 * @param[in] thread_index thread index
91 * @param[in,out] b0 buffer containing packet to be translated
92 * @param[in,out] ip0 ip header
93 * @param[out] p_proto protocol used for matching
94 * @param[out] p_value address and port after NAT translation
95 * @param[out] p_dont_translate if packet should not be translated
96 * @param d optional parameter
97 * @param e optional parameter
100 icmp_match_out2in_det (vlib_node_runtime_t * node,
101 u32 thread_index, vlib_buffer_t * b0,
102 ip4_header_t * ip0, ip4_address_t * addr,
103 u16 * port, u32 * fib_index,
104 nat_protocol_t * proto, void *d, void *e,
107 det44_main_t *dm = &det44_main;
108 icmp46_header_t *icmp0;
111 snat_det_out_key_t key0;
113 icmp_echo_header_t *echo0, *inner_echo0 = 0;
114 ip4_header_t *inner_ip0;
116 icmp46_header_t *inner_icmp0;
117 snat_det_map_t *mp0 = 0;
118 ip4_address_t new_addr0 = { {0} };
119 snat_det_session_t *ses0 = 0;
120 ip4_address_t out_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];
127 if (!icmp_type_is_error_message
128 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
130 protocol = NAT_PROTOCOL_ICMP;
131 key0.ext_host_addr = ip0->src_address;
132 key0.ext_host_port = 0;
133 key0.out_port = vnet_buffer (b0)->ip.reass.l4_src_port;
134 out_addr = ip0->dst_address;
138 /* if error message, then it's not fragmented and we can access it */
139 inner_ip0 = (ip4_header_t *) (echo0 + 1);
140 l4_header = ip4_next_header (inner_ip0);
141 protocol = ip_proto_to_nat_proto (inner_ip0->protocol);
142 key0.ext_host_addr = inner_ip0->dst_address;
143 out_addr = inner_ip0->src_address;
146 case NAT_PROTOCOL_ICMP:
147 inner_icmp0 = (icmp46_header_t *) l4_header;
148 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
149 key0.ext_host_port = 0;
150 key0.out_port = inner_echo0->identifier;
152 case NAT_PROTOCOL_UDP:
153 case NAT_PROTOCOL_TCP:
154 key0.ext_host_port = ((tcp_udp_header_t *) l4_header)->dst_port;
155 key0.out_port = ((tcp_udp_header_t *) l4_header)->src_port;
158 b0->error = node->errors[DET44_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
159 next0 = DET44_OUT2IN_NEXT_DROP;
164 mp0 = snat_det_map_by_out (&out_addr);
165 if (PREDICT_FALSE (!mp0))
167 /* Don't NAT packet aimed at the intfc address */
168 if (PREDICT_FALSE (!det44_is_interface_addr (node, sw_if_index0,
169 ip0->dst_address.as_u32)))
174 det44_log_info ("unknown dst address: %U",
175 format_ip4_address, &ip0->dst_address);
179 snat_det_reverse (mp0, &ip0->dst_address,
180 clib_net_to_host_u16 (key0.out_port), &new_addr0);
182 ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
183 if (PREDICT_FALSE (!ses0))
185 /* Don't NAT packet aimed at the intfc address */
186 if (PREDICT_FALSE (!det44_is_interface_addr (node, sw_if_index0,
187 ip0->dst_address.as_u32)))
192 det44_log_info ("no match src %U:%d dst %U:%d for user %U",
193 format_ip4_address, &key0.ext_host_addr,
194 clib_net_to_host_u16 (key0.ext_host_port),
195 format_ip4_address, &out_addr,
196 clib_net_to_host_u16 (key0.out_port),
197 format_ip4_address, &new_addr0);
198 b0->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
199 next0 = DET44_OUT2IN_NEXT_DROP;
204 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_reply
205 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
206 reass.icmp_type_or_tcp_flags)))
208 b0->error = node->errors[DET44_OUT2IN_ERROR_BAD_ICMP_TYPE];
209 next0 = DET44_OUT2IN_NEXT_DROP;
220 *fib_index = dm->inside_fib_index;
221 *port = ses0->in_port;
224 *(snat_det_session_t **) d = ses0;
226 *(snat_det_map_t **) e = mp0;
231 #ifndef CLIB_MARCH_VARIANT
233 det44_icmp_out2in (vlib_buffer_t * b0,
235 icmp46_header_t * icmp0,
238 vlib_node_runtime_t * node,
239 u32 next0, u32 thread_index, void *d, void *e)
241 vlib_main_t *vm = vlib_get_main ();
242 u32 new_addr0, old_addr0, next0_tmp, fib_index;
243 u16 old_id0, new_id0, port, checksum0;
244 icmp_echo_header_t *echo0, *inner_echo0;
245 icmp46_header_t *inner_icmp0;
246 ip4_header_t *inner_ip0;
251 nat_protocol_t proto;
253 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
254 next0_tmp = icmp_match_out2in_det (node, thread_index, b0, ip0,
255 &addr, &port, &fib_index, &proto,
256 d, e, &dont_translate);
259 if (next0 == DET44_OUT2IN_NEXT_DROP || dont_translate)
262 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
265 ip_incremental_checksum_buffer (vm, b0,
267 (u8 *) vlib_buffer_get_current (b0),
268 ntohs (ip0->length) -
269 ip4_header_bytes (ip0), 0);
270 checksum0 = ~ip_csum_fold (sum0);
271 if (checksum0 != 0 && checksum0 != 0xffff)
273 next0 = DET44_OUT2IN_NEXT_DROP;
278 old_addr0 = ip0->dst_address.as_u32;
279 new_addr0 = ip0->dst_address.as_u32 = addr.as_u32;
280 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
282 sum0 = ip0->checksum;
283 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
284 dst_address /* changed member */ );
285 ip0->checksum = ip_csum_fold (sum0);
288 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
290 if (icmp0->checksum == 0)
291 icmp0->checksum = 0xffff;
293 if (!icmp_type_is_error_message (icmp0->type))
296 if (PREDICT_FALSE (new_id0 != echo0->identifier))
298 old_id0 = echo0->identifier;
300 echo0->identifier = new_id0;
302 sum0 = icmp0->checksum;
304 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
305 identifier /* changed member */ );
306 icmp0->checksum = ip_csum_fold (sum0);
311 inner_ip0 = (ip4_header_t *) (echo0 + 1);
312 l4_header = ip4_next_header (inner_ip0);
314 if (!ip4_header_checksum_is_valid (inner_ip0))
316 next0 = DET44_OUT2IN_NEXT_DROP;
320 old_addr0 = inner_ip0->src_address.as_u32;
321 inner_ip0->src_address = addr;
322 new_addr0 = inner_ip0->src_address.as_u32;
324 sum0 = icmp0->checksum;
325 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
326 src_address /* changed member */ );
327 icmp0->checksum = ip_csum_fold (sum0);
331 case NAT_PROTOCOL_ICMP:
332 inner_icmp0 = (icmp46_header_t *) l4_header;
333 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
335 old_id0 = inner_echo0->identifier;
337 inner_echo0->identifier = new_id0;
339 sum0 = icmp0->checksum;
341 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
343 icmp0->checksum = ip_csum_fold (sum0);
345 case NAT_PROTOCOL_UDP:
346 case NAT_PROTOCOL_TCP:
347 old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
349 ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
351 sum0 = icmp0->checksum;
352 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
354 icmp0->checksum = ip_csum_fold (sum0);
367 VLIB_NODE_FN (det44_out2in_node) (vlib_main_t * vm,
368 vlib_node_runtime_t * node,
369 vlib_frame_t * frame)
371 u32 n_left_from, *from, *to_next;
372 det44_out2in_next_t next_index;
373 u32 pkts_processed = 0;
374 det44_main_t *dm = &det44_main;
375 u32 thread_index = vm->thread_index;
377 from = vlib_frame_vector_args (frame);
378 n_left_from = frame->n_vectors;
379 next_index = node->cached_next_index;
381 while (n_left_from > 0)
385 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
387 while (n_left_from >= 4 && n_left_to_next >= 2)
390 vlib_buffer_t *b0, *b1;
391 u32 next0 = DET44_OUT2IN_NEXT_LOOKUP;
392 u32 next1 = DET44_OUT2IN_NEXT_LOOKUP;
393 u32 sw_if_index0, sw_if_index1;
394 ip4_header_t *ip0, *ip1;
395 ip_csum_t sum0, sum1;
396 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
397 u16 new_port0, old_port0, old_port1, new_port1;
398 udp_header_t *udp0, *udp1;
399 tcp_header_t *tcp0, *tcp1;
401 snat_det_out_key_t key0, key1;
402 snat_det_map_t *mp0, *mp1;
403 snat_det_session_t *ses0 = 0, *ses1 = 0;
404 u32 rx_fib_index0, rx_fib_index1;
405 icmp46_header_t *icmp0, *icmp1;
407 /* Prefetch next iteration. */
409 vlib_buffer_t *p2, *p3;
411 p2 = vlib_get_buffer (vm, from[2]);
412 p3 = vlib_get_buffer (vm, from[3]);
414 vlib_prefetch_buffer_header (p2, LOAD);
415 vlib_prefetch_buffer_header (p3, LOAD);
417 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
418 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
421 /* speculatively enqueue b0 and b1 to the current next frame */
422 to_next[0] = bi0 = from[0];
423 to_next[1] = bi1 = from[1];
429 b0 = vlib_get_buffer (vm, bi0);
430 b1 = vlib_get_buffer (vm, bi1);
432 ip0 = vlib_buffer_get_current (b0);
433 udp0 = ip4_next_header (ip0);
434 tcp0 = (tcp_header_t *) udp0;
436 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
438 if (PREDICT_FALSE (ip0->ttl == 1))
440 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
441 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
442 ICMP4_time_exceeded_ttl_exceeded_in_transit,
444 next0 = DET44_OUT2IN_NEXT_ICMP_ERROR;
448 proto0 = ip_proto_to_nat_proto (ip0->protocol);
450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
453 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
454 icmp0 = (icmp46_header_t *) udp0;
456 next0 = det44_icmp_out2in (b0, ip0, icmp0, sw_if_index0,
457 rx_fib_index0, node, next0,
458 thread_index, &ses0, &mp0);
462 key0.ext_host_addr = ip0->src_address;
463 key0.ext_host_port = tcp0->src;
464 key0.out_port = tcp0->dst;
466 mp0 = snat_det_map_by_out (&ip0->dst_address);
467 if (PREDICT_FALSE (!mp0))
469 det44_log_info ("unknown dst address: %U",
470 format_ip4_address, &ip0->dst_address);
471 next0 = DET44_OUT2IN_NEXT_DROP;
472 b0->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
476 snat_det_reverse (mp0, &ip0->dst_address,
477 clib_net_to_host_u16 (tcp0->dst), &new_addr0);
479 ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
480 if (PREDICT_FALSE (!ses0))
482 det44_log_info ("no match src %U:%d dst %U:%d for user %U",
483 format_ip4_address, &ip0->src_address,
484 clib_net_to_host_u16 (tcp0->src),
485 format_ip4_address, &ip0->dst_address,
486 clib_net_to_host_u16 (tcp0->dst),
487 format_ip4_address, &new_addr0);
488 next0 = DET44_OUT2IN_NEXT_DROP;
489 b0->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
492 old_port0 = udp0->dst_port;
493 udp0->dst_port = new_port0 = ses0->in_port;
495 old_addr0 = ip0->dst_address;
496 ip0->dst_address = new_addr0;
497 vnet_buffer (b0)->sw_if_index[VLIB_TX] = dm->inside_fib_index;
499 sum0 = ip0->checksum;
500 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
502 dst_address /* changed member */ );
503 ip0->checksum = ip_csum_fold (sum0);
505 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
507 if (tcp0->flags & TCP_FLAG_FIN
508 && ses0->state == DET44_SESSION_TCP_ESTABLISHED)
509 ses0->state = DET44_SESSION_TCP_CLOSE_WAIT;
510 else if (tcp0->flags & TCP_FLAG_ACK
511 && ses0->state == DET44_SESSION_TCP_LAST_ACK)
512 snat_det_ses_close (mp0, ses0);
514 sum0 = tcp0->checksum;
515 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
517 dst_address /* changed member */ );
518 sum0 = ip_csum_update (sum0, old_port0, new_port0,
519 ip4_header_t /* cheat */ ,
520 length /* changed member */ );
521 tcp0->checksum = ip_csum_fold (sum0);
523 else if (udp0->checksum)
525 sum0 = udp0->checksum;
526 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
528 dst_address /* changed member */ );
529 sum0 = ip_csum_update (sum0, old_port0, new_port0,
530 ip4_header_t /* cheat */ ,
531 length /* changed member */ );
532 udp0->checksum = ip_csum_fold (sum0);
537 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
538 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
540 det44_out2in_trace_t *t =
541 vlib_add_trace (vm, node, b0, sizeof (*t));
542 t->sw_if_index = sw_if_index0;
543 t->next_index = next0;
544 t->session_index = ~0;
546 t->session_index = ses0 - mp0->sessions;
549 pkts_processed += next0 != DET44_OUT2IN_NEXT_DROP;
551 b1 = vlib_get_buffer (vm, bi1);
553 ip1 = vlib_buffer_get_current (b1);
554 udp1 = ip4_next_header (ip1);
555 tcp1 = (tcp_header_t *) udp1;
557 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
559 if (PREDICT_FALSE (ip1->ttl == 1))
561 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
562 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
563 ICMP4_time_exceeded_ttl_exceeded_in_transit,
565 next1 = DET44_OUT2IN_NEXT_ICMP_ERROR;
569 proto1 = ip_proto_to_nat_proto (ip1->protocol);
571 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
574 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
575 icmp1 = (icmp46_header_t *) udp1;
577 next1 = det44_icmp_out2in (b1, ip1, icmp1, sw_if_index1,
578 rx_fib_index1, node, next1,
579 thread_index, &ses1, &mp1);
583 key1.ext_host_addr = ip1->src_address;
584 key1.ext_host_port = tcp1->src;
585 key1.out_port = tcp1->dst;
587 mp1 = snat_det_map_by_out (&ip1->dst_address);
588 if (PREDICT_FALSE (!mp1))
590 det44_log_info ("unknown dst address: %U",
591 format_ip4_address, &ip1->dst_address);
592 next1 = DET44_OUT2IN_NEXT_DROP;
593 b1->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
597 snat_det_reverse (mp1, &ip1->dst_address,
598 clib_net_to_host_u16 (tcp1->dst), &new_addr1);
600 ses1 = snat_det_get_ses_by_out (mp1, &new_addr1, key1.as_u64);
601 if (PREDICT_FALSE (!ses1))
603 det44_log_info ("no match src %U:%d dst %U:%d for user %U",
604 format_ip4_address, &ip1->src_address,
605 clib_net_to_host_u16 (tcp1->src),
606 format_ip4_address, &ip1->dst_address,
607 clib_net_to_host_u16 (tcp1->dst),
608 format_ip4_address, &new_addr1);
609 next1 = DET44_OUT2IN_NEXT_DROP;
610 b1->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
613 old_port1 = udp1->dst_port;
614 udp1->dst_port = new_port1 = ses1->in_port;
616 old_addr1 = ip1->dst_address;
617 ip1->dst_address = new_addr1;
618 vnet_buffer (b1)->sw_if_index[VLIB_TX] = dm->inside_fib_index;
620 sum1 = ip1->checksum;
621 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
623 dst_address /* changed member */ );
624 ip1->checksum = ip_csum_fold (sum1);
626 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
628 if (tcp1->flags & TCP_FLAG_FIN
629 && ses1->state == DET44_SESSION_TCP_ESTABLISHED)
630 ses1->state = DET44_SESSION_TCP_CLOSE_WAIT;
631 else if (tcp1->flags & TCP_FLAG_ACK
632 && ses1->state == DET44_SESSION_TCP_LAST_ACK)
633 snat_det_ses_close (mp1, ses1);
635 sum1 = tcp1->checksum;
636 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
638 dst_address /* changed member */ );
639 sum1 = ip_csum_update (sum1, old_port1, new_port1,
640 ip4_header_t /* cheat */ ,
641 length /* changed member */ );
642 tcp1->checksum = ip_csum_fold (sum1);
644 else if (udp1->checksum)
646 sum1 = udp1->checksum;
647 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
649 dst_address /* changed member */ );
650 sum1 = ip_csum_update (sum1, old_port1, new_port1,
651 ip4_header_t /* cheat */ ,
652 length /* changed member */ );
653 udp1->checksum = ip_csum_fold (sum1);
658 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
659 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
661 det44_out2in_trace_t *t =
662 vlib_add_trace (vm, node, b1, sizeof (*t));
663 t->sw_if_index = sw_if_index1;
664 t->next_index = next1;
665 t->session_index = ~0;
667 t->session_index = ses1 - mp1->sessions;
670 pkts_processed += next1 != DET44_OUT2IN_NEXT_DROP;
672 /* verify speculative enqueues, maybe switch current next frame */
673 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
674 to_next, n_left_to_next,
675 bi0, bi1, next0, next1);
678 while (n_left_from > 0 && n_left_to_next > 0)
682 u32 next0 = DET44_OUT2IN_NEXT_LOOKUP;
686 ip4_address_t new_addr0, old_addr0;
687 u16 new_port0, old_port0;
691 snat_det_out_key_t key0;
693 snat_det_session_t *ses0 = 0;
695 icmp46_header_t *icmp0;
697 /* speculatively enqueue b0 to the current next frame */
705 b0 = vlib_get_buffer (vm, bi0);
707 ip0 = vlib_buffer_get_current (b0);
708 udp0 = ip4_next_header (ip0);
709 tcp0 = (tcp_header_t *) udp0;
711 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
713 if (PREDICT_FALSE (ip0->ttl == 1))
715 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
716 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
717 ICMP4_time_exceeded_ttl_exceeded_in_transit,
719 next0 = DET44_OUT2IN_NEXT_ICMP_ERROR;
723 proto0 = ip_proto_to_nat_proto (ip0->protocol);
725 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
728 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
729 icmp0 = (icmp46_header_t *) udp0;
731 next0 = det44_icmp_out2in (b0, ip0, icmp0, sw_if_index0,
732 rx_fib_index0, node, next0,
733 thread_index, &ses0, &mp0);
737 key0.ext_host_addr = ip0->src_address;
738 key0.ext_host_port = tcp0->src;
739 key0.out_port = tcp0->dst;
741 mp0 = snat_det_map_by_out (&ip0->dst_address);
742 if (PREDICT_FALSE (!mp0))
744 det44_log_info ("unknown dst address: %U",
745 format_ip4_address, &ip0->dst_address);
746 next0 = DET44_OUT2IN_NEXT_DROP;
747 b0->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
751 snat_det_reverse (mp0, &ip0->dst_address,
752 clib_net_to_host_u16 (tcp0->dst), &new_addr0);
754 ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
755 if (PREDICT_FALSE (!ses0))
757 det44_log_info ("no match src %U:%d dst %U:%d for user %U",
758 format_ip4_address, &ip0->src_address,
759 clib_net_to_host_u16 (tcp0->src),
760 format_ip4_address, &ip0->dst_address,
761 clib_net_to_host_u16 (tcp0->dst),
762 format_ip4_address, &new_addr0);
763 next0 = DET44_OUT2IN_NEXT_DROP;
764 b0->error = node->errors[DET44_OUT2IN_ERROR_NO_TRANSLATION];
767 old_port0 = udp0->dst_port;
768 udp0->dst_port = new_port0 = ses0->in_port;
770 old_addr0 = ip0->dst_address;
771 ip0->dst_address = new_addr0;
772 vnet_buffer (b0)->sw_if_index[VLIB_TX] = dm->inside_fib_index;
774 sum0 = ip0->checksum;
775 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
777 dst_address /* changed member */ );
778 ip0->checksum = ip_csum_fold (sum0);
780 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
782 if (tcp0->flags & TCP_FLAG_FIN
783 && ses0->state == DET44_SESSION_TCP_ESTABLISHED)
784 ses0->state = DET44_SESSION_TCP_CLOSE_WAIT;
785 else if (tcp0->flags & TCP_FLAG_ACK
786 && ses0->state == DET44_SESSION_TCP_LAST_ACK)
787 snat_det_ses_close (mp0, ses0);
789 sum0 = tcp0->checksum;
790 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
792 dst_address /* changed member */ );
793 sum0 = ip_csum_update (sum0, old_port0, new_port0,
794 ip4_header_t /* cheat */ ,
795 length /* changed member */ );
796 tcp0->checksum = ip_csum_fold (sum0);
798 else if (udp0->checksum)
800 sum0 = udp0->checksum;
801 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
803 dst_address /* changed member */ );
804 sum0 = ip_csum_update (sum0, old_port0, new_port0,
805 ip4_header_t /* cheat */ ,
806 length /* changed member */ );
807 udp0->checksum = ip_csum_fold (sum0);
812 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
813 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
815 det44_out2in_trace_t *t =
816 vlib_add_trace (vm, node, b0, sizeof (*t));
817 t->sw_if_index = sw_if_index0;
818 t->next_index = next0;
819 t->session_index = ~0;
821 t->session_index = ses0 - mp0->sessions;
824 pkts_processed += next0 != DET44_OUT2IN_NEXT_DROP;
826 /* verify speculative enqueue, maybe switch current next frame */
827 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
828 to_next, n_left_to_next,
832 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
835 vlib_node_increment_counter (vm, dm->out2in_node_index,
836 DET44_OUT2IN_ERROR_OUT2IN_PACKETS,
838 return frame->n_vectors;
842 VLIB_REGISTER_NODE (det44_out2in_node) = {
843 .name = "det44-out2in",
844 .vector_size = sizeof (u32),
845 .format_trace = format_det44_out2in_trace,
846 .type = VLIB_NODE_TYPE_INTERNAL,
847 .n_errors = ARRAY_LEN(det44_out2in_error_strings),
848 .error_strings = det44_out2in_error_strings,
849 .runtime_data_bytes = sizeof (det44_runtime_t),
850 .n_next_nodes = DET44_OUT2IN_N_NEXT,
851 /* edit / add dispositions here */
853 [DET44_OUT2IN_NEXT_DROP] = "error-drop",
854 [DET44_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
855 [DET44_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
861 * fd.io coding-style-patch-verification: ON
864 * eval: (c-set-style "gnu")