nat: nat44 split slow and fast path
[vpp.git] / src / plugins / nat / in2out_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 inside to outside 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_in2out_ed_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nat_in2out_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 } nat_in2out_ed_trace_t;
48
49 static u8 *
50 format_nat_in2out_ed_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   nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
55   char *tag;
56
57   tag =
58     t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
59     "NAT44_IN2OUT_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 #ifndef CLIB_MARCH_VARIANT
68 int
69 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
70 {
71   snat_main_t *sm = &snat_main;
72   nat44_is_idle_session_ctx_t *ctx = arg;
73   snat_session_t *s;
74   u64 sess_timeout_time;
75   nat_ed_ses_key_t ed_key;
76   clib_bihash_kv_16_8_t ed_kv;
77   int i;
78   snat_address_t *a;
79   snat_session_key_t key;
80   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
81                                                        ctx->thread_index);
82
83   s = pool_elt_at_index (tsm->sessions, kv->value);
84   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
85   if (ctx->now >= sess_timeout_time)
86     {
87       if (is_fwd_bypass_session (s))
88         goto delete;
89
90       ed_key.l_addr = s->out2in.addr;
91       ed_key.r_addr = s->ext_host_addr;
92       ed_key.fib_index = s->out2in.fib_index;
93       if (snat_is_unk_proto_session (s))
94         {
95           ed_key.proto = s->in2out.port;
96           ed_key.r_port = 0;
97           ed_key.l_port = 0;
98         }
99       else
100         {
101           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
102           ed_key.l_port = s->out2in.port;
103           ed_key.r_port = s->ext_host_port;
104         }
105       ed_kv.key[0] = ed_key.as_u64[0];
106       ed_kv.key[1] = ed_key.as_u64[1];
107       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
108         nat_elog_warn ("out2in_ed key del failed");
109
110       if (snat_is_unk_proto_session (s))
111         goto delete;
112
113       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
114                                            s->in2out.addr.as_u32,
115                                            s->out2in.addr.as_u32,
116                                            s->in2out.protocol,
117                                            s->in2out.port,
118                                            s->out2in.port,
119                                            s->in2out.fib_index);
120
121       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
122                              &s->in2out.addr, s->in2out.port,
123                              &s->ext_host_nat_addr, s->ext_host_nat_port,
124                              &s->out2in.addr, s->out2in.port,
125                              &s->ext_host_addr, s->ext_host_port,
126                              s->in2out.protocol, is_twice_nat_session (s));
127
128       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
129                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
130                    ctx->thread_index);
131
132       if (is_twice_nat_session (s))
133         {
134           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
135             {
136               key.protocol = s->in2out.protocol;
137               key.port = s->ext_host_nat_port;
138               a = sm->twice_nat_addresses + i;
139               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
140                 {
141                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
142                                                       ctx->thread_index,
143                                                       &key);
144                   break;
145                 }
146             }
147         }
148
149       if (snat_is_session_static (s))
150         goto delete;
151
152       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
153                                           &s->out2in);
154     delete:
155       nat44_delete_session (sm, s, ctx->thread_index);
156       return 1;
157     }
158
159   return 0;
160 }
161 #endif
162
163 static inline u32
164 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
165                           ip4_header_t * ip0, icmp46_header_t * icmp0,
166                           u32 sw_if_index0, u32 rx_fib_index0,
167                           vlib_node_runtime_t * node, u32 next0, f64 now,
168                           u32 thread_index, snat_session_t ** p_s0)
169 {
170   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
171                        next0, thread_index, p_s0, 0);
172   snat_session_t *s0 = *p_s0;
173   if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
174     {
175       /* Accounting */
176       nat44_session_update_counters (s0, now,
177                                      vlib_buffer_length_in_chain
178                                      (sm->vlib_main, b0), thread_index);
179       /* Per-user LRU list maintenance */
180       nat44_session_update_lru (sm, s0, thread_index);
181     }
182   return next0;
183 }
184
185 static u32
186 slow_path_ed (snat_main_t * sm,
187               vlib_buffer_t * b,
188               u32 rx_fib_index,
189               clib_bihash_kv_16_8_t * kv,
190               snat_session_t ** sessionp,
191               vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
192 {
193   snat_session_t *s = 0;
194   snat_user_t *u;
195   snat_session_key_t key0, key1;
196   lb_nat_type_t lb = 0, is_sm = 0;
197   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
198   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
199   u32 proto = ip_proto_to_snat_proto (key->proto);
200   nat_outside_fib_t *outside_fib;
201   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
202   u8 identity_nat;
203   fib_prefix_t pfx = {
204     .fp_proto = FIB_PROTOCOL_IP4,
205     .fp_len = 32,
206     .fp_addr = {
207                 .ip4.as_u32 = key->r_addr.as_u32,
208                 },
209   };
210   nat44_is_idle_session_ctx_t ctx;
211
212   nat44_session_try_cleanup (&key->l_addr, rx_fib_index, thread_index, now);
213
214   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
215     {
216       b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
217       nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
218       nat_elog_notice ("maximum sessions exceeded");
219       return NAT_NEXT_DROP;
220     }
221
222   key0.addr = key->l_addr;
223   key0.port = key->l_port;
224   key1.protocol = key0.protocol = proto;
225   key0.fib_index = rx_fib_index;
226   key1.fib_index = sm->outside_fib_index;
227   /* First try to match static mapping by local address and port */
228   if (snat_static_mapping_match
229       (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
230     {
231       /* Try to create dynamic translation */
232       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
233                                                thread_index, &key1,
234                                                sm->port_per_thread,
235                                                tsm->snat_thread_index))
236         {
237           nat_elog_notice ("addresses exhausted");
238           b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
239           return NAT_NEXT_DROP;
240         }
241     }
242   else
243     {
244       if (PREDICT_FALSE (identity_nat))
245         {
246           *sessionp = s;
247           return next;
248         }
249
250       is_sm = 1;
251     }
252
253   if (proto == SNAT_PROTOCOL_TCP)
254     {
255       if (!tcp_flags_is_init
256           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
257         {
258           b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
259           return NAT_NEXT_DROP;
260         }
261     }
262
263   u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
264   if (!u)
265     {
266       nat_elog_warn ("create NAT user failed");
267       if (!is_sm)
268         snat_free_outside_address_and_port (sm->addresses,
269                                             thread_index, &key1);
270       return NAT_NEXT_DROP;
271     }
272
273   s = nat_ed_session_alloc (sm, u, thread_index, now);
274   if (!s)
275     {
276       nat44_delete_user_with_no_session (sm, u, thread_index);
277       nat_elog_warn ("create NAT session failed");
278       if (!is_sm)
279         snat_free_outside_address_and_port (sm->addresses,
280                                             thread_index, &key1);
281       return NAT_NEXT_DROP;
282     }
283
284   user_session_increment (sm, u, is_sm);
285   if (is_sm)
286     s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
287   if (lb)
288     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
289   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
290   s->ext_host_addr = key->r_addr;
291   s->ext_host_port = key->r_port;
292   s->in2out = key0;
293   s->out2in = key1;
294   s->out2in.protocol = key0.protocol;
295
296   switch (vec_len (sm->outside_fibs))
297     {
298     case 0:
299       s->out2in.fib_index = sm->outside_fib_index;
300       break;
301     case 1:
302       s->out2in.fib_index = sm->outside_fibs[0].fib_index;
303       break;
304     default:
305       /* *INDENT-OFF* */
306       vec_foreach (outside_fib, sm->outside_fibs)
307        {
308           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
309           if (FIB_NODE_INDEX_INVALID != fei)
310             {
311               if (fib_entry_get_resolving_interface (fei) != ~0)
312                 {
313                   s->out2in.fib_index = outside_fib->fib_index;
314                   break;
315                 }
316             }
317         }
318       /* *INDENT-ON* */
319       break;
320     }
321
322   /* Add to lookup tables */
323   kv->value = s - tsm->sessions;
324   ctx.now = now;
325   ctx.thread_index = thread_index;
326   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
327                                                nat44_i2o_ed_is_idle_session_cb,
328                                                &ctx))
329     nat_elog_notice ("in2out-ed key add failed");
330
331   make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
332               key1.port, key->r_port);
333   kv->value = s - tsm->sessions;
334   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
335                                                nat44_o2i_ed_is_idle_session_cb,
336                                                &ctx))
337     nat_elog_notice ("out2in-ed key add failed");
338
339   *sessionp = s;
340
341   /* log NAT event */
342   snat_ipfix_logging_nat44_ses_create (thread_index,
343                                        s->in2out.addr.as_u32,
344                                        s->out2in.addr.as_u32,
345                                        s->in2out.protocol,
346                                        s->in2out.port,
347                                        s->out2in.port, s->in2out.fib_index);
348
349   nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
350                          &s->in2out.addr, s->in2out.port,
351                          &s->ext_host_nat_addr, s->ext_host_nat_port,
352                          &s->out2in.addr, s->out2in.port,
353                          &s->ext_host_addr, s->ext_host_port,
354                          s->in2out.protocol, 0);
355
356   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
357                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
358                &s->ext_host_nat_addr, s->ext_host_nat_port,
359                s->in2out.protocol, s->in2out.fib_index, s->flags,
360                thread_index, 0);
361
362   return next;
363 }
364
365 static_always_inline int
366 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
367                         u32 sw_if_index, ip4_header_t * ip, u32 proto,
368                         u32 rx_fib_index, u32 thread_index)
369 {
370   udp_header_t *udp = ip4_next_header (ip);
371   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
372   clib_bihash_kv_16_8_t kv, value;
373   snat_session_key_t key0, key1;
374
375   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
376               sm->outside_fib_index, udp->dst_port, udp->src_port);
377
378   /* NAT packet aimed at external address if */
379   /* has active sessions */
380   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
381     {
382       key0.addr = ip->dst_address;
383       key0.port = udp->dst_port;
384       key0.protocol = proto;
385       key0.fib_index = sm->outside_fib_index;
386       /* or is static mappings */
387       if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
388         return 0;
389     }
390   else
391     return 0;
392
393   if (sm->forwarding_enabled)
394     return 1;
395
396   return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
397                                   rx_fib_index);
398 }
399
400 static_always_inline int
401 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
402                                       u32 thread_index, f64 now,
403                                       vlib_main_t * vm, vlib_buffer_t * b)
404 {
405   nat_ed_ses_key_t key;
406   clib_bihash_kv_16_8_t kv, value;
407   snat_session_t *s = 0;
408   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
409
410   if (!sm->forwarding_enabled)
411     return 0;
412
413   if (ip->protocol == IP_PROTOCOL_ICMP)
414     {
415       key.as_u64[0] = key.as_u64[1] = 0;
416       if (get_icmp_i2o_ed_key (b, ip, &key))
417         return 0;
418       key.fib_index = 0;
419       kv.key[0] = key.as_u64[0];
420       kv.key[1] = key.as_u64[1];
421     }
422   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
423     {
424       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
425                   vnet_buffer (b)->ip.reass.l4_src_port,
426                   vnet_buffer (b)->ip.reass.l4_dst_port);
427     }
428   else
429     {
430       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
431                   0);
432     }
433
434   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
435     {
436       s = pool_elt_at_index (tsm->sessions, value.value);
437       if (is_fwd_bypass_session (s))
438         {
439           if (ip->protocol == IP_PROTOCOL_TCP)
440             {
441               if (nat44_set_tcp_session_state_i2o (sm, s, b, thread_index))
442                 return 1;
443             }
444           /* Accounting */
445           nat44_session_update_counters (s, now,
446                                          vlib_buffer_length_in_chain (vm, b),
447                                          thread_index);
448           /* Per-user LRU list maintenance */
449           nat44_session_update_lru (sm, s, thread_index);
450           return 1;
451         }
452       else
453         return 0;
454     }
455
456   return 0;
457 }
458
459 static_always_inline int
460 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
461                                        u8 proto, u16 src_port, u16 dst_port,
462                                        u32 thread_index, u32 rx_sw_if_index,
463                                        u32 tx_sw_if_index)
464 {
465   clib_bihash_kv_16_8_t kv, value;
466   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
467   snat_interface_t *i;
468   snat_session_t *s;
469   u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
470   u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
471
472   /* src NAT check */
473   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
474               src_port, dst_port);
475   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
476     {
477       s = pool_elt_at_index (tsm->sessions, value.value);
478       if (nat44_is_ses_closed (s))
479         {
480           nat_free_session_data (sm, s, thread_index, 0);
481           nat44_delete_session (sm, s, thread_index);
482         }
483       else
484         s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
485       return 1;
486     }
487
488   /* dst NAT check */
489   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
490               dst_port, src_port);
491   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
492     {
493       s = pool_elt_at_index (tsm->sessions, value.value);
494       if (is_fwd_bypass_session (s))
495         return 0;
496
497       /* hairpinning */
498       /* *INDENT-OFF* */
499       pool_foreach (i, sm->output_feature_interfaces,
500       ({
501         if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
502            return 0;
503       }));
504       /* *INDENT-ON* */
505       return 1;
506     }
507
508   return 0;
509 }
510
511 #ifndef CLIB_MARCH_VARIANT
512 u32
513 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
514                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
515                       u8 * p_proto, snat_session_key_t * p_value,
516                       u8 * p_dont_translate, void *d, void *e)
517 {
518   u32 sw_if_index;
519   u32 rx_fib_index;
520   nat_ed_ses_key_t key;
521   snat_session_t *s = 0;
522   u8 dont_translate = 0;
523   clib_bihash_kv_16_8_t kv, value;
524   u32 next = ~0;
525   int err;
526   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
527
528   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
529   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
530
531   key.as_u64[0] = key.as_u64[1] = 0;
532   err = get_icmp_i2o_ed_key (b, ip, &key);
533   if (err != 0)
534     {
535       b->error = node->errors[err];
536       next = NAT_NEXT_DROP;
537       goto out;
538     }
539   key.fib_index = rx_fib_index;
540
541   kv.key[0] = key.as_u64[0];
542   kv.key[1] = key.as_u64[1];
543
544   if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
545     {
546       if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
547         {
548           if (PREDICT_FALSE
549               (nat44_ed_not_translate_output_feature
550                (sm, ip, key.proto, key.l_port, key.r_port, thread_index,
551                 sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_TX])))
552             {
553               dont_translate = 1;
554               goto out;
555             }
556         }
557       else
558         {
559           if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
560                                                      ip, SNAT_PROTOCOL_ICMP,
561                                                      rx_fib_index,
562                                                      thread_index)))
563             {
564               dont_translate = 1;
565               goto out;
566             }
567         }
568
569       if (PREDICT_FALSE
570           (icmp_type_is_error_message
571            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
572         {
573           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
574           next = NAT_NEXT_DROP;
575           goto out;
576         }
577
578       next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
579                            thread_index, vlib_time_now (sm->vlib_main));
580
581       if (PREDICT_FALSE (next == NAT_NEXT_DROP))
582         goto out;
583
584       if (!s)
585         {
586           dont_translate = 1;
587           goto out;
588         }
589     }
590   else
591     {
592       if (PREDICT_FALSE
593           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
594            ICMP4_echo_request
595            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
596            ICMP4_echo_reply
597            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
598                                            reass.icmp_type_or_tcp_flags)))
599         {
600           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
601           next = NAT_NEXT_DROP;
602           goto out;
603         }
604
605       s = pool_elt_at_index (tsm->sessions, value.value);
606     }
607
608   *p_proto = ip_proto_to_snat_proto (key.proto);
609 out:
610   if (s)
611     *p_value = s->out2in;
612   *p_dont_translate = dont_translate;
613   if (d)
614     *(snat_session_t **) d = s;
615   return next;
616 }
617 #endif
618
619 static snat_session_t *
620 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
621                                vlib_buffer_t * b,
622                                ip4_header_t * ip,
623                                u32 rx_fib_index,
624                                u32 thread_index,
625                                f64 now,
626                                vlib_main_t * vm, vlib_node_runtime_t * node)
627 {
628   clib_bihash_kv_8_8_t kv, value;
629   clib_bihash_kv_16_8_t s_kv, s_value;
630   snat_static_mapping_t *m;
631   u32 old_addr, new_addr = 0;
632   ip_csum_t sum;
633   snat_user_t *u;
634   dlist_elt_t *head, *elt;
635   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
636   u32 elt_index, head_index, ses_index;
637   snat_session_t *s;
638   u32 outside_fib_index = sm->outside_fib_index;
639   int i;
640   u8 is_sm = 0;
641   nat_outside_fib_t *outside_fib;
642   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
643   fib_prefix_t pfx = {
644     .fp_proto = FIB_PROTOCOL_IP4,
645     .fp_len = 32,
646     .fp_addr = {
647                 .ip4.as_u32 = ip->dst_address.as_u32,
648                 },
649   };
650
651   switch (vec_len (sm->outside_fibs))
652     {
653     case 0:
654       outside_fib_index = sm->outside_fib_index;
655       break;
656     case 1:
657       outside_fib_index = sm->outside_fibs[0].fib_index;
658       break;
659     default:
660       /* *INDENT-OFF* */
661       vec_foreach (outside_fib, sm->outside_fibs)
662         {
663           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
664           if (FIB_NODE_INDEX_INVALID != fei)
665             {
666               if (fib_entry_get_resolving_interface (fei) != ~0)
667                 {
668                   outside_fib_index = outside_fib->fib_index;
669                   break;
670                 }
671             }
672         }
673       /* *INDENT-ON* */
674       break;
675     }
676   old_addr = ip->src_address.as_u32;
677
678   make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
679               rx_fib_index, 0, 0);
680
681   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
682     {
683       s = pool_elt_at_index (tsm->sessions, s_value.value);
684       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
685     }
686   else
687     {
688       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
689         {
690           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
691           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
692           nat_elog_notice ("maximum sessions exceeded");
693           return 0;
694         }
695
696       u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
697                                   thread_index);
698       if (!u)
699         {
700           nat_elog_warn ("create NAT user failed");
701           return 0;
702         }
703
704       make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
705
706       /* Try to find static mapping first */
707       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
708         {
709           m = pool_elt_at_index (sm->static_mappings, value.value);
710           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
711           is_sm = 1;
712           goto create_ses;
713         }
714       /* Fallback to 3-tuple key */
715       else
716         {
717           /* Choose same out address as for TCP/UDP session to same destination */
718           head_index = u->sessions_per_user_list_head_index;
719           head = pool_elt_at_index (tsm->list_pool, head_index);
720           elt_index = head->next;
721           if (PREDICT_FALSE (elt_index == ~0))
722             ses_index = ~0;
723           else
724             {
725               elt = pool_elt_at_index (tsm->list_pool, elt_index);
726               ses_index = elt->value;
727             }
728
729           while (ses_index != ~0)
730             {
731               s = pool_elt_at_index (tsm->sessions, ses_index);
732               elt_index = elt->next;
733               elt = pool_elt_at_index (tsm->list_pool, elt_index);
734               ses_index = elt->value;
735
736               if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
737                 {
738                   new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
739
740                   make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
741                               ip->protocol, outside_fib_index, 0, 0);
742                   if (clib_bihash_search_16_8
743                       (&tsm->out2in_ed, &s_kv, &s_value))
744                     goto create_ses;
745
746                   break;
747                 }
748             }
749
750           for (i = 0; i < vec_len (sm->addresses); i++)
751             {
752               make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
753                           ip->protocol, outside_fib_index, 0, 0);
754               if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
755                 {
756                   new_addr = ip->src_address.as_u32 =
757                     sm->addresses[i].addr.as_u32;
758                   goto create_ses;
759                 }
760             }
761           return 0;
762         }
763
764     create_ses:
765       s = nat_ed_session_alloc (sm, u, thread_index, now);
766       if (!s)
767         {
768           nat44_delete_user_with_no_session (sm, u, thread_index);
769           nat_elog_warn ("create NAT session failed");
770           return 0;
771         }
772
773       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
774       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
775       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
776       s->out2in.addr.as_u32 = new_addr;
777       s->out2in.fib_index = outside_fib_index;
778       s->in2out.addr.as_u32 = old_addr;
779       s->in2out.fib_index = rx_fib_index;
780       s->in2out.port = s->out2in.port = ip->protocol;
781       if (is_sm)
782         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
783       user_session_increment (sm, u, is_sm);
784
785       /* Add to lookup tables */
786       make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
787                   rx_fib_index, 0, 0);
788       s_kv.value = s - tsm->sessions;
789       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
790         nat_elog_notice ("in2out key add failed");
791
792       make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
793                   outside_fib_index, 0, 0);
794       s_kv.value = s - tsm->sessions;
795       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
796         nat_elog_notice ("out2in key add failed");
797     }
798
799   /* Update IP checksum */
800   sum = ip->checksum;
801   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
802   ip->checksum = ip_csum_fold (sum);
803
804   /* Accounting */
805   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
806                                  thread_index);
807   /* Per-user LRU list maintenance */
808   nat44_session_update_lru (sm, s, thread_index);
809
810   /* Hairpinning */
811   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
812     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
813
814   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
815     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
816
817   return s;
818 }
819
820 static inline uword
821 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
822                                           vlib_node_runtime_t * node,
823                                           vlib_frame_t * frame,
824                                           int is_output_feature)
825 {
826   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
827   nat_next_t next_index;
828   snat_main_t *sm = &snat_main;
829   f64 now = vlib_time_now (vm);
830   u32 thread_index = vm->thread_index;
831   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
832   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
833     0, def_slow;
834
835   def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH :
836     NAT_NEXT_IN2OUT_ED_SLOW_PATH;
837
838   stats_node_index = sm->ed_in2out_node_index;
839
840   from = vlib_frame_vector_args (frame);
841   n_left_from = frame->n_vectors;
842   next_index = node->cached_next_index;
843
844   while (n_left_from > 0)
845     {
846       u32 n_left_to_next;
847
848       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
849
850       while (n_left_from > 0 && n_left_to_next > 0)
851         {
852           u32 bi0;
853           vlib_buffer_t *b0;
854           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
855             new_addr0, old_addr0;
856           u16 old_port0, new_port0;
857           ip4_header_t *ip0;
858           udp_header_t *udp0;
859           tcp_header_t *tcp0;
860           snat_session_t *s0 = 0;
861           clib_bihash_kv_16_8_t kv0, value0;
862           ip_csum_t sum0;
863
864           /* speculatively enqueue b0 to the current next frame */
865           bi0 = from[0];
866           to_next[0] = bi0;
867           from += 1;
868           to_next += 1;
869           n_left_from -= 1;
870           n_left_to_next -= 1;
871
872           b0 = vlib_get_buffer (vm, bi0);
873
874           if (is_output_feature)
875             {
876               vnet_feature_next (&nat_buffer_opaque (b0)->arc_next, b0);
877               iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
878             }
879
880           next0 = nat_buffer_opaque (b0)->arc_next;
881
882           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
883                                   iph_offset0);
884
885           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
886           rx_fib_index0 =
887             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
888                                                  sw_if_index0);
889
890           if (PREDICT_FALSE (ip0->ttl == 1))
891             {
892               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
893               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
894                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
895                                            0);
896               next0 = NAT_NEXT_ICMP_ERROR;
897               goto trace0;
898             }
899
900           udp0 = ip4_next_header (ip0);
901           tcp0 = (tcp_header_t *) udp0;
902           proto0 = ip_proto_to_snat_proto (ip0->protocol);
903
904           if (PREDICT_FALSE (proto0 == ~0))
905             {
906               next0 = def_slow;
907               goto trace0;
908             }
909
910           if (is_output_feature)
911             {
912               if (PREDICT_FALSE (nat_not_translate_output_feature_fwd
913                                  (sm, ip0, thread_index, now, vm, b0)))
914                 goto trace0;
915             }
916
917           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
918             {
919               next0 = def_slow;
920               goto trace0;
921             }
922
923           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
924                       ip0->protocol, rx_fib_index0,
925                       vnet_buffer (b0)->ip.reass.l4_src_port,
926                       vnet_buffer (b0)->ip.reass.l4_dst_port);
927
928           // lookup for session
929           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
930             {
931               // session does not exist go slow path
932               next0 = def_slow;
933               goto trace0;
934             }
935
936           s0 = pool_elt_at_index (tsm->sessions, value0.value);
937
938           b0->flags |= VNET_BUFFER_F_IS_NATED;
939
940           if (!is_output_feature)
941             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
942
943           old_addr0 = ip0->src_address.as_u32;
944           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
945           sum0 = ip0->checksum;
946           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
947                                  src_address);
948           if (PREDICT_FALSE (is_twice_nat_session (s0)))
949             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
950                                    s0->ext_host_addr.as_u32, ip4_header_t,
951                                    dst_address);
952           ip0->checksum = ip_csum_fold (sum0);
953
954           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
955
956           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
957             {
958               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
959                 {
960                   new_port0 = udp0->src_port = s0->out2in.port;
961                   sum0 = tcp0->checksum;
962                   sum0 =
963                     ip_csum_update (sum0, old_addr0, new_addr0,
964                                     ip4_header_t, dst_address);
965                   sum0 =
966                     ip_csum_update (sum0, old_port0, new_port0,
967                                     ip4_header_t, length);
968                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
969                     {
970                       sum0 =
971                         ip_csum_update (sum0, ip0->dst_address.as_u32,
972                                         s0->ext_host_addr.as_u32,
973                                         ip4_header_t, dst_address);
974                       sum0 =
975                         ip_csum_update (sum0,
976                                         vnet_buffer (b0)->ip.
977                                         reass.l4_dst_port, s0->ext_host_port,
978                                         ip4_header_t, length);
979                       tcp0->dst_port = s0->ext_host_port;
980                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
981                     }
982                   mss_clamping (sm, tcp0, &sum0);
983                   tcp0->checksum = ip_csum_fold (sum0);
984                 }
985               tcp_packets++;
986               if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
987                 goto trace0;
988             }
989           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
990                    && udp0->checksum)
991             {
992               new_port0 = udp0->src_port = s0->out2in.port;
993               sum0 = udp0->checksum;
994               sum0 =
995                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
996                                 dst_address);
997               sum0 =
998                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
999                                 length);
1000               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1001                 {
1002                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1003                                          s0->ext_host_addr.as_u32,
1004                                          ip4_header_t, dst_address);
1005                   sum0 =
1006                     ip_csum_update (sum0,
1007                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1008                                     s0->ext_host_port, ip4_header_t, length);
1009                   udp0->dst_port = s0->ext_host_port;
1010                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1011                 }
1012               udp0->checksum = ip_csum_fold (sum0);
1013               udp_packets++;
1014             }
1015           else
1016             {
1017               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1018                 {
1019                   new_port0 = udp0->src_port = s0->out2in.port;
1020                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1021                     {
1022                       udp0->dst_port = s0->ext_host_port;
1023                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1024                     }
1025                   udp_packets++;
1026                 }
1027             }
1028
1029           /* Accounting */
1030           nat44_session_update_counters (s0, now,
1031                                          vlib_buffer_length_in_chain
1032                                          (vm, b0), thread_index);
1033           /* Per-user LRU list maintenance */
1034           nat44_session_update_lru (sm, s0, thread_index);
1035
1036         trace0:
1037           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1038                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1039             {
1040               nat_in2out_ed_trace_t *t =
1041                 vlib_add_trace (vm, node, b0, sizeof (*t));
1042               t->sw_if_index = sw_if_index0;
1043               t->next_index = next0;
1044               t->is_slow_path = 0;
1045
1046               if (s0)
1047                 t->session_index = s0 - tsm->sessions;
1048               else
1049                 t->session_index = ~0;
1050             }
1051
1052           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1053           /* verify speculative enqueue, maybe switch current next frame */
1054           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1055                                            to_next, n_left_to_next,
1056                                            bi0, next0);
1057         }
1058
1059       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1060     }
1061
1062   vlib_node_increment_counter (vm, stats_node_index,
1063                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1064                                pkts_processed);
1065   vlib_node_increment_counter (vm, stats_node_index,
1066                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1067   vlib_node_increment_counter (vm, stats_node_index,
1068                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1069   vlib_node_increment_counter (vm, stats_node_index,
1070                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1071                                icmp_packets);
1072   vlib_node_increment_counter (vm, stats_node_index,
1073                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1074                                other_packets);
1075   return frame->n_vectors;
1076 }
1077
1078 static inline uword
1079 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t * vm,
1080                                           vlib_node_runtime_t * node,
1081                                           vlib_frame_t * frame,
1082                                           int is_output_feature)
1083 {
1084   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
1085   nat_next_t next_index;
1086   snat_main_t *sm = &snat_main;
1087   f64 now = vlib_time_now (vm);
1088   u32 thread_index = vm->thread_index;
1089   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1090   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets = 0;
1091
1092   stats_node_index = sm->ed_in2out_slowpath_node_index;
1093
1094   from = vlib_frame_vector_args (frame);
1095   n_left_from = frame->n_vectors;
1096   next_index = node->cached_next_index;
1097
1098   while (n_left_from > 0)
1099     {
1100       u32 n_left_to_next;
1101
1102       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1103
1104       while (n_left_from > 0 && n_left_to_next > 0)
1105         {
1106           u32 bi0;
1107           vlib_buffer_t *b0;
1108           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1109             new_addr0, old_addr0;
1110           u16 old_port0, new_port0;
1111           ip4_header_t *ip0;
1112           udp_header_t *udp0;
1113           tcp_header_t *tcp0;
1114           icmp46_header_t *icmp0;
1115           snat_session_t *s0 = 0;
1116           clib_bihash_kv_16_8_t kv0, value0;
1117           ip_csum_t sum0;
1118
1119           /* speculatively enqueue b0 to the current next frame */
1120           bi0 = from[0];
1121           to_next[0] = bi0;
1122           from += 1;
1123           to_next += 1;
1124           n_left_from -= 1;
1125           n_left_to_next -= 1;
1126
1127           b0 = vlib_get_buffer (vm, bi0);
1128
1129           if (is_output_feature)
1130             iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1131
1132           next0 = nat_buffer_opaque (b0)->arc_next;
1133
1134           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1135                                   iph_offset0);
1136
1137           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1138           rx_fib_index0 =
1139             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1140                                                  sw_if_index0);
1141
1142           if (PREDICT_FALSE (ip0->ttl == 1))
1143             {
1144               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1145               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1146                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1147                                            0);
1148               next0 = NAT_NEXT_ICMP_ERROR;
1149               goto trace0;
1150             }
1151
1152           udp0 = ip4_next_header (ip0);
1153           tcp0 = (tcp_header_t *) udp0;
1154           icmp0 = (icmp46_header_t *) udp0;
1155           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1156
1157           if (PREDICT_FALSE (proto0 == ~0))
1158             {
1159               s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1160                                                   rx_fib_index0,
1161                                                   thread_index, now,
1162                                                   vm, node);
1163               if (!s0)
1164                 next0 = NAT_NEXT_DROP;
1165
1166               other_packets++;
1167               goto trace0;
1168             }
1169
1170           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1171             {
1172               next0 = icmp_in2out_ed_slow_path
1173                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1174                  node, next0, now, thread_index, &s0);
1175               icmp_packets++;
1176               goto trace0;
1177             }
1178
1179           // move down
1180           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1181                       ip0->protocol, rx_fib_index0,
1182                       vnet_buffer (b0)->ip.reass.l4_src_port,
1183                       vnet_buffer (b0)->ip.reass.l4_dst_port);
1184
1185           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1186             {
1187
1188               if (is_output_feature)
1189                 {
1190                   if (PREDICT_FALSE
1191                       (nat44_ed_not_translate_output_feature
1192                        (sm, ip0, ip0->protocol,
1193                         vnet_buffer (b0)->ip.reass.l4_src_port,
1194                         vnet_buffer (b0)->ip.reass.l4_dst_port,
1195                         thread_index, sw_if_index0,
1196                         vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1197                     goto trace0;
1198
1199                   /*
1200                    * Send DHCP packets to the ipv4 stack, or we won't
1201                    * be able to use dhcp client on the outside interface
1202                    */
1203                   if (PREDICT_FALSE
1204                       (proto0 == SNAT_PROTOCOL_UDP
1205                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1206                            clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1207                        && ip0->dst_address.as_u32 == 0xffffffff))
1208                     goto trace0;
1209                 }
1210               else
1211                 {
1212                   if (PREDICT_FALSE
1213                       (nat44_ed_not_translate
1214                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1215                         thread_index)))
1216                     goto trace0;
1217                 }
1218
1219               next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1220                                     next0, thread_index, now);
1221
1222               if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1223                 goto trace0;
1224
1225               if (PREDICT_FALSE (!s0))
1226                 goto trace0;
1227
1228             }
1229           else
1230             {
1231               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1232             }
1233
1234
1235           b0->flags |= VNET_BUFFER_F_IS_NATED;
1236
1237           if (!is_output_feature)
1238             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1239
1240           old_addr0 = ip0->src_address.as_u32;
1241           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1242           sum0 = ip0->checksum;
1243           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1244                                  src_address);
1245           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1246             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1247                                    s0->ext_host_addr.as_u32, ip4_header_t,
1248                                    dst_address);
1249           ip0->checksum = ip_csum_fold (sum0);
1250
1251           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1252
1253           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1254             {
1255               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1256                 {
1257                   new_port0 = udp0->src_port = s0->out2in.port;
1258                   sum0 = tcp0->checksum;
1259                   sum0 =
1260                     ip_csum_update (sum0, old_addr0, new_addr0,
1261                                     ip4_header_t, dst_address);
1262                   sum0 =
1263                     ip_csum_update (sum0, old_port0, new_port0,
1264                                     ip4_header_t, length);
1265                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1266                     {
1267                       sum0 =
1268                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1269                                         s0->ext_host_addr.as_u32,
1270                                         ip4_header_t, dst_address);
1271                       sum0 =
1272                         ip_csum_update (sum0,
1273                                         vnet_buffer (b0)->ip.
1274                                         reass.l4_dst_port, s0->ext_host_port,
1275                                         ip4_header_t, length);
1276                       tcp0->dst_port = s0->ext_host_port;
1277                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1278                     }
1279                   mss_clamping (sm, tcp0, &sum0);
1280                   tcp0->checksum = ip_csum_fold (sum0);
1281                 }
1282               tcp_packets++;
1283               if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
1284                 goto trace0;
1285             }
1286           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1287                    && udp0->checksum)
1288             {
1289               new_port0 = udp0->src_port = s0->out2in.port;
1290               sum0 = udp0->checksum;
1291               sum0 =
1292                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1293                                 dst_address);
1294               sum0 =
1295                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1296                                 length);
1297               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1298                 {
1299                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1300                                          s0->ext_host_addr.as_u32,
1301                                          ip4_header_t, dst_address);
1302                   sum0 =
1303                     ip_csum_update (sum0,
1304                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1305                                     s0->ext_host_port, ip4_header_t, length);
1306                   udp0->dst_port = s0->ext_host_port;
1307                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1308                 }
1309               udp0->checksum = ip_csum_fold (sum0);
1310               udp_packets++;
1311             }
1312           else
1313             {
1314               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1315                 {
1316                   new_port0 = udp0->src_port = s0->out2in.port;
1317                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1318                     {
1319                       udp0->dst_port = s0->ext_host_port;
1320                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1321                     }
1322                   udp_packets++;
1323                 }
1324             }
1325
1326           /* Accounting */
1327           nat44_session_update_counters (s0, now,
1328                                          vlib_buffer_length_in_chain
1329                                          (vm, b0), thread_index);
1330           /* Per-user LRU list maintenance */
1331           nat44_session_update_lru (sm, s0, thread_index);
1332
1333         trace0:
1334           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1335                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1336             {
1337               nat_in2out_ed_trace_t *t =
1338                 vlib_add_trace (vm, node, b0, sizeof (*t));
1339               t->sw_if_index = sw_if_index0;
1340               t->next_index = next0;
1341               t->is_slow_path = 1;
1342
1343               if (s0)
1344                 t->session_index = s0 - tsm->sessions;
1345               else
1346                 t->session_index = ~0;
1347             }
1348
1349           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1350
1351           /* verify speculative enqueue, maybe switch current next frame */
1352           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1353                                            to_next, n_left_to_next,
1354                                            bi0, next0);
1355         }
1356
1357       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1358     }
1359
1360   vlib_node_increment_counter (vm, stats_node_index,
1361                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1362                                pkts_processed);
1363   vlib_node_increment_counter (vm, stats_node_index,
1364                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1365   vlib_node_increment_counter (vm, stats_node_index,
1366                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1367   vlib_node_increment_counter (vm, stats_node_index,
1368                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1369                                icmp_packets);
1370   vlib_node_increment_counter (vm, stats_node_index,
1371                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1372                                other_packets);
1373   return frame->n_vectors;
1374 }
1375
1376 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1377                                      vlib_node_runtime_t * node,
1378                                      vlib_frame_t * frame)
1379 {
1380   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1381 }
1382
1383 /* *INDENT-OFF* */
1384 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1385   .name = "nat44-ed-in2out",
1386   .vector_size = sizeof (u32),
1387   .sibling_of = "nat-default",
1388   .format_trace = format_nat_in2out_ed_trace,
1389   .type = VLIB_NODE_TYPE_INTERNAL,
1390   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1391   .error_strings = nat_in2out_ed_error_strings,
1392   .runtime_data_bytes = sizeof (snat_runtime_t),
1393 };
1394 /* *INDENT-ON* */
1395
1396 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1397                                             vlib_node_runtime_t * node,
1398                                             vlib_frame_t * frame)
1399 {
1400   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1401 }
1402
1403 /* *INDENT-OFF* */
1404 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1405   .name = "nat44-ed-in2out-output",
1406   .vector_size = sizeof (u32),
1407   .sibling_of = "nat-default",
1408   .format_trace = format_nat_in2out_ed_trace,
1409   .type = VLIB_NODE_TYPE_INTERNAL,
1410   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1411   .error_strings = nat_in2out_ed_error_strings,
1412   .runtime_data_bytes = sizeof (snat_runtime_t),
1413 };
1414 /* *INDENT-ON* */
1415
1416 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1417                                               vlib_node_runtime_t *
1418                                               node, vlib_frame_t * frame)
1419 {
1420   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1421 }
1422
1423 /* *INDENT-OFF* */
1424 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1425   .name = "nat44-ed-in2out-slowpath",
1426   .vector_size = sizeof (u32),
1427   .sibling_of = "nat-default",
1428   .format_trace = format_nat_in2out_ed_trace,
1429   .type = VLIB_NODE_TYPE_INTERNAL,
1430   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1431   .error_strings = nat_in2out_ed_error_strings,
1432   .runtime_data_bytes = sizeof (snat_runtime_t),
1433 };
1434 /* *INDENT-ON* */
1435
1436 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1437                                                      vlib_node_runtime_t
1438                                                      * node,
1439                                                      vlib_frame_t * frame)
1440 {
1441   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1442 }
1443
1444 /* *INDENT-OFF* */
1445 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1446   .name = "nat44-ed-in2out-output-slowpath",
1447   .vector_size = sizeof (u32),
1448   .sibling_of = "nat-default",
1449   .format_trace = format_nat_in2out_ed_trace,
1450   .type = VLIB_NODE_TYPE_INTERNAL,
1451   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1452   .error_strings = nat_in2out_ed_error_strings,
1453   .runtime_data_bytes = sizeof (snat_runtime_t),
1454 };
1455 /* *INDENT-ON* */
1456
1457 static u8 *
1458 format_nat_pre_trace (u8 * s, va_list * args)
1459 {
1460   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1461   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1462   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1463   return format (s, "in2out next_index %d", t->next_index);
1464 }
1465
1466 VLIB_NODE_FN (nat_pre_in2out_node)
1467   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1468 {
1469   return nat_pre_node_fn_inline (vm, node, frame,
1470                                  NAT_NEXT_IN2OUT_ED_FAST_PATH);
1471 }
1472
1473 /* *INDENT-OFF* */
1474 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1475   .name = "nat-pre-in2out",
1476   .vector_size = sizeof (u32),
1477   .sibling_of = "nat-default",
1478   .format_trace = format_nat_pre_trace,
1479   .type = VLIB_NODE_TYPE_INTERNAL,
1480   .n_errors = 0,
1481 };
1482 /* *INDENT-ON* */
1483
1484 /*
1485  * fd.io coding-style-patch-verification: ON
1486  *
1487  * Local Variables:
1488  * eval: (c-set-style "gnu")
1489  * End:
1490  */