nat: simplify bihash buckets/mem config
[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 #include <nat/lib/nat_inlines.h>
39
40 typedef struct
41 {
42   u32 sw_if_index;
43   u32 next_index;
44   u32 session_index;
45   u32 is_slow_path;
46   u32 is_hairpinning;
47 } snat_in2out_trace_t;
48
49 /* packet trace format function */
50 static u8 *
51 format_snat_in2out_trace (u8 * s, va_list * args)
52 {
53   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
56   char *tag;
57
58   tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
59
60   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
61               t->sw_if_index, t->next_index, t->session_index);
62   if (t->is_hairpinning)
63     {
64       s = format (s, ", with-hairpinning");
65     }
66
67   return s;
68 }
69
70 static u8 *
71 format_snat_in2out_fast_trace (u8 * s, va_list * args)
72 {
73   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75   snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
76
77   s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
78               t->sw_if_index, t->next_index);
79
80   return s;
81 }
82
83 #define foreach_snat_in2out_error                       \
84 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
85 _(IN2OUT_PACKETS, "good in2out packets processed")      \
86 _(OUT_OF_PORTS, "out of ports")                         \
87 _(BAD_OUTSIDE_FIB, "outside VRF ID not found")          \
88 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
89 _(NO_TRANSLATION, "no translation")                     \
90 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
91 _(DROP_FRAGMENT, "drop fragment")                       \
92 _(TCP_PACKETS, "TCP packets")                           \
93 _(UDP_PACKETS, "UDP packets")                           \
94 _(ICMP_PACKETS, "ICMP packets")                         \
95 _(OTHER_PACKETS, "other protocol packets")              \
96 _(FRAGMENTS, "fragments")                               \
97 _(CACHED_FRAGMENTS, "cached fragments")                 \
98 _(PROCESSED_FRAGMENTS, "processed fragments")
99
100 typedef enum
101 {
102 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
103   foreach_snat_in2out_error
104 #undef _
105     SNAT_IN2OUT_N_ERROR,
106 } snat_in2out_error_t;
107
108 static char *snat_in2out_error_strings[] = {
109 #define _(sym,string) string,
110   foreach_snat_in2out_error
111 #undef _
112 };
113
114 typedef enum
115 {
116   SNAT_IN2OUT_NEXT_LOOKUP,
117   SNAT_IN2OUT_NEXT_DROP,
118   SNAT_IN2OUT_NEXT_ICMP_ERROR,
119   SNAT_IN2OUT_NEXT_SLOW_PATH,
120   SNAT_IN2OUT_N_NEXT,
121 } snat_in2out_next_t;
122
123 static inline int
124 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
125                     u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
126                     u32 rx_fib_index0, u32 thread_index)
127 {
128   udp_header_t *udp0 = ip4_next_header (ip0);
129   clib_bihash_kv_8_8_t kv0, value0;
130
131   init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
132               proto0);
133
134   /* NAT packet aimed at external address if */
135   /* has active sessions */
136   if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
137                               &value0))
138     {
139       /* or is static mappings */
140       ip4_address_t dummy_addr;
141       u16 dummy_port;
142       u32 dummy_fib_index;
143       if (!snat_static_mapping_match
144           (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
145            proto0, &dummy_addr, &dummy_port, &dummy_fib_index, 1, 0, 0, 0, 0,
146            0))
147         return 0;
148     }
149   else
150     return 0;
151
152   if (sm->forwarding_enabled)
153     return 1;
154
155   return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
156                                   rx_fib_index0);
157 }
158
159 static inline int
160 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
161                                   u32 proto0, u16 src_port, u16 dst_port,
162                                   u32 thread_index, u32 sw_if_index)
163 {
164   clib_bihash_kv_8_8_t kv0, value0;
165   snat_interface_t *i;
166
167   /* src NAT check */
168   init_nat_k (&kv0, ip0->src_address, src_port,
169               ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
170
171   if (!clib_bihash_search_8_8
172       (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
173     return 1;
174
175   /* dst NAT check */
176   init_nat_k (&kv0, ip0->dst_address, dst_port,
177               ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
178   if (!clib_bihash_search_8_8
179       (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
180     {
181       /* hairpinning */
182     /* *INDENT-OFF* */
183     pool_foreach (i, sm->output_feature_interfaces,
184     ({
185       if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
186         return 0;
187     }));
188     /* *INDENT-ON* */
189       return 1;
190     }
191
192   return 0;
193 }
194
195 #ifndef CLIB_MARCH_VARIANT
196 int
197 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
198 {
199   snat_main_t *sm = &snat_main;
200   nat44_is_idle_session_ctx_t *ctx = arg;
201   snat_session_t *s;
202   u64 sess_timeout_time;
203   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
204                                                        ctx->thread_index);
205   clib_bihash_kv_8_8_t s_kv;
206
207   s = pool_elt_at_index (tsm->sessions, kv->value);
208   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
209   if (ctx->now >= sess_timeout_time)
210     {
211       init_nat_o2i_k (&s_kv, s);
212       if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
213         nat_elog_warn ("out2in key del failed");
214
215       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
216                                            s->in2out.addr.as_u32,
217                                            s->out2in.addr.as_u32,
218                                            s->nat_proto,
219                                            s->in2out.port,
220                                            s->out2in.port,
221                                            s->in2out.fib_index);
222
223       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
224                                &s->in2out.addr, s->in2out.port,
225                                &s->out2in.addr, s->out2in.port, s->nat_proto);
226
227       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
228                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
229                    ctx->thread_index);
230
231       if (!snat_is_session_static (s))
232         snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
233                                             &s->out2in.addr,
234                                             s->out2in.port, s->nat_proto);
235
236       nat44_delete_session (sm, s, ctx->thread_index);
237       return 1;
238     }
239
240   return 0;
241 }
242 #endif
243
244 static u32
245 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
246            ip4_header_t * ip0,
247            ip4_address_t i2o_addr,
248            u16 i2o_port,
249            u32 rx_fib_index0,
250            nat_protocol_t nat_proto,
251            snat_session_t ** sessionp,
252            vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
253 {
254   snat_user_t *u;
255   snat_session_t *s = 0;
256   clib_bihash_kv_8_8_t kv0;
257   u8 is_sm = 0;
258   nat_outside_fib_t *outside_fib;
259   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
260   u8 identity_nat;
261   fib_prefix_t pfx = {
262     .fp_proto = FIB_PROTOCOL_IP4,
263     .fp_len = 32,
264     .fp_addr = {
265                 .ip4.as_u32 = ip0->dst_address.as_u32,
266                 },
267   };
268   nat44_is_idle_session_ctx_t ctx0;
269   ip4_address_t sm_addr;
270   u16 sm_port;
271   u32 sm_fib_index;
272
273   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
274     {
275       b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
276       nat_ipfix_logging_max_sessions (thread_index,
277                                       sm->max_translations_per_thread);
278       nat_elog_notice ("maximum sessions exceeded");
279       return SNAT_IN2OUT_NEXT_DROP;
280     }
281
282   /* First try to match static mapping by local address and port */
283   if (snat_static_mapping_match
284       (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
285        &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat))
286     {
287       /* Try to create dynamic translation */
288       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
289                                                thread_index,
290                                                nat_proto,
291                                                &sm_addr, &sm_port,
292                                                sm->port_per_thread,
293                                                sm->per_thread_data
294                                                [thread_index].snat_thread_index))
295         {
296           b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
297           return SNAT_IN2OUT_NEXT_DROP;
298         }
299     }
300   else
301     {
302       if (PREDICT_FALSE (identity_nat))
303         {
304           *sessionp = s;
305           return next0;
306         }
307
308       is_sm = 1;
309     }
310
311   u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
312                               thread_index);
313   if (!u)
314     {
315       nat_elog_warn ("create NAT user failed");
316       return SNAT_IN2OUT_NEXT_DROP;
317     }
318
319   s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
320   if (!s)
321     {
322       nat44_delete_user_with_no_session (sm, u, thread_index);
323       nat_elog_warn ("create NAT session failed");
324       return SNAT_IN2OUT_NEXT_DROP;
325     }
326
327   if (is_sm)
328     s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
329   user_session_increment (sm, u, is_sm);
330   s->in2out.addr = i2o_addr;
331   s->in2out.port = i2o_port;
332   s->in2out.fib_index = rx_fib_index0;
333   s->nat_proto = nat_proto;
334   s->out2in.addr = sm_addr;
335   s->out2in.port = sm_port;
336   s->out2in.fib_index = sm->outside_fib_index;
337   switch (vec_len (sm->outside_fibs))
338     {
339     case 0:
340       s->out2in.fib_index = sm->outside_fib_index;
341       break;
342     case 1:
343       s->out2in.fib_index = sm->outside_fibs[0].fib_index;
344       break;
345     default:
346       /* *INDENT-OFF* */
347       vec_foreach (outside_fib, sm->outside_fibs)
348         {
349           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
350           if (FIB_NODE_INDEX_INVALID != fei)
351             {
352               if (fib_entry_get_resolving_interface (fei) != ~0)
353                 {
354                   s->out2in.fib_index = outside_fib->fib_index;
355                   break;
356                 }
357             }
358         }
359       /* *INDENT-ON* */
360       break;
361     }
362   s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
363   s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
364   *sessionp = s;
365
366   /* Add to translation hashes */
367   ctx0.now = now;
368   ctx0.thread_index = thread_index;
369   init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
370   if (clib_bihash_add_or_overwrite_stale_8_8
371       (&sm->per_thread_data[thread_index].in2out, &kv0,
372        nat44_i2o_is_idle_session_cb, &ctx0))
373     nat_elog_notice ("in2out key add failed");
374
375   init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
376   if (clib_bihash_add_or_overwrite_stale_8_8
377       (&sm->per_thread_data[thread_index].out2in, &kv0,
378        nat44_o2i_is_idle_session_cb, &ctx0))
379     nat_elog_notice ("out2in key add failed");
380
381   /* log NAT event */
382   snat_ipfix_logging_nat44_ses_create (thread_index,
383                                        s->in2out.addr.as_u32,
384                                        s->out2in.addr.as_u32,
385                                        s->nat_proto,
386                                        s->in2out.port,
387                                        s->out2in.port, s->in2out.fib_index);
388
389   nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
390                            &s->in2out.addr, s->in2out.port, &s->out2in.addr,
391                            s->out2in.port, s->nat_proto);
392
393   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
394                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
395                &s->ext_host_nat_addr, s->ext_host_nat_port,
396                s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
397
398   return next0;
399 }
400
401 #ifndef CLIB_MARCH_VARIANT
402 static_always_inline snat_in2out_error_t
403 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
404               ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
405 {
406   icmp46_header_t *icmp0;
407   icmp_echo_header_t *echo0, *inner_echo0 = 0;
408   ip4_header_t *inner_ip0 = 0;
409   void *l4_header = 0;
410   icmp46_header_t *inner_icmp0;
411
412   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
413   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
414
415   if (!icmp_type_is_error_message
416       (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
417     {
418       *nat_proto = NAT_PROTOCOL_ICMP;
419       *addr = ip0->src_address;
420       *port = vnet_buffer (b)->ip.reass.l4_src_port;
421     }
422   else
423     {
424       inner_ip0 = (ip4_header_t *) (echo0 + 1);
425       l4_header = ip4_next_header (inner_ip0);
426       *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
427       *addr = inner_ip0->dst_address;
428       switch (*nat_proto)
429         {
430         case NAT_PROTOCOL_ICMP:
431           inner_icmp0 = (icmp46_header_t *) l4_header;
432           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
433           *port = inner_echo0->identifier;
434           break;
435         case NAT_PROTOCOL_UDP:
436         case NAT_PROTOCOL_TCP:
437           *port = ((tcp_udp_header_t *) l4_header)->dst_port;
438           break;
439         default:
440           return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
441         }
442     }
443   return -1;                    /* success */
444 }
445
446 /**
447  * Get address and port values to be used for ICMP packet translation
448  * and create session if needed
449  *
450  * @param[in,out] sm             NAT main
451  * @param[in,out] node           NAT node runtime
452  * @param[in] thread_index       thread index
453  * @param[in,out] b0             buffer containing packet to be translated
454  * @param[in,out] ip0            ip header
455  * @param[out] p_proto           protocol used for matching
456  * @param[out] p_value           address and port after NAT translation
457  * @param[out] p_dont_translate  if packet should not be translated
458  * @param d                      optional parameter
459  * @param e                      optional parameter
460  */
461 u32
462 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
463                         u32 thread_index, vlib_buffer_t * b0,
464                         ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
465                         u32 * fib_index, nat_protocol_t * proto, void *d,
466                         void *e, u8 * dont_translate)
467 {
468   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
469   u32 sw_if_index0;
470   snat_session_t *s0 = 0;
471   clib_bihash_kv_8_8_t kv0, value0;
472   u32 next0 = ~0;
473   int err;
474   vlib_main_t *vm = vlib_get_main ();
475   *dont_translate = 0;
476
477   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
478   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
479
480   err = icmp_get_key (b0, ip0, addr, port, proto);
481   if (err != -1)
482     {
483       b0->error = node->errors[err];
484       next0 = SNAT_IN2OUT_NEXT_DROP;
485       goto out;
486     }
487
488   init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
489   if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
490     {
491       if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
492         {
493           if (PREDICT_FALSE
494               (nat_not_translate_output_feature
495                (sm, ip0, *proto, *port, *port, thread_index, 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, NAT_PROTOCOL_ICMP,
505                                                  *fib_index, thread_index)))
506             {
507               *dont_translate = 1;
508               goto out;
509             }
510         }
511
512       if (PREDICT_FALSE
513           (icmp_type_is_error_message
514            (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
515         {
516           b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
517           next0 = SNAT_IN2OUT_NEXT_DROP;
518           goto out;
519         }
520
521       next0 =
522         slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
523                    next0, thread_index, vlib_time_now (vm));
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 (tsm->sessions, value0.value);
550     }
551
552 out:
553   if (s0)
554     {
555       *addr = s0->out2in.addr;
556       *port = s0->out2in.port;
557       *fib_index = s0->out2in.fib_index;
558     }
559   if (d)
560     *(snat_session_t **) (d) = s0;
561   return next0;
562 }
563 #endif
564
565 #ifndef CLIB_MARCH_VARIANT
566 /**
567  * Get address and port values to be used for ICMP packet translation
568  *
569  * @param[in] sm                 NAT main
570  * @param[in,out] node           NAT node runtime
571  * @param[in] thread_index       thread index
572  * @param[in,out] b0             buffer containing packet to be translated
573  * @param[in,out] ip0            ip header
574  * @param[out] p_proto           protocol used for matching
575  * @param[out] p_value           address and port after NAT translation
576  * @param[out] p_dont_translate  if packet should not be translated
577  * @param d                      optional parameter
578  * @param e                      optional parameter
579  */
580 u32
581 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
582                         u32 thread_index, vlib_buffer_t * b0,
583                         ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
584                         u32 * fib_index, nat_protocol_t * proto, void *d,
585                         void *e, u8 * dont_translate)
586 {
587   u32 sw_if_index0;
588   u8 is_addr_only;
589   u32 next0 = ~0;
590   int err;
591   *dont_translate = 0;
592
593   sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
594   *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
595
596   err = icmp_get_key (b0, ip0, addr, port, proto);
597   if (err != -1)
598     {
599       b0->error = node->errors[err];
600       next0 = SNAT_IN2OUT_NEXT_DROP;
601       goto out;
602     }
603
604   ip4_address_t sm_addr;
605   u16 sm_port;
606   u32 sm_fib_index;
607
608   if (snat_static_mapping_match
609       (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
610        &sm_fib_index, 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                                                   *fib_index)))
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   return next0;
646 }
647 #endif
648
649 #ifndef CLIB_MARCH_VARIANT
650 u32
651 icmp_in2out (snat_main_t * sm,
652              vlib_buffer_t * b0,
653              ip4_header_t * ip0,
654              icmp46_header_t * icmp0,
655              u32 sw_if_index0,
656              u32 rx_fib_index0,
657              vlib_node_runtime_t * node,
658              u32 next0, u32 thread_index, void *d, void *e)
659 {
660   vlib_main_t *vm = vlib_get_main ();
661   ip4_address_t addr;
662   u16 port;
663   u32 fib_index;
664   nat_protocol_t protocol;
665   icmp_echo_header_t *echo0, *inner_echo0 = 0;
666   ip4_header_t *inner_ip0;
667   void *l4_header = 0;
668   icmp46_header_t *inner_icmp0;
669   u8 dont_translate;
670   u32 new_addr0, old_addr0;
671   u16 old_id0, new_id0;
672   u16 old_checksum0, new_checksum0;
673   ip_csum_t sum0;
674   u16 checksum0;
675   u32 next0_tmp;
676
677   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
678
679   next0_tmp =
680     sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
681                               &fib_index, &protocol, d, e, &dont_translate);
682   if (next0_tmp != ~0)
683     next0 = next0_tmp;
684   if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
685     goto out;
686
687   if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
688     {
689       sum0 =
690         ip_incremental_checksum_buffer (vm, b0,
691                                         (u8 *) icmp0 -
692                                         (u8 *) 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 = 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 = port;
719           if (PREDICT_FALSE (new_id0 != echo0->identifier))
720             {
721               old_id0 = echo0->identifier;
722               new_id0 = 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 = 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 NAT_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 = 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 NAT_PROTOCOL_UDP:
782             case NAT_PROTOCOL_TCP:
783               old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
784               new_id0 = 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] = 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   vlib_main_t *vm = vlib_get_main ();
823
824   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
825                        next0, thread_index, p_s0, 0);
826   snat_session_t *s0 = *p_s0;
827   if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
828     {
829       /* Accounting */
830       nat44_session_update_counters (s0, now,
831                                      vlib_buffer_length_in_chain
832                                      (vm, b0), thread_index);
833       /* Per-user LRU list maintenance */
834       nat44_session_update_lru (sm, s0, thread_index);
835     }
836   return next0;
837 }
838
839 static int
840 nat_in2out_sm_unknown_proto (snat_main_t * sm,
841                              vlib_buffer_t * b,
842                              ip4_header_t * ip, u32 rx_fib_index)
843 {
844   clib_bihash_kv_8_8_t kv, value;
845   snat_static_mapping_t *m;
846   u32 old_addr, new_addr;
847   ip_csum_t sum;
848
849   init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
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           u32 rx_fib_index0, rx_fib_index1;
915           u32 proto0, proto1;
916           snat_session_t *s0 = 0, *s1 = 0;
917           clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
918           u32 iph_offset0 = 0, iph_offset1 = 0;
919
920           /* Prefetch next iteration. */
921           {
922             vlib_buffer_t *p2, *p3;
923
924             p2 = vlib_get_buffer (vm, from[2]);
925             p3 = vlib_get_buffer (vm, from[3]);
926
927             vlib_prefetch_buffer_header (p2, LOAD);
928             vlib_prefetch_buffer_header (p3, LOAD);
929
930             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
931             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
932           }
933
934           /* speculatively enqueue b0 and b1 to the current next frame */
935           to_next[0] = bi0 = from[0];
936           to_next[1] = bi1 = from[1];
937           from += 2;
938           to_next += 2;
939           n_left_from -= 2;
940           n_left_to_next -= 2;
941
942           b0 = vlib_get_buffer (vm, bi0);
943           b1 = vlib_get_buffer (vm, bi1);
944
945           if (is_output_feature)
946             iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
947
948           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
949                                   iph_offset0);
950
951           udp0 = ip4_next_header (ip0);
952           tcp0 = (tcp_header_t *) udp0;
953           icmp0 = (icmp46_header_t *) udp0;
954
955           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
956           rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
957                                    sw_if_index0);
958
959           next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
960
961           if (PREDICT_FALSE (ip0->ttl == 1))
962             {
963               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
964               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
965                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
966                                            0);
967               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
968               goto trace00;
969             }
970
971           proto0 = ip_proto_to_nat_proto (ip0->protocol);
972
973           /* Next configured feature, probably ip4-lookup */
974           if (is_slow_path)
975             {
976               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
977                 {
978                   if (nat_in2out_sm_unknown_proto
979                       (sm, b0, ip0, rx_fib_index0))
980                     {
981                       next0 = SNAT_IN2OUT_NEXT_DROP;
982                       b0->error =
983                         node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
984                     }
985                   other_packets++;
986                   goto trace00;
987                 }
988
989               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
990                 {
991                   next0 = icmp_in2out_slow_path
992                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
993                      node, next0, now, thread_index, &s0);
994                   icmp_packets++;
995                   goto trace00;
996                 }
997             }
998           else
999             {
1000               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1001                 {
1002                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1003                   goto trace00;
1004                 }
1005
1006               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1007                 {
1008                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1009                   goto trace00;
1010                 }
1011             }
1012
1013           init_nat_k (&kv0, ip0->src_address,
1014                       vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1015                       proto0);
1016           if (PREDICT_FALSE
1017               (clib_bihash_search_8_8
1018                (&sm->per_thread_data[thread_index].in2out, &kv0,
1019                 &value0) != 0))
1020             {
1021               if (is_slow_path)
1022                 {
1023                   if (is_output_feature)
1024                     {
1025                       if (PREDICT_FALSE
1026                           (nat_not_translate_output_feature
1027                            (sm, ip0, proto0,
1028                             vnet_buffer (b0)->ip.reass.l4_src_port,
1029                             vnet_buffer (b0)->ip.reass.l4_dst_port,
1030                             thread_index, sw_if_index0)))
1031                         goto trace00;
1032
1033                       /*
1034                        * Send DHCP packets to the ipv4 stack, or we won't
1035                        * be able to use dhcp client on the outside interface
1036                        */
1037                       if (PREDICT_FALSE
1038                           (proto0 == NAT_PROTOCOL_UDP
1039                            && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1040                                clib_host_to_net_u16
1041                                (UDP_DST_PORT_dhcp_to_server))
1042                            && ip0->dst_address.as_u32 == 0xffffffff))
1043                         goto trace00;
1044                     }
1045                   else
1046                     {
1047                       if (PREDICT_FALSE
1048                           (snat_not_translate
1049                            (sm, node, sw_if_index0, ip0, proto0,
1050                             rx_fib_index0, thread_index)))
1051                         goto trace00;
1052                     }
1053
1054                   next0 = slow_path (sm, b0, ip0,
1055                                      ip0->src_address,
1056                                      vnet_buffer (b0)->ip.reass.l4_src_port,
1057                                      rx_fib_index0,
1058                                      proto0,
1059                                      &s0, node, next0, thread_index, now);
1060                   if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1061                     goto trace00;
1062
1063                   if (PREDICT_FALSE (!s0))
1064                     goto trace00;
1065                 }
1066               else
1067                 {
1068                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1069                   goto trace00;
1070                 }
1071             }
1072           else
1073             s0 =
1074               pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1075                                  value0.value);
1076
1077           b0->flags |= VNET_BUFFER_F_IS_NATED;
1078
1079           old_addr0 = ip0->src_address.as_u32;
1080           ip0->src_address = s0->out2in.addr;
1081           new_addr0 = ip0->src_address.as_u32;
1082           if (!is_output_feature)
1083             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1084
1085           sum0 = ip0->checksum;
1086           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1087                                  ip4_header_t,
1088                                  src_address /* changed member */ );
1089           ip0->checksum = ip_csum_fold (sum0);
1090
1091
1092           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1093             {
1094               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1095                 {
1096                   old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1097                   new_port0 = udp0->src_port = s0->out2in.port;
1098                   sum0 = tcp0->checksum;
1099                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1100                                          ip4_header_t,
1101                                          dst_address /* changed member */ );
1102                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1103                                          ip4_header_t /* cheat */ ,
1104                                          length /* changed member */ );
1105                   mss_clamping (sm->mss_clamping, tcp0, &sum0);
1106                   tcp0->checksum = ip_csum_fold (sum0);
1107                 }
1108               tcp_packets++;
1109             }
1110           else
1111             {
1112               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1113                 {
1114                   udp0->src_port = s0->out2in.port;
1115                   if (PREDICT_FALSE (udp0->checksum))
1116                     {
1117                       old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1118                       new_port0 = udp0->src_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_nat_proto (ip1->protocol);
1181
1182           /* Next configured feature, probably ip4-lookup */
1183           if (is_slow_path)
1184             {
1185               if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
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 == NAT_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 == NAT_PROTOCOL_OTHER))
1210                 {
1211                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1212                   goto trace01;
1213                 }
1214
1215               if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1216                 {
1217                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1218                   goto trace01;
1219                 }
1220             }
1221
1222           init_nat_k (&kv1, ip1->src_address,
1223                       vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1224                       proto1);
1225           if (PREDICT_FALSE
1226               (clib_bihash_search_8_8
1227                (&sm->per_thread_data[thread_index].in2out, &kv1,
1228                 &value1) != 0))
1229             {
1230               if (is_slow_path)
1231                 {
1232                   if (is_output_feature)
1233                     {
1234                       if (PREDICT_FALSE
1235                           (nat_not_translate_output_feature
1236                            (sm, ip1, proto1,
1237                             vnet_buffer (b1)->ip.reass.l4_src_port,
1238                             vnet_buffer (b1)->ip.reass.l4_dst_port,
1239                             thread_index, sw_if_index1)))
1240                         goto trace01;
1241
1242                       /*
1243                        * Send DHCP packets to the ipv4 stack, or we won't
1244                        * be able to use dhcp client on the outside interface
1245                        */
1246                       if (PREDICT_FALSE
1247                           (proto1 == NAT_PROTOCOL_UDP
1248                            && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1249                                clib_host_to_net_u16
1250                                (UDP_DST_PORT_dhcp_to_server))
1251                            && ip1->dst_address.as_u32 == 0xffffffff))
1252                         goto trace01;
1253                     }
1254                   else
1255                     {
1256                       if (PREDICT_FALSE
1257                           (snat_not_translate
1258                            (sm, node, sw_if_index1, ip1, proto1,
1259                             rx_fib_index1, thread_index)))
1260                         goto trace01;
1261                     }
1262
1263                   next1 =
1264                     slow_path (sm, b1, ip1, ip1->src_address,
1265                                vnet_buffer (b1)->ip.reass.l4_src_port,
1266                                rx_fib_index1, proto1, &s1, node, next1,
1267                                thread_index, now);
1268                   if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1269                     goto trace01;
1270
1271                   if (PREDICT_FALSE (!s1))
1272                     goto trace01;
1273                 }
1274               else
1275                 {
1276                   next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1277                   goto trace01;
1278                 }
1279             }
1280           else
1281             s1 =
1282               pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1283                                  value1.value);
1284
1285           b1->flags |= VNET_BUFFER_F_IS_NATED;
1286
1287           old_addr1 = ip1->src_address.as_u32;
1288           ip1->src_address = s1->out2in.addr;
1289           new_addr1 = ip1->src_address.as_u32;
1290           if (!is_output_feature)
1291             vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1292
1293           sum1 = ip1->checksum;
1294           sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1295                                  ip4_header_t,
1296                                  src_address /* changed member */ );
1297           ip1->checksum = ip_csum_fold (sum1);
1298
1299           if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1300             {
1301               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1302                 {
1303                   old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1304                   new_port1 = udp1->src_port = s1->out2in.port;
1305                   sum1 = tcp1->checksum;
1306                   sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1307                                          ip4_header_t,
1308                                          dst_address /* changed member */ );
1309                   sum1 = ip_csum_update (sum1, old_port1, new_port1,
1310                                          ip4_header_t /* cheat */ ,
1311                                          length /* changed member */ );
1312                   mss_clamping (sm->mss_clamping, tcp1, &sum1);
1313                   tcp1->checksum = ip_csum_fold (sum1);
1314                 }
1315               tcp_packets++;
1316             }
1317           else
1318             {
1319               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1320                 {
1321                   udp1->src_port = s1->out2in.port;
1322                   if (PREDICT_FALSE (udp1->checksum))
1323                     {
1324                       old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1325                       new_port1 = udp1->src_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           u32 rx_fib_index0;
1382           u32 proto0;
1383           snat_session_t *s0 = 0;
1384           clib_bihash_kv_8_8_t kv0, value0;
1385           u32 iph_offset0 = 0;
1386
1387           /* speculatively enqueue b0 to the current next frame */
1388           bi0 = from[0];
1389           to_next[0] = bi0;
1390           from += 1;
1391           to_next += 1;
1392           n_left_from -= 1;
1393           n_left_to_next -= 1;
1394
1395           b0 = vlib_get_buffer (vm, bi0);
1396           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1397
1398           if (is_output_feature)
1399             iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1400
1401           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1402                                   iph_offset0);
1403
1404           udp0 = ip4_next_header (ip0);
1405           tcp0 = (tcp_header_t *) udp0;
1406           icmp0 = (icmp46_header_t *) udp0;
1407
1408           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1409           rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1410                                    sw_if_index0);
1411
1412           if (PREDICT_FALSE (ip0->ttl == 1))
1413             {
1414               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1415               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1416                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1417                                            0);
1418               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1419               goto trace0;
1420             }
1421
1422           proto0 = ip_proto_to_nat_proto (ip0->protocol);
1423
1424           /* Next configured feature, probably ip4-lookup */
1425           if (is_slow_path)
1426             {
1427               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1428                 {
1429                   if (nat_in2out_sm_unknown_proto
1430                       (sm, b0, ip0, rx_fib_index0))
1431                     {
1432                       next0 = SNAT_IN2OUT_NEXT_DROP;
1433                       b0->error =
1434                         node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1435                     }
1436                   other_packets++;
1437                   goto trace0;
1438                 }
1439
1440               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1441                 {
1442                   next0 = icmp_in2out_slow_path
1443                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1444                      next0, now, thread_index, &s0);
1445                   icmp_packets++;
1446                   goto trace0;
1447                 }
1448             }
1449           else
1450             {
1451               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1452                 {
1453                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1454                   goto trace0;
1455                 }
1456
1457               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1458                 {
1459                   next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1460                   goto trace0;
1461                 }
1462             }
1463
1464           init_nat_k (&kv0, ip0->src_address,
1465                       vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1466                       proto0);
1467
1468           if (clib_bihash_search_8_8
1469               (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1470             {
1471               if (is_slow_path)
1472                 {
1473                   if (is_output_feature)
1474                     {
1475                       if (PREDICT_FALSE
1476                           (nat_not_translate_output_feature
1477                            (sm, ip0, proto0,
1478                             vnet_buffer (b0)->ip.reass.l4_src_port,
1479                             vnet_buffer (b0)->ip.reass.l4_dst_port,
1480                             thread_index, sw_if_index0)))
1481                         goto trace0;
1482
1483                       /*
1484                        * Send DHCP packets to the ipv4 stack, or we won't
1485                        * be able to use dhcp client on the outside interface
1486                        */
1487                       if (PREDICT_FALSE
1488                           (proto0 == NAT_PROTOCOL_UDP
1489                            && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1490                                clib_host_to_net_u16
1491                                (UDP_DST_PORT_dhcp_to_server))
1492                            && ip0->dst_address.as_u32 == 0xffffffff))
1493                         goto trace0;
1494                     }
1495                   else
1496                     {
1497                       if (PREDICT_FALSE
1498                           (snat_not_translate
1499                            (sm, node, sw_if_index0, ip0, proto0,
1500                             rx_fib_index0, thread_index)))
1501                         goto trace0;
1502                     }
1503
1504                   next0 = slow_path (sm, b0, ip0,
1505                                      ip0->src_address,
1506                                      vnet_buffer (b0)->ip.reass.l4_src_port,
1507                                      rx_fib_index0,
1508                                      proto0,
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 == NAT_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->mss_clamping, 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                   udp0->src_port = s0->out2in.port;
1565                   if (PREDICT_FALSE (udp0->checksum))
1566                     {
1567                       old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1568                       new_port0 = udp0->src_port;
1569                       sum0 = udp0->checksum;
1570                       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address      /* changed member */
1571                         );
1572                       sum0 =
1573                         ip_csum_update (sum0, old_port0, new_port0,
1574                                         ip4_header_t /* cheat */ ,
1575                                         length /* changed member */ );
1576                       udp0->checksum = ip_csum_fold (sum0);
1577                     }
1578                 }
1579               udp_packets++;
1580             }
1581
1582           /* Accounting */
1583           nat44_session_update_counters (s0, now,
1584                                          vlib_buffer_length_in_chain (vm, b0),
1585                                          thread_index);
1586           /* Per-user LRU list maintenance */
1587           nat44_session_update_lru (sm, s0, thread_index);
1588
1589         trace0:
1590           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1591                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1592             {
1593               snat_in2out_trace_t *t =
1594                 vlib_add_trace (vm, node, b0, sizeof (*t));
1595               t->is_slow_path = is_slow_path;
1596               t->sw_if_index = sw_if_index0;
1597               t->next_index = next0;
1598               t->session_index = ~0;
1599               if (s0)
1600                 t->session_index =
1601                   s0 - sm->per_thread_data[thread_index].sessions;
1602             }
1603
1604           pkts_processed += next0 == SNAT_IN2OUT_NEXT_LOOKUP;
1605
1606           /* verify speculative enqueue, maybe switch current next frame */
1607           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1608                                            to_next, n_left_to_next,
1609                                            bi0, next0);
1610         }
1611
1612       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1613     }
1614
1615   vlib_node_increment_counter (vm, stats_node_index,
1616                                SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1617                                pkts_processed);
1618   vlib_node_increment_counter (vm, stats_node_index,
1619                                SNAT_IN2OUT_ERROR_TCP_PACKETS, tcp_packets);
1620   vlib_node_increment_counter (vm, stats_node_index,
1621                                SNAT_IN2OUT_ERROR_UDP_PACKETS, udp_packets);
1622   vlib_node_increment_counter (vm, stats_node_index,
1623                                SNAT_IN2OUT_ERROR_ICMP_PACKETS, icmp_packets);
1624   vlib_node_increment_counter (vm, stats_node_index,
1625                                SNAT_IN2OUT_ERROR_OTHER_PACKETS,
1626                                other_packets);
1627   vlib_node_increment_counter (vm, stats_node_index,
1628                                SNAT_IN2OUT_ERROR_FRAGMENTS, fragments);
1629
1630   return frame->n_vectors;
1631 }
1632
1633 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1634                                  vlib_node_runtime_t * node,
1635                                  vlib_frame_t * frame)
1636 {
1637   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1638                                      0);
1639 }
1640
1641 /* *INDENT-OFF* */
1642 VLIB_REGISTER_NODE (snat_in2out_node) = {
1643   .name = "nat44-in2out",
1644   .vector_size = sizeof (u32),
1645   .format_trace = format_snat_in2out_trace,
1646   .type = VLIB_NODE_TYPE_INTERNAL,
1647
1648   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1649   .error_strings = snat_in2out_error_strings,
1650
1651   .runtime_data_bytes = sizeof (snat_runtime_t),
1652
1653   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1654
1655   /* edit / add dispositions here */
1656   .next_nodes = {
1657     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1658     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1659     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1660     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1661   },
1662 };
1663 /* *INDENT-ON* */
1664
1665 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1666                                         vlib_node_runtime_t * node,
1667                                         vlib_frame_t * frame)
1668 {
1669   return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1670                                      1);
1671 }
1672
1673 /* *INDENT-OFF* */
1674 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1675   .name = "nat44-in2out-output",
1676   .vector_size = sizeof (u32),
1677   .format_trace = format_snat_in2out_trace,
1678   .type = VLIB_NODE_TYPE_INTERNAL,
1679
1680   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1681   .error_strings = snat_in2out_error_strings,
1682
1683   .runtime_data_bytes = sizeof (snat_runtime_t),
1684
1685   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1686
1687   /* edit / add dispositions here */
1688   .next_nodes = {
1689     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1690     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1691     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1692     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1693   },
1694 };
1695 /* *INDENT-ON* */
1696
1697 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1698                                           vlib_node_runtime_t * node,
1699                                           vlib_frame_t * frame)
1700 {
1701   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1702                                      0);
1703 }
1704
1705 /* *INDENT-OFF* */
1706 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1707   .name = "nat44-in2out-slowpath",
1708   .vector_size = sizeof (u32),
1709   .format_trace = format_snat_in2out_trace,
1710   .type = VLIB_NODE_TYPE_INTERNAL,
1711
1712   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1713   .error_strings = snat_in2out_error_strings,
1714
1715   .runtime_data_bytes = sizeof (snat_runtime_t),
1716
1717   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1718
1719   /* edit / add dispositions here */
1720   .next_nodes = {
1721     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1722     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1723     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1724     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1725   },
1726 };
1727 /* *INDENT-ON* */
1728
1729 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1730                                                  vlib_node_runtime_t * node,
1731                                                  vlib_frame_t * frame)
1732 {
1733   return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1734                                      1);
1735 }
1736
1737 /* *INDENT-OFF* */
1738 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1739   .name = "nat44-in2out-output-slowpath",
1740   .vector_size = sizeof (u32),
1741   .format_trace = format_snat_in2out_trace,
1742   .type = VLIB_NODE_TYPE_INTERNAL,
1743
1744   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1745   .error_strings = snat_in2out_error_strings,
1746
1747   .runtime_data_bytes = sizeof (snat_runtime_t),
1748
1749   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1750
1751   /* edit / add dispositions here */
1752   .next_nodes = {
1753     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1754     [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1755     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1756     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1757   },
1758 };
1759 /* *INDENT-ON* */
1760
1761 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1762                                       vlib_node_runtime_t * node,
1763                                       vlib_frame_t * frame)
1764 {
1765   u32 n_left_from, *from, *to_next;
1766   snat_in2out_next_t next_index;
1767   u32 pkts_processed = 0;
1768   snat_main_t *sm = &snat_main;
1769   u32 stats_node_index;
1770   int is_hairpinning = 0;
1771
1772   stats_node_index = sm->in2out_fast_node_index;
1773
1774   from = vlib_frame_vector_args (frame);
1775   n_left_from = frame->n_vectors;
1776   next_index = node->cached_next_index;
1777
1778   while (n_left_from > 0)
1779     {
1780       u32 n_left_to_next;
1781
1782       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1783
1784       while (n_left_from > 0 && n_left_to_next > 0)
1785         {
1786           u32 bi0;
1787           vlib_buffer_t *b0;
1788           u32 next0;
1789           u32 sw_if_index0;
1790           ip4_header_t *ip0;
1791           ip_csum_t sum0;
1792           u32 new_addr0, old_addr0;
1793           u16 old_port0, new_port0;
1794           udp_header_t *udp0;
1795           tcp_header_t *tcp0;
1796           icmp46_header_t *icmp0;
1797           u32 proto0;
1798           u32 rx_fib_index0;
1799           ip4_address_t sm0_addr;
1800           u16 sm0_port;
1801           u32 sm0_fib_index;
1802
1803
1804           /* speculatively enqueue b0 to the current next frame */
1805           bi0 = from[0];
1806           to_next[0] = bi0;
1807           from += 1;
1808           to_next += 1;
1809           n_left_from -= 1;
1810           n_left_to_next -= 1;
1811
1812           b0 = vlib_get_buffer (vm, bi0);
1813           next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1814
1815           ip0 = vlib_buffer_get_current (b0);
1816           udp0 = ip4_next_header (ip0);
1817           tcp0 = (tcp_header_t *) udp0;
1818           icmp0 = (icmp46_header_t *) udp0;
1819
1820           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1821           rx_fib_index0 =
1822             ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1823
1824           if (PREDICT_FALSE (ip0->ttl == 1))
1825             {
1826               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1827               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1828                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1829                                            0);
1830               next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1831               goto trace0;
1832             }
1833
1834           proto0 = ip_proto_to_nat_proto (ip0->protocol);
1835
1836           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1837             goto trace0;
1838
1839           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1840             {
1841               next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1842                                    rx_fib_index0, node, next0, ~0, 0, 0);
1843               goto trace0;
1844             }
1845
1846           if (snat_static_mapping_match
1847               (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1848                &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0))
1849             {
1850               b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1851               next0 = SNAT_IN2OUT_NEXT_DROP;
1852               goto trace0;
1853             }
1854
1855           new_addr0 = sm0_addr.as_u32;
1856           new_port0 = sm0_port;
1857           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1858           old_addr0 = ip0->src_address.as_u32;
1859           ip0->src_address.as_u32 = new_addr0;
1860
1861           sum0 = ip0->checksum;
1862           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1863                                  ip4_header_t,
1864                                  src_address /* changed member */ );
1865           ip0->checksum = ip_csum_fold (sum0);
1866
1867           if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1868             {
1869               old_port0 = udp0->src_port;
1870               udp0->src_port = new_port0;
1871
1872               if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1873                 {
1874                   sum0 = tcp0->checksum;
1875                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1876                                          ip4_header_t,
1877                                          dst_address /* changed member */ );
1878                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1879                                          ip4_header_t /* cheat */ ,
1880                                          length /* changed member */ );
1881                   mss_clamping (sm->mss_clamping, tcp0, &sum0);
1882                   tcp0->checksum = ip_csum_fold (sum0);
1883                 }
1884               else if (udp0->checksum)
1885                 {
1886                   sum0 = udp0->checksum;
1887                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1888                                          ip4_header_t,
1889                                          dst_address /* changed member */ );
1890                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1891                                          ip4_header_t /* cheat */ ,
1892                                          length /* changed member */ );
1893                   udp0->checksum = ip_csum_fold (sum0);
1894                 }
1895             }
1896           else
1897             {
1898               if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1899                 {
1900                   sum0 = tcp0->checksum;
1901                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1902                                          ip4_header_t,
1903                                          dst_address /* changed member */ );
1904                   mss_clamping (sm->mss_clamping, tcp0, &sum0);
1905                   tcp0->checksum = ip_csum_fold (sum0);
1906                 }
1907               else if (udp0->checksum)
1908                 {
1909                   sum0 = udp0->checksum;
1910                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1911                                          ip4_header_t,
1912                                          dst_address /* changed member */ );
1913                   udp0->checksum = ip_csum_fold (sum0);
1914                 }
1915             }
1916
1917           /* Hairpinning */
1918           snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1919                             0 /* do_trace */ );
1920           is_hairpinning = 1;
1921
1922         trace0:
1923           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1924                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1925             {
1926               snat_in2out_trace_t *t =
1927                 vlib_add_trace (vm, node, b0, sizeof (*t));
1928               t->sw_if_index = sw_if_index0;
1929               t->next_index = next0;
1930               t->is_hairpinning = is_hairpinning;
1931             }
1932
1933           pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1934
1935           /* verify speculative enqueue, maybe switch current next frame */
1936           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1937                                            to_next, n_left_to_next,
1938                                            bi0, next0);
1939         }
1940
1941       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1942     }
1943
1944   vlib_node_increment_counter (vm, stats_node_index,
1945                                SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1946                                pkts_processed);
1947   return frame->n_vectors;
1948 }
1949
1950
1951 /* *INDENT-OFF* */
1952 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1953   .name = "nat44-in2out-fast",
1954   .vector_size = sizeof (u32),
1955   .format_trace = format_snat_in2out_fast_trace,
1956   .type = VLIB_NODE_TYPE_INTERNAL,
1957
1958   .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1959   .error_strings = snat_in2out_error_strings,
1960
1961   .runtime_data_bytes = sizeof (snat_runtime_t),
1962
1963   .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1964
1965   /* edit / add dispositions here */
1966   .next_nodes = {
1967     [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1968     [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1969     [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1970     [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1971   },
1972 };
1973 /* *INDENT-ON* */
1974
1975 /*
1976  * fd.io coding-style-patch-verification: ON
1977  *
1978  * Local Variables:
1979  * eval: (c-set-style "gnu")
1980  * End:
1981  */