NAT44: endpoint-dependent mode session timeout improvement (VPP-1423)
[vpp.git] / src / plugins / nat / out2in_ed.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT44 endpoint-dependent outside to inside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
28 #include <nat/nat.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_reass.h>
31 #include <nat/nat_inlines.h>
32
33 #define foreach_nat_out2in_ed_error                     \
34 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol")         \
35 _(OUT2IN_PACKETS, "Good out2in packets processed")      \
36 _(OUT_OF_PORTS, "Out of ports")                         \
37 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
38 _(NO_TRANSLATION, "No translation")                     \
39 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded")   \
40 _(DROP_FRAGMENT, "Drop fragment")                       \
41 _(MAX_REASS, "Maximum reassemblies exceeded")           \
42 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
43
44 typedef enum
45 {
46 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
47   foreach_nat_out2in_ed_error
48 #undef _
49     NAT_OUT2IN_ED_N_ERROR,
50 } nat_out2in_ed_error_t;
51
52 static char *nat_out2in_ed_error_strings[] = {
53 #define _(sym,string) string,
54   foreach_nat_out2in_ed_error
55 #undef _
56 };
57
58 typedef enum
59 {
60   NAT44_ED_OUT2IN_NEXT_DROP,
61   NAT44_ED_OUT2IN_NEXT_LOOKUP,
62   NAT44_ED_OUT2IN_NEXT_ICMP_ERROR,
63   NAT44_ED_OUT2IN_NEXT_IN2OUT,
64   NAT44_ED_OUT2IN_NEXT_SLOW_PATH,
65   NAT44_ED_OUT2IN_NEXT_REASS,
66   NAT44_ED_OUT2IN_N_NEXT,
67 } nat44_ed_out2in_next_t;
68
69 typedef struct
70 {
71   u32 sw_if_index;
72   u32 next_index;
73   u32 session_index;
74   u32 is_slow_path;
75 } nat44_ed_out2in_trace_t;
76
77 vlib_node_registration_t nat44_ed_out2in_node;
78 vlib_node_registration_t nat44_ed_out2in_slowpath_node;
79 vlib_node_registration_t nat44_ed_out2in_reass_node;
80
81 static u8 *
82 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
83 {
84   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
85   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86   nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
87   char *tag;
88
89   tag =
90     t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
91     "NAT44_OUT2IN_ED_FAST_PATH";
92
93   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
94               t->sw_if_index, t->next_index, t->session_index);
95
96   return s;
97 }
98
99 static inline u32
100 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
101                           ip4_header_t * ip0, icmp46_header_t * icmp0,
102                           u32 sw_if_index0, u32 rx_fib_index0,
103                           vlib_node_runtime_t * node, u32 next0, f64 now,
104                           u32 thread_index, snat_session_t ** p_s0)
105 {
106   next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
107                        next0, thread_index, p_s0, 0);
108   snat_session_t *s0 = *p_s0;
109   if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP && s0))
110     {
111       /* Accounting */
112       nat44_session_update_counters (s0, now,
113                                      vlib_buffer_length_in_chain
114                                      (sm->vlib_main, b0));
115       /* Per-user LRU list maintenance */
116       nat44_session_update_lru (sm, s0, thread_index);
117     }
118   return next0;
119 }
120
121 int
122 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
123 {
124   snat_main_t *sm = &snat_main;
125   nat44_is_idle_session_ctx_t *ctx = arg;
126   snat_session_t *s;
127   u64 sess_timeout_time;
128   nat_ed_ses_key_t ed_key;
129   clib_bihash_kv_16_8_t ed_kv;
130   int i;
131   snat_address_t *a;
132   snat_session_key_t key;
133   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
134                                                        ctx->thread_index);
135
136   s = pool_elt_at_index (tsm->sessions, kv->value);
137   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
138   if (ctx->now >= sess_timeout_time)
139     {
140       ed_key.l_addr = s->in2out.addr;
141       ed_key.r_addr = s->ext_host_addr;
142       ed_key.fib_index = s->in2out.fib_index;
143       if (snat_is_unk_proto_session (s))
144         {
145           ed_key.proto = s->in2out.port;
146           ed_key.r_port = 0;
147           ed_key.l_port = 0;
148         }
149       else
150         {
151           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
152           ed_key.l_port = s->in2out.port;
153           ed_key.r_port = s->ext_host_port;
154         }
155       if (is_twice_nat_session (s))
156         {
157           ed_key.r_addr = s->ext_host_nat_addr;
158           ed_key.r_port = s->ext_host_nat_port;
159         }
160       ed_kv.key[0] = ed_key.as_u64[0];
161       ed_kv.key[1] = ed_key.as_u64[1];
162       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
163         nat_log_warn ("in2out_ed key del failed");
164
165       if (snat_is_unk_proto_session (s))
166         goto delete;
167
168       snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
169                                            s->out2in.addr.as_u32,
170                                            s->in2out.protocol,
171                                            s->in2out.port,
172                                            s->out2in.port,
173                                            s->in2out.fib_index);
174
175       if (is_twice_nat_session (s))
176         {
177           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
178             {
179               key.protocol = s->in2out.protocol;
180               key.port = s->ext_host_nat_port;
181               a = sm->twice_nat_addresses + i;
182               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
183                 {
184                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
185                                                       ctx->thread_index,
186                                                       &key);
187                   break;
188                 }
189             }
190         }
191
192       if (snat_is_session_static (s))
193         goto delete;
194
195       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
196                                           &s->out2in);
197     delete:
198       nat44_delete_session (sm, s, ctx->thread_index);
199       return 1;
200     }
201
202   return 0;
203 }
204
205 static snat_session_t *
206 create_session_for_static_mapping_ed (snat_main_t * sm,
207                                       vlib_buffer_t * b,
208                                       snat_session_key_t l_key,
209                                       snat_session_key_t e_key,
210                                       vlib_node_runtime_t * node,
211                                       u32 thread_index,
212                                       twice_nat_type_t twice_nat,
213                                       lb_nat_type_t lb_nat, f64 now)
214 {
215   snat_session_t *s;
216   snat_user_t *u;
217   ip4_header_t *ip;
218   udp_header_t *udp;
219   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
220   clib_bihash_kv_16_8_t kv;
221   snat_session_key_t eh_key;
222   nat44_is_idle_session_ctx_t ctx;
223
224   if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
225     {
226       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
227       nat_log_notice ("maximum sessions exceeded");
228       return 0;
229     }
230
231   u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
232   if (!u)
233     {
234       nat_log_warn ("create NAT user failed");
235       return 0;
236     }
237
238   s = nat_ed_session_alloc (sm, u, thread_index, now);
239   if (!s)
240     {
241       nat44_delete_user_with_no_session (sm, u, thread_index);
242       nat_log_warn ("create NAT session failed");
243       return 0;
244     }
245
246   ip = vlib_buffer_get_current (b);
247   udp = ip4_next_header (ip);
248
249   s->ext_host_addr.as_u32 = ip->src_address.as_u32;
250   s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
251   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
252   if (lb_nat)
253     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
254   if (lb_nat == AFFINITY_LB_NAT)
255     s->flags |= SNAT_SESSION_FLAG_AFFINITY;
256   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
257   s->out2in = e_key;
258   s->in2out = l_key;
259   s->in2out.protocol = s->out2in.protocol;
260   user_session_increment (sm, u, 1);
261
262   /* Add to lookup tables */
263   make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
264               e_key.fib_index, e_key.port, s->ext_host_port);
265   kv.value = s - tsm->sessions;
266   ctx.now = now;
267   ctx.thread_index = thread_index;
268   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
269                                                nat44_o2i_ed_is_idle_session_cb,
270                                                &ctx))
271     nat_log_notice ("out2in-ed key add failed");
272
273   if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
274                                  ip->src_address.as_u32 == l_key.addr.as_u32))
275     {
276       eh_key.protocol = e_key.protocol;
277       if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
278                                                thread_index, &eh_key,
279                                                sm->port_per_thread,
280                                                tsm->snat_thread_index))
281         {
282           b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
283           nat44_delete_session (sm, s, thread_index);
284           if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
285             nat_log_notice ("out2in-ed key del failed");
286           return 0;
287         }
288       s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
289       s->ext_host_nat_port = eh_key.port;
290       s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
291       make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
292                   l_key.fib_index, l_key.port, s->ext_host_nat_port);
293     }
294   else
295     {
296       make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
297                   l_key.fib_index, l_key.port, s->ext_host_port);
298     }
299   kv.value = s - tsm->sessions;
300   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
301                                                nat44_i2o_ed_is_idle_session_cb,
302                                                &ctx))
303     nat_log_notice ("in2out-ed key add failed");
304
305   return s;
306 }
307
308 static_always_inline int
309 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
310 {
311   icmp46_header_t *icmp0;
312   nat_ed_ses_key_t key0;
313   icmp_echo_header_t *echo0, *inner_echo0 = 0;
314   ip4_header_t *inner_ip0;
315   void *l4_header = 0;
316   icmp46_header_t *inner_icmp0;
317
318   icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
319   echo0 = (icmp_echo_header_t *) (icmp0 + 1);
320
321   if (!icmp_is_error_message (icmp0))
322     {
323       key0.proto = IP_PROTOCOL_ICMP;
324       key0.l_addr = ip0->dst_address;
325       key0.r_addr = ip0->src_address;
326       key0.l_port = echo0->identifier;
327       key0.r_port = 0;
328     }
329   else
330     {
331       inner_ip0 = (ip4_header_t *) (echo0 + 1);
332       l4_header = ip4_next_header (inner_ip0);
333       key0.proto = inner_ip0->protocol;
334       key0.l_addr = inner_ip0->src_address;
335       key0.r_addr = inner_ip0->dst_address;
336       switch (ip_proto_to_snat_proto (inner_ip0->protocol))
337         {
338         case SNAT_PROTOCOL_ICMP:
339           inner_icmp0 = (icmp46_header_t *) l4_header;
340           inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
341           key0.l_port = inner_echo0->identifier;
342           key0.r_port = 0;
343           break;
344         case SNAT_PROTOCOL_UDP:
345         case SNAT_PROTOCOL_TCP:
346           key0.l_port = ((tcp_udp_header_t *) l4_header)->src_port;
347           key0.r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
348           break;
349         default:
350           return -1;
351         }
352     }
353   *p_key0 = key0;
354   return 0;
355 }
356
357 static int
358 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
359               u16 dst_port, u32 thread_index, u32 rx_fib_index)
360 {
361   clib_bihash_kv_16_8_t kv, value;
362   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
363
364   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
365               rx_fib_index, src_port, dst_port);
366   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
367     return 1;
368
369   return 0;
370 }
371
372 static void
373 create_bypass_for_fwd (snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
374                        u32 thread_index)
375 {
376   nat_ed_ses_key_t key;
377   clib_bihash_kv_16_8_t kv, value;
378   udp_header_t *udp;
379   snat_user_t *u;
380   snat_session_t *s = 0;
381   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
382   f64 now = vlib_time_now (sm->vlib_main);
383
384   if (ip->protocol == IP_PROTOCOL_ICMP)
385     {
386       if (icmp_get_ed_key (ip, &key))
387         return;
388     }
389   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
390     {
391       udp = ip4_next_header (ip);
392       key.r_addr = ip->src_address;
393       key.l_addr = ip->dst_address;
394       key.proto = ip->protocol;
395       key.l_port = udp->dst_port;
396       key.r_port = udp->src_port;
397     }
398   else
399     {
400       key.r_addr = ip->src_address;
401       key.l_addr = ip->dst_address;
402       key.proto = ip->protocol;
403       key.l_port = key.r_port = 0;
404     }
405   key.fib_index = 0;
406   kv.key[0] = key.as_u64[0];
407   kv.key[1] = key.as_u64[1];
408
409   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
410     {
411       s = pool_elt_at_index (tsm->sessions, value.value);
412     }
413   else
414     {
415       if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
416         return;
417
418       u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
419                                   thread_index);
420       if (!u)
421         {
422           nat_log_warn ("create NAT user failed");
423           return;
424         }
425
426       s = nat_ed_session_alloc (sm, u, thread_index, now);
427       if (!s)
428         {
429           nat44_delete_user_with_no_session (sm, u, thread_index);
430           nat_log_warn ("create NAT session failed");
431           return;
432         }
433
434       s->ext_host_addr = key.r_addr;
435       s->ext_host_port = key.r_port;
436       s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
437       s->out2in.addr = key.l_addr;
438       s->out2in.port = key.l_port;
439       s->out2in.protocol = ip_proto_to_snat_proto (key.proto);
440       s->out2in.fib_index = 0;
441       s->in2out = s->out2in;
442       user_session_increment (sm, u, 0);
443
444       kv.value = s - tsm->sessions;
445       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
446         nat_log_notice ("in2out_ed key add failed");
447     }
448
449   if (ip->protocol == IP_PROTOCOL_TCP)
450     {
451       tcp_header_t *tcp = ip4_next_header (ip);
452       if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
453         return;
454     }
455
456   /* Accounting */
457   nat44_session_update_counters (s, now, 0);
458   /* Per-user LRU list maintenance */
459   nat44_session_update_lru (sm, s, thread_index);
460 }
461
462 u32
463 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
464                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
465                       u8 * p_proto, snat_session_key_t * p_value,
466                       u8 * p_dont_translate, void *d, void *e)
467 {
468   u32 next = ~0, sw_if_index, rx_fib_index;
469   icmp46_header_t *icmp;
470   nat_ed_ses_key_t key;
471   clib_bihash_kv_16_8_t kv, value;
472   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
473   snat_session_t *s = 0;
474   u8 dont_translate = 0, is_addr_only;
475   snat_session_key_t e_key, l_key;
476
477   icmp = (icmp46_header_t *) ip4_next_header (ip);
478   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
479   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
480
481   if (icmp_get_ed_key (ip, &key))
482     {
483       b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
484       next = NAT44_ED_OUT2IN_NEXT_DROP;
485       goto out;
486     }
487   key.fib_index = rx_fib_index;
488   kv.key[0] = key.as_u64[0];
489   kv.key[1] = key.as_u64[1];
490
491   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
492     {
493       /* Try to match static mapping */
494       e_key.addr = ip->dst_address;
495       e_key.port = key.l_port;
496       e_key.protocol = ip_proto_to_snat_proto (key.proto);
497       e_key.fib_index = rx_fib_index;
498       if (snat_static_mapping_match
499           (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0))
500         {
501           if (!sm->forwarding_enabled)
502             {
503               /* Don't NAT packet aimed at the intfc address */
504               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
505                                                     ip->dst_address.as_u32)))
506                 {
507                   dont_translate = 1;
508                   goto out;
509                 }
510               b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
511               next = NAT44_ED_OUT2IN_NEXT_DROP;
512               goto out;
513             }
514           else
515             {
516               dont_translate = 1;
517               if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
518                                 thread_index, rx_fib_index))
519                 {
520                   next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
521                   goto out;
522                 }
523               create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
524               goto out;
525             }
526         }
527
528       if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
529                          (icmp->type != ICMP4_echo_request || !is_addr_only)))
530         {
531           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
532           next = NAT44_ED_OUT2IN_NEXT_DROP;
533           goto out;
534         }
535
536       /* Create session initiated by host from external network */
537       s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
538                                                 thread_index, 0, 0,
539                                                 vlib_time_now
540                                                 (sm->vlib_main));
541
542       if (!s)
543         {
544           next = NAT44_ED_OUT2IN_NEXT_DROP;
545           goto out;
546         }
547     }
548   else
549     {
550       if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
551                          icmp->type != ICMP4_echo_request &&
552                          !icmp_is_error_message (icmp)))
553         {
554           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
555           next = NAT44_ED_OUT2IN_NEXT_DROP;
556           goto out;
557         }
558
559       s = pool_elt_at_index (tsm->sessions, value.value);
560     }
561
562   *p_proto = ip_proto_to_snat_proto (key.proto);
563 out:
564   if (s)
565     *p_value = s->in2out;
566   *p_dont_translate = dont_translate;
567   if (d)
568     *(snat_session_t **) d = s;
569   return next;
570 }
571
572 static snat_session_t *
573 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
574                                vlib_buffer_t * b,
575                                ip4_header_t * ip,
576                                u32 rx_fib_index,
577                                u32 thread_index,
578                                f64 now,
579                                vlib_main_t * vm, vlib_node_runtime_t * node)
580 {
581   clib_bihash_kv_8_8_t kv, value;
582   clib_bihash_kv_16_8_t s_kv, s_value;
583   snat_static_mapping_t *m;
584   u32 old_addr, new_addr;
585   ip_csum_t sum;
586   snat_session_t *s;
587   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
588   snat_user_t *u;
589
590   old_addr = ip->dst_address.as_u32;
591
592   make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
593               rx_fib_index, 0, 0);
594
595   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
596     {
597       s = pool_elt_at_index (tsm->sessions, s_value.value);
598       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
599     }
600   else
601     {
602       if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
603         {
604           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
605           nat_log_notice ("maximum sessions exceeded");
606           return 0;
607         }
608
609       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
610       if (clib_bihash_search_8_8
611           (&sm->static_mapping_by_external, &kv, &value))
612         {
613           b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
614           return 0;
615         }
616
617       m = pool_elt_at_index (sm->static_mappings, value.value);
618
619       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
620
621       u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
622                                   thread_index);
623       if (!u)
624         {
625           nat_log_warn ("create NAT user failed");
626           return 0;
627         }
628
629       /* Create a new session */
630       s = nat_ed_session_alloc (sm, u, thread_index, now);
631       if (!s)
632         {
633           nat44_delete_user_with_no_session (sm, u, thread_index);
634           nat_log_warn ("create NAT session failed");
635           return 0;
636         }
637
638       s->ext_host_addr.as_u32 = ip->src_address.as_u32;
639       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
640       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
641       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
642       s->out2in.addr.as_u32 = old_addr;
643       s->out2in.fib_index = rx_fib_index;
644       s->in2out.addr.as_u32 = new_addr;
645       s->in2out.fib_index = m->fib_index;
646       s->in2out.port = s->out2in.port = ip->protocol;
647       user_session_increment (sm, u, 1);
648
649       /* Add to lookup tables */
650       s_kv.value = s - tsm->sessions;
651       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
652         nat_log_notice ("out2in key add failed");
653
654       make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
655                   m->fib_index, 0, 0);
656       s_kv.value = s - tsm->sessions;
657       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
658         nat_log_notice ("in2out key add failed");
659     }
660
661   /* Update IP checksum */
662   sum = ip->checksum;
663   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
664   ip->checksum = ip_csum_fold (sum);
665
666   vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
667
668   /* Accounting */
669   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
670   /* Per-user LRU list maintenance */
671   nat44_session_update_lru (sm, s, thread_index);
672
673   return s;
674 }
675
676 static inline uword
677 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
678                                 vlib_node_runtime_t * node,
679                                 vlib_frame_t * frame, int is_slow_path)
680 {
681   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
682   nat44_ed_out2in_next_t next_index;
683   snat_main_t *sm = &snat_main;
684   f64 now = vlib_time_now (vm);
685   u32 thread_index = vm->thread_index;
686   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
687
688   stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
689     nat44_ed_out2in_node.index;
690
691   from = vlib_frame_vector_args (frame);
692   n_left_from = frame->n_vectors;
693   next_index = node->cached_next_index;
694
695   while (n_left_from > 0)
696     {
697       u32 n_left_to_next;
698
699       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
700
701       while (n_left_from >= 4 && n_left_to_next >= 2)
702         {
703           u32 bi0, bi1;
704           vlib_buffer_t *b0, *b1;
705           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
706             new_addr0;
707           u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
708             new_addr1;
709           u16 old_port0, new_port0, old_port1, new_port1;
710           ip4_header_t *ip0, *ip1;
711           udp_header_t *udp0, *udp1;
712           tcp_header_t *tcp0, *tcp1;
713           icmp46_header_t *icmp0, *icmp1;
714           snat_session_t *s0 = 0, *s1 = 0;
715           clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
716           ip_csum_t sum0, sum1;
717           snat_session_key_t e_key0, l_key0, e_key1, l_key1;
718           lb_nat_type_t lb_nat0, lb_nat1;
719           twice_nat_type_t twice_nat0, twice_nat1;
720
721           /* Prefetch next iteration. */
722           {
723             vlib_buffer_t *p2, *p3;
724
725             p2 = vlib_get_buffer (vm, from[2]);
726             p3 = vlib_get_buffer (vm, from[3]);
727
728             vlib_prefetch_buffer_header (p2, LOAD);
729             vlib_prefetch_buffer_header (p3, LOAD);
730
731             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
732             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
733           }
734
735           /* speculatively enqueue b0 and b1 to the current next frame */
736           to_next[0] = bi0 = from[0];
737           to_next[1] = bi1 = from[1];
738           from += 2;
739           to_next += 2;
740           n_left_from -= 2;
741           n_left_to_next -= 2;
742
743           b0 = vlib_get_buffer (vm, bi0);
744           b1 = vlib_get_buffer (vm, bi1);
745
746           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
747           vnet_buffer (b0)->snat.flags = 0;
748           ip0 = vlib_buffer_get_current (b0);
749
750           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
751           rx_fib_index0 =
752             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
753                                                  sw_if_index0);
754
755           if (PREDICT_FALSE (ip0->ttl == 1))
756             {
757               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
758               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
759                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
760                                            0);
761               next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
762               goto trace00;
763             }
764
765           udp0 = ip4_next_header (ip0);
766           tcp0 = (tcp_header_t *) udp0;
767           icmp0 = (icmp46_header_t *) udp0;
768           proto0 = ip_proto_to_snat_proto (ip0->protocol);
769
770           if (is_slow_path)
771             {
772               if (PREDICT_FALSE (proto0 == ~0))
773                 {
774                   s0 =
775                     nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
776                                                    thread_index, now, vm,
777                                                    node);
778                   if (!sm->forwarding_enabled)
779                     {
780                       if (!s0)
781                         next0 = NAT44_ED_OUT2IN_NEXT_DROP;
782                       goto trace00;
783                     }
784                 }
785
786               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
787                 {
788                   next0 = icmp_out2in_ed_slow_path
789                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
790                      next0, now, thread_index, &s0);
791                   goto trace00;
792                 }
793             }
794           else
795             {
796               if (PREDICT_FALSE (proto0 == ~0))
797                 {
798                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
799                   goto trace00;
800                 }
801
802               if (ip4_is_fragment (ip0))
803                 {
804                   next0 = NAT44_ED_OUT2IN_NEXT_REASS;
805                   goto trace00;
806                 }
807
808               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
809                 {
810                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
811                   goto trace00;
812                 }
813             }
814
815           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
816                       ip0->protocol, rx_fib_index0, udp0->dst_port,
817                       udp0->src_port);
818
819           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
820             {
821               if (is_slow_path)
822                 {
823                   /* Try to match static mapping by external address and port,
824                      destination address and port in packet */
825                   e_key0.addr = ip0->dst_address;
826                   e_key0.port = udp0->dst_port;
827                   e_key0.protocol = proto0;
828                   e_key0.fib_index = rx_fib_index0;
829                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
830                                                  &twice_nat0, &lb_nat0,
831                                                  &ip0->src_address))
832                     {
833                       /*
834                        * Send DHCP packets to the ipv4 stack, or we won't
835                        * be able to use dhcp client on the outside interface
836                        */
837                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
838                                          && (udp0->dst_port ==
839                                              clib_host_to_net_u16
840                                              (UDP_DST_PORT_dhcp_to_client))))
841                         {
842                           vnet_feature_next (&next0, b0);
843                           goto trace00;
844                         }
845
846                       if (!sm->forwarding_enabled)
847                         {
848                           b0->error =
849                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
850                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
851                         }
852                       else
853                         {
854                           if (next_src_nat (sm, ip0, ip0->protocol,
855                                             udp0->src_port, udp0->dst_port,
856                                             thread_index, rx_fib_index0))
857                             {
858                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
859                               goto trace00;
860                             }
861                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
862                                                  thread_index);
863                         }
864                       goto trace00;
865                     }
866
867                   /* Create session initiated by host from external network */
868                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
869                                                              e_key0, node,
870                                                              thread_index,
871                                                              twice_nat0,
872                                                              lb_nat0, now);
873
874                   if (!s0)
875                     {
876                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
877                       goto trace00;
878                     }
879                 }
880               else
881                 {
882                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
883                   goto trace00;
884                 }
885             }
886           else
887             {
888               s0 = pool_elt_at_index (tsm->sessions, value0.value);
889             }
890
891           old_addr0 = ip0->dst_address.as_u32;
892           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
893           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
894
895           sum0 = ip0->checksum;
896           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
897                                  dst_address);
898           if (PREDICT_FALSE (is_twice_nat_session (s0)))
899             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
900                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
901                                    src_address);
902           ip0->checksum = ip_csum_fold (sum0);
903
904           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
905             {
906               old_port0 = tcp0->dst_port;
907               new_port0 = tcp0->dst_port = s0->in2out.port;
908
909               sum0 = tcp0->checksum;
910               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
911                                      dst_address);
912               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
913                                      length);
914               if (is_twice_nat_session (s0))
915                 {
916                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
917                                          s0->ext_host_nat_addr.as_u32,
918                                          ip4_header_t, dst_address);
919                   sum0 = ip_csum_update (sum0, tcp0->src_port,
920                                          s0->ext_host_nat_port, ip4_header_t,
921                                          length);
922                   tcp0->src_port = s0->ext_host_nat_port;
923                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
924                 }
925               tcp0->checksum = ip_csum_fold (sum0);
926               if (nat44_set_tcp_session_state_o2i
927                   (sm, s0, tcp0, thread_index))
928                 goto trace00;
929             }
930           else
931             {
932               udp0->dst_port = s0->in2out.port;
933               if (is_twice_nat_session (s0))
934                 {
935                   udp0->src_port = s0->ext_host_nat_port;
936                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
937                 }
938               udp0->checksum = 0;
939             }
940
941           /* Accounting */
942           nat44_session_update_counters (s0, now,
943                                          vlib_buffer_length_in_chain (vm,
944                                                                       b0));
945           /* Per-user LRU list maintenance */
946           nat44_session_update_lru (sm, s0, thread_index);
947
948         trace00:
949           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
950                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
951             {
952               nat44_ed_out2in_trace_t *t =
953                 vlib_add_trace (vm, node, b0, sizeof (*t));
954               t->is_slow_path = is_slow_path;
955               t->sw_if_index = sw_if_index0;
956               t->next_index = next0;
957               t->session_index = ~0;
958               if (s0)
959                 t->session_index = s0 - tsm->sessions;
960             }
961
962           pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
963
964           next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
965           vnet_buffer (b1)->snat.flags = 0;
966           ip1 = vlib_buffer_get_current (b1);
967
968           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
969           rx_fib_index1 =
970             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
971                                                  sw_if_index1);
972
973           if (PREDICT_FALSE (ip1->ttl == 1))
974             {
975               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
976               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
977                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
978                                            0);
979               next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
980               goto trace01;
981             }
982
983           udp1 = ip4_next_header (ip1);
984           tcp1 = (tcp_header_t *) udp1;
985           icmp1 = (icmp46_header_t *) udp1;
986           proto1 = ip_proto_to_snat_proto (ip1->protocol);
987
988           if (is_slow_path)
989             {
990               if (PREDICT_FALSE (proto1 == ~0))
991                 {
992                   s1 =
993                     nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
994                                                    thread_index, now, vm,
995                                                    node);
996                   if (!sm->forwarding_enabled)
997                     {
998                       if (!s1)
999                         next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1000                       goto trace01;
1001                     }
1002                 }
1003
1004               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1005                 {
1006                   next1 = icmp_out2in_ed_slow_path
1007                     (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1008                      next1, now, thread_index, &s1);
1009                   goto trace01;
1010                 }
1011             }
1012           else
1013             {
1014               if (PREDICT_FALSE (proto1 == ~0))
1015                 {
1016                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1017                   goto trace01;
1018                 }
1019
1020               if (ip4_is_fragment (ip1))
1021                 {
1022                   next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1023                   goto trace01;
1024                 }
1025
1026               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1027                 {
1028                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1029                   goto trace01;
1030                 }
1031             }
1032
1033           make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1034                       ip1->protocol, rx_fib_index1, udp1->dst_port,
1035                       udp1->src_port);
1036
1037           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1038             {
1039               if (is_slow_path)
1040                 {
1041                   /* Try to match static mapping by external address and port,
1042                      destination address and port in packet */
1043                   e_key1.addr = ip1->dst_address;
1044                   e_key1.port = udp1->dst_port;
1045                   e_key1.protocol = proto1;
1046                   e_key1.fib_index = rx_fib_index1;
1047                   if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1048                                                  &twice_nat1, &lb_nat1,
1049                                                  &ip1->src_address))
1050                     {
1051                       /*
1052                        * Send DHCP packets to the ipv4 stack, or we won't
1053                        * be able to use dhcp client on the outside interface
1054                        */
1055                       if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1056                                          && (udp1->dst_port ==
1057                                              clib_host_to_net_u16
1058                                              (UDP_DST_PORT_dhcp_to_client))))
1059                         {
1060                           vnet_feature_next (&next1, b1);
1061                           goto trace01;
1062                         }
1063
1064                       if (!sm->forwarding_enabled)
1065                         {
1066                           b1->error =
1067                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1068                           next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1069                         }
1070                       else
1071                         {
1072                           if (next_src_nat (sm, ip1, ip1->protocol,
1073                                             udp1->src_port, udp1->dst_port,
1074                                             thread_index, rx_fib_index1))
1075                             {
1076                               next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1077                               goto trace01;
1078                             }
1079                           create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1080                                                  thread_index);
1081                         }
1082                       goto trace01;
1083                     }
1084
1085                   /* Create session initiated by host from external network */
1086                   s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1087                                                              e_key1, node,
1088                                                              thread_index,
1089                                                              twice_nat1,
1090                                                              lb_nat1, now);
1091
1092                   if (!s1)
1093                     {
1094                       next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1095                       goto trace01;
1096                     }
1097                 }
1098               else
1099                 {
1100                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1101                   goto trace01;
1102                 }
1103             }
1104           else
1105             {
1106               s1 = pool_elt_at_index (tsm->sessions, value1.value);
1107             }
1108
1109           old_addr1 = ip1->dst_address.as_u32;
1110           new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1111           vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1112
1113           sum1 = ip1->checksum;
1114           sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1115                                  dst_address);
1116           if (PREDICT_FALSE (is_twice_nat_session (s1)))
1117             sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1118                                    s1->ext_host_nat_addr.as_u32, ip4_header_t,
1119                                    src_address);
1120           ip1->checksum = ip_csum_fold (sum1);
1121
1122           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1123             {
1124               old_port1 = tcp1->dst_port;
1125               new_port1 = tcp1->dst_port = s1->in2out.port;
1126
1127               sum1 = tcp1->checksum;
1128               sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1129                                      dst_address);
1130               sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1131                                      length);
1132               if (is_twice_nat_session (s1))
1133                 {
1134                   sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1135                                          s1->ext_host_nat_addr.as_u32,
1136                                          ip4_header_t, dst_address);
1137                   sum1 = ip_csum_update (sum1, tcp1->src_port,
1138                                          s1->ext_host_nat_port, ip4_header_t,
1139                                          length);
1140                   tcp1->src_port = s1->ext_host_nat_port;
1141                   ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1142                 }
1143               tcp1->checksum = ip_csum_fold (sum1);
1144               if (nat44_set_tcp_session_state_o2i
1145                   (sm, s1, tcp1, thread_index))
1146                 goto trace01;
1147             }
1148           else
1149             {
1150               udp1->dst_port = s1->in2out.port;
1151               if (is_twice_nat_session (s1))
1152                 {
1153                   udp1->src_port = s1->ext_host_nat_port;
1154                   ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1155                 }
1156               udp1->checksum = 0;
1157             }
1158
1159           /* Accounting */
1160           nat44_session_update_counters (s1, now,
1161                                          vlib_buffer_length_in_chain (vm,
1162                                                                       b1));
1163           /* Per-user LRU list maintenance */
1164           nat44_session_update_lru (sm, s1, thread_index);
1165
1166         trace01:
1167           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1168                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1169             {
1170               nat44_ed_out2in_trace_t *t =
1171                 vlib_add_trace (vm, node, b1, sizeof (*t));
1172               t->is_slow_path = is_slow_path;
1173               t->sw_if_index = sw_if_index1;
1174               t->next_index = next1;
1175               t->session_index = ~0;
1176               if (s1)
1177                 t->session_index = s1 - tsm->sessions;
1178             }
1179
1180           pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
1181
1182           /* verify speculative enqueues, maybe switch current next frame */
1183           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1184                                            to_next, n_left_to_next,
1185                                            bi0, bi1, next0, next1);
1186         }
1187
1188       while (n_left_from > 0 && n_left_to_next > 0)
1189         {
1190           u32 bi0;
1191           vlib_buffer_t *b0;
1192           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1193             new_addr0;
1194           u16 old_port0, new_port0;
1195           ip4_header_t *ip0;
1196           udp_header_t *udp0;
1197           tcp_header_t *tcp0;
1198           icmp46_header_t *icmp0;
1199           snat_session_t *s0 = 0;
1200           clib_bihash_kv_16_8_t kv0, value0;
1201           ip_csum_t sum0;
1202           snat_session_key_t e_key0, l_key0;
1203           lb_nat_type_t lb_nat0;
1204           twice_nat_type_t twice_nat0;
1205
1206           /* speculatively enqueue b0 to the current next frame */
1207           bi0 = from[0];
1208           to_next[0] = bi0;
1209           from += 1;
1210           to_next += 1;
1211           n_left_from -= 1;
1212           n_left_to_next -= 1;
1213
1214           b0 = vlib_get_buffer (vm, bi0);
1215           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1216           vnet_buffer (b0)->snat.flags = 0;
1217           ip0 = vlib_buffer_get_current (b0);
1218
1219           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1220           rx_fib_index0 =
1221             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1222                                                  sw_if_index0);
1223
1224           if (PREDICT_FALSE (ip0->ttl == 1))
1225             {
1226               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1227               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1228                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1229                                            0);
1230               next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1231               goto trace0;
1232             }
1233
1234           udp0 = ip4_next_header (ip0);
1235           tcp0 = (tcp_header_t *) udp0;
1236           icmp0 = (icmp46_header_t *) udp0;
1237           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1238
1239           if (is_slow_path)
1240             {
1241               if (PREDICT_FALSE (proto0 == ~0))
1242                 {
1243                   s0 =
1244                     nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1245                                                    thread_index, now, vm,
1246                                                    node);
1247                   if (!sm->forwarding_enabled)
1248                     {
1249                       if (!s0)
1250                         next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1251                       goto trace0;
1252                     }
1253                 }
1254
1255               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1256                 {
1257                   next0 = icmp_out2in_ed_slow_path
1258                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1259                      next0, now, thread_index, &s0);
1260                   goto trace0;
1261                 }
1262             }
1263           else
1264             {
1265               if (PREDICT_FALSE (proto0 == ~0))
1266                 {
1267                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1268                   goto trace0;
1269                 }
1270
1271               if (ip4_is_fragment (ip0))
1272                 {
1273                   next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1274                   goto trace0;
1275                 }
1276
1277               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1278                 {
1279                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1280                   goto trace0;
1281                 }
1282             }
1283
1284           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1285                       ip0->protocol, rx_fib_index0, udp0->dst_port,
1286                       udp0->src_port);
1287
1288           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1289             {
1290               if (is_slow_path)
1291                 {
1292                   /* Try to match static mapping by external address and port,
1293                      destination address and port in packet */
1294                   e_key0.addr = ip0->dst_address;
1295                   e_key0.port = udp0->dst_port;
1296                   e_key0.protocol = proto0;
1297                   e_key0.fib_index = rx_fib_index0;
1298                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1299                                                  &twice_nat0, &lb_nat0,
1300                                                  &ip0->src_address))
1301                     {
1302                       /*
1303                        * Send DHCP packets to the ipv4 stack, or we won't
1304                        * be able to use dhcp client on the outside interface
1305                        */
1306                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1307                                          && (udp0->dst_port ==
1308                                              clib_host_to_net_u16
1309                                              (UDP_DST_PORT_dhcp_to_client))))
1310                         {
1311                           vnet_feature_next (&next0, b0);
1312                           goto trace0;
1313                         }
1314
1315                       if (!sm->forwarding_enabled)
1316                         {
1317                           b0->error =
1318                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1319                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1320                         }
1321                       else
1322                         {
1323                           if (next_src_nat (sm, ip0, ip0->protocol,
1324                                             udp0->src_port, udp0->dst_port,
1325                                             thread_index, rx_fib_index0))
1326                             {
1327                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1328                               goto trace0;
1329                             }
1330                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1331                                                  thread_index);
1332                         }
1333                       goto trace0;
1334                     }
1335
1336                   /* Create session initiated by host from external network */
1337                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1338                                                              e_key0, node,
1339                                                              thread_index,
1340                                                              twice_nat0,
1341                                                              lb_nat0, now);
1342
1343                   if (!s0)
1344                     {
1345                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1346                       goto trace0;
1347                     }
1348                 }
1349               else
1350                 {
1351                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1352                   goto trace0;
1353                 }
1354             }
1355           else
1356             {
1357               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1358             }
1359
1360           old_addr0 = ip0->dst_address.as_u32;
1361           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1362           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1363
1364           sum0 = ip0->checksum;
1365           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1366                                  dst_address);
1367           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1368             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1369                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1370                                    src_address);
1371           ip0->checksum = ip_csum_fold (sum0);
1372
1373           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1374             {
1375               old_port0 = tcp0->dst_port;
1376               new_port0 = tcp0->dst_port = s0->in2out.port;
1377
1378               sum0 = tcp0->checksum;
1379               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1380                                      dst_address);
1381               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1382                                      length);
1383               if (is_twice_nat_session (s0))
1384                 {
1385                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1386                                          s0->ext_host_nat_addr.as_u32,
1387                                          ip4_header_t, dst_address);
1388                   sum0 = ip_csum_update (sum0, tcp0->src_port,
1389                                          s0->ext_host_nat_port, ip4_header_t,
1390                                          length);
1391                   tcp0->src_port = s0->ext_host_nat_port;
1392                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1393                 }
1394               tcp0->checksum = ip_csum_fold (sum0);
1395               if (nat44_set_tcp_session_state_o2i
1396                   (sm, s0, tcp0, thread_index))
1397                 goto trace0;
1398             }
1399           else
1400             {
1401               udp0->dst_port = s0->in2out.port;
1402               if (is_twice_nat_session (s0))
1403                 {
1404                   udp0->src_port = s0->ext_host_nat_port;
1405                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1406                 }
1407               udp0->checksum = 0;
1408             }
1409
1410           /* Accounting */
1411           nat44_session_update_counters (s0, now,
1412                                          vlib_buffer_length_in_chain (vm,
1413                                                                       b0));
1414           /* Per-user LRU list maintenance */
1415           nat44_session_update_lru (sm, s0, thread_index);
1416
1417         trace0:
1418           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1419                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1420             {
1421               nat44_ed_out2in_trace_t *t =
1422                 vlib_add_trace (vm, node, b0, sizeof (*t));
1423               t->is_slow_path = is_slow_path;
1424               t->sw_if_index = sw_if_index0;
1425               t->next_index = next0;
1426               t->session_index = ~0;
1427               if (s0)
1428                 t->session_index = s0 - tsm->sessions;
1429             }
1430
1431           pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1432           /* verify speculative enqueue, maybe switch current next frame */
1433           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1434                                            to_next, n_left_to_next,
1435                                            bi0, next0);
1436         }
1437
1438       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1439     }
1440
1441   vlib_node_increment_counter (vm, stats_node_index,
1442                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1443                                pkts_processed);
1444   return frame->n_vectors;
1445 }
1446
1447 static uword
1448 nat44_ed_out2in_fast_path_fn (vlib_main_t * vm,
1449                               vlib_node_runtime_t * node,
1450                               vlib_frame_t * frame)
1451 {
1452   return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1453 }
1454
1455 /* *INDENT-OFF* */
1456 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1457   .function = nat44_ed_out2in_fast_path_fn,
1458   .name = "nat44-ed-out2in",
1459   .vector_size = sizeof (u32),
1460   .format_trace = format_nat44_ed_out2in_trace,
1461   .type = VLIB_NODE_TYPE_INTERNAL,
1462   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1463   .error_strings = nat_out2in_ed_error_strings,
1464   .runtime_data_bytes = sizeof (snat_runtime_t),
1465   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1466   .next_nodes = {
1467     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1468     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1469     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1470     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1471     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1472     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1473   },
1474 };
1475 /* *INDENT-ON* */
1476
1477 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_node,
1478                               nat44_ed_out2in_fast_path_fn);
1479
1480 static uword
1481 nat44_ed_out2in_slow_path_fn (vlib_main_t * vm,
1482                               vlib_node_runtime_t * node,
1483                               vlib_frame_t * frame)
1484 {
1485   return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1486 }
1487
1488 /* *INDENT-OFF* */
1489 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1490   .function = nat44_ed_out2in_slow_path_fn,
1491   .name = "nat44-ed-out2in-slowpath",
1492   .vector_size = sizeof (u32),
1493   .format_trace = format_nat44_ed_out2in_trace,
1494   .type = VLIB_NODE_TYPE_INTERNAL,
1495   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1496   .error_strings = nat_out2in_ed_error_strings,
1497   .runtime_data_bytes = sizeof (snat_runtime_t),
1498   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1499   .next_nodes = {
1500     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1501     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1502     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1503     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1504     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1505     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1506   },
1507 };
1508 /* *INDENT-ON* */
1509
1510 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_slowpath_node,
1511                               nat44_ed_out2in_slow_path_fn);
1512
1513 static uword
1514 nat44_ed_out2in_reass_node_fn (vlib_main_t * vm,
1515                                vlib_node_runtime_t * node,
1516                                vlib_frame_t * frame)
1517 {
1518   u32 n_left_from, *from, *to_next;
1519   nat44_ed_out2in_next_t next_index;
1520   u32 pkts_processed = 0;
1521   snat_main_t *sm = &snat_main;
1522   f64 now = vlib_time_now (vm);
1523   u32 thread_index = vm->thread_index;
1524   snat_main_per_thread_data_t *per_thread_data =
1525     &sm->per_thread_data[thread_index];
1526   u32 *fragments_to_drop = 0;
1527   u32 *fragments_to_loopback = 0;
1528
1529   from = vlib_frame_vector_args (frame);
1530   n_left_from = frame->n_vectors;
1531   next_index = node->cached_next_index;
1532
1533   while (n_left_from > 0)
1534     {
1535       u32 n_left_to_next;
1536
1537       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1538
1539       while (n_left_from > 0 && n_left_to_next > 0)
1540         {
1541           u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1542           vlib_buffer_t *b0;
1543           u32 next0;
1544           u8 cached0 = 0;
1545           ip4_header_t *ip0;
1546           nat_reass_ip4_t *reass0;
1547           udp_header_t *udp0;
1548           tcp_header_t *tcp0;
1549           icmp46_header_t *icmp0;
1550           clib_bihash_kv_16_8_t kv0, value0;
1551           snat_session_t *s0 = 0;
1552           u16 old_port0, new_port0;
1553           ip_csum_t sum0;
1554           snat_session_key_t e_key0, l_key0;
1555           lb_nat_type_t lb0;
1556           twice_nat_type_t twice_nat0;
1557
1558           /* speculatively enqueue b0 to the current next frame */
1559           bi0 = from[0];
1560           to_next[0] = bi0;
1561           from += 1;
1562           to_next += 1;
1563           n_left_from -= 1;
1564           n_left_to_next -= 1;
1565
1566           b0 = vlib_get_buffer (vm, bi0);
1567           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1568
1569           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1570           rx_fib_index0 =
1571             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1572                                                  sw_if_index0);
1573
1574           if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1575             {
1576               next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1577               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1578               goto trace0;
1579             }
1580
1581           ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1582           udp0 = ip4_next_header (ip0);
1583           tcp0 = (tcp_header_t *) udp0;
1584           icmp0 = (icmp46_header_t *) udp0;
1585           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1586
1587           reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1588                                                  ip0->dst_address,
1589                                                  ip0->fragment_id,
1590                                                  ip0->protocol,
1591                                                  1, &fragments_to_drop);
1592
1593           if (PREDICT_FALSE (!reass0))
1594             {
1595               next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1596               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1597               nat_log_notice ("maximum reassemblies exceeded");
1598               goto trace0;
1599             }
1600
1601           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1602             {
1603               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1604                 {
1605                   next0 = icmp_out2in_ed_slow_path
1606                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1607                      next0, now, thread_index, &s0);
1608
1609                   if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1610                     {
1611                       if (s0)
1612                         reass0->sess_index = s0 - per_thread_data->sessions;
1613                       else
1614                         reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1615                       reass0->thread_index = thread_index;
1616                       nat_ip4_reass_get_frags (reass0,
1617                                                &fragments_to_loopback);
1618                     }
1619
1620                   goto trace0;
1621                 }
1622
1623               make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1624                           ip0->protocol, rx_fib_index0, udp0->dst_port,
1625                           udp0->src_port);
1626
1627               if (clib_bihash_search_16_8
1628                   (&per_thread_data->out2in_ed, &kv0, &value0))
1629                 {
1630                   /* Try to match static mapping by external address and port,
1631                      destination address and port in packet */
1632                   e_key0.addr = ip0->dst_address;
1633                   e_key0.port = udp0->dst_port;
1634                   e_key0.protocol = proto0;
1635                   e_key0.fib_index = rx_fib_index0;
1636                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1637                                                  &twice_nat0, &lb0, 0))
1638                     {
1639                       /*
1640                        * Send DHCP packets to the ipv4 stack, or we won't
1641                        * be able to use dhcp client on the outside interface
1642                        */
1643                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1644                                          && (udp0->dst_port
1645                                              ==
1646                                              clib_host_to_net_u16
1647                                              (UDP_DST_PORT_dhcp_to_client))))
1648                         {
1649                           vnet_feature_next (&next0, b0);
1650                           goto trace0;
1651                         }
1652
1653                       if (!sm->forwarding_enabled)
1654                         {
1655                           b0->error =
1656                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1657                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1658                         }
1659                       else
1660                         {
1661                           if (next_src_nat (sm, ip0, ip0->protocol,
1662                                             udp0->src_port, udp0->dst_port,
1663                                             thread_index, rx_fib_index0))
1664                             {
1665                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1666                               goto trace0;
1667                             }
1668                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1669                                                  thread_index);
1670                           reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1671                           nat_ip4_reass_get_frags (reass0,
1672                                                    &fragments_to_loopback);
1673                         }
1674                       goto trace0;
1675                     }
1676
1677                   /* Create session initiated by host from external network */
1678                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1679                                                              e_key0, node,
1680                                                              thread_index,
1681                                                              twice_nat0, lb0,
1682                                                              now);
1683                   if (!s0)
1684                     {
1685                       b0->error =
1686                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1687                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1688                       goto trace0;
1689                     }
1690                   reass0->sess_index = s0 - per_thread_data->sessions;
1691                   reass0->thread_index = thread_index;
1692                 }
1693               else
1694                 {
1695                   s0 = pool_elt_at_index (per_thread_data->sessions,
1696                                           value0.value);
1697                   reass0->sess_index = value0.value;
1698                 }
1699               nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1700             }
1701           else
1702             {
1703               if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1704                 goto trace0;
1705               if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1706                 {
1707                   if (nat_ip4_reass_add_fragment
1708                       (reass0, bi0, &fragments_to_drop))
1709                     {
1710                       b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1711                       nat_log_notice
1712                         ("maximum fragments per reassembly exceeded");
1713                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1714                       goto trace0;
1715                     }
1716                   cached0 = 1;
1717                   goto trace0;
1718                 }
1719               s0 = pool_elt_at_index (per_thread_data->sessions,
1720                                       reass0->sess_index);
1721             }
1722
1723           old_addr0 = ip0->dst_address.as_u32;
1724           ip0->dst_address = s0->in2out.addr;
1725           new_addr0 = ip0->dst_address.as_u32;
1726           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1727
1728           sum0 = ip0->checksum;
1729           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1730                                  ip4_header_t,
1731                                  dst_address /* changed member */ );
1732           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1733             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1734                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1735                                    src_address);
1736           ip0->checksum = ip_csum_fold (sum0);
1737
1738           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1739             {
1740               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1741                 {
1742                   old_port0 = tcp0->dst_port;
1743                   tcp0->dst_port = s0->in2out.port;
1744                   new_port0 = tcp0->dst_port;
1745
1746                   sum0 = tcp0->checksum;
1747                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1748                                          ip4_header_t,
1749                                          dst_address /* changed member */ );
1750
1751                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1752                                          ip4_header_t /* cheat */ ,
1753                                          length /* changed member */ );
1754                   if (is_twice_nat_session (s0))
1755                     {
1756                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1757                                              s0->ext_host_nat_addr.as_u32,
1758                                              ip4_header_t, dst_address);
1759                       sum0 = ip_csum_update (sum0, tcp0->src_port,
1760                                              s0->ext_host_nat_port,
1761                                              ip4_header_t, length);
1762                       tcp0->src_port = s0->ext_host_nat_port;
1763                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1764                     }
1765                   tcp0->checksum = ip_csum_fold (sum0);
1766                 }
1767               else
1768                 {
1769                   old_port0 = udp0->dst_port;
1770                   udp0->dst_port = s0->in2out.port;
1771                   if (is_twice_nat_session (s0))
1772                     {
1773                       udp0->src_port = s0->ext_host_nat_port;
1774                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1775                     }
1776                   udp0->checksum = 0;
1777                 }
1778             }
1779
1780           /* Accounting */
1781           nat44_session_update_counters (s0, now,
1782                                          vlib_buffer_length_in_chain (vm,
1783                                                                       b0));
1784           /* Per-user LRU list maintenance */
1785           nat44_session_update_lru (sm, s0, thread_index);
1786
1787         trace0:
1788           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1789                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1790             {
1791               nat44_reass_trace_t *t =
1792                 vlib_add_trace (vm, node, b0, sizeof (*t));
1793               t->cached = cached0;
1794               t->sw_if_index = sw_if_index0;
1795               t->next_index = next0;
1796             }
1797
1798           if (cached0)
1799             {
1800               n_left_to_next++;
1801               to_next--;
1802             }
1803           else
1804             {
1805               pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1806
1807               /* verify speculative enqueue, maybe switch current next frame */
1808               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1809                                                to_next, n_left_to_next,
1810                                                bi0, next0);
1811             }
1812
1813           if (n_left_from == 0 && vec_len (fragments_to_loopback))
1814             {
1815               from = vlib_frame_vector_args (frame);
1816               u32 len = vec_len (fragments_to_loopback);
1817               if (len <= VLIB_FRAME_SIZE)
1818                 {
1819                   clib_memcpy (from, fragments_to_loopback,
1820                                sizeof (u32) * len);
1821                   n_left_from = len;
1822                   vec_reset_length (fragments_to_loopback);
1823                 }
1824               else
1825                 {
1826                   clib_memcpy (from,
1827                                fragments_to_loopback + (len -
1828                                                         VLIB_FRAME_SIZE),
1829                                sizeof (u32) * VLIB_FRAME_SIZE);
1830                   n_left_from = VLIB_FRAME_SIZE;
1831                   _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1832                 }
1833             }
1834         }
1835
1836       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1837     }
1838
1839   vlib_node_increment_counter (vm, nat44_ed_out2in_reass_node.index,
1840                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1841                                pkts_processed);
1842
1843   nat_send_all_to_node (vm, fragments_to_drop, node,
1844                         &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1845                         NAT44_ED_OUT2IN_NEXT_DROP);
1846
1847   vec_free (fragments_to_drop);
1848   vec_free (fragments_to_loopback);
1849   return frame->n_vectors;
1850 }
1851
1852 /* *INDENT-OFF* */
1853 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1854   .function = nat44_ed_out2in_reass_node_fn,
1855   .name = "nat44-ed-out2in-reass",
1856   .vector_size = sizeof (u32),
1857   .format_trace = format_nat44_reass_trace,
1858   .type = VLIB_NODE_TYPE_INTERNAL,
1859   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1860   .error_strings = nat_out2in_ed_error_strings,
1861   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1862   .next_nodes = {
1863     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1864     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1865     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1866     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1867     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1868     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1869   },
1870 };
1871 /* *INDENT-ON* */
1872
1873 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_reass_node,
1874                               nat44_ed_out2in_reass_node_fn);
1875
1876 /*
1877  * fd.io coding-style-patch-verification: ON
1878  *
1879  * Local Variables:
1880  * eval: (c-set-style "gnu")
1881  * End:
1882  */