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