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