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