nat: nat44-ed i2o & o2i trace fix
[vpp.git] / src / plugins / nat / nat44-ed / nat44_ed_out2in.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 outside to inside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/udp/udp_local.h>
26 #include <vppinfra/error.h>
27
28 #include <nat/lib/nat_syslog.h>
29 #include <nat/lib/ipfix_logging.h>
30
31 #include <nat/nat44-ed/nat44_ed.h>
32 #include <nat/nat44-ed/nat44_ed_inlines.h>
33
34 static char *nat_out2in_ed_error_strings[] = {
35 #define _(sym,string) string,
36   foreach_nat_out2in_ed_error
37 #undef _
38 };
39
40 typedef struct
41 {
42   u32 sw_if_index;
43   u32 next_index;
44   u32 session_index;
45   nat_translation_error_e translation_error;
46   nat_6t_flow_t i2of;
47   nat_6t_flow_t o2if;
48   clib_bihash_kv_16_8_t search_key;
49   u8 is_slow_path;
50   u8 translation_via_i2of;
51   u8 lookup_skipped;
52 } nat44_ed_out2in_trace_t;
53
54 static u8 *
55 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
56 {
57   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59   nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
60   char *tag;
61
62   tag =
63     t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
64     "NAT44_OUT2IN_ED_FAST_PATH";
65
66   s = format (s, "%s: sw_if_index %d, next index %d", tag, t->sw_if_index,
67               t->next_index);
68   if (~0 != t->session_index)
69     {
70       s = format (s, ", session %d, translation result '%U' via %s",
71                   t->session_index, format_nat_ed_translation_error,
72                   t->translation_error,
73                   t->translation_via_i2of ? "i2of" : "o2if");
74       s = format (s, "\n  i2of %U", format_nat_6t_flow, &t->i2of);
75       s = format (s, "\n  o2if %U", format_nat_6t_flow, &t->o2if);
76     }
77   if (!t->is_slow_path)
78     {
79       if (t->lookup_skipped)
80         {
81           s = format (s, "\n lookup skipped - cached session index used");
82         }
83       else
84         {
85           s = format (s, "\n  search key %U", format_ed_session_kvp,
86                       &t->search_key);
87         }
88     }
89
90   return s;
91 }
92
93 static int
94 next_src_nat (snat_main_t *sm, ip4_header_t *ip, u16 src_port, u16 dst_port,
95               u32 thread_index, u32 rx_fib_index)
96 {
97   clib_bihash_kv_16_8_t kv, value;
98
99   init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
100              rx_fib_index, ip->protocol);
101   if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
102     return 1;
103
104   return 0;
105 }
106
107 static void create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b,
108                                    snat_session_t *s, ip4_header_t *ip,
109                                    u32 rx_fib_index, u32 thread_index);
110
111 static snat_session_t *create_session_for_static_mapping_ed (
112   snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
113   u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
114   nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
115   u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
116   snat_static_mapping_t *mapping);
117
118 static inline u32
119 icmp_out2in_ed_slow_path (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
120                           icmp46_header_t *icmp, u32 sw_if_index,
121                           u32 rx_fib_index, vlib_node_runtime_t *node,
122                           u32 next, f64 now, u32 thread_index,
123                           snat_session_t **s_p)
124 {
125   vlib_main_t *vm = vlib_get_main ();
126
127   ip_csum_t sum;
128   u16 checksum;
129
130   snat_session_t *s = 0;
131   u8 is_addr_only, identity_nat;
132   ip4_address_t sm_addr;
133   u16 sm_port;
134   u32 sm_fib_index;
135   snat_static_mapping_t *m;
136   u8 lookup_protocol;
137   ip4_address_t lookup_saddr, lookup_daddr;
138   u16 lookup_sport, lookup_dport;
139
140   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
141   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
142
143   if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr, &lookup_sport,
144                                           &lookup_daddr, &lookup_dport,
145                                           &lookup_protocol))
146     {
147       b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
148       next = NAT_NEXT_DROP;
149       goto out;
150     }
151
152   if (snat_static_mapping_match (
153         vm, sm, ip->dst_address, lookup_sport, rx_fib_index,
154         ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
155         &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat, &m))
156     {
157       // static mapping not matched
158       if (!sm->forwarding_enabled)
159         {
160           /* Don't NAT packet aimed at the intfc address */
161           if (!is_interface_addr (sm, node, sw_if_index,
162                                   ip->dst_address.as_u32))
163             {
164               b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
165               next = NAT_NEXT_DROP;
166             }
167         }
168       else
169         {
170           if (next_src_nat (sm, ip, lookup_sport, lookup_dport, thread_index,
171                             rx_fib_index))
172             {
173               next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
174             }
175           else
176             {
177               create_bypass_for_fwd (sm, b, s, ip, rx_fib_index, thread_index);
178             }
179         }
180       goto out;
181     }
182
183   if (PREDICT_FALSE (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
184                        ICMP4_echo_reply &&
185                      (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
186                         ICMP4_echo_request ||
187                       !is_addr_only)))
188     {
189       b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
190       next = NAT_NEXT_DROP;
191       goto out;
192     }
193
194   if (PREDICT_FALSE (identity_nat))
195     {
196       goto out;
197     }
198
199   /* Create session initiated by host from external network */
200   s = create_session_for_static_mapping_ed (
201     sm, b, sm_addr, sm_port, sm_fib_index, ip->dst_address, lookup_sport,
202     rx_fib_index, ip_proto_to_nat_proto (lookup_protocol), node, rx_fib_index,
203     thread_index, 0, 0, vlib_time_now (vm), m);
204   if (!s)
205     next = NAT_NEXT_DROP;
206
207   if (PREDICT_TRUE (!ip4_is_fragment (ip)))
208     {
209       sum = ip_incremental_checksum_buffer (
210         vm, b, (u8 *) icmp - (u8 *) vlib_buffer_get_current (b),
211         ntohs (ip->length) - ip4_header_bytes (ip), 0);
212       checksum = ~ip_csum_fold (sum);
213       if (checksum != 0 && checksum != 0xffff)
214         {
215           next = NAT_NEXT_DROP;
216           goto out;
217         }
218     }
219
220   if (PREDICT_TRUE (next != NAT_NEXT_DROP && s))
221     {
222       /* Accounting */
223       nat44_session_update_counters (
224         s, now, vlib_buffer_length_in_chain (vm, b), thread_index);
225       /* Per-user LRU list maintenance */
226       nat44_session_update_lru (sm, s, thread_index);
227     }
228 out:
229   if (NAT_NEXT_DROP == next && s)
230     {
231       nat_ed_session_delete (sm, s, thread_index, 1);
232       s = 0;
233     }
234   *s_p = s;
235   return next;
236 }
237
238 // allocate exact address based on preference
239 static_always_inline int
240 nat_alloc_addr_and_port_exact (snat_address_t * a,
241                                u32 thread_index,
242                                nat_protocol_t proto,
243                                ip4_address_t * addr,
244                                u16 * port,
245                                u16 port_per_thread, u32 snat_thread_index)
246 {
247   snat_main_t *sm = &snat_main;
248   u32 portnum;
249
250   switch (proto)
251     {
252 #define _(N, j, n, s) \
253     case NAT_PROTOCOL_##N: \
254       if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
255         { \
256           while (1) \
257             { \
258               portnum = (port_per_thread * \
259                 snat_thread_index) + \
260                 snat_random_port(0, port_per_thread - 1) + 1024; \
261               if (a->busy_##n##_port_refcounts[portnum]) \
262                 continue; \
263               --a->busy_##n##_port_refcounts[portnum]; \
264               a->busy_##n##_ports_per_thread[thread_index]++; \
265               a->busy_##n##_ports++; \
266               *addr = a->addr; \
267               *port = clib_host_to_net_u16(portnum); \
268               return 0; \
269             } \
270         } \
271       break;
272       foreach_nat_protocol
273 #undef _
274         default : nat_elog_info (sm, "unknown protocol");
275       return 1;
276     }
277
278   /* Totally out of translations to use... */
279   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
280   return 1;
281 }
282
283 static_always_inline int
284 nat44_ed_alloc_outside_addr_and_port (snat_address_t *addresses, u32 fib_index,
285                                       u32 thread_index, nat_protocol_t proto,
286                                       ip4_address_t *addr, u16 *port,
287                                       u16 port_per_thread,
288                                       u32 snat_thread_index)
289 {
290   snat_main_t *sm = &snat_main;
291   snat_address_t *a, *ga = 0;
292   u32 portnum;
293   int i;
294
295   for (i = 0; i < vec_len (addresses); i++)
296     {
297       a = addresses + i;
298       switch (proto)
299         {
300 #define _(N, j, n, s)                                                         \
301   case NAT_PROTOCOL_##N:                                                      \
302     if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread)       \
303       {                                                                       \
304         if (a->fib_index == fib_index)                                        \
305           {                                                                   \
306             while (1)                                                         \
307               {                                                               \
308                 portnum = (port_per_thread * snat_thread_index) +             \
309                           snat_random_port (0, port_per_thread - 1) + 1024;   \
310                 if (a->busy_##n##_port_refcounts[portnum])                    \
311                   continue;                                                   \
312                 --a->busy_##n##_port_refcounts[portnum];                      \
313                 a->busy_##n##_ports_per_thread[thread_index]++;               \
314                 a->busy_##n##_ports++;                                        \
315                 *addr = a->addr;                                              \
316                 *port = clib_host_to_net_u16 (portnum);                       \
317                 return 0;                                                     \
318               }                                                               \
319           }                                                                   \
320         else if (a->fib_index == ~0)                                          \
321           {                                                                   \
322             ga = a;                                                           \
323           }                                                                   \
324       }                                                                       \
325     break;
326           foreach_nat_protocol
327 #undef _
328             default : nat_elog_info (sm, "unknown protocol");
329           return 1;
330         }
331     }
332
333   if (ga)
334     {
335       a = ga;
336       switch (proto)
337         {
338 #define _(N, j, n, s)                                                         \
339   case NAT_PROTOCOL_##N:                                                      \
340     while (1)                                                                 \
341       {                                                                       \
342         portnum = (port_per_thread * snat_thread_index) +                     \
343                   snat_random_port (0, port_per_thread - 1) + 1024;           \
344         if (a->busy_##n##_port_refcounts[portnum])                            \
345           continue;                                                           \
346         ++a->busy_##n##_port_refcounts[portnum];                              \
347         a->busy_##n##_ports_per_thread[thread_index]++;                       \
348         a->busy_##n##_ports++;                                                \
349         *addr = a->addr;                                                      \
350         *port = clib_host_to_net_u16 (portnum);                               \
351         return 0;                                                             \
352       }
353           break;
354           foreach_nat_protocol
355 #undef _
356             default : nat_elog_info (sm, "unknown protocol");
357           return 1;
358         }
359     }
360
361   /* Totally out of translations to use... */
362   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
363   return 1;
364 }
365
366 static snat_session_t *
367 create_session_for_static_mapping_ed (
368   snat_main_t *sm, vlib_buffer_t *b, ip4_address_t i2o_addr, u16 i2o_port,
369   u32 i2o_fib_index, ip4_address_t o2i_addr, u16 o2i_port, u32 o2i_fib_index,
370   nat_protocol_t nat_proto, vlib_node_runtime_t *node, u32 rx_fib_index,
371   u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now,
372   snat_static_mapping_t *mapping)
373 {
374   snat_session_t *s;
375   ip4_header_t *ip;
376   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
377
378   if (PREDICT_FALSE
379       (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
380     {
381       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
382       nat_elog_notice (sm, "maximum sessions exceeded");
383       return 0;
384     }
385
386   s = nat_ed_session_alloc (sm, thread_index, now, nat_proto);
387   if (!s)
388     {
389       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
390       nat_elog_warn (sm, "create NAT session failed");
391       return 0;
392     }
393
394   ip = vlib_buffer_get_current (b);
395
396   s->ext_host_addr.as_u32 = ip->src_address.as_u32;
397   s->ext_host_port =
398     nat_proto == NAT_PROTOCOL_ICMP ? 0 : vnet_buffer (b)->ip.reass.l4_src_port;
399   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
400   if (lb_nat)
401     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
402   if (lb_nat == AFFINITY_LB_NAT)
403     s->flags |= SNAT_SESSION_FLAG_AFFINITY;
404   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
405   s->out2in.addr = o2i_addr;
406   s->out2in.port = o2i_port;
407   s->out2in.fib_index = o2i_fib_index;
408   s->in2out.addr = i2o_addr;
409   s->in2out.port = i2o_port;
410   s->in2out.fib_index = i2o_fib_index;
411   s->nat_proto = nat_proto;
412
413   if (NAT_PROTOCOL_ICMP == nat_proto)
414     {
415       nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr, o2i_port,
416                             o2i_addr, o2i_port, o2i_fib_index, ip->protocol);
417       nat_6t_flow_icmp_id_rewrite_set (&s->o2i, i2o_port);
418     }
419   else
420     {
421       nat_6t_o2i_flow_init (sm, thread_index, s, s->ext_host_addr,
422                             s->ext_host_port, o2i_addr, o2i_port,
423                             o2i_fib_index, ip->protocol);
424       nat_6t_flow_dport_rewrite_set (&s->o2i, i2o_port);
425     }
426   nat_6t_flow_daddr_rewrite_set (&s->o2i, i2o_addr.as_u32);
427   nat_6t_flow_txfib_rewrite_set (&s->o2i, i2o_fib_index);
428
429   if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
430     {
431       b->error = node->errors[NAT_OUT2IN_ED_ERROR_HASH_ADD_FAILED];
432       nat_ed_session_delete (sm, s, thread_index, 1);
433       nat_elog_warn (sm, "out2in flow hash add failed");
434       return 0;
435     }
436
437   if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
438                                  ip->src_address.as_u32 == i2o_addr.as_u32))
439     {
440       int rc = 0;
441       snat_address_t *filter = 0;
442
443       // if exact address is specified use this address
444       if (is_exact_address (mapping))
445         {
446           snat_address_t *ap;
447           vec_foreach (ap, sm->twice_nat_addresses)
448           {
449             if (mapping->pool_addr.as_u32 == ap->addr.as_u32)
450               {
451                 filter = ap;
452                 break;
453               }
454           }
455         }
456
457       if (filter)
458         {
459           rc = nat_alloc_addr_and_port_exact (filter,
460                                               thread_index,
461                                               nat_proto,
462                                               &s->ext_host_nat_addr,
463                                               &s->ext_host_nat_port,
464                                               sm->port_per_thread,
465                                               tsm->snat_thread_index);
466           s->flags |= SNAT_SESSION_FLAG_EXACT_ADDRESS;
467         }
468       else
469         {
470           rc = nat44_ed_alloc_outside_addr_and_port (
471             sm->twice_nat_addresses, 0, thread_index, nat_proto,
472             &s->ext_host_nat_addr, &s->ext_host_nat_port, sm->port_per_thread,
473             tsm->snat_thread_index);
474         }
475
476       if (rc)
477         {
478           b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
479           if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
480             {
481               nat_elog_warn (sm, "out2in flow hash del failed");
482             }
483           snat_free_outside_address_and_port (
484             sm->twice_nat_addresses, thread_index, &s->ext_host_nat_addr,
485             s->ext_host_nat_port, s->nat_proto);
486           nat_ed_session_delete (sm, s, thread_index, 1);
487           return 0;
488         }
489
490       s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
491
492       nat_6t_flow_saddr_rewrite_set (&s->o2i, s->ext_host_nat_addr.as_u32);
493       if (NAT_PROTOCOL_ICMP == nat_proto)
494         {
495           nat_6t_flow_icmp_id_rewrite_set (&s->o2i, s->ext_host_nat_port);
496         }
497       else
498         {
499           nat_6t_flow_sport_rewrite_set (&s->o2i, s->ext_host_nat_port);
500         }
501
502       nat_6t_l3_l4_csum_calc (&s->o2i);
503
504       nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
505                             s->ext_host_nat_addr, s->ext_host_nat_port,
506                             i2o_fib_index, ip->protocol);
507       nat_6t_flow_daddr_rewrite_set (&s->i2o, s->ext_host_addr.as_u32);
508       if (NAT_PROTOCOL_ICMP == nat_proto)
509         {
510           nat_6t_flow_icmp_id_rewrite_set (&s->i2o, s->ext_host_port);
511         }
512       else
513         {
514           nat_6t_flow_dport_rewrite_set (&s->i2o, s->ext_host_port);
515         }
516     }
517   else
518     {
519       if (NAT_PROTOCOL_ICMP == nat_proto)
520         {
521           nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
522                                 s->ext_host_addr, i2o_port, i2o_fib_index,
523                                 ip->protocol);
524         }
525       else
526         {
527           nat_6t_i2o_flow_init (sm, thread_index, s, i2o_addr, i2o_port,
528                                 s->ext_host_addr, s->ext_host_port,
529                                 i2o_fib_index, ip->protocol);
530         }
531     }
532
533   nat_6t_flow_saddr_rewrite_set (&s->i2o, o2i_addr.as_u32);
534   if (NAT_PROTOCOL_ICMP == nat_proto)
535     {
536       nat_6t_flow_icmp_id_rewrite_set (&s->i2o, o2i_port);
537     }
538   else
539     {
540       nat_6t_flow_sport_rewrite_set (&s->i2o, o2i_port);
541     }
542
543   if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
544     {
545       nat_elog_notice (sm, "in2out flow hash add failed");
546       if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
547         {
548           nat_elog_warn (sm, "out2in flow hash del failed");
549         }
550       nat_ed_session_delete (sm, s, thread_index, 1);
551       return 0;
552     }
553
554   nat_ipfix_logging_nat44_ses_create (thread_index,
555                                       s->in2out.addr.as_u32,
556                                       s->out2in.addr.as_u32,
557                                       s->nat_proto,
558                                       s->in2out.port,
559                                       s->out2in.port, s->in2out.fib_index);
560
561   nat_syslog_nat44_sadd (0, s->in2out.fib_index, &s->in2out.addr,
562                          s->in2out.port, &s->ext_host_nat_addr,
563                          s->ext_host_nat_port, &s->out2in.addr, s->out2in.port,
564                          &s->ext_host_addr, s->ext_host_port, s->nat_proto,
565                          is_twice_nat_session (s));
566
567   per_vrf_sessions_register_session (s, thread_index);
568
569   return s;
570 }
571
572 static void
573 create_bypass_for_fwd (snat_main_t *sm, vlib_buffer_t *b, snat_session_t *s,
574                        ip4_header_t *ip, u32 rx_fib_index, u32 thread_index)
575 {
576   clib_bihash_kv_16_8_t kv, value;
577   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
578   vlib_main_t *vm = vlib_get_main ();
579   f64 now = vlib_time_now (vm);
580   u16 lookup_sport, lookup_dport;
581   u8 lookup_protocol;
582   ip4_address_t lookup_saddr, lookup_daddr;
583
584   if (ip->protocol == IP_PROTOCOL_ICMP)
585     {
586       if (nat_get_icmp_session_lookup_values (b, ip, &lookup_saddr,
587                                               &lookup_sport, &lookup_daddr,
588                                               &lookup_dport, &lookup_protocol))
589         return;
590     }
591   else
592     {
593       if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
594         {
595           lookup_sport = vnet_buffer (b)->ip.reass.l4_dst_port;
596           lookup_dport = vnet_buffer (b)->ip.reass.l4_src_port;
597         }
598       else
599         {
600           lookup_sport = 0;
601           lookup_dport = 0;
602         }
603       lookup_saddr.as_u32 = ip->dst_address.as_u32;
604       lookup_daddr.as_u32 = ip->src_address.as_u32;
605       lookup_protocol = ip->protocol;
606     }
607
608   init_ed_k (&kv, lookup_saddr, lookup_sport, lookup_daddr, lookup_dport,
609              rx_fib_index, lookup_protocol);
610
611   if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
612     {
613       ASSERT (thread_index == ed_value_get_thread_index (&value));
614       s =
615         pool_elt_at_index (tsm->sessions,
616                            ed_value_get_session_index (&value));
617     }
618   else if (ip->protocol == IP_PROTOCOL_ICMP &&
619            icmp_type_is_error_message
620            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
621     {
622       return;
623     }
624   else
625     {
626       u32 proto;
627
628       if (PREDICT_FALSE
629           (nat44_ed_maximum_sessions_exceeded
630            (sm, rx_fib_index, thread_index)))
631         return;
632
633       s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
634       if (!s)
635         {
636           nat_elog_warn (sm, "create NAT session failed");
637           return;
638         }
639
640       proto = ip_proto_to_nat_proto (ip->protocol);
641
642       s->ext_host_addr = ip->src_address;
643       s->ext_host_port = lookup_dport;
644       s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
645       s->out2in.addr = ip->dst_address;
646       s->out2in.port = lookup_sport;
647       s->nat_proto = proto;
648       if (proto == NAT_PROTOCOL_OTHER)
649         {
650           s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
651           s->out2in.port = ip->protocol;
652         }
653       s->out2in.fib_index = rx_fib_index;
654       s->in2out.addr = s->out2in.addr;
655       s->in2out.port = s->out2in.port;
656       s->in2out.fib_index = s->out2in.fib_index;
657
658       nat_6t_i2o_flow_init (sm, thread_index, s, ip->dst_address, lookup_sport,
659                             ip->src_address, lookup_dport, rx_fib_index,
660                             ip->protocol);
661       nat_6t_flow_txfib_rewrite_set (&s->i2o, rx_fib_index);
662       if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
663         {
664           nat_elog_notice (sm, "in2out flow add failed");
665           nat_ed_session_delete (sm, s, thread_index, 1);
666           return;
667         }
668
669       per_vrf_sessions_register_session (s, thread_index);
670     }
671
672   if (ip->protocol == IP_PROTOCOL_TCP)
673     {
674       tcp_header_t *tcp = ip4_next_header (ip);
675       nat44_set_tcp_session_state_o2i (sm, now, s, tcp->flags,
676                                        tcp->ack_number, tcp->seq_number,
677                                        thread_index);
678     }
679
680   /* Accounting */
681   nat44_session_update_counters (s, now, 0, thread_index);
682   /* Per-user LRU list maintenance */
683   nat44_session_update_lru (sm, s, thread_index);
684 }
685
686 static snat_session_t *
687 nat44_ed_out2in_slowpath_unknown_proto (snat_main_t *sm, vlib_buffer_t *b,
688                                         ip4_header_t *ip, u32 rx_fib_index,
689                                         u32 thread_index, f64 now,
690                                         vlib_main_t *vm,
691                                         vlib_node_runtime_t *node)
692 {
693   clib_bihash_kv_8_8_t kv, value;
694   snat_static_mapping_t *m;
695   snat_session_t *s;
696
697   if (PREDICT_FALSE (
698         nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
699     {
700       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
701       nat_elog_notice (sm, "maximum sessions exceeded");
702       return 0;
703     }
704
705   init_nat_k (&kv, ip->dst_address, 0, 0, 0);
706   if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
707     {
708       b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
709       return 0;
710     }
711
712   m = pool_elt_at_index (sm->static_mappings, value.value);
713
714   /* Create a new session */
715   s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
716   if (!s)
717     {
718       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
719       nat_elog_warn (sm, "create NAT session failed");
720       return 0;
721     }
722
723   s->ext_host_addr.as_u32 = ip->src_address.as_u32;
724   s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
725   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
726   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
727   s->out2in.addr.as_u32 = ip->dst_address.as_u32;
728   s->out2in.fib_index = rx_fib_index;
729   s->in2out.addr.as_u32 = m->local_addr.as_u32;
730   s->in2out.fib_index = m->fib_index;
731   s->in2out.port = s->out2in.port = ip->protocol;
732
733   nat_6t_o2i_flow_init (sm, thread_index, s, ip->dst_address, 0,
734                         ip->src_address, 0, m->fib_index, ip->protocol);
735   nat_6t_flow_saddr_rewrite_set (&s->i2o, ip->dst_address.as_u32);
736   if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 1))
737     {
738       nat_elog_notice (sm, "in2out key add failed");
739       nat_ed_session_delete (sm, s, thread_index, 1);
740       return NULL;
741     }
742
743   nat_6t_o2i_flow_init (sm, thread_index, s, ip->src_address, 0,
744                         ip->dst_address, 0, rx_fib_index, ip->protocol);
745   nat_6t_flow_daddr_rewrite_set (&s->o2i, m->local_addr.as_u32);
746   nat_6t_flow_txfib_rewrite_set (&s->o2i, m->fib_index);
747   if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 1))
748     {
749       nat_elog_notice (sm, "out2in flow hash add failed");
750       nat_ed_session_delete (sm, s, thread_index, 1);
751       return NULL;
752     }
753
754   per_vrf_sessions_register_session (s, thread_index);
755
756   /* Accounting */
757   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
758                                  thread_index);
759   /* Per-user LRU list maintenance */
760   nat44_session_update_lru (sm, s, thread_index);
761
762   return s;
763 }
764
765 static inline uword
766 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
767                                           vlib_node_runtime_t * node,
768                                           vlib_frame_t * frame,
769                                           int is_multi_worker)
770 {
771   u32 n_left_from, *from;
772   snat_main_t *sm = &snat_main;
773   f64 now = vlib_time_now (vm);
774   u32 thread_index = vm->thread_index;
775   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
776
777   from = vlib_frame_vector_args (frame);
778   n_left_from = frame->n_vectors;
779
780   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
781   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
782   vlib_get_buffers (vm, from, b, n_left_from);
783
784   while (n_left_from > 0)
785     {
786       vlib_buffer_t *b0;
787       u32 sw_if_index0, rx_fib_index0;
788       nat_protocol_t proto0;
789       ip4_header_t *ip0;
790       snat_session_t *s0 = 0;
791       clib_bihash_kv_16_8_t kv0, value0;
792       nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
793       nat_6t_flow_t *f = 0;
794       nat_6t_t lookup;
795       int lookup_skipped = 0;
796
797       b0 = *b;
798       b++;
799
800       /* Prefetch next iteration. */
801       if (PREDICT_TRUE (n_left_from >= 2))
802         {
803           vlib_buffer_t *p2;
804
805           p2 = *b;
806
807           vlib_prefetch_buffer_header (p2, LOAD);
808
809           CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
810         }
811
812       next[0] = vnet_buffer2 (b0)->nat.arc_next;
813
814       lookup.sport = vnet_buffer (b0)->ip.reass.l4_src_port;
815       lookup.dport = vnet_buffer (b0)->ip.reass.l4_dst_port;
816
817       vnet_buffer (b0)->snat.flags = 0;
818       ip0 = vlib_buffer_get_current (b0);
819
820       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
821       rx_fib_index0 =
822         fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
823
824       lookup.fib_index = rx_fib_index0;
825
826       if (PREDICT_FALSE (ip0->ttl == 1))
827         {
828           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
829           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
830                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
831                                        0);
832           next[0] = NAT_NEXT_ICMP_ERROR;
833           goto trace0;
834         }
835
836       proto0 = ip_proto_to_nat_proto (ip0->protocol);
837
838       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
839         {
840           if (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
841                 ICMP4_echo_request &&
842               vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
843                 ICMP4_echo_reply &&
844               !icmp_type_is_error_message (
845                 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
846             {
847               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
848               next[0] = NAT_NEXT_DROP;
849               goto trace0;
850             }
851           int err = nat_get_icmp_session_lookup_values (
852             b0, ip0, &lookup.saddr, &lookup.sport, &lookup.daddr,
853             &lookup.dport, &lookup.proto);
854           if (err != 0)
855             {
856               b0->error = node->errors[err];
857               next[0] = NAT_NEXT_DROP;
858               goto trace0;
859             }
860         }
861       else
862         {
863           lookup.saddr.as_u32 = ip0->src_address.as_u32;
864           lookup.daddr.as_u32 = ip0->dst_address.as_u32;
865           lookup.proto = ip0->protocol;
866         }
867
868       /* there might be a stashed index in vnet_buffer2 from handoff or
869        * classify node, see if it can be used */
870       if (is_multi_worker &&
871           !pool_is_free_index (tsm->sessions,
872                                vnet_buffer2 (b0)->nat.cached_session_index))
873         {
874           s0 = pool_elt_at_index (tsm->sessions,
875                                   vnet_buffer2 (b0)->nat.cached_session_index);
876           if (PREDICT_TRUE (nat_6t_t_eq (&s0->o2i.match, &lookup)) ||
877               (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
878                nat_6t_t_eq (&s0->i2o.match, &lookup)))
879             {
880               /* yes, this is the droid we're looking for */
881               lookup_skipped = 1;
882               goto skip_lookup;
883             }
884           s0 = NULL;
885         }
886
887       init_ed_k (&kv0, lookup.saddr, lookup.sport, lookup.daddr, lookup.dport,
888                  lookup.fib_index, lookup.proto);
889
890       // lookup flow
891       if (clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
892         {
893           // flow does not exist go slow path
894           next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
895           goto trace0;
896         }
897       ASSERT (thread_index == ed_value_get_thread_index (&value0));
898       s0 =
899         pool_elt_at_index (tsm->sessions,
900                            ed_value_get_session_index (&value0));
901     skip_lookup:
902
903       if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
904         {
905           // session is closed, go slow path
906           nat_free_session_data (sm, s0, thread_index, 0);
907           nat_ed_session_delete (sm, s0, thread_index, 1);
908           next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
909           goto trace0;
910         }
911
912       if (s0->tcp_closed_timestamp)
913         {
914           if (now >= s0->tcp_closed_timestamp)
915             {
916               // session is closed, go slow path, freed in slow path
917               next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
918             }
919           else
920             {
921               // session in transitory timeout, drop
922               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
923               next[0] = NAT_NEXT_DROP;
924             }
925           goto trace0;
926         }
927
928       // drop if session expired
929       u64 sess_timeout_time;
930       sess_timeout_time =
931         s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
932       if (now >= sess_timeout_time)
933         {
934           // session is closed, go slow path
935           nat_free_session_data (sm, s0, thread_index, 0);
936           nat_ed_session_delete (sm, s0, thread_index, 1);
937           next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
938           goto trace0;
939         }
940
941       if (nat_6t_t_eq (&s0->o2i.match, &lookup))
942         {
943           f = &s0->o2i;
944         }
945       else if (s0->flags & SNAT_SESSION_FLAG_TWICE_NAT &&
946                nat_6t_t_eq (&s0->i2o.match, &lookup))
947         {
948           f = &s0->i2o;
949         }
950       else
951         {
952           /*
953            * Send DHCP packets to the ipv4 stack, or we won't
954            * be able to use dhcp client on the outside interface
955            */
956           if (PREDICT_FALSE (
957                 proto0 == NAT_PROTOCOL_UDP &&
958                 (vnet_buffer (b0)->ip.reass.l4_dst_port ==
959                  clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client))))
960             {
961               goto trace0;
962             }
963
964           if (!sm->forwarding_enabled)
965             {
966               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
967               next[0] = NAT_NEXT_DROP;
968               goto trace0;
969             }
970           else
971             {
972               if (nat_6t_t_eq (&s0->i2o.match, &lookup))
973                 {
974                   f = &s0->i2o;
975                 }
976               else
977                 {
978                   // FIXME TODO bypass ???
979                   //  create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
980                   //                       thread_index);
981                   translation_error = NAT_ED_TRNSL_ERR_FLOW_MISMATCH;
982                   nat_free_session_data (sm, s0, thread_index, 0);
983                   nat_ed_session_delete (sm, s0, thread_index, 1);
984                   next[0] = NAT_NEXT_DROP;
985                   goto trace0;
986                 }
987             }
988         }
989
990       if (NAT_ED_TRNSL_ERR_SUCCESS !=
991           (translation_error = nat_6t_flow_buf_translate (
992              sm, b0, ip0, f, proto0, 0 /* is_output_feature */)))
993         {
994           next[0] = NAT_NEXT_DROP;
995           goto trace0;
996         }
997
998       switch (proto0)
999         {
1000         case NAT_PROTOCOL_TCP:
1001           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.tcp,
1002                                          thread_index, sw_if_index0, 1);
1003           nat44_set_tcp_session_state_o2i (sm, now, s0,
1004                                            vnet_buffer (b0)->ip.
1005                                            reass.icmp_type_or_tcp_flags,
1006                                            vnet_buffer (b0)->ip.
1007                                            reass.tcp_ack_number,
1008                                            vnet_buffer (b0)->ip.
1009                                            reass.tcp_seq_number,
1010                                            thread_index);
1011           break;
1012         case NAT_PROTOCOL_UDP:
1013           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.udp,
1014                                          thread_index, sw_if_index0, 1);
1015           break;
1016         case NAT_PROTOCOL_ICMP:
1017           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.icmp,
1018                                          thread_index, sw_if_index0, 1);
1019           break;
1020         case NAT_PROTOCOL_OTHER:
1021           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.other,
1022                                          thread_index, sw_if_index0, 1);
1023           break;
1024         }
1025
1026       /* Accounting */
1027       nat44_session_update_counters (s0, now,
1028                                      vlib_buffer_length_in_chain (vm, b0),
1029                                      thread_index);
1030       /* Per-user LRU list maintenance */
1031       nat44_session_update_lru (sm, s0, thread_index);
1032
1033     trace0:
1034       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1035                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1036         {
1037           nat44_ed_out2in_trace_t *t =
1038             vlib_add_trace (vm, node, b0, sizeof (*t));
1039           t->sw_if_index = sw_if_index0;
1040           t->next_index = next[0];
1041           t->is_slow_path = 0;
1042           t->translation_error = translation_error;
1043           clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1044           t->lookup_skipped = lookup_skipped;
1045
1046           if (s0)
1047             {
1048               t->session_index = s0 - tsm->sessions;
1049               clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1050               clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1051               t->translation_via_i2of = (&s0->i2o == f);
1052             }
1053           else
1054             {
1055               t->session_index = ~0;
1056             }
1057         }
1058
1059       if (next[0] == NAT_NEXT_DROP)
1060         {
1061           vlib_increment_simple_counter (&sm->counters.fastpath.out2in.drops,
1062                                          thread_index, sw_if_index0, 1);
1063         }
1064
1065       n_left_from--;
1066       next++;
1067     }
1068
1069   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1070                                frame->n_vectors);
1071   return frame->n_vectors;
1072 }
1073
1074 static inline uword
1075 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
1076                                           vlib_node_runtime_t * node,
1077                                           vlib_frame_t * frame)
1078 {
1079   u32 n_left_from, *from;
1080   snat_main_t *sm = &snat_main;
1081   f64 now = vlib_time_now (vm);
1082   u32 thread_index = vm->thread_index;
1083   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1084   snat_static_mapping_t *m;
1085
1086   from = vlib_frame_vector_args (frame);
1087   n_left_from = frame->n_vectors;
1088
1089   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
1090   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
1091   vlib_get_buffers (vm, from, b, n_left_from);
1092
1093   while (n_left_from > 0)
1094     {
1095       vlib_buffer_t *b0;
1096       u32 sw_if_index0, rx_fib_index0;
1097       nat_protocol_t proto0;
1098       ip4_header_t *ip0;
1099       udp_header_t *udp0;
1100       icmp46_header_t *icmp0;
1101       snat_session_t *s0 = 0;
1102       clib_bihash_kv_16_8_t kv0, value0;
1103       lb_nat_type_t lb_nat0;
1104       twice_nat_type_t twice_nat0;
1105       u8 identity_nat0;
1106       ip4_address_t sm_addr;
1107       u16 sm_port;
1108       u32 sm_fib_index;
1109       nat_translation_error_e translation_error = NAT_ED_TRNSL_ERR_SUCCESS;
1110
1111       b0 = *b;
1112       next[0] = vnet_buffer2 (b0)->nat.arc_next;
1113
1114       vnet_buffer (b0)->snat.flags = 0;
1115       ip0 = vlib_buffer_get_current (b0);
1116
1117       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1118       rx_fib_index0 =
1119         fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1120
1121       if (PREDICT_FALSE (ip0->ttl == 1))
1122         {
1123           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1124           icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1125                                        ICMP4_time_exceeded_ttl_exceeded_in_transit,
1126                                        0);
1127           next[0] = NAT_NEXT_ICMP_ERROR;
1128           goto trace0;
1129         }
1130
1131       udp0 = ip4_next_header (ip0);
1132       icmp0 = (icmp46_header_t *) udp0;
1133       proto0 = ip_proto_to_nat_proto (ip0->protocol);
1134
1135       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1136         {
1137           s0 = nat44_ed_out2in_slowpath_unknown_proto (
1138             sm, b0, ip0, rx_fib_index0, thread_index, now, vm, node);
1139           if (!sm->forwarding_enabled)
1140             {
1141               if (!s0)
1142                 next[0] = NAT_NEXT_DROP;
1143             }
1144           if (NAT_NEXT_DROP != next[0] &&
1145               NAT_ED_TRNSL_ERR_SUCCESS !=
1146                 (translation_error = nat_6t_flow_buf_translate (
1147                    sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1148             {
1149               goto trace0;
1150             }
1151
1152           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.other,
1153                                          thread_index, sw_if_index0, 1);
1154           goto trace0;
1155         }
1156
1157       if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1158         {
1159           next[0] = icmp_out2in_ed_slow_path
1160             (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1161              next[0], now, thread_index, &s0);
1162
1163           if (NAT_NEXT_DROP != next[0] && s0 &&
1164               NAT_ED_TRNSL_ERR_SUCCESS !=
1165                 (translation_error = nat_6t_flow_buf_translate (
1166                    sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1167             {
1168               goto trace0;
1169             }
1170
1171           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.icmp,
1172                                          thread_index, sw_if_index0, 1);
1173           goto trace0;
1174         }
1175
1176       init_ed_k (&kv0, ip0->src_address,
1177                  vnet_buffer (b0)->ip.reass.l4_src_port, ip0->dst_address,
1178                  vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1179                  ip0->protocol);
1180
1181       s0 = NULL;
1182       if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv0, &value0))
1183         {
1184           ASSERT (thread_index == ed_value_get_thread_index (&value0));
1185           s0 =
1186             pool_elt_at_index (tsm->sessions,
1187                                ed_value_get_session_index (&value0));
1188
1189           if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1190             {
1191               nat_free_session_data (sm, s0, thread_index, 0);
1192               nat_ed_session_delete (sm, s0, thread_index, 1);
1193               s0 = NULL;
1194             }
1195         }
1196
1197       if (!s0)
1198         {
1199           /* Try to match static mapping by external address and port,
1200              destination address and port in packet */
1201
1202           if (snat_static_mapping_match (
1203                 vm, sm, ip0->dst_address,
1204                 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0, proto0,
1205                 &sm_addr, &sm_port, &sm_fib_index, 1, 0, &twice_nat0, &lb_nat0,
1206                 &ip0->src_address, &identity_nat0, &m))
1207             {
1208               /*
1209                * Send DHCP packets to the ipv4 stack, or we won't
1210                * be able to use dhcp client on the outside interface
1211                */
1212               if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1213                                  && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1214                                      clib_host_to_net_u16
1215                                      (UDP_DST_PORT_dhcp_to_client))))
1216                 {
1217                   goto trace0;
1218                 }
1219
1220               if (!sm->forwarding_enabled)
1221                 {
1222                   b0->error =
1223                     node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1224                   next[0] = NAT_NEXT_DROP;
1225                 }
1226               else
1227                 {
1228                   if (next_src_nat
1229                       (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1230                        vnet_buffer (b0)->ip.reass.l4_dst_port,
1231                        thread_index, rx_fib_index0))
1232                     {
1233                       next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1234                     }
1235                   else
1236                     {
1237                       create_bypass_for_fwd (sm, b0, s0, ip0, rx_fib_index0,
1238                                              thread_index);
1239                     }
1240                 }
1241               goto trace0;
1242             }
1243
1244           if (PREDICT_FALSE (identity_nat0))
1245             goto trace0;
1246
1247           if ((proto0 == NAT_PROTOCOL_TCP)
1248               && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1249                                      reass.icmp_type_or_tcp_flags))
1250             {
1251               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1252               next[0] = NAT_NEXT_DROP;
1253               goto trace0;
1254             }
1255
1256           /* Create session initiated by host from external network */
1257           s0 = create_session_for_static_mapping_ed (sm, b0,
1258                                                      sm_addr, sm_port,
1259                                                      sm_fib_index,
1260                                                      ip0->dst_address,
1261                                                      vnet_buffer (b0)->
1262                                                      ip.reass.l4_dst_port,
1263                                                      rx_fib_index0, proto0,
1264                                                      node, rx_fib_index0,
1265                                                      thread_index, twice_nat0,
1266                                                      lb_nat0, now, m);
1267           if (!s0)
1268             {
1269               next[0] = NAT_NEXT_DROP;
1270               goto trace0;
1271             }
1272         }
1273
1274       if (NAT_ED_TRNSL_ERR_SUCCESS !=
1275           (translation_error = nat_6t_flow_buf_translate (
1276              sm, b0, ip0, &s0->o2i, proto0, 0 /* is_output_feature */)))
1277         {
1278           next[0] = NAT_NEXT_DROP;
1279           goto trace0;
1280         }
1281
1282       if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1283         {
1284           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.tcp,
1285                                          thread_index, sw_if_index0, 1);
1286           nat44_set_tcp_session_state_o2i (sm, now, s0,
1287                                            vnet_buffer (b0)->ip.
1288                                            reass.icmp_type_or_tcp_flags,
1289                                            vnet_buffer (b0)->ip.
1290                                            reass.tcp_ack_number,
1291                                            vnet_buffer (b0)->ip.
1292                                            reass.tcp_seq_number,
1293                                            thread_index);
1294         }
1295       else
1296         {
1297           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.udp,
1298                                          thread_index, sw_if_index0, 1);
1299         }
1300
1301       /* Accounting */
1302       nat44_session_update_counters (s0, now,
1303                                      vlib_buffer_length_in_chain (vm, b0),
1304                                      thread_index);
1305       /* Per-user LRU list maintenance */
1306       nat44_session_update_lru (sm, s0, thread_index);
1307
1308     trace0:
1309       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1310                          && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1311         {
1312           nat44_ed_out2in_trace_t *t =
1313             vlib_add_trace (vm, node, b0, sizeof (*t));
1314           t->sw_if_index = sw_if_index0;
1315           t->next_index = next[0];
1316           t->is_slow_path = 1;
1317           t->translation_error = translation_error;
1318           clib_memcpy (&t->search_key, &kv0, sizeof (t->search_key));
1319
1320           if (s0)
1321             {
1322               t->session_index = s0 - tsm->sessions;
1323               clib_memcpy (&t->i2of, &s0->i2o, sizeof (t->i2of));
1324               clib_memcpy (&t->o2if, &s0->o2i, sizeof (t->o2if));
1325             }
1326           else
1327             {
1328               t->session_index = ~0;
1329             }
1330         }
1331
1332       if (next[0] == NAT_NEXT_DROP)
1333         {
1334           vlib_increment_simple_counter (&sm->counters.slowpath.out2in.drops,
1335                                          thread_index, sw_if_index0, 1);
1336         }
1337
1338       n_left_from--;
1339       next++;
1340       b++;
1341     }
1342
1343   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1344                                frame->n_vectors);
1345
1346   return frame->n_vectors;
1347 }
1348
1349 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1350                                      vlib_node_runtime_t * node,
1351                                      vlib_frame_t * frame)
1352 {
1353   if (snat_main.num_workers > 1)
1354     {
1355       return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1356     }
1357   else
1358     {
1359       return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1360     }
1361 }
1362
1363 /* *INDENT-OFF* */
1364 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1365   .name = "nat44-ed-out2in",
1366   .vector_size = sizeof (u32),
1367   .sibling_of = "nat-default",
1368   .format_trace = format_nat44_ed_out2in_trace,
1369   .type = VLIB_NODE_TYPE_INTERNAL,
1370   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1371   .error_strings = nat_out2in_ed_error_strings,
1372   .runtime_data_bytes = sizeof (snat_runtime_t),
1373 };
1374 /* *INDENT-ON* */
1375
1376 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1377                                               vlib_node_runtime_t * node,
1378                                               vlib_frame_t * frame)
1379 {
1380   return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1381 }
1382
1383 /* *INDENT-OFF* */
1384 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1385   .name = "nat44-ed-out2in-slowpath",
1386   .vector_size = sizeof (u32),
1387   .sibling_of = "nat-default",
1388   .format_trace = format_nat44_ed_out2in_trace,
1389   .type = VLIB_NODE_TYPE_INTERNAL,
1390   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1391   .error_strings = nat_out2in_ed_error_strings,
1392   .runtime_data_bytes = sizeof (snat_runtime_t),
1393 };
1394 /* *INDENT-ON* */
1395
1396 static u8 *
1397 format_nat_pre_trace (u8 * s, va_list * args)
1398 {
1399   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1400   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1401   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1402   return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1403                  t->arc_next_index);
1404 }
1405
1406 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1407                                     vlib_node_runtime_t * node,
1408                                     vlib_frame_t * frame)
1409 {
1410   return nat_pre_node_fn_inline (vm, node, frame,
1411                                  NAT_NEXT_OUT2IN_ED_FAST_PATH);
1412 }
1413
1414 /* *INDENT-OFF* */
1415 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1416   .name = "nat-pre-out2in",
1417   .vector_size = sizeof (u32),
1418   .sibling_of = "nat-default",
1419   .format_trace = format_nat_pre_trace,
1420   .type = VLIB_NODE_TYPE_INTERNAL,
1421   .n_errors = 0,
1422  };
1423 /* *INDENT-ON* */
1424
1425 /*
1426  * fd.io coding-style-patch-verification: ON
1427  *
1428  * Local Variables:
1429  * eval: (c-set-style "gnu")
1430  * End:
1431  */