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