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