nat: fix unknown proto translation out2in_ed
[vpp.git] / src / plugins / nat / out2in_ed.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT44 endpoint-dependent outside to inside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
28 #include <nat/nat.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
34
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nat_out2in_ed_error
38 #undef _
39 };
40
41 typedef struct
42 {
43   u32 sw_if_index;
44   u32 next_index;
45   u32 session_index;
46   u32 is_slow_path;
47 } nat44_ed_out2in_trace_t;
48
49 static u8 *
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
51 {
52   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54   nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
55   char *tag;
56
57   tag =
58     t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59     "NAT44_OUT2IN_ED_FAST_PATH";
60
61   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62               t->sw_if_index, t->next_index, t->session_index);
63
64   return s;
65 }
66
67 static inline u32
68 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
69                           ip4_header_t * ip0, icmp46_header_t * icmp0,
70                           u32 sw_if_index0, u32 rx_fib_index0,
71                           vlib_node_runtime_t * node, u32 next0, f64 now,
72                           u32 thread_index, snat_session_t ** p_s0)
73 {
74   next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75                        next0, thread_index, p_s0, 0);
76   snat_session_t *s0 = *p_s0;
77   if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
78     {
79       /* Accounting */
80       nat44_session_update_counters (s0, now,
81                                      vlib_buffer_length_in_chain
82                                      (sm->vlib_main, b0), thread_index);
83       /* Per-user LRU list maintenance */
84       nat44_session_update_lru (sm, s0, thread_index);
85     }
86   return next0;
87 }
88
89 #ifndef CLIB_MARCH_VARIANT
90 int
91 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
92 {
93   snat_main_t *sm = &snat_main;
94   nat44_is_idle_session_ctx_t *ctx = arg;
95   snat_session_t *s;
96   u64 sess_timeout_time;
97   nat_ed_ses_key_t ed_key;
98   clib_bihash_kv_16_8_t ed_kv;
99   int i;
100   snat_address_t *a;
101   snat_session_key_t key;
102   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
103                                                        ctx->thread_index);
104
105   s = pool_elt_at_index (tsm->sessions, kv->value);
106   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
107   if (ctx->now >= sess_timeout_time)
108     {
109       ed_key.l_addr = s->in2out.addr;
110       ed_key.r_addr = s->ext_host_addr;
111       ed_key.fib_index = s->in2out.fib_index;
112       if (snat_is_unk_proto_session (s))
113         {
114           ed_key.proto = s->in2out.port;
115           ed_key.r_port = 0;
116           ed_key.l_port = 0;
117         }
118       else
119         {
120           ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
121           ed_key.l_port = s->in2out.port;
122           ed_key.r_port = s->ext_host_port;
123         }
124       if (is_twice_nat_session (s))
125         {
126           ed_key.r_addr = s->ext_host_nat_addr;
127           ed_key.r_port = s->ext_host_nat_port;
128         }
129       ed_kv.key[0] = ed_key.as_u64[0];
130       ed_kv.key[1] = ed_key.as_u64[1];
131       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
132         nat_elog_warn ("in2out_ed key del failed");
133
134       if (snat_is_unk_proto_session (s))
135         goto delete;
136
137       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
138                                            s->in2out.addr.as_u32,
139                                            s->out2in.addr.as_u32,
140                                            s->in2out.protocol,
141                                            s->in2out.port,
142                                            s->out2in.port,
143                                            s->in2out.fib_index);
144
145       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
146                              &s->in2out.addr, s->in2out.port,
147                              &s->ext_host_nat_addr, s->ext_host_nat_port,
148                              &s->out2in.addr, s->out2in.port,
149                              &s->ext_host_addr, s->ext_host_port,
150                              s->in2out.protocol, is_twice_nat_session (s));
151
152       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
153                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
154                    ctx->thread_index);
155
156       if (is_twice_nat_session (s))
157         {
158           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
159             {
160               key.protocol = s->in2out.protocol;
161               key.port = s->ext_host_nat_port;
162               a = sm->twice_nat_addresses + i;
163               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
164                 {
165                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
166                                                       ctx->thread_index,
167                                                       &key);
168                   break;
169                 }
170             }
171         }
172
173       if (snat_is_session_static (s))
174         goto delete;
175
176       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
177                                           &s->out2in);
178     delete:
179       nat44_delete_session (sm, s, ctx->thread_index);
180       return 1;
181     }
182
183   return 0;
184 }
185 #endif
186
187 static snat_session_t *
188 create_session_for_static_mapping_ed (snat_main_t * sm,
189                                       vlib_buffer_t * b,
190                                       snat_session_key_t l_key,
191                                       snat_session_key_t e_key,
192                                       vlib_node_runtime_t * node,
193                                       u32 thread_index,
194                                       twice_nat_type_t twice_nat,
195                                       lb_nat_type_t lb_nat, f64 now)
196 {
197   snat_session_t *s;
198   snat_user_t *u;
199   ip4_header_t *ip;
200   udp_header_t *udp;
201   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
202   clib_bihash_kv_16_8_t kv;
203   snat_session_key_t eh_key;
204   nat44_is_idle_session_ctx_t ctx;
205
206   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
207     {
208       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
209       nat_elog_notice ("maximum sessions exceeded");
210       return 0;
211     }
212
213   u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
214   if (!u)
215     {
216       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
217       nat_elog_warn ("create NAT user failed");
218       return 0;
219     }
220
221   s = nat_ed_session_alloc (sm, u, thread_index, now);
222   if (!s)
223     {
224       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
225       nat44_delete_user_with_no_session (sm, u, thread_index);
226       nat_elog_warn ("create NAT session failed");
227       return 0;
228     }
229
230   ip = vlib_buffer_get_current (b);
231   udp = ip4_next_header (ip);
232
233   s->ext_host_addr.as_u32 = ip->src_address.as_u32;
234   s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
235   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
236   if (lb_nat)
237     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
238   if (lb_nat == AFFINITY_LB_NAT)
239     s->flags |= SNAT_SESSION_FLAG_AFFINITY;
240   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
241   s->out2in = e_key;
242   s->in2out = l_key;
243   s->in2out.protocol = s->out2in.protocol;
244   user_session_increment (sm, u, 1);
245
246   /* Add to lookup tables */
247   make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
248               e_key.fib_index, e_key.port, s->ext_host_port);
249   kv.value = s - tsm->sessions;
250   ctx.now = now;
251   ctx.thread_index = thread_index;
252   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
253                                                nat44_o2i_ed_is_idle_session_cb,
254                                                &ctx))
255     nat_elog_notice ("out2in-ed key add failed");
256
257   if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
258                                  ip->src_address.as_u32 == l_key.addr.as_u32))
259     {
260       eh_key.protocol = e_key.protocol;
261       if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
262                                                thread_index, &eh_key,
263                                                sm->port_per_thread,
264                                                tsm->snat_thread_index))
265         {
266           b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
267           nat44_delete_session (sm, s, thread_index);
268           if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
269             nat_elog_notice ("out2in-ed key del failed");
270           return 0;
271         }
272       s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
273       s->ext_host_nat_port = eh_key.port;
274       s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
275       make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
276                   l_key.fib_index, l_key.port, s->ext_host_nat_port);
277     }
278   else
279     {
280       make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
281                   l_key.fib_index, l_key.port, s->ext_host_port);
282     }
283   kv.value = s - tsm->sessions;
284   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
285                                                nat44_i2o_ed_is_idle_session_cb,
286                                                &ctx))
287     nat_elog_notice ("in2out-ed key add failed");
288
289   snat_ipfix_logging_nat44_ses_create (thread_index,
290                                        s->in2out.addr.as_u32,
291                                        s->out2in.addr.as_u32,
292                                        s->in2out.protocol,
293                                        s->in2out.port,
294                                        s->out2in.port, s->in2out.fib_index);
295
296   nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
297                          &s->in2out.addr, s->in2out.port,
298                          &s->ext_host_nat_addr, s->ext_host_nat_port,
299                          &s->out2in.addr, s->out2in.port,
300                          &s->ext_host_addr, s->ext_host_port,
301                          s->in2out.protocol, is_twice_nat_session (s));
302
303   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
304                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
305                &s->ext_host_nat_addr, s->ext_host_nat_port,
306                s->in2out.protocol, s->in2out.fib_index, s->flags,
307                thread_index, 0);
308
309   return s;
310 }
311
312 static int
313 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
314               u16 dst_port, u32 thread_index, u32 rx_fib_index)
315 {
316   clib_bihash_kv_16_8_t kv, value;
317   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
318
319   make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
320               rx_fib_index, src_port, dst_port);
321   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
322     return 1;
323
324   return 0;
325 }
326
327 static void
328 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
329                        u32 rx_fib_index, u32 thread_index)
330 {
331   nat_ed_ses_key_t key;
332   clib_bihash_kv_16_8_t kv, value;
333   udp_header_t *udp;
334   snat_user_t *u;
335   snat_session_t *s = 0;
336   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
337   f64 now = vlib_time_now (sm->vlib_main);
338
339   if (ip->protocol == IP_PROTOCOL_ICMP)
340     {
341       if (get_icmp_o2i_ed_key (b, ip, &key))
342         return;
343     }
344   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
345     {
346       udp = ip4_next_header (ip);
347       key.r_addr = ip->src_address;
348       key.l_addr = ip->dst_address;
349       key.proto = ip->protocol;
350       key.l_port = udp->dst_port;
351       key.r_port = udp->src_port;
352     }
353   else
354     {
355       key.r_addr = ip->src_address;
356       key.l_addr = ip->dst_address;
357       key.proto = ip->protocol;
358       key.l_port = key.r_port = 0;
359     }
360   key.fib_index = 0;
361   kv.key[0] = key.as_u64[0];
362   kv.key[1] = key.as_u64[1];
363
364   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
365     {
366       s = pool_elt_at_index (tsm->sessions, value.value);
367     }
368   else
369     {
370       u32 proto;
371
372       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
373         return;
374
375       u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
376                                   thread_index);
377       if (!u)
378         {
379           nat_elog_warn ("create NAT user failed");
380           return;
381         }
382
383       s = nat_ed_session_alloc (sm, u, thread_index, now);
384       if (!s)
385         {
386           nat44_delete_user_with_no_session (sm, u, thread_index);
387           nat_elog_warn ("create NAT session failed");
388           return;
389         }
390
391       proto = ip_proto_to_snat_proto (key.proto);
392
393       s->ext_host_addr = key.r_addr;
394       s->ext_host_port = key.r_port;
395       s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
396       s->out2in.addr = key.l_addr;
397       s->out2in.port = key.l_port;
398       s->out2in.protocol = proto;
399       if (proto == ~0)
400         {
401           s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
402           s->out2in.port = ip->protocol;
403         }
404       s->out2in.fib_index = 0;
405       s->in2out = s->out2in;
406       user_session_increment (sm, u, 0);
407
408       kv.value = s - tsm->sessions;
409       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
410         nat_elog_notice ("in2out_ed key add failed");
411     }
412
413   if (ip->protocol == IP_PROTOCOL_TCP)
414     {
415       tcp_header_t *tcp = ip4_next_header (ip);
416       if (nat44_set_tcp_session_state_o2i
417           (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
418            thread_index))
419         return;
420     }
421
422   /* Accounting */
423   nat44_session_update_counters (s, now, 0, thread_index);
424   /* Per-user LRU list maintenance */
425   nat44_session_update_lru (sm, s, thread_index);
426 }
427
428 static inline void
429 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
430                               ip4_header_t * ip, u32 rx_fib_index)
431 {
432   ip4_header_t ip_wkr = {
433     .src_address = ip->dst_address,
434   };
435   u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
436
437   create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
438 }
439
440 #ifndef CLIB_MARCH_VARIANT
441 u32
442 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
443                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
444                       u8 * p_proto, snat_session_key_t * p_value,
445                       u8 * p_dont_translate, void *d, void *e)
446 {
447   u32 next = ~0, sw_if_index, rx_fib_index;
448   nat_ed_ses_key_t key;
449   clib_bihash_kv_16_8_t kv, value;
450   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
451   snat_session_t *s = 0;
452   u8 dont_translate = 0, is_addr_only, identity_nat;
453   snat_session_key_t e_key, l_key;
454
455   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
456   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
457
458   if (get_icmp_o2i_ed_key (b, ip, &key))
459     {
460       b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
461       next = NAT_NEXT_DROP;
462       goto out;
463     }
464   key.fib_index = rx_fib_index;
465   kv.key[0] = key.as_u64[0];
466   kv.key[1] = key.as_u64[1];
467
468   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
469     {
470       /* Try to match static mapping */
471       e_key.addr = ip->dst_address;
472       e_key.port = key.l_port;
473       e_key.protocol = ip_proto_to_snat_proto (key.proto);
474       e_key.fib_index = rx_fib_index;
475       if (snat_static_mapping_match
476           (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
477         {
478           if (!sm->forwarding_enabled)
479             {
480               /* Don't NAT packet aimed at the intfc address */
481               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
482                                                     ip->dst_address.as_u32)))
483                 {
484                   dont_translate = 1;
485                   goto out;
486                 }
487               b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
488               next = NAT_NEXT_DROP;
489               goto out;
490             }
491           else
492             {
493               dont_translate = 1;
494               if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
495                                 thread_index, rx_fib_index))
496                 {
497                   next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
498                   goto out;
499                 }
500               if (sm->num_workers > 1)
501                 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
502               else
503                 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
504               goto out;
505             }
506         }
507
508       if (PREDICT_FALSE
509           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
510            ICMP4_echo_reply
511            && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
512                ICMP4_echo_request || !is_addr_only)))
513         {
514           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
515           next = NAT_NEXT_DROP;
516           goto out;
517         }
518
519       if (PREDICT_FALSE (identity_nat))
520         {
521           dont_translate = 1;
522           goto out;
523         }
524
525       /* Create session initiated by host from external network */
526       s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
527                                                 thread_index, 0, 0,
528                                                 vlib_time_now
529                                                 (sm->vlib_main));
530
531       if (!s)
532         {
533           next = NAT_NEXT_DROP;
534           goto out;
535         }
536     }
537   else
538     {
539       if (PREDICT_FALSE
540           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
541            ICMP4_echo_reply
542            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
543            ICMP4_echo_request
544            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
545                                            reass.icmp_type_or_tcp_flags)))
546         {
547           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
548           next = NAT_NEXT_DROP;
549           goto out;
550         }
551
552       s = pool_elt_at_index (tsm->sessions, value.value);
553     }
554
555   *p_proto = ip_proto_to_snat_proto (key.proto);
556 out:
557   if (s)
558     *p_value = s->in2out;
559   *p_dont_translate = dont_translate;
560   if (d)
561     *(snat_session_t **) d = s;
562   return next;
563 }
564 #endif
565
566 static snat_session_t *
567 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
568                                vlib_buffer_t * b,
569                                ip4_header_t * ip,
570                                u32 rx_fib_index,
571                                u32 thread_index,
572                                f64 now,
573                                vlib_main_t * vm, vlib_node_runtime_t * node)
574 {
575   clib_bihash_kv_8_8_t kv, value;
576   clib_bihash_kv_16_8_t s_kv, s_value;
577   snat_static_mapping_t *m;
578   u32 old_addr, new_addr;
579   ip_csum_t sum;
580   snat_session_t *s;
581   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
582   snat_user_t *u;
583
584   old_addr = ip->dst_address.as_u32;
585
586   make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
587               rx_fib_index, 0, 0);
588
589   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
590     {
591       s = pool_elt_at_index (tsm->sessions, s_value.value);
592       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
593     }
594   else
595     {
596       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
597         {
598           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
599           nat_elog_notice ("maximum sessions exceeded");
600           return 0;
601         }
602
603       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
604       if (clib_bihash_search_8_8
605           (&sm->static_mapping_by_external, &kv, &value))
606         {
607           b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
608           return 0;
609         }
610
611       m = pool_elt_at_index (sm->static_mappings, value.value);
612
613       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
614
615       u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
616                                   thread_index);
617       if (!u)
618         {
619           b->error = node->errors[NAT_OUT2IN_ED_ERROR_CANNOT_CREATE_USER];
620           nat_elog_warn ("create NAT user failed");
621           return 0;
622         }
623
624       /* Create a new session */
625       s = nat_ed_session_alloc (sm, u, thread_index, now);
626       if (!s)
627         {
628           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
629           nat44_delete_user_with_no_session (sm, u, thread_index);
630           nat_elog_warn ("create NAT session failed");
631           return 0;
632         }
633
634       s->ext_host_addr.as_u32 = ip->src_address.as_u32;
635       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
636       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
637       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
638       s->out2in.addr.as_u32 = old_addr;
639       s->out2in.fib_index = rx_fib_index;
640       s->in2out.addr.as_u32 = new_addr;
641       s->in2out.fib_index = m->fib_index;
642       s->in2out.port = s->out2in.port = ip->protocol;
643       user_session_increment (sm, u, 1);
644
645       /* Add to lookup tables */
646       s_kv.value = s - tsm->sessions;
647       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
648         nat_elog_notice ("out2in key add failed");
649
650       make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
651                   m->fib_index, 0, 0);
652       s_kv.value = s - tsm->sessions;
653       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
654         nat_elog_notice ("in2out key add failed");
655     }
656
657   /* Update IP checksum */
658   sum = ip->checksum;
659   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
660   ip->checksum = ip_csum_fold (sum);
661
662   vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
663
664   /* Accounting */
665   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
666                                  thread_index);
667   /* Per-user LRU list maintenance */
668   nat44_session_update_lru (sm, s, thread_index);
669
670   return s;
671 }
672
673 static inline uword
674 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
675                                           vlib_node_runtime_t * node,
676                                           vlib_frame_t * frame)
677 {
678   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
679   nat_next_t next_index;
680   snat_main_t *sm = &snat_main;
681   f64 now = vlib_time_now (vm);
682   u32 thread_index = vm->thread_index;
683   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
684   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
685     0, fragments = 0;
686   u32 tcp_closed_drops = 0;
687
688   stats_node_index = sm->ed_out2in_node_index;
689
690   from = vlib_frame_vector_args (frame);
691   n_left_from = frame->n_vectors;
692   next_index = node->cached_next_index;
693
694   while (n_left_from > 0)
695     {
696       u32 n_left_to_next;
697
698       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
699
700       while (n_left_from > 0 && n_left_to_next > 0)
701         {
702           u32 bi0;
703           vlib_buffer_t *b0;
704           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
705             new_addr0;
706           u16 old_port0, new_port0;
707           ip4_header_t *ip0;
708           udp_header_t *udp0;
709           tcp_header_t *tcp0;
710           snat_session_t *s0 = 0;
711           clib_bihash_kv_16_8_t kv0, value0;
712           ip_csum_t sum0;
713
714           /* speculatively enqueue b0 to the current next frame */
715           bi0 = from[0];
716           to_next[0] = bi0;
717           from += 1;
718           to_next += 1;
719           n_left_from -= 1;
720           n_left_to_next -= 1;
721
722           b0 = vlib_get_buffer (vm, bi0);
723           next0 = nat_buffer_opaque (b0)->arc_next;
724
725           vnet_buffer (b0)->snat.flags = 0;
726           ip0 = vlib_buffer_get_current (b0);
727
728           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
729           rx_fib_index0 =
730             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
731                                                  sw_if_index0);
732
733           if (PREDICT_FALSE (ip0->ttl == 1))
734             {
735               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
736               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
737                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
738                                            0);
739               next0 = NAT_NEXT_ICMP_ERROR;
740               goto trace0;
741             }
742
743           udp0 = ip4_next_header (ip0);
744           tcp0 = (tcp_header_t *) udp0;
745           proto0 = ip_proto_to_snat_proto (ip0->protocol);
746
747           if (PREDICT_FALSE (proto0 == ~0))
748             {
749               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
750               goto trace0;
751             }
752
753           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
754             {
755               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
756               goto trace0;
757             }
758
759           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
760                       ip0->protocol, rx_fib_index0,
761                       vnet_buffer (b0)->ip.reass.l4_dst_port,
762                       vnet_buffer (b0)->ip.reass.l4_src_port);
763
764           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
765             {
766               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
767               goto trace0;
768             }
769           s0 = pool_elt_at_index (tsm->sessions, value0.value);
770
771           if (s0->tcp_close_timestamp)
772             {
773               if (now >= s0->tcp_close_timestamp)
774                 {
775                   // session is closed, go slow path
776                   next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
777                 }
778               else
779                 {
780                   // session in transitory timeout, drop
781                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
782                   ++tcp_closed_drops;
783                   next0 = NAT_NEXT_DROP;
784                 }
785               goto trace0;
786             }
787
788           // drop if session expired
789           u64 sess_timeout_time;
790           sess_timeout_time = s0->last_heard +
791             (f64) nat44_session_get_timeout (sm, s0);
792           if (now >= sess_timeout_time)
793             {
794               // delete session
795               nat_free_session_data (sm, s0, thread_index, 0);
796               nat44_delete_session (sm, s0, thread_index);
797
798               b0->error = node->errors[NAT_OUT2IN_ED_ERROR_SESS_EXPIRED];
799               next0 = NAT_NEXT_DROP;
800               goto trace0;
801             }
802           //
803
804           old_addr0 = ip0->dst_address.as_u32;
805           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
806           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
807
808           sum0 = ip0->checksum;
809           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
810                                  dst_address);
811           if (PREDICT_FALSE (is_twice_nat_session (s0)))
812             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
813                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
814                                    src_address);
815           ip0->checksum = ip_csum_fold (sum0);
816
817           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
818
819           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
820             {
821               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
822                 {
823                   new_port0 = udp0->dst_port = s0->in2out.port;
824                   sum0 = tcp0->checksum;
825                   sum0 =
826                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
827                                     dst_address);
828                   sum0 =
829                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
830                                     length);
831                   if (is_twice_nat_session (s0))
832                     {
833                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
834                                              s0->ext_host_nat_addr.as_u32,
835                                              ip4_header_t, dst_address);
836                       sum0 =
837                         ip_csum_update (sum0,
838                                         vnet_buffer (b0)->ip.
839                                         reass.l4_src_port,
840                                         s0->ext_host_nat_port, ip4_header_t,
841                                         length);
842                       tcp0->src_port = s0->ext_host_nat_port;
843                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
844                     }
845                   tcp0->checksum = ip_csum_fold (sum0);
846                 }
847               tcp_packets++;
848               if (nat44_set_tcp_session_state_o2i
849                   (sm, now, s0,
850                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
851                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
852                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
853                 goto trace0;
854             }
855           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
856                    && udp0->checksum)
857             {
858               new_port0 = udp0->dst_port = s0->in2out.port;
859               sum0 = udp0->checksum;
860               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
861                                      dst_address);
862               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
863                                      length);
864               if (PREDICT_FALSE (is_twice_nat_session (s0)))
865                 {
866                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
867                                          s0->ext_host_nat_addr.as_u32,
868                                          ip4_header_t, dst_address);
869                   sum0 =
870                     ip_csum_update (sum0,
871                                     vnet_buffer (b0)->ip.reass.l4_src_port,
872                                     s0->ext_host_nat_port, ip4_header_t,
873                                     length);
874                   udp0->src_port = s0->ext_host_nat_port;
875                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
876                 }
877               udp0->checksum = ip_csum_fold (sum0);
878               udp_packets++;
879             }
880           else
881             {
882               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
883                 {
884                   new_port0 = udp0->dst_port = s0->in2out.port;
885                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
886                     {
887                       udp0->src_port = s0->ext_host_nat_port;
888                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
889                     }
890                 }
891               udp_packets++;
892             }
893
894           /* Accounting */
895           nat44_session_update_counters (s0, now,
896                                          vlib_buffer_length_in_chain (vm, b0),
897                                          thread_index);
898           /* Per-user LRU list maintenance */
899           nat44_session_update_lru (sm, s0, thread_index);
900
901         trace0:
902           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
903                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
904             {
905               nat44_ed_out2in_trace_t *t =
906                 vlib_add_trace (vm, node, b0, sizeof (*t));
907               t->sw_if_index = sw_if_index0;
908               t->next_index = next0;
909               t->is_slow_path = 0;
910
911               if (s0)
912                 t->session_index = s0 - tsm->sessions;
913               else
914                 t->session_index = ~0;
915             }
916
917           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
918           /* verify speculative enqueue, maybe switch current next frame */
919           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
920                                            to_next, n_left_to_next,
921                                            bi0, next0);
922         }
923
924       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
925     }
926
927   vlib_node_increment_counter (vm, stats_node_index,
928                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
929                                pkts_processed);
930   vlib_node_increment_counter (vm, stats_node_index,
931                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
932   vlib_node_increment_counter (vm, stats_node_index,
933                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
934   vlib_node_increment_counter (vm, stats_node_index,
935                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
936                                icmp_packets);
937   vlib_node_increment_counter (vm, stats_node_index,
938                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
939                                other_packets);
940   vlib_node_increment_counter (vm, stats_node_index,
941                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
942   return frame->n_vectors;
943 }
944
945 static inline uword
946 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
947                                           vlib_node_runtime_t * node,
948                                           vlib_frame_t * frame)
949 {
950   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
951   nat_next_t next_index;
952   snat_main_t *sm = &snat_main;
953   f64 now = vlib_time_now (vm);
954   u32 thread_index = vm->thread_index;
955   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
956   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
957     0, fragments = 0;
958
959   stats_node_index = sm->ed_out2in_slowpath_node_index;
960
961   from = vlib_frame_vector_args (frame);
962   n_left_from = frame->n_vectors;
963   next_index = node->cached_next_index;
964
965   while (n_left_from > 0)
966     {
967       u32 n_left_to_next;
968
969       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
970
971       while (n_left_from > 0 && n_left_to_next > 0)
972         {
973           u32 bi0;
974           vlib_buffer_t *b0;
975           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
976             new_addr0;
977           u16 old_port0, new_port0;
978           ip4_header_t *ip0;
979           udp_header_t *udp0;
980           tcp_header_t *tcp0;
981           icmp46_header_t *icmp0;
982           snat_session_t *s0 = 0;
983           clib_bihash_kv_16_8_t kv0, value0;
984           ip_csum_t sum0;
985           snat_session_key_t e_key0, l_key0;
986           lb_nat_type_t lb_nat0;
987           twice_nat_type_t twice_nat0;
988           u8 identity_nat0;
989
990           /* speculatively enqueue b0 to the current next frame */
991           bi0 = from[0];
992           to_next[0] = bi0;
993           from += 1;
994           to_next += 1;
995           n_left_from -= 1;
996           n_left_to_next -= 1;
997
998           b0 = vlib_get_buffer (vm, bi0);
999           next0 = nat_buffer_opaque (b0)->arc_next;
1000
1001           vnet_buffer (b0)->snat.flags = 0;
1002           ip0 = vlib_buffer_get_current (b0);
1003
1004           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1005           rx_fib_index0 =
1006             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1007                                                  sw_if_index0);
1008
1009           if (PREDICT_FALSE (ip0->ttl == 1))
1010             {
1011               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1012               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1013                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
1014                                            0);
1015               next0 = NAT_NEXT_ICMP_ERROR;
1016               goto trace0;
1017             }
1018
1019           udp0 = ip4_next_header (ip0);
1020           tcp0 = (tcp_header_t *) udp0;
1021           icmp0 = (icmp46_header_t *) udp0;
1022           proto0 = ip_proto_to_snat_proto (ip0->protocol);
1023
1024           if (PREDICT_FALSE (proto0 == ~0))
1025             {
1026               s0 =
1027                 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1028                                                thread_index, now, vm, node);
1029               if (!sm->forwarding_enabled)
1030                 {
1031                   if (!s0)
1032                     next0 = NAT_NEXT_DROP;
1033                 }
1034               other_packets++;
1035               goto trace0;
1036             }
1037
1038           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1039             {
1040               next0 = icmp_out2in_ed_slow_path
1041                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1042                  next0, now, thread_index, &s0);
1043               icmp_packets++;
1044               goto trace0;
1045             }
1046
1047           make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1048                       ip0->protocol, rx_fib_index0,
1049                       vnet_buffer (b0)->ip.reass.l4_dst_port,
1050                       vnet_buffer (b0)->ip.reass.l4_src_port);
1051
1052           s0 = NULL;
1053           if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1054             {
1055               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1056
1057               if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1058                 {
1059                   nat_free_session_data (sm, s0, thread_index, 0);
1060                   nat44_delete_session (sm, s0, thread_index);
1061                   s0 = NULL;
1062                 }
1063             }
1064
1065           if (!s0)
1066             {
1067               /* Try to match static mapping by external address and port,
1068                  destination address and port in packet */
1069               e_key0.addr = ip0->dst_address;
1070               e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1071               e_key0.protocol = proto0;
1072               e_key0.fib_index = rx_fib_index0;
1073
1074               if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1075                                              &twice_nat0, &lb_nat0,
1076                                              &ip0->src_address,
1077                                              &identity_nat0))
1078                 {
1079                   /*
1080                    * Send DHCP packets to the ipv4 stack, or we won't
1081                    * be able to use dhcp client on the outside interface
1082                    */
1083                   if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1084                                      && (vnet_buffer (b0)->ip.
1085                                          reass.l4_dst_port ==
1086                                          clib_host_to_net_u16
1087                                          (UDP_DST_PORT_dhcp_to_client))))
1088                     {
1089                       goto trace0;
1090                     }
1091
1092                   if (!sm->forwarding_enabled)
1093                     {
1094                       b0->error =
1095                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1096                       next0 = NAT_NEXT_DROP;
1097                     }
1098                   else
1099                     {
1100                       if (next_src_nat (sm, ip0, ip0->protocol,
1101                                         vnet_buffer (b0)->ip.
1102                                         reass.l4_src_port,
1103                                         vnet_buffer (b0)->ip.
1104                                         reass.l4_dst_port, thread_index,
1105                                         rx_fib_index0))
1106                         {
1107                           next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1108                           goto trace0;
1109                         }
1110                       if (sm->num_workers > 1)
1111                         create_bypass_for_fwd_worker (sm, b0, ip0,
1112                                                       rx_fib_index0);
1113                       else
1114                         create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1115                                                thread_index);
1116                     }
1117                   goto trace0;
1118                 }
1119
1120               if (PREDICT_FALSE (identity_nat0))
1121                 goto trace0;
1122
1123               if ((proto0 == SNAT_PROTOCOL_TCP)
1124                   && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1125                                          reass.icmp_type_or_tcp_flags))
1126                 {
1127                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1128                   next0 = NAT_NEXT_DROP;
1129                   goto trace0;
1130                 }
1131
1132               /* Create session initiated by host from external network */
1133               s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1134                                                          e_key0, node,
1135                                                          thread_index,
1136                                                          twice_nat0,
1137                                                          lb_nat0, now);
1138               if (!s0)
1139                 {
1140                   next0 = NAT_NEXT_DROP;
1141                   goto trace0;
1142                 }
1143             }
1144
1145           old_addr0 = ip0->dst_address.as_u32;
1146           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1147           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1148
1149           sum0 = ip0->checksum;
1150           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1151                                  dst_address);
1152           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1153             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1154                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1155                                    src_address);
1156           ip0->checksum = ip_csum_fold (sum0);
1157
1158           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1159
1160           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1161             {
1162               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1163                 {
1164                   new_port0 = udp0->dst_port = s0->in2out.port;
1165                   sum0 = tcp0->checksum;
1166                   sum0 =
1167                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1168                                     dst_address);
1169                   sum0 =
1170                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1171                                     length);
1172                   if (is_twice_nat_session (s0))
1173                     {
1174                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1175                                              s0->ext_host_nat_addr.as_u32,
1176                                              ip4_header_t, dst_address);
1177                       sum0 =
1178                         ip_csum_update (sum0,
1179                                         vnet_buffer (b0)->ip.
1180                                         reass.l4_src_port,
1181                                         s0->ext_host_nat_port, ip4_header_t,
1182                                         length);
1183                       tcp0->src_port = s0->ext_host_nat_port;
1184                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1185                     }
1186                   tcp0->checksum = ip_csum_fold (sum0);
1187                 }
1188               tcp_packets++;
1189               if (nat44_set_tcp_session_state_o2i
1190                   (sm, now, s0,
1191                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1192                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
1193                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1194                 goto trace0;
1195             }
1196           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1197                    && udp0->checksum)
1198             {
1199               new_port0 = udp0->dst_port = s0->in2out.port;
1200               sum0 = udp0->checksum;
1201               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1202                                      dst_address);
1203               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1204                                      length);
1205               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1206                 {
1207                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1208                                          s0->ext_host_nat_addr.as_u32,
1209                                          ip4_header_t, dst_address);
1210                   sum0 =
1211                     ip_csum_update (sum0,
1212                                     vnet_buffer (b0)->ip.reass.l4_src_port,
1213                                     s0->ext_host_nat_port, ip4_header_t,
1214                                     length);
1215                   udp0->src_port = s0->ext_host_nat_port;
1216                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1217                 }
1218               udp0->checksum = ip_csum_fold (sum0);
1219               udp_packets++;
1220             }
1221           else
1222             {
1223               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1224                 {
1225                   new_port0 = udp0->dst_port = s0->in2out.port;
1226                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1227                     {
1228                       udp0->src_port = s0->ext_host_nat_port;
1229                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1230                     }
1231                 }
1232               udp_packets++;
1233             }
1234
1235           /* Accounting */
1236           nat44_session_update_counters (s0, now,
1237                                          vlib_buffer_length_in_chain (vm, b0),
1238                                          thread_index);
1239           /* Per-user LRU list maintenance */
1240           nat44_session_update_lru (sm, s0, thread_index);
1241
1242         trace0:
1243           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1244                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1245             {
1246               nat44_ed_out2in_trace_t *t =
1247                 vlib_add_trace (vm, node, b0, sizeof (*t));
1248               t->sw_if_index = sw_if_index0;
1249               t->next_index = next0;
1250               t->is_slow_path = 1;
1251
1252               if (s0)
1253                 t->session_index = s0 - tsm->sessions;
1254               else
1255                 t->session_index = ~0;
1256             }
1257
1258           pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1259           /* verify speculative enqueue, maybe switch current next frame */
1260           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1261                                            to_next, n_left_to_next,
1262                                            bi0, next0);
1263         }
1264
1265       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1266     }
1267
1268   vlib_node_increment_counter (vm, stats_node_index,
1269                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1270                                pkts_processed);
1271   vlib_node_increment_counter (vm, stats_node_index,
1272                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1273   vlib_node_increment_counter (vm, stats_node_index,
1274                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1275   vlib_node_increment_counter (vm, stats_node_index,
1276                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1277                                icmp_packets);
1278   vlib_node_increment_counter (vm, stats_node_index,
1279                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1280                                other_packets);
1281   vlib_node_increment_counter (vm, stats_node_index,
1282                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1283   return frame->n_vectors;
1284 }
1285
1286 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1287                                      vlib_node_runtime_t * node,
1288                                      vlib_frame_t * frame)
1289 {
1290   return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1291 }
1292
1293 /* *INDENT-OFF* */
1294 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1295   .name = "nat44-ed-out2in",
1296   .vector_size = sizeof (u32),
1297   .sibling_of = "nat-default",
1298   .format_trace = format_nat44_ed_out2in_trace,
1299   .type = VLIB_NODE_TYPE_INTERNAL,
1300   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1301   .error_strings = nat_out2in_ed_error_strings,
1302   .runtime_data_bytes = sizeof (snat_runtime_t),
1303 };
1304 /* *INDENT-ON* */
1305
1306 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1307                                               vlib_node_runtime_t * node,
1308                                               vlib_frame_t * frame)
1309 {
1310   return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1311 }
1312
1313 /* *INDENT-OFF* */
1314 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1315   .name = "nat44-ed-out2in-slowpath",
1316   .vector_size = sizeof (u32),
1317   .sibling_of = "nat-default",
1318   .format_trace = format_nat44_ed_out2in_trace,
1319   .type = VLIB_NODE_TYPE_INTERNAL,
1320   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1321   .error_strings = nat_out2in_ed_error_strings,
1322   .runtime_data_bytes = sizeof (snat_runtime_t),
1323 };
1324 /* *INDENT-ON* */
1325
1326 static u8 *
1327 format_nat_pre_trace (u8 * s, va_list * args)
1328 {
1329   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1330   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1331   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1332   return format (s, "out2in next_index %d", t->next_index);
1333 }
1334
1335 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1336                                     vlib_node_runtime_t * node,
1337                                     vlib_frame_t * frame)
1338 {
1339   return nat_pre_node_fn_inline (vm, node, frame,
1340                                  NAT_NEXT_OUT2IN_ED_FAST_PATH);
1341 }
1342
1343 /* *INDENT-OFF* */
1344 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1345   .name = "nat-pre-out2in",
1346   .vector_size = sizeof (u32),
1347   .sibling_of = "nat-default",
1348   .format_trace = format_nat_pre_trace,
1349   .type = VLIB_NODE_TYPE_INTERNAL,
1350   .n_errors = 0,
1351  };
1352 /* *INDENT-ON* */
1353
1354 /*
1355  * fd.io coding-style-patch-verification: ON
1356  *
1357  * Local Variables:
1358  * eval: (c-set-style "gnu")
1359  * End:
1360  */