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