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