295958927cdbb04697bcc2189a1e39dca48231a1
[vpp.git] / src / plugins / nat / in2out_ed.c
1 /*
2  * Copyright (c) 2018 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 inside to outside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
28 #include <nat/nat.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
34
35 static char *nat_in2out_ed_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nat_in2out_ed_error
38 #undef _
39 };
40
41 typedef struct
42 {
43   u32 sw_if_index;
44   u32 next_index;
45   u32 session_index;
46   u32 is_slow_path;
47 } nat_in2out_ed_trace_t;
48
49 static u8 *
50 format_nat_in2out_ed_trace (u8 * s, va_list * args)
51 {
52   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54   nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
55   char *tag;
56
57   tag =
58     t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
59     "NAT44_IN2OUT_ED_FAST_PATH";
60
61   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62               t->sw_if_index, t->next_index, t->session_index);
63
64   return s;
65 }
66
67 #ifndef CLIB_MARCH_VARIANT
68 int
69 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
70 {
71   snat_main_t *sm = &snat_main;
72   nat44_is_idle_session_ctx_t *ctx = arg;
73   snat_session_t *s;
74   u64 sess_timeout_time;
75   nat_ed_ses_key_t ed_key;
76   clib_bihash_kv_16_8_t ed_kv;
77   int i;
78   snat_address_t *a;
79   snat_session_key_t key;
80   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
81                                                        ctx->thread_index);
82
83   s = pool_elt_at_index (tsm->sessions, kv->value);
84   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
85   if (ctx->now >= sess_timeout_time)
86     {
87       if (is_fwd_bypass_session (s))
88         goto delete;
89
90       ed_key.l_addr = s->out2in.addr;
91       ed_key.r_addr = s->ext_host_addr;
92       ed_key.fib_index = s->out2in.fib_index;
93       if (snat_is_unk_proto_session (s))
94         {
95           ed_key.proto = s->in2out.port;
96           ed_key.r_port = 0;
97           ed_key.l_port = 0;
98         }
99       else
100         {
101           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
102           ed_key.l_port = s->out2in.port;
103           ed_key.r_port = s->ext_host_port;
104         }
105       ed_kv.key[0] = ed_key.as_u64[0];
106       ed_kv.key[1] = ed_key.as_u64[1];
107       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
108         nat_elog_warn ("out2in_ed key del failed");
109
110       if (snat_is_unk_proto_session (s))
111         goto delete;
112
113       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
114                                            s->in2out.addr.as_u32,
115                                            s->out2in.addr.as_u32,
116                                            s->in2out.protocol,
117                                            s->in2out.port,
118                                            s->out2in.port,
119                                            s->in2out.fib_index);
120
121       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
122                              &s->in2out.addr, s->in2out.port,
123                              &s->ext_host_nat_addr, s->ext_host_nat_port,
124                              &s->out2in.addr, s->out2in.port,
125                              &s->ext_host_addr, s->ext_host_port,
126                              s->in2out.protocol, is_twice_nat_session (s));
127
128       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
129                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
130                    ctx->thread_index);
131
132       if (is_twice_nat_session (s))
133         {
134           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
135             {
136               key.protocol = s->in2out.protocol;
137               key.port = s->ext_host_nat_port;
138               a = sm->twice_nat_addresses + i;
139               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
140                 {
141                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
142                                                       ctx->thread_index,
143                                                       &key);
144                   break;
145                 }
146             }
147         }
148
149       if (snat_is_session_static (s))
150         goto delete;
151
152       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
153                                           &s->out2in);
154     delete:
155       nat44_delete_session (sm, s, ctx->thread_index);
156       return 1;
157     }
158
159   return 0;
160 }
161 #endif
162
163 static inline u32
164 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
165                           ip4_header_t * ip0, icmp46_header_t * icmp0,
166                           u32 sw_if_index0, u32 rx_fib_index0,
167                           vlib_node_runtime_t * node, u32 next0, f64 now,
168                           u32 thread_index, snat_session_t ** p_s0)
169 {
170   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
171                        next0, thread_index, p_s0, 0);
172   snat_session_t *s0 = *p_s0;
173   if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
174     {
175       /* Accounting */
176       nat44_session_update_counters (s0, now,
177                                      vlib_buffer_length_in_chain
178                                      (sm->vlib_main, b0), thread_index);
179       /* Per-user LRU list maintenance */
180       nat44_session_update_lru (sm, s0, thread_index);
181     }
182   return next0;
183 }
184
185 static_always_inline u16
186 snat_random_port (u16 min, u16 max)
187 {
188   snat_main_t *sm = &snat_main;
189   return min + random_u32 (&sm->random_seed) /
190     (random_u32_max () / (max - min + 1) + 1);
191 }
192
193 static int
194 nat_ed_alloc_addr_and_port (snat_main_t * sm, u32 fib_index,
195                             u32 thread_index, nat_ed_ses_key_t * key,
196                             snat_session_key_t * key1, u16 port_per_thread,
197                             u32 snat_thread_index,
198                             snat_session_t * s,
199                             clib_bihash_kv_16_8_t * out2in_ed_kv)
200 {
201   int i;
202   snat_address_t *a, *ga = 0;
203   u32 portnum;
204   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
205
206   const u16 port_thread_offset = (port_per_thread * snat_thread_index) + 1024;
207
208   for (i = 0; i < vec_len (sm->addresses); i++)
209     {
210       a = sm->addresses + i;
211       switch (key1->protocol)
212         {
213 #define _(N, j, n, unused)                                                    \
214   case SNAT_PROTOCOL_##N:                                                     \
215     if (a->fib_index == fib_index)                                            \
216       {                                                                       \
217         u16 port = snat_random_port (1, port_per_thread);                     \
218         u16 attempts = port_per_thread;                                       \
219         while (attempts > 0)                                                  \
220           {                                                                   \
221             --attempts;                                                       \
222             portnum = port_thread_offset + port;                              \
223             make_ed_kv (out2in_ed_kv, &a->addr, &key->r_addr, key->proto,     \
224                         s->out2in.fib_index, clib_host_to_net_u16 (portnum),  \
225                         key->r_port);                                         \
226             out2in_ed_kv->value = s - tsm->sessions;                          \
227             int rv = clib_bihash_add_del_16_8 (&tsm->out2in_ed, out2in_ed_kv, \
228                                                2 /* is_add */);               \
229             if (0 == rv)                                                      \
230               {                                                               \
231                 ++a->busy_##n##_port_refcounts[portnum];                      \
232                 a->busy_##n##_ports_per_thread[thread_index]++;               \
233                 a->busy_##n##_ports++;                                        \
234                 key1->addr = a->addr;                                         \
235                 key1->port = clib_host_to_net_u16 (portnum);                  \
236                 return 0;                                                     \
237               }                                                               \
238             port = (port + 1) % port_per_thread;                              \
239           }                                                                   \
240       }                                                                       \
241     else if (a->fib_index == ~0)                                              \
242       {                                                                       \
243         ga = a;                                                               \
244       }                                                                       \
245     break;
246
247           foreach_snat_protocol;
248         default:
249           nat_elog_info ("unknown protocol");
250           return 1;
251         }
252     }
253
254   if (ga)
255     {
256       /* fake fib_index to reuse macro */
257       fib_index = ~0;
258       a = ga;
259       switch (key1->protocol)
260         {
261           foreach_snat_protocol;
262         default:
263           nat_elog_info ("unknown protocol");
264           return 1;
265         }
266     }
267
268 #undef _
269
270   /* Totally out of translations to use... */
271   snat_ipfix_logging_addresses_exhausted (thread_index, 0);
272   return 1;
273 }
274
275 static u32
276 slow_path_ed (snat_main_t * sm,
277               vlib_buffer_t * b,
278               u32 rx_fib_index,
279               clib_bihash_kv_16_8_t * kv,
280               snat_session_t ** sessionp,
281               vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
282 {
283   snat_session_t *s = NULL;
284   snat_user_t *u = NULL;
285   snat_session_key_t key0, key1;
286   lb_nat_type_t lb = 0, is_sm = 0;
287   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
288   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
289   u32 proto = ip_proto_to_snat_proto (key->proto);
290   nat_outside_fib_t *outside_fib;
291   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
292   clib_bihash_kv_16_8_t out2in_ed_kv;
293   bool out2in_ed_inserted = false;
294   u8 identity_nat;
295   fib_prefix_t pfx = {
296     .fp_proto = FIB_PROTOCOL_IP4,
297     .fp_len = 32,
298     .fp_addr = {
299                 .ip4.as_u32 = key->r_addr.as_u32,
300                 },
301   };
302   nat44_is_idle_session_ctx_t ctx;
303
304   if (PREDICT_TRUE (proto == SNAT_PROTOCOL_TCP))
305     {
306       if (PREDICT_FALSE
307           (!tcp_flags_is_init
308            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
309         {
310           b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
311           return NAT_NEXT_DROP;
312         }
313     }
314
315   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
316     {
317       if (!nat_global_lru_free_one (sm, thread_index, now))
318         {
319           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
320           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
321           nat_elog_notice ("maximum sessions exceeded");
322           return NAT_NEXT_DROP;
323         }
324     }
325
326   key0.addr = key->l_addr;
327   key0.port = key->l_port;
328   key1.protocol = key0.protocol = proto;
329   key0.fib_index = rx_fib_index;
330   key1.fib_index = sm->outside_fib_index;
331
332   /* First try to match static mapping by local address and port */
333   if (snat_static_mapping_match
334       (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
335     {
336       u =
337         nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
338       if (!u)
339         {
340           nat_elog_warn ("create NAT user failed");
341           b->error = node->errors[NAT_IN2OUT_ED_ERROR_CANNOT_CREATE_USER];
342           goto drop;
343         }
344
345       s = nat_ed_session_alloc (sm, u, thread_index, now);
346       if (!s)
347         {
348           nat_elog_warn ("create NAT session failed");
349           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
350           goto drop;
351         }
352       switch (vec_len (sm->outside_fibs))
353         {
354         case 0:
355           s->out2in.fib_index = sm->outside_fib_index;
356           break;
357         case 1:
358           s->out2in.fib_index = sm->outside_fibs[0].fib_index;
359           break;
360         default:
361           /* *INDENT-OFF* */
362           vec_foreach (outside_fib, sm->outside_fibs)
363           {
364             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
365             if (FIB_NODE_INDEX_INVALID != fei)
366               {
367                 if (fib_entry_get_resolving_interface (fei) != ~0)
368                   {
369                     s->out2in.fib_index = outside_fib->fib_index;
370                     break;
371                   }
372               }
373           }
374           /* *INDENT-ON* */
375           break;
376         }
377
378       /* Try to create dynamic translation */
379       if (nat_ed_alloc_addr_and_port (sm, rx_fib_index,
380                                       thread_index, key, &key1,
381                                       sm->port_per_thread,
382                                       tsm->snat_thread_index, s,
383                                       &out2in_ed_kv))
384         {
385           nat_elog_notice ("addresses exhausted");
386           b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
387           goto drop;
388         }
389
390       out2in_ed_inserted = true;
391     }
392   else
393     {
394       if (PREDICT_FALSE (identity_nat))
395         {
396           *sessionp = s;
397           return next;
398         }
399       is_sm = 1;
400       u =
401         nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
402       if (!u)
403         {
404           nat_elog_warn ("create NAT user failed");
405           b->error = node->errors[NAT_IN2OUT_ED_ERROR_CANNOT_CREATE_USER];
406           goto drop;
407         }
408
409       s = nat_ed_session_alloc (sm, u, thread_index, now);
410       if (!s)
411         {
412           nat44_delete_user_with_no_session (sm, u, thread_index);
413           nat_elog_warn ("create NAT session failed");
414           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
415           goto drop;
416         }
417       switch (vec_len (sm->outside_fibs))
418         {
419         case 0:
420           s->out2in.fib_index = sm->outside_fib_index;
421           break;
422         case 1:
423           s->out2in.fib_index = sm->outside_fibs[0].fib_index;
424           break;
425         default:
426           /* *INDENT-OFF* */
427           vec_foreach (outside_fib, sm->outside_fibs)
428           {
429             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
430             if (FIB_NODE_INDEX_INVALID != fei)
431               {
432                 if (fib_entry_get_resolving_interface (fei) != ~0)
433                   {
434                     s->out2in.fib_index = outside_fib->fib_index;
435                     break;
436                   }
437               }
438           }
439           /* *INDENT-ON* */
440           break;
441         }
442
443       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
444
445
446       make_ed_kv (&out2in_ed_kv, &key1.addr, &key->r_addr, key->proto,
447                   s->out2in.fib_index, key1.port, key->r_port);
448       out2in_ed_kv.value = s - tsm->sessions;
449       if (clib_bihash_add_or_overwrite_stale_16_8
450           (&tsm->out2in_ed, &out2in_ed_kv, nat44_o2i_ed_is_idle_session_cb,
451            &ctx))
452         nat_elog_notice ("out2in-ed key add failed");
453       out2in_ed_inserted = true;
454     }
455
456   user_session_increment (sm, u, is_sm);
457   if (lb)
458     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
459   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
460   s->ext_host_addr = key->r_addr;
461   s->ext_host_port = key->r_port;
462   s->in2out = key0;
463   s->out2in = key1;
464   s->out2in.protocol = key0.protocol;
465
466   kv->value = s - tsm->sessions;
467   ctx.now = now;
468   ctx.thread_index = thread_index;
469   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
470                                                nat44_i2o_ed_is_idle_session_cb,
471                                                &ctx))
472     nat_elog_notice ("in2out-ed key add failed");
473
474   *sessionp = s;
475
476   /* log NAT event */
477   snat_ipfix_logging_nat44_ses_create (thread_index,
478                                        s->in2out.addr.as_u32,
479                                        s->out2in.addr.as_u32,
480                                        s->in2out.protocol,
481                                        s->in2out.port,
482                                        s->out2in.port, s->in2out.fib_index);
483
484   nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
485                          &s->in2out.addr, s->in2out.port,
486                          &s->ext_host_nat_addr, s->ext_host_nat_port,
487                          &s->out2in.addr, s->out2in.port,
488                          &s->ext_host_addr, s->ext_host_port,
489                          s->in2out.protocol, 0);
490
491   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
492                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
493                &s->ext_host_nat_addr, s->ext_host_nat_port,
494                s->in2out.protocol, s->in2out.fib_index, s->flags,
495                thread_index, 0);
496
497   return next;
498 drop:
499   if (out2in_ed_inserted)
500     {
501       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &out2in_ed_kv, 0))
502         nat_elog_notice ("out2in-ed key del failed");
503     }
504   if (s)
505     {
506       nat_free_session_data (sm, s, thread_index, 0);
507       nat44_ed_delete_session (sm, s, thread_index, 1);
508     }
509   if (u)
510     {
511       nat44_delete_user_with_no_session (sm, u, thread_index);
512     }
513   return NAT_NEXT_DROP;
514 }
515
516 static_always_inline int
517 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
518                         u32 sw_if_index, ip4_header_t * ip, u32 proto,
519                         u32 rx_fib_index, u32 thread_index)
520 {
521   udp_header_t *udp = ip4_next_header (ip);
522   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
523   clib_bihash_kv_16_8_t kv, value;
524   snat_session_key_t key0, key1;
525
526   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
527               sm->outside_fib_index, udp->dst_port, udp->src_port);
528
529   /* NAT packet aimed at external address if */
530   /* has active sessions */
531   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
532     {
533       key0.addr = ip->dst_address;
534       key0.port = udp->dst_port;
535       key0.protocol = proto;
536       key0.fib_index = sm->outside_fib_index;
537       /* or is static mappings */
538       if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
539         return 0;
540     }
541   else
542     return 0;
543
544   if (sm->forwarding_enabled)
545     return 1;
546
547   return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
548                                   rx_fib_index);
549 }
550
551 static_always_inline int
552 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
553                                       u32 thread_index, f64 now,
554                                       vlib_main_t * vm, vlib_buffer_t * b)
555 {
556   nat_ed_ses_key_t key;
557   clib_bihash_kv_16_8_t kv, value;
558   snat_session_t *s = 0;
559   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
560
561   if (!sm->forwarding_enabled)
562     return 0;
563
564   if (ip->protocol == IP_PROTOCOL_ICMP)
565     {
566       key.as_u64[0] = key.as_u64[1] = 0;
567       if (get_icmp_i2o_ed_key (b, ip, &key))
568         return 0;
569       key.fib_index = 0;
570       kv.key[0] = key.as_u64[0];
571       kv.key[1] = key.as_u64[1];
572     }
573   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
574     {
575       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
576                   vnet_buffer (b)->ip.reass.l4_src_port,
577                   vnet_buffer (b)->ip.reass.l4_dst_port);
578     }
579   else
580     {
581       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
582                   0);
583     }
584
585   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
586     {
587       s = pool_elt_at_index (tsm->sessions, value.value);
588       if (is_fwd_bypass_session (s))
589         {
590           if (ip->protocol == IP_PROTOCOL_TCP)
591             {
592               if (nat44_set_tcp_session_state_i2o
593                   (sm, now, s, b, thread_index))
594                 return 1;
595             }
596           /* Accounting */
597           nat44_session_update_counters (s, now,
598                                          vlib_buffer_length_in_chain (vm, b),
599                                          thread_index);
600           /* Per-user LRU list maintenance */
601           nat44_session_update_lru (sm, s, thread_index);
602           return 1;
603         }
604       else
605         return 0;
606     }
607
608   return 0;
609 }
610
611 static_always_inline int
612 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
613                                        u8 proto, u16 src_port, u16 dst_port,
614                                        u32 thread_index, u32 rx_sw_if_index,
615                                        u32 tx_sw_if_index)
616 {
617   clib_bihash_kv_16_8_t kv, value;
618   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
619   snat_interface_t *i;
620   snat_session_t *s;
621   u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
622   u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
623
624   /* src NAT check */
625   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
626               src_port, dst_port);
627   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
628     {
629       s = pool_elt_at_index (tsm->sessions, value.value);
630       if (nat44_is_ses_closed (s))
631         {
632           nat_free_session_data (sm, s, thread_index, 0);
633           nat44_delete_session (sm, s, thread_index);
634         }
635       else
636         s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
637       return 1;
638     }
639
640   /* dst NAT check */
641   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
642               dst_port, src_port);
643   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
644     {
645       s = pool_elt_at_index (tsm->sessions, value.value);
646       if (is_fwd_bypass_session (s))
647         return 0;
648
649       /* hairpinning */
650       /* *INDENT-OFF* */
651       pool_foreach (i, sm->output_feature_interfaces,
652       ({
653         if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
654            return 0;
655       }));
656       /* *INDENT-ON* */
657       return 1;
658     }
659
660   return 0;
661 }
662
663 #ifndef CLIB_MARCH_VARIANT
664 u32
665 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
666                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
667                       u8 * p_proto, snat_session_key_t * p_value,
668                       u8 * p_dont_translate, void *d, void *e)
669 {
670   u32 sw_if_index;
671   u32 rx_fib_index;
672   nat_ed_ses_key_t key;
673   snat_session_t *s = 0;
674   u8 dont_translate = 0;
675   clib_bihash_kv_16_8_t kv, value;
676   u32 next = ~0;
677   int err;
678   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
679
680   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
681   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
682
683   key.as_u64[0] = key.as_u64[1] = 0;
684   err = get_icmp_i2o_ed_key (b, ip, &key);
685   if (err != 0)
686     {
687       b->error = node->errors[err];
688       next = NAT_NEXT_DROP;
689       goto out;
690     }
691   key.fib_index = rx_fib_index;
692
693   kv.key[0] = key.as_u64[0];
694   kv.key[1] = key.as_u64[1];
695
696   if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
697     {
698       if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
699         {
700           if (PREDICT_FALSE
701               (nat44_ed_not_translate_output_feature
702                (sm, ip, key.proto, key.l_port, key.r_port, thread_index,
703                 sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_TX])))
704             {
705               dont_translate = 1;
706               goto out;
707             }
708         }
709       else
710         {
711           if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
712                                                      ip, SNAT_PROTOCOL_ICMP,
713                                                      rx_fib_index,
714                                                      thread_index)))
715             {
716               dont_translate = 1;
717               goto out;
718             }
719         }
720
721       if (PREDICT_FALSE
722           (icmp_type_is_error_message
723            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
724         {
725           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
726           next = NAT_NEXT_DROP;
727           goto out;
728         }
729
730       next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
731                            thread_index, vlib_time_now (sm->vlib_main));
732
733       if (PREDICT_FALSE (next == NAT_NEXT_DROP))
734         goto out;
735
736       if (!s)
737         {
738           dont_translate = 1;
739           goto out;
740         }
741     }
742   else
743     {
744       if (PREDICT_FALSE
745           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
746            ICMP4_echo_request
747            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
748            ICMP4_echo_reply
749            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
750                                            reass.icmp_type_or_tcp_flags)))
751         {
752           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
753           next = NAT_NEXT_DROP;
754           goto out;
755         }
756
757       s = pool_elt_at_index (tsm->sessions, value.value);
758     }
759
760   *p_proto = ip_proto_to_snat_proto (key.proto);
761 out:
762   if (s)
763     *p_value = s->out2in;
764   *p_dont_translate = dont_translate;
765   if (d)
766     *(snat_session_t **) d = s;
767   return next;
768 }
769 #endif
770
771 static snat_session_t *
772 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
773                                vlib_buffer_t * b,
774                                ip4_header_t * ip,
775                                u32 rx_fib_index,
776                                u32 thread_index,
777                                f64 now,
778                                vlib_main_t * vm, vlib_node_runtime_t * node)
779 {
780   clib_bihash_kv_8_8_t kv, value;
781   clib_bihash_kv_16_8_t s_kv, s_value;
782   snat_static_mapping_t *m;
783   u32 old_addr, new_addr = 0;
784   ip_csum_t sum;
785   snat_user_t *u;
786   dlist_elt_t *head, *elt;
787   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
788   u32 elt_index, head_index, ses_index;
789   snat_session_t *s;
790   u32 outside_fib_index = sm->outside_fib_index;
791   int i;
792   u8 is_sm = 0;
793   nat_outside_fib_t *outside_fib;
794   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
795   fib_prefix_t pfx = {
796     .fp_proto = FIB_PROTOCOL_IP4,
797     .fp_len = 32,
798     .fp_addr = {
799                 .ip4.as_u32 = ip->dst_address.as_u32,
800                 },
801   };
802
803   switch (vec_len (sm->outside_fibs))
804     {
805     case 0:
806       outside_fib_index = sm->outside_fib_index;
807       break;
808     case 1:
809       outside_fib_index = sm->outside_fibs[0].fib_index;
810       break;
811     default:
812       /* *INDENT-OFF* */
813       vec_foreach (outside_fib, sm->outside_fibs)
814         {
815           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
816           if (FIB_NODE_INDEX_INVALID != fei)
817             {
818               if (fib_entry_get_resolving_interface (fei) != ~0)
819                 {
820                   outside_fib_index = outside_fib->fib_index;
821                   break;
822                 }
823             }
824         }
825       /* *INDENT-ON* */
826       break;
827     }
828   old_addr = ip->src_address.as_u32;
829
830   make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
831               rx_fib_index, 0, 0);
832
833   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
834     {
835       s = pool_elt_at_index (tsm->sessions, s_value.value);
836       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
837     }
838   else
839     {
840       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
841         {
842           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
843           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
844           nat_elog_notice ("maximum sessions exceeded");
845           return 0;
846         }
847
848       u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
849                                   thread_index);
850       if (!u)
851         {
852           b->error = node->errors[NAT_IN2OUT_ED_ERROR_CANNOT_CREATE_USER];
853           nat_elog_warn ("create NAT user failed");
854           return 0;
855         }
856
857       make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
858
859       /* Try to find static mapping first */
860       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
861         {
862           m = pool_elt_at_index (sm->static_mappings, value.value);
863           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
864           is_sm = 1;
865           goto create_ses;
866         }
867       /* Fallback to 3-tuple key */
868       else
869         {
870           /* Choose same out address as for TCP/UDP session to same destination */
871           head_index = u->sessions_per_user_list_head_index;
872           head = pool_elt_at_index (tsm->list_pool, head_index);
873           elt_index = head->next;
874           if (PREDICT_FALSE (elt_index == ~0))
875             ses_index = ~0;
876           else
877             {
878               elt = pool_elt_at_index (tsm->list_pool, elt_index);
879               ses_index = elt->value;
880             }
881
882           while (ses_index != ~0)
883             {
884               s = pool_elt_at_index (tsm->sessions, ses_index);
885               elt_index = elt->next;
886               elt = pool_elt_at_index (tsm->list_pool, elt_index);
887               ses_index = elt->value;
888
889               if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
890                 {
891                   new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
892
893                   make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
894                               ip->protocol, outside_fib_index, 0, 0);
895                   if (clib_bihash_search_16_8
896                       (&tsm->out2in_ed, &s_kv, &s_value))
897                     goto create_ses;
898
899                   break;
900                 }
901             }
902
903           for (i = 0; i < vec_len (sm->addresses); i++)
904             {
905               make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
906                           ip->protocol, outside_fib_index, 0, 0);
907               if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
908                 {
909                   new_addr = ip->src_address.as_u32 =
910                     sm->addresses[i].addr.as_u32;
911                   goto create_ses;
912                 }
913             }
914           return 0;
915         }
916
917     create_ses:
918       s = nat_ed_session_alloc (sm, u, thread_index, now);
919       if (!s)
920         {
921           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
922           nat44_delete_user_with_no_session (sm, u, thread_index);
923           nat_elog_warn ("create NAT session failed");
924           return 0;
925         }
926
927       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
928       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
929       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
930       s->out2in.addr.as_u32 = new_addr;
931       s->out2in.fib_index = outside_fib_index;
932       s->in2out.addr.as_u32 = old_addr;
933       s->in2out.fib_index = rx_fib_index;
934       s->in2out.port = s->out2in.port = ip->protocol;
935       if (is_sm)
936         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
937       user_session_increment (sm, u, is_sm);
938
939       /* Add to lookup tables */
940       make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
941                   rx_fib_index, 0, 0);
942       s_kv.value = s - tsm->sessions;
943       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
944         nat_elog_notice ("in2out key add failed");
945
946       make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
947                   outside_fib_index, 0, 0);
948       s_kv.value = s - tsm->sessions;
949       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
950         nat_elog_notice ("out2in key add failed");
951     }
952
953   /* Update IP checksum */
954   sum = ip->checksum;
955   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
956   ip->checksum = ip_csum_fold (sum);
957
958   /* Accounting */
959   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
960                                  thread_index);
961   /* Per-user LRU list maintenance */
962   nat44_session_update_lru (sm, s, thread_index);
963
964   /* Hairpinning */
965   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
966     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
967
968   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
969     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
970
971   return s;
972 }
973
974 static inline uword
975 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
976                                           vlib_node_runtime_t * node,
977                                           vlib_frame_t * frame,
978                                           int is_output_feature)
979 {
980   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
981   nat_next_t next_index;
982   snat_main_t *sm = &snat_main;
983   f64 now = vlib_time_now (vm);
984   u32 thread_index = vm->thread_index;
985   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
986   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
987     0, def_slow;
988
989   def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH :
990     NAT_NEXT_IN2OUT_ED_SLOW_PATH;
991
992   stats_node_index = sm->ed_in2out_node_index;
993
994   from = vlib_frame_vector_args (frame);
995   n_left_from = frame->n_vectors;
996   next_index = node->cached_next_index;
997
998   while (n_left_from > 0)
999     {
1000       u32 n_left_to_next;
1001
1002       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1003
1004       while (n_left_from > 0 && n_left_to_next > 0)
1005         {
1006           u32 bi0;
1007           vlib_buffer_t *b0;
1008           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1009             new_addr0, old_addr0;
1010           u16 old_port0, new_port0;
1011           ip4_header_t *ip0;
1012           udp_header_t *udp0;
1013           tcp_header_t *tcp0;
1014           snat_session_t *s0 = 0;
1015           clib_bihash_kv_16_8_t kv0, value0;
1016           ip_csum_t sum0;
1017
1018           /* speculatively enqueue b0 to the current next frame */
1019           bi0 = from[0];
1020           to_next[0] = bi0;
1021           from += 1;
1022           to_next += 1;
1023           n_left_from -= 1;
1024           n_left_to_next -= 1;
1025
1026           b0 = vlib_get_buffer (vm, bi0);
1027
1028           if (is_output_feature)
1029             {
1030               vnet_feature_next (&nat_buffer_opaque (b0)->arc_next, b0);
1031               iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1032             }
1033
1034           next0 = nat_buffer_opaque (b0)->arc_next;
1035
1036           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1037                                   iph_offset0);
1038
1039           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1040           rx_fib_index0 =
1041             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1042                                                  sw_if_index0);
1043
1044           if (PREDICT_FALSE (ip0->ttl == 1))
1045             {
1046               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1047               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1048                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1049                                            0);
1050               next0 = NAT_NEXT_ICMP_ERROR;
1051               goto trace0;
1052             }
1053
1054           udp0 = ip4_next_header (ip0);
1055           tcp0 = (tcp_header_t *) udp0;
1056           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1057
1058           if (PREDICT_FALSE (proto0 == ~0))
1059             {
1060               next0 = def_slow;
1061               goto trace0;
1062             }
1063
1064           if (is_output_feature)
1065             {
1066               if (PREDICT_FALSE (nat_not_translate_output_feature_fwd
1067                                  (sm, ip0, thread_index, now, vm, b0)))
1068                 goto trace0;
1069             }
1070
1071           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1072             {
1073               next0 = def_slow;
1074               goto trace0;
1075             }
1076
1077           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1078                       ip0->protocol, rx_fib_index0,
1079                       vnet_buffer (b0)->ip.reass.l4_src_port,
1080                       vnet_buffer (b0)->ip.reass.l4_dst_port);
1081
1082           // lookup for session
1083           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1084             {
1085               // session does not exist go slow path
1086               next0 = def_slow;
1087               goto trace0;
1088             }
1089           s0 = pool_elt_at_index (tsm->sessions, value0.value);
1090
1091           if (s0->tcp_close_timestamp)
1092             {
1093               if (now >= s0->tcp_close_timestamp)
1094                 {
1095                   // session is closed, go slow path
1096                   next0 = def_slow;
1097                 }
1098               else
1099                 {
1100                   // session in transitory timeout, drop
1101                   b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TCP_CLOSED];
1102                   next0 = NAT_NEXT_DROP;
1103                 }
1104               goto trace0;
1105             }
1106
1107           // drop if session expired
1108           u64 sess_timeout_time;
1109           sess_timeout_time = s0->last_heard +
1110             (f64) nat44_session_get_timeout (sm, s0);
1111           if (now >= sess_timeout_time)
1112             {
1113               nat_free_session_data (sm, s0, thread_index, 0);
1114               nat44_delete_session (sm, s0, thread_index);
1115               // session is closed, go slow path
1116               next0 = def_slow;
1117               goto trace0;
1118             }
1119
1120           b0->flags |= VNET_BUFFER_F_IS_NATED;
1121
1122           if (!is_output_feature)
1123             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1124
1125           old_addr0 = ip0->src_address.as_u32;
1126           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1127           sum0 = ip0->checksum;
1128           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1129                                  src_address);
1130           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1131             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1132                                    s0->ext_host_addr.as_u32, ip4_header_t,
1133                                    dst_address);
1134           ip0->checksum = ip_csum_fold (sum0);
1135
1136           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1137
1138           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1139             {
1140               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1141                 {
1142                   new_port0 = udp0->src_port = s0->out2in.port;
1143                   sum0 = tcp0->checksum;
1144                   sum0 =
1145                     ip_csum_update (sum0, old_addr0, new_addr0,
1146                                     ip4_header_t, dst_address);
1147                   sum0 =
1148                     ip_csum_update (sum0, old_port0, new_port0,
1149                                     ip4_header_t, length);
1150                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1151                     {
1152                       sum0 =
1153                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1154                                         s0->ext_host_addr.as_u32,
1155                                         ip4_header_t, dst_address);
1156                       sum0 =
1157                         ip_csum_update (sum0,
1158                                         vnet_buffer (b0)->ip.
1159                                         reass.l4_dst_port, s0->ext_host_port,
1160                                         ip4_header_t, length);
1161                       tcp0->dst_port = s0->ext_host_port;
1162                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1163                     }
1164                   mss_clamping (sm, tcp0, &sum0);
1165                   tcp0->checksum = ip_csum_fold (sum0);
1166                 }
1167               tcp_packets++;
1168               if (nat44_set_tcp_session_state_i2o
1169                   (sm, now, s0, b0, thread_index))
1170                 goto trace0;
1171             }
1172           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1173                    && udp0->checksum)
1174             {
1175               new_port0 = udp0->src_port = s0->out2in.port;
1176               sum0 = udp0->checksum;
1177               sum0 =
1178                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1179                                 dst_address);
1180               sum0 =
1181                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1182                                 length);
1183               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1184                 {
1185                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1186                                          s0->ext_host_addr.as_u32,
1187                                          ip4_header_t, dst_address);
1188                   sum0 =
1189                     ip_csum_update (sum0,
1190                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1191                                     s0->ext_host_port, ip4_header_t, length);
1192                   udp0->dst_port = s0->ext_host_port;
1193                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1194                 }
1195               udp0->checksum = ip_csum_fold (sum0);
1196               udp_packets++;
1197             }
1198           else
1199             {
1200               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1201                 {
1202                   new_port0 = udp0->src_port = s0->out2in.port;
1203                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1204                     {
1205                       udp0->dst_port = s0->ext_host_port;
1206                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1207                     }
1208                   udp_packets++;
1209                 }
1210             }
1211
1212           /* Accounting */
1213           nat44_session_update_counters (s0, now,
1214                                          vlib_buffer_length_in_chain
1215                                          (vm, b0), thread_index);
1216           /* Per-user LRU list maintenance */
1217           nat44_session_update_lru (sm, s0, thread_index);
1218
1219         trace0:
1220           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1221                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1222             {
1223               nat_in2out_ed_trace_t *t =
1224                 vlib_add_trace (vm, node, b0, sizeof (*t));
1225               t->sw_if_index = sw_if_index0;
1226               t->next_index = next0;
1227               t->is_slow_path = 0;
1228
1229               if (s0)
1230                 t->session_index = s0 - tsm->sessions;
1231               else
1232                 t->session_index = ~0;
1233             }
1234
1235           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1236           /* verify speculative enqueue, maybe switch current next frame */
1237           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1238                                            to_next, n_left_to_next,
1239                                            bi0, next0);
1240         }
1241
1242       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1243     }
1244
1245   vlib_node_increment_counter (vm, stats_node_index,
1246                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1247                                pkts_processed);
1248   vlib_node_increment_counter (vm, stats_node_index,
1249                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1250   vlib_node_increment_counter (vm, stats_node_index,
1251                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1252   vlib_node_increment_counter (vm, stats_node_index,
1253                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1254                                icmp_packets);
1255   vlib_node_increment_counter (vm, stats_node_index,
1256                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1257                                other_packets);
1258   return frame->n_vectors;
1259 }
1260
1261 static inline uword
1262 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t * vm,
1263                                           vlib_node_runtime_t * node,
1264                                           vlib_frame_t * frame,
1265                                           int is_output_feature)
1266 {
1267   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
1268   nat_next_t next_index;
1269   snat_main_t *sm = &snat_main;
1270   f64 now = vlib_time_now (vm);
1271   u32 thread_index = vm->thread_index;
1272   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1273   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets = 0;
1274
1275   stats_node_index = sm->ed_in2out_slowpath_node_index;
1276
1277   from = vlib_frame_vector_args (frame);
1278   n_left_from = frame->n_vectors;
1279   next_index = node->cached_next_index;
1280
1281   while (n_left_from > 0)
1282     {
1283       u32 n_left_to_next;
1284
1285       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1286
1287       while (n_left_from > 0 && n_left_to_next > 0)
1288         {
1289           u32 bi0;
1290           vlib_buffer_t *b0;
1291           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1292             new_addr0, old_addr0;
1293           u16 old_port0, new_port0;
1294           ip4_header_t *ip0;
1295           udp_header_t *udp0;
1296           tcp_header_t *tcp0;
1297           icmp46_header_t *icmp0;
1298           snat_session_t *s0 = 0;
1299           clib_bihash_kv_16_8_t kv0, value0;
1300           ip_csum_t sum0;
1301
1302           /* speculatively enqueue b0 to the current next frame */
1303           bi0 = from[0];
1304           to_next[0] = bi0;
1305           from += 1;
1306           to_next += 1;
1307           n_left_from -= 1;
1308           n_left_to_next -= 1;
1309
1310           b0 = vlib_get_buffer (vm, bi0);
1311
1312           if (is_output_feature)
1313             iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1314
1315           next0 = nat_buffer_opaque (b0)->arc_next;
1316
1317           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1318                                   iph_offset0);
1319
1320           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1321           rx_fib_index0 =
1322             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1323                                                  sw_if_index0);
1324
1325           if (PREDICT_FALSE (ip0->ttl == 1))
1326             {
1327               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1328               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1329                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1330                                            0);
1331               next0 = NAT_NEXT_ICMP_ERROR;
1332               goto trace0;
1333             }
1334
1335           udp0 = ip4_next_header (ip0);
1336           tcp0 = (tcp_header_t *) udp0;
1337           icmp0 = (icmp46_header_t *) udp0;
1338           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1339
1340           if (PREDICT_FALSE (proto0 == ~0))
1341             {
1342               s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1343                                                   rx_fib_index0,
1344                                                   thread_index, now,
1345                                                   vm, node);
1346               if (!s0)
1347                 next0 = NAT_NEXT_DROP;
1348
1349               other_packets++;
1350               goto trace0;
1351             }
1352
1353           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1354             {
1355               next0 = icmp_in2out_ed_slow_path
1356                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1357                  node, next0, now, thread_index, &s0);
1358               icmp_packets++;
1359               goto trace0;
1360             }
1361
1362           // move down
1363           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1364                       ip0->protocol, rx_fib_index0,
1365                       vnet_buffer (b0)->ip.reass.l4_src_port,
1366                       vnet_buffer (b0)->ip.reass.l4_dst_port);
1367
1368           if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1369             {
1370               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1371
1372               if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1373                 {
1374                   nat_free_session_data (sm, s0, thread_index, 0);
1375                   nat44_delete_session (sm, s0, thread_index);
1376                   s0 = NULL;
1377                 }
1378             }
1379
1380           if (!s0)
1381             {
1382               if (is_output_feature)
1383                 {
1384                   if (PREDICT_FALSE
1385                       (nat44_ed_not_translate_output_feature
1386                        (sm, ip0, ip0->protocol,
1387                         vnet_buffer (b0)->ip.reass.l4_src_port,
1388                         vnet_buffer (b0)->ip.reass.l4_dst_port,
1389                         thread_index, sw_if_index0,
1390                         vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1391                     goto trace0;
1392
1393                   /*
1394                    * Send DHCP packets to the ipv4 stack, or we won't
1395                    * be able to use dhcp client on the outside interface
1396                    */
1397                   if (PREDICT_FALSE
1398                       (proto0 == SNAT_PROTOCOL_UDP
1399                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1400                            clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1401                        && ip0->dst_address.as_u32 == 0xffffffff))
1402                     goto trace0;
1403                 }
1404               else
1405                 {
1406                   if (PREDICT_FALSE
1407                       (nat44_ed_not_translate
1408                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1409                         thread_index)))
1410                     goto trace0;
1411                 }
1412
1413               next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1414                                     next0, thread_index, now);
1415
1416               if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1417                 goto trace0;
1418
1419               if (PREDICT_FALSE (!s0))
1420                 goto trace0;
1421
1422             }
1423
1424           b0->flags |= VNET_BUFFER_F_IS_NATED;
1425
1426           if (!is_output_feature)
1427             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1428
1429           old_addr0 = ip0->src_address.as_u32;
1430           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1431           sum0 = ip0->checksum;
1432           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1433                                  src_address);
1434           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1435             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1436                                    s0->ext_host_addr.as_u32, ip4_header_t,
1437                                    dst_address);
1438           ip0->checksum = ip_csum_fold (sum0);
1439
1440           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1441
1442           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1443             {
1444               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1445                 {
1446                   new_port0 = udp0->src_port = s0->out2in.port;
1447                   sum0 = tcp0->checksum;
1448                   sum0 =
1449                     ip_csum_update (sum0, old_addr0, new_addr0,
1450                                     ip4_header_t, dst_address);
1451                   sum0 =
1452                     ip_csum_update (sum0, old_port0, new_port0,
1453                                     ip4_header_t, length);
1454                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1455                     {
1456                       sum0 =
1457                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1458                                         s0->ext_host_addr.as_u32,
1459                                         ip4_header_t, dst_address);
1460                       sum0 =
1461                         ip_csum_update (sum0,
1462                                         vnet_buffer (b0)->ip.
1463                                         reass.l4_dst_port, s0->ext_host_port,
1464                                         ip4_header_t, length);
1465                       tcp0->dst_port = s0->ext_host_port;
1466                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1467                     }
1468                   mss_clamping (sm, tcp0, &sum0);
1469                   tcp0->checksum = ip_csum_fold (sum0);
1470                 }
1471               tcp_packets++;
1472               if (nat44_set_tcp_session_state_i2o
1473                   (sm, now, s0, b0, thread_index))
1474                 goto trace0;
1475             }
1476           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1477                    && udp0->checksum)
1478             {
1479               new_port0 = udp0->src_port = s0->out2in.port;
1480               sum0 = udp0->checksum;
1481               sum0 =
1482                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1483                                 dst_address);
1484               sum0 =
1485                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1486                                 length);
1487               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1488                 {
1489                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1490                                          s0->ext_host_addr.as_u32,
1491                                          ip4_header_t, dst_address);
1492                   sum0 =
1493                     ip_csum_update (sum0,
1494                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1495                                     s0->ext_host_port, ip4_header_t, length);
1496                   udp0->dst_port = s0->ext_host_port;
1497                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1498                 }
1499               udp0->checksum = ip_csum_fold (sum0);
1500               udp_packets++;
1501             }
1502           else
1503             {
1504               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1505                 {
1506                   new_port0 = udp0->src_port = s0->out2in.port;
1507                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1508                     {
1509                       udp0->dst_port = s0->ext_host_port;
1510                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1511                     }
1512                   udp_packets++;
1513                 }
1514             }
1515
1516           /* Accounting */
1517           nat44_session_update_counters (s0, now,
1518                                          vlib_buffer_length_in_chain
1519                                          (vm, b0), thread_index);
1520           /* Per-user LRU list maintenance */
1521           nat44_session_update_lru (sm, s0, thread_index);
1522
1523         trace0:
1524           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1525                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1526             {
1527               nat_in2out_ed_trace_t *t =
1528                 vlib_add_trace (vm, node, b0, sizeof (*t));
1529               t->sw_if_index = sw_if_index0;
1530               t->next_index = next0;
1531               t->is_slow_path = 1;
1532
1533               if (s0)
1534                 t->session_index = s0 - tsm->sessions;
1535               else
1536                 t->session_index = ~0;
1537             }
1538
1539           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1540
1541           /* verify speculative enqueue, maybe switch current next frame */
1542           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1543                                            to_next, n_left_to_next,
1544                                            bi0, next0);
1545         }
1546
1547       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1548     }
1549
1550   vlib_node_increment_counter (vm, stats_node_index,
1551                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1552                                pkts_processed);
1553   vlib_node_increment_counter (vm, stats_node_index,
1554                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1555   vlib_node_increment_counter (vm, stats_node_index,
1556                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1557   vlib_node_increment_counter (vm, stats_node_index,
1558                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1559                                icmp_packets);
1560   vlib_node_increment_counter (vm, stats_node_index,
1561                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1562                                other_packets);
1563   return frame->n_vectors;
1564 }
1565
1566 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1567                                      vlib_node_runtime_t * node,
1568                                      vlib_frame_t * frame)
1569 {
1570   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1571 }
1572
1573 /* *INDENT-OFF* */
1574 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1575   .name = "nat44-ed-in2out",
1576   .vector_size = sizeof (u32),
1577   .sibling_of = "nat-default",
1578   .format_trace = format_nat_in2out_ed_trace,
1579   .type = VLIB_NODE_TYPE_INTERNAL,
1580   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1581   .error_strings = nat_in2out_ed_error_strings,
1582   .runtime_data_bytes = sizeof (snat_runtime_t),
1583 };
1584 /* *INDENT-ON* */
1585
1586 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1587                                             vlib_node_runtime_t * node,
1588                                             vlib_frame_t * frame)
1589 {
1590   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1591 }
1592
1593 /* *INDENT-OFF* */
1594 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1595   .name = "nat44-ed-in2out-output",
1596   .vector_size = sizeof (u32),
1597   .sibling_of = "nat-default",
1598   .format_trace = format_nat_in2out_ed_trace,
1599   .type = VLIB_NODE_TYPE_INTERNAL,
1600   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1601   .error_strings = nat_in2out_ed_error_strings,
1602   .runtime_data_bytes = sizeof (snat_runtime_t),
1603 };
1604 /* *INDENT-ON* */
1605
1606 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1607                                               vlib_node_runtime_t *
1608                                               node, vlib_frame_t * frame)
1609 {
1610   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1611 }
1612
1613 /* *INDENT-OFF* */
1614 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1615   .name = "nat44-ed-in2out-slowpath",
1616   .vector_size = sizeof (u32),
1617   .sibling_of = "nat-default",
1618   .format_trace = format_nat_in2out_ed_trace,
1619   .type = VLIB_NODE_TYPE_INTERNAL,
1620   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1621   .error_strings = nat_in2out_ed_error_strings,
1622   .runtime_data_bytes = sizeof (snat_runtime_t),
1623 };
1624 /* *INDENT-ON* */
1625
1626 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1627                                                      vlib_node_runtime_t
1628                                                      * node,
1629                                                      vlib_frame_t * frame)
1630 {
1631   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1632 }
1633
1634 /* *INDENT-OFF* */
1635 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1636   .name = "nat44-ed-in2out-output-slowpath",
1637   .vector_size = sizeof (u32),
1638   .sibling_of = "nat-default",
1639   .format_trace = format_nat_in2out_ed_trace,
1640   .type = VLIB_NODE_TYPE_INTERNAL,
1641   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1642   .error_strings = nat_in2out_ed_error_strings,
1643   .runtime_data_bytes = sizeof (snat_runtime_t),
1644 };
1645 /* *INDENT-ON* */
1646
1647 static u8 *
1648 format_nat_pre_trace (u8 * s, va_list * args)
1649 {
1650   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1651   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1652   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1653   return format (s, "in2out next_index %d", t->next_index);
1654 }
1655
1656 VLIB_NODE_FN (nat_pre_in2out_node)
1657   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1658 {
1659   return nat_pre_node_fn_inline (vm, node, frame,
1660                                  NAT_NEXT_IN2OUT_ED_FAST_PATH);
1661 }
1662
1663 /* *INDENT-OFF* */
1664 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1665   .name = "nat-pre-in2out",
1666   .vector_size = sizeof (u32),
1667   .sibling_of = "nat-default",
1668   .format_trace = format_nat_pre_trace,
1669   .type = VLIB_NODE_TYPE_INTERNAL,
1670   .n_errors = 0,
1671 };
1672 /* *INDENT-ON* */
1673
1674 /*
1675  * fd.io coding-style-patch-verification: ON
1676  *
1677  * Local Variables:
1678  * eval: (c-set-style "gnu")
1679  * End:
1680  */