nat: more long read after short write optimization
[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   return s;
314 }
315
316 static int
317 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
318               u16 dst_port, u32 thread_index, u32 rx_fib_index)
319 {
320   clib_bihash_kv_16_8_t kv, value;
321   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
322
323   init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
324              rx_fib_index, ip->protocol);
325   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
326     return 1;
327
328   return 0;
329 }
330
331 static void
332 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
333                        u32 rx_fib_index, u32 thread_index)
334 {
335   clib_bihash_kv_16_8_t kv, value;
336   udp_header_t *udp;
337   snat_session_t *s = 0;
338   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
339   vlib_main_t *vm = vlib_get_main ();
340   f64 now = vlib_time_now (vm);
341   u16 l_port, r_port;
342
343   if (ip->protocol == IP_PROTOCOL_ICMP)
344     {
345       if (get_icmp_o2i_ed_key
346           (b, ip, rx_fib_index, ~0, ~0, 0, &l_port, &r_port, &kv))
347         return;
348     }
349   else
350     {
351       if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
352         {
353           udp = ip4_next_header (ip);
354           l_port = udp->dst_port;
355           r_port = udp->src_port;
356         }
357       else
358         {
359           l_port = 0;
360           r_port = 0;
361         }
362       init_ed_k (&kv, ip->dst_address, l_port, ip->src_address, r_port,
363                  rx_fib_index, ip->protocol);
364     }
365
366   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
367     {
368       ASSERT (thread_index == ed_value_get_thread_index (&value));
369       s =
370         pool_elt_at_index (tsm->sessions,
371                            ed_value_get_session_index (&value));
372     }
373   else
374     {
375       u32 proto;
376
377       if (PREDICT_FALSE
378           (nat44_ed_maximum_sessions_exceeded
379            (sm, rx_fib_index, thread_index)))
380         return;
381
382       s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
383       if (!s)
384         {
385           nat_elog_warn ("create NAT session failed");
386           return;
387         }
388
389       proto = ip_proto_to_nat_proto (ip->protocol);
390
391       s->ext_host_addr = ip->src_address;
392       s->ext_host_port = r_port;
393       s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
394       s->out2in.addr = ip->dst_address;
395       s->out2in.port = l_port;
396       s->nat_proto = proto;
397       if (proto == NAT_PROTOCOL_OTHER)
398         {
399           s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
400           s->out2in.port = ip->protocol;
401         }
402       s->out2in.fib_index = 0;
403       s->in2out.addr = s->out2in.addr;
404       s->in2out.port = s->out2in.port;
405       s->in2out.fib_index = s->out2in.fib_index;
406
407       kv.value = s - tsm->sessions;
408       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
409         nat_elog_notice ("in2out_ed key add failed");
410     }
411
412   if (ip->protocol == IP_PROTOCOL_TCP)
413     {
414       tcp_header_t *tcp = ip4_next_header (ip);
415       if (nat44_set_tcp_session_state_o2i
416           (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
417            thread_index))
418         return;
419     }
420
421   /* Accounting */
422   nat44_session_update_counters (s, now, 0, thread_index);
423   /* Per-user LRU list maintenance */
424   nat44_session_update_lru (sm, s, thread_index);
425 }
426
427 static inline void
428 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
429                               ip4_header_t * ip, u32 rx_fib_index)
430 {
431   ip4_header_t ip_wkr = {
432     .src_address = ip->dst_address,
433   };
434   u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
435
436   create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
437 }
438
439 #ifndef CLIB_MARCH_VARIANT
440 u32
441 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
442                       u32 thread_index, vlib_buffer_t * b,
443                       ip4_header_t * ip, ip4_address_t * addr,
444                       u16 * port, u32 * fib_index, nat_protocol_t * proto,
445                       void *d, void *e, u8 * dont_translate)
446 {
447   u32 next = ~0, sw_if_index, rx_fib_index;
448   clib_bihash_kv_16_8_t kv, value;
449   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
450   snat_session_t *s = 0;
451   u8 is_addr_only, identity_nat;
452   u16 l_port, r_port;
453   vlib_main_t *vm = vlib_get_main ();
454   ip4_address_t sm_addr;
455   u16 sm_port;
456   u32 sm_fib_index;
457   *dont_translate = 0;
458
459   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
460   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
461
462   if (get_icmp_o2i_ed_key
463       (b, ip, rx_fib_index, ~0, ~0, proto, &l_port, &r_port, &kv))
464     {
465       b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
466       next = NAT_NEXT_DROP;
467       goto out;
468     }
469
470   if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
471     {
472       /* Try to match static mapping */
473       if (snat_static_mapping_match
474           (sm, ip->dst_address, l_port, rx_fib_index,
475            ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
476            &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat))
477         {
478           if (!sm->forwarding_enabled)
479             {
480               /* Don't NAT packet aimed at the intfc address */
481               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
482                                                     ip->dst_address.as_u32)))
483                 {
484                   *dont_translate = 1;
485                   goto out;
486                 }
487               b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
488               next = NAT_NEXT_DROP;
489               goto out;
490             }
491           else
492             {
493               *dont_translate = 1;
494               if (next_src_nat (sm, ip, l_port, r_port,
495                                 thread_index, rx_fib_index))
496                 {
497                   next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
498                   goto out;
499                 }
500               if (sm->num_workers > 1)
501                 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
502               else
503                 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
504               goto out;
505             }
506         }
507
508       if (PREDICT_FALSE
509           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
510            ICMP4_echo_reply
511            && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
512                ICMP4_echo_request || !is_addr_only)))
513         {
514           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
515           next = NAT_NEXT_DROP;
516           goto out;
517         }
518
519       if (PREDICT_FALSE (identity_nat))
520         {
521           *dont_translate = 1;
522           goto out;
523         }
524
525       /* Create session initiated by host from external network */
526       s =
527         create_session_for_static_mapping_ed (sm, b, sm_addr, sm_port,
528                                               sm_fib_index, ip->dst_address,
529                                               l_port, rx_fib_index, *proto,
530                                               node, rx_fib_index,
531                                               thread_index, 0, 0,
532                                               vlib_time_now (vm));
533
534       if (!s)
535         {
536           next = NAT_NEXT_DROP;
537           goto out;
538         }
539     }
540   else
541     {
542       if (PREDICT_FALSE
543           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
544            ICMP4_echo_reply
545            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
546            ICMP4_echo_request
547            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
548                                            reass.icmp_type_or_tcp_flags)))
549         {
550           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
551           next = NAT_NEXT_DROP;
552           goto out;
553         }
554
555       ASSERT (thread_index == ed_value_get_thread_index (&value));
556       s =
557         pool_elt_at_index (tsm->sessions,
558                            ed_value_get_session_index (&value));
559     }
560 out:
561   if (s)
562     {
563       *addr = s->in2out.addr;
564       *port = s->in2out.port;
565       *fib_index = s->in2out.fib_index;
566     }
567   if (d)
568     *(snat_session_t **) d = s;
569   return next;
570 }
571 #endif
572
573 static snat_session_t *
574 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
575                                vlib_buffer_t * b,
576                                ip4_header_t * ip,
577                                u32 rx_fib_index,
578                                u32 thread_index,
579                                f64 now,
580                                vlib_main_t * vm, vlib_node_runtime_t * node)
581 {
582   clib_bihash_kv_8_8_t kv, value;
583   clib_bihash_kv_16_8_t s_kv, s_value;
584   snat_static_mapping_t *m;
585   u32 old_addr, new_addr;
586   ip_csum_t sum;
587   snat_session_t *s;
588   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
589
590   old_addr = ip->dst_address.as_u32;
591
592   init_ed_k (&s_kv, ip->dst_address, 0, ip->src_address, 0, rx_fib_index,
593              ip->protocol);
594
595   if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
596     {
597       ASSERT (thread_index == ed_value_get_thread_index (&s_value));
598       s =
599         pool_elt_at_index (tsm->sessions,
600                            ed_value_get_session_index (&s_value));
601       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
602     }
603   else
604     {
605       if (PREDICT_FALSE
606           (nat44_ed_maximum_sessions_exceeded
607            (sm, rx_fib_index, thread_index)))
608         {
609           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
610           nat_elog_notice ("maximum sessions exceeded");
611           return 0;
612         }
613
614       init_nat_k (&kv, ip->dst_address, 0, 0, 0);
615       if (clib_bihash_search_8_8
616           (&sm->static_mapping_by_external, &kv, &value))
617         {
618           b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
619           return 0;
620         }
621
622       m = pool_elt_at_index (sm->static_mappings, value.value);
623
624       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
625
626       /* Create a new session */
627       s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
628       if (!s)
629         {
630           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
631           nat_elog_warn ("create NAT session failed");
632           return 0;
633         }
634
635       s->ext_host_addr.as_u32 = ip->src_address.as_u32;
636       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
637       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
638       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
639       s->out2in.addr.as_u32 = old_addr;
640       s->out2in.fib_index = rx_fib_index;
641       s->in2out.addr.as_u32 = new_addr;
642       s->in2out.fib_index = m->fib_index;
643       s->in2out.port = s->out2in.port = ip->protocol;
644
645       /* Add to lookup tables */
646       s_kv.value = s - tsm->sessions;
647       if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
648         nat_elog_notice ("out2in key add failed");
649
650       init_ed_kv (&s_kv, ip->dst_address, 0, ip->src_address, 0, m->fib_index,
651                   ip->protocol, thread_index, s - tsm->sessions);
652       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
653         nat_elog_notice ("in2out key add failed");
654     }
655
656   /* Update IP checksum */
657   sum = ip->checksum;
658   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
659   ip->checksum = ip_csum_fold (sum);
660
661   vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
662
663   /* Accounting */
664   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
665                                  thread_index);
666   /* Per-user LRU list maintenance */
667   nat44_session_update_lru (sm, s, thread_index);
668
669   return s;
670 }
671
672 static inline uword
673 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
674                                           vlib_node_runtime_t * node,
675                                           vlib_frame_t * frame,
676                                           int is_multi_worker)
677 {
678   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
679   nat_next_t next_index;
680   snat_main_t *sm = &snat_main;
681   f64 now = vlib_time_now (vm);
682   u32 thread_index = vm->thread_index;
683   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
684   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
685     0, fragments = 0;
686
687   stats_node_index = sm->ed_out2in_node_index;
688
689   from = vlib_frame_vector_args (frame);
690   n_left_from = frame->n_vectors;
691   next_index = node->cached_next_index;
692
693   while (n_left_from > 0)
694     {
695       u32 n_left_to_next;
696
697       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
698
699       while (n_left_from > 0 && n_left_to_next > 0)
700         {
701           u32 bi0;
702           vlib_buffer_t *b0;
703           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
704             new_addr0;
705           u16 old_port0, new_port0;
706           ip4_header_t *ip0;
707           udp_header_t *udp0;
708           tcp_header_t *tcp0;
709           snat_session_t *s0 = 0;
710           clib_bihash_kv_16_8_t kv0, value0;
711           ip_csum_t sum0;
712
713           /* speculatively enqueue b0 to the current next frame */
714           bi0 = from[0];
715           to_next[0] = bi0;
716           from += 1;
717           to_next += 1;
718           n_left_from -= 1;
719           n_left_to_next -= 1;
720
721           b0 = vlib_get_buffer (vm, bi0);
722           next0 = vnet_buffer2 (b0)->nat.arc_next;
723
724           vnet_buffer (b0)->snat.flags = 0;
725           ip0 = vlib_buffer_get_current (b0);
726
727           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
728           rx_fib_index0 =
729             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
730                                                  sw_if_index0);
731
732           if (PREDICT_FALSE (ip0->ttl == 1))
733             {
734               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
735               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
736                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
737                                            0);
738               next0 = NAT_NEXT_ICMP_ERROR;
739               goto trace0;
740             }
741
742           udp0 = ip4_next_header (ip0);
743           tcp0 = (tcp_header_t *) udp0;
744           proto0 = ip_proto_to_nat_proto (ip0->protocol);
745
746           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
747             {
748               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
749               goto trace0;
750             }
751
752           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
753             {
754               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
755               goto trace0;
756             }
757
758           init_ed_k (&kv0, ip0->dst_address,
759                      vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
760                      vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
761                      ip0->protocol);
762
763           /* there is a stashed index in vnet_buffer2 from handoff node,
764            * see if we can use it */
765           if (is_multi_worker && PREDICT_TRUE
766               (!pool_is_free_index
767                (tsm->sessions,
768                 vnet_buffer2 (b0)->nat.ed_out2in_nat_session_index)))
769             {
770               s0 = pool_elt_at_index (tsm->sessions,
771                                       vnet_buffer2 (b0)->
772                                       nat.ed_out2in_nat_session_index);
773               if (PREDICT_TRUE
774                   (s0->out2in.addr.as_u32 == ip0->dst_address.as_u32
775                    && s0->out2in.port ==
776                    vnet_buffer (b0)->ip.reass.l4_dst_port
777                    && s0->nat_proto == ip_proto_to_nat_proto (ip0->protocol)
778                    && s0->out2in.fib_index == rx_fib_index0
779                    && s0->ext_host_addr.as_u32 == ip0->src_address.as_u32
780                    && s0->ext_host_port ==
781                    vnet_buffer (b0)->ip.reass.l4_src_port))
782                 {
783                   /* yes, this is the droid we're looking for */
784                   goto skip_lookup;
785                 }
786             }
787
788           if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
789             {
790               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
791               goto trace0;
792             }
793           ASSERT (thread_index == ed_value_get_thread_index (&value0));
794           s0 =
795             pool_elt_at_index (tsm->sessions,
796                                ed_value_get_session_index (&value0));
797
798         skip_lookup:
799           if (s0->tcp_closed_timestamp)
800             {
801               if (now >= s0->tcp_closed_timestamp)
802                 {
803                   // session is closed, go slow path
804                   next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
805                 }
806               else
807                 {
808                   // session in transitory timeout, drop
809                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
810                   next0 = NAT_NEXT_DROP;
811                 }
812               goto trace0;
813             }
814
815           // drop if session expired
816           u64 sess_timeout_time;
817           sess_timeout_time = s0->last_heard +
818             (f64) nat44_session_get_timeout (sm, s0);
819           if (now >= sess_timeout_time)
820             {
821               // session is closed, go slow path
822               nat_free_session_data (sm, s0, thread_index, 0);
823               nat_ed_session_delete (sm, s0, thread_index, 1);
824               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
825               goto trace0;
826             }
827           //
828
829           old_addr0 = ip0->dst_address.as_u32;
830           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
831           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
832
833           sum0 = ip0->checksum;
834           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
835                                  dst_address);
836           if (PREDICT_FALSE (is_twice_nat_session (s0)))
837             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
838                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
839                                    src_address);
840           ip0->checksum = ip_csum_fold (sum0);
841
842           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
843
844           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
845             {
846               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
847                 {
848                   new_port0 = udp0->dst_port = s0->in2out.port;
849                   sum0 = tcp0->checksum;
850                   sum0 =
851                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
852                                     dst_address);
853                   sum0 =
854                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
855                                     length);
856                   if (is_twice_nat_session (s0))
857                     {
858                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
859                                              s0->ext_host_nat_addr.as_u32,
860                                              ip4_header_t, dst_address);
861                       sum0 =
862                         ip_csum_update (sum0,
863                                         vnet_buffer (b0)->ip.
864                                         reass.l4_src_port,
865                                         s0->ext_host_nat_port, ip4_header_t,
866                                         length);
867                       tcp0->src_port = s0->ext_host_nat_port;
868                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
869                     }
870                   tcp0->checksum = ip_csum_fold (sum0);
871                 }
872               tcp_packets++;
873               if (nat44_set_tcp_session_state_o2i
874                   (sm, now, s0,
875                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
876                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
877                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
878                 goto trace0;
879             }
880           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
881                    && udp0->checksum)
882             {
883               new_port0 = udp0->dst_port = s0->in2out.port;
884               sum0 = udp0->checksum;
885               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
886                                      dst_address);
887               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
888                                      length);
889               if (PREDICT_FALSE (is_twice_nat_session (s0)))
890                 {
891                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
892                                          s0->ext_host_nat_addr.as_u32,
893                                          ip4_header_t, dst_address);
894                   sum0 =
895                     ip_csum_update (sum0,
896                                     vnet_buffer (b0)->ip.reass.l4_src_port,
897                                     s0->ext_host_nat_port, ip4_header_t,
898                                     length);
899                   udp0->src_port = s0->ext_host_nat_port;
900                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
901                 }
902               udp0->checksum = ip_csum_fold (sum0);
903               udp_packets++;
904             }
905           else
906             {
907               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
908                 {
909                   new_port0 = udp0->dst_port = s0->in2out.port;
910                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
911                     {
912                       udp0->src_port = s0->ext_host_nat_port;
913                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
914                     }
915                 }
916               udp_packets++;
917             }
918
919           /* Accounting */
920           nat44_session_update_counters (s0, now,
921                                          vlib_buffer_length_in_chain (vm, b0),
922                                          thread_index);
923           /* Per-user LRU list maintenance */
924           nat44_session_update_lru (sm, s0, thread_index);
925
926         trace0:
927           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
928                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
929             {
930               nat44_ed_out2in_trace_t *t =
931                 vlib_add_trace (vm, node, b0, sizeof (*t));
932               t->sw_if_index = sw_if_index0;
933               t->next_index = next0;
934               t->is_slow_path = 0;
935
936               if (s0)
937                 t->session_index = s0 - tsm->sessions;
938               else
939                 t->session_index = ~0;
940             }
941
942           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
943           /* verify speculative enqueue, maybe switch current next frame */
944           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
945                                            to_next, n_left_to_next,
946                                            bi0, next0);
947         }
948
949       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
950     }
951
952   vlib_node_increment_counter (vm, stats_node_index,
953                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
954                                pkts_processed);
955   vlib_node_increment_counter (vm, stats_node_index,
956                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
957   vlib_node_increment_counter (vm, stats_node_index,
958                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
959   vlib_node_increment_counter (vm, stats_node_index,
960                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
961                                icmp_packets);
962   vlib_node_increment_counter (vm, stats_node_index,
963                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
964                                other_packets);
965   vlib_node_increment_counter (vm, stats_node_index,
966                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
967   return frame->n_vectors;
968 }
969
970 static inline uword
971 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
972                                           vlib_node_runtime_t * node,
973                                           vlib_frame_t * frame)
974 {
975   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
976   nat_next_t next_index;
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   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
982     0, fragments = 0;
983
984   stats_node_index = sm->ed_out2in_slowpath_node_index;
985
986   from = vlib_frame_vector_args (frame);
987   n_left_from = frame->n_vectors;
988   next_index = node->cached_next_index;
989
990   while (n_left_from > 0)
991     {
992       u32 n_left_to_next;
993
994       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
995
996       while (n_left_from > 0 && n_left_to_next > 0)
997         {
998           u32 bi0;
999           vlib_buffer_t *b0;
1000           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1001             new_addr0;
1002           u16 old_port0, new_port0;
1003           ip4_header_t *ip0;
1004           udp_header_t *udp0;
1005           tcp_header_t *tcp0;
1006           icmp46_header_t *icmp0;
1007           snat_session_t *s0 = 0;
1008           clib_bihash_kv_16_8_t kv0, value0;
1009           ip_csum_t sum0;
1010           lb_nat_type_t lb_nat0;
1011           twice_nat_type_t twice_nat0;
1012           u8 identity_nat0;
1013           ip4_address_t sm_addr;
1014           u16 sm_port;
1015           u32 sm_fib_index;
1016
1017           /* speculatively enqueue b0 to the current next frame */
1018           bi0 = from[0];
1019           to_next[0] = bi0;
1020           from += 1;
1021           to_next += 1;
1022           n_left_from -= 1;
1023           n_left_to_next -= 1;
1024
1025           b0 = vlib_get_buffer (vm, bi0);
1026           next0 = vnet_buffer2 (b0)->nat.arc_next;
1027
1028           vnet_buffer (b0)->snat.flags = 0;
1029           ip0 = vlib_buffer_get_current (b0);
1030
1031           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1032           rx_fib_index0 =
1033             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1034                                                  sw_if_index0);
1035
1036           if (PREDICT_FALSE (ip0->ttl == 1))
1037             {
1038               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1039               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1040                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1041                                            0);
1042               next0 = NAT_NEXT_ICMP_ERROR;
1043               goto trace0;
1044             }
1045
1046           udp0 = ip4_next_header (ip0);
1047           tcp0 = (tcp_header_t *) udp0;
1048           icmp0 = (icmp46_header_t *) udp0;
1049           proto0 = ip_proto_to_nat_proto (ip0->protocol);
1050
1051           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1052             {
1053               s0 =
1054                 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1055                                                thread_index, now, vm, node);
1056               if (!sm->forwarding_enabled)
1057                 {
1058                   if (!s0)
1059                     next0 = NAT_NEXT_DROP;
1060                 }
1061               other_packets++;
1062               goto trace0;
1063             }
1064
1065           if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1066             {
1067               next0 = icmp_out2in_ed_slow_path
1068                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1069                  next0, now, thread_index, &s0);
1070               icmp_packets++;
1071               goto trace0;
1072             }
1073
1074           init_ed_k (&kv0, ip0->dst_address,
1075                      vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
1076                      vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1077                      ip0->protocol);
1078
1079           s0 = NULL;
1080           if (!clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
1081             {
1082               ASSERT (thread_index == ed_value_get_thread_index (&value0));
1083               s0 =
1084                 pool_elt_at_index (tsm->sessions,
1085                                    ed_value_get_session_index (&value0));
1086
1087               if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1088                 {
1089                   nat_free_session_data (sm, s0, thread_index, 0);
1090                   nat_ed_session_delete (sm, s0, thread_index, 1);
1091                   s0 = NULL;
1092                 }
1093             }
1094
1095           if (!s0)
1096             {
1097               /* Try to match static mapping by external address and port,
1098                  destination address and port in packet */
1099
1100               if (snat_static_mapping_match
1101                   (sm, ip0->dst_address,
1102                    vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1103                    proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1104                    &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0))
1105                 {
1106                   /*
1107                    * Send DHCP packets to the ipv4 stack, or we won't
1108                    * be able to use dhcp client on the outside interface
1109                    */
1110                   if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1111                                      && (vnet_buffer (b0)->ip.
1112                                          reass.l4_dst_port ==
1113                                          clib_host_to_net_u16
1114                                          (UDP_DST_PORT_dhcp_to_client))))
1115                     {
1116                       goto trace0;
1117                     }
1118
1119                   if (!sm->forwarding_enabled)
1120                     {
1121                       b0->error =
1122                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1123                       next0 = NAT_NEXT_DROP;
1124                     }
1125                   else
1126                     {
1127                       if (next_src_nat
1128                           (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1129                            vnet_buffer (b0)->ip.reass.l4_dst_port,
1130                            thread_index, rx_fib_index0))
1131                         {
1132                           next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1133                           goto trace0;
1134                         }
1135                       if (sm->num_workers > 1)
1136                         create_bypass_for_fwd_worker (sm, b0, ip0,
1137                                                       rx_fib_index0);
1138                       else
1139                         create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1140                                                thread_index);
1141                     }
1142                   goto trace0;
1143                 }
1144
1145               if (PREDICT_FALSE (identity_nat0))
1146                 goto trace0;
1147
1148               if ((proto0 == NAT_PROTOCOL_TCP)
1149                   && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1150                                          reass.icmp_type_or_tcp_flags))
1151                 {
1152                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1153                   next0 = NAT_NEXT_DROP;
1154                   goto trace0;
1155                 }
1156
1157               /* Create session initiated by host from external network */
1158               s0 = create_session_for_static_mapping_ed (sm, b0,
1159                                                          sm_addr, sm_port,
1160                                                          sm_fib_index,
1161                                                          ip0->dst_address,
1162                                                          vnet_buffer (b0)->
1163                                                          ip.reass.l4_dst_port,
1164                                                          rx_fib_index0,
1165                                                          proto0, node,
1166                                                          rx_fib_index0,
1167                                                          thread_index,
1168                                                          twice_nat0, lb_nat0,
1169                                                          now);
1170               if (!s0)
1171                 {
1172                   next0 = NAT_NEXT_DROP;
1173                   goto trace0;
1174                 }
1175             }
1176
1177           old_addr0 = ip0->dst_address.as_u32;
1178           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1179           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1180
1181           sum0 = ip0->checksum;
1182           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1183                                  dst_address);
1184           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1185             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1186                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1187                                    src_address);
1188           ip0->checksum = ip_csum_fold (sum0);
1189
1190           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1191
1192           if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1193             {
1194               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1195                 {
1196                   new_port0 = udp0->dst_port = s0->in2out.port;
1197                   sum0 = tcp0->checksum;
1198                   sum0 =
1199                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1200                                     dst_address);
1201                   sum0 =
1202                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1203                                     length);
1204                   if (is_twice_nat_session (s0))
1205                     {
1206                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1207                                              s0->ext_host_nat_addr.as_u32,
1208                                              ip4_header_t, dst_address);
1209                       sum0 =
1210                         ip_csum_update (sum0,
1211                                         vnet_buffer (b0)->ip.
1212                                         reass.l4_src_port,
1213                                         s0->ext_host_nat_port, ip4_header_t,
1214                                         length);
1215                       tcp0->src_port = s0->ext_host_nat_port;
1216                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1217                     }
1218                   tcp0->checksum = ip_csum_fold (sum0);
1219                 }
1220               tcp_packets++;
1221               if (nat44_set_tcp_session_state_o2i
1222                   (sm, now, s0,
1223                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1224                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
1225                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1226                 goto trace0;
1227             }
1228           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1229                    && udp0->checksum)
1230             {
1231               new_port0 = udp0->dst_port = s0->in2out.port;
1232               sum0 = udp0->checksum;
1233               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1234                                      dst_address);
1235               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1236                                      length);
1237               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1238                 {
1239                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1240                                          s0->ext_host_nat_addr.as_u32,
1241                                          ip4_header_t, dst_address);
1242                   sum0 =
1243                     ip_csum_update (sum0,
1244                                     vnet_buffer (b0)->ip.reass.l4_src_port,
1245                                     s0->ext_host_nat_port, ip4_header_t,
1246                                     length);
1247                   udp0->src_port = s0->ext_host_nat_port;
1248                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1249                 }
1250               udp0->checksum = ip_csum_fold (sum0);
1251               udp_packets++;
1252             }
1253           else
1254             {
1255               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1256                 {
1257                   new_port0 = udp0->dst_port = s0->in2out.port;
1258                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1259                     {
1260                       udp0->src_port = s0->ext_host_nat_port;
1261                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1262                     }
1263                 }
1264               udp_packets++;
1265             }
1266
1267           /* Accounting */
1268           nat44_session_update_counters (s0, now,
1269                                          vlib_buffer_length_in_chain (vm, b0),
1270                                          thread_index);
1271           /* Per-user LRU list maintenance */
1272           nat44_session_update_lru (sm, s0, thread_index);
1273
1274         trace0:
1275           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1276                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1277             {
1278               nat44_ed_out2in_trace_t *t =
1279                 vlib_add_trace (vm, node, b0, sizeof (*t));
1280               t->sw_if_index = sw_if_index0;
1281               t->next_index = next0;
1282               t->is_slow_path = 1;
1283
1284               if (s0)
1285                 t->session_index = s0 - tsm->sessions;
1286               else
1287                 t->session_index = ~0;
1288             }
1289
1290           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1291           /* verify speculative enqueue, maybe switch current next frame */
1292           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1293                                            to_next, n_left_to_next,
1294                                            bi0, next0);
1295         }
1296
1297       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1298     }
1299
1300   vlib_node_increment_counter (vm, stats_node_index,
1301                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1302                                pkts_processed);
1303   vlib_node_increment_counter (vm, stats_node_index,
1304                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1305   vlib_node_increment_counter (vm, stats_node_index,
1306                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1307   vlib_node_increment_counter (vm, stats_node_index,
1308                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1309                                icmp_packets);
1310   vlib_node_increment_counter (vm, stats_node_index,
1311                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1312                                other_packets);
1313   vlib_node_increment_counter (vm, stats_node_index,
1314                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1315   return frame->n_vectors;
1316 }
1317
1318 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1319                                      vlib_node_runtime_t * node,
1320                                      vlib_frame_t * frame)
1321 {
1322   if (snat_main.num_workers > 1)
1323     {
1324       return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1325     }
1326   else
1327     {
1328       return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1329     }
1330 }
1331
1332 /* *INDENT-OFF* */
1333 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1334   .name = "nat44-ed-out2in",
1335   .vector_size = sizeof (u32),
1336   .sibling_of = "nat-default",
1337   .format_trace = format_nat44_ed_out2in_trace,
1338   .type = VLIB_NODE_TYPE_INTERNAL,
1339   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1340   .error_strings = nat_out2in_ed_error_strings,
1341   .runtime_data_bytes = sizeof (snat_runtime_t),
1342 };
1343 /* *INDENT-ON* */
1344
1345 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1346                                               vlib_node_runtime_t * node,
1347                                               vlib_frame_t * frame)
1348 {
1349   return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1350 }
1351
1352 /* *INDENT-OFF* */
1353 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1354   .name = "nat44-ed-out2in-slowpath",
1355   .vector_size = sizeof (u32),
1356   .sibling_of = "nat-default",
1357   .format_trace = format_nat44_ed_out2in_trace,
1358   .type = VLIB_NODE_TYPE_INTERNAL,
1359   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1360   .error_strings = nat_out2in_ed_error_strings,
1361   .runtime_data_bytes = sizeof (snat_runtime_t),
1362 };
1363 /* *INDENT-ON* */
1364
1365 static u8 *
1366 format_nat_pre_trace (u8 * s, va_list * args)
1367 {
1368   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1369   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1370   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1371   return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1372                  t->arc_next_index);
1373 }
1374
1375 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1376                                     vlib_node_runtime_t * node,
1377                                     vlib_frame_t * frame)
1378 {
1379   return nat_pre_node_fn_inline (vm, node, frame,
1380                                  NAT_NEXT_OUT2IN_ED_FAST_PATH);
1381 }
1382
1383 /* *INDENT-OFF* */
1384 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1385   .name = "nat-pre-out2in",
1386   .vector_size = sizeof (u32),
1387   .sibling_of = "nat-default",
1388   .format_trace = format_nat_pre_trace,
1389   .type = VLIB_NODE_TYPE_INTERNAL,
1390   .n_errors = 0,
1391  };
1392 /* *INDENT-ON* */
1393
1394 /*
1395  * fd.io coding-style-patch-verification: ON
1396  *
1397  * Local Variables:
1398  * eval: (c-set-style "gnu")
1399  * End:
1400  */