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