nat: move deterministic nat to det44 sub feature
[vpp.git] / src / plugins / nat / det44 / det44_out2in.c
1 /*
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:
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 /**
17  * @file
18  * @brief Deterministic NAT (CGN) outside to inside translation
19  */
20
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>
27
28 #include <nat/det44/det44.h>
29 #include <nat/det44/det44_inlines.h>
30
31 #include <nat/lib/lib.h>
32 #include <nat/lib/inlines.h>
33 #include <nat/lib/nat_inlines.h>
34
35 typedef enum
36 {
37   DET44_OUT2IN_NEXT_DROP,
38   DET44_OUT2IN_NEXT_LOOKUP,
39   DET44_OUT2IN_NEXT_ICMP_ERROR,
40   DET44_OUT2IN_N_NEXT,
41 } det44_out2in_next_t;
42
43 typedef struct
44 {
45   u32 sw_if_index;
46   u32 next_index;
47   u32 session_index;
48 } det44_out2in_trace_t;
49
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")
55
56 typedef enum
57 {
58 #define _(sym,str) DET44_OUT2IN_ERROR_##sym,
59   foreach_det44_out2in_error
60 #undef _
61     DET44_OUT2IN_N_ERROR,
62 } det44_out2in_error_t;
63
64 static char *det44_out2in_error_strings[] = {
65 #define _(sym,string) string,
66   foreach_det44_out2in_error
67 #undef _
68 };
69
70 static u8 *
71 format_det44_out2in_trace (u8 * s, va_list * args)
72 {
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 *);
76
77   s =
78     format (s,
79             "DET44_OUT2IN: sw_if_index %d, next index %d, session index %d",
80             t->sw_if_index, t->next_index, t->session_index);
81   return s;
82 }
83
84 #ifndef CLIB_MARCH_VARIANT
85 /**
86  * Get address and port values to be used for ICMP packet translation
87  * and create session if needed
88  *
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
98  */
99 u32
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,
105                        u8 * dont_translate)
106 {
107   det44_main_t *dm = &det44_main;
108   icmp46_header_t *icmp0;
109   u32 sw_if_index0;
110   u8 protocol;
111   snat_det_out_key_t key0;
112   u32 next0 = ~0;
113   icmp_echo_header_t *echo0, *inner_echo0 = 0;
114   ip4_header_t *inner_ip0;
115   void *l4_header = 0;
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;
121   *dont_translate = 0;
122
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
127   if (!icmp_type_is_error_message
128       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
129     {
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;
135     }
136   else
137     {
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;
144       switch (protocol)
145         {
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;
151           break;
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;
156           break;
157         default:
158           b0->error = node->errors[DET44_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
159           next0 = DET44_OUT2IN_NEXT_DROP;
160           goto out;
161         }
162     }
163
164   mp0 = snat_det_map_by_out (&out_addr);
165   if (PREDICT_FALSE (!mp0))
166     {
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)))
170         {
171           *dont_translate = 1;
172           goto out;
173         }
174       det44_log_info ("unknown dst address:  %U",
175                       format_ip4_address, &ip0->dst_address);
176       goto out;
177     }
178
179   snat_det_reverse (mp0, &ip0->dst_address,
180                     clib_net_to_host_u16 (key0.out_port), &new_addr0);
181
182   ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
183   if (PREDICT_FALSE (!ses0))
184     {
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)))
188         {
189           *dont_translate = 1;
190           goto out;
191         }
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;
200       goto out;
201     }
202
203   if (PREDICT_FALSE
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)))
207     {
208       b0->error = node->errors[DET44_OUT2IN_ERROR_BAD_ICMP_TYPE];
209       next0 = DET44_OUT2IN_NEXT_DROP;
210       goto out;
211     }
212
213   goto out;
214
215 out:
216   *proto = protocol;
217   if (ses0)
218     {
219       *addr = new_addr0;
220       *fib_index = dm->inside_fib_index;
221       *port = ses0->in_port;
222     }
223   if (d)
224     *(snat_det_session_t **) d = ses0;
225   if (e)
226     *(snat_det_map_t **) e = mp0;
227   return next0;
228 }
229 #endif
230
231 #ifndef CLIB_MARCH_VARIANT
232 u32
233 det44_icmp_out2in (vlib_buffer_t * b0,
234                    ip4_header_t * ip0,
235                    icmp46_header_t * icmp0,
236                    u32 sw_if_index0,
237                    u32 rx_fib_index0,
238                    vlib_node_runtime_t * node,
239                    u32 next0, u32 thread_index, void *d, void *e)
240 {
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;
247   ip4_address_t addr;
248   void *l4_header;
249   u8 dont_translate;
250   ip_csum_t sum0;
251   nat_protocol_t proto;
252
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);
257   if (next0_tmp != ~0)
258     next0 = next0_tmp;
259   if (next0 == DET44_OUT2IN_NEXT_DROP || dont_translate)
260     goto out;
261
262   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
263     {
264       sum0 =
265         ip_incremental_checksum_buffer (vm, b0,
266                                         (u8 *) icmp0 -
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)
272         {
273           next0 = DET44_OUT2IN_NEXT_DROP;
274           goto out;
275         }
276     }
277
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;
281
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);
286
287
288   if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
289     {
290       if (icmp0->checksum == 0)
291         icmp0->checksum = 0xffff;
292
293       if (!icmp_type_is_error_message (icmp0->type))
294         {
295           new_id0 = port;
296           if (PREDICT_FALSE (new_id0 != echo0->identifier))
297             {
298               old_id0 = echo0->identifier;
299               new_id0 = port;
300               echo0->identifier = new_id0;
301
302               sum0 = icmp0->checksum;
303               sum0 =
304                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
305                                 identifier /* changed member */ );
306               icmp0->checksum = ip_csum_fold (sum0);
307             }
308         }
309       else
310         {
311           inner_ip0 = (ip4_header_t *) (echo0 + 1);
312           l4_header = ip4_next_header (inner_ip0);
313
314           if (!ip4_header_checksum_is_valid (inner_ip0))
315             {
316               next0 = DET44_OUT2IN_NEXT_DROP;
317               goto out;
318             }
319
320           old_addr0 = inner_ip0->src_address.as_u32;
321           inner_ip0->src_address = addr;
322           new_addr0 = inner_ip0->src_address.as_u32;
323
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);
328
329           switch (proto)
330             {
331             case NAT_PROTOCOL_ICMP:
332               inner_icmp0 = (icmp46_header_t *) l4_header;
333               inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
334
335               old_id0 = inner_echo0->identifier;
336               new_id0 = port;
337               inner_echo0->identifier = new_id0;
338
339               sum0 = icmp0->checksum;
340               sum0 =
341                 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
342                                 identifier);
343               icmp0->checksum = ip_csum_fold (sum0);
344               break;
345             case NAT_PROTOCOL_UDP:
346             case NAT_PROTOCOL_TCP:
347               old_id0 = ((tcp_udp_header_t *) l4_header)->src_port;
348               new_id0 = port;
349               ((tcp_udp_header_t *) l4_header)->src_port = new_id0;
350
351               sum0 = icmp0->checksum;
352               sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
353                                      src_port);
354               icmp0->checksum = ip_csum_fold (sum0);
355               break;
356             default:
357               ASSERT (0);
358             }
359         }
360     }
361
362 out:
363   return next0;
364 }
365 #endif
366
367 VLIB_NODE_FN (det44_out2in_node) (vlib_main_t * vm,
368                                   vlib_node_runtime_t * node,
369                                   vlib_frame_t * frame)
370 {
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;
376
377   from = vlib_frame_vector_args (frame);
378   n_left_from = frame->n_vectors;
379   next_index = node->cached_next_index;
380
381   while (n_left_from > 0)
382     {
383       u32 n_left_to_next;
384
385       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
386
387       while (n_left_from >= 4 && n_left_to_next >= 2)
388         {
389           u32 bi0, bi1;
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;
400           u32 proto0, proto1;
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;
406
407           /* Prefetch next iteration. */
408           {
409             vlib_buffer_t *p2, *p3;
410
411             p2 = vlib_get_buffer (vm, from[2]);
412             p3 = vlib_get_buffer (vm, from[3]);
413
414             vlib_prefetch_buffer_header (p2, LOAD);
415             vlib_prefetch_buffer_header (p3, LOAD);
416
417             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
418             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
419           }
420
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];
424           from += 2;
425           to_next += 2;
426           n_left_from -= 2;
427           n_left_to_next -= 2;
428
429           b0 = vlib_get_buffer (vm, bi0);
430           b1 = vlib_get_buffer (vm, bi1);
431
432           ip0 = vlib_buffer_get_current (b0);
433           udp0 = ip4_next_header (ip0);
434           tcp0 = (tcp_header_t *) udp0;
435
436           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
437
438           if (PREDICT_FALSE (ip0->ttl == 1))
439             {
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,
443                                            0);
444               next0 = DET44_OUT2IN_NEXT_ICMP_ERROR;
445               goto trace0;
446             }
447
448           proto0 = ip_proto_to_nat_proto (ip0->protocol);
449
450           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
451             {
452               rx_fib_index0 =
453                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
454               icmp0 = (icmp46_header_t *) udp0;
455
456               next0 = det44_icmp_out2in (b0, ip0, icmp0, sw_if_index0,
457                                          rx_fib_index0, node, next0,
458                                          thread_index, &ses0, &mp0);
459               goto trace0;
460             }
461
462           key0.ext_host_addr = ip0->src_address;
463           key0.ext_host_port = tcp0->src;
464           key0.out_port = tcp0->dst;
465
466           mp0 = snat_det_map_by_out (&ip0->dst_address);
467           if (PREDICT_FALSE (!mp0))
468             {
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];
473               goto trace0;
474             }
475
476           snat_det_reverse (mp0, &ip0->dst_address,
477                             clib_net_to_host_u16 (tcp0->dst), &new_addr0);
478
479           ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
480           if (PREDICT_FALSE (!ses0))
481             {
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];
490               goto trace0;
491             }
492           old_port0 = udp0->dst_port;
493           udp0->dst_port = new_port0 = ses0->in_port;
494
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;
498
499           sum0 = ip0->checksum;
500           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
501                                  ip4_header_t,
502                                  dst_address /* changed member */ );
503           ip0->checksum = ip_csum_fold (sum0);
504
505           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
506             {
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);
513
514               sum0 = tcp0->checksum;
515               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
516                                      ip4_header_t,
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);
522             }
523           else if (udp0->checksum)
524             {
525               sum0 = udp0->checksum;
526               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
527                                      ip4_header_t,
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);
533             }
534
535         trace0:
536
537           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
538                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
539             {
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;
545               if (ses0)
546                 t->session_index = ses0 - mp0->sessions;
547             }
548
549           pkts_processed += next0 != DET44_OUT2IN_NEXT_DROP;
550
551           b1 = vlib_get_buffer (vm, bi1);
552
553           ip1 = vlib_buffer_get_current (b1);
554           udp1 = ip4_next_header (ip1);
555           tcp1 = (tcp_header_t *) udp1;
556
557           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
558
559           if (PREDICT_FALSE (ip1->ttl == 1))
560             {
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,
564                                            0);
565               next1 = DET44_OUT2IN_NEXT_ICMP_ERROR;
566               goto trace1;
567             }
568
569           proto1 = ip_proto_to_nat_proto (ip1->protocol);
570
571           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
572             {
573               rx_fib_index1 =
574                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
575               icmp1 = (icmp46_header_t *) udp1;
576
577               next1 = det44_icmp_out2in (b1, ip1, icmp1, sw_if_index1,
578                                          rx_fib_index1, node, next1,
579                                          thread_index, &ses1, &mp1);
580               goto trace1;
581             }
582
583           key1.ext_host_addr = ip1->src_address;
584           key1.ext_host_port = tcp1->src;
585           key1.out_port = tcp1->dst;
586
587           mp1 = snat_det_map_by_out (&ip1->dst_address);
588           if (PREDICT_FALSE (!mp1))
589             {
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];
594               goto trace1;
595             }
596
597           snat_det_reverse (mp1, &ip1->dst_address,
598                             clib_net_to_host_u16 (tcp1->dst), &new_addr1);
599
600           ses1 = snat_det_get_ses_by_out (mp1, &new_addr1, key1.as_u64);
601           if (PREDICT_FALSE (!ses1))
602             {
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];
611               goto trace1;
612             }
613           old_port1 = udp1->dst_port;
614           udp1->dst_port = new_port1 = ses1->in_port;
615
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;
619
620           sum1 = ip1->checksum;
621           sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
622                                  ip4_header_t,
623                                  dst_address /* changed member */ );
624           ip1->checksum = ip_csum_fold (sum1);
625
626           if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
627             {
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);
634
635               sum1 = tcp1->checksum;
636               sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
637                                      ip4_header_t,
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);
643             }
644           else if (udp1->checksum)
645             {
646               sum1 = udp1->checksum;
647               sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
648                                      ip4_header_t,
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);
654             }
655
656         trace1:
657
658           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
659                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
660             {
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;
666               if (ses1)
667                 t->session_index = ses1 - mp1->sessions;
668             }
669
670           pkts_processed += next1 != DET44_OUT2IN_NEXT_DROP;
671
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);
676         }
677
678       while (n_left_from > 0 && n_left_to_next > 0)
679         {
680           u32 bi0;
681           vlib_buffer_t *b0;
682           u32 next0 = DET44_OUT2IN_NEXT_LOOKUP;
683           u32 sw_if_index0;
684           ip4_header_t *ip0;
685           ip_csum_t sum0;
686           ip4_address_t new_addr0, old_addr0;
687           u16 new_port0, old_port0;
688           udp_header_t *udp0;
689           tcp_header_t *tcp0;
690           u32 proto0;
691           snat_det_out_key_t key0;
692           snat_det_map_t *mp0;
693           snat_det_session_t *ses0 = 0;
694           u32 rx_fib_index0;
695           icmp46_header_t *icmp0;
696
697           /* speculatively enqueue b0 to the current next frame */
698           bi0 = from[0];
699           to_next[0] = bi0;
700           from += 1;
701           to_next += 1;
702           n_left_from -= 1;
703           n_left_to_next -= 1;
704
705           b0 = vlib_get_buffer (vm, bi0);
706
707           ip0 = vlib_buffer_get_current (b0);
708           udp0 = ip4_next_header (ip0);
709           tcp0 = (tcp_header_t *) udp0;
710
711           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
712
713           if (PREDICT_FALSE (ip0->ttl == 1))
714             {
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,
718                                            0);
719               next0 = DET44_OUT2IN_NEXT_ICMP_ERROR;
720               goto trace00;
721             }
722
723           proto0 = ip_proto_to_nat_proto (ip0->protocol);
724
725           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
726             {
727               rx_fib_index0 =
728                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
729               icmp0 = (icmp46_header_t *) udp0;
730
731               next0 = det44_icmp_out2in (b0, ip0, icmp0, sw_if_index0,
732                                          rx_fib_index0, node, next0,
733                                          thread_index, &ses0, &mp0);
734               goto trace00;
735             }
736
737           key0.ext_host_addr = ip0->src_address;
738           key0.ext_host_port = tcp0->src;
739           key0.out_port = tcp0->dst;
740
741           mp0 = snat_det_map_by_out (&ip0->dst_address);
742           if (PREDICT_FALSE (!mp0))
743             {
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];
748               goto trace00;
749             }
750
751           snat_det_reverse (mp0, &ip0->dst_address,
752                             clib_net_to_host_u16 (tcp0->dst), &new_addr0);
753
754           ses0 = snat_det_get_ses_by_out (mp0, &new_addr0, key0.as_u64);
755           if (PREDICT_FALSE (!ses0))
756             {
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];
765               goto trace00;
766             }
767           old_port0 = udp0->dst_port;
768           udp0->dst_port = new_port0 = ses0->in_port;
769
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;
773
774           sum0 = ip0->checksum;
775           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
776                                  ip4_header_t,
777                                  dst_address /* changed member */ );
778           ip0->checksum = ip_csum_fold (sum0);
779
780           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
781             {
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);
788
789               sum0 = tcp0->checksum;
790               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
791                                      ip4_header_t,
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);
797             }
798           else if (udp0->checksum)
799             {
800               sum0 = udp0->checksum;
801               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
802                                      ip4_header_t,
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);
808             }
809
810         trace00:
811
812           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
813                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
814             {
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;
820               if (ses0)
821                 t->session_index = ses0 - mp0->sessions;
822             }
823
824           pkts_processed += next0 != DET44_OUT2IN_NEXT_DROP;
825
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,
829                                            bi0, next0);
830         }
831
832       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
833     }
834
835   vlib_node_increment_counter (vm, dm->out2in_node_index,
836                                DET44_OUT2IN_ERROR_OUT2IN_PACKETS,
837                                pkts_processed);
838   return frame->n_vectors;
839 }
840
841 /* *INDENT-OFF* */
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 */
852   .next_nodes = {
853     [DET44_OUT2IN_NEXT_DROP] = "error-drop",
854     [DET44_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
855     [DET44_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
856   },
857 };
858 /* *INDENT-ON* */
859
860 /*
861  * fd.io coding-style-patch-verification: ON
862  *
863  * Local Variables:
864  * eval: (c-set-style "gnu")
865  * End:
866  */