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