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