nat: fix per thread data vlib_main_t usage
[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
30 typedef struct
31 {
32   u32 sw_if_index;
33   u32 next_index;
34   u32 session_index;
35 } nat_det_in2out_trace_t;
36
37 typedef enum
38 {
39   NAT_DET_IN2OUT_NEXT_LOOKUP,
40   NAT_DET_IN2OUT_NEXT_DROP,
41   NAT_DET_IN2OUT_NEXT_ICMP_ERROR,
42   NAT_DET_IN2OUT_N_NEXT,
43 } nat_det_in2out_next_t;
44
45 #define foreach_nat_det_in2out_error                    \
46 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")         \
47 _(NO_TRANSLATION, "No translation")                     \
48 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
49 _(OUT_OF_PORTS, "Out of ports")                         \
50 _(IN2OUT_PACKETS, "Good in2out packets processed")
51
52 typedef enum
53 {
54 #define _(sym,str) NAT_DET_IN2OUT_ERROR_##sym,
55   foreach_nat_det_in2out_error
56 #undef _
57     NAT_DET_IN2OUT_N_ERROR,
58 } nat_det_in2out_error_t;
59
60 static char *nat_det_in2out_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_nat_det_in2out_error
63 #undef _
64 };
65
66 static u8 *
67 format_nat_det_in2out_trace (u8 * s, va_list * args)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71   nat_det_in2out_trace_t *t = va_arg (*args, nat_det_in2out_trace_t *);
72
73   s = format (s, "NAT_DET_IN2OUT: sw_if_index %d, next index %d, session %d",
74               t->sw_if_index, t->next_index, t->session_index);
75
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_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
97                        u32 thread_index, vlib_buffer_t * b0,
98                        ip4_header_t * ip0, u8 * p_proto,
99                        snat_session_key_t * p_value, u8 * p_dont_translate,
100                        void *d, void *e)
101 {
102   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
103   icmp46_header_t *icmp0;
104   u32 sw_if_index0;
105   u32 rx_fib_index0;
106   u8 protocol;
107   snat_det_out_key_t key0;
108   u8 dont_translate = 0;
109   u32 next0 = ~0;
110   icmp_echo_header_t *echo0, *inner_echo0 = 0;
111   ip4_header_t *inner_ip0;
112   void *l4_header = 0;
113   icmp46_header_t *inner_icmp0;
114   snat_det_map_t *dm0 = 0;
115   ip4_address_t new_addr0;
116   u16 lo_port0, i0;
117   snat_det_session_t *ses0 = 0;
118   ip4_address_t in_addr;
119   u16 in_port;
120
121   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
122   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
123   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
124   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
125
126   if (!icmp_type_is_error_message
127       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
128     {
129       protocol = SNAT_PROTOCOL_ICMP;
130       in_addr = ip0->src_address;
131       in_port = vnet_buffer (b0)->ip.reass.l4_src_port;
132     }
133   else
134     {
135       /* if error message, then it's not fragmented and we can access it */
136       inner_ip0 = (ip4_header_t *) (echo0 + 1);
137       l4_header = ip4_next_header (inner_ip0);
138       protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
139       in_addr = inner_ip0->dst_address;
140       switch (protocol)
141         {
142         case SNAT_PROTOCOL_ICMP:
143           inner_icmp0 = (icmp46_header_t *) l4_header;
144           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
145           in_port = inner_echo0->identifier;
146           break;
147         case SNAT_PROTOCOL_UDP:
148         case SNAT_PROTOCOL_TCP:
149           in_port = ((tcp_udp_header_t *) l4_header)->dst_port;
150           break;
151         default:
152           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
153           next0 = NAT_DET_IN2OUT_NEXT_DROP;
154           goto out;
155         }
156     }
157
158   dm0 = snat_det_map_by_user (sm, &in_addr);
159   if (PREDICT_FALSE (!dm0))
160     {
161       nat_log_info ("no match for internal host %U",
162                     format_ip4_address, &in_addr);
163       if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
164                                                   IP_PROTOCOL_ICMP,
165                                                   rx_fib_index0)))
166         {
167           dont_translate = 1;
168           goto out;
169         }
170       next0 = NAT_DET_IN2OUT_NEXT_DROP;
171       b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
172       goto out;
173     }
174
175   snat_det_forward (dm0, &in_addr, &new_addr0, &lo_port0);
176
177   key0.ext_host_addr = ip0->dst_address;
178   key0.ext_host_port = 0;
179
180   ses0 = snat_det_find_ses_by_in (dm0, &in_addr, in_port, key0);
181   if (PREDICT_FALSE (!ses0))
182     {
183       if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
184                                                   IP_PROTOCOL_ICMP,
185                                                   rx_fib_index0)))
186         {
187           dont_translate = 1;
188           goto out;
189         }
190       if (icmp0->type != ICMP4_echo_request)
191         {
192           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
193           next0 = NAT_DET_IN2OUT_NEXT_DROP;
194           goto out;
195         }
196       for (i0 = 0; i0 < dm0->ports_per_host; i0++)
197         {
198           key0.out_port = clib_host_to_net_u16 (lo_port0 +
199                                                 ((i0 +
200                                                   clib_net_to_host_u16
201                                                   (echo0->identifier)) %
202                                                  dm0->ports_per_host));
203
204           if (snat_det_get_ses_by_out (dm0, &in_addr, key0.as_u64))
205             continue;
206
207           ses0 =
208             snat_det_ses_create (thread_index, dm0,
209                                  &in_addr, echo0->identifier, &key0);
210           break;
211         }
212       if (PREDICT_FALSE (!ses0))
213         {
214           next0 = NAT_DET_IN2OUT_NEXT_DROP;
215           b0->error = node->errors[NAT_DET_IN2OUT_ERROR_OUT_OF_PORTS];
216           goto out;
217         }
218     }
219
220   if (PREDICT_FALSE
221       (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
222        && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
223                                        reass.icmp_type_or_tcp_flags)))
224     {
225       b0->error = node->errors[NAT_DET_IN2OUT_ERROR_BAD_ICMP_TYPE];
226       next0 = NAT_DET_IN2OUT_NEXT_DROP;
227       goto out;
228     }
229
230   u32 now = (u32) vlib_time_now (tsm->vlib_main);
231
232   ses0->state = SNAT_SESSION_ICMP_ACTIVE;
233   ses0->expire = now + sm->icmp_timeout;
234
235 out:
236   *p_proto = protocol;
237   if (ses0)
238     {
239       p_value->addr = new_addr0;
240       p_value->fib_index = sm->outside_fib_index;
241       p_value->port = ses0->out.out_port;
242     }
243   *p_dont_translate = dont_translate;
244   if (d)
245     *(snat_det_session_t **) d = ses0;
246   if (e)
247     *(snat_det_map_t **) e = dm0;
248   return next0;
249 }
250 #endif
251
252 VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm,
253                                      vlib_node_runtime_t * node,
254                                      vlib_frame_t * frame)
255 {
256   u32 n_left_from, *from, *to_next;
257   nat_det_in2out_next_t next_index;
258   u32 pkts_processed = 0;
259   snat_main_t *sm = &snat_main;
260   u32 now = (u32) vlib_time_now (vm);
261   u32 thread_index = vm->thread_index;
262
263   from = vlib_frame_vector_args (frame);
264   n_left_from = frame->n_vectors;
265   next_index = node->cached_next_index;
266
267   while (n_left_from > 0)
268     {
269       u32 n_left_to_next;
270
271       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
272
273       while (n_left_from >= 4 && n_left_to_next >= 2)
274         {
275           u32 bi0, bi1;
276           vlib_buffer_t *b0, *b1;
277           u32 next0, next1;
278           u32 sw_if_index0, sw_if_index1;
279           ip4_header_t *ip0, *ip1;
280           ip_csum_t sum0, sum1;
281           ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
282           u16 old_port0, new_port0, lo_port0, i0;
283           u16 old_port1, new_port1, lo_port1, i1;
284           udp_header_t *udp0, *udp1;
285           tcp_header_t *tcp0, *tcp1;
286           u32 proto0, proto1;
287           snat_det_out_key_t key0, key1;
288           snat_det_map_t *dm0, *dm1;
289           snat_det_session_t *ses0 = 0, *ses1 = 0;
290           u32 rx_fib_index0, rx_fib_index1;
291           icmp46_header_t *icmp0, *icmp1;
292
293           /* Prefetch next iteration. */
294           {
295             vlib_buffer_t *p2, *p3;
296
297             p2 = vlib_get_buffer (vm, from[2]);
298             p3 = vlib_get_buffer (vm, from[3]);
299
300             vlib_prefetch_buffer_header (p2, LOAD);
301             vlib_prefetch_buffer_header (p3, LOAD);
302
303             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
304             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
305           }
306
307           /* speculatively enqueue b0 and b1 to the current next frame */
308           to_next[0] = bi0 = from[0];
309           to_next[1] = bi1 = from[1];
310           from += 2;
311           to_next += 2;
312           n_left_from -= 2;
313           n_left_to_next -= 2;
314
315           b0 = vlib_get_buffer (vm, bi0);
316           b1 = vlib_get_buffer (vm, bi1);
317
318           next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
319           next1 = NAT_DET_IN2OUT_NEXT_LOOKUP;
320
321           ip0 = vlib_buffer_get_current (b0);
322           udp0 = ip4_next_header (ip0);
323           tcp0 = (tcp_header_t *) udp0;
324
325           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
326
327           if (PREDICT_FALSE (ip0->ttl == 1))
328             {
329               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
330               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
331                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
332                                            0);
333               next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
334               goto trace0;
335             }
336
337           proto0 = ip_proto_to_snat_proto (ip0->protocol);
338
339           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
340             {
341               rx_fib_index0 =
342                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
343               icmp0 = (icmp46_header_t *) udp0;
344
345               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
346                                    rx_fib_index0, node, next0, thread_index,
347                                    &ses0, &dm0);
348               goto trace0;
349             }
350
351           dm0 = snat_det_map_by_user (sm, &ip0->src_address);
352           if (PREDICT_FALSE (!dm0))
353             {
354               nat_log_info ("no match for internal host %U",
355                             format_ip4_address, &ip0->src_address);
356               next0 = NAT_DET_IN2OUT_NEXT_DROP;
357               b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
358               goto trace0;
359             }
360
361           snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
362
363           key0.ext_host_addr = ip0->dst_address;
364           key0.ext_host_port = tcp0->dst;
365
366           ses0 =
367             snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
368           if (PREDICT_FALSE (!ses0))
369             {
370               for (i0 = 0; i0 < dm0->ports_per_host; i0++)
371                 {
372                   key0.out_port = clib_host_to_net_u16 (lo_port0 +
373                                                         ((i0 +
374                                                           clib_net_to_host_u16
375                                                           (tcp0->src)) %
376                                                          dm0->
377                                                          ports_per_host));
378
379                   if (snat_det_get_ses_by_out
380                       (dm0, &ip0->src_address, key0.as_u64))
381                     continue;
382
383                   ses0 =
384                     snat_det_ses_create (thread_index, dm0, &ip0->src_address,
385                                          tcp0->src, &key0);
386                   break;
387                 }
388               if (PREDICT_FALSE (!ses0))
389                 {
390                   /* too many sessions for user, send ICMP error packet */
391                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
392                   icmp4_error_set_vnet_buffer (b0,
393                                                ICMP4_destination_unreachable,
394                                                ICMP4_destination_unreachable_destination_unreachable_host,
395                                                0);
396                   next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
397                   goto trace0;
398                 }
399             }
400
401           old_port0 = udp0->src_port;
402           udp0->src_port = new_port0 = ses0->out.out_port;
403
404           old_addr0.as_u32 = ip0->src_address.as_u32;
405           ip0->src_address.as_u32 = new_addr0.as_u32;
406           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
407
408           sum0 = ip0->checksum;
409           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
410                                  ip4_header_t,
411                                  src_address /* changed member */ );
412           ip0->checksum = ip_csum_fold (sum0);
413
414           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
415             {
416               if (tcp0->flags & TCP_FLAG_SYN)
417                 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
418               else if (tcp0->flags & TCP_FLAG_ACK
419                        && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
420                 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
421               else if (tcp0->flags & TCP_FLAG_FIN
422                        && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
423                 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
424               else if (tcp0->flags & TCP_FLAG_ACK
425                        && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
426                 snat_det_ses_close (dm0, ses0);
427               else if (tcp0->flags & TCP_FLAG_FIN
428                        && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
429                 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
430               else if (tcp0->flags == 0
431                        && ses0->state == SNAT_SESSION_UNKNOWN)
432                 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
433
434               sum0 = tcp0->checksum;
435               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
436                                      ip4_header_t,
437                                      dst_address /* changed member */ );
438               sum0 = ip_csum_update (sum0, old_port0, new_port0,
439                                      ip4_header_t /* cheat */ ,
440                                      length /* changed member */ );
441               mss_clamping (sm, tcp0, &sum0);
442               tcp0->checksum = ip_csum_fold (sum0);
443             }
444           else
445             {
446               ses0->state = SNAT_SESSION_UDP_ACTIVE;
447
448               if (PREDICT_FALSE (udp0->checksum))
449                 {
450                   sum0 = udp0->checksum;
451                   sum0 =
452                     ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
453                                     ip4_header_t,
454                                     dst_address /* changed member */ );
455                   sum0 =
456                     ip_csum_update (sum0, old_port0, new_port0,
457                                     ip4_header_t /* cheat */ ,
458                                     length /* changed member */ );
459                   udp0->checksum = ip_csum_fold (sum0);
460                 }
461             }
462
463           switch (ses0->state)
464             {
465             case SNAT_SESSION_UDP_ACTIVE:
466               ses0->expire = now + sm->udp_timeout;
467               break;
468             case SNAT_SESSION_TCP_SYN_SENT:
469             case SNAT_SESSION_TCP_FIN_WAIT:
470             case SNAT_SESSION_TCP_CLOSE_WAIT:
471             case SNAT_SESSION_TCP_LAST_ACK:
472               ses0->expire = now + sm->tcp_transitory_timeout;
473               break;
474             case SNAT_SESSION_TCP_ESTABLISHED:
475               ses0->expire = now + sm->tcp_established_timeout;
476               break;
477             }
478
479         trace0:
480           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
481                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
482             {
483               nat_det_in2out_trace_t *t =
484                 vlib_add_trace (vm, node, b0, sizeof (*t));
485               t->sw_if_index = sw_if_index0;
486               t->next_index = next0;
487               t->session_index = ~0;
488               if (ses0)
489                 t->session_index = ses0 - dm0->sessions;
490             }
491
492           pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
493
494           ip1 = vlib_buffer_get_current (b1);
495           udp1 = ip4_next_header (ip1);
496           tcp1 = (tcp_header_t *) udp1;
497
498           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
499
500           if (PREDICT_FALSE (ip1->ttl == 1))
501             {
502               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
503               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
504                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
505                                            0);
506               next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
507               goto trace1;
508             }
509
510           proto1 = ip_proto_to_snat_proto (ip1->protocol);
511
512           if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
513             {
514               rx_fib_index1 =
515                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index1);
516               icmp1 = (icmp46_header_t *) udp1;
517
518               next1 = icmp_in2out (sm, b1, ip1, icmp1, sw_if_index1,
519                                    rx_fib_index1, node, next1, thread_index,
520                                    &ses1, &dm1);
521               goto trace1;
522             }
523
524           dm1 = snat_det_map_by_user (sm, &ip1->src_address);
525           if (PREDICT_FALSE (!dm1))
526             {
527               nat_log_info ("no match for internal host %U",
528                             format_ip4_address, &ip0->src_address);
529               next1 = NAT_DET_IN2OUT_NEXT_DROP;
530               b1->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
531               goto trace1;
532             }
533
534           snat_det_forward (dm1, &ip1->src_address, &new_addr1, &lo_port1);
535
536           key1.ext_host_addr = ip1->dst_address;
537           key1.ext_host_port = tcp1->dst;
538
539           ses1 =
540             snat_det_find_ses_by_in (dm1, &ip1->src_address, tcp1->src, key1);
541           if (PREDICT_FALSE (!ses1))
542             {
543               for (i1 = 0; i1 < dm1->ports_per_host; i1++)
544                 {
545                   key1.out_port = clib_host_to_net_u16 (lo_port1 +
546                                                         ((i1 +
547                                                           clib_net_to_host_u16
548                                                           (tcp1->src)) %
549                                                          dm1->
550                                                          ports_per_host));
551
552                   if (snat_det_get_ses_by_out
553                       (dm1, &ip1->src_address, key1.as_u64))
554                     continue;
555
556                   ses1 =
557                     snat_det_ses_create (thread_index, dm1, &ip1->src_address,
558                                          tcp1->src, &key1);
559                   break;
560                 }
561               if (PREDICT_FALSE (!ses1))
562                 {
563                   /* too many sessions for user, send ICMP error packet */
564                   vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
565                   icmp4_error_set_vnet_buffer (b1,
566                                                ICMP4_destination_unreachable,
567                                                ICMP4_destination_unreachable_destination_unreachable_host,
568                                                0);
569                   next1 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
570                   goto trace1;
571                 }
572             }
573
574           old_port1 = udp1->src_port;
575           udp1->src_port = new_port1 = ses1->out.out_port;
576
577           old_addr1.as_u32 = ip1->src_address.as_u32;
578           ip1->src_address.as_u32 = new_addr1.as_u32;
579           vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
580
581           sum1 = ip1->checksum;
582           sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
583                                  ip4_header_t,
584                                  src_address /* changed member */ );
585           ip1->checksum = ip_csum_fold (sum1);
586
587           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
588             {
589               if (tcp1->flags & TCP_FLAG_SYN)
590                 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
591               else if (tcp1->flags & TCP_FLAG_ACK
592                        && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
593                 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
594               else if (tcp1->flags & TCP_FLAG_FIN
595                        && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
596                 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
597               else if (tcp1->flags & TCP_FLAG_ACK
598                        && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
599                 snat_det_ses_close (dm1, ses1);
600               else if (tcp1->flags & TCP_FLAG_FIN
601                        && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
602                 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
603               else if (tcp1->flags == 0
604                        && ses1->state == SNAT_SESSION_UNKNOWN)
605                 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
606
607               sum1 = tcp1->checksum;
608               sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
609                                      ip4_header_t,
610                                      dst_address /* changed member */ );
611               sum1 = ip_csum_update (sum1, old_port1, new_port1,
612                                      ip4_header_t /* cheat */ ,
613                                      length /* changed member */ );
614               mss_clamping (sm, tcp1, &sum1);
615               tcp1->checksum = ip_csum_fold (sum1);
616             }
617           else
618             {
619               ses1->state = SNAT_SESSION_UDP_ACTIVE;
620
621               if (PREDICT_FALSE (udp1->checksum))
622                 {
623                   sum1 = udp1->checksum;
624                   sum1 =
625                     ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
626                                     ip4_header_t,
627                                     dst_address /* changed member */ );
628                   sum1 =
629                     ip_csum_update (sum1, old_port1, new_port1,
630                                     ip4_header_t /* cheat */ ,
631                                     length /* changed member */ );
632                   udp1->checksum = ip_csum_fold (sum1);
633                 }
634             }
635
636           switch (ses1->state)
637             {
638             case SNAT_SESSION_UDP_ACTIVE:
639               ses1->expire = now + sm->udp_timeout;
640               break;
641             case SNAT_SESSION_TCP_SYN_SENT:
642             case SNAT_SESSION_TCP_FIN_WAIT:
643             case SNAT_SESSION_TCP_CLOSE_WAIT:
644             case SNAT_SESSION_TCP_LAST_ACK:
645               ses1->expire = now + sm->tcp_transitory_timeout;
646               break;
647             case SNAT_SESSION_TCP_ESTABLISHED:
648               ses1->expire = now + sm->tcp_established_timeout;
649               break;
650             }
651
652         trace1:
653           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
654                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
655             {
656               nat_det_in2out_trace_t *t =
657                 vlib_add_trace (vm, node, b1, sizeof (*t));
658               t->sw_if_index = sw_if_index1;
659               t->next_index = next1;
660               t->session_index = ~0;
661               if (ses1)
662                 t->session_index = ses1 - dm1->sessions;
663             }
664
665           pkts_processed += next1 != NAT_DET_IN2OUT_NEXT_DROP;
666
667           /* verify speculative enqueues, maybe switch current next frame */
668           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
669                                            to_next, n_left_to_next,
670                                            bi0, bi1, next0, next1);
671         }
672
673       while (n_left_from > 0 && n_left_to_next > 0)
674         {
675           u32 bi0;
676           vlib_buffer_t *b0;
677           u32 next0;
678           u32 sw_if_index0;
679           ip4_header_t *ip0;
680           ip_csum_t sum0;
681           ip4_address_t new_addr0, old_addr0;
682           u16 old_port0, new_port0, lo_port0, i0;
683           udp_header_t *udp0;
684           tcp_header_t *tcp0;
685           u32 proto0;
686           snat_det_out_key_t key0;
687           snat_det_map_t *dm0;
688           snat_det_session_t *ses0 = 0;
689           u32 rx_fib_index0;
690           icmp46_header_t *icmp0;
691
692           /* speculatively enqueue b0 to the current next frame */
693           bi0 = from[0];
694           to_next[0] = bi0;
695           from += 1;
696           to_next += 1;
697           n_left_from -= 1;
698           n_left_to_next -= 1;
699
700           b0 = vlib_get_buffer (vm, bi0);
701           next0 = NAT_DET_IN2OUT_NEXT_LOOKUP;
702
703           ip0 = vlib_buffer_get_current (b0);
704           udp0 = ip4_next_header (ip0);
705           tcp0 = (tcp_header_t *) udp0;
706
707           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
708
709           if (PREDICT_FALSE (ip0->ttl == 1))
710             {
711               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
712               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
713                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
714                                            0);
715               next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
716               goto trace00;
717             }
718
719           proto0 = ip_proto_to_snat_proto (ip0->protocol);
720
721           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
722             {
723               rx_fib_index0 =
724                 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
725               icmp0 = (icmp46_header_t *) udp0;
726
727               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
728                                    rx_fib_index0, node, next0, thread_index,
729                                    &ses0, &dm0);
730               goto trace00;
731             }
732
733           dm0 = snat_det_map_by_user (sm, &ip0->src_address);
734           if (PREDICT_FALSE (!dm0))
735             {
736               nat_log_info ("no match for internal host %U",
737                             format_ip4_address, &ip0->src_address);
738               next0 = NAT_DET_IN2OUT_NEXT_DROP;
739               b0->error = node->errors[NAT_DET_IN2OUT_ERROR_NO_TRANSLATION];
740               goto trace00;
741             }
742
743           snat_det_forward (dm0, &ip0->src_address, &new_addr0, &lo_port0);
744
745           key0.ext_host_addr = ip0->dst_address;
746           key0.ext_host_port = tcp0->dst;
747
748           ses0 =
749             snat_det_find_ses_by_in (dm0, &ip0->src_address, tcp0->src, key0);
750           if (PREDICT_FALSE (!ses0))
751             {
752               for (i0 = 0; i0 < dm0->ports_per_host; i0++)
753                 {
754                   key0.out_port = clib_host_to_net_u16 (lo_port0 +
755                                                         ((i0 +
756                                                           clib_net_to_host_u16
757                                                           (tcp0->src)) %
758                                                          dm0->
759                                                          ports_per_host));
760
761                   if (snat_det_get_ses_by_out
762                       (dm0, &ip0->src_address, key0.as_u64))
763                     continue;
764
765                   ses0 =
766                     snat_det_ses_create (thread_index, dm0, &ip0->src_address,
767                                          tcp0->src, &key0);
768                   break;
769                 }
770               if (PREDICT_FALSE (!ses0))
771                 {
772                   /* too many sessions for user, send ICMP error packet */
773                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
774                   icmp4_error_set_vnet_buffer (b0,
775                                                ICMP4_destination_unreachable,
776                                                ICMP4_destination_unreachable_destination_unreachable_host,
777                                                0);
778                   next0 = NAT_DET_IN2OUT_NEXT_ICMP_ERROR;
779                   goto trace00;
780                 }
781             }
782
783           old_port0 = udp0->src_port;
784           udp0->src_port = new_port0 = ses0->out.out_port;
785
786           old_addr0.as_u32 = ip0->src_address.as_u32;
787           ip0->src_address.as_u32 = new_addr0.as_u32;
788           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
789
790           sum0 = ip0->checksum;
791           sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
792                                  ip4_header_t,
793                                  src_address /* changed member */ );
794           ip0->checksum = ip_csum_fold (sum0);
795
796           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
797             {
798               if (tcp0->flags & TCP_FLAG_SYN)
799                 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
800               else if (tcp0->flags & TCP_FLAG_ACK
801                        && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
802                 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
803               else if (tcp0->flags & TCP_FLAG_FIN
804                        && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
805                 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
806               else if (tcp0->flags & TCP_FLAG_ACK
807                        && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
808                 snat_det_ses_close (dm0, ses0);
809               else if (tcp0->flags & TCP_FLAG_FIN
810                        && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
811                 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
812               else if (tcp0->flags == 0
813                        && ses0->state == SNAT_SESSION_UNKNOWN)
814                 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
815
816               sum0 = tcp0->checksum;
817               sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
818                                      ip4_header_t,
819                                      dst_address /* changed member */ );
820               sum0 = ip_csum_update (sum0, old_port0, new_port0,
821                                      ip4_header_t /* cheat */ ,
822                                      length /* changed member */ );
823               mss_clamping (sm, tcp0, &sum0);
824               tcp0->checksum = ip_csum_fold (sum0);
825             }
826           else
827             {
828               ses0->state = SNAT_SESSION_UDP_ACTIVE;
829
830               if (PREDICT_FALSE (udp0->checksum))
831                 {
832                   sum0 = udp0->checksum;
833                   sum0 =
834                     ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
835                                     ip4_header_t,
836                                     dst_address /* changed member */ );
837                   sum0 =
838                     ip_csum_update (sum0, old_port0, new_port0,
839                                     ip4_header_t /* cheat */ ,
840                                     length /* changed member */ );
841                   udp0->checksum = ip_csum_fold (sum0);
842                 }
843             }
844
845           switch (ses0->state)
846             {
847             case SNAT_SESSION_UDP_ACTIVE:
848               ses0->expire = now + sm->udp_timeout;
849               break;
850             case SNAT_SESSION_TCP_SYN_SENT:
851             case SNAT_SESSION_TCP_FIN_WAIT:
852             case SNAT_SESSION_TCP_CLOSE_WAIT:
853             case SNAT_SESSION_TCP_LAST_ACK:
854               ses0->expire = now + sm->tcp_transitory_timeout;
855               break;
856             case SNAT_SESSION_TCP_ESTABLISHED:
857               ses0->expire = now + sm->tcp_established_timeout;
858               break;
859             }
860
861         trace00:
862           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
863                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
864             {
865               nat_det_in2out_trace_t *t =
866                 vlib_add_trace (vm, node, b0, sizeof (*t));
867               t->sw_if_index = sw_if_index0;
868               t->next_index = next0;
869               t->session_index = ~0;
870               if (ses0)
871                 t->session_index = ses0 - dm0->sessions;
872             }
873
874           pkts_processed += next0 != NAT_DET_IN2OUT_NEXT_DROP;
875
876           /* verify speculative enqueue, maybe switch current next frame */
877           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
878                                            to_next, n_left_to_next,
879                                            bi0, next0);
880         }
881
882       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
883     }
884
885   vlib_node_increment_counter (vm, sm->det_in2out_node_index,
886                                NAT_DET_IN2OUT_ERROR_IN2OUT_PACKETS,
887                                pkts_processed);
888   return frame->n_vectors;
889 }
890
891 /* *INDENT-OFF* */
892 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
893   .name = "nat44-det-in2out",
894   .vector_size = sizeof (u32),
895   .format_trace = format_nat_det_in2out_trace,
896   .type = VLIB_NODE_TYPE_INTERNAL,
897   .n_errors = ARRAY_LEN(nat_det_in2out_error_strings),
898   .error_strings = nat_det_in2out_error_strings,
899   .n_next_nodes = NAT_DET_IN2OUT_N_NEXT,
900   /* edit / add dispositions here */
901   .next_nodes = {
902     [NAT_DET_IN2OUT_NEXT_DROP] = "error-drop",
903     [NAT_DET_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
904     [NAT_DET_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
905   },
906 };
907 /* *INDENT-ON* */
908
909 /*
910  * fd.io coding-style-patch-verification: ON
911  *
912  * Local Variables:
913  * eval: (c-set-style "gnu")
914  * End:
915  */