nat: twice-nat static mapping pool address
[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, 0))
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   per_vrf_sessions_register_session (s, thread_index);
492
493   return next;
494 }
495
496 static_always_inline int
497 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
498                         u32 sw_if_index, ip4_header_t * ip, u32 proto,
499                         u32 rx_fib_index, u32 thread_index)
500 {
501   udp_header_t *udp = ip4_next_header (ip);
502   clib_bihash_kv_16_8_t kv, value;
503
504   init_ed_k (&kv, ip->dst_address, udp->dst_port, ip->src_address,
505              udp->src_port, sm->outside_fib_index, ip->protocol);
506
507   /* NAT packet aimed at external address if has active sessions */
508   if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
509     {
510       /* or is static mappings */
511       ip4_address_t placeholder_addr;
512       u16 placeholder_port;
513       u32 placeholder_fib_index;
514       if (!snat_static_mapping_match
515           (sm, ip->dst_address, udp->dst_port, sm->outside_fib_index, proto,
516            &placeholder_addr, &placeholder_port, &placeholder_fib_index, 1, 0,
517            0, 0, 0, 0, 0))
518         return 0;
519     }
520   else
521     return 0;
522
523   if (sm->forwarding_enabled)
524     return 1;
525
526   return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
527                                   rx_fib_index);
528 }
529
530 static_always_inline int
531 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
532                                       u32 thread_index, f64 now,
533                                       vlib_main_t * vm, vlib_buffer_t * b)
534 {
535   clib_bihash_kv_16_8_t kv, value;
536   snat_session_t *s = 0;
537   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
538
539   if (!sm->forwarding_enabled)
540     return 0;
541
542   if (ip->protocol == IP_PROTOCOL_ICMP)
543     {
544       if (get_icmp_i2o_ed_key (b, ip, 0, ~0, ~0, 0, 0, 0, &kv))
545         return 0;
546     }
547   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
548     {
549       init_ed_k (&kv, ip->src_address, vnet_buffer (b)->ip.reass.l4_src_port,
550                  ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port, 0,
551                  ip->protocol);
552     }
553   else
554     {
555       init_ed_k (&kv, ip->src_address, 0, ip->dst_address, 0, 0,
556                  ip->protocol);
557     }
558
559   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
560     {
561       ASSERT (thread_index == ed_value_get_thread_index (&value));
562       s =
563         pool_elt_at_index (tsm->sessions,
564                            ed_value_get_session_index (&value));
565       if (is_fwd_bypass_session (s))
566         {
567           if (ip->protocol == IP_PROTOCOL_TCP)
568             {
569               if (nat44_set_tcp_session_state_i2o
570                   (sm, now, s, b, thread_index))
571                 return 1;
572             }
573           /* Accounting */
574           nat44_session_update_counters (s, now,
575                                          vlib_buffer_length_in_chain (vm, b),
576                                          thread_index);
577           /* Per-user LRU list maintenance */
578           nat44_session_update_lru (sm, s, thread_index);
579           return 1;
580         }
581       else
582         return 0;
583     }
584
585   return 0;
586 }
587
588 static_always_inline int
589 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
590                                        u16 src_port, u16 dst_port,
591                                        u32 thread_index, u32 rx_sw_if_index,
592                                        u32 tx_sw_if_index)
593 {
594   clib_bihash_kv_16_8_t kv, value;
595   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
596   snat_interface_t *i;
597   snat_session_t *s;
598   u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
599   u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
600
601   /* src NAT check */
602   init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
603              tx_fib_index, ip->protocol);
604   if (!clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
605     {
606       ASSERT (thread_index == ed_value_get_thread_index (&value));
607       s =
608         pool_elt_at_index (tsm->sessions,
609                            ed_value_get_session_index (&value));
610       if (nat44_is_ses_closed (s))
611         {
612           nat_free_session_data (sm, s, thread_index, 0);
613           nat_ed_session_delete (sm, s, thread_index, 1);
614         }
615       else
616         s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
617       return 1;
618     }
619
620   /* dst NAT check */
621   init_ed_k (&kv, ip->dst_address, dst_port, ip->src_address, src_port,
622              rx_fib_index, ip->protocol);
623   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
624     {
625       ASSERT (thread_index == ed_value_get_thread_index (&value));
626       s =
627         pool_elt_at_index (tsm->sessions,
628                            ed_value_get_session_index (&value));
629       if (is_fwd_bypass_session (s))
630         return 0;
631
632       /* hairpinning */
633       /* *INDENT-OFF* */
634       pool_foreach (i, sm->output_feature_interfaces,
635       ({
636         if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
637            return 0;
638       }));
639       /* *INDENT-ON* */
640       return 1;
641     }
642
643   return 0;
644 }
645
646 #ifndef CLIB_MARCH_VARIANT
647 u32
648 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
649                       u32 thread_index, vlib_buffer_t * b,
650                       ip4_header_t * ip, ip4_address_t * addr,
651                       u16 * port, u32 * fib_index, nat_protocol_t * proto,
652                       void *d, void *e, u8 * dont_translate)
653 {
654   u32 sw_if_index;
655   u32 rx_fib_index;
656   clib_bihash_kv_16_8_t kv, value;
657   u32 next = ~0;
658   int err;
659   snat_session_t *s = NULL;
660   u16 l_port = 0, r_port = 0;   // initialize to workaround gcc warning
661   vlib_main_t *vm = vlib_get_main ();
662   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
663   *dont_translate = 0;
664
665   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
666   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
667
668   err =
669     get_icmp_i2o_ed_key (b, ip, rx_fib_index, ~0, ~0, proto, &l_port,
670                          &r_port, &kv);
671   if (err != 0)
672     {
673       b->error = node->errors[err];
674       next = NAT_NEXT_DROP;
675       goto out;
676     }
677
678   if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
679     {
680       if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
681         {
682           if (PREDICT_FALSE
683               (nat44_ed_not_translate_output_feature
684                (sm, ip, l_port, r_port, thread_index,
685                 sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_TX])))
686             {
687               *dont_translate = 1;
688               goto out;
689             }
690         }
691       else
692         {
693           if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
694                                                      ip, NAT_PROTOCOL_ICMP,
695                                                      rx_fib_index,
696                                                      thread_index)))
697             {
698               *dont_translate = 1;
699               goto out;
700             }
701         }
702
703       if (PREDICT_FALSE
704           (icmp_type_is_error_message
705            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
706         {
707           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
708           next = NAT_NEXT_DROP;
709           goto out;
710         }
711
712       next =
713         slow_path_ed (sm, b, ip->src_address, ip->dst_address, l_port, r_port,
714                       ip->protocol, rx_fib_index, &s, node, next,
715                       thread_index, vlib_time_now (vm));
716
717       if (PREDICT_FALSE (next == NAT_NEXT_DROP))
718         goto out;
719
720       if (!s)
721         {
722           *dont_translate = 1;
723           goto out;
724         }
725     }
726   else
727     {
728       if (PREDICT_FALSE
729           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
730            ICMP4_echo_request
731            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
732            ICMP4_echo_reply
733            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
734                                            reass.icmp_type_or_tcp_flags)))
735         {
736           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
737           next = NAT_NEXT_DROP;
738           goto out;
739         }
740
741       ASSERT (thread_index == ed_value_get_thread_index (&value));
742       s =
743         pool_elt_at_index (tsm->sessions,
744                            ed_value_get_session_index (&value));
745     }
746 out:
747   if (s)
748     {
749       *addr = s->out2in.addr;
750       *port = s->out2in.port;
751       *fib_index = s->out2in.fib_index;
752     }
753   if (d)
754     {
755       *(snat_session_t **) d = s;
756     }
757   return next;
758 }
759 #endif
760
761 static snat_session_t *
762 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
763                                vlib_buffer_t * b,
764                                ip4_header_t * ip,
765                                u32 rx_fib_index,
766                                u32 thread_index,
767                                f64 now,
768                                vlib_main_t * vm, vlib_node_runtime_t * node)
769 {
770   clib_bihash_kv_8_8_t kv, value;
771   clib_bihash_kv_16_8_t s_kv, s_value;
772   snat_static_mapping_t *m;
773   u32 old_addr, new_addr = 0;
774   ip_csum_t sum;
775   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
776   snat_session_t *s;
777   u32 outside_fib_index = sm->outside_fib_index;
778   int i;
779   u8 is_sm = 0;
780
781   switch (vec_len (sm->outside_fibs))
782     {
783     case 0:
784       outside_fib_index = sm->outside_fib_index;
785       break;
786     case 1:
787       outside_fib_index = sm->outside_fibs[0].fib_index;
788       break;
789     default:
790       outside_fib_index = nat_outside_fib_index_lookup (sm, ip->dst_address);
791       break;
792     }
793   old_addr = ip->src_address.as_u32;
794
795   init_ed_k (&s_kv, ip->src_address, 0, ip->dst_address, 0, rx_fib_index,
796              ip->protocol);
797
798   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
799     {
800       ASSERT (thread_index == ed_value_get_thread_index (&s_value));
801       s =
802         pool_elt_at_index (tsm->sessions,
803                            ed_value_get_session_index (&s_value));
804       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
805     }
806   else
807     {
808       if (PREDICT_FALSE
809           (nat44_ed_maximum_sessions_exceeded
810            (sm, rx_fib_index, thread_index)))
811         {
812           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
813           nat_ipfix_logging_max_sessions (thread_index,
814                                           sm->max_translations_per_thread);
815           nat_elog_notice ("maximum sessions exceeded");
816           return 0;
817         }
818
819       init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
820
821       /* Try to find static mapping first */
822       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
823         {
824           m = pool_elt_at_index (sm->static_mappings, value.value);
825           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
826           is_sm = 1;
827           goto create_ses;
828         }
829       else
830         {
831           /* *INDENT-OFF* */
832           pool_foreach (s, tsm->sessions, {
833             if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
834               {
835                 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
836
837                 init_ed_k(&s_kv, s->out2in.addr, 0, ip->dst_address, 0, outside_fib_index, ip->protocol);
838                 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
839                   goto create_ses;
840
841                 break;
842               }
843           });
844           /* *INDENT-ON* */
845
846           for (i = 0; i < vec_len (sm->addresses); i++)
847             {
848               init_ed_k (&s_kv, sm->addresses[i].addr, 0, ip->dst_address, 0,
849                          outside_fib_index, ip->protocol);
850               if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
851                 {
852                   new_addr = ip->src_address.as_u32 =
853                     sm->addresses[i].addr.as_u32;
854                   goto create_ses;
855                 }
856             }
857           return 0;
858         }
859
860     create_ses:
861       s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
862       if (!s)
863         {
864           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
865           nat_elog_warn ("create NAT session failed");
866           return 0;
867         }
868
869       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
870       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
871       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
872       s->out2in.addr.as_u32 = new_addr;
873       s->out2in.fib_index = outside_fib_index;
874       s->in2out.addr.as_u32 = old_addr;
875       s->in2out.fib_index = rx_fib_index;
876       s->in2out.port = s->out2in.port = ip->protocol;
877       if (is_sm)
878         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
879
880       /* Add to lookup tables */
881       init_ed_kv (&s_kv, s->in2out.addr, 0, ip->dst_address, 0, rx_fib_index,
882                   ip->protocol, thread_index, s - tsm->sessions);
883       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
884         nat_elog_notice ("in2out key add failed");
885
886       init_ed_kv (&s_kv, s->out2in.addr, 0, ip->dst_address, 0,
887                   outside_fib_index, ip->protocol, thread_index,
888                   s - tsm->sessions);
889       if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
890         nat_elog_notice ("out2in key add failed");
891
892       per_vrf_sessions_register_session (s, thread_index);
893     }
894
895   /* Update IP checksum */
896   sum = ip->checksum;
897   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
898   ip->checksum = ip_csum_fold (sum);
899
900   /* Accounting */
901   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
902                                  thread_index);
903   /* Per-user LRU list maintenance */
904   nat44_session_update_lru (sm, s, thread_index);
905
906   /* Hairpinning */
907   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
908     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
909
910   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
911     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
912
913   return s;
914 }
915
916 static inline uword
917 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
918                                           vlib_node_runtime_t * node,
919                                           vlib_frame_t * frame,
920                                           int is_output_feature)
921 {
922   u32 n_left_from, *from;
923   snat_main_t *sm = &snat_main;
924   f64 now = vlib_time_now (vm);
925   u32 thread_index = vm->thread_index;
926   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
927   u32 def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH
928     : NAT_NEXT_IN2OUT_ED_SLOW_PATH;
929
930   from = vlib_frame_vector_args (frame);
931   n_left_from = frame->n_vectors;
932
933   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
934   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
935   vlib_get_buffers (vm, from, b, n_left_from);
936
937   while (n_left_from > 0)
938     {
939       vlib_buffer_t *b0;
940       u32 sw_if_index0, rx_fib_index0, proto0, new_addr0, old_addr0,
941         iph_offset0 = 0;
942       u16 old_port0, new_port0;
943       ip4_header_t *ip0;
944       udp_header_t *udp0;
945       tcp_header_t *tcp0;
946       snat_session_t *s0 = 0;
947       clib_bihash_kv_16_8_t kv0, value0;
948       ip_csum_t sum0;
949
950       b0 = *b;
951       b++;
952
953       /* Prefetch next iteration. */
954       if (PREDICT_TRUE (n_left_from >= 2))
955         {
956           vlib_buffer_t *p2;
957
958           p2 = *b;
959
960           vlib_prefetch_buffer_header (p2, LOAD);
961
962           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
963         }
964
965       if (is_output_feature)
966         {
967           vnet_feature_next (&vnet_buffer2 (b0)->nat.arc_next, b0);
968           iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
969         }
970
971       next[0] = vnet_buffer2 (b0)->nat.arc_next;
972
973       ip0 =
974         (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) + iph_offset0);
975
976       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
977       rx_fib_index0 =
978         fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
979
980       if (PREDICT_FALSE (ip0->ttl == 1))
981         {
982           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
983           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
984                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
985                                        0);
986           next[0] = NAT_NEXT_ICMP_ERROR;
987           goto trace0;
988         }
989
990       udp0 = ip4_next_header (ip0);
991       tcp0 = (tcp_header_t *) udp0;
992       proto0 = ip_proto_to_nat_proto (ip0->protocol);
993
994       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
995         {
996           next[0] = def_slow;
997           goto trace0;
998         }
999
1000       if (is_output_feature)
1001         {
1002           if (PREDICT_FALSE
1003               (nat_not_translate_output_feature_fwd
1004                (sm, ip0, thread_index, now, vm, b0)))
1005             goto trace0;
1006         }
1007
1008       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1009         {
1010           next[0] = def_slow;
1011           goto trace0;
1012         }
1013
1014       init_ed_k (&kv0, ip0->src_address,
1015                  vnet_buffer (b0)->ip.reass.l4_src_port, ip0->dst_address,
1016                  vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1017                  ip0->protocol);
1018
1019       // lookup for session
1020       if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1021         {
1022           // session does not exist go slow path
1023           next[0] = def_slow;
1024           goto trace0;
1025         }
1026       ASSERT (thread_index == ed_value_get_thread_index (&value0));
1027       s0 =
1028         pool_elt_at_index (tsm->sessions,
1029                            ed_value_get_session_index (&value0));
1030
1031       if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
1032         {
1033           // session is closed, go slow path
1034           nat_free_session_data (sm, s0, thread_index, 0);
1035           nat_ed_session_delete (sm, s0, thread_index, 1);
1036           next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
1037           goto trace0;
1038         }
1039
1040       if (s0->tcp_closed_timestamp)
1041         {
1042           if (now >= s0->tcp_closed_timestamp)
1043             {
1044               // session is closed, go slow path, freed in slow path
1045               next[0] = def_slow;
1046             }
1047           else
1048             {
1049               // session in transitory timeout, drop
1050               b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TCP_CLOSED];
1051               next[0] = NAT_NEXT_DROP;
1052             }
1053           goto trace0;
1054         }
1055
1056       // drop if session expired
1057       u64 sess_timeout_time;
1058       sess_timeout_time =
1059         s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
1060       if (now >= sess_timeout_time)
1061         {
1062           nat_free_session_data (sm, s0, thread_index, 0);
1063           nat_ed_session_delete (sm, s0, thread_index, 1);
1064           // session is closed, go slow path
1065           next[0] = def_slow;
1066           goto trace0;
1067         }
1068
1069       b0->flags |= VNET_BUFFER_F_IS_NATED;
1070
1071       if (!is_output_feature)
1072         vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1073
1074       old_addr0 = ip0->src_address.as_u32;
1075       new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1076       sum0 = ip0->checksum;
1077       sum0 =
1078         ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1079                         src_address);
1080       if (PREDICT_FALSE (is_twice_nat_session (s0)))
1081         sum0 =
1082           ip_csum_update (sum0, ip0->dst_address.as_u32,
1083                           s0->ext_host_addr.as_u32, ip4_header_t,
1084                           dst_address);
1085       ip0->checksum = ip_csum_fold (sum0);
1086
1087       old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1088
1089       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1090         {
1091           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1092             {
1093               new_port0 = udp0->src_port = s0->out2in.port;
1094               sum0 = tcp0->checksum;
1095               sum0 =
1096                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1097                                 dst_address);
1098               sum0 =
1099                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1100                                 length);
1101               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1102                 {
1103                   sum0 =
1104                     ip_csum_update (sum0, ip0->dst_address.as_u32,
1105                                     s0->ext_host_addr.as_u32, ip4_header_t,
1106                                     dst_address);
1107                   sum0 =
1108                     ip_csum_update (sum0,
1109                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1110                                     s0->ext_host_port, ip4_header_t, length);
1111                   tcp0->dst_port = s0->ext_host_port;
1112                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1113                 }
1114               mss_clamping (sm->mss_clamping, tcp0, &sum0);
1115               tcp0->checksum = ip_csum_fold (sum0);
1116             }
1117           vlib_increment_simple_counter (&sm->counters.fastpath.in2out_ed.tcp,
1118                                          thread_index, sw_if_index0, 1);
1119           if (nat44_set_tcp_session_state_i2o (sm, now, s0, b0, thread_index))
1120             goto trace0;
1121         }
1122       else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1123                && udp0->checksum)
1124         {
1125           new_port0 = udp0->src_port = s0->out2in.port;
1126           sum0 = udp0->checksum;
1127           sum0 =
1128             ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1129                             dst_address);
1130           sum0 =
1131             ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length);
1132           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1133             {
1134               sum0 =
1135                 ip_csum_update (sum0, ip0->dst_address.as_u32,
1136                                 s0->ext_host_addr.as_u32, ip4_header_t,
1137                                 dst_address);
1138               sum0 =
1139                 ip_csum_update (sum0, vnet_buffer (b0)->ip.reass.l4_dst_port,
1140                                 s0->ext_host_port, ip4_header_t, length);
1141               udp0->dst_port = s0->ext_host_port;
1142               ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1143             }
1144           udp0->checksum = ip_csum_fold (sum0);
1145           vlib_increment_simple_counter (&sm->counters.fastpath.in2out_ed.udp,
1146                                          thread_index, sw_if_index0, 1);
1147         }
1148       else
1149         {
1150           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1151             {
1152               new_port0 = udp0->src_port = s0->out2in.port;
1153               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1154                 {
1155                   udp0->dst_port = s0->ext_host_port;
1156                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1157                 }
1158               vlib_increment_simple_counter (&sm->counters.fastpath.
1159                                              in2out_ed.udp, thread_index,
1160                                              sw_if_index0, 1);
1161             }
1162         }
1163
1164       /* Accounting */
1165       nat44_session_update_counters (s0, now,
1166                                      vlib_buffer_length_in_chain (vm, b0),
1167                                      thread_index);
1168       /* Per-user LRU list maintenance */
1169       nat44_session_update_lru (sm, s0, thread_index);
1170
1171     trace0:
1172       if (PREDICT_FALSE
1173           ((node->flags & VLIB_NODE_FLAG_TRACE)
1174            && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1175         {
1176           nat_in2out_ed_trace_t *t =
1177             vlib_add_trace (vm, node, b0, sizeof (*t));
1178           t->sw_if_index = sw_if_index0;
1179           t->next_index = next[0];
1180           t->is_slow_path = 0;
1181
1182           if (s0)
1183             t->session_index = s0 - tsm->sessions;
1184           else
1185             t->session_index = ~0;
1186         }
1187
1188       if (next[0] == NAT_NEXT_DROP)
1189         {
1190           vlib_increment_simple_counter (&sm->counters.fastpath.
1191                                          in2out_ed.drops, thread_index,
1192                                          sw_if_index0, 1);
1193         }
1194
1195       n_left_from--;
1196       next++;
1197     }
1198
1199   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1200                                frame->n_vectors);
1201   return frame->n_vectors;
1202 }
1203
1204 static inline uword
1205 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t * vm,
1206                                           vlib_node_runtime_t * node,
1207                                           vlib_frame_t * frame,
1208                                           int is_output_feature)
1209 {
1210   u32 n_left_from, *from;
1211   snat_main_t *sm = &snat_main;
1212   f64 now = vlib_time_now (vm);
1213   u32 thread_index = vm->thread_index;
1214   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1215
1216   from = vlib_frame_vector_args (frame);
1217   n_left_from = frame->n_vectors;
1218
1219   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1220   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1221   vlib_get_buffers (vm, from, b, n_left_from);
1222
1223   while (n_left_from > 0)
1224     {
1225       vlib_buffer_t *b0;
1226       u32 sw_if_index0, rx_fib_index0, proto0, new_addr0, old_addr0,
1227         iph_offset0 = 0;
1228       u16 old_port0, new_port0;
1229       ip4_header_t *ip0;
1230       udp_header_t *udp0;
1231       tcp_header_t *tcp0;
1232       icmp46_header_t *icmp0;
1233       snat_session_t *s0 = 0;
1234       clib_bihash_kv_16_8_t kv0, value0;
1235       ip_csum_t sum0;
1236
1237       b0 = *b;
1238
1239       if (is_output_feature)
1240         iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1241
1242       next[0] = vnet_buffer2 (b0)->nat.arc_next;
1243
1244       ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1245                               iph_offset0);
1246
1247       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1248       rx_fib_index0 =
1249         fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1250
1251       if (PREDICT_FALSE (ip0->ttl == 1))
1252         {
1253           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1254           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1255                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1256                                        0);
1257           next[0] = NAT_NEXT_ICMP_ERROR;
1258           goto trace0;
1259         }
1260
1261       udp0 = ip4_next_header (ip0);
1262       tcp0 = (tcp_header_t *) udp0;
1263       icmp0 = (icmp46_header_t *) udp0;
1264       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1265
1266       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1267         {
1268           s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1269                                               rx_fib_index0,
1270                                               thread_index, now, vm, node);
1271           if (!s0)
1272             next[0] = NAT_NEXT_DROP;
1273
1274           vlib_increment_simple_counter (&sm->counters.slowpath.
1275                                          in2out_ed.other, thread_index,
1276                                          sw_if_index0, 1);
1277           goto trace0;
1278         }
1279
1280       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1281         {
1282           next[0] =
1283             icmp_in2out_ed_slow_path (sm, b0, ip0, icmp0, sw_if_index0,
1284                                       rx_fib_index0, node, next[0], now,
1285                                       thread_index, &s0);
1286           vlib_increment_simple_counter (&sm->counters.slowpath.
1287                                          in2out_ed.icmp, thread_index,
1288                                          sw_if_index0, 1);
1289           goto trace0;
1290         }
1291
1292       init_ed_k (&kv0, ip0->src_address,
1293                  vnet_buffer (b0)->ip.reass.l4_src_port, ip0->dst_address,
1294                  vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1295                  ip0->protocol);
1296       if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1297         {
1298           ASSERT (thread_index == ed_value_get_thread_index (&value0));
1299           s0 =
1300             pool_elt_at_index (tsm->sessions,
1301                                ed_value_get_session_index (&value0));
1302
1303           if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1304             {
1305               nat_free_session_data (sm, s0, thread_index, 0);
1306               nat_ed_session_delete (sm, s0, thread_index, 1);
1307               s0 = NULL;
1308             }
1309         }
1310
1311       if (!s0)
1312         {
1313           if (is_output_feature)
1314             {
1315               if (PREDICT_FALSE
1316                   (nat44_ed_not_translate_output_feature
1317                    (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1318                     vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1319                     sw_if_index0, vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1320                 goto trace0;
1321
1322               /*
1323                * Send DHCP packets to the ipv4 stack, or we won't
1324                * be able to use dhcp client on the outside interface
1325                */
1326               if (PREDICT_FALSE
1327                   (proto0 == NAT_PROTOCOL_UDP
1328                    && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1329                        clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1330                    && ip0->dst_address.as_u32 == 0xffffffff))
1331                 goto trace0;
1332             }
1333           else
1334             {
1335               if (PREDICT_FALSE
1336                   (nat44_ed_not_translate
1337                    (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1338                     thread_index)))
1339                 goto trace0;
1340             }
1341
1342           next[0] =
1343             slow_path_ed (sm, b0, ip0->src_address, ip0->dst_address,
1344                           vnet_buffer (b0)->ip.reass.l4_src_port,
1345                           vnet_buffer (b0)->ip.reass.l4_dst_port,
1346                           ip0->protocol, rx_fib_index0, &s0, node, next[0],
1347                           thread_index, now);
1348
1349           if (PREDICT_FALSE (next[0] == NAT_NEXT_DROP))
1350             goto trace0;
1351
1352           if (PREDICT_FALSE (!s0))
1353             goto trace0;
1354
1355         }
1356
1357       b0->flags |= VNET_BUFFER_F_IS_NATED;
1358
1359       if (!is_output_feature)
1360         vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1361
1362       old_addr0 = ip0->src_address.as_u32;
1363       new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1364       sum0 = ip0->checksum;
1365       sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1366                              src_address);
1367       if (PREDICT_FALSE (is_twice_nat_session (s0)))
1368         sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1369                                s0->ext_host_addr.as_u32, ip4_header_t,
1370                                dst_address);
1371       ip0->checksum = ip_csum_fold (sum0);
1372
1373       old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1374
1375       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1376         {
1377           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1378             {
1379               new_port0 = udp0->src_port = s0->out2in.port;
1380               sum0 = tcp0->checksum;
1381               sum0 =
1382                 ip_csum_update (sum0, old_addr0, new_addr0,
1383                                 ip4_header_t, dst_address);
1384               sum0 =
1385                 ip_csum_update (sum0, old_port0, new_port0,
1386                                 ip4_header_t, length);
1387               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1388                 {
1389                   sum0 =
1390                     ip_csum_update (sum0, ip0->dst_address.as_u32,
1391                                     s0->ext_host_addr.as_u32,
1392                                     ip4_header_t, dst_address);
1393                   sum0 =
1394                     ip_csum_update (sum0,
1395                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1396                                     s0->ext_host_port, ip4_header_t, length);
1397                   tcp0->dst_port = s0->ext_host_port;
1398                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1399                 }
1400               mss_clamping (sm->mss_clamping, tcp0, &sum0);
1401               tcp0->checksum = ip_csum_fold (sum0);
1402             }
1403           vlib_increment_simple_counter (&sm->counters.slowpath.in2out_ed.tcp,
1404                                          thread_index, sw_if_index0, 1);
1405           if (nat44_set_tcp_session_state_i2o (sm, now, s0, b0, thread_index))
1406             goto trace0;
1407         }
1408       else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1409                && udp0->checksum)
1410         {
1411           new_port0 = udp0->src_port = s0->out2in.port;
1412           sum0 = udp0->checksum;
1413           sum0 =
1414             ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1415                             dst_address);
1416           sum0 =
1417             ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length);
1418           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1419             {
1420               sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1421                                      s0->ext_host_addr.as_u32,
1422                                      ip4_header_t, dst_address);
1423               sum0 =
1424                 ip_csum_update (sum0,
1425                                 vnet_buffer (b0)->ip.reass.l4_dst_port,
1426                                 s0->ext_host_port, ip4_header_t, length);
1427               udp0->dst_port = s0->ext_host_port;
1428               ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1429             }
1430           udp0->checksum = ip_csum_fold (sum0);
1431           vlib_increment_simple_counter (&sm->counters.slowpath.in2out_ed.udp,
1432                                          thread_index, sw_if_index0, 1);
1433         }
1434       else
1435         {
1436           if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1437             {
1438               new_port0 = udp0->src_port = s0->out2in.port;
1439               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1440                 {
1441                   udp0->dst_port = s0->ext_host_port;
1442                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1443                 }
1444               vlib_increment_simple_counter (&sm->counters.slowpath.
1445                                              in2out_ed.udp, thread_index,
1446                                              sw_if_index0, 1);
1447             }
1448         }
1449
1450       /* Accounting */
1451       nat44_session_update_counters (s0, now,
1452                                      vlib_buffer_length_in_chain
1453                                      (vm, b0), thread_index);
1454       /* Per-user LRU list maintenance */
1455       nat44_session_update_lru (sm, s0, thread_index);
1456
1457     trace0:
1458       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1459                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1460         {
1461           nat_in2out_ed_trace_t *t =
1462             vlib_add_trace (vm, node, b0, sizeof (*t));
1463           t->sw_if_index = sw_if_index0;
1464           t->next_index = next[0];
1465           t->is_slow_path = 1;
1466
1467           if (s0)
1468             t->session_index = s0 - tsm->sessions;
1469           else
1470             t->session_index = ~0;
1471         }
1472
1473       if (next[0] == NAT_NEXT_DROP)
1474         {
1475           vlib_increment_simple_counter (&sm->counters.slowpath.
1476                                          in2out_ed.drops, thread_index,
1477                                          sw_if_index0, 1);
1478         }
1479
1480       n_left_from--;
1481       next++;
1482       b++;
1483     }
1484
1485   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1486                                frame->n_vectors);
1487
1488   return frame->n_vectors;
1489 }
1490
1491 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1492                                      vlib_node_runtime_t * node,
1493                                      vlib_frame_t * frame)
1494 {
1495   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1496 }
1497
1498 /* *INDENT-OFF* */
1499 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1500   .name = "nat44-ed-in2out",
1501   .vector_size = sizeof (u32),
1502   .sibling_of = "nat-default",
1503   .format_trace = format_nat_in2out_ed_trace,
1504   .type = VLIB_NODE_TYPE_INTERNAL,
1505   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1506   .error_strings = nat_in2out_ed_error_strings,
1507   .runtime_data_bytes = sizeof (snat_runtime_t),
1508 };
1509 /* *INDENT-ON* */
1510
1511 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1512                                             vlib_node_runtime_t * node,
1513                                             vlib_frame_t * frame)
1514 {
1515   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1516 }
1517
1518 /* *INDENT-OFF* */
1519 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1520   .name = "nat44-ed-in2out-output",
1521   .vector_size = sizeof (u32),
1522   .sibling_of = "nat-default",
1523   .format_trace = format_nat_in2out_ed_trace,
1524   .type = VLIB_NODE_TYPE_INTERNAL,
1525   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1526   .error_strings = nat_in2out_ed_error_strings,
1527   .runtime_data_bytes = sizeof (snat_runtime_t),
1528 };
1529 /* *INDENT-ON* */
1530
1531 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1532                                               vlib_node_runtime_t *
1533                                               node, vlib_frame_t * frame)
1534 {
1535   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1536 }
1537
1538 /* *INDENT-OFF* */
1539 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1540   .name = "nat44-ed-in2out-slowpath",
1541   .vector_size = sizeof (u32),
1542   .sibling_of = "nat-default",
1543   .format_trace = format_nat_in2out_ed_trace,
1544   .type = VLIB_NODE_TYPE_INTERNAL,
1545   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1546   .error_strings = nat_in2out_ed_error_strings,
1547   .runtime_data_bytes = sizeof (snat_runtime_t),
1548 };
1549 /* *INDENT-ON* */
1550
1551 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1552                                                      vlib_node_runtime_t
1553                                                      * node,
1554                                                      vlib_frame_t * frame)
1555 {
1556   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1557 }
1558
1559 /* *INDENT-OFF* */
1560 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1561   .name = "nat44-ed-in2out-output-slowpath",
1562   .vector_size = sizeof (u32),
1563   .sibling_of = "nat-default",
1564   .format_trace = format_nat_in2out_ed_trace,
1565   .type = VLIB_NODE_TYPE_INTERNAL,
1566   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1567   .error_strings = nat_in2out_ed_error_strings,
1568   .runtime_data_bytes = sizeof (snat_runtime_t),
1569 };
1570 /* *INDENT-ON* */
1571
1572 static u8 *
1573 format_nat_pre_trace (u8 * s, va_list * args)
1574 {
1575   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1576   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1577   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1578   return format (s, "in2out next_index %d arc_next_index %d", t->next_index,
1579                  t->arc_next_index);
1580 }
1581
1582 VLIB_NODE_FN (nat_pre_in2out_node)
1583   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1584 {
1585   return nat_pre_node_fn_inline (vm, node, frame,
1586                                  NAT_NEXT_IN2OUT_ED_FAST_PATH);
1587 }
1588
1589 /* *INDENT-OFF* */
1590 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1591   .name = "nat-pre-in2out",
1592   .vector_size = sizeof (u32),
1593   .sibling_of = "nat-default",
1594   .format_trace = format_nat_pre_trace,
1595   .type = VLIB_NODE_TYPE_INTERNAL,
1596   .n_errors = 0,
1597 };
1598 /* *INDENT-ON* */
1599
1600 /*
1601  * fd.io coding-style-patch-verification: ON
1602  *
1603  * Local Variables:
1604  * eval: (c-set-style "gnu")
1605  * End:
1606  */