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