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