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