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