21ef75c820caf6d488ccc00e54f86acaf6e96704
[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
35 static char *nat_in2out_ed_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nat_in2out_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 } nat_in2out_ed_trace_t;
48
49 static u8 *
50 format_nat_in2out_ed_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   nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
55   char *tag;
56
57   tag =
58     t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
59     "NAT44_IN2OUT_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 #ifndef CLIB_MARCH_VARIANT
68 int
69 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
70 {
71   snat_main_t *sm = &snat_main;
72   nat44_is_idle_session_ctx_t *ctx = arg;
73   snat_session_t *s;
74   u64 sess_timeout_time;
75   u8 proto;
76   u16 r_port, l_port;
77   ip4_address_t *l_addr, *r_addr;
78   u32 fib_index;
79   clib_bihash_kv_16_8_t ed_kv;
80   int i;
81   snat_address_t *a;
82   snat_session_key_t key;
83   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
84                                                        ctx->thread_index);
85
86   s = pool_elt_at_index (tsm->sessions, kv->value);
87   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
88   if (ctx->now >= sess_timeout_time)
89     {
90       if (is_fwd_bypass_session (s))
91         goto delete;
92
93       l_addr = &s->out2in.addr;
94       r_addr = &s->ext_host_addr;
95       fib_index = s->out2in.fib_index;
96       if (snat_is_unk_proto_session (s))
97         {
98           proto = s->in2out.port;
99           r_port = 0;
100           l_port = 0;
101         }
102       else
103         {
104           proto = snat_proto_to_ip_proto (s->in2out.protocol);
105           l_port = s->out2in.port;
106           r_port = s->ext_host_port;
107         }
108       make_ed_kv (l_addr, r_addr, proto, fib_index, l_port, r_port, ~0ULL,
109                   &ed_kv);
110       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
111         nat_elog_warn ("out2in_ed key del failed");
112
113       if (snat_is_unk_proto_session (s))
114         goto delete;
115
116       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
117                                            s->in2out.addr.as_u32,
118                                            s->out2in.addr.as_u32,
119                                            s->in2out.protocol,
120                                            s->in2out.port,
121                                            s->out2in.port,
122                                            s->in2out.fib_index);
123
124       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
125                              &s->in2out.addr, s->in2out.port,
126                              &s->ext_host_nat_addr, s->ext_host_nat_port,
127                              &s->out2in.addr, s->out2in.port,
128                              &s->ext_host_addr, s->ext_host_port,
129                              s->in2out.protocol, is_twice_nat_session (s));
130
131       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
132                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
133                    ctx->thread_index);
134
135       if (is_twice_nat_session (s))
136         {
137           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
138             {
139               key.protocol = s->in2out.protocol;
140               key.port = s->ext_host_nat_port;
141               a = sm->twice_nat_addresses + i;
142               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
143                 {
144                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
145                                                       ctx->thread_index,
146                                                       &key);
147                   break;
148                 }
149             }
150         }
151
152       if (snat_is_session_static (s))
153         goto delete;
154
155       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
156                                           &s->out2in);
157     delete:
158       nat44_ed_delete_session (sm, s, ctx->thread_index, 1);
159       return 1;
160     }
161
162   return 0;
163 }
164 #endif
165
166 static inline u32
167 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
168                           ip4_header_t * ip0, icmp46_header_t * icmp0,
169                           u32 sw_if_index0, u32 rx_fib_index0,
170                           vlib_node_runtime_t * node, u32 next0, f64 now,
171                           u32 thread_index, snat_session_t ** p_s0)
172 {
173   next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
174                        next0, thread_index, p_s0, 0);
175   snat_session_t *s0 = *p_s0;
176   if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
177     {
178       /* Accounting */
179       nat44_session_update_counters (s0, now,
180                                      vlib_buffer_length_in_chain
181                                      (sm->vlib_main, b0), thread_index);
182       /* Per-user LRU list maintenance */
183       nat44_session_update_lru (sm, s0, thread_index);
184     }
185   return next0;
186 }
187
188 static_always_inline u16
189 snat_random_port (u16 min, u16 max)
190 {
191   snat_main_t *sm = &snat_main;
192   return min + random_u32 (&sm->random_seed) /
193     (random_u32_max () / (max - min + 1) + 1);
194 }
195
196 static int
197 nat_ed_alloc_addr_and_port (snat_main_t * sm, u32 rx_fib_index,
198                             u32 snat_proto, u32 thread_index,
199                             ip4_address_t r_addr, u16 r_port, u8 proto,
200                             u16 port_per_thread, u32 snat_thread_index,
201                             snat_session_t * s,
202                             ip4_address_t * allocated_addr,
203                             u16 * allocated_port,
204                             clib_bihash_kv_16_8_t * out2in_ed_kv)
205 {
206   int i;
207   snat_address_t *a, *ga = 0;
208   u32 portnum;
209   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
210
211   const u16 port_thread_offset = (port_per_thread * snat_thread_index) + 1024;
212
213   for (i = 0; i < vec_len (sm->addresses); i++)
214     {
215       a = sm->addresses + i;
216       switch (snat_proto)
217         {
218 #define _(N, j, n, unused)                                                    \
219   case SNAT_PROTOCOL_##N:                                                     \
220     if (a->fib_index == rx_fib_index)                                         \
221       {                                                                       \
222         u16 port = snat_random_port (1, port_per_thread);                     \
223         u16 attempts = port_per_thread;                                       \
224         while (attempts > 0)                                                  \
225           {                                                                   \
226             --attempts;                                                       \
227             portnum = port_thread_offset + port;                              \
228             make_ed_kv (&a->addr, &r_addr, proto, s->out2in.fib_index,        \
229                         clib_host_to_net_u16 (portnum), r_port,               \
230                         s - tsm->sessions, out2in_ed_kv);                     \
231             int rv = clib_bihash_add_del_16_8 (&tsm->out2in_ed, out2in_ed_kv, \
232                                                2 /* is_add */);               \
233             if (0 == rv)                                                      \
234               {                                                               \
235                 ++a->busy_##n##_port_refcounts[portnum];                      \
236                 a->busy_##n##_ports_per_thread[thread_index]++;               \
237                 a->busy_##n##_ports++;                                        \
238                 *allocated_addr = a->addr;                                    \
239                 *allocated_port = clib_host_to_net_u16 (portnum);             \
240                 return 0;                                                     \
241               }                                                               \
242             port = (port + 1) % port_per_thread;                              \
243           }                                                                   \
244       }                                                                       \
245     else if (a->fib_index == ~0)                                              \
246       {                                                                       \
247         ga = a;                                                               \
248       }                                                                       \
249     break;
250
251           foreach_snat_protocol;
252         default:
253           nat_elog_info ("unknown protocol");
254           return 1;
255         }
256     }
257
258   if (ga)
259     {
260       /* fake fib_index to reuse macro */
261       rx_fib_index = ~0;
262       a = ga;
263       switch (snat_proto)
264         {
265           foreach_snat_protocol;
266         default:
267           nat_elog_info ("unknown protocol");
268           return 1;
269         }
270     }
271
272 #undef _
273
274   /* Totally out of translations to use... */
275   snat_ipfix_logging_addresses_exhausted (thread_index, 0);
276   return 1;
277 }
278
279 static u32
280 slow_path_ed (snat_main_t * sm,
281               vlib_buffer_t * b,
282               ip4_address_t l_addr,
283               ip4_address_t r_addr,
284               u16 l_port,
285               u16 r_port,
286               u8 proto,
287               u32 rx_fib_index,
288               snat_session_t ** sessionp,
289               vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
290 {
291   snat_session_t *s = NULL;
292   snat_session_key_t key0, key1;
293   lb_nat_type_t lb = 0;
294   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
295   u32 snat_proto = ip_proto_to_snat_proto (proto);
296   nat_outside_fib_t *outside_fib;
297   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
298   clib_bihash_kv_16_8_t out2in_ed_kv;
299   ip4_address_t allocated_addr;
300   u16 allocated_port;
301   u8 identity_nat;
302   fib_prefix_t pfx = {
303     .fp_proto = FIB_PROTOCOL_IP4,
304     .fp_len = 32,
305     .fp_addr = {.ip4.as_u32 = r_addr.as_u32,},
306   };
307   nat44_is_idle_session_ctx_t ctx;
308
309   if (PREDICT_TRUE (snat_proto == SNAT_PROTOCOL_TCP))
310     {
311       if (PREDICT_FALSE
312           (!tcp_flags_is_init
313            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
314         {
315           b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
316           return NAT_NEXT_DROP;
317         }
318     }
319
320   // TODO: based on fib index do a lookup
321   if (PREDICT_FALSE
322       (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
323     {
324       if (!nat_global_lru_free_one (sm, thread_index, now))
325         {
326           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
327           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
328           nat_elog_notice ("maximum sessions exceeded");
329           return NAT_NEXT_DROP;
330         }
331     }
332
333   key0.addr = l_addr;
334   key0.port = l_port;
335   key1.protocol = key0.protocol = snat_proto;
336   key0.fib_index = rx_fib_index;
337   key1.fib_index = sm->outside_fib_index;
338
339   /* First try to match static mapping by local address and port */
340   if (snat_static_mapping_match
341       (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
342     {
343       s = nat_ed_session_alloc (sm, thread_index, now);
344       if (!s)
345         {
346           nat_elog_warn ("create NAT session failed");
347           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
348           return NAT_NEXT_DROP;
349         }
350       switch (vec_len (sm->outside_fibs))
351         {
352         case 0:
353           s->out2in.fib_index = sm->outside_fib_index;
354           break;
355         case 1:
356           s->out2in.fib_index = sm->outside_fibs[0].fib_index;
357           break;
358         default:
359           /* *INDENT-OFF* */
360           vec_foreach (outside_fib, sm->outside_fibs)
361           {
362             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
363             if (FIB_NODE_INDEX_INVALID != fei)
364               {
365                 if (fib_entry_get_resolving_interface (fei) != ~0)
366                   {
367                     s->out2in.fib_index = outside_fib->fib_index;
368                     break;
369                   }
370               }
371           }
372           /* *INDENT-ON* */
373           break;
374         }
375
376       /* Try to create dynamic translation */
377       if (nat_ed_alloc_addr_and_port (sm, rx_fib_index, snat_proto,
378                                       thread_index, r_addr, r_port, proto,
379                                       sm->port_per_thread,
380                                       tsm->snat_thread_index, s,
381                                       &allocated_addr,
382                                       &allocated_port, &out2in_ed_kv))
383         {
384           nat_elog_notice ("addresses exhausted");
385           b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
386           nat_free_session_data (sm, s, thread_index, 0);
387           nat44_ed_delete_session (sm, s, thread_index, 1);
388           return NAT_NEXT_DROP;
389         }
390       key1.addr = allocated_addr;
391       key1.port = allocated_port;
392     }
393   else
394     {
395       if (PREDICT_FALSE (identity_nat))
396         {
397           *sessionp = s;
398           return next;
399         }
400       s = nat_ed_session_alloc (sm, thread_index, now);
401       if (!s)
402         {
403           nat_elog_warn ("create NAT session failed");
404           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
405           return NAT_NEXT_DROP;
406         }
407       switch (vec_len (sm->outside_fibs))
408         {
409         case 0:
410           s->out2in.fib_index = sm->outside_fib_index;
411           break;
412         case 1:
413           s->out2in.fib_index = sm->outside_fibs[0].fib_index;
414           break;
415         default:
416           /* *INDENT-OFF* */
417           vec_foreach (outside_fib, sm->outside_fibs)
418           {
419             fei = fib_table_lookup (outside_fib->fib_index, &pfx);
420             if (FIB_NODE_INDEX_INVALID != fei)
421               {
422                 if (fib_entry_get_resolving_interface (fei) != ~0)
423                   {
424                     s->out2in.fib_index = outside_fib->fib_index;
425                     break;
426                   }
427               }
428           }
429           /* *INDENT-ON* */
430           break;
431         }
432
433       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
434
435
436       make_ed_kv (&key1.addr, &r_addr, proto,
437                   s->out2in.fib_index, key1.port, r_port, s - tsm->sessions,
438                   &out2in_ed_kv);
439       if (clib_bihash_add_or_overwrite_stale_16_8
440           (&tsm->out2in_ed, &out2in_ed_kv, nat44_o2i_ed_is_idle_session_cb,
441            &ctx))
442         nat_elog_notice ("out2in-ed key add failed");
443     }
444
445   if (lb)
446     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
447   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
448   s->ext_host_addr = r_addr;
449   s->ext_host_port = r_port;
450   s->in2out = key0;
451   s->out2in = key1;
452   s->out2in.protocol = key0.protocol;
453
454   clib_bihash_kv_16_8_t in2out_ed_kv;
455   make_ed_kv (&l_addr, &r_addr, proto, rx_fib_index, l_port, r_port,
456               s - tsm->sessions, &in2out_ed_kv);
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->in2out.protocol,
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,
479                          s->in2out.protocol, 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->in2out.protocol, s->in2out.fib_index, s->flags,
485                thread_index, 0);
486
487   return next;
488 }
489
490 static_always_inline int
491 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
492                         u32 sw_if_index, ip4_header_t * ip, u32 proto,
493                         u32 rx_fib_index, u32 thread_index)
494 {
495   udp_header_t *udp = ip4_next_header (ip);
496   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
497   clib_bihash_kv_16_8_t kv, value;
498   snat_session_key_t key0, key1;
499
500   make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
501               sm->outside_fib_index, udp->dst_port, udp->src_port, ~0ULL,
502               &kv);
503
504   /* NAT packet aimed at external address if has active sessions */
505   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
506     {
507       key0.addr = ip->dst_address;
508       key0.port = udp->dst_port;
509       key0.protocol = proto;
510       key0.fib_index = sm->outside_fib_index;
511       /* or is static mappings */
512       if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
513         return 0;
514     }
515   else
516     return 0;
517
518   if (sm->forwarding_enabled)
519     return 1;
520
521   return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
522                                   rx_fib_index);
523 }
524
525 static_always_inline int
526 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
527                                       u32 thread_index, f64 now,
528                                       vlib_main_t * vm, vlib_buffer_t * b)
529 {
530   clib_bihash_kv_16_8_t kv, value;
531   snat_session_t *s = 0;
532   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
533
534   if (!sm->forwarding_enabled)
535     return 0;
536
537   if (ip->protocol == IP_PROTOCOL_ICMP)
538     {
539       if (get_icmp_i2o_ed_key (b, ip, 0, ~0ULL, 0, 0, 0, &kv))
540         return 0;
541     }
542   else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
543     {
544       make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol, 0,
545                   vnet_buffer (b)->ip.reass.l4_src_port,
546                   vnet_buffer (b)->ip.reass.l4_dst_port, ~0ULL, &kv);
547     }
548   else
549     {
550       make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
551                   0, ~0ULL, &kv);
552     }
553
554   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
555     {
556       s = pool_elt_at_index (tsm->sessions, value.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   make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
595               tx_fib_index, src_port, dst_port, ~0ULL, &kv);
596   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
597     {
598       s = pool_elt_at_index (tsm->sessions, value.value);
599       if (nat44_is_ses_closed (s))
600         {
601           nat_free_session_data (sm, s, thread_index, 0);
602           nat44_ed_delete_session (sm, s, thread_index, 1);
603         }
604       else
605         s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
606       return 1;
607     }
608
609   /* dst NAT check */
610   make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
611               rx_fib_index, dst_port, src_port, ~0ULL, &kv);
612   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
613     {
614       s = pool_elt_at_index (tsm->sessions, value.value);
615       if (is_fwd_bypass_session (s))
616         return 0;
617
618       /* hairpinning */
619       /* *INDENT-OFF* */
620       pool_foreach (i, sm->output_feature_interfaces,
621       ({
622         if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
623            return 0;
624       }));
625       /* *INDENT-ON* */
626       return 1;
627     }
628
629   return 0;
630 }
631
632 #ifndef CLIB_MARCH_VARIANT
633 u32
634 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
635                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
636                       u8 * p_proto, snat_session_key_t * p_value,
637                       u8 * p_dont_translate, void *d, void *e)
638 {
639   u32 sw_if_index;
640   u32 rx_fib_index;
641   snat_session_t *s = 0;
642   u8 dont_translate = 0;
643   clib_bihash_kv_16_8_t kv, value;
644   u32 next = ~0;
645   int err;
646   u16 l_port = 0, r_port = 0;   // initialize to workaround gcc warning
647   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
648
649   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
650   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
651
652   err =
653     get_icmp_i2o_ed_key (b, ip, rx_fib_index, ~0ULL, p_proto, &l_port,
654                          &r_port, &kv);
655   if (err != 0)
656     {
657       b->error = node->errors[err];
658       next = NAT_NEXT_DROP;
659       goto out;
660     }
661
662   if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
663     {
664       if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
665         {
666           if (PREDICT_FALSE
667               (nat44_ed_not_translate_output_feature
668                (sm, ip, l_port, r_port, thread_index,
669                 sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_TX])))
670             {
671               dont_translate = 1;
672               goto out;
673             }
674         }
675       else
676         {
677           if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
678                                                      ip, SNAT_PROTOCOL_ICMP,
679                                                      rx_fib_index,
680                                                      thread_index)))
681             {
682               dont_translate = 1;
683               goto out;
684             }
685         }
686
687       if (PREDICT_FALSE
688           (icmp_type_is_error_message
689            (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
690         {
691           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
692           next = NAT_NEXT_DROP;
693           goto out;
694         }
695
696       next =
697         slow_path_ed (sm, b, ip->src_address, ip->dst_address, l_port, r_port,
698                       ip->protocol, rx_fib_index, &s, node, next,
699                       thread_index, vlib_time_now (sm->vlib_main));
700
701       if (PREDICT_FALSE (next == NAT_NEXT_DROP))
702         goto out;
703
704       if (!s)
705         {
706           dont_translate = 1;
707           goto out;
708         }
709     }
710   else
711     {
712       if (PREDICT_FALSE
713           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
714            ICMP4_echo_request
715            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
716            ICMP4_echo_reply
717            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
718                                            reass.icmp_type_or_tcp_flags)))
719         {
720           b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
721           next = NAT_NEXT_DROP;
722           goto out;
723         }
724
725       s = pool_elt_at_index (tsm->sessions, value.value);
726     }
727 out:
728   if (s)
729     *p_value = s->out2in;
730   *p_dont_translate = dont_translate;
731   if (d)
732     *(snat_session_t **) d = s;
733   return next;
734 }
735 #endif
736
737 static snat_session_t *
738 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
739                                vlib_buffer_t * b,
740                                ip4_header_t * ip,
741                                u32 rx_fib_index,
742                                u32 thread_index,
743                                f64 now,
744                                vlib_main_t * vm, vlib_node_runtime_t * node)
745 {
746   clib_bihash_kv_8_8_t kv, value;
747   clib_bihash_kv_16_8_t s_kv, s_value;
748   snat_static_mapping_t *m;
749   u32 old_addr, new_addr = 0;
750   ip_csum_t sum;
751   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
752   snat_session_t *s;
753   u32 outside_fib_index = sm->outside_fib_index;
754   int i;
755   u8 is_sm = 0;
756   nat_outside_fib_t *outside_fib;
757   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
758   fib_prefix_t pfx = {
759     .fp_proto = FIB_PROTOCOL_IP4,
760     .fp_len = 32,
761     .fp_addr = {
762                 .ip4.as_u32 = ip->dst_address.as_u32,
763                 },
764   };
765
766   switch (vec_len (sm->outside_fibs))
767     {
768     case 0:
769       outside_fib_index = sm->outside_fib_index;
770       break;
771     case 1:
772       outside_fib_index = sm->outside_fibs[0].fib_index;
773       break;
774     default:
775       /* *INDENT-OFF* */
776       vec_foreach (outside_fib, sm->outside_fibs)
777         {
778           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
779           if (FIB_NODE_INDEX_INVALID != fei)
780             {
781               if (fib_entry_get_resolving_interface (fei) != ~0)
782                 {
783                   outside_fib_index = outside_fib->fib_index;
784                   break;
785                 }
786             }
787         }
788       /* *INDENT-ON* */
789       break;
790     }
791   old_addr = ip->src_address.as_u32;
792
793   make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
794               rx_fib_index, 0, 0, ~0ULL, &s_kv);
795
796   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
797     {
798       s = pool_elt_at_index (tsm->sessions, s_value.value);
799       new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
800     }
801   else
802     {
803       if (PREDICT_FALSE
804           (nat44_ed_maximum_sessions_exceeded
805            (sm, rx_fib_index, thread_index)))
806         {
807           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
808           nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
809           nat_elog_notice ("maximum sessions exceeded");
810           return 0;
811         }
812
813       make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
814
815       /* Try to find static mapping first */
816       if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
817         {
818           m = pool_elt_at_index (sm->static_mappings, value.value);
819           new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
820           is_sm = 1;
821           goto create_ses;
822         }
823       else
824         {
825           /* *INDENT-OFF* */
826           pool_foreach (s, tsm->sessions, {
827             if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
828               {
829                 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
830
831                 make_ed_kv (&s->out2in.addr, &ip->dst_address, ip->protocol,
832                             outside_fib_index, 0, 0, ~0ULL, &s_kv);
833                 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
834                   goto create_ses;
835
836                 break;
837               }
838           });
839           /* *INDENT-ON* */
840
841           for (i = 0; i < vec_len (sm->addresses); i++)
842             {
843               make_ed_kv (&sm->addresses[i].addr, &ip->dst_address,
844                           ip->protocol, outside_fib_index, 0, 0, ~0ULL,
845                           &s_kv);
846               if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
847                 {
848                   new_addr = ip->src_address.as_u32 =
849                     sm->addresses[i].addr.as_u32;
850                   goto create_ses;
851                 }
852             }
853           return 0;
854         }
855
856     create_ses:
857       s = nat_ed_session_alloc (sm, thread_index, now);
858       if (!s)
859         {
860           b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_USER_SESS_EXCEEDED];
861           nat_elog_warn ("create NAT session failed");
862           return 0;
863         }
864
865       s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
866       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
867       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
868       s->out2in.addr.as_u32 = new_addr;
869       s->out2in.fib_index = outside_fib_index;
870       s->in2out.addr.as_u32 = old_addr;
871       s->in2out.fib_index = rx_fib_index;
872       s->in2out.port = s->out2in.port = ip->protocol;
873       if (is_sm)
874         s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
875
876       /* Add to lookup tables */
877       make_ed_kv (&s->in2out.addr, &ip->dst_address, ip->protocol,
878                   rx_fib_index, 0, 0, s - tsm->sessions, &s_kv);
879       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
880         nat_elog_notice ("in2out key add failed");
881
882       make_ed_kv (&s->out2in.addr, &ip->dst_address, ip->protocol,
883                   outside_fib_index, 0, 0, s - tsm->sessions, &s_kv);
884       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
885         nat_elog_notice ("out2in key add failed");
886     }
887
888   /* Update IP checksum */
889   sum = ip->checksum;
890   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
891   ip->checksum = ip_csum_fold (sum);
892
893   /* Accounting */
894   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
895                                  thread_index);
896   /* Per-user LRU list maintenance */
897   nat44_session_update_lru (sm, s, thread_index);
898
899   /* Hairpinning */
900   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
901     nat44_ed_hairpinning_unknown_proto (sm, b, ip);
902
903   if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
904     vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
905
906   return s;
907 }
908
909 static inline uword
910 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
911                                           vlib_node_runtime_t * node,
912                                           vlib_frame_t * frame,
913                                           int is_output_feature)
914 {
915   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
916   nat_next_t next_index;
917   snat_main_t *sm = &snat_main;
918   f64 now = vlib_time_now (vm);
919   u32 thread_index = vm->thread_index;
920   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
921   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
922     0, def_slow;
923
924   def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH :
925     NAT_NEXT_IN2OUT_ED_SLOW_PATH;
926
927   stats_node_index = sm->ed_in2out_node_index;
928
929   from = vlib_frame_vector_args (frame);
930   n_left_from = frame->n_vectors;
931   next_index = node->cached_next_index;
932
933   while (n_left_from > 0)
934     {
935       u32 n_left_to_next;
936
937       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
938
939       while (n_left_from > 0 && n_left_to_next > 0)
940         {
941           u32 bi0;
942           vlib_buffer_t *b0;
943           u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
944             new_addr0, old_addr0;
945           u16 old_port0, new_port0;
946           ip4_header_t *ip0;
947           udp_header_t *udp0;
948           tcp_header_t *tcp0;
949           snat_session_t *s0 = 0;
950           clib_bihash_kv_16_8_t kv0, value0;
951           ip_csum_t sum0;
952
953           /* speculatively enqueue b0 to the current next frame */
954           bi0 = from[0];
955           to_next[0] = bi0;
956           from += 1;
957           to_next += 1;
958           n_left_from -= 1;
959           n_left_to_next -= 1;
960
961           b0 = vlib_get_buffer (vm, bi0);
962
963           if (is_output_feature)
964             {
965               vnet_feature_next (&vnet_buffer2 (b0)->nat.arc_next, b0);
966               iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
967             }
968
969           next0 = vnet_buffer2 (b0)->nat.arc_next;
970
971           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
972                                   iph_offset0);
973
974           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
975           rx_fib_index0 =
976             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
977                                                  sw_if_index0);
978
979           if (PREDICT_FALSE (ip0->ttl == 1))
980             {
981               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
982               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
983                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
984                                            0);
985               next0 = NAT_NEXT_ICMP_ERROR;
986               goto trace0;
987             }
988
989           udp0 = ip4_next_header (ip0);
990           tcp0 = (tcp_header_t *) udp0;
991           proto0 = ip_proto_to_snat_proto (ip0->protocol);
992
993           if (PREDICT_FALSE (proto0 == ~0))
994             {
995               next0 = def_slow;
996               goto trace0;
997             }
998
999           if (is_output_feature)
1000             {
1001               if (PREDICT_FALSE (nat_not_translate_output_feature_fwd
1002                                  (sm, ip0, thread_index, now, vm, b0)))
1003                 goto trace0;
1004             }
1005
1006           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1007             {
1008               next0 = def_slow;
1009               goto trace0;
1010             }
1011
1012           make_ed_kv (&ip0->src_address, &ip0->dst_address,
1013                       ip0->protocol, rx_fib_index0,
1014                       vnet_buffer (b0)->ip.reass.l4_src_port,
1015                       vnet_buffer (b0)->ip.reass.l4_dst_port, ~0ULL, &kv0);
1016
1017           // lookup for session
1018           if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1019             {
1020               // session does not exist go slow path
1021               next0 = def_slow;
1022               goto trace0;
1023             }
1024           s0 = pool_elt_at_index (tsm->sessions, value0.value);
1025
1026           if (s0->tcp_close_timestamp)
1027             {
1028               if (now >= s0->tcp_close_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               nat44_ed_delete_session (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 == SNAT_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, 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_snat_proto (ip0->protocol);
1274
1275           if (PREDICT_FALSE (proto0 == ~0))
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 == SNAT_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           // move down
1298           make_ed_kv (&ip0->src_address, &ip0->dst_address,
1299                       ip0->protocol, rx_fib_index0,
1300                       vnet_buffer (b0)->ip.reass.l4_src_port,
1301                       vnet_buffer (b0)->ip.reass.l4_dst_port, ~0ULL, &kv0);
1302
1303           if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1304             {
1305               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1306
1307               if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1308                 {
1309                   nat_free_session_data (sm, s0, thread_index, 0);
1310                   nat44_ed_delete_session (sm, s0, thread_index, 1);
1311                   s0 = NULL;
1312                 }
1313             }
1314
1315           if (!s0)
1316             {
1317               if (is_output_feature)
1318                 {
1319                   if (PREDICT_FALSE
1320                       (nat44_ed_not_translate_output_feature
1321                        (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1322                         vnet_buffer (b0)->ip.reass.l4_dst_port, thread_index,
1323                         sw_if_index0,
1324                         vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1325                     goto trace0;
1326
1327                   /*
1328                    * Send DHCP packets to the ipv4 stack, or we won't
1329                    * be able to use dhcp client on the outside interface
1330                    */
1331                   if (PREDICT_FALSE
1332                       (proto0 == SNAT_PROTOCOL_UDP
1333                        && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1334                            clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1335                        && ip0->dst_address.as_u32 == 0xffffffff))
1336                     goto trace0;
1337                 }
1338               else
1339                 {
1340                   if (PREDICT_FALSE
1341                       (nat44_ed_not_translate
1342                        (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1343                         thread_index)))
1344                     goto trace0;
1345                 }
1346
1347               next0 =
1348                 slow_path_ed (sm, b0, ip0->src_address, ip0->dst_address,
1349                               vnet_buffer (b0)->ip.reass.l4_src_port,
1350                               vnet_buffer (b0)->ip.reass.l4_dst_port,
1351                               ip0->protocol, rx_fib_index0, &s0, node, next0,
1352                               thread_index, now);
1353
1354               if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1355                 goto trace0;
1356
1357               if (PREDICT_FALSE (!s0))
1358                 goto trace0;
1359
1360             }
1361
1362           b0->flags |= VNET_BUFFER_F_IS_NATED;
1363
1364           if (!is_output_feature)
1365             vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1366
1367           old_addr0 = ip0->src_address.as_u32;
1368           new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1369           sum0 = ip0->checksum;
1370           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1371                                  src_address);
1372           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1373             sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1374                                    s0->ext_host_addr.as_u32, ip4_header_t,
1375                                    dst_address);
1376           ip0->checksum = ip_csum_fold (sum0);
1377
1378           old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1379
1380           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1381             {
1382               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1383                 {
1384                   new_port0 = udp0->src_port = s0->out2in.port;
1385                   sum0 = tcp0->checksum;
1386                   sum0 =
1387                     ip_csum_update (sum0, old_addr0, new_addr0,
1388                                     ip4_header_t, dst_address);
1389                   sum0 =
1390                     ip_csum_update (sum0, old_port0, new_port0,
1391                                     ip4_header_t, length);
1392                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1393                     {
1394                       sum0 =
1395                         ip_csum_update (sum0, ip0->dst_address.as_u32,
1396                                         s0->ext_host_addr.as_u32,
1397                                         ip4_header_t, dst_address);
1398                       sum0 =
1399                         ip_csum_update (sum0,
1400                                         vnet_buffer (b0)->ip.
1401                                         reass.l4_dst_port, s0->ext_host_port,
1402                                         ip4_header_t, length);
1403                       tcp0->dst_port = s0->ext_host_port;
1404                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1405                     }
1406                   mss_clamping (sm, tcp0, &sum0);
1407                   tcp0->checksum = ip_csum_fold (sum0);
1408                 }
1409               tcp_packets++;
1410               if (nat44_set_tcp_session_state_i2o
1411                   (sm, now, s0, b0, thread_index))
1412                 goto trace0;
1413             }
1414           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1415                    && udp0->checksum)
1416             {
1417               new_port0 = udp0->src_port = s0->out2in.port;
1418               sum0 = udp0->checksum;
1419               sum0 =
1420                 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1421                                 dst_address);
1422               sum0 =
1423                 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1424                                 length);
1425               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1426                 {
1427                   sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1428                                          s0->ext_host_addr.as_u32,
1429                                          ip4_header_t, dst_address);
1430                   sum0 =
1431                     ip_csum_update (sum0,
1432                                     vnet_buffer (b0)->ip.reass.l4_dst_port,
1433                                     s0->ext_host_port, ip4_header_t, length);
1434                   udp0->dst_port = s0->ext_host_port;
1435                   ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1436                 }
1437               udp0->checksum = ip_csum_fold (sum0);
1438               udp_packets++;
1439             }
1440           else
1441             {
1442               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1443                 {
1444                   new_port0 = udp0->src_port = s0->out2in.port;
1445                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1446                     {
1447                       udp0->dst_port = s0->ext_host_port;
1448                       ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1449                     }
1450                   udp_packets++;
1451                 }
1452             }
1453
1454           /* Accounting */
1455           nat44_session_update_counters (s0, now,
1456                                          vlib_buffer_length_in_chain
1457                                          (vm, b0), thread_index);
1458           /* Per-user LRU list maintenance */
1459           nat44_session_update_lru (sm, s0, thread_index);
1460
1461         trace0:
1462           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1463                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1464             {
1465               nat_in2out_ed_trace_t *t =
1466                 vlib_add_trace (vm, node, b0, sizeof (*t));
1467               t->sw_if_index = sw_if_index0;
1468               t->next_index = next0;
1469               t->is_slow_path = 1;
1470
1471               if (s0)
1472                 t->session_index = s0 - tsm->sessions;
1473               else
1474                 t->session_index = ~0;
1475             }
1476
1477           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1478
1479           /* verify speculative enqueue, maybe switch current next frame */
1480           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1481                                            to_next, n_left_to_next,
1482                                            bi0, next0);
1483         }
1484
1485       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1486     }
1487
1488   vlib_node_increment_counter (vm, stats_node_index,
1489                                NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1490                                pkts_processed);
1491   vlib_node_increment_counter (vm, stats_node_index,
1492                                NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1493   vlib_node_increment_counter (vm, stats_node_index,
1494                                NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1495   vlib_node_increment_counter (vm, stats_node_index,
1496                                NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1497                                icmp_packets);
1498   vlib_node_increment_counter (vm, stats_node_index,
1499                                NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1500                                other_packets);
1501   return frame->n_vectors;
1502 }
1503
1504 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1505                                      vlib_node_runtime_t * node,
1506                                      vlib_frame_t * frame)
1507 {
1508   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1509 }
1510
1511 /* *INDENT-OFF* */
1512 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1513   .name = "nat44-ed-in2out",
1514   .vector_size = sizeof (u32),
1515   .sibling_of = "nat-default",
1516   .format_trace = format_nat_in2out_ed_trace,
1517   .type = VLIB_NODE_TYPE_INTERNAL,
1518   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1519   .error_strings = nat_in2out_ed_error_strings,
1520   .runtime_data_bytes = sizeof (snat_runtime_t),
1521 };
1522 /* *INDENT-ON* */
1523
1524 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1525                                             vlib_node_runtime_t * node,
1526                                             vlib_frame_t * frame)
1527 {
1528   return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1529 }
1530
1531 /* *INDENT-OFF* */
1532 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1533   .name = "nat44-ed-in2out-output",
1534   .vector_size = sizeof (u32),
1535   .sibling_of = "nat-default",
1536   .format_trace = format_nat_in2out_ed_trace,
1537   .type = VLIB_NODE_TYPE_INTERNAL,
1538   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1539   .error_strings = nat_in2out_ed_error_strings,
1540   .runtime_data_bytes = sizeof (snat_runtime_t),
1541 };
1542 /* *INDENT-ON* */
1543
1544 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1545                                               vlib_node_runtime_t *
1546                                               node, vlib_frame_t * frame)
1547 {
1548   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1549 }
1550
1551 /* *INDENT-OFF* */
1552 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1553   .name = "nat44-ed-in2out-slowpath",
1554   .vector_size = sizeof (u32),
1555   .sibling_of = "nat-default",
1556   .format_trace = format_nat_in2out_ed_trace,
1557   .type = VLIB_NODE_TYPE_INTERNAL,
1558   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1559   .error_strings = nat_in2out_ed_error_strings,
1560   .runtime_data_bytes = sizeof (snat_runtime_t),
1561 };
1562 /* *INDENT-ON* */
1563
1564 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1565                                                      vlib_node_runtime_t
1566                                                      * node,
1567                                                      vlib_frame_t * frame)
1568 {
1569   return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1570 }
1571
1572 /* *INDENT-OFF* */
1573 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1574   .name = "nat44-ed-in2out-output-slowpath",
1575   .vector_size = sizeof (u32),
1576   .sibling_of = "nat-default",
1577   .format_trace = format_nat_in2out_ed_trace,
1578   .type = VLIB_NODE_TYPE_INTERNAL,
1579   .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1580   .error_strings = nat_in2out_ed_error_strings,
1581   .runtime_data_bytes = sizeof (snat_runtime_t),
1582 };
1583 /* *INDENT-ON* */
1584
1585 static u8 *
1586 format_nat_pre_trace (u8 * s, va_list * args)
1587 {
1588   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1589   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1590   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1591   return format (s, "in2out next_index %d", t->next_index);
1592 }
1593
1594 VLIB_NODE_FN (nat_pre_in2out_node)
1595   (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1596 {
1597   return nat_pre_node_fn_inline (vm, node, frame,
1598                                  NAT_NEXT_IN2OUT_ED_FAST_PATH);
1599 }
1600
1601 /* *INDENT-OFF* */
1602 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1603   .name = "nat-pre-in2out",
1604   .vector_size = sizeof (u32),
1605   .sibling_of = "nat-default",
1606   .format_trace = format_nat_pre_trace,
1607   .type = VLIB_NODE_TYPE_INTERNAL,
1608   .n_errors = 0,
1609 };
1610 /* *INDENT-ON* */
1611
1612 /*
1613  * fd.io coding-style-patch-verification: ON
1614  *
1615  * Local Variables:
1616  * eval: (c-set-style "gnu")
1617  * End:
1618  */