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