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