9e8ba27285bd7939d1d6205fdcf840a86be89b8b
[vpp.git] / src / plugins / nat / nat_det_out2in.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file
17  * @brief Deterministic/CGN NAT44 outside to inside network translation
18  */
19
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>
26 #include <nat/nat.h>
27 #include <nat/nat_det.h>
28 #include <nat/nat_inlines.h>
29
30 typedef enum
31 {
32   NAT_DET_OUT2IN_NEXT_DROP,
33   NAT_DET_OUT2IN_NEXT_LOOKUP,
34   NAT_DET_OUT2IN_NEXT_ICMP_ERROR,
35   NAT_DET_OUT2IN_N_NEXT,
36 } nat_det_out2in_next_t;
37
38 typedef struct
39 {
40   u32 sw_if_index;
41   u32 next_index;
42   u32 session_index;
43 } nat_det_out2in_trace_t;
44
45 #define foreach_nat_det_out2in_error                       \
46 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")         \
47 _(NO_TRANSLATION, "No translation")                     \
48 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
49 _(OUT2IN_PACKETS, "Good out2in packets processed")
50
51 typedef enum
52 {
53 #define _(sym,str) NAT_DET_OUT2IN_ERROR_##sym,
54   foreach_nat_det_out2in_error
55 #undef _
56     SNAT_OUT2IN_N_ERROR,
57 } nat_det_out2in_error_t;
58
59 static char *nat_det_out2in_error_strings[] = {
60 #define _(sym,string) string,
61   foreach_nat_det_out2in_error
62 #undef _
63 };
64
65 vlib_node_registration_t snat_det_out2in_node;
66
67 static u8 *
68 format_nat_det_out2in_trace (u8 * s, va_list * args)
69 {
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_out2in_trace_t *t = va_arg (*args, nat_det_out2in_trace_t *);
73
74   s =
75     format (s,
76             "NAT_DET_OUT2IN: sw_if_index %d, next index %d, session index %d",
77             t->sw_if_index, t->next_index, t->session_index);
78   return s;
79 }
80
81 /**
82  * Get address and port values to be used for ICMP packet translation
83  * and create session if needed
84  *
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[out] p_proto           protocol used for matching
90  * @param[out] p_value           address and port after NAT translation
91  * @param[out] p_dont_translate  if packet should not be translated
92  * @param d                      optional parameter
93  * @param e                      optional parameter
94  */
95 u32
96 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
97                        u32 thread_index, vlib_buffer_t * b0,
98                        ip4_header_t * ip0, u8 * p_proto,
99                        snat_session_key_t * p_value,
100                        u8 * p_dont_translate, void *d, void *e)
101 {
102   icmp46_header_t *icmp0;
103   u32 sw_if_index0;
104   u8 protocol;
105   snat_det_out_key_t key0;
106   u8 dont_translate = 0;
107   u32 next0 = ~0;
108   icmp_echo_header_t *echo0, *inner_echo0 = 0;
109   ip4_header_t *inner_ip0;
110   void *l4_header = 0;
111   icmp46_header_t *inner_icmp0;
112   snat_det_map_t *dm0 = 0;
113   ip4_address_t new_addr0 = { {0} };
114   snat_det_session_t *ses0 = 0;
115   ip4_address_t out_addr;
116
117   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
118   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
119   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
120
121   if (!icmp_is_error_message (icmp0))
122     {
123       protocol = SNAT_PROTOCOL_ICMP;
124       key0.ext_host_addr = ip0->src_address;
125       key0.ext_host_port = 0;
126       key0.out_port = echo0->identifier;
127       out_addr = ip0->dst_address;
128     }
129   else
130     {
131       inner_ip0 = (ip4_header_t *) (echo0 + 1);
132       l4_header = ip4_next_header (inner_ip0);
133       protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
134       key0.ext_host_addr = inner_ip0->dst_address;
135       out_addr = inner_ip0->src_address;
136       switch (protocol)
137         {
138         case SNAT_PROTOCOL_ICMP:
139           inner_icmp0 = (icmp46_header_t *) l4_header;
140           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
141           key0.ext_host_port = 0;
142           key0.out_port = inner_echo0->identifier;
143           break;
144         case SNAT_PROTOCOL_UDP:
145         case SNAT_PROTOCOL_TCP:
146           key0.ext_host_port = ((tcp_udp_header_t *) l4_header)->dst_port;
147           key0.out_port = ((tcp_udp_header_t *) l4_header)->src_port;
148           break;
149         default:
150           b0->error = node->errors[NAT_DET_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
151           next0 = NAT_DET_OUT2IN_NEXT_DROP;
152           goto out;
153         }
154     }
155
156   dm0 = snat_det_map_by_out (sm, &out_addr);
157   if (PREDICT_FALSE (!dm0))
158     {
159       /* Don't NAT packet aimed at the intfc address */
160       if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
161                                             ip0->dst_address.as_u32)))
162         {
163           dont_translate = 1;
164           goto out;
165         }
166       nat_log_info ("unknown dst address:  %U",
167                     format_ip4_address, &ip0->dst_address);
168       goto out;
169     }
170
171   snat_det_reverse (dm0, &ip0->dst_address,
172                     clib_net_to_host_u16 (key0.out_port), &new_addr0);
173
174   ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
175   if (PREDICT_FALSE (!ses0))
176     {
177       /* Don't NAT packet aimed at the intfc address */
178       if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
179                                             ip0->dst_address.as_u32)))
180         {
181           dont_translate = 1;
182           goto out;
183         }
184       nat_log_info ("no match src %U:%d dst %U:%d for user %U",
185                     format_ip4_address, &key0.ext_host_addr,
186                     clib_net_to_host_u16 (key0.ext_host_port),
187                     format_ip4_address, &out_addr,
188                     clib_net_to_host_u16 (key0.out_port),
189                     format_ip4_address, &new_addr0);
190       b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
191       next0 = NAT_DET_OUT2IN_NEXT_DROP;
192       goto out;
193     }
194
195   if (PREDICT_FALSE (icmp0->type != ICMP4_echo_reply &&
196                      !icmp_is_error_message (icmp0)))
197     {
198       b0->error = node->errors[NAT_DET_OUT2IN_ERROR_BAD_ICMP_TYPE];
199       next0 = NAT_DET_OUT2IN_NEXT_DROP;
200       goto out;
201     }
202
203   goto out;
204
205 out:
206   *p_proto = protocol;
207   if (ses0)
208     {
209       p_value->addr = new_addr0;
210       p_value->fib_index = sm->inside_fib_index;
211       p_value->port = ses0->in_port;
212     }
213   *p_dont_translate = dont_translate;
214   if (d)
215     *(snat_det_session_t **) d = ses0;
216   if (e)
217     *(snat_det_map_t **) e = dm0;
218   return next0;
219 }
220
221 static uword
222 snat_det_out2in_node_fn (vlib_main_t * vm,
223                          vlib_node_runtime_t * node, vlib_frame_t * frame)
224 {
225   u32 n_left_from, *from, *to_next;
226   nat_det_out2in_next_t next_index;
227   u32 pkts_processed = 0;
228   snat_main_t *sm = &snat_main;
229   u32 thread_index = vm->thread_index;
230
231   from = vlib_frame_vector_args (frame);
232   n_left_from = frame->n_vectors;
233   next_index = node->cached_next_index;
234
235   while (n_left_from > 0)
236     {
237       u32 n_left_to_next;
238
239       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
240
241       while (n_left_from >= 4 && n_left_to_next >= 2)
242         {
243           u32 bi0, bi1;
244           vlib_buffer_t *b0, *b1;
245           u32 next0 = NAT_DET_OUT2IN_NEXT_LOOKUP;
246           u32 next1 = NAT_DET_OUT2IN_NEXT_LOOKUP;
247           u32 sw_if_index0, sw_if_index1;
248           ip4_header_t *ip0, *ip1;
249           ip_csum_t sum0, sum1;
250           ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
251           u16 new_port0, old_port0, old_port1, new_port1;
252           udp_header_t *udp0, *udp1;
253           tcp_header_t *tcp0, *tcp1;
254           u32 proto0, proto1;
255           snat_det_out_key_t key0, key1;
256           snat_det_map_t *dm0, *dm1;
257           snat_det_session_t *ses0 = 0, *ses1 = 0;
258           u32 rx_fib_index0, rx_fib_index1;
259           icmp46_header_t *icmp0, *icmp1;
260
261           /* Prefetch next iteration. */
262           {
263             vlib_buffer_t *p2, *p3;
264
265             p2 = vlib_get_buffer (vm, from[2]);
266             p3 = vlib_get_buffer (vm, from[3]);
267
268             vlib_prefetch_buffer_header (p2, LOAD);
269             vlib_prefetch_buffer_header (p3, LOAD);
270
271             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
272             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
273           }
274
275           /* speculatively enqueue b0 and b1 to the current next frame */
276           to_next[0] = bi0 = from[0];
277           to_next[1] = bi1 = from[1];
278           from += 2;
279           to_next += 2;
280           n_left_from -= 2;
281           n_left_to_next -= 2;
282
283           b0 = vlib_get_buffer (vm, bi0);
284           b1 = vlib_get_buffer (vm, bi1);
285
286           ip0 = vlib_buffer_get_current (b0);
287           udp0 = ip4_next_header (ip0);
288           tcp0 = (tcp_header_t *) udp0;
289
290           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
291
292           if (PREDICT_FALSE (ip0->ttl == 1))
293             {
294               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
295               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
296                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
297                                            0);
298               next0 = NAT_DET_OUT2IN_NEXT_ICMP_ERROR;
299               goto trace0;
300             }
301
302           proto0 = ip_proto_to_snat_proto (ip0->protocol);
303
304           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
305             {
306               rx_fib_index0 =
307                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
308               icmp0 = (icmp46_header_t *) udp0;
309
310               next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
311                                    rx_fib_index0, node, next0, thread_index,
312                                    &ses0, &dm0);
313               goto trace0;
314             }
315
316           key0.ext_host_addr = ip0->src_address;
317           key0.ext_host_port = tcp0->src;
318           key0.out_port = tcp0->dst;
319
320           dm0 = snat_det_map_by_out (sm, &ip0->dst_address);
321           if (PREDICT_FALSE (!dm0))
322             {
323               nat_log_info ("unknown dst address:  %U",
324                             format_ip4_address, &ip0->dst_address);
325               next0 = NAT_DET_OUT2IN_NEXT_DROP;
326               b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
327               goto trace0;
328             }
329
330           snat_det_reverse (dm0, &ip0->dst_address,
331                             clib_net_to_host_u16 (tcp0->dst), &new_addr0);
332
333           ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
334           if (PREDICT_FALSE (!ses0))
335             {
336               nat_log_info ("no match src %U:%d dst %U:%d for user %U",
337                             format_ip4_address, &ip0->src_address,
338                             clib_net_to_host_u16 (tcp0->src),
339                             format_ip4_address, &ip0->dst_address,
340                             clib_net_to_host_u16 (tcp0->dst),
341                             format_ip4_address, &new_addr0);
342               next0 = NAT_DET_OUT2IN_NEXT_DROP;
343               b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
344               goto trace0;
345             }
346           new_port0 = ses0->in_port;
347
348           old_addr0 = ip0->dst_address;
349           ip0->dst_address = new_addr0;
350           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
351
352           sum0 = ip0->checksum;
353           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
354                                  ip4_header_t,
355                                  dst_address /* changed member */ );
356           ip0->checksum = ip_csum_fold (sum0);
357
358           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
359             {
360               if (tcp0->flags & TCP_FLAG_FIN
361                   && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
362                 ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
363               else if (tcp0->flags & TCP_FLAG_ACK
364                        && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
365                 snat_det_ses_close (dm0, ses0);
366
367               old_port0 = tcp0->dst;
368               tcp0->dst = new_port0;
369
370               sum0 = tcp0->checksum;
371               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
372                                      ip4_header_t,
373                                      dst_address /* changed member */ );
374
375               sum0 = ip_csum_update (sum0, old_port0, new_port0,
376                                      ip4_header_t /* cheat */ ,
377                                      length /* changed member */ );
378               tcp0->checksum = ip_csum_fold (sum0);
379             }
380           else
381             {
382               old_port0 = udp0->dst_port;
383               udp0->dst_port = new_port0;
384               udp0->checksum = 0;
385             }
386
387         trace0:
388
389           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
390                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
391             {
392               nat_det_out2in_trace_t *t =
393                 vlib_add_trace (vm, node, b0, sizeof (*t));
394               t->sw_if_index = sw_if_index0;
395               t->next_index = next0;
396               t->session_index = ~0;
397               if (ses0)
398                 t->session_index = ses0 - dm0->sessions;
399             }
400
401           pkts_processed += next0 != NAT_DET_OUT2IN_NEXT_DROP;
402
403           b1 = vlib_get_buffer (vm, bi1);
404
405           ip1 = vlib_buffer_get_current (b1);
406           udp1 = ip4_next_header (ip1);
407           tcp1 = (tcp_header_t *) udp1;
408
409           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
410
411           if (PREDICT_FALSE (ip1->ttl == 1))
412             {
413               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
414               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
415                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
416                                            0);
417               next1 = NAT_DET_OUT2IN_NEXT_ICMP_ERROR;
418               goto trace1;
419             }
420
421           proto1 = ip_proto_to_snat_proto (ip1->protocol);
422
423           if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
424             {
425               rx_fib_index1 =
426                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
427               icmp1 = (icmp46_header_t *) udp1;
428
429               next1 = icmp_out2in (sm, b1, ip1, icmp1, sw_if_index1,
430                                    rx_fib_index1, node, next1, thread_index,
431                                    &ses1, &dm1);
432               goto trace1;
433             }
434
435           key1.ext_host_addr = ip1->src_address;
436           key1.ext_host_port = tcp1->src;
437           key1.out_port = tcp1->dst;
438
439           dm1 = snat_det_map_by_out (sm, &ip1->dst_address);
440           if (PREDICT_FALSE (!dm1))
441             {
442               nat_log_info ("unknown dst address:  %U",
443                             format_ip4_address, &ip1->dst_address);
444               next1 = NAT_DET_OUT2IN_NEXT_DROP;
445               b1->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
446               goto trace1;
447             }
448
449           snat_det_reverse (dm1, &ip1->dst_address,
450                             clib_net_to_host_u16 (tcp1->dst), &new_addr1);
451
452           ses1 = snat_det_get_ses_by_out (dm1, &new_addr1, key1.as_u64);
453           if (PREDICT_FALSE (!ses1))
454             {
455               nat_log_info ("no match src %U:%d dst %U:%d for user %U",
456                             format_ip4_address, &ip1->src_address,
457                             clib_net_to_host_u16 (tcp1->src),
458                             format_ip4_address, &ip1->dst_address,
459                             clib_net_to_host_u16 (tcp1->dst),
460                             format_ip4_address, &new_addr1);
461               next1 = NAT_DET_OUT2IN_NEXT_DROP;
462               b1->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
463               goto trace1;
464             }
465           new_port1 = ses1->in_port;
466
467           old_addr1 = ip1->dst_address;
468           ip1->dst_address = new_addr1;
469           vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
470
471           sum1 = ip1->checksum;
472           sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
473                                  ip4_header_t,
474                                  dst_address /* changed member */ );
475           ip1->checksum = ip_csum_fold (sum1);
476
477           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
478             {
479               if (tcp1->flags & TCP_FLAG_FIN
480                   && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
481                 ses1->state = SNAT_SESSION_TCP_CLOSE_WAIT;
482               else if (tcp1->flags & TCP_FLAG_ACK
483                        && ses1->state == SNAT_SESSION_TCP_LAST_ACK)
484                 snat_det_ses_close (dm1, ses1);
485
486               old_port1 = tcp1->dst;
487               tcp1->dst = new_port1;
488
489               sum1 = tcp1->checksum;
490               sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
491                                      ip4_header_t,
492                                      dst_address /* changed member */ );
493
494               sum1 = ip_csum_update (sum1, old_port1, new_port1,
495                                      ip4_header_t /* cheat */ ,
496                                      length /* changed member */ );
497               tcp1->checksum = ip_csum_fold (sum1);
498             }
499           else
500             {
501               old_port1 = udp1->dst_port;
502               udp1->dst_port = new_port1;
503               udp1->checksum = 0;
504             }
505
506         trace1:
507
508           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
509                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
510             {
511               nat_det_out2in_trace_t *t =
512                 vlib_add_trace (vm, node, b1, sizeof (*t));
513               t->sw_if_index = sw_if_index1;
514               t->next_index = next1;
515               t->session_index = ~0;
516               if (ses1)
517                 t->session_index = ses1 - dm1->sessions;
518             }
519
520           pkts_processed += next1 != NAT_DET_OUT2IN_NEXT_DROP;
521
522           /* verify speculative enqueues, maybe switch current next frame */
523           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
524                                            to_next, n_left_to_next,
525                                            bi0, bi1, next0, next1);
526         }
527
528       while (n_left_from > 0 && n_left_to_next > 0)
529         {
530           u32 bi0;
531           vlib_buffer_t *b0;
532           u32 next0 = NAT_DET_OUT2IN_NEXT_LOOKUP;
533           u32 sw_if_index0;
534           ip4_header_t *ip0;
535           ip_csum_t sum0;
536           ip4_address_t new_addr0, old_addr0;
537           u16 new_port0, old_port0;
538           udp_header_t *udp0;
539           tcp_header_t *tcp0;
540           u32 proto0;
541           snat_det_out_key_t key0;
542           snat_det_map_t *dm0;
543           snat_det_session_t *ses0 = 0;
544           u32 rx_fib_index0;
545           icmp46_header_t *icmp0;
546
547           /* speculatively enqueue b0 to the current next frame */
548           bi0 = from[0];
549           to_next[0] = bi0;
550           from += 1;
551           to_next += 1;
552           n_left_from -= 1;
553           n_left_to_next -= 1;
554
555           b0 = vlib_get_buffer (vm, bi0);
556
557           ip0 = vlib_buffer_get_current (b0);
558           udp0 = ip4_next_header (ip0);
559           tcp0 = (tcp_header_t *) udp0;
560
561           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
562
563           if (PREDICT_FALSE (ip0->ttl == 1))
564             {
565               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
566               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
567                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
568                                            0);
569               next0 = NAT_DET_OUT2IN_NEXT_ICMP_ERROR;
570               goto trace00;
571             }
572
573           proto0 = ip_proto_to_snat_proto (ip0->protocol);
574
575           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
576             {
577               rx_fib_index0 =
578                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
579               icmp0 = (icmp46_header_t *) udp0;
580
581               next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
582                                    rx_fib_index0, node, next0, thread_index,
583                                    &ses0, &dm0);
584               goto trace00;
585             }
586
587           key0.ext_host_addr = ip0->src_address;
588           key0.ext_host_port = tcp0->src;
589           key0.out_port = tcp0->dst;
590
591           dm0 = snat_det_map_by_out (sm, &ip0->dst_address);
592           if (PREDICT_FALSE (!dm0))
593             {
594               nat_log_info ("unknown dst address:  %U",
595                             format_ip4_address, &ip0->dst_address);
596               next0 = NAT_DET_OUT2IN_NEXT_DROP;
597               b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
598               goto trace00;
599             }
600
601           snat_det_reverse (dm0, &ip0->dst_address,
602                             clib_net_to_host_u16 (tcp0->dst), &new_addr0);
603
604           ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
605           if (PREDICT_FALSE (!ses0))
606             {
607               nat_log_info ("no match src %U:%d dst %U:%d for user %U",
608                             format_ip4_address, &ip0->src_address,
609                             clib_net_to_host_u16 (tcp0->src),
610                             format_ip4_address, &ip0->dst_address,
611                             clib_net_to_host_u16 (tcp0->dst),
612                             format_ip4_address, &new_addr0);
613               next0 = NAT_DET_OUT2IN_NEXT_DROP;
614               b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
615               goto trace00;
616             }
617           new_port0 = ses0->in_port;
618
619           old_addr0 = ip0->dst_address;
620           ip0->dst_address = new_addr0;
621           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
622
623           sum0 = ip0->checksum;
624           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
625                                  ip4_header_t,
626                                  dst_address /* changed member */ );
627           ip0->checksum = ip_csum_fold (sum0);
628
629           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
630             {
631               if (tcp0->flags & TCP_FLAG_FIN
632                   && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
633                 ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
634               else if (tcp0->flags & TCP_FLAG_ACK
635                        && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
636                 snat_det_ses_close (dm0, ses0);
637
638               old_port0 = tcp0->dst;
639               tcp0->dst = new_port0;
640
641               sum0 = tcp0->checksum;
642               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
643                                      ip4_header_t,
644                                      dst_address /* changed member */ );
645
646               sum0 = ip_csum_update (sum0, old_port0, new_port0,
647                                      ip4_header_t /* cheat */ ,
648                                      length /* changed member */ );
649               tcp0->checksum = ip_csum_fold (sum0);
650             }
651           else
652             {
653               old_port0 = udp0->dst_port;
654               udp0->dst_port = new_port0;
655               udp0->checksum = 0;
656             }
657
658         trace00:
659
660           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
661                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
662             {
663               nat_det_out2in_trace_t *t =
664                 vlib_add_trace (vm, node, b0, sizeof (*t));
665               t->sw_if_index = sw_if_index0;
666               t->next_index = next0;
667               t->session_index = ~0;
668               if (ses0)
669                 t->session_index = ses0 - dm0->sessions;
670             }
671
672           pkts_processed += next0 != NAT_DET_OUT2IN_NEXT_DROP;
673
674           /* verify speculative enqueue, maybe switch current next frame */
675           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
676                                            to_next, n_left_to_next,
677                                            bi0, next0);
678         }
679
680       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
681     }
682
683   vlib_node_increment_counter (vm, snat_det_out2in_node.index,
684                                NAT_DET_OUT2IN_ERROR_OUT2IN_PACKETS,
685                                pkts_processed);
686   return frame->n_vectors;
687 }
688
689 /* *INDENT-OFF* */
690 VLIB_REGISTER_NODE (snat_det_out2in_node) = {
691   .function = snat_det_out2in_node_fn,
692   .name = "nat44-det-out2in",
693   .vector_size = sizeof (u32),
694   .format_trace = format_nat_det_out2in_trace,
695   .type = VLIB_NODE_TYPE_INTERNAL,
696   .n_errors = ARRAY_LEN(nat_det_out2in_error_strings),
697   .error_strings = nat_det_out2in_error_strings,
698   .runtime_data_bytes = sizeof (snat_runtime_t),
699   .n_next_nodes = NAT_DET_OUT2IN_N_NEXT,
700   /* edit / add dispositions here */
701   .next_nodes = {
702     [NAT_DET_OUT2IN_NEXT_DROP] = "error-drop",
703     [NAT_DET_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
704     [NAT_DET_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
705   },
706 };
707 /* *INDENT-ON* */
708
709 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_out2in_node, snat_det_out2in_node_fn);
710
711 /*
712  * fd.io coding-style-patch-verification: ON
713  *
714  * Local Variables:
715  * eval: (c-set-style "gnu")
716  * End:
717  */