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