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