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