e1eda3208e32eb774babeaeebb2fca5de95d8591
[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   u32 tcp_closed_drops = 0;
697
698   stats_node_index = sm->ed_out2in_node_index;
699
700   from = vlib_frame_vector_args (frame);
701   n_left_from = frame->n_vectors;
702   next_index = node->cached_next_index;
703
704   while (n_left_from > 0)
705     {
706       u32 n_left_to_next;
707
708       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
709
710       while (n_left_from > 0 && n_left_to_next > 0)
711         {
712           u32 bi0;
713           vlib_buffer_t *b0;
714           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
715             new_addr0;
716           u16 old_port0, new_port0;
717           ip4_header_t *ip0;
718           udp_header_t *udp0;
719           tcp_header_t *tcp0;
720           snat_session_t *s0 = 0;
721           clib_bihash_kv_16_8_t kv0, value0;
722           ip_csum_t sum0;
723
724           /* speculatively enqueue b0 to the current next frame */
725           bi0 = from[0];
726           to_next[0] = bi0;
727           from += 1;
728           to_next += 1;
729           n_left_from -= 1;
730           n_left_to_next -= 1;
731
732           b0 = vlib_get_buffer (vm, bi0);
733           next0 = nat_buffer_opaque (b0)->arc_next;
734
735           vnet_buffer (b0)->snat.flags = 0;
736           ip0 = vlib_buffer_get_current (b0);
737
738           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
739           rx_fib_index0 =
740             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
741                                                  sw_if_index0);
742
743           if (PREDICT_FALSE (ip0->ttl == 1))
744             {
745               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
746               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
747                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
748                                            0);
749               next0 = NAT_NEXT_ICMP_ERROR;
750               goto trace0;
751             }
752
753           udp0 = ip4_next_header (ip0);
754           tcp0 = (tcp_header_t *) udp0;
755           proto0 = ip_proto_to_snat_proto (ip0->protocol);
756
757           if (PREDICT_FALSE (proto0 == ~0))
758             {
759               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
760               goto trace0;
761             }
762
763           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
764             {
765               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
766               goto trace0;
767             }
768
769           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
770                       ip0->protocol, rx_fib_index0,
771                       vnet_buffer (b0)->ip.reass.l4_dst_port,
772                       vnet_buffer (b0)->ip.reass.l4_src_port);
773
774           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
775             {
776               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
777               goto trace0;
778             }
779           s0 = pool_elt_at_index (tsm->sessions, value0.value);
780
781           if (s0->tcp_close_timestamp)
782             {
783               if (now >= s0->tcp_close_timestamp)
784                 {
785                   // session is closed, go slow path
786                   next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
787                 }
788               else
789                 {
790                   // session in transitory timeout, drop
791                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
792                   ++tcp_closed_drops;
793                   next0 = NAT_NEXT_DROP;
794                 }
795               goto trace0;
796             }
797
798           // drop if session expired
799           u64 sess_timeout_time;
800           sess_timeout_time = s0->last_heard +
801             (f64) nat44_session_get_timeout (sm, s0);
802           if (now >= sess_timeout_time)
803             {
804               // delete session
805               nat_free_session_data (sm, s0, thread_index, 0);
806               nat44_delete_session (sm, s0, thread_index);
807
808               // session no longer exists, go slow path
809               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
810               goto trace0;
811             }
812           //
813
814           old_addr0 = ip0->dst_address.as_u32;
815           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
816           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
817
818           sum0 = ip0->checksum;
819           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
820                                  dst_address);
821           if (PREDICT_FALSE (is_twice_nat_session (s0)))
822             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
823                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
824                                    src_address);
825           ip0->checksum = ip_csum_fold (sum0);
826
827           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
828
829           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
830             {
831               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
832                 {
833                   new_port0 = udp0->dst_port = s0->in2out.port;
834                   sum0 = tcp0->checksum;
835                   sum0 =
836                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
837                                     dst_address);
838                   sum0 =
839                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
840                                     length);
841                   if (is_twice_nat_session (s0))
842                     {
843                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
844                                              s0->ext_host_nat_addr.as_u32,
845                                              ip4_header_t, dst_address);
846                       sum0 =
847                         ip_csum_update (sum0,
848                                         vnet_buffer (b0)->ip.
849                                         reass.l4_src_port,
850                                         s0->ext_host_nat_port, ip4_header_t,
851                                         length);
852                       tcp0->src_port = s0->ext_host_nat_port;
853                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
854                     }
855                   tcp0->checksum = ip_csum_fold (sum0);
856                 }
857               tcp_packets++;
858               if (nat44_set_tcp_session_state_o2i
859                   (sm, now, s0,
860                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
861                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
862                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
863                 goto trace0;
864             }
865           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
866                    && udp0->checksum)
867             {
868               new_port0 = udp0->dst_port = s0->in2out.port;
869               sum0 = udp0->checksum;
870               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
871                                      dst_address);
872               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
873                                      length);
874               if (PREDICT_FALSE (is_twice_nat_session (s0)))
875                 {
876                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
877                                          s0->ext_host_nat_addr.as_u32,
878                                          ip4_header_t, dst_address);
879                   sum0 =
880                     ip_csum_update (sum0,
881                                     vnet_buffer (b0)->ip.reass.l4_src_port,
882                                     s0->ext_host_nat_port, ip4_header_t,
883                                     length);
884                   udp0->src_port = s0->ext_host_nat_port;
885                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
886                 }
887               udp0->checksum = ip_csum_fold (sum0);
888               udp_packets++;
889             }
890           else
891             {
892               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
893                 {
894                   new_port0 = udp0->dst_port = s0->in2out.port;
895                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
896                     {
897                       udp0->src_port = s0->ext_host_nat_port;
898                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
899                     }
900                 }
901               udp_packets++;
902             }
903
904           /* Accounting */
905           nat44_session_update_counters (s0, now,
906                                          vlib_buffer_length_in_chain (vm, b0),
907                                          thread_index);
908           /* Per-user LRU list maintenance */
909           nat44_session_update_lru (sm, s0, thread_index);
910
911         trace0:
912           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
913                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
914             {
915               nat44_ed_out2in_trace_t *t =
916                 vlib_add_trace (vm, node, b0, sizeof (*t));
917               t->sw_if_index = sw_if_index0;
918               t->next_index = next0;
919               t->is_slow_path = 0;
920
921               if (s0)
922                 t->session_index = s0 - tsm->sessions;
923               else
924                 t->session_index = ~0;
925             }
926
927           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
928           /* verify speculative enqueue, maybe switch current next frame */
929           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
930                                            to_next, n_left_to_next,
931                                            bi0, next0);
932         }
933
934       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
935     }
936
937   vlib_node_increment_counter (vm, stats_node_index,
938                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
939                                pkts_processed);
940   vlib_node_increment_counter (vm, stats_node_index,
941                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
942   vlib_node_increment_counter (vm, stats_node_index,
943                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
944   vlib_node_increment_counter (vm, stats_node_index,
945                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
946                                icmp_packets);
947   vlib_node_increment_counter (vm, stats_node_index,
948                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
949                                other_packets);
950   vlib_node_increment_counter (vm, stats_node_index,
951                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
952   return frame->n_vectors;
953 }
954
955 static inline uword
956 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
957                                           vlib_node_runtime_t * node,
958                                           vlib_frame_t * frame)
959 {
960   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
961   nat_next_t next_index;
962   snat_main_t *sm = &snat_main;
963   f64 now = vlib_time_now (vm);
964   u32 thread_index = vm->thread_index;
965   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
966   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
967     0, fragments = 0;
968
969   stats_node_index = sm->ed_out2in_slowpath_node_index;
970
971   from = vlib_frame_vector_args (frame);
972   n_left_from = frame->n_vectors;
973   next_index = node->cached_next_index;
974
975   while (n_left_from > 0)
976     {
977       u32 n_left_to_next;
978
979       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
980
981       while (n_left_from > 0 && n_left_to_next > 0)
982         {
983           u32 bi0;
984           vlib_buffer_t *b0;
985           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
986             new_addr0;
987           u16 old_port0, new_port0;
988           ip4_header_t *ip0;
989           udp_header_t *udp0;
990           tcp_header_t *tcp0;
991           icmp46_header_t *icmp0;
992           snat_session_t *s0 = 0;
993           clib_bihash_kv_16_8_t kv0, value0;
994           ip_csum_t sum0;
995           snat_session_key_t e_key0, l_key0;
996           lb_nat_type_t lb_nat0;
997           twice_nat_type_t twice_nat0;
998           u8 identity_nat0;
999
1000           /* speculatively enqueue b0 to the current next frame */
1001           bi0 = from[0];
1002           to_next[0] = bi0;
1003           from += 1;
1004           to_next += 1;
1005           n_left_from -= 1;
1006           n_left_to_next -= 1;
1007
1008           b0 = vlib_get_buffer (vm, bi0);
1009           next0 = nat_buffer_opaque (b0)->arc_next;
1010
1011           vnet_buffer (b0)->snat.flags = 0;
1012           ip0 = vlib_buffer_get_current (b0);
1013
1014           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1015           rx_fib_index0 =
1016             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1017                                                  sw_if_index0);
1018
1019           if (PREDICT_FALSE (ip0->ttl == 1))
1020             {
1021               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1022               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1023                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1024                                            0);
1025               next0 = NAT_NEXT_ICMP_ERROR;
1026               goto trace0;
1027             }
1028
1029           udp0 = ip4_next_header (ip0);
1030           tcp0 = (tcp_header_t *) udp0;
1031           icmp0 = (icmp46_header_t *) udp0;
1032           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1033
1034           if (PREDICT_FALSE (proto0 == ~0))
1035             {
1036               s0 =
1037                 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1038                                                thread_index, now, vm, node);
1039               if (!sm->forwarding_enabled)
1040                 {
1041                   if (!s0)
1042                     next0 = NAT_NEXT_DROP;
1043                 }
1044               other_packets++;
1045               goto trace0;
1046             }
1047
1048           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1049             {
1050               next0 = icmp_out2in_ed_slow_path
1051                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1052                  next0, now, thread_index, &s0);
1053               icmp_packets++;
1054               goto trace0;
1055             }
1056
1057           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1058                       ip0->protocol, rx_fib_index0,
1059                       vnet_buffer (b0)->ip.reass.l4_dst_port,
1060                       vnet_buffer (b0)->ip.reass.l4_src_port);
1061
1062           s0 = NULL;
1063           if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1064             {
1065               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1066
1067               if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1068                 {
1069                   nat_free_session_data (sm, s0, thread_index, 0);
1070                   nat44_delete_session (sm, s0, thread_index);
1071                   s0 = NULL;
1072                 }
1073             }
1074
1075           if (!s0)
1076             {
1077               /* Try to match static mapping by external address and port,
1078                  destination address and port in packet */
1079               e_key0.addr = ip0->dst_address;
1080               e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1081               e_key0.protocol = proto0;
1082               e_key0.fib_index = rx_fib_index0;
1083
1084               if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1085                                              &twice_nat0, &lb_nat0,
1086                                              &ip0->src_address,
1087                                              &identity_nat0))
1088                 {
1089                   /*
1090                    * Send DHCP packets to the ipv4 stack, or we won't
1091                    * be able to use dhcp client on the outside interface
1092                    */
1093                   if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1094                                      && (vnet_buffer (b0)->ip.
1095                                          reass.l4_dst_port ==
1096                                          clib_host_to_net_u16
1097                                          (UDP_DST_PORT_dhcp_to_client))))
1098                     {
1099                       goto trace0;
1100                     }
1101
1102                   if (!sm->forwarding_enabled)
1103                     {
1104                       b0->error =
1105                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1106                       next0 = NAT_NEXT_DROP;
1107                     }
1108                   else
1109                     {
1110                       if (next_src_nat (sm, ip0, ip0->protocol,
1111                                         vnet_buffer (b0)->ip.
1112                                         reass.l4_src_port,
1113                                         vnet_buffer (b0)->ip.
1114                                         reass.l4_dst_port, thread_index,
1115                                         rx_fib_index0))
1116                         {
1117                           next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1118                           goto trace0;
1119                         }
1120                       if (sm->num_workers > 1)
1121                         create_bypass_for_fwd_worker (sm, b0, ip0,
1122                                                       rx_fib_index0);
1123                       else
1124                         create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1125                                                thread_index);
1126                     }
1127                   goto trace0;
1128                 }
1129
1130               if (PREDICT_FALSE (identity_nat0))
1131                 goto trace0;
1132
1133               if ((proto0 == SNAT_PROTOCOL_TCP)
1134                   && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1135                                          reass.icmp_type_or_tcp_flags))
1136                 {
1137                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1138                   next0 = NAT_NEXT_DROP;
1139                   goto trace0;
1140                 }
1141
1142               /* Create session initiated by host from external network */
1143               s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1144                                                          e_key0, node,
1145                                                          thread_index,
1146                                                          twice_nat0,
1147                                                          lb_nat0, now);
1148               if (!s0)
1149                 {
1150                   next0 = NAT_NEXT_DROP;
1151                   goto trace0;
1152                 }
1153             }
1154
1155           old_addr0 = ip0->dst_address.as_u32;
1156           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1157           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1158
1159           sum0 = ip0->checksum;
1160           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1161                                  dst_address);
1162           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1163             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1164                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1165                                    src_address);
1166           ip0->checksum = ip_csum_fold (sum0);
1167
1168           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1169
1170           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1171             {
1172               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1173                 {
1174                   new_port0 = udp0->dst_port = s0->in2out.port;
1175                   sum0 = tcp0->checksum;
1176                   sum0 =
1177                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1178                                     dst_address);
1179                   sum0 =
1180                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1181                                     length);
1182                   if (is_twice_nat_session (s0))
1183                     {
1184                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1185                                              s0->ext_host_nat_addr.as_u32,
1186                                              ip4_header_t, dst_address);
1187                       sum0 =
1188                         ip_csum_update (sum0,
1189                                         vnet_buffer (b0)->ip.
1190                                         reass.l4_src_port,
1191                                         s0->ext_host_nat_port, ip4_header_t,
1192                                         length);
1193                       tcp0->src_port = s0->ext_host_nat_port;
1194                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1195                     }
1196                   tcp0->checksum = ip_csum_fold (sum0);
1197                 }
1198               tcp_packets++;
1199               if (nat44_set_tcp_session_state_o2i
1200                   (sm, now, s0,
1201                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1202                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
1203                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1204                 goto trace0;
1205             }
1206           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1207                    && udp0->checksum)
1208             {
1209               new_port0 = udp0->dst_port = s0->in2out.port;
1210               sum0 = udp0->checksum;
1211               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1212                                      dst_address);
1213               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1214                                      length);
1215               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1216                 {
1217                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1218                                          s0->ext_host_nat_addr.as_u32,
1219                                          ip4_header_t, dst_address);
1220                   sum0 =
1221                     ip_csum_update (sum0,
1222                                     vnet_buffer (b0)->ip.reass.l4_src_port,
1223                                     s0->ext_host_nat_port, ip4_header_t,
1224                                     length);
1225                   udp0->src_port = s0->ext_host_nat_port;
1226                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1227                 }
1228               udp0->checksum = ip_csum_fold (sum0);
1229               udp_packets++;
1230             }
1231           else
1232             {
1233               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1234                 {
1235                   new_port0 = udp0->dst_port = s0->in2out.port;
1236                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1237                     {
1238                       udp0->src_port = s0->ext_host_nat_port;
1239                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1240                     }
1241                 }
1242               udp_packets++;
1243             }
1244
1245           /* Accounting */
1246           nat44_session_update_counters (s0, now,
1247                                          vlib_buffer_length_in_chain (vm, b0),
1248                                          thread_index);
1249           /* Per-user LRU list maintenance */
1250           nat44_session_update_lru (sm, s0, thread_index);
1251
1252         trace0:
1253           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1254                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1255             {
1256               nat44_ed_out2in_trace_t *t =
1257                 vlib_add_trace (vm, node, b0, sizeof (*t));
1258               t->sw_if_index = sw_if_index0;
1259               t->next_index = next0;
1260               t->is_slow_path = 1;
1261
1262               if (s0)
1263                 t->session_index = s0 - tsm->sessions;
1264               else
1265                 t->session_index = ~0;
1266             }
1267
1268           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1269           /* verify speculative enqueue, maybe switch current next frame */
1270           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1271                                            to_next, n_left_to_next,
1272                                            bi0, next0);
1273         }
1274
1275       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1276     }
1277
1278   vlib_node_increment_counter (vm, stats_node_index,
1279                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1280                                pkts_processed);
1281   vlib_node_increment_counter (vm, stats_node_index,
1282                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1283   vlib_node_increment_counter (vm, stats_node_index,
1284                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1285   vlib_node_increment_counter (vm, stats_node_index,
1286                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1287                                icmp_packets);
1288   vlib_node_increment_counter (vm, stats_node_index,
1289                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1290                                other_packets);
1291   vlib_node_increment_counter (vm, stats_node_index,
1292                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1293   return frame->n_vectors;
1294 }
1295
1296 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1297                                      vlib_node_runtime_t * node,
1298                                      vlib_frame_t * frame)
1299 {
1300   return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1301 }
1302
1303 /* *INDENT-OFF* */
1304 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1305   .name = "nat44-ed-out2in",
1306   .vector_size = sizeof (u32),
1307   .sibling_of = "nat-default",
1308   .format_trace = format_nat44_ed_out2in_trace,
1309   .type = VLIB_NODE_TYPE_INTERNAL,
1310   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1311   .error_strings = nat_out2in_ed_error_strings,
1312   .runtime_data_bytes = sizeof (snat_runtime_t),
1313 };
1314 /* *INDENT-ON* */
1315
1316 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1317                                               vlib_node_runtime_t * node,
1318                                               vlib_frame_t * frame)
1319 {
1320   return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1321 }
1322
1323 /* *INDENT-OFF* */
1324 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1325   .name = "nat44-ed-out2in-slowpath",
1326   .vector_size = sizeof (u32),
1327   .sibling_of = "nat-default",
1328   .format_trace = format_nat44_ed_out2in_trace,
1329   .type = VLIB_NODE_TYPE_INTERNAL,
1330   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1331   .error_strings = nat_out2in_ed_error_strings,
1332   .runtime_data_bytes = sizeof (snat_runtime_t),
1333 };
1334 /* *INDENT-ON* */
1335
1336 static u8 *
1337 format_nat_pre_trace (u8 * s, va_list * args)
1338 {
1339   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1340   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1341   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1342   return format (s, "out2in next_index %d", t->next_index);
1343 }
1344
1345 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1346                                     vlib_node_runtime_t * node,
1347                                     vlib_frame_t * frame)
1348 {
1349   return nat_pre_node_fn_inline (vm, node, frame,
1350                                  NAT_NEXT_OUT2IN_ED_FAST_PATH);
1351 }
1352
1353 /* *INDENT-OFF* */
1354 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1355   .name = "nat-pre-out2in",
1356   .vector_size = sizeof (u32),
1357   .sibling_of = "nat-default",
1358   .format_trace = format_nat_pre_trace,
1359   .type = VLIB_NODE_TYPE_INTERNAL,
1360   .n_errors = 0,
1361  };
1362 /* *INDENT-ON* */
1363
1364 /*
1365  * fd.io coding-style-patch-verification: ON
1366  *
1367  * Local Variables:
1368  * eval: (c-set-style "gnu")
1369  * End:
1370  */