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