NAT: VPP-1537 IPFIX per worker processing
[vpp.git] / src / plugins / nat / in2out.c
1 /*
2  * Copyright (c) 2016 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 NAT44 inside to outside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <nat/nat.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_syslog.h>
32
33 #include <vppinfra/hash.h>
34 #include <vppinfra/error.h>
35 #include <vppinfra/elog.h>
36
37 typedef struct
38 {
39   u32 sw_if_index;
40   u32 next_index;
41   u32 session_index;
42   u32 is_slow_path;
43 } snat_in2out_trace_t;
44
45 /* packet trace format function */
46 static u8 *
47 format_snat_in2out_trace (u8 * s, va_list * args)
48 {
49   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
50   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
51   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
52   char *tag;
53
54   tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
55
56   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
57               t->sw_if_index, t->next_index, t->session_index);
58
59   return s;
60 }
61
62 static u8 *
63 format_snat_in2out_fast_trace (u8 * s, va_list * args)
64 {
65   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
66   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
67   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
68
69   s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
70               t->sw_if_index, t->next_index);
71
72   return s;
73 }
74
75 vlib_node_registration_t snat_in2out_node;
76 vlib_node_registration_t snat_in2out_slowpath_node;
77 vlib_node_registration_t snat_in2out_fast_node;
78 vlib_node_registration_t snat_in2out_output_node;
79 vlib_node_registration_t snat_in2out_output_slowpath_node;
80 vlib_node_registration_t nat44_in2out_reass_node;
81
82 #define foreach_snat_in2out_error                       \
83 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
84 _(IN2OUT_PACKETS, "good in2out packets processed")      \
85 _(OUT_OF_PORTS, "out of ports")                         \
86 _(BAD_OUTSIDE_FIB, "outside VRF ID not found")          \
87 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
88 _(NO_TRANSLATION, "no translation")                     \
89 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
90 _(DROP_FRAGMENT, "drop fragment")                       \
91 _(MAX_REASS, "maximum reassemblies exceeded")           \
92 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
93 _(TCP_PACKETS, "TCP packets")                           \
94 _(UDP_PACKETS, "UDP packets")                           \
95 _(ICMP_PACKETS, "ICMP packets")                         \
96 _(OTHER_PACKETS, "other protocol packets")              \
97 _(FRAGMENTS, "fragments")                               \
98 _(CACHED_FRAGMENTS, "cached fragments")                 \
99 _(PROCESSED_FRAGMENTS, "processed fragments")
100
101 typedef enum
102 {
103 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
104   foreach_snat_in2out_error
105 #undef _
106     SNAT_IN2OUT_N_ERROR,
107 } snat_in2out_error_t;
108
109 static char *snat_in2out_error_strings[] = {
110 #define _(sym,string) string,
111   foreach_snat_in2out_error
112 #undef _
113 };
114
115 typedef enum
116 {
117   SNAT_IN2OUT_NEXT_LOOKUP,
118   SNAT_IN2OUT_NEXT_DROP,
119   SNAT_IN2OUT_NEXT_ICMP_ERROR,
120   SNAT_IN2OUT_NEXT_SLOW_PATH,
121   SNAT_IN2OUT_NEXT_REASS,
122   SNAT_IN2OUT_N_NEXT,
123 } snat_in2out_next_t;
124
125 static inline int
126 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
127                     u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
128                     u32 rx_fib_index0, u32 thread_index)
129 {
130   udp_header_t *udp0 = ip4_next_header (ip0);
131   snat_session_key_t key0, sm0;
132   clib_bihash_kv_8_8_t kv0, value0;
133
134   key0.addr = ip0->dst_address;
135   key0.port = udp0->dst_port;
136   key0.protocol = proto0;
137   key0.fib_index = sm->outside_fib_index;
138   kv0.key = key0.as_u64;
139
140   /* NAT packet aimed at external address if */
141   /* has active sessions */
142   if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
143                               &value0))
144     {
145       /* or is static mappings */
146       if (!snat_static_mapping_match (sm, key0, &sm0, 1, 0, 0, 0, 0, 0))
147         return 0;
148     }
149   else
150     return 0;
151
152   if (sm->forwarding_enabled)
153     return 1;
154
155   return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
156                                   rx_fib_index0);
157 }
158
159 static inline int
160 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
161                                   u32 proto0, u16 src_port, u16 dst_port,
162                                   u32 thread_index, u32 sw_if_index)
163 {
164   snat_session_key_t key0;
165   clib_bihash_kv_8_8_t kv0, value0;
166   snat_interface_t *i;
167
168   /* src NAT check */
169   key0.addr = ip0->src_address;
170   key0.port = src_port;
171   key0.protocol = proto0;
172   key0.fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
173   kv0.key = key0.as_u64;
174
175   if (!clib_bihash_search_8_8
176       (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
177     return 1;
178
179   /* dst NAT check */
180   key0.addr = ip0->dst_address;
181   key0.port = dst_port;
182   key0.protocol = proto0;
183   kv0.key = key0.as_u64;
184   if (!clib_bihash_search_8_8
185       (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
186     {
187       /* hairpinning */
188     /* *INDENT-OFF* */
189     pool_foreach (i, sm->output_feature_interfaces,
190     ({
191       if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
192         return 0;
193     }));
194     /* *INDENT-ON* */
195       return 1;
196     }
197
198   return 0;
199 }
200
201 int
202 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
203 {
204   snat_main_t *sm = &snat_main;
205   nat44_is_idle_session_ctx_t *ctx = arg;
206   snat_session_t *s;
207   u64 sess_timeout_time;
208   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
209                                                        ctx->thread_index);
210   clib_bihash_kv_8_8_t s_kv;
211
212   s = pool_elt_at_index (tsm->sessions, kv->value);
213   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
214   if (ctx->now >= sess_timeout_time)
215     {
216       s_kv.key = s->out2in.as_u64;
217       if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
218         nat_log_warn ("out2in key del failed");
219
220       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
221                                            s->in2out.addr.as_u32,
222                                            s->out2in.addr.as_u32,
223                                            s->in2out.protocol,
224                                            s->in2out.port,
225                                            s->out2in.port,
226                                            s->in2out.fib_index);
227
228       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
229                                &s->in2out.addr, s->in2out.port,
230                                &s->out2in.addr, s->out2in.port,
231                                s->in2out.protocol);
232
233       if (!snat_is_session_static (s))
234         snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
235                                             &s->out2in);
236
237       nat44_delete_session (sm, s, ctx->thread_index);
238       return 1;
239     }
240
241   return 0;
242 }
243
244 static u32
245 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
246            ip4_header_t * ip0,
247            u32 rx_fib_index0,
248            snat_session_key_t * key0,
249            snat_session_t ** sessionp,
250            vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
251 {
252   snat_user_t *u;
253   snat_session_t *s = 0;
254   clib_bihash_kv_8_8_t kv0;
255   snat_session_key_t key1;
256   udp_header_t *udp0 = ip4_next_header (ip0);
257   u8 is_sm = 0;
258   nat_outside_fib_t *outside_fib;
259   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
260   u8 identity_nat;
261   fib_prefix_t pfx = {
262     .fp_proto = FIB_PROTOCOL_IP4,
263     .fp_len = 32,
264     .fp_addr = {
265                 .ip4.as_u32 = ip0->dst_address.as_u32,
266                 },
267   };
268   nat44_is_idle_session_ctx_t ctx0;
269
270   if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
271     {
272       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
273       nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
274       nat_log_notice ("maximum sessions exceeded");
275       return SNAT_IN2OUT_NEXT_DROP;
276     }
277
278   key1.protocol = key0->protocol;
279
280   /* First try to match static mapping by local address and port */
281   if (snat_static_mapping_match
282       (sm, *key0, &key1, 0, 0, 0, 0, 0, &identity_nat))
283     {
284       /* Try to create dynamic translation */
285       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
286                                                thread_index, &key1,
287                                                sm->port_per_thread,
288                                                sm->per_thread_data
289                                                [thread_index].snat_thread_index))
290         {
291           b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
292           return SNAT_IN2OUT_NEXT_DROP;
293         }
294     }
295   else
296     {
297       if (PREDICT_FALSE (identity_nat))
298         {
299           *sessionp = s;
300           return next0;
301         }
302
303       is_sm = 1;
304     }
305
306   u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
307                               thread_index);
308   if (!u)
309     {
310       nat_log_warn ("create NAT user failed");
311       return SNAT_IN2OUT_NEXT_DROP;
312     }
313
314   s = nat_session_alloc_or_recycle (sm, u, thread_index);
315   if (!s)
316     {
317       nat44_delete_user_with_no_session (sm, u, thread_index);
318       nat_log_warn ("create NAT session failed");
319       return SNAT_IN2OUT_NEXT_DROP;
320     }
321
322   if (is_sm)
323     s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
324   user_session_increment (sm, u, is_sm);
325   s->in2out = *key0;
326   s->out2in = key1;
327   s->out2in.protocol = key0->protocol;
328   s->out2in.fib_index = sm->outside_fib_index;
329   switch (vec_len (sm->outside_fibs))
330     {
331     case 0:
332       s->out2in.fib_index = sm->outside_fib_index;
333       break;
334     case 1:
335       s->out2in.fib_index = sm->outside_fibs[0].fib_index;
336       break;
337     default:
338       /* *INDENT-OFF* */
339       vec_foreach (outside_fib, sm->outside_fibs)
340         {
341           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
342           if (FIB_NODE_INDEX_INVALID != fei)
343             {
344               if (fib_entry_get_resolving_interface (fei) != ~0)
345                 {
346                   s->out2in.fib_index = outside_fib->fib_index;
347                   break;
348                 }
349             }
350         }
351       /* *INDENT-ON* */
352       break;
353     }
354   s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
355   s->ext_host_port = udp0->dst_port;
356   *sessionp = s;
357
358   /* Add to translation hashes */
359   ctx0.now = now;
360   ctx0.thread_index = thread_index;
361   kv0.key = s->in2out.as_u64;
362   kv0.value = s - sm->per_thread_data[thread_index].sessions;
363   if (clib_bihash_add_or_overwrite_stale_8_8
364       (&sm->per_thread_data[thread_index].in2out, &kv0,
365        nat44_i2o_is_idle_session_cb, &ctx0))
366     nat_log_notice ("in2out key add failed");
367
368   kv0.key = s->out2in.as_u64;
369   kv0.value = s - sm->per_thread_data[thread_index].sessions;
370
371   if (clib_bihash_add_or_overwrite_stale_8_8
372       (&sm->per_thread_data[thread_index].out2in, &kv0,
373        nat44_o2i_is_idle_session_cb, &ctx0))
374     nat_log_notice ("out2in key add failed");
375
376   /* log NAT event */
377   snat_ipfix_logging_nat44_ses_create (thread_index,
378                                        s->in2out.addr.as_u32,
379                                        s->out2in.addr.as_u32,
380                                        s->in2out.protocol,
381                                        s->in2out.port,
382                                        s->out2in.port, s->in2out.fib_index);
383
384   nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
385                            &s->in2out.addr, s->in2out.port, &s->out2in.addr,
386                            s->out2in.port, s->in2out.protocol);
387
388   return next0;
389 }
390
391 static_always_inline
392   snat_in2out_error_t icmp_get_key (ip4_header_t * ip0,
393                                     snat_session_key_t * p_key0)
394 {
395   icmp46_header_t *icmp0;
396   snat_session_key_t key0;
397   icmp_echo_header_t *echo0, *inner_echo0 = 0;
398   ip4_header_t *inner_ip0 = 0;
399   void *l4_header = 0;
400   icmp46_header_t *inner_icmp0;
401
402   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
403   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
404
405   if (!icmp_is_error_message (icmp0))
406     {
407       key0.protocol = SNAT_PROTOCOL_ICMP;
408       key0.addr = ip0->src_address;
409       key0.port = echo0->identifier;
410     }
411   else
412     {
413       inner_ip0 = (ip4_header_t *) (echo0 + 1);
414       l4_header = ip4_next_header (inner_ip0);
415       key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
416       key0.addr = inner_ip0->dst_address;
417       switch (key0.protocol)
418         {
419         case SNAT_PROTOCOL_ICMP:
420           inner_icmp0 = (icmp46_header_t *) l4_header;
421           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
422           key0.port = inner_echo0->identifier;
423           break;
424         case SNAT_PROTOCOL_UDP:
425         case SNAT_PROTOCOL_TCP:
426           key0.port = ((tcp_udp_header_t *) l4_header)->dst_port;
427           break;
428         default:
429           return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
430         }
431     }
432   *p_key0 = key0;
433   return -1;                    /* success */
434 }
435
436 /**
437  * Get address and port values to be used for ICMP packet translation
438  * and create session if needed
439  *
440  * @param[in,out] sm             NAT main
441  * @param[in,out] node           NAT node runtime
442  * @param[in] thread_index       thread index
443  * @param[in,out] b0             buffer containing packet to be translated
444  * @param[out] p_proto           protocol used for matching
445  * @param[out] p_value           address and port after NAT translation
446  * @param[out] p_dont_translate  if packet should not be translated
447  * @param d                      optional parameter
448  * @param e                      optional parameter
449  */
450 u32
451 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
452                         u32 thread_index, vlib_buffer_t * b0,
453                         ip4_header_t * ip0, u8 * p_proto,
454                         snat_session_key_t * p_value,
455                         u8 * p_dont_translate, void *d, void *e)
456 {
457   icmp46_header_t *icmp0;
458   u32 sw_if_index0;
459   u32 rx_fib_index0;
460   snat_session_key_t key0;
461   snat_session_t *s0 = 0;
462   u8 dont_translate = 0;
463   clib_bihash_kv_8_8_t kv0, value0;
464   u32 next0 = ~0;
465   int err;
466
467   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
468   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
469   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
470
471   err = icmp_get_key (ip0, &key0);
472   if (err != -1)
473     {
474       b0->error = node->errors[err];
475       next0 = SNAT_IN2OUT_NEXT_DROP;
476       goto out;
477     }
478   key0.fib_index = rx_fib_index0;
479
480   kv0.key = key0.as_u64;
481
482   if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
483                               &value0))
484     {
485       if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
486         {
487           if (PREDICT_FALSE (nat_not_translate_output_feature (sm, ip0,
488                                                                key0.protocol,
489                                                                key0.port,
490                                                                key0.port,
491                                                                thread_index,
492                                                                sw_if_index0)))
493             {
494               dont_translate = 1;
495               goto out;
496             }
497         }
498       else
499         {
500           if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
501                                                  ip0, SNAT_PROTOCOL_ICMP,
502                                                  rx_fib_index0,
503                                                  thread_index)))
504             {
505               dont_translate = 1;
506               goto out;
507             }
508         }
509
510       if (PREDICT_FALSE (icmp_is_error_message (icmp0)))
511         {
512           b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
513           next0 = SNAT_IN2OUT_NEXT_DROP;
514           goto out;
515         }
516
517       next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0,
518                          thread_index, vlib_time_now (sm->vlib_main));
519
520       if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
521         goto out;
522
523       if (!s0)
524         {
525           dont_translate = 1;
526           goto out;
527         }
528     }
529   else
530     {
531       if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
532                          icmp0->type != ICMP4_echo_reply &&
533                          !icmp_is_error_message (icmp0)))
534         {
535           b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
536           next0 = SNAT_IN2OUT_NEXT_DROP;
537           goto out;
538         }
539
540       s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
541                               value0.value);
542     }
543
544 out:
545   *p_proto = key0.protocol;
546   if (s0)
547     *p_value = s0->out2in;
548   *p_dont_translate = dont_translate;
549   if (d)
550     *(snat_session_t **) d = s0;
551   return next0;
552 }
553
554 /**
555  * Get address and port values to be used for ICMP packet translation
556  *
557  * @param[in] sm                 NAT main
558  * @param[in,out] node           NAT node runtime
559  * @param[in] thread_index       thread index
560  * @param[in,out] b0             buffer containing packet to be translated
561  * @param[out] p_proto           protocol used for matching
562  * @param[out] p_value           address and port after NAT translation
563  * @param[out] p_dont_translate  if packet should not be translated
564  * @param d                      optional parameter
565  * @param e                      optional parameter
566  */
567 u32
568 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
569                         u32 thread_index, vlib_buffer_t * b0,
570                         ip4_header_t * ip0, u8 * p_proto,
571                         snat_session_key_t * p_value,
572                         u8 * p_dont_translate, void *d, void *e)
573 {
574   icmp46_header_t *icmp0;
575   u32 sw_if_index0;
576   u32 rx_fib_index0;
577   snat_session_key_t key0;
578   snat_session_key_t sm0;
579   u8 dont_translate = 0;
580   u8 is_addr_only;
581   u32 next0 = ~0;
582   int err;
583
584   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
585   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
586   rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
587
588   err = icmp_get_key (ip0, &key0);
589   if (err != -1)
590     {
591       b0->error = node->errors[err];
592       next0 = SNAT_IN2OUT_NEXT_DROP;
593       goto out2;
594     }
595   key0.fib_index = rx_fib_index0;
596
597   if (snat_static_mapping_match
598       (sm, key0, &sm0, 0, &is_addr_only, 0, 0, 0, 0))
599     {
600       if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
601                                                   IP_PROTOCOL_ICMP,
602                                                   rx_fib_index0)))
603         {
604           dont_translate = 1;
605           goto out;
606         }
607
608       if (icmp_is_error_message (icmp0))
609         {
610           next0 = SNAT_IN2OUT_NEXT_DROP;
611           goto out;
612         }
613
614       b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
615       next0 = SNAT_IN2OUT_NEXT_DROP;
616       goto out;
617     }
618
619   if (PREDICT_FALSE (icmp0->type != ICMP4_echo_request &&
620                      (icmp0->type != ICMP4_echo_reply || !is_addr_only) &&
621                      !icmp_is_error_message (icmp0)))
622     {
623       b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
624       next0 = SNAT_IN2OUT_NEXT_DROP;
625       goto out;
626     }
627
628 out:
629   *p_value = sm0;
630 out2:
631   *p_proto = key0.protocol;
632   *p_dont_translate = dont_translate;
633   return next0;
634 }
635
636 u32
637 icmp_in2out (snat_main_t * sm,
638              vlib_buffer_t * b0,
639              ip4_header_t * ip0,
640              icmp46_header_t * icmp0,
641              u32 sw_if_index0,
642              u32 rx_fib_index0,
643              vlib_node_runtime_t * node,
644              u32 next0, u32 thread_index, void *d, void *e)
645 {
646   snat_session_key_t sm0;
647   u8 protocol;
648   icmp_echo_header_t *echo0, *inner_echo0 = 0;
649   ip4_header_t *inner_ip0;
650   void *l4_header = 0;
651   icmp46_header_t *inner_icmp0;
652   u8 dont_translate;
653   u32 new_addr0, old_addr0;
654   u16 old_id0, new_id0;
655   u16 old_checksum0, new_checksum0;
656   ip_csum_t sum0;
657   u16 checksum0;
658   u32 next0_tmp;
659
660   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
661
662   next0_tmp = sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0,
663                                         &protocol, &sm0, &dont_translate, d,
664                                         e);
665   if (next0_tmp != ~0)
666     next0 = next0_tmp;
667   if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
668     goto out;
669
670   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
671     {
672       sum0 = ip_incremental_checksum_buffer (sm->vlib_main, b0, (u8 *) icmp0 -
673                                              (u8 *)
674                                              vlib_buffer_get_current (b0),
675                                              ntohs (ip0->length) -
676                                              ip4_header_bytes (ip0), 0);
677       checksum0 = ~ip_csum_fold (sum0);
678       if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
679         {
680           next0 = SNAT_IN2OUT_NEXT_DROP;
681           goto out;
682         }
683     }
684
685   old_addr0 = ip0->src_address.as_u32;
686   new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
687
688   sum0 = ip0->checksum;
689   sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
690                          src_address /* changed member */ );
691   ip0->checksum = ip_csum_fold (sum0);
692
693   if (icmp0->checksum == 0)
694     icmp0->checksum = 0xffff;
695
696   if (!icmp_is_error_message (icmp0))
697     {
698       new_id0 = sm0.port;
699       if (PREDICT_FALSE (new_id0 != echo0->identifier))
700         {
701           old_id0 = echo0->identifier;
702           new_id0 = sm0.port;
703           echo0->identifier = new_id0;
704
705           sum0 = icmp0->checksum;
706           sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
707                                  identifier);
708           icmp0->checksum = ip_csum_fold (sum0);
709         }
710     }
711   else
712     {
713       inner_ip0 = (ip4_header_t *) (echo0 + 1);
714       l4_header = ip4_next_header (inner_ip0);
715
716       if (!ip4_header_checksum_is_valid (inner_ip0))
717         {
718           next0 = SNAT_IN2OUT_NEXT_DROP;
719           goto out;
720         }
721
722       /* update inner destination IP address */
723       old_addr0 = inner_ip0->dst_address.as_u32;
724       inner_ip0->dst_address = sm0.addr;
725       new_addr0 = inner_ip0->dst_address.as_u32;
726       sum0 = icmp0->checksum;
727       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
728                              dst_address /* changed member */ );
729       icmp0->checksum = ip_csum_fold (sum0);
730
731       /* update inner IP header checksum */
732       old_checksum0 = inner_ip0->checksum;
733       sum0 = inner_ip0->checksum;
734       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
735                              dst_address /* changed member */ );
736       inner_ip0->checksum = ip_csum_fold (sum0);
737       new_checksum0 = inner_ip0->checksum;
738       sum0 = icmp0->checksum;
739       sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
740                              checksum);
741       icmp0->checksum = ip_csum_fold (sum0);
742
743       switch (protocol)
744         {
745         case SNAT_PROTOCOL_ICMP:
746           inner_icmp0 = (icmp46_header_t *) l4_header;
747           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
748
749           old_id0 = inner_echo0->identifier;
750           new_id0 = sm0.port;
751           inner_echo0->identifier = new_id0;
752
753           sum0 = icmp0->checksum;
754           sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
755                                  identifier);
756           icmp0->checksum = ip_csum_fold (sum0);
757           break;
758         case SNAT_PROTOCOL_UDP:
759         case SNAT_PROTOCOL_TCP:
760           old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
761           new_id0 = sm0.port;
762           ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
763
764           sum0 = icmp0->checksum;
765           sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
766                                  dst_port);
767           icmp0->checksum = ip_csum_fold (sum0);
768           break;
769         default:
770           ASSERT (0);
771         }
772     }
773
774   if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
775     {
776       if (sm->deterministic ||
777           0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
778                                       sm->endpoint_dependent))
779         vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
780     }
781
782 out:
783   return next0;
784 }
785
786 static inline u32
787 icmp_in2out_slow_path (snat_main_t * sm,
788                        vlib_buffer_t * b0,
789                        ip4_header_t * ip0,
790                        icmp46_header_t * icmp0,
791                        u32 sw_if_index0,
792                        u32 rx_fib_index0,
793                        vlib_node_runtime_t * node,
794                        u32 next0,
795                        f64 now, u32 thread_index, snat_session_t ** p_s0)
796 {
797   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
798                        next0, thread_index, p_s0, 0);
799   snat_session_t *s0 = *p_s0;
800   if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
801     {
802       /* Accounting */
803       nat44_session_update_counters (s0, now,
804                                      vlib_buffer_length_in_chain
805                                      (sm->vlib_main, b0));
806       /* Per-user LRU list maintenance */
807       nat44_session_update_lru (sm, s0, thread_index);
808     }
809   return next0;
810 }
811
812 static int
813 nat_in2out_sm_unknown_proto (snat_main_t * sm,
814                              vlib_buffer_t * b,
815                              ip4_header_t * ip, u32 rx_fib_index)
816 {
817   clib_bihash_kv_8_8_t kv, value;
818   snat_static_mapping_t *m;
819   snat_session_key_t m_key;
820   u32 old_addr, new_addr;
821   ip_csum_t sum;
822
823   m_key.addr = ip->src_address;
824   m_key.port = 0;
825   m_key.protocol = 0;
826   m_key.fib_index = rx_fib_index;
827   kv.key = m_key.as_u64;
828   if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
829     return 1;
830
831   m = pool_elt_at_index (sm->static_mappings, value.value);
832
833   old_addr = ip->src_address.as_u32;
834   new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
835   sum = ip->checksum;
836   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
837   ip->checksum = ip_csum_fold (sum);
838
839
840   /* Hairpinning */
841   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
842     {
843       vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
844       nat_hairpinning_sm_unknown_proto (sm, b, ip);
845     }
846
847   return 0;
848 }
849
850 static inline uword
851 snat_in2out_node_fn_inline (vlib_main_t * vm,
852                             vlib_node_runtime_t * node,
853                             vlib_frame_t * frame, int is_slow_path,
854                             int is_output_feature)
855 {
856   u32 n_left_from, *from, *to_next;
857   snat_in2out_next_t next_index;
858   u32 pkts_processed = 0;
859   snat_main_t *sm = &snat_main;
860   f64 now = vlib_time_now (vm);
861   u32 stats_node_index;
862   u32 thread_index = vm->thread_index;
863   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
864     0, fragments = 0;
865
866   stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
867     snat_in2out_node.index;
868
869   from = vlib_frame_vector_args (frame);
870   n_left_from = frame->n_vectors;
871   next_index = node->cached_next_index;
872
873   while (n_left_from > 0)
874     {
875       u32 n_left_to_next;
876
877       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
878
879       while (n_left_from >= 4 && n_left_to_next >= 2)
880         {
881           u32 bi0, bi1;
882           vlib_buffer_t *b0, *b1;
883           u32 next0, next1;
884           u32 sw_if_index0, sw_if_index1;
885           ip4_header_t *ip0, *ip1;
886           ip_csum_t sum0, sum1;
887           u32 new_addr0, old_addr0, new_addr1, old_addr1;
888           u16 old_port0, new_port0, old_port1, new_port1;
889           udp_header_t *udp0, *udp1;
890           tcp_header_t *tcp0, *tcp1;
891           icmp46_header_t *icmp0, *icmp1;
892           snat_session_key_t key0, key1;
893           u32 rx_fib_index0, rx_fib_index1;
894           u32 proto0, proto1;
895           snat_session_t *s0 = 0, *s1 = 0;
896           clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
897           u32 iph_offset0 = 0, iph_offset1 = 0;
898
899           /* Prefetch next iteration. */
900           {
901             vlib_buffer_t *p2, *p3;
902
903             p2 = vlib_get_buffer (vm, from[2]);
904             p3 = vlib_get_buffer (vm, from[3]);
905
906             vlib_prefetch_buffer_header (p2, LOAD);
907             vlib_prefetch_buffer_header (p3, LOAD);
908
909             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
910             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
911           }
912
913           /* speculatively enqueue b0 and b1 to the current next frame */
914           to_next[0] = bi0 = from[0];
915           to_next[1] = bi1 = from[1];
916           from += 2;
917           to_next += 2;
918           n_left_from -= 2;
919           n_left_to_next -= 2;
920
921           b0 = vlib_get_buffer (vm, bi0);
922           b1 = vlib_get_buffer (vm, bi1);
923
924           if (is_output_feature)
925             iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
926
927           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
928                                   iph_offset0);
929
930           udp0 = ip4_next_header (ip0);
931           tcp0 = (tcp_header_t *) udp0;
932           icmp0 = (icmp46_header_t *) udp0;
933
934           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
935           rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
936                                    sw_if_index0);
937
938           next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
939
940           if (PREDICT_FALSE (ip0->ttl == 1))
941             {
942               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
943               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
944                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
945                                            0);
946               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
947               goto trace00;
948             }
949
950           proto0 = ip_proto_to_snat_proto (ip0->protocol);
951
952           /* Next configured feature, probably ip4-lookup */
953           if (is_slow_path)
954             {
955               if (PREDICT_FALSE (proto0 == ~0))
956                 {
957                   if (nat_in2out_sm_unknown_proto
958                       (sm, b0, ip0, rx_fib_index0))
959                     {
960                       next0 = SNAT_IN2OUT_NEXT_DROP;
961                       b0->error =
962                         node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
963                     }
964                   other_packets++;
965                   goto trace00;
966                 }
967
968               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
969                 {
970                   next0 = icmp_in2out_slow_path
971                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
972                      node, next0, now, thread_index, &s0);
973                   icmp_packets++;
974                   goto trace00;
975                 }
976             }
977           else
978             {
979               if (PREDICT_FALSE (proto0 == ~0))
980                 {
981                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
982                   goto trace00;
983                 }
984
985               if (ip4_is_fragment (ip0))
986                 {
987                   next0 = SNAT_IN2OUT_NEXT_REASS;
988                   fragments++;
989                   goto trace00;
990                 }
991
992               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
993                 {
994                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
995                   goto trace00;
996                 }
997             }
998
999           key0.addr = ip0->src_address;
1000           key0.port = udp0->src_port;
1001           key0.protocol = proto0;
1002           key0.fib_index = rx_fib_index0;
1003
1004           kv0.key = key0.as_u64;
1005
1006           if (PREDICT_FALSE
1007               (clib_bihash_search_8_8
1008                (&sm->per_thread_data[thread_index].in2out, &kv0,
1009                 &value0) != 0))
1010             {
1011               if (is_slow_path)
1012                 {
1013                   if (is_output_feature)
1014                     {
1015                       if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1016                                                                            ip0,
1017                                                                            proto0,
1018                                                                            udp0->src_port,
1019                                                                            udp0->dst_port,
1020                                                                            thread_index,
1021                                                                            sw_if_index0)))
1022                         goto trace00;
1023                     }
1024                   else
1025                     {
1026                       if (PREDICT_FALSE
1027                           (snat_not_translate
1028                            (sm, node, sw_if_index0, ip0, proto0,
1029                             rx_fib_index0, thread_index)))
1030                         goto trace00;
1031                     }
1032
1033                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1034                                      &s0, node, next0, thread_index, now);
1035                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1036                     goto trace00;
1037
1038                   if (PREDICT_FALSE (!s0))
1039                     goto trace00;
1040                 }
1041               else
1042                 {
1043                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1044                   goto trace00;
1045                 }
1046             }
1047           else
1048             s0 =
1049               pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1050                                  value0.value);
1051
1052           b0->flags |= VNET_BUFFER_F_IS_NATED;
1053
1054           old_addr0 = ip0->src_address.as_u32;
1055           ip0->src_address = s0->out2in.addr;
1056           new_addr0 = ip0->src_address.as_u32;
1057           if (!is_output_feature)
1058             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1059
1060           sum0 = ip0->checksum;
1061           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1062                                  ip4_header_t,
1063                                  src_address /* changed member */ );
1064           ip0->checksum = ip_csum_fold (sum0);
1065
1066           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1067             {
1068               old_port0 = tcp0->src_port;
1069               tcp0->src_port = s0->out2in.port;
1070               new_port0 = tcp0->src_port;
1071
1072               sum0 = tcp0->checksum;
1073               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1074                                      ip4_header_t,
1075                                      dst_address /* changed member */ );
1076               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1077                                      ip4_header_t /* cheat */ ,
1078                                      length /* changed member */ );
1079               mss_clamping (sm, tcp0, &sum0);
1080               tcp0->checksum = ip_csum_fold (sum0);
1081               tcp_packets++;
1082             }
1083           else
1084             {
1085               old_port0 = udp0->src_port;
1086               udp0->src_port = s0->out2in.port;
1087               udp0->checksum = 0;
1088               udp_packets++;
1089             }
1090
1091           /* Accounting */
1092           nat44_session_update_counters (s0, now,
1093                                          vlib_buffer_length_in_chain (vm,
1094                                                                       b0));
1095           /* Per-user LRU list maintenance */
1096           nat44_session_update_lru (sm, s0, thread_index);
1097         trace00:
1098
1099           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1100                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1101             {
1102               snat_in2out_trace_t *t =
1103                 vlib_add_trace (vm, node, b0, sizeof (*t));
1104               t->is_slow_path = is_slow_path;
1105               t->sw_if_index = sw_if_index0;
1106               t->next_index = next0;
1107               t->session_index = ~0;
1108               if (s0)
1109                 t->session_index =
1110                   s0 - sm->per_thread_data[thread_index].sessions;
1111             }
1112
1113           pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1114
1115           if (is_output_feature)
1116             iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1117
1118           ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1119                                   iph_offset1);
1120
1121           udp1 = ip4_next_header (ip1);
1122           tcp1 = (tcp_header_t *) udp1;
1123           icmp1 = (icmp46_header_t *) udp1;
1124
1125           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1126           rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1127                                    sw_if_index1);
1128
1129           if (PREDICT_FALSE (ip1->ttl == 1))
1130             {
1131               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1132               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1133                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1134                                            0);
1135               next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1136               goto trace01;
1137             }
1138
1139           proto1 = ip_proto_to_snat_proto (ip1->protocol);
1140
1141           /* Next configured feature, probably ip4-lookup */
1142           if (is_slow_path)
1143             {
1144               if (PREDICT_FALSE (proto1 == ~0))
1145                 {
1146                   if (nat_in2out_sm_unknown_proto
1147                       (sm, b1, ip1, rx_fib_index1))
1148                     {
1149                       next1 = SNAT_IN2OUT_NEXT_DROP;
1150                       b1->error =
1151                         node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1152                     }
1153                   other_packets++;
1154                   goto trace01;
1155                 }
1156
1157               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1158                 {
1159                   next1 = icmp_in2out_slow_path
1160                     (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1161                      next1, now, thread_index, &s1);
1162                   icmp_packets++;
1163                   goto trace01;
1164                 }
1165             }
1166           else
1167             {
1168               if (PREDICT_FALSE (proto1 == ~0))
1169                 {
1170                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1171                   goto trace01;
1172                 }
1173
1174               if (ip4_is_fragment (ip1))
1175                 {
1176                   next1 = SNAT_IN2OUT_NEXT_REASS;
1177                   fragments++;
1178                   goto trace01;
1179                 }
1180
1181               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1182                 {
1183                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1184                   goto trace01;
1185                 }
1186             }
1187
1188           key1.addr = ip1->src_address;
1189           key1.port = udp1->src_port;
1190           key1.protocol = proto1;
1191           key1.fib_index = rx_fib_index1;
1192
1193           kv1.key = key1.as_u64;
1194
1195           if (PREDICT_FALSE
1196               (clib_bihash_search_8_8
1197                (&sm->per_thread_data[thread_index].in2out, &kv1,
1198                 &value1) != 0))
1199             {
1200               if (is_slow_path)
1201                 {
1202                   if (is_output_feature)
1203                     {
1204                       if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1205                                                                            ip1,
1206                                                                            proto1,
1207                                                                            udp1->src_port,
1208                                                                            udp1->dst_port,
1209                                                                            thread_index,
1210                                                                            sw_if_index1)))
1211                         goto trace01;
1212                     }
1213                   else
1214                     {
1215                       if (PREDICT_FALSE
1216                           (snat_not_translate
1217                            (sm, node, sw_if_index1, ip1, proto1,
1218                             rx_fib_index1, thread_index)))
1219                         goto trace01;
1220                     }
1221
1222                   next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1223                                      &s1, node, next1, thread_index, now);
1224                   if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1225                     goto trace01;
1226
1227                   if (PREDICT_FALSE (!s1))
1228                     goto trace01;
1229                 }
1230               else
1231                 {
1232                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1233                   goto trace01;
1234                 }
1235             }
1236           else
1237             s1 =
1238               pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1239                                  value1.value);
1240
1241           b1->flags |= VNET_BUFFER_F_IS_NATED;
1242
1243           old_addr1 = ip1->src_address.as_u32;
1244           ip1->src_address = s1->out2in.addr;
1245           new_addr1 = ip1->src_address.as_u32;
1246           if (!is_output_feature)
1247             vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1248
1249           sum1 = ip1->checksum;
1250           sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1251                                  ip4_header_t,
1252                                  src_address /* changed member */ );
1253           ip1->checksum = ip_csum_fold (sum1);
1254
1255           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1256             {
1257               old_port1 = tcp1->src_port;
1258               tcp1->src_port = s1->out2in.port;
1259               new_port1 = tcp1->src_port;
1260
1261               sum1 = tcp1->checksum;
1262               sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1263                                      ip4_header_t,
1264                                      dst_address /* changed member */ );
1265               sum1 = ip_csum_update (sum1, old_port1, new_port1,
1266                                      ip4_header_t /* cheat */ ,
1267                                      length /* changed member */ );
1268               mss_clamping (sm, tcp1, &sum1);
1269               tcp1->checksum = ip_csum_fold (sum1);
1270               tcp_packets++;
1271             }
1272           else
1273             {
1274               old_port1 = udp1->src_port;
1275               udp1->src_port = s1->out2in.port;
1276               udp1->checksum = 0;
1277               udp_packets++;
1278             }
1279
1280           /* Accounting */
1281           nat44_session_update_counters (s1, now,
1282                                          vlib_buffer_length_in_chain (vm,
1283                                                                       b1));
1284           /* Per-user LRU list maintenance */
1285           nat44_session_update_lru (sm, s1, thread_index);
1286         trace01:
1287
1288           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1289                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1290             {
1291               snat_in2out_trace_t *t =
1292                 vlib_add_trace (vm, node, b1, sizeof (*t));
1293               t->sw_if_index = sw_if_index1;
1294               t->next_index = next1;
1295               t->session_index = ~0;
1296               if (s1)
1297                 t->session_index =
1298                   s1 - sm->per_thread_data[thread_index].sessions;
1299             }
1300
1301           pkts_processed += next1 == SNAT_IN2OUT_NEXT_LOOKUP;
1302
1303           /* verify speculative enqueues, maybe switch current next frame */
1304           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1305                                            to_next, n_left_to_next,
1306                                            bi0, bi1, next0, next1);
1307         }
1308
1309       while (n_left_from > 0 && n_left_to_next > 0)
1310         {
1311           u32 bi0;
1312           vlib_buffer_t *b0;
1313           u32 next0;
1314           u32 sw_if_index0;
1315           ip4_header_t *ip0;
1316           ip_csum_t sum0;
1317           u32 new_addr0, old_addr0;
1318           u16 old_port0, new_port0;
1319           udp_header_t *udp0;
1320           tcp_header_t *tcp0;
1321           icmp46_header_t *icmp0;
1322           snat_session_key_t key0;
1323           u32 rx_fib_index0;
1324           u32 proto0;
1325           snat_session_t *s0 = 0;
1326           clib_bihash_kv_8_8_t kv0, value0;
1327           u32 iph_offset0 = 0;
1328
1329           /* speculatively enqueue b0 to the current next frame */
1330           bi0 = from[0];
1331           to_next[0] = bi0;
1332           from += 1;
1333           to_next += 1;
1334           n_left_from -= 1;
1335           n_left_to_next -= 1;
1336
1337           b0 = vlib_get_buffer (vm, bi0);
1338           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1339
1340           if (is_output_feature)
1341             iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1342
1343           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1344                                   iph_offset0);
1345
1346           udp0 = ip4_next_header (ip0);
1347           tcp0 = (tcp_header_t *) udp0;
1348           icmp0 = (icmp46_header_t *) udp0;
1349
1350           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1351           rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1352                                    sw_if_index0);
1353
1354           if (PREDICT_FALSE (ip0->ttl == 1))
1355             {
1356               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1357               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1358                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1359                                            0);
1360               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1361               goto trace0;
1362             }
1363
1364           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1365
1366           /* Next configured feature, probably ip4-lookup */
1367           if (is_slow_path)
1368             {
1369               if (PREDICT_FALSE (proto0 == ~0))
1370                 {
1371                   if (nat_in2out_sm_unknown_proto
1372                       (sm, b0, ip0, rx_fib_index0))
1373                     {
1374                       next0 = SNAT_IN2OUT_NEXT_DROP;
1375                       b0->error =
1376                         node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1377                     }
1378                   other_packets++;
1379                   goto trace0;
1380                 }
1381
1382               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1383                 {
1384                   next0 = icmp_in2out_slow_path
1385                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1386                      next0, now, thread_index, &s0);
1387                   icmp_packets++;
1388                   goto trace0;
1389                 }
1390             }
1391           else
1392             {
1393               if (PREDICT_FALSE (proto0 == ~0))
1394                 {
1395                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1396                   goto trace0;
1397                 }
1398
1399               if (ip4_is_fragment (ip0))
1400                 {
1401                   next0 = SNAT_IN2OUT_NEXT_REASS;
1402                   fragments++;
1403                   goto trace0;
1404                 }
1405
1406               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1407                 {
1408                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1409                   goto trace0;
1410                 }
1411             }
1412
1413           key0.addr = ip0->src_address;
1414           key0.port = udp0->src_port;
1415           key0.protocol = proto0;
1416           key0.fib_index = rx_fib_index0;
1417
1418           kv0.key = key0.as_u64;
1419
1420           if (clib_bihash_search_8_8
1421               (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1422             {
1423               if (is_slow_path)
1424                 {
1425                   if (is_output_feature)
1426                     {
1427                       if (PREDICT_FALSE (nat_not_translate_output_feature (sm,
1428                                                                            ip0,
1429                                                                            proto0,
1430                                                                            udp0->src_port,
1431                                                                            udp0->dst_port,
1432                                                                            thread_index,
1433                                                                            sw_if_index0)))
1434                         goto trace0;
1435                     }
1436                   else
1437                     {
1438                       if (PREDICT_FALSE
1439                           (snat_not_translate
1440                            (sm, node, sw_if_index0, ip0, proto0,
1441                             rx_fib_index0, thread_index)))
1442                         goto trace0;
1443                     }
1444
1445                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1446                                      &s0, node, next0, thread_index, now);
1447
1448                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1449                     goto trace0;
1450
1451                   if (PREDICT_FALSE (!s0))
1452                     goto trace0;
1453                 }
1454               else
1455                 {
1456                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1457                   goto trace0;
1458                 }
1459             }
1460           else
1461             s0 =
1462               pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1463                                  value0.value);
1464
1465           b0->flags |= VNET_BUFFER_F_IS_NATED;
1466
1467           old_addr0 = ip0->src_address.as_u32;
1468           ip0->src_address = s0->out2in.addr;
1469           new_addr0 = ip0->src_address.as_u32;
1470           if (!is_output_feature)
1471             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1472
1473           sum0 = ip0->checksum;
1474           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1475                                  ip4_header_t,
1476                                  src_address /* changed member */ );
1477           ip0->checksum = ip_csum_fold (sum0);
1478
1479           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1480             {
1481               old_port0 = tcp0->src_port;
1482               tcp0->src_port = s0->out2in.port;
1483               new_port0 = tcp0->src_port;
1484
1485               sum0 = tcp0->checksum;
1486               sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1487                                      ip4_header_t,
1488                                      dst_address /* changed member */ );
1489               sum0 = ip_csum_update (sum0, old_port0, new_port0,
1490                                      ip4_header_t /* cheat */ ,
1491                                      length /* changed member */ );
1492               mss_clamping (sm, tcp0, &sum0);
1493               tcp0->checksum = ip_csum_fold (sum0);
1494               tcp_packets++;
1495             }
1496           else
1497             {
1498               old_port0 = udp0->src_port;
1499               udp0->src_port = s0->out2in.port;
1500               udp0->checksum = 0;
1501               udp_packets++;
1502             }
1503
1504           /* Accounting */
1505           nat44_session_update_counters (s0, now,
1506                                          vlib_buffer_length_in_chain (vm,
1507                                                                       b0));
1508           /* Per-user LRU list maintenance */
1509           nat44_session_update_lru (sm, s0, thread_index);
1510
1511         trace0:
1512           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1513                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1514             {
1515               snat_in2out_trace_t *t =
1516                 vlib_add_trace (vm, node, b0, sizeof (*t));
1517               t->is_slow_path = is_slow_path;
1518               t->sw_if_index = sw_if_index0;
1519               t->next_index = next0;
1520               t->session_index = ~0;
1521               if (s0)
1522                 t->session_index =
1523                   s0 - sm->per_thread_data[thread_index].sessions;
1524             }
1525
1526           pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1527
1528           /* verify speculative enqueue, maybe switch current next frame */
1529           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1530                                            to_next, n_left_to_next,
1531                                            bi0, next0);
1532         }
1533
1534       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1535     }
1536
1537   vlib_node_increment_counter (vm, stats_node_index,
1538                                SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1539                                pkts_processed);
1540   vlib_node_increment_counter (vm, stats_node_index,
1541                                SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1542   vlib_node_increment_counter (vm, stats_node_index,
1543                                SNAT_IN2OUT_ERROR_UDP_PACKETS, tcp_packets);
1544   vlib_node_increment_counter (vm, stats_node_index,
1545                                SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1546   vlib_node_increment_counter (vm, stats_node_index,
1547                                SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1548                                other_packets);
1549   vlib_node_increment_counter (vm, stats_node_index,
1550                                SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1551
1552   return frame->n_vectors;
1553 }
1554
1555 static uword
1556 snat_in2out_fast_path_fn (vlib_main_t * vm,
1557                           vlib_node_runtime_t * node, vlib_frame_t * frame)
1558 {
1559   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1560                                      0);
1561 }
1562
1563 /* *INDENT-OFF* */
1564 VLIB_REGISTER_NODE (snat_in2out_node) = {
1565   .function = snat_in2out_fast_path_fn,
1566   .name = "nat44-in2out",
1567   .vector_size = sizeof (u32),
1568   .format_trace = format_snat_in2out_trace,
1569   .type = VLIB_NODE_TYPE_INTERNAL,
1570
1571   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1572   .error_strings = snat_in2out_error_strings,
1573
1574   .runtime_data_bytes = sizeof (snat_runtime_t),
1575
1576   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1577
1578   /* edit / add dispositions here */
1579   .next_nodes = {
1580     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1581     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1582     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1583     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1584     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1585   },
1586 };
1587 /* *INDENT-ON* */
1588
1589 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1590
1591 static uword
1592 snat_in2out_output_fast_path_fn (vlib_main_t * vm,
1593                                  vlib_node_runtime_t * node,
1594                                  vlib_frame_t * frame)
1595 {
1596   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1597                                      1);
1598 }
1599
1600 /* *INDENT-OFF* */
1601 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1602   .function = snat_in2out_output_fast_path_fn,
1603   .name = "nat44-in2out-output",
1604   .vector_size = sizeof (u32),
1605   .format_trace = format_snat_in2out_trace,
1606   .type = VLIB_NODE_TYPE_INTERNAL,
1607
1608   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1609   .error_strings = snat_in2out_error_strings,
1610
1611   .runtime_data_bytes = sizeof (snat_runtime_t),
1612
1613   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1614
1615   /* edit / add dispositions here */
1616   .next_nodes = {
1617     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1618     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1619     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1620     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1621     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1622   },
1623 };
1624 /* *INDENT-ON* */
1625
1626 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_node,
1627                               snat_in2out_output_fast_path_fn);
1628
1629 static uword
1630 snat_in2out_slow_path_fn (vlib_main_t * vm,
1631                           vlib_node_runtime_t * node, vlib_frame_t * frame)
1632 {
1633   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1634                                      0);
1635 }
1636
1637 /* *INDENT-OFF* */
1638 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1639   .function = snat_in2out_slow_path_fn,
1640   .name = "nat44-in2out-slowpath",
1641   .vector_size = sizeof (u32),
1642   .format_trace = format_snat_in2out_trace,
1643   .type = VLIB_NODE_TYPE_INTERNAL,
1644
1645   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1646   .error_strings = snat_in2out_error_strings,
1647
1648   .runtime_data_bytes = sizeof (snat_runtime_t),
1649
1650   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1651
1652   /* edit / add dispositions here */
1653   .next_nodes = {
1654     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1655     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1656     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1657     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1658     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1659   },
1660 };
1661 /* *INDENT-ON* */
1662
1663 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node,
1664                               snat_in2out_slow_path_fn);
1665
1666 static uword
1667 snat_in2out_output_slow_path_fn (vlib_main_t * vm,
1668                                  vlib_node_runtime_t * node,
1669                                  vlib_frame_t * frame)
1670 {
1671   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1672                                      1);
1673 }
1674
1675 /* *INDENT-OFF* */
1676 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1677   .function = snat_in2out_output_slow_path_fn,
1678   .name = "nat44-in2out-output-slowpath",
1679   .vector_size = sizeof (u32),
1680   .format_trace = format_snat_in2out_trace,
1681   .type = VLIB_NODE_TYPE_INTERNAL,
1682
1683   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1684   .error_strings = snat_in2out_error_strings,
1685
1686   .runtime_data_bytes = sizeof (snat_runtime_t),
1687
1688   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1689
1690   /* edit / add dispositions here */
1691   .next_nodes = {
1692     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1693     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1694     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1695     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1696     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1697   },
1698 };
1699 /* *INDENT-ON* */
1700
1701 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_output_slowpath_node,
1702                               snat_in2out_output_slow_path_fn);
1703
1704 static uword
1705 nat44_in2out_reass_node_fn (vlib_main_t * vm,
1706                             vlib_node_runtime_t * node, vlib_frame_t * frame)
1707 {
1708   u32 n_left_from, *from, *to_next;
1709   snat_in2out_next_t next_index;
1710   u32 pkts_processed = 0, cached_fragments = 0;
1711   snat_main_t *sm = &snat_main;
1712   f64 now = vlib_time_now (vm);
1713   u32 thread_index = vm->thread_index;
1714   snat_main_per_thread_data_t *per_thread_data =
1715     &sm->per_thread_data[thread_index];
1716   u32 *fragments_to_drop = 0;
1717   u32 *fragments_to_loopback = 0;
1718
1719   from = vlib_frame_vector_args (frame);
1720   n_left_from = frame->n_vectors;
1721   next_index = node->cached_next_index;
1722
1723   while (n_left_from > 0)
1724     {
1725       u32 n_left_to_next;
1726
1727       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1728
1729       while (n_left_from > 0 && n_left_to_next > 0)
1730         {
1731           u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1732           vlib_buffer_t *b0;
1733           u32 next0;
1734           u8 cached0 = 0;
1735           ip4_header_t *ip0;
1736           nat_reass_ip4_t *reass0;
1737           udp_header_t *udp0;
1738           tcp_header_t *tcp0;
1739           icmp46_header_t *icmp0;
1740           snat_session_key_t key0;
1741           clib_bihash_kv_8_8_t kv0, value0;
1742           snat_session_t *s0 = 0;
1743           u16 old_port0, new_port0;
1744           ip_csum_t sum0;
1745
1746           /* speculatively enqueue b0 to the current next frame */
1747           bi0 = from[0];
1748           to_next[0] = bi0;
1749           from += 1;
1750           to_next += 1;
1751           n_left_from -= 1;
1752           n_left_to_next -= 1;
1753
1754           b0 = vlib_get_buffer (vm, bi0);
1755           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1756
1757           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1758           rx_fib_index0 =
1759             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1760                                                  sw_if_index0);
1761
1762           if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1763             {
1764               next0 = SNAT_IN2OUT_NEXT_DROP;
1765               b0->error = node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT];
1766               goto trace0;
1767             }
1768
1769           ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1770           udp0 = ip4_next_header (ip0);
1771           tcp0 = (tcp_header_t *) udp0;
1772           icmp0 = (icmp46_header_t *) udp0;
1773           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1774
1775           reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1776                                                  ip0->dst_address,
1777                                                  ip0->fragment_id,
1778                                                  ip0->protocol,
1779                                                  1, &fragments_to_drop);
1780
1781           if (PREDICT_FALSE (!reass0))
1782             {
1783               next0 = SNAT_IN2OUT_NEXT_DROP;
1784               b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_REASS];
1785               nat_log_notice ("maximum reassemblies exceeded");
1786               goto trace0;
1787             }
1788
1789           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1790             {
1791               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1792                 {
1793                   next0 = icmp_in2out_slow_path
1794                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1795                      next0, now, thread_index, &s0);
1796
1797                   if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP))
1798                     {
1799                       if (s0)
1800                         reass0->sess_index = s0 - per_thread_data->sessions;
1801                       else
1802                         reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1803                       nat_ip4_reass_get_frags (reass0,
1804                                                &fragments_to_loopback);
1805                     }
1806
1807                   goto trace0;
1808                 }
1809
1810               key0.addr = ip0->src_address;
1811               key0.port = udp0->src_port;
1812               key0.protocol = proto0;
1813               key0.fib_index = rx_fib_index0;
1814               kv0.key = key0.as_u64;
1815
1816               if (clib_bihash_search_8_8
1817                   (&per_thread_data->in2out, &kv0, &value0))
1818                 {
1819                   if (PREDICT_FALSE
1820                       (snat_not_translate
1821                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1822                         thread_index)))
1823                     goto trace0;
1824
1825                   next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1826                                      &s0, node, next0, thread_index, now);
1827
1828                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1829                     goto trace0;
1830
1831                   if (PREDICT_FALSE (!s0))
1832                     goto trace0;
1833
1834                   reass0->sess_index = s0 - per_thread_data->sessions;
1835                 }
1836               else
1837                 {
1838                   s0 = pool_elt_at_index (per_thread_data->sessions,
1839                                           value0.value);
1840                   reass0->sess_index = value0.value;
1841                 }
1842               nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1843             }
1844           else
1845             {
1846               if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1847                 {
1848                   if (nat_ip4_reass_add_fragment
1849                       (thread_index, reass0, bi0, &fragments_to_drop))
1850                     {
1851                       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_FRAG];
1852                       nat_log_notice
1853                         ("maximum fragments per reassembly exceeded");
1854                       next0 = SNAT_IN2OUT_NEXT_DROP;
1855                       goto trace0;
1856                     }
1857                   cached0 = 1;
1858                   goto trace0;
1859                 }
1860               s0 = pool_elt_at_index (per_thread_data->sessions,
1861                                       reass0->sess_index);
1862             }
1863
1864           old_addr0 = ip0->src_address.as_u32;
1865           ip0->src_address = s0->out2in.addr;
1866           new_addr0 = ip0->src_address.as_u32;
1867           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1868
1869           sum0 = ip0->checksum;
1870           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1871                                  ip4_header_t,
1872                                  src_address /* changed member */ );
1873           ip0->checksum = ip_csum_fold (sum0);
1874
1875           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1876             {
1877               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1878                 {
1879                   old_port0 = tcp0->src_port;
1880                   tcp0->src_port = s0->out2in.port;
1881                   new_port0 = tcp0->src_port;
1882
1883                   sum0 = tcp0->checksum;
1884                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1885                                          ip4_header_t,
1886                                          dst_address /* changed member */ );
1887                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1888                                          ip4_header_t /* cheat */ ,
1889                                          length /* changed member */ );
1890                   tcp0->checksum = ip_csum_fold (sum0);
1891                 }
1892               else
1893                 {
1894                   old_port0 = udp0->src_port;
1895                   udp0->src_port = s0->out2in.port;
1896                   udp0->checksum = 0;
1897                 }
1898             }
1899
1900           /* Hairpinning */
1901           nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
1902                                    s0->ext_host_port, proto0, 0);
1903
1904           /* Accounting */
1905           nat44_session_update_counters (s0, now,
1906                                          vlib_buffer_length_in_chain (vm,
1907                                                                       b0));
1908           /* Per-user LRU list maintenance */
1909           nat44_session_update_lru (sm, s0, thread_index);
1910
1911         trace0:
1912           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1913                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1914             {
1915               nat44_reass_trace_t *t =
1916                 vlib_add_trace (vm, node, b0, sizeof (*t));
1917               t->cached = cached0;
1918               t->sw_if_index = sw_if_index0;
1919               t->next_index = next0;
1920             }
1921
1922           if (cached0)
1923             {
1924               n_left_to_next++;
1925               to_next--;
1926               cached_fragments++;
1927             }
1928           else
1929             {
1930               pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1931
1932               /* verify speculative enqueue, maybe switch current next frame */
1933               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1934                                                to_next, n_left_to_next,
1935                                                bi0, next0);
1936             }
1937
1938           if (n_left_from == 0 && vec_len (fragments_to_loopback))
1939             {
1940               from = vlib_frame_vector_args (frame);
1941               u32 len = vec_len (fragments_to_loopback);
1942               if (len <= VLIB_FRAME_SIZE)
1943                 {
1944                   clib_memcpy_fast (from, fragments_to_loopback,
1945                                     sizeof (u32) * len);
1946                   n_left_from = len;
1947                   vec_reset_length (fragments_to_loopback);
1948                 }
1949               else
1950                 {
1951                   clib_memcpy_fast (from, fragments_to_loopback +
1952                                     (len - VLIB_FRAME_SIZE),
1953                                     sizeof (u32) * VLIB_FRAME_SIZE);
1954                   n_left_from = VLIB_FRAME_SIZE;
1955                   _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1956                 }
1957             }
1958         }
1959
1960       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1961     }
1962
1963   vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1964                                SNAT_IN2OUT_ERROR_PROCESSED_FRAGMENTS,
1965                                pkts_processed);
1966   vlib_node_increment_counter (vm, nat44_in2out_reass_node.index,
1967                                SNAT_IN2OUT_ERROR_CACHED_FRAGMENTS,
1968                                cached_fragments);
1969
1970   nat_send_all_to_node (vm, fragments_to_drop, node,
1971                         &node->errors[SNAT_IN2OUT_ERROR_DROP_FRAGMENT],
1972                         SNAT_IN2OUT_NEXT_DROP);
1973
1974   vec_free (fragments_to_drop);
1975   vec_free (fragments_to_loopback);
1976   return frame->n_vectors;
1977 }
1978
1979 /* *INDENT-OFF* */
1980 VLIB_REGISTER_NODE (nat44_in2out_reass_node) = {
1981   .function = nat44_in2out_reass_node_fn,
1982   .name = "nat44-in2out-reass",
1983   .vector_size = sizeof (u32),
1984   .format_trace = format_nat44_reass_trace,
1985   .type = VLIB_NODE_TYPE_INTERNAL,
1986
1987   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1988   .error_strings = snat_in2out_error_strings,
1989
1990   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1991   .next_nodes = {
1992     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1993     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1994     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1995     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1996     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
1997   },
1998 };
1999 /* *INDENT-ON* */
2000
2001 VLIB_NODE_FUNCTION_MULTIARCH (nat44_in2out_reass_node,
2002                               nat44_in2out_reass_node_fn);
2003
2004 static uword
2005 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2006                                 vlib_node_runtime_t * node,
2007                                 vlib_frame_t * frame)
2008 {
2009   u32 n_left_from, *from, *to_next;
2010   snat_in2out_next_t next_index;
2011   u32 pkts_processed = 0;
2012   snat_main_t *sm = &snat_main;
2013   u32 stats_node_index;
2014
2015   stats_node_index = snat_in2out_fast_node.index;
2016
2017   from = vlib_frame_vector_args (frame);
2018   n_left_from = frame->n_vectors;
2019   next_index = node->cached_next_index;
2020
2021   while (n_left_from > 0)
2022     {
2023       u32 n_left_to_next;
2024
2025       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2026
2027       while (n_left_from > 0 && n_left_to_next > 0)
2028         {
2029           u32 bi0;
2030           vlib_buffer_t *b0;
2031           u32 next0;
2032           u32 sw_if_index0;
2033           ip4_header_t *ip0;
2034           ip_csum_t sum0;
2035           u32 new_addr0, old_addr0;
2036           u16 old_port0, new_port0;
2037           udp_header_t *udp0;
2038           tcp_header_t *tcp0;
2039           icmp46_header_t *icmp0;
2040           snat_session_key_t key0, sm0;
2041           u32 proto0;
2042           u32 rx_fib_index0;
2043
2044           /* speculatively enqueue b0 to the current next frame */
2045           bi0 = from[0];
2046           to_next[0] = bi0;
2047           from += 1;
2048           to_next += 1;
2049           n_left_from -= 1;
2050           n_left_to_next -= 1;
2051
2052           b0 = vlib_get_buffer (vm, bi0);
2053           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2054
2055           ip0 = vlib_buffer_get_current (b0);
2056           udp0 = ip4_next_header (ip0);
2057           tcp0 = (tcp_header_t *) udp0;
2058           icmp0 = (icmp46_header_t *) udp0;
2059
2060           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2061           rx_fib_index0 =
2062             ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
2063
2064           if (PREDICT_FALSE (ip0->ttl == 1))
2065             {
2066               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2067               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
2068                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
2069                                            0);
2070               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
2071               goto trace0;
2072             }
2073
2074           proto0 = ip_proto_to_snat_proto (ip0->protocol);
2075
2076           if (PREDICT_FALSE (proto0 == ~0))
2077             goto trace0;
2078
2079           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2080             {
2081               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
2082                                    rx_fib_index0, node, next0, ~0, 0, 0);
2083               goto trace0;
2084             }
2085
2086           key0.addr = ip0->src_address;
2087           key0.protocol = proto0;
2088           key0.port = udp0->src_port;
2089           key0.fib_index = rx_fib_index0;
2090
2091           if (snat_static_mapping_match (sm, key0, &sm0, 0, 0, 0, 0, 0, 0))
2092             {
2093               b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2094               next0 = SNAT_IN2OUT_NEXT_DROP;
2095               goto trace0;
2096             }
2097
2098           new_addr0 = sm0.addr.as_u32;
2099           new_port0 = sm0.port;
2100           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2101           old_addr0 = ip0->src_address.as_u32;
2102           ip0->src_address.as_u32 = new_addr0;
2103
2104           sum0 = ip0->checksum;
2105           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2106                                  ip4_header_t,
2107                                  src_address /* changed member */ );
2108           ip0->checksum = ip_csum_fold (sum0);
2109
2110           if (PREDICT_FALSE (new_port0 != udp0->dst_port))
2111             {
2112               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2113                 {
2114                   old_port0 = tcp0->src_port;
2115                   tcp0->src_port = new_port0;
2116
2117                   sum0 = tcp0->checksum;
2118                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2119                                          ip4_header_t,
2120                                          dst_address /* changed member */ );
2121                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
2122                                          ip4_header_t /* cheat */ ,
2123                                          length /* changed member */ );
2124                   mss_clamping (sm, tcp0, &sum0);
2125                   tcp0->checksum = ip_csum_fold (sum0);
2126                 }
2127               else
2128                 {
2129                   old_port0 = udp0->src_port;
2130                   udp0->src_port = new_port0;
2131                   udp0->checksum = 0;
2132                 }
2133             }
2134           else
2135             {
2136               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
2137                 {
2138                   sum0 = tcp0->checksum;
2139                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2140                                          ip4_header_t,
2141                                          dst_address /* changed member */ );
2142                   mss_clamping (sm, tcp0, &sum0);
2143                   tcp0->checksum = ip_csum_fold (sum0);
2144                 }
2145             }
2146
2147           /* Hairpinning */
2148           snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0, 0);
2149
2150         trace0:
2151           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2152                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2153             {
2154               snat_in2out_trace_t *t =
2155                 vlib_add_trace (vm, node, b0, sizeof (*t));
2156               t->sw_if_index = sw_if_index0;
2157               t->next_index = next0;
2158             }
2159
2160           pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2161
2162           /* verify speculative enqueue, maybe switch current next frame */
2163           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2164                                            to_next, n_left_to_next,
2165                                            bi0, next0);
2166         }
2167
2168       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2169     }
2170
2171   vlib_node_increment_counter (vm, stats_node_index,
2172                                SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2173                                pkts_processed);
2174   return frame->n_vectors;
2175 }
2176
2177
2178 /* *INDENT-OFF* */
2179 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2180   .function = snat_in2out_fast_static_map_fn,
2181   .name = "nat44-in2out-fast",
2182   .vector_size = sizeof (u32),
2183   .format_trace = format_snat_in2out_fast_trace,
2184   .type = VLIB_NODE_TYPE_INTERNAL,
2185
2186   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2187   .error_strings = snat_in2out_error_strings,
2188
2189   .runtime_data_bytes = sizeof (snat_runtime_t),
2190
2191   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2192
2193   /* edit / add dispositions here */
2194   .next_nodes = {
2195     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2196     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2197     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2198     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2199     [SNAT_IN2OUT_NEXT_REASS] = "nat44-in2out-reass",
2200   },
2201 };
2202 /* *INDENT-ON* */
2203
2204 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node,
2205                               snat_in2out_fast_static_map_fn);
2206
2207 /*
2208  * fd.io coding-style-patch-verification: ON
2209  *
2210  * Local Variables:
2211  * eval: (c-set-style "gnu")
2212  * End:
2213  */