NAT44: do not create session record for identity mapping (VPP-1439)
[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, identity_nat;
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, &identity_nat))
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       if (PREDICT_FALSE (identity_nat))
537         {
538           dont_translate = 1;
539           goto out;
540         }
541
542       /* Create session initiated by host from external network */
543       s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
544                                                 thread_index, 0, 0,
545                                                 vlib_time_now
546                                                 (sm->vlib_main));
547
548       if (!s)
549         {
550           next = NAT44_ED_OUT2IN_NEXT_DROP;
551           goto out;
552         }
553     }
554   else
555     {
556       if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
557                          icmp->type != ICMP4_echo_request &&
558                          !icmp_is_error_message (icmp)))
559         {
560           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
561           next = NAT44_ED_OUT2IN_NEXT_DROP;
562           goto out;
563         }
564
565       s = pool_elt_at_index (tsm->sessions, value.value);
566     }
567
568   *p_proto = ip_proto_to_snat_proto (key.proto);
569 out:
570   if (s)
571     *p_value = s->in2out;
572   *p_dont_translate = dont_translate;
573   if (d)
574     *(snat_session_t **) d = s;
575   return next;
576 }
577
578 static snat_session_t *
579 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
580                                vlib_buffer_t * b,
581                                ip4_header_t * ip,
582                                u32 rx_fib_index,
583                                u32 thread_index,
584                                f64 now,
585                                vlib_main_t * vm, vlib_node_runtime_t * node)
586 {
587   clib_bihash_kv_8_8_t kv, value;
588   clib_bihash_kv_16_8_t s_kv, s_value;
589   snat_static_mapping_t *m;
590   u32 old_addr, new_addr;
591   ip_csum_t sum;
592   snat_session_t *s;
593   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
594   snat_user_t *u;
595
596   old_addr = ip->dst_address.as_u32;
597
598   make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
599               rx_fib_index, 0, 0);
600
601   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
602     {
603       s = pool_elt_at_index (tsm->sessions, s_value.value);
604       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
605     }
606   else
607     {
608       if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
609         {
610           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
611           nat_log_notice ("maximum sessions exceeded");
612           return 0;
613         }
614
615       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
616       if (clib_bihash_search_8_8
617           (&sm->static_mapping_by_external, &kv, &value))
618         {
619           b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
620           return 0;
621         }
622
623       m = pool_elt_at_index (sm->static_mappings, value.value);
624
625       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
626
627       u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
628                                   thread_index);
629       if (!u)
630         {
631           nat_log_warn ("create NAT user failed");
632           return 0;
633         }
634
635       /* Create a new session */
636       s = nat_ed_session_alloc (sm, u, thread_index, now);
637       if (!s)
638         {
639           nat44_delete_user_with_no_session (sm, u, thread_index);
640           nat_log_warn ("create NAT session failed");
641           return 0;
642         }
643
644       s->ext_host_addr.as_u32 = ip->src_address.as_u32;
645       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
646       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
647       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
648       s->out2in.addr.as_u32 = old_addr;
649       s->out2in.fib_index = rx_fib_index;
650       s->in2out.addr.as_u32 = new_addr;
651       s->in2out.fib_index = m->fib_index;
652       s->in2out.port = s->out2in.port = ip->protocol;
653       user_session_increment (sm, u, 1);
654
655       /* Add to lookup tables */
656       s_kv.value = s - tsm->sessions;
657       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
658         nat_log_notice ("out2in key add failed");
659
660       make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
661                   m->fib_index, 0, 0);
662       s_kv.value = s - tsm->sessions;
663       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
664         nat_log_notice ("in2out key add failed");
665     }
666
667   /* Update IP checksum */
668   sum = ip->checksum;
669   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
670   ip->checksum = ip_csum_fold (sum);
671
672   vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
673
674   /* Accounting */
675   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
676   /* Per-user LRU list maintenance */
677   nat44_session_update_lru (sm, s, thread_index);
678
679   return s;
680 }
681
682 static inline uword
683 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
684                                 vlib_node_runtime_t * node,
685                                 vlib_frame_t * frame, int is_slow_path)
686 {
687   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
688   nat44_ed_out2in_next_t next_index;
689   snat_main_t *sm = &snat_main;
690   f64 now = vlib_time_now (vm);
691   u32 thread_index = vm->thread_index;
692   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
693
694   stats_node_index = is_slow_path ? nat44_ed_out2in_slowpath_node.index :
695     nat44_ed_out2in_node.index;
696
697   from = vlib_frame_vector_args (frame);
698   n_left_from = frame->n_vectors;
699   next_index = node->cached_next_index;
700
701   while (n_left_from > 0)
702     {
703       u32 n_left_to_next;
704
705       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
706
707       while (n_left_from >= 4 && n_left_to_next >= 2)
708         {
709           u32 bi0, bi1;
710           vlib_buffer_t *b0, *b1;
711           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
712             new_addr0;
713           u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
714             new_addr1;
715           u16 old_port0, new_port0, old_port1, new_port1;
716           ip4_header_t *ip0, *ip1;
717           udp_header_t *udp0, *udp1;
718           tcp_header_t *tcp0, *tcp1;
719           icmp46_header_t *icmp0, *icmp1;
720           snat_session_t *s0 = 0, *s1 = 0;
721           clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
722           ip_csum_t sum0, sum1;
723           snat_session_key_t e_key0, l_key0, e_key1, l_key1;
724           lb_nat_type_t lb_nat0, lb_nat1;
725           twice_nat_type_t twice_nat0, twice_nat1;
726           u8 identity_nat0, identity_nat1;
727
728           /* Prefetch next iteration. */
729           {
730             vlib_buffer_t *p2, *p3;
731
732             p2 = vlib_get_buffer (vm, from[2]);
733             p3 = vlib_get_buffer (vm, from[3]);
734
735             vlib_prefetch_buffer_header (p2, LOAD);
736             vlib_prefetch_buffer_header (p3, LOAD);
737
738             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
739             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
740           }
741
742           /* speculatively enqueue b0 and b1 to the current next frame */
743           to_next[0] = bi0 = from[0];
744           to_next[1] = bi1 = from[1];
745           from += 2;
746           to_next += 2;
747           n_left_from -= 2;
748           n_left_to_next -= 2;
749
750           b0 = vlib_get_buffer (vm, bi0);
751           b1 = vlib_get_buffer (vm, bi1);
752
753           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
754           vnet_buffer (b0)->snat.flags = 0;
755           ip0 = vlib_buffer_get_current (b0);
756
757           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
758           rx_fib_index0 =
759             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
760                                                  sw_if_index0);
761
762           if (PREDICT_FALSE (ip0->ttl == 1))
763             {
764               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
765               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
766                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
767                                            0);
768               next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
769               goto trace00;
770             }
771
772           udp0 = ip4_next_header (ip0);
773           tcp0 = (tcp_header_t *) udp0;
774           icmp0 = (icmp46_header_t *) udp0;
775           proto0 = ip_proto_to_snat_proto (ip0->protocol);
776
777           if (is_slow_path)
778             {
779               if (PREDICT_FALSE (proto0 == ~0))
780                 {
781                   s0 =
782                     nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
783                                                    thread_index, now, vm,
784                                                    node);
785                   if (!sm->forwarding_enabled)
786                     {
787                       if (!s0)
788                         next0 = NAT44_ED_OUT2IN_NEXT_DROP;
789                       goto trace00;
790                     }
791                 }
792
793               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
794                 {
795                   next0 = icmp_out2in_ed_slow_path
796                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
797                      next0, now, thread_index, &s0);
798                   goto trace00;
799                 }
800             }
801           else
802             {
803               if (PREDICT_FALSE (proto0 == ~0))
804                 {
805                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
806                   goto trace00;
807                 }
808
809               if (ip4_is_fragment (ip0))
810                 {
811                   next0 = NAT44_ED_OUT2IN_NEXT_REASS;
812                   goto trace00;
813                 }
814
815               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
816                 {
817                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
818                   goto trace00;
819                 }
820             }
821
822           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
823                       ip0->protocol, rx_fib_index0, udp0->dst_port,
824                       udp0->src_port);
825
826           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
827             {
828               if (is_slow_path)
829                 {
830                   /* Try to match static mapping by external address and port,
831                      destination address and port in packet */
832                   e_key0.addr = ip0->dst_address;
833                   e_key0.port = udp0->dst_port;
834                   e_key0.protocol = proto0;
835                   e_key0.fib_index = rx_fib_index0;
836                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
837                                                  &twice_nat0, &lb_nat0,
838                                                  &ip0->src_address,
839                                                  &identity_nat0))
840                     {
841                       /*
842                        * Send DHCP packets to the ipv4 stack, or we won't
843                        * be able to use dhcp client on the outside interface
844                        */
845                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
846                                          && (udp0->dst_port ==
847                                              clib_host_to_net_u16
848                                              (UDP_DST_PORT_dhcp_to_client))))
849                         {
850                           vnet_feature_next (&next0, b0);
851                           goto trace00;
852                         }
853
854                       if (!sm->forwarding_enabled)
855                         {
856                           b0->error =
857                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
858                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
859                         }
860                       else
861                         {
862                           if (next_src_nat (sm, ip0, ip0->protocol,
863                                             udp0->src_port, udp0->dst_port,
864                                             thread_index, rx_fib_index0))
865                             {
866                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
867                               goto trace00;
868                             }
869                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
870                                                  thread_index);
871                         }
872                       goto trace00;
873                     }
874
875                   if (PREDICT_FALSE (identity_nat0))
876                     goto trace00;
877
878                   /* Create session initiated by host from external network */
879                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
880                                                              e_key0, node,
881                                                              thread_index,
882                                                              twice_nat0,
883                                                              lb_nat0, now);
884
885                   if (!s0)
886                     {
887                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
888                       goto trace00;
889                     }
890                 }
891               else
892                 {
893                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
894                   goto trace00;
895                 }
896             }
897           else
898             {
899               s0 = pool_elt_at_index (tsm->sessions, value0.value);
900             }
901
902           old_addr0 = ip0->dst_address.as_u32;
903           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
904           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
905
906           sum0 = ip0->checksum;
907           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
908                                  dst_address);
909           if (PREDICT_FALSE (is_twice_nat_session (s0)))
910             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
911                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
912                                    src_address);
913           ip0->checksum = ip_csum_fold (sum0);
914
915           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
916             {
917               old_port0 = tcp0->dst_port;
918               new_port0 = tcp0->dst_port = s0->in2out.port;
919
920               sum0 = tcp0->checksum;
921               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
922                                      dst_address);
923               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
924                                      length);
925               if (is_twice_nat_session (s0))
926                 {
927                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
928                                          s0->ext_host_nat_addr.as_u32,
929                                          ip4_header_t, dst_address);
930                   sum0 = ip_csum_update (sum0, tcp0->src_port,
931                                          s0->ext_host_nat_port, ip4_header_t,
932                                          length);
933                   tcp0->src_port = s0->ext_host_nat_port;
934                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
935                 }
936               tcp0->checksum = ip_csum_fold (sum0);
937               if (nat44_set_tcp_session_state_o2i
938                   (sm, s0, tcp0, thread_index))
939                 goto trace00;
940             }
941           else
942             {
943               udp0->dst_port = s0->in2out.port;
944               if (is_twice_nat_session (s0))
945                 {
946                   udp0->src_port = s0->ext_host_nat_port;
947                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
948                 }
949               udp0->checksum = 0;
950             }
951
952           /* Accounting */
953           nat44_session_update_counters (s0, now,
954                                          vlib_buffer_length_in_chain (vm,
955                                                                       b0));
956           /* Per-user LRU list maintenance */
957           nat44_session_update_lru (sm, s0, thread_index);
958
959         trace00:
960           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
961                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
962             {
963               nat44_ed_out2in_trace_t *t =
964                 vlib_add_trace (vm, node, b0, sizeof (*t));
965               t->is_slow_path = is_slow_path;
966               t->sw_if_index = sw_if_index0;
967               t->next_index = next0;
968               t->session_index = ~0;
969               if (s0)
970                 t->session_index = s0 - tsm->sessions;
971             }
972
973           pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
974
975           next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
976           vnet_buffer (b1)->snat.flags = 0;
977           ip1 = vlib_buffer_get_current (b1);
978
979           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
980           rx_fib_index1 =
981             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
982                                                  sw_if_index1);
983
984           if (PREDICT_FALSE (ip1->ttl == 1))
985             {
986               vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
987               icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
988                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
989                                            0);
990               next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
991               goto trace01;
992             }
993
994           udp1 = ip4_next_header (ip1);
995           tcp1 = (tcp_header_t *) udp1;
996           icmp1 = (icmp46_header_t *) udp1;
997           proto1 = ip_proto_to_snat_proto (ip1->protocol);
998
999           if (is_slow_path)
1000             {
1001               if (PREDICT_FALSE (proto1 == ~0))
1002                 {
1003                   s1 =
1004                     nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1005                                                    thread_index, now, vm,
1006                                                    node);
1007                   if (!sm->forwarding_enabled)
1008                     {
1009                       if (!s1)
1010                         next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1011                       goto trace01;
1012                     }
1013                 }
1014
1015               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1016                 {
1017                   next1 = icmp_out2in_ed_slow_path
1018                     (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1019                      next1, now, thread_index, &s1);
1020                   goto trace01;
1021                 }
1022             }
1023           else
1024             {
1025               if (PREDICT_FALSE (proto1 == ~0))
1026                 {
1027                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1028                   goto trace01;
1029                 }
1030
1031               if (ip4_is_fragment (ip1))
1032                 {
1033                   next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1034                   goto trace01;
1035                 }
1036
1037               if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1038                 {
1039                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1040                   goto trace01;
1041                 }
1042             }
1043
1044           make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1045                       ip1->protocol, rx_fib_index1, udp1->dst_port,
1046                       udp1->src_port);
1047
1048           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1049             {
1050               if (is_slow_path)
1051                 {
1052                   /* Try to match static mapping by external address and port,
1053                      destination address and port in packet */
1054                   e_key1.addr = ip1->dst_address;
1055                   e_key1.port = udp1->dst_port;
1056                   e_key1.protocol = proto1;
1057                   e_key1.fib_index = rx_fib_index1;
1058                   if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1059                                                  &twice_nat1, &lb_nat1,
1060                                                  &ip1->src_address,
1061                                                  &identity_nat1))
1062                     {
1063                       /*
1064                        * Send DHCP packets to the ipv4 stack, or we won't
1065                        * be able to use dhcp client on the outside interface
1066                        */
1067                       if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1068                                          && (udp1->dst_port ==
1069                                              clib_host_to_net_u16
1070                                              (UDP_DST_PORT_dhcp_to_client))))
1071                         {
1072                           vnet_feature_next (&next1, b1);
1073                           goto trace01;
1074                         }
1075
1076                       if (!sm->forwarding_enabled)
1077                         {
1078                           b1->error =
1079                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1080                           next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1081                         }
1082                       else
1083                         {
1084                           if (next_src_nat (sm, ip1, ip1->protocol,
1085                                             udp1->src_port, udp1->dst_port,
1086                                             thread_index, rx_fib_index1))
1087                             {
1088                               next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1089                               goto trace01;
1090                             }
1091                           create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1092                                                  thread_index);
1093                         }
1094                       goto trace01;
1095                     }
1096
1097                   if (PREDICT_FALSE (identity_nat1))
1098                     goto trace01;
1099
1100                   /* Create session initiated by host from external network */
1101                   s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1102                                                              e_key1, node,
1103                                                              thread_index,
1104                                                              twice_nat1,
1105                                                              lb_nat1, now);
1106
1107                   if (!s1)
1108                     {
1109                       next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1110                       goto trace01;
1111                     }
1112                 }
1113               else
1114                 {
1115                   next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1116                   goto trace01;
1117                 }
1118             }
1119           else
1120             {
1121               s1 = pool_elt_at_index (tsm->sessions, value1.value);
1122             }
1123
1124           old_addr1 = ip1->dst_address.as_u32;
1125           new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1126           vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1127
1128           sum1 = ip1->checksum;
1129           sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1130                                  dst_address);
1131           if (PREDICT_FALSE (is_twice_nat_session (s1)))
1132             sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1133                                    s1->ext_host_nat_addr.as_u32, ip4_header_t,
1134                                    src_address);
1135           ip1->checksum = ip_csum_fold (sum1);
1136
1137           if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1138             {
1139               old_port1 = tcp1->dst_port;
1140               new_port1 = tcp1->dst_port = s1->in2out.port;
1141
1142               sum1 = tcp1->checksum;
1143               sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1144                                      dst_address);
1145               sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1146                                      length);
1147               if (is_twice_nat_session (s1))
1148                 {
1149                   sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1150                                          s1->ext_host_nat_addr.as_u32,
1151                                          ip4_header_t, dst_address);
1152                   sum1 = ip_csum_update (sum1, tcp1->src_port,
1153                                          s1->ext_host_nat_port, ip4_header_t,
1154                                          length);
1155                   tcp1->src_port = s1->ext_host_nat_port;
1156                   ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1157                 }
1158               tcp1->checksum = ip_csum_fold (sum1);
1159               if (nat44_set_tcp_session_state_o2i
1160                   (sm, s1, tcp1, thread_index))
1161                 goto trace01;
1162             }
1163           else
1164             {
1165               udp1->dst_port = s1->in2out.port;
1166               if (is_twice_nat_session (s1))
1167                 {
1168                   udp1->src_port = s1->ext_host_nat_port;
1169                   ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1170                 }
1171               udp1->checksum = 0;
1172             }
1173
1174           /* Accounting */
1175           nat44_session_update_counters (s1, now,
1176                                          vlib_buffer_length_in_chain (vm,
1177                                                                       b1));
1178           /* Per-user LRU list maintenance */
1179           nat44_session_update_lru (sm, s1, thread_index);
1180
1181         trace01:
1182           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1183                              && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1184             {
1185               nat44_ed_out2in_trace_t *t =
1186                 vlib_add_trace (vm, node, b1, sizeof (*t));
1187               t->is_slow_path = is_slow_path;
1188               t->sw_if_index = sw_if_index1;
1189               t->next_index = next1;
1190               t->session_index = ~0;
1191               if (s1)
1192                 t->session_index = s1 - tsm->sessions;
1193             }
1194
1195           pkts_processed += next1 != NAT44_ED_OUT2IN_NEXT_DROP;
1196
1197           /* verify speculative enqueues, maybe switch current next frame */
1198           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1199                                            to_next, n_left_to_next,
1200                                            bi0, bi1, next0, next1);
1201         }
1202
1203       while (n_left_from > 0 && n_left_to_next > 0)
1204         {
1205           u32 bi0;
1206           vlib_buffer_t *b0;
1207           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1208             new_addr0;
1209           u16 old_port0, new_port0;
1210           ip4_header_t *ip0;
1211           udp_header_t *udp0;
1212           tcp_header_t *tcp0;
1213           icmp46_header_t *icmp0;
1214           snat_session_t *s0 = 0;
1215           clib_bihash_kv_16_8_t kv0, value0;
1216           ip_csum_t sum0;
1217           snat_session_key_t e_key0, l_key0;
1218           lb_nat_type_t lb_nat0;
1219           twice_nat_type_t twice_nat0;
1220           u8 identity_nat0;
1221
1222           /* speculatively enqueue b0 to the current next frame */
1223           bi0 = from[0];
1224           to_next[0] = bi0;
1225           from += 1;
1226           to_next += 1;
1227           n_left_from -= 1;
1228           n_left_to_next -= 1;
1229
1230           b0 = vlib_get_buffer (vm, bi0);
1231           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1232           vnet_buffer (b0)->snat.flags = 0;
1233           ip0 = vlib_buffer_get_current (b0);
1234
1235           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1236           rx_fib_index0 =
1237             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1238                                                  sw_if_index0);
1239
1240           if (PREDICT_FALSE (ip0->ttl == 1))
1241             {
1242               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1243               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1244                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1245                                            0);
1246               next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1247               goto trace0;
1248             }
1249
1250           udp0 = ip4_next_header (ip0);
1251           tcp0 = (tcp_header_t *) udp0;
1252           icmp0 = (icmp46_header_t *) udp0;
1253           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1254
1255           if (is_slow_path)
1256             {
1257               if (PREDICT_FALSE (proto0 == ~0))
1258                 {
1259                   s0 =
1260                     nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1261                                                    thread_index, now, vm,
1262                                                    node);
1263                   if (!sm->forwarding_enabled)
1264                     {
1265                       if (!s0)
1266                         next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1267                       goto trace0;
1268                     }
1269                 }
1270
1271               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1272                 {
1273                   next0 = icmp_out2in_ed_slow_path
1274                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1275                      next0, now, thread_index, &s0);
1276                   goto trace0;
1277                 }
1278             }
1279           else
1280             {
1281               if (PREDICT_FALSE (proto0 == ~0))
1282                 {
1283                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1284                   goto trace0;
1285                 }
1286
1287               if (ip4_is_fragment (ip0))
1288                 {
1289                   next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1290                   goto trace0;
1291                 }
1292
1293               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1294                 {
1295                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1296                   goto trace0;
1297                 }
1298             }
1299
1300           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1301                       ip0->protocol, rx_fib_index0, udp0->dst_port,
1302                       udp0->src_port);
1303
1304           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1305             {
1306               if (is_slow_path)
1307                 {
1308                   /* Try to match static mapping by external address and port,
1309                      destination address and port in packet */
1310                   e_key0.addr = ip0->dst_address;
1311                   e_key0.port = udp0->dst_port;
1312                   e_key0.protocol = proto0;
1313                   e_key0.fib_index = rx_fib_index0;
1314                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1315                                                  &twice_nat0, &lb_nat0,
1316                                                  &ip0->src_address,
1317                                                  &identity_nat0))
1318                     {
1319                       /*
1320                        * Send DHCP packets to the ipv4 stack, or we won't
1321                        * be able to use dhcp client on the outside interface
1322                        */
1323                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1324                                          && (udp0->dst_port ==
1325                                              clib_host_to_net_u16
1326                                              (UDP_DST_PORT_dhcp_to_client))))
1327                         {
1328                           vnet_feature_next (&next0, b0);
1329                           goto trace0;
1330                         }
1331
1332                       if (!sm->forwarding_enabled)
1333                         {
1334                           b0->error =
1335                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1336                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1337                         }
1338                       else
1339                         {
1340                           if (next_src_nat (sm, ip0, ip0->protocol,
1341                                             udp0->src_port, udp0->dst_port,
1342                                             thread_index, rx_fib_index0))
1343                             {
1344                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1345                               goto trace0;
1346                             }
1347                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1348                                                  thread_index);
1349                         }
1350                       goto trace0;
1351                     }
1352
1353                   if (PREDICT_FALSE (identity_nat0))
1354                     goto trace0;
1355
1356                   /* Create session initiated by host from external network */
1357                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1358                                                              e_key0, node,
1359                                                              thread_index,
1360                                                              twice_nat0,
1361                                                              lb_nat0, now);
1362
1363                   if (!s0)
1364                     {
1365                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1366                       goto trace0;
1367                     }
1368                 }
1369               else
1370                 {
1371                   next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1372                   goto trace0;
1373                 }
1374             }
1375           else
1376             {
1377               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1378             }
1379
1380           old_addr0 = ip0->dst_address.as_u32;
1381           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1382           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1383
1384           sum0 = ip0->checksum;
1385           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1386                                  dst_address);
1387           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1388             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1389                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1390                                    src_address);
1391           ip0->checksum = ip_csum_fold (sum0);
1392
1393           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1394             {
1395               old_port0 = tcp0->dst_port;
1396               new_port0 = tcp0->dst_port = s0->in2out.port;
1397
1398               sum0 = tcp0->checksum;
1399               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1400                                      dst_address);
1401               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1402                                      length);
1403               if (is_twice_nat_session (s0))
1404                 {
1405                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1406                                          s0->ext_host_nat_addr.as_u32,
1407                                          ip4_header_t, dst_address);
1408                   sum0 = ip_csum_update (sum0, tcp0->src_port,
1409                                          s0->ext_host_nat_port, ip4_header_t,
1410                                          length);
1411                   tcp0->src_port = s0->ext_host_nat_port;
1412                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1413                 }
1414               tcp0->checksum = ip_csum_fold (sum0);
1415               if (nat44_set_tcp_session_state_o2i
1416                   (sm, s0, tcp0, thread_index))
1417                 goto trace0;
1418             }
1419           else
1420             {
1421               udp0->dst_port = s0->in2out.port;
1422               if (is_twice_nat_session (s0))
1423                 {
1424                   udp0->src_port = s0->ext_host_nat_port;
1425                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1426                 }
1427               udp0->checksum = 0;
1428             }
1429
1430           /* Accounting */
1431           nat44_session_update_counters (s0, now,
1432                                          vlib_buffer_length_in_chain (vm,
1433                                                                       b0));
1434           /* Per-user LRU list maintenance */
1435           nat44_session_update_lru (sm, s0, thread_index);
1436
1437         trace0:
1438           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1439                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1440             {
1441               nat44_ed_out2in_trace_t *t =
1442                 vlib_add_trace (vm, node, b0, sizeof (*t));
1443               t->is_slow_path = is_slow_path;
1444               t->sw_if_index = sw_if_index0;
1445               t->next_index = next0;
1446               t->session_index = ~0;
1447               if (s0)
1448                 t->session_index = s0 - tsm->sessions;
1449             }
1450
1451           pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1452           /* verify speculative enqueue, maybe switch current next frame */
1453           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1454                                            to_next, n_left_to_next,
1455                                            bi0, next0);
1456         }
1457
1458       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1459     }
1460
1461   vlib_node_increment_counter (vm, stats_node_index,
1462                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1463                                pkts_processed);
1464   return frame->n_vectors;
1465 }
1466
1467 static uword
1468 nat44_ed_out2in_fast_path_fn (vlib_main_t * vm,
1469                               vlib_node_runtime_t * node,
1470                               vlib_frame_t * frame)
1471 {
1472   return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1473 }
1474
1475 /* *INDENT-OFF* */
1476 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1477   .function = nat44_ed_out2in_fast_path_fn,
1478   .name = "nat44-ed-out2in",
1479   .vector_size = sizeof (u32),
1480   .format_trace = format_nat44_ed_out2in_trace,
1481   .type = VLIB_NODE_TYPE_INTERNAL,
1482   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1483   .error_strings = nat_out2in_ed_error_strings,
1484   .runtime_data_bytes = sizeof (snat_runtime_t),
1485   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1486   .next_nodes = {
1487     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1488     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1489     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1490     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1491     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1492     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1493   },
1494 };
1495 /* *INDENT-ON* */
1496
1497 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_node,
1498                               nat44_ed_out2in_fast_path_fn);
1499
1500 static uword
1501 nat44_ed_out2in_slow_path_fn (vlib_main_t * vm,
1502                               vlib_node_runtime_t * node,
1503                               vlib_frame_t * frame)
1504 {
1505   return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1506 }
1507
1508 /* *INDENT-OFF* */
1509 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1510   .function = nat44_ed_out2in_slow_path_fn,
1511   .name = "nat44-ed-out2in-slowpath",
1512   .vector_size = sizeof (u32),
1513   .format_trace = format_nat44_ed_out2in_trace,
1514   .type = VLIB_NODE_TYPE_INTERNAL,
1515   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1516   .error_strings = nat_out2in_ed_error_strings,
1517   .runtime_data_bytes = sizeof (snat_runtime_t),
1518   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1519   .next_nodes = {
1520     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1521     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1522     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1523     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1524     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1525     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1526   },
1527 };
1528 /* *INDENT-ON* */
1529
1530 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_slowpath_node,
1531                               nat44_ed_out2in_slow_path_fn);
1532
1533 static uword
1534 nat44_ed_out2in_reass_node_fn (vlib_main_t * vm,
1535                                vlib_node_runtime_t * node,
1536                                vlib_frame_t * frame)
1537 {
1538   u32 n_left_from, *from, *to_next;
1539   nat44_ed_out2in_next_t next_index;
1540   u32 pkts_processed = 0;
1541   snat_main_t *sm = &snat_main;
1542   f64 now = vlib_time_now (vm);
1543   u32 thread_index = vm->thread_index;
1544   snat_main_per_thread_data_t *per_thread_data =
1545     &sm->per_thread_data[thread_index];
1546   u32 *fragments_to_drop = 0;
1547   u32 *fragments_to_loopback = 0;
1548
1549   from = vlib_frame_vector_args (frame);
1550   n_left_from = frame->n_vectors;
1551   next_index = node->cached_next_index;
1552
1553   while (n_left_from > 0)
1554     {
1555       u32 n_left_to_next;
1556
1557       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1558
1559       while (n_left_from > 0 && n_left_to_next > 0)
1560         {
1561           u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1562           vlib_buffer_t *b0;
1563           u32 next0;
1564           u8 cached0 = 0;
1565           ip4_header_t *ip0;
1566           nat_reass_ip4_t *reass0;
1567           udp_header_t *udp0;
1568           tcp_header_t *tcp0;
1569           icmp46_header_t *icmp0;
1570           clib_bihash_kv_16_8_t kv0, value0;
1571           snat_session_t *s0 = 0;
1572           u16 old_port0, new_port0;
1573           ip_csum_t sum0;
1574           snat_session_key_t e_key0, l_key0;
1575           lb_nat_type_t lb0;
1576           twice_nat_type_t twice_nat0;
1577           u8 identity_nat0;
1578
1579           /* speculatively enqueue b0 to the current next frame */
1580           bi0 = from[0];
1581           to_next[0] = bi0;
1582           from += 1;
1583           to_next += 1;
1584           n_left_from -= 1;
1585           n_left_to_next -= 1;
1586
1587           b0 = vlib_get_buffer (vm, bi0);
1588           next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1589
1590           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1591           rx_fib_index0 =
1592             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1593                                                  sw_if_index0);
1594
1595           if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1596             {
1597               next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1598               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1599               goto trace0;
1600             }
1601
1602           ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1603           udp0 = ip4_next_header (ip0);
1604           tcp0 = (tcp_header_t *) udp0;
1605           icmp0 = (icmp46_header_t *) udp0;
1606           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1607
1608           reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1609                                                  ip0->dst_address,
1610                                                  ip0->fragment_id,
1611                                                  ip0->protocol,
1612                                                  1, &fragments_to_drop);
1613
1614           if (PREDICT_FALSE (!reass0))
1615             {
1616               next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1617               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1618               nat_log_notice ("maximum reassemblies exceeded");
1619               goto trace0;
1620             }
1621
1622           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1623             {
1624               if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1625                 {
1626                   next0 = icmp_out2in_ed_slow_path
1627                     (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1628                      next0, now, thread_index, &s0);
1629
1630                   if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1631                     {
1632                       if (s0)
1633                         reass0->sess_index = s0 - per_thread_data->sessions;
1634                       else
1635                         reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1636                       reass0->thread_index = thread_index;
1637                       nat_ip4_reass_get_frags (reass0,
1638                                                &fragments_to_loopback);
1639                     }
1640
1641                   goto trace0;
1642                 }
1643
1644               make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1645                           ip0->protocol, rx_fib_index0, udp0->dst_port,
1646                           udp0->src_port);
1647
1648               if (clib_bihash_search_16_8
1649                   (&per_thread_data->out2in_ed, &kv0, &value0))
1650                 {
1651                   /* Try to match static mapping by external address and port,
1652                      destination address and port in packet */
1653                   e_key0.addr = ip0->dst_address;
1654                   e_key0.port = udp0->dst_port;
1655                   e_key0.protocol = proto0;
1656                   e_key0.fib_index = rx_fib_index0;
1657                   if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1658                                                  &twice_nat0, &lb0, 0,
1659                                                  &identity_nat0))
1660                     {
1661                       /*
1662                        * Send DHCP packets to the ipv4 stack, or we won't
1663                        * be able to use dhcp client on the outside interface
1664                        */
1665                       if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1666                                          && (udp0->dst_port
1667                                              ==
1668                                              clib_host_to_net_u16
1669                                              (UDP_DST_PORT_dhcp_to_client))))
1670                         {
1671                           vnet_feature_next (&next0, b0);
1672                           goto trace0;
1673                         }
1674
1675                       if (!sm->forwarding_enabled)
1676                         {
1677                           b0->error =
1678                             node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1679                           next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1680                         }
1681                       else
1682                         {
1683                           if (next_src_nat (sm, ip0, ip0->protocol,
1684                                             udp0->src_port, udp0->dst_port,
1685                                             thread_index, rx_fib_index0))
1686                             {
1687                               next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1688                               goto trace0;
1689                             }
1690                           create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1691                                                  thread_index);
1692                           reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1693                           nat_ip4_reass_get_frags (reass0,
1694                                                    &fragments_to_loopback);
1695                         }
1696                       goto trace0;
1697                     }
1698
1699                   if (PREDICT_FALSE (identity_nat0))
1700                     {
1701                       reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1702                       goto trace0;
1703                     }
1704
1705                   /* Create session initiated by host from external network */
1706                   s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1707                                                              e_key0, node,
1708                                                              thread_index,
1709                                                              twice_nat0, lb0,
1710                                                              now);
1711                   if (!s0)
1712                     {
1713                       b0->error =
1714                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1715                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1716                       goto trace0;
1717                     }
1718                   reass0->sess_index = s0 - per_thread_data->sessions;
1719                   reass0->thread_index = thread_index;
1720                 }
1721               else
1722                 {
1723                   s0 = pool_elt_at_index (per_thread_data->sessions,
1724                                           value0.value);
1725                   reass0->sess_index = value0.value;
1726                 }
1727               nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1728             }
1729           else
1730             {
1731               if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1732                 goto trace0;
1733               if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1734                 {
1735                   if (nat_ip4_reass_add_fragment
1736                       (reass0, bi0, &fragments_to_drop))
1737                     {
1738                       b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1739                       nat_log_notice
1740                         ("maximum fragments per reassembly exceeded");
1741                       next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1742                       goto trace0;
1743                     }
1744                   cached0 = 1;
1745                   goto trace0;
1746                 }
1747               s0 = pool_elt_at_index (per_thread_data->sessions,
1748                                       reass0->sess_index);
1749             }
1750
1751           old_addr0 = ip0->dst_address.as_u32;
1752           ip0->dst_address = s0->in2out.addr;
1753           new_addr0 = ip0->dst_address.as_u32;
1754           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1755
1756           sum0 = ip0->checksum;
1757           sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1758                                  ip4_header_t,
1759                                  dst_address /* changed member */ );
1760           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1761             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1762                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1763                                    src_address);
1764           ip0->checksum = ip_csum_fold (sum0);
1765
1766           if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1767             {
1768               if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1769                 {
1770                   old_port0 = tcp0->dst_port;
1771                   tcp0->dst_port = s0->in2out.port;
1772                   new_port0 = tcp0->dst_port;
1773
1774                   sum0 = tcp0->checksum;
1775                   sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1776                                          ip4_header_t,
1777                                          dst_address /* changed member */ );
1778
1779                   sum0 = ip_csum_update (sum0, old_port0, new_port0,
1780                                          ip4_header_t /* cheat */ ,
1781                                          length /* changed member */ );
1782                   if (is_twice_nat_session (s0))
1783                     {
1784                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1785                                              s0->ext_host_nat_addr.as_u32,
1786                                              ip4_header_t, dst_address);
1787                       sum0 = ip_csum_update (sum0, tcp0->src_port,
1788                                              s0->ext_host_nat_port,
1789                                              ip4_header_t, length);
1790                       tcp0->src_port = s0->ext_host_nat_port;
1791                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1792                     }
1793                   tcp0->checksum = ip_csum_fold (sum0);
1794                 }
1795               else
1796                 {
1797                   old_port0 = udp0->dst_port;
1798                   udp0->dst_port = s0->in2out.port;
1799                   if (is_twice_nat_session (s0))
1800                     {
1801                       udp0->src_port = s0->ext_host_nat_port;
1802                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1803                     }
1804                   udp0->checksum = 0;
1805                 }
1806             }
1807
1808           /* Accounting */
1809           nat44_session_update_counters (s0, now,
1810                                          vlib_buffer_length_in_chain (vm,
1811                                                                       b0));
1812           /* Per-user LRU list maintenance */
1813           nat44_session_update_lru (sm, s0, thread_index);
1814
1815         trace0:
1816           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1817                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1818             {
1819               nat44_reass_trace_t *t =
1820                 vlib_add_trace (vm, node, b0, sizeof (*t));
1821               t->cached = cached0;
1822               t->sw_if_index = sw_if_index0;
1823               t->next_index = next0;
1824             }
1825
1826           if (cached0)
1827             {
1828               n_left_to_next++;
1829               to_next--;
1830             }
1831           else
1832             {
1833               pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1834
1835               /* verify speculative enqueue, maybe switch current next frame */
1836               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1837                                                to_next, n_left_to_next,
1838                                                bi0, next0);
1839             }
1840
1841           if (n_left_from == 0 && vec_len (fragments_to_loopback))
1842             {
1843               from = vlib_frame_vector_args (frame);
1844               u32 len = vec_len (fragments_to_loopback);
1845               if (len <= VLIB_FRAME_SIZE)
1846                 {
1847                   clib_memcpy (from, fragments_to_loopback,
1848                                sizeof (u32) * len);
1849                   n_left_from = len;
1850                   vec_reset_length (fragments_to_loopback);
1851                 }
1852               else
1853                 {
1854                   clib_memcpy (from,
1855                                fragments_to_loopback + (len -
1856                                                         VLIB_FRAME_SIZE),
1857                                sizeof (u32) * VLIB_FRAME_SIZE);
1858                   n_left_from = VLIB_FRAME_SIZE;
1859                   _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1860                 }
1861             }
1862         }
1863
1864       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1865     }
1866
1867   vlib_node_increment_counter (vm, nat44_ed_out2in_reass_node.index,
1868                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1869                                pkts_processed);
1870
1871   nat_send_all_to_node (vm, fragments_to_drop, node,
1872                         &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1873                         NAT44_ED_OUT2IN_NEXT_DROP);
1874
1875   vec_free (fragments_to_drop);
1876   vec_free (fragments_to_loopback);
1877   return frame->n_vectors;
1878 }
1879
1880 /* *INDENT-OFF* */
1881 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1882   .function = nat44_ed_out2in_reass_node_fn,
1883   .name = "nat44-ed-out2in-reass",
1884   .vector_size = sizeof (u32),
1885   .format_trace = format_nat44_reass_trace,
1886   .type = VLIB_NODE_TYPE_INTERNAL,
1887   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1888   .error_strings = nat_out2in_ed_error_strings,
1889   .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1890   .next_nodes = {
1891     [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1892     [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1893     [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1894     [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1895     [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1896     [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1897   },
1898 };
1899 /* *INDENT-ON* */
1900
1901 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_out2in_reass_node,
1902                               nat44_ed_out2in_reass_node_fn);
1903
1904 /*
1905  * fd.io coding-style-patch-verification: ON
1906  *
1907  * Local Variables:
1908  * eval: (c-set-style "gnu")
1909  * End:
1910  */