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