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