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