nat: remove non-error error counters
[vpp.git] / src / plugins / nat / nat_det_in2out.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 inside to outside 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 #include <nat/lib/nat_inlines.h>
30
31 typedef struct
32 {
33   u32 sw_if_index;
34   u32 next_index;
35   u32 session_index;
36 } nat_det_in2out_trace_t;
37
38 typedef enum
39 {
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;
45
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")
52
53 typedef enum
54 {
55 #define _(sym,str) NAT_DET_IN2OUT_ERROR_##sym,
56   foreach_nat_det_in2out_error
57 #undef _
58     NAT_DET_IN2OUT_N_ERROR,
59 } nat_det_in2out_error_t;
60
61 static char *nat_det_in2out_error_strings[] = {
62 #define _(sym,string) string,
63   foreach_nat_det_in2out_error
64 #undef _
65 };
66
67 static u8 *
68 format_nat_det_in2out_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_in2out_trace_t *t = va_arg (*args, nat_det_in2out_trace_t *);
73
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);
76
77   return s;
78 }
79
80 #ifndef CLIB_MARCH_VARIANT
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[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
95  */
96 u32
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,
102                        u8 * dont_translate)
103 {
104   vlib_main_t *vm = vlib_get_main ();
105   icmp46_header_t *icmp0;
106   u32 sw_if_index0;
107   u32 rx_fib_index0;
108   nat_protocol_t protocol;
109   snat_det_out_key_t key0;
110   u32 next0 = ~0;
111   icmp_echo_header_t *echo0, *inner_echo0 = 0;
112   ip4_header_t *inner_ip0;
113   void *l4_header = 0;
114   icmp46_header_t *inner_icmp0;
115   snat_det_map_t *dm0 = 0;
116   ip4_address_t new_addr0;
117   u16 lo_port0, i0;
118   snat_det_session_t *ses0 = 0;
119   ip4_address_t in_addr;
120   u16 in_port;
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   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
127
128   if (!icmp_type_is_error_message
129       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
130     {
131       protocol = NAT_PROTOCOL_ICMP;
132       in_addr = ip0->src_address;
133       in_port = vnet_buffer (b0)->ip.reass.l4_src_port;
134     }
135   else
136     {
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;
142       switch (protocol)
143         {
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;
148           break;
149         case NAT_PROTOCOL_UDP:
150         case NAT_PROTOCOL_TCP:
151           in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
152           break;
153         default:
154           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
155           next0 = NAT_DET_IN2OUT_NEXT_DROP;
156           goto out;
157         }
158     }
159
160   dm0 = snat_det_map_by_user (sm, &in_addr);
161   if (PREDICT_FALSE (!dm0))
162     {
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,
166                                                   IP_PROTOCOL_ICMP,
167                                                   rx_fib_index0)))
168         {
169           *dont_translate = 1;
170           goto out;
171         }
172       next0 = NAT_DET_IN2OUT_NEXT_DROP;
173       b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
174       goto out;
175     }
176
177   snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
178
179   key0.ext_host_addr = ip0->dst_address;
180   key0.ext_host_port = 0;
181
182   ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
183   if (PREDICT_FALSE (!ses0))
184     {
185       if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
186                                                   IP_PROTOCOL_ICMP,
187                                                   rx_fib_index0)))
188         {
189           *dont_translate = 1;
190           goto out;
191         }
192       if (icmp0->type != ICMP4_echo_request)
193         {
194           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
195           next0 = NAT_DET_IN2OUT_NEXT_DROP;
196           goto out;
197         }
198       for (i0 = 0; i0 < dm0->ports_per_host; i0++)
199         {
200           key0.out_port = clib_host_to_net_u16 (lo_port0 +
201                                                 ((i0 +
202                                                   clib_net_to_host_u16
203                                                   (echo0->identifier)) %
204                                                  dm0->ports_per_host));
205
206           if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
207             continue;
208
209           ses0 =
210             snat_det_ses_create (thread_index, dm0,
211                                  &in_addr, echo0->identifier, &key0);
212           break;
213         }
214       if (PREDICT_FALSE (!ses0))
215         {
216           next0 = NAT_DET_IN2OUT_NEXT_DROP;
217           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
218           goto out;
219         }
220     }
221
222   if (PREDICT_FALSE
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)))
226     {
227       b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
228       next0 = NAT_DET_IN2OUT_NEXT_DROP;
229       goto out;
230     }
231
232   u32 now = (u32) vlib_time_now (vm);
233
234   ses0->state = SNAT_SESSION_ICMP_ACTIVE;
235   ses0->expire = now + sm->icmp_timeout;
236
237 out:
238   *proto = protocol;
239   if (ses0)
240     {
241       *addr = new_addr0;
242       *fib_index = sm->outside_fib_index;
243       *port = ses0->out.out_port;
244     }
245   if (d)
246     *(snat_det_session_t **) d = ses0;
247   if (e)
248     *(snat_det_map_t **) e = dm0;
249   return next0;
250 }
251 #endif
252
253 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
254                                      vlib_node_runtime_t * node,
255                                      vlib_frame_t * frame)
256 {
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;
263
264   from = vlib_frame_vector_args (frame);
265   n_left_from = frame->n_vectors;
266   next_index = node->cached_next_index;
267
268   while (n_left_from > 0)
269     {
270       u32 n_left_to_next;
271
272       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
273
274       while (n_left_from >= 4 && n_left_to_next >= 2)
275         {
276           u32 bi0, bi1;
277           vlib_buffer_t *b0, *b1;
278           u32 next0, next1;
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;
287           u32 proto0, proto1;
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;
293
294           /* Prefetch next iteration. */
295           {
296             vlib_buffer_t *p2, *p3;
297
298             p2 = vlib_get_buffer (vm, from[2]);
299             p3 = vlib_get_buffer (vm, from[3]);
300
301             vlib_prefetch_buffer_header (p2, LOAD);
302             vlib_prefetch_buffer_header (p3, LOAD);
303
304             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
305             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
306           }
307
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];
311           from += 2;
312           to_next += 2;
313           n_left_from -= 2;
314           n_left_to_next -= 2;
315
316           b0 = vlib_get_buffer (vm, bi0);
317           b1 = vlib_get_buffer (vm, bi1);
318
319           next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
320           next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
321
322           ip0 = vlib_buffer_get_current (b0);
323           udp0 = ip4_next_header (ip0);
324           tcp0 = (tcp_header_t *) udp0;
325
326           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
327
328           if (PREDICT_FALSE (ip0->ttl == 1))
329             {
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,
333                                            0);
334               next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
335               goto trace0;
336             }
337
338           proto0 = ip_proto_to_nat_proto (ip0->protocol);
339
340           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
341             {
342               rx_fib_index0 =
343                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
344               icmp0 = (icmp46_header_t *) udp0;
345
346               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
347                                    rx_fib_index0, node, next0, thread_index,
348                                    &ses0, &dm0);
349               goto trace0;
350             }
351
352           dm0 = snat_det_map_by_user (sm, &ip0->src_address);
353           if (PREDICT_FALSE (!dm0))
354             {
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];
359               goto trace0;
360             }
361
362           snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
363
364           key0.ext_host_addr = ip0->dst_address;
365           key0.ext_host_port = tcp0->dst;
366
367           ses0 =
368             snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
369           if (PREDICT_FALSE (!ses0))
370             {
371               for (i0 = 0; i0 < dm0->ports_per_host; i0++)
372                 {
373                   key0.out_port = clib_host_to_net_u16 (lo_port0 +
374                                                         ((i0 +
375                                                           clib_net_to_host_u16
376                                                           (tcp0->src)) %
377                                                          dm0->
378                                                          ports_per_host));
379
380                   if (snat_det_get_ses_by_out
381                       (dm0, &ip0->src_address, key0.as_u64))
382                     continue;
383
384                   ses0 =
385                     snat_det_ses_create (thread_index, dm0, &ip0->src_address,
386                                          tcp0->src, &key0);
387                   break;
388                 }
389               if (PREDICT_FALSE (!ses0))
390                 {
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,
396                                                0);
397                   next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
398                   goto trace0;
399                 }
400             }
401
402           old_port0 = udp0->src_port;
403           udp0->src_port = new_port0 = ses0->out.out_port;
404
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;
408
409           sum0 = ip0->checksum;
410           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
411                                  ip4_header_t,
412                                  src_address /* changed member */ );
413           ip0->checksum = ip_csum_fold (sum0);
414
415           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
416             {
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;
434
435               sum0 = tcp0->checksum;
436               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
437                                      ip4_header_t,
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);
444             }
445           else
446             {
447               ses0->state = SNAT_SESSION_UDP_ACTIVE;
448
449               if (PREDICT_FALSE (udp0->checksum))
450                 {
451                   sum0 = udp0->checksum;
452                   sum0 =
453                     ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
454                                     ip4_header_t,
455                                     dst_address /* changed member */ );
456                   sum0 =
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);
461                 }
462             }
463
464           switch (ses0->state)
465             {
466             case SNAT_SESSION_UDP_ACTIVE:
467               ses0->expire = now + sm->udp_timeout;
468               break;
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;
474               break;
475             case SNAT_SESSION_TCP_ESTABLISHED:
476               ses0->expire = now + sm->tcp_established_timeout;
477               break;
478             }
479
480         trace0:
481           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
482                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
483             {
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;
489               if (ses0)
490                 t->session_index = ses0 - dm0->sessions;
491             }
492
493           pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
494
495           ip1 = vlib_buffer_get_current (b1);
496           udp1 = ip4_next_header (ip1);
497           tcp1 = (tcp_header_t *) udp1;
498
499           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
500
501           if (PREDICT_FALSE (ip1->ttl == 1))
502             {
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,
506                                            0);
507               next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
508               goto trace1;
509             }
510
511           proto1 = ip_proto_to_nat_proto (ip1->protocol);
512
513           if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
514             {
515               rx_fib_index1 =
516                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
517               icmp1 = (icmp46_header_t *) udp1;
518
519               next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
520                                    rx_fib_index1, node, next1, thread_index,
521                                    &ses1, &dm1);
522               goto trace1;
523             }
524
525           dm1 = snat_det_map_by_user (sm, &ip1->src_address);
526           if (PREDICT_FALSE (!dm1))
527             {
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];
532               goto trace1;
533             }
534
535           snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
536
537           key1.ext_host_addr = ip1->dst_address;
538           key1.ext_host_port = tcp1->dst;
539
540           ses1 =
541             snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
542           if (PREDICT_FALSE (!ses1))
543             {
544               for (i1 = 0; i1 < dm1->ports_per_host; i1++)
545                 {
546                   key1.out_port = clib_host_to_net_u16 (lo_port1 +
547                                                         ((i1 +
548                                                           clib_net_to_host_u16
549                                                           (tcp1->src)) %
550                                                          dm1->
551                                                          ports_per_host));
552
553                   if (snat_det_get_ses_by_out
554                       (dm1, &ip1->src_address, key1.as_u64))
555                     continue;
556
557                   ses1 =
558                     snat_det_ses_create (thread_index, dm1, &ip1->src_address,
559                                          tcp1->src, &key1);
560                   break;
561                 }
562               if (PREDICT_FALSE (!ses1))
563                 {
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,
569                                                0);
570                   next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
571                   goto trace1;
572                 }
573             }
574
575           old_port1 = udp1->src_port;
576           udp1->src_port = new_port1 = ses1->out.out_port;
577
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;
581
582           sum1 = ip1->checksum;
583           sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
584                                  ip4_header_t,
585                                  src_address /* changed member */ );
586           ip1->checksum = ip_csum_fold (sum1);
587
588           if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
589             {
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;
607
608               sum1 = tcp1->checksum;
609               sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
610                                      ip4_header_t,
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);
617             }
618           else
619             {
620               ses1->state = SNAT_SESSION_UDP_ACTIVE;
621
622               if (PREDICT_FALSE (udp1->checksum))
623                 {
624                   sum1 = udp1->checksum;
625                   sum1 =
626                     ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
627                                     ip4_header_t,
628                                     dst_address /* changed member */ );
629                   sum1 =
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);
634                 }
635             }
636
637           switch (ses1->state)
638             {
639             case SNAT_SESSION_UDP_ACTIVE:
640               ses1->expire = now + sm->udp_timeout;
641               break;
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;
647               break;
648             case SNAT_SESSION_TCP_ESTABLISHED:
649               ses1->expire = now + sm->tcp_established_timeout;
650               break;
651             }
652
653         trace1:
654           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
655                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
656             {
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;
662               if (ses1)
663                 t->session_index = ses1 - dm1->sessions;
664             }
665
666           pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
667
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);
672         }
673
674       while (n_left_from > 0 && n_left_to_next > 0)
675         {
676           u32 bi0;
677           vlib_buffer_t *b0;
678           u32 next0;
679           u32 sw_if_index0;
680           ip4_header_t *ip0;
681           ip_csum_t sum0;
682           ip4_address_t new_addr0, old_addr0;
683           u16 old_port0, new_port0, lo_port0, i0;
684           udp_header_t *udp0;
685           tcp_header_t *tcp0;
686           u32 proto0;
687           snat_det_out_key_t key0;
688           snat_det_map_t *dm0;
689           snat_det_session_t *ses0 = 0;
690           u32 rx_fib_index0;
691           icmp46_header_t *icmp0;
692
693           /* speculatively enqueue b0 to the current next frame */
694           bi0 = from[0];
695           to_next[0] = bi0;
696           from += 1;
697           to_next += 1;
698           n_left_from -= 1;
699           n_left_to_next -= 1;
700
701           b0 = vlib_get_buffer (vm, bi0);
702           next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
703
704           ip0 = vlib_buffer_get_current (b0);
705           udp0 = ip4_next_header (ip0);
706           tcp0 = (tcp_header_t *) udp0;
707
708           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
709
710           if (PREDICT_FALSE (ip0->ttl == 1))
711             {
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,
715                                            0);
716               next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
717               goto trace00;
718             }
719
720           proto0 = ip_proto_to_nat_proto (ip0->protocol);
721
722           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
723             {
724               rx_fib_index0 =
725                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
726               icmp0 = (icmp46_header_t *) udp0;
727
728               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
729                                    rx_fib_index0, node, next0, thread_index,
730                                    &ses0, &dm0);
731               goto trace00;
732             }
733
734           dm0 = snat_det_map_by_user (sm, &ip0->src_address);
735           if (PREDICT_FALSE (!dm0))
736             {
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];
741               goto trace00;
742             }
743
744           snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
745
746           key0.ext_host_addr = ip0->dst_address;
747           key0.ext_host_port = tcp0->dst;
748
749           ses0 =
750             snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
751           if (PREDICT_FALSE (!ses0))
752             {
753               for (i0 = 0; i0 < dm0->ports_per_host; i0++)
754                 {
755                   key0.out_port = clib_host_to_net_u16 (lo_port0 +
756                                                         ((i0 +
757                                                           clib_net_to_host_u16
758                                                           (tcp0->src)) %
759                                                          dm0->
760                                                          ports_per_host));
761
762                   if (snat_det_get_ses_by_out
763                       (dm0, &ip0->src_address, key0.as_u64))
764                     continue;
765
766                   ses0 =
767                     snat_det_ses_create (thread_index, dm0, &ip0->src_address,
768                                          tcp0->src, &key0);
769                   break;
770                 }
771               if (PREDICT_FALSE (!ses0))
772                 {
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,
778                                                0);
779                   next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
780                   goto trace00;
781                 }
782             }
783
784           old_port0 = udp0->src_port;
785           udp0->src_port = new_port0 = ses0->out.out_port;
786
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;
790
791           sum0 = ip0->checksum;
792           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
793                                  ip4_header_t,
794                                  src_address /* changed member */ );
795           ip0->checksum = ip_csum_fold (sum0);
796
797           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
798             {
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;
816
817               sum0 = tcp0->checksum;
818               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
819                                      ip4_header_t,
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);
826             }
827           else
828             {
829               ses0->state = SNAT_SESSION_UDP_ACTIVE;
830
831               if (PREDICT_FALSE (udp0->checksum))
832                 {
833                   sum0 = udp0->checksum;
834                   sum0 =
835                     ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
836                                     ip4_header_t,
837                                     dst_address /* changed member */ );
838                   sum0 =
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);
843                 }
844             }
845
846           switch (ses0->state)
847             {
848             case SNAT_SESSION_UDP_ACTIVE:
849               ses0->expire = now + sm->udp_timeout;
850               break;
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;
856               break;
857             case SNAT_SESSION_TCP_ESTABLISHED:
858               ses0->expire = now + sm->tcp_established_timeout;
859               break;
860             }
861
862         trace00:
863           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
864                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
865             {
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;
871               if (ses0)
872                 t->session_index = ses0 - dm0->sessions;
873             }
874
875           pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
876
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,
880                                            bi0, next0);
881         }
882
883       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
884     }
885
886   vlib_node_increment_counter (vm, sm->det_in2out_node_index,
887                                NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
888                                pkts_processed);
889   return frame->n_vectors;
890 }
891
892 /* *INDENT-OFF* */
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 */
902   .next_nodes = {
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",
906   },
907 };
908 /* *INDENT-ON* */
909
910 /*
911  * fd.io coding-style-patch-verification: ON
912  *
913  * Local Variables:
914  * eval: (c-set-style "gnu")
915  * End:
916  */