nat: output fib index fix
[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_always_inline u32
284 nat_outside_fib_index_lookup (snat_main_t * sm, ip4_address_t addr)
285 {
286   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
287   nat_outside_fib_t *outside_fib;
288   fib_prefix_t pfx = {
289     .fp_proto = FIB_PROTOCOL_IP4,
290     .fp_len = 32,
291     .fp_addr = {.ip4.as_u32 = addr.as_u32,}
292     ,
293   };
294   // TODO: multiple vrfs none can resolve addr
295   /* *INDENT-OFF* */
296   vec_foreach (outside_fib, sm->outside_fibs)
297     {
298       fei = fib_table_lookup (outside_fib->fib_index, &pfx);
299       if (FIB_NODE_INDEX_INVALID != fei)
300         {
301           if (fib_entry_get_resolving_interface (fei) != ~0)
302             {
303               return outside_fib->fib_index;
304             }
305         }
306     }
307   /* *INDENT-ON* */
308   return ~0;
309 }
310
311 static u32
312 slow_path_ed (snat_main_t * sm,
313               vlib_buffer_t * b,
314               ip4_address_t l_addr,
315               ip4_address_t r_addr,
316               u16 l_port,
317               u16 r_port,
318               u8 proto,
319               u32 rx_fib_index,
320               snat_session_t ** sessionp,
321               vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
322 {
323
324   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
325   clib_bihash_kv_16_8_t out2in_ed_kv;
326   nat44_is_idle_session_ctx_t ctx;
327   snat_session_key_t key0, key1;
328   ip4_address_t allocated_addr;
329   u16 allocated_port;
330   u32 tx_fib_index;
331   u8 identity_nat;
332
333   u32 nat_proto = ip_proto_to_nat_proto (proto);
334   snat_session_t *s = NULL;
335   lb_nat_type_t lb = 0;
336
337   if (PREDICT_TRUE (nat_proto == NAT_PROTOCOL_TCP))
338     {
339       if (PREDICT_FALSE
340           (!tcp_flags_is_init
341            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
342         {
343           b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
344           return NAT_NEXT_DROP;
345         }
346     }
347
348   if (PREDICT_FALSE
349       (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
350     {
351       if (!nat_lru_free_one (sm, thread_index, now))
352         {
353           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
354           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
355           nat_elog_notice ("maximum sessions exceeded");
356           return NAT_NEXT_DROP;
357         }
358     }
359
360   key0.addr = l_addr;
361   key0.port = l_port;
362   key1.protocol = key0.protocol = nat_proto;
363   key0.fib_index = rx_fib_index;
364   key1.fib_index = sm->outside_fib_index;
365   tx_fib_index = sm->outside_fib_index;
366
367   /* First try to match static mapping by local address and port */
368   if (snat_static_mapping_match
369       (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
370     {
371       s = nat_ed_session_alloc (sm, thread_index, now, proto);
372       if (!s)
373         {
374           nat_elog_warn ("create NAT session failed");
375           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
376           return NAT_NEXT_DROP;
377         }
378       switch (vec_len (sm->outside_fibs))
379         {
380         case 0:
381           tx_fib_index = sm->outside_fib_index;
382           break;
383         case 1:
384           tx_fib_index = sm->outside_fibs[0].fib_index;
385           break;
386         default:
387           tx_fib_index = nat_outside_fib_index_lookup (sm, r_addr);
388           break;
389         }
390
391       s->out2in.fib_index = tx_fib_index;
392       /* Try to create dynamic translation */
393       if (nat_ed_alloc_addr_and_port (sm, rx_fib_index, nat_proto,
394                                       thread_index, r_addr, r_port, proto,
395                                       sm->port_per_thread,
396                                       tsm->snat_thread_index, s,
397                                       &allocated_addr,
398                                       &allocated_port, &out2in_ed_kv))
399         {
400           nat_elog_notice ("addresses exhausted");
401           b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
402           nat_ed_session_delete (sm, s, thread_index, 1);
403           return NAT_NEXT_DROP;
404         }
405       key1.addr = allocated_addr;
406       key1.port = allocated_port;
407     }
408   else
409     {
410       if (PREDICT_FALSE (identity_nat))
411         {
412           *sessionp = s;
413           return next;
414         }
415       s = nat_ed_session_alloc (sm, thread_index, now, proto);
416       if (!s)
417         {
418           nat_elog_warn ("create NAT session failed");
419           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
420           return NAT_NEXT_DROP;
421         }
422       switch (vec_len (sm->outside_fibs))
423         {
424         case 0:
425           tx_fib_index = sm->outside_fib_index;
426           break;
427         case 1:
428           tx_fib_index = sm->outside_fibs[0].fib_index;
429           break;
430         default:
431           tx_fib_index = nat_outside_fib_index_lookup (sm, r_addr);
432           break;
433         }
434
435       s->out2in.fib_index = tx_fib_index;
436       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
437
438       make_ed_kv (&key1.addr, &r_addr, proto,
439                   s->out2in.fib_index, key1.port, r_port, thread_index,
440                   s - tsm->sessions, &out2in_ed_kv);
441       if (clib_bihash_add_or_overwrite_stale_16_8
442           (&sm->out2in_ed, &out2in_ed_kv, nat44_o2i_ed_is_idle_session_cb,
443            &ctx))
444         nat_elog_notice ("out2in-ed key add failed");
445     }
446
447   if (lb)
448     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
449   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
450   s->ext_host_addr = r_addr;
451   s->ext_host_port = r_port;
452   s->in2out = key0;
453   s->out2in = key1;
454   s->out2in.fib_index = tx_fib_index;
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
772   switch (vec_len (sm->outside_fibs))
773     {
774     case 0:
775       outside_fib_index = sm->outside_fib_index;
776       break;
777     case 1:
778       outside_fib_index = sm->outside_fibs[0].fib_index;
779       break;
780     default:
781       outside_fib_index = nat_outside_fib_index_lookup (sm, ip->dst_address);
782       break;
783     }
784   old_addr = ip->src_address.as_u32;
785
786   make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
787               rx_fib_index, 0, 0, ~0, ~0, &s_kv);
788
789   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
790     {
791       ASSERT (thread_index == ed_value_get_thread_index (&s_value));
792       s =
793         pool_elt_at_index (tsm->sessions,
794                            ed_value_get_session_index (&s_value));
795       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
796     }
797   else
798     {
799       if (PREDICT_FALSE
800           (nat44_ed_maximum_sessions_exceeded
801            (sm, rx_fib_index, thread_index)))
802         {
803           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
804           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
805           nat_elog_notice ("maximum sessions exceeded");
806           return 0;
807         }
808
809       make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
810
811       /* Try to find static mapping first */
812       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
813         {
814           m = pool_elt_at_index (sm->static_mappings, value.value);
815           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
816           is_sm = 1;
817           goto create_ses;
818         }
819       else
820         {
821           /* *INDENT-OFF* */
822           pool_foreach (s, tsm->sessions, {
823             if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
824               {
825                 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
826
827                 make_ed_kv (&s->out2in.addr, &ip->dst_address, ip->protocol,
828                             outside_fib_index, 0, 0, ~0, ~0, &s_kv);
829                 if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
830                   goto create_ses;
831
832                 break;
833               }
834           });
835           /* *INDENT-ON* */
836
837           for (i = 0; i < vec_len (sm->addresses); i++)
838             {
839               make_ed_kv (&sm->addresses[i].addr, &ip->dst_address,
840                           ip->protocol, outside_fib_index, 0, 0, ~0, ~0,
841                           &s_kv);
842               if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
843                 {
844                   new_addr = ip->src_address.as_u32 =
845                     sm->addresses[i].addr.as_u32;
846                   goto create_ses;
847                 }
848             }
849           return 0;
850         }
851
852     create_ses:
853       s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
854       if (!s)
855         {
856           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
857           nat_elog_warn ("create NAT session failed");
858           return 0;
859         }
860
861       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
862       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
863       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
864       s->out2in.addr.as_u32 = new_addr;
865       s->out2in.fib_index = outside_fib_index;
866       s->in2out.addr.as_u32 = old_addr;
867       s->in2out.fib_index = rx_fib_index;
868       s->in2out.port = s->out2in.port = ip->protocol;
869       if (is_sm)
870         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
871
872       /* Add to lookup tables */
873       make_ed_kv (&s->in2out.addr, &ip->dst_address, ip->protocol,
874                   rx_fib_index, 0, 0, thread_index, s - tsm->sessions, &s_kv);
875       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
876         nat_elog_notice ("in2out key add failed");
877
878       make_ed_kv (&s->out2in.addr, &ip->dst_address, ip->protocol,
879                   outside_fib_index, 0, 0, thread_index, s - tsm->sessions,
880                   &s_kv);
881       if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
882         nat_elog_notice ("out2in key add failed");
883     }
884
885   /* Update IP checksum */
886   sum = ip->checksum;
887   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
888   ip->checksum = ip_csum_fold (sum);
889
890   /* Accounting */
891   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
892                                  thread_index);
893   /* Per-user LRU list maintenance */
894   nat44_session_update_lru (sm, s, thread_index);
895
896   /* Hairpinning */
897   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
898     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
899
900   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
901     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
902
903   return s;
904 }
905
906 static inline uword
907 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
908                                           vlib_node_runtime_t * node,
909                                           vlib_frame_t * frame,
910                                           int is_output_feature)
911 {
912   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
913   nat_next_t next_index;
914   snat_main_t *sm = &snat_main;
915   f64 now = vlib_time_now (vm);
916   u32 thread_index = vm->thread_index;
917   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
918   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
919     0, def_slow;
920
921   def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH :
922     NAT_NEXT_IN2OUT_ED_SLOW_PATH;
923
924   stats_node_index = sm->ed_in2out_node_index;
925
926   from = vlib_frame_vector_args (frame);
927   n_left_from = frame->n_vectors;
928   next_index = node->cached_next_index;
929
930   while (n_left_from > 0)
931     {
932       u32 n_left_to_next;
933
934       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
935
936       while (n_left_from > 0 && n_left_to_next > 0)
937         {
938           u32 bi0;
939           vlib_buffer_t *b0;
940           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
941             new_addr0, old_addr0;
942           u16 old_port0, new_port0;
943           ip4_header_t *ip0;
944           udp_header_t *udp0;
945           tcp_header_t *tcp0;
946           snat_session_t *s0 = 0;
947           clib_bihash_kv_16_8_t kv0, value0;
948           ip_csum_t sum0;
949
950           /* speculatively enqueue b0 to the current next frame */
951           bi0 = from[0];
952           to_next[0] = bi0;
953           from += 1;
954           to_next += 1;
955           n_left_from -= 1;
956           n_left_to_next -= 1;
957
958           b0 = vlib_get_buffer (vm, bi0);
959
960           if (is_output_feature)
961             {
962               vnet_feature_next (&vnet_buffer2 (b0)->nat.arc_next, b0);
963               iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
964             }
965
966           next0 = vnet_buffer2 (b0)->nat.arc_next;
967
968           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
969                                   iph_offset0);
970
971           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
972           rx_fib_index0 =
973             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
974                                                  sw_if_index0);
975
976           if (PREDICT_FALSE (ip0->ttl == 1))
977             {
978               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
979               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
980                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
981                                            0);
982               next0 = NAT_NEXT_ICMP_ERROR;
983               goto trace0;
984             }
985
986           udp0 = ip4_next_header (ip0);
987           tcp0 = (tcp_header_t *) udp0;
988           proto0 = ip_proto_to_nat_proto (ip0->protocol);
989
990           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
991             {
992               next0 = def_slow;
993               goto trace0;
994             }
995
996           if (is_output_feature)
997             {
998               if (PREDICT_FALSE (nat_not_translate_output_feature_fwd
999                                  (sm, ip0, thread_index, now, vm, b0)))
1000                 goto trace0;
1001             }
1002
1003           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1004             {
1005               next0 = def_slow;
1006               goto trace0;
1007             }
1008
1009           make_ed_kv (&ip0->src_address, &ip0->dst_address,
1010                       ip0->protocol, rx_fib_index0,
1011                       vnet_buffer (b0)->ip.reass.l4_src_port,
1012                       vnet_buffer (b0)->ip.reass.l4_dst_port, ~0, ~0, &kv0);
1013
1014           // lookup for session
1015           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1016             {
1017               // session does not exist go slow path
1018               next0 = def_slow;
1019               goto trace0;
1020             }
1021           ASSERT (thread_index == ed_value_get_thread_index (&value0));
1022           s0 =
1023             pool_elt_at_index (tsm->sessions,
1024                                ed_value_get_session_index (&value0));
1025
1026           if (s0->tcp_closed_timestamp)
1027             {
1028               if (now >= s0->tcp_closed_timestamp)
1029                 {
1030                   // session is closed, go slow path
1031                   next0 = def_slow;
1032                 }
1033               else
1034                 {
1035                   // session in transitory timeout, drop
1036                   b0->error = node->errors[NAT_IN2OUT_ED_ERROR_TCP_CLOSED];
1037                   next0 = NAT_NEXT_DROP;
1038                 }
1039               goto trace0;
1040             }
1041
1042           // drop if session expired
1043           u64 sess_timeout_time;
1044           sess_timeout_time = s0->last_heard +
1045             (f64) nat44_session_get_timeout (sm, s0);
1046           if (now >= sess_timeout_time)
1047             {
1048               nat_free_session_data (sm, s0, thread_index, 0);
1049               nat_ed_session_delete (sm, s0, thread_index, 1);
1050               // session is closed, go slow path
1051               next0 = def_slow;
1052               goto trace0;
1053             }
1054
1055           b0->flags |= VNET_BUFFER_F_IS_NATED;
1056
1057           if (!is_output_feature)
1058             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1059
1060           old_addr0 = ip0->src_address.as_u32;
1061           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1062           sum0 = ip0->checksum;
1063           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1064                                  src_address);
1065           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1066             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1067                                    s0->ext_host_addr.as_u32, ip4_header_t,
1068                                    dst_address);
1069           ip0->checksum = ip_csum_fold (sum0);
1070
1071           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1072
1073           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1074             {
1075               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1076                 {
1077                   new_port0 = udp0->src_port = s0->out2in.port;
1078                   sum0 = tcp0->checksum;
1079                   sum0 =
1080                     ip_csum_update (sum0, old_addr0, new_addr0,
1081                                     ip4_header_t, dst_address);
1082                   sum0 =
1083                     ip_csum_update (sum0, old_port0, new_port0,
1084                                     ip4_header_t, length);
1085                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1086                     {
1087                       sum0 =
1088                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1089                                         s0->ext_host_addr.as_u32,
1090                                         ip4_header_t, dst_address);
1091                       sum0 =
1092                         ip_csum_update (sum0,
1093                                         vnet_buffer (b0)->ip.
1094                                         reass.l4_dst_port, s0->ext_host_port,
1095                                         ip4_header_t, length);
1096                       tcp0->dst_port = s0->ext_host_port;
1097                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1098                     }
1099                   mss_clamping (sm, tcp0, &sum0);
1100                   tcp0->checksum = ip_csum_fold (sum0);
1101                 }
1102               tcp_packets++;
1103               if (nat44_set_tcp_session_state_i2o
1104                   (sm, now, s0, b0, thread_index))
1105                 goto trace0;
1106             }
1107           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1108                    && udp0->checksum)
1109             {
1110               new_port0 = udp0->src_port = s0->out2in.port;
1111               sum0 = udp0->checksum;
1112               sum0 =
1113                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1114                                 dst_address);
1115               sum0 =
1116                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1117                                 length);
1118               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1119                 {
1120                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1121                                          s0->ext_host_addr.as_u32,
1122                                          ip4_header_t, dst_address);
1123                   sum0 =
1124                     ip_csum_update (sum0,
1125                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1126                                     s0->ext_host_port, ip4_header_t, length);
1127                   udp0->dst_port = s0->ext_host_port;
1128                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1129                 }
1130               udp0->checksum = ip_csum_fold (sum0);
1131               udp_packets++;
1132             }
1133           else
1134             {
1135               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1136                 {
1137                   new_port0 = udp0->src_port = s0->out2in.port;
1138                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1139                     {
1140                       udp0->dst_port = s0->ext_host_port;
1141                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1142                     }
1143                   udp_packets++;
1144                 }
1145             }
1146
1147           /* Accounting */
1148           nat44_session_update_counters (s0, now,
1149                                          vlib_buffer_length_in_chain
1150                                          (vm, b0), thread_index);
1151           /* Per-user LRU list maintenance */
1152           nat44_session_update_lru (sm, s0, thread_index);
1153
1154         trace0:
1155           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1156                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1157             {
1158               nat_in2out_ed_trace_t *t =
1159                 vlib_add_trace (vm, node, b0, sizeof (*t));
1160               t->sw_if_index = sw_if_index0;
1161               t->next_index = next0;
1162               t->is_slow_path = 0;
1163
1164               if (s0)
1165                 t->session_index = s0 - tsm->sessions;
1166               else
1167                 t->session_index = ~0;
1168             }
1169
1170           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1171           /* verify speculative enqueue, maybe switch current next frame */
1172           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1173                                            to_next, n_left_to_next,
1174                                            bi0, next0);
1175         }
1176
1177       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1178     }
1179
1180   vlib_node_increment_counter (vm, stats_node_index,
1181                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1182                                pkts_processed);
1183   vlib_node_increment_counter (vm, stats_node_index,
1184                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1185   vlib_node_increment_counter (vm, stats_node_index,
1186                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1187   vlib_node_increment_counter (vm, stats_node_index,
1188                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1189                                icmp_packets);
1190   vlib_node_increment_counter (vm, stats_node_index,
1191                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1192                                other_packets);
1193   return frame->n_vectors;
1194 }
1195
1196 static inline uword
1197 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t * vm,
1198                                           vlib_node_runtime_t * node,
1199                                           vlib_frame_t * frame,
1200                                           int is_output_feature)
1201 {
1202   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
1203   nat_next_t next_index;
1204   snat_main_t *sm = &snat_main;
1205   f64 now = vlib_time_now (vm);
1206   u32 thread_index = vm->thread_index;
1207   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1208   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets = 0;
1209
1210   stats_node_index = sm->ed_in2out_slowpath_node_index;
1211
1212   from = vlib_frame_vector_args (frame);
1213   n_left_from = frame->n_vectors;
1214   next_index = node->cached_next_index;
1215
1216   while (n_left_from > 0)
1217     {
1218       u32 n_left_to_next;
1219
1220       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1221
1222       while (n_left_from > 0 && n_left_to_next > 0)
1223         {
1224           u32 bi0;
1225           vlib_buffer_t *b0;
1226           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1227             new_addr0, old_addr0;
1228           u16 old_port0, new_port0;
1229           ip4_header_t *ip0;
1230           udp_header_t *udp0;
1231           tcp_header_t *tcp0;
1232           icmp46_header_t *icmp0;
1233           snat_session_t *s0 = 0;
1234           clib_bihash_kv_16_8_t kv0, value0;
1235           ip_csum_t sum0;
1236
1237           /* speculatively enqueue b0 to the current next frame */
1238           bi0 = from[0];
1239           to_next[0] = bi0;
1240           from += 1;
1241           to_next += 1;
1242           n_left_from -= 1;
1243           n_left_to_next -= 1;
1244
1245           b0 = vlib_get_buffer (vm, bi0);
1246
1247           if (is_output_feature)
1248             iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1249
1250           next0 = vnet_buffer2 (b0)->nat.arc_next;
1251
1252           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1253                                   iph_offset0);
1254
1255           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1256           rx_fib_index0 =
1257             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1258                                                  sw_if_index0);
1259
1260           if (PREDICT_FALSE (ip0->ttl == 1))
1261             {
1262               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1263               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1264                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1265                                            0);
1266               next0 = NAT_NEXT_ICMP_ERROR;
1267               goto trace0;
1268             }
1269
1270           udp0 = ip4_next_header (ip0);
1271           tcp0 = (tcp_header_t *) udp0;
1272           icmp0 = (icmp46_header_t *) udp0;
1273           proto0 = ip_proto_to_nat_proto (ip0->protocol);
1274
1275           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1276             {
1277               s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1278                                                   rx_fib_index0,
1279                                                   thread_index, now,
1280                                                   vm, node);
1281               if (!s0)
1282                 next0 = NAT_NEXT_DROP;
1283
1284               other_packets++;
1285               goto trace0;
1286             }
1287
1288           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1289             {
1290               next0 = icmp_in2out_ed_slow_path
1291                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1292                  node, next0, now, thread_index, &s0);
1293               icmp_packets++;
1294               goto trace0;
1295             }
1296
1297           // move down
1298           make_ed_kv (&ip0->src_address, &ip0->dst_address,
1299                       ip0->protocol, rx_fib_index0,
1300                       vnet_buffer (b0)->ip.reass.l4_src_port,
1301                       vnet_buffer (b0)->ip.reass.l4_dst_port, ~0, ~0, &kv0);
1302
1303           if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1304             {
1305               ASSERT (thread_index == ed_value_get_thread_index (&value0));
1306               s0 =
1307                 pool_elt_at_index (tsm->sessions,
1308                                    ed_value_get_session_index (&value0));
1309
1310               if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1311                 {
1312                   nat_free_session_data (sm, s0, thread_index, 0);
1313                   nat_ed_session_delete (sm, s0, thread_index, 1);
1314                   s0 = NULL;
1315                 }
1316             }
1317
1318           if (!s0)
1319             {
1320               if (is_output_feature)
1321                 {
1322                   if (PREDICT_FALSE
1323                       (nat44_ed_not_translate_output_feature
1324                        (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1325                         vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1326                         sw_if_index0,
1327                         vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1328                     goto trace0;
1329
1330                   /*
1331                    * Send DHCP packets to the ipv4 stack, or we won't
1332                    * be able to use dhcp client on the outside interface
1333                    */
1334                   if (PREDICT_FALSE
1335                       (proto0 == NAT_PROTOCOL_UDP
1336                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1337                            clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1338                        && ip0->dst_address.as_u32 == 0xffffffff))
1339                     goto trace0;
1340                 }
1341               else
1342                 {
1343                   if (PREDICT_FALSE
1344                       (nat44_ed_not_translate
1345                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1346                         thread_index)))
1347                     goto trace0;
1348                 }
1349
1350               next0 =
1351                 slow_path_ed (sm, b0, ip0->src_address, ip0->dst_address,
1352                               vnet_buffer (b0)->ip.reass.l4_src_port,
1353                               vnet_buffer (b0)->ip.reass.l4_dst_port,
1354                               ip0->protocol, rx_fib_index0, &s0, node, next0,
1355                               thread_index, now);
1356
1357               if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1358                 goto trace0;
1359
1360               if (PREDICT_FALSE (!s0))
1361                 goto trace0;
1362
1363             }
1364
1365           b0->flags |= VNET_BUFFER_F_IS_NATED;
1366
1367           if (!is_output_feature)
1368             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1369
1370           old_addr0 = ip0->src_address.as_u32;
1371           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1372           sum0 = ip0->checksum;
1373           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1374                                  src_address);
1375           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1376             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1377                                    s0->ext_host_addr.as_u32, ip4_header_t,
1378                                    dst_address);
1379           ip0->checksum = ip_csum_fold (sum0);
1380
1381           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1382
1383           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1384             {
1385               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1386                 {
1387                   new_port0 = udp0->src_port = s0->out2in.port;
1388                   sum0 = tcp0->checksum;
1389                   sum0 =
1390                     ip_csum_update (sum0, old_addr0, new_addr0,
1391                                     ip4_header_t, dst_address);
1392                   sum0 =
1393                     ip_csum_update (sum0, old_port0, new_port0,
1394                                     ip4_header_t, length);
1395                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1396                     {
1397                       sum0 =
1398                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1399                                         s0->ext_host_addr.as_u32,
1400                                         ip4_header_t, dst_address);
1401                       sum0 =
1402                         ip_csum_update (sum0,
1403                                         vnet_buffer (b0)->ip.
1404                                         reass.l4_dst_port, s0->ext_host_port,
1405                                         ip4_header_t, length);
1406                       tcp0->dst_port = s0->ext_host_port;
1407                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1408                     }
1409                   mss_clamping (sm, tcp0, &sum0);
1410                   tcp0->checksum = ip_csum_fold (sum0);
1411                 }
1412               tcp_packets++;
1413               if (nat44_set_tcp_session_state_i2o
1414                   (sm, now, s0, b0, thread_index))
1415                 goto trace0;
1416             }
1417           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1418                    && udp0->checksum)
1419             {
1420               new_port0 = udp0->src_port = s0->out2in.port;
1421               sum0 = udp0->checksum;
1422               sum0 =
1423                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1424                                 dst_address);
1425               sum0 =
1426                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1427                                 length);
1428               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1429                 {
1430                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1431                                          s0->ext_host_addr.as_u32,
1432                                          ip4_header_t, dst_address);
1433                   sum0 =
1434                     ip_csum_update (sum0,
1435                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1436                                     s0->ext_host_port, ip4_header_t, length);
1437                   udp0->dst_port = s0->ext_host_port;
1438                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1439                 }
1440               udp0->checksum = ip_csum_fold (sum0);
1441               udp_packets++;
1442             }
1443           else
1444             {
1445               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1446                 {
1447                   new_port0 = udp0->src_port = s0->out2in.port;
1448                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1449                     {
1450                       udp0->dst_port = s0->ext_host_port;
1451                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1452                     }
1453                   udp_packets++;
1454                 }
1455             }
1456
1457           /* Accounting */
1458           nat44_session_update_counters (s0, now,
1459                                          vlib_buffer_length_in_chain
1460                                          (vm, b0), thread_index);
1461           /* Per-user LRU list maintenance */
1462           nat44_session_update_lru (sm, s0, thread_index);
1463
1464         trace0:
1465           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1466                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1467             {
1468               nat_in2out_ed_trace_t *t =
1469                 vlib_add_trace (vm, node, b0, sizeof (*t));
1470               t->sw_if_index = sw_if_index0;
1471               t->next_index = next0;
1472               t->is_slow_path = 1;
1473
1474               if (s0)
1475                 t->session_index = s0 - tsm->sessions;
1476               else
1477                 t->session_index = ~0;
1478             }
1479
1480           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1481
1482           /* verify speculative enqueue, maybe switch current next frame */
1483           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1484                                            to_next, n_left_to_next,
1485                                            bi0, next0);
1486         }
1487
1488       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1489     }
1490
1491   vlib_node_increment_counter (vm, stats_node_index,
1492                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1493                                pkts_processed);
1494   vlib_node_increment_counter (vm, stats_node_index,
1495                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1496   vlib_node_increment_counter (vm, stats_node_index,
1497                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1498   vlib_node_increment_counter (vm, stats_node_index,
1499                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1500                                icmp_packets);
1501   vlib_node_increment_counter (vm, stats_node_index,
1502                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1503                                other_packets);
1504   return frame->n_vectors;
1505 }
1506
1507 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1508                                      vlib_node_runtime_t * node,
1509                                      vlib_frame_t * frame)
1510 {
1511   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1512 }
1513
1514 /* *INDENT-OFF* */
1515 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1516   .name = "nat44-ed-in2out",
1517   .vector_size = sizeof (u32),
1518   .sibling_of = "nat-default",
1519   .format_trace = format_nat_in2out_ed_trace,
1520   .type = VLIB_NODE_TYPE_INTERNAL,
1521   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1522   .error_strings = nat_in2out_ed_error_strings,
1523   .runtime_data_bytes = sizeof (snat_runtime_t),
1524 };
1525 /* *INDENT-ON* */
1526
1527 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1528                                             vlib_node_runtime_t * node,
1529                                             vlib_frame_t * frame)
1530 {
1531   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1532 }
1533
1534 /* *INDENT-OFF* */
1535 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1536   .name = "nat44-ed-in2out-output",
1537   .vector_size = sizeof (u32),
1538   .sibling_of = "nat-default",
1539   .format_trace = format_nat_in2out_ed_trace,
1540   .type = VLIB_NODE_TYPE_INTERNAL,
1541   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1542   .error_strings = nat_in2out_ed_error_strings,
1543   .runtime_data_bytes = sizeof (snat_runtime_t),
1544 };
1545 /* *INDENT-ON* */
1546
1547 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1548                                               vlib_node_runtime_t *
1549                                               node, vlib_frame_t * frame)
1550 {
1551   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1552 }
1553
1554 /* *INDENT-OFF* */
1555 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1556   .name = "nat44-ed-in2out-slowpath",
1557   .vector_size = sizeof (u32),
1558   .sibling_of = "nat-default",
1559   .format_trace = format_nat_in2out_ed_trace,
1560   .type = VLIB_NODE_TYPE_INTERNAL,
1561   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1562   .error_strings = nat_in2out_ed_error_strings,
1563   .runtime_data_bytes = sizeof (snat_runtime_t),
1564 };
1565 /* *INDENT-ON* */
1566
1567 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1568                                                      vlib_node_runtime_t
1569                                                      * node,
1570                                                      vlib_frame_t * frame)
1571 {
1572   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1573 }
1574
1575 /* *INDENT-OFF* */
1576 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1577   .name = "nat44-ed-in2out-output-slowpath",
1578   .vector_size = sizeof (u32),
1579   .sibling_of = "nat-default",
1580   .format_trace = format_nat_in2out_ed_trace,
1581   .type = VLIB_NODE_TYPE_INTERNAL,
1582   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1583   .error_strings = nat_in2out_ed_error_strings,
1584   .runtime_data_bytes = sizeof (snat_runtime_t),
1585 };
1586 /* *INDENT-ON* */
1587
1588 static u8 *
1589 format_nat_pre_trace (u8 * s, va_list * args)
1590 {
1591   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1592   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1593   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1594   return format (s, "in2out next_index %d arc_next_index %d", t->next_index,
1595                  t->arc_next_index);
1596 }
1597
1598 VLIB_NODE_FN (nat_pre_in2out_node)
1599   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1600 {
1601   return nat_pre_node_fn_inline (vm, node, frame,
1602                                  NAT_NEXT_IN2OUT_ED_FAST_PATH);
1603 }
1604
1605 /* *INDENT-OFF* */
1606 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1607   .name = "nat-pre-in2out",
1608   .vector_size = sizeof (u32),
1609   .sibling_of = "nat-default",
1610   .format_trace = format_nat_pre_trace,
1611   .type = VLIB_NODE_TYPE_INTERNAL,
1612   .n_errors = 0,
1613 };
1614 /* *INDENT-ON* */
1615
1616 /*
1617  * fd.io coding-style-patch-verification: ON
1618  *
1619  * Local Variables:
1620  * eval: (c-set-style "gnu")
1621  * End:
1622  */