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