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