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