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