NAT: VPP-1552 code migration from old multiarch scheme
[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 <vppinfra/error.h>
27 #include <nat/nat.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_syslog.h>
32
33 #define foreach_nat_in2out_ed_error                     \
34 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
35 _(IN2OUT_PACKETS, "good in2out packets processed")      \
36 _(OUT_OF_PORTS, "out of ports")                         \
37 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
38 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
39 _(DROP_FRAGMENT, "drop fragment")                       \
40 _(MAX_REASS, "maximum reassemblies exceeded")           \
41 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
42 _(NON_SYN, "non-SYN packet try to create session")      \
43 _(TCP_PACKETS, "TCP packets")                           \
44 _(UDP_PACKETS, "UDP packets")                           \
45 _(ICMP_PACKETS, "ICMP packets")                         \
46 _(OTHER_PACKETS, "other protocol packets")              \
47 _(FRAGMENTS, "fragments")                               \
48 _(CACHED_FRAGMENTS, "cached fragments")                 \
49 _(PROCESSED_FRAGMENTS, "processed fragments")
50
51
52 typedef enum
53 {
54 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
55   foreach_nat_in2out_ed_error
56 #undef _
57     NAT_IN2OUT_ED_N_ERROR,
58 } nat_in2out_ed_error_t;
59
60 static char *nat_in2out_ed_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_nat_in2out_ed_error
63 #undef _
64 };
65
66 typedef enum
67 {
68   NAT_IN2OUT_ED_NEXT_LOOKUP,
69   NAT_IN2OUT_ED_NEXT_DROP,
70   NAT_IN2OUT_ED_NEXT_ICMP_ERROR,
71   NAT_IN2OUT_ED_NEXT_SLOW_PATH,
72   NAT_IN2OUT_ED_NEXT_REASS,
73   NAT_IN2OUT_ED_N_NEXT,
74 } nat_in2out_ed_next_t;
75
76 typedef struct
77 {
78   u32 sw_if_index;
79   u32 next_index;
80   u32 session_index;
81   u32 is_slow_path;
82 } nat_in2out_ed_trace_t;
83
84 static u8 *
85 format_nat_in2out_ed_trace (u8 * s, va_list * args)
86 {
87   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
88   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
89   nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
90   char *tag;
91
92   tag =
93     t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
94     "NAT44_IN2OUT_ED_FAST_PATH";
95
96   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
97               t->sw_if_index, t->next_index, t->session_index);
98
99   return s;
100 }
101
102 static_always_inline int
103 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
104 {
105   icmp46_header_t *icmp0;
106   nat_ed_ses_key_t key0;
107   icmp_echo_header_t *echo0, *inner_echo0 = 0;
108   ip4_header_t *inner_ip0 = 0;
109   void *l4_header = 0;
110   icmp46_header_t *inner_icmp0;
111
112   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
113   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
114
115   if (!icmp_is_error_message (icmp0))
116     {
117       key0.proto = IP_PROTOCOL_ICMP;
118       key0.l_addr = ip0->src_address;
119       key0.r_addr = ip0->dst_address;
120       key0.l_port = echo0->identifier;
121       key0.r_port = 0;
122     }
123   else
124     {
125       inner_ip0 = (ip4_header_t *) (echo0 + 1);
126       l4_header = ip4_next_header (inner_ip0);
127       key0.proto = inner_ip0->protocol;
128       key0.r_addr = inner_ip0->src_address;
129       key0.l_addr = inner_ip0->dst_address;
130       switch (ip_proto_to_snat_proto (inner_ip0->protocol))
131         {
132         case SNAT_PROTOCOL_ICMP:
133           inner_icmp0 = (icmp46_header_t *) l4_header;
134           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
135           key0.r_port = 0;
136           key0.l_port = inner_echo0->identifier;
137           break;
138         case SNAT_PROTOCOL_UDP:
139         case SNAT_PROTOCOL_TCP:
140           key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
141           key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
142           break;
143         default:
144           return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
145         }
146     }
147   *p_key0 = key0;
148   return 0;
149 }
150
151 #ifndef CLIB_MARCH_VARIANT
152 int
153 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
154 {
155   snat_main_t *sm = &snat_main;
156   nat44_is_idle_session_ctx_t *ctx = arg;
157   snat_session_t *s;
158   u64 sess_timeout_time;
159   nat_ed_ses_key_t ed_key;
160   clib_bihash_kv_16_8_t ed_kv;
161   int i;
162   snat_address_t *a;
163   snat_session_key_t key;
164   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
165                                                        ctx->thread_index);
166
167   s = pool_elt_at_index (tsm->sessions, kv->value);
168   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
169   if (ctx->now >= sess_timeout_time)
170     {
171       if (is_fwd_bypass_session (s))
172         goto delete;
173
174       ed_key.l_addr = s->out2in.addr;
175       ed_key.r_addr = s->ext_host_addr;
176       ed_key.fib_index = s->out2in.fib_index;
177       if (snat_is_unk_proto_session (s))
178         {
179           ed_key.proto = s->in2out.port;
180           ed_key.r_port = 0;
181           ed_key.l_port = 0;
182         }
183       else
184         {
185           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
186           ed_key.l_port = s->out2in.port;
187           ed_key.r_port = s->ext_host_port;
188         }
189       ed_kv.key[0] = ed_key.as_u64[0];
190       ed_kv.key[1] = ed_key.as_u64[1];
191       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
192         nat_log_warn ("out2in_ed key del failed");
193
194       if (snat_is_unk_proto_session (s))
195         goto delete;
196
197       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
198                                            s->in2out.addr.as_u32,
199                                            s->out2in.addr.as_u32,
200                                            s->in2out.protocol,
201                                            s->in2out.port,
202                                            s->out2in.port,
203                                            s->in2out.fib_index);
204
205       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
206                              &s->in2out.addr, s->in2out.port,
207                              &s->ext_host_nat_addr, s->ext_host_nat_port,
208                              &s->out2in.addr, s->out2in.port,
209                              &s->ext_host_addr, s->ext_host_port,
210                              s->in2out.protocol, is_twice_nat_session (s));
211
212       if (is_twice_nat_session (s))
213         {
214           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
215             {
216               key.protocol = s->in2out.protocol;
217               key.port = s->ext_host_nat_port;
218               a = sm->twice_nat_addresses + i;
219               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
220                 {
221                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
222                                                       ctx->thread_index,
223                                                       &key);
224                   break;
225                 }
226             }
227         }
228
229       if (snat_is_session_static (s))
230         goto delete;
231
232       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
233                                           &s->out2in);
234     delete:
235       nat44_delete_session (sm, s, ctx->thread_index);
236       return 1;
237     }
238
239   return 0;
240 }
241 #endif
242
243 static inline u32
244 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
245                           ip4_header_t * ip0, icmp46_header_t * icmp0,
246                           u32 sw_if_index0, u32 rx_fib_index0,
247                           vlib_node_runtime_t * node, u32 next0, f64 now,
248                           u32 thread_index, snat_session_t ** p_s0)
249 {
250   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
251                        next0, thread_index, p_s0, 0);
252   snat_session_t *s0 = *p_s0;
253   if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP && s0))
254     {
255       /* Accounting */
256       nat44_session_update_counters (s0, now,
257                                      vlib_buffer_length_in_chain
258                                      (sm->vlib_main, b0));
259       /* Per-user LRU list maintenance */
260       nat44_session_update_lru (sm, s0, thread_index);
261     }
262   return next0;
263 }
264
265 static u32
266 slow_path_ed (snat_main_t * sm,
267               vlib_buffer_t * b,
268               u32 rx_fib_index,
269               clib_bihash_kv_16_8_t * kv,
270               snat_session_t ** sessionp,
271               vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now,
272               tcp_header_t * tcp)
273 {
274   snat_session_t *s = 0;
275   snat_user_t *u;
276   snat_session_key_t key0, key1;
277   lb_nat_type_t lb = 0, is_sm = 0;
278   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
279   nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
280   u32 proto = ip_proto_to_snat_proto (key->proto);
281   nat_outside_fib_t *outside_fib;
282   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
283   u8 identity_nat;
284   fib_prefix_t pfx = {
285     .fp_proto = FIB_PROTOCOL_IP4,
286     .fp_len = 32,
287     .fp_addr = {
288                 .ip4.as_u32 = key->r_addr.as_u32,
289                 },
290   };
291   nat44_is_idle_session_ctx_t ctx;
292
293   if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
294     {
295       b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
296       nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
297       nat_log_notice ("maximum sessions exceeded");
298       return NAT_IN2OUT_ED_NEXT_DROP;
299     }
300
301   key0.addr = key->l_addr;
302   key0.port = key->l_port;
303   key1.protocol = key0.protocol = proto;
304   key0.fib_index = rx_fib_index;
305   key1.fib_index = sm->outside_fib_index;
306   /* First try to match static mapping by local address and port */
307   if (snat_static_mapping_match
308       (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
309     {
310       /* Try to create dynamic translation */
311       if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
312                                                thread_index, &key1,
313                                                sm->port_per_thread,
314                                                tsm->snat_thread_index))
315         {
316           nat_log_notice ("addresses exhausted");
317           b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
318           return NAT_IN2OUT_ED_NEXT_DROP;
319         }
320     }
321   else
322     {
323       if (PREDICT_FALSE (identity_nat))
324         {
325           *sessionp = s;
326           return next;
327         }
328
329       is_sm = 1;
330     }
331
332   if (proto == SNAT_PROTOCOL_TCP)
333     {
334       if (!tcp_is_init (tcp))
335         {
336           b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
337           return NAT_IN2OUT_ED_NEXT_DROP;
338         }
339     }
340
341   u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
342   if (!u)
343     {
344       nat_log_warn ("create NAT user failed");
345       if (!is_sm)
346         snat_free_outside_address_and_port (sm->addresses,
347                                             thread_index, &key1);
348       return NAT_IN2OUT_ED_NEXT_DROP;
349     }
350
351   s = nat_ed_session_alloc (sm, u, thread_index, now);
352   if (!s)
353     {
354       nat44_delete_user_with_no_session (sm, u, thread_index);
355       nat_log_warn ("create NAT session failed");
356       if (!is_sm)
357         snat_free_outside_address_and_port (sm->addresses,
358                                             thread_index, &key1);
359       return NAT_IN2OUT_ED_NEXT_DROP;
360     }
361
362   user_session_increment (sm, u, is_sm);
363   if (is_sm)
364     s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
365   if (lb)
366     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
367   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
368   s->ext_host_addr = key->r_addr;
369   s->ext_host_port = key->r_port;
370   s->in2out = key0;
371   s->out2in = key1;
372   s->out2in.protocol = key0.protocol;
373
374   switch (vec_len (sm->outside_fibs))
375     {
376     case 0:
377       s->out2in.fib_index = sm->outside_fib_index;
378       break;
379     case 1:
380       s->out2in.fib_index = sm->outside_fibs[0].fib_index;
381       break;
382     default:
383       /* *INDENT-OFF* */
384       vec_foreach (outside_fib, sm->outside_fibs)
385        {
386           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
387           if (FIB_NODE_INDEX_INVALID != fei)
388             {
389               if (fib_entry_get_resolving_interface (fei) != ~0)
390                 {
391                   s->out2in.fib_index = outside_fib->fib_index;
392                   break;
393                 }
394             }
395         }
396       /* *INDENT-ON* */
397       break;
398     }
399
400   /* Add to lookup tables */
401   kv->value = s - tsm->sessions;
402   ctx.now = now;
403   ctx.thread_index = thread_index;
404   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
405                                                nat44_i2o_ed_is_idle_session_cb,
406                                                &ctx))
407     nat_log_notice ("in2out-ed key add failed");
408
409   make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
410               key1.port, key->r_port);
411   kv->value = s - tsm->sessions;
412   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
413                                                nat44_o2i_ed_is_idle_session_cb,
414                                                &ctx))
415     nat_log_notice ("out2in-ed key add failed");
416
417   *sessionp = s;
418
419   /* log NAT event */
420   snat_ipfix_logging_nat44_ses_create (thread_index,
421                                        s->in2out.addr.as_u32,
422                                        s->out2in.addr.as_u32,
423                                        s->in2out.protocol,
424                                        s->in2out.port,
425                                        s->out2in.port, s->in2out.fib_index);
426
427   nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
428                          &s->in2out.addr, s->in2out.port,
429                          &s->ext_host_nat_addr, s->ext_host_nat_port,
430                          &s->out2in.addr, s->out2in.port,
431                          &s->ext_host_addr, s->ext_host_port,
432                          s->in2out.protocol, 0);
433
434   return next;
435 }
436
437 static_always_inline int
438 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
439                         u32 sw_if_index, ip4_header_t * ip, u32 proto,
440                         u32 rx_fib_index, u32 thread_index)
441 {
442   udp_header_t *udp = ip4_next_header (ip);
443   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
444   clib_bihash_kv_16_8_t kv, value;
445   snat_session_key_t key0, key1;
446
447   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
448               sm->outside_fib_index, udp->dst_port, udp->src_port);
449
450   /* NAT packet aimed at external address if */
451   /* has active sessions */
452   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
453     {
454       key0.addr = ip->dst_address;
455       key0.port = udp->dst_port;
456       key0.protocol = proto;
457       key0.fib_index = sm->outside_fib_index;
458       /* or is static mappings */
459       if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
460         return 0;
461     }
462   else
463     return 0;
464
465   if (sm->forwarding_enabled)
466     return 1;
467
468   return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
469                                   rx_fib_index);
470 }
471
472 static_always_inline int
473 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
474                                       u32 thread_index, f64 now,
475                                       vlib_main_t * vm, vlib_buffer_t * b)
476 {
477   nat_ed_ses_key_t key;
478   clib_bihash_kv_16_8_t kv, value;
479   udp_header_t *udp;
480   snat_session_t *s = 0;
481   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
482
483   if (!sm->forwarding_enabled)
484     return 0;
485
486   if (ip->protocol == IP_PROTOCOL_ICMP)
487     {
488       key.as_u64[0] = key.as_u64[1] = 0;
489       if (icmp_get_ed_key (ip, &key))
490         return 0;
491       key.fib_index = 0;
492       kv.key[0] = key.as_u64[0];
493       kv.key[1] = key.as_u64[1];
494     }
495   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
496     {
497       udp = ip4_next_header (ip);
498       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
499                   udp->src_port, udp->dst_port);
500     }
501   else
502     {
503       make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
504                   0);
505     }
506
507   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
508     {
509       s = pool_elt_at_index (tsm->sessions, value.value);
510       if (is_fwd_bypass_session (s))
511         {
512           if (ip->protocol == IP_PROTOCOL_TCP)
513             {
514               tcp_header_t *tcp = ip4_next_header (ip);
515               if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
516                 return 1;
517             }
518           /* Accounting */
519           nat44_session_update_counters (s, now,
520                                          vlib_buffer_length_in_chain (vm, b));
521           /* Per-user LRU list maintenance */
522           nat44_session_update_lru (sm, s, thread_index);
523           return 1;
524         }
525       else
526         return 0;
527     }
528
529   return 0;
530 }
531
532 static_always_inline int
533 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
534                                        u8 proto, u16 src_port, u16 dst_port,
535                                        u32 thread_index, u32 rx_sw_if_index,
536                                        u32 tx_sw_if_index)
537 {
538   clib_bihash_kv_16_8_t kv, value;
539   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
540   snat_interface_t *i;
541   snat_session_t *s;
542   u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
543   u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
544
545   /* src NAT check */
546   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
547               src_port, dst_port);
548   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
549     {
550       s = pool_elt_at_index (tsm->sessions, value.value);
551       if (nat44_is_ses_closed (s))
552         {
553           nat_log_debug ("TCP close connection %U", format_snat_session,
554                          &sm->per_thread_data[thread_index], s);
555           nat_free_session_data (sm, s, thread_index);
556           nat44_delete_session (sm, s, thread_index);
557         }
558       else
559         s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
560       return 1;
561     }
562
563   /* dst NAT check */
564   make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
565               dst_port, src_port);
566   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
567     {
568       s = pool_elt_at_index (tsm->sessions, value.value);
569       if (is_fwd_bypass_session (s))
570         return 0;
571
572       /* hairpinning */
573       /* *INDENT-OFF* */
574       pool_foreach (i, sm->output_feature_interfaces,
575       ({
576         if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
577            return 0;
578       }));
579       /* *INDENT-ON* */
580       return 1;
581     }
582
583   return 0;
584 }
585
586 #ifndef CLIB_MARCH_VARIANT
587 u32
588 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
589                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
590                       u8 * p_proto, snat_session_key_t * p_value,
591                       u8 * p_dont_translate, void *d, void *e)
592 {
593   icmp46_header_t *icmp;
594   u32 sw_if_index;
595   u32 rx_fib_index;
596   nat_ed_ses_key_t key;
597   snat_session_t *s = 0;
598   u8 dont_translate = 0;
599   clib_bihash_kv_16_8_t kv, value;
600   u32 next = ~0;
601   int err;
602   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
603
604   icmp = (icmp46_header_t *) ip4_next_header (ip);
605   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
606   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
607
608   key.as_u64[0] = key.as_u64[1] = 0;
609   err = icmp_get_ed_key (ip, &key);
610   if (err != 0)
611     {
612       b->error = node->errors[err];
613       next = NAT_IN2OUT_ED_NEXT_DROP;
614       goto out;
615     }
616   key.fib_index = rx_fib_index;
617
618   kv.key[0] = key.as_u64[0];
619   kv.key[1] = key.as_u64[1];
620
621   if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
622     {
623       if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
624         {
625           if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
626                                                                     key.proto,
627                                                                     key.
628                                                                     l_port,
629                                                                     key.
630                                                                     r_port,
631                                                                     thread_index,
632                                                                     sw_if_index,
633                                                                     vnet_buffer
634                                                                     (b)->
635                                                                     sw_if_index
636                                                                     [VLIB_TX])))
637             {
638               dont_translate = 1;
639               goto out;
640             }
641         }
642       else
643         {
644           if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
645                                                      ip, SNAT_PROTOCOL_ICMP,
646                                                      rx_fib_index,
647                                                      thread_index)))
648             {
649               dont_translate = 1;
650               goto out;
651             }
652         }
653
654       if (PREDICT_FALSE (icmp_is_error_message (icmp)))
655         {
656           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
657           next = NAT_IN2OUT_ED_NEXT_DROP;
658           goto out;
659         }
660
661       next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
662                            thread_index, vlib_time_now (sm->vlib_main), 0);
663
664       if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
665         goto out;
666
667       if (!s)
668         {
669           dont_translate = 1;
670           goto out;
671         }
672     }
673   else
674     {
675       if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
676                          icmp->type != ICMP4_echo_reply &&
677                          !icmp_is_error_message (icmp)))
678         {
679           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
680           next = NAT_IN2OUT_ED_NEXT_DROP;
681           goto out;
682         }
683
684       s = pool_elt_at_index (tsm->sessions, value.value);
685     }
686
687   *p_proto = ip_proto_to_snat_proto (key.proto);
688 out:
689   if (s)
690     *p_value = s->out2in;
691   *p_dont_translate = dont_translate;
692   if (d)
693     *(snat_session_t **) d = s;
694   return next;
695 }
696 #endif
697
698 static snat_session_t *
699 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
700                                vlib_buffer_t * b,
701                                ip4_header_t * ip,
702                                u32 rx_fib_index,
703                                u32 thread_index,
704                                f64 now,
705                                vlib_main_t * vm, vlib_node_runtime_t * node)
706 {
707   clib_bihash_kv_8_8_t kv, value;
708   clib_bihash_kv_16_8_t s_kv, s_value;
709   snat_static_mapping_t *m;
710   u32 old_addr, new_addr = 0;
711   ip_csum_t sum;
712   snat_user_t *u;
713   dlist_elt_t *head, *elt;
714   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
715   u32 elt_index, head_index, ses_index;
716   snat_session_t *s;
717   u32 outside_fib_index = sm->outside_fib_index;
718   int i;
719   u8 is_sm = 0;
720   nat_outside_fib_t *outside_fib;
721   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
722   fib_prefix_t pfx = {
723     .fp_proto = FIB_PROTOCOL_IP4,
724     .fp_len = 32,
725     .fp_addr = {
726                 .ip4.as_u32 = ip->dst_address.as_u32,
727                 },
728   };
729
730   switch (vec_len (sm->outside_fibs))
731     {
732     case 0:
733       outside_fib_index = sm->outside_fib_index;
734       break;
735     case 1:
736       outside_fib_index = sm->outside_fibs[0].fib_index;
737       break;
738     default:
739       /* *INDENT-OFF* */
740       vec_foreach (outside_fib, sm->outside_fibs)
741         {
742           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
743           if (FIB_NODE_INDEX_INVALID != fei)
744             {
745               if (fib_entry_get_resolving_interface (fei) != ~0)
746                 {
747                   outside_fib_index = outside_fib->fib_index;
748                   break;
749                 }
750             }
751         }
752       /* *INDENT-ON* */
753       break;
754     }
755   old_addr = ip->src_address.as_u32;
756
757   make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
758               rx_fib_index, 0, 0);
759
760   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
761     {
762       s = pool_elt_at_index (tsm->sessions, s_value.value);
763       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
764     }
765   else
766     {
767       if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
768         {
769           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
770           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
771           nat_log_notice ("maximum sessions exceeded");
772           return 0;
773         }
774
775       u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
776                                   thread_index);
777       if (!u)
778         {
779           nat_log_warn ("create NAT user failed");
780           return 0;
781         }
782
783       make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
784
785       /* Try to find static mapping first */
786       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
787         {
788           m = pool_elt_at_index (sm->static_mappings, value.value);
789           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
790           is_sm = 1;
791           goto create_ses;
792         }
793       /* Fallback to 3-tuple key */
794       else
795         {
796           /* Choose same out address as for TCP/UDP session to same destination */
797           head_index = u->sessions_per_user_list_head_index;
798           head = pool_elt_at_index (tsm->list_pool, head_index);
799           elt_index = head->next;
800           if (PREDICT_FALSE (elt_index == ~0))
801             ses_index = ~0;
802           else
803             {
804               elt = pool_elt_at_index (tsm->list_pool, elt_index);
805               ses_index = elt->value;
806             }
807
808           while (ses_index != ~0)
809             {
810               s = pool_elt_at_index (tsm->sessions, ses_index);
811               elt_index = elt->next;
812               elt = pool_elt_at_index (tsm->list_pool, elt_index);
813               ses_index = elt->value;
814
815               if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
816                 {
817                   new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
818
819                   make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
820                               ip->protocol, outside_fib_index, 0, 0);
821                   if (clib_bihash_search_16_8
822                       (&tsm->out2in_ed, &s_kv, &s_value))
823                     goto create_ses;
824
825                   break;
826                 }
827             }
828
829           for (i = 0; i < vec_len (sm->addresses); i++)
830             {
831               make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
832                           ip->protocol, outside_fib_index, 0, 0);
833               if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
834                 {
835                   new_addr = ip->src_address.as_u32 =
836                     sm->addresses[i].addr.as_u32;
837                   goto create_ses;
838                 }
839             }
840           return 0;
841         }
842
843     create_ses:
844       s = nat_ed_session_alloc (sm, u, thread_index, now);
845       if (!s)
846         {
847           nat44_delete_user_with_no_session (sm, u, thread_index);
848           nat_log_warn ("create NAT session failed");
849           return 0;
850         }
851
852       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
853       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
854       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
855       s->out2in.addr.as_u32 = new_addr;
856       s->out2in.fib_index = outside_fib_index;
857       s->in2out.addr.as_u32 = old_addr;
858       s->in2out.fib_index = rx_fib_index;
859       s->in2out.port = s->out2in.port = ip->protocol;
860       if (is_sm)
861         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
862       user_session_increment (sm, u, is_sm);
863
864       /* Add to lookup tables */
865       make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
866                   rx_fib_index, 0, 0);
867       s_kv.value = s - tsm->sessions;
868       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
869         nat_log_notice ("in2out key add failed");
870
871       make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
872                   outside_fib_index, 0, 0);
873       s_kv.value = s - tsm->sessions;
874       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
875         nat_log_notice ("out2in key add failed");
876     }
877
878   /* Update IP checksum */
879   sum = ip->checksum;
880   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
881   ip->checksum = ip_csum_fold (sum);
882
883   /* Accounting */
884   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
885   /* Per-user LRU list maintenance */
886   nat44_session_update_lru (sm, s, thread_index);
887
888   /* Hairpinning */
889   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
890     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
891
892   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
893     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
894
895   return s;
896 }
897
898 static inline uword
899 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
900                                 vlib_node_runtime_t * node,
901                                 vlib_frame_t * frame, int is_slow_path,
902                                 int is_output_feature)
903 {
904   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
905   nat_in2out_ed_next_t next_index;
906   snat_main_t *sm = &snat_main;
907   f64 now = vlib_time_now (vm);
908   u32 thread_index = vm->thread_index;
909   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
910   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
911     0, fragments = 0;
912
913   stats_node_index = is_slow_path ? sm->ed_in2out_slowpath_node_index :
914     sm->ed_in2out_node_index;
915
916   from = vlib_frame_vector_args (frame);
917   n_left_from = frame->n_vectors;
918   next_index = node->cached_next_index;
919
920   while (n_left_from > 0)
921     {
922       u32 n_left_to_next;
923
924       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
925
926       while (n_left_from >= 4 && n_left_to_next >= 2)
927         {
928           u32 bi0, bi1;
929           vlib_buffer_t *b0, *b1;
930           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
931             new_addr0, old_addr0;
932           u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
933             new_addr1, old_addr1;
934           u16 old_port0, new_port0, old_port1, new_port1;
935           ip4_header_t *ip0, *ip1;
936           udp_header_t *udp0, *udp1;
937           tcp_header_t *tcp0, *tcp1;
938           icmp46_header_t *icmp0, *icmp1;
939           snat_session_t *s0 = 0, *s1 = 0;
940           clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
941           ip_csum_t sum0, sum1;
942
943           /* Prefetch next iteration. */
944           {
945             vlib_buffer_t *p2, *p3;
946
947             p2 = vlib_get_buffer (vm, from[2]);
948             p3 = vlib_get_buffer (vm, from[3]);
949
950             vlib_prefetch_buffer_header (p2, LOAD);
951             vlib_prefetch_buffer_header (p3, LOAD);
952
953             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
954             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
955           }
956
957           /* speculatively enqueue b0 and b1 to the current next frame */
958           to_next[0] = bi0 = from[0];
959           to_next[1] = bi1 = from[1];
960           from += 2;
961           to_next += 2;
962           n_left_from -= 2;
963           n_left_to_next -= 2;
964
965           b0 = vlib_get_buffer (vm, bi0);
966           b1 = vlib_get_buffer (vm, bi1);
967
968           next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
969
970           if (is_output_feature)
971             iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
972
973           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
974                                   iph_offset0);
975
976           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
977           rx_fib_index0 =
978             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
979                                                  sw_if_index0);
980
981           if (PREDICT_FALSE (ip0->ttl == 1))
982             {
983               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
984               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
985                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
986                                            0);
987               next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
988               goto trace00;
989             }
990
991           udp0 = ip4_next_header (ip0);
992           tcp0 = (tcp_header_t *) udp0;
993           icmp0 = (icmp46_header_t *) udp0;
994           proto0 = ip_proto_to_snat_proto (ip0->protocol);
995
996           if (is_slow_path)
997             {
998               if (PREDICT_FALSE (proto0 == ~0))
999                 {
1000                   s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1001                                                       rx_fib_index0,
1002                                                       thread_index, now, vm,
1003                                                       node);
1004                   if (!s0)
1005                     next0 = NAT_IN2OUT_ED_NEXT_DROP;
1006                   other_packets++;
1007                   goto trace00;
1008                 }
1009
1010               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1011                 {
1012                   next0 = icmp_in2out_ed_slow_path
1013                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1014                      next0, now, thread_index, &s0);
1015                   icmp_packets++;
1016                   goto trace00;
1017                 }
1018             }
1019           else
1020             {
1021               if (PREDICT_FALSE (proto0 == ~0))
1022                 {
1023                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1024                   goto trace00;
1025                 }
1026
1027               if (ip4_is_fragment (ip0))
1028                 {
1029                   next0 = NAT_IN2OUT_ED_NEXT_REASS;
1030                   fragments++;
1031                   goto trace00;
1032                 }
1033
1034               if (is_output_feature)
1035                 {
1036                   if (PREDICT_FALSE
1037                       (nat_not_translate_output_feature_fwd
1038                        (sm, ip0, thread_index, now, vm, b0)))
1039                     goto trace00;
1040                 }
1041
1042               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1043                 {
1044                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1045                   goto trace00;
1046                 }
1047             }
1048
1049           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1050                       ip0->protocol, rx_fib_index0, udp0->src_port,
1051                       udp0->dst_port);
1052
1053           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1054             {
1055               if (is_slow_path)
1056                 {
1057                   if (is_output_feature)
1058                     {
1059                       if (PREDICT_FALSE
1060                           (nat44_ed_not_translate_output_feature
1061                            (sm, ip0, ip0->protocol, udp0->src_port,
1062                             udp0->dst_port, thread_index, sw_if_index0,
1063                             vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1064                         goto trace00;
1065                     }
1066                   else
1067                     {
1068                       if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1069                                                                  sw_if_index0,
1070                                                                  ip0, proto0,
1071                                                                  rx_fib_index0,
1072                                                                  thread_index)))
1073                         goto trace00;
1074                     }
1075
1076                   next0 =
1077                     slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1078                                   next0, thread_index, now, tcp0);
1079
1080                   if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1081                     goto trace00;
1082
1083                   if (PREDICT_FALSE (!s0))
1084                     goto trace00;
1085                 }
1086               else
1087                 {
1088                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1089                   goto trace00;
1090                 }
1091             }
1092           else
1093             {
1094               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1095             }
1096
1097           b0->flags |= VNET_BUFFER_F_IS_NATED;
1098
1099           if (!is_output_feature)
1100             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1101
1102           old_addr0 = ip0->src_address.as_u32;
1103           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1104           sum0 = ip0->checksum;
1105           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1106                                  src_address);
1107           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1108             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1109                                    s0->ext_host_addr.as_u32, ip4_header_t,
1110                                    dst_address);
1111           ip0->checksum = ip_csum_fold (sum0);
1112
1113           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1114             {
1115               old_port0 = tcp0->src_port;
1116               new_port0 = tcp0->src_port = s0->out2in.port;
1117
1118               sum0 = tcp0->checksum;
1119               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1120                                      dst_address);
1121               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1122                                      length);
1123               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1124                 {
1125                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1126                                          s0->ext_host_addr.as_u32,
1127                                          ip4_header_t, dst_address);
1128                   sum0 = ip_csum_update (sum0, tcp0->dst_port,
1129                                          s0->ext_host_port, ip4_header_t,
1130                                          length);
1131                   tcp0->dst_port = s0->ext_host_port;
1132                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1133                 }
1134               mss_clamping (sm, tcp0, &sum0);
1135               tcp0->checksum = ip_csum_fold (sum0);
1136               tcp_packets++;
1137               if (nat44_set_tcp_session_state_i2o
1138                   (sm, s0, tcp0, thread_index))
1139                 goto trace00;
1140             }
1141           else
1142             {
1143               udp0->src_port = s0->out2in.port;
1144               udp0->checksum = 0;
1145               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1146                 {
1147                   udp0->dst_port = s0->ext_host_port;
1148                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1149                 }
1150               udp_packets++;
1151             }
1152
1153           /* Accounting */
1154           nat44_session_update_counters (s0, now,
1155                                          vlib_buffer_length_in_chain (vm,
1156                                                                       b0));
1157           /* Per-user LRU list maintenance */
1158           nat44_session_update_lru (sm, s0, thread_index);
1159
1160         trace00:
1161           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1162                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1163             {
1164               nat_in2out_ed_trace_t *t =
1165                 vlib_add_trace (vm, node, b0, sizeof (*t));
1166               t->is_slow_path = is_slow_path;
1167               t->sw_if_index = sw_if_index0;
1168               t->next_index = next0;
1169               t->session_index = ~0;
1170               if (s0)
1171                 t->session_index = s0 - tsm->sessions;
1172             }
1173
1174           pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1175
1176
1177           next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1178
1179           if (is_output_feature)
1180             iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1181
1182           ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1183                                   iph_offset1);
1184
1185           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1186           rx_fib_index1 =
1187             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1188                                                  sw_if_index1);
1189
1190           if (PREDICT_FALSE (ip1->ttl == 1))
1191             {
1192               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1193               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1194                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1195                                            0);
1196               next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1197               goto trace01;
1198             }
1199
1200           udp1 = ip4_next_header (ip1);
1201           tcp1 = (tcp_header_t *) udp1;
1202           icmp1 = (icmp46_header_t *) udp1;
1203           proto1 = ip_proto_to_snat_proto (ip1->protocol);
1204
1205           if (is_slow_path)
1206             {
1207               if (PREDICT_FALSE (proto1 == ~0))
1208                 {
1209                   s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1210                                                       rx_fib_index1,
1211                                                       thread_index, now, vm,
1212                                                       node);
1213                   if (!s1)
1214                     next1 = NAT_IN2OUT_ED_NEXT_DROP;
1215                   other_packets++;
1216                   goto trace01;
1217                 }
1218
1219               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1220                 {
1221                   next1 = icmp_in2out_ed_slow_path
1222                     (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1223                      next1, now, thread_index, &s1);
1224                   icmp_packets++;
1225                   goto trace01;
1226                 }
1227             }
1228           else
1229             {
1230               if (PREDICT_FALSE (proto1 == ~0))
1231                 {
1232                   next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1233                   goto trace01;
1234                 }
1235
1236               if (ip4_is_fragment (ip1))
1237                 {
1238                   next1 = NAT_IN2OUT_ED_NEXT_REASS;
1239                   fragments++;
1240                   goto trace01;
1241                 }
1242
1243               if (is_output_feature)
1244                 {
1245                   if (PREDICT_FALSE
1246                       (nat_not_translate_output_feature_fwd
1247                        (sm, ip1, thread_index, now, vm, b1)))
1248                     goto trace01;
1249                 }
1250
1251               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1252                 {
1253                   next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1254                   goto trace01;
1255                 }
1256             }
1257
1258           make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1259                       ip1->protocol, rx_fib_index1, udp1->src_port,
1260                       udp1->dst_port);
1261
1262           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1263             {
1264               if (is_slow_path)
1265                 {
1266                   if (is_output_feature)
1267                     {
1268                       if (PREDICT_FALSE
1269                           (nat44_ed_not_translate_output_feature
1270                            (sm, ip1, ip1->protocol, udp1->src_port,
1271                             udp1->dst_port, thread_index, sw_if_index1,
1272                             vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1273                         goto trace01;
1274                     }
1275                   else
1276                     {
1277                       if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1278                                                                  sw_if_index1,
1279                                                                  ip1, proto1,
1280                                                                  rx_fib_index1,
1281                                                                  thread_index)))
1282                         goto trace01;
1283                     }
1284
1285                   next1 =
1286                     slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1287                                   next1, thread_index, now, tcp1);
1288
1289                   if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1290                     goto trace01;
1291
1292                   if (PREDICT_FALSE (!s1))
1293                     goto trace01;
1294                 }
1295               else
1296                 {
1297                   next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1298                   goto trace01;
1299                 }
1300             }
1301           else
1302             {
1303               s1 = pool_elt_at_index (tsm->sessions, value1.value);
1304             }
1305
1306           b1->flags |= VNET_BUFFER_F_IS_NATED;
1307
1308           if (!is_output_feature)
1309             vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1310
1311           old_addr1 = ip1->src_address.as_u32;
1312           new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1313           sum1 = ip1->checksum;
1314           sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1315                                  src_address);
1316           if (PREDICT_FALSE (is_twice_nat_session (s1)))
1317             sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1318                                    s1->ext_host_addr.as_u32, ip4_header_t,
1319                                    dst_address);
1320           ip1->checksum = ip_csum_fold (sum1);
1321
1322           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1323             {
1324               old_port1 = tcp1->src_port;
1325               new_port1 = tcp1->src_port = s1->out2in.port;
1326
1327               sum1 = tcp1->checksum;
1328               sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1329                                      dst_address);
1330               sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1331                                      length);
1332               if (PREDICT_FALSE (is_twice_nat_session (s1)))
1333                 {
1334                   sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1335                                          s1->ext_host_addr.as_u32,
1336                                          ip4_header_t, dst_address);
1337                   sum1 = ip_csum_update (sum1, tcp1->dst_port,
1338                                          s1->ext_host_port, ip4_header_t,
1339                                          length);
1340                   tcp1->dst_port = s1->ext_host_port;
1341                   ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1342                 }
1343               tcp1->checksum = ip_csum_fold (sum1);
1344               mss_clamping (sm, tcp1, &sum1);
1345               tcp_packets++;
1346               if (nat44_set_tcp_session_state_i2o
1347                   (sm, s1, tcp1, thread_index))
1348                 goto trace01;
1349             }
1350           else
1351             {
1352               udp1->src_port = s1->out2in.port;
1353               udp1->checksum = 0;
1354               if (PREDICT_FALSE (is_twice_nat_session (s1)))
1355                 {
1356                   udp1->dst_port = s1->ext_host_port;
1357                   ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1358                 }
1359               udp_packets++;
1360             }
1361
1362           /* Accounting */
1363           nat44_session_update_counters (s1, now,
1364                                          vlib_buffer_length_in_chain (vm,
1365                                                                       b1));
1366           /* Per-user LRU list maintenance */
1367           nat44_session_update_lru (sm, s1, thread_index);
1368
1369         trace01:
1370           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1371                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1372             {
1373               nat_in2out_ed_trace_t *t =
1374                 vlib_add_trace (vm, node, b1, sizeof (*t));
1375               t->is_slow_path = is_slow_path;
1376               t->sw_if_index = sw_if_index1;
1377               t->next_index = next1;
1378               t->session_index = ~0;
1379               if (s1)
1380                 t->session_index = s1 - tsm->sessions;
1381             }
1382
1383           pkts_processed += next1 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1384
1385           /* verify speculative enqueues, maybe switch current next frame */
1386           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1387                                            to_next, n_left_to_next,
1388                                            bi0, bi1, next0, next1);
1389         }
1390
1391       while (n_left_from > 0 && n_left_to_next > 0)
1392         {
1393           u32 bi0;
1394           vlib_buffer_t *b0;
1395           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1396             new_addr0, old_addr0;
1397           u16 old_port0, new_port0;
1398           ip4_header_t *ip0;
1399           udp_header_t *udp0;
1400           tcp_header_t *tcp0;
1401           icmp46_header_t *icmp0;
1402           snat_session_t *s0 = 0;
1403           clib_bihash_kv_16_8_t kv0, value0;
1404           ip_csum_t sum0;
1405
1406           /* speculatively enqueue b0 to the current next frame */
1407           bi0 = from[0];
1408           to_next[0] = bi0;
1409           from += 1;
1410           to_next += 1;
1411           n_left_from -= 1;
1412           n_left_to_next -= 1;
1413
1414           b0 = vlib_get_buffer (vm, bi0);
1415           next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1416
1417           if (is_output_feature)
1418             iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1419
1420           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1421                                   iph_offset0);
1422
1423           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1424           rx_fib_index0 =
1425             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1426                                                  sw_if_index0);
1427
1428           if (PREDICT_FALSE (ip0->ttl == 1))
1429             {
1430               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1431               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1432                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1433                                            0);
1434               next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1435               goto trace0;
1436             }
1437
1438           udp0 = ip4_next_header (ip0);
1439           tcp0 = (tcp_header_t *) udp0;
1440           icmp0 = (icmp46_header_t *) udp0;
1441           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1442
1443           if (is_slow_path)
1444             {
1445               if (PREDICT_FALSE (proto0 == ~0))
1446                 {
1447                   s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1448                                                       rx_fib_index0,
1449                                                       thread_index, now, vm,
1450                                                       node);
1451                   if (!s0)
1452                     next0 = NAT_IN2OUT_ED_NEXT_DROP;
1453                   other_packets++;
1454                   goto trace0;
1455                 }
1456
1457               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1458                 {
1459                   next0 = icmp_in2out_ed_slow_path
1460                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1461                      next0, now, thread_index, &s0);
1462                   icmp_packets++;
1463                   goto trace0;
1464                 }
1465             }
1466           else
1467             {
1468               if (PREDICT_FALSE (proto0 == ~0))
1469                 {
1470                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1471                   goto trace0;
1472                 }
1473
1474               if (ip4_is_fragment (ip0))
1475                 {
1476                   next0 = NAT_IN2OUT_ED_NEXT_REASS;
1477                   fragments++;
1478                   goto trace0;
1479                 }
1480
1481               if (is_output_feature)
1482                 {
1483                   if (PREDICT_FALSE
1484                       (nat_not_translate_output_feature_fwd
1485                        (sm, ip0, thread_index, now, vm, b0)))
1486                     goto trace0;
1487                 }
1488
1489               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1490                 {
1491                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1492                   goto trace0;
1493                 }
1494             }
1495
1496           make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1497                       ip0->protocol, rx_fib_index0, udp0->src_port,
1498                       udp0->dst_port);
1499
1500           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1501             {
1502               if (is_slow_path)
1503                 {
1504                   if (is_output_feature)
1505                     {
1506                       if (PREDICT_FALSE
1507                           (nat44_ed_not_translate_output_feature
1508                            (sm, ip0, ip0->protocol, udp0->src_port,
1509                             udp0->dst_port, thread_index, sw_if_index0,
1510                             vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1511                         goto trace0;
1512                     }
1513                   else
1514                     {
1515                       if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1516                                                                  sw_if_index0,
1517                                                                  ip0, proto0,
1518                                                                  rx_fib_index0,
1519                                                                  thread_index)))
1520                         goto trace0;
1521                     }
1522
1523                   next0 =
1524                     slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1525                                   next0, thread_index, now, tcp0);
1526
1527                   if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1528                     goto trace0;
1529
1530                   if (PREDICT_FALSE (!s0))
1531                     goto trace0;
1532                 }
1533               else
1534                 {
1535                   next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1536                   goto trace0;
1537                 }
1538             }
1539           else
1540             {
1541               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1542             }
1543
1544           b0->flags |= VNET_BUFFER_F_IS_NATED;
1545
1546           if (!is_output_feature)
1547             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1548
1549           old_addr0 = ip0->src_address.as_u32;
1550           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1551           sum0 = ip0->checksum;
1552           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1553                                  src_address);
1554           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1555             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1556                                    s0->ext_host_addr.as_u32, ip4_header_t,
1557                                    dst_address);
1558           ip0->checksum = ip_csum_fold (sum0);
1559
1560           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1561             {
1562               old_port0 = tcp0->src_port;
1563               new_port0 = tcp0->src_port = s0->out2in.port;
1564
1565               sum0 = tcp0->checksum;
1566               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1567                                      dst_address);
1568               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1569                                      length);
1570               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1571                 {
1572                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1573                                          s0->ext_host_addr.as_u32,
1574                                          ip4_header_t, dst_address);
1575                   sum0 = ip_csum_update (sum0, tcp0->dst_port,
1576                                          s0->ext_host_port, ip4_header_t,
1577                                          length);
1578                   tcp0->dst_port = s0->ext_host_port;
1579                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1580                 }
1581               mss_clamping (sm, tcp0, &sum0);
1582               tcp0->checksum = ip_csum_fold (sum0);
1583               tcp_packets++;
1584               if (nat44_set_tcp_session_state_i2o
1585                   (sm, s0, tcp0, thread_index))
1586                 goto trace0;
1587             }
1588           else
1589             {
1590               udp0->src_port = s0->out2in.port;
1591               udp0->checksum = 0;
1592               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1593                 {
1594                   udp0->dst_port = s0->ext_host_port;
1595                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1596                 }
1597               udp_packets++;
1598             }
1599
1600           /* Accounting */
1601           nat44_session_update_counters (s0, now,
1602                                          vlib_buffer_length_in_chain (vm,
1603                                                                       b0));
1604           /* Per-user LRU list maintenance */
1605           nat44_session_update_lru (sm, s0, thread_index);
1606
1607         trace0:
1608           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1609                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1610             {
1611               nat_in2out_ed_trace_t *t =
1612                 vlib_add_trace (vm, node, b0, sizeof (*t));
1613               t->is_slow_path = is_slow_path;
1614               t->sw_if_index = sw_if_index0;
1615               t->next_index = next0;
1616               t->session_index = ~0;
1617               if (s0)
1618                 t->session_index = s0 - tsm->sessions;
1619             }
1620
1621           pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1622
1623           /* verify speculative enqueue, maybe switch current next frame */
1624           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1625                                            to_next, n_left_to_next,
1626                                            bi0, next0);
1627         }
1628
1629       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1630     }
1631
1632   vlib_node_increment_counter (vm, stats_node_index,
1633                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1634                                pkts_processed);
1635   vlib_node_increment_counter (vm, stats_node_index,
1636                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1637   vlib_node_increment_counter (vm, stats_node_index,
1638                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, tcp_packets);
1639   vlib_node_increment_counter (vm, stats_node_index,
1640                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1641                                icmp_packets);
1642   vlib_node_increment_counter (vm, stats_node_index,
1643                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1644                                other_packets);
1645   vlib_node_increment_counter (vm, stats_node_index,
1646                                NAT_IN2OUT_ED_ERROR_FRAGMENTS, fragments);
1647
1648   return frame->n_vectors;
1649 }
1650
1651 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1652                                      vlib_node_runtime_t * node,
1653                                      vlib_frame_t * frame)
1654 {
1655   return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1656 }
1657
1658 /* *INDENT-OFF* */
1659 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1660   .name = "nat44-ed-in2out",
1661   .vector_size = sizeof (u32),
1662   .format_trace = format_nat_in2out_ed_trace,
1663   .type = VLIB_NODE_TYPE_INTERNAL,
1664   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1665   .error_strings = nat_in2out_ed_error_strings,
1666   .runtime_data_bytes = sizeof (snat_runtime_t),
1667   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1668   .next_nodes = {
1669     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1670     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1671     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1672     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1673     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1674   },
1675 };
1676 /* *INDENT-ON* */
1677
1678 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1679                                             vlib_node_runtime_t * node,
1680                                             vlib_frame_t * frame)
1681 {
1682   return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1683 }
1684
1685 /* *INDENT-OFF* */
1686 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1687   .name = "nat44-ed-in2out-output",
1688   .vector_size = sizeof (u32),
1689   .format_trace = format_nat_in2out_ed_trace,
1690   .type = VLIB_NODE_TYPE_INTERNAL,
1691   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1692   .error_strings = nat_in2out_ed_error_strings,
1693   .runtime_data_bytes = sizeof (snat_runtime_t),
1694   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1695   .next_nodes = {
1696     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1697     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1698     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1699     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1700     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1701   },
1702 };
1703 /* *INDENT-ON* */
1704
1705 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1706                                               vlib_node_runtime_t * node,
1707                                               vlib_frame_t * frame)
1708 {
1709   return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1710 }
1711
1712 /* *INDENT-OFF* */
1713 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1714   .name = "nat44-ed-in2out-slowpath",
1715   .vector_size = sizeof (u32),
1716   .format_trace = format_nat_in2out_ed_trace,
1717   .type = VLIB_NODE_TYPE_INTERNAL,
1718   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1719   .error_strings = nat_in2out_ed_error_strings,
1720   .runtime_data_bytes = sizeof (snat_runtime_t),
1721   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1722   .next_nodes = {
1723     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1724     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1725     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1726     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1727     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1728   },
1729 };
1730 /* *INDENT-ON* */
1731
1732 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1733                                                      vlib_node_runtime_t *
1734                                                      node,
1735                                                      vlib_frame_t * frame)
1736 {
1737   return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1738 }
1739
1740 /* *INDENT-OFF* */
1741 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1742   .name = "nat44-ed-in2out-output-slowpath",
1743   .vector_size = sizeof (u32),
1744   .format_trace = format_nat_in2out_ed_trace,
1745   .type = VLIB_NODE_TYPE_INTERNAL,
1746   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1747   .error_strings = nat_in2out_ed_error_strings,
1748   .runtime_data_bytes = sizeof (snat_runtime_t),
1749   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1750   .next_nodes = {
1751     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1752     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1753     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1754     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1755     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1756   },
1757 };
1758 /* *INDENT-ON* */
1759
1760 static inline uword
1761 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1762                                       vlib_node_runtime_t * node,
1763                                       vlib_frame_t * frame,
1764                                       int is_output_feature)
1765 {
1766   u32 n_left_from, *from, *to_next;
1767   nat_in2out_ed_next_t next_index;
1768   u32 pkts_processed = 0, cached_fragments = 0;
1769   snat_main_t *sm = &snat_main;
1770   f64 now = vlib_time_now (vm);
1771   u32 thread_index = vm->thread_index;
1772   snat_main_per_thread_data_t *per_thread_data =
1773     &sm->per_thread_data[thread_index];
1774   u32 *fragments_to_drop = 0;
1775   u32 *fragments_to_loopback = 0;
1776
1777   from = vlib_frame_vector_args (frame);
1778   n_left_from = frame->n_vectors;
1779   next_index = node->cached_next_index;
1780
1781   while (n_left_from > 0)
1782     {
1783       u32 n_left_to_next;
1784
1785       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1786
1787       while (n_left_from > 0 && n_left_to_next > 0)
1788         {
1789           u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1790           u32 iph_offset0 = 0;
1791           vlib_buffer_t *b0;
1792           u32 next0;
1793           u8 cached0 = 0;
1794           ip4_header_t *ip0 = 0;
1795           nat_reass_ip4_t *reass0;
1796           udp_header_t *udp0;
1797           tcp_header_t *tcp0;
1798           icmp46_header_t *icmp0;
1799           clib_bihash_kv_16_8_t kv0, value0;
1800           snat_session_t *s0 = 0;
1801           u16 old_port0, new_port0;
1802           ip_csum_t sum0;
1803
1804           /* speculatively enqueue b0 to the current next frame */
1805           bi0 = from[0];
1806           to_next[0] = bi0;
1807           from += 1;
1808           to_next += 1;
1809           n_left_from -= 1;
1810           n_left_to_next -= 1;
1811
1812           b0 = vlib_get_buffer (vm, bi0);
1813
1814           next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1815
1816           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1817           rx_fib_index0 =
1818             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1819                                                  sw_if_index0);
1820
1821           if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1822             {
1823               next0 = NAT_IN2OUT_ED_NEXT_DROP;
1824               b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1825               goto trace0;
1826             }
1827
1828           if (is_output_feature)
1829             iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1830
1831           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1832                                   iph_offset0);
1833
1834           udp0 = ip4_next_header (ip0);
1835           tcp0 = (tcp_header_t *) udp0;
1836           icmp0 = (icmp46_header_t *) udp0;
1837           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1838
1839           reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1840                                                  ip0->dst_address,
1841                                                  ip0->fragment_id,
1842                                                  ip0->protocol,
1843                                                  1, &fragments_to_drop);
1844
1845           if (PREDICT_FALSE (!reass0))
1846             {
1847               next0 = NAT_IN2OUT_ED_NEXT_DROP;
1848               b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1849               nat_log_notice ("maximum reassemblies exceeded");
1850               goto trace0;
1851             }
1852
1853           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1854             {
1855               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1856                 {
1857                   if (is_output_feature)
1858                     {
1859                       if (PREDICT_FALSE
1860                           (nat_not_translate_output_feature_fwd
1861                            (sm, ip0, thread_index, now, vm, b0)))
1862                         reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1863                       goto trace0;
1864                     }
1865
1866                   next0 = icmp_in2out_ed_slow_path
1867                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1868                      next0, now, thread_index, &s0);
1869
1870                   if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1871                     {
1872                       if (s0)
1873                         reass0->sess_index = s0 - per_thread_data->sessions;
1874                       else
1875                         reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1876                       nat_ip4_reass_get_frags (reass0,
1877                                                &fragments_to_loopback);
1878                     }
1879
1880                   goto trace0;
1881                 }
1882
1883               make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1884                           ip0->protocol, rx_fib_index0, udp0->src_port,
1885                           udp0->dst_port);
1886
1887               if (clib_bihash_search_16_8
1888                   (&per_thread_data->in2out_ed, &kv0, &value0))
1889                 {
1890                   if (is_output_feature)
1891                     {
1892                       if (PREDICT_FALSE
1893                           (nat44_ed_not_translate_output_feature
1894                            (sm, ip0, ip0->protocol, udp0->src_port,
1895                             udp0->dst_port, thread_index, sw_if_index0,
1896                             vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1897                         {
1898                           reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1899                           nat_ip4_reass_get_frags (reass0,
1900                                                    &fragments_to_loopback);
1901                           goto trace0;
1902                         }
1903                     }
1904                   else
1905                     {
1906                       if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1907                                                                  sw_if_index0,
1908                                                                  ip0, proto0,
1909                                                                  rx_fib_index0,
1910                                                                  thread_index)))
1911                         {
1912                           reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1913                           nat_ip4_reass_get_frags (reass0,
1914                                                    &fragments_to_loopback);
1915                           goto trace0;
1916                         }
1917                     }
1918
1919                   next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1920                                         &s0, node, next0, thread_index, now,
1921                                         tcp0);
1922
1923                   if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1924                     goto trace0;
1925
1926                   if (PREDICT_FALSE (!s0))
1927                     {
1928                       reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1929                       goto trace0;
1930                     }
1931
1932                   reass0->sess_index = s0 - per_thread_data->sessions;
1933                 }
1934               else
1935                 {
1936                   s0 = pool_elt_at_index (per_thread_data->sessions,
1937                                           value0.value);
1938                   reass0->sess_index = value0.value;
1939                 }
1940               nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1941             }
1942           else
1943             {
1944               if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1945                 goto trace0;
1946               if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1947                 {
1948                   if (nat_ip4_reass_add_fragment
1949                       (thread_index, reass0, bi0, &fragments_to_drop))
1950                     {
1951                       b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1952                       nat_log_notice
1953                         ("maximum fragments per reassembly exceeded");
1954                       next0 = NAT_IN2OUT_ED_NEXT_DROP;
1955                       goto trace0;
1956                     }
1957                   cached0 = 1;
1958                   goto trace0;
1959                 }
1960               s0 = pool_elt_at_index (per_thread_data->sessions,
1961                                       reass0->sess_index);
1962             }
1963
1964           old_addr0 = ip0->src_address.as_u32;
1965           ip0->src_address = s0->out2in.addr;
1966           new_addr0 = ip0->src_address.as_u32;
1967           if (!is_output_feature)
1968             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1969
1970           sum0 = ip0->checksum;
1971           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1972                                  ip4_header_t,
1973                                  src_address /* changed member */ );
1974           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1975             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1976                                    s0->ext_host_addr.as_u32, ip4_header_t,
1977                                    dst_address);
1978           ip0->checksum = ip_csum_fold (sum0);
1979
1980           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1981             {
1982               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1983                 {
1984                   old_port0 = tcp0->src_port;
1985                   tcp0->src_port = s0->out2in.port;
1986                   new_port0 = tcp0->src_port;
1987
1988                   sum0 = tcp0->checksum;
1989                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1990                                          ip4_header_t,
1991                                          dst_address /* changed member */ );
1992                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1993                                          ip4_header_t /* cheat */ ,
1994                                          length /* changed member */ );
1995                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1996                     {
1997                       sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1998                                              s0->ext_host_addr.as_u32,
1999                                              ip4_header_t, dst_address);
2000                       sum0 = ip_csum_update (sum0, tcp0->dst_port,
2001                                              s0->ext_host_port, ip4_header_t,
2002                                              length);
2003                       tcp0->dst_port = s0->ext_host_port;
2004                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2005                     }
2006                   tcp0->checksum = ip_csum_fold (sum0);
2007                 }
2008               else
2009                 {
2010                   old_port0 = udp0->src_port;
2011                   udp0->src_port = s0->out2in.port;
2012                   udp0->checksum = 0;
2013                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
2014                     {
2015                       udp0->dst_port = s0->ext_host_port;
2016                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2017                     }
2018                 }
2019             }
2020
2021           /* Hairpinning */
2022           nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2023                                    s0->ext_host_port, proto0, 1);
2024
2025           /* Accounting */
2026           nat44_session_update_counters (s0, now,
2027                                          vlib_buffer_length_in_chain (vm,
2028                                                                       b0));
2029           /* Per-user LRU list maintenance */
2030           nat44_session_update_lru (sm, s0, thread_index);
2031
2032         trace0:
2033           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2034                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2035             {
2036               nat44_reass_trace_t *t =
2037                 vlib_add_trace (vm, node, b0, sizeof (*t));
2038               t->cached = cached0;
2039               t->sw_if_index = sw_if_index0;
2040               t->next_index = next0;
2041             }
2042
2043           if (cached0)
2044             {
2045               n_left_to_next++;
2046               to_next--;
2047               cached_fragments++;
2048             }
2049           else
2050             {
2051               pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
2052
2053               /* verify speculative enqueue, maybe switch current next frame */
2054               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2055                                                to_next, n_left_to_next,
2056                                                bi0, next0);
2057             }
2058
2059           if (n_left_from == 0 && vec_len (fragments_to_loopback))
2060             {
2061               from = vlib_frame_vector_args (frame);
2062               u32 len = vec_len (fragments_to_loopback);
2063               if (len <= VLIB_FRAME_SIZE)
2064                 {
2065                   clib_memcpy_fast (from, fragments_to_loopback,
2066                                     sizeof (u32) * len);
2067                   n_left_from = len;
2068                   vec_reset_length (fragments_to_loopback);
2069                 }
2070               else
2071                 {
2072                   clib_memcpy_fast (from, fragments_to_loopback +
2073                                     (len - VLIB_FRAME_SIZE),
2074                                     sizeof (u32) * VLIB_FRAME_SIZE);
2075                   n_left_from = VLIB_FRAME_SIZE;
2076                   _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2077                 }
2078             }
2079         }
2080
2081       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2082     }
2083
2084   vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2085                                NAT_IN2OUT_ED_ERROR_PROCESSED_FRAGMENTS,
2086                                pkts_processed);
2087   vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2088                                NAT_IN2OUT_ED_ERROR_CACHED_FRAGMENTS,
2089                                cached_fragments);
2090
2091   nat_send_all_to_node (vm, fragments_to_drop, node,
2092                         &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2093                         NAT_IN2OUT_ED_NEXT_DROP);
2094
2095   vec_free (fragments_to_drop);
2096   vec_free (fragments_to_loopback);
2097   return frame->n_vectors;
2098 }
2099
2100 VLIB_NODE_FN (nat44_ed_in2out_reass_node) (vlib_main_t * vm,
2101                                            vlib_node_runtime_t * node,
2102                                            vlib_frame_t * frame)
2103 {
2104   return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2105 }
2106
2107 /* *INDENT-OFF* */
2108 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2109   .name = "nat44-ed-in2out-reass",
2110   .vector_size = sizeof (u32),
2111   .format_trace = format_nat44_reass_trace,
2112   .type = VLIB_NODE_TYPE_INTERNAL,
2113   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2114   .error_strings = nat_in2out_ed_error_strings,
2115   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2116   .next_nodes = {
2117     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2118     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2119     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2120     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2121     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2122   },
2123 };
2124 /* *INDENT-ON* */
2125
2126 VLIB_NODE_FN (nat44_ed_in2out_reass_output_node) (vlib_main_t * vm,
2127                                                   vlib_node_runtime_t * node,
2128                                                   vlib_frame_t * frame)
2129 {
2130   return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2131 }
2132
2133 /* *INDENT-OFF* */
2134 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2135   .name = "nat44-ed-in2out-reass-output",
2136   .vector_size = sizeof (u32),
2137   .format_trace = format_nat44_reass_trace,
2138   .type = VLIB_NODE_TYPE_INTERNAL,
2139   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2140   .error_strings = nat_in2out_ed_error_strings,
2141   .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2142   .next_nodes = {
2143     [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2144     [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2145     [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2146     [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2147     [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2148   },
2149 };
2150 /* *INDENT-ON* */
2151
2152 /*
2153  * fd.io coding-style-patch-verification: ON
2154  *
2155  * Local Variables:
2156  * eval: (c-set-style "gnu")
2157  * End:
2158  */