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