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